John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 19 | #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 Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 28 | #include "core_jni_helpers.h" |
| 29 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
| 32 | using namespace uirenderer; |
| 33 | |
| 34 | static struct { |
| 35 | jclass clazz; |
| 36 | |
| 37 | jmethodID callOnFinished; |
| 38 | } gRenderNodeAnimatorClassInfo; |
| 39 | |
| 40 | static 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 Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 48 | class AnimationListenerLifecycleChecker : public AnimationListener { |
| 49 | public: |
| 50 | virtual void onAnimationFinished(BaseRenderNodeAnimator* animator) { |
| 51 | LOG_ALWAYS_FATAL("Lifecycle failure, nStart(%p) wasn't called", animator); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | static AnimationListenerLifecycleChecker sLifecycleChecker; |
| 56 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 57 | class AnimationListenerBridge : public AnimationListener { |
| 58 | public: |
| 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 Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 63 | AnimationListenerBridge(JNIEnv* env, jobject finishListener) { |
| 64 | mFinishListener = env->NewGlobalRef(finishListener); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 65 | env->GetJavaVM(&mJvm); |
| 66 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 67 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 68 | virtual ~AnimationListenerBridge() { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 69 | if (mFinishListener) { |
| 70 | onAnimationFinished(NULL); |
| 71 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 72 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 73 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 74 | virtual void onAnimationFinished(BaseRenderNodeAnimator*) { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 75 | LOG_ALWAYS_FATAL_IF(!mFinishListener, "Finished listener twice?"); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 76 | JNIEnv* env = getEnv(mJvm); |
| 77 | env->CallStaticVoidMethod( |
| 78 | gRenderNodeAnimatorClassInfo.clazz, |
| 79 | gRenderNodeAnimatorClassInfo.callOnFinished, |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 80 | mFinishListener); |
| 81 | releaseJavaObject(); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 82 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 83 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 84 | private: |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 85 | void releaseJavaObject() { |
| 86 | JNIEnv* env = getEnv(mJvm); |
| 87 | env->DeleteGlobalRef(mFinishListener); |
| 88 | mFinishListener = NULL; |
| 89 | } |
| 90 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 91 | JavaVM* mJvm; |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 92 | jobject mFinishListener; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | static inline RenderPropertyAnimator::RenderProperty toRenderProperty(jint property) { |
| 96 | LOG_ALWAYS_FATAL_IF(property < 0 || property > RenderPropertyAnimator::ALPHA, |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 97 | "Invalid property %d", property); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 98 | return static_cast<RenderPropertyAnimator::RenderProperty>(property); |
| 99 | } |
| 100 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 101 | static 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 Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 108 | static jlong createAnimator(JNIEnv* env, jobject clazz, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 109 | jint propertyRaw, jfloat finalValue) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 110 | RenderPropertyAnimator::RenderProperty property = toRenderProperty(propertyRaw); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 111 | BaseRenderNodeAnimator* animator = new RenderPropertyAnimator(property, finalValue); |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 112 | animator->setListener(&sLifecycleChecker); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 113 | return reinterpret_cast<jlong>( animator ); |
| 114 | } |
| 115 | |
| 116 | static jlong createCanvasPropertyFloatAnimator(JNIEnv* env, jobject clazz, |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 117 | jlong canvasPropertyPtr, jfloat finalValue) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 118 | CanvasPropertyPrimitive* canvasProperty = reinterpret_cast<CanvasPropertyPrimitive*>(canvasPropertyPtr); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 119 | BaseRenderNodeAnimator* animator = new CanvasPropertyPrimitiveAnimator(canvasProperty, finalValue); |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 120 | animator->setListener(&sLifecycleChecker); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 121 | return reinterpret_cast<jlong>( animator ); |
| 122 | } |
| 123 | |
| 124 | static jlong createCanvasPropertyPaintAnimator(JNIEnv* env, jobject clazz, |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 125 | jlong canvasPropertyPtr, jint paintFieldRaw, |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 126 | jfloat finalValue) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 127 | CanvasPropertyPaint* canvasProperty = reinterpret_cast<CanvasPropertyPaint*>(canvasPropertyPtr); |
| 128 | CanvasPropertyPaintAnimator::PaintField paintField = toPaintField(paintFieldRaw); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 129 | BaseRenderNodeAnimator* animator = new CanvasPropertyPaintAnimator( |
| 130 | canvasProperty, paintField, finalValue); |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 131 | animator->setListener(&sLifecycleChecker); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 132 | return reinterpret_cast<jlong>( animator ); |
| 133 | } |
| 134 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 135 | static jlong createRevealAnimator(JNIEnv* env, jobject clazz, |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 136 | jint centerX, jint centerY, jfloat startRadius, jfloat endRadius) { |
| 137 | BaseRenderNodeAnimator* animator = new RevealAnimator(centerX, centerY, startRadius, endRadius); |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 138 | animator->setListener(&sLifecycleChecker); |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 139 | return reinterpret_cast<jlong>( animator ); |
| 140 | } |
| 141 | |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 142 | static void setStartValue(JNIEnv* env, jobject clazz, jlong animatorPtr, jfloat startValue) { |
| 143 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 144 | animator->setStartValue(startValue); |
| 145 | } |
| 146 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 147 | static void setDuration(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong duration) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 148 | LOG_ALWAYS_FATAL_IF(duration < 0, "Duration cannot be negative"); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 149 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 150 | animator->setDuration(duration); |
| 151 | } |
| 152 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 153 | static jlong getDuration(JNIEnv* env, jobject clazz, jlong animatorPtr) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 154 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 155 | return static_cast<jlong>(animator->duration()); |
| 156 | } |
| 157 | |
| 158 | static 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 Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 164 | static void setInterpolator(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong interpolatorPtr) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 165 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 166 | Interpolator* interpolator = reinterpret_cast<Interpolator*>(interpolatorPtr); |
| 167 | animator->setInterpolator(interpolator); |
| 168 | } |
| 169 | |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 170 | static void setAllowRunningAsync(JNIEnv* env, jobject clazz, jlong animatorPtr, jboolean mayRunAsync) { |
| 171 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 172 | animator->setAllowRunningAsync(mayRunAsync); |
| 173 | } |
| 174 | |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 175 | static void setListener(JNIEnv* env, jobject clazz, jlong animatorPtr, jobject finishListener) { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 176 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 177 | animator->setListener(new AnimationListenerBridge(env, finishListener)); |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void start(JNIEnv* env, jobject clazz, jlong animatorPtr) { |
| 181 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 182 | animator->start(); |
| 183 | } |
| 184 | |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 185 | static void end(JNIEnv* env, jobject clazz, jlong animatorPtr) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 186 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 187 | animator->cancel(); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 188 | } |
| 189 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 190 | // ---------------------------------------------------------------------------- |
| 191 | // JNI Glue |
| 192 | // ---------------------------------------------------------------------------- |
| 193 | |
| 194 | const char* const kClassPathName = "android/view/RenderNodeAnimator"; |
| 195 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 196 | static const JNINativeMethod gMethods[] = { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 197 | { "nCreateAnimator", "(IF)J", (void*) createAnimator }, |
| 198 | { "nCreateCanvasPropertyFloatAnimator", "(JF)J", (void*) createCanvasPropertyFloatAnimator }, |
| 199 | { "nCreateCanvasPropertyPaintAnimator", "(JIF)J", (void*) createCanvasPropertyPaintAnimator }, |
| 200 | { "nCreateRevealAnimator", "(IIFF)J", (void*) createRevealAnimator }, |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 201 | { "nSetStartValue", "(JF)V", (void*) setStartValue }, |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 202 | { "nSetDuration", "(JJ)V", (void*) setDuration }, |
| 203 | { "nGetDuration", "(J)J", (void*) getDuration }, |
| 204 | { "nSetStartDelay", "(JJ)V", (void*) setStartDelay }, |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 205 | { "nSetInterpolator", "(JJ)V", (void*) setInterpolator }, |
John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 206 | { "nSetAllowRunningAsync", "(JZ)V", (void*) setAllowRunningAsync }, |
John Reck | c47c98b | 2014-12-09 09:07:35 -0800 | [diff] [blame] | 207 | { "nSetListener", "(JLandroid/view/RenderNodeAnimator;)V", (void*) setListener}, |
| 208 | { "nStart", "(J)V", (void*) start}, |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 209 | { "nEnd", "(J)V", (void*) end }, |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 212 | int register_android_view_RenderNodeAnimator(JNIEnv* env) { |
John Reck | 72d6e4f | 2014-11-21 14:10:10 -0800 | [diff] [blame] | 213 | sLifecycleChecker.incStrong(0); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 214 | gRenderNodeAnimatorClassInfo.clazz = FindClassOrDie(env, kClassPathName); |
| 215 | gRenderNodeAnimatorClassInfo.clazz = MakeGlobalRefOrDie(env, |
| 216 | gRenderNodeAnimatorClassInfo.clazz); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 217 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 218 | gRenderNodeAnimatorClassInfo.callOnFinished = GetStaticMethodIDOrDie( |
| 219 | env, gRenderNodeAnimatorClassInfo.clazz, "callOnFinished", |
| 220 | "(Landroid/view/RenderNodeAnimator;)V"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 221 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 222 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | |
| 226 | } // namespace android |