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 "content/browser/android/jni_helper.h" |
| 6 |
| 7 #include <android/bitmap.h> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_helper.h" |
| 12 #include "base/android/jni_string.h" |
| 13 #include "base/debug/trace_event.h" |
| 14 #include "base/logging.h" |
| 15 #include "jni/jni_helper_jni.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 |
| 18 using base::android::AttachCurrentThread; |
| 19 using base::android::CheckException; |
| 20 using base::android::GetStaticMethodID; |
| 21 using base::android::GetClass; |
| 22 using base::android::GetMethodID; |
| 23 using base::android::ScopedJavaLocalRef; |
| 24 |
| 25 AutoLocalFrame::AutoLocalFrame(int capacity) { |
| 26 AttachCurrentThread()->PushLocalFrame(capacity); |
| 27 } |
| 28 |
| 29 AutoLocalFrame::~AutoLocalFrame() { |
| 30 AttachCurrentThread()->PopLocalFrame(NULL); |
| 31 } |
| 32 |
| 33 AutoLockJavaBitmap::AutoLockJavaBitmap(jobject bitmap) : |
| 34 bitmap_(bitmap), |
| 35 pixels_(NULL) { |
| 36 int err = AndroidBitmap_lockPixels(AttachCurrentThread(), bitmap_, &pixels_); |
| 37 DCHECK(!err); |
| 38 DCHECK(pixels_); |
| 39 } |
| 40 |
| 41 AutoLockJavaBitmap::~AutoLockJavaBitmap() { |
| 42 // TODO(aelias): Add AndroidBitmap_notifyPixelsChanged() call here |
| 43 // once it's added to the NDK. Using hardware bitmaps will |
| 44 // be broken until this is fixed. |
| 45 int err = AndroidBitmap_unlockPixels(AttachCurrentThread(), bitmap_); |
| 46 DCHECK(!err); |
| 47 } |
| 48 |
| 49 gfx::Size AutoLockJavaBitmap::size() const { |
| 50 AndroidBitmapInfo info; |
| 51 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); |
| 52 DCHECK(!err); |
| 53 return gfx::Size(info.width, info.height); |
| 54 } |
| 55 |
| 56 int AutoLockJavaBitmap::format() const { |
| 57 AndroidBitmapInfo info; |
| 58 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); |
| 59 DCHECK(!err); |
| 60 return info.format; |
| 61 } |
| 62 |
| 63 uint32_t AutoLockJavaBitmap::stride() const { |
| 64 AndroidBitmapInfo info; |
| 65 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); |
| 66 DCHECK(!err); |
| 67 return info.stride; |
| 68 } |
| 69 |
| 70 void PrintJavaStackTrace() { |
| 71 JNIEnv* env = AttachCurrentThread(); |
| 72 |
| 73 ScopedJavaLocalRef<jclass> throwable_clazz = |
| 74 GetClass(env, "java/lang/Throwable"); |
| 75 jmethodID throwable_constructor = |
| 76 GetMethodID(env, throwable_clazz, "<init>", "()V"); |
| 77 |
| 78 ScopedJavaLocalRef<jobject> throwable_object(env, |
| 79 env->NewObject(throwable_clazz.obj(), throwable_constructor)); |
| 80 DCHECK(!throwable_object.is_null()); |
| 81 jmethodID printstacktrace = |
| 82 GetMethodID(env, throwable_clazz, "printStackTrace", "()V"); |
| 83 |
| 84 env->CallVoidMethod(throwable_object.obj(), printstacktrace); |
| 85 CheckException(env); |
| 86 } |
| 87 |
| 88 void ConvertJavaArrayOfStringsToVectorOfStrings( |
| 89 JNIEnv* env, |
| 90 jobjectArray jstrings, |
| 91 std::vector<std::string>* vec) { |
| 92 vec->clear(); |
| 93 jsize length = env->GetArrayLength(jstrings); |
| 94 for (jsize i = 0; i < length; ++i) { |
| 95 jstring item = static_cast<jstring>( |
| 96 env->GetObjectArrayElement(jstrings, i)); |
| 97 vec->push_back(base::android::ConvertJavaStringToUTF8(env, item)); |
| 98 env->DeleteLocalRef(item); |
| 99 } |
| 100 } |
| 101 |
| 102 ScopedJavaLocalRef<jobject> CreateJavaBitmap(const gfx::Size& size, bool keep) { |
| 103 JNIEnv* env = AttachCurrentThread(); |
| 104 ScopedJavaLocalRef<jobject> bitmap = |
| 105 Java_JNIHelper_createJavaBitmap(env, size.width(), size.height(), keep); |
| 106 CheckException(env); |
| 107 return bitmap; |
| 108 } |
| 109 |
| 110 void DeleteJavaBitmap(jobject bitmap) { |
| 111 JNIEnv* env = AttachCurrentThread(); |
| 112 Java_JNIHelper_deleteJavaBitmap(env, bitmap); |
| 113 CheckException(env); |
| 114 } |
| 115 |
| 116 void PaintJavaBitmapToJavaBitmap(jobject source_bitmap, |
| 117 const gfx::Rect& source_rect, |
| 118 jobject dest_bitmap, |
| 119 const gfx::Rect& dest_rect) { |
| 120 TRACE_EVENT0("jni", "PaintJavaBitmapToJavaBitmap"); |
| 121 JNIEnv* env = AttachCurrentThread(); |
| 122 |
| 123 Java_JNIHelper_paintJavaBitmapToJavaBitmap(env, |
| 124 source_bitmap, |
| 125 source_rect.x(), |
| 126 source_rect.y(), |
| 127 source_rect.right(), |
| 128 source_rect.bottom(), |
| 129 dest_bitmap, |
| 130 dest_rect.x(), |
| 131 dest_rect.y(), |
| 132 dest_rect.right(), |
| 133 dest_rect.bottom()); |
| 134 CheckException(env); |
| 135 } |
| 136 |
| 137 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) { |
| 138 TRACE_EVENT0("jni", "ConvertToJavaBitmap"); |
| 139 DCHECK(skbitmap); |
| 140 DCHECK_EQ(skbitmap->bytesPerPixel(), 4); |
| 141 |
| 142 // Create a temporary java bitmap. |
| 143 ScopedJavaLocalRef<jobject> jbitmap = |
| 144 CreateJavaBitmap(gfx::Size(skbitmap->width(), skbitmap->height()), false); |
| 145 |
| 146 // Lock and copy the pixels from the skbitmap. |
| 147 SkAutoLockPixels src_lock(*skbitmap); |
| 148 AutoLockJavaBitmap dst_lock(jbitmap.obj()); |
| 149 void* src_pixels = skbitmap->getPixels(); |
| 150 void* dst_pixels = dst_lock.pixels(); |
| 151 memcpy(dst_pixels, src_pixels, skbitmap->getSize()); |
| 152 |
| 153 return jbitmap; |
| 154 } |
| 155 |
| 156 bool RegisterJniHelper(JNIEnv* env) { |
| 157 return RegisterNativesImpl(env); |
| 158 } |
OLD | NEW |