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);
46119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckpublic:
47119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationContext& context() { return mContext; }
48119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
49d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // Called by the RenderNode when it has internally pulsed its own animations
50d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // this frame and does not need to be run again this frame.
51119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void notifyAnimationsRan();
52119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
53d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // Stops tracking the RenderNode and destroys the handle. The node must be
54d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // re-attached to the AnimationContext to receive managed animation
55d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    // pulses.
56d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck    void release();
57d0cd9db31639b246587fe494ec15d32d9fdb3dc7John Reck
58119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckprivate:
59119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    friend class AnimationContext;
60faecb78a6b11c780db47bc940ca7662899ab5d5eChih-Hung Hsieh    explicit AnimationHandle(AnimationContext& context);
61119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle(RenderNode& animatingNode, AnimationContext& context);
62119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ~AnimationHandle();
63119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
64119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void insertAfter(AnimationHandle* prev);
65119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void removeFromList();
66119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
67119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    sp<RenderNode> mRenderNode;
68119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
69119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationContext& mContext;
70119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
71119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle* mPreviousHandle;
72119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle* mNextHandle;
73119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck};
74119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
75119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationContext {
76119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    PREVENT_COPY_AND_ASSIGN(AnimationContext);
77119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckpublic:
78faecb78a6b11c780db47bc940ca7662899ab5d5eChih-Hung Hsieh    ANDROID_API explicit AnimationContext(renderthread::TimeLord& clock);
79119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual ~AnimationContext();
80119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
81119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    nsecs_t frameTimeMs() { return mFrameTimeMs; }
82119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    bool hasAnimations() {
83119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        return mCurrentFrameAnimations.mNextHandle
84119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck                || mNextFrameAnimations.mNextHandle;
85119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    }
86119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
87119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Will always add to the next frame list, which is swapped when
88119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // startFrame() is called
89119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API void addAnimatingRenderNode(RenderNode& node);
90119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
91119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Marks the start of a frame, which will update the frame time and move all
92119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // next frame animations into the current frame
93ec845a215e343cdb3b2e4c7b6aff7b24beb0236bJohn Reck    ANDROID_API virtual void startFrame(TreeInfo::TraversalMode mode);
94119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
95119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Runs any animations still left in mCurrentFrameAnimations that were not run
96119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // as part of the standard RenderNode:prepareTree pass.
97119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual void runRemainingAnimations(TreeInfo& info);
98119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
99119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener);
100119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
101e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck    ANDROID_API virtual void destroy();
102e2478d45ccbe5b6abb360ac9d44771b5f4a50bdeJohn Reck
103c82e879e563ad692cabf19f61a08559c6220171eDoris Liu    ANDROID_API virtual void pauseAnimators() {}
104718cd3eb70703c43f29ca37907bbf0e153d8cca0Doris Liu
105119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckprivate:
106119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    friend class AnimationHandle;
107119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void addAnimationHandle(AnimationHandle* handle);
108119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
109119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    renderthread::TimeLord& mClock;
110119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
111119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Animations left to run this frame, at the end of the frame this should
112119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // be null
113119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle mCurrentFrameAnimations;
114119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Animations queued for next frame
115119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle mNextFrameAnimations;
116119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
117119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    nsecs_t mFrameTimeMs;
118119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck};
119119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
120119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck} /* namespace uirenderer */
121119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck} /* namespace android */
122119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
123119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#endif /* TREEANIMATIONTRACKER_H_ */
124