1/*
2 * Copyright (C) 2014 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
17package android.view;
18
19import android.animation.Animator;
20import android.animation.TimeInterpolator;
21import android.animation.ValueAnimator;
22import android.graphics.Canvas;
23import android.graphics.CanvasProperty;
24import android.graphics.Paint;
25import android.util.SparseIntArray;
26
27import com.android.internal.util.VirtualRefBasePtr;
28import com.android.internal.view.animation.FallbackLUTInterpolator;
29import com.android.internal.view.animation.HasNativeInterpolator;
30import com.android.internal.view.animation.NativeInterpolatorFactory;
31
32import java.util.ArrayList;
33
34/**
35 * @hide
36 */
37public class RenderNodeAnimator extends Animator {
38    // Keep in sync with enum RenderProperty in Animator.h
39    public static final int TRANSLATION_X = 0;
40    public static final int TRANSLATION_Y = 1;
41    public static final int TRANSLATION_Z = 2;
42    public static final int SCALE_X = 3;
43    public static final int SCALE_Y = 4;
44    public static final int ROTATION = 5;
45    public static final int ROTATION_X = 6;
46    public static final int ROTATION_Y = 7;
47    public static final int X = 8;
48    public static final int Y = 9;
49    public static final int Z = 10;
50    public static final int ALPHA = 11;
51    // The last value in the enum, used for array size initialization
52    public static final int LAST_VALUE = ALPHA;
53
54    // Keep in sync with enum PaintFields in Animator.h
55    public static final int PAINT_STROKE_WIDTH = 0;
56
57    /**
58     * Field for the Paint alpha channel, which should be specified as a value
59     * between 0 and 255.
60     */
61    public static final int PAINT_ALPHA = 1;
62
63    // ViewPropertyAnimator uses a mask for its values, we need to remap them
64    // to the enum values here. RenderPropertyAnimator can't use the mask values
65    // directly as internally it uses a lookup table so it needs the values to
66    // be sequential starting from 0
67    private static final SparseIntArray sViewPropertyAnimatorMap = new SparseIntArray(15) {{
68        put(ViewPropertyAnimator.TRANSLATION_X, TRANSLATION_X);
69        put(ViewPropertyAnimator.TRANSLATION_Y, TRANSLATION_Y);
70        put(ViewPropertyAnimator.TRANSLATION_Z, TRANSLATION_Z);
71        put(ViewPropertyAnimator.SCALE_X, SCALE_X);
72        put(ViewPropertyAnimator.SCALE_Y, SCALE_Y);
73        put(ViewPropertyAnimator.ROTATION, ROTATION);
74        put(ViewPropertyAnimator.ROTATION_X, ROTATION_X);
75        put(ViewPropertyAnimator.ROTATION_Y, ROTATION_Y);
76        put(ViewPropertyAnimator.X, X);
77        put(ViewPropertyAnimator.Y, Y);
78        put(ViewPropertyAnimator.Z, Z);
79        put(ViewPropertyAnimator.ALPHA, ALPHA);
80    }};
81
82    private VirtualRefBasePtr mNativePtr;
83
84    private RenderNode mTarget;
85    private View mViewTarget;
86    private int mRenderProperty = -1;
87    private float mFinalValue;
88    private TimeInterpolator mInterpolator;
89
90    private static final int STATE_PREPARE = 0;
91    private static final int STATE_DELAYED = 1;
92    private static final int STATE_RUNNING = 2;
93    private static final int STATE_FINISHED = 3;
94    private int mState = STATE_PREPARE;
95
96    private long mUnscaledDuration = 300;
97    private long mUnscaledStartDelay = 0;
98    // If this is true, we will run any start delays on the UI thread. This is
99    // the safe default, and is necessary to ensure start listeners fire at
100    // the correct time. Animators created by RippleDrawable (the
101    // CanvasProperty<> ones) do not have this expectation, and as such will
102    // set this to false so that the renderthread handles the startdelay instead
103    private final boolean mUiThreadHandlesDelay;
104    private long mStartDelay = 0;
105    private long mStartTime;
106
107    public static int mapViewPropertyToRenderProperty(int viewProperty) {
108        return sViewPropertyAnimatorMap.get(viewProperty);
109    }
110
111    public RenderNodeAnimator(int property, float finalValue) {
112        mRenderProperty = property;
113        mFinalValue = finalValue;
114        mUiThreadHandlesDelay = true;
115        init(nCreateAnimator(property, finalValue));
116    }
117
118    public RenderNodeAnimator(CanvasProperty<Float> property, float finalValue) {
119        init(nCreateCanvasPropertyFloatAnimator(
120                property.getNativeContainer(), finalValue));
121        mUiThreadHandlesDelay = false;
122    }
123
124    /**
125     * Creates a new render node animator for a field on a Paint property.
126     *
127     * @param property The paint property to target
128     * @param paintField Paint field to animate, one of {@link #PAINT_ALPHA} or
129     *            {@link #PAINT_STROKE_WIDTH}
130     * @param finalValue The target value for the property
131     */
132    public RenderNodeAnimator(CanvasProperty<Paint> property, int paintField, float finalValue) {
133        init(nCreateCanvasPropertyPaintAnimator(
134                property.getNativeContainer(), paintField, finalValue));
135        mUiThreadHandlesDelay = false;
136    }
137
138    public RenderNodeAnimator(int x, int y, float startRadius, float endRadius) {
139        init(nCreateRevealAnimator(x, y, startRadius, endRadius));
140        mUiThreadHandlesDelay = true;
141    }
142
143    private void init(long ptr) {
144        mNativePtr = new VirtualRefBasePtr(ptr);
145    }
146
147    private void checkMutable() {
148        if (mState != STATE_PREPARE) {
149            throw new IllegalStateException("Animator has already started, cannot change it now!");
150        }
151        if (mNativePtr == null) {
152            throw new IllegalStateException("Animator's target has been destroyed "
153                    + "(trying to modify an animation after activity destroy?)");
154        }
155    }
156
157    static boolean isNativeInterpolator(TimeInterpolator interpolator) {
158        return interpolator.getClass().isAnnotationPresent(HasNativeInterpolator.class);
159    }
160
161    private void applyInterpolator() {
162        if (mInterpolator == null) return;
163
164        long ni;
165        if (isNativeInterpolator(mInterpolator)) {
166            ni = ((NativeInterpolatorFactory)mInterpolator).createNativeInterpolator();
167        } else {
168            long duration = nGetDuration(mNativePtr.get());
169            ni = FallbackLUTInterpolator.createNativeInterpolator(mInterpolator, duration);
170        }
171        nSetInterpolator(mNativePtr.get(), ni);
172    }
173
174    @Override
175    public void start() {
176        if (mTarget == null) {
177            throw new IllegalStateException("Missing target!");
178        }
179
180        if (mState != STATE_PREPARE) {
181            throw new IllegalStateException("Already started!");
182        }
183
184        mState = STATE_DELAYED;
185        applyInterpolator();
186
187        if (mNativePtr == null) {
188            // It's dead, immediately cancel
189            cancel();
190        } else if (mStartDelay <= 0 || !mUiThreadHandlesDelay) {
191            nSetStartDelay(mNativePtr.get(), mStartDelay);
192            doStart();
193        } else {
194            getHelper().addDelayedAnimation(this);
195        }
196    }
197
198    private void doStart() {
199        // Alpha is a special snowflake that has the canonical value stored
200        // in mTransformationInfo instead of in RenderNode, so we need to update
201        // it with the final value here.
202        if (mRenderProperty == RenderNodeAnimator.ALPHA) {
203            mViewTarget.ensureTransformationInfo();
204            mViewTarget.mTransformationInfo.mAlpha = mFinalValue;
205        }
206
207        moveToRunningState();
208
209        if (mViewTarget != null) {
210            // Kick off a frame to start the process
211            mViewTarget.invalidateViewProperty(true, false);
212        }
213    }
214
215    private void moveToRunningState() {
216        mState = STATE_RUNNING;
217        if (mNativePtr != null) {
218            nStart(mNativePtr.get());
219        }
220        notifyStartListeners();
221    }
222
223    private void notifyStartListeners() {
224        final ArrayList<AnimatorListener> listeners = cloneListeners();
225        final int numListeners = listeners == null ? 0 : listeners.size();
226        for (int i = 0; i < numListeners; i++) {
227            listeners.get(i).onAnimationStart(this);
228        }
229    }
230
231    @Override
232    public void cancel() {
233        if (mState != STATE_PREPARE && mState != STATE_FINISHED) {
234            if (mState == STATE_DELAYED) {
235                getHelper().removeDelayedAnimation(this);
236                moveToRunningState();
237            }
238
239            final ArrayList<AnimatorListener> listeners = cloneListeners();
240            final int numListeners = listeners == null ? 0 : listeners.size();
241            for (int i = 0; i < numListeners; i++) {
242                listeners.get(i).onAnimationCancel(this);
243            }
244
245            end();
246        }
247    }
248
249    @Override
250    public void end() {
251        if (mState != STATE_FINISHED) {
252            if (mState < STATE_RUNNING) {
253                getHelper().removeDelayedAnimation(this);
254                doStart();
255            }
256            if (mNativePtr != null) {
257                nEnd(mNativePtr.get());
258                if (mViewTarget != null) {
259                    // Kick off a frame to flush the state change
260                    mViewTarget.invalidateViewProperty(true, false);
261                }
262            } else {
263                // It's already dead, jump to onFinish
264                onFinished();
265            }
266        }
267    }
268
269    @Override
270    public void pause() {
271        throw new UnsupportedOperationException();
272    }
273
274    @Override
275    public void resume() {
276        throw new UnsupportedOperationException();
277    }
278
279    public void setTarget(View view) {
280        mViewTarget = view;
281        setTarget(mViewTarget.mRenderNode);
282    }
283
284    public void setTarget(Canvas canvas) {
285        if (!(canvas instanceof DisplayListCanvas)) {
286            throw new IllegalArgumentException("Not a GLES20RecordingCanvas");
287        }
288        final DisplayListCanvas recordingCanvas = (DisplayListCanvas) canvas;
289        setTarget(recordingCanvas.mNode);
290    }
291
292    private void setTarget(RenderNode node) {
293        checkMutable();
294        if (mTarget != null) {
295            throw new IllegalStateException("Target already set!");
296        }
297        nSetListener(mNativePtr.get(), this);
298        mTarget = node;
299        mTarget.addAnimator(this);
300    }
301
302    public void setStartValue(float startValue) {
303        checkMutable();
304        nSetStartValue(mNativePtr.get(), startValue);
305    }
306
307    @Override
308    public void setStartDelay(long startDelay) {
309        checkMutable();
310        if (startDelay < 0) {
311            throw new IllegalArgumentException("startDelay must be positive; " + startDelay);
312        }
313        mUnscaledStartDelay = startDelay;
314        mStartDelay = (long) (ValueAnimator.getDurationScale() * startDelay);
315    }
316
317    @Override
318    public long getStartDelay() {
319        return mUnscaledStartDelay;
320    }
321
322    @Override
323    public RenderNodeAnimator setDuration(long duration) {
324        checkMutable();
325        if (duration < 0) {
326            throw new IllegalArgumentException("duration must be positive; " + duration);
327        }
328        mUnscaledDuration = duration;
329        nSetDuration(mNativePtr.get(), (long) (duration * ValueAnimator.getDurationScale()));
330        return this;
331    }
332
333    @Override
334    public long getDuration() {
335        return mUnscaledDuration;
336    }
337
338    @Override
339    public long getTotalDuration() {
340        return mUnscaledDuration + mUnscaledStartDelay;
341    }
342
343    @Override
344    public boolean isRunning() {
345        return mState == STATE_DELAYED || mState == STATE_RUNNING;
346    }
347
348    @Override
349    public boolean isStarted() {
350        return mState != STATE_PREPARE;
351    }
352
353    @Override
354    public void setInterpolator(TimeInterpolator interpolator) {
355        checkMutable();
356        mInterpolator = interpolator;
357    }
358
359    @Override
360    public TimeInterpolator getInterpolator() {
361        return mInterpolator;
362    }
363
364    protected void onFinished() {
365        if (mState == STATE_PREPARE) {
366            // Unlikely but possible, the native side has been destroyed
367            // before we have started.
368            releaseNativePtr();
369            return;
370        }
371        if (mState == STATE_DELAYED) {
372            getHelper().removeDelayedAnimation(this);
373            notifyStartListeners();
374        }
375        mState = STATE_FINISHED;
376
377        final ArrayList<AnimatorListener> listeners = cloneListeners();
378        final int numListeners = listeners == null ? 0 : listeners.size();
379        for (int i = 0; i < numListeners; i++) {
380            listeners.get(i).onAnimationEnd(this);
381        }
382
383        // Release the native object, as it has a global reference to us. This
384        // breaks the cyclic reference chain, and allows this object to be
385        // GC'd
386        releaseNativePtr();
387    }
388
389    private void releaseNativePtr() {
390        if (mNativePtr != null) {
391            mNativePtr.release();
392            mNativePtr = null;
393        }
394    }
395
396    @SuppressWarnings("unchecked")
397    private ArrayList<AnimatorListener> cloneListeners() {
398        ArrayList<AnimatorListener> listeners = getListeners();
399        if (listeners != null) {
400            listeners = (ArrayList<AnimatorListener>) listeners.clone();
401        }
402        return listeners;
403    }
404
405    long getNativeAnimator() {
406        return mNativePtr.get();
407    }
408
409    /**
410     * @return true if the animator was started, false if still delayed
411     */
412    private boolean processDelayed(long frameTimeMs) {
413        if (mStartTime == 0) {
414            mStartTime = frameTimeMs;
415        } else if ((frameTimeMs - mStartTime) >= mStartDelay) {
416            doStart();
417            return true;
418        }
419        return false;
420    }
421
422    private static DelayedAnimationHelper getHelper() {
423        DelayedAnimationHelper helper = sAnimationHelper.get();
424        if (helper == null) {
425            helper = new DelayedAnimationHelper();
426            sAnimationHelper.set(helper);
427        }
428        return helper;
429    }
430
431    private static ThreadLocal<DelayedAnimationHelper> sAnimationHelper =
432            new ThreadLocal<DelayedAnimationHelper>();
433
434    private static class DelayedAnimationHelper implements Runnable {
435
436        private ArrayList<RenderNodeAnimator> mDelayedAnims = new ArrayList<RenderNodeAnimator>();
437        private final Choreographer mChoreographer;
438        private boolean mCallbackScheduled;
439
440        public DelayedAnimationHelper() {
441            mChoreographer = Choreographer.getInstance();
442        }
443
444        public void addDelayedAnimation(RenderNodeAnimator animator) {
445            mDelayedAnims.add(animator);
446            scheduleCallback();
447        }
448
449        public void removeDelayedAnimation(RenderNodeAnimator animator) {
450            mDelayedAnims.remove(animator);
451        }
452
453        private void scheduleCallback() {
454            if (!mCallbackScheduled) {
455                mCallbackScheduled = true;
456                mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, this, null);
457            }
458        }
459
460        @Override
461        public void run() {
462            long frameTimeMs = mChoreographer.getFrameTime();
463            mCallbackScheduled = false;
464
465            int end = 0;
466            for (int i = 0; i < mDelayedAnims.size(); i++) {
467                RenderNodeAnimator animator = mDelayedAnims.get(i);
468                if (!animator.processDelayed(frameTimeMs)) {
469                    if (end != i) {
470                        mDelayedAnims.set(end, animator);
471                    }
472                    end++;
473                }
474            }
475            while (mDelayedAnims.size() > end) {
476                mDelayedAnims.remove(mDelayedAnims.size() - 1);
477            }
478
479            if (mDelayedAnims.size() > 0) {
480                scheduleCallback();
481            }
482        }
483    }
484
485    // Called by native
486    private static void callOnFinished(RenderNodeAnimator animator) {
487        animator.onFinished();
488    }
489
490    @Override
491    public Animator clone() {
492        throw new IllegalStateException("Cannot clone this animator");
493    }
494
495    @Override
496    public void setAllowRunningAsynchronously(boolean mayRunAsync) {
497        checkMutable();
498        nSetAllowRunningAsync(mNativePtr.get(), mayRunAsync);
499    }
500
501    private static native long nCreateAnimator(int property, float finalValue);
502    private static native long nCreateCanvasPropertyFloatAnimator(
503            long canvasProperty, float finalValue);
504    private static native long nCreateCanvasPropertyPaintAnimator(
505            long canvasProperty, int paintField, float finalValue);
506    private static native long nCreateRevealAnimator(
507            int x, int y, float startRadius, float endRadius);
508
509    private static native void nSetStartValue(long nativePtr, float startValue);
510    private static native void nSetDuration(long nativePtr, long duration);
511    private static native long nGetDuration(long nativePtr);
512    private static native void nSetStartDelay(long nativePtr, long startDelay);
513    private static native void nSetInterpolator(long animPtr, long interpolatorPtr);
514    private static native void nSetAllowRunningAsync(long animPtr, boolean mayRunAsync);
515    private static native void nSetListener(long animPtr, RenderNodeAnimator listener);
516
517    private static native void nStart(long animPtr);
518    private static native void nEnd(long animPtr);
519}
520