Index: ui/gfx/android/bitmap_android.cc |
diff --git a/ui/gfx/android/bitmap_android.cc b/ui/gfx/android/bitmap_android.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..748533d8ff82322fe5499a551cf2dd3cd5d57f5c |
--- /dev/null |
+++ b/ui/gfx/android/bitmap_android.cc |
@@ -0,0 +1,104 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/gfx/android/bitmap_android.h" |
+ |
+#include <android/bitmap.h> |
+ |
+#include "base/android/jni_android.h" |
+#include "base/logging.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "ui/gfx/size.h" |
+ |
+using base::android::AttachCurrentThread; |
+using base::android::GetClass; |
+using base::android::GetMethodID; |
+using base::android::GetStaticFieldID; |
+using base::android::GetStaticMethodID; |
+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.
|
+ |
+AutoLockJavaBitmap::AutoLockJavaBitmap(jobject bitmap) : |
sky
2012/08/03 17:15:36
: on next line.
Jesse Greenwald
2012/08/03 20:10:06
Done.
|
+ bitmap_(bitmap), |
sky
2012/08/03 17:15:36
indentation is off.
Jesse Greenwald
2012/08/03 20:10:06
Done.
|
+ pixels_(NULL) { |
+ int err = AndroidBitmap_lockPixels(AttachCurrentThread(), bitmap_, &pixels_); |
+ DCHECK(!err); |
+ DCHECK(pixels_); |
+} |
+ |
+AutoLockJavaBitmap::~AutoLockJavaBitmap() { |
+ // 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.
|
+ // once it's added to the NDK. Using hardware bitmaps will |
+ // be broken until this is fixed. |
+ int err = AndroidBitmap_unlockPixels(AttachCurrentThread(), bitmap_); |
+ DCHECK(!err); |
+} |
+ |
+gfx::Size AutoLockJavaBitmap::size() const { |
+ AndroidBitmapInfo info; |
+ int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); |
+ DCHECK(!err); |
+ return gfx::Size(info.width, info.height); |
+} |
+ |
+int AutoLockJavaBitmap::format() const { |
+ AndroidBitmapInfo info; |
+ int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); |
+ DCHECK(!err); |
+ return info.format; |
+} |
+ |
+uint32_t AutoLockJavaBitmap::stride() const { |
+ AndroidBitmapInfo info; |
+ 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.
|
+ DCHECK(!err); |
+ return info.stride; |
+} |
+ |
+namespace { |
+static jclass g_AndroidBitmap_clazz = NULL; |
+static jmethodID g_AndroidBitmap_createBitmap_method = NULL; |
+static jobject g_BitmapConfig_ARGB8888 = NULL; |
+} // anonymous namespace |
+ |
+namespace gfx { |
+ |
+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
|
+ g_AndroidBitmap_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( |
+ base::android::GetUnscopedClass(env, "android/graphics/Bitmap"))); |
+ ScopedJavaLocalRef<jclass> bitmapConfig_clazz = base::android::GetClass( |
+ env, "android/graphics/Bitmap$Config"); |
+ g_AndroidBitmap_createBitmap_method = GetStaticMethodID(env, |
+ g_AndroidBitmap_clazz, "createBitmap", |
+ "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;"); |
+ jfieldID argb_8888_id = GetStaticFieldID(env, bitmapConfig_clazz, "ARGB_8888", |
+ "Landroid/graphics/Bitmap$Config;"); |
+ g_BitmapConfig_ARGB8888 = reinterpret_cast<jobject>(env->NewGlobalRef( |
+ env->GetStaticObjectField(bitmapConfig_clazz.obj(), argb_8888_id))); |
+} |
+ |
+ScopedJavaLocalRef<jobject> CreateJavaBitmap(const gfx::Size& size) { |
+ DCHECK(g_AndroidBitmap_clazz); |
+ JNIEnv* env = AttachCurrentThread(); |
+ jobject bitmap_object = env->CallStaticObjectMethod(g_AndroidBitmap_clazz, |
+ g_AndroidBitmap_createBitmap_method, size.width(), size.height(), |
+ g_BitmapConfig_ARGB8888); |
+ return ScopedJavaLocalRef<jobject>(env, bitmap_object); |
+} |
+ |
+ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) { |
+ DCHECK(skbitmap); |
+ DCHECK_EQ(skbitmap->bytesPerPixel(), 4); |
+ |
+ ScopedJavaLocalRef<jobject> jbitmap = |
+ 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,
|
+ SkAutoLockPixels src_lock(*skbitmap); |
+ AutoLockJavaBitmap dst_lock(jbitmap.obj()); |
+ void* src_pixels = skbitmap->getPixels(); |
+ void* dst_pixels = dst_lock.pixels(); |
+ memcpy(dst_pixels, src_pixels, skbitmap->getSize()); |
+ |
+ return jbitmap; |
+} |
+ |
+} // gfx namespace |
nilesh
2012/08/03 04:52:36
namespace gfx
Jesse Greenwald
2012/08/03 20:10:06
Done.
|