[cairo-commit] 3 commits - src/cairo-glitz-surface.c src/cairoint.h src/cairo-meta-surface.c src/cairo-pdf-surface.c src/cairo-pdf-surface-private.h src/cairo-quartz-surface.c src/cairo-surface.c src/cairo-surface-fallback.c src/cairo-surface-private.h src/cairo-xcb-surface.c src/cairo-xlib-surface.c test/mask.c test/trap-clip.c

Chris Wilson ickle at kemper.freedesktop.org
Tue Jun 2 07:14:46 PDT 2009


 src/cairo-glitz-surface.c       |   16 ++
 src/cairo-meta-surface.c        |    1 
 src/cairo-pdf-surface-private.h |   13 +
 src/cairo-pdf-surface.c         |  134 +++++++++++++++----
 src/cairo-quartz-surface.c      |   19 ++
 src/cairo-surface-fallback.c    |    2 
 src/cairo-surface-private.h     |    7 -
 src/cairo-surface.c             |  272 ++++++++++++++++++++++++++++++----------
 src/cairo-xcb-surface.c         |   17 ++
 src/cairo-xlib-surface.c        |   20 ++
 src/cairoint.h                  |   12 +
 test/mask.c                     |   12 +
 test/trap-clip.c                |   12 +
 13 files changed, 432 insertions(+), 105 deletions(-)

New commits:
commit c9ddaf8e7f0006f308aab0b87cf0425b2690d914
Author: Adrian Johnson <ajohnson at redneon.com>
Date:   Sun Sep 7 13:09:09 2008 +0930

    PDF: Don't embed the same pattern more than once
    
    The PDF surface now keeps track of all the patterns it is embedding in
    a hash table keyed by the unique_id returned by the
    _cairo_surface_get_unique_id().

diff --git a/src/cairo-pdf-surface-private.h b/src/cairo-pdf-surface-private.h
index d2fa43c..e64b78a 100644
--- a/src/cairo-pdf-surface-private.h
+++ b/src/cairo-pdf-surface-private.h
@@ -60,13 +60,19 @@ typedef struct _cairo_pdf_group_resources {
     cairo_array_t fonts;
 } cairo_pdf_group_resources_t;
 
+typedef struct _cairo_pdf_pattern_entry {
+    cairo_hash_entry_t base;
+    unsigned int id;
+    cairo_pdf_resource_t pattern_res;
+    cairo_pdf_resource_t gstate_res;
+} cairo_pdf_pattern_entry_t;
+
 typedef struct _cairo_pdf_pattern {
     double width;
     double height;
     cairo_rectangle_int_t extents;
     cairo_pattern_t *pattern;
-    cairo_pdf_resource_t pattern_res;
-    cairo_pdf_resource_t gstate_res;
+    cairo_pdf_pattern_entry_t *hash_entry;
 } cairo_pdf_pattern_t;
 
 typedef enum _cairo_pdf_operation {
@@ -118,7 +124,8 @@ struct _cairo_pdf_surface {
     cairo_array_t pages;
     cairo_array_t rgb_linear_functions;
     cairo_array_t alpha_linear_functions;
-    cairo_array_t patterns;
+    cairo_array_t page_patterns;
+    cairo_hash_table_t *all_patterns;
     cairo_array_t smask_groups;
     cairo_array_t knockout_group;
 
diff --git a/src/cairo-pdf-surface.c b/src/cairo-pdf-surface.c
index b1458d8..2918523 100644
--- a/src/cairo-pdf-surface.c
+++ b/src/cairo-pdf-surface.c
@@ -194,6 +194,9 @@ _cairo_pdf_surface_write_page (cairo_pdf_surface_t *surface);
 static cairo_status_t
 _cairo_pdf_surface_emit_font_subsets (cairo_pdf_surface_t *surface);
 
+static cairo_bool_t
+_cairo_pdf_pattern_equal (const void *key_a, const void *key_b);
+
 static const cairo_surface_backend_t cairo_pdf_surface_backend;
 static const cairo_paginated_surface_backend_t cairo_pdf_surface_paginated_backend;
 
@@ -268,23 +271,29 @@ _cairo_pdf_surface_create_for_stream_internal (cairo_output_stream_t	*output,
     _cairo_array_init (&surface->rgb_linear_functions, sizeof (cairo_pdf_rgb_linear_function_t));
     _cairo_array_init (&surface->alpha_linear_functions, sizeof (cairo_pdf_alpha_linear_function_t));
     _cairo_array_init (&surface->fonts, sizeof (cairo_pdf_font_t));
-    _cairo_array_init (&surface->patterns, sizeof (cairo_pdf_pattern_t));
     _cairo_array_init (&surface->smask_groups, sizeof (cairo_pdf_smask_group_t *));
     _cairo_array_init (&surface->knockout_group, sizeof (cairo_pdf_resource_t));
 
+    _cairo_array_init (&surface->page_patterns, sizeof (cairo_pdf_pattern_t));
+    surface->all_patterns = _cairo_hash_table_create (_cairo_pdf_pattern_equal);
+    if (unlikely (surface->all_patterns == NULL)) {
+	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
+	goto BAIL0;
+    }
+
     _cairo_pdf_group_resources_init (&surface->resources);
 
     surface->font_subsets = _cairo_scaled_font_subsets_create_composite ();
     if (! surface->font_subsets) {
 	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
-	goto BAIL0;
+	goto BAIL1;
     }
 
     surface->next_available_resource.id = 1;
     surface->pages_resource = _cairo_pdf_surface_new_object (surface);
     if (surface->pages_resource.id == 0) {
 	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
-        goto BAIL1;
+        goto BAIL2;
     }
 
     surface->pdf_version = CAIRO_PDF_VERSION_1_5;
@@ -324,8 +333,10 @@ _cairo_pdf_surface_create_for_stream_internal (cairo_output_stream_t	*output,
 	return surface->paginated_surface;
     }
 
-BAIL1:
+BAIL2:
     _cairo_scaled_font_subsets_destroy (surface->font_subsets);
+BAIL1:
+    _cairo_hash_table_destroy (surface->all_patterns);
 BAIL0:
     _cairo_array_fini (&surface->objects);
     free (surface);
@@ -573,12 +584,12 @@ _cairo_pdf_surface_clear (cairo_pdf_surface_t *surface)
     cairo_pdf_pattern_t *pattern;
     cairo_pdf_smask_group_t *group;
 
-    size = _cairo_array_num_elements (&surface->patterns);
+    size = _cairo_array_num_elements (&surface->page_patterns);
     for (i = 0; i < size; i++) {
-	pattern = (cairo_pdf_pattern_t *) _cairo_array_index (&surface->patterns, i);
+	pattern = (cairo_pdf_pattern_t *) _cairo_array_index (&surface->page_patterns, i);
 	cairo_pattern_destroy (pattern->pattern);
     }
-    _cairo_array_truncate (&surface->patterns, 0);
+    _cairo_array_truncate (&surface->page_patterns, 0);
 
     size = _cairo_array_num_elements (&surface->smask_groups);
     for (i = 0; i < size; i++) {
@@ -861,6 +872,21 @@ _cairo_pdf_surface_add_smask_group (cairo_pdf_surface_t     *surface,
     return _cairo_array_append (&surface->smask_groups, &group);
 }
 
+static cairo_bool_t
+_cairo_pdf_pattern_equal (const void *key_a, const void *key_b)
+{
+    const cairo_pdf_pattern_entry_t *a = key_a;
+    const cairo_pdf_pattern_entry_t *b = key_b;
+
+    return a->id == b->id;
+}
+
+static void
+_cairo_pdf_pattern_init_key (cairo_pdf_pattern_entry_t *key)
+{
+    key->base.hash = key->id;
+}
+
 static cairo_status_t
 _cairo_pdf_surface_add_pdf_pattern (cairo_pdf_surface_t		*surface,
 				    const cairo_pattern_t	*pattern,
@@ -869,6 +895,8 @@ _cairo_pdf_surface_add_pdf_pattern (cairo_pdf_surface_t		*surface,
 				    cairo_pdf_resource_t	*gstate_res)
 {
     cairo_pdf_pattern_t pdf_pattern;
+    cairo_pdf_pattern_entry_t pdf_pattern_key;
+    cairo_pdf_pattern_entry_t *pdf_pattern_entry;
     cairo_status_t status;
 
     /* Solid colors are emitted into the content stream */
@@ -878,6 +906,7 @@ _cairo_pdf_surface_add_pdf_pattern (cairo_pdf_surface_t		*surface,
 	return CAIRO_STATUS_SUCCESS;
     }
 
+    pdf_pattern_key.id = 0;
     if (pattern->type == CAIRO_PATTERN_TYPE_LINEAR ||
         pattern->type == CAIRO_PATTERN_TYPE_RADIAL)
     {
@@ -897,24 +926,50 @@ _cairo_pdf_surface_add_pdf_pattern (cairo_pdf_surface_t		*surface,
 	}
     }
 
+    if (pattern->type == CAIRO_PATTERN_TYPE_SURFACE) {
+	cairo_surface_pattern_t *surface = (cairo_surface_pattern_t *) pattern;
+
+	pdf_pattern_key.id  = surface->surface->unique_id;
+    }
+
+    _cairo_pdf_pattern_init_key (&pdf_pattern_key);
+    if (pdf_pattern_key.base.hash != 0 &&
+	(pdf_pattern_entry = _cairo_hash_table_lookup (surface->all_patterns,
+						       &pdf_pattern_key.base)))
+    {
+	*pattern_res = pdf_pattern_entry->pattern_res;
+	*gstate_res = pdf_pattern_entry->gstate_res;
+
+	return CAIRO_STATUS_SUCCESS;
+    }
+
+    pdf_pattern_entry = malloc (sizeof (cairo_pdf_pattern_entry_t));
+    if (pdf_pattern_entry == NULL)
+	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
+
+    pdf_pattern_entry->id = pdf_pattern_key.id;
+    _cairo_pdf_pattern_init_key (pdf_pattern_entry);
+
+    pdf_pattern.hash_entry = pdf_pattern_entry;
+
     status = _cairo_pattern_create_copy (&pdf_pattern.pattern, pattern);
     if (unlikely (status))
 	return status;
 
-    pdf_pattern.pattern_res = _cairo_pdf_surface_new_object (surface);
-    if (pdf_pattern.pattern_res.id == 0) {
+    pdf_pattern_entry->pattern_res = _cairo_pdf_surface_new_object (surface);
+    if (pdf_pattern_entry->pattern_res.id == 0) {
 	cairo_pattern_destroy (pdf_pattern.pattern);
 	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
     }
 
-    pdf_pattern.gstate_res.id = 0;
+    pdf_pattern_entry->gstate_res.id = 0;
 
     /* gradient patterns require an smask object to implement transparency */
     if (pattern->type == CAIRO_PATTERN_TYPE_LINEAR ||
         pattern->type == CAIRO_PATTERN_TYPE_RADIAL) {
         if (_cairo_pattern_is_opaque (pattern) == FALSE) {
-            pdf_pattern.gstate_res = _cairo_pdf_surface_new_object (surface);
-	    if (pdf_pattern.gstate_res.id == 0) {
+            pdf_pattern_entry->gstate_res = _cairo_pdf_surface_new_object (surface);
+	    if (pdf_pattern_entry->gstate_res.id == 0) {
 		cairo_pattern_destroy (pdf_pattern.pattern);
 		return _cairo_error (CAIRO_STATUS_NO_MEMORY);
 	    }
@@ -932,15 +987,22 @@ _cairo_pdf_surface_add_pdf_pattern (cairo_pdf_surface_t		*surface,
 	pdf_pattern.extents.height = surface->height;
     }
 
-    *pattern_res = pdf_pattern.pattern_res;
-    *gstate_res = pdf_pattern.gstate_res;
+    *pattern_res = pdf_pattern_entry->pattern_res;
+    *gstate_res = pdf_pattern_entry->gstate_res;
 
-    status = _cairo_array_append (&surface->patterns, &pdf_pattern);
+    status = _cairo_array_append (&surface->page_patterns, &pdf_pattern);
     if (unlikely (status)) {
 	cairo_pattern_destroy (pdf_pattern.pattern);
 	return status;
     }
 
+    if (pdf_pattern_entry->base.hash != 0) {
+	status = _cairo_hash_table_insert (surface->all_patterns,
+					   &pdf_pattern_entry->base);
+	if (unlikely (status))
+	    return status;
+    }
+
     return CAIRO_STATUS_SUCCESS;
 }
 
@@ -1287,6 +1349,16 @@ _cairo_pdf_surface_create_similar (void			*abstract_surface,
     return _cairo_meta_surface_create (content, width, height);
 }
 
+static void
+_cairo_pdf_pattern_entry_pluck (void *entry, void *closure)
+{
+    cairo_pdf_pattern_entry_t *pattern_entry = entry;
+    cairo_hash_table_t *patterns = closure;
+
+    _cairo_hash_table_remove (patterns, &pattern_entry->base);
+    free (pattern_entry);
+}
+
 static cairo_status_t
 _cairo_pdf_surface_finish (void *abstract_surface)
 {
@@ -1366,7 +1438,11 @@ _cairo_pdf_surface_finish (void *abstract_surface)
     _cairo_array_fini (&surface->pages);
     _cairo_array_fini (&surface->rgb_linear_functions);
     _cairo_array_fini (&surface->alpha_linear_functions);
-    _cairo_array_fini (&surface->patterns);
+    _cairo_array_fini (&surface->page_patterns);
+    _cairo_hash_table_foreach (surface->all_patterns,
+			       _cairo_pdf_pattern_entry_pluck,
+			       surface->all_patterns);
+    _cairo_hash_table_destroy (surface->all_patterns);
     _cairo_array_fini (&surface->smask_groups);
     _cairo_array_fini (&surface->fonts);
     _cairo_array_fini (&surface->knockout_group);
@@ -2062,9 +2138,9 @@ _cairo_pdf_surface_emit_surface_pattern (cairo_pdf_surface_t	*surface,
     cairo_matrix_translate (&pdf_p2d, 0.0, pattern_height);
     cairo_matrix_scale (&pdf_p2d, 1.0, -1.0);
 
-    _cairo_pdf_surface_update_object (surface, pdf_pattern->pattern_res);
+    _cairo_pdf_surface_update_object (surface, pdf_pattern->hash_entry->pattern_res);
     status = _cairo_pdf_surface_open_stream (surface,
-				             &pdf_pattern->pattern_res,
+				             &pdf_pattern->hash_entry->pattern_res,
 					     FALSE,
 					     "   /PatternType 1\n"
 					     "   /BBox [0 0 %d %d]\n"
@@ -2679,7 +2755,7 @@ _cairo_pdf_surface_emit_linear_pattern (cairo_pdf_surface_t    *surface,
 	}
     }
 
-    _cairo_pdf_surface_update_object (surface, pdf_pattern->pattern_res);
+    _cairo_pdf_surface_update_object (surface, pdf_pattern->hash_entry->pattern_res);
     _cairo_output_stream_printf (surface->output,
                                  "%d 0 obj\n"
                                  "<< /Type /Pattern\n"
@@ -2691,7 +2767,7 @@ _cairo_pdf_surface_emit_linear_pattern (cairo_pdf_surface_t    *surface,
                                  "         /Coords [ %f %f %f %f ]\n"
                                  "         /Domain [ %f %f ]\n"
                                  "         /Function %d 0 R\n",
-				 pdf_pattern->pattern_res.id,
+				 pdf_pattern->hash_entry->pattern_res.id,
                                  pat_to_pdf.xx, pat_to_pdf.yx,
                                  pat_to_pdf.xy, pat_to_pdf.yy,
                                  pat_to_pdf.x0, pat_to_pdf.y0,
@@ -2715,7 +2791,7 @@ _cairo_pdf_surface_emit_linear_pattern (cairo_pdf_surface_t    *surface,
     if (alpha_function.id != 0) {
 	cairo_pdf_resource_t mask_resource;
 
-	assert (pdf_pattern->gstate_res.id != 0);
+	assert (pdf_pattern->hash_entry->gstate_res.id != 0);
 
         /* Create pattern for SMask. */
         mask_resource = _cairo_pdf_surface_new_object (surface);
@@ -2758,7 +2834,7 @@ _cairo_pdf_surface_emit_linear_pattern (cairo_pdf_surface_t    *surface,
             return status;
 
 	status = cairo_pdf_surface_emit_transparency_group (surface,
-						            pdf_pattern->gstate_res,
+						            pdf_pattern->hash_entry->gstate_res,
 							    mask_resource);
 	if (unlikely (status))
 	    return status;
@@ -2802,7 +2878,7 @@ _cairo_pdf_surface_emit_radial_pattern (cairo_pdf_surface_t    *surface,
     y2 = _cairo_fixed_to_double (pattern->c2.y);
     r2 = _cairo_fixed_to_double (pattern->r2);
 
-    _cairo_pdf_surface_update_object (surface, pdf_pattern->pattern_res);
+    _cairo_pdf_surface_update_object (surface, pdf_pattern->hash_entry->pattern_res);
 
     _cairo_output_stream_printf (surface->output,
                                  "%d 0 obj\n"
@@ -2814,7 +2890,7 @@ _cairo_pdf_surface_emit_radial_pattern (cairo_pdf_surface_t    *surface,
                                  "         /ColorSpace /DeviceRGB\n"
                                  "         /Coords [ %f %f %f %f %f %f ]\n"
                                  "         /Function %d 0 R\n",
-				 pdf_pattern->pattern_res.id,
+				 pdf_pattern->hash_entry->pattern_res.id,
                                  pat_to_pdf.xx, pat_to_pdf.yx,
                                  pat_to_pdf.xy, pat_to_pdf.yy,
                                  pat_to_pdf.x0, pat_to_pdf.y0,
@@ -2837,7 +2913,7 @@ _cairo_pdf_surface_emit_radial_pattern (cairo_pdf_surface_t    *surface,
     if (alpha_function.id != 0) {
 	cairo_pdf_resource_t mask_resource;
 
-	assert (pdf_pattern->gstate_res.id != 0);
+	assert (pdf_pattern->hash_entry->gstate_res.id != 0);
 
 	/* Create pattern for SMask. */
         mask_resource = _cairo_pdf_surface_new_object (surface);
@@ -2875,7 +2951,7 @@ _cairo_pdf_surface_emit_radial_pattern (cairo_pdf_surface_t    *surface,
                                      "endobj\n");
 
 	status = cairo_pdf_surface_emit_transparency_group (surface,
-						            pdf_pattern->gstate_res,
+						            pdf_pattern->hash_entry->gstate_res,
 							    mask_resource);
 	if (unlikely (status))
 	    return status;
@@ -4522,7 +4598,7 @@ _cairo_pdf_surface_write_patterns_and_smask_groups (cairo_pdf_surface_t *surface
      */
     pattern_index = 0;
     group_index = 0;
-    while ((pattern_index < _cairo_array_num_elements (&surface->patterns)) ||
+    while ((pattern_index < _cairo_array_num_elements (&surface->page_patterns)) ||
 	   (group_index < _cairo_array_num_elements (&surface->smask_groups)))
     {
 	for (; group_index < _cairo_array_num_elements (&surface->smask_groups); group_index++) {
@@ -4532,8 +4608,8 @@ _cairo_pdf_surface_write_patterns_and_smask_groups (cairo_pdf_surface_t *surface
 		return status;
 	}
 
-	for (; pattern_index < _cairo_array_num_elements (&surface->patterns); pattern_index++) {
-	    _cairo_array_copy_element (&surface->patterns, pattern_index, &pattern);
+	for (; pattern_index < _cairo_array_num_elements (&surface->page_patterns); pattern_index++) {
+	    _cairo_array_copy_element (&surface->page_patterns, pattern_index, &pattern);
 	    status = _cairo_pdf_surface_emit_pattern (surface, &pattern);
 	    if (unlikely (status))
 		return status;
commit f7613eeecea1218d353a183078fb77bc5cf4e72e
Author: Paolo Bonzini <bonzini at gnu.org>
Date:   Tue Nov 25 20:15:15 2008 +0100

    Add more surface snapshots providers
    
    This patch adds more implementation of the snapshot method.  For
    surface types where acquire_source_image is already making a copy
    of the bits, doing another one as is the case for the fallback
    implementation is a waste.

diff --git a/src/cairo-glitz-surface.c b/src/cairo-glitz-surface.c
index 7cc2b23..cd0d254 100644
--- a/src/cairo-glitz-surface.c
+++ b/src/cairo-glitz-surface.c
@@ -300,6 +300,20 @@ _cairo_glitz_surface_acquire_source_image (void              *abstract_surface,
     return _cairo_glitz_surface_get_image (surface, NULL, image_out, NULL);
 }
 
+static cairo_surface_t *
+_cairo_glitz_surface_snapshot (void *abstract_surface)
+{
+    cairo_glitz_surface_t *surface = abstract_surface;
+    cairo_status_t status;
+    cairo_image_surface_t *image;
+
+    status = _cairo_glitz_surface_get_image (surface, NULL, &image, NULL);
+    if (unlikely (status))
+	return _cairo_surface_create_in_error (status);
+
+    return &image->base;
+}
+
 static void
 _cairo_glitz_surface_release_source_image (void              *abstract_surface,
 					   cairo_image_surface_t *image,
@@ -2317,7 +2331,7 @@ static const cairo_surface_backend_t cairo_glitz_surface_backend = {
     NULL, /* fill */
     NULL, /* show_glyphs */
 
-    NULL, /* snapshot */
+    _cairo_glitz_surface_snapshot,
     _cairo_glitz_surface_is_similar,
 
     _cairo_glitz_surface_reset
diff --git a/src/cairo-quartz-surface.c b/src/cairo-quartz-surface.c
index 982ede4..892a156 100644
--- a/src/cairo-quartz-surface.c
+++ b/src/cairo-quartz-surface.c
@@ -1522,6 +1522,23 @@ _cairo_quartz_surface_acquire_source_image (void *abstract_surface,
     return CAIRO_STATUS_SUCCESS;
 }
 
+static cairo_surface_t *
+_cairo_quartz_surface_snapshot (void *abstract_surface)
+{
+    cairo_int_status_t status;
+    cairo_quartz_surface_t *surface = abstract_surface;
+    cairo_image_surface_t *image;
+
+    if (surface->imageSurfaceEquiv)
+	return NULL;
+
+    status = _cairo_quartz_get_image (surface, &image);
+    if (unlikely (status))
+        return _cairo_surface_create_in_error (CAIRO_STATUS_NO_MEMORY);
+
+    return &image->base;
+}
+
 static void
 _cairo_quartz_surface_release_source_image (void *abstract_surface,
 					     cairo_image_surface_t *image,
@@ -2458,7 +2475,7 @@ static const struct _cairo_surface_backend cairo_quartz_surface_backend = {
     NULL, /* show_glyphs */
 #endif
 
-    NULL, /* snapshot */
+    _cairo_quartz_surface_snapshot,
     NULL, /* is_similar */
     NULL, /* reset */
     NULL  /* fill_stroke */
diff --git a/src/cairo-xcb-surface.c b/src/cairo-xcb-surface.c
index 077e1c4..30a5c6b 100644
--- a/src/cairo-xcb-surface.c
+++ b/src/cairo-xcb-surface.c
@@ -611,6 +611,20 @@ _cairo_xcb_surface_acquire_source_image (void                    *abstract_surfa
     return CAIRO_STATUS_SUCCESS;
 }
 
+static cairo_surface_t *
+_cairo_xcb_surface_snapshot (void *abstract_surface)
+{
+    cairo_xcb_surface_t *surface = abstract_surface;
+    cairo_image_surface_t *image;
+    cairo_status_t status;
+
+    status = _get_image_surface (surface, NULL, &image, NULL);
+    if (unlikely (status))
+	return _cairo_surface_create_in_error (status);
+
+    return &image->base;
+}
+
 static void
 _cairo_xcb_surface_release_source_image (void                   *abstract_surface,
 					 cairo_image_surface_t  *image,
@@ -1704,7 +1718,8 @@ static const cairo_surface_backend_t cairo_xcb_surface_backend = {
     NULL, /* stroke */
     NULL, /* fill */
     _cairo_xcb_surface_show_glyphs,
-    NULL,  /* snapshot */
+
+    _cairo_xcb_surface_snapshot,
 
     _cairo_xcb_surface_is_similar,
 
diff --git a/src/cairo-xlib-surface.c b/src/cairo-xlib-surface.c
index ce34061..25bdfb3 100644
--- a/src/cairo-xlib-surface.c
+++ b/src/cairo-xlib-surface.c
@@ -1105,6 +1105,22 @@ _cairo_xlib_surface_acquire_source_image (void                    *abstract_surf
     return CAIRO_STATUS_SUCCESS;
 }
 
+static cairo_surface_t *
+_cairo_xlib_surface_snapshot (void *abstract_surface)
+{
+    cairo_xlib_surface_t *surface = abstract_surface;
+    cairo_image_surface_t *image;
+    cairo_status_t status;
+
+    _cairo_xlib_display_notify (surface->display);
+
+    status = _get_image_surface (surface, NULL, &image, NULL);
+    if (unlikely (status))
+	return _cairo_surface_create_in_error (status);
+
+    return &image->base;
+}
+
 static void
 _cairo_xlib_surface_release_source_image (void                   *abstract_surface,
 					  cairo_image_surface_t  *image,
@@ -2505,7 +2521,9 @@ static const cairo_surface_backend_t cairo_xlib_surface_backend = {
     NULL, /* stroke */
     NULL, /* fill */
     _cairo_xlib_surface_show_glyphs,
-    NULL, /* snapshot */
+
+    _cairo_xlib_surface_snapshot,
+
     _cairo_xlib_surface_is_similar,
     _cairo_xlib_surface_reset,
     NULL, /* fill_stroke */
commit cffb398f5a484000be458d04ef8f8bce3f6c7e3d
Author: Chris Wilson <chris at chris-wilson.co.uk>
Date:   Tue May 26 21:07:07 2009 +0100

    Add a generic cow-snapshotting framework
    
    Provide a mechanism for backends to attach and remove snapshots. This can
    be used by backends to provide a cache for _cairo_surface_clone_similar(),
    or by the meta-surfaces to only emit a single pattern for each unique
    snapshot.
    
    In order to prevent stale data being returned upon a snapshot operation,
    if the surface is modified (via the 5 high level operations, and on
    notification of external modification) we break the association with any
    current snapshot of the surface and thus preserve the current data for
    their use.

diff --git a/src/cairo-meta-surface.c b/src/cairo-meta-surface.c
index 6938526..d505adc 100644
--- a/src/cairo-meta-surface.c
+++ b/src/cairo-meta-surface.c
@@ -542,7 +542,6 @@ _cairo_meta_surface_snapshot (void *abstract_other)
 
     _cairo_surface_init (&meta->base, &cairo_meta_surface_backend,
 			 other->base.content);
-    meta->base.is_snapshot = TRUE;
 
     meta->width_pixels = other->width_pixels;
     meta->height_pixels = other->height_pixels;
diff --git a/src/cairo-surface-fallback.c b/src/cairo-surface-fallback.c
index e1a87ee..830c1b3 100644
--- a/src/cairo-surface-fallback.c
+++ b/src/cairo-surface-fallback.c
@@ -1196,7 +1196,7 @@ _cairo_surface_fallback_fill_rectangles (cairo_surface_t         *surface,
     int x1, y1, x2, y2;
     int i;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (num_rects <= 0)
 	return CAIRO_STATUS_SUCCESS;
diff --git a/src/cairo-surface-private.h b/src/cairo-surface-private.h
index b07f780..c25b6dc 100644
--- a/src/cairo-surface-private.h
+++ b/src/cairo-surface-private.h
@@ -43,6 +43,8 @@
 #include "cairo-types-private.h"
 #include "cairo-reference-count-private.h"
 
+typedef void (*cairo_surface_func_t) (cairo_surface_t *);
+
 struct _cairo_surface {
     const cairo_surface_backend_t *backend;
 
@@ -94,7 +96,10 @@ struct _cairo_surface {
     unsigned int current_clip_serial;
 
     /* A "snapshot" surface is immutable. See _cairo_surface_snapshot. */
-    cairo_bool_t is_snapshot;
+    cairo_surface_t *snapshot_of;
+    cairo_surface_func_t snapshot_detach;
+    /* current snapshots of this surface */
+    cairo_array_t snapshots;
 
     /*
      * Surface font options, falling back to backend's default options,
diff --git a/src/cairo-surface.c b/src/cairo-surface.c
index d21ebf2..2a5fbc1 100644
--- a/src/cairo-surface.c
+++ b/src/cairo-surface.c
@@ -62,7 +62,13 @@ const cairo_surface_t name = {					\
     NULL,				/* clip */		\
     0,					/* next_clip_serial */	\
     0,					/* current_clip_serial */	\
-    FALSE,				/* is_snapshot */	\
+    NULL,				/* snapshot_of */	\
+    NULL,				/* snapshot_detach */	\
+    { 0,	/* size */					\
+      0,	/* num_elements */				\
+      0,	/* element_size */				\
+      NULL,	/* elements */					\
+    },					/* snapshots */		\
     FALSE,				/* has_font_options */	\
     { CAIRO_ANTIALIAS_DEFAULT,		/* antialias */		\
       CAIRO_SUBPIXEL_ORDER_DEFAULT,	/* subpixel_order */	\
@@ -204,6 +210,113 @@ _cairo_surface_allocate_unique_id (void)
 #endif
 }
 
+static cairo_bool_t
+_cairo_surface_has_snapshots (cairo_surface_t *surface)
+{
+    return surface->snapshots.num_elements != 0;
+}
+
+static void
+_cairo_surface_detach_snapshots (cairo_surface_t *surface)
+{
+    cairo_surface_t **snapshots;
+    unsigned int i;
+
+    if (! _cairo_surface_has_snapshots (surface))
+	return;
+
+    /* XXX do something intelligent! */
+
+    snapshots = _cairo_array_index (&surface->snapshots, 0);
+    for (i = 0; i < surface->snapshots.num_elements; i++) {
+	snapshots[i]->snapshot_of = NULL;
+
+	if (snapshots[i]->snapshot_detach != NULL)
+	    snapshots[i]->snapshot_detach (snapshots[i]);
+    }
+    surface->snapshots.num_elements = 0;
+
+    assert (! _cairo_surface_has_snapshots (surface));
+}
+
+cairo_status_t
+_cairo_surface_attach_snapshot (cairo_surface_t *surface,
+				cairo_surface_t *snapshot,
+				cairo_surface_func_t detach_func)
+{
+    assert (surface != snapshot);
+
+    if (snapshot->snapshot_of != NULL)
+	_cairo_surface_detach_snapshot (snapshot);
+
+    snapshot->snapshot_of = surface;
+    snapshot->snapshot_detach = detach_func;
+
+    return _cairo_array_append (&surface->snapshots, &snapshot);
+}
+
+cairo_surface_t *
+_cairo_surface_has_snapshot (cairo_surface_t *surface,
+			     const cairo_surface_backend_t *backend)
+{
+    cairo_surface_t **snapshots;
+    unsigned int i;
+
+    snapshots = _cairo_array_index (&surface->snapshots, 0);
+    for (i = 0; i < surface->snapshots.num_elements; i++) {
+	if (snapshots[i]->backend == backend)
+	    return snapshots[i];
+    }
+
+    return NULL;
+}
+
+void
+_cairo_surface_detach_snapshot (cairo_surface_t *snapshot)
+{
+    cairo_surface_t *surface;
+    cairo_surface_t **snapshots;
+    unsigned int i;
+
+    assert (snapshot->snapshot_of != NULL);
+    surface = snapshot->snapshot_of;
+
+    snapshots = _cairo_array_index (&surface->snapshots, 0);
+    for (i = 0; i < surface->snapshots.num_elements; i++) {
+	if (snapshots[i] == snapshot)
+	    break;
+    }
+    assert (i < surface->snapshots.num_elements);
+
+    surface->snapshots.num_elements--;
+    memmove (&snapshots[i],
+	     &snapshots[i+1],
+	     sizeof (cairo_surface_t *)*(surface->snapshots.num_elements - i));
+
+    snapshot->snapshot_of = NULL;
+
+    if (snapshot->snapshot_detach != NULL)
+	snapshot->snapshot_detach (snapshot);
+}
+
+static cairo_bool_t
+_cairo_surface_is_writable (cairo_surface_t *surface)
+{
+    return ! surface->finished &&
+	   surface->snapshot_of == NULL &&
+	   ! _cairo_surface_has_snapshots (surface);
+}
+
+static void
+_cairo_surface_begin_modification (cairo_surface_t *surface)
+{
+    assert (surface->status == CAIRO_STATUS_SUCCESS);
+    assert (! surface->finished);
+    assert (surface->snapshot_of == NULL);
+
+    _cairo_surface_detach_snapshots (surface);
+}
+
 void
 _cairo_surface_init (cairo_surface_t			*surface,
 		     const cairo_surface_backend_t	*backend,
@@ -236,7 +349,8 @@ _cairo_surface_init (cairo_surface_t			*surface,
     surface->next_clip_serial = 0;
     surface->current_clip_serial = 0;
 
-    surface->is_snapshot = FALSE;
+    _cairo_array_init (&surface->snapshots, sizeof (cairo_surface_t *));
+    surface->snapshot_of = NULL;
 
     surface->has_font_options = FALSE;
 }
@@ -466,6 +580,7 @@ cairo_surface_destroy (cairo_surface_t *surface)
 
     _cairo_user_data_array_fini (&surface->user_data);
     _cairo_user_data_array_fini (&surface->mime_data);
+    _cairo_array_fini (&surface->snapshots);
 
     free (surface);
 }
@@ -565,6 +680,9 @@ cairo_surface_finish (cairo_surface_t *surface)
     }
 
     surface->finished = TRUE;
+
+    if (surface->snapshot_of != NULL)
+	_cairo_surface_detach_snapshot (surface);
 }
 slim_hidden_def (cairo_surface_finish);
 
@@ -801,7 +919,7 @@ _cairo_surface_set_font_options (cairo_surface_t       *surface,
     if (surface->status)
 	return;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (surface->finished) {
 	status = _cairo_surface_set_error (surface,
@@ -877,6 +995,9 @@ cairo_surface_flush (cairo_surface_t *surface)
     if (surface->finished)
 	return;
 
+    /* update the current snapshots *before* the user updates the surface */
+    _cairo_surface_detach_snapshots (surface);
+
     if (surface->backend->flush) {
 	status = surface->backend->flush (surface);
 	if (unlikely (status))
@@ -896,11 +1017,6 @@ slim_hidden_def (cairo_surface_flush);
 void
 cairo_surface_mark_dirty (cairo_surface_t *surface)
 {
-    if (surface->status)
-	return;
-
-    assert (! surface->is_snapshot);
-
     cairo_surface_mark_dirty_rectangle (surface, 0, 0, -1, -1);
 }
 
@@ -932,13 +1048,18 @@ cairo_surface_mark_dirty_rectangle (cairo_surface_t *surface,
     if (surface->status)
 	return;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (surface->finished) {
 	status = _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
 	return;
     }
 
+    /* The application *should* have called cairo_surface_flush() before
+     * modifying the surface independently of cairo (and thus having to
+     * call mark_dirty()). */
+    assert (! _cairo_surface_has_snapshots (surface));
+
     /* Always reset the clip here, to avoid having external calls to
      * clip manipulation functions of the underlying device clip result
      * in a desync between the cairo clip and the backend clip, due to
@@ -990,13 +1111,15 @@ _cairo_surface_set_device_scale (cairo_surface_t *surface,
     if (surface->status)
 	return;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (surface->finished) {
 	status = _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
 	return;
     }
 
+    _cairo_surface_begin_modification (surface);
+
     surface->device_transform.xx = sx;
     surface->device_transform.yy = sy;
     surface->device_transform.xy = 0.0;
@@ -1036,13 +1159,15 @@ cairo_surface_set_device_offset (cairo_surface_t *surface,
     if (surface->status)
 	return;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (surface->finished) {
 	status = _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
 	return;
     }
 
+    _cairo_surface_begin_modification (surface);
+
     surface->device_transform.x0 = x_offset;
     surface->device_transform.y0 = y_offset;
 
@@ -1119,13 +1244,15 @@ cairo_surface_set_fallback_resolution (cairo_surface_t	*surface,
     if (surface->status)
 	return;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (surface->finished) {
 	status = _cairo_surface_set_error (surface, CAIRO_STATUS_SURFACE_FINISHED);
 	return;
     }
 
+    _cairo_surface_begin_modification (surface);
+
     surface->x_fallback_resolution = x_pixels_per_inch;
     surface->y_fallback_resolution = y_pixels_per_inch;
 }
@@ -1263,7 +1390,7 @@ _cairo_surface_acquire_dest_image (cairo_surface_t         *surface,
     if (surface->status)
 	return surface->status;
 
-    assert (!surface->finished);
+    assert (_cairo_surface_is_writable (surface));
 
     if (surface->backend->acquire_dest_image == NULL)
 	return CAIRO_INT_STATUS_UNSUPPORTED;
@@ -1300,7 +1427,7 @@ _cairo_surface_release_dest_image (cairo_surface_t         *surface,
 				   cairo_rectangle_int_t   *image_rect,
 				   void                    *image_extra)
 {
-    assert (!surface->finished);
+    assert (_cairo_surface_is_writable (surface));
 
     if (surface->backend->release_dest_image)
 	surface->backend->release_dest_image (surface, interest_rect,
@@ -1460,18 +1587,41 @@ _cairo_surface_snapshot (cairo_surface_t *surface)
     if (surface->finished)
 	return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
 
-    if (surface->is_snapshot)
+    if (surface->snapshot_of != NULL)
 	return cairo_surface_reference (surface);
 
-    snapshot = NULL;
-    if (surface->backend->snapshot != NULL)
+    snapshot = _cairo_surface_has_snapshot (surface, surface->backend);
+    if (snapshot != NULL)
+	return cairo_surface_reference (snapshot);
+
+    if (surface->backend->snapshot != NULL) {
 	snapshot = surface->backend->snapshot (surface);
+	if (unlikely (snapshot->status))
+	    return snapshot;
+
+	/* Is this surface just a proxy - e.g. paginated surfaces? */
+	if (snapshot->backend != surface->backend) {
+	    cairo_surface_t *previous;
+
+	    previous = _cairo_surface_has_snapshot (surface,
+		                                    snapshot->backend);
+	    if (previous != NULL) {
+		cairo_surface_destroy (snapshot);
+		return cairo_surface_reference (previous);
+	    }
+	}
+    }
 
-    if (snapshot == NULL)
-	snapshot = _cairo_surface_fallback_snapshot (surface);
+    if (snapshot == NULL) {
+	snapshot = _cairo_surface_has_snapshot (surface,
+						&_cairo_image_surface_backend);
+	if (snapshot != NULL)
+	    return cairo_surface_reference (snapshot);
 
-    if (unlikely (snapshot->status))
-	return snapshot;
+	snapshot = _cairo_surface_fallback_snapshot (surface);
+	if (unlikely (snapshot->status))
+	    return snapshot;
+    }
 
     status = _cairo_surface_copy_mime_data (snapshot, surface);
     if (unlikely (status)) {
@@ -1482,7 +1632,11 @@ _cairo_surface_snapshot (cairo_surface_t *surface)
     snapshot->device_transform = surface->device_transform;
     snapshot->device_transform_inverse = surface->device_transform_inverse;
 
-    snapshot->is_snapshot = TRUE;
+    status = _cairo_surface_attach_snapshot (surface, snapshot, NULL);
+    if (unlikely (status)) {
+	cairo_surface_destroy (snapshot);
+	return _cairo_surface_create_in_error (status);
+    }
 
     return snapshot;
 }
@@ -1532,6 +1686,11 @@ _cairo_surface_composite (cairo_operator_t	op,
 {
     cairo_int_status_t status;
 
+    if (dst->status)
+	return dst->status;
+
+    assert (_cairo_surface_is_writable (dst));
+
     if (mask) {
 	/* These operators aren't interpreted the same way by the backends;
 	 * they are implemented in terms of other operators in cairo-gstate.c
@@ -1539,14 +1698,6 @@ _cairo_surface_composite (cairo_operator_t	op,
 	assert (op != CAIRO_OPERATOR_SOURCE && op != CAIRO_OPERATOR_CLEAR);
     }
 
-    if (dst->status)
-	return dst->status;
-
-    assert (! dst->is_snapshot);
-
-    if (dst->finished)
-	return _cairo_surface_set_error (dst, CAIRO_STATUS_SURFACE_FINISHED);
-
     if (dst->backend->composite) {
 	status = dst->backend->composite (op,
 					  src, mask, dst,
@@ -1596,10 +1747,7 @@ _cairo_surface_fill_rectangle (cairo_surface_t	   *surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
-
-    if (surface->finished)
-	return _cairo_surface_set_error (surface,CAIRO_STATUS_SURFACE_FINISHED);
+    assert (_cairo_surface_is_writable (surface));
 
     rect.x = x;
     rect.y = y;
@@ -1637,7 +1785,7 @@ _cairo_surface_fill_region (cairo_surface_t	   *surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
+    assert (_cairo_surface_is_writable (surface));
 
     num_rects = cairo_region_num_rectangles (region);
     if (num_rects == 0)
@@ -1692,10 +1840,7 @@ _cairo_surface_fill_rectangles (cairo_surface_t		*surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
-
-    if (surface->finished)
-	return _cairo_surface_set_error (surface,CAIRO_STATUS_SURFACE_FINISHED);
+    assert (_cairo_surface_is_writable (surface));
 
     if (num_rects == 0)
 	return CAIRO_STATUS_SUCCESS;
@@ -1724,7 +1869,7 @@ _cairo_surface_paint (cairo_surface_t	*surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
+    _cairo_surface_begin_modification (surface);
 
     status = _cairo_surface_copy_pattern_for_destination (&source,
 							  surface,
@@ -1761,7 +1906,7 @@ _cairo_surface_mask (cairo_surface_t		*surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
+    _cairo_surface_begin_modification (surface);
 
     status = _cairo_surface_copy_pattern_for_destination (&source,
 							  surface,
@@ -1816,6 +1961,8 @@ _cairo_surface_fill_stroke (cairo_surface_t	    *surface,
     if (surface->status)
 	return surface->status;
 
+    _cairo_surface_begin_modification (surface);
+
     if (surface->backend->fill_stroke) {
 	cairo_pattern_union_t dev_stroke_source;
 	cairo_pattern_union_t dev_fill_source;
@@ -1894,7 +2041,7 @@ _cairo_surface_stroke (cairo_surface_t		*surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
+    _cairo_surface_begin_modification (surface);
 
     status = _cairo_surface_copy_pattern_for_destination (&source,
 							  surface,
@@ -1943,7 +2090,7 @@ _cairo_surface_fill (cairo_surface_t	*surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
+    _cairo_surface_begin_modification (surface);
 
     status = _cairo_surface_copy_pattern_for_destination (&source,
 							  surface,
@@ -1987,18 +2134,15 @@ _cairo_surface_composite_trapezoids (cairo_operator_t		op,
 {
     cairo_int_status_t status;
 
-    /* These operators aren't interpreted the same way by the backends;
-     * they are implemented in terms of other operators in cairo-gstate.c
-     */
-    assert (op != CAIRO_OPERATOR_SOURCE && op != CAIRO_OPERATOR_CLEAR);
-
     if (dst->status)
 	return dst->status;
 
-    assert (! dst->is_snapshot);
+    assert (_cairo_surface_is_writable (dst));
 
-    if (dst->finished)
-	return _cairo_surface_set_error (dst, CAIRO_STATUS_SURFACE_FINISHED);
+    /* These operators aren't interpreted the same way by the backends;
+     * they are implemented in terms of other operators in cairo-gstate.c
+     */
+    assert (op != CAIRO_OPERATOR_SOURCE && op != CAIRO_OPERATOR_CLEAR);
 
     if (dst->backend->composite_trapezoids) {
 	status = dst->backend->composite_trapezoids (op,
@@ -2028,7 +2172,7 @@ _cairo_surface_create_span_renderer (cairo_operator_t		op,
 				     cairo_antialias_t	        antialias,
 				     const cairo_composite_rectangles_t *rects)
 {
-    assert (! dst->is_snapshot);
+    assert (dst->snapshot_of == NULL);
 
     if (dst->status)
 	return _cairo_span_renderer_create_in_error (dst->status);
@@ -2055,7 +2199,7 @@ _cairo_surface_check_span_renderer   (cairo_operator_t		op,
 {
     cairo_int_status_t status;
 
-    assert (! dst->is_snapshot);
+    assert (dst->snapshot_of == NULL);
 
     if (dst->status)
 	return FALSE;
@@ -2096,7 +2240,7 @@ cairo_surface_copy_page (cairo_surface_t *surface)
     if (surface->status)
 	return;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (surface->finished) {
 	status_ignored = _cairo_surface_set_error (surface,
@@ -2133,7 +2277,7 @@ cairo_surface_show_page (cairo_surface_t *surface)
     if (surface->status)
 	return;
 
-    assert (! surface->is_snapshot);
+    assert (surface->snapshot_of == NULL);
 
     if (surface->finished) {
 	status_ignored = _cairo_surface_set_error (surface,
@@ -2251,6 +2395,9 @@ _cairo_surface_set_clip_region (cairo_surface_t	    *surface,
     if (surface->status)
 	return surface->status;
 
+    if (surface->finished)
+	return _cairo_surface_set_error (surface,CAIRO_STATUS_SURFACE_FINISHED);
+
     assert (surface->backend->set_clip_region != NULL);
 
     status = surface->backend->set_clip_region (surface, region);
@@ -2571,11 +2718,11 @@ _cairo_surface_show_text_glyphs (cairo_surface_t	    *surface,
     if (surface->status)
 	return surface->status;
 
-    assert (! surface->is_snapshot);
-
-    if (!num_glyphs && !utf8_len)
+    if (num_glyphs == 0 && utf8_len == 0)
 	return CAIRO_STATUS_SUCCESS;
 
+    _cairo_surface_begin_modification (surface);
+
     status = _cairo_surface_copy_pattern_for_destination (&source,
 						          surface,
 							  &dev_source.base);
@@ -2702,10 +2849,7 @@ _cairo_surface_old_show_glyphs (cairo_scaled_font_t	*scaled_font,
     if (dst->status)
 	return dst->status;
 
-    assert (! dst->is_snapshot);
-
-    if (dst->finished)
-	return _cairo_surface_set_error (dst, CAIRO_STATUS_SURFACE_FINISHED);
+    assert (_cairo_surface_is_writable (dst));
 
     if (dst->backend->old_show_glyphs) {
 	status = dst->backend->old_show_glyphs (scaled_font,
@@ -2820,7 +2964,7 @@ _cairo_surface_composite_fixup_unbounded (cairo_surface_t            *dst,
     if (dst->status)
 	return dst->status;
 
-    assert (! dst->is_snapshot);
+    assert (_cairo_surface_is_writable (dst));
 
     /* The RENDER/libpixman operators are clipped to the bounds of the untransformed,
      * non-repeating sources and masks. Other sources and masks can be ignored.
@@ -2898,7 +3042,7 @@ _cairo_surface_composite_shape_fixup_unbounded (cairo_surface_t            *dst,
     if (dst->status)
 	return dst->status;
 
-    assert (! dst->is_snapshot);
+    assert (_cairo_surface_is_writable (dst));
 
     /* The RENDER/libpixman operators are clipped to the bounds of the untransformed,
      * non-repeating sources and masks. Other sources and masks can be ignored.
diff --git a/src/cairoint.h b/src/cairoint.h
index fe2a1af..581bc18 100644
--- a/src/cairoint.h
+++ b/src/cairoint.h
@@ -1985,6 +1985,18 @@ _cairo_surface_clone_similar (cairo_surface_t  *surface,
 cairo_private cairo_surface_t *
 _cairo_surface_snapshot (cairo_surface_t *surface);
 
+cairo_private cairo_status_t
+_cairo_surface_attach_snapshot (cairo_surface_t *surface,
+				cairo_surface_t *snapshot,
+				cairo_surface_func_t detach_func);
+
+cairo_private cairo_surface_t *
+_cairo_surface_has_snapshot (cairo_surface_t *surface,
+			     const cairo_surface_backend_t *backend);
+
+cairo_private void
+_cairo_surface_detach_snapshot (cairo_surface_t *snapshot);
+
 cairo_private cairo_bool_t
 _cairo_surface_is_similar (cairo_surface_t *surface_a,
 	                   cairo_surface_t *surface_b,
diff --git a/test/mask.c b/test/mask.c
index bc1adfd..451054e 100644
--- a/test/mask.c
+++ b/test/mask.c
@@ -33,6 +33,7 @@
 #define PAD 2
 
 static const char *png_filename = "romedalen.png";
+static cairo_surface_t *image;
 
 static void
 set_solid_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
@@ -64,7 +65,13 @@ set_image_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
 {
     cairo_pattern_t *pattern;
 
-    pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
+    if (image == NULL || cairo_surface_status (image)) {
+	cairo_surface_destroy (image);
+	image = cairo_test_create_surface_from_png (ctx, png_filename);
+    }
+
+    pattern = cairo_pattern_create_for_surface (image);
+    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
     cairo_set_source (cr, pattern);
     cairo_pattern_destroy (pattern);
 }
@@ -225,6 +232,9 @@ draw (cairo_t *cr, int width, int height)
 
     cairo_destroy (cr2);
 
+    cairo_surface_destroy (image);
+    image = NULL;
+
     return CAIRO_TEST_SUCCESS;
 }
 
diff --git a/test/trap-clip.c b/test/trap-clip.c
index 3a8d5c0..0999d97 100644
--- a/test/trap-clip.c
+++ b/test/trap-clip.c
@@ -32,6 +32,7 @@
 #define PAD 2
 
 static const char *png_filename = "romedalen.png";
+static cairo_surface_t *image;
 
 static void
 set_solid_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
@@ -63,7 +64,13 @@ set_image_pattern (const cairo_test_context_t *ctx, cairo_t *cr, int x, int y)
 {
     cairo_pattern_t *pattern;
 
-    pattern = cairo_test_create_pattern_from_png (ctx, png_filename);
+    if (image == NULL || cairo_surface_status (image)) {
+	cairo_surface_destroy (image);
+	image = cairo_test_create_surface_from_png (ctx, png_filename);
+    }
+
+    pattern = cairo_pattern_create_for_surface (image);
+    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
     cairo_set_source (cr, pattern);
     cairo_pattern_destroy (pattern);
 }
@@ -193,6 +200,9 @@ draw (cairo_t *cr, int width, int height)
     if (cairo_status (cr) != CAIRO_STATUS_SUCCESS)
 	cairo_test_log (ctx, "%d %d .HERE!\n", (int)i, (int)j);
 
+    cairo_surface_destroy (image);
+    image = NULL;
+
     return CAIRO_TEST_SUCCESS;
 }
 


More information about the cairo-commit mailing list