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
4 * Copyright © 2005 Red Hat, Inc
5 * Copyright © 2007 Adrian Johnson
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.
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
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/
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.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is Red Hat, Inc.
35 * Kristian Høgsberg <krh@redhat.com>
36 * Carl Worth <cworth@cworth.org>
37 * Adrian Johnson <ajohnson@redneon.com>
41 * SECTION:cairo-recording
42 * @Title: Recording Surfaces
43 * @Short_Description: Records all drawing operations
44 * @See_Also: #cairo_surface_t
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.
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>
59 * cr = cairo_create (target);
60 * cairo_set_source_surface (cr, recording_surface, 0.0, 0.0);
63 * </programlisting></informalexample>
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.
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.
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"
95 CAIRO_RECORDING_REPLAY,
96 CAIRO_RECORDING_CREATE_REGIONS
97 } cairo_recording_replay_type_t;
99 static const cairo_surface_backend_t cairo_recording_surface_backend;
102 * CAIRO_HAS_RECORDING_SURFACE:
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.
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.
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).
120 static int bbtree_left_or_right (struct bbtree *bbt,
121 const cairo_box_t *box)
126 cairo_box_t *e = &bbt->left->extents;
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);
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);
140 cairo_box_t *e = &bbt->right->extents;
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);
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);
153 return left <= right;
156 #define INVALID_CHAIN ((cairo_command_header_t *)-1)
158 static struct bbtree *
159 bbtree_new (const cairo_box_t *box, cairo_command_header_t *chain)
161 struct bbtree *bbt = malloc (sizeof (*bbt));
165 bbt->left = bbt->right = NULL;
171 bbtree_init (struct bbtree *bbt, cairo_command_header_t *header)
173 _cairo_box_from_rectangle (&bbt->extents, &header->extents);
177 static cairo_status_t
178 bbtree_add (struct bbtree *bbt,
179 cairo_command_header_t *header,
180 const cairo_box_t *box)
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)
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);
192 bbtree_add (bbt->left, bbt->chain, &bbt->extents);
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);
199 bbtree_add (bbt->right, bbt->chain, &bbt->extents);
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);
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)
214 cairo_command_header_t *last = header;
215 while (last->chain) /* expected to be infrequent */
217 last->chain = bbt->chain;
219 return CAIRO_STATUS_SUCCESS;
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);
228 return bbtree_add (bbt->left, header, box);
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);
235 return bbtree_add (bbt->right, header, box);
238 return CAIRO_STATUS_SUCCESS;
241 static void bbtree_del (struct bbtree *bbt)
244 bbtree_del (bbt->left);
246 bbtree_del (bbt->right);
251 static cairo_bool_t box_outside (const cairo_box_t *a, const cairo_box_t *b)
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;
259 bbtree_foreach_mark_visible (struct bbtree *bbt,
260 const cairo_box_t *box,
261 unsigned int **indices)
263 cairo_command_header_t *chain;
265 for (chain = bbt->chain; chain; chain = chain->chain)
266 *(*indices)++ = chain->index;
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);
274 static inline int intcmp (const unsigned int a, const unsigned int b)
278 CAIRO_COMBSORT_DECLARE (sort_indices, unsigned int, intcmp)
280 static inline int sizecmp (unsigned int a, unsigned int b, cairo_command_header_t **elements)
282 const cairo_rectangle_int_t *r;
284 r = &elements[a]->extents;
285 a = r->width * r->height;
287 r = &elements[b]->extents;
288 b = r->width * r->height;
292 CAIRO_COMBSORT_DECLARE_WITH_DATA (sort_commands, unsigned int, sizecmp)
295 _cairo_recording_surface_destroy_bbtree (cairo_recording_surface_t *surface)
297 cairo_command_t **elements;
300 if (surface->bbtree.chain == INVALID_CHAIN)
303 if (surface->bbtree.left) {
304 bbtree_del (surface->bbtree.left);
305 surface->bbtree.left = NULL;
307 if (surface->bbtree.right) {
308 bbtree_del (surface->bbtree.right);
309 surface->bbtree.right = NULL;
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;
317 surface->bbtree.chain = INVALID_CHAIN;
320 static cairo_status_t
321 _cairo_recording_surface_create_bbtree (cairo_recording_surface_t *surface)
323 cairo_command_t **elements = _cairo_array_index (&surface->commands, 0);
324 if (unlikely (elements == NULL))
325 return _cairo_error (CAIRO_STATUS_NULL_POINTER);
327 unsigned int *indices;
328 cairo_status_t status;
329 unsigned int i, count;
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);
338 surface->num_indices = count;
341 indices = surface->indices;
342 for (i = 0; i < count; i++)
345 sort_commands (indices, count, elements);
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;
352 _cairo_box_from_rectangle (&box, &header->extents);
353 status = bbtree_add (&surface->bbtree, header, &box);
354 if (unlikely (status))
358 return CAIRO_STATUS_SUCCESS;
361 bbtree_del (&surface->bbtree);
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.
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.
376 * The recording phase of the recording surface is careful to snapshot all
377 * necessary objects (paths, patterns, etc.), in order to achieve
380 * Return value: a pointer to the newly created surface. The caller
381 * owns the surface and should call cairo_surface_destroy() when done
387 cairo_recording_surface_create (cairo_content_t content,
388 const cairo_rectangle_t *extents)
390 cairo_recording_surface_t *surface;
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));
396 _cairo_surface_init (&surface->base,
397 &cairo_recording_surface_backend,
402 surface->unbounded = TRUE;
404 /* unbounded -> 'infinite' extents */
405 if (extents != NULL) {
406 surface->extents_pixels = *extents;
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;
414 surface->unbounded = FALSE;
417 _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
419 surface->base.is_clear = TRUE;
421 surface->bbtree.left = surface->bbtree.right = NULL;
422 surface->bbtree.chain = INVALID_CHAIN;
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;
430 return &surface->base;
432 slim_hidden_def (cairo_recording_surface_create);
434 static cairo_surface_t *
435 _cairo_recording_surface_create_similar (void *abstract_surface,
436 cairo_content_t content,
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);
447 static cairo_status_t
448 _cairo_recording_surface_finish (void *abstract_surface)
450 cairo_recording_surface_t *surface = abstract_surface;
451 cairo_command_t **elements;
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];
459 switch (command->header.type) {
460 case CAIRO_COMMAND_PAINT:
461 _cairo_pattern_fini (&command->paint.source.base);
464 case CAIRO_COMMAND_MASK:
465 _cairo_pattern_fini (&command->mask.source.base);
466 _cairo_pattern_fini (&command->mask.mask.base);
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);
475 case CAIRO_COMMAND_FILL:
476 _cairo_pattern_fini (&command->fill.source.base);
477 _cairo_path_fixed_fini (&command->fill.path);
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);
492 _cairo_clip_destroy (command->header.clip);
496 _cairo_array_fini (&surface->commands);
498 if (surface->bbtree.left)
499 bbtree_del (surface->bbtree.left);
500 if (surface->bbtree.right)
501 bbtree_del (surface->bbtree.right);
503 free (surface->indices);
505 return CAIRO_STATUS_SUCCESS;
509 cairo_surface_t base;
510 cairo_surface_t *image;
513 static cairo_status_t
514 proxy_acquire_source_image (void *abstract_surface,
515 cairo_image_surface_t **image_out,
518 struct proxy *proxy = abstract_surface;
519 return _cairo_surface_acquire_source_image (proxy->image, image_out, image_extra);
523 proxy_release_source_image (void *abstract_surface,
524 cairo_image_surface_t *image,
527 struct proxy *proxy = abstract_surface;
528 _cairo_surface_release_source_image (proxy->image, image, image_extra);
531 static cairo_status_t
532 proxy_finish (void *abstract_surface)
534 return CAIRO_STATUS_SUCCESS;
537 static const cairo_surface_backend_t proxy_backend = {
538 CAIRO_INTERNAL_SURFACE_TYPE_NULL,
542 NULL, /* create similar */
543 NULL, /* create similar image */
544 NULL, /* map to image */
545 NULL, /* unmap image */
547 _cairo_surface_default_source,
548 proxy_acquire_source_image,
549 proxy_release_source_image,
552 static cairo_surface_t *
553 attach_proxy (cairo_surface_t *source,
554 cairo_surface_t *image)
558 proxy = malloc (sizeof (*proxy));
559 if (unlikely (proxy == NULL))
560 return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
562 _cairo_surface_init (&proxy->base, &proxy_backend, NULL, image->content);
564 proxy->image = image;
565 _cairo_surface_attach_snapshot (source, &proxy->base, NULL);
571 detach_proxy (cairo_surface_t *source,
572 cairo_surface_t *proxy)
574 cairo_surface_finish (proxy);
575 cairo_surface_destroy (proxy);
578 static cairo_surface_t *
579 get_proxy (cairo_surface_t *proxy)
581 return ((struct proxy *)proxy)->image;
584 static cairo_status_t
585 _cairo_recording_surface_acquire_source_image (void *abstract_surface,
586 cairo_image_surface_t **image_out,
589 cairo_recording_surface_t *surface = abstract_surface;
590 cairo_surface_t *image, *proxy;
591 cairo_status_t status;
593 proxy = _cairo_surface_has_snapshot (abstract_surface, &proxy_backend);
595 *image_out = (cairo_image_surface_t *)
596 cairo_surface_reference (get_proxy (proxy));
598 return CAIRO_STATUS_SUCCESS;
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);
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);
616 if (unlikely (status)) {
617 cairo_surface_destroy (image);
621 *image_out = (cairo_image_surface_t *) image;
623 return CAIRO_STATUS_SUCCESS;
627 _cairo_recording_surface_release_source_image (void *abstract_surface,
628 cairo_image_surface_t *image,
631 cairo_surface_destroy (&image->base);
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,
639 cairo_composite_rectangles_t *composite)
641 cairo_status_t status = CAIRO_STATUS_SUCCESS;
643 command->type = type;
645 command->region = CAIRO_RECORDING_REGION_ALL;
647 command->extents = composite->unbounded;
648 command->chain = NULL;
649 command->index = surface->commands.num_elements;
652 command->clip = NULL;
653 if (! _cairo_composite_rectangles_can_reduce_clip (composite,
656 command->clip = composite->clip;
657 composite->clip = NULL;
664 _cairo_recording_surface_break_self_copy_loop (cairo_recording_surface_t *surface)
666 cairo_surface_flush (&surface->base);
669 static cairo_status_t
670 _cairo_recording_surface_commit (cairo_recording_surface_t *surface,
671 cairo_command_header_t *command)
673 _cairo_recording_surface_break_self_copy_loop (surface);
674 return _cairo_array_append (&surface->commands, &command);
678 _cairo_recording_surface_reset (cairo_recording_surface_t *surface)
680 /* Reset the commands and temporaries */
681 _cairo_recording_surface_finish (surface);
683 surface->bbtree.left = surface->bbtree.right = NULL;
684 surface->bbtree.chain = INVALID_CHAIN;
686 surface->indices = NULL;
687 surface->num_indices = 0;
689 _cairo_array_init (&surface->commands, sizeof (cairo_command_t *));
693 is_identity_recording_pattern (const cairo_pattern_t *pattern)
695 cairo_surface_t *surface;
697 if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
700 if (!_cairo_matrix_is_identity(&pattern->matrix))
703 surface = ((cairo_surface_pattern_t *)pattern)->surface;
704 return surface->backend->type == CAIRO_SURFACE_TYPE_RECORDING;
707 static cairo_int_status_t
708 _cairo_recording_surface_paint (void *abstract_surface,
710 const cairo_pattern_t *source,
711 const cairo_clip_t *clip)
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;
718 TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
720 if (op == CAIRO_OPERATOR_CLEAR && clip == NULL) {
721 if (surface->optimize_clears) {
722 _cairo_recording_surface_reset (surface);
723 return CAIRO_STATUS_SUCCESS;
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)))))
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);
739 status = _cairo_composite_rectangles_init_for_paint (&composite,
743 if (unlikely (status))
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;
752 status = _command_init (surface,
753 &command->header, CAIRO_COMMAND_PAINT, op,
755 if (unlikely (status))
756 goto CLEANUP_COMMAND;
758 status = _cairo_pattern_init_snapshot (&command->source.base, source);
759 if (unlikely (status))
760 goto CLEANUP_COMMAND;
762 status = _cairo_recording_surface_commit (surface, &command->header);
763 if (unlikely (status))
766 _cairo_recording_surface_destroy_bbtree (surface);
768 _cairo_composite_rectangles_fini (&composite);
769 return CAIRO_STATUS_SUCCESS;
772 _cairo_pattern_fini (&command->source.base);
774 _cairo_clip_destroy (command->header.clip);
777 _cairo_composite_rectangles_fini (&composite);
781 static cairo_int_status_t
782 _cairo_recording_surface_mask (void *abstract_surface,
784 const cairo_pattern_t *source,
785 const cairo_pattern_t *mask,
786 const cairo_clip_t *clip)
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;
793 TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
795 status = _cairo_composite_rectangles_init_for_mask (&composite,
799 if (unlikely (status))
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;
808 status = _command_init (surface,
809 &command->header, CAIRO_COMMAND_MASK, op,
811 if (unlikely (status))
812 goto CLEANUP_COMMAND;
814 status = _cairo_pattern_init_snapshot (&command->source.base, source);
815 if (unlikely (status))
816 goto CLEANUP_COMMAND;
818 status = _cairo_pattern_init_snapshot (&command->mask.base, mask);
819 if (unlikely (status))
822 status = _cairo_recording_surface_commit (surface, &command->header);
823 if (unlikely (status))
826 _cairo_recording_surface_destroy_bbtree (surface);
828 _cairo_composite_rectangles_fini (&composite);
829 return CAIRO_STATUS_SUCCESS;
832 _cairo_pattern_fini (&command->mask.base);
834 _cairo_pattern_fini (&command->source.base);
836 _cairo_clip_destroy (command->header.clip);
839 _cairo_composite_rectangles_fini (&composite);
843 static cairo_int_status_t
844 _cairo_recording_surface_stroke (void *abstract_surface,
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,
852 cairo_antialias_t antialias,
853 const cairo_clip_t *clip)
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;
860 TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
862 status = _cairo_composite_rectangles_init_for_stroke (&composite,
867 if (unlikely (status))
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;
876 status = _command_init (surface,
877 &command->header, CAIRO_COMMAND_STROKE, op,
879 if (unlikely (status))
880 goto CLEANUP_COMMAND;
882 status = _cairo_pattern_init_snapshot (&command->source.base, source);
883 if (unlikely (status))
884 goto CLEANUP_COMMAND;
886 status = _cairo_path_fixed_init_copy (&command->path, path);
887 if (unlikely (status))
890 status = _cairo_stroke_style_init_copy (&command->style, style);
891 if (unlikely (status))
895 command->ctm_inverse = *ctm_inverse;
896 command->tolerance = tolerance;
897 command->antialias = antialias;
899 status = _cairo_recording_surface_commit (surface, &command->header);
900 if (unlikely (status))
903 _cairo_recording_surface_destroy_bbtree (surface);
905 _cairo_composite_rectangles_fini (&composite);
906 return CAIRO_STATUS_SUCCESS;
909 _cairo_stroke_style_fini (&command->style);
911 _cairo_path_fixed_fini (&command->path);
913 _cairo_pattern_fini (&command->source.base);
915 _cairo_clip_destroy (command->header.clip);
918 _cairo_composite_rectangles_fini (&composite);
922 static cairo_int_status_t
923 _cairo_recording_surface_fill (void *abstract_surface,
925 const cairo_pattern_t *source,
926 const cairo_path_fixed_t *path,
927 cairo_fill_rule_t fill_rule,
929 cairo_antialias_t antialias,
930 const cairo_clip_t *clip)
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;
937 TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
939 status = _cairo_composite_rectangles_init_for_fill (&composite,
943 if (unlikely (status))
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;
952 status =_command_init (surface,
953 &command->header, CAIRO_COMMAND_FILL, op,
955 if (unlikely (status))
956 goto CLEANUP_COMMAND;
958 status = _cairo_pattern_init_snapshot (&command->source.base, source);
959 if (unlikely (status))
960 goto CLEANUP_COMMAND;
962 status = _cairo_path_fixed_init_copy (&command->path, path);
963 if (unlikely (status))
966 command->fill_rule = fill_rule;
967 command->tolerance = tolerance;
968 command->antialias = antialias;
970 status = _cairo_recording_surface_commit (surface, &command->header);
971 if (unlikely (status))
974 _cairo_recording_surface_destroy_bbtree (surface);
976 _cairo_composite_rectangles_fini (&composite);
977 return CAIRO_STATUS_SUCCESS;
980 _cairo_path_fixed_fini (&command->path);
982 _cairo_pattern_fini (&command->source.base);
984 _cairo_clip_destroy (command->header.clip);
987 _cairo_composite_rectangles_fini (&composite);
992 _cairo_recording_surface_has_show_text_glyphs (void *abstract_surface)
997 static cairo_int_status_t
998 _cairo_recording_surface_show_text_glyphs (void *abstract_surface,
1000 const cairo_pattern_t *source,
1003 cairo_glyph_t *glyphs,
1005 const cairo_text_cluster_t *clusters,
1007 cairo_text_cluster_flags_t cluster_flags,
1008 cairo_scaled_font_t *scaled_font,
1009 const cairo_clip_t *clip)
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;
1016 TRACE ((stderr, "%s: surface=%d\n", __FUNCTION__, surface->base.unique_id));
1018 status = _cairo_composite_rectangles_init_for_glyphs (&composite,
1025 if (unlikely (status))
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;
1034 status = _command_init (surface,
1035 &command->header, CAIRO_COMMAND_SHOW_TEXT_GLYPHS,
1037 if (unlikely (status))
1038 goto CLEANUP_COMMAND;
1040 status = _cairo_pattern_init_snapshot (&command->source.base, source);
1041 if (unlikely (status))
1042 goto CLEANUP_COMMAND;
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;
1052 command->utf8 = malloc (utf8_len);
1053 if (unlikely (command->utf8 == NULL)) {
1054 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1055 goto CLEANUP_ARRAYS;
1057 memcpy (command->utf8, utf8, utf8_len);
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;
1065 memcpy (command->glyphs, glyphs, sizeof (glyphs[0]) * num_glyphs);
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;
1073 memcpy (command->clusters, clusters, sizeof (clusters[0]) * num_clusters);
1076 command->cluster_flags = cluster_flags;
1078 command->scaled_font = cairo_scaled_font_reference (scaled_font);
1080 status = _cairo_recording_surface_commit (surface, &command->header);
1081 if (unlikely (status))
1082 goto CLEANUP_SCALED_FONT;
1084 _cairo_composite_rectangles_fini (&composite);
1085 return CAIRO_STATUS_SUCCESS;
1087 CLEANUP_SCALED_FONT:
1088 cairo_scaled_font_destroy (command->scaled_font);
1090 free (command->utf8);
1091 free (command->glyphs);
1092 free (command->clusters);
1094 _cairo_pattern_fini (&command->source.base);
1096 _cairo_clip_destroy (command->header.clip);
1099 _cairo_composite_rectangles_fini (&composite);
1104 _command_init_copy (cairo_recording_surface_t *surface,
1105 cairo_command_header_t *dst,
1106 const cairo_command_header_t *src)
1108 dst->type = src->type;
1110 dst->region = CAIRO_RECORDING_REGION_ALL;
1112 dst->extents = src->extents;
1114 dst->index = surface->commands.num_elements;
1116 dst->clip = _cairo_clip_copy (src->clip);
1119 static cairo_status_t
1120 _cairo_recording_surface_copy__paint (cairo_recording_surface_t *surface,
1121 const cairo_command_t *src)
1123 cairo_command_paint_t *command;
1124 cairo_status_t status;
1126 command = malloc (sizeof (*command));
1127 if (unlikely (command == NULL)) {
1128 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1132 _command_init_copy (surface, &command->header, &src->header);
1134 status = _cairo_pattern_init_copy (&command->source.base,
1135 &src->paint.source.base);
1136 if (unlikely (status))
1139 status = _cairo_recording_surface_commit (surface, &command->header);
1140 if (unlikely (status))
1143 return CAIRO_STATUS_SUCCESS;
1146 _cairo_pattern_fini (&command->source.base);
1153 static cairo_status_t
1154 _cairo_recording_surface_copy__mask (cairo_recording_surface_t *surface,
1155 const cairo_command_t *src)
1157 cairo_command_mask_t *command;
1158 cairo_status_t status;
1160 command = malloc (sizeof (*command));
1161 if (unlikely (command == NULL)) {
1162 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1166 _command_init_copy (surface, &command->header, &src->header);
1168 status = _cairo_pattern_init_copy (&command->source.base,
1169 &src->mask.source.base);
1170 if (unlikely (status))
1173 status = _cairo_pattern_init_copy (&command->mask.base,
1174 &src->mask.source.base);
1175 if (unlikely (status))
1178 status = _cairo_recording_surface_commit (surface, &command->header);
1179 if (unlikely (status))
1182 return CAIRO_STATUS_SUCCESS;
1185 _cairo_pattern_fini (&command->mask.base);
1187 _cairo_pattern_fini (&command->source.base);
1194 static cairo_status_t
1195 _cairo_recording_surface_copy__stroke (cairo_recording_surface_t *surface,
1196 const cairo_command_t *src)
1198 cairo_command_stroke_t *command;
1199 cairo_status_t status;
1201 command = malloc (sizeof (*command));
1202 if (unlikely (command == NULL)) {
1203 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1207 _command_init_copy (surface, &command->header, &src->header);
1209 status = _cairo_pattern_init_copy (&command->source.base,
1210 &src->stroke.source.base);
1211 if (unlikely (status))
1214 status = _cairo_path_fixed_init_copy (&command->path, &src->stroke.path);
1215 if (unlikely (status))
1218 status = _cairo_stroke_style_init_copy (&command->style,
1219 &src->stroke.style);
1220 if (unlikely (status))
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;
1228 status = _cairo_recording_surface_commit (surface, &command->header);
1229 if (unlikely (status))
1232 return CAIRO_STATUS_SUCCESS;
1235 _cairo_stroke_style_fini (&command->style);
1237 _cairo_path_fixed_fini (&command->path);
1239 _cairo_pattern_fini (&command->source.base);
1246 static cairo_status_t
1247 _cairo_recording_surface_copy__fill (cairo_recording_surface_t *surface,
1248 const cairo_command_t *src)
1250 cairo_command_fill_t *command;
1251 cairo_status_t status;
1253 command = malloc (sizeof (*command));
1254 if (unlikely (command == NULL)) {
1255 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1259 _command_init_copy (surface, &command->header, &src->header);
1261 status = _cairo_pattern_init_copy (&command->source.base,
1262 &src->fill.source.base);
1263 if (unlikely (status))
1266 status = _cairo_path_fixed_init_copy (&command->path, &src->fill.path);
1267 if (unlikely (status))
1270 command->fill_rule = src->fill.fill_rule;
1271 command->tolerance = src->fill.tolerance;
1272 command->antialias = src->fill.antialias;
1274 status = _cairo_recording_surface_commit (surface, &command->header);
1275 if (unlikely (status))
1278 return CAIRO_STATUS_SUCCESS;
1281 _cairo_path_fixed_fini (&command->path);
1283 _cairo_pattern_fini (&command->source.base);
1290 static cairo_status_t
1291 _cairo_recording_surface_copy__glyphs (cairo_recording_surface_t *surface,
1292 const cairo_command_t *src)
1294 cairo_command_show_text_glyphs_t *command;
1295 cairo_status_t status;
1297 command = malloc (sizeof (*command));
1298 if (unlikely (command == NULL)) {
1299 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1303 _command_init_copy (surface, &command->header, &src->header);
1305 status = _cairo_pattern_init_copy (&command->source.base,
1306 &src->show_text_glyphs.source.base);
1307 if (unlikely (status))
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;
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);
1323 memcpy (command->utf8, src->show_text_glyphs.utf8, command->utf8_len);
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);
1332 memcpy (command->glyphs, src->show_text_glyphs.glyphs,
1333 sizeof (command->glyphs[0]) * command->num_glyphs);
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);
1342 memcpy (command->clusters, src->show_text_glyphs.clusters,
1343 sizeof (command->clusters[0]) * command->num_clusters);
1346 command->cluster_flags = src->show_text_glyphs.cluster_flags;
1348 command->scaled_font =
1349 cairo_scaled_font_reference (src->show_text_glyphs.scaled_font);
1351 status = _cairo_recording_surface_commit (surface, &command->header);
1352 if (unlikely (status))
1355 return CAIRO_STATUS_SUCCESS;
1358 free (command->utf8);
1359 free (command->glyphs);
1360 free (command->clusters);
1361 _cairo_pattern_fini (&command->source.base);
1368 static cairo_status_t
1369 _cairo_recording_surface_copy (cairo_recording_surface_t *dst,
1370 cairo_recording_surface_t *src)
1372 cairo_command_t **elements;
1373 int i, num_elements;
1374 cairo_status_t status;
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];
1381 switch (command->header.type) {
1382 case CAIRO_COMMAND_PAINT:
1383 status = _cairo_recording_surface_copy__paint (dst, command);
1386 case CAIRO_COMMAND_MASK:
1387 status = _cairo_recording_surface_copy__mask (dst, command);
1390 case CAIRO_COMMAND_STROKE:
1391 status = _cairo_recording_surface_copy__stroke (dst, command);
1394 case CAIRO_COMMAND_FILL:
1395 status = _cairo_recording_surface_copy__fill (dst, command);
1398 case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
1399 status = _cairo_recording_surface_copy__glyphs (dst, command);
1406 if (unlikely (status))
1410 return CAIRO_STATUS_SUCCESS;
1414 * _cairo_recording_surface_snapshot:
1415 * @surface: a #cairo_surface_t which must be a recording surface
1417 * Make an immutable copy of @surface. It is an error to call a
1418 * surface-modifying function on the result of this function.
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.
1424 * Return value: The snapshot surface.
1426 static cairo_surface_t *
1427 _cairo_recording_surface_snapshot (void *abstract_other)
1429 cairo_recording_surface_t *other = abstract_other;
1430 cairo_recording_surface_t *surface;
1431 cairo_status_t status;
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));
1437 _cairo_surface_init (&surface->base,
1438 &cairo_recording_surface_backend,
1440 other->base.content);
1442 surface->extents_pixels = other->extents_pixels;
1443 surface->extents = other->extents;
1444 surface->unbounded = other->unbounded;
1446 surface->base.is_clear = other->base.is_clear;
1448 surface->bbtree.left = surface->bbtree.right = NULL;
1449 surface->bbtree.chain = INVALID_CHAIN;
1451 surface->indices = NULL;
1452 surface->num_indices = 0;
1453 surface->optimize_clears = TRUE;
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);
1462 return &surface->base;
1466 _cairo_recording_surface_get_extents (void *abstract_surface,
1467 cairo_rectangle_int_t *rectangle)
1469 cairo_recording_surface_t *surface = abstract_surface;
1471 if (surface->unbounded)
1474 *rectangle = surface->extents;
1478 static const cairo_surface_backend_t cairo_recording_surface_backend = {
1479 CAIRO_SURFACE_TYPE_RECORDING,
1480 _cairo_recording_surface_finish,
1482 _cairo_default_context_create,
1484 _cairo_recording_surface_create_similar,
1485 NULL, /* create similar image */
1486 NULL, /* map to image */
1487 NULL, /* unmap image */
1489 _cairo_surface_default_source,
1490 _cairo_recording_surface_acquire_source_image,
1491 _cairo_recording_surface_release_source_image,
1492 _cairo_recording_surface_snapshot,
1494 NULL, /* copy_page */
1495 NULL, /* show_page */
1497 _cairo_recording_surface_get_extents,
1498 NULL, /* get_font_options */
1501 NULL, /* mark_dirty_rectangle */
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. */
1508 _cairo_recording_surface_paint,
1509 _cairo_recording_surface_mask,
1510 _cairo_recording_surface_stroke,
1511 _cairo_recording_surface_fill,
1512 NULL, /* fill-stroke */
1514 _cairo_recording_surface_has_show_text_glyphs,
1515 _cairo_recording_surface_show_text_glyphs,
1519 _cairo_recording_surface_get_path (cairo_surface_t *abstract_surface,
1520 cairo_path_fixed_t *path)
1522 cairo_recording_surface_t *surface;
1523 cairo_command_t **elements;
1524 int i, num_elements;
1525 cairo_int_status_t status;
1527 if (unlikely (abstract_surface->status))
1528 return abstract_surface->status;
1530 surface = (cairo_recording_surface_t *) abstract_surface;
1531 status = CAIRO_STATUS_SUCCESS;
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];
1538 switch (command->header.type) {
1539 case CAIRO_COMMAND_PAINT:
1540 case CAIRO_COMMAND_MASK:
1541 status = CAIRO_INT_STATUS_UNSUPPORTED;
1544 case CAIRO_COMMAND_STROKE:
1546 cairo_traps_t traps;
1548 _cairo_traps_init (&traps);
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,
1558 if (status == CAIRO_INT_STATUS_SUCCESS)
1559 status = _cairo_traps_path (&traps, path);
1561 _cairo_traps_fini (&traps);
1564 case CAIRO_COMMAND_FILL:
1566 status = _cairo_path_fixed_append (path,
1567 &command->fill.path,
1571 case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
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,
1584 if (unlikely (status))
1592 _cairo_recording_surface_get_visible_commands (cairo_recording_surface_t *surface,
1593 const cairo_rectangle_int_t *extents)
1595 unsigned int num_visible, *indices;
1598 if (surface->commands.num_elements == 0)
1601 _cairo_box_from_rectangle (&box, extents);
1603 if (surface->bbtree.chain == INVALID_CHAIN)
1604 _cairo_recording_surface_create_bbtree (surface);
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);
1616 _cairo_recording_surface_merge_source_attributes (cairo_recording_surface_t *surface,
1617 cairo_operator_t op,
1618 const cairo_pattern_t *source)
1620 if (op != CAIRO_OPERATOR_OVER)
1621 surface->has_only_op_over = FALSE;
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;
1628 if (_cairo_surface_is_snapshot (surf))
1629 free_me = surf = _cairo_surface_snapshot_get_target (surf);
1631 if (surf->type == CAIRO_SURFACE_TYPE_RECORDING) {
1632 cairo_recording_surface_t *rec_surf = (cairo_recording_surface_t *) surf;
1634 if (! _cairo_recording_surface_has_only_bilevel_alpha (rec_surf))
1635 surface->has_bilevel_alpha = FALSE;
1637 if (! _cairo_recording_surface_has_only_op_over (rec_surf))
1638 surface->has_only_op_over = FALSE;
1640 } else if (surf->type == CAIRO_SURFACE_TYPE_IMAGE) {
1641 cairo_image_surface_t *img_surf = (cairo_image_surface_t *) surf;
1643 if (_cairo_image_analyze_transparency (img_surf) == CAIRO_IMAGE_HAS_ALPHA)
1644 surface->has_bilevel_alpha = FALSE;
1647 if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
1648 surface->has_bilevel_alpha = FALSE;
1651 cairo_surface_destroy (free_me);
1654 } else if (source->type == CAIRO_PATTERN_TYPE_RASTER_SOURCE) {
1655 cairo_surface_t *image;
1656 cairo_surface_t *raster;
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);
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;
1667 _cairo_raster_source_pattern_release (source, raster);
1668 if (raster->type == CAIRO_SURFACE_TYPE_IMAGE)
1673 if (!_cairo_pattern_is_clear (source) && !_cairo_pattern_is_opaque (source, NULL))
1674 surface->has_bilevel_alpha = FALSE;
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)
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;
1697 if (unlikely (surface->base.status))
1698 return surface->base.status;
1700 if (unlikely (target->status))
1701 return target->status;
1703 if (unlikely (surface->base.finished))
1704 return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
1706 if (surface->base.is_clear)
1707 return CAIRO_STATUS_SUCCESS;
1709 assert (_cairo_surface_is_recording (&surface->base));
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;
1719 _cairo_surface_wrapper_set_inverse_transform (&wrapper, surface_transform);
1720 _cairo_surface_wrapper_set_clip (&wrapper, target_clip);
1722 /* Compute the extents of the target clip in recorded device space */
1723 if (! _cairo_surface_wrapper_get_target_extents (&wrapper, &extents))
1726 surface->has_bilevel_alpha = TRUE;
1727 surface->has_only_op_over = TRUE;
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;
1736 if (extents.width < r->width || extents.height < r->height) {
1738 _cairo_recording_surface_get_visible_commands (surface, &extents);
1739 use_indices = num_elements != surface->commands.num_elements;
1742 for (i = 0; i < num_elements; i++) {
1743 cairo_command_t *command = elements[use_indices ? surface->indices[i] : i];
1745 if (! replay_all && command->header.region != region)
1748 if (! _cairo_rectangle_intersects (&extents, &command->header.extents))
1751 switch (command->header.type) {
1752 case CAIRO_COMMAND_PAINT:
1753 status = _cairo_surface_wrapper_paint (&wrapper,
1755 &command->paint.source.base,
1756 command->header.clip);
1757 if (type == CAIRO_RECORDING_CREATE_REGIONS) {
1758 _cairo_recording_surface_merge_source_attributes (surface,
1760 &command->paint.source.base);
1764 case CAIRO_COMMAND_MASK:
1765 status = _cairo_surface_wrapper_mask (&wrapper,
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,
1773 &command->mask.source.base);
1774 _cairo_recording_surface_merge_source_attributes (surface,
1776 &command->mask.mask.base);
1780 case CAIRO_COMMAND_STROKE:
1781 status = _cairo_surface_wrapper_stroke (&wrapper,
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,
1794 &command->stroke.source.base);
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;
1803 stroke_command = NULL;
1804 if (type != CAIRO_RECORDING_CREATE_REGIONS && i < num_elements - 1)
1805 stroke_command = elements[i + 1];
1807 if (stroke_command != NULL &&
1808 type == CAIRO_RECORDING_REPLAY &&
1809 region != CAIRO_RECORDING_REGION_ALL)
1811 if (stroke_command->header.region != region)
1812 stroke_command = NULL;
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))
1822 status = _cairo_surface_wrapper_fill_stroke (&wrapper,
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,
1840 &command->fill.source.base);
1841 _cairo_recording_surface_merge_source_attributes (surface,
1843 &command->stroke.source.base);
1848 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1849 status = _cairo_surface_wrapper_fill (&wrapper,
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,
1860 &command->fill.source.base);
1865 case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
1866 status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
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,
1878 &command->show_text_glyphs.source.base);
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;
1893 assert (_cairo_int_status_is_error (status));
1897 if (unlikely (status))
1902 _cairo_surface_wrapper_fini (&wrapper);
1903 return _cairo_surface_set_error (&surface->base, status);
1907 _cairo_recording_surface_replay_one (cairo_recording_surface_t *surface,
1908 long unsigned index,
1909 cairo_surface_t *target)
1911 cairo_surface_wrapper_t wrapper;
1912 cairo_command_t **elements, *command;
1913 cairo_int_status_t status;
1915 if (unlikely (surface->base.status))
1916 return surface->base.status;
1918 if (unlikely (target->status))
1919 return target->status;
1921 if (unlikely (surface->base.finished))
1922 return _cairo_error (CAIRO_STATUS_SURFACE_FINISHED);
1924 assert (_cairo_surface_is_recording (&surface->base));
1927 * Use a surface wrapper because we may want to do transformed
1928 * replay in the future.
1930 _cairo_surface_wrapper_init (&wrapper, target);
1932 if (index > surface->commands.num_elements)
1933 return _cairo_error (CAIRO_STATUS_READ_ERROR);
1935 elements = _cairo_array_index (&surface->commands, 0);
1936 if (elements == NULL)
1937 return _cairo_error (CAIRO_STATUS_NULL_POINTER);
1939 command = elements[index];
1940 switch (command->header.type) {
1941 case CAIRO_COMMAND_PAINT:
1942 status = _cairo_surface_wrapper_paint (&wrapper,
1944 &command->paint.source.base,
1945 command->header.clip);
1948 case CAIRO_COMMAND_MASK:
1949 status = _cairo_surface_wrapper_mask (&wrapper,
1951 &command->mask.source.base,
1952 &command->mask.mask.base,
1953 command->header.clip);
1956 case CAIRO_COMMAND_STROKE:
1957 status = _cairo_surface_wrapper_stroke (&wrapper,
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);
1969 case CAIRO_COMMAND_FILL:
1970 status = _cairo_surface_wrapper_fill (&wrapper,
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);
1980 case CAIRO_COMMAND_SHOW_TEXT_GLYPHS:
1981 status = _cairo_surface_wrapper_show_text_glyphs (&wrapper,
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);
1996 _cairo_surface_wrapper_fini (&wrapper);
1997 return _cairo_surface_set_error (&surface->base, status);
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
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.
2012 _cairo_recording_surface_replay (cairo_surface_t *surface,
2013 cairo_surface_t *target)
2015 return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, NULL,
2017 CAIRO_RECORDING_REPLAY,
2018 CAIRO_RECORDING_REGION_ALL);
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)
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);
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.
2040 _cairo_recording_surface_replay_and_create_regions (cairo_surface_t *surface,
2041 cairo_surface_t *target)
2043 return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface, NULL, NULL,
2045 CAIRO_RECORDING_CREATE_REGIONS,
2046 CAIRO_RECORDING_REGION_ALL);
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)
2055 return _cairo_recording_surface_replay_internal ((cairo_recording_surface_t *) surface,
2056 surface_extents, NULL,
2058 CAIRO_RECORDING_REPLAY,
2062 static cairo_status_t
2063 _recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
2065 const cairo_matrix_t *transform)
2067 cairo_surface_t *null_surface;
2068 cairo_surface_t *analysis_surface;
2069 cairo_status_t status;
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);
2075 status = analysis_surface->status;
2076 if (unlikely (status)) {
2077 cairo_surface_destroy (analysis_surface);
2081 if (transform != NULL)
2082 _cairo_analysis_surface_set_ctm (analysis_surface, transform);
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);
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
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.
2106 cairo_recording_surface_ink_extents (cairo_surface_t *surface,
2112 cairo_status_t status;
2115 memset (&bbox, 0, sizeof (bbox));
2117 if (surface->status || ! _cairo_surface_is_recording (surface)) {
2118 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
2122 status = _recording_surface_get_ink_bbox ((cairo_recording_surface_t *) surface,
2125 if (unlikely (status))
2126 status = _cairo_surface_set_error (surface, status);
2130 *x0 = _cairo_fixed_to_double (bbox.p1.x);
2132 *y0 = _cairo_fixed_to_double (bbox.p1.y);
2134 *width = _cairo_fixed_to_double (bbox.p2.x - bbox.p1.x);
2136 *height = _cairo_fixed_to_double (bbox.p2.y - bbox.p1.y);
2140 _cairo_recording_surface_get_bbox (cairo_recording_surface_t *surface,
2142 const cairo_matrix_t *transform)
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);
2149 return CAIRO_STATUS_SUCCESS;
2152 return _recording_surface_get_ink_bbox (surface, bbox, transform);
2156 _cairo_recording_surface_get_ink_bbox (cairo_recording_surface_t *surface,
2158 const cairo_matrix_t *transform)
2160 return _recording_surface_get_ink_bbox (surface, bbox, transform);
2164 * cairo_recording_surface_get_extents:
2165 * @surface: a #cairo_recording_surface_t
2166 * @extents: the #cairo_rectangle_t to be assigned the extents
2168 * Get the extents of the recording-surface.
2170 * Return value: %TRUE if the surface is bounded, of recording type, and
2171 * not in an error state, otherwise %FALSE
2176 cairo_recording_surface_get_extents (cairo_surface_t *surface,
2177 cairo_rectangle_t *extents)
2179 cairo_recording_surface_t *record;
2181 if (surface->status || ! _cairo_surface_is_recording (surface)) {
2182 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
2186 record = (cairo_recording_surface_t *)surface;
2187 if (record->unbounded)
2190 *extents = record->extents_pixels;
2195 _cairo_recording_surface_has_only_bilevel_alpha (cairo_recording_surface_t *surface)
2197 return surface->has_bilevel_alpha;
2201 _cairo_recording_surface_has_only_op_over (cairo_recording_surface_t *surface)
2203 return surface->has_only_op_over;