Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / android / java_bitmap.cc
blob37da9d98aaf67e231d739460038564a29f317a09
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;
34 JavaBitmap::~JavaBitmap() {
35 int err = AndroidBitmap_unlockPixels(AttachCurrentThread(), bitmap_);
36 DCHECK(!err);
39 // static
40 bool JavaBitmap::RegisterJavaBitmap(JNIEnv* env) {
41 return RegisterNativesImpl(env);
44 static int SkColorTypeToBitmapFormat(SkColorType color_type) {
45 switch (color_type) {
46 case kAlpha_8_SkColorType:
47 return BITMAP_FORMAT_ALPHA_8;
48 case kARGB_4444_SkColorType:
49 return BITMAP_FORMAT_ARGB_4444;
50 case kN32_SkColorType:
51 return BITMAP_FORMAT_ARGB_8888;
52 case kRGB_565_SkColorType:
53 return BITMAP_FORMAT_RGB_565;
54 case kUnknown_SkColorType:
55 default:
56 NOTREACHED();
57 return BITMAP_FORMAT_NO_CONFIG;
61 ScopedJavaLocalRef<jobject> CreateJavaBitmap(int width,
62 int height,
63 SkColorType color_type) {
64 DCHECK_GT(width, 0);
65 DCHECK_GT(height, 0);
66 int java_bitmap_config = SkColorTypeToBitmapFormat(color_type);
67 return Java_BitmapHelper_createBitmap(
68 AttachCurrentThread(), width, height, java_bitmap_config);
71 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) {
72 DCHECK(skbitmap);
73 DCHECK(!skbitmap->isNull());
74 SkColorType color_type = skbitmap->colorType();
75 DCHECK((color_type == kRGB_565_SkColorType) ||
76 (color_type == kN32_SkColorType));
77 ScopedJavaLocalRef<jobject> jbitmap = CreateJavaBitmap(
78 skbitmap->width(), skbitmap->height(), color_type);
79 SkAutoLockPixels src_lock(*skbitmap);
80 JavaBitmap dst_lock(jbitmap.obj());
81 void* src_pixels = skbitmap->getPixels();
82 void* dst_pixels = dst_lock.pixels();
83 memcpy(dst_pixels, src_pixels, skbitmap->getSize());
85 return jbitmap;
88 SkBitmap CreateSkBitmapFromJavaBitmap(const JavaBitmap& jbitmap) {
89 DCHECK(!jbitmap.size().IsEmpty());
90 DCHECK_GT(jbitmap.stride(), 0U);
91 DCHECK(jbitmap.pixels());
93 gfx::Size src_size = jbitmap.size();
95 SkBitmap skbitmap;
96 switch (jbitmap.format()) {
97 case ANDROID_BITMAP_FORMAT_RGBA_8888:
98 skbitmap.allocPixels(SkImageInfo::MakeN32Premul(src_size.width(),
99 src_size.height()),
100 jbitmap.stride());
101 break;
102 case ANDROID_BITMAP_FORMAT_A_8:
103 skbitmap.allocPixels(SkImageInfo::MakeA8(src_size.width(),
104 src_size.height()),
105 jbitmap.stride());
106 break;
107 default:
108 CHECK(false) << "Invalid Java bitmap format: " << jbitmap.format();
109 break;
111 const void* src_pixels = jbitmap.pixels();
112 void* dst_pixels = skbitmap.getPixels();
113 memcpy(dst_pixels, src_pixels, skbitmap.getSize());
115 return skbitmap;
118 SkColorType ConvertToSkiaColorType(jobject bitmap_config) {
119 int jbitmap_config = Java_BitmapHelper_getBitmapFormatForConfig(
120 AttachCurrentThread(), bitmap_config);
121 switch (jbitmap_config) {
122 case BITMAP_FORMAT_ALPHA_8:
123 return kAlpha_8_SkColorType;
124 case BITMAP_FORMAT_ARGB_4444:
125 return kARGB_4444_SkColorType;
126 case BITMAP_FORMAT_ARGB_8888:
127 return kN32_SkColorType;
128 case BITMAP_FORMAT_RGB_565:
129 return kRGB_565_SkColorType;
130 case BITMAP_FORMAT_NO_CONFIG:
131 default:
132 return kUnknown_SkColorType;
136 } // namespace gfx