AnimationContext.h revision 119907cd2575c56b1ebf66348b52e67aaf6a88d8
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#ifndef TREEANIMATIONTRACKER_H_
17#define TREEANIMATIONTRACKER_H_
18
19#include <cutils/compiler.h>
20#include <utils/RefBase.h>
21#include <utils/StrongPointer.h>
22
23#include "renderthread/TimeLord.h"
24#include "utils/Macros.h"
25
26namespace android {
27namespace uirenderer {
28
29class AnimationContext;
30class AnimationListener;
31class BaseRenderNodeAnimator;
32class RenderNode;
33class TreeInfo;
34
35/*
36 * AnimationHandle is several classes merged into one.
37 * 1: It maintains the reference to the AnimationContext required to run animators.
38 * 2: It keeps a strong reference to RenderNodes with animators so that
39 *    we don't lose them if they are no longer in the display tree. This is
40 *    required so that we can keep animating them, and properly notify listeners
41 *    of onAnimationFinished.
42 * 3: It forms a doubly linked list so that we can cheaply move between states.
43 */
44class AnimationHandle {
45    PREVENT_COPY_AND_ASSIGN(AnimationHandle);
46public:
47    AnimationContext& context() { return mContext; }
48
49    void notifyAnimationsRan();
50
51private:
52    friend class AnimationContext;
53    AnimationHandle(AnimationContext& context);
54    AnimationHandle(RenderNode& animatingNode, AnimationContext& context);
55    ~AnimationHandle();
56
57    void insertAfter(AnimationHandle* prev);
58    void removeFromList();
59
60    sp<RenderNode> mRenderNode;
61
62    AnimationContext& mContext;
63
64    AnimationHandle* mPreviousHandle;
65    AnimationHandle* mNextHandle;
66};
67
68class AnimationContext {
69    PREVENT_COPY_AND_ASSIGN(AnimationContext);
70public:
71    ANDROID_API AnimationContext(renderthread::TimeLord& clock);
72    ANDROID_API virtual ~AnimationContext();
73
74    nsecs_t frameTimeMs() { return mFrameTimeMs; }
75    bool hasAnimations() {
76        return mCurrentFrameAnimations.mNextHandle
77                || mNextFrameAnimations.mNextHandle;
78    }
79
80    // Will always add to the next frame list, which is swapped when
81    // startFrame() is called
82    ANDROID_API void addAnimatingRenderNode(RenderNode& node);
83
84    // Marks the start of a frame, which will update the frame time and move all
85    // next frame animations into the current frame
86    ANDROID_API virtual void startFrame();
87
88    // Runs any animations still left in mCurrentFrameAnimations that were not run
89    // as part of the standard RenderNode:prepareTree pass.
90    ANDROID_API virtual void runRemainingAnimations(TreeInfo& info);
91
92    ANDROID_API virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener);
93
94private:
95    friend class AnimationHandle;
96    void addAnimationHandle(AnimationHandle* handle);
97
98    renderthread::TimeLord& mClock;
99
100    // Animations left to run this frame, at the end of the frame this should
101    // be null
102    AnimationHandle mCurrentFrameAnimations;
103    // Animations queued for next frame
104    AnimationHandle mNextFrameAnimations;
105
106    nsecs_t mFrameTimeMs;
107};
108
109} /* namespace uirenderer */
110} /* namespace android */
111
112#endif /* TREEANIMATIONTRACKER_H_ */
113