blob: cd0ed8c961f5990311b850e569cca03d7d834954 [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 Reck4f02bf42014-01-03 18:09:17 -0800161static jboolean android_view_ThreadedRenderer_initialize(JNIEnv* env, jobject clazz,
162 jlong proxyPtr, jobject jsurface) {
John Reck19b6bcf2014-02-14 20:03:38 -0800163 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800164 sp<ANativeWindow> window = android_view_Surface_getNativeWindow(env, jsurface);
John Reckf7d9c1d2014-04-09 10:01:03 -0700165 return proxy->initialize(window);
John Reck4f02bf42014-01-03 18:09:17 -0800166}
167
168static void android_view_ThreadedRenderer_updateSurface(JNIEnv* env, jobject clazz,
169 jlong proxyPtr, jobject jsurface) {
John Reck19b6bcf2014-02-14 20:03:38 -0800170 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800171 sp<ANativeWindow> window;
172 if (jsurface) {
173 window = android_view_Surface_getNativeWindow(env, jsurface);
174 }
John Reckf7d9c1d2014-04-09 10:01:03 -0700175 proxy->updateSurface(window);
176}
177
178static void android_view_ThreadedRenderer_pauseSurface(JNIEnv* env, jobject clazz,
179 jlong proxyPtr, jobject jsurface) {
180 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
181 sp<ANativeWindow> window;
182 if (jsurface) {
183 window = android_view_Surface_getNativeWindow(env, jsurface);
184 }
185 proxy->pauseSurface(window);
John Reck4f02bf42014-01-03 18:09:17 -0800186}
187
188static void android_view_ThreadedRenderer_setup(JNIEnv* env, jobject clazz,
189 jlong proxyPtr, jint width, jint height) {
John Reck19b6bcf2014-02-14 20:03:38 -0800190 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reck4f02bf42014-01-03 18:09:17 -0800191 proxy->setup(width, height);
192}
193
John Reckf9be7792014-05-02 18:21:16 -0700194static int android_view_ThreadedRenderer_syncAndDrawFrame(JNIEnv* env, jobject clazz,
John Reck18f16e62014-05-02 16:46:41 -0700195 jlong proxyPtr, jlong frameTimeNanos, jint dirtyLeft, jint dirtyTop,
John Reck4f02bf42014-01-03 18:09:17 -0800196 jint dirtyRight, jint dirtyBottom) {
John Reck19b6bcf2014-02-14 20:03:38 -0800197 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reckf9be7792014-05-02 18:21:16 -0700198 return proxy->syncAndDrawFrame(frameTimeNanos, dirtyLeft, dirtyTop, dirtyRight, dirtyBottom);
John Reck4f02bf42014-01-03 18:09:17 -0800199}
200
John Reckfae904d2014-04-14 11:01:57 -0700201static void android_view_ThreadedRenderer_destroyCanvasAndSurface(JNIEnv* env, jobject clazz,
John Reck4f02bf42014-01-03 18:09:17 -0800202 jlong proxyPtr) {
John Reck19b6bcf2014-02-14 20:03:38 -0800203 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reckfae904d2014-04-14 11:01:57 -0700204 proxy->destroyCanvasAndSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800205}
206
John Reck0d1f6342014-03-28 20:30:27 -0700207static void android_view_ThreadedRenderer_invokeFunctor(JNIEnv* env, jobject clazz,
208 jlong proxyPtr, jlong functorPtr, jboolean waitForCompletion) {
209 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
210 Functor* functor = reinterpret_cast<Functor*>(functorPtr);
211 proxy->invokeFunctor(functor, waitForCompletion);
212}
213
John Reckfc53ef272014-02-11 10:40:25 -0800214static void android_view_ThreadedRenderer_runWithGlContext(JNIEnv* env, jobject clazz,
215 jlong proxyPtr, jobject jrunnable) {
John Reck19b6bcf2014-02-14 20:03:38 -0800216 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
John Reckfc53ef272014-02-11 10:40:25 -0800217 RenderTask* task = new JavaTask(env, jrunnable);
218 proxy->runWithGlContext(task);
219}
220
John Reck19b6bcf2014-02-14 20:03:38 -0800221static jlong android_view_ThreadedRenderer_createDisplayListLayer(JNIEnv* env, jobject clazz,
222 jlong proxyPtr, jint width, jint height) {
223 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
224 DeferredLayerUpdater* layer = proxy->createDisplayListLayer(width, height);
225 return reinterpret_cast<jlong>(layer);
226}
227
228static jlong android_view_ThreadedRenderer_createTextureLayer(JNIEnv* env, jobject clazz,
229 jlong proxyPtr) {
230 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
231 DeferredLayerUpdater* layer = proxy->createTextureLayer();
232 return reinterpret_cast<jlong>(layer);
233}
234
235static jboolean android_view_ThreadedRenderer_copyLayerInto(JNIEnv* env, jobject clazz,
236 jlong proxyPtr, jlong layerPtr, jlong bitmapPtr) {
237 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
238 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr);
239 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(bitmapPtr);
240 return proxy->copyLayerInto(layer, bitmap);
241}
242
243static void android_view_ThreadedRenderer_destroyLayer(JNIEnv* env, jobject clazz,
244 jlong proxyPtr, jlong layerPtr) {
245 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
246 DeferredLayerUpdater* layer = reinterpret_cast<DeferredLayerUpdater*>(layerPtr);
247 proxy->destroyLayer(layer);
248}
249
John Reck28ad7b52014-04-07 16:59:25 -0700250static void android_view_ThreadedRenderer_fence(JNIEnv* env, jobject clazz,
251 jlong proxyPtr) {
252 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
253 proxy->fence();
254}
255
John Reckcec24ae2013-11-05 13:27:50 -0800256#endif
257
258// ----------------------------------------------------------------------------
259// JNI Glue
260// ----------------------------------------------------------------------------
261
262const char* const kClassPathName = "android/view/ThreadedRenderer";
263
264static JNINativeMethod gMethods[] = {
265#ifdef USE_OPENGL_RENDERER
266 { "postToRenderThread", "(Ljava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_postToRenderThread },
John Recke45b1fd2014-04-15 09:50:16 -0700267 { "nCreateRootRenderNode", "()J", (void*) android_view_ThreadedRenderer_createRootRenderNode },
268 { "nCreateProxy", "(ZJ)J", (void*) android_view_ThreadedRenderer_createProxy },
John Reck4f02bf42014-01-03 18:09:17 -0800269 { "nDeleteProxy", "(J)V", (void*) android_view_ThreadedRenderer_deleteProxy },
John Reck18f16e62014-05-02 16:46:41 -0700270 { "nSetFrameInterval", "(JJ)V", (void*) android_view_ThreadedRenderer_setFrameInterval },
John Reck4f02bf42014-01-03 18:09:17 -0800271 { "nInitialize", "(JLandroid/view/Surface;)Z", (void*) android_view_ThreadedRenderer_initialize },
272 { "nUpdateSurface", "(JLandroid/view/Surface;)V", (void*) android_view_ThreadedRenderer_updateSurface },
John Reckf7d9c1d2014-04-09 10:01:03 -0700273 { "nPauseSurface", "(JLandroid/view/Surface;)V", (void*) android_view_ThreadedRenderer_pauseSurface },
John Reck4f02bf42014-01-03 18:09:17 -0800274 { "nSetup", "(JII)V", (void*) android_view_ThreadedRenderer_setup },
John Reckf9be7792014-05-02 18:21:16 -0700275 { "nSyncAndDrawFrame", "(JJIIII)I", (void*) android_view_ThreadedRenderer_syncAndDrawFrame },
John Reckfae904d2014-04-14 11:01:57 -0700276 { "nDestroyCanvasAndSurface", "(J)V", (void*) android_view_ThreadedRenderer_destroyCanvasAndSurface },
John Reck0d1f6342014-03-28 20:30:27 -0700277 { "nInvokeFunctor", "(JJZ)V", (void*) android_view_ThreadedRenderer_invokeFunctor },
John Reckfc53ef272014-02-11 10:40:25 -0800278 { "nRunWithGlContext", "(JLjava/lang/Runnable;)V", (void*) android_view_ThreadedRenderer_runWithGlContext },
John Reck19b6bcf2014-02-14 20:03:38 -0800279 { "nCreateDisplayListLayer", "(JII)J", (void*) android_view_ThreadedRenderer_createDisplayListLayer },
280 { "nCreateTextureLayer", "(J)J", (void*) android_view_ThreadedRenderer_createTextureLayer },
281 { "nCopyLayerInto", "(JJJ)Z", (void*) android_view_ThreadedRenderer_copyLayerInto },
282 { "nDestroyLayer", "(JJ)V", (void*) android_view_ThreadedRenderer_destroyLayer },
John Reck28ad7b52014-04-07 16:59:25 -0700283 { "nFence", "(J)V", (void*) android_view_ThreadedRenderer_fence },
John Reckcec24ae2013-11-05 13:27:50 -0800284#endif
285};
286
287int register_android_view_ThreadedRenderer(JNIEnv* env) {
288#ifdef USE_OPENGL_RENDERER
289 jclass cls = env->FindClass("java/lang/Runnable");
290 gRunnableMethod = env->GetMethodID(cls, "run", "()V");
291 env->DeleteLocalRef(cls);
292#endif
293 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
294}
295
296}; // namespace android