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_SIZE (_cairo_surface_create_in_error(_cairo_error(CAIRO_STATUS_INVALID_SIZE)))
45 #define SURFACE_ERROR_INVALID_FORMAT (_cairo_surface_create_in_error(_cairo_error(CAIRO_STATUS_INVALID_FORMAT)))
48 DataProviderReleaseCallback (void *info
, const void *data
, size_t size
)
50 cairo_surface_t
*surface
= (cairo_surface_t
*) info
;
51 cairo_surface_destroy (surface
);
54 static cairo_surface_t
*
55 _cairo_quartz_image_surface_create_similar (void *asurface
,
56 cairo_content_t content
,
60 cairo_surface_t
*result
;
61 cairo_surface_t
*isurf
= cairo_image_surface_create (_cairo_format_from_content (content
),
64 if (cairo_surface_status(isurf
))
67 result
= cairo_quartz_image_surface_create (isurf
);
68 cairo_surface_destroy (isurf
);
74 _cairo_quartz_image_surface_finish (void *asurface
)
76 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
78 /* the imageSurface will be destroyed by the data provider's release callback */
79 CGImageRelease (surface
->image
);
81 return CAIRO_STATUS_SUCCESS
;
85 _cairo_quartz_image_surface_acquire_source_image (void *asurface
,
86 cairo_image_surface_t
**image_out
,
89 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
91 *image_out
= surface
->imageSurface
;
94 return CAIRO_STATUS_SUCCESS
;
98 _cairo_quartz_image_surface_acquire_dest_image (void *asurface
,
99 cairo_rectangle_int_t
*interest_rect
,
100 cairo_image_surface_t
**image_out
,
101 cairo_rectangle_int_t
*image_rect
,
104 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
106 *image_out
= surface
->imageSurface
;
107 *image_rect
= surface
->extents
;
110 return CAIRO_STATUS_SUCCESS
;
114 static cairo_int_status_t
115 _cairo_quartz_image_surface_get_extents (void *asurface
,
116 cairo_rectangle_int_t
*extents
)
118 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
120 *extents
= surface
->extents
;
122 return CAIRO_STATUS_SUCCESS
;
125 /* we assume some drawing happened to the image buffer; make sure it's
126 * represented in the CGImage on flush()
129 static cairo_status_t
130 _cairo_quartz_image_surface_flush (void *asurface
)
132 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
133 CGImageRef oldImage
= surface
->image
;
134 CGImageRef newImage
= NULL
;
136 /* To be released by the ReleaseCallback */
137 cairo_surface_reference ((cairo_surface_t
*) surface
->imageSurface
);
139 newImage
= _cairo_quartz_create_cgimage (surface
->imageSurface
->format
,
140 surface
->imageSurface
->width
,
141 surface
->imageSurface
->height
,
142 surface
->imageSurface
->stride
,
143 surface
->imageSurface
->data
,
146 DataProviderReleaseCallback
,
147 surface
->imageSurface
);
149 surface
->image
= newImage
;
150 CGImageRelease (oldImage
);
152 return CAIRO_STATUS_SUCCESS
;
155 static const cairo_surface_backend_t cairo_quartz_image_surface_backend
= {
156 CAIRO_SURFACE_TYPE_QUARTZ_IMAGE
,
157 _cairo_quartz_image_surface_create_similar
,
158 _cairo_quartz_image_surface_finish
,
159 _cairo_quartz_image_surface_acquire_source_image
,
160 NULL
, /* release_source_image */
161 _cairo_quartz_image_surface_acquire_dest_image
,
162 NULL
, /* release_dest_image */
163 NULL
, /* clone_similar */
164 NULL
, /* composite */
165 NULL
, /* fill_rectangles */
166 NULL
, /* composite_trapezoids */
167 NULL
, /* create_span_renderer */
168 NULL
, /* check_span_renderer */
169 NULL
, /* copy_page */
170 NULL
, /* show_page */
171 NULL
, /* set_clip_region */
172 NULL
, /* intersect_clip_path */
173 _cairo_quartz_image_surface_get_extents
,
174 NULL
, /* old_show_glyphs */
175 NULL
, /* get_font_options */
176 _cairo_quartz_image_surface_flush
,
177 NULL
, /* mark_dirty_rectangle */
178 NULL
, /* scaled_font_fini */
179 NULL
, /* scaled_glyph_fini */
185 NULL
, /* surface_show_glyphs */
187 NULL
, /* is_similar */
189 NULL
/* fill_stroke */
194 * cairo_quartz_image_surface_create
195 * @surface: a cairo image surface to wrap with a quartz image surface
197 * Creates a Quartz surface backed by a CGImageRef that references the
198 * given image surface. The resulting surface can be rendered quickly
199 * when used as a source when rendering to a #cairo_quartz_surface. If
200 * the data in the image surface is ever updated, cairo_surface_flush()
201 * must be called on the #cairo_quartz_image_surface to ensure that the
202 * CGImageRef refers to the updated data.
204 * Return value: the newly created surface.
209 cairo_quartz_image_surface_create (cairo_surface_t
*surface
)
211 cairo_quartz_image_surface_t
*qisurf
;
215 cairo_image_surface_t
*image_surface
;
216 int width
, height
, stride
;
217 cairo_format_t format
;
220 if (cairo_surface_get_type(surface
) != CAIRO_SURFACE_TYPE_IMAGE
)
221 return SURFACE_ERROR_TYPE_MISMATCH
;
223 image_surface
= (cairo_image_surface_t
*) surface
;
224 width
= image_surface
->width
;
225 height
= image_surface
->height
;
226 stride
= image_surface
->stride
;
227 format
= image_surface
->format
;
228 data
= image_surface
->data
;
230 if (!_cairo_quartz_verify_surface_size(width
, height
))
231 return SURFACE_ERROR_INVALID_SIZE
;
233 if (width
== 0 || height
== 0)
234 return SURFACE_ERROR_INVALID_SIZE
;
236 if (format
!= CAIRO_FORMAT_ARGB32
&& format
!= CAIRO_FORMAT_RGB24
)
237 return SURFACE_ERROR_INVALID_FORMAT
;
239 qisurf
= malloc(sizeof(cairo_quartz_image_surface_t
));
241 return SURFACE_ERROR_NO_MEMORY
;
243 memset (qisurf
, 0, sizeof(cairo_quartz_image_surface_t
));
245 /* In case the create_cgimage fails, this ref will
246 * be released via the callback (which will be called in
249 cairo_surface_reference (surface
);
251 image
= _cairo_quartz_create_cgimage (format
,
257 DataProviderReleaseCallback
,
262 return SURFACE_ERROR_NO_MEMORY
;
265 _cairo_surface_init (&qisurf
->base
,
266 &cairo_quartz_image_surface_backend
,
267 _cairo_content_from_format (format
));
269 qisurf
->extents
.x
= qisurf
->extents
.y
= 0;
270 qisurf
->extents
.width
= width
;
271 qisurf
->extents
.height
= height
;
273 qisurf
->image
= image
;
274 qisurf
->imageSurface
= image_surface
;
276 return &qisurf
->base
;
281 cairo_quartz_image_surface_get_image (cairo_surface_t
*asurface
)
283 cairo_quartz_image_surface_t
*surface
= (cairo_quartz_image_surface_t
*) asurface
;
285 if (cairo_surface_get_type(asurface
) != CAIRO_SURFACE_TYPE_QUARTZ_IMAGE
)
288 return (cairo_surface_t
*) surface
->imageSurface
;