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 "SkCanvas.h"
9 #include "SkData.h"
10 #include "SkImage.h"
11 #include "SkMaskFilter.h"
12 #include "SkMatrix.h"
13 #include "SkPaint.h"
14 #include "SkPath.h"
15 #include "SkPictureRecorder.h"
16 #include "SkSurface.h"
17
18 #include "sk_canvas.h"
19 #include "sk_data.h"
20 #include "sk_image.h"
21 #include "sk_paint.h"
22 #include "sk_path.h"
23 #include "sk_surface.h"
24 #include "sk_types_priv.h"
25
26 const struct {
27 sk_colortype_t fC;
28 SkColorType fSK;
29 } gColorTypeMap[] = {
30 { UNKNOWN_SK_COLORTYPE, kUnknown_SkColorType },
31 { RGBA_8888_SK_COLORTYPE, kRGBA_8888_SkColorType },
32 { BGRA_8888_SK_COLORTYPE, kBGRA_8888_SkColorType },
33 { ALPHA_8_SK_COLORTYPE, kAlpha_8_SkColorType },
34 };
35
36 const struct {
37 sk_alphatype_t fC;
38 SkAlphaType fSK;
39 } gAlphaTypeMap[] = {
40 { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType },
41 { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType },
42 { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType },
43 };
44
from_c_colortype(sk_colortype_t cCT,SkColorType * skCT)45 static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) {
46 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
47 if (gColorTypeMap[i].fC == cCT) {
48 if (skCT) {
49 *skCT = gColorTypeMap[i].fSK;
50 }
51 return true;
52 }
53 }
54 return false;
55 }
56
to_c_colortype(SkColorType skCT,sk_colortype_t * cCT)57 static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) {
58 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
59 if (gColorTypeMap[i].fSK == skCT) {
60 if (cCT) {
61 *cCT = gColorTypeMap[i].fC;
62 }
63 return true;
64 }
65 }
66 return false;
67 }
68
from_c_alphatype(sk_alphatype_t cAT,SkAlphaType * skAT)69 static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) {
70 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
71 if (gAlphaTypeMap[i].fC == cAT) {
72 if (skAT) {
73 *skAT = gAlphaTypeMap[i].fSK;
74 }
75 return true;
76 }
77 }
78 return false;
79 }
80
from_c_info(const sk_imageinfo_t & cinfo,SkImageInfo * info)81 static bool from_c_info(const sk_imageinfo_t& cinfo, SkImageInfo* info) {
82 SkColorType ct;
83 SkAlphaType at;
84
85 if (!from_c_colortype(cinfo.colorType, &ct)) {
86 // optionally report error to client?
87 return false;
88 }
89 if (!from_c_alphatype(cinfo.alphaType, &at)) {
90 // optionally report error to client?
91 return false;
92 }
93 if (info) {
94 *info = SkImageInfo::Make(cinfo.width, cinfo.height, ct, at);
95 }
96 return true;
97 }
98
99 const struct {
100 sk_pixelgeometry_t fC;
101 SkPixelGeometry fSK;
102 } gPixelGeometryMap[] = {
103 { UNKNOWN_SK_PIXELGEOMETRY, kUnknown_SkPixelGeometry },
104 { RGB_H_SK_PIXELGEOMETRY, kRGB_H_SkPixelGeometry },
105 { BGR_H_SK_PIXELGEOMETRY, kBGR_H_SkPixelGeometry },
106 { RGB_V_SK_PIXELGEOMETRY, kRGB_V_SkPixelGeometry },
107 { BGR_V_SK_PIXELGEOMETRY, kBGR_V_SkPixelGeometry },
108 };
109
110
from_c_pixelgeometry(sk_pixelgeometry_t cGeom,SkPixelGeometry * skGeom)111 static bool from_c_pixelgeometry(sk_pixelgeometry_t cGeom, SkPixelGeometry* skGeom) {
112 for (size_t i = 0; i < SK_ARRAY_COUNT(gPixelGeometryMap); ++i) {
113 if (gPixelGeometryMap[i].fC == cGeom) {
114 if (skGeom) {
115 *skGeom = gPixelGeometryMap[i].fSK;
116 }
117 return true;
118 }
119 }
120 return false;
121 }
122
from_c_matrix(const sk_matrix_t * cmatrix,SkMatrix * matrix)123 static void from_c_matrix(const sk_matrix_t* cmatrix, SkMatrix* matrix) {
124 matrix->setAll(cmatrix->mat[0], cmatrix->mat[1], cmatrix->mat[2],
125 cmatrix->mat[3], cmatrix->mat[4], cmatrix->mat[5],
126 cmatrix->mat[6], cmatrix->mat[7], cmatrix->mat[8]);
127 }
128
129 const struct {
130 sk_path_direction_t fC;
131 SkPath::Direction fSk;
132 } gPathDirMap[] = {
133 { CW_SK_PATH_DIRECTION, SkPath::kCW_Direction },
134 { CCW_SK_PATH_DIRECTION, SkPath::kCCW_Direction },
135 };
136
from_c_path_direction(sk_path_direction_t cdir,SkPath::Direction * dir)137 static bool from_c_path_direction(sk_path_direction_t cdir, SkPath::Direction* dir) {
138 for (size_t i = 0; i < SK_ARRAY_COUNT(gPathDirMap); ++i) {
139 if (gPathDirMap[i].fC == cdir) {
140 if (dir) {
141 *dir = gPathDirMap[i].fSk;
142 }
143 return true;
144 }
145 }
146 return false;
147 }
148
AsData(const sk_data_t * cdata)149 static SkData* AsData(const sk_data_t* cdata) {
150 return reinterpret_cast<SkData*>(const_cast<sk_data_t*>(cdata));
151 }
152
ToData(SkData * data)153 static sk_data_t* ToData(SkData* data) {
154 return reinterpret_cast<sk_data_t*>(data);
155 }
156
ToRect(const SkRect & rect)157 static sk_rect_t ToRect(const SkRect& rect) {
158 return reinterpret_cast<const sk_rect_t&>(rect);
159 }
160
AsRect(const sk_rect_t & crect)161 static const SkRect& AsRect(const sk_rect_t& crect) {
162 return reinterpret_cast<const SkRect&>(crect);
163 }
164
AsPath(const sk_path_t & cpath)165 static const SkPath& AsPath(const sk_path_t& cpath) {
166 return reinterpret_cast<const SkPath&>(cpath);
167 }
168
as_path(sk_path_t * cpath)169 static SkPath* as_path(sk_path_t* cpath) {
170 return reinterpret_cast<SkPath*>(cpath);
171 }
172
AsImage(const sk_image_t * cimage)173 static const SkImage* AsImage(const sk_image_t* cimage) {
174 return reinterpret_cast<const SkImage*>(cimage);
175 }
176
ToImage(SkImage * cimage)177 static sk_image_t* ToImage(SkImage* cimage) {
178 return reinterpret_cast<sk_image_t*>(cimage);
179 }
180
ToCanvas(SkCanvas * canvas)181 static sk_canvas_t* ToCanvas(SkCanvas* canvas) {
182 return reinterpret_cast<sk_canvas_t*>(canvas);
183 }
184
AsCanvas(sk_canvas_t * ccanvas)185 static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
186 return reinterpret_cast<SkCanvas*>(ccanvas);
187 }
188
AsPictureRecorder(sk_picture_recorder_t * crec)189 static SkPictureRecorder* AsPictureRecorder(sk_picture_recorder_t* crec) {
190 return reinterpret_cast<SkPictureRecorder*>(crec);
191 }
192
ToPictureRecorder(SkPictureRecorder * rec)193 static sk_picture_recorder_t* ToPictureRecorder(SkPictureRecorder* rec) {
194 return reinterpret_cast<sk_picture_recorder_t*>(rec);
195 }
196
AsPicture(const sk_picture_t * cpic)197 static const SkPicture* AsPicture(const sk_picture_t* cpic) {
198 return reinterpret_cast<const SkPicture*>(cpic);
199 }
200
AsPicture(sk_picture_t * cpic)201 static SkPicture* AsPicture(sk_picture_t* cpic) {
202 return reinterpret_cast<SkPicture*>(cpic);
203 }
204
ToPicture(SkPicture * pic)205 static sk_picture_t* ToPicture(SkPicture* pic) {
206 return reinterpret_cast<sk_picture_t*>(pic);
207 }
208
209 ///////////////////////////////////////////////////////////////////////////////////////////
210
sk_colortype_get_default_8888()211 sk_colortype_t sk_colortype_get_default_8888() {
212 sk_colortype_t ct;
213 if (!to_c_colortype(kN32_SkColorType, &ct)) {
214 ct = UNKNOWN_SK_COLORTYPE;
215 }
216 return ct;
217 }
218
219 ///////////////////////////////////////////////////////////////////////////////////////////
220
sk_image_new_raster_copy(const sk_imageinfo_t * cinfo,const void * pixels,size_t rowBytes)221 sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
222 size_t rowBytes) {
223 SkImageInfo info;
224 if (!from_c_info(*cinfo, &info)) {
225 return NULL;
226 }
227 return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes);
228 }
229
sk_image_new_from_encoded(const sk_data_t * cdata,const sk_irect_t * subset)230 sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata, const sk_irect_t* subset) {
231 return ToImage(SkImage::NewFromEncoded(AsData(cdata),
232 reinterpret_cast<const SkIRect*>(subset)));
233 }
234
sk_image_encode(const sk_image_t * cimage)235 sk_data_t* sk_image_encode(const sk_image_t* cimage) {
236 return ToData(AsImage(cimage)->encode());
237 }
238
sk_image_ref(const sk_image_t * cimage)239 void sk_image_ref(const sk_image_t* cimage) {
240 AsImage(cimage)->ref();
241 }
242
sk_image_unref(const sk_image_t * cimage)243 void sk_image_unref(const sk_image_t* cimage) {
244 AsImage(cimage)->unref();
245 }
246
sk_image_get_width(const sk_image_t * cimage)247 int sk_image_get_width(const sk_image_t* cimage) {
248 return AsImage(cimage)->width();
249 }
250
sk_image_get_height(const sk_image_t * cimage)251 int sk_image_get_height(const sk_image_t* cimage) {
252 return AsImage(cimage)->height();
253 }
254
sk_image_get_unique_id(const sk_image_t * cimage)255 uint32_t sk_image_get_unique_id(const sk_image_t* cimage) {
256 return AsImage(cimage)->uniqueID();
257 }
258
259 ///////////////////////////////////////////////////////////////////////////////////////////
260
sk_path_new()261 sk_path_t* sk_path_new() { return (sk_path_t*)new SkPath; }
262
sk_path_delete(sk_path_t * cpath)263 void sk_path_delete(sk_path_t* cpath) { delete as_path(cpath); }
264
sk_path_move_to(sk_path_t * cpath,float x,float y)265 void sk_path_move_to(sk_path_t* cpath, float x, float y) {
266 as_path(cpath)->moveTo(x, y);
267 }
268
sk_path_line_to(sk_path_t * cpath,float x,float y)269 void sk_path_line_to(sk_path_t* cpath, float x, float y) {
270 as_path(cpath)->lineTo(x, y);
271 }
272
sk_path_quad_to(sk_path_t * cpath,float x0,float y0,float x1,float y1)273 void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) {
274 as_path(cpath)->quadTo(x0, y0, x1, y1);
275 }
276
sk_path_conic_to(sk_path_t * cpath,float x0,float y0,float x1,float y1,float w)277 void sk_path_conic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float w) {
278 as_path(cpath)->conicTo(x0, y0, x1, y1, w);
279 }
280
sk_path_cubic_to(sk_path_t * cpath,float x0,float y0,float x1,float y1,float x2,float y2)281 void sk_path_cubic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float x2, float y2) {
282 as_path(cpath)->cubicTo(x0, y0, x1, y1, x2, y2);
283 }
284
sk_path_close(sk_path_t * cpath)285 void sk_path_close(sk_path_t* cpath) {
286 as_path(cpath)->close();
287 }
288
sk_path_add_rect(sk_path_t * cpath,const sk_rect_t * crect,sk_path_direction_t cdir)289 void sk_path_add_rect(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) {
290 SkPath::Direction dir;
291 if (!from_c_path_direction(cdir, &dir)) {
292 return;
293 }
294 as_path(cpath)->addRect(AsRect(*crect), dir);
295 }
296
sk_path_add_oval(sk_path_t * cpath,const sk_rect_t * crect,sk_path_direction_t cdir)297 void sk_path_add_oval(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) {
298 SkPath::Direction dir;
299 if (!from_c_path_direction(cdir, &dir)) {
300 return;
301 }
302 as_path(cpath)->addOval(AsRect(*crect), dir);
303 }
304
sk_path_get_bounds(const sk_path_t * cpath,sk_rect_t * crect)305 bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) {
306 const SkPath& path = AsPath(*cpath);
307
308 if (path.isEmpty()) {
309 if (crect) {
310 *crect = ToRect(SkRect::MakeEmpty());
311 }
312 return false;
313 }
314
315 if (crect) {
316 *crect = ToRect(path.getBounds());
317 }
318 return true;
319 }
320
321 ///////////////////////////////////////////////////////////////////////////////////////////
322
sk_canvas_save(sk_canvas_t * ccanvas)323 void sk_canvas_save(sk_canvas_t* ccanvas) {
324 AsCanvas(ccanvas)->save();
325 }
326
sk_canvas_save_layer(sk_canvas_t * ccanvas,const sk_rect_t * crect,const sk_paint_t * cpaint)327 void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
328 AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
329 }
330
sk_canvas_restore(sk_canvas_t * ccanvas)331 void sk_canvas_restore(sk_canvas_t* ccanvas) {
332 AsCanvas(ccanvas)->restore();
333 }
334
sk_canvas_translate(sk_canvas_t * ccanvas,float dx,float dy)335 void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
336 AsCanvas(ccanvas)->translate(dx, dy);
337 }
338
sk_canvas_scale(sk_canvas_t * ccanvas,float sx,float sy)339 void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
340 AsCanvas(ccanvas)->scale(sx, sy);
341 }
342
sk_canvas_rotate_degress(sk_canvas_t * ccanvas,float degrees)343 void sk_canvas_rotate_degress(sk_canvas_t* ccanvas, float degrees) {
344 AsCanvas(ccanvas)->rotate(degrees);
345 }
346
sk_canvas_rotate_radians(sk_canvas_t * ccanvas,float radians)347 void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) {
348 AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians));
349 }
350
sk_canvas_skew(sk_canvas_t * ccanvas,float sx,float sy)351 void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) {
352 AsCanvas(ccanvas)->skew(sx, sy);
353 }
354
sk_canvas_concat(sk_canvas_t * ccanvas,const sk_matrix_t * cmatrix)355 void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) {
356 SkASSERT(cmatrix);
357 SkMatrix matrix;
358 from_c_matrix(cmatrix, &matrix);
359 AsCanvas(ccanvas)->concat(matrix);
360 }
361
sk_canvas_clip_rect(sk_canvas_t * ccanvas,const sk_rect_t * crect)362 void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) {
363 AsCanvas(ccanvas)->clipRect(AsRect(*crect));
364 }
365
sk_canvas_clip_path(sk_canvas_t * ccanvas,const sk_path_t * cpath)366 void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) {
367 AsCanvas(ccanvas)->clipPath(AsPath(*cpath));
368 }
369
sk_canvas_draw_paint(sk_canvas_t * ccanvas,const sk_paint_t * cpaint)370 void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
371 AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
372 }
373
sk_canvas_draw_rect(sk_canvas_t * ccanvas,const sk_rect_t * crect,const sk_paint_t * cpaint)374 void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
375 AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
376 }
377
sk_canvas_draw_circle(sk_canvas_t * ccanvas,float cx,float cy,float rad,const sk_paint_t * cpaint)378 void sk_canvas_draw_circle(sk_canvas_t* ccanvas, float cx, float cy, float rad,
379 const sk_paint_t* cpaint) {
380 AsCanvas(ccanvas)->drawCircle(cx, cy, rad, AsPaint(*cpaint));
381 }
382
sk_canvas_draw_oval(sk_canvas_t * ccanvas,const sk_rect_t * crect,const sk_paint_t * cpaint)383 void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
384 AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
385 }
386
sk_canvas_draw_path(sk_canvas_t * ccanvas,const sk_path_t * cpath,const sk_paint_t * cpaint)387 void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
388 AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
389 }
390
sk_canvas_draw_image(sk_canvas_t * ccanvas,const sk_image_t * cimage,float x,float y,const sk_paint_t * cpaint)391 void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
392 const sk_paint_t* cpaint) {
393 AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
394 }
395
sk_canvas_draw_image_rect(sk_canvas_t * ccanvas,const sk_image_t * cimage,const sk_rect_t * csrcR,const sk_rect_t * cdstR,const sk_paint_t * cpaint)396 void sk_canvas_draw_image_rect(sk_canvas_t* ccanvas, const sk_image_t* cimage,
397 const sk_rect_t* csrcR, const sk_rect_t* cdstR,
398 const sk_paint_t* cpaint) {
399 SkCanvas* canvas = AsCanvas(ccanvas);
400 const SkImage* image = AsImage(cimage);
401 const SkRect& dst = AsRect(*cdstR);
402 const SkPaint* paint = AsPaint(cpaint);
403
404 if (csrcR) {
405 canvas->drawImageRect(image, AsRect(*csrcR), dst, paint);
406 } else {
407 canvas->drawImageRect(image, dst, paint);
408 }
409 }
410
sk_canvas_draw_picture(sk_canvas_t * ccanvas,const sk_picture_t * cpicture,const sk_matrix_t * cmatrix,const sk_paint_t * cpaint)411 void sk_canvas_draw_picture(sk_canvas_t* ccanvas, const sk_picture_t* cpicture,
412 const sk_matrix_t* cmatrix, const sk_paint_t* cpaint) {
413 const SkMatrix* matrixPtr = NULL;
414 SkMatrix matrix;
415 if (cmatrix) {
416 from_c_matrix(cmatrix, &matrix);
417 matrixPtr = &matrix;
418 }
419 AsCanvas(ccanvas)->drawPicture(AsPicture(cpicture), matrixPtr, AsPaint(cpaint));
420 }
421
422 ///////////////////////////////////////////////////////////////////////////////////////////
423
sk_surface_new_raster(const sk_imageinfo_t * cinfo,const sk_surfaceprops_t * props)424 sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo,
425 const sk_surfaceprops_t* props) {
426 SkImageInfo info;
427 if (!from_c_info(*cinfo, &info)) {
428 return NULL;
429 }
430 SkPixelGeometry geo = kUnknown_SkPixelGeometry;
431 if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
432 return NULL;
433 }
434
435 SkSurfaceProps surfProps(0, geo);
436 return (sk_surface_t*)SkSurface::NewRaster(info, &surfProps);
437 }
438
sk_surface_new_raster_direct(const sk_imageinfo_t * cinfo,void * pixels,size_t rowBytes,const sk_surfaceprops_t * props)439 sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
440 size_t rowBytes,
441 const sk_surfaceprops_t* props) {
442 SkImageInfo info;
443 if (!from_c_info(*cinfo, &info)) {
444 return NULL;
445 }
446 SkPixelGeometry geo = kUnknown_SkPixelGeometry;
447 if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
448 return NULL;
449 }
450
451 SkSurfaceProps surfProps(0, geo);
452 return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes, &surfProps);
453 }
454
sk_surface_unref(sk_surface_t * csurf)455 void sk_surface_unref(sk_surface_t* csurf) {
456 SkSafeUnref((SkSurface*)csurf);
457 }
458
sk_surface_get_canvas(sk_surface_t * csurf)459 sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
460 SkSurface* surf = (SkSurface*)csurf;
461 return (sk_canvas_t*)surf->getCanvas();
462 }
463
sk_surface_new_image_snapshot(sk_surface_t * csurf)464 sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
465 SkSurface* surf = (SkSurface*)csurf;
466 return (sk_image_t*)surf->newImageSnapshot();
467 }
468
469 ///////////////////////////////////////////////////////////////////////////////////////////
470
sk_picture_recorder_new()471 sk_picture_recorder_t* sk_picture_recorder_new() {
472 return ToPictureRecorder(new SkPictureRecorder);
473 }
474
sk_picture_recorder_delete(sk_picture_recorder_t * crec)475 void sk_picture_recorder_delete(sk_picture_recorder_t* crec) {
476 delete AsPictureRecorder(crec);
477 }
478
sk_picture_recorder_begin_recording(sk_picture_recorder_t * crec,const sk_rect_t * cbounds)479 sk_canvas_t* sk_picture_recorder_begin_recording(sk_picture_recorder_t* crec,
480 const sk_rect_t* cbounds) {
481 return ToCanvas(AsPictureRecorder(crec)->beginRecording(AsRect(*cbounds)));
482 }
483
sk_picture_recorder_end_recording(sk_picture_recorder_t * crec)484 sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) {
485 return ToPicture(AsPictureRecorder(crec)->endRecording());
486 }
487
sk_picture_ref(sk_picture_t * cpic)488 void sk_picture_ref(sk_picture_t* cpic) {
489 SkSafeRef(AsPicture(cpic));
490 }
491
sk_picture_unref(sk_picture_t * cpic)492 void sk_picture_unref(sk_picture_t* cpic) {
493 SkSafeUnref(AsPicture(cpic));
494 }
495
sk_picture_get_unique_id(sk_picture_t * cpic)496 uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) {
497 return AsPicture(cpic)->uniqueID();
498 }
499
sk_picture_get_bounds(sk_picture_t * cpic)500 sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) {
501 return ToRect(AsPicture(cpic)->cullRect());
502 }
503
504 ///////////////////////////////////////////////////////////////////////////////////////////
505
506 #include "../../include/effects/SkGradientShader.h"
507 #include "sk_shader.h"
508
509 const struct {
510 sk_shader_tilemode_t fC;
511 SkShader::TileMode fSK;
512 } gTileModeMap[] = {
513 { CLAMP_SK_SHADER_TILEMODE, SkShader::kClamp_TileMode },
514 { REPEAT_SK_SHADER_TILEMODE, SkShader::kRepeat_TileMode },
515 { MIRROR_SK_SHADER_TILEMODE, SkShader::kMirror_TileMode },
516 };
517
from_c_tilemode(sk_shader_tilemode_t cMode,SkShader::TileMode * skMode)518 static bool from_c_tilemode(sk_shader_tilemode_t cMode, SkShader::TileMode* skMode) {
519 for (size_t i = 0; i < SK_ARRAY_COUNT(gTileModeMap); ++i) {
520 if (cMode == gTileModeMap[i].fC) {
521 if (skMode) {
522 *skMode = gTileModeMap[i].fSK;
523 }
524 return true;
525 }
526 }
527 return false;
528 }
529
sk_shader_ref(sk_shader_t * cshader)530 void sk_shader_ref(sk_shader_t* cshader) {
531 SkSafeRef(AsShader(cshader));
532 }
533
sk_shader_unref(sk_shader_t * cshader)534 void sk_shader_unref(sk_shader_t* cshader) {
535 SkSafeUnref(AsShader(cshader));
536 }
537
sk_shader_new_linear_gradient(const sk_point_t pts[2],const sk_color_t colors[],const float colorPos[],int colorCount,sk_shader_tilemode_t cmode,const sk_matrix_t * cmatrix)538 sk_shader_t* sk_shader_new_linear_gradient(const sk_point_t pts[2],
539 const sk_color_t colors[],
540 const float colorPos[],
541 int colorCount,
542 sk_shader_tilemode_t cmode,
543 const sk_matrix_t* cmatrix) {
544 SkShader::TileMode mode;
545 if (!from_c_tilemode(cmode, &mode)) {
546 return NULL;
547 }
548 SkMatrix matrix;
549 if (cmatrix) {
550 from_c_matrix(cmatrix, &matrix);
551 } else {
552 matrix.setIdentity();
553 }
554 SkShader* s = SkGradientShader::CreateLinear(reinterpret_cast<const SkPoint*>(pts),
555 reinterpret_cast<const SkColor*>(colors),
556 colorPos, colorCount, mode, 0, &matrix);
557 return (sk_shader_t*)s;
558 }
559
to_skpoint(const sk_point_t & p)560 static const SkPoint& to_skpoint(const sk_point_t& p) {
561 return reinterpret_cast<const SkPoint&>(p);
562 }
563
sk_shader_new_radial_gradient(const sk_point_t * ccenter,float radius,const sk_color_t colors[],const float colorPos[],int colorCount,sk_shader_tilemode_t cmode,const sk_matrix_t * cmatrix)564 sk_shader_t* sk_shader_new_radial_gradient(const sk_point_t* ccenter,
565 float radius,
566 const sk_color_t colors[],
567 const float colorPos[],
568 int colorCount,
569 sk_shader_tilemode_t cmode,
570 const sk_matrix_t* cmatrix) {
571 SkShader::TileMode mode;
572 if (!from_c_tilemode(cmode, &mode)) {
573 return NULL;
574 }
575 SkMatrix matrix;
576 if (cmatrix) {
577 from_c_matrix(cmatrix, &matrix);
578 } else {
579 matrix.setIdentity();
580 }
581 SkPoint center = to_skpoint(*ccenter);
582 SkShader* s = SkGradientShader::CreateRadial(
583 center, (SkScalar)radius,
584 reinterpret_cast<const SkColor*>(colors),
585 reinterpret_cast<const SkScalar*>(colorPos),
586 colorCount, mode, 0, &matrix);
587 return (sk_shader_t*)s;
588 }
589
sk_shader_new_sweep_gradient(const sk_point_t * ccenter,const sk_color_t colors[],const float colorPos[],int colorCount,const sk_matrix_t * cmatrix)590 sk_shader_t* sk_shader_new_sweep_gradient(const sk_point_t* ccenter,
591 const sk_color_t colors[],
592 const float colorPos[],
593 int colorCount,
594 const sk_matrix_t* cmatrix) {
595 SkMatrix matrix;
596 if (cmatrix) {
597 from_c_matrix(cmatrix, &matrix);
598 } else {
599 matrix.setIdentity();
600 }
601 SkShader* s = SkGradientShader::CreateSweep(
602 (SkScalar)(ccenter->x),
603 (SkScalar)(ccenter->y),
604 reinterpret_cast<const SkColor*>(colors),
605 reinterpret_cast<const SkScalar*>(colorPos),
606 colorCount, 0, &matrix);
607 return (sk_shader_t*)s;
608 }
609
sk_shader_new_two_point_conical_gradient(const sk_point_t * start,float startRadius,const sk_point_t * end,float endRadius,const sk_color_t colors[],const float colorPos[],int colorCount,sk_shader_tilemode_t cmode,const sk_matrix_t * cmatrix)610 sk_shader_t* sk_shader_new_two_point_conical_gradient(const sk_point_t* start,
611 float startRadius,
612 const sk_point_t* end,
613 float endRadius,
614 const sk_color_t colors[],
615 const float colorPos[],
616 int colorCount,
617 sk_shader_tilemode_t cmode,
618 const sk_matrix_t* cmatrix) {
619 SkShader::TileMode mode;
620 if (!from_c_tilemode(cmode, &mode)) {
621 return NULL;
622 }
623 SkMatrix matrix;
624 if (cmatrix) {
625 from_c_matrix(cmatrix, &matrix);
626 } else {
627 matrix.setIdentity();
628 }
629 SkPoint skstart = to_skpoint(*start);
630 SkPoint skend = to_skpoint(*end);
631 SkShader* s = SkGradientShader::CreateTwoPointConical(
632 skstart, (SkScalar)startRadius,
633 skend, (SkScalar)endRadius,
634 reinterpret_cast<const SkColor*>(colors),
635 reinterpret_cast<const SkScalar*>(colorPos),
636 colorCount, mode, 0, &matrix);
637 return (sk_shader_t*)s;
638 }
639
640 ///////////////////////////////////////////////////////////////////////////////////////////
641
642 #include "../../include/effects/SkBlurMaskFilter.h"
643 #include "sk_maskfilter.h"
644
645 const struct {
646 sk_blurstyle_t fC;
647 SkBlurStyle fSk;
648 } gBlurStylePairs[] = {
649 { NORMAL_SK_BLUR_STYLE, kNormal_SkBlurStyle },
650 { SOLID_SK_BLUR_STYLE, kSolid_SkBlurStyle },
651 { OUTER_SK_BLUR_STYLE, kOuter_SkBlurStyle },
652 { INNER_SK_BLUR_STYLE, kInner_SkBlurStyle },
653 };
654
find_blurstyle(sk_blurstyle_t csrc,SkBlurStyle * dst)655 static bool find_blurstyle(sk_blurstyle_t csrc, SkBlurStyle* dst) {
656 for (size_t i = 0; i < SK_ARRAY_COUNT(gBlurStylePairs); ++i) {
657 if (gBlurStylePairs[i].fC == csrc) {
658 if (dst) {
659 *dst = gBlurStylePairs[i].fSk;
660 }
661 return true;
662 }
663 }
664 return false;
665 }
666
sk_maskfilter_ref(sk_maskfilter_t * cfilter)667 void sk_maskfilter_ref(sk_maskfilter_t* cfilter) {
668 SkSafeRef(AsMaskFilter(cfilter));
669 }
670
sk_maskfilter_unref(sk_maskfilter_t * cfilter)671 void sk_maskfilter_unref(sk_maskfilter_t* cfilter) {
672 SkSafeUnref(AsMaskFilter(cfilter));
673 }
674
sk_maskfilter_new_blur(sk_blurstyle_t cstyle,float sigma)675 sk_maskfilter_t* sk_maskfilter_new_blur(sk_blurstyle_t cstyle, float sigma) {
676 SkBlurStyle style;
677 if (!find_blurstyle(cstyle, &style)) {
678 return NULL;
679 }
680 return ToMaskFilter(SkBlurMaskFilter::Create(style, sigma));
681 }
682
683 ///////////////////////////////////////////////////////////////////////////////////////////
684
sk_data_new_with_copy(const void * src,size_t length)685 sk_data_t* sk_data_new_with_copy(const void* src, size_t length) {
686 return ToData(SkData::NewWithCopy(src, length));
687 }
688
sk_data_new_from_malloc(const void * memory,size_t length)689 sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) {
690 return ToData(SkData::NewFromMalloc(memory, length));
691 }
692
sk_data_new_subset(const sk_data_t * csrc,size_t offset,size_t length)693 sk_data_t* sk_data_new_subset(const sk_data_t* csrc, size_t offset, size_t length) {
694 return ToData(SkData::NewSubset(AsData(csrc), offset, length));
695 }
696
sk_data_ref(const sk_data_t * cdata)697 void sk_data_ref(const sk_data_t* cdata) {
698 SkSafeRef(AsData(cdata));
699 }
700
sk_data_unref(const sk_data_t * cdata)701 void sk_data_unref(const sk_data_t* cdata) {
702 SkSafeUnref(AsData(cdata));
703 }
704
sk_data_get_size(const sk_data_t * cdata)705 size_t sk_data_get_size(const sk_data_t* cdata) {
706 return AsData(cdata)->size();
707 }
708
sk_data_get_data(const sk_data_t * cdata)709 const void* sk_data_get_data(const sk_data_t* cdata) {
710 return AsData(cdata)->data();
711 }
712
713 ///////////////////////////////////////////////////////////////////////////////////////////
714