Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / gfx / android / java_bitmap.cc
blobea0a50fe6a927e133d71336bbcfbe2274cda9d3a
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/android/java_bitmap.h"
7 #include <android/bitmap.h>
9 #include "base/android/jni_string.h"
10 #include "base/logging.h"
11 #include "jni/BitmapHelper_jni.h"
12 #include "ui/gfx/geometry/size.h"
14 using base::android::AttachCurrentThread;
15 using base::android::ConvertUTF8ToJavaString;
17 namespace gfx {
19 JavaBitmap::JavaBitmap(jobject bitmap)
20 : bitmap_(bitmap),
21 pixels_(NULL) {
22 int err = AndroidBitmap_lockPixels(AttachCurrentThread(), bitmap_, &pixels_);
23 DCHECK(!err);
24 DCHECK(pixels_);
26 AndroidBitmapInfo info;
27 err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info);
28 DCHECK(!err);
29 size_ = gfx::Size(info.width, info.height);
30 format_ = info.format;
31 stride_ = info.stride;
32 byte_count_ = Java_BitmapHelper_getByteCount(AttachCurrentThread(), bitmap_);
35 JavaBitmap::~JavaBitmap() {
36 int err = AndroidBitmap_unlockPixels(AttachCurrentThread(), bitmap_);
37 DCHECK(!err);
40 // static
41 bool JavaBitmap::RegisterJavaBitmap(JNIEnv* env) {
42 return RegisterNativesImpl(env);
45 static int SkColorTypeToBitmapFormat(SkColorType color_type) {
46 switch (color_type) {
47 case kAlpha_8_SkColorType:
48 return BITMAP_FORMAT_ALPHA_8;
49 case kARGB_4444_SkColorType:
50 return BITMAP_FORMAT_ARGB_4444;
51 case kN32_SkColorType:
52 return BITMAP_FORMAT_ARGB_8888;
53 case kRGB_565_SkColorType:
54 return BITMAP_FORMAT_RGB_565;
55 case kUnknown_SkColorType:
56 default:
57 NOTREACHED();
58 return BITMAP_FORMAT_NO_CONFIG;
62 ScopedJavaLocalRef<jobject> CreateJavaBitmap(int width,
63 int height,
64 SkColorType color_type) {
65 DCHECK_GT(width, 0);
66 DCHECK_GT(height, 0);
67 int java_bitmap_config = SkColorTypeToBitmapFormat(color_type);
68 return Java_BitmapHelper_createBitmap(
69 AttachCurrentThread(), width, height, java_bitmap_config);
72 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) {
73 DCHECK(skbitmap);
74 DCHECK(!skbitmap->isNull());
75 SkColorType color_type = skbitmap->colorType();
76 DCHECK((color_type == kRGB_565_SkColorType) ||
77 (color_type == kN32_SkColorType));
78 ScopedJavaLocalRef<jobject> jbitmap = CreateJavaBitmap(
79 skbitmap->width(), skbitmap->height(), color_type);
80 SkAutoLockPixels src_lock(*skbitmap);
81 JavaBitmap dst_lock(jbitmap.obj());
82 void* src_pixels = skbitmap->getPixels();
83 void* dst_pixels = dst_lock.pixels();
84 memcpy(dst_pixels, src_pixels, skbitmap->getSize());
86 return jbitmap;
89 SkBitmap CreateSkBitmapFromJavaBitmap(const JavaBitmap& jbitmap) {
90 DCHECK(!jbitmap.size().IsEmpty());
91 DCHECK_GT(jbitmap.stride(), 0U);
92 DCHECK(jbitmap.pixels());
94 gfx::Size src_size = jbitmap.size();
96 SkBitmap skbitmap;
97 switch (jbitmap.format()) {
98 case ANDROID_BITMAP_FORMAT_RGBA_8888:
99 skbitmap.allocPixels(SkImageInfo::MakeN32Premul(src_size.width(),
100 src_size.height()),
101 jbitmap.stride());
102 break;
103 case ANDROID_BITMAP_FORMAT_A_8:
104 skbitmap.allocPixels(SkImageInfo::MakeA8(src_size.width(),
105 src_size.height()),
106 jbitmap.stride());
107 break;
108 default:
109 CHECK(false) << "Invalid Java bitmap format: " << jbitmap.format();
110 break;
112 CHECK_EQ(jbitmap.byte_count(), static_cast<int>(skbitmap.getSize()));
113 const void* src_pixels = jbitmap.pixels();
114 void* dst_pixels = skbitmap.getPixels();
115 memcpy(dst_pixels, src_pixels, skbitmap.getSize());
117 return skbitmap;
120 SkColorType ConvertToSkiaColorType(jobject bitmap_config) {
121 int jbitmap_config = Java_BitmapHelper_getBitmapFormatForConfig(
122 AttachCurrentThread(), bitmap_config);
123 switch (jbitmap_config) {
124 case BITMAP_FORMAT_ALPHA_8:
125 return kAlpha_8_SkColorType;
126 case BITMAP_FORMAT_ARGB_4444:
127 return kARGB_4444_SkColorType;
128 case BITMAP_FORMAT_ARGB_8888:
129 return kN32_SkColorType;
130 case BITMAP_FORMAT_RGB_565:
131 return kRGB_565_SkColorType;
132 case BITMAP_FORMAT_NO_CONFIG:
133 default:
134 return kUnknown_SkColorType;
138 } // namespace gfx