blob: 8e121de935053c5feffd681faa56c3cb7a67e55b [file] [log] [blame]
John Reckcec24ae2013-11-05 13:27:50 -08001/*
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
John Reck23b797a2014-01-03 18:08:34 -080017#define LOG_TAG "ThreadedRenderer"
John Reckcec24ae2013-11-05 13:27:50 -080018
19#include "jni.h"
20#include <nativehelper/JNIHelp.h>
21#include <android_runtime/AndroidRuntime.h>
22
John Reck4f02bf42014-01-03 18:09:17 -080023#include <utils/StrongPointer.h>
24#include <android_runtime/android_view_Surface.h>
25#include <system/window.h>
26
27#include <renderthread/RenderProxy.h>
John Reckcec24ae2013-11-05 13:27:50 -080028#include <renderthread/RenderTask.h>
29#include <renderthread/RenderThread.h>
30
31namespace android {
32
33#ifdef USE_OPENGL_RENDERER
34
John Reck4f02bf42014-01-03 18:09:17 -080035using namespace android::uirenderer;
36using namespace android::uirenderer::renderthread;
John Reckcec24ae2013-11-05 13:27:50 -080037
38static jmethodID gRunnableMethod;
39
John Reck4f02bf42014-01-03 18:09:17 -080040class JavaTask : public RenderTask {
John Reckcec24ae2013-11-05 13:27:50 -080041public:
42 JavaTask(JNIEnv* env, jobject jrunnable) {
43 env->GetJavaVM(&mVm);
44 mRunnable = env->NewGlobalRef(jrunnable);
45 }
46
John Reckcec24ae2013-11-05 13:27:50 -080047 virtual void run() {
48 env()->CallVoidMethod(mRunnable, gRunnableMethod);
John Reck4f02bf42014-01-03 18:09:17 -080049 env()->DeleteGlobalRef(mRunnable);
50 delete this;
John Reckcec24ae2013-11-05 13:27:50 -080051 };
52
53private:
54 JNIEnv* env() {
55 JNIEnv* env;
56 if (mVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
57 return 0;
58 }
59 return env;
60 }
61
62 JavaVM* mVm;
63 jobject mRunnable;
64};
65
66static void android_view_ThreadedRenderer_postToRenderThread(JNIEnv* env, jobject clazz,
67 jobject jrunnable) {
John Reck4f02bf42014-01-03 18:09:17 -080068 RenderTask* task = new JavaTask(env, jrunnable);
69 RenderThread::getInstance().queue(task);
70}
71
72static jlong android_view_ThreadedRenderer_createProxy(JNIEnv* env, jobject clazz,
73 jboolean translucent) {
74 return (jlong) new RenderProxy(translucent);
75}
76
77static void android_view_ThreadedRenderer_deleteProxy(JNIEnv* env, jobject clazz,
78 jlong proxyPtr) {
79 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
80 delete proxy;
81}
82
83static jboolean android_view_ThreadedRenderer_initialize(JNIEnv* env, jobject clazz,
84 jlong proxyPtr, jobject jsurface) {
85 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
86 sp<ANativeWindow> window = android_view_Surface_getNativeWindow(env, jsurface);
87 return proxy->initialize(window.get());
88}
89
90static void android_view_ThreadedRenderer_updateSurface(JNIEnv* env, jobject clazz,
91 jlong proxyPtr, jobject jsurface) {
92 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
93 sp<ANativeWindow> window;
94 if (jsurface) {
95 window = android_view_Surface_getNativeWindow(env, jsurface);
96 }
97 proxy->updateSurface(window.get());
98}
99
100static void android_view_ThreadedRenderer_setup(JNIEnv* env, jobject clazz,
101 jlong proxyPtr, jint width, jint height) {
102 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
103 proxy->setup(width, height);
104}
105
106static void android_view_ThreadedRenderer_drawDisplayList(JNIEnv* env, jobject clazz,
107 jlong proxyPtr, jlong displayListPtr, jint dirtyLeft, jint dirtyTop,
108 jint dirtyRight, jint dirtyBottom) {
109 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
110 DisplayList* displayList = reinterpret_cast<DisplayList*>( displayListPtr);
111 proxy->drawDisplayList(displayList, dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
112}
113
114static void android_view_ThreadedRenderer_destroyCanvas(JNIEnv* env, jobject clazz,
115 jlong proxyPtr) {
116 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
117 proxy->destroyCanvas();
118}
119
120static void android_view_ThreadedRenderer_attachFunctor(JNIEnv* env, jobject clazz,
121 jlong proxyPtr, jlong functorPtr) {
122 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
123 Functor* functor = reinterpret_cast<Functor*>(functorPtr);
124 proxy->attachFunctor(functor);
125}
126
127static void android_view_ThreadedRenderer_detachFunctor(JNIEnv* env, jobject clazz,
128 jlong proxyPtr, jlong functorPtr) {
129 RenderProxy* proxy = reinterpret_cast<RenderProxy*>( proxyPtr);
130 Functor* functor = reinterpret_cast<Functor*>(functorPtr);
131 proxy->detachFunctor(functor);
John Reckcec24ae2013-11-05 13:27:50 -0800132}
133
134#endif
135
136// ----------------------------------------------------------------------------
137// JNI Glue
138// ----------------------------------------------------------------------------
139
140const char* const kClassPathName = "android/view/ThreadedRenderer";
141
142static JNINativeMethod gMethods[] = {
143#ifdef USE_OPENGL_RENDERER
144 { "postToRenderThread", "(Ljava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_postToRenderThread },
John Reck4f02bf42014-01-03 18:09:17 -0800145 { "nCreateProxy", "(Z)J", (void*) android_view_ThreadedRenderer_createProxy },
146 { "nDeleteProxy", "(J)V", (void*) android_view_ThreadedRenderer_deleteProxy },
147 { "nInitialize", "(JLandroid/view/Surface;)Z", (void*) android_view_ThreadedRenderer_initialize },
148 { "nUpdateSurface", "(JLandroid/view/Surface;)V", (void*) android_view_ThreadedRenderer_updateSurface },
149 { "nSetup", "(JII)V", (void*) android_view_ThreadedRenderer_setup },
150 { "nDrawDisplayList", "(JJIIII)V", (void*) android_view_ThreadedRenderer_drawDisplayList},
151 { "nDestroyCanvas", "(J)V", (void*) android_view_ThreadedRenderer_destroyCanvas},
152 { "nAttachFunctor", "(JJ)V", (void*) android_view_ThreadedRenderer_attachFunctor},
153 { "nDetachFunctor", "(JJ)V", (void*) android_view_ThreadedRenderer_detachFunctor},
John Reckcec24ae2013-11-05 13:27:50 -0800154#endif
155};
156
157int register_android_view_ThreadedRenderer(JNIEnv* env) {
158#ifdef USE_OPENGL_RENDERER
159 jclass cls = env->FindClass("java/lang/Runnable");
160 gRunnableMethod = env->GetMethodID(cls, "run", "()V");
161 env->DeleteLocalRef(cls);
162#endif
163 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
164}
165
166}; // namespace android