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
, unsigned flags
)
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 status
= _cairo_surface_flush (target
, flags
);
75 cairo_surface_destroy (target
);
80 static cairo_surface_t
*
81 _cairo_surface_snapshot_source (void *abstract_surface
,
82 cairo_rectangle_int_t
*extents
)
84 cairo_surface_snapshot_t
*surface
= abstract_surface
;
85 return _cairo_surface_get_source (surface
->target
, extents
); /* XXX racy */
88 struct snapshot_extra
{
89 cairo_surface_t
*target
;
94 _cairo_surface_snapshot_acquire_source_image (void *abstract_surface
,
95 cairo_image_surface_t
**image_out
,
98 cairo_surface_snapshot_t
*surface
= abstract_surface
;
99 struct snapshot_extra
*extra
;
100 cairo_status_t status
;
102 extra
= malloc (sizeof (*extra
));
103 if (unlikely (extra
== NULL
))
104 return _cairo_error (CAIRO_STATUS_NO_MEMORY
);
106 extra
->target
= _cairo_surface_snapshot_get_target (&surface
->base
);
107 status
= _cairo_surface_acquire_source_image (extra
->target
, image_out
, &extra
->extra
);
108 if (unlikely (status
)) {
109 cairo_surface_destroy (extra
->target
);
118 _cairo_surface_snapshot_release_source_image (void *abstract_surface
,
119 cairo_image_surface_t
*image
,
122 struct snapshot_extra
*extra
= _extra
;
124 _cairo_surface_release_source_image (extra
->target
, image
, extra
->extra
);
125 cairo_surface_destroy (extra
->target
);
130 _cairo_surface_snapshot_get_extents (void *abstract_surface
,
131 cairo_rectangle_int_t
*extents
)
133 cairo_surface_snapshot_t
*surface
= abstract_surface
;
134 cairo_surface_t
*target
;
135 cairo_bool_t bounded
;
137 target
= _cairo_surface_snapshot_get_target (&surface
->base
);
138 bounded
= _cairo_surface_get_extents (target
, extents
);
139 cairo_surface_destroy (target
);
144 static const cairo_surface_backend_t _cairo_surface_snapshot_backend
= {
145 CAIRO_INTERNAL_SURFACE_TYPE_SNAPSHOT
,
146 _cairo_surface_snapshot_finish
,
149 NULL
, /* create similar */
150 NULL
, /* create similar image */
151 NULL
, /* map to image */
152 NULL
, /* unmap image */
154 _cairo_surface_snapshot_source
,
155 _cairo_surface_snapshot_acquire_source_image
,
156 _cairo_surface_snapshot_release_source_image
,
159 NULL
, /* copy_page */
160 NULL
, /* show_page */
162 _cairo_surface_snapshot_get_extents
,
163 NULL
, /* get-font-options */
165 _cairo_surface_snapshot_flush
,
169 _cairo_surface_snapshot_copy_on_write (cairo_surface_t
*surface
)
171 cairo_surface_snapshot_t
*snapshot
= (cairo_surface_snapshot_t
*) surface
;
172 cairo_image_surface_t
*image
;
173 cairo_surface_t
*clone
;
175 cairo_status_t status
;
177 TRACE ((stderr
, "%s: target=%d\n",
178 __FUNCTION__
, snapshot
->target
->unique_id
));
180 /* We need to make an image copy of the original surface since the
181 * snapshot may exceed the lifetime of the original device, i.e.
182 * when we later need to use the snapshot the data may have already
186 CAIRO_MUTEX_LOCK (snapshot
->mutex
);
188 if (snapshot
->target
->backend
->snapshot
!= NULL
) {
189 clone
= snapshot
->target
->backend
->snapshot (snapshot
->target
);
191 assert (clone
->status
|| ! _cairo_surface_is_snapshot (clone
));
196 /* XXX copy to a similar surface, leave acquisition till later?
197 * We should probably leave such decisions to the backend in case we
198 * rely upon devices/connections like Xlib.
200 status
= _cairo_surface_acquire_source_image (snapshot
->target
, &image
, &extra
);
201 if (unlikely (status
)) {
202 snapshot
->target
= _cairo_surface_create_in_error (status
);
203 status
= _cairo_surface_set_error (surface
, status
);
206 clone
= image
->base
.backend
->snapshot (&image
->base
);
207 _cairo_surface_release_source_image (snapshot
->target
, image
, extra
);
210 status
= _cairo_surface_set_error (surface
, clone
->status
);
211 snapshot
->target
= snapshot
->clone
= clone
;
212 snapshot
->base
.type
= clone
->type
;
214 CAIRO_MUTEX_UNLOCK (snapshot
->mutex
);
218 * _cairo_surface_snapshot:
219 * @surface: a #cairo_surface_t
221 * Make an immutable reference to @surface. It is an error to call a
222 * surface-modifying function on the result of this function. The
223 * resulting 'snapshot' is a lazily copied-on-write surface i.e. it
224 * remains a reference to the original surface until that surface is
225 * written to again, at which time a copy is made of the original surface
226 * and the snapshot then points to that instead. Multiple snapshots of the
227 * same unmodified surface point to the same copy.
229 * The caller owns the return value and should call
230 * cairo_surface_destroy() when finished with it. This function will not
231 * return %NULL, but will return a nil surface instead.
233 * Return value: The snapshot surface. Note that the return surface
234 * may not necessarily be of the same type as @surface.
237 _cairo_surface_snapshot (cairo_surface_t
*surface
)
239 cairo_surface_snapshot_t
*snapshot
;
240 cairo_status_t status
;
242 TRACE ((stderr
, "%s: target=%d\n", __FUNCTION__
, surface
->unique_id
));
244 if (unlikely (surface
->status
))
245 return _cairo_surface_create_in_error (surface
->status
);
247 if (unlikely (surface
->finished
))
248 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED
));
250 if (surface
->snapshot_of
!= NULL
)
251 return cairo_surface_reference (surface
);
253 if (_cairo_surface_is_snapshot (surface
))
254 return cairo_surface_reference (surface
);
256 snapshot
= (cairo_surface_snapshot_t
*)
257 _cairo_surface_has_snapshot (surface
, &_cairo_surface_snapshot_backend
);
258 if (snapshot
!= NULL
)
259 return cairo_surface_reference (&snapshot
->base
);
261 snapshot
= malloc (sizeof (cairo_surface_snapshot_t
));
262 if (unlikely (snapshot
== NULL
))
263 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED
));
265 _cairo_surface_init (&snapshot
->base
,
266 &_cairo_surface_snapshot_backend
,
269 snapshot
->base
.type
= surface
->type
;
271 CAIRO_MUTEX_INIT (snapshot
->mutex
);
272 snapshot
->target
= surface
;
273 snapshot
->clone
= NULL
;
275 status
= _cairo_surface_copy_mime_data (&snapshot
->base
, surface
);
276 if (unlikely (status
)) {
277 cairo_surface_destroy (&snapshot
->base
);
278 return _cairo_surface_create_in_error (status
);
281 snapshot
->base
.device_transform
= surface
->device_transform
;
282 snapshot
->base
.device_transform_inverse
= surface
->device_transform_inverse
;
284 _cairo_surface_attach_snapshot (surface
,
286 _cairo_surface_snapshot_copy_on_write
);
288 return &snapshot
->base
;