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
608
cairo_debug_reset_static_data (void)
64
{
65
    CAIRO_MUTEX_INITIALIZE ();
66

            
67
608
    _cairo_scaled_font_map_destroy ();
68

            
69
608
    _cairo_toy_font_face_reset_static_data ();
70

            
71
#if CAIRO_HAS_FT_FONT
72
608
    _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
608
    _cairo_intern_string_reset_static_data ();
80

            
81
608
    _cairo_scaled_font_reset_static_data ();
82

            
83
608
    _cairo_pattern_reset_static_data ();
84

            
85
608
    _cairo_clip_reset_static_data ();
86

            
87
608
    _cairo_image_reset_static_data ();
88

            
89
608
    _cairo_image_compositor_reset_static_data ();
90

            
91
608
    _cairo_default_context_reset_static_data ();
92

            
93
    CAIRO_MUTEX_FINALIZE ();
94
608
}
95

            
96
#if HAVE_VALGRIND
97
void
98
_cairo_debug_check_image_surface_is_defined (const cairo_surface_t *surface)
99
{
100
    const cairo_image_surface_t *image = (cairo_image_surface_t *) surface;
101
    const uint8_t *bits;
102
    int row, width;
103

            
104
    if (surface == NULL)
105
	return;
106

            
107
    if (! RUNNING_ON_VALGRIND)
108
	return;
109

            
110
    bits = image->data;
111
    switch (image->format) {
112
    case CAIRO_FORMAT_A1:
113
	width = (image->width + 7)/8;
114
	break;
115
    case CAIRO_FORMAT_A8:
116
	width = image->width;
117
	break;
118
    case CAIRO_FORMAT_RGB16_565:
119
	width = image->width*2;
120
	break;
121
    case CAIRO_FORMAT_RGB24:
122
    case CAIRO_FORMAT_RGB30:
123
    case CAIRO_FORMAT_ARGB32:
124
	width = image->width*4;
125
	break;
126
    case CAIRO_FORMAT_RGB96F:
127
	width = image->width*12;
128
	break;
129
    case CAIRO_FORMAT_RGBA128F:
130
	width = image->width*16;
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, const 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
    fprintf (stream, "\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
}
305

            
306
void
307
_cairo_debug_print_matrix (FILE *file, const cairo_matrix_t *matrix)
308
{
309
    fprintf (file, "[%g %g %g %g %g %g]\n",
310
	     matrix->xx, matrix->yx,
311
	     matrix->xy, matrix->yy,
312
	     matrix->x0, matrix->y0);
313
}
314

            
315
void
316
_cairo_debug_print_rect (FILE *file, const cairo_rectangle_int_t *rect)
317
{
318
    fprintf (file, "x: %d y: %d width: %d height: %d\n",
319
	     rect->x, rect->y,
320
	     rect->width, rect->height);
321
}
322

            
323
const char *
324
_cairo_debug_operator_to_string (cairo_operator_t op)
325
{
326
    switch (op) {
327
        case CAIRO_OPERATOR_CLEAR: return "CLEAR";
328
        case CAIRO_OPERATOR_SOURCE: return "SOURCE";
329
        case CAIRO_OPERATOR_OVER: return "OVER";
330
        case CAIRO_OPERATOR_IN: return "IN";
331
        case CAIRO_OPERATOR_OUT: return "OUT";
332
        case CAIRO_OPERATOR_ATOP: return "ATOP";
333
        case CAIRO_OPERATOR_DEST: return "DEST";
334
        case CAIRO_OPERATOR_DEST_OVER: return "DEST_OVER";
335
        case CAIRO_OPERATOR_DEST_IN: return "DEST_IN";
336
        case CAIRO_OPERATOR_DEST_OUT: return "DEST_OUT";
337
        case CAIRO_OPERATOR_DEST_ATOP: return "DEST_ATOP";
338
        case CAIRO_OPERATOR_XOR: return "XOR";
339
        case CAIRO_OPERATOR_ADD: return "ADD";
340
        case CAIRO_OPERATOR_SATURATE: return "SATURATE";
341
        case CAIRO_OPERATOR_MULTIPLY: return "MULTIPLY";
342
        case CAIRO_OPERATOR_SCREEN: return "SCREEN";
343
        case CAIRO_OPERATOR_OVERLAY: return "OVERLAY";
344
        case CAIRO_OPERATOR_DARKEN: return "DARKEN";
345
        case CAIRO_OPERATOR_LIGHTEN: return "LIGHTEN";
346
        case CAIRO_OPERATOR_COLOR_DODGE: return "COLOR_DODGE";
347
        case CAIRO_OPERATOR_COLOR_BURN: return "COLOR_BURN";
348
        case CAIRO_OPERATOR_HARD_LIGHT: return "HARD_LIGHT";
349
        case CAIRO_OPERATOR_SOFT_LIGHT: return "SOFT_LIGHT";
350
        case CAIRO_OPERATOR_DIFFERENCE: return "DIFFERENCE";
351
        case CAIRO_OPERATOR_EXCLUSION: return "EXCLUSION";
352
        case CAIRO_OPERATOR_HSL_HUE: return "HSL_HUE";
353
        case CAIRO_OPERATOR_HSL_SATURATION: return "HSL_SATURATION";
354
        case CAIRO_OPERATOR_HSL_COLOR: return "HSL_COLOR";
355
        case CAIRO_OPERATOR_HSL_LUMINOSITY: return "HSL_LUMINOSITY";
356
    }
357
    return "UNKNOWN";
358
}
359

            
360
const char *
361
_cairo_debug_status_to_string (cairo_int_status_t status)
362
{
363
    switch (status) {
364
	case CAIRO_INT_STATUS_SUCCESS: return "SUCCESS";
365
	case CAIRO_INT_STATUS_NO_MEMORY: return "NO_MEMORY";
366
	case CAIRO_INT_STATUS_INVALID_RESTORE: return "INVALID_RESTORE";
367
	case CAIRO_INT_STATUS_INVALID_POP_GROUP: return "INVALID_POP_GROUP";
368
	case CAIRO_INT_STATUS_NO_CURRENT_POINT: return "NO_CURRENT_POINT";
369
	case CAIRO_INT_STATUS_INVALID_MATRIX: return "INVALID_MATRIX";
370
	case CAIRO_INT_STATUS_INVALID_STATUS: return "INVALID_STATUS";
371
	case CAIRO_INT_STATUS_NULL_POINTER: return "NULL_POINTER";
372
	case CAIRO_INT_STATUS_INVALID_STRING: return "INVALID_STRING";
373
	case CAIRO_INT_STATUS_INVALID_PATH_DATA: return "INVALID_PATH_DATA";
374
	case CAIRO_INT_STATUS_READ_ERROR: return "READ_ERROR";
375
	case CAIRO_INT_STATUS_WRITE_ERROR: return "WRITE_ERROR";
376
	case CAIRO_INT_STATUS_SURFACE_FINISHED: return "SURFACE_FINISHED";
377
	case CAIRO_INT_STATUS_SURFACE_TYPE_MISMATCH: return "SURFACE_TYPE_MISMATCH";
378
	case CAIRO_INT_STATUS_PATTERN_TYPE_MISMATCH: return "PATTERN_TYPE_MISMATCH";
379
	case CAIRO_INT_STATUS_INVALID_CONTENT: return "INVALID_CONTENT";
380
	case CAIRO_INT_STATUS_INVALID_FORMAT: return "INVALID_FORMAT";
381
	case CAIRO_INT_STATUS_INVALID_VISUAL: return "INVALID_VISUAL";
382
	case CAIRO_INT_STATUS_FILE_NOT_FOUND: return "FILE_NOT_FOUND";
383
	case CAIRO_INT_STATUS_INVALID_DASH: return "INVALID_DASH";
384
	case CAIRO_INT_STATUS_INVALID_DSC_COMMENT: return "INVALID_DSC_COMMENT";
385
	case CAIRO_INT_STATUS_INVALID_INDEX: return "INVALID_INDEX";
386
	case CAIRO_INT_STATUS_CLIP_NOT_REPRESENTABLE: return "CLIP_NOT_REPRESENTABLE";
387
	case CAIRO_INT_STATUS_TEMP_FILE_ERROR: return "TEMP_FILE_ERROR";
388
	case CAIRO_INT_STATUS_INVALID_STRIDE: return "INVALID_STRIDE";
389
	case CAIRO_INT_STATUS_FONT_TYPE_MISMATCH: return "FONT_TYPE_MISMATCH";
390
	case CAIRO_INT_STATUS_USER_FONT_IMMUTABLE: return "USER_FONT_IMMUTABLE";
391
	case CAIRO_INT_STATUS_USER_FONT_ERROR: return "USER_FONT_ERROR";
392
	case CAIRO_INT_STATUS_NEGATIVE_COUNT: return "NEGATIVE_COUNT";
393
	case CAIRO_INT_STATUS_INVALID_CLUSTERS: return "INVALID_CLUSTERS";
394
	case CAIRO_INT_STATUS_INVALID_SLANT: return "INVALID_SLANT";
395
	case CAIRO_INT_STATUS_INVALID_WEIGHT: return "INVALID_WEIGHT";
396
	case CAIRO_INT_STATUS_INVALID_SIZE: return "INVALID_SIZE";
397
	case CAIRO_INT_STATUS_USER_FONT_NOT_IMPLEMENTED: return "USER_FONT_NOT_IMPLEMENTED";
398
	case CAIRO_INT_STATUS_DEVICE_TYPE_MISMATCH: return "DEVICE_TYPE_MISMATCH";
399
	case CAIRO_INT_STATUS_DEVICE_ERROR: return "DEVICE_ERROR";
400
	case CAIRO_INT_STATUS_INVALID_MESH_CONSTRUCTION: return "INVALID_MESH_CONSTRUCTION";
401
	case CAIRO_INT_STATUS_DEVICE_FINISHED: return "DEVICE_FINISHED";
402
	case CAIRO_INT_STATUS_JBIG2_GLOBAL_MISSING: return "JBIG2_GLOBAL_MISSING";
403
	case CAIRO_INT_STATUS_PNG_ERROR: return "PNG_ERROR";
404
	case CAIRO_INT_STATUS_FREETYPE_ERROR: return "FREETYPE_ERROR";
405
	case CAIRO_INT_STATUS_WIN32_GDI_ERROR: return "WIN32_GDI_ERROR";
406
	case CAIRO_INT_STATUS_TAG_ERROR: return "TAG_ERROR";
407
	case CAIRO_INT_STATUS_DWRITE_ERROR: return "DWRITE_ERROR";
408
	case CAIRO_INT_STATUS_SVG_FONT_ERROR: return "SVG_FONT_ERROR";
409

            
410
	case CAIRO_INT_STATUS_LAST_STATUS: return "LAST_STATUS";
411

            
412
	case CAIRO_INT_STATUS_UNSUPPORTED: return "UNSUPPORTED";
413
	case CAIRO_INT_STATUS_DEGENERATE: return "DEGENERATE";
414
	case CAIRO_INT_STATUS_NOTHING_TO_DO: return "NOTHING_TO_DO";
415
	case CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY: return "FLATTEN_TRANSPARENCY";
416
	case CAIRO_INT_STATUS_IMAGE_FALLBACK: return "IMAGE_FALLBACK";
417
	case CAIRO_INT_STATUS_ANALYZE_RECORDING_SURFACE_PATTERN: return "ANALYZE_RECORDING_SURFACE_PATTERN";
418
    }
419
    return "UNKNOWN";
420
}