OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "ui/gfx/android/bitmap_android.h" | |
6 | |
7 #include <android/bitmap.h> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/logging.h" | |
11 #include "third_party/skia/include/core/SkBitmap.h" | |
12 #include "ui/gfx/size.h" | |
13 | |
14 using base::android::AttachCurrentThread; | |
15 using base::android::GetClass; | |
16 using base::android::GetMethodID; | |
17 using base::android::GetStaticFieldID; | |
18 using base::android::GetStaticMethodID; | |
19 using gfx::AutoLockJavaBitmap; | |
nilesh
2012/08/03 04:52:36
start "namespace gfx" here and move anonymous name
Jesse Greenwald
2012/08/03 20:10:06
Done.
| |
20 | |
21 AutoLockJavaBitmap::AutoLockJavaBitmap(jobject bitmap) : | |
sky
2012/08/03 17:15:36
: on next line.
Jesse Greenwald
2012/08/03 20:10:06
Done.
| |
22 bitmap_(bitmap), | |
sky
2012/08/03 17:15:36
indentation is off.
Jesse Greenwald
2012/08/03 20:10:06
Done.
| |
23 pixels_(NULL) { | |
24 int err = AndroidBitmap_lockPixels(AttachCurrentThread(), bitmap_, &pixels_); | |
25 DCHECK(!err); | |
26 DCHECK(pixels_); | |
27 } | |
28 | |
29 AutoLockJavaBitmap::~AutoLockJavaBitmap() { | |
30 // TODO(aelias): Add AndroidBitmap_notifyPixelsChanged() call here | |
Yaron
2012/08/02 18:40:10
remove comment
Jesse Greenwald
2012/08/03 20:10:06
Done.
| |
31 // once it's added to the NDK. Using hardware bitmaps will | |
32 // be broken until this is fixed. | |
33 int err = AndroidBitmap_unlockPixels(AttachCurrentThread(), bitmap_); | |
34 DCHECK(!err); | |
35 } | |
36 | |
37 gfx::Size AutoLockJavaBitmap::size() const { | |
38 AndroidBitmapInfo info; | |
39 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); | |
40 DCHECK(!err); | |
41 return gfx::Size(info.width, info.height); | |
42 } | |
43 | |
44 int AutoLockJavaBitmap::format() const { | |
45 AndroidBitmapInfo info; | |
46 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); | |
47 DCHECK(!err); | |
48 return info.format; | |
49 } | |
50 | |
51 uint32_t AutoLockJavaBitmap::stride() const { | |
52 AndroidBitmapInfo info; | |
53 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); | |
nilesh
2012/08/03 04:52:36
you can skip the variable "err" and directly check
Jesse Greenwald
2012/08/03 20:10:06
The call to AndroidBitmap_getInfo has side effects
nilesh
2012/08/03 20:41:25
My bad, thanks for pointing out.
| |
54 DCHECK(!err); | |
55 return info.stride; | |
56 } | |
57 | |
58 namespace { | |
59 static jclass g_AndroidBitmap_clazz = NULL; | |
60 static jmethodID g_AndroidBitmap_createBitmap_method = NULL; | |
61 static jobject g_BitmapConfig_ARGB8888 = NULL; | |
62 } // anonymous namespace | |
63 | |
64 namespace gfx { | |
65 | |
66 void RegisterBitmapAndroid(JNIEnv* env) { | |
sky
2012/08/03 17:15:36
Why is there here and not in the location it's cal
Jesse Greenwald
2012/08/03 20:10:06
The function is forward declared in gfx_jni_regist
sky
2012/08/03 22:33:29
I don't see any calls to this function here. Can't
Jesse Greenwald
2012/08/03 22:41:51
This function initializes various g_* globals (see
| |
67 g_AndroidBitmap_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( | |
68 base::android::GetUnscopedClass(env, "android/graphics/Bitmap"))); | |
69 ScopedJavaLocalRef<jclass> bitmapConfig_clazz = base::android::GetClass( | |
70 env, "android/graphics/Bitmap$Config"); | |
71 g_AndroidBitmap_createBitmap_method = GetStaticMethodID(env, | |
72 g_AndroidBitmap_clazz, "createBitmap", | |
73 "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;"); | |
74 jfieldID argb_8888_id = GetStaticFieldID(env, bitmapConfig_clazz, "ARGB_8888", | |
75 "Landroid/graphics/Bitmap$Config;"); | |
76 g_BitmapConfig_ARGB8888 = reinterpret_cast<jobject>(env->NewGlobalRef( | |
77 env->GetStaticObjectField(bitmapConfig_clazz.obj(), argb_8888_id))); | |
78 } | |
79 | |
80 ScopedJavaLocalRef<jobject> CreateJavaBitmap(const gfx::Size& size) { | |
81 DCHECK(g_AndroidBitmap_clazz); | |
82 JNIEnv* env = AttachCurrentThread(); | |
83 jobject bitmap_object = env->CallStaticObjectMethod(g_AndroidBitmap_clazz, | |
84 g_AndroidBitmap_createBitmap_method, size.width(), size.height(), | |
85 g_BitmapConfig_ARGB8888); | |
86 return ScopedJavaLocalRef<jobject>(env, bitmap_object); | |
87 } | |
88 | |
89 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) { | |
90 DCHECK(skbitmap); | |
91 DCHECK_EQ(skbitmap->bytesPerPixel(), 4); | |
92 | |
93 ScopedJavaLocalRef<jobject> jbitmap = | |
94 gfx::CreateJavaBitmap(gfx::Size(skbitmap->width(), skbitmap->height())); | |
nilesh
2012/08/03 04:52:36
You dont need the gfx qualifier as you are in gfx
Jesse Greenwald
2012/08/03 20:10:06
I removed some of the gfx:: qualifiers. However,
| |
95 SkAutoLockPixels src_lock(*skbitmap); | |
96 AutoLockJavaBitmap dst_lock(jbitmap.obj()); | |
97 void* src_pixels = skbitmap->getPixels(); | |
98 void* dst_pixels = dst_lock.pixels(); | |
99 memcpy(dst_pixels, src_pixels, skbitmap->getSize()); | |
100 | |
101 return jbitmap; | |
102 } | |
103 | |
104 } // gfx namespace | |
nilesh
2012/08/03 04:52:36
namespace gfx
Jesse Greenwald
2012/08/03 20:10:06
Done.
| |
OLD | NEW |