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 | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 8 | #include "sk_canvas.h" |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 9 | #include "sk_data.h" |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 10 | #include "sk_image.h" |
| 11 | #include "sk_paint.h" |
| 12 | #include "sk_path.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 13 | #include "sk_surface.h" |
reed | f5872d2 | 2015-01-10 17:59:31 -0800 | [diff] [blame] | 14 | #include "sk_types_priv.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 15 | |
| 16 | #include "SkCanvas.h" |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 17 | #include "SkData.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 18 | #include "SkImage.h" |
reed | 0eafc9b | 2014-12-23 14:11:11 -0800 | [diff] [blame] | 19 | #include "SkMaskFilter.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 20 | #include "SkMatrix.h" |
| 21 | #include "SkPaint.h" |
| 22 | #include "SkPath.h" |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 23 | #include "SkPictureRecorder.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 24 | #include "SkSurface.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 | } |
| 227 | return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes); |
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) { |
| 231 | return ToImage(SkImage::NewFromEncoded(AsData(cdata), |
| 232 | reinterpret_cast<const SkIRect*>(subset))); |
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 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 261 | sk_path_t* sk_path_new() { |
| 262 | return (sk_path_t*)SkNEW(SkPath); |
| 263 | } |
| 264 | |
| 265 | void sk_path_delete(sk_path_t* cpath) { |
| 266 | SkDELETE(as_path(cpath)); |
| 267 | } |
| 268 | |
| 269 | void sk_path_move_to(sk_path_t* cpath, float x, float y) { |
| 270 | as_path(cpath)->moveTo(x, y); |
| 271 | } |
| 272 | |
| 273 | void sk_path_line_to(sk_path_t* cpath, float x, float y) { |
| 274 | as_path(cpath)->lineTo(x, y); |
| 275 | } |
| 276 | |
| 277 | void 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 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 281 | void 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 | |
| 285 | void 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 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 289 | void sk_path_close(sk_path_t* cpath) { |
| 290 | as_path(cpath)->close(); |
| 291 | } |
| 292 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 293 | void 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 | |
| 301 | void 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 | |
| 309 | bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) { |
| 310 | const SkPath& path = AsPath(*cpath); |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 311 | |
| 312 | if (path.isEmpty()) { |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 313 | if (crect) { |
| 314 | *crect = ToRect(SkRect::MakeEmpty()); |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 315 | } |
| 316 | return false; |
| 317 | } |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 318 | |
| 319 | if (crect) { |
| 320 | *crect = ToRect(path.getBounds()); |
| 321 | } |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 325 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 326 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 327 | void sk_canvas_save(sk_canvas_t* ccanvas) { |
| 328 | AsCanvas(ccanvas)->save(); |
| 329 | } |
| 330 | |
| 331 | void 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 | |
| 335 | void sk_canvas_restore(sk_canvas_t* ccanvas) { |
| 336 | AsCanvas(ccanvas)->restore(); |
| 337 | } |
| 338 | |
| 339 | void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) { |
| 340 | AsCanvas(ccanvas)->translate(dx, dy); |
| 341 | } |
| 342 | |
| 343 | void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 344 | AsCanvas(ccanvas)->scale(sx, sy); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 345 | } |
| 346 | |
abarth | 2fc6ea6 | 2014-12-01 14:04:03 -0800 | [diff] [blame] | 347 | void sk_canvas_rotate_degress(sk_canvas_t* ccanvas, float degrees) { |
| 348 | AsCanvas(ccanvas)->rotate(degrees); |
| 349 | } |
| 350 | |
| 351 | void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) { |
| 352 | AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians)); |
| 353 | } |
| 354 | |
| 355 | void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) { |
| 356 | AsCanvas(ccanvas)->skew(sx, sy); |
| 357 | } |
| 358 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 359 | void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) { |
abarth | 9503ac7 | 2014-12-02 15:47:33 -0800 | [diff] [blame] | 360 | SkASSERT(cmatrix); |
| 361 | SkMatrix matrix; |
| 362 | from_c_matrix(cmatrix, &matrix); |
| 363 | AsCanvas(ccanvas)->concat(matrix); |
| 364 | } |
| 365 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 366 | void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) { |
| 367 | AsCanvas(ccanvas)->clipRect(AsRect(*crect)); |
| 368 | } |
| 369 | |
| 370 | void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) { |
| 371 | AsCanvas(ccanvas)->clipPath(AsPath(*cpath)); |
| 372 | } |
| 373 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 374 | void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) { |
| 375 | AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint)); |
| 376 | } |
| 377 | |
| 378 | void 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 | |
| 382 | void 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 | |
| 386 | void 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 | |
| 390 | void 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 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 395 | void 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) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame^] | 398 | 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 | } |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void 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 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 421 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 422 | |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 423 | sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo, |
| 424 | const sk_surfaceprops_t* props) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 425 | SkImageInfo info; |
| 426 | if (!from_c_info(*cinfo, &info)) { |
| 427 | return NULL; |
| 428 | } |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 429 | 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); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 436 | } |
| 437 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 438 | 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] | 439 | size_t rowBytes, |
| 440 | const sk_surfaceprops_t* props) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 441 | SkImageInfo info; |
| 442 | if (!from_c_info(*cinfo, &info)) { |
| 443 | return NULL; |
| 444 | } |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 445 | 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); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 452 | } |
| 453 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 454 | void sk_surface_unref(sk_surface_t* csurf) { |
| 455 | SkSafeUnref((SkSurface*)csurf); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) { |
| 459 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 460 | return (sk_canvas_t*)surf->getCanvas(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) { |
| 464 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 465 | return (sk_image_t*)surf->newImageSnapshot(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 466 | } |
| 467 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 468 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 469 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 470 | sk_picture_recorder_t* sk_picture_recorder_new() { |
| 471 | return ToPictureRecorder(new SkPictureRecorder); |
| 472 | } |
| 473 | |
| 474 | void sk_picture_recorder_delete(sk_picture_recorder_t* crec) { |
| 475 | delete AsPictureRecorder(crec); |
| 476 | } |
| 477 | |
| 478 | sk_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 | |
| 483 | sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) { |
| 484 | return ToPicture(AsPictureRecorder(crec)->endRecording()); |
| 485 | } |
| 486 | |
| 487 | void sk_picture_ref(sk_picture_t* cpic) { |
| 488 | SkSafeRef(AsPicture(cpic)); |
| 489 | } |
| 490 | |
| 491 | void sk_picture_unref(sk_picture_t* cpic) { |
| 492 | SkSafeUnref(AsPicture(cpic)); |
| 493 | } |
| 494 | |
| 495 | uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) { |
| 496 | return AsPicture(cpic)->uniqueID(); |
| 497 | } |
| 498 | |
| 499 | sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) { |
| 500 | return ToRect(AsPicture(cpic)->cullRect()); |
| 501 | } |
| 502 | |
| 503 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 504 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 505 | #include "../../include/effects/SkGradientShader.h" |
| 506 | #include "sk_shader.h" |
| 507 | |
| 508 | const 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 | |
| 517 | static 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 | |
| 529 | void sk_shader_ref(sk_shader_t* cshader) { |
| 530 | SkSafeRef(AsShader(cshader)); |
| 531 | } |
| 532 | |
| 533 | void sk_shader_unref(sk_shader_t* cshader) { |
| 534 | SkSafeUnref(AsShader(cshader)); |
| 535 | } |
| 536 | |
| 537 | sk_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) { |
abarth | 9503ac7 | 2014-12-02 15:47:33 -0800 | [diff] [blame] | 549 | from_c_matrix(cmatrix, &matrix); |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 550 | } 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 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | 0eafc9b | 2014-12-23 14:11:11 -0800 | [diff] [blame] | 560 | |
| 561 | #include "../../include/effects/SkBlurMaskFilter.h" |
| 562 | #include "sk_maskfilter.h" |
| 563 | |
| 564 | const 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 | |
| 574 | static 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 | |
| 586 | void sk_maskfilter_ref(sk_maskfilter_t* cfilter) { |
| 587 | SkSafeRef(AsMaskFilter(cfilter)); |
| 588 | } |
| 589 | |
| 590 | void sk_maskfilter_unref(sk_maskfilter_t* cfilter) { |
| 591 | SkSafeUnref(AsMaskFilter(cfilter)); |
| 592 | } |
| 593 | |
| 594 | sk_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 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 603 | |
| 604 | sk_data_t* sk_data_new_with_copy(const void* src, size_t length) { |
| 605 | return ToData(SkData::NewWithCopy(src, length)); |
| 606 | } |
| 607 | |
| 608 | sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) { |
| 609 | return ToData(SkData::NewFromMalloc(memory, length)); |
| 610 | } |
| 611 | |
| 612 | sk_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 | |
| 616 | void sk_data_ref(const sk_data_t* cdata) { |
| 617 | SkSafeRef(AsData(cdata)); |
| 618 | } |
| 619 | |
| 620 | void sk_data_unref(const sk_data_t* cdata) { |
| 621 | SkSafeUnref(AsData(cdata)); |
| 622 | } |
| 623 | |
| 624 | size_t sk_data_get_size(const sk_data_t* cdata) { |
| 625 | return AsData(cdata)->size(); |
| 626 | } |
| 627 | |
| 628 | const void* sk_data_get_data(const sk_data_t* cdata) { |
| 629 | return AsData(cdata)->data(); |
| 630 | } |
| 631 | |
| 632 | /////////////////////////////////////////////////////////////////////////////////////////// |