Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / c / sk_surface.cpp
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7
8 #include "sk_surface.h"
9
10 #include "SkCanvas.h"
11 #include "SkImage.h"
12 #include "SkMatrix.h"
13 #include "SkPaint.h"
14 #include "SkPath.h"
15 #include "SkSurface.h"
16
17 const struct {
18     sk_colortype_t  fC;
19     SkColorType     fSK;
20 } gColorTypeMap[] = {
21     { UNKNOWN_SK_COLORTYPE,     kUnknown_SkColorType    },
22     { RGBA_8888_SK_COLORTYPE,   kRGBA_8888_SkColorType  },
23     { BGRA_8888_SK_COLORTYPE,   kBGRA_8888_SkColorType  },
24     { ALPHA_8_SK_COLORTYPE,     kAlpha_8_SkColorType    },
25 };
26
27 const struct {
28     sk_alphatype_t  fC;
29     SkAlphaType     fSK;
30 } gAlphaTypeMap[] = {
31     { OPAQUE_SK_ALPHATYPE,      kOpaque_SkAlphaType     },
32     { PREMUL_SK_ALPHATYPE,      kPremul_SkAlphaType     },
33     { UNPREMUL_SK_ALPHATYPE,    kUnpremul_SkAlphaType   },
34 };
35
36 static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) {
37     for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
38         if (gColorTypeMap[i].fC == cCT) {
39             if (skCT) {
40                 *skCT = gColorTypeMap[i].fSK;
41             }
42             return true;
43         }
44     }
45     return false;
46 }
47
48 static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) {
49     for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
50         if (gColorTypeMap[i].fSK == skCT) {
51             if (cCT) {
52                 *cCT = gColorTypeMap[i].fC;
53             }
54             return true;
55         }
56     }
57     return false;
58 }
59
60 static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) {
61     for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
62         if (gAlphaTypeMap[i].fC == cAT) {
63             if (skAT) {
64                 *skAT = gAlphaTypeMap[i].fSK;
65             }
66             return true;
67         }
68     }
69     return false;
70 }
71
72 static bool from_c_info(const sk_imageinfo_t& cinfo, SkImageInfo* info) {
73     SkColorType ct;
74     SkAlphaType at;
75
76     if (!from_c_colortype(cinfo.colorType, &ct)) {
77         // optionally report error to client?
78         return false;
79     }
80     if (!from_c_alphatype(cinfo.alphaType, &at)) {
81         // optionally report error to client?
82         return false;
83     }
84     if (info) {
85         *info = SkImageInfo::Make(cinfo.width, cinfo.height, ct, at);
86     }
87     return true;
88 }
89
90 static const SkRect& AsRect(const sk_rect_t& crect) {
91     return reinterpret_cast<const SkRect&>(crect);
92 }
93
94 static const SkPath& AsPath(const sk_path_t& cpath) {
95     return reinterpret_cast<const SkPath&>(cpath);
96 }
97
98 static SkPath* as_path(sk_path_t* cpath) {
99     return reinterpret_cast<SkPath*>(cpath);
100 }
101
102 static const SkImage* AsImage(const sk_image_t* cimage) {
103     return reinterpret_cast<const SkImage*>(cimage);
104 }
105
106 static const SkPaint& AsPaint(const sk_paint_t& cpaint) {
107     return reinterpret_cast<const SkPaint&>(cpaint);
108 }
109
110 static const SkPaint* AsPaint(const sk_paint_t* cpaint) {
111     return reinterpret_cast<const SkPaint*>(cpaint);
112 }
113
114 static SkPaint* AsPaint(sk_paint_t* cpaint) {
115     return reinterpret_cast<SkPaint*>(cpaint);
116 }
117
118 static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
119     return reinterpret_cast<SkCanvas*>(ccanvas);
120 }
121
122 ///////////////////////////////////////////////////////////////////////////////////////////
123
124 sk_colortype_t sk_colortype_get_default_8888() {
125     sk_colortype_t ct;
126     if (!to_c_colortype(kN32_SkColorType, &ct)) {
127         ct = UNKNOWN_SK_COLORTYPE;
128     }
129     return ct;
130 }
131
132 ///////////////////////////////////////////////////////////////////////////////////////////
133
134 sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
135                                      size_t rowBytes) {
136     SkImageInfo info;
137     if (!from_c_info(*cinfo, &info)) {
138         return NULL;
139     }
140     return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes);
141 }
142
143 void sk_image_ref(const sk_image_t* cimage) {
144     AsImage(cimage)->ref();
145 }
146
147 void sk_image_unref(const sk_image_t* cimage) {
148     AsImage(cimage)->unref();
149 }
150
151 int sk_image_get_width(const sk_image_t* cimage) {
152     return AsImage(cimage)->width();
153 }
154
155 int sk_image_get_height(const sk_image_t* cimage) {
156     return AsImage(cimage)->height();
157 }
158
159 uint32_t sk_image_get_unique_id(const sk_image_t* cimage) {
160     return AsImage(cimage)->uniqueID();
161 }
162
163 ///////////////////////////////////////////////////////////////////////////////////////////
164
165 sk_paint_t* sk_paint_new() {
166     return (sk_paint_t*)SkNEW(SkPaint);
167 }
168
169 void sk_paint_delete(sk_paint_t* cpaint) {
170     SkDELETE(AsPaint(cpaint));
171 }
172
173 bool sk_paint_is_antialias(const sk_paint_t* cpaint) {
174     return AsPaint(*cpaint).isAntiAlias();
175 }
176
177 void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) {
178     AsPaint(cpaint)->setAntiAlias(aa);
179 }
180
181 sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) {
182     return AsPaint(*cpaint).getColor();
183 }
184
185 void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) {
186     AsPaint(cpaint)->setColor(c);
187 }
188
189 ///////////////////////////////////////////////////////////////////////////////////////////
190
191 sk_path_t* sk_path_new() {
192     return (sk_path_t*)SkNEW(SkPath);
193 }
194
195 void sk_path_delete(sk_path_t* cpath) {
196     SkDELETE(as_path(cpath));
197 }
198
199 void sk_path_move_to(sk_path_t* cpath, float x, float y) {
200     as_path(cpath)->moveTo(x, y);
201 }
202
203 void sk_path_line_to(sk_path_t* cpath, float x, float y) {
204     as_path(cpath)->lineTo(x, y);
205 }
206
207 void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) {
208     as_path(cpath)->quadTo(x0, y0, x1, y1);
209 }
210
211 void sk_path_close(sk_path_t* cpath) {
212     as_path(cpath)->close();
213 }
214
215 ///////////////////////////////////////////////////////////////////////////////////////////
216
217 void sk_canvas_save(sk_canvas_t* ccanvas) {
218     AsCanvas(ccanvas)->save();
219 }
220
221 void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
222     AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
223 }
224
225 void sk_canvas_restore(sk_canvas_t* ccanvas) {
226     AsCanvas(ccanvas)->restore();
227 }
228
229 void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
230     AsCanvas(ccanvas)->translate(dx, dy);
231 }
232
233 void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
234     AsCanvas(ccanvas)->scale(sx, sy);
235 }
236
237 void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
238     AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
239 }
240
241 void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
242     AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
243 }
244
245 void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
246     AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
247 }
248
249 void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
250     AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
251 }
252
253 void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
254                           const sk_paint_t* cpaint) {
255     AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
256 }
257
258 ///////////////////////////////////////////////////////////////////////////////////////////
259
260 sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) {
261     SkImageInfo info;
262     if (!from_c_info(*cinfo, &info)) {
263         return NULL;
264     }
265     return (sk_surface_t*)SkSurface::NewRaster(info);
266 }
267
268 sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
269                                            size_t rowBytes) {
270     SkImageInfo info;
271     if (!from_c_info(*cinfo, &info)) {
272         return NULL;
273     }
274     return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes);
275 }
276
277 void sk_surface_delete(sk_surface_t* csurf) {
278     SkSurface* surf = (SkSurface*)csurf;
279     SkSafeUnref(surf);
280 }
281
282 sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
283     SkSurface* surf = (SkSurface*)csurf;
284     return (sk_canvas_t*)surf->getCanvas();
285 }
286
287 sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
288     SkSurface* surf = (SkSurface*)csurf;
289     return (sk_image_t*)surf->newImageSnapshot();
290 }
291
292
293 ///////////////////
294
295 void sk_test_capi(SkCanvas* canvas) {
296     sk_imageinfo_t cinfo;
297     cinfo.width = 100;
298     cinfo.height = 100;
299     cinfo.colorType = (sk_colortype_t)kN32_SkColorType;
300     cinfo.alphaType = (sk_alphatype_t)kPremul_SkAlphaType;
301     
302     sk_surface_t* csurface = sk_surface_new_raster(&cinfo);
303     sk_canvas_t* ccanvas = sk_surface_get_canvas(csurface);
304     
305     sk_paint_t* cpaint = sk_paint_new();
306     sk_paint_set_antialias(cpaint, true);
307     sk_paint_set_color(cpaint, 0xFFFF0000);
308     
309     sk_rect_t cr = { 5, 5, 95, 95 };
310     sk_canvas_draw_oval(ccanvas, &cr, cpaint);
311     
312     cr.left += 25;
313     cr.top += 25;
314     cr.right -= 25;
315     cr.bottom -= 25;
316     sk_paint_set_color(cpaint, 0xFF00FF00);
317     sk_canvas_draw_rect(ccanvas, &cr, cpaint);
318     
319     sk_path_t* cpath = sk_path_new();
320     sk_path_move_to(cpath, 50, 50);
321     sk_path_line_to(cpath, 100, 100);
322     sk_path_line_to(cpath, 50, 100);
323     sk_path_close(cpath);
324
325     sk_canvas_draw_path(ccanvas, cpath, cpaint);
326
327     sk_image_t* cimage = sk_surface_new_image_snapshot(csurface);
328     
329     // HERE WE CROSS THE C..C++ boundary
330     canvas->drawImage((const SkImage*)cimage, 20, 20, NULL);
331     
332     sk_path_delete(cpath);
333     sk_paint_delete(cpaint);
334     sk_image_unref(cimage);
335     sk_surface_delete(csurface);
336 }