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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkData.h" |
| 10 | #include "include/core/SkImage.h" |
| 11 | #include "include/core/SkMaskFilter.h" |
| 12 | #include "include/core/SkMatrix.h" |
| 13 | #include "include/core/SkPaint.h" |
Mike Reed | e9d783c | 2020-08-17 14:14:13 -0400 | [diff] [blame] | 14 | #include "include/core/SkPathBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/core/SkPictureRecorder.h" |
| 16 | #include "include/core/SkSurface.h" |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 17 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "include/c/sk_canvas.h" |
| 19 | #include "include/c/sk_data.h" |
| 20 | #include "include/c/sk_image.h" |
| 21 | #include "include/c/sk_paint.h" |
| 22 | #include "include/c/sk_path.h" |
Stan Iliev | 8231031 | 2019-06-05 16:55:54 -0400 | [diff] [blame] | 23 | #include "include/c/sk_picture.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 24 | #include "include/c/sk_surface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/c/sk_types_priv.h" |
halcanary | 219f18f | 2015-09-01 10:01:38 -0700 | [diff] [blame] | 26 | |
reed | e332396 | 2014-10-24 11:16:19 -0700 | [diff] [blame] | 27 | const struct { |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 28 | sk_pixelgeometry_t fC; |
| 29 | SkPixelGeometry fSK; |
| 30 | } gPixelGeometryMap[] = { |
| 31 | { UNKNOWN_SK_PIXELGEOMETRY, kUnknown_SkPixelGeometry }, |
| 32 | { RGB_H_SK_PIXELGEOMETRY, kRGB_H_SkPixelGeometry }, |
| 33 | { BGR_H_SK_PIXELGEOMETRY, kBGR_H_SkPixelGeometry }, |
| 34 | { RGB_V_SK_PIXELGEOMETRY, kRGB_V_SkPixelGeometry }, |
| 35 | { BGR_V_SK_PIXELGEOMETRY, kBGR_V_SkPixelGeometry }, |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | static bool from_c_pixelgeometry(sk_pixelgeometry_t cGeom, SkPixelGeometry* skGeom) { |
| 40 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPixelGeometryMap); ++i) { |
| 41 | if (gPixelGeometryMap[i].fC == cGeom) { |
| 42 | if (skGeom) { |
| 43 | *skGeom = gPixelGeometryMap[i].fSK; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
abarth | 9503ac7 | 2014-12-02 15:47:33 -0800 | [diff] [blame] | 51 | static void from_c_matrix(const sk_matrix_t* cmatrix, SkMatrix* matrix) { |
| 52 | matrix->setAll(cmatrix->mat[0], cmatrix->mat[1], cmatrix->mat[2], |
| 53 | cmatrix->mat[3], cmatrix->mat[4], cmatrix->mat[5], |
| 54 | cmatrix->mat[6], cmatrix->mat[7], cmatrix->mat[8]); |
| 55 | } |
| 56 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 57 | const struct { |
| 58 | sk_path_direction_t fC; |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 59 | SkPathDirection fSk; |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 60 | } gPathDirMap[] = { |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 61 | { CW_SK_PATH_DIRECTION, SkPathDirection::kCW }, |
| 62 | { CCW_SK_PATH_DIRECTION, SkPathDirection::kCCW }, |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
Mike Reed | 30bc527 | 2019-11-22 18:34:02 +0000 | [diff] [blame] | 65 | static bool from_c_path_direction(sk_path_direction_t cdir, SkPathDirection* dir) { |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 66 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPathDirMap); ++i) { |
| 67 | if (gPathDirMap[i].fC == cdir) { |
| 68 | if (dir) { |
| 69 | *dir = gPathDirMap[i].fSk; |
| 70 | } |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 77 | static SkData* AsData(const sk_data_t* cdata) { |
| 78 | return reinterpret_cast<SkData*>(const_cast<sk_data_t*>(cdata)); |
| 79 | } |
| 80 | |
| 81 | static sk_data_t* ToData(SkData* data) { |
| 82 | return reinterpret_cast<sk_data_t*>(data); |
| 83 | } |
| 84 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 85 | static sk_rect_t ToRect(const SkRect& rect) { |
| 86 | return reinterpret_cast<const sk_rect_t&>(rect); |
| 87 | } |
| 88 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 89 | static const SkRect& AsRect(const sk_rect_t& crect) { |
| 90 | return reinterpret_cast<const SkRect&>(crect); |
| 91 | } |
| 92 | |
| 93 | static const SkPath& AsPath(const sk_path_t& cpath) { |
| 94 | return reinterpret_cast<const SkPath&>(cpath); |
| 95 | } |
| 96 | |
Mike Reed | e9d783c | 2020-08-17 14:14:13 -0400 | [diff] [blame] | 97 | static SkPathBuilder* as_pathbuilder(sk_pathbuilder_t* cbuilder) { |
| 98 | return reinterpret_cast<SkPathBuilder*>(cbuilder); |
| 99 | } |
| 100 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 101 | static SkPath* as_path(sk_path_t* cpath) { |
| 102 | return reinterpret_cast<SkPath*>(cpath); |
| 103 | } |
| 104 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 105 | static const SkImage* AsImage(const sk_image_t* cimage) { |
| 106 | return reinterpret_cast<const SkImage*>(cimage); |
| 107 | } |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 108 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 109 | static sk_image_t* ToImage(SkImage* cimage) { |
| 110 | return reinterpret_cast<sk_image_t*>(cimage); |
| 111 | } |
| 112 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 113 | static sk_canvas_t* ToCanvas(SkCanvas* canvas) { |
| 114 | return reinterpret_cast<sk_canvas_t*>(canvas); |
| 115 | } |
| 116 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 117 | static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) { |
| 118 | return reinterpret_cast<SkCanvas*>(ccanvas); |
| 119 | } |
| 120 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 121 | static SkPictureRecorder* AsPictureRecorder(sk_picture_recorder_t* crec) { |
| 122 | return reinterpret_cast<SkPictureRecorder*>(crec); |
| 123 | } |
| 124 | |
| 125 | static sk_picture_recorder_t* ToPictureRecorder(SkPictureRecorder* rec) { |
| 126 | return reinterpret_cast<sk_picture_recorder_t*>(rec); |
| 127 | } |
| 128 | |
| 129 | static const SkPicture* AsPicture(const sk_picture_t* cpic) { |
| 130 | return reinterpret_cast<const SkPicture*>(cpic); |
| 131 | } |
| 132 | |
| 133 | static SkPicture* AsPicture(sk_picture_t* cpic) { |
| 134 | return reinterpret_cast<SkPicture*>(cpic); |
| 135 | } |
| 136 | |
| 137 | static sk_picture_t* ToPicture(SkPicture* pic) { |
| 138 | return reinterpret_cast<sk_picture_t*>(pic); |
| 139 | } |
| 140 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 141 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels, |
| 144 | size_t rowBytes) { |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 145 | const SkImageInfo* info = reinterpret_cast<const SkImageInfo*>(cinfo); |
| 146 | return (sk_image_t*)SkImage::MakeRasterCopy(SkPixmap(*info, pixels, rowBytes)).release(); |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Mike Reed | 564d49e | 2020-07-28 12:52:31 -0400 | [diff] [blame] | 149 | sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata) { |
| 150 | return ToImage(SkImage::MakeFromEncoded(sk_ref_sp(AsData(cdata))).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | sk_data_t* sk_image_encode(const sk_image_t* cimage) { |
Mike Reed | 6409f84 | 2017-07-11 16:03:13 -0400 | [diff] [blame] | 154 | return ToData(AsImage(cimage)->encodeToData().release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 155 | } |
| 156 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 157 | void sk_image_ref(const sk_image_t* cimage) { |
| 158 | AsImage(cimage)->ref(); |
| 159 | } |
| 160 | |
| 161 | void sk_image_unref(const sk_image_t* cimage) { |
| 162 | AsImage(cimage)->unref(); |
| 163 | } |
| 164 | |
| 165 | int sk_image_get_width(const sk_image_t* cimage) { |
| 166 | return AsImage(cimage)->width(); |
| 167 | } |
| 168 | |
| 169 | int sk_image_get_height(const sk_image_t* cimage) { |
| 170 | return AsImage(cimage)->height(); |
| 171 | } |
| 172 | |
| 173 | uint32_t sk_image_get_unique_id(const sk_image_t* cimage) { |
| 174 | return AsImage(cimage)->uniqueID(); |
| 175 | } |
| 176 | |
| 177 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 178 | |
Mike Reed | e9d783c | 2020-08-17 14:14:13 -0400 | [diff] [blame] | 179 | sk_pathbuilder_t* sk_pathbuilder_new() { return (sk_pathbuilder_t*)new SkPathBuilder; } |
| 180 | |
| 181 | void sk_pathbuilder_delete(sk_pathbuilder_t* cbuilder) { delete as_pathbuilder(cbuilder); } |
| 182 | |
| 183 | void sk_pathbuilder_move_to(sk_pathbuilder_t* cbuilder, float x, float y) { |
| 184 | as_pathbuilder(cbuilder)->moveTo(x, y); |
| 185 | } |
| 186 | |
| 187 | void sk_pathbuilder_line_to(sk_pathbuilder_t* cbuilder, float x, float y) { |
| 188 | as_pathbuilder(cbuilder)->lineTo(x, y); |
| 189 | } |
| 190 | |
| 191 | void sk_pathbuilder_quad_to(sk_pathbuilder_t* cbuilder, |
| 192 | float x0, float y0, float x1, float y1) { |
| 193 | as_pathbuilder(cbuilder)->quadTo(x0, y0, x1, y1); |
| 194 | } |
| 195 | |
| 196 | void sk_pathbuilder_conic_to(sk_pathbuilder_t* cbuilder, |
| 197 | float x0, float y0, float x1, float y1, float w) { |
| 198 | as_pathbuilder(cbuilder)->conicTo(x0, y0, x1, y1, w); |
| 199 | } |
| 200 | |
| 201 | void sk_pathbuilder_cubic_to(sk_pathbuilder_t* cbuilder, |
| 202 | float x0, float y0, float x1, float y1, float x2, float y2) { |
| 203 | as_pathbuilder(cbuilder)->cubicTo(x0, y0, x1, y1, x2, y2); |
| 204 | } |
| 205 | |
| 206 | void sk_pathbuilder_close(sk_pathbuilder_t* cbuilder) { |
| 207 | as_pathbuilder(cbuilder)->close(); |
| 208 | } |
| 209 | |
| 210 | void sk_pathbuilder_add_rect(sk_pathbuilder_t* cbuilder, const sk_rect_t* crect, sk_path_direction_t cdir) { |
| 211 | SkPathDirection dir; |
| 212 | if (!from_c_path_direction(cdir, &dir)) { |
| 213 | return; |
| 214 | } |
| 215 | as_pathbuilder(cbuilder)->addRect(AsRect(*crect), dir); |
| 216 | } |
| 217 | |
| 218 | void sk_pathbuilder_add_oval(sk_pathbuilder_t* cbuilder, const sk_rect_t* crect, sk_path_direction_t cdir) { |
| 219 | SkPathDirection dir; |
| 220 | if (!from_c_path_direction(cdir, &dir)) { |
| 221 | return; |
| 222 | } |
| 223 | as_pathbuilder(cbuilder)->addOval(AsRect(*crect), dir); |
| 224 | } |
| 225 | |
| 226 | sk_path_t* sk_pathbuilder_detach_path(sk_pathbuilder_t* cbuilder) { |
| 227 | return (sk_path_t*)(new SkPath(as_pathbuilder(cbuilder)->detach())); |
| 228 | } |
| 229 | |
| 230 | sk_path_t* sk_pathbuilder_snapshot_path(sk_pathbuilder_t* cbuilder) { |
| 231 | return (sk_path_t*)(new SkPath(as_pathbuilder(cbuilder)->snapshot())); |
| 232 | } |
| 233 | |
| 234 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 235 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 236 | void sk_path_delete(sk_path_t* cpath) { delete as_path(cpath); } |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 237 | |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 238 | bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) { |
| 239 | const SkPath& path = AsPath(*cpath); |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 240 | |
| 241 | if (path.isEmpty()) { |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 242 | if (crect) { |
| 243 | *crect = ToRect(SkRect::MakeEmpty()); |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 244 | } |
| 245 | return false; |
| 246 | } |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 247 | |
| 248 | if (crect) { |
| 249 | *crect = ToRect(path.getBounds()); |
| 250 | } |
reed | 73c2501 | 2014-11-17 06:15:42 -0800 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 254 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 255 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 256 | void sk_canvas_save(sk_canvas_t* ccanvas) { |
| 257 | AsCanvas(ccanvas)->save(); |
| 258 | } |
| 259 | |
| 260 | void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 261 | AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint)); |
| 262 | } |
| 263 | |
| 264 | void sk_canvas_restore(sk_canvas_t* ccanvas) { |
| 265 | AsCanvas(ccanvas)->restore(); |
| 266 | } |
| 267 | |
| 268 | void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) { |
| 269 | AsCanvas(ccanvas)->translate(dx, dy); |
| 270 | } |
| 271 | |
| 272 | void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 273 | AsCanvas(ccanvas)->scale(sx, sy); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Mike Klein | fa2d468 | 2020-10-28 16:25:37 -0500 | [diff] [blame] | 276 | void sk_canvas_rotate_degrees(sk_canvas_t* ccanvas, float degrees) { |
abarth | 2fc6ea6 | 2014-12-01 14:04:03 -0800 | [diff] [blame] | 277 | AsCanvas(ccanvas)->rotate(degrees); |
| 278 | } |
| 279 | |
| 280 | void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) { |
| 281 | AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians)); |
| 282 | } |
| 283 | |
| 284 | void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) { |
| 285 | AsCanvas(ccanvas)->skew(sx, sy); |
| 286 | } |
| 287 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 288 | void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) { |
abarth | 9503ac7 | 2014-12-02 15:47:33 -0800 | [diff] [blame] | 289 | SkASSERT(cmatrix); |
| 290 | SkMatrix matrix; |
| 291 | from_c_matrix(cmatrix, &matrix); |
| 292 | AsCanvas(ccanvas)->concat(matrix); |
| 293 | } |
| 294 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 295 | void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) { |
| 296 | AsCanvas(ccanvas)->clipRect(AsRect(*crect)); |
| 297 | } |
| 298 | |
| 299 | void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) { |
| 300 | AsCanvas(ccanvas)->clipPath(AsPath(*cpath)); |
| 301 | } |
| 302 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 303 | void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) { |
| 304 | AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint)); |
| 305 | } |
| 306 | |
| 307 | void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 308 | AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint)); |
| 309 | } |
| 310 | |
reed | 4a7940b | 2016-02-09 13:25:51 -0800 | [diff] [blame] | 311 | void sk_canvas_draw_circle(sk_canvas_t* ccanvas, float cx, float cy, float rad, |
| 312 | const sk_paint_t* cpaint) { |
| 313 | AsCanvas(ccanvas)->drawCircle(cx, cy, rad, AsPaint(*cpaint)); |
| 314 | } |
| 315 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 316 | void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 317 | AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint)); |
| 318 | } |
| 319 | |
| 320 | void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) { |
| 321 | AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint)); |
| 322 | } |
| 323 | |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 324 | static SkSamplingOptions to_sampling(const sk_sampling_options_t* s) { |
| 325 | if (s) { |
| 326 | if (s->useCubic) { |
| 327 | return SkSamplingOptions({s->cubic.B, s->cubic.C}); |
| 328 | } else { |
| 329 | return SkSamplingOptions(SkFilterMode(s->filter), SkMipmapMode(s->mipmap)); |
| 330 | } |
| 331 | } |
| 332 | return SkSamplingOptions(); |
| 333 | } |
| 334 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 335 | void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y, |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 336 | const sk_sampling_options_t* csamp, const sk_paint_t* cpaint) { |
| 337 | AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, to_sampling(csamp), AsPaint(cpaint)); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 338 | } |
| 339 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 340 | void sk_canvas_draw_image_rect(sk_canvas_t* ccanvas, const sk_image_t* cimage, |
| 341 | const sk_rect_t* csrcR, const sk_rect_t* cdstR, |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 342 | const sk_sampling_options_t* csamp, const sk_paint_t* cpaint) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 343 | SkCanvas* canvas = AsCanvas(ccanvas); |
| 344 | const SkImage* image = AsImage(cimage); |
| 345 | const SkRect& dst = AsRect(*cdstR); |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 346 | const SkSamplingOptions sampling = to_sampling(csamp); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 347 | const SkPaint* paint = AsPaint(cpaint); |
| 348 | |
| 349 | if (csrcR) { |
Mike Reed | 18aeb57 | 2021-01-19 17:58:25 -0500 | [diff] [blame] | 350 | canvas->drawImageRect(image, AsRect(*csrcR), dst, sampling, paint, |
| 351 | SkCanvas::kStrict_SrcRectConstraint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 352 | } else { |
Mike Reed | 34c56a5 | 2021-01-22 15:26:41 -0500 | [diff] [blame] | 353 | canvas->drawImageRect(image, dst, sampling, paint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 354 | } |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | void sk_canvas_draw_picture(sk_canvas_t* ccanvas, const sk_picture_t* cpicture, |
| 358 | const sk_matrix_t* cmatrix, const sk_paint_t* cpaint) { |
John Stiles | fe0de30 | 2020-08-14 10:52:06 -0400 | [diff] [blame] | 359 | const SkMatrix* matrixPtr = nullptr; |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 360 | SkMatrix matrix; |
| 361 | if (cmatrix) { |
| 362 | from_c_matrix(cmatrix, &matrix); |
| 363 | matrixPtr = &matrix; |
| 364 | } |
| 365 | AsCanvas(ccanvas)->drawPicture(AsPicture(cpicture), matrixPtr, AsPaint(cpaint)); |
| 366 | } |
| 367 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 368 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 369 | |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 370 | sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo, |
| 371 | const sk_surfaceprops_t* props) { |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 372 | const SkImageInfo* info = reinterpret_cast<const SkImageInfo*>(cinfo); |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 373 | SkPixelGeometry geo = kUnknown_SkPixelGeometry; |
| 374 | if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) { |
John Stiles | fe0de30 | 2020-08-14 10:52:06 -0400 | [diff] [blame] | 375 | return nullptr; |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | SkSurfaceProps surfProps(0, geo); |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 379 | return (sk_surface_t*)SkSurface::MakeRaster(*info, &surfProps).release(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 380 | } |
| 381 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 382 | 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] | 383 | size_t rowBytes, |
| 384 | const sk_surfaceprops_t* props) { |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 385 | const SkImageInfo* info = reinterpret_cast<const SkImageInfo*>(cinfo); |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 386 | SkPixelGeometry geo = kUnknown_SkPixelGeometry; |
| 387 | if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) { |
John Stiles | fe0de30 | 2020-08-14 10:52:06 -0400 | [diff] [blame] | 388 | return nullptr; |
robertphillips | 702edbd | 2015-06-23 06:26:08 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | SkSurfaceProps surfProps(0, geo); |
Mike Reed | 66c505f | 2018-09-28 11:27:57 -0400 | [diff] [blame] | 392 | return (sk_surface_t*)SkSurface::MakeRasterDirect(*info, pixels, rowBytes, &surfProps).release(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 393 | } |
| 394 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 395 | void sk_surface_unref(sk_surface_t* csurf) { |
| 396 | SkSafeUnref((SkSurface*)csurf); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) { |
| 400 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 401 | return (sk_canvas_t*)surf->getCanvas(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) { |
| 405 | SkSurface* surf = (SkSurface*)csurf; |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 406 | return (sk_image_t*)surf->makeImageSnapshot().release(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 407 | } |
| 408 | |
reed | afa278e | 2014-11-24 19:11:48 -0800 | [diff] [blame] | 409 | /////////////////////////////////////////////////////////////////////////////////////////// |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 410 | |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 411 | sk_picture_recorder_t* sk_picture_recorder_new() { |
| 412 | return ToPictureRecorder(new SkPictureRecorder); |
| 413 | } |
| 414 | |
| 415 | void sk_picture_recorder_delete(sk_picture_recorder_t* crec) { |
| 416 | delete AsPictureRecorder(crec); |
| 417 | } |
| 418 | |
| 419 | sk_canvas_t* sk_picture_recorder_begin_recording(sk_picture_recorder_t* crec, |
| 420 | const sk_rect_t* cbounds) { |
| 421 | return ToCanvas(AsPictureRecorder(crec)->beginRecording(AsRect(*cbounds))); |
| 422 | } |
| 423 | |
| 424 | sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) { |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 425 | return ToPicture(AsPictureRecorder(crec)->finishRecordingAsPicture().release()); |
reed | e371989 | 2014-12-22 17:46:00 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void sk_picture_ref(sk_picture_t* cpic) { |
| 429 | SkSafeRef(AsPicture(cpic)); |
| 430 | } |
| 431 | |
| 432 | void sk_picture_unref(sk_picture_t* cpic) { |
| 433 | SkSafeUnref(AsPicture(cpic)); |
| 434 | } |
| 435 | |
| 436 | uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) { |
| 437 | return AsPicture(cpic)->uniqueID(); |
| 438 | } |
| 439 | |
| 440 | sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) { |
| 441 | return ToRect(AsPicture(cpic)->cullRect()); |
| 442 | } |
| 443 | |
| 444 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 445 | |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 446 | 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] | 447 | return ToData(SkData::MakeWithCopy(src, length).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | 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] | 451 | return ToData(SkData::MakeFromMalloc(memory, length).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | 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] | 455 | return ToData(SkData::MakeSubset(AsData(csrc), offset, length).release()); |
reed | b2a5d7e | 2014-12-25 14:16:21 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void sk_data_ref(const sk_data_t* cdata) { |
| 459 | SkSafeRef(AsData(cdata)); |
| 460 | } |
| 461 | |
| 462 | void sk_data_unref(const sk_data_t* cdata) { |
| 463 | SkSafeUnref(AsData(cdata)); |
| 464 | } |
| 465 | |
| 466 | size_t sk_data_get_size(const sk_data_t* cdata) { |
| 467 | return AsData(cdata)->size(); |
| 468 | } |
| 469 | |
| 470 | const void* sk_data_get_data(const sk_data_t* cdata) { |
| 471 | return AsData(cdata)->data(); |
| 472 | } |
| 473 | |
| 474 | /////////////////////////////////////////////////////////////////////////////////////////// |