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" |
| 9 | #include "sk_image.h" |
| 10 | #include "sk_paint.h" |
| 11 | #include "sk_path.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 12 | #include "sk_surface.h" |
| 13 | |
| 14 | #include "SkCanvas.h" |
| 15 | #include "SkImage.h" |
| 16 | #include "SkMatrix.h" |
| 17 | #include "SkPaint.h" |
| 18 | #include "SkPath.h" |
| 19 | #include "SkSurface.h" |
| 20 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 21 | const struct { |
| 22 | sk_colortype_t fC; |
| 23 | SkColorType fSK; |
| 24 | } gColorTypeMap[] = { |
| 25 | { UNKNOWN_SK_COLORTYPE, kUnknown_SkColorType }, |
| 26 | { RGBA_8888_SK_COLORTYPE, kRGBA_8888_SkColorType }, |
| 27 | { BGRA_8888_SK_COLORTYPE, kBGRA_8888_SkColorType }, |
| 28 | { ALPHA_8_SK_COLORTYPE, kAlpha_8_SkColorType }, |
| 29 | }; |
| 30 | |
| 31 | const struct { |
| 32 | sk_alphatype_t fC; |
| 33 | SkAlphaType fSK; |
| 34 | } gAlphaTypeMap[] = { |
| 35 | { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType }, |
| 36 | { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType }, |
| 37 | { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType }, |
| 38 | }; |
| 39 | |
| 40 | static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) { |
| 41 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) { |
| 42 | if (gColorTypeMap[i].fC == cCT) { |
| 43 | if (skCT) { |
| 44 | *skCT = gColorTypeMap[i].fSK; |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) { |
| 53 | for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) { |
| 54 | if (gColorTypeMap[i].fSK == skCT) { |
| 55 | if (cCT) { |
| 56 | *cCT = gColorTypeMap[i].fC; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) { |
| 65 | for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) { |
| 66 | if (gAlphaTypeMap[i].fC == cAT) { |
| 67 | if (skAT) { |
| 68 | *skAT = gAlphaTypeMap[i].fSK; |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | static bool from_c_info(const sk_imageinfo_t& cinfo, SkImageInfo* info) { |
| 77 | SkColorType ct; |
| 78 | SkAlphaType at; |
| 79 | |
| 80 | if (!from_c_colortype(cinfo.colorType, &ct)) { |
| 81 | // optionally report error to client? |
| 82 | return false; |
| 83 | } |
| 84 | if (!from_c_alphatype(cinfo.alphaType, &at)) { |
| 85 | // optionally report error to client? |
| 86 | return false; |
| 87 | } |
| 88 | if (info) { |
| 89 | *info = SkImageInfo::Make(cinfo.width, cinfo.height, ct, at); |
| 90 | } |
| 91 | return true; |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 94 | const struct { |
| 95 | sk_path_direction_t fC; |
| 96 | SkPath::Direction fSk; |
| 97 | } gPathDirMap[] = { |
| 98 | { CW_SK_PATH_DIRECTION, SkPath::kCW_Direction }, |
| 99 | { CCW_SK_PATH_DIRECTION, SkPath::kCCW_Direction }, |
| 100 | }; |
| 101 | |
| 102 | static bool from_c_path_direction(sk_path_direction_t cdir, SkPath::Direction* dir) { |
| 103 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPathDirMap); ++i) { |
| 104 | if (gPathDirMap[i].fC == cdir) { |
| 105 | if (dir) { |
| 106 | *dir = gPathDirMap[i].fSk; |
| 107 | } |
| 108 | return true; |
| 109 | } |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 114 | static const SkRect& AsRect(const sk_rect_t& crect) { |
| 115 | return reinterpret_cast<const SkRect&>(crect); |
| 116 | } |
| 117 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 118 | static SkRect* as_rect(sk_rect_t* crect) { |
| 119 | return reinterpret_cast<SkRect*>(crect); |
| 120 | } |
| 121 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 122 | static const SkPath& AsPath(const sk_path_t& cpath) { |
| 123 | return reinterpret_cast<const SkPath&>(cpath); |
| 124 | } |
| 125 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 126 | static SkPath* as_path(sk_path_t* cpath) { |
| 127 | return reinterpret_cast<SkPath*>(cpath); |
| 128 | } |
| 129 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 130 | static const SkImage* AsImage(const sk_image_t* cimage) { |
| 131 | return reinterpret_cast<const SkImage*>(cimage); |
| 132 | } |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 133 | |
| 134 | static const SkPaint& AsPaint(const sk_paint_t& cpaint) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 135 | return reinterpret_cast<const SkPaint&>(cpaint); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static const SkPaint* AsPaint(const sk_paint_t* cpaint) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 139 | return reinterpret_cast<const SkPaint*>(cpaint); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 140 | } |
| 141 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 142 | static SkPaint* AsPaint(sk_paint_t* cpaint) { |
| 143 | return reinterpret_cast<SkPaint*>(cpaint); |
| 144 | } |
| 145 | |
| 146 | static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) { |
| 147 | return reinterpret_cast<SkCanvas*>(ccanvas); |
| 148 | } |
| 149 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame^] | 150 | static SkShader* AsShader(sk_shader_t* cshader) { |
| 151 | return reinterpret_cast<SkShader*>(cshader); |
| 152 | } |
| 153 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 154 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 155 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 156 | sk_colortype_t sk_colortype_get_default_8888() { |
| 157 | sk_colortype_t ct; |
| 158 | if (!to_c_colortype(kN32_SkColorType, &ct)) { |
| 159 | ct = UNKNOWN_SK_COLORTYPE; |
| 160 | } |
| 161 | return ct; |
| 162 | } |
| 163 | |
| 164 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 165 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 166 | sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels, |
| 167 | size_t rowBytes) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 168 | SkImageInfo info; |
| 169 | if (!from_c_info(*cinfo, &info)) { |
| 170 | return NULL; |
| 171 | } |
| 172 | return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes); |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void sk_image_ref(const sk_image_t* cimage) { |
| 176 | AsImage(cimage)->ref(); |
| 177 | } |
| 178 | |
| 179 | void sk_image_unref(const sk_image_t* cimage) { |
| 180 | AsImage(cimage)->unref(); |
| 181 | } |
| 182 | |
| 183 | int sk_image_get_width(const sk_image_t* cimage) { |
| 184 | return AsImage(cimage)->width(); |
| 185 | } |
| 186 | |
| 187 | int sk_image_get_height(const sk_image_t* cimage) { |
| 188 | return AsImage(cimage)->height(); |
| 189 | } |
| 190 | |
| 191 | uint32_t sk_image_get_unique_id(const sk_image_t* cimage) { |
| 192 | return AsImage(cimage)->uniqueID(); |
| 193 | } |
| 194 | |
| 195 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 196 | |
| 197 | sk_paint_t* sk_paint_new() { |
| 198 | return (sk_paint_t*)SkNEW(SkPaint); |
| 199 | } |
| 200 | |
| 201 | void sk_paint_delete(sk_paint_t* cpaint) { |
| 202 | SkDELETE(AsPaint(cpaint)); |
| 203 | } |
| 204 | |
| 205 | bool sk_paint_is_antialias(const sk_paint_t* cpaint) { |
| 206 | return AsPaint(*cpaint).isAntiAlias(); |
| 207 | } |
| 208 | |
| 209 | void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) { |
| 210 | AsPaint(cpaint)->setAntiAlias(aa); |
| 211 | } |
| 212 | |
| 213 | sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) { |
| 214 | return AsPaint(*cpaint).getColor(); |
| 215 | } |
| 216 | |
| 217 | void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) { |
| 218 | AsPaint(cpaint)->setColor(c); |
| 219 | } |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 220 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame^] | 221 | void sk_paint_set_shader(sk_paint_t* cpaint, sk_shader_t* cshader) { |
| 222 | AsPaint(cpaint)->setShader(AsShader(cshader)); |
| 223 | } |
| 224 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 225 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 226 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 227 | sk_path_t* sk_path_new() { |
| 228 | return (sk_path_t*)SkNEW(SkPath); |
| 229 | } |
| 230 | |
| 231 | void sk_path_delete(sk_path_t* cpath) { |
| 232 | SkDELETE(as_path(cpath)); |
| 233 | } |
| 234 | |
| 235 | void sk_path_move_to(sk_path_t* cpath, float x, float y) { |
| 236 | as_path(cpath)->moveTo(x, y); |
| 237 | } |
| 238 | |
| 239 | void sk_path_line_to(sk_path_t* cpath, float x, float y) { |
| 240 | as_path(cpath)->lineTo(x, y); |
| 241 | } |
| 242 | |
| 243 | void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) { |
| 244 | as_path(cpath)->quadTo(x0, y0, x1, y1); |
| 245 | } |
| 246 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 247 | void sk_path_conic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float w) { |
| 248 | as_path(cpath)->conicTo(x0, y0, x1, y1, w); |
| 249 | } |
| 250 | |
| 251 | void sk_path_cubic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float x2, float y2) { |
| 252 | as_path(cpath)->cubicTo(x0, y0, x1, y1, x2, y2); |
| 253 | } |
| 254 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 255 | void sk_path_close(sk_path_t* cpath) { |
| 256 | as_path(cpath)->close(); |
| 257 | } |
| 258 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 259 | void sk_path_add_rect(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) { |
| 260 | SkPath::Direction dir; |
| 261 | if (!from_c_path_direction(cdir, &dir)) { |
| 262 | return; |
| 263 | } |
| 264 | as_path(cpath)->addRect(AsRect(*crect), dir); |
| 265 | } |
| 266 | |
| 267 | void sk_path_add_oval(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) { |
| 268 | SkPath::Direction dir; |
| 269 | if (!from_c_path_direction(cdir, &dir)) { |
| 270 | return; |
| 271 | } |
| 272 | as_path(cpath)->addOval(AsRect(*crect), dir); |
| 273 | } |
| 274 | |
| 275 | bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) { |
| 276 | const SkPath& path = AsPath(*cpath); |
| 277 | SkRect* rect = as_rect(crect); |
| 278 | |
| 279 | if (path.isEmpty()) { |
| 280 | if (rect) { |
| 281 | rect->setEmpty(); |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | *rect = path.getBounds(); |
| 286 | return true; |
| 287 | } |
| 288 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 289 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 290 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 291 | void sk_canvas_save(sk_canvas_t* ccanvas) { |
| 292 | AsCanvas(ccanvas)->save(); |
| 293 | } |
| 294 | |
| 295 | void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 296 | AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint)); |
| 297 | } |
| 298 | |
| 299 | void sk_canvas_restore(sk_canvas_t* ccanvas) { |
| 300 | AsCanvas(ccanvas)->restore(); |
| 301 | } |
| 302 | |
| 303 | void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) { |
| 304 | AsCanvas(ccanvas)->translate(dx, dy); |
| 305 | } |
| 306 | |
| 307 | void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 308 | AsCanvas(ccanvas)->scale(sx, sy); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) { |
| 312 | AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint)); |
| 313 | } |
| 314 | |
| 315 | void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 316 | AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint)); |
| 317 | } |
| 318 | |
| 319 | void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 320 | AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint)); |
| 321 | } |
| 322 | |
| 323 | void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) { |
| 324 | AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint)); |
| 325 | } |
| 326 | |
| 327 | void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y, |
| 328 | const sk_paint_t* cpaint) { |
| 329 | AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint)); |
| 330 | } |
| 331 | |
| 332 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 333 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 334 | sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 335 | SkImageInfo info; |
| 336 | if (!from_c_info(*cinfo, &info)) { |
| 337 | return NULL; |
| 338 | } |
| 339 | return (sk_surface_t*)SkSurface::NewRaster(info); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 340 | } |
| 341 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 342 | sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels, |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 343 | size_t rowBytes) { |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 344 | SkImageInfo info; |
| 345 | if (!from_c_info(*cinfo, &info)) { |
| 346 | return NULL; |
| 347 | } |
| 348 | return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 349 | } |
| 350 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame^] | 351 | void sk_surface_unref(sk_surface_t* csurf) { |
| 352 | SkSafeUnref((SkSurface*)csurf); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) { |
| 356 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 357 | return (sk_canvas_t*)surf->getCanvas(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) { |
| 361 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 362 | return (sk_image_t*)surf->newImageSnapshot(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 363 | } |
| 364 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame^] | 365 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 366 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame^] | 367 | #include "../../include/effects/SkGradientShader.h" |
| 368 | #include "sk_shader.h" |
| 369 | |
| 370 | const struct { |
| 371 | sk_shader_tilemode_t fC; |
| 372 | SkShader::TileMode fSK; |
| 373 | } gTileModeMap[] = { |
| 374 | { CLAMP_SK_SHADER_TILEMODE, SkShader::kClamp_TileMode }, |
| 375 | { REPEAT_SK_SHADER_TILEMODE, SkShader::kRepeat_TileMode }, |
| 376 | { MIRROR_SK_SHADER_TILEMODE, SkShader::kMirror_TileMode }, |
| 377 | }; |
| 378 | |
| 379 | static bool from_c_tilemode(sk_shader_tilemode_t cMode, SkShader::TileMode* skMode) { |
| 380 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTileModeMap); ++i) { |
| 381 | if (cMode == gTileModeMap[i].fC) { |
| 382 | if (skMode) { |
| 383 | *skMode = gTileModeMap[i].fSK; |
| 384 | } |
| 385 | return true; |
| 386 | } |
| 387 | } |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | void sk_shader_ref(sk_shader_t* cshader) { |
| 392 | SkSafeRef(AsShader(cshader)); |
| 393 | } |
| 394 | |
| 395 | void sk_shader_unref(sk_shader_t* cshader) { |
| 396 | SkSafeUnref(AsShader(cshader)); |
| 397 | } |
| 398 | |
| 399 | sk_shader_t* sk_shader_new_linear_gradient(const sk_point_t pts[2], |
| 400 | const sk_color_t colors[], |
| 401 | const float colorPos[], |
| 402 | int colorCount, |
| 403 | sk_shader_tilemode_t cmode, |
| 404 | const sk_matrix_t* cmatrix) { |
| 405 | SkShader::TileMode mode; |
| 406 | if (!from_c_tilemode(cmode, &mode)) { |
| 407 | return NULL; |
| 408 | } |
| 409 | SkMatrix matrix; |
| 410 | if (cmatrix) { |
| 411 | matrix.setAll(cmatrix->mat[0], cmatrix->mat[1], cmatrix->mat[2], |
| 412 | cmatrix->mat[3], cmatrix->mat[4], cmatrix->mat[5], |
| 413 | cmatrix->mat[6], cmatrix->mat[7], cmatrix->mat[8]); |
| 414 | } else { |
| 415 | matrix.setIdentity(); |
| 416 | } |
| 417 | SkShader* s = SkGradientShader::CreateLinear(reinterpret_cast<const SkPoint*>(pts), |
| 418 | reinterpret_cast<const SkColor*>(colors), |
| 419 | colorPos, colorCount, mode, 0, &matrix); |
| 420 | return (sk_shader_t*)s; |
| 421 | } |
| 422 | |
| 423 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 424 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 425 | |
| 426 | void sk_test_capi(SkCanvas* canvas) { |
| 427 | sk_imageinfo_t cinfo; |
| 428 | cinfo.width = 100; |
| 429 | cinfo.height = 100; |
| 430 | cinfo.colorType = (sk_colortype_t)kN32_SkColorType; |
| 431 | cinfo.alphaType = (sk_alphatype_t)kPremul_SkAlphaType; |
| 432 | |
| 433 | sk_surface_t* csurface = sk_surface_new_raster(&cinfo); |
| 434 | sk_canvas_t* ccanvas = sk_surface_get_canvas(csurface); |
| 435 | |
| 436 | sk_paint_t* cpaint = sk_paint_new(); |
| 437 | sk_paint_set_antialias(cpaint, true); |
| 438 | sk_paint_set_color(cpaint, 0xFFFF0000); |
| 439 | |
| 440 | sk_rect_t cr = { 5, 5, 95, 95 }; |
| 441 | sk_canvas_draw_oval(ccanvas, &cr, cpaint); |
| 442 | |
| 443 | cr.left += 25; |
| 444 | cr.top += 25; |
| 445 | cr.right -= 25; |
| 446 | cr.bottom -= 25; |
| 447 | sk_paint_set_color(cpaint, 0xFF00FF00); |
| 448 | sk_canvas_draw_rect(ccanvas, &cr, cpaint); |
| 449 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 450 | sk_path_t* cpath = sk_path_new(); |
| 451 | sk_path_move_to(cpath, 50, 50); |
| 452 | sk_path_line_to(cpath, 100, 100); |
| 453 | sk_path_line_to(cpath, 50, 100); |
| 454 | sk_path_close(cpath); |
| 455 | |
| 456 | sk_canvas_draw_path(ccanvas, cpath, cpaint); |
| 457 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 458 | sk_image_t* cimage = sk_surface_new_image_snapshot(csurface); |
| 459 | |
| 460 | // HERE WE CROSS THE C..C++ boundary |
| 461 | canvas->drawImage((const SkImage*)cimage, 20, 20, NULL); |
| 462 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 463 | sk_path_delete(cpath); |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 464 | sk_paint_delete(cpaint); |
| 465 | sk_image_unref(cimage); |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame^] | 466 | sk_surface_unref(csurface); |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 467 | } |