Tizen 2.0 Release
[framework/graphics/cairo.git] / src / skia / cairo-skia-surface.cpp
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
3  *
4  * Copyright © 2007 Mozilla Corporation
5  *
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.
13  *
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
19  *
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/
24  *
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.
28  *
29  * The Original Code is the cairo graphics library.
30  *
31  * The Initial Developer of the Original Code is Mozilla Corporation.
32  *
33  * Contributor(s):
34  *      Vladimir Vukicevic <vladimir@mozilla.com>
35  */
36
37 #include "cairoint.h"
38
39 #include "cairo-skia.h"
40 #include "cairo-skia-private.h"
41
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"
46
47 static cairo_skia_surface_t *
48 _cairo_skia_surface_create_internal (SkBitmap::Config config,
49                                      bool opaque,
50                                      unsigned char *data,
51                                      int width,
52                                      int height,
53                                      int stride);
54
55 static cairo_surface_t *
56 _cairo_skia_surface_create_similar (void *asurface,
57                                     cairo_content_t content,
58                                     int width,
59                                     int height)
60 {
61     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
62     SkBitmap::Config config;
63     bool opaque;
64
65     if (content == surface->image.base.content)
66     {
67         config = surface->bitmap->getConfig ();
68         opaque = surface->bitmap->isOpaque ();
69     }
70     else if (! format_to_sk_config (_cairo_format_from_content (content),
71                                     config, opaque))
72     {
73         return NULL;
74     }
75
76     return &_cairo_skia_surface_create_internal (config, opaque,
77                                                  NULL,
78                                                  width, height,
79                                                  0)->image.base;
80 }
81
82 static cairo_status_t
83 _cairo_skia_surface_finish (void *asurface)
84 {
85     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
86
87     cairo_surface_finish (&surface->image.base);
88     delete surface->bitmap;
89
90     return CAIRO_STATUS_SUCCESS;
91 }
92
93 static cairo_image_surface_t *
94 _cairo_skia_surface_map_to_image (void *asurface,
95                                   const cairo_rectangle_int_t *extents)
96 {
97     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
98
99     surface->bitmap->lockPixels ();
100     return _cairo_image_surface_map_to_image (&surface->image, extents);
101 }
102
103 static cairo_int_status_t
104 _cairo_skia_surface_unmap_image (void *asurface,
105                                  cairo_image_surface_t *image)
106 {
107     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
108     cairo_int_status_t status;
109
110     status = _cairo_image_surface_unmap_image (&surface->image, image);
111     surface->bitmap->unlockPixels ();
112
113     return status;
114 }
115
116 static cairo_status_t
117 _cairo_skia_surface_acquire_source_image (void *asurface,
118                                           cairo_image_surface_t **image_out,
119                                           void **image_extra)
120 {
121     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
122
123     surface->bitmap->lockPixels ();
124
125     *image_out = &surface->image;
126     *image_extra = NULL;
127     return CAIRO_STATUS_SUCCESS;
128 }
129
130 static void
131 _cairo_skia_surface_release_source_image (void *asurface,
132                                           cairo_image_surface_t *image,
133                                           void *image_extra)
134 {
135     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
136
137     surface->bitmap->unlockPixels ();
138 }
139
140 static cairo_bool_t
141 _cairo_skia_surface_get_extents (void *asurface,
142                                   cairo_rectangle_int_t *extents)
143 {
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;
148     return TRUE;
149 }
150
151 static void
152 _cairo_skia_surface_get_font_options (void                  *abstract_surface,
153                                        cairo_font_options_t  *options)
154 {
155     _cairo_font_options_init_default (options);
156
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);
159 }
160
161 static const struct _cairo_surface_backend
162 cairo_skia_surface_backend = {
163     CAIRO_SURFACE_TYPE_SKIA,
164     _cairo_skia_surface_finish,
165
166     _cairo_skia_context_create,
167
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,
172
173     _cairo_surface_default_source,
174     _cairo_skia_surface_acquire_source_image,
175     _cairo_skia_surface_release_source_image,
176     NULL, /* snapshot */
177
178     NULL, /* copy_page */
179     NULL, /* show_page */
180
181     _cairo_skia_surface_get_extents,
182     _cairo_skia_surface_get_font_options,
183
184     NULL, /* flush */
185     NULL, /* mark_dirty_rectangle */
186
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
194 };
195
196 /*
197  * Surface constructors
198  */
199
200 static inline pixman_format_code_t
201 sk_config_to_pixman_format_code (SkBitmap::Config config,
202                                  bool opaque)
203 {
204     switch (config) {
205     case SkBitmap::kARGB_8888_Config:
206         return opaque ? PIXMAN_x8r8g8b8 : PIXMAN_a8r8g8b8;
207
208     case SkBitmap::kA8_Config:
209         return PIXMAN_a8;
210
211     case SkBitmap::kA1_Config:
212         return PIXMAN_a1;
213     case SkBitmap::kRGB_565_Config:
214         return PIXMAN_r5g6b5;
215     case SkBitmap::kARGB_4444_Config:
216         return PIXMAN_a4r4g4b4;
217
218     case SkBitmap::kNo_Config:
219     case SkBitmap::kIndex8_Config:
220     case SkBitmap::kRLE_Index8_Config:
221     case SkBitmap::kConfigCount:
222     default:
223         ASSERT_NOT_REACHED;
224         return (pixman_format_code_t) -1;
225     }
226 }
227
228 static cairo_skia_surface_t *
229 _cairo_skia_surface_create_internal (SkBitmap::Config config,
230                                      bool opaque,
231                                      unsigned char *data,
232                                      int width,
233                                      int height,
234                                      int stride)
235 {
236     cairo_skia_surface_t *surface;
237     pixman_image_t *pixman_image;
238     pixman_format_code_t pixman_format;
239
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));
243
244     pixman_format = sk_config_to_pixman_format_code (config, opaque);
245     pixman_image = pixman_image_create_bits (pixman_format,
246                                              width, height,
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));
250
251     _cairo_surface_init (&surface->image.base,
252                          &cairo_skia_surface_backend,
253                          NULL, /* device */
254                          _cairo_content_from_pixman_format (pixman_format));
255
256     _cairo_image_surface_init (&surface->image, pixman_image, pixman_format);
257
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);
262
263     surface->image.base.is_clear = data == NULL;
264
265     return surface;
266 }
267
268 cairo_surface_t *
269 cairo_skia_surface_create (cairo_format_t format,
270                            int width,
271                            int height)
272 {
273     SkBitmap::Config config;
274     bool opaque;
275
276     if (! CAIRO_FORMAT_VALID (format) ||
277         ! format_to_sk_config (format, config, opaque))
278     {
279         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
280     }
281
282     return &_cairo_skia_surface_create_internal (config, opaque, NULL, width, height, 0)->image.base;
283 }
284
285 cairo_surface_t *
286 cairo_skia_surface_create_for_data (unsigned char *data,
287                                     cairo_format_t format,
288                                     int width,
289                                     int height,
290                                     int stride)
291 {
292     SkBitmap::Config config;
293     bool opaque;
294
295     if (! CAIRO_FORMAT_VALID (format) ||
296         ! format_to_sk_config (format, config, opaque))
297     {
298         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
299     }
300
301     return &_cairo_skia_surface_create_internal (config, opaque, data, width, height, stride)->image.base;
302 }
303
304 /***
305
306 Todo:
307
308 *** Skia:
309
310 - mask()
311
312 *** Sk:
313
314 High:
315 - antialiased clipping?
316
317 Medium:
318 - implement clip path reset (to avoid restore/save)
319 - implement complex radial patterns (2 centers and 2 radii)
320
321 Low:
322 - implement EXTEND_NONE
323
324 ***/