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
;
34 JavaBitmap::~JavaBitmap() {
35 int err
= AndroidBitmap_unlockPixels(AttachCurrentThread(), bitmap_
);
40 bool JavaBitmap::RegisterJavaBitmap(JNIEnv
* env
) {
41 return RegisterNativesImpl(env
);
44 static int SkColorTypeToBitmapFormat(SkColorType 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
:
57 return BITMAP_FORMAT_NO_CONFIG
;
61 ScopedJavaLocalRef
<jobject
> CreateJavaBitmap(int width
,
63 SkColorType color_type
) {
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
) {
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());
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();
96 switch (jbitmap
.format()) {
97 case ANDROID_BITMAP_FORMAT_RGBA_8888
:
98 skbitmap
.allocPixels(SkImageInfo::MakeN32Premul(src_size
.width(),
102 case ANDROID_BITMAP_FORMAT_A_8
:
103 skbitmap
.allocPixels(SkImageInfo::MakeA8(src_size
.width(),
108 CHECK(false) << "Invalid Java bitmap format: " << jbitmap
.format();
111 const void* src_pixels
= jbitmap
.pixels();
112 void* dst_pixels
= skbitmap
.getPixels();
113 memcpy(dst_pixels
, src_pixels
, skbitmap
.getSize());
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
:
132 return kUnknown_SkColorType
;