John Reck | cec24ae | 2013-11-05 13:27:50 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "GLRenderer" |
| 18 | |
| 19 | #include "jni.h" |
| 20 | #include <nativehelper/JNIHelp.h> |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | |
| 23 | #include <renderthread/RenderTask.h> |
| 24 | #include <renderthread/RenderThread.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | #ifdef USE_OPENGL_RENDERER |
| 29 | |
| 30 | namespace RT = android::uirenderer::renderthread; |
| 31 | |
| 32 | static jmethodID gRunnableMethod; |
| 33 | |
| 34 | class JavaTask : public RT::RenderTask { |
| 35 | public: |
| 36 | JavaTask(JNIEnv* env, jobject jrunnable) { |
| 37 | env->GetJavaVM(&mVm); |
| 38 | mRunnable = env->NewGlobalRef(jrunnable); |
| 39 | } |
| 40 | |
| 41 | virtual ~JavaTask() { |
| 42 | env()->DeleteGlobalRef(mRunnable); |
| 43 | } |
| 44 | |
| 45 | virtual void run() { |
| 46 | env()->CallVoidMethod(mRunnable, gRunnableMethod); |
| 47 | }; |
| 48 | |
| 49 | private: |
| 50 | JNIEnv* env() { |
| 51 | JNIEnv* env; |
| 52 | if (mVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 53 | return 0; |
| 54 | } |
| 55 | return env; |
| 56 | } |
| 57 | |
| 58 | JavaVM* mVm; |
| 59 | jobject mRunnable; |
| 60 | }; |
| 61 | |
| 62 | static void android_view_ThreadedRenderer_postToRenderThread(JNIEnv* env, jobject clazz, |
| 63 | jobject jrunnable) { |
| 64 | RT::RenderTask* task = new JavaTask(env, jrunnable); |
| 65 | RT::RenderThread::getInstance().queue(task); |
| 66 | } |
| 67 | |
| 68 | #endif |
| 69 | |
| 70 | // ---------------------------------------------------------------------------- |
| 71 | // JNI Glue |
| 72 | // ---------------------------------------------------------------------------- |
| 73 | |
| 74 | const char* const kClassPathName = "android/view/ThreadedRenderer"; |
| 75 | |
| 76 | static JNINativeMethod gMethods[] = { |
| 77 | #ifdef USE_OPENGL_RENDERER |
| 78 | { "postToRenderThread", "(Ljava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_postToRenderThread }, |
| 79 | #endif |
| 80 | }; |
| 81 | |
| 82 | int register_android_view_ThreadedRenderer(JNIEnv* env) { |
| 83 | #ifdef USE_OPENGL_RENDERER |
| 84 | jclass cls = env->FindClass("java/lang/Runnable"); |
| 85 | gRunnableMethod = env->GetMethodID(cls, "run", "()V"); |
| 86 | env->DeleteLocalRef(cls); |
| 87 | #endif |
| 88 | return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 89 | } |
| 90 | |
| 91 | }; // namespace android |