reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 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 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 8 | #include "SkCanvas.h" |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 9 | #include "SkData.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 10 | #include "SkImage.h" |
reed | 0eafc9b | 2014-12-23 14:11:11 -0800 | [diff] [blame] | 11 | #include "SkMaskFilter.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 12 | #include "SkMatrix.h" |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkPath.h" |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 15 | #include "SkPictureRecorder.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 16 | #include "SkSurface.h" |
| 17 | |
halcanary | 219f18f | 2015-09-01 10:01:38 -0700 | [diff] [blame] | 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 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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; |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 97 | } |
| 98 | |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 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 | |
| 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 | |
abarth | 9503ac7 | 2014-12-02 15:47:33 -0800 | [diff] [blame] | 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 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 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 | |
| 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 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 149 | static SkData* AsData(const sk_data_t* cdata) { |
| 150 | return reinterpret_cast<SkData*>(const_cast<sk_data_t*>(cdata)); |
| 151 | } |
| 152 | |
| 153 | static sk_data_t* ToData(SkData* data) { |
| 154 | return reinterpret_cast<sk_data_t*>(data); |
| 155 | } |
| 156 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 157 | static sk_rect_t ToRect(const SkRect& rect) { |
| 158 | return reinterpret_cast<const sk_rect_t&>(rect); |
| 159 | } |
| 160 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 161 | static const SkRect& AsRect(const sk_rect_t& crect) { |
| 162 | return reinterpret_cast<const SkRect&>(crect); |
| 163 | } |
| 164 | |
| 165 | static const SkPath& AsPath(const sk_path_t& cpath) { |
| 166 | return reinterpret_cast<const SkPath&>(cpath); |
| 167 | } |
| 168 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 169 | static SkPath* as_path(sk_path_t* cpath) { |
| 170 | return reinterpret_cast<SkPath*>(cpath); |
| 171 | } |
| 172 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 173 | static const SkImage* AsImage(const sk_image_t* cimage) { |
| 174 | return reinterpret_cast<const SkImage*>(cimage); |
| 175 | } |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 176 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 177 | static sk_image_t* ToImage(SkImage* cimage) { |
| 178 | return reinterpret_cast<sk_image_t*>(cimage); |
| 179 | } |
| 180 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 181 | static sk_canvas_t* ToCanvas(SkCanvas* canvas) { |
| 182 | return reinterpret_cast<sk_canvas_t*>(canvas); |
| 183 | } |
| 184 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 185 | static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) { |
| 186 | return reinterpret_cast<SkCanvas*>(ccanvas); |
| 187 | } |
| 188 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 189 | static SkPictureRecorder* AsPictureRecorder(sk_picture_recorder_t* crec) { |
| 190 | return reinterpret_cast<SkPictureRecorder*>(crec); |
| 191 | } |
| 192 | |
| 193 | static sk_picture_recorder_t* ToPictureRecorder(SkPictureRecorder* rec) { |
| 194 | return reinterpret_cast<sk_picture_recorder_t*>(rec); |
| 195 | } |
| 196 | |
| 197 | static const SkPicture* AsPicture(const sk_picture_t* cpic) { |
| 198 | return reinterpret_cast<const SkPicture*>(cpic); |
| 199 | } |
| 200 | |
| 201 | static SkPicture* AsPicture(sk_picture_t* cpic) { |
| 202 | return reinterpret_cast<SkPicture*>(cpic); |
| 203 | } |
| 204 | |
| 205 | static sk_picture_t* ToPicture(SkPicture* pic) { |
| 206 | return reinterpret_cast<sk_picture_t*>(pic); |
| 207 | } |
| 208 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 209 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 210 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 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 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 221 | sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels, |
| 222 | size_t rowBytes) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 223 | SkImageInfo info; |
| 224 | if (!from_c_info(*cinfo, &info)) { |
| 225 | return NULL; |
| 226 | } |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 227 | return (sk_image_t*)SkImage::MakeRasterCopy(SkPixmap(info, pixels, rowBytes)).release(); |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 228 | } |
| 229 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 230 | sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata, const sk_irect_t* subset) { |
reed | 7fb4f8b | 2016-03-11 04:33:52 -0800 | [diff] [blame] | 231 | return ToImage(SkImage::MakeFromEncoded(sk_ref_sp(AsData(cdata)), |
| 232 | reinterpret_cast<const SkIRect*>(subset)).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | sk_data_t* sk_image_encode(const sk_image_t* cimage) { |
| 236 | return ToData(AsImage(cimage)->encode()); |
| 237 | } |
| 238 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 239 | void sk_image_ref(const sk_image_t* cimage) { |
| 240 | AsImage(cimage)->ref(); |
| 241 | } |
| 242 | |
| 243 | void sk_image_unref(const sk_image_t* cimage) { |
| 244 | AsImage(cimage)->unref(); |
| 245 | } |
| 246 | |
| 247 | int sk_image_get_width(const sk_image_t* cimage) { |
| 248 | return AsImage(cimage)->width(); |
| 249 | } |
| 250 | |
| 251 | int sk_image_get_height(const sk_image_t* cimage) { |
| 252 | return AsImage(cimage)->height(); |
| 253 | } |
| 254 | |
| 255 | uint32_t sk_image_get_unique_id(const sk_image_t* cimage) { |
| 256 | return AsImage(cimage)->uniqueID(); |
| 257 | } |
| 258 | |
| 259 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 260 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 261 | sk_path_t* sk_path_new() { return (sk_path_t*)new SkPath; } |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 262 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 263 | void sk_path_delete(sk_path_t* cpath) { delete as_path(cpath); } |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 264 | |
| 265 | void sk_path_move_to(sk_path_t* cpath, float x, float y) { |
| 266 | as_path(cpath)->moveTo(x, y); |
| 267 | } |
| 268 | |
| 269 | void sk_path_line_to(sk_path_t* cpath, float x, float y) { |
| 270 | as_path(cpath)->lineTo(x, y); |
| 271 | } |
| 272 | |
| 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 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 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 | |
| 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 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 285 | void sk_path_close(sk_path_t* cpath) { |
| 286 | as_path(cpath)->close(); |
| 287 | } |
| 288 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 305 | bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) { |
| 306 | const SkPath& path = AsPath(*cpath); |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 307 | |
| 308 | if (path.isEmpty()) { |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 309 | if (crect) { |
| 310 | *crect = ToRect(SkRect::MakeEmpty()); |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 311 | } |
| 312 | return false; |
| 313 | } |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 314 | |
| 315 | if (crect) { |
| 316 | *crect = ToRect(path.getBounds()); |
| 317 | } |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 318 | return true; |
| 319 | } |
| 320 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 321 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 322 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 323 | void sk_canvas_save(sk_canvas_t* ccanvas) { |
| 324 | AsCanvas(ccanvas)->save(); |
| 325 | } |
| 326 | |
| 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 | |
| 331 | void sk_canvas_restore(sk_canvas_t* ccanvas) { |
| 332 | AsCanvas(ccanvas)->restore(); |
| 333 | } |
| 334 | |
| 335 | void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) { |
| 336 | AsCanvas(ccanvas)->translate(dx, dy); |
| 337 | } |
| 338 | |
| 339 | void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 340 | AsCanvas(ccanvas)->scale(sx, sy); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 341 | } |
| 342 | |
abarth | 2fc6ea6 | 2014-12-01 14:04:03 -0800 | [diff] [blame] | 343 | void sk_canvas_rotate_degress(sk_canvas_t* ccanvas, float degrees) { |
| 344 | AsCanvas(ccanvas)->rotate(degrees); |
| 345 | } |
| 346 | |
| 347 | void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) { |
| 348 | AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians)); |
| 349 | } |
| 350 | |
| 351 | void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) { |
| 352 | AsCanvas(ccanvas)->skew(sx, sy); |
| 353 | } |
| 354 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 355 | void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) { |
abarth | 9503ac7 | 2014-12-02 15:47:33 -0800 | [diff] [blame] | 356 | SkASSERT(cmatrix); |
| 357 | SkMatrix matrix; |
| 358 | from_c_matrix(cmatrix, &matrix); |
| 359 | AsCanvas(ccanvas)->concat(matrix); |
| 360 | } |
| 361 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 362 | void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) { |
| 363 | AsCanvas(ccanvas)->clipRect(AsRect(*crect)); |
| 364 | } |
| 365 | |
| 366 | void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) { |
| 367 | AsCanvas(ccanvas)->clipPath(AsPath(*cpath)); |
| 368 | } |
| 369 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 370 | void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) { |
| 371 | AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint)); |
| 372 | } |
| 373 | |
| 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 | |
reed | 4a7940b | 2016-02-09 13:25:51 -0800 | [diff] [blame] | 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 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 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) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 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 | } |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 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 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 422 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 423 | |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 424 | sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo, |
| 425 | const sk_surfaceprops_t* props) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 426 | SkImageInfo info; |
| 427 | if (!from_c_info(*cinfo, &info)) { |
| 428 | return NULL; |
| 429 | } |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 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); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 437 | } |
| 438 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 439 | sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels, |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 440 | size_t rowBytes, |
| 441 | const sk_surfaceprops_t* props) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 442 | SkImageInfo info; |
| 443 | if (!from_c_info(*cinfo, &info)) { |
| 444 | return NULL; |
| 445 | } |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 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); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 453 | } |
| 454 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 455 | void sk_surface_unref(sk_surface_t* csurf) { |
| 456 | SkSafeUnref((SkSurface*)csurf); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) { |
| 460 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 461 | return (sk_canvas_t*)surf->getCanvas(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) { |
| 465 | SkSurface* surf = (SkSurface*)csurf; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 466 | return (sk_image_t*)surf->makeImageSnapshot().release(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 467 | } |
| 468 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 469 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 470 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 471 | sk_picture_recorder_t* sk_picture_recorder_new() { |
| 472 | return ToPictureRecorder(new SkPictureRecorder); |
| 473 | } |
| 474 | |
| 475 | void sk_picture_recorder_delete(sk_picture_recorder_t* crec) { |
| 476 | delete AsPictureRecorder(crec); |
| 477 | } |
| 478 | |
| 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 | |
| 484 | sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame^] | 485 | return ToPicture(AsPictureRecorder(crec)->finishRecordingAsPicture().release()); |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | void sk_picture_ref(sk_picture_t* cpic) { |
| 489 | SkSafeRef(AsPicture(cpic)); |
| 490 | } |
| 491 | |
| 492 | void sk_picture_unref(sk_picture_t* cpic) { |
| 493 | SkSafeUnref(AsPicture(cpic)); |
| 494 | } |
| 495 | |
| 496 | uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) { |
| 497 | return AsPicture(cpic)->uniqueID(); |
| 498 | } |
| 499 | |
| 500 | sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) { |
| 501 | return ToRect(AsPicture(cpic)->cullRect()); |
| 502 | } |
| 503 | |
| 504 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 505 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 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 | |
| 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 | |
| 530 | void sk_shader_ref(sk_shader_t* cshader) { |
| 531 | SkSafeRef(AsShader(cshader)); |
| 532 | } |
| 533 | |
| 534 | void sk_shader_unref(sk_shader_t* cshader) { |
| 535 | SkSafeUnref(AsShader(cshader)); |
| 536 | } |
| 537 | |
| 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) { |
abarth | 9503ac7 | 2014-12-02 15:47:33 -0800 | [diff] [blame] | 550 | from_c_matrix(cmatrix, &matrix); |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 551 | } else { |
| 552 | matrix.setIdentity(); |
| 553 | } |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 554 | 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(); |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 558 | } |
| 559 | |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 560 | static const SkPoint& to_skpoint(const sk_point_t& p) { |
| 561 | return reinterpret_cast<const SkPoint&>(p); |
| 562 | } |
| 563 | |
| 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); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 582 | 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(); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | sk_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 | } |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 599 | 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(); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | sk_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); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 627 | 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(); |
halcanary | 65cc3e4 | 2015-08-12 07:37:34 -0700 | [diff] [blame] | 632 | } |
| 633 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 634 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | 0eafc9b | 2014-12-23 14:11:11 -0800 | [diff] [blame] | 635 | |
| 636 | #include "../../include/effects/SkBlurMaskFilter.h" |
| 637 | #include "sk_maskfilter.h" |
| 638 | |
| 639 | const 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 | |
| 649 | static 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 | |
| 661 | void sk_maskfilter_ref(sk_maskfilter_t* cfilter) { |
| 662 | SkSafeRef(AsMaskFilter(cfilter)); |
| 663 | } |
| 664 | |
| 665 | void sk_maskfilter_unref(sk_maskfilter_t* cfilter) { |
| 666 | SkSafeUnref(AsMaskFilter(cfilter)); |
| 667 | } |
| 668 | |
| 669 | sk_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 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 678 | |
| 679 | sk_data_t* sk_data_new_with_copy(const void* src, size_t length) { |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 680 | return ToData(SkData::MakeWithCopy(src, length).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) { |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 684 | return ToData(SkData::MakeFromMalloc(memory, length).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | sk_data_t* sk_data_new_subset(const sk_data_t* csrc, size_t offset, size_t length) { |
reed | fde0511 | 2016-03-11 13:02:28 -0800 | [diff] [blame] | 688 | return ToData(SkData::MakeSubset(AsData(csrc), offset, length).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | void sk_data_ref(const sk_data_t* cdata) { |
| 692 | SkSafeRef(AsData(cdata)); |
| 693 | } |
| 694 | |
| 695 | void sk_data_unref(const sk_data_t* cdata) { |
| 696 | SkSafeUnref(AsData(cdata)); |
| 697 | } |
| 698 | |
| 699 | size_t sk_data_get_size(const sk_data_t* cdata) { |
| 700 | return AsData(cdata)->size(); |
| 701 | } |
| 702 | |
| 703 | const void* sk_data_get_data(const sk_data_t* cdata) { |
| 704 | return AsData(cdata)->data(); |
| 705 | } |
| 706 | |
| 707 | /////////////////////////////////////////////////////////////////////////////////////////// |