OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "sk_surface.h" | 8 #include "sk_surface.h" |
9 | 9 |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkImage.h" | 11 #include "SkImage.h" |
12 #include "SkMatrix.h" | 12 #include "SkMatrix.h" |
13 #include "SkPaint.h" | 13 #include "SkPaint.h" |
14 #include "SkPath.h" | 14 #include "SkPath.h" |
15 #include "SkSurface.h" | 15 #include "SkSurface.h" |
16 | 16 |
17 static SkImageInfo make(const sk_imageinfo_t& cinfo) { | 17 const struct { |
18 return SkImageInfo::Make(cinfo.width, cinfo.height, | 18 sk_colortype_t fC; |
19 (SkColorType)cinfo.colorType, (SkAlphaType)cinfo.al
phaType); | 19 SkColorType fSK; |
| 20 } gColorTypeMap[] = { |
| 21 { UNKNOWN_SK_COLORTYPE, kUnknown_SkColorType }, |
| 22 { RGBA_8888_SK_COLORTYPE, kRGBA_8888_SkColorType }, |
| 23 { BGRA_8888_SK_COLORTYPE, kBGRA_8888_SkColorType }, |
| 24 { ALPHA_8_SK_COLORTYPE, kAlpha_8_SkColorType }, |
| 25 }; |
| 26 |
| 27 const struct { |
| 28 sk_alphatype_t fC; |
| 29 SkAlphaType fSK; |
| 30 } gAlphaTypeMap[] = { |
| 31 { OPAQUE_SK_ALPHATYPE, kOpaque_SkAlphaType }, |
| 32 { PREMUL_SK_ALPHATYPE, kPremul_SkAlphaType }, |
| 33 { UNPREMUL_SK_ALPHATYPE, kUnpremul_SkAlphaType }, |
| 34 }; |
| 35 |
| 36 static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) { |
| 37 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) { |
| 38 if (gColorTypeMap[i].fC == cCT) { |
| 39 if (skCT) { |
| 40 *skCT = gColorTypeMap[i].fSK; |
| 41 } |
| 42 return true; |
| 43 } |
| 44 } |
| 45 return false; |
| 46 } |
| 47 |
| 48 static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) { |
| 49 for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) { |
| 50 if (gColorTypeMap[i].fSK == skCT) { |
| 51 if (cCT) { |
| 52 *cCT = gColorTypeMap[i].fC; |
| 53 } |
| 54 return true; |
| 55 } |
| 56 } |
| 57 return false; |
| 58 } |
| 59 |
| 60 static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) { |
| 61 for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) { |
| 62 if (gAlphaTypeMap[i].fC == cAT) { |
| 63 if (skAT) { |
| 64 *skAT = gAlphaTypeMap[i].fSK; |
| 65 } |
| 66 return true; |
| 67 } |
| 68 } |
| 69 return false; |
| 70 } |
| 71 |
| 72 static bool from_c_info(const sk_imageinfo_t& cinfo, SkImageInfo* info) { |
| 73 SkColorType ct; |
| 74 SkAlphaType at; |
| 75 |
| 76 if (!from_c_colortype(cinfo.colorType, &ct)) { |
| 77 // optionally report error to client? |
| 78 return false; |
| 79 } |
| 80 if (!from_c_alphatype(cinfo.alphaType, &at)) { |
| 81 // optionally report error to client? |
| 82 return false; |
| 83 } |
| 84 if (info) { |
| 85 *info = SkImageInfo::Make(cinfo.width, cinfo.height, ct, at); |
| 86 } |
| 87 return true; |
20 } | 88 } |
21 | 89 |
22 static const SkRect& AsRect(const sk_rect_t& crect) { | 90 static const SkRect& AsRect(const sk_rect_t& crect) { |
23 return reinterpret_cast<const SkRect&>(crect); | 91 return reinterpret_cast<const SkRect&>(crect); |
24 } | 92 } |
25 | 93 |
26 static const SkPath& AsPath(const sk_path_t& cpath) { | 94 static const SkPath& AsPath(const sk_path_t& cpath) { |
27 return reinterpret_cast<const SkPath&>(cpath); | 95 return reinterpret_cast<const SkPath&>(cpath); |
28 } | 96 } |
29 | 97 |
(...skipping 16 matching lines...) Expand all Loading... |
46 static SkPaint* AsPaint(sk_paint_t* cpaint) { | 114 static SkPaint* AsPaint(sk_paint_t* cpaint) { |
47 return reinterpret_cast<SkPaint*>(cpaint); | 115 return reinterpret_cast<SkPaint*>(cpaint); |
48 } | 116 } |
49 | 117 |
50 static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) { | 118 static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) { |
51 return reinterpret_cast<SkCanvas*>(ccanvas); | 119 return reinterpret_cast<SkCanvas*>(ccanvas); |
52 } | 120 } |
53 | 121 |
54 ////////////////////////////////////////////////////////////////////////////////
/////////// | 122 ////////////////////////////////////////////////////////////////////////////////
/////////// |
55 | 123 |
| 124 sk_colortype_t sk_colortype_get_default_8888() { |
| 125 sk_colortype_t ct; |
| 126 if (!to_c_colortype(kN32_SkColorType, &ct)) { |
| 127 ct = UNKNOWN_SK_COLORTYPE; |
| 128 } |
| 129 return ct; |
| 130 } |
| 131 |
| 132 ////////////////////////////////////////////////////////////////////////////////
/////////// |
| 133 |
56 sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pi
xels, | 134 sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pi
xels, |
57 size_t rowBytes) { | 135 size_t rowBytes) { |
58 return (sk_image_t*)SkImage::NewRasterCopy(make(*cinfo), pixels, rowBytes); | 136 SkImageInfo info; |
| 137 if (!from_c_info(*cinfo, &info)) { |
| 138 return NULL; |
| 139 } |
| 140 return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes); |
59 } | 141 } |
60 | 142 |
61 void sk_image_ref(const sk_image_t* cimage) { | 143 void sk_image_ref(const sk_image_t* cimage) { |
62 AsImage(cimage)->ref(); | 144 AsImage(cimage)->ref(); |
63 } | 145 } |
64 | 146 |
65 void sk_image_unref(const sk_image_t* cimage) { | 147 void sk_image_unref(const sk_image_t* cimage) { |
66 AsImage(cimage)->unref(); | 148 AsImage(cimage)->unref(); |
67 } | 149 } |
68 | 150 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 } | 251 } |
170 | 252 |
171 void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float
x, float y, | 253 void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float
x, float y, |
172 const sk_paint_t* cpaint) { | 254 const sk_paint_t* cpaint) { |
173 AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint)); | 255 AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint)); |
174 } | 256 } |
175 | 257 |
176 ////////////////////////////////////////////////////////////////////////////////
/////////// | 258 ////////////////////////////////////////////////////////////////////////////////
/////////// |
177 | 259 |
178 sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) { | 260 sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) { |
179 return (sk_surface_t*)SkSurface::NewRaster(make(*cinfo)); | 261 SkImageInfo info; |
| 262 if (!from_c_info(*cinfo, &info)) { |
| 263 return NULL; |
| 264 } |
| 265 return (sk_surface_t*)SkSurface::NewRaster(info); |
180 } | 266 } |
181 | 267 |
182 sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pi
xels, | 268 sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pi
xels, |
183 size_t rowBytes) { | 269 size_t rowBytes) { |
184 return (sk_surface_t*)SkSurface::NewRasterDirect(make(*cinfo), pixels, rowBy
tes); | 270 SkImageInfo info; |
| 271 if (!from_c_info(*cinfo, &info)) { |
| 272 return NULL; |
| 273 } |
| 274 return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes); |
185 } | 275 } |
186 | 276 |
187 void sk_surface_delete(sk_surface_t* csurf) { | 277 void sk_surface_delete(sk_surface_t* csurf) { |
188 SkSurface* surf = (SkSurface*)csurf; | 278 SkSurface* surf = (SkSurface*)csurf; |
189 SkSafeUnref(surf); | 279 SkSafeUnref(surf); |
190 } | 280 } |
191 | 281 |
192 sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) { | 282 sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) { |
193 SkSurface* surf = (SkSurface*)csurf; | 283 SkSurface* surf = (SkSurface*)csurf; |
194 return (sk_canvas_t*)surf->getCanvas(); | 284 return (sk_canvas_t*)surf->getCanvas(); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 sk_image_t* cimage = sk_surface_new_image_snapshot(csurface); | 327 sk_image_t* cimage = sk_surface_new_image_snapshot(csurface); |
238 | 328 |
239 // HERE WE CROSS THE C..C++ boundary | 329 // HERE WE CROSS THE C..C++ boundary |
240 canvas->drawImage((const SkImage*)cimage, 20, 20, NULL); | 330 canvas->drawImage((const SkImage*)cimage, 20, 20, NULL); |
241 | 331 |
242 sk_path_delete(cpath); | 332 sk_path_delete(cpath); |
243 sk_paint_delete(cpaint); | 333 sk_paint_delete(cpaint); |
244 sk_image_unref(cimage); | 334 sk_image_unref(cimage); |
245 sk_surface_delete(csurface); | 335 sk_surface_delete(csurface); |
246 } | 336 } |
OLD | NEW |