AnimationContext.h revision 119907cd2575c56b1ebf66348b52e67aaf6a88d8
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
23119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include "renderthread/TimeLord.h"
24119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#include "utils/Macros.h"
25119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
26119907cd2575c56b1ebf66348b52e67aaf6a88d8John Recknamespace android {
27119907cd2575c56b1ebf66348b52e67aaf6a88d8John Recknamespace uirenderer {
28119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
29119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationContext;
30119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationListener;
31119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass BaseRenderNodeAnimator;
32119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass RenderNode;
33119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass TreeInfo;
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
49119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void notifyAnimationsRan();
50119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
51119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckprivate:
52119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    friend class AnimationContext;
53119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle(AnimationContext& context);
54119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle(RenderNode& animatingNode, AnimationContext& context);
55119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ~AnimationHandle();
56119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
57119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void insertAfter(AnimationHandle* prev);
58119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void removeFromList();
59119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
60119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    sp<RenderNode> mRenderNode;
61119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
62119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationContext& mContext;
63119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
64119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle* mPreviousHandle;
65119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle* mNextHandle;
66119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck};
67119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
68119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckclass AnimationContext {
69119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    PREVENT_COPY_AND_ASSIGN(AnimationContext);
70119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckpublic:
71119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API AnimationContext(renderthread::TimeLord& clock);
72119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual ~AnimationContext();
73119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
74119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    nsecs_t frameTimeMs() { return mFrameTimeMs; }
75119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    bool hasAnimations() {
76119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck        return mCurrentFrameAnimations.mNextHandle
77119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck                || mNextFrameAnimations.mNextHandle;
78119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    }
79119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
80119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Will always add to the next frame list, which is swapped when
81119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // startFrame() is called
82119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API void addAnimatingRenderNode(RenderNode& node);
83119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
84119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Marks the start of a frame, which will update the frame time and move all
85119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // next frame animations into the current frame
86119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual void startFrame();
87119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
88119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Runs any animations still left in mCurrentFrameAnimations that were not run
89119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // as part of the standard RenderNode:prepareTree pass.
90119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual void runRemainingAnimations(TreeInfo& info);
91119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
92119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    ANDROID_API virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener);
93119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
94119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reckprivate:
95119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    friend class AnimationHandle;
96119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    void addAnimationHandle(AnimationHandle* handle);
97119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
98119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    renderthread::TimeLord& mClock;
99119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
100119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Animations left to run this frame, at the end of the frame this should
101119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // be null
102119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle mCurrentFrameAnimations;
103119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    // Animations queued for next frame
104119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    AnimationHandle mNextFrameAnimations;
105119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
106119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck    nsecs_t mFrameTimeMs;
107119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck};
108119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
109119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck} /* namespace uirenderer */
110119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck} /* namespace android */
111119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck
112119907cd2575c56b1ebf66348b52e67aaf6a88d8John Reck#endif /* TREEANIMATIONTRACKER_H_ */
113