Home | History | Annotate | Download | only in c
      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 
      8 #include "SkCanvas.h"
      9 #include "SkData.h"
     10 #include "SkImage.h"
     11 #include "SkMaskFilter.h"
     12 #include "SkMatrix.h"
     13 #include "SkPaint.h"
     14 #include "SkPath.h"
     15 #include "SkPictureRecorder.h"
     16 #include "SkSurface.h"
     17 
     18 #include "sk_canvas.h"
     19 #include "sk_data.h"
     20 #include "sk_image.h"
     21 #include "sk_paint.h"
     22 #include "sk_path.h"
     23 #include "sk_surface.h"
     24 #include "sk_types_priv.h"
     25 
     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;
     97 }
     98 
     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 
    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 
    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 
    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 
    157 static sk_rect_t ToRect(const SkRect& rect) {
    158     return reinterpret_cast<const sk_rect_t&>(rect);
    159 }
    160 
    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 
    169 static SkPath* as_path(sk_path_t* cpath) {
    170     return reinterpret_cast<SkPath*>(cpath);
    171 }
    172 
    173 static const SkImage* AsImage(const sk_image_t* cimage) {
    174     return reinterpret_cast<const SkImage*>(cimage);
    175 }
    176 
    177 static sk_image_t* ToImage(SkImage* cimage) {
    178     return reinterpret_cast<sk_image_t*>(cimage);
    179 }
    180 
    181 static sk_canvas_t* ToCanvas(SkCanvas* canvas) {
    182     return reinterpret_cast<sk_canvas_t*>(canvas);
    183 }
    184 
    185 static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
    186     return reinterpret_cast<SkCanvas*>(ccanvas);
    187 }
    188 
    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 
    209 ///////////////////////////////////////////////////////////////////////////////////////////
    210 
    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 
    221 sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
    222                                      size_t rowBytes) {
    223     SkImageInfo info;
    224     if (!from_c_info(*cinfo, &info)) {
    225         return NULL;
    226     }
    227     return (sk_image_t*)SkImage::MakeRasterCopy(SkPixmap(info, pixels, rowBytes)).release();
    228 }
    229 
    230 sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata, const sk_irect_t* subset) {
    231     return ToImage(SkImage::MakeFromEncoded(sk_ref_sp(AsData(cdata)),
    232                                            reinterpret_cast<const SkIRect*>(subset)).release());
    233 }
    234 
    235 sk_data_t* sk_image_encode(const sk_image_t* cimage) {
    236     return ToData(AsImage(cimage)->encodeToData().release());
    237 }
    238 
    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 
    261 sk_path_t* sk_path_new() { return (sk_path_t*)new SkPath; }
    262 
    263 void sk_path_delete(sk_path_t* cpath) { delete as_path(cpath); }
    264 
    265 void sk_path_move_to(sk_path_t* cpath, float x, float y) {
    266     as_path(cpath)->moveTo(x, y);
    267 }
    268 
    269 void sk_path_line_to(sk_path_t* cpath, float x, float y) {
    270     as_path(cpath)->lineTo(x, y);
    271 }
    272 
    273 void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) {
    274     as_path(cpath)->quadTo(x0, y0, x1, y1);
    275 }
    276 
    277 void sk_path_conic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float w) {
    278     as_path(cpath)->conicTo(x0, y0, x1, y1, w);
    279 }
    280 
    281 void sk_path_cubic_to(sk_path_t* cpath, float x0, float y0, float x1, float y1, float x2, float y2) {
    282     as_path(cpath)->cubicTo(x0, y0, x1, y1, x2, y2);
    283 }
    284 
    285 void sk_path_close(sk_path_t* cpath) {
    286     as_path(cpath)->close();
    287 }
    288 
    289 void sk_path_add_rect(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) {
    290     SkPath::Direction dir;
    291     if (!from_c_path_direction(cdir, &dir)) {
    292         return;
    293     }
    294     as_path(cpath)->addRect(AsRect(*crect), dir);
    295 }
    296 
    297 void sk_path_add_oval(sk_path_t* cpath, const sk_rect_t* crect, sk_path_direction_t cdir) {
    298     SkPath::Direction dir;
    299     if (!from_c_path_direction(cdir, &dir)) {
    300         return;
    301     }
    302     as_path(cpath)->addOval(AsRect(*crect), dir);
    303 }
    304 
    305 bool sk_path_get_bounds(const sk_path_t* cpath, sk_rect_t* crect) {
    306     const SkPath& path = AsPath(*cpath);
    307 
    308     if (path.isEmpty()) {
    309         if (crect) {
    310             *crect = ToRect(SkRect::MakeEmpty());
    311         }
    312         return false;
    313     }
    314 
    315     if (crect) {
    316         *crect = ToRect(path.getBounds());
    317     }
    318     return true;
    319 }
    320 
    321 ///////////////////////////////////////////////////////////////////////////////////////////
    322 
    323 void sk_canvas_save(sk_canvas_t* ccanvas) {
    324     AsCanvas(ccanvas)->save();
    325 }
    326 
    327 void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
    328     AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
    329 }
    330 
    331 void sk_canvas_restore(sk_canvas_t* ccanvas) {
    332     AsCanvas(ccanvas)->restore();
    333 }
    334 
    335 void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
    336     AsCanvas(ccanvas)->translate(dx, dy);
    337 }
    338 
    339 void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
    340     AsCanvas(ccanvas)->scale(sx, sy);
    341 }
    342 
    343 void sk_canvas_rotate_degress(sk_canvas_t* ccanvas, float degrees) {
    344     AsCanvas(ccanvas)->rotate(degrees);
    345 }
    346 
    347 void sk_canvas_rotate_radians(sk_canvas_t* ccanvas, float radians) {
    348     AsCanvas(ccanvas)->rotate(SkRadiansToDegrees(radians));
    349 }
    350 
    351 void sk_canvas_skew(sk_canvas_t* ccanvas, float sx, float sy) {
    352     AsCanvas(ccanvas)->skew(sx, sy);
    353 }
    354 
    355 void sk_canvas_concat(sk_canvas_t* ccanvas, const sk_matrix_t* cmatrix) {
    356     SkASSERT(cmatrix);
    357     SkMatrix matrix;
    358     from_c_matrix(cmatrix, &matrix);
    359     AsCanvas(ccanvas)->concat(matrix);
    360 }
    361 
    362 void sk_canvas_clip_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect) {
    363     AsCanvas(ccanvas)->clipRect(AsRect(*crect));
    364 }
    365 
    366 void sk_canvas_clip_path(sk_canvas_t* ccanvas, const sk_path_t* cpath) {
    367     AsCanvas(ccanvas)->clipPath(AsPath(*cpath));
    368 }
    369 
    370 void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
    371     AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
    372 }
    373 
    374 void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
    375     AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
    376 }
    377 
    378 void sk_canvas_draw_circle(sk_canvas_t* ccanvas, float cx, float cy, float rad,
    379                            const sk_paint_t* cpaint) {
    380     AsCanvas(ccanvas)->drawCircle(cx, cy, rad, AsPaint(*cpaint));
    381 }
    382 
    383 void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
    384     AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
    385 }
    386 
    387 void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
    388     AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
    389 }
    390 
    391 void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
    392                           const sk_paint_t* cpaint) {
    393     AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
    394 }
    395 
    396 void sk_canvas_draw_image_rect(sk_canvas_t* ccanvas, const sk_image_t* cimage,
    397                                const sk_rect_t* csrcR, const sk_rect_t* cdstR,
    398                                const sk_paint_t* cpaint) {
    399     SkCanvas* canvas = AsCanvas(ccanvas);
    400     const SkImage* image = AsImage(cimage);
    401     const SkRect& dst = AsRect(*cdstR);
    402     const SkPaint* paint = AsPaint(cpaint);
    403 
    404     if (csrcR) {
    405         canvas->drawImageRect(image, AsRect(*csrcR), dst, paint);
    406     } else {
    407         canvas->drawImageRect(image, dst, paint);
    408     }
    409 }
    410 
    411 void sk_canvas_draw_picture(sk_canvas_t* ccanvas, const sk_picture_t* cpicture,
    412                             const sk_matrix_t* cmatrix, const sk_paint_t* cpaint) {
    413     const SkMatrix* matrixPtr = NULL;
    414     SkMatrix matrix;
    415     if (cmatrix) {
    416         from_c_matrix(cmatrix, &matrix);
    417         matrixPtr = &matrix;
    418     }
    419     AsCanvas(ccanvas)->drawPicture(AsPicture(cpicture), matrixPtr, AsPaint(cpaint));
    420 }
    421 
    422 ///////////////////////////////////////////////////////////////////////////////////////////
    423 
    424 sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo,
    425                                     const sk_surfaceprops_t* props) {
    426     SkImageInfo info;
    427     if (!from_c_info(*cinfo, &info)) {
    428         return NULL;
    429     }
    430     SkPixelGeometry geo = kUnknown_SkPixelGeometry;
    431     if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
    432         return NULL;
    433     }
    434 
    435     SkSurfaceProps surfProps(0, geo);
    436     return (sk_surface_t*)SkSurface::MakeRaster(info, &surfProps).release();
    437 }
    438 
    439 sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
    440                                            size_t rowBytes,
    441                                            const sk_surfaceprops_t* props) {
    442     SkImageInfo info;
    443     if (!from_c_info(*cinfo, &info)) {
    444         return NULL;
    445     }
    446     SkPixelGeometry geo = kUnknown_SkPixelGeometry;
    447     if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
    448         return NULL;
    449     }
    450 
    451     SkSurfaceProps surfProps(0, geo);
    452     return (sk_surface_t*)SkSurface::MakeRasterDirect(info, pixels, rowBytes, &surfProps).release();
    453 }
    454 
    455 void sk_surface_unref(sk_surface_t* csurf) {
    456     SkSafeUnref((SkSurface*)csurf);
    457 }
    458 
    459 sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
    460     SkSurface* surf = (SkSurface*)csurf;
    461     return (sk_canvas_t*)surf->getCanvas();
    462 }
    463 
    464 sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
    465     SkSurface* surf = (SkSurface*)csurf;
    466     return (sk_image_t*)surf->makeImageSnapshot().release();
    467 }
    468 
    469 ///////////////////////////////////////////////////////////////////////////////////////////
    470 
    471 sk_picture_recorder_t* sk_picture_recorder_new() {
    472     return ToPictureRecorder(new SkPictureRecorder);
    473 }
    474 
    475 void sk_picture_recorder_delete(sk_picture_recorder_t* crec) {
    476     delete AsPictureRecorder(crec);
    477 }
    478 
    479 sk_canvas_t* sk_picture_recorder_begin_recording(sk_picture_recorder_t* crec,
    480                                                  const sk_rect_t* cbounds) {
    481     return ToCanvas(AsPictureRecorder(crec)->beginRecording(AsRect(*cbounds)));
    482 }
    483 
    484 sk_picture_t* sk_picture_recorder_end_recording(sk_picture_recorder_t* crec) {
    485     return ToPicture(AsPictureRecorder(crec)->finishRecordingAsPicture().release());
    486 }
    487 
    488 void sk_picture_ref(sk_picture_t* cpic) {
    489     SkSafeRef(AsPicture(cpic));
    490 }
    491 
    492 void sk_picture_unref(sk_picture_t* cpic) {
    493     SkSafeUnref(AsPicture(cpic));
    494 }
    495 
    496 uint32_t sk_picture_get_unique_id(sk_picture_t* cpic) {
    497     return AsPicture(cpic)->uniqueID();
    498 }
    499 
    500 sk_rect_t sk_picture_get_bounds(sk_picture_t* cpic) {
    501     return ToRect(AsPicture(cpic)->cullRect());
    502 }
    503 
    504 ///////////////////////////////////////////////////////////////////////////////////////////
    505 
    506 sk_data_t* sk_data_new_with_copy(const void* src, size_t length) {
    507     return ToData(SkData::MakeWithCopy(src, length).release());
    508 }
    509 
    510 sk_data_t* sk_data_new_from_malloc(const void* memory, size_t length) {
    511     return ToData(SkData::MakeFromMalloc(memory, length).release());
    512 }
    513 
    514 sk_data_t* sk_data_new_subset(const sk_data_t* csrc, size_t offset, size_t length) {
    515     return ToData(SkData::MakeSubset(AsData(csrc), offset, length).release());
    516 }
    517 
    518 void sk_data_ref(const sk_data_t* cdata) {
    519     SkSafeRef(AsData(cdata));
    520 }
    521 
    522 void sk_data_unref(const sk_data_t* cdata) {
    523     SkSafeUnref(AsData(cdata));
    524 }
    525 
    526 size_t sk_data_get_size(const sk_data_t* cdata) {
    527     return AsData(cdata)->size();
    528 }
    529 
    530 const void* sk_data_get_data(const sk_data_t* cdata) {
    531     return AsData(cdata)->data();
    532 }
    533 
    534 ///////////////////////////////////////////////////////////////////////////////////////////
    535