blob: 98bec1bbd9bcbd6914e6308c6f2d4f28737d06aa [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) {
John Reck19b6bcf2014-02-14 20:03:38 -080079 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -080080 delete proxy;
81}
82
83static jboolean android_view_ThreadedRenderer_initialize(JNIEnv* env, jobject clazz,
84 jlong proxyPtr, jobject jsurface) {
John Reck19b6bcf2014-02-14 20:03:38 -080085 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -080086 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) {
John Reck19b6bcf2014-02-14 20:03:38 -080092 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -080093 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) {
John Reck19b6bcf2014-02-14 20:03:38 -0800102 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800103 proxy->setup(width, height);
104}
105
John Reckbe34f2f2014-03-10 08:58:44 -0700106static void android_view_ThreadedRenderer_setDisplayListData(JNIEnv* env, jobject clazz,
John Reck44fd8d22014-02-26 11:00:11 -0800107 jlong proxyPtr, jlong displayListPtr, jlong newDataPtr) {
108 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Recke18264b2014-03-12 13:56:30 -0700109 RenderNode* displayList = reinterpret_cast<RenderNode*>(displayListPtr);
John Reck44fd8d22014-02-26 11:00:11 -0800110 DisplayListData* newData = reinterpret_cast<DisplayListData*>(newDataPtr);
John Reckbe34f2f2014-03-10 08:58:44 -0700111 proxy->setDisplayListData(displayList, newData);
John Reck44fd8d22014-02-26 11:00:11 -0800112}
113
John Reck4f02bf42014-01-03 18:09:17 -0800114static void android_view_ThreadedRenderer_drawDisplayList(JNIEnv* env, jobject clazz,
115 jlong proxyPtr, jlong displayListPtr, jint dirtyLeft, jint dirtyTop,
116 jint dirtyRight, jint dirtyBottom) {
John Reck19b6bcf2014-02-14 20:03:38 -0800117 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Recke18264b2014-03-12 13:56:30 -0700118 RenderNode* displayList = reinterpret_cast<RenderNode*>(displayListPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800119 proxy->drawDisplayList(displayList, dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
120}
121
122static void android_view_ThreadedRenderer_destroyCanvas(JNIEnv* env, jobject clazz,
123 jlong proxyPtr) {
John Reck19b6bcf2014-02-14 20:03:38 -0800124 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800125 proxy->destroyCanvas();
126}
127
128static void android_view_ThreadedRenderer_attachFunctor(JNIEnv* env, jobject clazz,
129 jlong proxyPtr, jlong functorPtr) {
John Reck19b6bcf2014-02-14 20:03:38 -0800130 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800131 Functor* functor = reinterpret_cast<Functor*>(functorPtr);
132 proxy->attachFunctor(functor);
133}
134
135static void android_view_ThreadedRenderer_detachFunctor(JNIEnv* env, jobject clazz,
136 jlong proxyPtr, jlong functorPtr) {
John Reck19b6bcf2014-02-14 20:03:38 -0800137 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800138 Functor* functor = reinterpret_cast<Functor*>(functorPtr);
139 proxy->detachFunctor(functor);
John Reckcec24ae2013-11-05 13:27:50 -0800140}
141
John Reck0d1f6342014-03-28 20:30:27 -0700142static void android_view_ThreadedRenderer_invokeFunctor(JNIEnv* env, jobject clazz,
143 jlong proxyPtr, jlong functorPtr, jboolean waitForCompletion) {
144 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
145 Functor* functor = reinterpret_cast<Functor*>(functorPtr);
146 proxy->invokeFunctor(functor, waitForCompletion);
147}
148
John Reckfc53ef272014-02-11 10:40:25 -0800149static void android_view_ThreadedRenderer_runWithGlContext(JNIEnv* env, jobject clazz,
150 jlong proxyPtr, jobject jrunnable) {
John Reck19b6bcf2014-02-14 20:03:38 -0800151 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reckfc53ef272014-02-11 10:40:25 -0800152 RenderTask* task = new JavaTask(env, jrunnable);
153 proxy->runWithGlContext(task);
154}
155
John Reck19b6bcf2014-02-14 20:03:38 -0800156static jlong android_view_ThreadedRenderer_createDisplayListLayer(JNIEnv* env, jobject clazz,
157 jlong proxyPtr, jint width, jint height) {
158 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
159 DeferredLayerUpdater* layer = proxy->createDisplayListLayer(width, height);
160 return reinterpret_cast<jlong>(layer);
161}
162
163static jlong android_view_ThreadedRenderer_createTextureLayer(JNIEnv* env, jobject clazz,
164 jlong proxyPtr) {
165 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
166 DeferredLayerUpdater* layer = proxy->createTextureLayer();
167 return reinterpret_cast<jlong>(layer);
168}
169
170static jboolean android_view_ThreadedRenderer_copyLayerInto(JNIEnv* env, jobject clazz,
171 jlong proxyPtr, jlong layerPtr, jlong bitmapPtr) {
172 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
173 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr);
174 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapPtr);
175 return proxy->copyLayerInto(layer, bitmap);
176}
177
178static void android_view_ThreadedRenderer_destroyLayer(JNIEnv* env, jobject clazz,
179 jlong proxyPtr, jlong layerPtr) {
180 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
181 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr);
182 proxy->destroyLayer(layer);
183}
184
John Reckcec24ae2013-11-05 13:27:50 -0800185#endif
186
187// ----------------------------------------------------------------------------
188// JNI Glue
189// ----------------------------------------------------------------------------
190
191const char* const kClassPathName = "android/view/ThreadedRenderer";
192
193static JNINativeMethod gMethods[] = {
194#ifdef USE_OPENGL_RENDERER
195 { "postToRenderThread", "(Ljava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_postToRenderThread },
John Reck4f02bf42014-01-03 18:09:17 -0800196 { "nCreateProxy", "(Z)J", (void*) android_view_ThreadedRenderer_createProxy },
197 { "nDeleteProxy", "(J)V", (void*) android_view_ThreadedRenderer_deleteProxy },
198 { "nInitialize", "(JLandroid/view/Surface;)Z", (void*) android_view_ThreadedRenderer_initialize },
199 { "nUpdateSurface", "(JLandroid/view/Surface;)V", (void*) android_view_ThreadedRenderer_updateSurface },
200 { "nSetup", "(JII)V", (void*) android_view_ThreadedRenderer_setup },
John Reckbe34f2f2014-03-10 08:58:44 -0700201 { "nSetDisplayListData", "(JJJ)V", (void*) android_view_ThreadedRenderer_setDisplayListData },
John Reck44fd8d22014-02-26 11:00:11 -0800202 { "nDrawDisplayList", "(JJIIII)V", (void*) android_view_ThreadedRenderer_drawDisplayList },
203 { "nDestroyCanvas", "(J)V", (void*) android_view_ThreadedRenderer_destroyCanvas },
204 { "nAttachFunctor", "(JJ)V", (void*) android_view_ThreadedRenderer_attachFunctor },
205 { "nDetachFunctor", "(JJ)V", (void*) android_view_ThreadedRenderer_detachFunctor },
John Reck0d1f6342014-03-28 20:30:27 -0700206 { "nInvokeFunctor", "(JJZ)V", (void*) android_view_ThreadedRenderer_invokeFunctor },
John Reckfc53ef272014-02-11 10:40:25 -0800207 { "nRunWithGlContext", "(JLjava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_runWithGlContext },
John Reck19b6bcf2014-02-14 20:03:38 -0800208 { "nCreateDisplayListLayer", "(JII)J", (void*) android_view_ThreadedRenderer_createDisplayListLayer },
209 { "nCreateTextureLayer", "(J)J", (void*) android_view_ThreadedRenderer_createTextureLayer },
210 { "nCopyLayerInto", "(JJJ)Z", (void*) android_view_ThreadedRenderer_copyLayerInto },
211 { "nDestroyLayer", "(JJ)V", (void*) android_view_ThreadedRenderer_destroyLayer },
John Reckcec24ae2013-11-05 13:27:50 -0800212#endif
213};
214
215int register_android_view_ThreadedRenderer(JNIEnv* env) {
216#ifdef USE_OPENGL_RENDERER
217 jclass cls = env->FindClass("java/lang/Runnable");
218 gRunnableMethod = env->GetMethodID(cls, "run", "()V");
219 env->DeleteLocalRef(cls);
220#endif
221 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
222}
223
224}; // namespace android