blob: c9eac79ef9b698c31d6af7be80a18f8e470e7ca5 [file] [log] [blame]
John Recke45b1fd2014-04-15 09:50:16 -07001/*
2 * Copyright (C) 2013 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 "OpenGLRenderer"
18
John Recke45b1fd2014-04-15 09:50:16 -070019#include "jni.h"
20#include "GraphicsJNI.h"
21#include <nativehelper/JNIHelp.h>
22#include <android_runtime/AndroidRuntime.h>
23
24#include <Animator.h>
25#include <Interpolator.h>
26#include <RenderProperties.h>
27
Andreas Gampe987f79f2014-11-18 17:29:46 -080028#include "core_jni_helpers.h"
29
John Recke45b1fd2014-04-15 09:50:16 -070030namespace android {
31
32using namespace uirenderer;
33
34static struct {
35 jclass clazz;
36
37 jmethodID callOnFinished;
38} gRenderNodeAnimatorClassInfo;
39
40static JNIEnv* getEnv(JavaVM* vm) {
41 JNIEnv* env;
42 if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
43 return 0;
44 }
45 return env;
46}
47
John Reck72d6e4f2014-11-21 14:10:10 -080048class AnimationListenerLifecycleChecker : public AnimationListener {
49public:
50 virtual void onAnimationFinished(BaseRenderNodeAnimator* animator) {
51 LOG_ALWAYS_FATAL("Lifecycle failure, nStart(%p) wasn't called", animator);
52 }
53};
54
55static AnimationListenerLifecycleChecker sLifecycleChecker;
56
John Reck52244ff2014-05-01 21:27:37 -070057class AnimationListenerBridge : public AnimationListener {
58public:
59 // This holds a strong reference to a Java WeakReference<T> object. This avoids
60 // cyclic-references-of-doom. If you think "I know, just use NewWeakGlobalRef!"
61 // then you end up with basically a PhantomReference, which is totally not
62 // what we want.
John Reck119907c2014-08-14 09:02:01 -070063 AnimationListenerBridge(JNIEnv* env, jobject finishListener) {
64 mFinishListener = env->NewGlobalRef(finishListener);
John Reck52244ff2014-05-01 21:27:37 -070065 env->GetJavaVM(&mJvm);
66 }
John Recke45b1fd2014-04-15 09:50:16 -070067
John Reck52244ff2014-05-01 21:27:37 -070068 virtual ~AnimationListenerBridge() {
John Reck119907c2014-08-14 09:02:01 -070069 if (mFinishListener) {
70 onAnimationFinished(NULL);
71 }
John Reck52244ff2014-05-01 21:27:37 -070072 }
John Recke45b1fd2014-04-15 09:50:16 -070073
John Reckff941dc2014-05-14 16:34:14 -070074 virtual void onAnimationFinished(BaseRenderNodeAnimator*) {
John Reck119907c2014-08-14 09:02:01 -070075 LOG_ALWAYS_FATAL_IF(!mFinishListener, "Finished listener twice?");
John Reck52244ff2014-05-01 21:27:37 -070076 JNIEnv* env = getEnv(mJvm);
77 env->CallStaticVoidMethod(
78 gRenderNodeAnimatorClassInfo.clazz,
79 gRenderNodeAnimatorClassInfo.callOnFinished,
John Reck119907c2014-08-14 09:02:01 -070080 mFinishListener);
81 releaseJavaObject();
John Reck52244ff2014-05-01 21:27:37 -070082 }
John Recke45b1fd2014-04-15 09:50:16 -070083
John Reck52244ff2014-05-01 21:27:37 -070084private:
John Reck119907c2014-08-14 09:02:01 -070085 void releaseJavaObject() {
86 JNIEnv* env = getEnv(mJvm);
87 env->DeleteGlobalRef(mFinishListener);
88 mFinishListener = NULL;
89 }
90
John Reck52244ff2014-05-01 21:27:37 -070091 JavaVM* mJvm;
John Reck119907c2014-08-14 09:02:01 -070092 jobject mFinishListener;
John Reck52244ff2014-05-01 21:27:37 -070093};
94
95static inline RenderPropertyAnimator::RenderProperty toRenderProperty(jint property) {
96 LOG_ALWAYS_FATAL_IF(property < 0 || property > RenderPropertyAnimator::ALPHA,
John Recke45b1fd2014-04-15 09:50:16 -070097 "Invalid property %d", property);
John Reck52244ff2014-05-01 21:27:37 -070098 return static_cast<RenderPropertyAnimator::RenderProperty>(property);
99}
100
John Reck52244ff2014-05-01 21:27:37 -0700101static inline CanvasPropertyPaintAnimator::PaintField toPaintField(jint field) {
102 LOG_ALWAYS_FATAL_IF(field < 0
103 || field > CanvasPropertyPaintAnimator::ALPHA,
104 "Invalid paint field %d", field);
105 return static_cast<CanvasPropertyPaintAnimator::PaintField>(field);
106}
107
John Reck119907c2014-08-14 09:02:01 -0700108static jlong createAnimator(JNIEnv* env, jobject clazz,
John Reckff941dc2014-05-14 16:34:14 -0700109 jint propertyRaw, jfloat finalValue) {
John Reck52244ff2014-05-01 21:27:37 -0700110 RenderPropertyAnimator::RenderProperty property = toRenderProperty(propertyRaw);
John Reckff941dc2014-05-14 16:34:14 -0700111 BaseRenderNodeAnimator* animator = new RenderPropertyAnimator(property, finalValue);
John Reck72d6e4f2014-11-21 14:10:10 -0800112 animator->setListener(&sLifecycleChecker);
John Reck52244ff2014-05-01 21:27:37 -0700113 return reinterpret_cast<jlong>( animator );
114}
115
116static jlong createCanvasPropertyFloatAnimator(JNIEnv* env, jobject clazz,
John Reck119907c2014-08-14 09:02:01 -0700117 jlong canvasPropertyPtr, jfloat finalValue) {
John Reck52244ff2014-05-01 21:27:37 -0700118 CanvasPropertyPrimitive* canvasProperty = reinterpret_cast<CanvasPropertyPrimitive*>(canvasPropertyPtr);
John Reckff941dc2014-05-14 16:34:14 -0700119 BaseRenderNodeAnimator* animator = new CanvasPropertyPrimitiveAnimator(canvasProperty, finalValue);
John Reck72d6e4f2014-11-21 14:10:10 -0800120 animator->setListener(&sLifecycleChecker);
John Reck52244ff2014-05-01 21:27:37 -0700121 return reinterpret_cast<jlong>( animator );
122}
123
124static jlong createCanvasPropertyPaintAnimator(JNIEnv* env, jobject clazz,
John Reck119907c2014-08-14 09:02:01 -0700125 jlong canvasPropertyPtr, jint paintFieldRaw,
John Reckff941dc2014-05-14 16:34:14 -0700126 jfloat finalValue) {
John Reck52244ff2014-05-01 21:27:37 -0700127 CanvasPropertyPaint* canvasProperty = reinterpret_cast<CanvasPropertyPaint*>(canvasPropertyPtr);
128 CanvasPropertyPaintAnimator::PaintField paintField = toPaintField(paintFieldRaw);
John Reckff941dc2014-05-14 16:34:14 -0700129 BaseRenderNodeAnimator* animator = new CanvasPropertyPaintAnimator(
130 canvasProperty, paintField, finalValue);
John Reck72d6e4f2014-11-21 14:10:10 -0800131 animator->setListener(&sLifecycleChecker);
John Recke45b1fd2014-04-15 09:50:16 -0700132 return reinterpret_cast<jlong>( animator );
133}
134
John Reck119907c2014-08-14 09:02:01 -0700135static jlong createRevealAnimator(JNIEnv* env, jobject clazz,
Chris Craikaf4d04c2014-07-29 12:50:14 -0700136 jint centerX, jint centerY, jfloat startRadius, jfloat endRadius) {
137 BaseRenderNodeAnimator* animator = new RevealAnimator(centerX, centerY, startRadius, endRadius);
John Reck72d6e4f2014-11-21 14:10:10 -0800138 animator->setListener(&sLifecycleChecker);
John Reckd3de42c2014-07-15 14:29:33 -0700139 return reinterpret_cast<jlong>( animator );
140}
141
John Reckc6b32642014-06-02 11:00:09 -0700142static void setStartValue(JNIEnv* env, jobject clazz, jlong animatorPtr, jfloat startValue) {
143 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
144 animator->setStartValue(startValue);
145}
146
Alan Viverettead2f8e32014-05-16 13:28:33 -0700147static void setDuration(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong duration) {
John Recke45b1fd2014-04-15 09:50:16 -0700148 LOG_ALWAYS_FATAL_IF(duration < 0, "Duration cannot be negative");
John Reckff941dc2014-05-14 16:34:14 -0700149 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
John Recke45b1fd2014-04-15 09:50:16 -0700150 animator->setDuration(duration);
151}
152
Alan Viverettead2f8e32014-05-16 13:28:33 -0700153static jlong getDuration(JNIEnv* env, jobject clazz, jlong animatorPtr) {
John Reckff941dc2014-05-14 16:34:14 -0700154 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
Alan Viverettead2f8e32014-05-16 13:28:33 -0700155 return static_cast<jlong>(animator->duration());
156}
157
158static void setStartDelay(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong startDelay) {
159 LOG_ALWAYS_FATAL_IF(startDelay < 0, "Start delay cannot be negative");
160 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
161 animator->setStartDelay(startDelay);
162}
163
John Reck315c3292014-05-09 19:21:04 -0700164static void setInterpolator(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong interpolatorPtr) {
John Reckff941dc2014-05-14 16:34:14 -0700165 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
John Reck315c3292014-05-09 19:21:04 -0700166 Interpolator* interpolator = reinterpret_cast<Interpolator*>(interpolatorPtr);
167 animator->setInterpolator(interpolator);
168}
169
John Reckf5945a02014-09-05 15:57:47 -0700170static void setAllowRunningAsync(JNIEnv* env, jobject clazz, jlong animatorPtr, jboolean mayRunAsync) {
171 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
172 animator->setAllowRunningAsync(mayRunAsync);
173}
174
John Reckc47c98b2014-12-09 09:07:35 -0800175static void setListener(JNIEnv* env, jobject clazz, jlong animatorPtr, jobject finishListener) {
John Reck8d8af3c2014-07-01 15:23:45 -0700176 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
John Reck72d6e4f2014-11-21 14:10:10 -0800177 animator->setListener(new AnimationListenerBridge(env, finishListener));
John Reckc47c98b2014-12-09 09:07:35 -0800178}
179
180static void start(JNIEnv* env, jobject clazz, jlong animatorPtr) {
181 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
John Reck8d8af3c2014-07-01 15:23:45 -0700182 animator->start();
183}
184
John Reckd3de42c2014-07-15 14:29:33 -0700185static void end(JNIEnv* env, jobject clazz, jlong animatorPtr) {
John Reck68bfe0a2014-06-24 15:34:58 -0700186 BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
Doris Liuc4bb1852016-02-19 21:39:21 +0000187 animator->cancel();
John Reck68bfe0a2014-06-24 15:34:58 -0700188}
189
John Recke45b1fd2014-04-15 09:50:16 -0700190// ----------------------------------------------------------------------------
191// JNI Glue
192// ----------------------------------------------------------------------------
193
194const char* const kClassPathName = "android/view/RenderNodeAnimator";
195
Daniel Micay76f6a862015-09-19 17:31:01 -0400196static const JNINativeMethod gMethods[] = {
John Reck119907c2014-08-14 09:02:01 -0700197 { "nCreateAnimator", "(IF)J", (void*) createAnimator },
198 { "nCreateCanvasPropertyFloatAnimator", "(JF)J", (void*) createCanvasPropertyFloatAnimator },
199 { "nCreateCanvasPropertyPaintAnimator", "(JIF)J", (void*) createCanvasPropertyPaintAnimator },
200 { "nCreateRevealAnimator", "(IIFF)J", (void*) createRevealAnimator },
John Reckc6b32642014-06-02 11:00:09 -0700201 { "nSetStartValue", "(JF)V", (void*) setStartValue },
Alan Viverettead2f8e32014-05-16 13:28:33 -0700202 { "nSetDuration", "(JJ)V", (void*) setDuration },
203 { "nGetDuration", "(J)J", (void*) getDuration },
204 { "nSetStartDelay", "(JJ)V", (void*) setStartDelay },
John Reck315c3292014-05-09 19:21:04 -0700205 { "nSetInterpolator", "(JJ)V", (void*) setInterpolator },
John Reckf5945a02014-09-05 15:57:47 -0700206 { "nSetAllowRunningAsync", "(JZ)V", (void*) setAllowRunningAsync },
John Reckc47c98b2014-12-09 09:07:35 -0800207 { "nSetListener", "(JLandroid/view/RenderNodeAnimator;)V", (void*) setListener},
208 { "nStart", "(J)V", (void*) start},
John Reckd3de42c2014-07-15 14:29:33 -0700209 { "nEnd", "(J)V", (void*) end },
John Recke45b1fd2014-04-15 09:50:16 -0700210};
211
John Recke45b1fd2014-04-15 09:50:16 -0700212int register_android_view_RenderNodeAnimator(JNIEnv* env) {
John Reck72d6e4f2014-11-21 14:10:10 -0800213 sLifecycleChecker.incStrong(0);
Andreas Gampe987f79f2014-11-18 17:29:46 -0800214 gRenderNodeAnimatorClassInfo.clazz = FindClassOrDie(env, kClassPathName);
215 gRenderNodeAnimatorClassInfo.clazz = MakeGlobalRefOrDie(env,
216 gRenderNodeAnimatorClassInfo.clazz);
John Recke45b1fd2014-04-15 09:50:16 -0700217
Andreas Gampe987f79f2014-11-18 17:29:46 -0800218 gRenderNodeAnimatorClassInfo.callOnFinished = GetStaticMethodIDOrDie(
219 env, gRenderNodeAnimatorClassInfo.clazz, "callOnFinished",
220 "(Landroid/view/RenderNodeAnimator;)V");
John Recke45b1fd2014-04-15 09:50:16 -0700221
Andreas Gampe987f79f2014-11-18 17:29:46 -0800222 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
John Recke45b1fd2014-04-15 09:50:16 -0700223}
224
225
226} // namespace android