Upload Tizen2.0 source
[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_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
101     if (extents->width < surface->image.width ||
102         extents->height < surface->image.height)
103     {
104         return _cairo_surface_create_for_rectangle_int (&surface->image.base,
105                                                         extents);
106     }
107
108     return cairo_surface_reference (&surface->image.base);
109 }
110
111 static cairo_int_status_t
112 _cairo_skia_surface_unmap_image (void *asurface,
113                                  cairo_image_surface_t *image)
114 {
115     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
116
117     surface->bitmap->unlockPixels ();
118     return CAIRO_INT_STATUS_SUCCESS;
119 }
120
121 static cairo_status_t
122 _cairo_skia_surface_acquire_source_image (void *asurface,
123                                           cairo_image_surface_t **image_out,
124                                           void **image_extra)
125 {
126     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
127
128     surface->bitmap->lockPixels ();
129
130     *image_out = &surface->image;
131     *image_extra = NULL;
132     return CAIRO_STATUS_SUCCESS;
133 }
134
135 static void
136 _cairo_skia_surface_release_source_image (void *asurface,
137                                           cairo_image_surface_t *image,
138                                           void *image_extra)
139 {
140     cairo_skia_surface_t *surface = (cairo_skia_surface_t *) asurface;
141
142     surface->bitmap->unlockPixels ();
143 }
144
145 static cairo_bool_t
146 _cairo_skia_surface_get_extents (void *asurface,
147                                   cairo_rectangle_int_t *extents)
148 {
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;
153     return TRUE;
154 }
155
156 static void
157 _cairo_skia_surface_get_font_options (void                  *abstract_surface,
158                                        cairo_font_options_t  *options)
159 {
160     _cairo_font_options_init_default (options);
161
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);
164 }
165
166 static const struct _cairo_surface_backend
167 cairo_skia_surface_backend = {
168     CAIRO_SURFACE_TYPE_SKIA,
169     _cairo_skia_surface_finish,
170
171     _cairo_skia_context_create,
172
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,
177
178     _cairo_surface_default_source,
179     _cairo_skia_surface_acquire_source_image,
180     _cairo_skia_surface_release_source_image,
181     NULL, /* snapshot */
182
183     NULL, /* copy_page */
184     NULL, /* show_page */
185
186     _cairo_skia_surface_get_extents,
187     _cairo_skia_surface_get_font_options,
188
189     NULL, /* flush */
190     NULL, /* mark_dirty_rectangle */
191
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
199 };
200
201 /*
202  * Surface constructors
203  */
204
205 static inline pixman_format_code_t
206 sk_config_to_pixman_format_code (SkBitmap::Config config,
207                                  bool opaque)
208 {
209     switch (config) {
210     case SkBitmap::kARGB_8888_Config:
211         return opaque ? PIXMAN_x8r8g8b8 : PIXMAN_a8r8g8b8;
212
213     case SkBitmap::kA8_Config:
214         return PIXMAN_a8;
215
216     case SkBitmap::kA1_Config:
217         return PIXMAN_a1;
218     case SkBitmap::kRGB_565_Config:
219         return PIXMAN_r5g6b5;
220     case SkBitmap::kARGB_4444_Config:
221         return PIXMAN_a4r4g4b4;
222
223     case SkBitmap::kNo_Config:
224     case SkBitmap::kIndex8_Config:
225     case SkBitmap::kRLE_Index8_Config:
226     case SkBitmap::kConfigCount:
227     default:
228         ASSERT_NOT_REACHED;
229         return (pixman_format_code_t) -1;
230     }
231 }
232
233 static cairo_skia_surface_t *
234 _cairo_skia_surface_create_internal (SkBitmap::Config config,
235                                      bool opaque,
236                                      unsigned char *data,
237                                      int width,
238                                      int height,
239                                      int stride)
240 {
241     cairo_skia_surface_t *surface;
242     pixman_image_t *pixman_image;
243     pixman_format_code_t pixman_format;
244
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));
248
249     pixman_format = sk_config_to_pixman_format_code (config, opaque);
250     pixman_image = pixman_image_create_bits (pixman_format,
251                                              width, height,
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));
255
256     _cairo_surface_init (&surface->image.base,
257                          &cairo_skia_surface_backend,
258                          NULL, /* device */
259                          _cairo_content_from_pixman_format (pixman_format));
260
261     _cairo_image_surface_init (&surface->image, pixman_image, pixman_format);
262
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);
267
268     surface->image.base.is_clear = data == NULL;
269
270     return surface;
271 }
272
273 cairo_surface_t *
274 cairo_skia_surface_create (cairo_format_t format,
275                            int width,
276                            int height)
277 {
278     SkBitmap::Config config;
279     bool opaque;
280
281     if (! CAIRO_FORMAT_VALID (format) ||
282         ! format_to_sk_config (format, config, opaque))
283     {
284         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
285     }
286
287     return &_cairo_skia_surface_create_internal (config, opaque, NULL, width, height, 0)->image.base;
288 }
289
290 cairo_surface_t *
291 cairo_skia_surface_create_for_data (unsigned char *data,
292                                     cairo_format_t format,
293                                     int width,
294                                     int height,
295                                     int stride)
296 {
297     SkBitmap::Config config;
298     bool opaque;
299
300     if (! CAIRO_FORMAT_VALID (format) ||
301         ! format_to_sk_config (format, config, opaque))
302     {
303         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
304     }
305
306     return &_cairo_skia_surface_create_internal (config, opaque, data, width, height, stride)->image.base;
307 }
308
309 /***
310
311 Todo:
312
313 *** Skia:
314
315 - mask()
316
317 *** Sk:
318
319 High:
320 - antialiased clipping?
321
322 Medium:
323 - implement clip path reset (to avoid restore/save)
324 - implement complex radial patterns (2 centers and 2 radii)
325
326 Low:
327 - implement EXTEND_NONE
328
329 ***/