beta-0.89.2
[luatex.git] / source / libs / cairo / cairo-src / src / cairo-debug.c
blob33d46aa3f7956ba346cbb84925bc66370b91fa3a
1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2005 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Red Hat, Inc.
32 * Contributor(s):
33 * Carl D. Worth <cworth@cworth.org>
36 #include "cairoint.h"
37 #include "cairo-image-surface-private.h"
39 /**
40 * cairo_debug_reset_static_data:
42 * Resets all static data within cairo to its original state,
43 * (ie. identical to the state at the time of program invocation). For
44 * example, all caches within cairo will be flushed empty.
46 * This function is intended to be useful when using memory-checking
47 * tools such as valgrind. When valgrind's memcheck analyzes a
48 * cairo-using program without a call to cairo_debug_reset_static_data(),
49 * it will report all data reachable via cairo's static objects as
50 * "still reachable". Calling cairo_debug_reset_static_data() just prior
51 * to program termination will make it easier to get squeaky clean
52 * reports from valgrind.
54 * WARNING: It is only safe to call this function when there are no
55 * active cairo objects remaining, (ie. the appropriate destroy
56 * functions have been called as necessary). If there are active cairo
57 * objects, this call is likely to cause a crash, (eg. an assertion
58 * failure due to a hash table being destroyed when non-empty).
60 * Since: 1.0
61 **/
62 void
63 cairo_debug_reset_static_data (void)
65 CAIRO_MUTEX_INITIALIZE ();
67 _cairo_scaled_font_map_destroy ();
69 _cairo_toy_font_face_reset_static_data ();
71 #if CAIRO_HAS_FT_FONT
72 _cairo_ft_font_reset_static_data ();
73 #endif
75 #if CAIRO_HAS_WIN32_FONT
76 _cairo_win32_font_reset_static_data ();
77 #endif
79 _cairo_intern_string_reset_static_data ();
81 _cairo_scaled_font_reset_static_data ();
83 _cairo_pattern_reset_static_data ();
85 _cairo_clip_reset_static_data ();
87 _cairo_image_reset_static_data ();
89 #if CAIRO_HAS_DRM_SURFACE
90 _cairo_drm_device_reset_static_data ();
91 #endif
93 _cairo_default_context_reset_static_data ();
95 #if CAIRO_HAS_COGL_SURFACE
96 _cairo_cogl_context_reset_static_data ();
97 #endif
99 CAIRO_MUTEX_FINALIZE ();
102 #if HAVE_VALGRIND
103 void
104 _cairo_debug_check_image_surface_is_defined (const cairo_surface_t *surface)
106 const cairo_image_surface_t *image = (cairo_image_surface_t *) surface;
107 const uint8_t *bits;
108 int row, width;
110 if (surface == NULL)
111 return;
113 if (! RUNNING_ON_VALGRIND)
114 return;
116 bits = image->data;
117 switch (image->format) {
118 case CAIRO_FORMAT_A1:
119 width = (image->width + 7)/8;
120 break;
121 case CAIRO_FORMAT_A8:
122 width = image->width;
123 break;
124 case CAIRO_FORMAT_RGB16_565:
125 width = image->width*2;
126 break;
127 case CAIRO_FORMAT_RGB24:
128 case CAIRO_FORMAT_RGB30:
129 case CAIRO_FORMAT_ARGB32:
130 width = image->width*4;
131 break;
132 case CAIRO_FORMAT_INVALID:
133 default:
134 /* XXX compute width from pixman bpp */
135 return;
138 for (row = 0; row < image->height; row++) {
139 VALGRIND_CHECK_MEM_IS_DEFINED (bits, width);
140 /* and then silence any future valgrind warnings */
141 VALGRIND_MAKE_MEM_DEFINED (bits, width);
142 bits += image->stride;
145 #endif
148 #if 0
149 void
150 _cairo_image_surface_write_to_ppm (cairo_image_surface_t *isurf, const char *fn)
152 char *fmt;
153 if (isurf->format == CAIRO_FORMAT_ARGB32 || isurf->format == CAIRO_FORMAT_RGB24)
154 fmt = "P6";
155 else if (isurf->format == CAIRO_FORMAT_A8)
156 fmt = "P5";
157 else
158 return;
160 FILE *fp = fopen(fn, "wb");
161 if (!fp)
162 return;
164 fprintf (fp, "%s %d %d 255\n", fmt,isurf->width, isurf->height);
165 for (int j = 0; j < isurf->height; j++) {
166 unsigned char *row = isurf->data + isurf->stride * j;
167 for (int i = 0; i < isurf->width; i++) {
168 if (isurf->format == CAIRO_FORMAT_ARGB32 || isurf->format == CAIRO_FORMAT_RGB24) {
169 unsigned char r = *row++;
170 unsigned char g = *row++;
171 unsigned char b = *row++;
172 *row++;
173 putc(r, fp);
174 putc(g, fp);
175 putc(b, fp);
176 } else {
177 unsigned char a = *row++;
178 putc(a, fp);
183 fclose (fp);
185 fprintf (stderr, "Wrote %s\n", fn);
187 #endif
189 static cairo_status_t
190 _print_move_to (void *closure,
191 const cairo_point_t *point)
193 fprintf (closure,
194 " %f %f m",
195 _cairo_fixed_to_double (point->x),
196 _cairo_fixed_to_double (point->y));
198 return CAIRO_STATUS_SUCCESS;
201 static cairo_status_t
202 _print_line_to (void *closure,
203 const cairo_point_t *point)
205 fprintf (closure,
206 " %f %f l",
207 _cairo_fixed_to_double (point->x),
208 _cairo_fixed_to_double (point->y));
210 return CAIRO_STATUS_SUCCESS;
213 static cairo_status_t
214 _print_curve_to (void *closure,
215 const cairo_point_t *p1,
216 const cairo_point_t *p2,
217 const cairo_point_t *p3)
219 fprintf (closure,
220 " %f %f %f %f %f %f c",
221 _cairo_fixed_to_double (p1->x),
222 _cairo_fixed_to_double (p1->y),
223 _cairo_fixed_to_double (p2->x),
224 _cairo_fixed_to_double (p2->y),
225 _cairo_fixed_to_double (p3->x),
226 _cairo_fixed_to_double (p3->y));
228 return CAIRO_STATUS_SUCCESS;
231 static cairo_status_t
232 _print_close (void *closure)
234 fprintf (closure, " h");
236 return CAIRO_STATUS_SUCCESS;
239 void
240 _cairo_debug_print_path (FILE *stream, cairo_path_fixed_t *path)
242 cairo_status_t status;
243 cairo_box_t box;
245 fprintf (stream,
246 "path: extents=(%f, %f), (%f, %f)\n",
247 _cairo_fixed_to_double (path->extents.p1.x),
248 _cairo_fixed_to_double (path->extents.p1.y),
249 _cairo_fixed_to_double (path->extents.p2.x),
250 _cairo_fixed_to_double (path->extents.p2.y));
252 status = _cairo_path_fixed_interpret (path,
253 _print_move_to,
254 _print_line_to,
255 _print_curve_to,
256 _print_close,
257 stream);
258 assert (status == CAIRO_STATUS_SUCCESS);
260 if (_cairo_path_fixed_is_box (path, &box)) {
261 fprintf (stream, "[box (%d, %d), (%d, %d)]",
262 box.p1.x, box.p1.y, box.p2.x, box.p2.y);
265 printf ("\n");
268 void
269 _cairo_debug_print_polygon (FILE *stream, cairo_polygon_t *polygon)
271 int n;
273 fprintf (stream,
274 "polygon: extents=(%f, %f), (%f, %f)\n",
275 _cairo_fixed_to_double (polygon->extents.p1.x),
276 _cairo_fixed_to_double (polygon->extents.p1.y),
277 _cairo_fixed_to_double (polygon->extents.p2.x),
278 _cairo_fixed_to_double (polygon->extents.p2.y));
279 if (polygon->num_limits) {
280 fprintf (stream,
281 " : limit=(%f, %f), (%f, %f) x %d\n",
282 _cairo_fixed_to_double (polygon->limit.p1.x),
283 _cairo_fixed_to_double (polygon->limit.p1.y),
284 _cairo_fixed_to_double (polygon->limit.p2.x),
285 _cairo_fixed_to_double (polygon->limit.p2.y),
286 polygon->num_limits);
289 for (n = 0; n < polygon->num_edges; n++) {
290 cairo_edge_t *edge = &polygon->edges[n];
292 fprintf (stream,
293 " [%d] = [(%f, %f), (%f, %f)], top=%f, bottom=%f, dir=%d\n",
295 _cairo_fixed_to_double (edge->line.p1.x),
296 _cairo_fixed_to_double (edge->line.p1.y),
297 _cairo_fixed_to_double (edge->line.p2.x),
298 _cairo_fixed_to_double (edge->line.p2.y),
299 _cairo_fixed_to_double (edge->top),
300 _cairo_fixed_to_double (edge->bottom),
301 edge->dir);