OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/android/jni_helper.h" | 5 #include "content/public/browser/android/bitmap_holder.h" |
6 | 6 |
7 #include <android/bitmap.h> | 7 #include <android/bitmap.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/android/jni_helper.h" | 11 #include "base/android/jni_helper.h" |
12 #include "base/android/jni_string.h" | |
13 #include "base/debug/trace_event.h" | 12 #include "base/debug/trace_event.h" |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "jni/jni_helper_jni.h" | 14 #include "jni/bitmap_holder_jni.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
17 | 16 |
18 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
19 using base::android::CheckException; | 18 using base::android::CheckException; |
20 using base::android::GetStaticMethodID; | |
21 using base::android::GetClass; | |
22 using base::android::GetMethodID; | |
23 using base::android::ScopedJavaLocalRef; | 19 using base::android::ScopedJavaLocalRef; |
24 | 20 |
| 21 namespace content { |
| 22 |
25 AutoLocalFrame::AutoLocalFrame(int capacity) { | 23 AutoLocalFrame::AutoLocalFrame(int capacity) { |
26 AttachCurrentThread()->PushLocalFrame(capacity); | 24 AttachCurrentThread()->PushLocalFrame(capacity); |
27 } | 25 } |
28 | 26 |
29 AutoLocalFrame::~AutoLocalFrame() { | 27 AutoLocalFrame::~AutoLocalFrame() { |
30 AttachCurrentThread()->PopLocalFrame(NULL); | 28 AttachCurrentThread()->PopLocalFrame(NULL); |
31 } | 29 } |
32 | 30 |
33 AutoLockJavaBitmap::AutoLockJavaBitmap(jobject bitmap) : | 31 AutoLockJavaBitmap::AutoLockJavaBitmap(jobject bitmap) : |
34 bitmap_(bitmap), | 32 bitmap_(bitmap), |
(...skipping 25 matching lines...) Expand all Loading... |
60 return info.format; | 58 return info.format; |
61 } | 59 } |
62 | 60 |
63 uint32_t AutoLockJavaBitmap::stride() const { | 61 uint32_t AutoLockJavaBitmap::stride() const { |
64 AndroidBitmapInfo info; | 62 AndroidBitmapInfo info; |
65 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); | 63 int err = AndroidBitmap_getInfo(AttachCurrentThread(), bitmap_, &info); |
66 DCHECK(!err); | 64 DCHECK(!err); |
67 return info.stride; | 65 return info.stride; |
68 } | 66 } |
69 | 67 |
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) { | 68 ScopedJavaLocalRef<jobject> CreateJavaBitmap(const gfx::Size& size, bool keep) { |
103 JNIEnv* env = AttachCurrentThread(); | 69 JNIEnv* env = AttachCurrentThread(); |
104 ScopedJavaLocalRef<jobject> bitmap = | 70 ScopedJavaLocalRef<jobject> bitmap = |
105 Java_JNIHelper_createJavaBitmap(env, size.width(), size.height(), keep); | 71 Java_BitmapHolder_createJavaBitmap(env, size.width(), size.height(), keep)
; |
106 CheckException(env); | 72 CheckException(env); |
107 return bitmap; | 73 return bitmap; |
108 } | 74 } |
109 | 75 |
110 void DeleteJavaBitmap(jobject bitmap) { | 76 void DeleteJavaBitmap(jobject bitmap) { |
111 JNIEnv* env = AttachCurrentThread(); | 77 JNIEnv* env = AttachCurrentThread(); |
112 Java_JNIHelper_deleteJavaBitmap(env, bitmap); | 78 Java_BitmapHolder_deleteJavaBitmap(env, bitmap); |
113 CheckException(env); | 79 CheckException(env); |
114 } | 80 } |
115 | 81 |
116 void PaintJavaBitmapToJavaBitmap(jobject source_bitmap, | 82 void PaintJavaBitmapToJavaBitmap(jobject source_bitmap, |
117 const gfx::Rect& source_rect, | 83 const gfx::Rect& source_rect, |
118 jobject dest_bitmap, | 84 jobject dest_bitmap, |
119 const gfx::Rect& dest_rect) { | 85 const gfx::Rect& dest_rect) { |
120 TRACE_EVENT0("jni", "PaintJavaBitmapToJavaBitmap"); | 86 TRACE_EVENT0("jni", "PaintJavaBitmapToJavaBitmap"); |
121 JNIEnv* env = AttachCurrentThread(); | 87 JNIEnv* env = AttachCurrentThread(); |
122 | 88 |
123 Java_JNIHelper_paintJavaBitmapToJavaBitmap(env, | 89 Java_BitmapHolder_paintJavaBitmapToJavaBitmap(env, |
124 source_bitmap, | 90 source_bitmap, |
125 source_rect.x(), | 91 source_rect.x(), |
126 source_rect.y(), | 92 source_rect.y(), |
127 source_rect.right(), | 93 source_rect.right(), |
128 source_rect.bottom(), | 94 source_rect.bottom(), |
129 dest_bitmap, | 95 dest_bitmap, |
130 dest_rect.x(), | 96 dest_rect.x(), |
131 dest_rect.y(), | 97 dest_rect.y(), |
132 dest_rect.right(), | 98 dest_rect.right(), |
133 dest_rect.bottom()); | 99 dest_rect.bottom()); |
134 CheckException(env); | 100 CheckException(env); |
135 } | 101 } |
136 | 102 |
137 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) { | 103 ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) { |
138 TRACE_EVENT0("jni", "ConvertToJavaBitmap"); | 104 TRACE_EVENT0("jni", "ConvertToJavaBitmap"); |
139 DCHECK(skbitmap); | 105 DCHECK(skbitmap); |
140 DCHECK_EQ(skbitmap->bytesPerPixel(), 4); | 106 DCHECK_EQ(skbitmap->bytesPerPixel(), 4); |
141 | 107 |
142 // Create a temporary java bitmap. | 108 // Create a temporary java bitmap. |
143 ScopedJavaLocalRef<jobject> jbitmap = | 109 ScopedJavaLocalRef<jobject> jbitmap = |
144 CreateJavaBitmap(gfx::Size(skbitmap->width(), skbitmap->height()), false); | 110 CreateJavaBitmap(gfx::Size(skbitmap->width(), skbitmap->height()), false); |
145 | 111 |
146 // Lock and copy the pixels from the skbitmap. | 112 // Lock and copy the pixels from the skbitmap. |
147 SkAutoLockPixels src_lock(*skbitmap); | 113 SkAutoLockPixels src_lock(*skbitmap); |
148 AutoLockJavaBitmap dst_lock(jbitmap.obj()); | 114 AutoLockJavaBitmap dst_lock(jbitmap.obj()); |
149 void* src_pixels = skbitmap->getPixels(); | 115 void* src_pixels = skbitmap->getPixels(); |
150 void* dst_pixels = dst_lock.pixels(); | 116 void* dst_pixels = dst_lock.pixels(); |
151 memcpy(dst_pixels, src_pixels, skbitmap->getSize()); | 117 memcpy(dst_pixels, src_pixels, skbitmap->getSize()); |
152 | 118 |
153 return jbitmap; | 119 return jbitmap; |
154 } | 120 } |
155 | 121 |
156 bool RegisterJniHelper(JNIEnv* env) { | 122 } // namespace content |
| 123 |
| 124 bool RegisterBitmapHolder(JNIEnv* env) { |
157 return RegisterNativesImpl(env); | 125 return RegisterNativesImpl(env); |
158 } | 126 } |
OLD | NEW |