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 © 2007 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-skia.h"
40 #include "cairo-skia-private.h"
42 #include "cairo-composite-rectangles-private.h"
43 #include "cairo-error-private.h"
44 #include "cairo-surface-backend-private.h"
45 #include "cairo-surface-fallback-private.h"
47 static cairo_skia_surface_t *
48 _cairo_skia_surface_create_internal (SkBitmap::Config config,
55 static cairo_surface_t *
56 _cairo_skia_surface_create_similar (void *asurface,
57 cairo_content_t content,
61 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
62 SkBitmap::Config config;
65 if (content == surface->image.base.content)
67 config = surface->bitmap->getConfig ();
68 opaque = surface->bitmap->isOpaque ();
70 else if (! format_to_sk_config (_cairo_format_from_content (content),
76 return &_cairo_skia_surface_create_internal (config, opaque,
83 _cairo_skia_surface_finish (void *asurface)
85 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
87 cairo_surface_finish (&surface->image.base);
88 delete surface->bitmap;
90 return CAIRO_STATUS_SUCCESS;
93 static cairo_surface_t *
94 _cairo_skia_surface_map_to_image (void *asurface,
95 const cairo_rectangle_int_t *extents)
97 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
99 surface->bitmap->lockPixels ();
101 if (extents->width < surface->image.width ||
102 extents->height < surface->image.height)
104 return _cairo_surface_create_for_rectangle_int (&surface->image.base,
108 return cairo_surface_reference (&surface->image.base);
111 static cairo_int_status_t
112 _cairo_skia_surface_unmap_image (void *asurface,
113 cairo_image_surface_t *image)
115 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
117 surface->bitmap->unlockPixels ();
118 return CAIRO_INT_STATUS_SUCCESS;
121 static cairo_status_t
122 _cairo_skia_surface_acquire_source_image (void *asurface,
123 cairo_image_surface_t **image_out,
126 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
128 surface->bitmap->lockPixels ();
130 *image_out = &surface->image;
132 return CAIRO_STATUS_SUCCESS;
136 _cairo_skia_surface_release_source_image (void *asurface,
137 cairo_image_surface_t *image,
140 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
142 surface->bitmap->unlockPixels ();
146 _cairo_skia_surface_get_extents (void *asurface,
147 cairo_rectangle_int_t *extents)
149 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
150 extents->x = extents->y = 0;
151 extents->width = surface->image.width;
152 extents->height = surface->image.height;
157 _cairo_skia_surface_get_font_options (void *abstract_surface,
158 cairo_font_options_t *options)
160 _cairo_font_options_init_default (options);
162 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
163 _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
166 static const struct _cairo_surface_backend
167 cairo_skia_surface_backend = {
168 CAIRO_SURFACE_TYPE_SKIA,
169 _cairo_skia_surface_finish,
171 _cairo_skia_context_create,
173 _cairo_skia_surface_create_similar,
174 NULL, //_cairo_skia_surface_create_similar_image,
175 _cairo_skia_surface_map_to_image,
176 _cairo_skia_surface_unmap_image,
178 _cairo_surface_default_source,
179 _cairo_skia_surface_acquire_source_image,
180 _cairo_skia_surface_release_source_image,
183 NULL, /* copy_page */
184 NULL, /* show_page */
186 _cairo_skia_surface_get_extents,
187 _cairo_skia_surface_get_font_options,
190 NULL, /* mark_dirty_rectangle */
192 /* XXX native surface functions? */
193 _cairo_surface_fallback_paint,
194 _cairo_surface_fallback_mask,
195 _cairo_surface_fallback_stroke,
196 _cairo_surface_fallback_fill,
197 NULL, /* fill/stroke */
198 _cairo_surface_fallback_glyphs
202 * Surface constructors
205 static inline pixman_format_code_t
206 sk_config_to_pixman_format_code (SkBitmap::Config config,
210 case SkBitmap::kARGB_8888_Config:
211 return opaque ? PIXMAN_x8r8g8b8 : PIXMAN_a8r8g8b8;
213 case SkBitmap::kA8_Config:
216 case SkBitmap::kA1_Config:
218 case SkBitmap::kRGB_565_Config:
219 return PIXMAN_r5g6b5;
220 case SkBitmap::kARGB_4444_Config:
221 return PIXMAN_a4r4g4b4;
223 case SkBitmap::kNo_Config:
224 case SkBitmap::kIndex8_Config:
225 case SkBitmap::kRLE_Index8_Config:
226 case SkBitmap::kConfigCount:
229 return (pixman_format_code_t) -1;
233 static cairo_skia_surface_t *
234 _cairo_skia_surface_create_internal (SkBitmap::Config config,
241 cairo_skia_surface_t *surface;
242 pixman_image_t *pixman_image;
243 pixman_format_code_t pixman_format;
245 surface = (cairo_skia_surface_t *) malloc (sizeof (cairo_skia_surface_t));
246 if (unlikely (surface == NULL))
247 return (cairo_skia_surface_t *) _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
249 pixman_format = sk_config_to_pixman_format_code (config, opaque);
250 pixman_image = pixman_image_create_bits (pixman_format,
252 (uint32_t *) data, stride);
253 if (unlikely (pixman_image == NULL))
254 return (cairo_skia_surface_t *) _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
256 _cairo_surface_init (&surface->image.base,
257 &cairo_skia_surface_backend,
259 _cairo_content_from_pixman_format (pixman_format));
261 _cairo_image_surface_init (&surface->image, pixman_image, pixman_format);
263 surface->bitmap = new SkBitmap;
264 surface->bitmap->setConfig (config, width, height, surface->image.stride);
265 surface->bitmap->setIsOpaque (opaque);
266 surface->bitmap->setPixels (surface->image.data);
268 surface->image.base.is_clear = data == NULL;
274 cairo_skia_surface_create (cairo_format_t format,
278 SkBitmap::Config config;
281 if (! CAIRO_FORMAT_VALID (format) ||
282 ! format_to_sk_config (format, config, opaque))
284 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
287 return &_cairo_skia_surface_create_internal (config, opaque, NULL, width, height, 0)->image.base;
291 cairo_skia_surface_create_for_data (unsigned char *data,
292 cairo_format_t format,
297 SkBitmap::Config config;
300 if (! CAIRO_FORMAT_VALID (format) ||
301 ! format_to_sk_config (format, config, opaque))
303 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
306 return &_cairo_skia_surface_create_internal (config, opaque, data, width, height, stride)->image.base;
320 - antialiased clipping?
323 - implement clip path reset (to avoid restore/save)
324 - implement complex radial patterns (2 centers and 2 radii)
327 - implement EXTEND_NONE