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 "sk_surface.h" |
| 9 | |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkImage.h" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkPaint.h" |
| 14 | #include "SkPath.h" |
| 15 | #include "SkSurface.h" |
| 16 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 17 | static SkImageInfo make(const sk_imageinfo_t& cinfo) { |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 18 | return SkImageInfo::Make(cinfo.width, cinfo.height, |
| 19 | (SkColorType)cinfo.colorType, (SkAlphaType)cinfo.alphaType); |
| 20 | } |
| 21 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 22 | static const SkRect& AsRect(const sk_rect_t& crect) { |
| 23 | return reinterpret_cast<const SkRect&>(crect); |
| 24 | } |
| 25 | |
| 26 | static const SkPath& AsPath(const sk_path_t& cpath) { |
| 27 | return reinterpret_cast<const SkPath&>(cpath); |
| 28 | } |
| 29 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 30 | static SkPath* as_path(sk_path_t* cpath) { |
| 31 | return reinterpret_cast<SkPath*>(cpath); |
| 32 | } |
| 33 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 34 | static const SkImage* AsImage(const sk_image_t* cimage) { |
| 35 | return reinterpret_cast<const SkImage*>(cimage); |
| 36 | } |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 37 | |
| 38 | static const SkPaint& AsPaint(const sk_paint_t& cpaint) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 39 | return reinterpret_cast<const SkPaint&>(cpaint); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static const SkPaint* AsPaint(const sk_paint_t* cpaint) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 43 | return reinterpret_cast<const SkPaint*>(cpaint); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 44 | } |
| 45 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 46 | static SkPaint* AsPaint(sk_paint_t* cpaint) { |
| 47 | return reinterpret_cast<SkPaint*>(cpaint); |
| 48 | } |
| 49 | |
| 50 | static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) { |
| 51 | return reinterpret_cast<SkCanvas*>(ccanvas); |
| 52 | } |
| 53 | |
| 54 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 | sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels, |
| 57 | size_t rowBytes) { |
| 58 | return (sk_image_t*)SkImage::NewRasterCopy(make(*cinfo), pixels, rowBytes); |
| 59 | } |
| 60 | |
| 61 | void sk_image_ref(const sk_image_t* cimage) { |
| 62 | AsImage(cimage)->ref(); |
| 63 | } |
| 64 | |
| 65 | void sk_image_unref(const sk_image_t* cimage) { |
| 66 | AsImage(cimage)->unref(); |
| 67 | } |
| 68 | |
| 69 | int sk_image_get_width(const sk_image_t* cimage) { |
| 70 | return AsImage(cimage)->width(); |
| 71 | } |
| 72 | |
| 73 | int sk_image_get_height(const sk_image_t* cimage) { |
| 74 | return AsImage(cimage)->height(); |
| 75 | } |
| 76 | |
| 77 | uint32_t sk_image_get_unique_id(const sk_image_t* cimage) { |
| 78 | return AsImage(cimage)->uniqueID(); |
| 79 | } |
| 80 | |
| 81 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | sk_paint_t* sk_paint_new() { |
| 84 | return (sk_paint_t*)SkNEW(SkPaint); |
| 85 | } |
| 86 | |
| 87 | void sk_paint_delete(sk_paint_t* cpaint) { |
| 88 | SkDELETE(AsPaint(cpaint)); |
| 89 | } |
| 90 | |
| 91 | bool sk_paint_is_antialias(const sk_paint_t* cpaint) { |
| 92 | return AsPaint(*cpaint).isAntiAlias(); |
| 93 | } |
| 94 | |
| 95 | void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) { |
| 96 | AsPaint(cpaint)->setAntiAlias(aa); |
| 97 | } |
| 98 | |
| 99 | sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) { |
| 100 | return AsPaint(*cpaint).getColor(); |
| 101 | } |
| 102 | |
| 103 | void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) { |
| 104 | AsPaint(cpaint)->setColor(c); |
| 105 | } |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 106 | |
| 107 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 108 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 109 | sk_path_t* sk_path_new() { |
| 110 | return (sk_path_t*)SkNEW(SkPath); |
| 111 | } |
| 112 | |
| 113 | void sk_path_delete(sk_path_t* cpath) { |
| 114 | SkDELETE(as_path(cpath)); |
| 115 | } |
| 116 | |
| 117 | void sk_path_move_to(sk_path_t* cpath, float x, float y) { |
| 118 | as_path(cpath)->moveTo(x, y); |
| 119 | } |
| 120 | |
| 121 | void sk_path_line_to(sk_path_t* cpath, float x, float y) { |
| 122 | as_path(cpath)->lineTo(x, y); |
| 123 | } |
| 124 | |
| 125 | void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) { |
| 126 | as_path(cpath)->quadTo(x0, y0, x1, y1); |
| 127 | } |
| 128 | |
| 129 | void sk_path_close(sk_path_t* cpath) { |
| 130 | as_path(cpath)->close(); |
| 131 | } |
| 132 | |
| 133 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 134 | |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 135 | void sk_canvas_save(sk_canvas_t* ccanvas) { |
| 136 | AsCanvas(ccanvas)->save(); |
| 137 | } |
| 138 | |
| 139 | void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 140 | AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint)); |
| 141 | } |
| 142 | |
| 143 | void sk_canvas_restore(sk_canvas_t* ccanvas) { |
| 144 | AsCanvas(ccanvas)->restore(); |
| 145 | } |
| 146 | |
| 147 | void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) { |
| 148 | AsCanvas(ccanvas)->translate(dx, dy); |
| 149 | } |
| 150 | |
| 151 | void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) { |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 152 | AsCanvas(ccanvas)->scale(sx, sy); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) { |
| 156 | AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint)); |
| 157 | } |
| 158 | |
| 159 | void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 160 | AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint)); |
| 161 | } |
| 162 | |
| 163 | void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) { |
| 164 | AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint)); |
| 165 | } |
| 166 | |
| 167 | void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) { |
| 168 | AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint)); |
| 169 | } |
| 170 | |
| 171 | void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y, |
| 172 | const sk_paint_t* cpaint) { |
| 173 | AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint)); |
| 174 | } |
| 175 | |
| 176 | /////////////////////////////////////////////////////////////////////////////////////////// |
| 177 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 178 | sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) { |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 179 | return (sk_surface_t*)SkSurface::NewRaster(make(*cinfo)); |
| 180 | } |
| 181 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 182 | 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] | 183 | size_t rowBytes) { |
| 184 | return (sk_surface_t*)SkSurface::NewRasterDirect(make(*cinfo), pixels, rowBytes); |
| 185 | } |
| 186 | |
| 187 | void sk_surface_delete(sk_surface_t* csurf) { |
| 188 | SkSurface* surf = (SkSurface*)csurf; |
| 189 | SkSafeUnref(surf); |
| 190 | } |
| 191 | |
| 192 | sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) { |
| 193 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 194 | return (sk_canvas_t*)surf->getCanvas(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) { |
| 198 | SkSurface* surf = (SkSurface*)csurf; |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 199 | return (sk_image_t*)surf->newImageSnapshot(); |
reed | 938dfba | 2014-10-06 06:08:16 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 203 | /////////////////// |
| 204 | |
| 205 | void sk_test_capi(SkCanvas* canvas) { |
| 206 | sk_imageinfo_t cinfo; |
| 207 | cinfo.width = 100; |
| 208 | cinfo.height = 100; |
| 209 | cinfo.colorType = (sk_colortype_t)kN32_SkColorType; |
| 210 | cinfo.alphaType = (sk_alphatype_t)kPremul_SkAlphaType; |
| 211 | |
| 212 | sk_surface_t* csurface = sk_surface_new_raster(&cinfo); |
| 213 | sk_canvas_t* ccanvas = sk_surface_get_canvas(csurface); |
| 214 | |
| 215 | sk_paint_t* cpaint = sk_paint_new(); |
| 216 | sk_paint_set_antialias(cpaint, true); |
| 217 | sk_paint_set_color(cpaint, 0xFFFF0000); |
| 218 | |
| 219 | sk_rect_t cr = { 5, 5, 95, 95 }; |
| 220 | sk_canvas_draw_oval(ccanvas, &cr, cpaint); |
| 221 | |
| 222 | cr.left += 25; |
| 223 | cr.top += 25; |
| 224 | cr.right -= 25; |
| 225 | cr.bottom -= 25; |
| 226 | sk_paint_set_color(cpaint, 0xFF00FF00); |
| 227 | sk_canvas_draw_rect(ccanvas, &cr, cpaint); |
| 228 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 229 | sk_path_t* cpath = sk_path_new(); |
| 230 | sk_path_move_to(cpath, 50, 50); |
| 231 | sk_path_line_to(cpath, 100, 100); |
| 232 | sk_path_line_to(cpath, 50, 100); |
| 233 | sk_path_close(cpath); |
| 234 | |
| 235 | sk_canvas_draw_path(ccanvas, cpath, cpaint); |
| 236 | |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 237 | sk_image_t* cimage = sk_surface_new_image_snapshot(csurface); |
| 238 | |
| 239 | // HERE WE CROSS THE C..C++ boundary |
| 240 | canvas->drawImage((const SkImage*)cimage, 20, 20, NULL); |
| 241 | |
robertphillips | a624e87 | 2014-10-08 06:04:35 -0700 | [diff] [blame] | 242 | sk_path_delete(cpath); |
reed | 8e47478 | 2014-10-06 11:00:51 -0700 | [diff] [blame] | 243 | sk_paint_delete(cpaint); |
| 244 | sk_image_unref(cimage); |
| 245 | sk_surface_delete(csurface); |
| 246 | } |