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 © 2003 University of Southern California
5 * Copyright © 2009,2010,2011 Intel Corporation
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 University of Southern
36 * Carl D. Worth <cworth@cworth.org>
37 * Chris Wilson <chris@chris-wilson.co.uk>
42 #include "cairo-boxes-private.h"
43 #include "cairo-clip-private.h"
44 #include "cairo-composite-rectangles-private.h"
45 #include "cairo-compositor-private.h"
46 #include "cairo-default-context-private.h"
47 #include "cairo-error-private.h"
48 #include "cairo-image-surface-inline.h"
49 #include "cairo-paginated-private.h"
50 #include "cairo-pattern-private.h"
51 #include "cairo-recording-surface-private.h"
52 #include "cairo-region-private.h"
53 #include "cairo-scaled-font-private.h"
54 #include "cairo-surface-snapshot-private.h"
55 #include "cairo-surface-subsurface-private.h"
56 #include "cairo-surface-shadow-private.h"
57 #include "cairo-list-inline.h"
59 /* Limit on the width / height of an image surface in pixels. This is
60 * mainly determined by coordinates of things sent to pixman at the
61 * moment being in 16.16 format. */
62 #define MAX_IMAGE_SIZE 32767
64 #define MAX_IMAGE_SHADOW_SIZE 256
65 #define MIN_IMAGE_SHADOW_SIZE 32
69 * @Title: Image Surfaces
70 * @Short_Description: Rendering to memory buffers
71 * @See_Also: #cairo_surface_t
73 * Image surfaces provide the ability to render to memory buffers
74 * either allocated by cairo or by the calling code. The supported
75 * image formats are those defined in #cairo_format_t.
79 * CAIRO_HAS_IMAGE_SURFACE:
81 * Defined if the image surface backend is available.
82 * The image surface backend is always built in.
83 * This macro was added for completeness in cairo 1.8.
88 static cairo_list_t shadow_caches;
89 static unsigned long shadow_caches_size = 0;
90 static cairo_recursive_mutex_t shadow_caches_mutex;
91 static unsigned shadow_caches_mutex_depth = 0;
92 static cairo_atomic_int_t shadow_caches_ref_count = 0;
95 _cairo_image_shadow_caches_init (void)
97 _cairo_atomic_int_inc (&shadow_caches_ref_count);
99 if (shadow_caches_ref_count == 1)
100 cairo_list_init (&shadow_caches);
102 CAIRO_RECURSIVE_MUTEX_INIT (shadow_caches_mutex);
106 _cairo_image_shadow_caches_destroy (void)
108 assert (shadow_caches_ref_count != 0);
110 if (! _cairo_atomic_int_dec_and_test (&shadow_caches_ref_count))
113 if (shadow_caches_mutex_depth == 0) {
114 CAIRO_MUTEX_FINI (shadow_caches_mutex);
116 while (! cairo_list_is_empty (&shadow_caches)) {
117 cairo_shadow_cache_t *shadow;
119 shadow = cairo_list_first_entry (&shadow_caches,
120 cairo_shadow_cache_t,
122 cairo_list_del (&shadow->link);
123 cairo_surface_destroy (shadow->surface);
126 shadow_caches_size = 0;
130 static cairo_status_t
131 _cairo_image_surface_shadow_cache_acquire (void *abstract_surface)
133 cairo_image_surface_t *surface = abstract_surface;
135 if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
136 return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
138 if (unlikely (surface->base.status))
139 return surface->base.status;
141 CAIRO_MUTEX_LOCK (shadow_caches_mutex);
142 shadow_caches_mutex_depth++;
144 return CAIRO_STATUS_SUCCESS;
148 _cairo_image_surface_shadow_cache_release (void *abstract_surface)
150 cairo_image_surface_t *surface = abstract_surface;
152 if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
155 if (unlikely (surface->base.status))
158 assert (shadow_caches_mutex_depth > 0);
159 shadow_caches_mutex_depth--;
161 CAIRO_MUTEX_UNLOCK (shadow_caches_mutex);
164 static cairo_list_t *
165 _cairo_image_surface_get_shadow_cache (void *abstract_surface)
167 cairo_image_surface_t *surface = abstract_surface;
169 if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
172 if (unlikely (surface->base.status))
175 return &shadow_caches;
178 static unsigned long *
179 _cairo_image_surface_get_shadow_cache_size (void *abstract_surface)
181 cairo_image_surface_t *surface = abstract_surface;
183 if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
186 if (unlikely (surface->base.status))
189 return &shadow_caches_size;
193 _cairo_image_surface_has_shadow_cache (void *abstract_surface)
195 cairo_image_surface_t *surface = abstract_surface;
197 if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
203 _cairo_image_surface_is_size_valid (int width, int height)
205 return 0 <= width && width <= MAX_IMAGE_SIZE &&
206 0 <= height && height <= MAX_IMAGE_SIZE;
210 _cairo_format_from_pixman_format (pixman_format_code_t pixman_format)
212 switch (pixman_format) {
213 case PIXMAN_a8r8g8b8:
214 return CAIRO_FORMAT_ARGB32;
215 case PIXMAN_x2r10g10b10:
216 return CAIRO_FORMAT_RGB30;
217 case PIXMAN_x8r8g8b8:
218 return CAIRO_FORMAT_RGB24;
220 return CAIRO_FORMAT_A8;
222 return CAIRO_FORMAT_A1;
224 return CAIRO_FORMAT_RGB16_565;
225 case PIXMAN_r8g8b8a8: case PIXMAN_r8g8b8x8:
226 case PIXMAN_a8b8g8r8: case PIXMAN_x8b8g8r8: case PIXMAN_r8g8b8:
227 case PIXMAN_b8g8r8: case PIXMAN_b5g6r5:
228 case PIXMAN_a1r5g5b5: case PIXMAN_x1r5g5b5: case PIXMAN_a1b5g5r5:
229 case PIXMAN_x1b5g5r5: case PIXMAN_a4r4g4b4: case PIXMAN_x4r4g4b4:
230 case PIXMAN_a4b4g4r4: case PIXMAN_x4b4g4r4: case PIXMAN_r3g3b2:
231 case PIXMAN_b2g3r3: case PIXMAN_a2r2g2b2: case PIXMAN_a2b2g2r2:
232 case PIXMAN_c8: case PIXMAN_g8: case PIXMAN_x4a4:
233 case PIXMAN_a4: case PIXMAN_r1g2b1: case PIXMAN_b1g2r1:
234 case PIXMAN_a1r1g1b1: case PIXMAN_a1b1g1r1: case PIXMAN_c4:
235 case PIXMAN_g4: case PIXMAN_g1:
236 case PIXMAN_yuy2: case PIXMAN_yv12:
237 case PIXMAN_b8g8r8x8:
238 case PIXMAN_b8g8r8a8:
239 case PIXMAN_a2b10g10r10:
240 case PIXMAN_x2b10g10r10:
241 case PIXMAN_a2r10g10b10:
242 case PIXMAN_x14r6g6b6:
244 return CAIRO_FORMAT_INVALID;
247 return CAIRO_FORMAT_INVALID;
251 _cairo_content_from_pixman_format (pixman_format_code_t pixman_format)
253 cairo_content_t content;
256 if (PIXMAN_FORMAT_RGB (pixman_format))
257 content |= CAIRO_CONTENT_COLOR;
258 if (PIXMAN_FORMAT_A (pixman_format))
259 content |= CAIRO_CONTENT_ALPHA;
265 _cairo_image_surface_init (cairo_image_surface_t *surface,
266 pixman_image_t *pixman_image,
267 pixman_format_code_t pixman_format)
269 surface->parent = NULL;
270 surface->pixman_image = pixman_image;
272 surface->pixman_format = pixman_format;
273 surface->format = _cairo_format_from_pixman_format (pixman_format);
274 surface->data = (uint8_t *) pixman_image_get_data (pixman_image);
275 surface->owns_data = FALSE;
276 surface->transparency = CAIRO_IMAGE_UNKNOWN;
277 surface->color = CAIRO_IMAGE_UNKNOWN_COLOR;
279 surface->width = pixman_image_get_width (pixman_image);
280 surface->height = pixman_image_get_height (pixman_image);
281 surface->stride = pixman_image_get_stride (pixman_image);
282 surface->depth = pixman_image_get_depth (pixman_image);
284 surface->base.is_clear = surface->width == 0 || surface->height == 0;
286 surface->compositor = _cairo_image_spans_compositor_get ();
288 _cairo_image_shadow_caches_init ();
292 _cairo_image_surface_create_for_pixman_image (pixman_image_t *pixman_image,
293 pixman_format_code_t pixman_format)
295 cairo_image_surface_t *surface;
297 surface = malloc (sizeof (cairo_image_surface_t));
298 if (unlikely (surface == NULL))
299 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
301 _cairo_surface_init (&surface->base,
302 &_cairo_image_surface_backend,
304 _cairo_content_from_pixman_format (pixman_format));
306 _cairo_image_surface_init (surface, pixman_image, pixman_format);
308 return &surface->base;
312 _pixman_format_from_masks (cairo_format_masks_t *masks,
313 pixman_format_code_t *format_ret)
315 pixman_format_code_t format;
318 cairo_format_masks_t format_masks;
320 a = _cairo_popcount (masks->alpha_mask);
321 r = _cairo_popcount (masks->red_mask);
322 g = _cairo_popcount (masks->green_mask);
323 b = _cairo_popcount (masks->blue_mask);
325 if (masks->red_mask) {
326 if (masks->red_mask > masks->blue_mask)
327 format_type = PIXMAN_TYPE_ARGB;
329 format_type = PIXMAN_TYPE_ABGR;
330 } else if (masks->alpha_mask) {
331 format_type = PIXMAN_TYPE_A;
336 format = PIXMAN_FORMAT (masks->bpp, format_type, a, r, g, b);
338 if (! pixman_format_supported_destination (format))
341 /* Sanity check that we got out of PIXMAN_FORMAT exactly what we
342 * expected. This avoid any problems from something bizarre like
343 * alpha in the least-significant bits, or insane channel order,
345 if (!_pixman_format_to_masks (format, &format_masks) ||
346 masks->bpp != format_masks.bpp ||
347 masks->red_mask != format_masks.red_mask ||
348 masks->green_mask != format_masks.green_mask ||
349 masks->blue_mask != format_masks.blue_mask)
354 *format_ret = format;
358 /* A mask consisting of N bits set to 1. */
359 #define MASK(N) ((1UL << (N))-1)
362 _pixman_format_to_masks (pixman_format_code_t format,
363 cairo_format_masks_t *masks)
367 masks->bpp = PIXMAN_FORMAT_BPP (format);
369 /* Number of bits in each channel */
370 a = PIXMAN_FORMAT_A (format);
371 r = PIXMAN_FORMAT_R (format);
372 g = PIXMAN_FORMAT_G (format);
373 b = PIXMAN_FORMAT_B (format);
375 switch (PIXMAN_FORMAT_TYPE (format)) {
376 case PIXMAN_TYPE_ARGB:
377 masks->alpha_mask = MASK (a) << (r + g + b);
378 masks->red_mask = MASK (r) << (g + b);
379 masks->green_mask = MASK (g) << (b);
380 masks->blue_mask = MASK (b);
382 case PIXMAN_TYPE_ABGR:
383 masks->alpha_mask = MASK (a) << (b + g + r);
384 masks->blue_mask = MASK (b) << (g + r);
385 masks->green_mask = MASK (g) << (r);
386 masks->red_mask = MASK (r);
388 #ifdef PIXMAN_TYPE_BGRA
389 case PIXMAN_TYPE_BGRA:
390 masks->blue_mask = MASK (b) << (masks->bpp - b);
391 masks->green_mask = MASK (g) << (masks->bpp - b - g);
392 masks->red_mask = MASK (r) << (masks->bpp - b - g - r);
393 masks->alpha_mask = MASK (a);
397 masks->alpha_mask = MASK (a);
399 masks->green_mask = 0;
400 masks->blue_mask = 0;
402 case PIXMAN_TYPE_OTHER:
403 case PIXMAN_TYPE_COLOR:
404 case PIXMAN_TYPE_GRAY:
405 case PIXMAN_TYPE_YUY2:
406 case PIXMAN_TYPE_YV12:
408 masks->alpha_mask = 0;
410 masks->green_mask = 0;
411 masks->blue_mask = 0;
417 _cairo_format_to_pixman_format_code (cairo_format_t format)
419 pixman_format_code_t ret;
421 case CAIRO_FORMAT_A1:
424 case CAIRO_FORMAT_A8:
427 case CAIRO_FORMAT_RGB24:
428 ret = PIXMAN_x8r8g8b8;
430 case CAIRO_FORMAT_RGB30:
431 ret = PIXMAN_x2r10g10b10;
433 case CAIRO_FORMAT_RGB16_565:
436 case CAIRO_FORMAT_ARGB32:
437 case CAIRO_FORMAT_INVALID:
439 ret = PIXMAN_a8r8g8b8;
446 _cairo_image_surface_create_with_pixman_format (unsigned char *data,
447 pixman_format_code_t pixman_format,
452 cairo_surface_t *surface;
453 pixman_image_t *pixman_image;
455 if (! _cairo_image_surface_is_size_valid (width, height))
457 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
460 pixman_image = pixman_image_create_bits (pixman_format, width, height,
461 (uint32_t *) data, stride);
463 if (unlikely (pixman_image == NULL))
464 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
466 surface = _cairo_image_surface_create_for_pixman_image (pixman_image,
468 if (unlikely (surface->status)) {
469 pixman_image_unref (pixman_image);
473 /* we can not make any assumptions about the initial state of user data */
474 surface->is_clear = data == NULL;
479 * cairo_image_surface_create:
480 * @format: format of pixels in the surface to create
481 * @width: width of the surface, in pixels
482 * @height: height of the surface, in pixels
484 * Creates an image surface of the specified format and
485 * dimensions. Initially the surface contents are all
486 * 0. (Specifically, within each pixel, each color or alpha channel
487 * belonging to format will be 0. The contents of bits within a pixel,
488 * but not belonging to the given format are undefined).
490 * Return value: a pointer to the newly created surface. The caller
491 * owns the surface and should call cairo_surface_destroy() when done
494 * This function always returns a valid pointer, but it will return a
495 * pointer to a "nil" surface if an error such as out of memory
496 * occurs. You can use cairo_surface_status() to check for this.
501 cairo_image_surface_create (cairo_format_t format,
505 pixman_format_code_t pixman_format;
507 if (! CAIRO_FORMAT_VALID (format))
508 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
510 pixman_format = _cairo_format_to_pixman_format_code (format);
512 return _cairo_image_surface_create_with_pixman_format (NULL, pixman_format,
515 slim_hidden_def (cairo_image_surface_create);
518 _cairo_image_surface_create_with_content (cairo_content_t content,
522 return cairo_image_surface_create (_cairo_format_from_content (content),
527 * cairo_format_stride_for_width:
528 * @format: A #cairo_format_t value
529 * @width: The desired width of an image surface to be created.
531 * This function provides a stride value that will respect all
532 * alignment requirements of the accelerated image-rendering code
533 * within cairo. Typical usage will be of the form:
535 * <informalexample><programlisting>
537 * unsigned char *data;
538 * #cairo_surface_t *surface;
540 * stride = cairo_format_stride_for_width (format, width);
541 * data = malloc (stride * height);
542 * surface = cairo_image_surface_create_for_data (data, format,
545 * </programlisting></informalexample>
547 * Return value: the appropriate stride to use given the desired
548 * format and width, or -1 if either the format is invalid or the width
554 cairo_format_stride_for_width (cairo_format_t format,
559 if (! CAIRO_FORMAT_VALID (format)) {
560 _cairo_error_throw (CAIRO_STATUS_INVALID_FORMAT);
564 bpp = _cairo_format_bits_per_pixel (format);
565 if ((unsigned) (width) >= (INT32_MAX - 7) / (unsigned) (bpp))
568 return CAIRO_STRIDE_FOR_WIDTH_BPP (width, bpp);
570 slim_hidden_def (cairo_format_stride_for_width);
573 * cairo_image_surface_create_for_data:
574 * @data: a pointer to a buffer supplied by the application in which
575 * to write contents. This pointer must be suitably aligned for any
576 * kind of variable, (for example, a pointer returned by malloc).
577 * @format: the format of pixels in the buffer
578 * @width: the width of the image to be stored in the buffer
579 * @height: the height of the image to be stored in the buffer
580 * @stride: the number of bytes between the start of rows in the
581 * buffer as allocated. This value should always be computed by
582 * cairo_format_stride_for_width() before allocating the data
585 * Creates an image surface for the provided pixel data. The output
586 * buffer must be kept around until the #cairo_surface_t is destroyed
587 * or cairo_surface_finish() is called on the surface. The initial
588 * contents of @data will be used as the initial image contents; you
589 * must explicitly clear the buffer, using, for example,
590 * cairo_rectangle() and cairo_fill() if you want it cleared.
592 * Note that the stride may be larger than
593 * width*bytes_per_pixel to provide proper alignment for each pixel
594 * and row. This alignment is required to allow high-performance rendering
595 * within cairo. The correct way to obtain a legal stride value is to
596 * call cairo_format_stride_for_width() with the desired format and
597 * maximum image width value, and then use the resulting stride value
598 * to allocate the data and to create the image surface. See
599 * cairo_format_stride_for_width() for example code.
601 * Return value: a pointer to the newly created surface. The caller
602 * owns the surface and should call cairo_surface_destroy() when done
605 * This function always returns a valid pointer, but it will return a
606 * pointer to a "nil" surface in the case of an error such as out of
607 * memory or an invalid stride value. In case of invalid stride value
608 * the error status of the returned surface will be
609 * %CAIRO_STATUS_INVALID_STRIDE. You can use
610 * cairo_surface_status() to check for this.
612 * See cairo_surface_set_user_data() for a means of attaching a
613 * destroy-notification fallback to the surface if necessary.
618 cairo_image_surface_create_for_data (unsigned char *data,
619 cairo_format_t format,
624 pixman_format_code_t pixman_format;
627 if (! CAIRO_FORMAT_VALID (format))
628 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
630 if ((stride & (CAIRO_STRIDE_ALIGNMENT-1)) != 0)
631 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
633 if (! _cairo_image_surface_is_size_valid (width, height))
634 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
636 minstride = cairo_format_stride_for_width (format, width);
638 if (stride > -minstride) {
639 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
642 if (stride < minstride) {
643 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
647 pixman_format = _cairo_format_to_pixman_format_code (format);
648 return _cairo_image_surface_create_with_pixman_format (data,
653 slim_hidden_def (cairo_image_surface_create_for_data);
656 * cairo_image_surface_get_data:
657 * @surface: a #cairo_image_surface_t
659 * Get a pointer to the data of the image surface, for direct
660 * inspection or modification.
662 * A call to cairo_surface_flush() is required before accessing the
663 * pixel data to ensure that all pending drawing operations are
664 * finished. A call to cairo_surface_mark_dirty() is required after
665 * the data is modified.
667 * Return value: a pointer to the image data of this surface or %NULL
668 * if @surface is not an image surface, or if cairo_surface_finish()
674 cairo_image_surface_get_data (cairo_surface_t *surface)
676 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
678 if (! _cairo_surface_is_image (surface)) {
679 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
683 return image_surface->data;
685 slim_hidden_def (cairo_image_surface_get_data);
688 * cairo_image_surface_get_format:
689 * @surface: a #cairo_image_surface_t
691 * Get the format of the surface.
693 * Return value: the format of the surface
698 cairo_image_surface_get_format (cairo_surface_t *surface)
700 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
702 if (! _cairo_surface_is_image (surface)) {
703 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
704 return CAIRO_FORMAT_INVALID;
707 return image_surface->format;
709 slim_hidden_def (cairo_image_surface_get_format);
712 * cairo_image_surface_get_width:
713 * @surface: a #cairo_image_surface_t
715 * Get the width of the image surface in pixels.
717 * Return value: the width of the surface in pixels.
722 cairo_image_surface_get_width (cairo_surface_t *surface)
724 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
726 if (! _cairo_surface_is_image (surface)) {
727 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
731 return image_surface->width;
733 slim_hidden_def (cairo_image_surface_get_width);
736 * cairo_image_surface_get_height:
737 * @surface: a #cairo_image_surface_t
739 * Get the height of the image surface in pixels.
741 * Return value: the height of the surface in pixels.
746 cairo_image_surface_get_height (cairo_surface_t *surface)
748 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
750 if (! _cairo_surface_is_image (surface)) {
751 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
755 return image_surface->height;
757 slim_hidden_def (cairo_image_surface_get_height);
760 * cairo_image_surface_get_stride:
761 * @surface: a #cairo_image_surface_t
763 * Get the stride of the image surface in bytes
765 * Return value: the stride of the image surface in bytes (or 0 if
766 * @surface is not an image surface). The stride is the distance in
767 * bytes from the beginning of one row of the image data to the
768 * beginning of the next row.
773 cairo_image_surface_get_stride (cairo_surface_t *surface)
776 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
778 if (! _cairo_surface_is_image (surface)) {
779 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
783 return image_surface->stride;
785 slim_hidden_def (cairo_image_surface_get_stride);
788 _cairo_format_from_content (cairo_content_t content)
791 case CAIRO_CONTENT_COLOR:
792 return CAIRO_FORMAT_RGB24;
793 case CAIRO_CONTENT_ALPHA:
794 return CAIRO_FORMAT_A8;
795 case CAIRO_CONTENT_COLOR_ALPHA:
796 return CAIRO_FORMAT_ARGB32;
800 return CAIRO_FORMAT_INVALID;
804 _cairo_content_from_format (cairo_format_t format)
807 case CAIRO_FORMAT_ARGB32:
808 return CAIRO_CONTENT_COLOR_ALPHA;
809 case CAIRO_FORMAT_RGB30:
810 return CAIRO_CONTENT_COLOR;
811 case CAIRO_FORMAT_RGB24:
812 return CAIRO_CONTENT_COLOR;
813 case CAIRO_FORMAT_RGB16_565:
814 return CAIRO_CONTENT_COLOR;
815 case CAIRO_FORMAT_A8:
816 case CAIRO_FORMAT_A1:
817 return CAIRO_CONTENT_ALPHA;
818 case CAIRO_FORMAT_INVALID:
823 return CAIRO_CONTENT_COLOR_ALPHA;
827 _cairo_format_bits_per_pixel (cairo_format_t format)
830 case CAIRO_FORMAT_ARGB32:
831 case CAIRO_FORMAT_RGB30:
832 case CAIRO_FORMAT_RGB24:
834 case CAIRO_FORMAT_RGB16_565:
836 case CAIRO_FORMAT_A8:
838 case CAIRO_FORMAT_A1:
840 case CAIRO_FORMAT_INVALID:
848 _cairo_image_surface_create_similar (void *abstract_other,
849 cairo_content_t content,
853 cairo_image_surface_t *other = abstract_other;
855 TRACE ((stderr, "%s (other=%u)\n", __FUNCTION__, other->base.unique_id));
857 if (! _cairo_image_surface_is_size_valid (width, height))
858 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
860 if (content == other->base.content) {
861 return _cairo_image_surface_create_with_pixman_format (NULL,
862 other->pixman_format,
867 return _cairo_image_surface_create_with_content (content,
872 _cairo_image_surface_snapshot (void *abstract_surface)
874 cairo_image_surface_t *image = abstract_surface;
875 cairo_image_surface_t *clone;
877 /* If we own the image, we can simply steal the memory for the snapshot */
878 if (image->owns_data && image->base._finishing) {
879 clone = (cairo_image_surface_t *)
880 _cairo_image_surface_create_for_pixman_image (image->pixman_image,
881 image->pixman_format);
882 if (unlikely (clone->base.status))
885 image->pixman_image = NULL;
887 clone->transparency = image->transparency;
888 clone->color = image->color;
890 clone->owns_data = image->owns_data;
891 image->owns_data = FALSE;
895 clone = (cairo_image_surface_t *)
896 _cairo_image_surface_create_with_pixman_format (NULL,
897 image->pixman_format,
901 if (unlikely (clone->base.status))
904 if (clone->stride == image->stride) {
905 memcpy (clone->data, image->data, clone->stride * clone->height);
907 pixman_image_composite32 (PIXMAN_OP_SRC,
908 image->pixman_image, NULL, clone->pixman_image,
912 image->width, image->height);
914 clone->base.is_clear = FALSE;
918 cairo_image_surface_t *
919 _cairo_image_surface_map_to_image (void *abstract_other,
920 const cairo_rectangle_int_t *extents)
922 cairo_image_surface_t *other = abstract_other;
923 cairo_surface_t *surface;
927 data += extents->y * other->stride;
928 data += extents->x * PIXMAN_FORMAT_BPP (other->pixman_format)/ 8;
931 _cairo_image_surface_create_with_pixman_format (data,
932 other->pixman_format,
937 cairo_surface_set_device_offset (surface, -extents->x, -extents->y);
938 return (cairo_image_surface_t *) surface;
942 _cairo_image_surface_unmap_image (void *abstract_surface,
943 cairo_image_surface_t *image)
945 cairo_surface_finish (&image->base);
946 cairo_surface_destroy (&image->base);
948 return CAIRO_INT_STATUS_SUCCESS;
952 _cairo_image_surface_finish (void *abstract_surface)
954 cairo_image_surface_t *surface = abstract_surface;
956 if (surface->pixman_image) {
957 pixman_image_unref (surface->pixman_image);
958 surface->pixman_image = NULL;
961 if (surface->owns_data) {
962 free (surface->data);
963 surface->data = NULL;
966 if (surface->parent) {
967 cairo_surface_destroy (surface->parent);
968 surface->parent = NULL;
971 _cairo_image_shadow_caches_destroy ();
973 return CAIRO_STATUS_SUCCESS;
977 _cairo_image_surface_assume_ownership_of_data (cairo_image_surface_t *surface)
979 surface->owns_data = TRUE;
983 _cairo_image_surface_source (void *abstract_surface,
984 cairo_rectangle_int_t *extents)
986 cairo_image_surface_t *surface = abstract_surface;
989 extents->x = extents->y = 0;
990 extents->width = surface->width;
991 extents->height = surface->height;
994 return &surface->base;
998 _cairo_image_surface_acquire_source_image (void *abstract_surface,
999 cairo_image_surface_t **image_out,
1002 *image_out = abstract_surface;
1003 *image_extra = NULL;
1005 return CAIRO_STATUS_SUCCESS;
1009 _cairo_image_surface_release_source_image (void *abstract_surface,
1010 cairo_image_surface_t *image,
1015 /* high level image interface */
1017 _cairo_image_surface_get_extents (void *abstract_surface,
1018 cairo_rectangle_int_t *rectangle)
1020 cairo_image_surface_t *surface = abstract_surface;
1024 rectangle->width = surface->width;
1025 rectangle->height = surface->height;
1031 _cairo_image_surface_paint (void *abstract_surface,
1032 cairo_operator_t op,
1033 const cairo_pattern_t *source,
1034 const cairo_clip_t *clip)
1036 cairo_image_surface_t *surface = abstract_surface;
1037 cairo_int_status_t status;
1039 TRACE ((stderr, "%s (surface=%d)\n",
1040 __FUNCTION__, surface->base.unique_id));
1042 status = cairo_device_acquire (surface->base.device);
1043 if (unlikely (status))
1046 status = _cairo_surface_shadow_paint (abstract_surface, op, source,
1047 clip, &source->shadow);
1049 if (unlikely (status)) {
1050 cairo_device_release (surface->base.device);
1054 if (source->shadow.draw_shadow_only) {
1055 cairo_device_release (surface->base.device);
1059 status = _cairo_compositor_paint (surface->compositor,
1060 &surface->base, op, source, clip);
1061 cairo_device_release (surface->base.device);
1066 _cairo_image_surface_mask (void *abstract_surface,
1067 cairo_operator_t op,
1068 const cairo_pattern_t *source,
1069 const cairo_pattern_t *mask,
1070 const cairo_clip_t *clip)
1072 cairo_image_surface_t *surface = abstract_surface;
1073 cairo_int_status_t status;
1075 TRACE ((stderr, "%s (surface=%d)\n",
1076 __FUNCTION__, surface->base.unique_id));
1078 status = cairo_device_acquire (surface->base.device);
1079 if (unlikely (status))
1082 status = _cairo_surface_shadow_mask (abstract_surface, op, source,
1083 mask, clip, &source->shadow);
1085 if (unlikely (status)) {
1086 cairo_device_release (surface->base.device);
1090 if (source->shadow.draw_shadow_only) {
1091 cairo_device_release (surface->base.device);
1095 status = _cairo_compositor_mask (surface->compositor,
1096 &surface->base, op, source, mask, clip);
1097 cairo_device_release (surface->base.device);
1102 _cairo_image_surface_stroke (void *abstract_surface,
1103 cairo_operator_t op,
1104 const cairo_pattern_t *source,
1105 const cairo_path_fixed_t *path,
1106 const cairo_stroke_style_t *style,
1107 const cairo_matrix_t *ctm,
1108 const cairo_matrix_t *ctm_inverse,
1110 cairo_antialias_t antialias,
1111 const cairo_clip_t *clip)
1113 cairo_int_status_t status;
1114 cairo_image_surface_t *surface = abstract_surface;
1115 cairo_shadow_type_t shadow_type = source->shadow.type;
1117 TRACE ((stderr, "%s (surface=%d)\n",
1118 __FUNCTION__, surface->base.unique_id));
1120 status = cairo_device_acquire (surface->base.device);
1121 if (unlikely (status))
1124 if (shadow_type != CAIRO_SHADOW_INSET)
1125 status = _cairo_surface_shadow_stroke (abstract_surface, op, source,
1126 path, style, ctm, ctm_inverse,
1127 tolerance, antialias, clip,
1130 if (unlikely (status)) {
1131 cairo_device_release (surface->base.device);
1135 if (shadow_type == CAIRO_SHADOW_DROP &&
1136 source->shadow.draw_shadow_only) {
1137 cairo_device_release (surface->base.device);
1141 if (! source->shadow.draw_shadow_only)
1142 status = _cairo_compositor_stroke (surface->compositor, &surface->base,
1144 style, ctm, ctm_inverse,
1145 tolerance, antialias, clip);
1147 if (unlikely (status)) {
1148 cairo_device_release (surface->base.device);
1152 if (shadow_type == CAIRO_SHADOW_INSET)
1153 status = _cairo_surface_shadow_stroke (abstract_surface, op, source,
1154 path, style, ctm, ctm_inverse,
1155 tolerance, antialias, clip,
1158 cairo_device_release (surface->base.device);
1163 _cairo_image_surface_fill (void *abstract_surface,
1164 cairo_operator_t op,
1165 const cairo_pattern_t *source,
1166 const cairo_path_fixed_t *path,
1167 cairo_fill_rule_t fill_rule,
1169 cairo_antialias_t antialias,
1170 const cairo_clip_t *clip)
1172 cairo_int_status_t status;
1173 cairo_image_surface_t *surface = abstract_surface;
1174 cairo_shadow_type_t shadow_type = source->shadow.type;
1176 TRACE ((stderr, "%s (surface=%d)\n",
1177 __FUNCTION__, surface->base.unique_id));
1179 status = cairo_device_acquire (surface->base.device);
1180 if (unlikely (status))
1183 if (shadow_type != CAIRO_SHADOW_INSET)
1184 status = _cairo_surface_shadow_fill (abstract_surface, op, source,
1186 tolerance, antialias, clip,
1189 if (unlikely (status)) {
1190 cairo_device_release (surface->base.device);
1194 if (shadow_type == CAIRO_SHADOW_DROP &&
1195 source->shadow.draw_shadow_only) {
1196 cairo_device_release (surface->base.device);
1200 if (! source->shadow.draw_shadow_only) {
1201 if (! source->shadow.path_is_fill_with_spread ||
1202 source->shadow.type != CAIRO_SHADOW_INSET)
1203 status = _cairo_compositor_fill (surface->compositor,
1206 fill_rule, tolerance,
1210 status = _cairo_compositor_paint (surface->compositor,
1215 if (unlikely (status)) {
1216 cairo_device_release (surface->base.device);
1220 if (shadow_type == CAIRO_SHADOW_INSET)
1221 status = _cairo_surface_shadow_fill (abstract_surface, op, source,
1223 tolerance, antialias, clip,
1226 cairo_device_release (surface->base.device);
1231 _cairo_image_surface_glyphs (void *abstract_surface,
1232 cairo_operator_t op,
1233 const cairo_pattern_t *source,
1234 cairo_glyph_t *glyphs,
1236 cairo_scaled_font_t *scaled_font,
1237 const cairo_clip_t *clip)
1239 cairo_int_status_t status;
1240 cairo_image_surface_t *surface = abstract_surface;
1241 cairo_shadow_type_t shadow_type = source->shadow.type;
1243 TRACE ((stderr, "%s (surface=%d)\n",
1244 __FUNCTION__, surface->base.unique_id));
1246 status = cairo_device_acquire (surface->base.device);
1247 if (unlikely (status))
1250 if (shadow_type != CAIRO_SHADOW_INSET)
1251 status = _cairo_surface_shadow_glyphs (abstract_surface, op, source,
1257 if (unlikely (status)) {
1258 cairo_device_release (surface->base.device);
1262 if (shadow_type == CAIRO_SHADOW_DROP &&
1263 source->shadow.draw_shadow_only) {
1264 cairo_device_release (surface->base.device);
1268 if (! source->shadow.draw_shadow_only)
1269 status = _cairo_compositor_glyphs (surface->compositor, &surface->base,
1271 glyphs, num_glyphs, scaled_font,
1274 if (unlikely (status)) {
1275 cairo_device_release (surface->base.device);
1279 if (shadow_type == CAIRO_SHADOW_INSET)
1280 status = _cairo_surface_shadow_glyphs (abstract_surface, op, source,
1286 cairo_device_release (surface->base.device);
1291 _cairo_image_surface_get_font_options (void *abstract_surface,
1292 cairo_font_options_t *options)
1294 _cairo_font_options_init_default (options);
1296 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
1297 _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
1300 static cairo_surface_t *
1301 _cairo_image_surface_shadow_surface (void *surface,
1302 cairo_bool_t has_blur,
1303 int width, int height,
1304 int *width_out, int *height_out)
1306 int shadow_width, shadow_height;
1307 cairo_image_surface_t *shadow_surface = NULL;
1309 if (width < MIN_IMAGE_SHADOW_SIZE)
1310 shadow_width = width;
1311 else if (has_blur) {
1312 if (width < MIN_IMAGE_SHADOW_SIZE * 2)
1313 shadow_width = MIN_IMAGE_SHADOW_SIZE;
1314 else if (width > MAX_IMAGE_SHADOW_SIZE * 2)
1315 shadow_width = MAX_IMAGE_SHADOW_SIZE;
1317 shadow_width = width * 0.5;
1320 if (width > MAX_IMAGE_SHADOW_SIZE)
1321 shadow_width = MAX_IMAGE_SHADOW_SIZE;
1323 shadow_width = width;
1326 if (height < MIN_IMAGE_SHADOW_SIZE)
1327 shadow_height = height;
1328 else if (has_blur) {
1329 if (height < MIN_IMAGE_SHADOW_SIZE * 2)
1330 shadow_height = MIN_IMAGE_SHADOW_SIZE;
1331 else if (height > MAX_IMAGE_SHADOW_SIZE * 2)
1332 shadow_height = MAX_IMAGE_SHADOW_SIZE;
1334 shadow_height = height * 0.5;
1337 if (height > MAX_IMAGE_SHADOW_SIZE)
1338 shadow_height = MAX_IMAGE_SHADOW_SIZE;
1340 shadow_height = height;
1343 shadow_surface = (cairo_image_surface_t *)
1344 cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1347 if (unlikely (shadow_surface->base.status)) {
1348 cairo_surface_destroy (&shadow_surface->base);
1352 *width_out = shadow_width;
1353 *height_out = shadow_height;
1355 return &shadow_surface->base;
1358 const cairo_surface_backend_t _cairo_image_surface_backend = {
1359 CAIRO_SURFACE_TYPE_IMAGE,
1360 _cairo_image_surface_finish,
1362 _cairo_default_context_create,
1364 _cairo_image_surface_create_similar,
1365 NULL, /* create similar image */
1366 _cairo_image_surface_map_to_image,
1367 _cairo_image_surface_unmap_image,
1369 _cairo_image_surface_source,
1370 _cairo_image_surface_acquire_source_image,
1371 _cairo_image_surface_release_source_image,
1372 _cairo_image_surface_snapshot,
1374 NULL, /* copy_page */
1375 NULL, /* show_page */
1377 _cairo_image_surface_get_extents,
1378 _cairo_image_surface_get_font_options,
1383 _cairo_image_surface_paint,
1384 _cairo_image_surface_mask,
1385 _cairo_image_surface_stroke,
1386 _cairo_image_surface_fill,
1387 NULL, /* fill-stroke */
1388 _cairo_image_surface_glyphs,
1389 NULL, /* has_text_glyphs */
1390 NULL, /* show_text_glyphs */
1391 NULL, /* get_supported_mime_types */
1392 _cairo_image_surface_shadow_surface,
1393 NULL, /* get_glyph_shadow_surface */
1394 NULL, /* get_shadow_mask_surface */
1395 NULL, /* get_glyph_shadow_mask_surface */
1396 _cairo_image_surface_shadow_cache_acquire,
1397 _cairo_image_surface_shadow_cache_release,
1398 _cairo_image_surface_get_shadow_cache,
1399 _cairo_image_surface_get_shadow_cache_size,
1400 _cairo_image_surface_has_shadow_cache,
1403 /* A convenience function for when one needs to coerce an image
1404 * surface to an alternate format. */
1405 cairo_image_surface_t *
1406 _cairo_image_surface_coerce (cairo_image_surface_t *surface)
1408 return _cairo_image_surface_coerce_to_format (surface,
1409 _cairo_format_from_content (surface->base.content));
1412 /* A convenience function for when one needs to coerce an image
1413 * surface to an alternate format. */
1414 cairo_image_surface_t *
1415 _cairo_image_surface_coerce_to_format (cairo_image_surface_t *surface,
1416 cairo_format_t format)
1418 cairo_image_surface_t *clone;
1419 cairo_status_t status;
1421 status = surface->base.status;
1422 if (unlikely (status))
1423 return (cairo_image_surface_t *)_cairo_surface_create_in_error (status);
1425 if (surface->format == format)
1426 return (cairo_image_surface_t *)cairo_surface_reference(&surface->base);
1428 clone = (cairo_image_surface_t *)
1429 cairo_image_surface_create (format, surface->width, surface->height);
1430 if (unlikely (clone->base.status))
1433 pixman_image_composite32 (PIXMAN_OP_SRC,
1434 surface->pixman_image, NULL, clone->pixman_image,
1438 surface->width, surface->height);
1439 clone->base.is_clear = FALSE;
1441 clone->base.device_transform =
1442 surface->base.device_transform;
1443 clone->base.device_transform_inverse =
1444 surface->base.device_transform_inverse;
1449 cairo_image_surface_t *
1450 _cairo_image_surface_create_from_image (cairo_image_surface_t *other,
1451 pixman_format_code_t format,
1453 int width, int height, int stride)
1455 cairo_image_surface_t *surface = NULL;
1456 cairo_status_t status;
1457 pixman_image_t *image;
1460 status = other->base.status;
1461 if (unlikely (status))
1465 mem = _cairo_malloc_ab (height, stride);
1466 if (unlikely (mem == NULL)) {
1467 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1472 image = pixman_image_create_bits (format, width, height, mem, stride);
1473 if (unlikely (image == NULL)) {
1474 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1478 surface = (cairo_image_surface_t *)
1479 _cairo_image_surface_create_for_pixman_image (image, format);
1480 if (unlikely (surface->base.status)) {
1481 status = surface->base.status;
1485 pixman_image_composite32 (PIXMAN_OP_SRC,
1486 other->pixman_image, NULL, image,
1491 surface->base.is_clear = FALSE;
1492 surface->owns_data = mem != NULL;
1497 pixman_image_unref (image);
1501 cairo_surface_destroy (&surface->base);
1502 return (cairo_image_surface_t *) _cairo_surface_create_in_error (status);
1505 cairo_image_transparency_t
1506 _cairo_image_analyze_transparency (cairo_image_surface_t *image)
1510 if (image->transparency != CAIRO_IMAGE_UNKNOWN)
1511 return image->transparency;
1513 if ((image->base.content & CAIRO_CONTENT_ALPHA) == 0)
1514 return image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1516 if (image->base.is_clear)
1517 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1519 if ((image->base.content & CAIRO_CONTENT_COLOR) == 0) {
1520 if (image->format == CAIRO_FORMAT_A1) {
1521 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1522 } else if (image->format == CAIRO_FORMAT_A8) {
1523 for (y = 0; y < image->height; y++) {
1524 uint8_t *alpha = (uint8_t *) (image->data + y * image->stride);
1526 for (x = 0; x < image->width; x++, alpha++) {
1527 if (*alpha > 0 && *alpha < 255)
1528 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1531 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1533 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1537 if (image->format == CAIRO_FORMAT_RGB16_565) {
1538 image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1539 return CAIRO_IMAGE_IS_OPAQUE;
1542 if (image->format != CAIRO_FORMAT_ARGB32)
1543 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1545 image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1546 for (y = 0; y < image->height; y++) {
1547 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1549 for (x = 0; x < image->width; x++, pixel++) {
1550 int a = (*pixel & 0xff000000) >> 24;
1551 if (a > 0 && a < 255) {
1552 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1553 } else if (a == 0) {
1554 image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1559 return image->transparency;
1563 _cairo_image_analyze_color (cairo_image_surface_t *image)
1567 if (image->color != CAIRO_IMAGE_UNKNOWN_COLOR)
1568 return image->color;
1570 if (image->format == CAIRO_FORMAT_A1)
1571 return image->color = CAIRO_IMAGE_IS_MONOCHROME;
1573 if (image->format == CAIRO_FORMAT_A8)
1574 return image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1576 if (image->format == CAIRO_FORMAT_ARGB32) {
1577 image->color = CAIRO_IMAGE_IS_MONOCHROME;
1578 for (y = 0; y < image->height; y++) {
1579 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1581 for (x = 0; x < image->width; x++, pixel++) {
1582 int a = (*pixel & 0xff000000) >> 24;
1583 int r = (*pixel & 0x00ff0000) >> 16;
1584 int g = (*pixel & 0x0000ff00) >> 8;
1585 int b = (*pixel & 0x000000ff);
1589 r = (r * 255 + a / 2) / a;
1590 g = (g * 255 + a / 2) / a;
1591 b = (b * 255 + a / 2) / a;
1593 if (!(r == g && g == b))
1594 return image->color = CAIRO_IMAGE_IS_COLOR;
1595 else if (r > 0 && r < 255)
1596 image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1599 return image->color;
1602 if (image->format == CAIRO_FORMAT_RGB24) {
1603 image->color = CAIRO_IMAGE_IS_MONOCHROME;
1604 for (y = 0; y < image->height; y++) {
1605 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1607 for (x = 0; x < image->width; x++, pixel++) {
1608 int r = (*pixel & 0x00ff0000) >> 16;
1609 int g = (*pixel & 0x0000ff00) >> 8;
1610 int b = (*pixel & 0x000000ff);
1611 if (!(r == g && g == b))
1612 return image->color = CAIRO_IMAGE_IS_COLOR;
1613 else if (r > 0 && r < 255)
1614 image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1617 return image->color;
1620 return image->color = CAIRO_IMAGE_IS_COLOR;
1623 cairo_image_surface_t *
1624 _cairo_image_surface_clone_subimage (cairo_surface_t *surface,
1625 const cairo_rectangle_int_t *extents)
1627 cairo_surface_t *image;
1628 cairo_surface_pattern_t pattern;
1629 cairo_status_t status;
1631 image = cairo_surface_create_similar_image (surface,
1632 _cairo_format_from_content (surface->content),
1636 return to_image_surface (image);
1638 /* TODO: check me with non-identity device_transform. Should we
1639 * clone the scaling, too? */
1640 cairo_surface_set_device_offset (image,
1644 _cairo_pattern_init_for_surface (&pattern, surface);
1645 pattern.base.filter = CAIRO_FILTER_NEAREST;
1647 status = _cairo_surface_paint (image,
1648 CAIRO_OPERATOR_SOURCE,
1652 _cairo_pattern_fini (&pattern.base);
1654 if (unlikely (status))
1657 /* We use the parent as a flag during map-to-image/umap-image that the
1658 * resultant image came from a fallback rather than as direct call
1659 * to the backend's map_to_image(). Whilst we use it as a simple flag,
1660 * we need to make sure the parent surface obeys the reference counting
1661 * semantics and is consistent for all callers.
1663 _cairo_image_surface_set_parent (to_image_surface (image),
1664 cairo_surface_reference (surface));
1666 return to_image_surface (image);
1669 cairo_surface_destroy (image);
1670 return to_image_surface (_cairo_surface_create_in_error (status));