aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gm/cgm.c9
-rw-r--r--gn/core.gni1
-rw-r--r--include/c/sk_colorspace.h25
-rw-r--r--include/c/sk_imageinfo.h62
-rw-r--r--include/c/sk_types.h36
-rw-r--r--src/c/sk_imageinfo.cpp143
-rw-r--r--src/c/sk_surface.cpp104
-rw-r--r--tests/CTest.cpp18
8 files changed, 262 insertions, 136 deletions
diff --git a/gm/cgm.c b/gm/cgm.c
index bbd170b9d3..e9d29460a9 100644
--- a/gm/cgm.c
+++ b/gm/cgm.c
@@ -11,6 +11,7 @@
#include "sk_canvas.h"
#include "sk_data.h"
#include "sk_image.h"
+#include "sk_imageinfo.h"
#include "sk_paint.h"
#include "sk_shader.h"
#include "sk_surface.h"
@@ -50,11 +51,11 @@ static void do_draw(sk_canvas_t* canvas) {
void sk_test_c_api(sk_canvas_t* canvas) {
do_draw(canvas);
- sk_imageinfo_t info = {
- W, H, sk_colortype_get_default_8888(), OPAQUE_SK_ALPHATYPE
- };
+ sk_imageinfo_t* info = sk_imageinfo_new(W, H, RGBA_8888_SK_COLORTYPE, OPAQUE_SK_ALPHATYPE,
+ NULL);
sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY };
- sk_surface_t* surf = sk_surface_new_raster(&info, &surfaceProps);
+ sk_surface_t* surf = sk_surface_new_raster(info, &surfaceProps);
+ sk_imageinfo_delete(info);
do_draw(sk_surface_get_canvas(surf));
sk_image_t* img0 = sk_surface_new_image_snapshot(surf);
diff --git a/gn/core.gni b/gn/core.gni
index 65d3892211..f8d7fdf4f9 100644
--- a/gn/core.gni
+++ b/gn/core.gni
@@ -8,6 +8,7 @@ _src = get_path_info("../src", "abspath")
_include = get_path_info("../include", "abspath")
skia_core_sources = [
+ "$_src/c/sk_imageinfo.cpp",
"$_src/c/sk_paint.cpp",
"$_src/c/sk_surface.cpp",
"$_src/c/sk_types_priv.h",
diff --git a/include/c/sk_colorspace.h b/include/c/sk_colorspace.h
new file mode 100644
index 0000000000..f96d9bc0b1
--- /dev/null
+++ b/include/c/sk_colorspace.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2018 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_imageinfo_DEFINED
+#define sk_imageinfo_DEFINED
+
+#include "sk_types.h"
+
+SK_C_PLUS_PLUS_BEGIN_GUARD
+
+sk_colorspace_t* sk_colorspace_new_srgb();
+
+void sk_colorspace_ref(sk_colorspace_t*);
+void sk_colorspace_unref(sk_colorspace_t*);
+
+SK_C_PLUS_PLUS_END_GUARD
+
+#endif
diff --git a/include/c/sk_imageinfo.h b/include/c/sk_imageinfo.h
new file mode 100644
index 0000000000..08f88b01be
--- /dev/null
+++ b/include/c/sk_imageinfo.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2018 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_imageinfo_DEFINED
+#define sk_imageinfo_DEFINED
+
+#include "sk_types.h"
+
+SK_C_PLUS_PLUS_BEGIN_GUARD
+
+typedef enum {
+ UNKNOWN_SK_COLORTYPE,
+ RGBA_8888_SK_COLORTYPE,
+ BGRA_8888_SK_COLORTYPE,
+ ALPHA_8_SK_COLORTYPE,
+ GRAY_8_SK_COLORTYPE,
+ RGBA_F16_SK_COLORTYPE,
+ RGBA_F32_SK_COLORTYPE,
+} sk_colortype_t;
+
+typedef enum {
+ OPAQUE_SK_ALPHATYPE,
+ PREMUL_SK_ALPHATYPE,
+ UNPREMUL_SK_ALPHATYPE,
+} sk_alphatype_t;
+
+/**
+ * Allocate a new imageinfo object. If colorspace is not null, it's owner-count will be
+ * incremented automatically.
+ */
+sk_imageinfo_t* sk_imageinfo_new(int width, int height, sk_colortype_t ct, sk_alphatype_t at,
+ sk_colorspace_t* cs);
+
+/**
+ * Free the imageinfo object. If it contains a reference to a colorspace, its owner-count will
+ * be decremented automatically.
+ */
+void sk_imageinfo_delete(sk_imageinfo_t*);
+
+int32_t sk_imageinfo_get_width(sk_imageinfo_t*);
+int32_t sk_imageinfo_get_height(sk_imageinfo_t*);
+sk_colortype_t sk_imageinfo_get_colortype(sk_imageinfo_t*);
+sk_alphatype_t sk_imageinfo_get_alphatype(sk_imageinfo_t*);
+
+/**
+ * Return the colorspace object reference contained in the imageinfo, or null if there is none.
+ * Note: this does not modify the owner-count on the colorspace object. If the caller needs to
+ * use the colorspace beyond the lifetime of the imageinfo, it should manually call
+ * sk_colorspace_ref() (and then call unref() when it is done).
+ */
+sk_colorspace_t* sk_imageinfo_get_colorspace(sk_imageinfo_t*);
+
+SK_C_PLUS_PLUS_END_GUARD
+
+#endif
diff --git a/include/c/sk_types.h b/include/c/sk_types.h
index 679e106e33..852526f2b6 100644
--- a/include/c/sk_types.h
+++ b/include/c/sk_types.h
@@ -53,19 +53,6 @@ typedef uint32_t sk_color_t;
#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;
-
-typedef enum {
INTERSECT_SK_CLIPTYPE,
DIFFERENCE_SK_CLIPTYPE,
} sk_cliptype_t;
@@ -78,18 +65,6 @@ typedef enum {
BGR_V_SK_PIXELGEOMETRY,
} sk_pixelgeometry_t;
-/**
- Return the default sk_colortype_t; this is operating-system dependent.
-*/
-SK_API sk_colortype_t sk_colortype_get_default_8888(void);
-
-typedef struct {
- int32_t width;
- int32_t height;
- sk_colortype_t colorType;
- sk_alphatype_t alphaType;
-} sk_imageinfo_t;
-
typedef struct {
sk_pixelgeometry_t pixelGeometry;
} sk_surfaceprops_t;
@@ -187,6 +162,17 @@ typedef struct sk_data_t sk_data_t;
encoded data or other means.
*/
typedef struct sk_image_t sk_image_t;
+
+/**
+ * Describes the color components. See ICC Profiles.
+ */
+typedef struct sk_colorspace_t sk_colorspace_t;
+
+/**
+ * Describes an image buffer : width, height, pixel type, colorspace, etc.
+ */
+typedef struct sk_imageinfo_t sk_imageinfo_t;
+
/**
A sk_maskfilter_t is an object that perform transformations on an
alpha-channel mask before drawing it; it may be installed into a
diff --git a/src/c/sk_imageinfo.cpp b/src/c/sk_imageinfo.cpp
new file mode 100644
index 0000000000..992fc36b04
--- /dev/null
+++ b/src/c/sk_imageinfo.cpp
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkImageInfo.h"
+#include "SkColorSpace.h"
+
+#include "sk_imageinfo.h"
+#include "sk_colorspace.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 },
+ { GRAY_8_SK_COLORTYPE, kGray_8_SkColorType },
+ { RGBA_F16_SK_COLORTYPE, kRGBA_F16_SkColorType },
+ { RGBA_F32_SK_COLORTYPE, kRGBA_F32_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 to_c_alphatype(SkAlphaType skAT, sk_alphatype_t* cAT) {
+ for (size_t i = 0; i < SK_ARRAY_COUNT(gAlphaTypeMap); ++i) {
+ if (gAlphaTypeMap[i].fSK == skAT) {
+ if (cAT) {
+ *cAT = gAlphaTypeMap[i].fC;
+ }
+ return true;
+ }
+ }
+ return false;
+}
+
+const SkImageInfo* ToImageInfo(const sk_imageinfo_t* cinfo) {
+ return reinterpret_cast<const SkImageInfo*>(cinfo);
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////////
+
+sk_imageinfo_t* sk_imageinfo_new(int w, int h, sk_colortype_t cct, sk_alphatype_t cat,
+ sk_colorspace_t* ccs) {
+ SkColorType ct;
+ SkAlphaType at;
+ if (!from_c_colortype(cct, &ct) || !from_c_alphatype(cat, &at)) {
+ return nullptr;
+ }
+ SkColorSpace* cs = (SkColorSpace*)ccs;
+
+ SkImageInfo* info = new SkImageInfo(SkImageInfo::Make(w, h, ct, at, sk_ref_sp(cs)));
+ return reinterpret_cast<sk_imageinfo_t*>(info);
+}
+
+void sk_imageinfo_delete(sk_imageinfo_t* cinfo) {
+ delete ToImageInfo(cinfo);
+}
+
+int sk_imageinfo_get_width(const sk_imageinfo_t* cinfo) {
+ return ToImageInfo(cinfo)->width();
+}
+
+int sk_imageinfo_get_height(const sk_imageinfo_t* cinfo) {
+ return ToImageInfo(cinfo)->height();
+}
+
+sk_colortype_t sk_imageinfo_get_colortype(const sk_imageinfo_t* cinfo) {
+ sk_colortype_t ct;
+ return to_c_colortype(ToImageInfo(cinfo)->colorType(), &ct) ? ct : UNKNOWN_SK_COLORTYPE;
+}
+
+sk_alphatype_t sk_imageinfo_get_alphatype(const sk_imageinfo_t* cinfo) {
+ sk_alphatype_t at;
+ // odd that we return premul on failure...
+ return to_c_alphatype(ToImageInfo(cinfo)->alphaType(), &at) ? at : PREMUL_SK_ALPHATYPE;
+}
+
+sk_colorspace_t* sk_imageinfo_get_colorspace(const sk_imageinfo_t* cinfo) {
+ return reinterpret_cast<sk_colorspace_t*>(ToImageInfo(cinfo)->colorSpace());
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////////
+
+sk_colorspace_t* sk_colorspace_new_srgb() {
+ return reinterpret_cast<sk_colorspace_t*>(SkColorSpace::MakeSRGB().release());
+}
+
+void sk_colorspace_ref(sk_colorspace_t* cs) {
+ SkSafeRef(reinterpret_cast<SkColorSpace*>(cs));
+}
+
+void sk_colorspace_unref(sk_colorspace_t* cs) {
+ SkSafeUnref(reinterpret_cast<SkColorSpace*>(cs));
+}
+
diff --git a/src/c/sk_surface.cpp b/src/c/sk_surface.cpp
index a77f61e85f..36894e0355 100644
--- a/src/c/sk_surface.cpp
+++ b/src/c/sk_surface.cpp
@@ -24,79 +24,6 @@
#include "sk_types_priv.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;
-}
-
-const struct {
sk_pixelgeometry_t fC;
SkPixelGeometry fSK;
} gPixelGeometryMap[] = {
@@ -208,23 +135,10 @@ static sk_picture_t* ToPicture(SkPicture* pic) {
///////////////////////////////////////////////////////////////////////////////////////////
-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::MakeRasterCopy(SkPixmap(info, pixels, rowBytes)).release();
+ const SkImageInfo* info = reinterpret_cast<const SkImageInfo*>(cinfo);
+ return (sk_image_t*)SkImage::MakeRasterCopy(SkPixmap(*info, pixels, rowBytes)).release();
}
sk_image_t* sk_image_new_from_encoded(const sk_data_t* cdata, const sk_irect_t* subset) {
@@ -423,33 +337,27 @@ void sk_canvas_draw_picture(sk_canvas_t* ccanvas, const sk_picture_t* cpicture,
sk_surface_t* sk_surface_new_raster(const sk_imageinfo_t* cinfo,
const sk_surfaceprops_t* props) {
- SkImageInfo info;
- if (!from_c_info(*cinfo, &info)) {
- return NULL;
- }
+ const SkImageInfo* info = reinterpret_cast<const SkImageInfo*>(cinfo);
SkPixelGeometry geo = kUnknown_SkPixelGeometry;
if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
return NULL;
}
SkSurfaceProps surfProps(0, geo);
- return (sk_surface_t*)SkSurface::MakeRaster(info, &surfProps).release();
+ return (sk_surface_t*)SkSurface::MakeRaster(*info, &surfProps).release();
}
sk_surface_t* sk_surface_new_raster_direct(const sk_imageinfo_t* cinfo, void* pixels,
size_t rowBytes,
const sk_surfaceprops_t* props) {
- SkImageInfo info;
- if (!from_c_info(*cinfo, &info)) {
- return NULL;
- }
+ const SkImageInfo* info = reinterpret_cast<const SkImageInfo*>(cinfo);
SkPixelGeometry geo = kUnknown_SkPixelGeometry;
if (props && !from_c_pixelgeometry(props->pixelGeometry, &geo)) {
return NULL;
}
SkSurfaceProps surfProps(0, geo);
- return (sk_surface_t*)SkSurface::MakeRasterDirect(info, pixels, rowBytes, &surfProps).release();
+ return (sk_surface_t*)SkSurface::MakeRasterDirect(*info, pixels, rowBytes, &surfProps).release();
}
void sk_surface_unref(sk_surface_t* csurf) {
diff --git a/tests/CTest.cpp b/tests/CTest.cpp
index 425b264718..cd9522f2ff 100644
--- a/tests/CTest.cpp
+++ b/tests/CTest.cpp
@@ -7,6 +7,7 @@
#include "Test.h"
#include "sk_canvas.h"
+#include "sk_imageinfo.h"
#include "sk_paint.h"
#include "sk_shader.h"
#include "sk_surface.h"
@@ -15,9 +16,9 @@
#include <stdint.h>
static void shader_test(skiatest::Reporter* reporter) {
- sk_imageinfo_t info =
- {64, 64, sk_colortype_get_default_8888(), PREMUL_SK_ALPHATYPE};
- sk_surface_t* surface = sk_surface_new_raster(&info, nullptr);
+ sk_imageinfo_t* info = sk_imageinfo_new(64, 64, RGBA_8888_SK_COLORTYPE, PREMUL_SK_ALPHATYPE,
+ NULL);
+ sk_surface_t* surface = sk_surface_new_raster(info, nullptr);
sk_canvas_t* canvas = sk_surface_get_canvas(surface);
sk_paint_t* paint = sk_paint_new();
@@ -52,18 +53,16 @@ static void shader_test(skiatest::Reporter* reporter) {
sk_paint_delete(paint);
sk_surface_unref(surface);
+ sk_imageinfo_delete(info);
}
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
- };
+ sk_imageinfo_t* info = sk_imageinfo_new(1, 1, RGBA_8888_SK_COLORTYPE, PREMUL_SK_ALPHATYPE,
+ NULL);
uint32_t pixel[1] = { 0 };
sk_surfaceprops_t surfaceProps = { UNKNOWN_SK_PIXELGEOMETRY };
- sk_surface_t* surface = sk_surface_new_raster_direct(&info, pixel, sizeof(uint32_t),
+ sk_surface_t* surface = sk_surface_new_raster_direct(info, pixel, sizeof(uint32_t),
&surfaceProps);
sk_paint_t* paint = sk_paint_new();
@@ -82,6 +81,7 @@ static void test_c(skiatest::Reporter* reporter) {
sk_paint_delete(paint);
sk_surface_unref(surface);
+ sk_imageinfo_delete(info);
}
DEF_TEST(C_API, reporter) {