blob: 909ed36a21276d35551d0afd38a7f319bfd65003 [file] [log] [blame]
John Reck119907c2014-08-14 09:02:01 -07001/*
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
John Reckec845a22014-09-05 15:23:38 -070023#include "TreeInfo.h"
John Reck119907c2014-08-14 09:02:01 -070024#include "renderthread/TimeLord.h"
25#include "utils/Macros.h"
26
27namespace android {
28namespace uirenderer {
29
30class AnimationContext;
31class AnimationListener;
32class BaseRenderNodeAnimator;
33class RenderNode;
John Reck119907c2014-08-14 09:02:01 -070034
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
John Reckd0cd9db2014-08-28 08:43:39 -070049 // Called by the RenderNode when it has internally pulsed its own animations
50 // this frame and does not need to be run again this frame.
John Reck119907c2014-08-14 09:02:01 -070051 void notifyAnimationsRan();
52
John Reckd0cd9db2014-08-28 08:43:39 -070053 // Stops tracking the RenderNode and destroys the handle. The node must be
54 // re-attached to the AnimationContext to receive managed animation
55 // pulses.
56 void release();
57
John Reck119907c2014-08-14 09:02:01 -070058private:
59 friend class AnimationContext;
60 AnimationHandle(AnimationContext& context);
61 AnimationHandle(RenderNode& animatingNode, AnimationContext& context);
62 ~AnimationHandle();
63
64 void insertAfter(AnimationHandle* prev);
65 void removeFromList();
66
67 sp<RenderNode> mRenderNode;
68
69 AnimationContext& mContext;
70
71 AnimationHandle* mPreviousHandle;
72 AnimationHandle* mNextHandle;
73};
74
75class AnimationContext {
76 PREVENT_COPY_AND_ASSIGN(AnimationContext);
77public:
78 ANDROID_API AnimationContext(renderthread::TimeLord& clock);
79 ANDROID_API virtual ~AnimationContext();
80
81 nsecs_t frameTimeMs() { return mFrameTimeMs; }
82 bool hasAnimations() {
83 return mCurrentFrameAnimations.mNextHandle
84 || mNextFrameAnimations.mNextHandle;
85 }
86
87 // Will always add to the next frame list, which is swapped when
88 // startFrame() is called
89 ANDROID_API void addAnimatingRenderNode(RenderNode& node);
90
91 // Marks the start of a frame, which will update the frame time and move all
92 // next frame animations into the current frame
John Reckec845a22014-09-05 15:23:38 -070093 ANDROID_API virtual void startFrame(TreeInfo::TraversalMode mode);
John Reck119907c2014-08-14 09:02:01 -070094
95 // Runs any animations still left in mCurrentFrameAnimations that were not run
96 // as part of the standard RenderNode:prepareTree pass.
97 ANDROID_API virtual void runRemainingAnimations(TreeInfo& info);
98
99 ANDROID_API virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener);
100
John Recke2478d42014-09-03 16:46:05 -0700101 ANDROID_API virtual void destroy();
102
John Reck119907c2014-08-14 09:02:01 -0700103private:
104 friend class AnimationHandle;
105 void addAnimationHandle(AnimationHandle* handle);
106
107 renderthread::TimeLord& mClock;
108
109 // Animations left to run this frame, at the end of the frame this should
110 // be null
111 AnimationHandle mCurrentFrameAnimations;
112 // Animations queued for next frame
113 AnimationHandle mNextFrameAnimations;
114
115 nsecs_t mFrameTimeMs;
116};
117
118} /* namespace uirenderer */
119} /* namespace android */
120
121#endif /* TREEANIMATIONTRACKER_H_ */