Fix bug in _cairo_gl_has_extension
[platform/core/graphics/cairo.git] / src / cairo-recording-surface.c
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-recording-surface-inline.h"
90 #include "cairo-surface-snapshot-inline.h"
91 #include "cairo-surface-wrapper-private.h"
92 #include "cairo-traps-private.h"
93
94 typedef enum {
95     CAIRO_RECORDING_REPLAY,
96     CAIRO_RECORDING_CREATE_REGIONS
97 } cairo_recording_replay_type_t;
98
99 static const cairo_surface_backend_t cairo_recording_surface_backend;
100
101 /**
102  * CAIRO_HAS_RECORDING_SURFACE:
103  *
104  * Defined if the recording surface backend is available.
105  * The recording surface backend is always built in.
106  * This macro was added for completeness in cairo 1.10.
107  *
108  * Since: 1.10
109  **/
110
111 /* Currently all recording surfaces do have a size which should be passed
112  * in as the maximum size of any target surface against which the
113  * recording-surface will ever be replayed.
114  *
115  * XXX: The naming of "pixels" in the size here is a misnomer. It's
116  * actually a size in whatever device-space units are desired (again,
117  * according to the intended replay target).
118  */
119
120 static int bbtree_left_or_right (struct bbtree *bbt,
121                                  const cairo_box_t *box)
122 {
123     int left, right;
124
125     if (bbt->left) {
126         cairo_box_t *e = &bbt->left->extents;
127         cairo_box_t b;
128
129         b.p1.x = MIN (e->p1.x, box->p1.x);
130         b.p1.y = MIN (e->p1.y, box->p1.y);
131         b.p2.x = MAX (e->p2.x, box->p2.x);
132         b.p2.y = MAX (e->p2.y, box->p2.y);
133
134         left = _cairo_fixed_integer_part (b.p2.x - b.p1.x) * _cairo_fixed_integer_part (b.p2.y - b.p1.y);
135         left -= _cairo_fixed_integer_part (e->p2.x - e->p1.x) * _cairo_fixed_integer_part (e->p2.y - e->p1.y);
136     } else
137         left = 0;
138
139     if (bbt->right) {
140         cairo_box_t *e = &bbt->right->extents;
141         cairo_box_t b;
142
143         b.p1.x = MIN (e->p1.x, box->p1.x);
144         b.p1.y = MIN (e->p1.y, box->p1.y);
145         b.p2.x = MAX (e->p2.x, box->p2.x);
146         b.p2.y = MAX (e->p2.y, box->p2.y);
147
148         right = _cairo_fixed_integer_part (b.p2.x - b.p1.x) * _cairo_fixed_integer_part (b.p2.y - b.p1.y);
149         right -= _cairo_fixed_integer_part (e->p2.x - e->p1.x) * _cairo_fixed_integer_part (e->p2.y - e->p1.y);
150     } else
151         right = 0;
152
153     return left <= right;
154 }
155
156 #define INVALID_CHAIN ((cairo_command_header_t *)-1)
157
158 static struct bbtree *
159 bbtree_new (const cairo_box_t *box, cairo_command_header_t *chain)
160 {
161     struct bbtree *bbt = malloc (sizeof (*bbt));
162     if (bbt == NULL)
163         return NULL;
164     bbt->extents = *box;
165     bbt->left = bbt->right = NULL;
166     bbt->chain = chain;
167     return bbt;
168 }
169
170 static void
171 bbtree_init (struct bbtree *bbt, cairo_command_header_t *header)
172 {
173     _cairo_box_from_rectangle (&bbt->extents, &header->extents);
174     bbt->chain = header;
175 }
176
177 static cairo_status_t
178 bbtree_add (struct bbtree *bbt,
179             cairo_command_header_t *header,
180             const cairo_box_t *box)
181 {
182     if (box->p1.x < bbt->extents.p1.x || box->p1.y < bbt->extents.p1.y ||
183         box->p2.x > bbt->extents.p2.x || box->p2.y > bbt->extents.p2.y)
184     {
185         if (bbt->chain) {
186             if (bbtree_left_or_right (bbt, &bbt->extents)) {
187                 if (bbt->left == NULL) {
188                     bbt->left = bbtree_new (&bbt->extents, bbt->chain);
189                     if (unlikely (bbt->left == NULL))
190                         return _cairo_error (CAIRO_STATUS_NO_MEMORY);
191                 } else
192                     bbtree_add (bbt->left, bbt->chain, &bbt->extents);
193             } else {
194                 if (bbt->right == NULL) {
195                     bbt->right = bbtree_new (&bbt->extents, bbt->chain);
196                     if (unlikely (bbt->right == NULL))
197                         return _cairo_error (CAIRO_STATUS_NO_MEMORY);
198                 } else
199                     bbtree_add (bbt->right, bbt->chain, &bbt->extents);
200             }
201
202             bbt->chain = NULL;
203         }
204
205         bbt->extents.p1.x = MIN (bbt->extents.p1.x, box->p1.x);
206         bbt->extents.p1.y = MIN (bbt->extents.p1.y, box->p1.y);
207         bbt->extents.p2.x = MAX (bbt->extents.p2.x, box->p2.x);
208         bbt->extents.p2.y = MAX (bbt->extents.p2.y, box->p2.y);
209     }
210
211     if (box->p1.x == bbt->extents.p1.x && box->p1.y == bbt->extents.p1.y &&
212         box->p2.x == bbt->extents.p2.x && box->p2.y == bbt->extents.p2.y)
213     {
214         cairo_command_header_t *last = header;
215         while (last->chain) /* expected to be infrequent */
216             last = last->chain;
217         last->chain = bbt->chain;
218         bbt->chain = header;
219         return CAIRO_STATUS_SUCCESS;
220     }
221
222     if (bbtree_left_or_right (bbt, box)) {
223         if (bbt->left == NULL) {
224             bbt->left = bbtree_new (box, header);
225             if (unlikely (bbt->left == NULL))
226                 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
227         } else
228             return bbtree_add (bbt->left, header, box);
229     } else {
230         if (bbt->right == NULL) {
231             bbt->right = bbtree_new (box, header);
232             if (unlikely (bbt->right == NULL))
233                 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
234         } else
235             return bbtree_add (bbt->right, header, box);
236     }
237
238     return CAIRO_STATUS_SUCCESS;
239 }
240
241 static void bbtree_del (struct bbtree *bbt)
242 {
243     if (bbt->left)
244         bbtree_del (bbt->left);
245     if (bbt->right)
246         bbtree_del (bbt->right);
247
248     free (bbt);
249 }
250
251 static cairo_bool_t box_outside (const cairo_box_t *a, const cairo_box_t *b)
252 {
253     return
254         a->p1.x >= b->p2.x || a->p1.y >= b->p2.y ||
255         a->p2.x <= b->p1.x || a->p2.y <= b->p1.y;
256 }
257
258 static void
259 bbtree_foreach_mark_visible (struct bbtree *bbt,
260                              const cairo_box_t *box,
261                              unsigned int **indices)
262 {
263     cairo_command_header_t *chain;
264
265     for (chain = bbt->chain; chain; chain = chain->chain)
266         *(*indices)++ = chain->index;
267
268     if (bbt->left && ! box_outside (box, &bbt->left->extents))
269         bbtree_foreach_mark_visible (bbt->left, box, indices);
270     if (bbt->right && ! box_outside (box, &bbt->right->extents))
271         bbtree_foreach_mark_visible (bbt->right, box, indices);
272 }
273
274 static inline int intcmp (const unsigned int a, const unsigned int b)
275 {
276     return a - b;
277 }
278 CAIRO_COMBSORT_DECLARE (sort_indices, unsigned int, intcmp)
279
280 static inline int sizecmp (unsigned int a, unsigned int b, cairo_command_header_t **elements)
281 {
282     const cairo_rectangle_int_t *r;
283
284     r = &elements[a]->extents;
285     a = r->width * r->height;
286
287     r = &elements[b]->extents;
288     b = r->width * r->height;
289
290     return b - a;
291 }
292 CAIRO_COMBSORT_DECLARE_WITH_DATA (sort_commands, unsigned int, sizecmp)
293
294 static void
295 _cairo_recording_surface_destroy_bbtree (cairo_recording_surface_t *surface)
296 {
297     cairo_command_t **elements;
298     int i, num_elements;
299
300     if (surface->bbtree.chain == INVALID_CHAIN)
301         return;
302
303     if (surface->bbtree.left) {
304         bbtree_del (surface->bbtree.left);
305         surface->bbtree.left = NULL;
306     }
307     if (surface->bbtree.right) {
308         bbtree_del (surface->bbtree.right);
309         surface->bbtree.right = NULL;
310     }
311
312     elements = _cairo_array_index (&surface->commands, 0);
313     num_elements = surface->commands.num_elements;
314     for (i = 0; i < num_elements; i++)
315         elements[i]->header.chain = NULL;
316
317     surface->bbtree.chain = INVALID_CHAIN;
318 }
319
320 static cairo_status_t
321 _cairo_recording_surface_create_bbtree (cairo_recording_surface_t *surface)
322 {
323     cairo_command_t **elements = _cairo_array_index (&surface->commands, 0);
324     if (unlikely (elements == NULL))
325         return _cairo_error (CAIRO_STATUS_NULL_POINTER);
326
327     unsigned int *indices;
328     cairo_status_t status;
329     unsigned int i, count;
330
331     count = surface->commands.num_elements;
332     if (count > surface->num_indices) {
333         free (surface->indices);
334         surface->indices = _cairo_malloc_ab (count, sizeof (int));
335         if (unlikely (surface->indices == NULL))
336             return _cairo_error (CAIRO_STATUS_NO_MEMORY);
337
338         surface->num_indices = count;
339     }
340
341     indices = surface->indices;
342     for (i = 0; i < count; i++)
343         indices[i] = i;
344
345     sort_commands (indices, count, elements);
346
347     bbtree_init (&surface->bbtree, &elements[indices[0]]->header);
348     for (i = 1; i < count; i++) {
349         cairo_command_header_t *header = &elements[indices[i]]->header;
350         cairo_box_t box;
351
352         _cairo_box_from_rectangle (&box, &header->extents);
353         status = bbtree_add (&surface->bbtree, header, &box);
354         if (unlikely (status))
355             goto cleanup;
356     }
357
358     return CAIRO_STATUS_SUCCESS;
359
360 cleanup:
361     bbtree_del (&surface->bbtree);
362     return status;
363 }
364
365 /**
366  * cairo_recording_surface_create:
367  * @content: the content of the recording surface
368  * @extents: the extents to record in pixels, can be %NULL to record
369  *           unbounded operations.
370  *
371  * Creates a recording-surface which can be used to record all drawing operations
372  * at the highest level (that is, the level of paint, mask, stroke, fill
373  * and show_text_glyphs). The recording surface can then be "replayed" against
374  * any target surface by using it as a source to drawing operations.
375  *
376  * The recording phase of the recording surface is careful to snapshot all
377  * necessary objects (paths, patterns, etc.), in order to achieve
378  * accurate replay.
379  *
380  * Return value: a pointer to the newly created surface. The caller
381  * owns the surface and should call cairo_surface_destroy() when done
382  * with it.
383  *
384  * Since: 1.10
385  **/
386 cairo_surface_t *
387 cairo_recording_surface_create (cairo_content_t          content,
388                                 const cairo_rectangle_t *extents)
389 {
390     cairo_recording_surface_t *surface;
391
392     surface = malloc (sizeof (cairo_recording_surface_t));
393     if (unlikely (surface == NULL))
394         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
395
396     _cairo_surface_init (&surface->base,
397                          &cairo_recording_surface_backend,
398                          NULL, /* device */
399                          content);
400
401
402     surface->unbounded = TRUE;
403
404     /* unbounded -> 'infinite' extents */
405     if (extents != NULL) {
406         surface->extents_pixels = *extents;
407
408         /* XXX check for overflow */
409         surface->extents.x = floor (extents->x);
410         surface->extents.y = floor (extents->y);
411         surface->extents.width = ceil (extents->x + extents->width) - surface->extents.x;
412         surface->extents.height = ceil (extents->y + extents->height) - surface->extents.y;
413
414         surface->unbounded = FALSE;
415     }
416
417     _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
418
419     surface->base.is_clear = TRUE;
420
421     surface->bbtree.left = surface->bbtree.right = NULL;
422     surface->bbtree.chain = INVALID_CHAIN;
423
424     surface->indices = NULL;
425     surface->num_indices = 0;
426     surface->optimize_clears = TRUE;
427     surface->has_bilevel_alpha = FALSE;
428     surface->has_only_op_over = FALSE;
429
430     return &surface->base;
431 }
432 slim_hidden_def (cairo_recording_surface_create);
433
434 static cairo_surface_t *
435 _cairo_recording_surface_create_similar (void                  *abstract_surface,
436                                          cairo_content_t        content,
437                                          int                    width,
438                                          int                    height)
439 {
440     cairo_rectangle_t extents;
441     extents.x = extents.y = 0;
442     extents.width = width;
443     extents.height = height;
444     return cairo_recording_surface_create (content, &extents);
445 }
446
447 static cairo_status_t
448 _cairo_recording_surface_finish (void *abstract_surface)
449 {
450     cairo_recording_surface_t *surface = abstract_surface;
451     cairo_command_t **elements;
452     int i, num_elements;
453
454     num_elements = surface->commands.num_elements;
455     elements = _cairo_array_index (&surface->commands, 0);
456     for (i = 0; i < num_elements; i++) {
457         cairo_command_t *command = elements[i];
458
459         switch (command->header.type) {
460         case CAIRO_COMMAND_PAINT:
461             _cairo_pattern_fini (&command->paint.source.base);
462             break;
463
464         case CAIRO_COMMAND_MASK:
465             _cairo_pattern_fini (&command->mask.source.base);
466             _cairo_pattern_fini (&command->mask.mask.base);
467             break;
468
469         case CAIRO_COMMAND_STROKE:
470             _cairo_pattern_fini (&command->stroke.source.base);
471             _cairo_path_fixed_fini (&command->stroke.path);
472             _cairo_stroke_style_fini (&command->stroke.style);
473             break;
474
475         case CAIRO_COMMAND_FILL:
476             _cairo_pattern_fini (&command->fill.source.base);
477             _cairo_path_fixed_fini (&command->fill.path);
478             break;
479
480         case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
481             _cairo_pattern_fini (&command->show_text_glyphs.source.base);
482             free (command->show_text_glyphs.utf8);
483             free (command->show_text_glyphs.glyphs);
484             free (command->show_text_glyphs.clusters);
485             cairo_scaled_font_destroy (command->show_text_glyphs.scaled_font);
486             break;
487
488         default:
489             ASSERT_NOT_REACHED;
490         }
491
492         _cairo_clip_destroy (command->header.clip);
493         free (command);
494     }
495
496     _cairo_array_fini (&surface->commands);
497
498     if (surface->bbtree.left)
499         bbtree_del (surface->bbtree.left);
500     if (surface->bbtree.right)
501         bbtree_del (surface->bbtree.right);
502
503     free (surface->indices);
504
505     return CAIRO_STATUS_SUCCESS;
506 }
507
508 struct proxy {
509     cairo_surface_t base;
510     cairo_surface_t *image;
511 };
512
513 static cairo_status_t
514 proxy_acquire_source_image (void                         *abstract_surface,
515                             cairo_image_surface_t       **image_out,
516                             void                        **image_extra)
517 {
518     struct proxy *proxy = abstract_surface;
519     return _cairo_surface_acquire_source_image (proxy->image, image_out, image_extra);
520 }
521
522 static void
523 proxy_release_source_image (void                        *abstract_surface,
524                             cairo_image_surface_t       *image,
525                             void                        *image_extra)
526 {
527     struct proxy *proxy = abstract_surface;
528     _cairo_surface_release_source_image (proxy->image, image, image_extra);
529 }
530
531 static cairo_status_t
532 proxy_finish (void *abstract_surface)
533 {
534     return CAIRO_STATUS_SUCCESS;
535 }
536
537 static const cairo_surface_backend_t proxy_backend  = {
538     CAIRO_INTERNAL_SURFACE_TYPE_NULL,
539     proxy_finish,
540     NULL,
541
542     NULL, /* create similar */
543     NULL, /* create similar image */
544     NULL, /* map to image */
545     NULL, /* unmap image */
546
547     _cairo_surface_default_source,
548     proxy_acquire_source_image,
549     proxy_release_source_image,
550 };
551
552 static cairo_surface_t *
553 attach_proxy (cairo_surface_t *source,
554               cairo_surface_t *image)
555 {
556     struct proxy *proxy;
557
558     proxy = malloc (sizeof (*proxy));
559     if (unlikely (proxy == NULL))
560         return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
561
562     _cairo_surface_init (&proxy->base, &proxy_backend, NULL, image->content);
563
564     proxy->image = image;
565     _cairo_surface_attach_snapshot (source, &proxy->base, NULL);
566
567     return &proxy->base;
568 }
569
570 static void
571 detach_proxy (cairo_surface_t *source,
572               cairo_surface_t *proxy)
573 {
574     cairo_surface_finish (proxy);
575     cairo_surface_destroy (proxy);
576 }
577
578 static cairo_surface_t *
579 get_proxy (cairo_surface_t *proxy)
580 {
581     return ((struct proxy *)proxy)->image;
582 }
583
584 static cairo_status_t
585 _cairo_recording_surface_acquire_source_image (void                      *abstract_surface,
586                                                cairo_image_surface_t    **image_out,
587                                                void                     **image_extra)
588 {
589     cairo_recording_surface_t *surface = abstract_surface;
590     cairo_surface_t *image, *proxy;
591     cairo_status_t status;
592
593     proxy = _cairo_surface_has_snapshot (abstract_surface, &proxy_backend);
594     if (proxy != NULL) {
595         *image_out = (cairo_image_surface_t *)
596             cairo_surface_reference (get_proxy (proxy));
597         *image_extra = NULL;
598         return CAIRO_STATUS_SUCCESS;
599     }
600
601     assert (! surface->unbounded);
602     image = _cairo_image_surface_create_with_content (surface->base.content,
603                                                       surface->extents.width,
604                                                       surface->extents.height);
605     if (unlikely (image->status)) {
606         status = image->status;
607         cairo_surface_destroy (image);
608         return status;
609     }
610
611     /* Handle recursion by returning future reads from the current image */
612     proxy = attach_proxy (abstract_surface, image);
613     status = _cairo_recording_surface_replay (&surface->base, image);
614     detach_proxy (abstract_surface, proxy);
615
616     if (unlikely (status)) {
617         cairo_surface_destroy (image);
618         return status;
619     }
620
621     *image_out = (cairo_image_surface_t *) image;
622     *image_extra = NULL;
623     return CAIRO_STATUS_SUCCESS;
624 }
625
626 static void
627 _cairo_recording_surface_release_source_image (void                     *abstract_surface,
628                                                cairo_image_surface_t    *image,
629                                                void                     *image_extra)
630 {
631     cairo_surface_destroy (&image->base);
632 }
633
634 static cairo_status_t
635 _command_init (cairo_recording_surface_t *surface,
636                cairo_command_header_t *command,
637                cairo_command_type_t type,
638                cairo_operator_t op,
639                cairo_composite_rectangles_t *composite)
640 {
641     cairo_status_t status = CAIRO_STATUS_SUCCESS;
642
643     command->type = type;
644     command->op = op;
645     command->region = CAIRO_RECORDING_REGION_ALL;
646
647     command->extents = composite->unbounded;
648     command->chain = NULL;
649     command->index = surface->commands.num_elements;
650
651     /* steal the clip */
652     command->clip = NULL;
653     if (! _cairo_composite_rectangles_can_reduce_clip (composite,
654                                                        composite->clip))
655     {
656         command->clip = composite->clip;
657         composite->clip = NULL;
658     }
659
660     return status;
661 }
662
663 static void
664 _cairo_recording_surface_break_self_copy_loop (cairo_recording_surface_t *surface)
665 {
666     cairo_surface_flush (&surface->base);
667 }
668
669 static cairo_status_t
670 _cairo_recording_surface_commit (cairo_recording_surface_t *surface,
671                                  cairo_command_header_t *command)
672 {
673     _cairo_recording_surface_break_self_copy_loop (surface);
674     return _cairo_array_append (&surface->commands, &command);
675 }
676
677 static void
678 _cairo_recording_surface_reset (cairo_recording_surface_t *surface)
679 {
680     /* Reset the commands and temporaries */
681     _cairo_recording_surface_finish (surface);
682
683     surface->bbtree.left = surface->bbtree.right = NULL;
684     surface->bbtree.chain = INVALID_CHAIN;
685
686     surface->indices = NULL;
687     surface->num_indices = 0;
688
689     _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
690 }
691
692 static cairo_bool_t
693 is_identity_recording_pattern (const cairo_pattern_t *pattern)
694 {
695     cairo_surface_t *surface;
696
697     if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
698         return FALSE;
699
700     if (!_cairo_matrix_is_identity(&pattern->matrix))
701         return FALSE;
702
703     surface = ((cairo_surface_pattern_t *)pattern)->surface;
704     return surface->backend->type == CAIRO_SURFACE_TYPE_RECORDING;
705 }
706
707 static cairo_int_status_t
708 _cairo_recording_surface_paint (void                      *abstract_surface,
709                                 cairo_operator_t           op,
710                                 const cairo_pattern_t     *source,
711                                 const cairo_clip_t        *clip)
712 {
713     cairo_status_t status;
714     cairo_recording_surface_t *surface = abstract_surface;
715     cairo_command_paint_t *command;
716     cairo_composite_rectangles_t composite;
717
718     TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
719
720     if (op == CAIRO_OPERATOR_CLEAR && clip == NULL) {
721         if (surface->optimize_clears) {
722             _cairo_recording_surface_reset (surface);
723             return CAIRO_STATUS_SUCCESS;
724         }
725     }
726
727     if (clip == NULL && surface->optimize_clears &&
728         (op == CAIRO_OPERATOR_SOURCE ||
729          (op == CAIRO_OPERATOR_OVER &&
730           (surface->base.is_clear || _cairo_pattern_is_opaque_solid (source)))))
731     {
732         _cairo_recording_surface_reset (surface);
733         if (is_identity_recording_pattern (source)) {
734             cairo_surface_t *src = ((cairo_surface_pattern_t *)source)->surface;
735             return _cairo_recording_surface_replay (src, &surface->base);
736         }
737     }
738
739     status = _cairo_composite_rectangles_init_for_paint (&composite,
740                                                          &surface->base,
741                                                          op, source,
742                                                          clip);
743     if (unlikely (status))
744         return status;
745
746     command = malloc (sizeof (cairo_command_paint_t));
747     if (unlikely (command == NULL)) {
748         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
749         goto CLEANUP_COMPOSITE;
750     }
751
752     status = _command_init (surface,
753                             &command->header, CAIRO_COMMAND_PAINT, op,
754                             &composite);
755     if (unlikely (status))
756         goto CLEANUP_COMMAND;
757
758     status = _cairo_pattern_init_snapshot (&command->source.base, source);
759     if (unlikely (status))
760         goto CLEANUP_COMMAND;
761
762     status = _cairo_recording_surface_commit (surface, &command->header);
763     if (unlikely (status))
764         goto CLEANUP_SOURCE;
765
766     _cairo_recording_surface_destroy_bbtree (surface);
767
768     _cairo_composite_rectangles_fini (&composite);
769     return CAIRO_STATUS_SUCCESS;
770
771   CLEANUP_SOURCE:
772     _cairo_pattern_fini (&command->source.base);
773   CLEANUP_COMMAND:
774     _cairo_clip_destroy (command->header.clip);
775     free (command);
776 CLEANUP_COMPOSITE:
777     _cairo_composite_rectangles_fini (&composite);
778     return status;
779 }
780
781 static cairo_int_status_t
782 _cairo_recording_surface_mask (void                     *abstract_surface,
783                                cairo_operator_t          op,
784                                const cairo_pattern_t    *source,
785                                const cairo_pattern_t    *mask,
786                                const cairo_clip_t       *clip)
787 {
788     cairo_status_t status;
789     cairo_recording_surface_t *surface = abstract_surface;
790     cairo_command_mask_t *command;
791     cairo_composite_rectangles_t composite;
792
793     TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
794
795     status = _cairo_composite_rectangles_init_for_mask (&composite,
796                                                         &surface->base,
797                                                         op, source, mask,
798                                                         clip);
799     if (unlikely (status))
800         return status;
801
802     command = malloc (sizeof (cairo_command_mask_t));
803     if (unlikely (command == NULL)) {
804         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
805         goto CLEANUP_COMPOSITE;
806     }
807
808     status = _command_init (surface,
809                             &command->header, CAIRO_COMMAND_MASK, op,
810                             &composite);
811     if (unlikely (status))
812         goto CLEANUP_COMMAND;
813
814     status = _cairo_pattern_init_snapshot (&command->source.base, source);
815     if (unlikely (status))
816         goto CLEANUP_COMMAND;
817
818     status = _cairo_pattern_init_snapshot (&command->mask.base, mask);
819     if (unlikely (status))
820         goto CLEANUP_SOURCE;
821
822     status = _cairo_recording_surface_commit (surface, &command->header);
823     if (unlikely (status))
824         goto CLEANUP_MASK;
825
826     _cairo_recording_surface_destroy_bbtree (surface);
827
828     _cairo_composite_rectangles_fini (&composite);
829     return CAIRO_STATUS_SUCCESS;
830
831   CLEANUP_MASK:
832     _cairo_pattern_fini (&command->mask.base);
833   CLEANUP_SOURCE:
834     _cairo_pattern_fini (&command->source.base);
835   CLEANUP_COMMAND:
836     _cairo_clip_destroy (command->header.clip);
837     free (command);
838 CLEANUP_COMPOSITE:
839     _cairo_composite_rectangles_fini (&composite);
840     return status;
841 }
842
843 static cairo_int_status_t
844 _cairo_recording_surface_stroke (void                   *abstract_surface,
845                                  cairo_operator_t        op,
846                                  const cairo_pattern_t  *source,
847                                  const cairo_path_fixed_t       *path,
848                                  const cairo_stroke_style_t     *style,
849                                  const cairo_matrix_t           *ctm,
850                                  const cairo_matrix_t           *ctm_inverse,
851                                  double                  tolerance,
852                                  cairo_antialias_t       antialias,
853                                  const cairo_clip_t     *clip)
854 {
855     cairo_status_t status;
856     cairo_recording_surface_t *surface = abstract_surface;
857     cairo_command_stroke_t *command;
858     cairo_composite_rectangles_t composite;
859
860     TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
861
862     status = _cairo_composite_rectangles_init_for_stroke (&composite,
863                                                           &surface->base,
864                                                           op, source,
865                                                           path, style, ctm,
866                                                           clip);
867     if (unlikely (status))
868         return status;
869
870     command = malloc (sizeof (cairo_command_stroke_t));
871     if (unlikely (command == NULL)) {
872         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
873         goto CLEANUP_COMPOSITE;
874     }
875
876     status = _command_init (surface,
877                             &command->header, CAIRO_COMMAND_STROKE, op,
878                             &composite);
879     if (unlikely (status))
880         goto CLEANUP_COMMAND;
881
882     status = _cairo_pattern_init_snapshot (&command->source.base, source);
883     if (unlikely (status))
884         goto CLEANUP_COMMAND;
885
886     status = _cairo_path_fixed_init_copy (&command->path, path);
887     if (unlikely (status))
888         goto CLEANUP_SOURCE;
889
890     status = _cairo_stroke_style_init_copy (&command->style, style);
891     if (unlikely (status))
892         goto CLEANUP_PATH;
893
894     command->ctm = *ctm;
895     command->ctm_inverse = *ctm_inverse;
896     command->tolerance = tolerance;
897     command->antialias = antialias;
898
899     status = _cairo_recording_surface_commit (surface, &command->header);
900     if (unlikely (status))
901         goto CLEANUP_STYLE;
902
903     _cairo_recording_surface_destroy_bbtree (surface);
904
905     _cairo_composite_rectangles_fini (&composite);
906     return CAIRO_STATUS_SUCCESS;
907
908   CLEANUP_STYLE:
909     _cairo_stroke_style_fini (&command->style);
910   CLEANUP_PATH:
911     _cairo_path_fixed_fini (&command->path);
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 _cairo_recording_surface_fill (void                     *abstract_surface,
924                                cairo_operator_t          op,
925                                const cairo_pattern_t    *source,
926                                const cairo_path_fixed_t *path,
927                                cairo_fill_rule_t         fill_rule,
928                                double                    tolerance,
929                                cairo_antialias_t         antialias,
930                                const cairo_clip_t       *clip)
931 {
932     cairo_status_t status;
933     cairo_recording_surface_t *surface = abstract_surface;
934     cairo_command_fill_t *command;
935     cairo_composite_rectangles_t composite;
936
937     TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
938
939     status = _cairo_composite_rectangles_init_for_fill (&composite,
940                                                         &surface->base,
941                                                         op, source, path,
942                                                         clip);
943     if (unlikely (status))
944         return status;
945
946     command = malloc (sizeof (cairo_command_fill_t));
947     if (unlikely (command == NULL)) {
948         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
949         goto CLEANUP_COMPOSITE;
950     }
951
952     status =_command_init (surface,
953                            &command->header, CAIRO_COMMAND_FILL, op,
954                            &composite);
955     if (unlikely (status))
956         goto CLEANUP_COMMAND;
957
958     status = _cairo_pattern_init_snapshot (&command->source.base, source);
959     if (unlikely (status))
960         goto CLEANUP_COMMAND;
961
962     status = _cairo_path_fixed_init_copy (&command->path, path);
963     if (unlikely (status))
964         goto CLEANUP_SOURCE;
965
966     command->fill_rule = fill_rule;
967     command->tolerance = tolerance;
968     command->antialias = antialias;
969
970     status = _cairo_recording_surface_commit (surface, &command->header);
971     if (unlikely (status))
972         goto CLEANUP_PATH;
973
974     _cairo_recording_surface_destroy_bbtree (surface);
975
976     _cairo_composite_rectangles_fini (&composite);
977     return CAIRO_STATUS_SUCCESS;
978
979   CLEANUP_PATH:
980     _cairo_path_fixed_fini (&command->path);
981   CLEANUP_SOURCE:
982     _cairo_pattern_fini (&command->source.base);
983   CLEANUP_COMMAND:
984     _cairo_clip_destroy (command->header.clip);
985     free (command);
986 CLEANUP_COMPOSITE:
987     _cairo_composite_rectangles_fini (&composite);
988     return status;
989 }
990
991 static cairo_bool_t
992 _cairo_recording_surface_has_show_text_glyphs (void *abstract_surface)
993 {
994     return TRUE;
995 }
996
997 static cairo_int_status_t
998 _cairo_recording_surface_show_text_glyphs (void                         *abstract_surface,
999                                            cairo_operator_t              op,
1000                                            const cairo_pattern_t        *source,
1001                                            const char                   *utf8,
1002                                            int                           utf8_len,
1003                                            cairo_glyph_t                *glyphs,
1004                                            int                           num_glyphs,
1005                                            const cairo_text_cluster_t   *clusters,
1006                                            int                           num_clusters,
1007                                            cairo_text_cluster_flags_t    cluster_flags,
1008                                            cairo_scaled_font_t          *scaled_font,
1009                                            const cairo_clip_t           *clip)
1010 {
1011     cairo_status_t status;
1012     cairo_recording_surface_t *surface = abstract_surface;
1013     cairo_command_show_text_glyphs_t *command;
1014     cairo_composite_rectangles_t composite;
1015
1016     TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
1017
1018     status = _cairo_composite_rectangles_init_for_glyphs (&composite,
1019                                                           &surface->base,
1020                                                           op, source,
1021                                                           scaled_font,
1022                                                           glyphs, num_glyphs,
1023                                                           clip,
1024                                                           NULL);
1025     if (unlikely (status))
1026         return status;
1027
1028     command = malloc (sizeof (cairo_command_show_text_glyphs_t));
1029     if (unlikely (command == NULL)) {
1030         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1031         goto CLEANUP_COMPOSITE;
1032     }
1033
1034     status = _command_init (surface,
1035                             &command->header, CAIRO_COMMAND_SHOW_TEXT_GLYPHS,
1036                             op, &composite);
1037     if (unlikely (status))
1038         goto CLEANUP_COMMAND;
1039
1040     status = _cairo_pattern_init_snapshot (&command->source.base, source);
1041     if (unlikely (status))
1042         goto CLEANUP_COMMAND;
1043
1044     command->utf8 = NULL;
1045     command->utf8_len = utf8_len;
1046     command->glyphs = NULL;
1047     command->num_glyphs = num_glyphs;
1048     command->clusters = NULL;
1049     command->num_clusters = num_clusters;
1050
1051     if (utf8_len) {
1052         command->utf8 = malloc (utf8_len);
1053         if (unlikely (command->utf8 == NULL)) {
1054             status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1055             goto CLEANUP_ARRAYS;
1056         }
1057         memcpy (command->utf8, utf8, utf8_len);
1058     }
1059     if (num_glyphs) {
1060         command->glyphs = _cairo_malloc_ab (num_glyphs, sizeof (glyphs[0]));
1061         if (unlikely (command->glyphs == NULL)) {
1062             status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1063             goto CLEANUP_ARRAYS;
1064         }
1065         memcpy (command->glyphs, glyphs, sizeof (glyphs[0]) * num_glyphs);
1066     }
1067     if (num_clusters) {
1068         command->clusters = _cairo_malloc_ab (num_clusters, sizeof (clusters[0]));
1069         if (unlikely (command->clusters == NULL)) {
1070             status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1071             goto CLEANUP_ARRAYS;
1072         }
1073         memcpy (command->clusters, clusters, sizeof (clusters[0]) * num_clusters);
1074     }
1075
1076     command->cluster_flags = cluster_flags;
1077
1078     command->scaled_font = cairo_scaled_font_reference (scaled_font);
1079
1080     status = _cairo_recording_surface_commit (surface, &command->header);
1081     if (unlikely (status))
1082         goto CLEANUP_SCALED_FONT;
1083
1084     _cairo_composite_rectangles_fini (&composite);
1085     return CAIRO_STATUS_SUCCESS;
1086
1087   CLEANUP_SCALED_FONT:
1088     cairo_scaled_font_destroy (command->scaled_font);
1089   CLEANUP_ARRAYS:
1090     free (command->utf8);
1091     free (command->glyphs);
1092     free (command->clusters);
1093
1094     _cairo_pattern_fini (&command->source.base);
1095   CLEANUP_COMMAND:
1096     _cairo_clip_destroy (command->header.clip);
1097     free (command);
1098 CLEANUP_COMPOSITE:
1099     _cairo_composite_rectangles_fini (&composite);
1100     return status;
1101 }
1102
1103 static void
1104 _command_init_copy (cairo_recording_surface_t *surface,
1105                     cairo_command_header_t *dst,
1106                     const cairo_command_header_t *src)
1107 {
1108     dst->type = src->type;
1109     dst->op = src->op;
1110     dst->region = CAIRO_RECORDING_REGION_ALL;
1111
1112     dst->extents = src->extents;
1113     dst->chain = NULL;
1114     dst->index = surface->commands.num_elements;
1115
1116     dst->clip = _cairo_clip_copy (src->clip);
1117 }
1118
1119 static cairo_status_t
1120 _cairo_recording_surface_copy__paint (cairo_recording_surface_t *surface,
1121                                       const cairo_command_t *src)
1122 {
1123     cairo_command_paint_t *command;
1124     cairo_status_t status;
1125
1126     command = malloc (sizeof (*command));
1127     if (unlikely (command == NULL)) {
1128         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1129         goto err;
1130     }
1131
1132     _command_init_copy (surface, &command->header, &src->header);
1133
1134     status = _cairo_pattern_init_copy (&command->source.base,
1135                                        &src->paint.source.base);
1136     if (unlikely (status))
1137         goto err_command;
1138
1139     status = _cairo_recording_surface_commit (surface, &command->header);
1140     if (unlikely (status))
1141         goto err_source;
1142
1143     return CAIRO_STATUS_SUCCESS;
1144
1145 err_source:
1146     _cairo_pattern_fini (&command->source.base);
1147 err_command:
1148     free(command);
1149 err:
1150     return status;
1151 }
1152
1153 static cairo_status_t
1154 _cairo_recording_surface_copy__mask (cairo_recording_surface_t *surface,
1155                                      const cairo_command_t *src)
1156 {
1157     cairo_command_mask_t *command;
1158     cairo_status_t status;
1159
1160     command = malloc (sizeof (*command));
1161     if (unlikely (command == NULL)) {
1162         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1163         goto err;
1164     }
1165
1166     _command_init_copy (surface, &command->header, &src->header);
1167
1168     status = _cairo_pattern_init_copy (&command->source.base,
1169                                        &src->mask.source.base);
1170     if (unlikely (status))
1171         goto err_command;
1172
1173     status = _cairo_pattern_init_copy (&command->mask.base,
1174                                        &src->mask.source.base);
1175     if (unlikely (status))
1176         goto err_source;
1177
1178     status = _cairo_recording_surface_commit (surface, &command->header);
1179     if (unlikely (status))
1180         goto err_mask;
1181
1182     return CAIRO_STATUS_SUCCESS;
1183
1184 err_mask:
1185     _cairo_pattern_fini (&command->mask.base);
1186 err_source:
1187     _cairo_pattern_fini (&command->source.base);
1188 err_command:
1189     free(command);
1190 err:
1191     return status;
1192 }
1193
1194 static cairo_status_t
1195 _cairo_recording_surface_copy__stroke (cairo_recording_surface_t *surface,
1196                                      const cairo_command_t *src)
1197 {
1198     cairo_command_stroke_t *command;
1199     cairo_status_t status;
1200
1201     command = malloc (sizeof (*command));
1202     if (unlikely (command == NULL)) {
1203         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1204         goto err;
1205     }
1206
1207     _command_init_copy (surface, &command->header, &src->header);
1208
1209     status = _cairo_pattern_init_copy (&command->source.base,
1210                                        &src->stroke.source.base);
1211     if (unlikely (status))
1212         goto err_command;
1213
1214     status = _cairo_path_fixed_init_copy (&command->path, &src->stroke.path);
1215     if (unlikely (status))
1216         goto err_source;
1217
1218     status = _cairo_stroke_style_init_copy (&command->style,
1219                                             &src->stroke.style);
1220     if (unlikely (status))
1221         goto err_path;
1222
1223     command->ctm = src->stroke.ctm;
1224     command->ctm_inverse = src->stroke.ctm_inverse;
1225     command->tolerance = src->stroke.tolerance;
1226     command->antialias = src->stroke.antialias;
1227
1228     status = _cairo_recording_surface_commit (surface, &command->header);
1229     if (unlikely (status))
1230         goto err_style;
1231
1232     return CAIRO_STATUS_SUCCESS;
1233
1234 err_style:
1235     _cairo_stroke_style_fini (&command->style);
1236 err_path:
1237     _cairo_path_fixed_fini (&command->path);
1238 err_source:
1239     _cairo_pattern_fini (&command->source.base);
1240 err_command:
1241     free(command);
1242 err:
1243     return status;
1244 }
1245
1246 static cairo_status_t
1247 _cairo_recording_surface_copy__fill (cairo_recording_surface_t *surface,
1248                                      const cairo_command_t *src)
1249 {
1250     cairo_command_fill_t *command;
1251     cairo_status_t status;
1252
1253     command = malloc (sizeof (*command));
1254     if (unlikely (command == NULL)) {
1255         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1256         goto err;
1257     }
1258
1259     _command_init_copy (surface, &command->header, &src->header);
1260
1261     status = _cairo_pattern_init_copy (&command->source.base,
1262                                        &src->fill.source.base);
1263     if (unlikely (status))
1264         goto err_command;
1265
1266     status = _cairo_path_fixed_init_copy (&command->path, &src->fill.path);
1267     if (unlikely (status))
1268         goto err_source;
1269
1270     command->fill_rule = src->fill.fill_rule;
1271     command->tolerance = src->fill.tolerance;
1272     command->antialias = src->fill.antialias;
1273
1274     status = _cairo_recording_surface_commit (surface, &command->header);
1275     if (unlikely (status))
1276         goto err_path;
1277
1278     return CAIRO_STATUS_SUCCESS;
1279
1280 err_path:
1281     _cairo_path_fixed_fini (&command->path);
1282 err_source:
1283     _cairo_pattern_fini (&command->source.base);
1284 err_command:
1285     free(command);
1286 err:
1287     return status;
1288 }
1289
1290 static cairo_status_t
1291 _cairo_recording_surface_copy__glyphs (cairo_recording_surface_t *surface,
1292                                        const cairo_command_t *src)
1293 {
1294     cairo_command_show_text_glyphs_t *command;
1295     cairo_status_t status;
1296
1297     command = malloc (sizeof (*command));
1298     if (unlikely (command == NULL)) {
1299         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1300         goto err;
1301     }
1302
1303     _command_init_copy (surface, &command->header, &src->header);
1304
1305     status = _cairo_pattern_init_copy (&command->source.base,
1306                                        &src->show_text_glyphs.source.base);
1307     if (unlikely (status))
1308         goto err_command;
1309
1310     command->utf8 = NULL;
1311     command->utf8_len = src->show_text_glyphs.utf8_len;
1312     command->glyphs = NULL;
1313     command->num_glyphs = src->show_text_glyphs.num_glyphs;
1314     command->clusters = NULL;
1315     command->num_clusters = src->show_text_glyphs.num_clusters;
1316
1317     if (command->utf8_len) {
1318         command->utf8 = malloc (command->utf8_len);
1319         if (unlikely (command->utf8 == NULL)) {
1320             status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1321             goto err_arrays;
1322         }
1323         memcpy (command->utf8, src->show_text_glyphs.utf8, command->utf8_len);
1324     }
1325     if (command->num_glyphs) {
1326         command->glyphs = _cairo_malloc_ab (command->num_glyphs,
1327                                             sizeof (command->glyphs[0]));
1328         if (unlikely (command->glyphs == NULL)) {
1329             status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1330             goto err_arrays;
1331         }
1332         memcpy (command->glyphs, src->show_text_glyphs.glyphs,
1333                 sizeof (command->glyphs[0]) * command->num_glyphs);
1334     }
1335     if (command->num_clusters) {
1336         command->clusters = _cairo_malloc_ab (command->num_clusters,
1337                                               sizeof (command->clusters[0]));
1338         if (unlikely (command->clusters == NULL)) {
1339             status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1340             goto err_arrays;
1341         }
1342         memcpy (command->clusters, src->show_text_glyphs.clusters,
1343                 sizeof (command->clusters[0]) * command->num_clusters);
1344     }
1345
1346     command->cluster_flags = src->show_text_glyphs.cluster_flags;
1347
1348     command->scaled_font =
1349         cairo_scaled_font_reference (src->show_text_glyphs.scaled_font);
1350
1351     status = _cairo_recording_surface_commit (surface, &command->header);
1352     if (unlikely (status))
1353         goto err_arrays;
1354
1355     return CAIRO_STATUS_SUCCESS;
1356
1357 err_arrays:
1358     free (command->utf8);
1359     free (command->glyphs);
1360     free (command->clusters);
1361     _cairo_pattern_fini (&command->source.base);
1362 err_command:
1363     free(command);
1364 err:
1365     return status;
1366 }
1367
1368 static cairo_status_t
1369 _cairo_recording_surface_copy (cairo_recording_surface_t *dst,
1370                                cairo_recording_surface_t *src)
1371 {
1372     cairo_command_t **elements;
1373     int i, num_elements;
1374     cairo_status_t status;
1375
1376     elements = _cairo_array_index (&src->commands, 0);
1377     num_elements = src->commands.num_elements;
1378     for (i = 0; i < num_elements; i++) {
1379         const cairo_command_t *command = elements[i];
1380
1381         switch (command->header.type) {
1382         case CAIRO_COMMAND_PAINT:
1383             status = _cairo_recording_surface_copy__paint (dst, command);
1384             break;
1385
1386         case CAIRO_COMMAND_MASK:
1387             status = _cairo_recording_surface_copy__mask (dst, command);
1388             break;
1389
1390         case CAIRO_COMMAND_STROKE:
1391             status = _cairo_recording_surface_copy__stroke (dst, command);
1392             break;
1393
1394         case CAIRO_COMMAND_FILL:
1395             status = _cairo_recording_surface_copy__fill (dst, command);
1396             break;
1397
1398         case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
1399             status = _cairo_recording_surface_copy__glyphs (dst, command);
1400             break;
1401
1402         default:
1403             ASSERT_NOT_REACHED;
1404         }
1405
1406         if (unlikely (status))
1407             return status;
1408     }
1409
1410     return CAIRO_STATUS_SUCCESS;
1411 }
1412
1413 /**
1414  * _cairo_recording_surface_snapshot:
1415  * @surface: a #cairo_surface_t which must be a recording surface
1416  *
1417  * Make an immutable copy of @surface. It is an error to call a
1418  * surface-modifying function on the result of this function.
1419  *
1420  * The caller owns the return value and should call
1421  * cairo_surface_destroy() when finished with it. This function will not
1422  * return %NULL, but will return a nil surface instead.
1423  *
1424  * Return value: The snapshot surface.
1425  **/
1426 static cairo_surface_t *
1427 _cairo_recording_surface_snapshot (void *abstract_other)
1428 {
1429     cairo_recording_surface_t *other = abstract_other;
1430     cairo_recording_surface_t *surface;
1431     cairo_status_t status;
1432
1433     surface = malloc (sizeof (cairo_recording_surface_t));
1434     if (unlikely (surface == NULL))
1435         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
1436
1437     _cairo_surface_init (&surface->base,
1438                          &cairo_recording_surface_backend,
1439                          NULL, /* device */
1440                          other->base.content);
1441
1442     surface->extents_pixels = other->extents_pixels;
1443     surface->extents = other->extents;
1444     surface->unbounded = other->unbounded;
1445
1446     surface->base.is_clear = other->base.is_clear;
1447
1448     surface->bbtree.left = surface->bbtree.right = NULL;
1449     surface->bbtree.chain = INVALID_CHAIN;
1450
1451     surface->indices = NULL;
1452     surface->num_indices = 0;
1453     surface->optimize_clears = TRUE;
1454
1455     _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
1456     status = _cairo_recording_surface_copy (surface, other);
1457     if (unlikely (status)) {
1458         cairo_surface_destroy (&surface->base);
1459         return _cairo_surface_create_in_error (status);
1460     }
1461
1462     return &surface->base;
1463 }
1464
1465 static cairo_bool_t
1466 _cairo_recording_surface_get_extents (void                  *abstract_surface,
1467                                       cairo_rectangle_int_t *rectangle)
1468 {
1469     cairo_recording_surface_t *surface = abstract_surface;
1470
1471     if (surface->unbounded)
1472         return FALSE;
1473
1474     *rectangle = surface->extents;
1475     return TRUE;
1476 }
1477
1478 static const cairo_surface_backend_t cairo_recording_surface_backend = {
1479     CAIRO_SURFACE_TYPE_RECORDING,
1480     _cairo_recording_surface_finish,
1481
1482     _cairo_default_context_create,
1483
1484     _cairo_recording_surface_create_similar,
1485     NULL, /* create similar image */
1486     NULL, /* map to image */
1487     NULL, /* unmap image */
1488
1489     _cairo_surface_default_source,
1490     _cairo_recording_surface_acquire_source_image,
1491     _cairo_recording_surface_release_source_image,
1492     _cairo_recording_surface_snapshot,
1493
1494     NULL, /* copy_page */
1495     NULL, /* show_page */
1496
1497     _cairo_recording_surface_get_extents,
1498     NULL, /* get_font_options */
1499
1500     NULL, /* flush */
1501     NULL, /* mark_dirty_rectangle */
1502
1503     /* Here are the 5 basic drawing operations, (which are in some
1504      * sense the only things that cairo_recording_surface should need to
1505      * implement).  However, we implement the more generic show_text_glyphs
1506      * instead of show_glyphs.  One or the other is eough. */
1507
1508     _cairo_recording_surface_paint,
1509     _cairo_recording_surface_mask,
1510     _cairo_recording_surface_stroke,
1511     _cairo_recording_surface_fill,
1512     NULL, /* fill-stroke */
1513     NULL,
1514     _cairo_recording_surface_has_show_text_glyphs,
1515     _cairo_recording_surface_show_text_glyphs,
1516 };
1517
1518 cairo_int_status_t
1519 _cairo_recording_surface_get_path (cairo_surface_t    *abstract_surface,
1520                                    cairo_path_fixed_t *path)
1521 {
1522     cairo_recording_surface_t *surface;
1523     cairo_command_t **elements;
1524     int i, num_elements;
1525     cairo_int_status_t status;
1526
1527     if (unlikely (abstract_surface->status))
1528         return abstract_surface->status;
1529
1530     surface = (cairo_recording_surface_t *) abstract_surface;
1531     status = CAIRO_STATUS_SUCCESS;
1532
1533     num_elements = surface->commands.num_elements;
1534     elements = _cairo_array_index (&surface->commands, 0);
1535     for (i = 0; i < num_elements; i++) {
1536         cairo_command_t *command = elements[i];
1537
1538         switch (command->header.type) {
1539         case CAIRO_COMMAND_PAINT:
1540         case CAIRO_COMMAND_MASK:
1541             status = CAIRO_INT_STATUS_UNSUPPORTED;
1542             break;
1543
1544         case CAIRO_COMMAND_STROKE:
1545         {
1546             cairo_traps_t traps;
1547
1548             _cairo_traps_init (&traps);
1549
1550             /* XXX call cairo_stroke_to_path() when that is implemented */
1551             status = _cairo_path_fixed_stroke_polygon_to_traps (&command->stroke.path,
1552                                                                 &command->stroke.style,
1553                                                                 &command->stroke.ctm,
1554                                                                 &command->stroke.ctm_inverse,
1555                                                                 command->stroke.tolerance,
1556                                                                 &traps);
1557
1558             if (status == CAIRO_INT_STATUS_SUCCESS)
1559                 status = _cairo_traps_path (&traps, path);
1560
1561             _cairo_traps_fini (&traps);
1562             break;
1563         }
1564         case CAIRO_COMMAND_FILL:
1565         {
1566             status = _cairo_path_fixed_append (path,
1567                                                &command->fill.path,
1568                                                0, 0);
1569             break;
1570         }
1571         case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
1572         {
1573             status = _cairo_scaled_font_glyph_path (command->show_text_glyphs.scaled_font,
1574                                                     command->show_text_glyphs.glyphs,
1575                                                     command->show_text_glyphs.num_glyphs,
1576                                                     path);
1577             break;
1578         }
1579
1580         default:
1581             ASSERT_NOT_REACHED;
1582         }
1583
1584         if (unlikely (status))
1585             break;
1586     }
1587
1588     return status;
1589 }
1590
1591 static int
1592 _cairo_recording_surface_get_visible_commands (cairo_recording_surface_t *surface,
1593                                                const cairo_rectangle_int_t *extents)
1594 {
1595     unsigned int num_visible, *indices;
1596     cairo_box_t box;
1597
1598     if (surface->commands.num_elements == 0)
1599             return 0;
1600
1601     _cairo_box_from_rectangle (&box, extents);
1602
1603     if (surface->bbtree.chain == INVALID_CHAIN)
1604         _cairo_recording_surface_create_bbtree (surface);
1605
1606     indices = surface->indices;
1607     bbtree_foreach_mark_visible (&surface->bbtree, &box, &indices);
1608     num_visible = indices - surface->indices;
1609     if (num_visible > 1)
1610         sort_indices (surface->indices, num_visible);
1611
1612     return num_visible;
1613 }
1614
1615 static void
1616 _cairo_recording_surface_merge_source_attributes (cairo_recording_surface_t  *surface,
1617                                                   cairo_operator_t            op,
1618                                                   const cairo_pattern_t      *source)
1619 {
1620     if (op != CAIRO_OPERATOR_OVER)
1621         surface->has_only_op_over = FALSE;
1622
1623     if (source->type == CAIRO_PATTERN_TYPE_SURFACE) {
1624         cairo_surface_pattern_t *surf_pat = (cairo_surface_pattern_t *) source;
1625         cairo_surface_t *surf = surf_pat->surface;
1626         cairo_surface_t *free_me = NULL;
1627
1628         if (_cairo_surface_is_snapshot (surf))
1629             free_me = surf = _cairo_surface_snapshot_get_target (surf);
1630
1631         if (surf->type == CAIRO_SURFACE_TYPE_RECORDING) {
1632             cairo_recording_surface_t *rec_surf = (cairo_recording_surface_t *) surf;
1633
1634             if (! _cairo_recording_surface_has_only_bilevel_alpha (rec_surf))
1635                 surface->has_bilevel_alpha = FALSE;
1636
1637             if (! _cairo_recording_surface_has_only_op_over (rec_surf))
1638                 surface->has_only_op_over = FALSE;
1639
1640         } else if (surf->type == CAIRO_SURFACE_TYPE_IMAGE) {
1641             cairo_image_surface_t *img_surf = (cairo_image_surface_t *) surf;
1642
1643             if (_cairo_image_analyze_transparency (img_surf) == CAIRO_IMAGE_HAS_ALPHA)
1644                 surface->has_bilevel_alpha = FALSE;
1645
1646         } else {
1647             if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
1648                 surface->has_bilevel_alpha = FALSE;
1649         }
1650
1651         cairo_surface_destroy (free_me);
1652         return;
1653
1654     } else if (source->type == CAIRO_PATTERN_TYPE_RASTER_SOURCE) {
1655         cairo_surface_t *image;
1656         cairo_surface_t *raster;
1657
1658         image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
1659         raster = _cairo_raster_source_pattern_acquire (source, image, NULL);
1660         cairo_surface_destroy (image);
1661         if (raster) {
1662             if (raster->type == CAIRO_SURFACE_TYPE_IMAGE) {
1663                 if (_cairo_image_analyze_transparency ((cairo_image_surface_t *)raster) == CAIRO_IMAGE_HAS_ALPHA)
1664                     surface->has_bilevel_alpha = FALSE;
1665             }
1666
1667             _cairo_raster_source_pattern_release (source, raster);
1668             if (raster->type == CAIRO_SURFACE_TYPE_IMAGE)
1669                 return;
1670         }
1671     }
1672
1673     if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
1674         surface->has_bilevel_alpha = FALSE;
1675 }
1676
1677 static cairo_status_t
1678 _cairo_recording_surface_replay_internal (cairo_recording_surface_t     *surface,
1679                                           const cairo_rectangle_int_t *surface_extents,
1680                                           const cairo_matrix_t *surface_transform,
1681                                           cairo_surface_t            *target,
1682                                           const cairo_clip_t *target_clip,
1683                                           cairo_recording_replay_type_t type,
1684                                           cairo_recording_region_type_t region)
1685 {
1686     cairo_surface_wrapper_t wrapper;
1687     cairo_command_t **elements;
1688     cairo_bool_t replay_all =
1689         type == CAIRO_RECORDING_REPLAY &&
1690         region == CAIRO_RECORDING_REGION_ALL;
1691     cairo_int_status_t status = CAIRO_STATUS_SUCCESS;
1692     cairo_rectangle_int_t extents;
1693     cairo_bool_t use_indices = FALSE;
1694     const cairo_rectangle_int_t *r;
1695     unsigned int i, num_elements;
1696
1697     if (unlikely (surface->base.status))
1698         return surface->base.status;
1699
1700     if (unlikely (target->status))
1701         return target->status;
1702
1703     if (unlikely (surface->base.finished))
1704         return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
1705
1706     if (surface->base.is_clear)
1707         return CAIRO_STATUS_SUCCESS;
1708
1709     assert (_cairo_surface_is_recording (&surface->base));
1710
1711     _cairo_surface_wrapper_init (&wrapper, target);
1712     if (surface_extents)
1713         _cairo_surface_wrapper_intersect_extents (&wrapper, surface_extents);
1714     r = &_cairo_unbounded_rectangle;
1715     if (! surface->unbounded) {
1716         _cairo_surface_wrapper_intersect_extents (&wrapper, &surface->extents);
1717         r = &surface->extents;
1718     }
1719     _cairo_surface_wrapper_set_inverse_transform (&wrapper, surface_transform);
1720     _cairo_surface_wrapper_set_clip (&wrapper, target_clip);
1721
1722     /* Compute the extents of the target clip in recorded device space */
1723     if (! _cairo_surface_wrapper_get_target_extents (&wrapper, &extents))
1724         goto done;
1725
1726     surface->has_bilevel_alpha = TRUE;
1727     surface->has_only_op_over = TRUE;
1728
1729     num_elements = surface->commands.num_elements;
1730     elements = _cairo_array_index (&surface->commands, 0);
1731     if (elements == NULL) {
1732         status = CAIRO_STATUS_NULL_POINTER;
1733         goto done;
1734     }
1735
1736     if (extents.width < r->width || extents.height < r->height) {
1737         num_elements =
1738             _cairo_recording_surface_get_visible_commands (surface, &extents);
1739         use_indices = num_elements != surface->commands.num_elements;
1740     }
1741
1742     for (i = 0; i < num_elements; i++) {
1743         cairo_command_t *command = elements[use_indices ? surface->indices[i] : i];
1744
1745         if (! replay_all && command->header.region != region)
1746             continue;
1747
1748         if (! _cairo_rectangle_intersects (&extents, &command->header.extents))
1749             continue;
1750
1751         switch (command->header.type) {
1752         case CAIRO_COMMAND_PAINT:
1753             status = _cairo_surface_wrapper_paint (&wrapper,
1754                                                    command->header.op,
1755                                                    &command->paint.source.base,
1756                                                    command->header.clip);
1757             if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1758                 _cairo_recording_surface_merge_source_attributes (surface,
1759                                                                   command->header.op,
1760                                                                   &command->paint.source.base);
1761             }
1762             break;
1763
1764         case CAIRO_COMMAND_MASK:
1765             status = _cairo_surface_wrapper_mask (&wrapper,
1766                                                   command->header.op,
1767                                                   &command->mask.source.base,
1768                                                   &command->mask.mask.base,
1769                                                   command->header.clip);
1770             if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1771                 _cairo_recording_surface_merge_source_attributes (surface,
1772                                                                   command->header.op,
1773                                                                   &command->mask.source.base);
1774                 _cairo_recording_surface_merge_source_attributes (surface,
1775                                                                   command->header.op,
1776                                                                   &command->mask.mask.base);
1777             }
1778             break;
1779
1780         case CAIRO_COMMAND_STROKE:
1781             status = _cairo_surface_wrapper_stroke (&wrapper,
1782                                                     command->header.op,
1783                                                     &command->stroke.source.base,
1784                                                     &command->stroke.path,
1785                                                     &command->stroke.style,
1786                                                     &command->stroke.ctm,
1787                                                     &command->stroke.ctm_inverse,
1788                                                     command->stroke.tolerance,
1789                                                     command->stroke.antialias,
1790                                                     command->header.clip);
1791             if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1792                 _cairo_recording_surface_merge_source_attributes (surface,
1793                                                                   command->header.op,
1794                                                                   &command->stroke.source.base);
1795             }
1796             break;
1797
1798         case CAIRO_COMMAND_FILL:
1799             status = CAIRO_INT_STATUS_UNSUPPORTED;
1800             if (_cairo_surface_wrapper_has_fill_stroke (&wrapper)) {
1801                 cairo_command_t *stroke_command;
1802
1803                 stroke_command = NULL;
1804                 if (type != CAIRO_RECORDING_CREATE_REGIONS && i < num_elements - 1)
1805                     stroke_command = elements[i + 1];
1806
1807                 if (stroke_command != NULL &&
1808                     type == CAIRO_RECORDING_REPLAY &&
1809                     region != CAIRO_RECORDING_REGION_ALL)
1810                 {
1811                     if (stroke_command->header.region != region)
1812                         stroke_command = NULL;
1813                 }
1814
1815                 if (stroke_command != NULL &&
1816                     stroke_command->header.type == CAIRO_COMMAND_STROKE &&
1817                     _cairo_path_fixed_equal (&command->fill.path,
1818                                              &stroke_command->stroke.path) &&
1819                     _cairo_clip_equal (command->header.clip,
1820                                        stroke_command->header.clip))
1821                 {
1822                     status = _cairo_surface_wrapper_fill_stroke (&wrapper,
1823                                                                  command->header.op,
1824                                                                  &command->fill.source.base,
1825                                                                  command->fill.fill_rule,
1826                                                                  command->fill.tolerance,
1827                                                                  command->fill.antialias,
1828                                                                  &command->fill.path,
1829                                                                  stroke_command->header.op,
1830                                                                  &stroke_command->stroke.source.base,
1831                                                                  &stroke_command->stroke.style,
1832                                                                  &stroke_command->stroke.ctm,
1833                                                                  &stroke_command->stroke.ctm_inverse,
1834                                                                  stroke_command->stroke.tolerance,
1835                                                                  stroke_command->stroke.antialias,
1836                                                                  command->header.clip);
1837                     if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1838                         _cairo_recording_surface_merge_source_attributes (surface,
1839                                                                           command->header.op,
1840                                                                           &command->fill.source.base);
1841                         _cairo_recording_surface_merge_source_attributes (surface,
1842                                                                           command->header.op,
1843                                                                           &command->stroke.source.base);
1844                     }
1845                     i++;
1846                 }
1847             }
1848             if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1849                 status = _cairo_surface_wrapper_fill (&wrapper,
1850                                                       command->header.op,
1851                                                       &command->fill.source.base,
1852                                                       &command->fill.path,
1853                                                       command->fill.fill_rule,
1854                                                       command->fill.tolerance,
1855                                                       command->fill.antialias,
1856                                                       command->header.clip);
1857                 if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1858                     _cairo_recording_surface_merge_source_attributes (surface,
1859                                                                       command->header.op,
1860                                                                       &command->fill.source.base);
1861                 }
1862             }
1863             break;
1864
1865         case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
1866             status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
1867                                                               command->header.op,
1868                                                               &command->show_text_glyphs.source.base,
1869                                                               command->show_text_glyphs.utf8, command->show_text_glyphs.utf8_len,
1870                                                               command->show_text_glyphs.glyphs, command->show_text_glyphs.num_glyphs,
1871                                                               command->show_text_glyphs.clusters, command->show_text_glyphs.num_clusters,
1872                                                               command->show_text_glyphs.cluster_flags,
1873                                                               command->show_text_glyphs.scaled_font,
1874                                                               command->header.clip);
1875             if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1876                 _cairo_recording_surface_merge_source_attributes (surface,
1877                                                                   command->header.op,
1878                                                                   &command->show_text_glyphs.source.base);
1879             }
1880             break;
1881
1882         default:
1883             ASSERT_NOT_REACHED;
1884         }
1885
1886         if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1887             if (status == CAIRO_INT_STATUS_SUCCESS) {
1888                 command->header.region = CAIRO_RECORDING_REGION_NATIVE;
1889             } else if (status == CAIRO_INT_STATUS_IMAGE_FALLBACK) {
1890                 command->header.region = CAIRO_RECORDING_REGION_IMAGE_FALLBACK;
1891                 status = CAIRO_INT_STATUS_SUCCESS;
1892             } else {
1893                 assert (_cairo_int_status_is_error (status));
1894             }
1895         }
1896
1897         if (unlikely (status))
1898             break;
1899     }
1900
1901 done:
1902     _cairo_surface_wrapper_fini (&wrapper);
1903     return _cairo_surface_set_error (&surface->base, status);
1904 }
1905
1906 cairo_status_t
1907 _cairo_recording_surface_replay_one (cairo_recording_surface_t  *surface,
1908                                      long unsigned index,
1909                                      cairo_surface_t         *target)
1910 {
1911     cairo_surface_wrapper_t wrapper;
1912     cairo_command_t **elements, *command;
1913     cairo_int_status_t status;
1914
1915     if (unlikely (surface->base.status))
1916         return surface->base.status;
1917
1918     if (unlikely (target->status))
1919         return target->status;
1920
1921     if (unlikely (surface->base.finished))
1922         return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
1923
1924     assert (_cairo_surface_is_recording (&surface->base));
1925
1926     /* XXX
1927      * Use a surface wrapper because we may want to do transformed
1928      * replay in the future.
1929      */
1930     _cairo_surface_wrapper_init (&wrapper, target);
1931
1932     if (index > surface->commands.num_elements)
1933         return _cairo_error (CAIRO_STATUS_READ_ERROR);
1934
1935     elements = _cairo_array_index (&surface->commands, 0);
1936     if (elements == NULL)
1937         return _cairo_error (CAIRO_STATUS_NULL_POINTER);
1938
1939     command = elements[index];
1940     switch (command->header.type) {
1941     case CAIRO_COMMAND_PAINT:
1942         status = _cairo_surface_wrapper_paint (&wrapper,
1943                                                command->header.op,
1944                                                &command->paint.source.base,
1945                                                command->header.clip);
1946         break;
1947
1948     case CAIRO_COMMAND_MASK:
1949         status = _cairo_surface_wrapper_mask (&wrapper,
1950                                               command->header.op,
1951                                               &command->mask.source.base,
1952                                               &command->mask.mask.base,
1953                                               command->header.clip);
1954         break;
1955
1956     case CAIRO_COMMAND_STROKE:
1957         status = _cairo_surface_wrapper_stroke (&wrapper,
1958                                                 command->header.op,
1959                                                 &command->stroke.source.base,
1960                                                 &command->stroke.path,
1961                                                 &command->stroke.style,
1962                                                 &command->stroke.ctm,
1963                                                 &command->stroke.ctm_inverse,
1964                                                 command->stroke.tolerance,
1965                                                 command->stroke.antialias,
1966                                                 command->header.clip);
1967         break;
1968
1969     case CAIRO_COMMAND_FILL:
1970         status = _cairo_surface_wrapper_fill (&wrapper,
1971                                               command->header.op,
1972                                               &command->fill.source.base,
1973                                               &command->fill.path,
1974                                               command->fill.fill_rule,
1975                                               command->fill.tolerance,
1976                                               command->fill.antialias,
1977                                               command->header.clip);
1978         break;
1979
1980     case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
1981         status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
1982                                                           command->header.op,
1983                                                           &command->show_text_glyphs.source.base,
1984                                                           command->show_text_glyphs.utf8, command->show_text_glyphs.utf8_len,
1985                                                           command->show_text_glyphs.glyphs, command->show_text_glyphs.num_glyphs,
1986                                                           command->show_text_glyphs.clusters, command->show_text_glyphs.num_clusters,
1987                                                           command->show_text_glyphs.cluster_flags,
1988                                                           command->show_text_glyphs.scaled_font,
1989                                                           command->header.clip);
1990         break;
1991
1992     default:
1993         ASSERT_NOT_REACHED;
1994     }
1995
1996     _cairo_surface_wrapper_fini (&wrapper);
1997     return _cairo_surface_set_error (&surface->base, status);
1998 }
1999 /**
2000  * _cairo_recording_surface_replay:
2001  * @surface: the #cairo_recording_surface_t
2002  * @target: a target #cairo_surface_t onto which to replay the operations
2003  * @width_pixels: width of the surface, in pixels
2004  * @height_pixels: height of the surface, in pixels
2005  *
2006  * A recording surface can be "replayed" against any target surface,
2007  * after which the results in target will be identical to the results
2008  * that would have been obtained if the original operations applied to
2009  * the recording surface had instead been applied to the target surface.
2010  **/
2011 cairo_status_t
2012 _cairo_recording_surface_replay (cairo_surface_t *surface,
2013                                  cairo_surface_t *target)
2014 {
2015     return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, NULL,
2016                                                      target, NULL,
2017                                                      CAIRO_RECORDING_REPLAY,
2018                                                      CAIRO_RECORDING_REGION_ALL);
2019 }
2020
2021 cairo_status_t
2022 _cairo_recording_surface_replay_with_clip (cairo_surface_t *surface,
2023                                            const cairo_matrix_t *surface_transform,
2024                                            cairo_surface_t *target,
2025                                            const cairo_clip_t *target_clip)
2026 {
2027     return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, surface_transform,
2028                                                      target, target_clip,
2029                                                      CAIRO_RECORDING_REPLAY,
2030                                                      CAIRO_RECORDING_REGION_ALL);
2031 }
2032
2033 /* Replay recording to surface. When the return status of each operation is
2034  * one of %CAIRO_STATUS_SUCCESS, %CAIRO_INT_STATUS_UNSUPPORTED, or
2035  * %CAIRO_INT_STATUS_FLATTEN_TRANSPARENCY the status of each operation
2036  * will be stored in the recording surface. Any other status will abort the
2037  * replay and return the status.
2038  */
2039 cairo_status_t
2040 _cairo_recording_surface_replay_and_create_regions (cairo_surface_t *surface,
2041                                                     cairo_surface_t *target)
2042 {
2043     return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, NULL,
2044                                                      target, NULL,
2045                                                      CAIRO_RECORDING_CREATE_REGIONS,
2046                                                      CAIRO_RECORDING_REGION_ALL);
2047 }
2048
2049 cairo_status_t
2050 _cairo_recording_surface_replay_region (cairo_surface_t          *surface,
2051                                         const cairo_rectangle_int_t *surface_extents,
2052                                         cairo_surface_t          *target,
2053                                         cairo_recording_region_type_t  region)
2054 {
2055     return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface,
2056                                                      surface_extents, NULL,
2057                                                      target, NULL,
2058                                                      CAIRO_RECORDING_REPLAY,
2059                                                      region);
2060 }
2061
2062 static cairo_status_t
2063 _recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
2064                                  cairo_box_t *bbox,
2065                                  const cairo_matrix_t *transform)
2066 {
2067     cairo_surface_t *null_surface;
2068     cairo_surface_t *analysis_surface;
2069     cairo_status_t status;
2070
2071     null_surface = _cairo_null_surface_create (surface->base.content);
2072     analysis_surface = _cairo_analysis_surface_create (null_surface);
2073     cairo_surface_destroy (null_surface);
2074
2075     status = analysis_surface->status;
2076     if (unlikely (status)) {
2077         cairo_surface_destroy (analysis_surface);
2078         return status;
2079     }
2080
2081     if (transform != NULL)
2082         _cairo_analysis_surface_set_ctm (analysis_surface, transform);
2083
2084     status = _cairo_recording_surface_replay (&surface->base, analysis_surface);
2085     _cairo_analysis_surface_get_bounding_box (analysis_surface, bbox);
2086     cairo_surface_destroy (analysis_surface);
2087
2088     return status;
2089 }
2090
2091 /**
2092  * cairo_recording_surface_ink_extents:
2093  * @surface: a #cairo_recording_surface_t
2094  * @x0: the x-coordinate of the top-left of the ink bounding box
2095  * @y0: the y-coordinate of the top-left of the ink bounding box
2096  * @width: the width of the ink bounding box
2097  * @height: the height of the ink bounding box
2098  *
2099  * Measures the extents of the operations stored within the recording-surface.
2100  * This is useful to compute the required size of an image surface (or
2101  * equivalent) into which to replay the full sequence of drawing operations.
2102  *
2103  * Since: 1.10
2104  **/
2105 void
2106 cairo_recording_surface_ink_extents (cairo_surface_t *surface,
2107                                      double *x0,
2108                                      double *y0,
2109                                      double *width,
2110                                      double *height)
2111 {
2112     cairo_status_t status;
2113     cairo_box_t bbox;
2114
2115     memset (&bbox, 0, sizeof (bbox));
2116
2117     if (surface->status || ! _cairo_surface_is_recording (surface)) {
2118         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
2119         goto DONE;
2120     }
2121
2122     status = _recording_surface_get_ink_bbox ((cairo_recording_surface_t *) surface,
2123                                          &bbox,
2124                                          NULL);
2125     if (unlikely (status))
2126         status = _cairo_surface_set_error (surface, status);
2127
2128 DONE:
2129     if (x0)
2130         *x0 = _cairo_fixed_to_double (bbox.p1.x);
2131     if (y0)
2132         *y0 = _cairo_fixed_to_double (bbox.p1.y);
2133     if (width)
2134         *width = _cairo_fixed_to_double (bbox.p2.x - bbox.p1.x);
2135     if (height)
2136         *height = _cairo_fixed_to_double (bbox.p2.y - bbox.p1.y);
2137 }
2138
2139 cairo_status_t
2140 _cairo_recording_surface_get_bbox (cairo_recording_surface_t *surface,
2141                                    cairo_box_t *bbox,
2142                                    const cairo_matrix_t *transform)
2143 {
2144     if (! surface->unbounded) {
2145         _cairo_box_from_rectangle (bbox, &surface->extents);
2146         if (transform != NULL)
2147             _cairo_matrix_transform_bounding_box_fixed (transform, bbox, NULL);
2148
2149         return CAIRO_STATUS_SUCCESS;
2150     }
2151
2152     return _recording_surface_get_ink_bbox (surface, bbox, transform);
2153 }
2154
2155 cairo_status_t
2156 _cairo_recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
2157                                        cairo_box_t *bbox,
2158                                        const cairo_matrix_t *transform)
2159 {
2160     return _recording_surface_get_ink_bbox (surface, bbox, transform);
2161 }
2162
2163 /**
2164  * cairo_recording_surface_get_extents:
2165  * @surface: a #cairo_recording_surface_t
2166  * @extents: the #cairo_rectangle_t to be assigned the extents
2167  *
2168  * Get the extents of the recording-surface.
2169  *
2170  * Return value: %TRUE if the surface is bounded, of recording type, and
2171  * not in an error state, otherwise %FALSE
2172  *
2173  * Since: 1.12
2174  **/
2175 cairo_bool_t
2176 cairo_recording_surface_get_extents (cairo_surface_t *surface,
2177                                      cairo_rectangle_t *extents)
2178 {
2179     cairo_recording_surface_t *record;
2180
2181     if (surface->status || ! _cairo_surface_is_recording (surface)) {
2182         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
2183         return FALSE;
2184     }
2185
2186     record = (cairo_recording_surface_t *)surface;
2187     if (record->unbounded)
2188         return FALSE;
2189
2190     *extents = record->extents_pixels;
2191     return TRUE;
2192 }
2193
2194 cairo_bool_t
2195 _cairo_recording_surface_has_only_bilevel_alpha (cairo_recording_surface_t *surface)
2196 {
2197     return surface->has_bilevel_alpha;
2198 }
2199
2200 cairo_bool_t
2201 _cairo_recording_surface_has_only_op_over (cairo_recording_surface_t *surface)
2202 {
2203     return surface->has_only_op_over;
2204 }