9471e474741e75990fcf9de5bb77835b219556c9
[framework/graphics/cairo.git] / src / cairo-surface-snapshot.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2002 University of Southern California
4  * Copyright © 2005 Red Hat, Inc.
5  * Copyright © 2009 Intel Corporation
6  *
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.
14  *
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
20  *
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/
25  *
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.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is University of Southern
33  * California.
34  *
35  * Contributor(s):
36  *      Carl D. Worth <cworth@cworth.org>
37  *      Chris Wilson <chris@chris-wilson.co.uk>
38  */
39
40 #include "cairoint.h"
41
42 #include "cairo-error-private.h"
43 #include "cairo-image-surface-private.h"
44 #include "cairo-surface-snapshot-inline.h"
45
46 static cairo_status_t
47 _cairo_surface_snapshot_finish (void *abstract_surface)
48 {
49     cairo_surface_snapshot_t *surface = abstract_surface;
50     cairo_status_t status = CAIRO_STATUS_SUCCESS;
51
52     TRACE ((stderr, "%s\n", __FUNCTION__));
53
54     if (surface->clone != NULL) {
55         cairo_surface_finish (surface->clone);
56         status = surface->clone->status;
57
58         cairo_surface_destroy (surface->clone);
59     }
60
61     CAIRO_MUTEX_FINI (surface->mutex);
62
63     return status;
64 }
65
66 static cairo_status_t
67 _cairo_surface_snapshot_flush (void *abstract_surface)
68 {
69     cairo_surface_snapshot_t *surface = abstract_surface;
70     cairo_surface_t *target;
71     cairo_status_t status;
72
73     target = _cairo_surface_snapshot_get_target (&surface->base);
74     cairo_surface_flush (target);
75     status = target->status;
76     cairo_surface_destroy (target);
77
78     return status;
79 }
80
81 static cairo_surface_t *
82 _cairo_surface_snapshot_source (void                    *abstract_surface,
83                                 cairo_rectangle_int_t *extents)
84 {
85     cairo_surface_snapshot_t *surface = abstract_surface;
86     return _cairo_surface_get_source (surface->target, extents); /* XXX racy */
87 }
88
89 struct snapshot_extra {
90     cairo_surface_t *target;
91     void *extra;
92 };
93
94 static cairo_status_t
95 _cairo_surface_snapshot_acquire_source_image (void                    *abstract_surface,
96                                               cairo_image_surface_t  **image_out,
97                                               void                   **extra_out)
98 {
99     cairo_surface_snapshot_t *surface = abstract_surface;
100     struct snapshot_extra *extra;
101     cairo_status_t status;
102
103     extra = malloc (sizeof (*extra));
104     if (unlikely (extra == NULL))
105         return _cairo_error (CAIRO_STATUS_NO_MEMORY);
106
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);
111         free (extra);
112     }
113
114     *extra_out = extra;
115     return status;
116 }
117
118 static void
119 _cairo_surface_snapshot_release_source_image (void                   *abstract_surface,
120                                               cairo_image_surface_t  *image,
121                                               void                   *_extra)
122 {
123     struct snapshot_extra *extra = _extra;
124
125     _cairo_surface_release_source_image (extra->target, image, extra->extra);
126     cairo_surface_destroy (extra->target);
127     free (extra);
128 }
129
130 static cairo_bool_t
131 _cairo_surface_snapshot_get_extents (void                  *abstract_surface,
132                                      cairo_rectangle_int_t *extents)
133 {
134     cairo_surface_snapshot_t *surface = abstract_surface;
135     cairo_surface_t *target;
136     cairo_bool_t bounded;
137
138     target = _cairo_surface_snapshot_get_target (&surface->base);
139     bounded = _cairo_surface_get_extents (target, extents);
140     cairo_surface_destroy (target);
141
142     return bounded;
143 }
144
145 static const cairo_surface_backend_t _cairo_surface_snapshot_backend = {
146     CAIRO_INTERNAL_SURFACE_TYPE_SNAPSHOT,
147     _cairo_surface_snapshot_finish,
148     NULL,
149
150     NULL, /* create similar */
151     NULL, /* create similar image  */
152     NULL, /* map to image */
153     NULL, /* unmap image  */
154
155     _cairo_surface_snapshot_source,
156     _cairo_surface_snapshot_acquire_source_image,
157     _cairo_surface_snapshot_release_source_image,
158     NULL, /* snapshot */
159
160     NULL, /* copy_page */
161     NULL, /* show_page */
162
163     _cairo_surface_snapshot_get_extents,
164     NULL, /* get-font-options */
165
166     _cairo_surface_snapshot_flush,
167 };
168
169 static void
170 _cairo_surface_snapshot_copy_on_write (cairo_surface_t *surface)
171 {
172     cairo_surface_snapshot_t *snapshot = (cairo_surface_snapshot_t *) surface;
173     cairo_image_surface_t *image;
174     cairo_surface_t *clone;
175     void *extra;
176     cairo_status_t status;
177
178     TRACE ((stderr, "%s: target=%d\n",
179             __FUNCTION__, snapshot->target->unique_id));
180
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
184      * been lost.
185      */
186
187     CAIRO_MUTEX_LOCK (snapshot->mutex);
188
189     if (snapshot->target->backend->snapshot != NULL) {
190         clone = snapshot->target->backend->snapshot (snapshot->target);
191         if (clone != NULL) {
192             assert (clone->status || ! _cairo_surface_is_snapshot (clone));
193             goto done;
194         }
195     }
196
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.
200     */
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);
205         goto unlock;
206     }
207     clone = image->base.backend->snapshot (&image->base);
208     _cairo_surface_release_source_image (snapshot->target, image, extra);
209
210 done:
211     status = _cairo_surface_set_error (surface, clone->status);
212     snapshot->target = snapshot->clone = clone;
213     snapshot->base.type = clone->type;
214 unlock:
215     CAIRO_MUTEX_UNLOCK (snapshot->mutex);
216 }
217
218 /**
219  * _cairo_surface_snapshot:
220  * @surface: a #cairo_surface_t
221  *
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.
229  *
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.
233  *
234  * Return value: The snapshot surface. Note that the return surface
235  * may not necessarily be of the same type as @surface.
236  **/
237 cairo_surface_t *
238 _cairo_surface_snapshot (cairo_surface_t *surface)
239 {
240     cairo_surface_snapshot_t *snapshot;
241     cairo_status_t status;
242
243     TRACE ((stderr, "%s: target=%d\n", __FUNCTION__, surface->unique_id));
244
245     if (unlikely (surface->status))
246         return _cairo_surface_create_in_error (surface->status);
247
248     if (unlikely (surface->finished))
249         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_SURFACE_FINISHED));
250
251     if (surface->snapshot_of != NULL)
252         return cairo_surface_reference (surface);
253
254     if (_cairo_surface_is_snapshot (surface))
255         return cairo_surface_reference (surface);
256
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);
261
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));
265
266     _cairo_surface_init (&snapshot->base,
267                          &_cairo_surface_snapshot_backend,
268                          NULL, /* device */
269                          surface->content);
270     snapshot->base.type = surface->type;
271
272     CAIRO_MUTEX_INIT (snapshot->mutex);
273     snapshot->target = surface;
274     snapshot->clone = NULL;
275
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);
280     }
281
282     snapshot->base.device_transform = surface->device_transform;
283     snapshot->base.device_transform_inverse = surface->device_transform_inverse;
284
285     _cairo_surface_attach_snapshot (surface,
286                                     &snapshot->base,
287                                     _cairo_surface_snapshot_copy_on_write);
288
289     return &snapshot->base;
290 }