Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(509)

Unified Diff: skia/public/type_converters.cc

Issue 1028543002: Turn the utility process image decoder into a Mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and address comments. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « skia/public/type_converters.h ('k') | skia/skia.gyp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: skia/public/type_converters.cc
diff --git a/skia/public/type_converters.cc b/skia/public/type_converters.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fe8bbc3f384674a1d42c03141f551fa05aea0dd6
--- /dev/null
+++ b/skia/public/type_converters.cc
@@ -0,0 +1,142 @@
+// 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();
+ SkAutoLockPixels lock(bitmap);
+ if (!bitmap.getPixels())
+ return nullptr;
+ 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
« no previous file with comments | « skia/public/type_converters.h ('k') | skia/skia.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698