Tizen 2.0 Release
[framework/graphics/cairo.git] / src / cairo-debug.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2005 Red Hat, Inc.
4  *
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.
12  *
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
18  *
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/
23  *
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.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Red Hat, Inc.
31  *
32  * Contributor(s):
33  *      Carl D. Worth <cworth@cworth.org>
34  */
35
36 #include "cairoint.h"
37 #include "cairo-image-surface-private.h"
38
39 /**
40  * cairo_debug_reset_static_data:
41  *
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.
45  *
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.
53  *
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).
59  *
60  * Since: 1.0
61  **/
62 void
63 cairo_debug_reset_static_data (void)
64 {
65     CAIRO_MUTEX_INITIALIZE ();
66
67     _cairo_scaled_font_map_destroy ();
68
69     _cairo_toy_font_face_reset_static_data ();
70
71 #if CAIRO_HAS_FT_FONT
72     _cairo_ft_font_reset_static_data ();
73 #endif
74
75 #if CAIRO_HAS_WIN32_FONT
76     _cairo_win32_font_reset_static_data ();
77 #endif
78
79     _cairo_intern_string_reset_static_data ();
80
81     _cairo_scaled_font_reset_static_data ();
82
83     _cairo_pattern_reset_static_data ();
84
85     _cairo_clip_reset_static_data ();
86
87     _cairo_image_reset_static_data ();
88
89 #if CAIRO_HAS_DRM_SURFACE
90     _cairo_drm_device_reset_static_data ();
91 #endif
92
93     _cairo_default_context_reset_static_data ();
94
95 #if CAIRO_HAS_COGL_SURFACE
96     _cairo_cogl_context_reset_static_data ();
97 #endif
98
99     CAIRO_MUTEX_FINALIZE ();
100 }
101
102 #if HAVE_VALGRIND
103 void
104 _cairo_debug_check_image_surface_is_defined (const cairo_surface_t *surface)
105 {
106     const cairo_image_surface_t *image = (cairo_image_surface_t *) surface;
107     const uint8_t *bits;
108     int row, width;
109
110     if (surface == NULL)
111         return;
112
113     if (! RUNNING_ON_VALGRIND)
114         return;
115
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;
136     }
137
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;
143     }
144 }
145 #endif
146
147
148 #if 0
149 void
150 _cairo_image_surface_write_to_ppm (cairo_image_surface_t *isurf, const char *fn)
151 {
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;
159
160     FILE *fp = fopen(fn, "wb");
161     if (!fp)
162         return;
163
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);
179             }
180         }
181     }
182
183     fclose (fp);
184
185     fprintf (stderr, "Wrote %s\n", fn);
186 }
187 #endif
188
189 static cairo_status_t
190 _print_move_to (void *closure,
191                 const cairo_point_t *point)
192 {
193     fprintf (closure,
194              " %f %f m",
195              _cairo_fixed_to_double (point->x),
196              _cairo_fixed_to_double (point->y));
197
198     return CAIRO_STATUS_SUCCESS;
199 }
200
201 static cairo_status_t
202 _print_line_to (void *closure,
203                 const cairo_point_t *point)
204 {
205     fprintf (closure,
206              " %f %f l",
207              _cairo_fixed_to_double (point->x),
208              _cairo_fixed_to_double (point->y));
209
210     return CAIRO_STATUS_SUCCESS;
211 }
212
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)
218 {
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));
227
228     return CAIRO_STATUS_SUCCESS;
229 }
230
231 static cairo_status_t
232 _print_close (void *closure)
233 {
234     fprintf (closure, " h");
235
236     return CAIRO_STATUS_SUCCESS;
237 }
238
239 void
240 _cairo_debug_print_path (FILE *stream, cairo_path_fixed_t *path)
241 {
242     cairo_status_t status;
243     cairo_box_t box;
244
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));
251
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);
259
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);
263     }
264
265     printf ("\n");
266 }
267
268 void
269 _cairo_debug_print_polygon (FILE *stream, cairo_polygon_t *polygon)
270 {
271     int n;
272
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);
287     }
288
289     for (n = 0; n < polygon->num_edges; n++) {
290         cairo_edge_t *edge = &polygon->edges[n];
291
292         fprintf (stream,
293                  "  [%d] = [(%f, %f), (%f, %f)], top=%f, bottom=%f, dir=%d\n",
294                  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);
302
303     }
304 }