blob: cdd036e936ab2c0a97d72c58bec79c7d1ca402fb [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
John Recke45b1fd2014-04-15 09:50:16 -070019#include <algorithm>
20
John Reckcec24ae2013-11-05 13:27:50 -080021#include "jni.h"
22#include <nativehelper/JNIHelp.h>
23#include <android_runtime/AndroidRuntime.h>
24
John Reck4f02bf42014-01-03 18:09:17 -080025#include <utils/StrongPointer.h>
26#include <android_runtime/android_view_Surface.h>
27#include <system/window.h>
28
John Reck52244ff2014-05-01 21:27:37 -070029#include <Animator.h>
John Recke45b1fd2014-04-15 09:50:16 -070030#include <RenderNode.h>
John Reck4f02bf42014-01-03 18:09:17 -080031#include <renderthread/RenderProxy.h>
John Reckcec24ae2013-11-05 13:27:50 -080032#include <renderthread/RenderTask.h>
33#include <renderthread/RenderThread.h>
34
35namespace android {
36
37#ifdef USE_OPENGL_RENDERER
38
John Reck4f02bf42014-01-03 18:09:17 -080039using namespace android::uirenderer;
40using namespace android::uirenderer::renderthread;
John Reckcec24ae2013-11-05 13:27:50 -080041
42static jmethodID gRunnableMethod;
43
John Reck4f02bf42014-01-03 18:09:17 -080044class JavaTask : public RenderTask {
John Reckcec24ae2013-11-05 13:27:50 -080045public:
46 JavaTask(JNIEnv* env, jobject jrunnable) {
47 env->GetJavaVM(&mVm);
48 mRunnable = env->NewGlobalRef(jrunnable);
49 }
50
John Reckcec24ae2013-11-05 13:27:50 -080051 virtual void run() {
52 env()->CallVoidMethod(mRunnable, gRunnableMethod);
John Reck4f02bf42014-01-03 18:09:17 -080053 env()->DeleteGlobalRef(mRunnable);
54 delete this;
John Reckcec24ae2013-11-05 13:27:50 -080055 };
56
57private:
58 JNIEnv* env() {
59 JNIEnv* env;
60 if (mVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
61 return 0;
62 }
63 return env;
64 }
65
66 JavaVM* mVm;
67 jobject mRunnable;
68};
69
John Reck52244ff2014-05-01 21:27:37 -070070class OnFinishedEvent {
71public:
72 OnFinishedEvent(BaseAnimator* animator, AnimationListener* listener)
73 : animator(animator), listener(listener) {}
74 sp<BaseAnimator> animator;
75 sp<AnimationListener> listener;
76};
77
John Recke45b1fd2014-04-15 09:50:16 -070078class InvokeAnimationListeners : public MessageHandler {
79public:
John Reck52244ff2014-05-01 21:27:37 -070080 InvokeAnimationListeners(std::vector<OnFinishedEvent>& events) {
81 mOnFinishedEvents.swap(events);
John Recke45b1fd2014-04-15 09:50:16 -070082 }
83
John Reck52244ff2014-05-01 21:27:37 -070084 static void callOnFinished(OnFinishedEvent& event) {
85 event.listener->onAnimationFinished(event.animator.get());
John Recke45b1fd2014-04-15 09:50:16 -070086 }
87
88 virtual void handleMessage(const Message& message) {
John Reck52244ff2014-05-01 21:27:37 -070089 std::for_each(mOnFinishedEvents.begin(), mOnFinishedEvents.end(), callOnFinished);
90 mOnFinishedEvents.clear();
John Recke45b1fd2014-04-15 09:50:16 -070091 }
92
93private:
John Reck52244ff2014-05-01 21:27:37 -070094 std::vector<OnFinishedEvent> mOnFinishedEvents;
John Recke45b1fd2014-04-15 09:50:16 -070095};
96
John Reck52244ff2014-05-01 21:27:37 -070097class RootRenderNode : public RenderNode, public AnimationHook {
John Recke45b1fd2014-04-15 09:50:16 -070098public:
99 RootRenderNode() : RenderNode() {
100 mLooper = Looper::getForThread();
101 LOG_ALWAYS_FATAL_IF(!mLooper.get(),
102 "Must create RootRenderNode on a thread with a looper!");
103 }
104
105 virtual ~RootRenderNode() {}
106
John Reck52244ff2014-05-01 21:27:37 -0700107 virtual void callOnFinished(BaseAnimator* animator, AnimationListener* listener) {
108 OnFinishedEvent event(animator, listener);
109 mOnFinishedEvents.push_back(event);
John Recke45b1fd2014-04-15 09:50:16 -0700110 }
111
112 virtual void prepareTree(TreeInfo& info) {
John Reck52244ff2014-05-01 21:27:37 -0700113 info.animationHook = this;
John Recke45b1fd2014-04-15 09:50:16 -0700114 RenderNode::prepareTree(info);
John Reck52244ff2014-05-01 21:27:37 -0700115 info.animationHook = NULL;
John Recke45b1fd2014-04-15 09:50:16 -0700116
117 // post all the finished stuff
John Reck52244ff2014-05-01 21:27:37 -0700118 if (mOnFinishedEvents.size()) {
John Recke45b1fd2014-04-15 09:50:16 -0700119 sp<InvokeAnimationListeners> message
John Reck52244ff2014-05-01 21:27:37 -0700120 = new InvokeAnimationListeners(mOnFinishedEvents);
John Recke45b1fd2014-04-15 09:50:16 -0700121 mLooper->sendMessage(message, 0);
122 }
123 }
124
125private:
126 sp<Looper> mLooper;
John Reck52244ff2014-05-01 21:27:37 -0700127 std::vector<OnFinishedEvent> mOnFinishedEvents;
John Recke45b1fd2014-04-15 09:50:16 -0700128};
129
John Reckcec24ae2013-11-05 13:27:50 -0800130static void android_view_ThreadedRenderer_postToRenderThread(JNIEnv* env, jobject clazz,
131 jobject jrunnable) {
John Reck4f02bf42014-01-03 18:09:17 -0800132 RenderTask* task = new JavaTask(env, jrunnable);
133 RenderThread::getInstance().queue(task);
134}
135
John Recke45b1fd2014-04-15 09:50:16 -0700136static jlong android_view_ThreadedRenderer_createRootRenderNode(JNIEnv* env, jobject clazz) {
137 RootRenderNode* node = new RootRenderNode();
138 node->incStrong(0);
139 node->setName("RootRenderNode");
140 return reinterpret_cast<jlong>(node);
141}
142
John Reck4f02bf42014-01-03 18:09:17 -0800143static jlong android_view_ThreadedRenderer_createProxy(JNIEnv* env, jobject clazz,
John Recke45b1fd2014-04-15 09:50:16 -0700144 jboolean translucent, jlong rootRenderNodePtr) {
145 RenderNode* rootRenderNode = reinterpret_cast<RenderNode*>(rootRenderNodePtr);
146 return (jlong) new RenderProxy(translucent, rootRenderNode);
John Reck4f02bf42014-01-03 18:09:17 -0800147}
148
149static void android_view_ThreadedRenderer_deleteProxy(JNIEnv* env, jobject clazz,
150 jlong proxyPtr) {
John Reck19b6bcf2014-02-14 20:03:38 -0800151 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800152 delete proxy;
153}
154
John Reck18f16e62014-05-02 16:46:41 -0700155static void android_view_ThreadedRenderer_setFrameInterval(JNIEnv* env, jobject clazz,
156 jlong proxyPtr, jlong frameIntervalNanos) {
157 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
158 proxy->setFrameInterval(frameIntervalNanos);
159}
160
John Recke4280ba2014-05-05 16:39:37 -0700161static jboolean android_view_ThreadedRenderer_loadSystemProperties(JNIEnv* env, jobject clazz,
162 jlong proxyPtr) {
163 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
164 return proxy->loadSystemProperties();
165}
166
John Reck4f02bf42014-01-03 18:09:17 -0800167static jboolean android_view_ThreadedRenderer_initialize(JNIEnv* env, jobject clazz,
168 jlong proxyPtr, jobject jsurface) {
John Reck19b6bcf2014-02-14 20:03:38 -0800169 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800170 sp<ANativeWindow> window = android_view_Surface_getNativeWindow(env, jsurface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700171 return proxy->initialize(window);
John Reck4f02bf42014-01-03 18:09:17 -0800172}
173
174static void android_view_ThreadedRenderer_updateSurface(JNIEnv* env, jobject clazz,
175 jlong proxyPtr, jobject jsurface) {
John Reck19b6bcf2014-02-14 20:03:38 -0800176 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800177 sp<ANativeWindow> window;
178 if (jsurface) {
179 window = android_view_Surface_getNativeWindow(env, jsurface);
180 }
John Reckf7d9c1d2014-04-09 10:01:03 -0700181 proxy->updateSurface(window);
182}
183
184static void android_view_ThreadedRenderer_pauseSurface(JNIEnv* env, jobject clazz,
185 jlong proxyPtr, jobject jsurface) {
186 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
187 sp<ANativeWindow> window;
188 if (jsurface) {
189 window = android_view_Surface_getNativeWindow(env, jsurface);
190 }
191 proxy->pauseSurface(window);
John Reck4f02bf42014-01-03 18:09:17 -0800192}
193
194static void android_view_ThreadedRenderer_setup(JNIEnv* env, jobject clazz,
195 jlong proxyPtr, jint width, jint height) {
John Reck19b6bcf2014-02-14 20:03:38 -0800196 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800197 proxy->setup(width, height);
198}
199
John Reck63a06672014-05-07 13:45:54 -0700200static void android_view_ThreadedRenderer_setOpaque(JNIEnv* env, jobject clazz,
201 jlong proxyPtr, jboolean opaque) {
202 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
203 proxy->setOpaque(opaque);
204}
205
John Reckf9be7792014-05-02 18:21:16 -0700206static int android_view_ThreadedRenderer_syncAndDrawFrame(JNIEnv* env, jobject clazz,
John Reck18f16e62014-05-02 16:46:41 -0700207 jlong proxyPtr, jlong frameTimeNanos, jint dirtyLeft, jint dirtyTop,
John Reck4f02bf42014-01-03 18:09:17 -0800208 jint dirtyRight, jint dirtyBottom) {
John Reck19b6bcf2014-02-14 20:03:38 -0800209 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reckf9be7792014-05-02 18:21:16 -0700210 return proxy->syncAndDrawFrame(frameTimeNanos, dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
John Reck4f02bf42014-01-03 18:09:17 -0800211}
212
John Reckfae904d2014-04-14 11:01:57 -0700213static void android_view_ThreadedRenderer_destroyCanvasAndSurface(JNIEnv* env, jobject clazz,
John Reck4f02bf42014-01-03 18:09:17 -0800214 jlong proxyPtr) {
John Reck19b6bcf2014-02-14 20:03:38 -0800215 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reckfae904d2014-04-14 11:01:57 -0700216 proxy->destroyCanvasAndSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800217}
218
John Reck0d1f6342014-03-28 20:30:27 -0700219static void android_view_ThreadedRenderer_invokeFunctor(JNIEnv* env, jobject clazz,
220 jlong proxyPtr, jlong functorPtr, jboolean waitForCompletion) {
221 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
222 Functor* functor = reinterpret_cast<Functor*>(functorPtr);
223 proxy->invokeFunctor(functor, waitForCompletion);
224}
225
John Reckfc53ef272014-02-11 10:40:25 -0800226static void android_view_ThreadedRenderer_runWithGlContext(JNIEnv* env, jobject clazz,
227 jlong proxyPtr, jobject jrunnable) {
John Reck19b6bcf2014-02-14 20:03:38 -0800228 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reckfc53ef272014-02-11 10:40:25 -0800229 RenderTask* task = new JavaTask(env, jrunnable);
230 proxy->runWithGlContext(task);
231}
232
John Reck19b6bcf2014-02-14 20:03:38 -0800233static jlong android_view_ThreadedRenderer_createDisplayListLayer(JNIEnv* env, jobject clazz,
234 jlong proxyPtr, jint width, jint height) {
235 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
236 DeferredLayerUpdater* layer = proxy->createDisplayListLayer(width, height);
237 return reinterpret_cast<jlong>(layer);
238}
239
240static jlong android_view_ThreadedRenderer_createTextureLayer(JNIEnv* env, jobject clazz,
241 jlong proxyPtr) {
242 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
243 DeferredLayerUpdater* layer = proxy->createTextureLayer();
244 return reinterpret_cast<jlong>(layer);
245}
246
247static jboolean android_view_ThreadedRenderer_copyLayerInto(JNIEnv* env, jobject clazz,
248 jlong proxyPtr, jlong layerPtr, jlong bitmapPtr) {
249 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
250 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr);
251 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapPtr);
252 return proxy->copyLayerInto(layer, bitmap);
253}
254
255static void android_view_ThreadedRenderer_destroyLayer(JNIEnv* env, jobject clazz,
256 jlong proxyPtr, jlong layerPtr) {
257 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
258 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr);
259 proxy->destroyLayer(layer);
260}
261
John Reck28ad7b52014-04-07 16:59:25 -0700262static void android_view_ThreadedRenderer_fence(JNIEnv* env, jobject clazz,
263 jlong proxyPtr) {
264 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
265 proxy->fence();
266}
267
John Reckcec24ae2013-11-05 13:27:50 -0800268#endif
269
270// ----------------------------------------------------------------------------
271// JNI Glue
272// ----------------------------------------------------------------------------
273
274const char* const kClassPathName = "android/view/ThreadedRenderer";
275
276static JNINativeMethod gMethods[] = {
277#ifdef USE_OPENGL_RENDERER
278 { "postToRenderThread", "(Ljava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_postToRenderThread },
John Recke45b1fd2014-04-15 09:50:16 -0700279 { "nCreateRootRenderNode", "()J", (void*) android_view_ThreadedRenderer_createRootRenderNode },
280 { "nCreateProxy", "(ZJ)J", (void*) android_view_ThreadedRenderer_createProxy },
John Reck4f02bf42014-01-03 18:09:17 -0800281 { "nDeleteProxy", "(J)V", (void*) android_view_ThreadedRenderer_deleteProxy },
John Reck18f16e62014-05-02 16:46:41 -0700282 { "nSetFrameInterval", "(JJ)V", (void*) android_view_ThreadedRenderer_setFrameInterval },
John Recke4280ba2014-05-05 16:39:37 -0700283 { "nLoadSystemProperties", "(J)Z", (void*) android_view_ThreadedRenderer_loadSystemProperties },
John Reck4f02bf42014-01-03 18:09:17 -0800284 { "nInitialize", "(JLandroid/view/Surface;)Z", (void*) android_view_ThreadedRenderer_initialize },
285 { "nUpdateSurface", "(JLandroid/view/Surface;)V", (void*) android_view_ThreadedRenderer_updateSurface },
John Reckf7d9c1d2014-04-09 10:01:03 -0700286 { "nPauseSurface", "(JLandroid/view/Surface;)V", (void*) android_view_ThreadedRenderer_pauseSurface },
John Reck4f02bf42014-01-03 18:09:17 -0800287 { "nSetup", "(JII)V", (void*) android_view_ThreadedRenderer_setup },
John Reck63a06672014-05-07 13:45:54 -0700288 { "nSetOpaque", "(JZ)V", (void*) android_view_ThreadedRenderer_setOpaque },
John Reckf9be7792014-05-02 18:21:16 -0700289 { "nSyncAndDrawFrame", "(JJIIII)I", (void*) android_view_ThreadedRenderer_syncAndDrawFrame },
John Reckfae904d2014-04-14 11:01:57 -0700290 { "nDestroyCanvasAndSurface", "(J)V", (void*) android_view_ThreadedRenderer_destroyCanvasAndSurface },
John Reck0d1f6342014-03-28 20:30:27 -0700291 { "nInvokeFunctor", "(JJZ)V", (void*) android_view_ThreadedRenderer_invokeFunctor },
John Reckfc53ef272014-02-11 10:40:25 -0800292 { "nRunWithGlContext", "(JLjava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_runWithGlContext },
John Reck19b6bcf2014-02-14 20:03:38 -0800293 { "nCreateDisplayListLayer", "(JII)J", (void*) android_view_ThreadedRenderer_createDisplayListLayer },
294 { "nCreateTextureLayer", "(J)J", (void*) android_view_ThreadedRenderer_createTextureLayer },
295 { "nCopyLayerInto", "(JJJ)Z", (void*) android_view_ThreadedRenderer_copyLayerInto },
296 { "nDestroyLayer", "(JJ)V", (void*) android_view_ThreadedRenderer_destroyLayer },
John Reck28ad7b52014-04-07 16:59:25 -0700297 { "nFence", "(J)V", (void*) android_view_ThreadedRenderer_fence },
John Reckcec24ae2013-11-05 13:27:50 -0800298#endif
299};
300
301int register_android_view_ThreadedRenderer(JNIEnv* env) {
302#ifdef USE_OPENGL_RENDERER
303 jclass cls = env->FindClass("java/lang/Runnable");
304 gRunnableMethod = env->GetMethodID(cls, "run", "()V");
305 env->DeleteLocalRef(cls);
306#endif
307 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
308}
309
310}; // namespace android