1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright � 2008 Mozilla Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
29 * The Original Code is the cairo graphics library.
31 * The Initial Developer of the Original Code is Mozilla Corporation.
34 * Vladimir Vukicevic <vladimir@mozilla.com>
39 #include "cairo-quartz-image.h"
40 #include "cairo-quartz-private.h"
42 #define SURFACE_ERROR_NO_MEMORY (_cairo_surface_create_in_error(_cairo_error(CAIRO_STATUS_NO_MEMORY)))
43 #define SURFACE_ERROR_TYPE_MISMATCH (_cairo_surface_create_in_error(_cairo_error(CAIRO_STATUS_SURFACE_TYPE_MISMATCH)))
44 #define SURFACE_ERROR_INVALID_FORMAT (_cairo_surface_create_in_error(_cairo_error(CAIRO_STATUS_INVALID_FORMAT)))
47 DataProviderReleaseCallback (void *info
, const void *data
, size_t size
)
49 cairo_surface_t
*surface
= (cairo_surface_t
*) info
;
50 cairo_surface_destroy (surface
);
53 static cairo_surface_t
*
54 _cairo_quartz_image_surface_create_similar (void *asurface
,
55 cairo_content_t content
,
59 cairo_surface_t
*result
;
60 cairo_surface_t
*isurf
= cairo_image_surface_create (_cairo_format_from_content (content
),
63 if (cairo_surface_status(isurf
))
66 result
= cairo_quartz_image_surface_create (isurf
);
67 cairo_surface_destroy (isurf
);
73 _cairo_quartz_image_surface_finish (void *asurface
)
75 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
77 /* the imageSurface will be destroyed by the data provider's release callback */
78 CGImageRelease (surface
->image
);
80 return CAIRO_STATUS_SUCCESS
;
84 _cairo_quartz_image_surface_acquire_source_image (void *asurface
,
85 cairo_image_surface_t
**image_out
,
88 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
90 *image_out
= surface
->imageSurface
;
93 return CAIRO_STATUS_SUCCESS
;
97 _cairo_quartz_image_surface_acquire_dest_image (void *asurface
,
98 cairo_rectangle_int_t
*interest_rect
,
99 cairo_image_surface_t
**image_out
,
100 cairo_rectangle_int_t
*image_rect
,
103 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
105 *image_out
= surface
->imageSurface
;
106 *image_rect
= surface
->extents
;
109 return CAIRO_STATUS_SUCCESS
;
113 static cairo_int_status_t
114 _cairo_quartz_image_surface_get_extents (void *asurface
,
115 cairo_rectangle_int_t
*extents
)
117 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
119 *extents
= surface
->extents
;
121 return CAIRO_STATUS_SUCCESS
;
124 /* we assume some drawing happened to the image buffer; make sure it's
125 * represented in the CGImage on flush()
128 static cairo_status_t
129 _cairo_quartz_image_surface_flush (void *asurface
)
131 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
132 CGImageRef oldImage
= surface
->image
;
133 CGImageRef newImage
= NULL
;
135 /* To be released by the ReleaseCallback */
136 cairo_surface_reference ((cairo_surface_t
*) surface
->imageSurface
);
138 newImage
= _cairo_quartz_create_cgimage (surface
->imageSurface
->format
,
139 surface
->imageSurface
->width
,
140 surface
->imageSurface
->height
,
141 surface
->imageSurface
->stride
,
142 surface
->imageSurface
->data
,
145 DataProviderReleaseCallback
,
146 surface
->imageSurface
);
148 surface
->image
= newImage
;
149 CGImageRelease (oldImage
);
151 return CAIRO_STATUS_SUCCESS
;
154 static const cairo_surface_backend_t cairo_quartz_image_surface_backend
= {
155 CAIRO_SURFACE_TYPE_QUARTZ_IMAGE
,
156 _cairo_quartz_image_surface_create_similar
,
157 _cairo_quartz_image_surface_finish
,
158 _cairo_quartz_image_surface_acquire_source_image
,
159 NULL
, /* release_source_image */
160 _cairo_quartz_image_surface_acquire_dest_image
,
161 NULL
, /* release_dest_image */
162 NULL
, /* clone_similar */
163 NULL
, /* composite */
164 NULL
, /* fill_rectangles */
165 NULL
, /* composite_trapezoids */
166 NULL
, /* copy_page */
167 NULL
, /* show_page */
168 NULL
, /* set_clip_region */
169 NULL
, /* intersect_clip_path */
170 _cairo_quartz_image_surface_get_extents
,
171 NULL
, /* old_show_glyphs */
172 NULL
, /* get_font_options */
173 _cairo_quartz_image_surface_flush
,
174 NULL
, /* mark_dirty_rectangle */
175 NULL
, /* scaled_font_fini */
176 NULL
, /* scaled_glyph_fini */
182 NULL
, /* surface_show_glyphs */
184 NULL
, /* is_similar */
186 NULL
/* fill_stroke */
191 * cairo_quartz_image_surface_create
192 * @surface: a cairo image surface to wrap with a quartz image surface
194 * Creates a Quartz surface backed by a CGImageRef that references the
195 * given image surface. The resulting surface can be rendered quickly
196 * when used as a source when rendering to a #cairo_quartz_surface. If
197 * the data in the image surface is ever updated, cairo_surface_flush()
198 * must be called on the #cairo_quartz_image_surface to ensure that the
199 * CGImageRef refers to the updated data.
201 * Return value: the newly created surface.
206 cairo_quartz_image_surface_create (cairo_surface_t
*surface
)
208 cairo_quartz_image_surface_t
*qisurf
;
212 cairo_image_surface_t
*image_surface
;
213 int width
, height
, stride
;
214 cairo_format_t format
;
217 if (cairo_surface_get_type(surface
) != CAIRO_SURFACE_TYPE_IMAGE
)
218 return SURFACE_ERROR_TYPE_MISMATCH
;
220 image_surface
= (cairo_image_surface_t
*) surface
;
221 width
= image_surface
->width
;
222 height
= image_surface
->height
;
223 stride
= image_surface
->stride
;
224 format
= image_surface
->format
;
225 data
= image_surface
->data
;
227 if (!_cairo_quartz_verify_surface_size(width
, height
))
228 return SURFACE_ERROR_NO_MEMORY
;
230 if (width
== 0 || height
== 0)
231 return SURFACE_ERROR_NO_MEMORY
;
233 if (format
!= CAIRO_FORMAT_ARGB32
&& format
!= CAIRO_FORMAT_RGB24
)
234 return SURFACE_ERROR_INVALID_FORMAT
;
236 qisurf
= malloc(sizeof(cairo_quartz_image_surface_t
));
238 return SURFACE_ERROR_NO_MEMORY
;
240 memset (qisurf
, 0, sizeof(cairo_quartz_image_surface_t
));
242 /* In case the create_cgimage fails, this ref will
243 * be released via the callback (which will be called in
246 cairo_surface_reference (surface
);
248 image
= _cairo_quartz_create_cgimage (format
,
254 DataProviderReleaseCallback
,
259 return SURFACE_ERROR_NO_MEMORY
;
262 _cairo_surface_init (&qisurf
->base
,
263 &cairo_quartz_image_surface_backend
,
264 _cairo_content_from_format (format
));
266 qisurf
->extents
.x
= qisurf
->extents
.y
= 0;
267 qisurf
->extents
.width
= width
;
268 qisurf
->extents
.height
= height
;
270 qisurf
->image
= image
;
271 qisurf
->imageSurface
= image_surface
;
273 return &qisurf
->base
;
278 cairo_quartz_image_surface_get_image (cairo_surface_t
*asurface
)
280 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
282 if (cairo_surface_get_type(asurface
) != CAIRO_SURFACE_TYPE_QUARTZ_IMAGE
)
285 return (cairo_surface_t
*) surface
->imageSurface
;