more c
authorreed <reed@chromium.org>
Tue, 25 Nov 2014 03:11:48 +0000 (19:11 -0800)
committerCommit bot <commit-bot@chromium.org>
Tue, 25 Nov 2014 03:11:48 +0000 (19:11 -0800)
BUG=skia:
TBR=

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

gm/cgm.c [new file with mode: 0644]
gm/cgms.cpp [new file with mode: 0644]
gyp/gmslides.gypi
include/c/sk_matrix.h [new file with mode: 0644]
include/c/sk_paint.h
include/c/sk_shader.h [new file with mode: 0644]
include/c/sk_surface.h
include/c/sk_types.h
src/c/sk_surface.cpp
tests/CTest.cpp

diff --git a/gm/cgm.c b/gm/cgm.c
new file mode 100644 (file)
index 0000000..cddb2c1
--- /dev/null
+++ b/gm/cgm.c
@@ -0,0 +1,48 @@
+/*
+ * 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
+
+#include "sk_canvas.h"
+#include "sk_paint.h"
+#include "sk_shader.h"
+#include "sk_surface.h"
+
+extern void sk_test_c_api(sk_canvas_t*);
+
+#define W   256
+#define H   256
+
+static sk_shader_t* make_shader() {
+    sk_point_t pts[] = { { 0, 0 }, { W, H } };
+    sk_color_t colors[] = { 0xFF00FF00, 0xFF0000FF };
+    return sk_shader_new_linear_gradient(pts, colors, NULL, 2, CLAMP_SK_SHADER_TILEMODE, NULL);
+}
+
+void sk_test_c_api(sk_canvas_t* canvas) {
+    sk_paint_t* paint = sk_paint_new();
+    sk_paint_set_antialias(paint, true);
+
+    sk_paint_set_color(paint, 0xFFFFFFFF);
+    sk_canvas_draw_paint(canvas, paint);
+
+    sk_rect_t r = { 10, 10, W - 10, H - 10 };
+
+    sk_paint_set_color(paint, 0xFFFF0000);
+    sk_canvas_draw_rect(canvas, &r, paint);
+
+    sk_shader_t* shader = make_shader();
+    sk_paint_set_shader(paint, shader);
+    sk_shader_unref(shader);
+
+    sk_canvas_draw_oval(canvas, &r, paint);
+
+    sk_paint_delete(paint);
+}
+
+
diff --git a/gm/cgms.cpp b/gm/cgms.cpp
new file mode 100644 (file)
index 0000000..4f2c1b1
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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 "gm.h"
+#include "sk_types.h"
+
+extern "C" void sk_test_c_api(sk_canvas_t*);
+
+class C_GM : public skiagm::GM {
+public:
+    C_GM() {}
+
+protected:
+    SkString onShortName() SK_OVERRIDE {
+        return SkString("c_gms");
+    }
+
+    SkISize onISize() SK_OVERRIDE {
+        return SkISize::Make(640, 480);
+    }
+
+    void onDraw(SkCanvas* canvas) SK_OVERRIDE {
+        sk_test_c_api((sk_canvas_t*)canvas);
+    }
+
+private:
+    typedef GM INHERITED;
+};
+
+DEF_GM( return new C_GM; )
+
index 66427e83ffc3a904e80c7261ebce9d973de6d36d..f1ab2b74a5c9e5b2cee12e7da354e470618504b4 100644 (file)
@@ -43,6 +43,8 @@
         '../gm/circularclips.cpp',
         '../gm/clip_strokerect.cpp',
         '../gm/clippedbitmapshaders.cpp',
+        '../gm/cgms.cpp',
+        '../gm/cgm.c',
         '../gm/colorcube.cpp',
         '../gm/coloremoji.cpp',
         '../gm/colorfilterimagefilter.cpp',
diff --git a/include/c/sk_matrix.h b/include/c/sk_matrix.h
new file mode 100644 (file)
index 0000000..12d9465
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * 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_matrix_DEFINED
+#define sk_matrix_DEFINED
+
+#include "sk_types.h"
+
+SK_C_PLUS_PLUS_BEGIN_GUARD
+
+void sk_matrix_set_identity(sk_matrix_t*);
+
+void sk_matrix_set_translate(sk_matrix_t*, float tx, float ty);
+void sk_matrix_pre_translate(sk_matrix_t*, float tx, float ty);
+void sk_matrix_post_translate(sk_matrix_t*, float tx, float ty);
+
+void sk_matrix_set_scale(sk_matrix_t*, float sx, float sy);
+void sk_matrix_pre_scale(sk_matrix_t*, float sx, float sy);
+void sk_matrix_post_scale(sk_matrix_t*, float sx, float sy);
+
+SK_C_PLUS_PLUS_END_GUARD
+
+#endif
index 840433b1b0938098e867b18aaaf71c98c7d49467..92ac5d57ba364e807bf9bee33a3dedc3720bdcae 100644 (file)
@@ -17,11 +17,19 @@ SK_C_PLUS_PLUS_BEGIN_GUARD
 
 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);
 
+/**
+ *  Set the paint's shader to the specified parameter. This will automatically call unref() on
+ *  any previous value, and call ref() on the new value.
+ */
+void sk_paint_set_shader(sk_paint_t*, sk_shader_t*);
+
 SK_C_PLUS_PLUS_END_GUARD
 
 #endif
diff --git a/include/c/sk_shader.h b/include/c/sk_shader.h
new file mode 100644 (file)
index 0000000..9f83ff0
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * 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_shader_DEFINED
+#define sk_shader_DEFINED
+
+#include "sk_types.h"
+
+SK_C_PLUS_PLUS_BEGIN_GUARD
+
+void sk_shader_ref(sk_shader_t*);
+void sk_shader_unref(sk_shader_t*);
+
+typedef enum {
+    CLAMP_SK_SHADER_TILEMODE,
+    REPEAT_SK_SHADER_TILEMODE,
+    MIRROR_SK_SHADER_TILEMODE,
+} sk_shader_tilemode_t;
+
+sk_shader_t* sk_shader_new_linear_gradient(const sk_point_t pts[2],
+                                           const sk_color_t colors[],
+                                           const float colorPos[],
+                                           int colorCount,
+                                           sk_shader_tilemode_t tileMode,
+                                           const sk_matrix_t* localMatrix);
+
+
+SK_C_PLUS_PLUS_END_GUARD
+
+#endif
index 96aa32f9746c90449e9ff1918b7bf1580cdb112e..ce7a5689596dc2a1b0690d8d73c27f6c02f07f6c 100644 (file)
@@ -17,7 +17,7 @@ SK_C_PLUS_PLUS_BEGIN_GUARD
 
 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*);
+void sk_surface_unref(sk_surface_t*);
 
 /**
  *  Return the canvas associated with this surface. Note: the canvas is owned by the surface,
index 71ce219c018aad201b7146e4d166b7a381845226..4c6389689ccdeec7913d66856ecd8fcb51158870 100644 (file)
@@ -57,6 +57,11 @@ typedef struct {
     sk_alphatype_t  alphaType;
 } sk_imageinfo_t;
 
+typedef struct {
+    float   x;
+    float   y;
+} sk_point_t;
+
 typedef struct {
     float   left;
     float   top;
@@ -64,10 +69,15 @@ typedef struct {
     float   bottom;
 } sk_rect_t;
 
+typedef struct {
+    float   mat[9];
+} sk_matrix_t;
+
 typedef struct sk_canvas_t sk_canvas_t;
 typedef struct sk_image_t sk_image_t;
 typedef struct sk_paint_t sk_paint_t;
 typedef struct sk_path_t sk_path_t;
+typedef struct sk_shader_t sk_shader_t;
 typedef struct sk_surface_t sk_surface_t;
 
 //////////////////////////////////////////////////////////////////////////////////////////
index 45a1ef9c3274ee2a19d9b1487d6459e5ef998cd7..1cd4f3d87e37c84de4753cfb722c53f314c0baff 100644 (file)
@@ -147,6 +147,10 @@ static SkCanvas* AsCanvas(sk_canvas_t* ccanvas) {
     return reinterpret_cast<SkCanvas*>(ccanvas);
 }
 
+static SkShader* AsShader(sk_shader_t* cshader) {
+    return reinterpret_cast<SkShader*>(cshader);
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////
 
 sk_colortype_t sk_colortype_get_default_8888() {
@@ -214,6 +218,10 @@ void sk_paint_set_color(sk_paint_t* cpaint, sk_color_t c) {
     AsPaint(cpaint)->setColor(c);
 }
 
+void sk_paint_set_shader(sk_paint_t* cpaint, sk_shader_t* cshader) {
+    AsPaint(cpaint)->setShader(AsShader(cshader));
+}
+
 ///////////////////////////////////////////////////////////////////////////////////////////
 
 sk_path_t* sk_path_new() {
@@ -340,9 +348,8 @@ sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pi
     return (sk_surface_t*)SkSurface::NewRasterDirect(info, pixels, rowBytes);
 }
 
-void sk_surface_delete(sk_surface_t* csurf) {
-    SkSurface* surf = (SkSurface*)csurf;
-    SkSafeUnref(surf);
+void sk_surface_unref(sk_surface_t* csurf) {
+    SkSafeUnref((SkSurface*)csurf);
 }
 
 sk_canvas_t* sk_surface_get_canvas(sk_surface_t* csurf) {
@@ -355,8 +362,66 @@ sk_image_t* sk_surface_new_image_snapshot(sk_surface_t* csurf) {
     return (sk_image_t*)surf->newImageSnapshot();
 }
 
+///////////////////////////////////////////////////////////////////////////////////////////
+
+#include "../../include/effects/SkGradientShader.h"
+#include "sk_shader.h"
+
+const struct {
+    sk_shader_tilemode_t    fC;
+    SkShader::TileMode      fSK;
+} gTileModeMap[] = {
+    { CLAMP_SK_SHADER_TILEMODE,     SkShader::kClamp_TileMode },
+    { REPEAT_SK_SHADER_TILEMODE,    SkShader::kRepeat_TileMode },
+    { MIRROR_SK_SHADER_TILEMODE,    SkShader::kMirror_TileMode  },
+};
+
+static bool from_c_tilemode(sk_shader_tilemode_t cMode, SkShader::TileMode* skMode) {
+    for (size_t i = 0; i < SK_ARRAY_COUNT(gTileModeMap); ++i) {
+        if (cMode == gTileModeMap[i].fC) {
+            if (skMode) {
+                *skMode = gTileModeMap[i].fSK;
+            }
+            return true;
+        }
+    }
+    return false;
+}
+
+void sk_shader_ref(sk_shader_t* cshader) {
+    SkSafeRef(AsShader(cshader));
+}
+
+void sk_shader_unref(sk_shader_t* cshader) {
+    SkSafeUnref(AsShader(cshader));
+}
+
+sk_shader_t* sk_shader_new_linear_gradient(const sk_point_t pts[2],
+                                           const sk_color_t colors[],
+                                           const float colorPos[],
+                                           int colorCount,
+                                           sk_shader_tilemode_t cmode,
+                                           const sk_matrix_t* cmatrix) {
+    SkShader::TileMode mode;
+    if (!from_c_tilemode(cmode, &mode)) {
+        return NULL;
+    }
+    SkMatrix matrix;
+    if (cmatrix) {
+        matrix.setAll(cmatrix->mat[0], cmatrix->mat[1], cmatrix->mat[2],
+                      cmatrix->mat[3], cmatrix->mat[4], cmatrix->mat[5],
+                      cmatrix->mat[6], cmatrix->mat[7], cmatrix->mat[8]);
+    } else {
+        matrix.setIdentity();
+    }
+    SkShader* s = SkGradientShader::CreateLinear(reinterpret_cast<const SkPoint*>(pts),
+                                                 reinterpret_cast<const SkColor*>(colors),
+                                                 colorPos, colorCount, mode, 0, &matrix);
+    return (sk_shader_t*)s;
+}
 
-///////////////////
+///////////////////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////////////////
 
 void sk_test_capi(SkCanvas* canvas) {
     sk_imageinfo_t cinfo;
@@ -398,5 +463,5 @@ void sk_test_capi(SkCanvas* canvas) {
     sk_path_delete(cpath);
     sk_paint_delete(cpaint);
     sk_image_unref(cimage);
-    sk_surface_delete(csurface);
+    sk_surface_unref(csurface);
 }
index 8127e9b10641608890e6f1e66cec849196cb5aed..81702ac7d08287b42c3f2c0fbf67b6c6fbc265f9 100644 (file)
@@ -31,7 +31,7 @@ static void test_c(skiatest::Reporter* reporter) {
     REPORTER_ASSERT(reporter, 0xFFFFFFFF == pixel[0]);
 
     sk_paint_delete(paint);
-    sk_surface_delete(surface);
+    sk_surface_unref(surface);
 }
 
 DEF_TEST(C_API, reporter) {