move c api into include
authorreed <reed@google.com>
Fri, 24 Oct 2014 18:16:19 +0000 (11:16 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 24 Oct 2014 18:16:19 +0000 (11:16 -0700)
BUG=skia:

Review URL: https://codereview.chromium.org/665203004

experimental/sk_surface.cpp [deleted file]
experimental/sk_surface.h [deleted file]
gyp/core.gyp
gyp/core.gypi
gyp/experimental.gyp
gyp/tests.gypi
include/c/sk_surface.h [new file with mode: 0644]
src/c/sk_surface.cpp [new file with mode: 0644]
tests/CTest.cpp [new file with mode: 0644]

diff --git a/experimental/sk_surface.cpp b/experimental/sk_surface.cpp
deleted file mode 100644 (file)
index ae37c46..0000000
+++ /dev/null
@@ -1,246 +0,0 @@
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#include "sk_surface.h"
-
-#include "SkCanvas.h"
-#include "SkImage.h"
-#include "SkMatrix.h"
-#include "SkPaint.h"
-#include "SkPath.h"
-#include "SkSurface.h"
-
-static SkImageInfo make(const sk_imageinfo_t& cinfo) {
-    return SkImageInfo::Make(cinfo.width, cinfo.height,
-                             (SkColorType)cinfo.colorType, (SkAlphaType)cinfo.alphaType);
-}
-
-static const SkRect& AsRect(const sk_rect_t& crect) {
-    return reinterpret_cast<const SkRect&>(crect);
-}
-
-static const SkPath& AsPath(const sk_path_t& cpath) {
-    return reinterpret_cast<const SkPath&>(cpath);
-}
-
-static SkPath* as_path(sk_path_t* cpath) {
-    return reinterpret_cast<SkPath*>(cpath);
-}
-
-static const SkImage* AsImage(const sk_image_t* cimage) {
-    return reinterpret_cast<const SkImage*>(cimage);
-}
-
-static const SkPaint& AsPaint(const sk_paint_t& cpaint) {
-    return reinterpret_cast<const SkPaint&>(cpaint);
-}
-
-static const SkPaint* AsPaint(const sk_paint_t* cpaint) {
-    return reinterpret_cast<const SkPaint*>(cpaint);
-}
-
-static SkPaint* AsPaint(sk_paint_t* cpaint) {
-    return reinterpret_cast<SkPaint*>(cpaint);
-}
-
-static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
-    return reinterpret_cast<SkCanvas*>(ccanvas);
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////
-
-sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
-                                     size_t rowBytes) {
-    return (sk_image_t*)SkImage::NewRasterCopy(make(*cinfo), pixels, rowBytes);
-}
-
-void sk_image_ref(const sk_image_t* cimage) {
-    AsImage(cimage)->ref();
-}
-
-void sk_image_unref(const sk_image_t* cimage) {
-    AsImage(cimage)->unref();
-}
-
-int sk_image_get_width(const sk_image_t* cimage) {
-    return AsImage(cimage)->width();
-}
-
-int sk_image_get_height(const sk_image_t* cimage) {
-    return AsImage(cimage)->height();
-}
-
-uint32_t sk_image_get_unique_id(const sk_image_t* cimage) {
-    return AsImage(cimage)->uniqueID();
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////
-
-sk_paint_t* sk_paint_new() {
-    return (sk_paint_t*)SkNEW(SkPaint);
-}
-
-void sk_paint_delete(sk_paint_t* cpaint) {
-    SkDELETE(AsPaint(cpaint));
-}
-
-bool sk_paint_is_antialias(const sk_paint_t* cpaint) {
-    return AsPaint(*cpaint).isAntiAlias();
-}
-
-void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) {
-    AsPaint(cpaint)->setAntiAlias(aa);
-}
-
-sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) {
-    return AsPaint(*cpaint).getColor();
-}
-
-void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) {
-    AsPaint(cpaint)->setColor(c);
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////
-
-sk_path_t* sk_path_new() {
-    return (sk_path_t*)SkNEW(SkPath);
-}
-
-void sk_path_delete(sk_path_t* cpath) {
-    SkDELETE(as_path(cpath));
-}
-
-void sk_path_move_to(sk_path_t* cpath, float x, float y) {
-    as_path(cpath)->moveTo(x, y);
-}
-
-void sk_path_line_to(sk_path_t* cpath, float x, float y) {
-    as_path(cpath)->lineTo(x, y);
-}
-
-void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) {
-    as_path(cpath)->quadTo(x0, y0, x1, y1);
-}
-
-void sk_path_close(sk_path_t* cpath) {
-    as_path(cpath)->close();
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////
-
-void sk_canvas_save(sk_canvas_t* ccanvas) {
-    AsCanvas(ccanvas)->save();
-}
-
-void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
-    AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
-}
-
-void sk_canvas_restore(sk_canvas_t* ccanvas) {
-    AsCanvas(ccanvas)->restore();
-}
-
-void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
-    AsCanvas(ccanvas)->translate(dx, dy);
-}
-
-void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
-    AsCanvas(ccanvas)->scale(sx, sy);
-}
-
-void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
-    AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
-}
-
-void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
-    AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
-}
-
-void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
-    AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
-}
-
-void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
-    AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
-}
-
-void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
-                          const sk_paint_t* cpaint) {
-    AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////
-
-sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) {
-    return (sk_surface_t*)SkSurface::NewRaster(make(*cinfo));
-}
-
-sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
-                                           size_t rowBytes) {
-    return (sk_surface_t*)SkSurface::NewRasterDirect(make(*cinfo), pixels, rowBytes);
-}
-
-void sk_surface_delete(sk_surface_t* csurf) {
-    SkSurface* surf = (SkSurface*)csurf;
-    SkSafeUnref(surf);
-}
-
-sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
-    SkSurface* surf = (SkSurface*)csurf;
-    return (sk_canvas_t*)surf->getCanvas();
-}
-
-sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
-    SkSurface* surf = (SkSurface*)csurf;
-    return (sk_image_t*)surf->newImageSnapshot();
-}
-
-
-///////////////////
-
-void sk_test_capi(SkCanvas* canvas) {
-    sk_imageinfo_t cinfo;
-    cinfo.width = 100;
-    cinfo.height = 100;
-    cinfo.colorType = (sk_colortype_t)kN32_SkColorType;
-    cinfo.alphaType = (sk_alphatype_t)kPremul_SkAlphaType;
-    
-    sk_surface_t* csurface = sk_surface_new_raster(&cinfo);
-    sk_canvas_t* ccanvas = sk_surface_get_canvas(csurface);
-    
-    sk_paint_t* cpaint = sk_paint_new();
-    sk_paint_set_antialias(cpaint, true);
-    sk_paint_set_color(cpaint, 0xFFFF0000);
-    
-    sk_rect_t cr = { 5, 5, 95, 95 };
-    sk_canvas_draw_oval(ccanvas, &cr, cpaint);
-    
-    cr.left += 25;
-    cr.top += 25;
-    cr.right -= 25;
-    cr.bottom -= 25;
-    sk_paint_set_color(cpaint, 0xFF00FF00);
-    sk_canvas_draw_rect(ccanvas, &cr, cpaint);
-    
-    sk_path_t* cpath = sk_path_new();
-    sk_path_move_to(cpath, 50, 50);
-    sk_path_line_to(cpath, 100, 100);
-    sk_path_line_to(cpath, 50, 100);
-    sk_path_close(cpath);
-
-    sk_canvas_draw_path(ccanvas, cpath, cpaint);
-
-    sk_image_t* cimage = sk_surface_new_image_snapshot(csurface);
-    
-    // HERE WE CROSS THE C..C++ boundary
-    canvas->drawImage((const SkImage*)cimage, 20, 20, NULL);
-    
-    sk_path_delete(cpath);
-    sk_paint_delete(cpaint);
-    sk_image_unref(cimage);
-    sk_surface_delete(csurface);
-}
diff --git a/experimental/sk_surface.h b/experimental/sk_surface.h
deleted file mode 100644 (file)
index e170049..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * Copyright 2014 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-#ifndef sk_types_DEFINED
-#define sk_types_DEFINED
-
-#include <stdint.h>
-#include <stddef.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef uint32_t sk_color_t;
-
-#define sk_color_set_argb(a, r, g, b)   (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
-#define sk_color_get_a(c)               (((c) >> 24) & 0xFF)
-#define sk_color_get_r(c)               (((c) >> 16) & 0xFF)
-#define sk_color_get_g(c)               (((c) >>  8) & 0xFF)
-#define sk_color_get_b(c)               (((c) >>  0) & 0xFF)
-
-typedef enum {
-    UNKNOWN_SK_COLORTYPE,
-    RGBA_8888_SK_COLORTYPE,
-    BGRA_8888_SK_COLORTYPE,
-    ALPHA_8_SK_COLORTYPE,
-} sk_colortype_t;
-
-typedef enum {
-    PREMUL_SK_ALPHATYPE,
-    UNPREMUL_SK_ALPHATYPE,
-} sk_alphatype_t;
-
-typedef struct {
-    int32_t         width;
-    int32_t         height;
-    sk_colortype_t  colorType;
-    sk_alphatype_t  alphaType;
-} sk_imageinfo_t;
-
-typedef struct {
-    float   left;
-    float   top;
-    float   right;
-    float   bottom;
-} sk_rect_t;
-
-typedef struct sk_path_t sk_path_t;
-
-sk_path_t* sk_path_new();
-void sk_path_delete(sk_path_t*);
-void sk_path_move_to(sk_path_t*, float x, float y);
-void sk_path_line_to(sk_path_t*, float x, float y);
-void sk_path_quad_to(sk_path_t*, float x0, float y0, float x1, float y1);
-void sk_path_close(sk_path_t*);
-
-typedef struct sk_paint_t sk_paint_t;
-
-sk_paint_t* sk_paint_new();
-void sk_paint_delete(sk_paint_t*);
-bool sk_paint_is_antialias(const sk_paint_t*);
-void sk_paint_set_antialias(sk_paint_t*, bool);
-sk_color_t sk_paint_get_color(const sk_paint_t*);
-void sk_paint_set_color(sk_paint_t*, sk_color_t);
-
-typedef struct sk_canvas_t sk_canvas_t;
-typedef struct sk_image_t sk_image_t;
-
-void sk_canvas_save(sk_canvas_t*);
-void sk_canvas_save_layer(sk_canvas_t*, const sk_rect_t*, const sk_paint_t*);
-void sk_canvas_restore(sk_canvas_t*);
-
-void sk_canvas_translate(sk_canvas_t*, float dx, float dy);
-void sk_canvas_scale(sk_canvas_t*, float sx, float sy);
-
-void sk_canvas_draw_paint(sk_canvas_t*, const sk_paint_t*);
-void sk_canvas_draw_rect(sk_canvas_t*, const sk_rect_t*, const sk_paint_t*);
-void sk_canvas_draw_oval(sk_canvas_t*, const sk_rect_t*, const sk_paint_t*);
-void sk_canvas_draw_path(sk_canvas_t*, const sk_path_t*, const sk_paint_t*);
-void sk_canvas_draw_image(sk_canvas_t*, const sk_image_t*, float x, float y, const sk_paint_t*);
-
-sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t*, const void* pixels, size_t rowBytes);
-void sk_image_ref(const sk_image_t*);
-void sk_image_unref(const sk_image_t*);
-int sk_image_get_width(const sk_image_t*);
-int sk_image_get_height(const sk_image_t*);
-uint32_t sk_image_get_unique_id(const sk_image_t*);
-
-typedef struct sk_surface_t sk_surface_t;
-
-sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t*);
-sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t*, void* pixels, size_t rowBytes);
-void sk_surface_delete(sk_surface_t*);
-sk_canvas_t* sk_surface_get_canvas(sk_surface_t*);
-sk_image_t* sk_surface_new_image_snapshot(sk_surface_t*);
-
-#ifdef __cplusplus
-    class SkCanvas;
-    void sk_test_capi(SkCanvas*);
-}
-#endif
-
-#endif
index afbc78a4ce4efe571fd2ed9ce97a9780c3fdfb0a..91545ee0c3b46ec6e5568a5a03e94145d007a2b9 100644 (file)
@@ -13,6 +13,7 @@
       ],
 
       'include_dirs': [
+        '../include/c',
         '../include/config',
         '../include/core',
         '../include/pathops',
       ],
       'direct_dependent_settings': {
         'include_dirs': [
+          '../include/c',
           '../include/config',
           '../include/core',
           '../include/pathops',
index 553e36d7ba230a689528bb0ceabac3aa952b97ac..9170fabde5dde84078b06a093624d72731a3300d 100644 (file)
@@ -7,6 +7,9 @@
 #
 {
     'sources': [
+        '<(skia_src_path)/c/sk_surface.cpp',
+        '<(skia_include_path)/c/sk_surface.h',
+
         '<(skia_src_path)/core/SkAAClip.cpp',
         '<(skia_src_path)/core/SkAnnotation.cpp',
         '<(skia_src_path)/core/SkAdvancedTypefaceMetrics.cpp',
index ca21b3b82471ed5b16d7fb78405cf25351a74299..c3789fad573bc1374d69255ea40ceadedd3c1ac8 100644 (file)
@@ -12,8 +12,6 @@
         '../experimental/SkSetPoly3To3.cpp',
         '../experimental/SkSetPoly3To3_A.cpp',
         '../experimental/SkSetPoly3To3_D.cpp',
-        '../experimental/sk_surface.h',
-        '../experimental/sk_surface.cpp',
       ],
       'direct_dependent_settings': {
         'include_dirs': [
index b6e060646e0a9bec39b240a80e3d3b0180f4c484..59604db6c5ce49579be5396de7fa994e0a2cf22d 100644 (file)
@@ -58,6 +58,7 @@
     '../tests/BlendTest.cpp',
     '../tests/BlitRowTest.cpp',
     '../tests/BlurTest.cpp',
+    '../tests/CTest.cpp',
     '../tests/CachedDataTest.cpp',
     '../tests/CachedDecodingPixelRefTest.cpp',
     '../tests/CanvasStateHelpers.cpp',
diff --git a/include/c/sk_surface.h b/include/c/sk_surface.h
new file mode 100644 (file)
index 0000000..960c331
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+// EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL EXPERIMENTAL
+// DO NOT USE -- FOR INTERNAL TESTING ONLY
+
+#ifndef sk_types_DEFINED
+#define sk_types_DEFINED
+
+#include <stdint.h>
+#include <stddef.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef uint32_t sk_color_t;
+
+#define sk_color_set_argb(a, r, g, b)   (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
+#define sk_color_get_a(c)               (((c) >> 24) & 0xFF)
+#define sk_color_get_r(c)               (((c) >> 16) & 0xFF)
+#define sk_color_get_g(c)               (((c) >>  8) & 0xFF)
+#define sk_color_get_b(c)               (((c) >>  0) & 0xFF)
+
+typedef enum {
+    UNKNOWN_SK_COLORTYPE,
+    RGBA_8888_SK_COLORTYPE,
+    BGRA_8888_SK_COLORTYPE,
+    ALPHA_8_SK_COLORTYPE,
+} sk_colortype_t;
+
+typedef enum {
+    OPAQUE_SK_ALPHATYPE,
+    PREMUL_SK_ALPHATYPE,
+    UNPREMUL_SK_ALPHATYPE,
+} sk_alphatype_t;
+
+sk_colortype_t sk_colortype_get_default_8888();
+
+typedef struct {
+    int32_t         width;
+    int32_t         height;
+    sk_colortype_t  colorType;
+    sk_alphatype_t  alphaType;
+} sk_imageinfo_t;
+
+typedef struct {
+    float   left;
+    float   top;
+    float   right;
+    float   bottom;
+} sk_rect_t;
+
+typedef struct sk_path_t sk_path_t;
+
+sk_path_t* sk_path_new();
+void sk_path_delete(sk_path_t*);
+void sk_path_move_to(sk_path_t*, float x, float y);
+void sk_path_line_to(sk_path_t*, float x, float y);
+void sk_path_quad_to(sk_path_t*, float x0, float y0, float x1, float y1);
+void sk_path_close(sk_path_t*);
+
+typedef struct sk_paint_t sk_paint_t;
+
+sk_paint_t* sk_paint_new();
+void sk_paint_delete(sk_paint_t*);
+bool sk_paint_is_antialias(const sk_paint_t*);
+void sk_paint_set_antialias(sk_paint_t*, bool);
+sk_color_t sk_paint_get_color(const sk_paint_t*);
+void sk_paint_set_color(sk_paint_t*, sk_color_t);
+
+typedef struct sk_canvas_t sk_canvas_t;
+typedef struct sk_image_t sk_image_t;
+
+void sk_canvas_save(sk_canvas_t*);
+void sk_canvas_save_layer(sk_canvas_t*, const sk_rect_t*, const sk_paint_t*);
+void sk_canvas_restore(sk_canvas_t*);
+
+void sk_canvas_translate(sk_canvas_t*, float dx, float dy);
+void sk_canvas_scale(sk_canvas_t*, float sx, float sy);
+
+void sk_canvas_draw_paint(sk_canvas_t*, const sk_paint_t*);
+void sk_canvas_draw_rect(sk_canvas_t*, const sk_rect_t*, const sk_paint_t*);
+void sk_canvas_draw_oval(sk_canvas_t*, const sk_rect_t*, const sk_paint_t*);
+void sk_canvas_draw_path(sk_canvas_t*, const sk_path_t*, const sk_paint_t*);
+void sk_canvas_draw_image(sk_canvas_t*, const sk_image_t*, float x, float y, const sk_paint_t*);
+
+/**
+ *  Return a new image that has made a copy of the provided pixels, or NULL on failure.
+ *  Balance with a call to sk_image_unref().
+ */
+sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t*, const void* pixels, size_t rowBytes);
+void sk_image_ref(const sk_image_t*);
+void sk_image_unref(const sk_image_t*);
+int sk_image_get_width(const sk_image_t*);
+int sk_image_get_height(const sk_image_t*);
+uint32_t sk_image_get_unique_id(const sk_image_t*);
+
+typedef struct sk_surface_t sk_surface_t;
+
+sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t*);
+sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t*, void* pixels, size_t rowBytes);
+void sk_surface_delete(sk_surface_t*);
+
+/**
+ *  Return the canvas associated with this surface. Note: the canvas is owned by the surface,
+ *  so the returned object is only valid while the owning surface is valid.
+ */
+sk_canvas_t* sk_surface_get_canvas(sk_surface_t*);
+sk_image_t* sk_surface_new_image_snapshot(sk_surface_t*);
+
+#ifdef __cplusplus
+    class SkCanvas;
+    void sk_test_capi(SkCanvas*);
+}
+#endif
+
+#endif
diff --git a/src/c/sk_surface.cpp b/src/c/sk_surface.cpp
new file mode 100644 (file)
index 0000000..ec16fc2
--- /dev/null
@@ -0,0 +1,336 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "sk_surface.h"
+
+#include "SkCanvas.h"
+#include "SkImage.h"
+#include "SkMatrix.h"
+#include "SkPaint.h"
+#include "SkPath.h"
+#include "SkSurface.h"
+
+const struct {
+    sk_colortype_t  fC;
+    SkColorType     fSK;
+} gColorTypeMap[] = {
+    { UNKNOWN_SK_COLORTYPE,     kUnknown_SkColorType    },
+    { RGBA_8888_SK_COLORTYPE,   kRGBA_8888_SkColorType  },
+    { BGRA_8888_SK_COLORTYPE,   kBGRA_8888_SkColorType  },
+    { ALPHA_8_SK_COLORTYPE,     kAlpha_8_SkColorType    },
+};
+
+const struct {
+    sk_alphatype_t  fC;
+    SkAlphaType     fSK;
+} gAlphaTypeMap[] = {
+    { OPAQUE_SK_ALPHATYPE,      kOpaque_SkAlphaType     },
+    { PREMUL_SK_ALPHATYPE,      kPremul_SkAlphaType     },
+    { UNPREMUL_SK_ALPHATYPE,    kUnpremul_SkAlphaType   },
+};
+
+static bool from_c_colortype(sk_colortype_t cCT, SkColorType* skCT) {
+    for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
+        if (gColorTypeMap[i].fC == cCT) {
+            if (skCT) {
+                *skCT = gColorTypeMap[i].fSK;
+            }
+            return true;
+        }
+    }
+    return false;
+}
+
+static bool to_c_colortype(SkColorType skCT, sk_colortype_t* cCT) {
+    for (size_t i = 0; i < SK_ARRAY_COUNT(gColorTypeMap); ++i) {
+        if (gColorTypeMap[i].fSK == skCT) {
+            if (cCT) {
+                *cCT = gColorTypeMap[i].fC;
+            }
+            return true;
+        }
+    }
+    return false;
+}
+
+static bool from_c_alphatype(sk_alphatype_t cAT, SkAlphaType* skAT) {
+    for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
+        if (gAlphaTypeMap[i].fC == cAT) {
+            if (skAT) {
+                *skAT = gAlphaTypeMap[i].fSK;
+            }
+            return true;
+        }
+    }
+    return false;
+}
+
+static bool from_c_info(const sk_imageinfo_t& cinfo, SkImageInfo* info) {
+    SkColorType ct;
+    SkAlphaType at;
+
+    if (!from_c_colortype(cinfo.colorType, &ct)) {
+        // optionally report error to client?
+        return false;
+    }
+    if (!from_c_alphatype(cinfo.alphaType, &at)) {
+        // optionally report error to client?
+        return false;
+    }
+    if (info) {
+        *info = SkImageInfo::Make(cinfo.width, cinfo.height, ct, at);
+    }
+    return true;
+}
+
+static const SkRect& AsRect(const sk_rect_t& crect) {
+    return reinterpret_cast<const SkRect&>(crect);
+}
+
+static const SkPath& AsPath(const sk_path_t& cpath) {
+    return reinterpret_cast<const SkPath&>(cpath);
+}
+
+static SkPath* as_path(sk_path_t* cpath) {
+    return reinterpret_cast<SkPath*>(cpath);
+}
+
+static const SkImage* AsImage(const sk_image_t* cimage) {
+    return reinterpret_cast<const SkImage*>(cimage);
+}
+
+static const SkPaint& AsPaint(const sk_paint_t& cpaint) {
+    return reinterpret_cast<const SkPaint&>(cpaint);
+}
+
+static const SkPaint* AsPaint(const sk_paint_t* cpaint) {
+    return reinterpret_cast<const SkPaint*>(cpaint);
+}
+
+static SkPaint* AsPaint(sk_paint_t* cpaint) {
+    return reinterpret_cast<SkPaint*>(cpaint);
+}
+
+static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
+    return reinterpret_cast<SkCanvas*>(ccanvas);
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////
+
+sk_colortype_t sk_colortype_get_default_8888() {
+    sk_colortype_t ct;
+    if (!to_c_colortype(kN32_SkColorType, &ct)) {
+        ct = UNKNOWN_SK_COLORTYPE;
+    }
+    return ct;
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////
+
+sk_image_t* sk_image_new_raster_copy(const sk_imageinfo_t* cinfo, const void* pixels,
+                                     size_t rowBytes) {
+    SkImageInfo info;
+    if (!from_c_info(*cinfo, &info)) {
+        return NULL;
+    }
+    return (sk_image_t*)SkImage::NewRasterCopy(info, pixels, rowBytes);
+}
+
+void sk_image_ref(const sk_image_t* cimage) {
+    AsImage(cimage)->ref();
+}
+
+void sk_image_unref(const sk_image_t* cimage) {
+    AsImage(cimage)->unref();
+}
+
+int sk_image_get_width(const sk_image_t* cimage) {
+    return AsImage(cimage)->width();
+}
+
+int sk_image_get_height(const sk_image_t* cimage) {
+    return AsImage(cimage)->height();
+}
+
+uint32_t sk_image_get_unique_id(const sk_image_t* cimage) {
+    return AsImage(cimage)->uniqueID();
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////
+
+sk_paint_t* sk_paint_new() {
+    return (sk_paint_t*)SkNEW(SkPaint);
+}
+
+void sk_paint_delete(sk_paint_t* cpaint) {
+    SkDELETE(AsPaint(cpaint));
+}
+
+bool sk_paint_is_antialias(const sk_paint_t* cpaint) {
+    return AsPaint(*cpaint).isAntiAlias();
+}
+
+void sk_paint_set_antialias(sk_paint_t* cpaint, bool aa) {
+    AsPaint(cpaint)->setAntiAlias(aa);
+}
+
+sk_color_t sk_paint_get_color(const sk_paint_t* cpaint) {
+    return AsPaint(*cpaint).getColor();
+}
+
+void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) {
+    AsPaint(cpaint)->setColor(c);
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////
+
+sk_path_t* sk_path_new() {
+    return (sk_path_t*)SkNEW(SkPath);
+}
+
+void sk_path_delete(sk_path_t* cpath) {
+    SkDELETE(as_path(cpath));
+}
+
+void sk_path_move_to(sk_path_t* cpath, float x, float y) {
+    as_path(cpath)->moveTo(x, y);
+}
+
+void sk_path_line_to(sk_path_t* cpath, float x, float y) {
+    as_path(cpath)->lineTo(x, y);
+}
+
+void sk_path_quad_to(sk_path_t* cpath, float x0, float y0, float x1, float y1) {
+    as_path(cpath)->quadTo(x0, y0, x1, y1);
+}
+
+void sk_path_close(sk_path_t* cpath) {
+    as_path(cpath)->close();
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////
+
+void sk_canvas_save(sk_canvas_t* ccanvas) {
+    AsCanvas(ccanvas)->save();
+}
+
+void sk_canvas_save_layer(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
+    AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
+}
+
+void sk_canvas_restore(sk_canvas_t* ccanvas) {
+    AsCanvas(ccanvas)->restore();
+}
+
+void sk_canvas_translate(sk_canvas_t* ccanvas, float dx, float dy) {
+    AsCanvas(ccanvas)->translate(dx, dy);
+}
+
+void sk_canvas_scale(sk_canvas_t* ccanvas, float sx, float sy) {
+    AsCanvas(ccanvas)->scale(sx, sy);
+}
+
+void sk_canvas_draw_paint(sk_canvas_t* ccanvas, const sk_paint_t* cpaint) {
+    AsCanvas(ccanvas)->drawPaint(AsPaint(*cpaint));
+}
+
+void sk_canvas_draw_rect(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
+    AsCanvas(ccanvas)->drawRect(AsRect(*crect), AsPaint(*cpaint));
+}
+
+void sk_canvas_draw_oval(sk_canvas_t* ccanvas, const sk_rect_t* crect, const sk_paint_t* cpaint) {
+    AsCanvas(ccanvas)->drawOval(AsRect(*crect), AsPaint(*cpaint));
+}
+
+void sk_canvas_draw_path(sk_canvas_t* ccanvas, const sk_path_t* cpath, const sk_paint_t* cpaint) {
+    AsCanvas(ccanvas)->drawPath(AsPath(*cpath), AsPaint(*cpaint));
+}
+
+void sk_canvas_draw_image(sk_canvas_t* ccanvas, const sk_image_t* cimage, float x, float y,
+                          const sk_paint_t* cpaint) {
+    AsCanvas(ccanvas)->drawImage(AsImage(cimage), x, y, AsPaint(cpaint));
+}
+
+///////////////////////////////////////////////////////////////////////////////////////////
+
+sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo) {
+    SkImageInfo info;
+    if (!from_c_info(*cinfo, &info)) {
+        return NULL;
+    }
+    return (sk_surface_t*)SkSurface::NewRaster(info);
+}
+
+sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
+                                           size_t rowBytes) {
+    SkImageInfo info;
+    if (!from_c_info(*cinfo, &info)) {
+        return NULL;
+    }
+    return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes);
+}
+
+void sk_surface_delete(sk_surface_t* csurf) {
+    SkSurface* surf = (SkSurface*)csurf;
+    SkSafeUnref(surf);
+}
+
+sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
+    SkSurface* surf = (SkSurface*)csurf;
+    return (sk_canvas_t*)surf->getCanvas();
+}
+
+sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
+    SkSurface* surf = (SkSurface*)csurf;
+    return (sk_image_t*)surf->newImageSnapshot();
+}
+
+
+///////////////////
+
+void sk_test_capi(SkCanvas* canvas) {
+    sk_imageinfo_t cinfo;
+    cinfo.width = 100;
+    cinfo.height = 100;
+    cinfo.colorType = (sk_colortype_t)kN32_SkColorType;
+    cinfo.alphaType = (sk_alphatype_t)kPremul_SkAlphaType;
+    
+    sk_surface_t* csurface = sk_surface_new_raster(&cinfo);
+    sk_canvas_t* ccanvas = sk_surface_get_canvas(csurface);
+    
+    sk_paint_t* cpaint = sk_paint_new();
+    sk_paint_set_antialias(cpaint, true);
+    sk_paint_set_color(cpaint, 0xFFFF0000);
+    
+    sk_rect_t cr = { 5, 5, 95, 95 };
+    sk_canvas_draw_oval(ccanvas, &cr, cpaint);
+    
+    cr.left += 25;
+    cr.top += 25;
+    cr.right -= 25;
+    cr.bottom -= 25;
+    sk_paint_set_color(cpaint, 0xFF00FF00);
+    sk_canvas_draw_rect(ccanvas, &cr, cpaint);
+    
+    sk_path_t* cpath = sk_path_new();
+    sk_path_move_to(cpath, 50, 50);
+    sk_path_line_to(cpath, 100, 100);
+    sk_path_line_to(cpath, 50, 100);
+    sk_path_close(cpath);
+
+    sk_canvas_draw_path(ccanvas, cpath, cpaint);
+
+    sk_image_t* cimage = sk_surface_new_image_snapshot(csurface);
+    
+    // HERE WE CROSS THE C..C++ boundary
+    canvas->drawImage((const SkImage*)cimage, 20, 20, NULL);
+    
+    sk_path_delete(cpath);
+    sk_paint_delete(cpaint);
+    sk_image_unref(cimage);
+    sk_surface_delete(csurface);
+}
diff --git a/tests/CTest.cpp b/tests/CTest.cpp
new file mode 100644 (file)
index 0000000..f663dcf
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "sk_surface.h"
+#include "Test.h"
+
+static void test_c(skiatest::Reporter* reporter) {
+    sk_colortype_t ct = sk_colortype_get_default_8888();
+
+    sk_imageinfo_t info = {
+        1, 1, ct, PREMUL_SK_ALPHATYPE
+    };
+    uint32_t pixel[1] = { 0 };
+
+    sk_surface_t* surface = sk_surface_new_raster_direct(&info, pixel, sizeof(uint32_t));
+    sk_paint_t* paint = sk_paint_new();
+
+    sk_canvas_t* canvas = sk_surface_get_canvas(surface);
+    sk_canvas_draw_paint(canvas, paint);
+    REPORTER_ASSERT(reporter, 0xFF000000 == pixel[0]);
+
+    sk_paint_set_color(paint, sk_color_set_argb(0xFF, 0xFF, 0xFF, 0xFF));
+    sk_canvas_draw_paint(canvas, paint);
+    REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
+
+    sk_paint_delete(paint);
+    sk_surface_delete(surface);
+}
+
+DEF_TEST(C_API, reporter) {
+    test_c(reporter);
+}