2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "sk_surface.h"
15 #include "SkSurface.h"
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 },
31 { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType },
32 { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType },
33 { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType },
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) {
40 *skCT = gColorTypeMap[i].fSK;
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) {
52 *cCT = gColorTypeMap[i].fC;
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) {
64 *skAT = gAlphaTypeMap[i].fSK;
72 static bool from_c_info(const sk_imageinfo_t& cinfo, SkImageInfo* info) {
76 if (!from_c_colortype(cinfo.colorType, &ct)) {
77 // optionally report error to client?
80 if (!from_c_alphatype(cinfo.alphaType, &at)) {
81 // optionally report error to client?
85 *info = SkImageInfo::Make(cinfo.width, cinfo.height, ct, at);
90 static const SkRect& AsRect(const sk_rect_t& crect) {
91 return reinterpret_cast<const SkRect&>(crect);
94 static const SkPath& AsPath(const sk_path_t& cpath) {
95 return reinterpret_cast<const SkPath&>(cpath);
98 static SkPath* as_path(sk_path_t* cpath) {
99 return reinterpret_cast<SkPath*>(cpath);
102 static const SkImage* AsImage(const sk_image_t* cimage) {
103 return reinterpret_cast<const SkImage*>(cimage);
106 static const SkPaint& AsPaint(const sk_paint_t& cpaint) {
107 return reinterpret_cast<const SkPaint&>(cpaint);
110 static const SkPaint* AsPaint(const sk_paint_t* cpaint) {
111 return reinterpret_cast<const SkPaint*>(cpaint);
114 static SkPaint* AsPaint(sk_paint_t* cpaint) {
115 return reinterpret_cast<SkPaint*>(cpaint);
118 static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
119 return reinterpret_cast<SkCanvas*>(ccanvas);
122 ///////////////////////////////////////////////////////////////////////////////////////////
124 sk_colortype_t sk_colortype_get_default_8888() {
126 if (!to_c_colortype(kN32_SkColorType, &ct)) {
127 ct = UNKNOWN_SK_COLORTYPE;
132 ///////////////////////////////////////////////////////////////////////////////////////////
134 sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
137 if (!from_c_info(*cinfo, &info)) {
140 return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes);
143 void sk_image_ref(const sk_image_t* cimage) {
144 AsImage(cimage)->ref();
147 void sk_image_unref(const sk_image_t* cimage) {
148 AsImage(cimage)->unref();
151 int sk_image_get_width(const sk_image_t* cimage) {
152 return AsImage(cimage)->width();
155 int sk_image_get_height(const sk_image_t* cimage) {
156 return AsImage(cimage)->height();
159 uint32_t sk_image_get_unique_id(const sk_image_t* cimage) {
160 return AsImage(cimage)->uniqueID();
163 ///////////////////////////////////////////////////////////////////////////////////////////
165 sk_paint_t* sk_paint_new() {
166 return (sk_paint_t*)SkNEW(SkPaint);
169 void sk_paint_delete(sk_paint_t* cpaint) {
170 SkDELETE(AsPaint(cpaint));
173 bool sk_paint_is_antialias(const sk_paint_t* cpaint) {
174 return AsPaint(*cpaint).isAntiAlias();
177 void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) {
178 AsPaint(cpaint)->setAntiAlias(aa);
181 sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) {
182 return AsPaint(*cpaint).getColor();
185 void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) {
186 AsPaint(cpaint)->setColor(c);
189 ///////////////////////////////////////////////////////////////////////////////////////////
191 sk_path_t* sk_path_new() {
192 return (sk_path_t*)SkNEW(SkPath);
195 void sk_path_delete(sk_path_t* cpath) {
196 SkDELETE(as_path(cpath));
199 void sk_path_move_to(sk_path_t* cpath, float x, float y) {
200 as_path(cpath)->moveTo(x, y);
203 void sk_path_line_to(sk_path_t* cpath, float x, float y) {
204 as_path(cpath)->lineTo(x, y);
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);
211 void sk_path_close(sk_path_t* cpath) {
212 as_path(cpath)->close();
215 ///////////////////////////////////////////////////////////////////////////////////////////
217 void sk_canvas_save(sk_canvas_t* ccanvas) {
218 AsCanvas(ccanvas)->save();
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));
225 void sk_canvas_restore(sk_canvas_t* ccanvas) {
226 AsCanvas(ccanvas)->restore();
229 void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
230 AsCanvas(ccanvas)->translate(dx, dy);
233 void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
234 AsCanvas(ccanvas)->scale(sx, sy);
237 void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
238 AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
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));
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));
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));
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));
258 ///////////////////////////////////////////////////////////////////////////////////////////
260 sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) {
262 if (!from_c_info(*cinfo, &info)) {
265 return (sk_surface_t*)SkSurface::NewRaster(info);
268 sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
271 if (!from_c_info(*cinfo, &info)) {
274 return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes);
277 void sk_surface_delete(sk_surface_t* csurf) {
278 SkSurface* surf = (SkSurface*)csurf;
282 sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
283 SkSurface* surf = (SkSurface*)csurf;
284 return (sk_canvas_t*)surf->getCanvas();
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();
295 void sk_test_capi(SkCanvas* canvas) {
296 sk_imageinfo_t cinfo;
299 cinfo.colorType = (sk_colortype_t)kN32_SkColorType;
300 cinfo.alphaType = (sk_alphatype_t)kPremul_SkAlphaType;
302 sk_surface_t* csurface = sk_surface_new_raster(&cinfo);
303 sk_canvas_t* ccanvas = sk_surface_get_canvas(csurface);
305 sk_paint_t* cpaint = sk_paint_new();
306 sk_paint_set_antialias(cpaint, true);
307 sk_paint_set_color(cpaint, 0xFFFF0000);
309 sk_rect_t cr = { 5, 5, 95, 95 };
310 sk_canvas_draw_oval(ccanvas, &cr, cpaint);
316 sk_paint_set_color(cpaint, 0xFF00FF00);
317 sk_canvas_draw_rect(ccanvas, &cr, cpaint);
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);
325 sk_canvas_draw_path(ccanvas, cpath, cpaint);
327 sk_image_t* cimage = sk_surface_new_image_snapshot(csurface);
329 // HERE WE CROSS THE C..C++ boundary
330 canvas->drawImage((const SkImage*)cimage, 20, 20, NULL);
332 sk_path_delete(cpath);
333 sk_paint_delete(cpaint);
334 sk_image_unref(cimage);
335 sk_surface_delete(csurface);