1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright © 2005 Red Hat, Inc
5
 * Copyright © 2007 Adrian Johnson
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it either under the terms of the GNU Lesser General Public
9
 * License version 2.1 as published by the Free Software Foundation
10
 * (the "LGPL") or, at your option, under the terms of the Mozilla
11
 * Public License Version 1.1 (the "MPL"). If you do not alter this
12
 * notice, a recipient may use your version of this file under either
13
 * the MPL or the LGPL.
14
 *
15
 * You should have received a copy of the LGPL along with this library
16
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18
 * You should have received a copy of the MPL along with this library
19
 * in the file COPYING-MPL-1.1
20
 *
21
 * The contents of this file are subject to the Mozilla Public License
22
 * Version 1.1 (the "License"); you may not use this file except in
23
 * compliance with the License. You may obtain a copy of the License at
24
 * http://www.mozilla.org/MPL/
25
 *
26
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28
 * the specific language governing rights and limitations.
29
 *
30
 * The Original Code is the cairo graphics library.
31
 *
32
 * The Initial Developer of the Original Code is Red Hat, Inc.
33
 *
34
 * Contributor(s):
35
 *	Kristian Høgsberg <krh@redhat.com>
36
 *	Carl Worth <cworth@cworth.org>
37
 *	Adrian Johnson <ajohnson@redneon.com>
38
 */
39

            
40
/**
41
 * SECTION:cairo-recording
42
 * @Title: Recording Surfaces
43
 * @Short_Description: Records all drawing operations
44
 * @See_Also: #cairo_surface_t
45
 *
46
 * A recording surface is a surface that records all drawing operations at
47
 * the highest level of the surface backend interface, (that is, the
48
 * level of paint, mask, stroke, fill, and show_text_glyphs). The recording
49
 * surface can then be "replayed" against any target surface by using it
50
 * as a source surface.
51
 *
52
 * If you want to replay a surface so that the results in target will be
53
 * identical to the results that would have been obtained if the original
54
 * operations applied to the recording surface had instead been applied to the
55
 * target surface, you can use code like this:
56
 * <informalexample><programlisting>
57
 * cairo_t *cr;
58
 *
59
 * cr = cairo_create (target);
60
 * cairo_set_source_surface (cr, recording_surface, 0.0, 0.0);
61
 * cairo_paint (cr);
62
 * cairo_destroy (cr);
63
 * </programlisting></informalexample>
64
 *
65
 * A recording surface is logically unbounded, i.e. it has no implicit constraint
66
 * on the size of the drawing surface. However, in practice this is rarely
67
 * useful as you wish to replay against a particular target surface with
68
 * known bounds. For this case, it is more efficient to specify the target
69
 * extents to the recording surface upon creation.
70
 *
71
 * The recording phase of the recording surface is careful to snapshot all
72
 * necessary objects (paths, patterns, etc.), in order to achieve
73
 * accurate replay. The efficiency of the recording surface could be
74
 * improved by improving the implementation of snapshot for the
75
 * various objects. For example, it would be nice to have a
76
 * copy-on-write implementation for _cairo_surface_snapshot.
77
 **/
78

            
79
#include "cairoint.h"
80

            
81
#include "cairo-array-private.h"
82
#include "cairo-analysis-surface-private.h"
83
#include "cairo-clip-private.h"
84
#include "cairo-combsort-inline.h"
85
#include "cairo-composite-rectangles-private.h"
86
#include "cairo-default-context-private.h"
87
#include "cairo-error-private.h"
88
#include "cairo-image-surface-private.h"
89
#include "cairo-list-inline.h"
90
#include "cairo-recording-surface-inline.h"
91
#include "cairo-surface-snapshot-inline.h"
92
#include "cairo-surface-wrapper-private.h"
93
#include "cairo-traps-private.h"
94

            
95
typedef struct _cairo_recording_surface_replay_params {
96
    const cairo_rectangle_int_t *surface_extents;
97
    const cairo_matrix_t *surface_transform;
98
    cairo_surface_t	     *target;
99
    const cairo_clip_t *target_clip;
100
    cairo_bool_t surface_is_unbounded;
101
    cairo_recording_replay_type_t type;
102
    cairo_recording_region_type_t region;
103
    unsigned int regions_id;
104
    const cairo_color_t *foreground_color;
105
    cairo_bool_t foreground_used;
106
    cairo_bool_t replay_all;
107
} cairo_recording_surface_replay_params_t;
108

            
109
static const cairo_surface_backend_t cairo_recording_surface_backend;
110

            
111
/**
112
 * CAIRO_HAS_RECORDING_SURFACE:
113
 *
114
 * Defined if the recording surface backend is available.
115
 * The recording surface backend is always built in.
116
 * This macro was added for completeness in cairo 1.10.
117
 *
118
 * Since: 1.10
119
 **/
120

            
121
/* Currently all recording surfaces do have a size which should be passed
122
 * in as the maximum size of any target surface against which the
123
 * recording-surface will ever be replayed.
124
 *
125
 * XXX: The naming of "pixels" in the size here is a misnomer. It's
126
 * actually a size in whatever device-space units are desired (again,
127
 * according to the intended replay target).
128
 */
129

            
130
1594
static int bbtree_left_or_right (struct bbtree *bbt,
131
				 const cairo_box_t *box)
132
{
133
    int left, right;
134

            
135
1594
    if (bbt->left) {
136
983
	cairo_box_t *e = &bbt->left->extents;
137
	cairo_box_t b;
138

            
139
983
	b.p1.x = MIN (e->p1.x, box->p1.x);
140
983
	b.p1.y = MIN (e->p1.y, box->p1.y);
141
983
	b.p2.x = MAX (e->p2.x, box->p2.x);
142
983
	b.p2.y = MAX (e->p2.y, box->p2.y);
143

            
144
983
	left = _cairo_fixed_integer_part (b.p2.x - b.p1.x) * _cairo_fixed_integer_part (b.p2.y - b.p1.y);
145
983
	left -= _cairo_fixed_integer_part (e->p2.x - e->p1.x) * _cairo_fixed_integer_part (e->p2.y - e->p1.y);
146
    } else
147
611
	left = 0;
148

            
149
1594
    if (bbt->right) {
150
504
	cairo_box_t *e = &bbt->right->extents;
151
	cairo_box_t b;
152

            
153
504
	b.p1.x = MIN (e->p1.x, box->p1.x);
154
504
	b.p1.y = MIN (e->p1.y, box->p1.y);
155
504
	b.p2.x = MAX (e->p2.x, box->p2.x);
156
504
	b.p2.y = MAX (e->p2.y, box->p2.y);
157

            
158
504
	right = _cairo_fixed_integer_part (b.p2.x - b.p1.x) * _cairo_fixed_integer_part (b.p2.y - b.p1.y);
159
504
	right -= _cairo_fixed_integer_part (e->p2.x - e->p1.x) * _cairo_fixed_integer_part (e->p2.y - e->p1.y);
160
    } else
161
1090
	right = 0;
162

            
163
1594
    return left <= right;
164
}
165

            
166
#define INVALID_CHAIN ((cairo_command_header_t *)-1)
167

            
168
static struct bbtree *
169
1090
bbtree_new (const cairo_box_t *box, cairo_command_header_t *chain)
170
{
171
1090
    struct bbtree *bbt = _cairo_calloc (sizeof (*bbt));
172
1090
    if (bbt == NULL)
173
	return NULL;
174
1090
    bbt->extents = *box;
175
1090
    bbt->left = bbt->right = NULL;
176
1090
    bbt->chain = chain;
177
1090
    return bbt;
178
}
179

            
180
static void
181
1318
bbtree_init (struct bbtree *bbt, cairo_command_header_t *header)
182
{
183
1318
    _cairo_box_from_rectangle (&bbt->extents, &header->extents);
184
1318
    bbt->chain = header;
185
1318
}
186

            
187
static cairo_status_t
188
1239
bbtree_add (struct bbtree *bbt,
189
	    cairo_command_header_t *header,
190
	    const cairo_box_t *box)
191
{
192
1239
    if (box->p1.x < bbt->extents.p1.x || box->p1.y < bbt->extents.p1.y ||
193
745
	box->p2.x > bbt->extents.p2.x || box->p2.y > bbt->extents.p2.y)
194
    {
195
887
	if (bbt->chain) {
196
584
	    if (bbtree_left_or_right (bbt, &bbt->extents)) {
197
584
		if (bbt->left == NULL) {
198
584
		    bbt->left = bbtree_new (&bbt->extents, bbt->chain);
199
584
		    if (unlikely (bbt->left == NULL))
200
			return _cairo_error (CAIRO_STATUS_NO_MEMORY);
201
		} else
202
		    bbtree_add (bbt->left, bbt->chain, &bbt->extents);
203
	    } else {
204
		if (bbt->right == NULL) {
205
		    bbt->right = bbtree_new (&bbt->extents, bbt->chain);
206
		    if (unlikely (bbt->right == NULL))
207
			return _cairo_error (CAIRO_STATUS_NO_MEMORY);
208
		} else
209
		    bbtree_add (bbt->right, bbt->chain, &bbt->extents);
210
	    }
211

            
212
584
	    bbt->chain = NULL;
213
	}
214

            
215
887
	bbt->extents.p1.x = MIN (bbt->extents.p1.x, box->p1.x);
216
887
	bbt->extents.p1.y = MIN (bbt->extents.p1.y, box->p1.y);
217
887
	bbt->extents.p2.x = MAX (bbt->extents.p2.x, box->p2.x);
218
887
	bbt->extents.p2.y = MAX (bbt->extents.p2.y, box->p2.y);
219
    }
220

            
221
1239
    if (box->p1.x == bbt->extents.p1.x && box->p1.y == bbt->extents.p1.y &&
222
414
	box->p2.x == bbt->extents.p2.x && box->p2.y == bbt->extents.p2.y)
223
    {
224
229
	cairo_command_header_t *last = header;
225
229
	while (last->chain) /* expected to be infrequent */
226
	    last = last->chain;
227
229
	last->chain = bbt->chain;
228
229
	bbt->chain = header;
229
229
	return CAIRO_STATUS_SUCCESS;
230
    }
231

            
232
1010
    if (bbtree_left_or_right (bbt, box)) {
233
297
	if (bbt->left == NULL) {
234
27
	    bbt->left = bbtree_new (box, header);
235
27
	    if (unlikely (bbt->left == NULL))
236
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
237
	} else
238
270
	    return bbtree_add (bbt->left, header, box);
239
    } else {
240
713
	if (bbt->right == NULL) {
241
479
	    bbt->right = bbtree_new (box, header);
242
479
	    if (unlikely (bbt->right == NULL))
243
		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
244
	} else
245
234
	    return bbtree_add (bbt->right, header, box);
246
    }
247

            
248
506
    return CAIRO_STATUS_SUCCESS;
249
}
250

            
251
601
static void bbtree_del (struct bbtree *bbt)
252
{
253
601
    if (bbt->left)
254
168
	bbtree_del (bbt->left);
255
601
    if (bbt->right)
256
156
	bbtree_del (bbt->right);
257

            
258
601
    free (bbt);
259
601
}
260

            
261
629510
static cairo_bool_t box_outside (const cairo_box_t *a, const cairo_box_t *b)
262
{
263
    return
264
527513
	a->p1.x >= b->p2.x || a->p1.y >= b->p2.y ||
265
1157023
	a->p2.x <= b->p1.x || a->p2.y <= b->p1.y;
266
}
267

            
268
static void
269
508088
bbtree_foreach_mark_visible (struct bbtree *bbt,
270
			     const cairo_box_t *box,
271
			     unsigned int **indices)
272
{
273
    cairo_command_header_t *chain;
274

            
275
879895
    for (chain = bbt->chain; chain; chain = chain->chain)
276
371807
	*(*indices)++ = chain->index;
277

            
278
508088
    if (bbt->left && ! box_outside (box, &bbt->left->extents))
279
171986
	bbtree_foreach_mark_visible (bbt->left, box, indices);
280
508088
    if (bbt->right && ! box_outside (box, &bbt->right->extents))
281
111506
	bbtree_foreach_mark_visible (bbt->right, box, indices);
282
508088
}
283

            
284
149101
static inline int intcmp (const unsigned int a, const unsigned int b)
285
{
286
149101
    return a - b;
287
}
288
296725
CAIRO_COMBSORT_DECLARE (sort_indices, unsigned int, intcmp)
289

            
290
1928
static inline int sizecmp (unsigned int a, unsigned int b, cairo_command_header_t **elements)
291
{
292
    const cairo_rectangle_int_t *r;
293

            
294
1928
    r = &elements[a]->extents;
295
1928
    a = r->width * r->height;
296

            
297
1928
    r = &elements[b]->extents;
298
1928
    b = r->width * r->height;
299

            
300
1928
    return b - a;
301
}
302
3707
CAIRO_COMBSORT_DECLARE_WITH_DATA (sort_commands, unsigned int, sizecmp)
303

            
304
static void
305
2345
_cairo_recording_surface_destroy_bbtree (cairo_recording_surface_t *surface)
306
{
307
    cairo_command_t **elements;
308
    int i, num_elements;
309

            
310
2345
    if (surface->bbtree.chain == INVALID_CHAIN)
311
2345
	return;
312

            
313
    if (surface->bbtree.left) {
314
	bbtree_del (surface->bbtree.left);
315
	surface->bbtree.left = NULL;
316
    }
317
    if (surface->bbtree.right) {
318
	bbtree_del (surface->bbtree.right);
319
	surface->bbtree.right = NULL;
320
    }
321

            
322
    elements = _cairo_array_index (&surface->commands, 0);
323
    num_elements = surface->commands.num_elements;
324
    for (i = 0; i < num_elements; i++)
325
	elements[i]->header.chain = NULL;
326

            
327
    surface->bbtree.chain = INVALID_CHAIN;
328
}
329

            
330
static cairo_status_t
331
1318
_cairo_recording_surface_create_bbtree (cairo_recording_surface_t *surface)
332
{
333
1318
    cairo_command_t **elements = _cairo_array_index (&surface->commands, 0);
334
    unsigned int *indices;
335
    cairo_status_t status;
336
    unsigned int i, count;
337

            
338
1318
    count = surface->commands.num_elements;
339
1318
    if (count > surface->num_indices) {
340
1318
	free (surface->indices);
341
1318
	surface->indices = _cairo_malloc_ab (count, sizeof (int));
342
1318
	if (unlikely (surface->indices == NULL))
343
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
344

            
345
1318
	surface->num_indices = count;
346
    }
347

            
348
1318
    indices = surface->indices;
349
3371
    for (i = 0; i < count; i++)
350
2053
	indices[i] = i;
351

            
352
1318
    sort_commands (indices, count, elements);
353

            
354
1318
    bbtree_init (&surface->bbtree, &elements[indices[0]]->header);
355
2053
    for (i = 1; i < count; i++) {
356
735
	cairo_command_header_t *header = &elements[indices[i]]->header;
357
	cairo_box_t box;
358

            
359
735
	_cairo_box_from_rectangle (&box, &header->extents);
360
735
	status = bbtree_add (&surface->bbtree, header, &box);
361
735
	if (unlikely (status))
362
	    goto cleanup;
363
    }
364

            
365
1318
    return CAIRO_STATUS_SUCCESS;
366

            
367
cleanup:
368
    if (surface->bbtree.left)
369
	bbtree_del (surface->bbtree.left);
370
    if (surface->bbtree.right)
371
	bbtree_del (surface->bbtree.right);
372
    return status;
373
}
374

            
375
/**
376
 * cairo_recording_surface_create:
377
 * @content: the content of the recording surface
378
 * @extents: the extents to record in pixels, can be %NULL to record
379
 *           unbounded operations.
380
 *
381
 * Creates a recording-surface which can be used to record all drawing operations
382
 * at the highest level (that is, the level of paint, mask, stroke, fill
383
 * and show_text_glyphs). The recording surface can then be "replayed" against
384
 * any target surface by using it as a source to drawing operations.
385
 *
386
 * The recording phase of the recording surface is careful to snapshot all
387
 * necessary objects (paths, patterns, etc.), in order to achieve
388
 * accurate replay.
389
 *
390
 * Return value: a pointer to the newly created surface. The caller
391
 * owns the surface and should call cairo_surface_destroy() when done
392
 * with it.
393
 *
394
 * Since: 1.10
395
 **/
396
cairo_surface_t *
397
1799
cairo_recording_surface_create (cairo_content_t		 content,
398
				const cairo_rectangle_t	*extents)
399
{
400
    cairo_recording_surface_t *surface;
401

            
402
1799
    surface = _cairo_calloc (sizeof (cairo_recording_surface_t));
403
1799
    if (unlikely (surface == NULL))
404
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
405

            
406
1799
    _cairo_surface_init (&surface->base,
407
			 &cairo_recording_surface_backend,
408
			 NULL, /* device */
409
			 content,
410
			 TRUE); /* is_vector */
411

            
412

            
413
1799
    surface->unbounded = TRUE;
414

            
415
    /* unbounded -> 'infinite' extents */
416
1799
    if (extents != NULL) {
417
119
	surface->extents_pixels = *extents;
418

            
419
	/* XXX check for overflow */
420
119
	surface->extents.x = floor (extents->x);
421
119
	surface->extents.y = floor (extents->y);
422
119
	surface->extents.width = ceil (extents->x + extents->width) - surface->extents.x;
423
119
	surface->extents.height = ceil (extents->y + extents->height) - surface->extents.y;
424

            
425
119
	surface->unbounded = FALSE;
426
    }
427

            
428
1799
    _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
429

            
430
1799
    surface->base.is_clear = TRUE;
431

            
432
1799
    surface->bbtree.left = surface->bbtree.right = NULL;
433
1799
    surface->bbtree.chain = INVALID_CHAIN;
434

            
435
1799
    surface->indices = NULL;
436
1799
    surface->num_indices = 0;
437
1799
    surface->optimize_clears = TRUE;
438
1799
    surface->has_bilevel_alpha = FALSE;
439
1799
    surface->has_only_op_over = FALSE;
440
1799
    surface->has_tags = FALSE;
441

            
442
1799
    CAIRO_MUTEX_INIT (surface->mutex);
443

            
444
1799
    cairo_list_init (&surface->region_array_list);
445

            
446
1799
    return &surface->base;
447
}
448

            
449
static cairo_surface_t *
450
12
_cairo_recording_surface_create_similar (void		       *abstract_surface,
451
					 cairo_content_t	content,
452
					 int			width,
453
					 int			height)
454
{
455
    cairo_rectangle_t extents;
456
12
    extents.x = extents.y = 0;
457
12
    extents.width = width;
458
12
    extents.height = height;
459
12
    return cairo_recording_surface_create (content, &extents);
460
}
461

            
462
static void
463
105
destroy_pattern_region_array (const cairo_pattern_t *pattern,
464
			      unsigned int region_id)
465
{
466
105
    if (region_id != 0) {
467
12
	if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
468
12
	    cairo_surface_pattern_t *surface_pattern = (cairo_surface_pattern_t *) pattern;
469
12
	    if (_cairo_surface_is_recording (surface_pattern->surface))
470
		_cairo_recording_surface_region_array_remove (surface_pattern->surface, region_id);
471
	}
472
    }
473
105
}
474

            
475
static void
476
24
_cairo_recording_surface_region_array_destroy (cairo_recording_surface_t       *surface,
477
					       cairo_recording_regions_array_t *region_array)
478
{
479
    cairo_command_t **elements;
480
    cairo_recording_region_element_t *region_elements;
481
    int i, num_elements;
482

            
483
24
    num_elements = MIN(surface->commands.num_elements, _cairo_array_num_elements(&region_array->regions));
484
24
    elements = _cairo_array_index (&surface->commands, 0);
485
24
    region_elements = _cairo_array_index (&region_array->regions, 0);
486
129
    for (i = 0; i < num_elements; i++) {
487
105
	cairo_command_t *command = elements[i];
488
105
	cairo_recording_region_element_t *region_element = &region_elements[i];
489

            
490
105
	switch (command->header.type) {
491
21
	    case CAIRO_COMMAND_PAINT:
492
21
		destroy_pattern_region_array (&command->paint.source.base, region_element->source_id);
493
21
		break;
494

            
495
	    case CAIRO_COMMAND_MASK:
496
		destroy_pattern_region_array (&command->mask.source.base, region_element->source_id);
497
		destroy_pattern_region_array (&command->mask.mask.base, region_element->mask_id);
498
		break;
499

            
500
	    case CAIRO_COMMAND_STROKE:
501
		destroy_pattern_region_array (&command->stroke.source.base, region_element->source_id);
502
		break;
503

            
504
84
	    case CAIRO_COMMAND_FILL:
505
84
		destroy_pattern_region_array (&command->fill.source.base, region_element->source_id);
506
84
		break;
507

            
508
	    case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
509
		destroy_pattern_region_array (&command->show_text_glyphs.source.base, region_element->source_id);
510
		break;
511

            
512
	    case CAIRO_COMMAND_TAG:
513
		break;
514

            
515
	    default:
516
		ASSERT_NOT_REACHED;
517
	}
518
    }
519

            
520
24
    _cairo_array_fini (&region_array->regions);
521
24
    free (region_array);
522
24
}
523

            
524
static cairo_status_t
525
1112
_cairo_recording_surface_finish (void *abstract_surface)
526
{
527
1112
    cairo_recording_surface_t *surface = abstract_surface;
528
    cairo_command_t **elements;
529
    int i, num_elements;
530
    cairo_recording_regions_array_t *region_array, *region_next;
531

            
532
    /* Normally backend surfaces hold a reference to the surface as
533
     * well as the region and free the region before the surface. So
534
     * the regions should already be freed at this point but just in
535
     * case we ensure the regions are freed before destroying the
536
     * surface. */
537
1112
    cairo_list_foreach_entry_safe (region_array, region_next,
538
				   cairo_recording_regions_array_t,
539
				   &surface->region_array_list, link)
540
    {
541
	cairo_list_del (&region_array->link);
542
	_cairo_recording_surface_region_array_destroy (surface, region_array);
543
    }
544

            
545
1112
    num_elements = surface->commands.num_elements;
546
1112
    elements = _cairo_array_index (&surface->commands, 0);
547
2779
    for (i = 0; i < num_elements; i++) {
548
1667
	cairo_command_t *command = elements[i];
549

            
550
1667
	switch (command->header.type) {
551
369
	case CAIRO_COMMAND_PAINT:
552
369
	    _cairo_pattern_fini (&command->paint.source.base);
553
369
	    break;
554

            
555
54
	case CAIRO_COMMAND_MASK:
556
54
	    _cairo_pattern_fini (&command->mask.source.base);
557
54
	    _cairo_pattern_fini (&command->mask.mask.base);
558
54
	    break;
559

            
560
31
	case CAIRO_COMMAND_STROKE:
561
31
	    _cairo_pattern_fini (&command->stroke.source.base);
562
31
	    _cairo_path_fixed_fini (&command->stroke.path);
563
31
	    _cairo_stroke_style_fini (&command->stroke.style);
564
31
	    break;
565

            
566
1009
	case CAIRO_COMMAND_FILL:
567
1009
	    _cairo_pattern_fini (&command->fill.source.base);
568
1009
	    _cairo_path_fixed_fini (&command->fill.path);
569
1009
	    break;
570

            
571
198
	case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
572
198
	    _cairo_pattern_fini (&command->show_text_glyphs.source.base);
573
198
	    free (command->show_text_glyphs.utf8);
574
198
	    free (command->show_text_glyphs.glyphs);
575
198
	    free (command->show_text_glyphs.clusters);
576
198
	    cairo_scaled_font_destroy (command->show_text_glyphs.scaled_font);
577
198
	    break;
578

            
579
6
	case CAIRO_COMMAND_TAG:
580
6
	    free (command->tag.tag_name);
581
6
	    if (command->tag.begin) {
582
3
		free (command->tag.attributes);
583
	    }
584
6
	    break;
585

            
586
	    default:
587
	    ASSERT_NOT_REACHED;
588
	}
589

            
590
1667
	_cairo_clip_destroy (command->header.clip);
591
1667
	free (command);
592
    }
593

            
594
1112
    _cairo_array_fini (&surface->commands);
595

            
596
1112
    if (surface->bbtree.left)
597
188
	bbtree_del (surface->bbtree.left);
598
1112
    if (surface->bbtree.right)
599
89
	bbtree_del (surface->bbtree.right);
600

            
601
1112
    free (surface->indices);
602

            
603
1112
    return CAIRO_STATUS_SUCCESS;
604
}
605

            
606
struct proxy {
607
    cairo_surface_t base;
608
    cairo_surface_t *image;
609
};
610

            
611
static cairo_status_t
612
proxy_acquire_source_image (void			 *abstract_surface,
613
			    cairo_image_surface_t	**image_out,
614
			    void			**image_extra)
615
{
616
    struct proxy *proxy = abstract_surface;
617
    return _cairo_surface_acquire_source_image (proxy->image, image_out, image_extra);
618
}
619

            
620
static void
621
proxy_release_source_image (void			*abstract_surface,
622
			    cairo_image_surface_t	*image,
623
			    void			*image_extra)
624
{
625
    struct proxy *proxy = abstract_surface;
626
    _cairo_surface_release_source_image (proxy->image, image, image_extra);
627
}
628

            
629
static cairo_status_t
630
3
proxy_finish (void *abstract_surface)
631
{
632
3
    return CAIRO_STATUS_SUCCESS;
633
}
634

            
635
static const cairo_surface_backend_t proxy_backend  = {
636
    CAIRO_INTERNAL_SURFACE_TYPE_NULL,
637
    proxy_finish,
638
    NULL,
639

            
640
    NULL, /* create similar */
641
    NULL, /* create similar image */
642
    NULL, /* map to image */
643
    NULL, /* unmap image */
644

            
645
    _cairo_surface_default_source,
646
    proxy_acquire_source_image,
647
    proxy_release_source_image,
648
};
649

            
650
static cairo_surface_t *
651
3
attach_proxy (cairo_surface_t *source,
652
	      cairo_surface_t *image)
653
{
654
    struct proxy *proxy;
655

            
656
3
    proxy = _cairo_calloc (sizeof (*proxy));
657
3
    if (unlikely (proxy == NULL))
658
	return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
659

            
660
3
    _cairo_surface_init (&proxy->base, &proxy_backend, NULL, image->content, FALSE);
661

            
662
3
    proxy->image = image;
663
3
    _cairo_surface_attach_snapshot (source, &proxy->base, NULL);
664

            
665
3
    return &proxy->base;
666
}
667

            
668
static void
669
3
detach_proxy (cairo_surface_t *source,
670
	      cairo_surface_t *proxy)
671
{
672
3
    cairo_surface_finish (proxy);
673
3
    cairo_surface_destroy (proxy);
674
3
}
675

            
676
static cairo_surface_t *
677
get_proxy (cairo_surface_t *proxy)
678
{
679
    return ((struct proxy *)proxy)->image;
680
}
681

            
682
static cairo_status_t
683
3
_cairo_recording_surface_acquire_source_image (void			 *abstract_surface,
684
					       cairo_image_surface_t	**image_out,
685
					       void			**image_extra)
686
{
687
3
    cairo_recording_surface_t *surface = abstract_surface;
688
    cairo_surface_t *image, *proxy;
689
    cairo_status_t status;
690

            
691
3
    proxy = _cairo_surface_has_snapshot (abstract_surface, &proxy_backend);
692
3
    if (proxy != NULL) {
693
	*image_out = (cairo_image_surface_t *)
694
	    cairo_surface_reference (get_proxy (proxy));
695
	*image_extra = NULL;
696
	return CAIRO_STATUS_SUCCESS;
697
    }
698

            
699
3
    if (surface->unbounded)
700
	return CAIRO_INT_STATUS_UNSUPPORTED;
701
3
    image = _cairo_image_surface_create_with_content (surface->base.content,
702
						      surface->extents.width,
703
						      surface->extents.height);
704
3
    cairo_surface_set_device_offset (image, -surface->extents.x, -surface->extents.y);
705
3
    if (unlikely (image->status))
706
	return image->status;
707

            
708
3
    cairo_surface_set_device_offset(image, -surface->extents.x, -surface->extents.y);
709

            
710
    /* Handle recursion by returning future reads from the current image */
711
3
    proxy = attach_proxy (abstract_surface, image);
712
3
    status = _cairo_recording_surface_replay (&surface->base, image);
713
3
    detach_proxy (abstract_surface, proxy);
714

            
715
3
    if (unlikely (status)) {
716
	cairo_surface_destroy (image);
717
	return status;
718
    }
719

            
720
3
    *image_out = (cairo_image_surface_t *) image;
721
3
    *image_extra = NULL;
722
3
    return CAIRO_STATUS_SUCCESS;
723
}
724

            
725
static void
726
3
_cairo_recording_surface_release_source_image (void			*abstract_surface,
727
					       cairo_image_surface_t	*image,
728
					       void			*image_extra)
729
{
730
3
    cairo_surface_destroy (&image->base);
731
3
}
732

            
733
static cairo_status_t
734
2642
_command_init (cairo_recording_surface_t *surface,
735
	       cairo_command_header_t *command,
736
	       cairo_command_type_t type,
737
	       cairo_operator_t op,
738
	       cairo_composite_rectangles_t *composite)
739
{
740
2642
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
741

            
742
2642
    command->type = type;
743
2642
    command->op = op;
744

            
745
2642
    command->extents = composite ? composite->unbounded : _cairo_empty_rectangle;
746
2642
    command->chain = NULL;
747
2642
    command->index = surface->commands.num_elements;
748

            
749
    /* steal the clip */
750
2642
    command->clip = NULL;
751
2642
    if (composite && ! _cairo_composite_rectangles_can_reduce_clip (composite,
752
								    composite->clip))
753
    {
754
231
	command->clip = composite->clip;
755
231
	composite->clip = NULL;
756
    }
757

            
758
2642
    return status;
759
}
760

            
761
static void
762
3074
_cairo_recording_surface_break_self_copy_loop (cairo_recording_surface_t *surface)
763
{
764
3074
    cairo_surface_flush (&surface->base);
765
3074
}
766

            
767
static cairo_status_t
768
3074
_cairo_recording_surface_commit (cairo_recording_surface_t *surface,
769
				 cairo_command_header_t *command)
770
{
771
3074
    _cairo_recording_surface_break_self_copy_loop (surface);
772
3074
    return _cairo_array_append (&surface->commands, &command);
773
}
774

            
775
static void
776
336
_cairo_recording_surface_reset (cairo_recording_surface_t *surface)
777
{
778
    /* Reset the commands and temporaries */
779
336
    _cairo_recording_surface_finish (surface);
780

            
781
336
    surface->bbtree.left = surface->bbtree.right = NULL;
782
336
    surface->bbtree.chain = INVALID_CHAIN;
783

            
784
336
    surface->indices = NULL;
785
336
    surface->num_indices = 0;
786

            
787
336
    _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
788
336
}
789

            
790
static cairo_int_status_t
791
456
_cairo_recording_surface_paint (void			  *abstract_surface,
792
				cairo_operator_t	   op,
793
				const cairo_pattern_t	  *source,
794
				const cairo_clip_t	  *clip)
795
{
796
    cairo_status_t status;
797
456
    cairo_recording_surface_t *surface = abstract_surface;
798
    cairo_command_paint_t *command;
799
    cairo_composite_rectangles_t composite;
800

            
801
    TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
802

            
803
456
    if (op == CAIRO_OPERATOR_CLEAR && clip == NULL) {
804
	if (surface->optimize_clears) {
805
	    _cairo_recording_surface_reset (surface);
806
	    return CAIRO_STATUS_SUCCESS;
807
	}
808
    }
809

            
810
456
    if (clip == NULL && surface->optimize_clears &&
811
381
	(op == CAIRO_OPERATOR_SOURCE ||
812
381
	 (op == CAIRO_OPERATOR_OVER &&
813
381
	  (surface->base.is_clear || _cairo_pattern_is_opaque_solid (source)))))
814
    {
815
336
	_cairo_recording_surface_reset (surface);
816
    }
817

            
818
456
    status = _cairo_composite_rectangles_init_for_paint (&composite,
819
							 &surface->base,
820
							 op, source,
821
							 clip);
822
456
    if (unlikely (status))
823
	return status;
824

            
825
456
    command = _cairo_calloc (sizeof (cairo_command_paint_t));
826
456
    if (unlikely (command == NULL)) {
827
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
828
	goto CLEANUP_COMPOSITE;
829
    }
830

            
831
456
    status = _command_init (surface,
832
			    &command->header, CAIRO_COMMAND_PAINT, op,
833
			    &composite);
834
456
    if (unlikely (status))
835
	goto CLEANUP_COMMAND;
836

            
837
456
    status = _cairo_pattern_init_snapshot (&command->source.base, source);
838
456
    if (unlikely (status))
839
	goto CLEANUP_COMMAND;
840

            
841
456
    status = _cairo_recording_surface_commit (surface, &command->header);
842
456
    if (unlikely (status))
843
	goto CLEANUP_SOURCE;
844

            
845
456
    _cairo_recording_surface_destroy_bbtree (surface);
846

            
847
456
    _cairo_composite_rectangles_fini (&composite);
848
456
    return CAIRO_STATUS_SUCCESS;
849

            
850
  CLEANUP_SOURCE:
851
    _cairo_pattern_fini (&command->source.base);
852
  CLEANUP_COMMAND:
853
    _cairo_clip_destroy (command->header.clip);
854
    free (command);
855
CLEANUP_COMPOSITE:
856
    _cairo_composite_rectangles_fini (&composite);
857
    return status;
858
}
859

            
860
static cairo_int_status_t
861
75
_cairo_recording_surface_mask (void			*abstract_surface,
862
			       cairo_operator_t		 op,
863
			       const cairo_pattern_t	*source,
864
			       const cairo_pattern_t	*mask,
865
			       const cairo_clip_t	*clip)
866
{
867
    cairo_status_t status;
868
75
    cairo_recording_surface_t *surface = abstract_surface;
869
    cairo_command_mask_t *command;
870
    cairo_composite_rectangles_t composite;
871

            
872
    TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
873

            
874
75
    status = _cairo_composite_rectangles_init_for_mask (&composite,
875
							&surface->base,
876
							op, source, mask,
877
							clip);
878
75
    if (unlikely (status))
879
	return status;
880

            
881
75
    command = _cairo_calloc (sizeof (cairo_command_mask_t));
882
75
    if (unlikely (command == NULL)) {
883
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
884
	goto CLEANUP_COMPOSITE;
885
    }
886

            
887
75
    status = _command_init (surface,
888
			    &command->header, CAIRO_COMMAND_MASK, op,
889
			    &composite);
890
75
    if (unlikely (status))
891
	goto CLEANUP_COMMAND;
892

            
893
75
    status = _cairo_pattern_init_snapshot (&command->source.base, source);
894
75
    if (unlikely (status))
895
	goto CLEANUP_COMMAND;
896

            
897
75
    status = _cairo_pattern_init_snapshot (&command->mask.base, mask);
898
75
    if (unlikely (status))
899
	goto CLEANUP_SOURCE;
900

            
901
75
    status = _cairo_recording_surface_commit (surface, &command->header);
902
75
    if (unlikely (status))
903
	goto CLEANUP_MASK;
904

            
905
75
    _cairo_recording_surface_destroy_bbtree (surface);
906

            
907
75
    _cairo_composite_rectangles_fini (&composite);
908
75
    return CAIRO_STATUS_SUCCESS;
909

            
910
  CLEANUP_MASK:
911
    _cairo_pattern_fini (&command->mask.base);
912
  CLEANUP_SOURCE:
913
    _cairo_pattern_fini (&command->source.base);
914
  CLEANUP_COMMAND:
915
    _cairo_clip_destroy (command->header.clip);
916
    free (command);
917
CLEANUP_COMPOSITE:
918
    _cairo_composite_rectangles_fini (&composite);
919
    return status;
920
}
921

            
922
static cairo_int_status_t
923
733
_cairo_recording_surface_stroke (void			*abstract_surface,
924
				 cairo_operator_t	 op,
925
				 const cairo_pattern_t	*source,
926
				 const cairo_path_fixed_t	*path,
927
				 const cairo_stroke_style_t	*style,
928
				 const cairo_matrix_t		*ctm,
929
				 const cairo_matrix_t		*ctm_inverse,
930
				 double			 tolerance,
931
				 cairo_antialias_t	 antialias,
932
				 const cairo_clip_t	*clip)
933
{
934
    cairo_status_t status;
935
733
    cairo_recording_surface_t *surface = abstract_surface;
936
    cairo_command_stroke_t *command;
937
    cairo_composite_rectangles_t composite;
938

            
939
    TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
940

            
941
733
    status = _cairo_composite_rectangles_init_for_stroke (&composite,
942
							  &surface->base,
943
							  op, source,
944
							  path, style, ctm,
945
							  clip);
946
733
    if (unlikely (status))
947
36
	return status;
948

            
949
697
    command = _cairo_calloc (sizeof (cairo_command_stroke_t));
950
697
    if (unlikely (command == NULL)) {
951
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
952
	goto CLEANUP_COMPOSITE;
953
    }
954

            
955
697
    status = _command_init (surface,
956
			    &command->header, CAIRO_COMMAND_STROKE, op,
957
			    &composite);
958
697
    if (unlikely (status))
959
	goto CLEANUP_COMMAND;
960

            
961
697
    status = _cairo_pattern_init_snapshot (&command->source.base, source);
962
697
    if (unlikely (status))
963
	goto CLEANUP_COMMAND;
964

            
965
697
    status = _cairo_path_fixed_init_copy (&command->path, path);
966
697
    if (unlikely (status))
967
	goto CLEANUP_SOURCE;
968

            
969
697
    status = _cairo_stroke_style_init_copy (&command->style, style);
970
697
    if (unlikely (status))
971
	goto CLEANUP_PATH;
972

            
973
697
    command->ctm = *ctm;
974
697
    command->ctm_inverse = *ctm_inverse;
975
697
    command->tolerance = tolerance;
976
697
    command->antialias = antialias;
977

            
978
697
    status = _cairo_recording_surface_commit (surface, &command->header);
979
697
    if (unlikely (status))
980
	goto CLEANUP_STYLE;
981

            
982
697
    _cairo_recording_surface_destroy_bbtree (surface);
983

            
984
697
    _cairo_composite_rectangles_fini (&composite);
985
697
    return CAIRO_STATUS_SUCCESS;
986

            
987
  CLEANUP_STYLE:
988
    _cairo_stroke_style_fini (&command->style);
989
  CLEANUP_PATH:
990
    _cairo_path_fixed_fini (&command->path);
991
  CLEANUP_SOURCE:
992
    _cairo_pattern_fini (&command->source.base);
993
  CLEANUP_COMMAND:
994
    _cairo_clip_destroy (command->header.clip);
995
    free (command);
996
CLEANUP_COMPOSITE:
997
    _cairo_composite_rectangles_fini (&composite);
998
    return status;
999
}
static cairo_int_status_t
1111
_cairo_recording_surface_fill (void			*abstract_surface,
			       cairo_operator_t		 op,
			       const cairo_pattern_t	*source,
			       const cairo_path_fixed_t	*path,
			       cairo_fill_rule_t	 fill_rule,
			       double			 tolerance,
			       cairo_antialias_t	 antialias,
			       const cairo_clip_t	*clip)
{
    cairo_status_t status;
1111
    cairo_recording_surface_t *surface = abstract_surface;
    cairo_command_fill_t *command;
    cairo_composite_rectangles_t composite;
    TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
1111
    status = _cairo_composite_rectangles_init_for_fill (&composite,
							&surface->base,
							op, source, path,
							clip);
1111
    if (unlikely (status))
	return status;
1111
    command = _cairo_calloc (sizeof (cairo_command_fill_t));
1111
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto CLEANUP_COMPOSITE;
    }
1111
    status =_command_init (surface,
			   &command->header, CAIRO_COMMAND_FILL, op,
			   &composite);
1111
    if (unlikely (status))
	goto CLEANUP_COMMAND;
1111
    status = _cairo_pattern_init_snapshot (&command->source.base, source);
1111
    if (unlikely (status))
	goto CLEANUP_COMMAND;
1111
    status = _cairo_path_fixed_init_copy (&command->path, path);
1111
    if (unlikely (status))
	goto CLEANUP_SOURCE;
1111
    command->fill_rule = fill_rule;
1111
    command->tolerance = tolerance;
1111
    command->antialias = antialias;
1111
    status = _cairo_recording_surface_commit (surface, &command->header);
1111
    if (unlikely (status))
	goto CLEANUP_PATH;
1111
    _cairo_recording_surface_destroy_bbtree (surface);
1111
    _cairo_composite_rectangles_fini (&composite);
1111
    return CAIRO_STATUS_SUCCESS;
  CLEANUP_PATH:
    _cairo_path_fixed_fini (&command->path);
  CLEANUP_SOURCE:
    _cairo_pattern_fini (&command->source.base);
  CLEANUP_COMMAND:
    _cairo_clip_destroy (command->header.clip);
    free (command);
CLEANUP_COMPOSITE:
    _cairo_composite_rectangles_fini (&composite);
    return status;
}
static cairo_bool_t
513
_cairo_recording_surface_has_show_text_glyphs (void *abstract_surface)
{
513
    return TRUE;
}
static cairo_int_status_t
303
_cairo_recording_surface_show_text_glyphs (void				*abstract_surface,
					   cairo_operator_t		 op,
					   const cairo_pattern_t	*source,
					   const char			*utf8,
					   int				 utf8_len,
					   cairo_glyph_t		*glyphs,
					   int				 num_glyphs,
					   const cairo_text_cluster_t	*clusters,
					   int				 num_clusters,
					   cairo_text_cluster_flags_t	 cluster_flags,
					   cairo_scaled_font_t		*scaled_font,
					   const cairo_clip_t		*clip)
{
    cairo_status_t status;
303
    cairo_recording_surface_t *surface = abstract_surface;
    cairo_command_show_text_glyphs_t *command;
    cairo_composite_rectangles_t composite;
    TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
303
    status = _cairo_composite_rectangles_init_for_glyphs (&composite,
							  &surface->base,
							  op, source,
							  scaled_font,
							  glyphs, num_glyphs,
							  clip,
							  NULL);
303
    if (unlikely (status))
6
	return status;
297
    command = _cairo_calloc (sizeof (cairo_command_show_text_glyphs_t));
297
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto CLEANUP_COMPOSITE;
    }
297
    status = _command_init (surface,
			    &command->header, CAIRO_COMMAND_SHOW_TEXT_GLYPHS,
			    op, &composite);
297
    if (unlikely (status))
	goto CLEANUP_COMMAND;
297
    status = _cairo_pattern_init_snapshot (&command->source.base, source);
297
    if (unlikely (status))
	goto CLEANUP_COMMAND;
297
    command->utf8 = NULL;
297
    command->utf8_len = utf8_len;
297
    command->glyphs = NULL;
297
    command->num_glyphs = num_glyphs;
297
    command->clusters = NULL;
297
    command->num_clusters = num_clusters;
297
    if (utf8_len) {
210
	command->utf8 = _cairo_malloc (utf8_len);
210
	if (unlikely (command->utf8 == NULL)) {
	    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    goto CLEANUP_ARRAYS;
	}
210
	memcpy (command->utf8, utf8, utf8_len);
    }
297
    if (num_glyphs) {
297
	command->glyphs = _cairo_malloc_ab (num_glyphs, sizeof (glyphs[0]));
297
	if (unlikely (command->glyphs == NULL)) {
	    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    goto CLEANUP_ARRAYS;
	}
297
	memcpy (command->glyphs, glyphs, sizeof (glyphs[0]) * num_glyphs);
    }
297
    if (num_clusters) {
210
	command->clusters = _cairo_malloc_ab (num_clusters, sizeof (clusters[0]));
210
	if (unlikely (command->clusters == NULL)) {
	    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    goto CLEANUP_ARRAYS;
	}
210
	memcpy (command->clusters, clusters, sizeof (clusters[0]) * num_clusters);
    }
297
    command->cluster_flags = cluster_flags;
297
    status = scaled_font->status;
297
    if (unlikely (status))
	goto CLEANUP_ARRAYS;
297
    command->scaled_font = cairo_scaled_font_reference (scaled_font);
297
    status = _cairo_recording_surface_commit (surface, &command->header);
297
    if (unlikely (status))
	goto CLEANUP_SCALED_FONT;
297
    _cairo_composite_rectangles_fini (&composite);
297
    return CAIRO_STATUS_SUCCESS;
  CLEANUP_SCALED_FONT:
    cairo_scaled_font_destroy (command->scaled_font);
  CLEANUP_ARRAYS:
    free (command->utf8);
    free (command->glyphs);
    free (command->clusters);
    _cairo_pattern_fini (&command->source.base);
  CLEANUP_COMMAND:
    _cairo_clip_destroy (command->header.clip);
    free (command);
CLEANUP_COMPOSITE:
    _cairo_composite_rectangles_fini (&composite);
    return status;
}
static cairo_int_status_t
6
_cairo_recording_surface_tag (void			 *abstract_surface,
			      cairo_bool_t                begin,
			      const char                 *tag_name,
			      const char                 *attributes)
{
    cairo_status_t status;
6
    cairo_recording_surface_t *surface = abstract_surface;
    cairo_command_tag_t *command;
    TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
6
    surface->has_tags = TRUE;
6
    command = _cairo_calloc (sizeof (cairo_command_tag_t));
6
    if (unlikely (command == NULL)) {
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }
6
    status = _command_init (surface,
			    &command->header, CAIRO_COMMAND_TAG, CAIRO_OPERATOR_SOURCE,
			    NULL);
6
    if (unlikely (status))
	goto CLEANUP_COMMAND;
6
    command->begin = begin;
6
    command->tag_name = strdup (tag_name);
6
    if (unlikely (command->tag_name == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto CLEANUP_COMMAND;
    }
6
    if (begin) {
3
	if (attributes) {
3
	    command->attributes = strdup (attributes);
3
	    if (unlikely (command->attributes == NULL)) {
		status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
		goto CLEANUP_STRINGS;
	    }
	}
    }
6
    status = _cairo_recording_surface_commit (surface, &command->header);
6
    if (unlikely (status))
	goto CLEANUP_STRINGS;
6
    _cairo_recording_surface_destroy_bbtree (surface);
6
    return CAIRO_STATUS_SUCCESS;
  CLEANUP_STRINGS:
    free (command->tag_name);
    free (command->attributes);
  CLEANUP_COMMAND:
    _cairo_clip_destroy (command->header.clip);
    free (command);
    return status;
}
static cairo_bool_t
_cairo_recording_surface_supports_color_glyph (void                 *abstract_surface,
                                               cairo_scaled_font_t  *scaled_font,
                                               unsigned long         glyph_index)
{
    return TRUE;
}
static void
432
_command_init_copy (cairo_recording_surface_t *surface,
		    cairo_command_header_t *dst,
		    const cairo_command_header_t *src)
{
432
    dst->type = src->type;
432
    dst->op = src->op;
432
    dst->extents = src->extents;
432
    dst->chain = NULL;
432
    dst->index = surface->commands.num_elements;
432
    dst->clip = _cairo_clip_copy (src->clip);
432
}
static cairo_status_t
36
_cairo_recording_surface_copy__paint (cairo_recording_surface_t *surface,
				      const cairo_command_t *src)
{
    cairo_command_paint_t *command;
    cairo_status_t status;
36
    command = _cairo_calloc (sizeof (*command));
36
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto err;
    }
36
    _command_init_copy (surface, &command->header, &src->header);
36
    status = _cairo_pattern_init_copy (&command->source.base,
				       &src->paint.source.base);
36
    if (unlikely (status))
	goto err_command;
36
    status = _cairo_recording_surface_commit (surface, &command->header);
36
    if (unlikely (status))
	goto err_source;
36
    return CAIRO_STATUS_SUCCESS;
err_source:
    _cairo_pattern_fini (&command->source.base);
err_command:
    free(command);
err:
    return status;
}
static cairo_status_t
_cairo_recording_surface_copy__mask (cairo_recording_surface_t *surface,
				     const cairo_command_t *src)
{
    cairo_command_mask_t *command;
    cairo_status_t status;
    command = _cairo_calloc (sizeof (*command));
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto err;
    }
    _command_init_copy (surface, &command->header, &src->header);
    status = _cairo_pattern_init_copy (&command->source.base,
				       &src->mask.source.base);
    if (unlikely (status))
	goto err_command;
    status = _cairo_pattern_init_copy (&command->mask.base,
				       &src->mask.mask.base);
    if (unlikely (status))
	goto err_source;
    status = _cairo_recording_surface_commit (surface, &command->header);
    if (unlikely (status))
	goto err_mask;
    return CAIRO_STATUS_SUCCESS;
err_mask:
    _cairo_pattern_fini (&command->mask.base);
err_source:
    _cairo_pattern_fini (&command->source.base);
err_command:
    free(command);
err:
    return status;
}
static cairo_status_t
6
_cairo_recording_surface_copy__stroke (cairo_recording_surface_t *surface,
				     const cairo_command_t *src)
{
    cairo_command_stroke_t *command;
    cairo_status_t status;
6
    command = _cairo_calloc (sizeof (*command));
6
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto err;
    }
6
    _command_init_copy (surface, &command->header, &src->header);
6
    status = _cairo_pattern_init_copy (&command->source.base,
				       &src->stroke.source.base);
6
    if (unlikely (status))
	goto err_command;
6
    status = _cairo_path_fixed_init_copy (&command->path, &src->stroke.path);
6
    if (unlikely (status))
	goto err_source;
6
    status = _cairo_stroke_style_init_copy (&command->style,
					    &src->stroke.style);
6
    if (unlikely (status))
	goto err_path;
6
    command->ctm = src->stroke.ctm;
6
    command->ctm_inverse = src->stroke.ctm_inverse;
6
    command->tolerance = src->stroke.tolerance;
6
    command->antialias = src->stroke.antialias;
6
    status = _cairo_recording_surface_commit (surface, &command->header);
6
    if (unlikely (status))
	goto err_style;
6
    return CAIRO_STATUS_SUCCESS;
err_style:
    _cairo_stroke_style_fini (&command->style);
err_path:
    _cairo_path_fixed_fini (&command->path);
err_source:
    _cairo_pattern_fini (&command->source.base);
err_command:
    free(command);
err:
    return status;
}
static cairo_status_t
366
_cairo_recording_surface_copy__fill (cairo_recording_surface_t *surface,
				     const cairo_command_t *src)
{
    cairo_command_fill_t *command;
    cairo_status_t status;
366
    command = _cairo_calloc (sizeof (*command));
366
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto err;
    }
366
    _command_init_copy (surface, &command->header, &src->header);
366
    status = _cairo_pattern_init_copy (&command->source.base,
				       &src->fill.source.base);
366
    if (unlikely (status))
	goto err_command;
366
    status = _cairo_path_fixed_init_copy (&command->path, &src->fill.path);
366
    if (unlikely (status))
	goto err_source;
366
    command->fill_rule = src->fill.fill_rule;
366
    command->tolerance = src->fill.tolerance;
366
    command->antialias = src->fill.antialias;
366
    status = _cairo_recording_surface_commit (surface, &command->header);
366
    if (unlikely (status))
	goto err_path;
366
    return CAIRO_STATUS_SUCCESS;
err_path:
    _cairo_path_fixed_fini (&command->path);
err_source:
    _cairo_pattern_fini (&command->source.base);
err_command:
    free(command);
err:
    return status;
}
static cairo_status_t
24
_cairo_recording_surface_copy__glyphs (cairo_recording_surface_t *surface,
				       const cairo_command_t *src)
{
    cairo_command_show_text_glyphs_t *command;
    cairo_status_t status;
24
    command = _cairo_calloc (sizeof (*command));
24
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto err;
    }
24
    _command_init_copy (surface, &command->header, &src->header);
24
    status = _cairo_pattern_init_copy (&command->source.base,
				       &src->show_text_glyphs.source.base);
24
    if (unlikely (status))
	goto err_command;
24
    command->utf8 = NULL;
24
    command->utf8_len = src->show_text_glyphs.utf8_len;
24
    command->glyphs = NULL;
24
    command->num_glyphs = src->show_text_glyphs.num_glyphs;
24
    command->clusters = NULL;
24
    command->num_clusters = src->show_text_glyphs.num_clusters;
24
    if (command->utf8_len) {
24
	command->utf8 = _cairo_malloc (command->utf8_len);
24
	if (unlikely (command->utf8 == NULL)) {
	    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    goto err_arrays;
	}
24
	memcpy (command->utf8, src->show_text_glyphs.utf8, command->utf8_len);
    }
24
    if (command->num_glyphs) {
24
	command->glyphs = _cairo_malloc_ab (command->num_glyphs,
					    sizeof (command->glyphs[0]));
24
	if (unlikely (command->glyphs == NULL)) {
	    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    goto err_arrays;
	}
24
	memcpy (command->glyphs, src->show_text_glyphs.glyphs,
24
		sizeof (command->glyphs[0]) * command->num_glyphs);
    }
24
    if (command->num_clusters) {
24
	command->clusters = _cairo_malloc_ab (command->num_clusters,
					      sizeof (command->clusters[0]));
24
	if (unlikely (command->clusters == NULL)) {
	    status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	    goto err_arrays;
	}
24
	memcpy (command->clusters, src->show_text_glyphs.clusters,
24
		sizeof (command->clusters[0]) * command->num_clusters);
    }
24
    command->cluster_flags = src->show_text_glyphs.cluster_flags;
24
    command->scaled_font =
24
	cairo_scaled_font_reference (src->show_text_glyphs.scaled_font);
24
    status = _cairo_recording_surface_commit (surface, &command->header);
24
    if (unlikely (status))
	goto err_arrays;
24
    return CAIRO_STATUS_SUCCESS;
err_arrays:
    free (command->utf8);
    free (command->glyphs);
    free (command->clusters);
    _cairo_pattern_fini (&command->source.base);
err_command:
    free(command);
err:
    return status;
}
static cairo_status_t
_cairo_recording_surface_copy__tag (cairo_recording_surface_t *surface,
				    const cairo_command_t *src)
{
    cairo_command_tag_t *command;
    cairo_status_t status;
    command = _cairo_calloc (sizeof (*command));
    if (unlikely (command == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto err;
    }
    _command_init_copy (surface, &command->header, &src->header);
    command->begin = src->tag.begin;
    command->tag_name = strdup (src->tag.tag_name);
    if (unlikely (command->tag_name == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto err_command;
    }
    if (src->tag.begin) {
	if (src->tag.attributes) {
	    command->attributes = strdup (src->tag.attributes);
	    if (unlikely (command->attributes == NULL)) {
		status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
		goto err_command;
	    }
	}
    }
    status = _cairo_recording_surface_commit (surface, &command->header);
    if (unlikely (status))
	goto err_command;
    return CAIRO_STATUS_SUCCESS;
err_command:
    free(command->tag_name);
    free(command->attributes);
    free(command);
err:
    return status;
}
static cairo_status_t
159
_cairo_recording_surface_copy (cairo_recording_surface_t *dst,
			       cairo_recording_surface_t *src)
{
    cairo_command_t **elements;
    int i, num_elements;
    cairo_status_t status;
159
    elements = _cairo_array_index (&src->commands, 0);
159
    num_elements = src->commands.num_elements;
591
    for (i = 0; i < num_elements; i++) {
432
	const cairo_command_t *command = elements[i];
432
	switch (command->header.type) {
36
	case CAIRO_COMMAND_PAINT:
36
	    status = _cairo_recording_surface_copy__paint (dst, command);
36
	    break;
	case CAIRO_COMMAND_MASK:
	    status = _cairo_recording_surface_copy__mask (dst, command);
	    break;
6
	case CAIRO_COMMAND_STROKE:
6
	    status = _cairo_recording_surface_copy__stroke (dst, command);
6
	    break;
366
	case CAIRO_COMMAND_FILL:
366
	    status = _cairo_recording_surface_copy__fill (dst, command);
366
	    break;
24
	case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
24
	    status = _cairo_recording_surface_copy__glyphs (dst, command);
24
	    break;
	case CAIRO_COMMAND_TAG:
	    status = _cairo_recording_surface_copy__tag (dst, command);
	    break;
	default:
	    ASSERT_NOT_REACHED;
	}
432
	if (unlikely (status))
	    return status;
    }
159
    return CAIRO_STATUS_SUCCESS;
}
/**
 * _cairo_recording_surface_snapshot:
 * @surface: a #cairo_surface_t which must be a recording surface
 *
 * Make an immutable copy of @surface. It is an error to call a
 * surface-modifying function on the result of this function.
 *
 * The caller owns the return value and should call
 * cairo_surface_destroy() when finished with it. This function will not
 * return %NULL, but will return a nil surface instead.
 *
 * Return value: The snapshot surface.
 **/
static cairo_surface_t *
159
_cairo_recording_surface_snapshot (void *abstract_other)
{
159
    cairo_recording_surface_t *other = abstract_other;
    cairo_recording_surface_t *surface;
    cairo_status_t status;
159
    surface = _cairo_calloc (sizeof (cairo_recording_surface_t));
159
    if (unlikely (surface == NULL))
	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
159
    _cairo_surface_init (&surface->base,
			 &cairo_recording_surface_backend,
			 NULL, /* device */
			 other->base.content,
159
			 other->base.is_vector);
159
    surface->extents_pixels = other->extents_pixels;
159
    surface->extents = other->extents;
159
    surface->unbounded = other->unbounded;
159
    surface->has_bilevel_alpha = other->has_bilevel_alpha;
159
    surface->has_only_op_over = other->has_only_op_over;
159
    surface->has_tags = other->has_tags;
159
    surface->base.is_clear = other->base.is_clear;
159
    surface->bbtree.left = surface->bbtree.right = NULL;
159
    surface->bbtree.chain = INVALID_CHAIN;
159
    surface->indices = NULL;
159
    surface->num_indices = 0;
159
    surface->optimize_clears = TRUE;
159
    CAIRO_MUTEX_INIT (surface->mutex);
159
    cairo_list_init (&surface->region_array_list);
159
    _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
159
    status = _cairo_recording_surface_copy (surface, other);
159
    if (unlikely (status)) {
	cairo_surface_destroy (&surface->base);
	return _cairo_surface_create_in_error (status);
    }
159
    return &surface->base;
}
static cairo_bool_t
1095768
_cairo_recording_surface_get_extents (void		    *abstract_surface,
				      cairo_rectangle_int_t *rectangle)
{
1095768
    cairo_recording_surface_t *surface = abstract_surface;
1095768
    if (surface->unbounded)
1050615
	return FALSE;
45153
    *rectangle = surface->extents;
45153
    return TRUE;
}
static const cairo_surface_backend_t cairo_recording_surface_backend = {
    CAIRO_SURFACE_TYPE_RECORDING,
    _cairo_recording_surface_finish,
    _cairo_default_context_create,
    _cairo_recording_surface_create_similar,
    NULL, /* create similar image */
    NULL, /* map to image */
    NULL, /* unmap image */
    _cairo_surface_default_source,
    _cairo_recording_surface_acquire_source_image,
    _cairo_recording_surface_release_source_image,
    _cairo_recording_surface_snapshot,
    NULL, /* copy_page */
    NULL, /* show_page */
    _cairo_recording_surface_get_extents,
    NULL, /* get_font_options */
    NULL, /* flush */
    NULL, /* mark_dirty_rectangle */
    /* Here are the 5 basic drawing operations, (which are in some
     * sense the only things that cairo_recording_surface should need to
     * implement).  However, we implement the more generic show_text_glyphs
     * instead of show_glyphs.  One or the other is eough. */
    _cairo_recording_surface_paint,
    _cairo_recording_surface_mask,
    _cairo_recording_surface_stroke,
    _cairo_recording_surface_fill,
    NULL, /* fill-stroke */
    NULL,
    _cairo_recording_surface_has_show_text_glyphs,
    _cairo_recording_surface_show_text_glyphs,
    NULL, /* get_supported_mime_types */
    _cairo_recording_surface_tag,
    _cairo_recording_surface_supports_color_glyph,
    NULL, /* analyze_recording_surface */
    NULL, /* command_id */
};
static unsigned int
42
_cairo_recording_surface_regions_allocate_unique_id (void)
{
    static cairo_atomic_int_t unique_id;
#if CAIRO_NO_MUTEX
    if (++unique_id == 0)
	unique_id = 1;
    return unique_id;
#else
    int old, id;
    do {
42
	old = _cairo_atomic_uint_get (&unique_id);
42
	id = old + 1;
42
	if (id == 0)
	    id = 1;
42
    } while (! _cairo_atomic_uint_cmpxchg (&unique_id, old, id));
42
    return id;
#endif
}
static cairo_recording_regions_array_t *
96
_cairo_recording_surface_region_array_find (cairo_recording_surface_t *surface,
					    unsigned int               id)
{
    cairo_recording_regions_array_t *regions;
100
    cairo_list_foreach_entry (regions, cairo_recording_regions_array_t,
			      &surface->region_array_list, link)
    {
100
	if (regions->id == id)
96
	    return regions;
    }
    return NULL;
}
/* Create and initialize a new #cairo_recording_regions_array_t. Attach
 * it to the recording surface and return its id
 */
cairo_status_t
42
_cairo_recording_surface_region_array_attach (cairo_surface_t *abstract_surface,
                                              unsigned int    *id)
{
    cairo_recording_regions_array_t *region_array;
42
    cairo_recording_surface_t *surface = (cairo_recording_surface_t *) abstract_surface;
42
    assert (_cairo_surface_is_recording (abstract_surface));
42
    region_array = _cairo_calloc (sizeof (cairo_recording_regions_array_t));
42
    if (region_array == NULL) {
	*id = 0;
        return _cairo_error (CAIRO_STATUS_NO_MEMORY);
    }
42
    region_array->id = _cairo_recording_surface_regions_allocate_unique_id ();
42
    CAIRO_REFERENCE_COUNT_INIT (&region_array->ref_count, 1);
42
    _cairo_array_init (&region_array->regions, sizeof (cairo_recording_region_element_t));
42
    CAIRO_MUTEX_LOCK (surface->mutex);
42
    cairo_list_add (&region_array->link, &surface->region_array_list);
42
    CAIRO_MUTEX_UNLOCK (surface->mutex);
42
    *id = region_array->id;
42
    return CAIRO_STATUS_SUCCESS;
}
void
24
_cairo_recording_surface_region_array_remove (cairo_surface_t *abstract_surface,
                                              unsigned int     id)
{
    cairo_recording_regions_array_t *region_array;
24
    cairo_recording_surface_t *surface = (cairo_recording_surface_t *) abstract_surface;
24
    if (id == 0)
	return;
24
    assert (_cairo_surface_is_recording (abstract_surface));
24
    CAIRO_MUTEX_LOCK (surface->mutex);
24
    region_array = _cairo_recording_surface_region_array_find (surface, id);
24
    if (region_array) {
24
	if (_cairo_reference_count_dec_and_test (&region_array->ref_count))
24
	    cairo_list_del (&region_array->link);
	else
	    region_array = NULL;
    }
24
    CAIRO_MUTEX_UNLOCK (surface->mutex);
24
    if (region_array)
24
	_cairo_recording_surface_region_array_destroy (surface, region_array);
}
void
_cairo_recording_surface_region_array_reference (cairo_surface_t *abstract_surface,
						 unsigned int     id)
{
    cairo_recording_regions_array_t *region_array;
    cairo_recording_surface_t *surface = (cairo_recording_surface_t *) abstract_surface;
    assert (_cairo_surface_is_recording (abstract_surface));
    CAIRO_MUTEX_LOCK (surface->mutex);
    region_array = _cairo_recording_surface_region_array_find (surface, id);
    if (region_array) {
	_cairo_reference_count_inc (&region_array->ref_count);
    }
    CAIRO_MUTEX_UNLOCK (surface->mutex);
}
cairo_int_status_t
327
_cairo_recording_surface_get_path (cairo_surface_t    *abstract_surface,
				   cairo_path_fixed_t *path)
{
    cairo_recording_surface_t *surface;
    cairo_command_t **elements;
    int i, num_elements;
    cairo_int_status_t status;
327
    if (unlikely (abstract_surface->status))
	return abstract_surface->status;
327
    surface = (cairo_recording_surface_t *) abstract_surface;
327
    status = CAIRO_STATUS_SUCCESS;
327
    num_elements = surface->commands.num_elements;
327
    elements = _cairo_array_index (&surface->commands, 0);
615
    for (i = 0; i < num_elements; i++) {
324
	cairo_command_t *command = elements[i];
324
	switch (command->header.type) {
36
	case CAIRO_COMMAND_PAINT:
	case CAIRO_COMMAND_MASK:
36
	    status = CAIRO_INT_STATUS_UNSUPPORTED;
36
	    break;
213
	case CAIRO_COMMAND_STROKE:
	{
	    cairo_traps_t traps;
213
	    _cairo_traps_init (&traps);
	    /* XXX call cairo_stroke_to_path() when that is implemented */
213
	    status = _cairo_path_fixed_stroke_polygon_to_traps (&command->stroke.path,
213
								&command->stroke.style,
213
								&command->stroke.ctm,
213
								&command->stroke.ctm_inverse,
								command->stroke.tolerance,
								&traps);
213
	    if (status == CAIRO_INT_STATUS_SUCCESS)
213
		status = _cairo_traps_path (&traps, path);
213
	    _cairo_traps_fini (&traps);
213
	    break;
	}
24
	case CAIRO_COMMAND_FILL:
	{
24
	    status = _cairo_path_fixed_append (path,
24
					       &command->fill.path,
					       0, 0);
24
	    break;
	}
51
	case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
	{
51
	    status = _cairo_scaled_font_glyph_path (command->show_text_glyphs.scaled_font,
51
						    command->show_text_glyphs.glyphs,
51
						    command->show_text_glyphs.num_glyphs,
						    path);
51
	    break;
	}
	case CAIRO_COMMAND_TAG:
	    break;
	default:
	    ASSERT_NOT_REACHED;
	}
324
	if (unlikely (status))
36
	    break;
    }
327
    return status;
}
static int
224596
_cairo_recording_surface_get_visible_commands (cairo_recording_surface_t *surface,
					       const cairo_rectangle_int_t *extents)
{
    unsigned int num_visible, *indices;
    cairo_box_t box;
224596
    if (surface->commands.num_elements == 0)
	return 0;
224596
    _cairo_box_from_rectangle (&box, extents);
224596
    if (surface->bbtree.chain == INVALID_CHAIN)
1318
	_cairo_recording_surface_create_bbtree (surface);
224596
    indices = surface->indices;
224596
    bbtree_foreach_mark_visible (&surface->bbtree, &box, &indices);
224596
    num_visible = indices - surface->indices;
224596
    if (num_visible > 1)
146688
	sort_indices (surface->indices, num_visible);
224596
    return num_visible;
}
static void
135
_cairo_recording_surface_merge_source_attributes (cairo_recording_surface_t  *surface,
						  cairo_operator_t            op,
						  const cairo_pattern_t      *source)
{
135
    if (op != CAIRO_OPERATOR_OVER)
84
	surface->has_only_op_over = FALSE;
135
    if (source->type == CAIRO_PATTERN_TYPE_SURFACE) {
27
	cairo_surface_pattern_t *surf_pat = (cairo_surface_pattern_t *) source;
27
	cairo_surface_t *surf = surf_pat->surface;
27
	cairo_surface_t *free_me = NULL;
27
	if (_cairo_surface_is_snapshot (surf))
27
	    free_me = surf = _cairo_surface_snapshot_get_target (surf);
27
	if (unlikely (surf->status))
	    // There was some kind of error and the surface could be a nil error
	    // surface with various "problems" (e.g. ->backend == NULL).
	    return;
27
	if (surf->type == CAIRO_SURFACE_TYPE_RECORDING) {
27
	    cairo_recording_surface_t *rec_surf = (cairo_recording_surface_t *) surf;
27
	    if (! _cairo_recording_surface_has_only_bilevel_alpha (rec_surf))
		surface->has_bilevel_alpha = FALSE;
27
	    if (! _cairo_recording_surface_has_only_op_over (rec_surf))
12
		surface->has_only_op_over = FALSE;
	} else if (surf->type == CAIRO_SURFACE_TYPE_IMAGE) {
	    cairo_image_surface_t *img_surf = (cairo_image_surface_t *) surf;
	    if (_cairo_image_analyze_transparency (img_surf) == CAIRO_IMAGE_HAS_ALPHA)
		surface->has_bilevel_alpha = FALSE;
	} else {
	    if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
		surface->has_bilevel_alpha = FALSE;
	}
27
	cairo_surface_destroy (free_me);
27
	return;
108
    } else if (source->type == CAIRO_PATTERN_TYPE_RASTER_SOURCE) {
	cairo_surface_t *image;
	cairo_surface_t *raster;
	image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
	raster = _cairo_raster_source_pattern_acquire (source, image, NULL);
	cairo_surface_destroy (image);
	if (raster) {
	    if (raster->type == CAIRO_SURFACE_TYPE_IMAGE) {
		if (_cairo_image_analyze_transparency ((cairo_image_surface_t *)raster) == CAIRO_IMAGE_HAS_ALPHA)
		    surface->has_bilevel_alpha = FALSE;
	    }
	    _cairo_raster_source_pattern_release (source, raster);
	    if (raster->type == CAIRO_SURFACE_TYPE_IMAGE)
		return;
	}
    }
108
    if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
36
	surface->has_bilevel_alpha = FALSE;
}
static cairo_status_t
237038
_cairo_recording_surface_replay_internal (cairo_recording_surface_t	*surface,
					  cairo_recording_surface_replay_params_t *params)
{
    cairo_surface_wrapper_t wrapper;
    cairo_command_t **elements;
237038
    cairo_recording_regions_array_t *regions_array = NULL;
237038
    cairo_recording_region_element_t *region_elements = NULL;
237038
    cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
    cairo_rectangle_int_t extents;
237038
    cairo_bool_t use_indices = FALSE;
    const cairo_rectangle_int_t *r;
    unsigned int i, num_elements;
237038
    if (unlikely (surface->base.status))
	return surface->base.status;
237038
    if (unlikely (params->target->status))
	return params->target->status;
237038
    if (unlikely (surface->base.finished))
	return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
237038
    if (surface->base.is_clear)
123
	return CAIRO_STATUS_SUCCESS;
236915
    assert (_cairo_surface_is_recording (&surface->base));
236915
    if (params->regions_id != 0) {
72
	regions_array = _cairo_recording_surface_region_array_find (surface, params->regions_id);
72
	assert (regions_array != NULL);
    }
236915
    _cairo_surface_wrapper_init (&wrapper, params->target);
236915
    if (params->surface_extents)
	_cairo_surface_wrapper_intersect_extents (&wrapper, params->surface_extents);
236915
    r = &_cairo_unbounded_rectangle;
236915
    if (! surface->unbounded && !params->surface_is_unbounded) {
11163
	_cairo_surface_wrapper_intersect_extents (&wrapper, &surface->extents);
11163
	r = &surface->extents;
    }
236915
    _cairo_surface_wrapper_set_inverse_transform (&wrapper, params->surface_transform);
236915
    _cairo_surface_wrapper_set_clip (&wrapper, params->target_clip);
236915
    if (params->foreground_color) {
327
	params->target->foreground_source = _cairo_pattern_create_solid (params->foreground_color);
327
	params->target->foreground_used = FALSE;
    }
    /* Compute the extents of the target clip in recorded device space */
236915
    if (! _cairo_surface_wrapper_get_target_extents (&wrapper, params->surface_is_unbounded, &extents))
8
	goto done;
236907
    surface->has_bilevel_alpha = TRUE;
236907
    surface->has_only_op_over = TRUE;
236907
    num_elements = surface->commands.num_elements;
236907
    if (regions_array) {
72
	if (params->type == CAIRO_RECORDING_CREATE_REGIONS) {
	    /* Re-running create regions with the same region id is not supported. */
39
	    assert (_cairo_array_num_elements (&regions_array->regions) == 0);
	    void *array_elems;
39
	    status = _cairo_array_allocate (&regions_array->regions, num_elements, &array_elems);
39
	    if (unlikely (status))
		return status;
	    /* Set regions to CAIRO_RECORDING_REGION_ALL and ids to 0 */
39
	    memset (array_elems, 0, num_elements * sizeof (cairo_recording_region_element_t));
	} else {
33
	    assert (_cairo_array_num_elements (&regions_array->regions) == num_elements);
	}
    }
236907
    elements = _cairo_array_index (&surface->commands, 0);
236907
    if (regions_array)
72
	region_elements = _cairo_array_index (&regions_array->regions, 0);
236907
    if (!params->replay_all && (extents.width < r->width || extents.height < r->height)) {
224596
	num_elements =
224596
	    _cairo_recording_surface_get_visible_commands (surface, &extents);
224596
	use_indices = num_elements != surface->commands.num_elements;
    }
236907
    cairo_bool_t target_is_analysis = _cairo_surface_is_analysis (params->target);
654857
    for (i = 0; i < num_elements; i++) {
417950
	cairo_command_t *command = elements[use_indices ? surface->indices[i] : i];
417950
	cairo_recording_region_element_t *region_element = NULL;
417950
	unsigned int source_region_id = 0;
417950
	unsigned int mask_region_id = 0;
417950
	if (region_elements)
260
	    region_element = &region_elements[use_indices ? surface->indices[i] : i];
417950
	if (region_element && params->type == CAIRO_RECORDING_REPLAY_REGION &&
125
            region_element->region != params->region)
	{
20
	    continue;
	}
417930
	if (! _cairo_rectangle_intersects (&extents, &command->header.extents)) {
6
	    if (command->header.type != CAIRO_COMMAND_TAG)
		continue;
	}
417930
	if (params->target->backend->command_id) {
1626
	    status = params->target->backend->command_id (params->target, params->regions_id, i);
1626
	    if (unlikely (status))
		return status;
	}
417930
	switch (command->header.type) {
266412
	case CAIRO_COMMAND_PAINT:
266412
	    if (region_element)
46
		source_region_id = region_element->source_id;
266412
	    status = _cairo_surface_wrapper_paint (&wrapper,
						   command->header.op,
266412
						   &command->paint.source.base,
						   source_region_id,
266412
						   command->header.clip);
266412
	    if (params->type == CAIRO_RECORDING_CREATE_REGIONS) {
27
		_cairo_recording_surface_merge_source_attributes (surface,
								  command->header.op,
27
								  &command->paint.source.base);
27
		if (region_element && target_is_analysis)
27
		    region_element->source_id = _cairo_analysis_surface_get_source_region_id (params->target);
	    }
266412
	    break;
2976
	case CAIRO_COMMAND_MASK:
2976
	    if (region_element) {
		source_region_id = region_element->source_id;
		mask_region_id = region_element->mask_id;
	    }
2976
	    status = _cairo_surface_wrapper_mask (&wrapper,
						  command->header.op,
2976
						  &command->mask.source.base,
						  source_region_id,
2976
						  &command->mask.mask.base,
						  mask_region_id,
2976
						  command->header.clip);
2976
	    if (params->type == CAIRO_RECORDING_CREATE_REGIONS) {
		_cairo_recording_surface_merge_source_attributes (surface,
								  command->header.op,
								  &command->mask.source.base);
		_cairo_recording_surface_merge_source_attributes (surface,
								  command->header.op,
								  &command->mask.mask.base);
		if (region_element && target_is_analysis) {
		    region_element->source_id = _cairo_analysis_surface_get_source_region_id (params->target);
		    region_element->mask_id = _cairo_analysis_surface_get_mask_region_id (params->target);
		}
	    }
2976
	    break;
1771
	case CAIRO_COMMAND_STROKE:
1771
	    if (region_element)
		source_region_id = region_element->source_id;
1771
	    status = _cairo_surface_wrapper_stroke (&wrapper,
						    command->header.op,
1771
						    &command->stroke.source.base,
						    source_region_id,
1771
						    &command->stroke.path,
1771
						    &command->stroke.style,
1771
						    &command->stroke.ctm,
1771
						    &command->stroke.ctm_inverse,
						    command->stroke.tolerance,
						    command->stroke.antialias,
1771
						    command->header.clip);
1771
	    if (params->type == CAIRO_RECORDING_CREATE_REGIONS) {
		_cairo_recording_surface_merge_source_attributes (surface,
								  command->header.op,
								  &command->stroke.source.base);
		if (region_element && target_is_analysis)
		    region_element->source_id = _cairo_analysis_surface_get_source_region_id (params->target);
	    }
1771
	    break;
111101
	case CAIRO_COMMAND_FILL:
111101
	    status = CAIRO_INT_STATUS_UNSUPPORTED;
111101
	    if (region_element)
194
		source_region_id = region_element->source_id;
111101
	    if (_cairo_surface_wrapper_has_fill_stroke (&wrapper)) {
50
		cairo_command_t *stroke_command = NULL;
50
		cairo_recording_region_element_t *stroke_region_element = NULL;
50
		unsigned stroke_region_id = 0;
		/* The analysis surface does not implement
		 * fill_stroke.  When creating regions the fill and
		 * stroke commands are tested separately.
		 */
50
		if (params->type != CAIRO_RECORDING_CREATE_REGIONS && i < num_elements - 1) {
41
		    stroke_command = elements[i + 1];
41
		    if (region_elements)
41
			stroke_region_element = &region_elements[i + 1];
		}
50
		if (stroke_region_element)
41
		    stroke_region_id = stroke_region_element->source_id;
50
		if (stroke_command && stroke_region_element &&
41
		    params->type == CAIRO_RECORDING_REPLAY_REGION &&
41
		    params->region != CAIRO_RECORDING_REGION_ALL)
		{
41
		    if (stroke_region_element->region != params->region)
5
			stroke_command = NULL;
		}
50
		if (stroke_command != NULL &&
36
		    stroke_command->header.type == CAIRO_COMMAND_STROKE &&
		    _cairo_path_fixed_equal (&command->fill.path,
					     &stroke_command->stroke.path) &&
		    _cairo_clip_equal (command->header.clip,
				       stroke_command->header.clip))
		{
		    status = _cairo_surface_wrapper_fill_stroke (&wrapper,
								 command->header.op,
								 &command->fill.source.base,
								 source_region_id,
								 command->fill.fill_rule,
								 command->fill.tolerance,
								 command->fill.antialias,
								 &command->fill.path,
								 stroke_command->header.op,
								 &stroke_command->stroke.source.base,
								 stroke_region_id,
								 &stroke_command->stroke.style,
								 &stroke_command->stroke.ctm,
								 &stroke_command->stroke.ctm_inverse,
								 stroke_command->stroke.tolerance,
								 stroke_command->stroke.antialias,
								 command->header.clip);
		    if (params->type == CAIRO_RECORDING_CREATE_REGIONS) {
			_cairo_recording_surface_merge_source_attributes (surface,
									  command->header.op,
									  &command->fill.source.base);
			_cairo_recording_surface_merge_source_attributes (surface,
									  command->header.op,
									  &command->stroke.source.base);
		    }
		    i++;
		}
	    }
111101
	    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
111101
		status = _cairo_surface_wrapper_fill (&wrapper,
						      command->header.op,
111101
						      &command->fill.source.base,
						      source_region_id,
111101
						      &command->fill.path,
						      command->fill.fill_rule,
						      command->fill.tolerance,
						      command->fill.antialias,
111101
						      command->header.clip);
111101
		if (params->type == CAIRO_RECORDING_CREATE_REGIONS) {
108
		    _cairo_recording_surface_merge_source_attributes (surface,
								      command->header.op,
108
								      &command->fill.source.base);
108
		if (region_element && target_is_analysis)
108
		    region_element->source_id = _cairo_analysis_surface_get_source_region_id (params->target);
		}
	    }
111101
	    break;
35664
	case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
35664
	    if (region_element)
		source_region_id = region_element->source_id;
35664
	    status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
							      command->header.op,
35664
							      &command->show_text_glyphs.source.base,
							      source_region_id,
35664
							      command->show_text_glyphs.utf8, command->show_text_glyphs.utf8_len,
35664
							      command->show_text_glyphs.glyphs, command->show_text_glyphs.num_glyphs,
35664
							      command->show_text_glyphs.clusters, command->show_text_glyphs.num_clusters,
							      command->show_text_glyphs.cluster_flags,
							      command->show_text_glyphs.scaled_font,
35664
							      command->header.clip);
35664
	    if (params->type == CAIRO_RECORDING_CREATE_REGIONS) {
		_cairo_recording_surface_merge_source_attributes (surface,
								  command->header.op,
								  &command->show_text_glyphs.source.base);
		if (region_element && target_is_analysis)
		    region_element->source_id = _cairo_analysis_surface_get_source_region_id (params->target);
	    }
35664
	    break;
6
	case CAIRO_COMMAND_TAG:
6
	    status = _cairo_surface_wrapper_tag (&wrapper,
						 command->tag.begin,
6
						 command->tag.tag_name,
6
						 command->tag.attributes);
6
	    break;
	default:
	    ASSERT_NOT_REACHED;
	}
	/* It's possible that a degenerate clip caused the command to end up doing nothing when replayed. */
417930
	if (unlikely (status == CAIRO_INT_STATUS_NOTHING_TO_DO))
210
	    status = CAIRO_INT_STATUS_SUCCESS;
417930
	if (params->type == CAIRO_RECORDING_CREATE_REGIONS && region_element) {
135
	    if (status == CAIRO_INT_STATUS_SUCCESS) {
106
		region_element->region = CAIRO_RECORDING_REGION_NATIVE;
29
	    } else if (status == CAIRO_INT_STATUS_IMAGE_FALLBACK) {
29
		region_element->region = CAIRO_RECORDING_REGION_IMAGE_FALLBACK;
29
		status = CAIRO_INT_STATUS_SUCCESS;
	    } else {
		assert (_cairo_int_status_is_error (status));
	    }
	}
417930
	if (unlikely (status))
	    break;
    }
236907
done:
236915
    if (params->foreground_color) {
327
	cairo_pattern_destroy (params->target->foreground_source);
327
	params->target->foreground_source = NULL;
327
	params->foreground_used = params->target->foreground_used;
    }
236915
    _cairo_surface_wrapper_fini (&wrapper);
236915
    return _cairo_surface_set_error (&surface->base, status);
}
cairo_status_t
_cairo_recording_surface_replay_one (cairo_recording_surface_t	*surface,
				     long unsigned index,
				     cairo_surface_t	     *target)
{
    cairo_surface_wrapper_t wrapper;
    cairo_command_t **elements, *command;
    cairo_int_status_t status = CAIRO_INT_STATUS_SUCCESS;
    if (unlikely (surface->base.status))
	return surface->base.status;
    if (unlikely (target->status))
	return target->status;
    if (unlikely (surface->base.finished))
	return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
    assert (_cairo_surface_is_recording (&surface->base));
    /* XXX
     * Use a surface wrapper because we may want to do transformed
     * replay in the future.
     */
    _cairo_surface_wrapper_init (&wrapper, target);
    if (index > surface->commands.num_elements)
	return _cairo_error (CAIRO_STATUS_READ_ERROR);
    elements = _cairo_array_index (&surface->commands, 0);
    command = elements[index];
    switch (command->header.type) {
    case CAIRO_COMMAND_PAINT:
	status = _cairo_surface_wrapper_paint (&wrapper,
					       command->header.op,
					       &command->paint.source.base,
					       0,
					       command->header.clip);
	break;
    case CAIRO_COMMAND_MASK:
	status = _cairo_surface_wrapper_mask (&wrapper,
					      command->header.op,
					      &command->mask.source.base,
					      0,
					      &command->mask.mask.base,
					      0,
					      command->header.clip);
	break;
    case CAIRO_COMMAND_STROKE:
	status = _cairo_surface_wrapper_stroke (&wrapper,
						command->header.op,
						&command->stroke.source.base,
						0,
						&command->stroke.path,
						&command->stroke.style,
						&command->stroke.ctm,
						&command->stroke.ctm_inverse,
						command->stroke.tolerance,
						command->stroke.antialias,
						command->header.clip);
	break;
    case CAIRO_COMMAND_FILL:
	status = _cairo_surface_wrapper_fill (&wrapper,
					      command->header.op,
					      &command->fill.source.base,
					      0,
					      &command->fill.path,
					      command->fill.fill_rule,
					      command->fill.tolerance,
					      command->fill.antialias,
					      command->header.clip);
	break;
    case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
	status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
							  command->header.op,
							  &command->show_text_glyphs.source.base,
							  0,
							  command->show_text_glyphs.utf8, command->show_text_glyphs.utf8_len,
							  command->show_text_glyphs.glyphs, command->show_text_glyphs.num_glyphs,
							  command->show_text_glyphs.clusters, command->show_text_glyphs.num_clusters,
							  command->show_text_glyphs.cluster_flags,
							  command->show_text_glyphs.scaled_font,
							  command->header.clip);
	break;
    case CAIRO_COMMAND_TAG:
	status = _cairo_surface_wrapper_tag (&wrapper,
					     command->tag.begin,
					     command->tag.tag_name,
					     command->tag.attributes);
	break;
    default:
	ASSERT_NOT_REACHED;
    }
    _cairo_surface_wrapper_fini (&wrapper);
    return _cairo_surface_set_error (&surface->base, status);
}
/**
 * _cairo_recording_surface_replay:
 * @surface: the #cairo_recording_surface_t
 * @target: a target #cairo_surface_t onto which to replay the operations
 * @width_pixels: width of the surface, in pixels
 * @height_pixels: height of the surface, in pixels
 *
 * A recording surface can be "replayed" against any target surface,
 * after which the results in target will be identical to the results
 * that would have been obtained if the original operations applied to
 * the recording surface had instead been applied to the target surface.
 **/
cairo_status_t
1827
_cairo_recording_surface_replay (cairo_surface_t *surface,
				 cairo_surface_t *target)
{
    cairo_recording_surface_replay_params_t params;
1827
    params.surface_extents = NULL;
1827
    params.surface_transform = NULL;
1827
    params.target = target;
1827
    params.target_clip = NULL;
1827
    params.surface_is_unbounded = FALSE;
1827
    params.type = CAIRO_RECORDING_REPLAY;
1827
    params.region = CAIRO_RECORDING_REGION_ALL;
1827
    params.regions_id = 0;
1827
    params.foreground_color = NULL;
1827
    params.replay_all = FALSE;
1827
    return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, &params);
}
cairo_status_t
348
_cairo_recording_surface_replay_with_foreground_color (cairo_surface_t     *surface,
                                                       cairo_surface_t     *target,
						       const cairo_color_t *foreground_color,
						       cairo_bool_t        *foreground_used)
{
    cairo_recording_surface_replay_params_t params;
    cairo_status_t status;
348
    params.surface_extents = NULL;
348
    params.surface_transform = NULL;
348
    params.target = target;
348
    params.target_clip = NULL;
348
    params.surface_is_unbounded = FALSE;
348
    params.type = CAIRO_RECORDING_REPLAY;
348
    params.region = CAIRO_RECORDING_REGION_ALL;
348
    params.regions_id = 0;
348
    params.foreground_color = foreground_color;
348
    params.foreground_used = FALSE;
348
    params.replay_all = FALSE;
348
    status = _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, &params);
348
    *foreground_used = params.foreground_used;
348
    return status;
}
cairo_status_t
140
_cairo_recording_surface_replay_with_transform (cairo_surface_t *surface,
						const cairo_matrix_t *surface_transform,
						cairo_surface_t *target,
						cairo_bool_t surface_is_unbounded,
						cairo_bool_t replay_all)
{
    cairo_recording_surface_replay_params_t params;
140
    params.surface_extents = NULL;
140
    params.surface_transform = surface_transform;
140
    params.target = target;
140
    params.target_clip = NULL;
140
    params.surface_is_unbounded = surface_is_unbounded;
140
    params.type = CAIRO_RECORDING_REPLAY;
140
    params.region = CAIRO_RECORDING_REGION_ALL;
140
    params.regions_id = 0;
140
    params.foreground_color = NULL;
140
    params.replay_all = replay_all;
140
    return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, &params);
}
cairo_status_t
234648
_cairo_recording_surface_replay_with_clip (cairo_surface_t *surface,
					   const cairo_matrix_t *surface_transform,
					   cairo_surface_t *target,
					   const cairo_clip_t *target_clip)
{
    cairo_recording_surface_replay_params_t params;
234648
    params.surface_extents = NULL;
234648
    params.surface_transform = surface_transform;
234648
    params.target = target;
234648
    params.target_clip = target_clip;
234648
    params.surface_is_unbounded = FALSE;
234648
    params.type = CAIRO_RECORDING_REPLAY;
234648
    params.region = CAIRO_RECORDING_REGION_ALL;
234648
    params.regions_id = 0;
234648
    params.foreground_color = NULL;
234648
    params.replay_all = FALSE;
234648
    return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, &params);
}
/* Replay recording to surface. When the return status of each operation is
 * one of %CAIRO_STATUS_SUCCESS, %CAIRO_INT_STATUS_UNSUPPORTED, or
 * %CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY the status of each operation
 * will be stored in the recording surface. Any other status will abort the
 * replay and return the status.
 */
cairo_status_t
42
_cairo_recording_surface_replay_and_create_regions (cairo_surface_t *surface,
						    unsigned int regions_id,
						    const cairo_matrix_t *surface_transform,
						    cairo_surface_t *target,
						    cairo_bool_t surface_is_unbounded,
						    cairo_bool_t replay_all)
{
    cairo_recording_surface_replay_params_t params;
42
    params.surface_extents = NULL;
42
    params.surface_transform = surface_transform;
42
    params.target = target;
42
    params.target_clip = NULL;
42
    params.surface_is_unbounded = surface_is_unbounded;
42
    params.type = CAIRO_RECORDING_CREATE_REGIONS;
42
    params.region = CAIRO_RECORDING_REGION_ALL;
42
    params.regions_id = regions_id;
42
    params.foreground_color = NULL;
42
    params.replay_all = replay_all;
42
    return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, &params);
}
cairo_status_t
33
_cairo_recording_surface_replay_region (cairo_surface_t          *surface,
					unsigned int regions_id,
					const cairo_rectangle_int_t *surface_extents,
					cairo_surface_t          *target,
					cairo_recording_region_type_t  region)
{
    cairo_recording_surface_replay_params_t params;
33
    params.surface_extents = surface_extents;
33
    params.surface_transform = NULL;
33
    params.target = target;
33
    params.target_clip = NULL;
33
    params.surface_is_unbounded = FALSE;
33
    params.type = CAIRO_RECORDING_REPLAY_REGION;
33
    params.region = region;
33
    params.regions_id = regions_id;
33
    params.foreground_color = NULL;
33
    params.replay_all = FALSE;
33
    return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, &params);
}
static cairo_status_t
1076
_recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
				 cairo_box_t *bbox,
				 const cairo_matrix_t *transform)
{
    cairo_surface_t *null_surface;
    cairo_surface_t *analysis_surface;
    cairo_status_t status;
1076
    null_surface = _cairo_null_surface_create (surface->base.content);
1076
    analysis_surface = _cairo_analysis_surface_create (null_surface, FALSE);
1076
    cairo_surface_destroy (null_surface);
1076
    status = analysis_surface->status;
1076
    if (unlikely (status))
	return status;
1076
    if (transform != NULL)
1008
	_cairo_analysis_surface_set_ctm (analysis_surface, transform);
1076
    status = _cairo_recording_surface_replay (&surface->base, analysis_surface);
1076
    _cairo_analysis_surface_get_bounding_box (analysis_surface, bbox);
1076
    cairo_surface_destroy (analysis_surface);
1076
    return status;
}
/**
 * cairo_recording_surface_ink_extents:
 * @surface: a #cairo_recording_surface_t
 * @x0: the x-coordinate of the top-left of the ink bounding box
 * @y0: the y-coordinate of the top-left of the ink bounding box
 * @width: the width of the ink bounding box
 * @height: the height of the ink bounding box
 *
 * Measures the extents of the operations stored within the recording-surface.
 * This is useful to compute the required size of an image surface (or
 * equivalent) into which to replay the full sequence of drawing operations.
 *
 * Since: 1.10
 **/
void
71
cairo_recording_surface_ink_extents (cairo_surface_t *surface,
				     double *x0,
				     double *y0,
				     double *width,
				     double *height)
{
    cairo_status_t status;
    cairo_box_t bbox;
71
    memset (&bbox, 0, sizeof (bbox));
71
    if (surface->status || ! _cairo_surface_is_recording (surface)) {
7
	_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
7
	goto DONE;
    }
64
    status = _recording_surface_get_ink_bbox ((cairo_recording_surface_t *) surface,
					 &bbox,
					 NULL);
64
    if (unlikely (status))
	status = _cairo_surface_set_error (surface, status);
64
DONE:
71
    if (x0)
71
	*x0 = _cairo_fixed_to_double (bbox.p1.x);
71
    if (y0)
71
	*y0 = _cairo_fixed_to_double (bbox.p1.y);
71
    if (width)
71
	*width = _cairo_fixed_to_double (bbox.p2.x - bbox.p1.x);
71
    if (height)
71
	*height = _cairo_fixed_to_double (bbox.p2.y - bbox.p1.y);
71
}
cairo_status_t
1008
_cairo_recording_surface_get_bbox (cairo_recording_surface_t *surface,
				   cairo_box_t *bbox,
				   const cairo_matrix_t *transform)
{
1008
    if (! surface->unbounded) {
	_cairo_box_from_rectangle (bbox, &surface->extents);
	if (transform != NULL)
	    _cairo_matrix_transform_bounding_box_fixed (transform, bbox, NULL);
	return CAIRO_STATUS_SUCCESS;
    }
1008
    return _recording_surface_get_ink_bbox (surface, bbox, transform);
}
cairo_status_t
4
_cairo_recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
				       cairo_box_t *bbox,
				       const cairo_matrix_t *transform)
{
4
    return _recording_surface_get_ink_bbox (surface, bbox, transform);
}
/**
 * cairo_recording_surface_get_extents:
 * @surface: a #cairo_recording_surface_t
 * @extents: the #cairo_rectangle_t to be assigned the extents
 *
 * Get the extents of the recording-surface.
 *
 * Return value: %TRUE if the surface is bounded, of recording type, and
 * not in an error state, otherwise %FALSE
 *
 * Since: 1.12
 **/
cairo_bool_t
cairo_recording_surface_get_extents (cairo_surface_t *surface,
				     cairo_rectangle_t *extents)
{
    cairo_recording_surface_t *record;
    if (surface->status || ! _cairo_surface_is_recording (surface)) {
	_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
	return FALSE;
    }
    record = (cairo_recording_surface_t *)surface;
    if (record->unbounded)
	return FALSE;
    *extents = record->extents_pixels;
    return TRUE;
}
cairo_bool_t
31
_cairo_recording_surface_has_only_bilevel_alpha (cairo_recording_surface_t *surface)
{
31
    return surface->has_bilevel_alpha;
}
cairo_bool_t
31
_cairo_recording_surface_has_only_op_over (cairo_recording_surface_t *surface)
{
31
    return surface->has_only_op_over;
}
cairo_bool_t
20
_cairo_recording_surface_has_tags (cairo_surface_t *surface)
{
    cairo_recording_surface_t *record;
20
    if (surface->status || ! _cairo_surface_is_recording (surface)) {
	_cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
	return FALSE;
    }
20
    record = (cairo_recording_surface_t *)surface;
20
    return record->has_tags;
}
static void
print_indent (FILE *file, int indent)
{
    fprintf (file, "%*s", indent * 2, "");
}
static void
print_pattern (FILE                  *file,
	       const cairo_pattern_t *pattern,
	       unsigned int           region_id,
	       int                    indent,
	       cairo_bool_t           recurse)
{
    switch (pattern->type) {
	case CAIRO_PATTERN_TYPE_SOLID: {
	    cairo_solid_pattern_t *p = (cairo_solid_pattern_t *) pattern;
	    if (pattern->is_foreground_marker) {
		fprintf (file, "solid foreground\n");
	    } else {
		fprintf (file, "solid rgba: %f %f %f %f\n",
			 p->color.red,
			 p->color.green,
			 p->color.blue,
			 p->color.alpha);
	    }
	} break;
	case CAIRO_PATTERN_TYPE_SURFACE: {
	    cairo_surface_pattern_t *p = (cairo_surface_pattern_t *) pattern;
	    fprintf (file, "surface ");
	    if (p->surface->type == CAIRO_SURFACE_TYPE_RECORDING) {
		fprintf (file, "recording id: %d\n", p->surface->unique_id);
		if (recurse) {
		    _cairo_debug_print_recording_surface (file, p->surface,
							  region_id,
							  indent + 1, recurse);
		}
	    } else if (p->surface->type == CAIRO_SURFACE_TYPE_IMAGE) {
		cairo_image_surface_t *image = (cairo_image_surface_t *)p->surface;
		fprintf (file, "image format: ");
		switch (image->format) {
		    case CAIRO_FORMAT_INVALID: fputs ("INVALID", file); break;
		    case CAIRO_FORMAT_ARGB32: fputs ("ARGB32", file); break;
		    case CAIRO_FORMAT_RGB24: fputs ("RGB24", file); break;
		    case CAIRO_FORMAT_A8: fputs ("A8", file); break;
		    case CAIRO_FORMAT_A1: fputs ("A1", file); break;
		    case CAIRO_FORMAT_RGB16_565: fputs ("RGB16_565", file); break;
		    case CAIRO_FORMAT_RGB30: fputs ("RGB30", file); break;
		    case CAIRO_FORMAT_RGB96F: fputs ("RGB96F", file); break;
		    case CAIRO_FORMAT_RGBA128F: fputs ("RGBA128F", file); break;
		}
		fprintf (file, " width: %d height: %d\n", image->width, image->height);
	    } else {
		fprintf (file, "type %d\n", p->surface->type);
	    }
	} break;
	case CAIRO_PATTERN_TYPE_LINEAR:
	    fprintf (file, "linear\n");
	    break;
	case CAIRO_PATTERN_TYPE_RADIAL:
	    fprintf (file, "radial\n");
	    break;
	case CAIRO_PATTERN_TYPE_MESH:
	    fprintf (file, "mesh\n");
	    break;
	case CAIRO_PATTERN_TYPE_RASTER_SOURCE:
	    fprintf (file, "raster\n");
	    break;
    }
}
void
_cairo_debug_print_recording_surface (FILE            *file,
				      cairo_surface_t *surface,
                                      unsigned int     regions_id,
				      int              indent,
				      cairo_bool_t     recurse)
{
    cairo_command_t **elements;
    cairo_recording_region_element_t *region_elements = NULL;
    unsigned int i, num_elements;
    cairo_recording_surface_t *recording_surface;
    cairo_surface_t *free_me = NULL;
    char common[100];
    if (_cairo_surface_is_snapshot (surface))
	free_me = surface = _cairo_surface_snapshot_get_target (surface);
    assert (_cairo_surface_is_recording (surface));
    recording_surface = (cairo_recording_surface_t *)surface;
    print_indent (file, indent);
    indent++;
    fprintf(file, "recording surface id: %d   regions id: %d\n", recording_surface->base.unique_id, regions_id);
    num_elements = recording_surface->commands.num_elements;
    elements = _cairo_array_index (&recording_surface->commands, 0);
    if (regions_id != 0) {
	cairo_recording_regions_array_t *regions_array;
	regions_array = _cairo_recording_surface_region_array_find (recording_surface, regions_id);
	assert (regions_array != NULL);
	assert (_cairo_array_num_elements (&regions_array->regions) == num_elements);
	region_elements = _cairo_array_index (&regions_array->regions, 0);
    }
    for (i = 0; i < num_elements; i++) {
	cairo_command_t *command = elements[i];
	unsigned int source_region_id = 0;
	unsigned int mask_region_id = 0;
	common[0] = 0;
	if (region_elements) {
	    cairo_recording_region_element_t *region_element = &region_elements[i];
	    strcpy (common, "region: ");
	    switch (region_element->region) {
		case CAIRO_RECORDING_REGION_ALL: strcat (common, "all"); break;
		case CAIRO_RECORDING_REGION_NATIVE: strcat (common, "native"); break;
		case CAIRO_RECORDING_REGION_IMAGE_FALLBACK: strcat (common, "fallback"); break;
	    }
	    source_region_id  = region_element->source_id;
	    mask_region_id = region_element->mask_id;
	}
	sprintf (common + strlen(common), " op: %s", _cairo_debug_operator_to_string (command->header.op));
	switch (command->header.type) {
	    case CAIRO_COMMAND_PAINT:
		print_indent (file, indent);
		fprintf(file, "%d PAINT %s source: ", i, common);
		print_pattern (file, &command->paint.source.base, source_region_id, indent + 1, recurse);
		break;
	    case CAIRO_COMMAND_MASK:
		print_indent (file, indent);
		fprintf(file, "%d MASK %s\n", i, common);
		print_indent (file, indent + 1);
		fprintf(file, "source: ");
		print_pattern (file, &command->mask.source.base, source_region_id, indent + 1, recurse);
		print_indent (file, indent + 1);
		fprintf(file, "mask: ");
		print_pattern (file, &command->mask.mask.base, mask_region_id, indent + 1, recurse);
		break;
	    case CAIRO_COMMAND_STROKE:
		print_indent (file, indent);
		fprintf(file, "%d STROKE %s source:", i, common);
		print_pattern (file, &command->stroke.source.base, source_region_id, indent + 1, recurse);
		break;
	    case CAIRO_COMMAND_FILL:
		print_indent (file, indent);
		fprintf(file, "%d FILL %s source: ", i, common);
		print_pattern (file, &command->fill.source.base, source_region_id, indent + 1, recurse);
		break;
	    case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
		print_indent (file, indent);
		fprintf(file, "%d SHOW_TEXT_GLYPHS %s font_type: ", i, common);
		switch (command->show_text_glyphs.scaled_font->backend->type) {
		    case CAIRO_FONT_TYPE_TOY: fputs ("toy", file); break;
		    case CAIRO_FONT_TYPE_FT: fputs ("ft", file); break;
		    case CAIRO_FONT_TYPE_WIN32: fputs ("win32", file); break;
		    case CAIRO_FONT_TYPE_QUARTZ: fputs ("quartz", file); break;
		    case CAIRO_FONT_TYPE_USER: fputs ("user", file); break;
		    case CAIRO_FONT_TYPE_DWRITE: fputs ("dwrite", file); break;
		}
		fprintf (file, " glyphs:");
		for (unsigned j = 0; j < command->show_text_glyphs.num_glyphs; j++)
		    fprintf (file, " %ld", command->show_text_glyphs.glyphs[j].index);
		fprintf (file, " source:");
		print_pattern (file, &command->show_text_glyphs.source.base, source_region_id, indent + 1, recurse);
		break;
	    case CAIRO_COMMAND_TAG:
		print_indent (file, indent);
		fprintf(file, "%d %s %s '%s'\n",
			i,
			command->tag.begin ? "BEGIN TAG" : "END TAG",
			command->tag.tag_name, command->tag.attributes);
		break;
	    default:
		ASSERT_NOT_REACHED;
	}
    }
    cairo_surface_destroy (free_me);
}