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_image_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 ();
100 return _cairo_image_surface_map_to_image (&surface->image, extents);
103 static cairo_int_status_t
104 _cairo_skia_surface_unmap_image (void *asurface,
105 cairo_image_surface_t *image)
107 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
108 cairo_int_status_t status;
110 status = _cairo_image_surface_unmap_image (&surface->image, image);
111 surface->bitmap->unlockPixels ();
116 static cairo_status_t
117 _cairo_skia_surface_acquire_source_image (void *asurface,
118 cairo_image_surface_t **image_out,
121 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
123 surface->bitmap->lockPixels ();
125 *image_out = &surface->image;
127 return CAIRO_STATUS_SUCCESS;
131 _cairo_skia_surface_release_source_image (void *asurface,
132 cairo_image_surface_t *image,
135 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
137 surface->bitmap->unlockPixels ();
141 _cairo_skia_surface_get_extents (void *asurface,
142 cairo_rectangle_int_t *extents)
144 cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
145 extents->x = extents->y = 0;
146 extents->width = surface->image.width;
147 extents->height = surface->image.height;
152 _cairo_skia_surface_get_font_options (void *abstract_surface,
153 cairo_font_options_t *options)
155 _cairo_font_options_init_default (options);
157 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
158 _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
161 static const struct _cairo_surface_backend
162 cairo_skia_surface_backend = {
163 CAIRO_SURFACE_TYPE_SKIA,
164 _cairo_skia_surface_finish,
166 _cairo_skia_context_create,
168 _cairo_skia_surface_create_similar,
169 NULL, //_cairo_skia_surface_create_similar_image,
170 _cairo_skia_surface_map_to_image,
171 _cairo_skia_surface_unmap_image,
173 _cairo_surface_default_source,
174 _cairo_skia_surface_acquire_source_image,
175 _cairo_skia_surface_release_source_image,
178 NULL, /* copy_page */
179 NULL, /* show_page */
181 _cairo_skia_surface_get_extents,
182 _cairo_skia_surface_get_font_options,
185 NULL, /* mark_dirty_rectangle */
187 /* XXX native surface functions? */
188 _cairo_surface_fallback_paint,
189 _cairo_surface_fallback_mask,
190 _cairo_surface_fallback_stroke,
191 _cairo_surface_fallback_fill,
192 NULL, /* fill/stroke */
193 _cairo_surface_fallback_glyphs
197 * Surface constructors
200 static inline pixman_format_code_t
201 sk_config_to_pixman_format_code (SkBitmap::Config config,
205 case SkBitmap::kARGB_8888_Config:
206 return opaque ? PIXMAN_x8r8g8b8 : PIXMAN_a8r8g8b8;
208 case SkBitmap::kA8_Config:
211 case SkBitmap::kA1_Config:
213 case SkBitmap::kRGB_565_Config:
214 return PIXMAN_r5g6b5;
215 case SkBitmap::kARGB_4444_Config:
216 return PIXMAN_a4r4g4b4;
218 case SkBitmap::kNo_Config:
219 case SkBitmap::kIndex8_Config:
220 case SkBitmap::kRLE_Index8_Config:
221 case SkBitmap::kConfigCount:
224 return (pixman_format_code_t) -1;
228 static cairo_skia_surface_t *
229 _cairo_skia_surface_create_internal (SkBitmap::Config config,
236 cairo_skia_surface_t *surface;
237 pixman_image_t *pixman_image;
238 pixman_format_code_t pixman_format;
240 surface = (cairo_skia_surface_t *) malloc (sizeof (cairo_skia_surface_t));
241 if (unlikely (surface == NULL))
242 return (cairo_skia_surface_t *) _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
244 pixman_format = sk_config_to_pixman_format_code (config, opaque);
245 pixman_image = pixman_image_create_bits (pixman_format,
247 (uint32_t *) data, stride);
248 if (unlikely (pixman_image == NULL))
249 return (cairo_skia_surface_t *) _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
251 _cairo_surface_init (&surface->image.base,
252 &cairo_skia_surface_backend,
254 _cairo_content_from_pixman_format (pixman_format));
256 _cairo_image_surface_init (&surface->image, pixman_image, pixman_format);
258 surface->bitmap = new SkBitmap;
259 surface->bitmap->setConfig (config, width, height, surface->image.stride);
260 surface->bitmap->setIsOpaque (opaque);
261 surface->bitmap->setPixels (surface->image.data);
263 surface->image.base.is_clear = data == NULL;
269 cairo_skia_surface_create (cairo_format_t format,
273 SkBitmap::Config config;
276 if (! CAIRO_FORMAT_VALID (format) ||
277 ! format_to_sk_config (format, config, opaque))
279 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
282 return &_cairo_skia_surface_create_internal (config, opaque, NULL, width, height, 0)->image.base;
286 cairo_skia_surface_create_for_data (unsigned char *data,
287 cairo_format_t format,
292 SkBitmap::Config config;
295 if (! CAIRO_FORMAT_VALID (format) ||
296 ! format_to_sk_config (format, config, opaque))
298 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
301 return &_cairo_skia_surface_create_internal (config, opaque, data, width, height, stride)->image.base;
315 - antialiased clipping?
318 - implement clip path reset (to avoid restore/save)
319 - implement complex radial patterns (2 centers and 2 radii)
322 - implement EXTEND_NONE