android_view_RenderNodeAnimator.cpp revision ff941dcd815021bb20d6504eb486acb1e50592c3
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 "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
28namespace android {
29
30using namespace uirenderer;
31
32static struct {
33    jclass clazz;
34
35    jmethodID callOnFinished;
36} gRenderNodeAnimatorClassInfo;
37
38#ifdef USE_OPENGL_RENDERER
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
48class AnimationListenerBridge : public AnimationListener {
49public:
50    // This holds a strong reference to a Java WeakReference<T> object. This avoids
51    // cyclic-references-of-doom. If you think "I know, just use NewWeakGlobalRef!"
52    // then you end up with basically a PhantomReference, which is totally not
53    // what we want.
54    AnimationListenerBridge(JNIEnv* env, jobject weakThis) {
55        mWeakThis = env->NewGlobalRef(weakThis);
56        env->GetJavaVM(&mJvm);
57    }
58
59    virtual ~AnimationListenerBridge() {
60        JNIEnv* env = getEnv(mJvm);
61        env->DeleteGlobalRef(mWeakThis);
62        mWeakThis = NULL;
63    }
64
65    virtual void onAnimationFinished(BaseRenderNodeAnimator*) {
66        JNIEnv* env = getEnv(mJvm);
67        env->CallStaticVoidMethod(
68                gRenderNodeAnimatorClassInfo.clazz,
69                gRenderNodeAnimatorClassInfo.callOnFinished,
70                mWeakThis);
71    }
72
73private:
74    JavaVM* mJvm;
75    jobject mWeakThis;
76};
77
78static inline RenderPropertyAnimator::RenderProperty toRenderProperty(jint property) {
79    LOG_ALWAYS_FATAL_IF(property < 0 || property > RenderPropertyAnimator::ALPHA,
80            "Invalid property %d", property);
81    return static_cast<RenderPropertyAnimator::RenderProperty>(property);
82}
83
84static inline CanvasPropertyPaintAnimator::PaintField toPaintField(jint field) {
85    LOG_ALWAYS_FATAL_IF(field < 0
86            || field > CanvasPropertyPaintAnimator::ALPHA,
87            "Invalid paint field %d", field);
88    return static_cast<CanvasPropertyPaintAnimator::PaintField>(field);
89}
90
91static jlong createAnimator(JNIEnv* env, jobject clazz, jobject weakThis,
92        jint propertyRaw, jfloat finalValue) {
93    RenderPropertyAnimator::RenderProperty property = toRenderProperty(propertyRaw);
94
95    BaseRenderNodeAnimator* animator = new RenderPropertyAnimator(property, finalValue);
96    animator->setListener(new AnimationListenerBridge(env, weakThis));
97    return reinterpret_cast<jlong>( animator );
98}
99
100static jlong createCanvasPropertyFloatAnimator(JNIEnv* env, jobject clazz,
101        jobject weakThis, jlong canvasPropertyPtr, jfloat finalValue) {
102    CanvasPropertyPrimitive* canvasProperty = reinterpret_cast<CanvasPropertyPrimitive*>(canvasPropertyPtr);
103    BaseRenderNodeAnimator* animator = new CanvasPropertyPrimitiveAnimator(canvasProperty, finalValue);
104    animator->setListener(new AnimationListenerBridge(env, weakThis));
105    return reinterpret_cast<jlong>( animator );
106}
107
108static jlong createCanvasPropertyPaintAnimator(JNIEnv* env, jobject clazz,
109        jobject weakThis, jlong canvasPropertyPtr, jint paintFieldRaw,
110        jfloat finalValue) {
111    CanvasPropertyPaint* canvasProperty = reinterpret_cast<CanvasPropertyPaint*>(canvasPropertyPtr);
112    CanvasPropertyPaintAnimator::PaintField paintField = toPaintField(paintFieldRaw);
113    BaseRenderNodeAnimator* animator = new CanvasPropertyPaintAnimator(
114            canvasProperty, paintField, finalValue);
115    animator->setListener(new AnimationListenerBridge(env, weakThis));
116    return reinterpret_cast<jlong>( animator );
117}
118
119static void setDuration(JNIEnv* env, jobject clazz, jlong animatorPtr, jint duration) {
120    LOG_ALWAYS_FATAL_IF(duration < 0, "Duration cannot be negative");
121    BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
122    animator->setDuration(duration);
123}
124
125static jint getDuration(JNIEnv* env, jobject clazz, jlong animatorPtr) {
126    BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
127    return static_cast<jint>(animator->duration());
128}
129
130static void setInterpolator(JNIEnv* env, jobject clazz, jlong animatorPtr, jlong interpolatorPtr) {
131    BaseRenderNodeAnimator* animator = reinterpret_cast<BaseRenderNodeAnimator*>(animatorPtr);
132    Interpolator* interpolator = reinterpret_cast<Interpolator*>(interpolatorPtr);
133    animator->setInterpolator(interpolator);
134}
135
136#endif
137
138// ----------------------------------------------------------------------------
139// JNI Glue
140// ----------------------------------------------------------------------------
141
142const char* const kClassPathName = "android/view/RenderNodeAnimator";
143
144static JNINativeMethod gMethods[] = {
145#ifdef USE_OPENGL_RENDERER
146    { "nCreateAnimator", "(Ljava/lang/ref/WeakReference;IF)J", (void*) createAnimator },
147    { "nCreateCanvasPropertyFloatAnimator", "(Ljava/lang/ref/WeakReference;JF)J", (void*) createCanvasPropertyFloatAnimator },
148    { "nCreateCanvasPropertyPaintAnimator", "(Ljava/lang/ref/WeakReference;JIF)J", (void*) createCanvasPropertyPaintAnimator },
149    { "nSetDuration", "(JI)V", (void*) setDuration },
150    { "nGetDuration", "(J)I", (void*) getDuration },
151    { "nSetInterpolator", "(JJ)V", (void*) setInterpolator },
152#endif
153};
154
155#define FIND_CLASS(var, className) \
156        var = env->FindClass(className); \
157        LOG_FATAL_IF(! var, "Unable to find class " className);
158
159#define GET_STATIC_METHOD_ID(var, clazz, methodName, methodDescriptor) \
160        var = env->GetStaticMethodID(clazz, methodName, methodDescriptor); \
161        LOG_FATAL_IF(! var, "Unable to find method " methodName);
162
163int register_android_view_RenderNodeAnimator(JNIEnv* env) {
164    FIND_CLASS(gRenderNodeAnimatorClassInfo.clazz, kClassPathName);
165    gRenderNodeAnimatorClassInfo.clazz = jclass(env->NewGlobalRef(gRenderNodeAnimatorClassInfo.clazz));
166
167    GET_STATIC_METHOD_ID(gRenderNodeAnimatorClassInfo.callOnFinished, gRenderNodeAnimatorClassInfo.clazz,
168            "callOnFinished", "(Ljava/lang/ref/WeakReference;)V");
169
170    return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
171}
172
173
174} // namespace android
175