1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2002 University of Southern California
4 * Copyright © 2005 Red Hat, Inc.
5 * Copyright © 2009 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-error-private.h"
43 #include "cairo-image-surface-private.h"
44 #include "cairo-surface-snapshot-inline.h"
47 _cairo_surface_snapshot_finish (void *abstract_surface)
49 cairo_surface_snapshot_t *surface = abstract_surface;
50 cairo_status_t status = CAIRO_STATUS_SUCCESS;
52 TRACE ((stderr, "%s\n", __FUNCTION__));
54 if (surface->clone != NULL) {
55 cairo_surface_finish (surface->clone);
56 status = surface->clone->status;
58 cairo_surface_destroy (surface->clone);
61 CAIRO_MUTEX_FINI (surface->mutex);
67 _cairo_surface_snapshot_flush (void *abstract_surface)
69 cairo_surface_snapshot_t *surface = abstract_surface;
70 cairo_surface_t *target;
71 cairo_status_t status;
73 target = _cairo_surface_snapshot_get_target (&surface->base);
74 cairo_surface_flush (target);
75 status = target->status;
76 cairo_surface_destroy (target);
81 static cairo_surface_t *
82 _cairo_surface_snapshot_source (void *abstract_surface,
83 cairo_rectangle_int_t *extents)
85 cairo_surface_snapshot_t *surface = abstract_surface;
86 return _cairo_surface_get_source (surface->target, extents); /* XXX racy */
89 struct snapshot_extra {
90 cairo_surface_t *target;
95 _cairo_surface_snapshot_acquire_source_image (void *abstract_surface,
96 cairo_image_surface_t **image_out,
99 cairo_surface_snapshot_t *surface = abstract_surface;
100 struct snapshot_extra *extra;
101 cairo_status_t status;
103 extra = malloc (sizeof (*extra));
104 if (unlikely (extra == NULL))
105 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
107 extra->target = _cairo_surface_snapshot_get_target (&surface->base);
108 status = _cairo_surface_acquire_source_image (extra->target, image_out, &extra->extra);
109 if (unlikely (status)) {
110 cairo_surface_destroy (extra->target);
119 _cairo_surface_snapshot_release_source_image (void *abstract_surface,
120 cairo_image_surface_t *image,
123 struct snapshot_extra *extra = _extra;
125 _cairo_surface_release_source_image (extra->target, image, extra->extra);
126 cairo_surface_destroy (extra->target);
131 _cairo_surface_snapshot_get_extents (void *abstract_surface,
132 cairo_rectangle_int_t *extents)
134 cairo_surface_snapshot_t *surface = abstract_surface;
135 cairo_surface_t *target;
136 cairo_bool_t bounded;
138 target = _cairo_surface_snapshot_get_target (&surface->base);
139 bounded = _cairo_surface_get_extents (target, extents);
140 cairo_surface_destroy (target);
145 static const cairo_surface_backend_t _cairo_surface_snapshot_backend = {
146 CAIRO_INTERNAL_SURFACE_TYPE_SNAPSHOT,
147 _cairo_surface_snapshot_finish,
150 NULL, /* create similar */
151 NULL, /* create similar image */
152 NULL, /* map to image */
153 NULL, /* unmap image */
155 _cairo_surface_snapshot_source,
156 _cairo_surface_snapshot_acquire_source_image,
157 _cairo_surface_snapshot_release_source_image,
160 NULL, /* copy_page */
161 NULL, /* show_page */
163 _cairo_surface_snapshot_get_extents,
164 NULL, /* get-font-options */
166 _cairo_surface_snapshot_flush,
170 _cairo_surface_snapshot_copy_on_write (cairo_surface_t *surface)
172 cairo_surface_snapshot_t *snapshot = (cairo_surface_snapshot_t *) surface;
173 cairo_image_surface_t *image;
174 cairo_surface_t *clone;
176 cairo_status_t status;
178 TRACE ((stderr, "%s: target=%d\n",
179 __FUNCTION__, snapshot->target->unique_id));
181 /* We need to make an image copy of the original surface since the
182 * snapshot may exceed the lifetime of the original device, i.e.
183 * when we later need to use the snapshot the data may have already
187 CAIRO_MUTEX_LOCK (snapshot->mutex);
189 if (snapshot->target->backend->snapshot != NULL) {
190 clone = snapshot->target->backend->snapshot (snapshot->target);
192 assert (clone->status || ! _cairo_surface_is_snapshot (clone));
197 /* XXX copy to a similar surface, leave acquisition till later?
198 * We should probably leave such decisions to the backend in case we
199 * rely upon devices/connections like Xlib.
201 status = _cairo_surface_acquire_source_image (snapshot->target, &image, &extra);
202 if (unlikely (status)) {
203 snapshot->target = _cairo_surface_create_in_error (status);
204 status = _cairo_surface_set_error (surface, status);
207 clone = image->base.backend->snapshot (&image->base);
208 _cairo_surface_release_source_image (snapshot->target, image, extra);
211 status = _cairo_surface_set_error (surface, clone->status);
212 snapshot->target = snapshot->clone = clone;
213 snapshot->base.type = clone->type;
215 CAIRO_MUTEX_UNLOCK (snapshot->mutex);
219 * _cairo_surface_snapshot:
220 * @surface: a #cairo_surface_t
222 * Make an immutable reference to @surface. It is an error to call a
223 * surface-modifying function on the result of this function. The
224 * resulting 'snapshot' is a lazily copied-on-write surface i.e. it
225 * remains a reference to the original surface until that surface is
226 * written to again, at which time a copy is made of the original surface
227 * and the snapshot then points to that instead. Multiple snapshots of the
228 * same unmodified surface point to the same copy.
230 * The caller owns the return value and should call
231 * cairo_surface_destroy() when finished with it. This function will not
232 * return %NULL, but will return a nil surface instead.
234 * Return value: The snapshot surface. Note that the return surface
235 * may not necessarily be of the same type as @surface.
238 _cairo_surface_snapshot (cairo_surface_t *surface)
240 cairo_surface_snapshot_t *snapshot;
241 cairo_status_t status;
243 TRACE ((stderr, "%s: target=%d\n", __FUNCTION__, surface->unique_id));
245 if (unlikely (surface->status))
246 return _cairo_surface_create_in_error (surface->status);
248 if (unlikely (surface->finished))
249 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
251 if (surface->snapshot_of != NULL)
252 return cairo_surface_reference (surface);
254 if (_cairo_surface_is_snapshot (surface))
255 return cairo_surface_reference (surface);
257 snapshot = (cairo_surface_snapshot_t *)
258 _cairo_surface_has_snapshot (surface, &_cairo_surface_snapshot_backend);
259 if (snapshot != NULL)
260 return cairo_surface_reference (&snapshot->base);
262 snapshot = malloc (sizeof (cairo_surface_snapshot_t));
263 if (unlikely (snapshot == NULL))
264 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
266 _cairo_surface_init (&snapshot->base,
267 &_cairo_surface_snapshot_backend,
270 snapshot->base.type = surface->type;
272 CAIRO_MUTEX_INIT (snapshot->mutex);
273 snapshot->target = surface;
274 snapshot->clone = NULL;
276 status = _cairo_surface_copy_mime_data (&snapshot->base, surface);
277 if (unlikely (status)) {
278 cairo_surface_destroy (&snapshot->base);
279 return _cairo_surface_create_in_error (status);
282 snapshot->base.device_transform = surface->device_transform;
283 snapshot->base.device_transform_inverse = surface->device_transform_inverse;
285 _cairo_surface_attach_snapshot (surface,
287 _cairo_surface_snapshot_copy_on_write);
289 return &snapshot->base;