blob: 84a9c541c2331161ef681b07510aadc25c5ddff3 [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
reed73c25012014-11-17 06:15:42 -08008#include "sk_canvas.h"
reedb2a5d7e2014-12-25 14:16:21 -08009#include "sk_data.h"
reed73c25012014-11-17 06:15:42 -080010#include "sk_image.h"
11#include "sk_paint.h"
12#include "sk_path.h"
reed938dfba2014-10-06 06:08:16 -070013#include "sk_surface.h"
reedf5872d22015-01-10 17:59:31 -080014#include "sk_types_priv.h"
reed938dfba2014-10-06 06:08:16 -070015
16#include "SkCanvas.h"
reedb2a5d7e2014-12-25 14:16:21 -080017#include "SkData.h"
reed938dfba2014-10-06 06:08:16 -070018#include "SkImage.h"
reed0eafc9b2014-12-23 14:11:11 -080019#include "SkMaskFilter.h"
reed938dfba2014-10-06 06:08:16 -070020#include "SkMatrix.h"
21#include "SkPaint.h"
22#include "SkPath.h"
reede3719892014-12-22 17:46:00 -080023#include "SkPictureRecorder.h"
reed938dfba2014-10-06 06:08:16 -070024#include "SkSurface.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
robertphillipsa624e872014-10-08 06:04:35 -0700261sk_path_t* sk_path_new() {
262 return (sk_path_t*)SkNEW(SkPath);
263}
264
265void sk_path_delete(sk_path_t* cpath) {
266 SkDELETE(as_path(cpath));
267}
268
269void sk_path_move_to(sk_path_t* cpath, float x, float y) {
270 as_path(cpath)->moveTo(x, y);
271}
272
273void sk_path_line_to(sk_path_t* cpath, float x, float y) {
274 as_path(cpath)->lineTo(x, y);
275}
276
277void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) {
278 as_path(cpath)->quadTo(x0, y0, x1, y1);
279}
280
reed73c25012014-11-17 06:15:42 -0800281void sk_path_conic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float w) {
282 as_path(cpath)->conicTo(x0, y0, x1, y1, w);
283}
284
285void sk_path_cubic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float x2, float y2) {
286 as_path(cpath)->cubicTo(x0, y0, x1, y1, x2, y2);
287}
288
robertphillipsa624e872014-10-08 06:04:35 -0700289void sk_path_close(sk_path_t* cpath) {
290 as_path(cpath)->close();
291}
292
reed73c25012014-11-17 06:15:42 -0800293void sk_path_add_rect(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) {
294 SkPath::Direction dir;
295 if (!from_c_path_direction(cdir, &dir)) {
296 return;
297 }
298 as_path(cpath)->addRect(AsRect(*crect), dir);
299}
300
301void sk_path_add_oval(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) {
302 SkPath::Direction dir;
303 if (!from_c_path_direction(cdir, &dir)) {
304 return;
305 }
306 as_path(cpath)->addOval(AsRect(*crect), dir);
307}
308
309bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) {
310 const SkPath& path = AsPath(*cpath);
reed73c25012014-11-17 06:15:42 -0800311
312 if (path.isEmpty()) {
reede3719892014-12-22 17:46:00 -0800313 if (crect) {
314 *crect = ToRect(SkRect::MakeEmpty());
reed73c25012014-11-17 06:15:42 -0800315 }
316 return false;
317 }
reede3719892014-12-22 17:46:00 -0800318
319 if (crect) {
320 *crect = ToRect(path.getBounds());
321 }
reed73c25012014-11-17 06:15:42 -0800322 return true;
323}
324
robertphillipsa624e872014-10-08 06:04:35 -0700325///////////////////////////////////////////////////////////////////////////////////////////
326
reed938dfba2014-10-06 06:08:16 -0700327void sk_canvas_save(sk_canvas_t* ccanvas) {
328 AsCanvas(ccanvas)->save();
329}
330
331void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
332 AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
333}
334
335void sk_canvas_restore(sk_canvas_t* ccanvas) {
336 AsCanvas(ccanvas)->restore();
337}
338
339void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
340 AsCanvas(ccanvas)->translate(dx, dy);
341}
342
343void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
reed8e474782014-10-06 11:00:51 -0700344 AsCanvas(ccanvas)->scale(sx, sy);
reed938dfba2014-10-06 06:08:16 -0700345}
346
abarth2fc6ea62014-12-01 14:04:03 -0800347void sk_canvas_rotate_degress(sk_canvas_t* ccanvas, float degrees) {
348 AsCanvas(ccanvas)->rotate(degrees);
349}
350
351void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) {
352 AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians));
353}
354
355void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) {
356 AsCanvas(ccanvas)->skew(sx, sy);
357}
358
reede3719892014-12-22 17:46:00 -0800359void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) {
abarth9503ac72014-12-02 15:47:33 -0800360 SkASSERT(cmatrix);
361 SkMatrix matrix;
362 from_c_matrix(cmatrix, &matrix);
363 AsCanvas(ccanvas)->concat(matrix);
364}
365
reede3719892014-12-22 17:46:00 -0800366void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) {
367 AsCanvas(ccanvas)->clipRect(AsRect(*crect));
368}
369
370void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) {
371 AsCanvas(ccanvas)->clipPath(AsPath(*cpath));
372}
373
reed938dfba2014-10-06 06:08:16 -0700374void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
375 AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
376}
377
378void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
379 AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
380}
381
382void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
383 AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
384}
385
386void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
387 AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
388}
389
390void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
391 const sk_paint_t* cpaint) {
392 AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
393}
394
reede3719892014-12-22 17:46:00 -0800395void sk_canvas_draw_image_rect(sk_canvas_t* ccanvas, const sk_image_t* cimage,
396 const sk_rect_t* csrcR, const sk_rect_t* cdstR,
397 const sk_paint_t* cpaint) {
reede47829b2015-08-06 10:02:53 -0700398 SkCanvas* canvas = AsCanvas(ccanvas);
399 const SkImage* image = AsImage(cimage);
400 const SkRect& dst = AsRect(*cdstR);
401 const SkPaint* paint = AsPaint(cpaint);
402
403 if (csrcR) {
404 canvas->drawImageRect(image, AsRect(*csrcR), dst, paint);
405 } else {
406 canvas->drawImageRect(image, dst, paint);
407 }
reede3719892014-12-22 17:46:00 -0800408}
409
410void sk_canvas_draw_picture(sk_canvas_t* ccanvas, const sk_picture_t* cpicture,
411 const sk_matrix_t* cmatrix, const sk_paint_t* cpaint) {
412 const SkMatrix* matrixPtr = NULL;
413 SkMatrix matrix;
414 if (cmatrix) {
415 from_c_matrix(cmatrix, &matrix);
416 matrixPtr = &matrix;
417 }
418 AsCanvas(ccanvas)->drawPicture(AsPicture(cpicture), matrixPtr, AsPaint(cpaint));
419}
420
reed938dfba2014-10-06 06:08:16 -0700421///////////////////////////////////////////////////////////////////////////////////////////
422
robertphillips702edbd2015-06-23 06:26:08 -0700423sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo,
424 const sk_surfaceprops_t* props) {
reede3323962014-10-24 11:16:19 -0700425 SkImageInfo info;
426 if (!from_c_info(*cinfo, &info)) {
427 return NULL;
428 }
robertphillips702edbd2015-06-23 06:26:08 -0700429 SkPixelGeometry geo = kUnknown_SkPixelGeometry;
430 if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
431 return NULL;
432 }
433
434 SkSurfaceProps surfProps(0, geo);
435 return (sk_surface_t*)SkSurface::NewRaster(info, &surfProps);
reed938dfba2014-10-06 06:08:16 -0700436}
437
reed8e474782014-10-06 11:00:51 -0700438sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
robertphillips702edbd2015-06-23 06:26:08 -0700439 size_t rowBytes,
440 const sk_surfaceprops_t* props) {
reede3323962014-10-24 11:16:19 -0700441 SkImageInfo info;
442 if (!from_c_info(*cinfo, &info)) {
443 return NULL;
444 }
robertphillips702edbd2015-06-23 06:26:08 -0700445 SkPixelGeometry geo = kUnknown_SkPixelGeometry;
446 if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
447 return NULL;
448 }
449
450 SkSurfaceProps surfProps(0, geo);
451 return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes, &surfProps);
reed938dfba2014-10-06 06:08:16 -0700452}
453
reedafa278e2014-11-24 19:11:48 -0800454void sk_surface_unref(sk_surface_t* csurf) {
455 SkSafeUnref((SkSurface*)csurf);
reed938dfba2014-10-06 06:08:16 -0700456}
457
458sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
459 SkSurface* surf = (SkSurface*)csurf;
reed8e474782014-10-06 11:00:51 -0700460 return (sk_canvas_t*)surf->getCanvas();
reed938dfba2014-10-06 06:08:16 -0700461}
462
463sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
464 SkSurface* surf = (SkSurface*)csurf;
reed8e474782014-10-06 11:00:51 -0700465 return (sk_image_t*)surf->newImageSnapshot();
reed938dfba2014-10-06 06:08:16 -0700466}
467
reedafa278e2014-11-24 19:11:48 -0800468///////////////////////////////////////////////////////////////////////////////////////////
reed938dfba2014-10-06 06:08:16 -0700469
reede3719892014-12-22 17:46:00 -0800470sk_picture_recorder_t* sk_picture_recorder_new() {
471 return ToPictureRecorder(new SkPictureRecorder);
472}
473
474void sk_picture_recorder_delete(sk_picture_recorder_t* crec) {
475 delete AsPictureRecorder(crec);
476}
477
478sk_canvas_t* sk_picture_recorder_begin_recording(sk_picture_recorder_t* crec,
479 const sk_rect_t* cbounds) {
480 return ToCanvas(AsPictureRecorder(crec)->beginRecording(AsRect(*cbounds)));
481}
482
483sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) {
484 return ToPicture(AsPictureRecorder(crec)->endRecording());
485}
486
487void sk_picture_ref(sk_picture_t* cpic) {
488 SkSafeRef(AsPicture(cpic));
489}
490
491void sk_picture_unref(sk_picture_t* cpic) {
492 SkSafeUnref(AsPicture(cpic));
493}
494
495uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) {
496 return AsPicture(cpic)->uniqueID();
497}
498
499sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) {
500 return ToRect(AsPicture(cpic)->cullRect());
501}
502
503///////////////////////////////////////////////////////////////////////////////////////////
504
reedafa278e2014-11-24 19:11:48 -0800505#include "../../include/effects/SkGradientShader.h"
506#include "sk_shader.h"
507
508const struct {
509 sk_shader_tilemode_t fC;
510 SkShader::TileMode fSK;
511} gTileModeMap[] = {
512 { CLAMP_SK_SHADER_TILEMODE, SkShader::kClamp_TileMode },
513 { REPEAT_SK_SHADER_TILEMODE, SkShader::kRepeat_TileMode },
514 { MIRROR_SK_SHADER_TILEMODE, SkShader::kMirror_TileMode },
515};
516
517static bool from_c_tilemode(sk_shader_tilemode_t cMode, SkShader::TileMode* skMode) {
518 for (size_t i = 0; i < SK_ARRAY_COUNT(gTileModeMap); ++i) {
519 if (cMode == gTileModeMap[i].fC) {
520 if (skMode) {
521 *skMode = gTileModeMap[i].fSK;
522 }
523 return true;
524 }
525 }
526 return false;
527}
528
529void sk_shader_ref(sk_shader_t* cshader) {
530 SkSafeRef(AsShader(cshader));
531}
532
533void sk_shader_unref(sk_shader_t* cshader) {
534 SkSafeUnref(AsShader(cshader));
535}
536
537sk_shader_t* sk_shader_new_linear_gradient(const sk_point_t pts[2],
538 const sk_color_t colors[],
539 const float colorPos[],
540 int colorCount,
541 sk_shader_tilemode_t cmode,
542 const sk_matrix_t* cmatrix) {
543 SkShader::TileMode mode;
544 if (!from_c_tilemode(cmode, &mode)) {
545 return NULL;
546 }
547 SkMatrix matrix;
548 if (cmatrix) {
abarth9503ac72014-12-02 15:47:33 -0800549 from_c_matrix(cmatrix, &matrix);
reedafa278e2014-11-24 19:11:48 -0800550 } else {
551 matrix.setIdentity();
552 }
553 SkShader* s = SkGradientShader::CreateLinear(reinterpret_cast<const SkPoint*>(pts),
554 reinterpret_cast<const SkColor*>(colors),
555 colorPos, colorCount, mode, 0, &matrix);
556 return (sk_shader_t*)s;
557}
558
559///////////////////////////////////////////////////////////////////////////////////////////
reed0eafc9b2014-12-23 14:11:11 -0800560
561#include "../../include/effects/SkBlurMaskFilter.h"
562#include "sk_maskfilter.h"
563
564const struct {
565 sk_blurstyle_t fC;
566 SkBlurStyle fSk;
567} gBlurStylePairs[] = {
568 { NORMAL_SK_BLUR_STYLE, kNormal_SkBlurStyle },
569 { SOLID_SK_BLUR_STYLE, kSolid_SkBlurStyle },
570 { OUTER_SK_BLUR_STYLE, kOuter_SkBlurStyle },
571 { INNER_SK_BLUR_STYLE, kInner_SkBlurStyle },
572};
573
574static bool find_blurstyle(sk_blurstyle_t csrc, SkBlurStyle* dst) {
575 for (size_t i = 0; i < SK_ARRAY_COUNT(gBlurStylePairs); ++i) {
576 if (gBlurStylePairs[i].fC == csrc) {
577 if (dst) {
578 *dst = gBlurStylePairs[i].fSk;
579 }
580 return true;
581 }
582 }
583 return false;
584}
585
586void sk_maskfilter_ref(sk_maskfilter_t* cfilter) {
587 SkSafeRef(AsMaskFilter(cfilter));
588}
589
590void sk_maskfilter_unref(sk_maskfilter_t* cfilter) {
591 SkSafeUnref(AsMaskFilter(cfilter));
592}
593
594sk_maskfilter_t* sk_maskfilter_new_blur(sk_blurstyle_t cstyle, float sigma) {
595 SkBlurStyle style;
596 if (!find_blurstyle(cstyle, &style)) {
597 return NULL;
598 }
599 return ToMaskFilter(SkBlurMaskFilter::Create(style, sigma));
600}
601
602///////////////////////////////////////////////////////////////////////////////////////////
reedb2a5d7e2014-12-25 14:16:21 -0800603
604sk_data_t* sk_data_new_with_copy(const void* src, size_t length) {
605 return ToData(SkData::NewWithCopy(src, length));
606}
607
608sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) {
609 return ToData(SkData::NewFromMalloc(memory, length));
610}
611
612sk_data_t* sk_data_new_subset(const sk_data_t* csrc, size_t offset, size_t length) {
613 return ToData(SkData::NewSubset(AsData(csrc), offset, length));
614}
615
616void sk_data_ref(const sk_data_t* cdata) {
617 SkSafeRef(AsData(cdata));
618}
619
620void sk_data_unref(const sk_data_t* cdata) {
621 SkSafeUnref(AsData(cdata));
622}
623
624size_t sk_data_get_size(const sk_data_t* cdata) {
625 return AsData(cdata)->size();
626}
627
628const void* sk_data_get_data(const sk_data_t* cdata) {
629 return AsData(cdata)->data();
630}
631
632///////////////////////////////////////////////////////////////////////////////////////////