1119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck/*
2119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * Copyright (C) 2014 The Android Open Source Project
3119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck *
4119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * you may not use this file except in compliance with the License.
6119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * You may obtain a copy of the License at
7119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck *
8119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck *
10119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * Unless required by applicable law or agreed to in writing, software
11119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * See the License for the specific language governing permissions and
14119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * limitations under the License.
15119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck */
16119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#ifndef TREEANIMATIONTRACKER_H_
17119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#define TREEANIMATIONTRACKER_H_
18119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
19119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include <cutils/compiler.h>
20119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include <utils/RefBase.h>
21119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include <utils/StrongPointer.h>
22119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
23ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck#include "TreeInfo.h"
24119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include "renderthread/TimeLord.h"
25119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include "utils/Macros.h"
26119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
27119907cd2575c56b1ebf66348b52e67aaf6a88d8John Recknamespace android {
28119907cd2575c56b1ebf66348b52e67aaf6a88d8John Recknamespace uirenderer {
29119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
30119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationContext;
31119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationListener;
32119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass BaseRenderNodeAnimator;
33119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass RenderNode;
34119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
35119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck/*
36119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * AnimationHandle is several classes merged into one.
37119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * 1: It maintains the reference to the AnimationContext required to run animators.
38119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * 2: It keeps a strong reference to RenderNodes with animators so that
39119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck *    we don't lose them if they are no longer in the display tree. This is
40119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck *    required so that we can keep animating them, and properly notify listeners
41119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck *    of onAnimationFinished.
42119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck * 3: It forms a doubly linked list so that we can cheaply move between states.
43119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck */
44119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationHandle {
45119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    PREVENT_COPY_AND_ASSIGN(AnimationHandle);
461bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck
47119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckpublic:
48119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationContext& context() { return mContext; }
49119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
50d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // Called by the RenderNode when it has internally pulsed its own animations
51d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // this frame and does not need to be run again this frame.
52119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void notifyAnimationsRan();
53119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
54d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // Stops tracking the RenderNode and destroys the handle. The node must be
55d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // re-attached to the AnimationContext to receive managed animation
56d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // pulses.
57d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    void release();
58d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck
59119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckprivate:
60119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    friend class AnimationContext;
61faecb78a6b11c780db47bc940ca7662899ab5d5eChih-Hung Hsieh    explicit AnimationHandle(AnimationContext& context);
62119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle(RenderNode& animatingNode, AnimationContext& context);
63119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ~AnimationHandle();
64119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
65119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void insertAfter(AnimationHandle* prev);
66119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void removeFromList();
67119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
68119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    sp<RenderNode> mRenderNode;
69119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
70119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationContext& mContext;
71119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
72119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle* mPreviousHandle;
73119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle* mNextHandle;
74119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck};
75119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
76119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationContext {
77119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    PREVENT_COPY_AND_ASSIGN(AnimationContext);
781bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck
79119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckpublic:
80faecb78a6b11c780db47bc940ca7662899ab5d5eChih-Hung Hsieh    ANDROID_API explicit AnimationContext(renderthread::TimeLord& clock);
81119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual ~AnimationContext();
82119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
83119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    nsecs_t frameTimeMs() { return mFrameTimeMs; }
84119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    bool hasAnimations() {
851bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck        return mCurrentFrameAnimations.mNextHandle || mNextFrameAnimations.mNextHandle;
86119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    }
87119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
88119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Will always add to the next frame list, which is swapped when
89119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // startFrame() is called
90119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API void addAnimatingRenderNode(RenderNode& node);
91119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
92119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Marks the start of a frame, which will update the frame time and move all
93119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // next frame animations into the current frame
94ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck    ANDROID_API virtual void startFrame(TreeInfo::TraversalMode mode);
95119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
96119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Runs any animations still left in mCurrentFrameAnimations that were not run
97119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // as part of the standard RenderNode:prepareTree pass.
98119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual void runRemainingAnimations(TreeInfo& info);
99119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
1001bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck    ANDROID_API virtual void callOnFinished(BaseRenderNodeAnimator* animator,
1011bcacfdcab0eaa0cee92bd7f5a1b5e271dd68e52John Reck                                            AnimationListener* listener);
102119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
103e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    ANDROID_API virtual void destroy();
104e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck
105c82e879e563ad692cabf19f61a08559c6220171eDoris Liu    ANDROID_API virtual void pauseAnimators() {}
106718cd3eb70703c43f29ca37907bbf0e153d8cca0Doris Liu
107119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckprivate:
108119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    friend class AnimationHandle;
109119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void addAnimationHandle(AnimationHandle* handle);
110119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
111119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    renderthread::TimeLord& mClock;
112119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
113119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Animations left to run this frame, at the end of the frame this should
114119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // be null
115119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle mCurrentFrameAnimations;
116119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Animations queued for next frame
117119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle mNextFrameAnimations;
118119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
119119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    nsecs_t mFrameTimeMs;
120119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck};
121119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
122119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck} /* namespace uirenderer */
123119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck} /* namespace android */
124119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
125119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#endif /* TREEANIMATIONTRACKER_H_ */
126