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
;
19 JavaBitmap::JavaBitmap(jobject bitmap
)
22 int err
= AndroidBitmap_lockPixels(AttachCurrentThread(), bitmap_
, &pixels_
);
26 AndroidBitmapInfo info
;
27 err
= AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_
, &info
);
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_
);
41 bool JavaBitmap::RegisterJavaBitmap(JNIEnv
* env
) {
42 return RegisterNativesImpl(env
);
45 static int SkColorTypeToBitmapFormat(SkColorType 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
:
58 return BITMAP_FORMAT_NO_CONFIG
;
62 ScopedJavaLocalRef
<jobject
> CreateJavaBitmap(int width
,
64 SkColorType color_type
) {
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
) {
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());
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();
97 switch (jbitmap
.format()) {
98 case ANDROID_BITMAP_FORMAT_RGBA_8888
:
99 skbitmap
.allocPixels(SkImageInfo::MakeN32Premul(src_size
.width(),
103 case ANDROID_BITMAP_FORMAT_A_8
:
104 skbitmap
.allocPixels(SkImageInfo::MakeA8(src_size
.width(),
109 CHECK(false) << "Invalid Java bitmap format: " << jbitmap
.format();
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());
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
:
134 return kUnknown_SkColorType
;