blob: b8fbc63f63f4200457b28c8a8262dd320b0aae2b [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 }
227 return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes);
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) {
231 return ToImage(SkImage::NewFromEncoded(AsData(cdata),
232 reinterpret_cast<const SkIRect*>(subset)));
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
378void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
379 AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
380}
381
382void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
383 AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
384}
385
386void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
387 const sk_paint_t* cpaint) {
388 AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
389}
390
reede3719892014-12-22 17:46:00 -0800391void sk_canvas_draw_image_rect(sk_canvas_t* ccanvas, const sk_image_t* cimage,
392 const sk_rect_t* csrcR, const sk_rect_t* cdstR,
393 const sk_paint_t* cpaint) {
reede47829b2015-08-06 10:02:53 -0700394 SkCanvas* canvas = AsCanvas(ccanvas);
395 const SkImage* image = AsImage(cimage);
396 const SkRect& dst = AsRect(*cdstR);
397 const SkPaint* paint = AsPaint(cpaint);
398
399 if (csrcR) {
400 canvas->drawImageRect(image, AsRect(*csrcR), dst, paint);
401 } else {
402 canvas->drawImageRect(image, dst, paint);
403 }
reede3719892014-12-22 17:46:00 -0800404}
405
406void sk_canvas_draw_picture(sk_canvas_t* ccanvas, const sk_picture_t* cpicture,
407 const sk_matrix_t* cmatrix, const sk_paint_t* cpaint) {
408 const SkMatrix* matrixPtr = NULL;
409 SkMatrix matrix;
410 if (cmatrix) {
411 from_c_matrix(cmatrix, &matrix);
412 matrixPtr = &matrix;
413 }
414 AsCanvas(ccanvas)->drawPicture(AsPicture(cpicture), matrixPtr, AsPaint(cpaint));
415}
416
reed938dfba2014-10-06 06:08:16 -0700417///////////////////////////////////////////////////////////////////////////////////////////
418
robertphillips702edbd2015-06-23 06:26:08 -0700419sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo,
420 const sk_surfaceprops_t* props) {
reede3323962014-10-24 11:16:19 -0700421 SkImageInfo info;
422 if (!from_c_info(*cinfo, &info)) {
423 return NULL;
424 }
robertphillips702edbd2015-06-23 06:26:08 -0700425 SkPixelGeometry geo = kUnknown_SkPixelGeometry;
426 if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
427 return NULL;
428 }
429
430 SkSurfaceProps surfProps(0, geo);
431 return (sk_surface_t*)SkSurface::NewRaster(info, &surfProps);
reed938dfba2014-10-06 06:08:16 -0700432}
433
reed8e474782014-10-06 11:00:51 -0700434sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
robertphillips702edbd2015-06-23 06:26:08 -0700435 size_t rowBytes,
436 const sk_surfaceprops_t* props) {
reede3323962014-10-24 11:16:19 -0700437 SkImageInfo info;
438 if (!from_c_info(*cinfo, &info)) {
439 return NULL;
440 }
robertphillips702edbd2015-06-23 06:26:08 -0700441 SkPixelGeometry geo = kUnknown_SkPixelGeometry;
442 if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
443 return NULL;
444 }
445
446 SkSurfaceProps surfProps(0, geo);
447 return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes, &surfProps);
reed938dfba2014-10-06 06:08:16 -0700448}
449
reedafa278e2014-11-24 19:11:48 -0800450void sk_surface_unref(sk_surface_t* csurf) {
451 SkSafeUnref((SkSurface*)csurf);
reed938dfba2014-10-06 06:08:16 -0700452}
453
454sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
455 SkSurface* surf = (SkSurface*)csurf;
reed8e474782014-10-06 11:00:51 -0700456 return (sk_canvas_t*)surf->getCanvas();
reed938dfba2014-10-06 06:08:16 -0700457}
458
459sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
460 SkSurface* surf = (SkSurface*)csurf;
reed8e474782014-10-06 11:00:51 -0700461 return (sk_image_t*)surf->newImageSnapshot();
reed938dfba2014-10-06 06:08:16 -0700462}
463
reedafa278e2014-11-24 19:11:48 -0800464///////////////////////////////////////////////////////////////////////////////////////////
reed938dfba2014-10-06 06:08:16 -0700465
reede3719892014-12-22 17:46:00 -0800466sk_picture_recorder_t* sk_picture_recorder_new() {
467 return ToPictureRecorder(new SkPictureRecorder);
468}
469
470void sk_picture_recorder_delete(sk_picture_recorder_t* crec) {
471 delete AsPictureRecorder(crec);
472}
473
474sk_canvas_t* sk_picture_recorder_begin_recording(sk_picture_recorder_t* crec,
475 const sk_rect_t* cbounds) {
476 return ToCanvas(AsPictureRecorder(crec)->beginRecording(AsRect(*cbounds)));
477}
478
479sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) {
480 return ToPicture(AsPictureRecorder(crec)->endRecording());
481}
482
483void sk_picture_ref(sk_picture_t* cpic) {
484 SkSafeRef(AsPicture(cpic));
485}
486
487void sk_picture_unref(sk_picture_t* cpic) {
488 SkSafeUnref(AsPicture(cpic));
489}
490
491uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) {
492 return AsPicture(cpic)->uniqueID();
493}
494
495sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) {
496 return ToRect(AsPicture(cpic)->cullRect());
497}
498
499///////////////////////////////////////////////////////////////////////////////////////////
500
reedafa278e2014-11-24 19:11:48 -0800501#include "../../include/effects/SkGradientShader.h"
502#include "sk_shader.h"
503
504const struct {
505 sk_shader_tilemode_t fC;
506 SkShader::TileMode fSK;
507} gTileModeMap[] = {
508 { CLAMP_SK_SHADER_TILEMODE, SkShader::kClamp_TileMode },
509 { REPEAT_SK_SHADER_TILEMODE, SkShader::kRepeat_TileMode },
510 { MIRROR_SK_SHADER_TILEMODE, SkShader::kMirror_TileMode },
511};
512
513static bool from_c_tilemode(sk_shader_tilemode_t cMode, SkShader::TileMode* skMode) {
514 for (size_t i = 0; i < SK_ARRAY_COUNT(gTileModeMap); ++i) {
515 if (cMode == gTileModeMap[i].fC) {
516 if (skMode) {
517 *skMode = gTileModeMap[i].fSK;
518 }
519 return true;
520 }
521 }
522 return false;
523}
524
525void sk_shader_ref(sk_shader_t* cshader) {
526 SkSafeRef(AsShader(cshader));
527}
528
529void sk_shader_unref(sk_shader_t* cshader) {
530 SkSafeUnref(AsShader(cshader));
531}
532
533sk_shader_t* sk_shader_new_linear_gradient(const sk_point_t pts[2],
534 const sk_color_t colors[],
535 const float colorPos[],
536 int colorCount,
537 sk_shader_tilemode_t cmode,
538 const sk_matrix_t* cmatrix) {
539 SkShader::TileMode mode;
540 if (!from_c_tilemode(cmode, &mode)) {
541 return NULL;
542 }
543 SkMatrix matrix;
544 if (cmatrix) {
abarth9503ac72014-12-02 15:47:33 -0800545 from_c_matrix(cmatrix, &matrix);
reedafa278e2014-11-24 19:11:48 -0800546 } else {
547 matrix.setIdentity();
548 }
549 SkShader* s = SkGradientShader::CreateLinear(reinterpret_cast<const SkPoint*>(pts),
550 reinterpret_cast<const SkColor*>(colors),
551 colorPos, colorCount, mode, 0, &matrix);
552 return (sk_shader_t*)s;
553}
554
halcanary65cc3e42015-08-12 07:37:34 -0700555static const SkPoint& to_skpoint(const sk_point_t& p) {
556 return reinterpret_cast<const SkPoint&>(p);
557}
558
559sk_shader_t* sk_shader_new_radial_gradient(const sk_point_t* ccenter,
560 float radius,
561 const sk_color_t colors[],
562 const float colorPos[],
563 int colorCount,
564 sk_shader_tilemode_t cmode,
565 const sk_matrix_t* cmatrix) {
566 SkShader::TileMode mode;
567 if (!from_c_tilemode(cmode, &mode)) {
568 return NULL;
569 }
570 SkMatrix matrix;
571 if (cmatrix) {
572 from_c_matrix(cmatrix, &matrix);
573 } else {
574 matrix.setIdentity();
575 }
576 SkPoint center = to_skpoint(*ccenter);
577 SkShader* s = SkGradientShader::CreateRadial(
578 center, (SkScalar)radius,
579 reinterpret_cast<const SkColor*>(colors),
580 reinterpret_cast<const SkScalar*>(colorPos),
581 colorCount, mode, 0, &matrix);
582 return (sk_shader_t*)s;
583}
584
585sk_shader_t* sk_shader_new_sweep_gradient(const sk_point_t* ccenter,
586 const sk_color_t colors[],
587 const float colorPos[],
588 int colorCount,
589 const sk_matrix_t* cmatrix) {
590 SkMatrix matrix;
591 if (cmatrix) {
592 from_c_matrix(cmatrix, &matrix);
593 } else {
594 matrix.setIdentity();
595 }
596 SkShader* s = SkGradientShader::CreateSweep(
597 (SkScalar)(ccenter->x),
598 (SkScalar)(ccenter->y),
599 reinterpret_cast<const SkColor*>(colors),
600 reinterpret_cast<const SkScalar*>(colorPos),
601 colorCount, 0, &matrix);
602 return (sk_shader_t*)s;
603}
604
605sk_shader_t* sk_shader_new_two_point_conical_gradient(const sk_point_t* start,
606 float startRadius,
607 const sk_point_t* end,
608 float endRadius,
609 const sk_color_t colors[],
610 const float colorPos[],
611 int colorCount,
612 sk_shader_tilemode_t cmode,
613 const sk_matrix_t* cmatrix) {
614 SkShader::TileMode mode;
615 if (!from_c_tilemode(cmode, &mode)) {
616 return NULL;
617 }
618 SkMatrix matrix;
619 if (cmatrix) {
620 from_c_matrix(cmatrix, &matrix);
621 } else {
622 matrix.setIdentity();
623 }
624 SkPoint skstart = to_skpoint(*start);
625 SkPoint skend = to_skpoint(*end);
626 SkShader* s = SkGradientShader::CreateTwoPointConical(
627 skstart, (SkScalar)startRadius,
628 skend, (SkScalar)endRadius,
629 reinterpret_cast<const SkColor*>(colors),
630 reinterpret_cast<const SkScalar*>(colorPos),
631 colorCount, mode, 0, &matrix);
632 return (sk_shader_t*)s;
633}
634
reedafa278e2014-11-24 19:11:48 -0800635///////////////////////////////////////////////////////////////////////////////////////////
reed0eafc9b2014-12-23 14:11:11 -0800636
637#include "../../include/effects/SkBlurMaskFilter.h"
638#include "sk_maskfilter.h"
639
640const struct {
641 sk_blurstyle_t fC;
642 SkBlurStyle fSk;
643} gBlurStylePairs[] = {
644 { NORMAL_SK_BLUR_STYLE, kNormal_SkBlurStyle },
645 { SOLID_SK_BLUR_STYLE, kSolid_SkBlurStyle },
646 { OUTER_SK_BLUR_STYLE, kOuter_SkBlurStyle },
647 { INNER_SK_BLUR_STYLE, kInner_SkBlurStyle },
648};
649
650static bool find_blurstyle(sk_blurstyle_t csrc, SkBlurStyle* dst) {
651 for (size_t i = 0; i < SK_ARRAY_COUNT(gBlurStylePairs); ++i) {
652 if (gBlurStylePairs[i].fC == csrc) {
653 if (dst) {
654 *dst = gBlurStylePairs[i].fSk;
655 }
656 return true;
657 }
658 }
659 return false;
660}
661
662void sk_maskfilter_ref(sk_maskfilter_t* cfilter) {
663 SkSafeRef(AsMaskFilter(cfilter));
664}
665
666void sk_maskfilter_unref(sk_maskfilter_t* cfilter) {
667 SkSafeUnref(AsMaskFilter(cfilter));
668}
669
670sk_maskfilter_t* sk_maskfilter_new_blur(sk_blurstyle_t cstyle, float sigma) {
671 SkBlurStyle style;
672 if (!find_blurstyle(cstyle, &style)) {
673 return NULL;
674 }
675 return ToMaskFilter(SkBlurMaskFilter::Create(style, sigma));
676}
677
678///////////////////////////////////////////////////////////////////////////////////////////
reedb2a5d7e2014-12-25 14:16:21 -0800679
680sk_data_t* sk_data_new_with_copy(const void* src, size_t length) {
681 return ToData(SkData::NewWithCopy(src, length));
682}
683
684sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) {
685 return ToData(SkData::NewFromMalloc(memory, length));
686}
687
688sk_data_t* sk_data_new_subset(const sk_data_t* csrc, size_t offset, size_t length) {
689 return ToData(SkData::NewSubset(AsData(csrc), offset, length));
690}
691
692void sk_data_ref(const sk_data_t* cdata) {
693 SkSafeRef(AsData(cdata));
694}
695
696void sk_data_unref(const sk_data_t* cdata) {
697 SkSafeUnref(AsData(cdata));
698}
699
700size_t sk_data_get_size(const sk_data_t* cdata) {
701 return AsData(cdata)->size();
702}
703
704const void* sk_data_get_data(const sk_data_t* cdata) {
705 return AsData(cdata)->data();
706}
707
708///////////////////////////////////////////////////////////////////////////////////////////