blob: 75a1643775fd26387c1e8c22d4c53fc7c82f8305 [file] [log] [blame]
reed8e474782014-10-06 11:00:51 -07001/*
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
reed938dfba2014-10-06 06:08:16 -07008#include "SkCanvas.h"
reedb2a5d7e2014-12-25 14:16:21 -08009#include "SkData.h"
reed938dfba2014-10-06 06:08:16 -070010#include "SkImage.h"
reed0eafc9b2014-12-23 14:11:11 -080011#include "SkMaskFilter.h"
reed938dfba2014-10-06 06:08:16 -070012#include "SkMatrix.h"
13#include "SkPaint.h"
14#include "SkPath.h"
reede3719892014-12-22 17:46:00 -080015#include "SkPictureRecorder.h"
reed938dfba2014-10-06 06:08:16 -070016#include "SkSurface.h"
17
halcanary219f18f2015-09-01 10:01:38 -070018#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
reede3323962014-10-24 11:16:19 -070026const 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
36const 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
45static 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
57static 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
69static 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
81static 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;
reed938dfba2014-10-06 06:08:16 -070097}
98
robertphillips702edbd2015-06-23 06:26:08 -070099const 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
111static 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
abarth9503ac72014-12-02 15:47:33 -0800123static 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
reed73c25012014-11-17 06:15:42 -0800129const 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
137static 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
reedb2a5d7e2014-12-25 14:16:21 -0800149static SkData* AsData(const sk_data_t* cdata) {
150 return reinterpret_cast<SkData*>(const_cast<sk_data_t*>(cdata));
151}
152
153static sk_data_t* ToData(SkData* data) {
154 return reinterpret_cast<sk_data_t*>(data);
155}
156
reede3719892014-12-22 17:46:00 -0800157static sk_rect_t ToRect(const SkRect& rect) {
158 return reinterpret_cast<const sk_rect_t&>(rect);
159}
160
reed8e474782014-10-06 11:00:51 -0700161static const SkRect& AsRect(const sk_rect_t& crect) {
162 return reinterpret_cast<const SkRect&>(crect);
163}
164
165static const SkPath& AsPath(const sk_path_t& cpath) {
166 return reinterpret_cast<const SkPath&>(cpath);
167}
168
robertphillipsa624e872014-10-08 06:04:35 -0700169static SkPath* as_path(sk_path_t* cpath) {
170 return reinterpret_cast<SkPath*>(cpath);
171}
172
reed8e474782014-10-06 11:00:51 -0700173static const SkImage* AsImage(const sk_image_t* cimage) {
174 return reinterpret_cast<const SkImage*>(cimage);
175}
reed938dfba2014-10-06 06:08:16 -0700176
reedb2a5d7e2014-12-25 14:16:21 -0800177static sk_image_t* ToImage(SkImage* cimage) {
178 return reinterpret_cast<sk_image_t*>(cimage);
179}
180
reede3719892014-12-22 17:46:00 -0800181static sk_canvas_t* ToCanvas(SkCanvas* canvas) {
182 return reinterpret_cast<sk_canvas_t*>(canvas);
183}
184
reed8e474782014-10-06 11:00:51 -0700185static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
186 return reinterpret_cast<SkCanvas*>(ccanvas);
187}
188
reede3719892014-12-22 17:46:00 -0800189static SkPictureRecorder* AsPictureRecorder(sk_picture_recorder_t* crec) {
190 return reinterpret_cast<SkPictureRecorder*>(crec);
191}
192
193static sk_picture_recorder_t* ToPictureRecorder(SkPictureRecorder* rec) {
194 return reinterpret_cast<sk_picture_recorder_t*>(rec);
195}
196
197static const SkPicture* AsPicture(const sk_picture_t* cpic) {
198 return reinterpret_cast<const SkPicture*>(cpic);
199}
200
201static SkPicture* AsPicture(sk_picture_t* cpic) {
202 return reinterpret_cast<SkPicture*>(cpic);
203}
204
205static sk_picture_t* ToPicture(SkPicture* pic) {
206 return reinterpret_cast<sk_picture_t*>(pic);
207}
208
reed8e474782014-10-06 11:00:51 -0700209///////////////////////////////////////////////////////////////////////////////////////////
210
reede3323962014-10-24 11:16:19 -0700211sk_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
reed8e474782014-10-06 11:00:51 -0700221sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
222 size_t rowBytes) {
reede3323962014-10-24 11:16:19 -0700223 SkImageInfo info;
224 if (!from_c_info(*cinfo, &info)) {
225 return NULL;
226 }
reed7fb4f8b2016-03-11 04:33:52 -0800227 return (sk_image_t*)SkImage::MakeRasterCopy(SkPixmap(info, pixels, rowBytes)).release();
reed8e474782014-10-06 11:00:51 -0700228}
229
reed871872f2015-06-22 12:48:26 -0700230sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata, const sk_irect_t* subset) {
reed7fb4f8b2016-03-11 04:33:52 -0800231 return ToImage(SkImage::MakeFromEncoded(sk_ref_sp(AsData(cdata)),
232 reinterpret_cast<const SkIRect*>(subset)).release());
reedb2a5d7e2014-12-25 14:16:21 -0800233}
234
235sk_data_t* sk_image_encode(const sk_image_t* cimage) {
236 return ToData(AsImage(cimage)->encode());
237}
238
reed8e474782014-10-06 11:00:51 -0700239void sk_image_ref(const sk_image_t* cimage) {
240 AsImage(cimage)->ref();
241}
242
243void sk_image_unref(const sk_image_t* cimage) {
244 AsImage(cimage)->unref();
245}
246
247int sk_image_get_width(const sk_image_t* cimage) {
248 return AsImage(cimage)->width();
249}
250
251int sk_image_get_height(const sk_image_t* cimage) {
252 return AsImage(cimage)->height();
253}
254
255uint32_t sk_image_get_unique_id(const sk_image_t* cimage) {
256 return AsImage(cimage)->uniqueID();
257}
258
259///////////////////////////////////////////////////////////////////////////////////////////
260
halcanary385fe4d2015-08-26 13:07:48 -0700261sk_path_t* sk_path_new() { return (sk_path_t*)new SkPath; }
robertphillipsa624e872014-10-08 06:04:35 -0700262
halcanary385fe4d2015-08-26 13:07:48 -0700263void sk_path_delete(sk_path_t* cpath) { delete as_path(cpath); }
robertphillipsa624e872014-10-08 06:04:35 -0700264
265void sk_path_move_to(sk_path_t* cpath, float x, float y) {
266 as_path(cpath)->moveTo(x, y);
267}
268
269void sk_path_line_to(sk_path_t* cpath, float x, float y) {
270 as_path(cpath)->lineTo(x, y);
271}
272
273void 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
reed73c25012014-11-17 06:15:42 -0800277void 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
281void 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
robertphillipsa624e872014-10-08 06:04:35 -0700285void sk_path_close(sk_path_t* cpath) {
286 as_path(cpath)->close();
287}
288
reed73c25012014-11-17 06:15:42 -0800289void 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
297void 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
305bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) {
306 const SkPath& path = AsPath(*cpath);
reed73c25012014-11-17 06:15:42 -0800307
308 if (path.isEmpty()) {
reede3719892014-12-22 17:46:00 -0800309 if (crect) {
310 *crect = ToRect(SkRect::MakeEmpty());
reed73c25012014-11-17 06:15:42 -0800311 }
312 return false;
313 }
reede3719892014-12-22 17:46:00 -0800314
315 if (crect) {
316 *crect = ToRect(path.getBounds());
317 }
reed73c25012014-11-17 06:15:42 -0800318 return true;
319}
320
robertphillipsa624e872014-10-08 06:04:35 -0700321///////////////////////////////////////////////////////////////////////////////////////////
322
reed938dfba2014-10-06 06:08:16 -0700323void sk_canvas_save(sk_canvas_t* ccanvas) {
324 AsCanvas(ccanvas)->save();
325}
326
327void 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
331void sk_canvas_restore(sk_canvas_t* ccanvas) {
332 AsCanvas(ccanvas)->restore();
333}
334
335void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
336 AsCanvas(ccanvas)->translate(dx, dy);
337}
338
339void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
reed8e474782014-10-06 11:00:51 -0700340 AsCanvas(ccanvas)->scale(sx, sy);
reed938dfba2014-10-06 06:08:16 -0700341}
342
abarth2fc6ea62014-12-01 14:04:03 -0800343void sk_canvas_rotate_degress(sk_canvas_t* ccanvas, float degrees) {
344 AsCanvas(ccanvas)->rotate(degrees);
345}
346
347void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) {
348 AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians));
349}
350
351void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) {
352 AsCanvas(ccanvas)->skew(sx, sy);
353}
354
reede3719892014-12-22 17:46:00 -0800355void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) {
abarth9503ac72014-12-02 15:47:33 -0800356 SkASSERT(cmatrix);
357 SkMatrix matrix;
358 from_c_matrix(cmatrix, &matrix);
359 AsCanvas(ccanvas)->concat(matrix);
360}
361
reede3719892014-12-22 17:46:00 -0800362void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) {
363 AsCanvas(ccanvas)->clipRect(AsRect(*crect));
364}
365
366void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) {
367 AsCanvas(ccanvas)->clipPath(AsPath(*cpath));
368}
369
reed938dfba2014-10-06 06:08:16 -0700370void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
371 AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
372}
373
374void 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
reed4a7940b2016-02-09 13:25:51 -0800378void 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
reed938dfba2014-10-06 06:08:16 -0700383void 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
387void 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
391void 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
reede3719892014-12-22 17:46:00 -0800396void 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) {
reede47829b2015-08-06 10:02:53 -0700399 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 }
reede3719892014-12-22 17:46:00 -0800409}
410
411void 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
reed938dfba2014-10-06 06:08:16 -0700422///////////////////////////////////////////////////////////////////////////////////////////
423
robertphillips702edbd2015-06-23 06:26:08 -0700424sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo,
425 const sk_surfaceprops_t* props) {
reede3323962014-10-24 11:16:19 -0700426 SkImageInfo info;
427 if (!from_c_info(*cinfo, &info)) {
428 return NULL;
429 }
robertphillips702edbd2015-06-23 06:26:08 -0700430 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);
reed938dfba2014-10-06 06:08:16 -0700437}
438
reed8e474782014-10-06 11:00:51 -0700439sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
robertphillips702edbd2015-06-23 06:26:08 -0700440 size_t rowBytes,
441 const sk_surfaceprops_t* props) {
reede3323962014-10-24 11:16:19 -0700442 SkImageInfo info;
443 if (!from_c_info(*cinfo, &info)) {
444 return NULL;
445 }
robertphillips702edbd2015-06-23 06:26:08 -0700446 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);
reed938dfba2014-10-06 06:08:16 -0700453}
454
reedafa278e2014-11-24 19:11:48 -0800455void sk_surface_unref(sk_surface_t* csurf) {
456 SkSafeUnref((SkSurface*)csurf);
reed938dfba2014-10-06 06:08:16 -0700457}
458
459sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
460 SkSurface* surf = (SkSurface*)csurf;
reed8e474782014-10-06 11:00:51 -0700461 return (sk_canvas_t*)surf->getCanvas();
reed938dfba2014-10-06 06:08:16 -0700462}
463
464sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
465 SkSurface* surf = (SkSurface*)csurf;
reed9ce9d672016-03-17 10:51:11 -0700466 return (sk_image_t*)surf->makeImageSnapshot().release();
reed938dfba2014-10-06 06:08:16 -0700467}
468
reedafa278e2014-11-24 19:11:48 -0800469///////////////////////////////////////////////////////////////////////////////////////////
reed938dfba2014-10-06 06:08:16 -0700470
reede3719892014-12-22 17:46:00 -0800471sk_picture_recorder_t* sk_picture_recorder_new() {
472 return ToPictureRecorder(new SkPictureRecorder);
473}
474
475void sk_picture_recorder_delete(sk_picture_recorder_t* crec) {
476 delete AsPictureRecorder(crec);
477}
478
479sk_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
484sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) {
reedca2622b2016-03-18 07:25:55 -0700485 return ToPicture(AsPictureRecorder(crec)->finishRecordingAsPicture().release());
reede3719892014-12-22 17:46:00 -0800486}
487
488void sk_picture_ref(sk_picture_t* cpic) {
489 SkSafeRef(AsPicture(cpic));
490}
491
492void sk_picture_unref(sk_picture_t* cpic) {
493 SkSafeUnref(AsPicture(cpic));
494}
495
496uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) {
497 return AsPicture(cpic)->uniqueID();
498}
499
500sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) {
501 return ToRect(AsPicture(cpic)->cullRect());
502}
503
504///////////////////////////////////////////////////////////////////////////////////////////
505
reedafa278e2014-11-24 19:11:48 -0800506#include "../../include/effects/SkGradientShader.h"
507#include "sk_shader.h"
508
509const 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
518static 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
530void sk_shader_ref(sk_shader_t* cshader) {
531 SkSafeRef(AsShader(cshader));
532}
533
534void sk_shader_unref(sk_shader_t* cshader) {
535 SkSafeUnref(AsShader(cshader));
536}
537
538sk_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) {
abarth9503ac72014-12-02 15:47:33 -0800550 from_c_matrix(cmatrix, &matrix);
reedafa278e2014-11-24 19:11:48 -0800551 } else {
552 matrix.setIdentity();
553 }
reed8a21c9f2016-03-08 18:50:00 -0800554 return (sk_shader_t*)SkGradientShader::MakeLinear(reinterpret_cast<const SkPoint*>(pts),
555 reinterpret_cast<const SkColor*>(colors),
556 colorPos, colorCount,
557 mode, 0, &matrix).release();
reedafa278e2014-11-24 19:11:48 -0800558}
559
halcanary65cc3e42015-08-12 07:37:34 -0700560static const SkPoint& to_skpoint(const sk_point_t& p) {
561 return reinterpret_cast<const SkPoint&>(p);
562}
563
564sk_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);
reed8a21c9f2016-03-08 18:50:00 -0800582 return (sk_shader_t*)SkGradientShader::MakeRadial(center, (SkScalar)radius,
583 reinterpret_cast<const SkColor*>(colors),
584 reinterpret_cast<const SkScalar*>(colorPos),
585 colorCount, mode, 0, &matrix).release();
halcanary65cc3e42015-08-12 07:37:34 -0700586}
587
588sk_shader_t* sk_shader_new_sweep_gradient(const sk_point_t* ccenter,
589 const sk_color_t colors[],
590 const float colorPos[],
591 int colorCount,
592 const sk_matrix_t* cmatrix) {
593 SkMatrix matrix;
594 if (cmatrix) {
595 from_c_matrix(cmatrix, &matrix);
596 } else {
597 matrix.setIdentity();
598 }
reed8a21c9f2016-03-08 18:50:00 -0800599 return (sk_shader_t*)SkGradientShader::MakeSweep((SkScalar)(ccenter->x),
600 (SkScalar)(ccenter->y),
601 reinterpret_cast<const SkColor*>(colors),
602 reinterpret_cast<const SkScalar*>(colorPos),
603 colorCount, 0, &matrix).release();
halcanary65cc3e42015-08-12 07:37:34 -0700604}
605
606sk_shader_t* sk_shader_new_two_point_conical_gradient(const sk_point_t* start,
607 float startRadius,
608 const sk_point_t* end,
609 float endRadius,
610 const sk_color_t colors[],
611 const float colorPos[],
612 int colorCount,
613 sk_shader_tilemode_t cmode,
614 const sk_matrix_t* cmatrix) {
615 SkShader::TileMode mode;
616 if (!from_c_tilemode(cmode, &mode)) {
617 return NULL;
618 }
619 SkMatrix matrix;
620 if (cmatrix) {
621 from_c_matrix(cmatrix, &matrix);
622 } else {
623 matrix.setIdentity();
624 }
625 SkPoint skstart = to_skpoint(*start);
626 SkPoint skend = to_skpoint(*end);
reed8a21c9f2016-03-08 18:50:00 -0800627 return (sk_shader_t*)SkGradientShader::MakeTwoPointConical(skstart, (SkScalar)startRadius,
628 skend, (SkScalar)endRadius,
629 reinterpret_cast<const SkColor*>(colors),
630 reinterpret_cast<const SkScalar*>(colorPos),
631 colorCount, mode, 0, &matrix).release();
halcanary65cc3e42015-08-12 07:37:34 -0700632}
633
reedafa278e2014-11-24 19:11:48 -0800634///////////////////////////////////////////////////////////////////////////////////////////
reed0eafc9b2014-12-23 14:11:11 -0800635
636#include "../../include/effects/SkBlurMaskFilter.h"
637#include "sk_maskfilter.h"
638
639const struct {
640 sk_blurstyle_t fC;
641 SkBlurStyle fSk;
642} gBlurStylePairs[] = {
643 { NORMAL_SK_BLUR_STYLE, kNormal_SkBlurStyle },
644 { SOLID_SK_BLUR_STYLE, kSolid_SkBlurStyle },
645 { OUTER_SK_BLUR_STYLE, kOuter_SkBlurStyle },
646 { INNER_SK_BLUR_STYLE, kInner_SkBlurStyle },
647};
648
649static bool find_blurstyle(sk_blurstyle_t csrc, SkBlurStyle* dst) {
650 for (size_t i = 0; i < SK_ARRAY_COUNT(gBlurStylePairs); ++i) {
651 if (gBlurStylePairs[i].fC == csrc) {
652 if (dst) {
653 *dst = gBlurStylePairs[i].fSk;
654 }
655 return true;
656 }
657 }
658 return false;
659}
660
661void sk_maskfilter_ref(sk_maskfilter_t* cfilter) {
662 SkSafeRef(AsMaskFilter(cfilter));
663}
664
665void sk_maskfilter_unref(sk_maskfilter_t* cfilter) {
666 SkSafeUnref(AsMaskFilter(cfilter));
667}
668
669sk_maskfilter_t* sk_maskfilter_new_blur(sk_blurstyle_t cstyle, float sigma) {
670 SkBlurStyle style;
671 if (!find_blurstyle(cstyle, &style)) {
672 return NULL;
673 }
674 return ToMaskFilter(SkBlurMaskFilter::Create(style, sigma));
675}
676
677///////////////////////////////////////////////////////////////////////////////////////////
reedb2a5d7e2014-12-25 14:16:21 -0800678
679sk_data_t* sk_data_new_with_copy(const void* src, size_t length) {
reedfde05112016-03-11 13:02:28 -0800680 return ToData(SkData::MakeWithCopy(src, length).release());
reedb2a5d7e2014-12-25 14:16:21 -0800681}
682
683sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) {
reedfde05112016-03-11 13:02:28 -0800684 return ToData(SkData::MakeFromMalloc(memory, length).release());
reedb2a5d7e2014-12-25 14:16:21 -0800685}
686
687sk_data_t* sk_data_new_subset(const sk_data_t* csrc, size_t offset, size_t length) {
reedfde05112016-03-11 13:02:28 -0800688 return ToData(SkData::MakeSubset(AsData(csrc), offset, length).release());
reedb2a5d7e2014-12-25 14:16:21 -0800689}
690
691void sk_data_ref(const sk_data_t* cdata) {
692 SkSafeRef(AsData(cdata));
693}
694
695void sk_data_unref(const sk_data_t* cdata) {
696 SkSafeUnref(AsData(cdata));
697}
698
699size_t sk_data_get_size(const sk_data_t* cdata) {
700 return AsData(cdata)->size();
701}
702
703const void* sk_data_get_data(const sk_data_t* cdata) {
704 return AsData(cdata)->data();
705}
706
707///////////////////////////////////////////////////////////////////////////////////////////