| 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 | |
| 19 | #include <Animator.h> |
| 20 | #include <Interpolator.h> |
| 21 | #include <RenderProperties.h> |
| 22 | |
| 23 | #include "graphics_jni_helpers.h" |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | using namespace uirenderer; |
| 28 | |
| 29 | static struct { |
| 30 | jclass clazz; |
| 31 | |
| 32 | jmethodID callOnFinished; |
| 33 | } gRenderNodeAnimatorClassInfo; |
| 34 | |
| 35 | static JNIEnv* getEnv(JavaVM* vm) { |
| 36 | JNIEnv* env; |
| 37 | if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 38 | return 0; |
| 39 | } |
| 40 | return env; |
| 41 | } |
| 42 | |
| 43 | class AnimationListenerLifecycleChecker : public AnimationListener { |
| 44 | public: |
| 45 | virtual void onAnimationFinished(BaseRenderNodeAnimator* animator) { |
| 46 | LOG_ALWAYS_FATAL("Lifecycle failure, nStart(%p) wasn't called", animator); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | static AnimationListenerLifecycleChecker sLifecycleChecker; |
| 51 | |
| 52 | class AnimationListenerBridge : public AnimationListener { |
| 53 | public: |
| 54 | // This holds a strong reference to a Java WeakReference<T> object. This avoids |
| 55 | // cyclic-references-of-doom. If you think "I know, just use NewWeakGlobalRef!" |
| 56 | // then you end up with basically a PhantomReference, which is totally not |
| 57 | // what we want. |
| 58 | AnimationListenerBridge(JNIEnv* env, jobject finishListener) { |
| 59 | mFinishListener = env->NewGlobalRef(finishListener); |
| 60 | env->GetJavaVM(&mJvm); |
| 61 | } |
| 62 | |
| 63 | virtual ~AnimationListenerBridge() { |
| 64 | if (mFinishListener) { |
| 65 | onAnimationFinished(NULL); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | virtual void onAnimationFinished(BaseRenderNodeAnimator*) { |
| 70 | LOG_ALWAYS_FATAL_IF(!mFinishListener, "Finished listener twice?"); |
| 71 | JNIEnv* env = getEnv(mJvm); |
| 72 | env->CallStaticVoidMethod( |
| 73 | gRenderNodeAnimatorClassInfo.clazz, |
| 74 | gRenderNodeAnimatorClassInfo.callOnFinished, |
| 75 | mFinishListener); |
| 76 | releaseJavaObject(); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | void releaseJavaObject() { |
| 81 | JNIEnv* env = getEnv(mJvm); |
| 82 | env->DeleteGlobalRef(mFinishListener); |
| 83 | mFinishListener = NULL; |
| 84 | } |
| 85 | |
| 86 | JavaVM* mJvm; |
| 87 | jobject mFinishListener; |
| 88 | }; |
| 89 | |
| 90 | static inline RenderPropertyAnimator::RenderProperty toRenderProperty(jint property) { |
| 91 | LOG_ALWAYS_FATAL_IF(property < 0 || property > RenderPropertyAnimator::ALPHA, |
| 92 | "Invalid property %d", property); |
| 93 | return static_cast<RenderPropertyAnimator::RenderProperty>(property); |
| 94 | } |
| 95 | |
| 96 | static inline CanvasPropertyPaintAnimator::PaintField toPaintField(jint field) { |
| 97 | LOG_ALWAYS_FATAL_IF(field < 0 |
| 98 | || field > CanvasPropertyPaintAnimator::ALPHA, |
| 99 | "Invalid paint field %d", field); |
| 100 | return static_cast<CanvasPropertyPaintAnimator::PaintField>(field); |
| 101 | } |
| 102 | |
| 103 | static jlong createAnimator(JNIEnv* env, jobject clazz, |
| 104 | jint propertyRaw, jfloat finalValue) { |
| 105 | RenderPropertyAnimator::RenderProperty property = toRenderProperty(propertyRaw); |
| 106 | BaseRenderNodeAnimator* animator = new RenderPropertyAnimator(property, finalValue); |
| 107 | animator->setListener(&sLifecycleChecker); |
| 108 | return reinterpret_cast<jlong>( animator ); |
| 109 | } |
| 110 | |
| 111 | static jlong createCanvasPropertyFloatAnimator(JNIEnv* env, jobject clazz, |
| 112 | jlong canvasPropertyPtr, jfloat finalValue) { |
| 113 | CanvasPropertyPrimitive* canvasProperty = reinterpret_cast<CanvasPropertyPrimitive*>(canvasPropertyPtr); |
| 114 | BaseRenderNodeAnimator* animator = new CanvasPropertyPrimitiveAnimator(canvasProperty, finalValue); |
| 115 | animator->setListener(&sLifecycleChecker); |
| 116 | return reinterpret_cast<jlong>( animator ); |
| 117 | } |
| 118 | |
| 119 | static jlong createCanvasPropertyPaintAnimator(JNIEnv* env, jobject clazz, |
| 120 | jlong canvasPropertyPtr, jint paintFieldRaw, |
| 121 | jfloat finalValue) { |
| 122 | CanvasPropertyPaint* canvasProperty = reinterpret_cast<CanvasPropertyPaint*>(canvasPropertyPtr); |
| 123 | CanvasPropertyPaintAnimator::PaintField paintField = toPaintField(paintFieldRaw); |
| 124 | BaseRenderNodeAnimator* animator = new CanvasPropertyPaintAnimator( |
| 125 | canvasProperty, paintField, finalValue); |
| 126 | animator->setListener(&sLifecycleChecker); |
| 127 | return reinterpret_cast<jlong>( animator ); |
| 128 | } |
| 129 | |
| 130 | static jlong createRevealAnimator(JNIEnv* env, jobject clazz, |
| 131 | jint centerX, jint centerY, jfloat startRadius, jfloat endRadius) { |
| 132 | BaseRenderNodeAnimator* animator = new RevealAnimator(centerX, centerY, startRadius, endRadius); |
| 133 | animator->setListener(&sLifecycleChecker); |
| 134 | return reinterpret_cast<jlong>( animator ); |
| 135 | } |
| 136 | |
| 137 | static void setStartValue(JNIEnv* env, jobject clazz, jlong animatorPtr, jfloat startValue) { |
| 138 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 139 | animator->setStartValue(startValue); |
| 140 | } |
| 141 | |
| 142 | static void setDuration(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong duration) { |
| 143 | LOG_ALWAYS_FATAL_IF(duration < 0, "Duration cannot be negative"); |
| 144 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 145 | animator->setDuration(duration); |
| 146 | } |
| 147 | |
| 148 | static jlong getDuration(JNIEnv* env, jobject clazz, jlong animatorPtr) { |
| 149 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 150 | return static_cast<jlong>(animator->duration()); |
| 151 | } |
| 152 | |
| 153 | static void setStartDelay(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong startDelay) { |
| 154 | LOG_ALWAYS_FATAL_IF(startDelay < 0, "Start delay cannot be negative"); |
| 155 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 156 | animator->setStartDelay(startDelay); |
| 157 | } |
| 158 | |
| 159 | static void setInterpolator(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong interpolatorPtr) { |
| 160 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 161 | Interpolator* interpolator = reinterpret_cast<Interpolator*>(interpolatorPtr); |
| 162 | animator->setInterpolator(interpolator); |
| 163 | } |
| 164 | |
| 165 | static void setAllowRunningAsync(JNIEnv* env, jobject clazz, jlong animatorPtr, jboolean mayRunAsync) { |
| 166 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 167 | animator->setAllowRunningAsync(mayRunAsync); |
| 168 | } |
| 169 | |
| 170 | static void setListener(JNIEnv* env, jobject clazz, jlong animatorPtr, jobject finishListener) { |
| 171 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 172 | animator->setListener(new AnimationListenerBridge(env, finishListener)); |
| 173 | } |
| 174 | |
| 175 | static void start(JNIEnv* env, jobject clazz, jlong animatorPtr) { |
| 176 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 177 | animator->start(); |
| 178 | } |
| 179 | |
| 180 | static void end(JNIEnv* env, jobject clazz, jlong animatorPtr) { |
| 181 | BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr); |
| 182 | animator->cancel(); |
| 183 | } |
| 184 | |
| 185 | // ---------------------------------------------------------------------------- |
| 186 | // JNI Glue |
| 187 | // ---------------------------------------------------------------------------- |
| 188 | |
| 189 | const char* const kClassPathName = "android/graphics/animation/RenderNodeAnimator"; |
| 190 | |
| 191 | static const JNINativeMethod gMethods[] = { |
| 192 | { "nCreateAnimator", "(IF)J", (void*) createAnimator }, |
| 193 | { "nCreateCanvasPropertyFloatAnimator", "(JF)J", (void*) createCanvasPropertyFloatAnimator }, |
| 194 | { "nCreateCanvasPropertyPaintAnimator", "(JIF)J", (void*) createCanvasPropertyPaintAnimator }, |
| 195 | { "nCreateRevealAnimator", "(IIFF)J", (void*) createRevealAnimator }, |
| 196 | { "nSetStartValue", "(JF)V", (void*) setStartValue }, |
| 197 | { "nSetDuration", "(JJ)V", (void*) setDuration }, |
| 198 | { "nGetDuration", "(J)J", (void*) getDuration }, |
| 199 | { "nSetStartDelay", "(JJ)V", (void*) setStartDelay }, |
| 200 | { "nSetInterpolator", "(JJ)V", (void*) setInterpolator }, |
| 201 | { "nSetAllowRunningAsync", "(JZ)V", (void*) setAllowRunningAsync }, |
| 202 | { "nSetListener", "(JLandroid/graphics/animation/RenderNodeAnimator;)V", (void*) setListener}, |
| 203 | { "nStart", "(J)V", (void*) start}, |
| 204 | { "nEnd", "(J)V", (void*) end }, |
| 205 | }; |
| 206 | |
| 207 | int register_android_graphics_animation_RenderNodeAnimator(JNIEnv* env) { |
| 208 | sLifecycleChecker.incStrong(0); |
| 209 | gRenderNodeAnimatorClassInfo.clazz = FindClassOrDie(env, kClassPathName); |
| 210 | gRenderNodeAnimatorClassInfo.clazz = MakeGlobalRefOrDie(env, |
| 211 | gRenderNodeAnimatorClassInfo.clazz); |
| 212 | |
| 213 | gRenderNodeAnimatorClassInfo.callOnFinished = GetStaticMethodIDOrDie( |
| 214 | env, gRenderNodeAnimatorClassInfo.clazz, "callOnFinished", |
| 215 | "(Landroid/graphics/animation/RenderNodeAnimator;)V"); |
| 216 | |
| 217 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | } // namespace android |