Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Unified Diff: content/browser/android/jni_helper.cc

Issue 10377059: Android content shell bringup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix deps so builds on clean machine Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/android/jni_helper.h ('k') | content/browser/android/library_loader_hooks.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/android/jni_helper.cc
diff --git a/content/browser/android/jni_helper.cc b/content/browser/android/jni_helper.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bd7bce6686ec2f1d184707596f1262b51c379e1b
--- /dev/null
+++ b/content/browser/android/jni_helper.cc
@@ -0,0 +1,158 @@
+// 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 "content/browser/android/jni_helper.h"
+
+#include <android/bitmap.h>
+#include <vector>
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_helper.h"
+#include "base/android/jni_string.h"
+#include "base/debug/trace_event.h"
+#include "base/logging.h"
+#include "jni/jni_helper_jni.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+
+using base::android::AttachCurrentThread;
+using base::android::CheckException;
+using base::android::GetStaticMethodID;
+using base::android::GetClass;
+using base::android::GetMethodID;
+using base::android::ScopedJavaLocalRef;
+
+AutoLocalFrame::AutoLocalFrame(int capacity) {
+ AttachCurrentThread()->PushLocalFrame(capacity);
+}
+
+AutoLocalFrame::~AutoLocalFrame() {
+ AttachCurrentThread()->PopLocalFrame(NULL);
+}
+
+AutoLockJavaBitmap::AutoLockJavaBitmap(jobject bitmap) :
+ bitmap_(bitmap),
+ pixels_(NULL) {
+ int err = AndroidBitmap_lockPixels(AttachCurrentThread(), bitmap_, &pixels_);
+ DCHECK(!err);
+ DCHECK(pixels_);
+}
+
+AutoLockJavaBitmap::~AutoLockJavaBitmap() {
+ // TODO(aelias): Add AndroidBitmap_notifyPixelsChanged() call here
+ // 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);
+ DCHECK(!err);
+ return info.stride;
+}
+
+void PrintJavaStackTrace() {
+ JNIEnv* env = AttachCurrentThread();
+
+ ScopedJavaLocalRef<jclass> throwable_clazz =
+ GetClass(env, "java/lang/Throwable");
+ jmethodID throwable_constructor =
+ GetMethodID(env, throwable_clazz, "<init>", "()V");
+
+ ScopedJavaLocalRef<jobject> throwable_object(env,
+ env->NewObject(throwable_clazz.obj(), throwable_constructor));
+ DCHECK(!throwable_object.is_null());
+ jmethodID printstacktrace =
+ GetMethodID(env, throwable_clazz, "printStackTrace", "()V");
+
+ env->CallVoidMethod(throwable_object.obj(), printstacktrace);
+ CheckException(env);
+}
+
+void ConvertJavaArrayOfStringsToVectorOfStrings(
+ JNIEnv* env,
+ jobjectArray jstrings,
+ std::vector<std::string>* vec) {
+ vec->clear();
+ jsize length = env->GetArrayLength(jstrings);
+ for (jsize i = 0; i < length; ++i) {
+ jstring item = static_cast<jstring>(
+ env->GetObjectArrayElement(jstrings, i));
+ vec->push_back(base::android::ConvertJavaStringToUTF8(env, item));
+ env->DeleteLocalRef(item);
+ }
+}
+
+ScopedJavaLocalRef<jobject> CreateJavaBitmap(const gfx::Size& size, bool keep) {
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobject> bitmap =
+ Java_JNIHelper_createJavaBitmap(env, size.width(), size.height(), keep);
+ CheckException(env);
+ return bitmap;
+}
+
+void DeleteJavaBitmap(jobject bitmap) {
+ JNIEnv* env = AttachCurrentThread();
+ Java_JNIHelper_deleteJavaBitmap(env, bitmap);
+ CheckException(env);
+}
+
+void PaintJavaBitmapToJavaBitmap(jobject source_bitmap,
+ const gfx::Rect& source_rect,
+ jobject dest_bitmap,
+ const gfx::Rect& dest_rect) {
+ TRACE_EVENT0("jni", "PaintJavaBitmapToJavaBitmap");
+ JNIEnv* env = AttachCurrentThread();
+
+ Java_JNIHelper_paintJavaBitmapToJavaBitmap(env,
+ source_bitmap,
+ source_rect.x(),
+ source_rect.y(),
+ source_rect.right(),
+ source_rect.bottom(),
+ dest_bitmap,
+ dest_rect.x(),
+ dest_rect.y(),
+ dest_rect.right(),
+ dest_rect.bottom());
+ CheckException(env);
+}
+
+ScopedJavaLocalRef<jobject> ConvertToJavaBitmap(const SkBitmap* skbitmap) {
+ TRACE_EVENT0("jni", "ConvertToJavaBitmap");
+ DCHECK(skbitmap);
+ DCHECK_EQ(skbitmap->bytesPerPixel(), 4);
+
+ // Create a temporary java bitmap.
+ ScopedJavaLocalRef<jobject> jbitmap =
+ CreateJavaBitmap(gfx::Size(skbitmap->width(), skbitmap->height()), false);
+
+ // Lock and copy the pixels from the skbitmap.
+ 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;
+}
+
+bool RegisterJniHelper(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
« no previous file with comments | « content/browser/android/jni_helper.h ('k') | content/browser/android/library_loader_hooks.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698