summaryrefslogtreecommitdiffstats
path: root/skia/public
diff options
context:
space:
mode:
authoramistry <amistry@chromium.org>2015-06-03 03:45:55 -0700
committerCommit bot <commit-bot@chromium.org>2015-06-03 10:46:27 +0000
commit84c5d6b1f4f32222c611157758f725036e00ba6a (patch)
treee18054073c17c56c2c93a812b6775c64a3985b28 /skia/public
parent7d75e630cd925d355b6e13d5a0ab4b3dc6a63229 (diff)
downloadchromium_src-84c5d6b1f4f32222c611157758f725036e00ba6a.zip
chromium_src-84c5d6b1f4f32222c611157758f725036e00ba6a.tar.gz
chromium_src-84c5d6b1f4f32222c611157758f725036e00ba6a.tar.bz2
Add a Mojo interface for SkBitmap and appropriate type converters.
Review URL: https://codereview.chromium.org/1150083005 Cr-Commit-Position: refs/heads/master@{#332585}
Diffstat (limited to 'skia/public')
-rw-r--r--skia/public/BUILD.gn20
-rw-r--r--skia/public/interfaces/BUILD.gn11
-rw-r--r--skia/public/interfaces/bitmap.mojom43
-rw-r--r--skia/public/type_converters.cc139
-rw-r--r--skia/public/type_converters.h26
5 files changed, 239 insertions, 0 deletions
diff --git a/skia/public/BUILD.gn b/skia/public/BUILD.gn
new file mode 100644
index 0000000..ce407df
--- /dev/null
+++ b/skia/public/BUILD.gn
@@ -0,0 +1,20 @@
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+source_set("public") {
+ sources = [
+ "type_converters.cc",
+ "type_converters.h",
+ ]
+
+ public_deps = [
+ "//skia/public/interfaces",
+ ]
+
+ deps = [
+ "//base",
+ "//skia",
+ "//third_party/mojo/src/mojo/public/cpp/bindings",
+ ]
+}
diff --git a/skia/public/interfaces/BUILD.gn b/skia/public/interfaces/BUILD.gn
new file mode 100644
index 0000000..ea57c50
--- /dev/null
+++ b/skia/public/interfaces/BUILD.gn
@@ -0,0 +1,11 @@
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import("//third_party/mojo/src/mojo/public/tools/bindings/mojom.gni")
+
+mojom("interfaces") {
+ sources = [
+ "bitmap.mojom",
+ ]
+}
diff --git a/skia/public/interfaces/bitmap.mojom b/skia/public/interfaces/bitmap.mojom
new file mode 100644
index 0000000..fd34120
--- /dev/null
+++ b/skia/public/interfaces/bitmap.mojom
@@ -0,0 +1,43 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file contains structures used to represent SkBitmaps in Mojo.
+module skia;
+
+// Mirror of SkColorType.
+enum ColorType {
+ UNKNOWN,
+ ALPHA_8,
+ RGB_565,
+ ARGB_4444,
+ RGBA_8888,
+ BGRA_8888,
+ INDEX_8,
+ GRAY_8,
+};
+
+// Mirror of SkAlphaType.
+enum AlphaType {
+ UNKNOWN,
+ OPAQUE,
+ PREMUL,
+ UNPREMUL,
+};
+
+// Mirror of SkColorProfileType.
+enum ColorProfileType {
+ LINEAR,
+ SRGB,
+};
+
+struct Bitmap {
+ ColorType color_type;
+ AlphaType alpha_type;
+ ColorProfileType profile_type;
+
+ uint32 width;
+ uint32 height;
+
+ array<uint8> pixel_data;
+};
diff --git a/skia/public/type_converters.cc b/skia/public/type_converters.cc
new file mode 100644
index 0000000..143710b
--- /dev/null
+++ b/skia/public/type_converters.cc
@@ -0,0 +1,139 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "skia/public/type_converters.h"
+
+#include <string.h>
+
+#include "base/logging.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+
+namespace mojo {
+
+namespace {
+
+SkColorType MojoColorTypeToSk(skia::ColorType type) {
+ switch (type) {
+ case skia::COLOR_TYPE_UNKNOWN:
+ return kUnknown_SkColorType;
+ case skia::COLOR_TYPE_ALPHA_8:
+ return kAlpha_8_SkColorType;
+ case skia::COLOR_TYPE_RGB_565:
+ return kRGB_565_SkColorType;
+ case skia::COLOR_TYPE_ARGB_4444:
+ return kARGB_4444_SkColorType;
+ case skia::COLOR_TYPE_RGBA_8888:
+ return kRGBA_8888_SkColorType;
+ case skia::COLOR_TYPE_BGRA_8888:
+ return kBGRA_8888_SkColorType;
+ case skia::COLOR_TYPE_INDEX_8:
+ return kIndex_8_SkColorType;
+ case skia::COLOR_TYPE_GRAY_8:
+ return kGray_8_SkColorType;
+ default:
+ NOTREACHED();
+ }
+ return kUnknown_SkColorType;
+}
+
+SkAlphaType MojoAlphaTypeToSk(skia::AlphaType type) {
+ switch (type) {
+ case skia::ALPHA_TYPE_UNKNOWN:
+ return kUnknown_SkAlphaType;
+ case skia::ALPHA_TYPE_OPAQUE:
+ return kOpaque_SkAlphaType;
+ case skia::ALPHA_TYPE_PREMUL:
+ return kPremul_SkAlphaType;
+ case skia::ALPHA_TYPE_UNPREMUL:
+ return kUnpremul_SkAlphaType;
+ default:
+ NOTREACHED();
+ }
+ return kUnknown_SkAlphaType;
+}
+
+skia::ColorType SkColorTypeToMojo(SkColorType type) {
+ switch (type) {
+ case kUnknown_SkColorType:
+ return skia::COLOR_TYPE_UNKNOWN;
+ case kAlpha_8_SkColorType:
+ return skia::COLOR_TYPE_ALPHA_8;
+ case kRGB_565_SkColorType:
+ return skia::COLOR_TYPE_RGB_565;
+ case kARGB_4444_SkColorType:
+ return skia::COLOR_TYPE_ARGB_4444;
+ case kRGBA_8888_SkColorType:
+ return skia::COLOR_TYPE_RGBA_8888;
+ case kBGRA_8888_SkColorType:
+ return skia::COLOR_TYPE_BGRA_8888;
+ case kIndex_8_SkColorType:
+ return skia::COLOR_TYPE_INDEX_8;
+ case kGray_8_SkColorType:
+ return skia::COLOR_TYPE_GRAY_8;
+ default:
+ NOTREACHED();
+ }
+ return skia::COLOR_TYPE_UNKNOWN;
+}
+
+skia::AlphaType SkAlphaTypeToMojo(SkAlphaType type) {
+ switch (type) {
+ case kUnknown_SkAlphaType:
+ return skia::ALPHA_TYPE_UNKNOWN;
+ case kOpaque_SkAlphaType:
+ return skia::ALPHA_TYPE_OPAQUE;
+ case kPremul_SkAlphaType:
+ return skia::ALPHA_TYPE_PREMUL;
+ case kUnpremul_SkAlphaType:
+ return skia::ALPHA_TYPE_UNPREMUL;
+ default:
+ NOTREACHED();
+ }
+ return skia::ALPHA_TYPE_UNKNOWN;
+}
+
+} // namespace
+
+SkBitmap TypeConverter<SkBitmap, skia::BitmapPtr>::Convert(
+ const skia::BitmapPtr& image) {
+ SkBitmap bitmap;
+ if (image.is_null())
+ return bitmap;
+ if (!bitmap.tryAllocPixels(SkImageInfo::Make(
+ image->width, image->height, MojoColorTypeToSk(image->color_type),
+ MojoAlphaTypeToSk(image->alpha_type)))) {
+ return SkBitmap();
+ }
+ if (bitmap.getSize() != image->pixel_data.size() || !bitmap.getPixels()) {
+ return SkBitmap();
+ }
+
+ memcpy(bitmap.getPixels(), &image->pixel_data[0], bitmap.getSize());
+ return bitmap;
+}
+
+skia::BitmapPtr TypeConverter<skia::BitmapPtr, SkBitmap>::Convert(
+ const SkBitmap& bitmap) {
+ if (bitmap.isNull())
+ return nullptr;
+
+ // NOTE: This code doesn't correctly serialize Index8 bitmaps.
+ const SkImageInfo& info = bitmap.info();
+ DCHECK_NE(info.colorType(), kIndex_8_SkColorType);
+ if (info.colorType() == kIndex_8_SkColorType)
+ return nullptr;
+ skia::BitmapPtr result = skia::Bitmap::New();
+ result->color_type = SkColorTypeToMojo(info.colorType());
+ result->alpha_type = SkAlphaTypeToMojo(info.alphaType());
+ result->width = info.width();
+ result->height = info.height();
+ size_t size = bitmap.getSize();
+ size_t row_bytes = bitmap.rowBytes();
+ result->pixel_data = mojo::Array<uint8_t>::New(size);
+ if (!bitmap.readPixels(info, &result->pixel_data[0], row_bytes, 0, 0))
+ return nullptr;
+ return result.Pass();
+}
+
+} // namespace mojo
diff --git a/skia/public/type_converters.h b/skia/public/type_converters.h
new file mode 100644
index 0000000..341fc69
--- /dev/null
+++ b/skia/public/type_converters.h
@@ -0,0 +1,26 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SKIA_PUBLIC_TYPE_CONVERTERS_H_
+#define SKIA_PUBLIC_TYPE_CONVERTERS_H_
+
+#include "skia/public/interfaces/bitmap.mojom.h"
+
+class SkBitmap;
+
+namespace mojo {
+
+template <>
+struct TypeConverter<SkBitmap, skia::BitmapPtr> {
+ static SkBitmap Convert(const skia::BitmapPtr& image);
+};
+
+template <>
+struct TypeConverter<skia::BitmapPtr, SkBitmap> {
+ static skia::BitmapPtr Convert(const SkBitmap& bitmap);
+};
+
+} // namespace mojo
+
+#endif // SKIA_PUBLIC_TYPE_CONVERTERS_H_