AnimationContext.h revision 718cd3eb70703c43f29ca37907bbf0e153d8cca0
1f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock/*
2f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * Copyright (C) 2014 The Android Open Source Project
3f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock *
4f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
5f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * you may not use this file except in compliance with the License.
6f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * You may obtain a copy of the License at
7f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock *
8f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
9f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock *
10f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * Unless required by applicable law or agreed to in writing, software
11f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
12f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * See the License for the specific language governing permissions and
14f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * limitations under the License.
15f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock */
16f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock#ifndef TREEANIMATIONTRACKER_H_
17f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock#define TREEANIMATIONTRACKER_H_
18f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock
19f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock#include <cutils/compiler.h>
20f55b7f2fb614b917b7d1943396880978258dd1bbJohn Spurlock#include <utils/RefBase.h>
21a29528979cdd6a885869b7651f3b0f93f75843bbJason Monk#include <utils/StrongPointer.h>
22f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock
23f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock#include "TreeInfo.h"
24f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock#include "renderthread/TimeLord.h"
25f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock#include "utils/Macros.h"
26f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock
27782cd6711bc15e3f03456210b3543a67606d6591Jason Monknamespace android {
28f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlocknamespace uirenderer {
29a29528979cdd6a885869b7651f3b0f93f75843bbJason Monk
30ec34da8f6f31c5ee1e543c0c7999a067487e0ce9Jason Monkclass AnimationContext;
319c7844cb91b43929d0a86b1c90aa1efb37f5463aJason Monkclass AnimationListener;
32f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlockclass BaseRenderNodeAnimator;
33f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlockclass RenderNode;
34782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
35782cd6711bc15e3f03456210b3543a67606d6591Jason Monk/*
36782cd6711bc15e3f03456210b3543a67606d6591Jason Monk * AnimationHandle is several classes merged into one.
37f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * 1: It maintains the reference to the AnimationContext required to run animators.
38782cd6711bc15e3f03456210b3543a67606d6591Jason Monk * 2: It keeps a strong reference to RenderNodes with animators so that
39a29528979cdd6a885869b7651f3b0f93f75843bbJason Monk *    we don't lose them if they are no longer in the display tree. This is
409e5341f4fe2b7e64a55e057478946546e176358bJason Monk *    required so that we can keep animating them, and properly notify listeners
41f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock *    of onAnimationFinished.
42f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock * 3: It forms a doubly linked list so that we can cheaply move between states.
43f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock */
44f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlockclass AnimationHandle {
45f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock    PREVENT_COPY_AND_ASSIGN(AnimationHandle);
46f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlockpublic:
47f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock    AnimationContext& context() { return mContext; }
48634acb9712f2627acf9279a78d120ea2da1e0464Yao Chen
49782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    // Called by the RenderNode when it has internally pulsed its own animations
509e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // this frame and does not need to be run again this frame.
519e5341f4fe2b7e64a55e057478946546e176358bJason Monk    void notifyAnimationsRan();
529e5341f4fe2b7e64a55e057478946546e176358bJason Monk
539e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // Stops tracking the RenderNode and destroys the handle. The node must be
549e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // re-attached to the AnimationContext to receive managed animation
559e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // pulses.
569e5341f4fe2b7e64a55e057478946546e176358bJason Monk    void release();
579e5341f4fe2b7e64a55e057478946546e176358bJason Monk
589e5341f4fe2b7e64a55e057478946546e176358bJason Monkprivate:
59f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock    friend class AnimationContext;
60f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock    AnimationHandle(AnimationContext& context);
61782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    AnimationHandle(RenderNode& animatingNode, AnimationContext& context);
62a29528979cdd6a885869b7651f3b0f93f75843bbJason Monk    ~AnimationHandle();
633edad31233d391eb55ffe688beb1234b5b79be8fJason Monk
643edad31233d391eb55ffe688beb1234b5b79be8fJason Monk    void insertAfter(AnimationHandle* prev);
65a29528979cdd6a885869b7651f3b0f93f75843bbJason Monk    void removeFromList();
66782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
679e5341f4fe2b7e64a55e057478946546e176358bJason Monk    sp<RenderNode> mRenderNode;
689e5341f4fe2b7e64a55e057478946546e176358bJason Monk
699e5341f4fe2b7e64a55e057478946546e176358bJason Monk    AnimationContext& mContext;
709e5341f4fe2b7e64a55e057478946546e176358bJason Monk
71b02c744f05b0cca05b77fcbaad4a546ad86081ebJohn Spurlock    AnimationHandle* mPreviousHandle;
72b02c744f05b0cca05b77fcbaad4a546ad86081ebJohn Spurlock    AnimationHandle* mNextHandle;
73f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock};
749c7844cb91b43929d0a86b1c90aa1efb37f5463aJason Monk
75f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlockclass AnimationContext {
76f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock    PREVENT_COPY_AND_ASSIGN(AnimationContext);
77782cd6711bc15e3f03456210b3543a67606d6591Jason Monkpublic:
78634acb9712f2627acf9279a78d120ea2da1e0464Yao Chen    ANDROID_API AnimationContext(renderthread::TimeLord& clock);
79782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    ANDROID_API virtual ~AnimationContext();
80782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
81782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    nsecs_t frameTimeMs() { return mFrameTimeMs; }
82a29528979cdd6a885869b7651f3b0f93f75843bbJason Monk    bool hasAnimations() {
83782cd6711bc15e3f03456210b3543a67606d6591Jason Monk        return mCurrentFrameAnimations.mNextHandle
84782cd6711bc15e3f03456210b3543a67606d6591Jason Monk                || mNextFrameAnimations.mNextHandle;
85782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    }
86782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
87782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    // Will always add to the next frame list, which is swapped when
88782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    // startFrame() is called
89782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    ANDROID_API void addAnimatingRenderNode(RenderNode& node);
90782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
91782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    // Marks the start of a frame, which will update the frame time and move all
92f88d8082a86bee00c604cbbcfb5261f5573936feJohn Spurlock    // next frame animations into the current frame
93de850bbcaa61c1874b803f2086443febbafd81a4Jason Monk    ANDROID_API virtual void startFrame(TreeInfo::TraversalMode mode);
949e5341f4fe2b7e64a55e057478946546e176358bJason Monk
959e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // Runs any animations still left in mCurrentFrameAnimations that were not run
969e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // as part of the standard RenderNode:prepareTree pass.
97782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    ANDROID_API virtual void runRemainingAnimations(TreeInfo& info);
98782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
99782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    ANDROID_API virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener);
100782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
101782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    ANDROID_API virtual void destroy();
102782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
103782cd6711bc15e3f03456210b3543a67606d6591Jason Monk    ANDROID_API virtual void detachAnimators() {}
104782cd6711bc15e3f03456210b3543a67606d6591Jason Monk
105782cd6711bc15e3f03456210b3543a67606d6591Jason Monkprivate:
1069e5341f4fe2b7e64a55e057478946546e176358bJason Monk    friend class AnimationHandle;
1079e5341f4fe2b7e64a55e057478946546e176358bJason Monk    void addAnimationHandle(AnimationHandle* handle);
1089e5341f4fe2b7e64a55e057478946546e176358bJason Monk
1099e5341f4fe2b7e64a55e057478946546e176358bJason Monk    renderthread::TimeLord& mClock;
1109e5341f4fe2b7e64a55e057478946546e176358bJason Monk
1119e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // Animations left to run this frame, at the end of the frame this should
1129e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // be null
1139e5341f4fe2b7e64a55e057478946546e176358bJason Monk    AnimationHandle mCurrentFrameAnimations;
1149e5341f4fe2b7e64a55e057478946546e176358bJason Monk    // Animations queued for next frame
1159e5341f4fe2b7e64a55e057478946546e176358bJason Monk    AnimationHandle mNextFrameAnimations;
1169e5341f4fe2b7e64a55e057478946546e176358bJason Monk
1179e5341f4fe2b7e64a55e057478946546e176358bJason Monk    nsecs_t mFrameTimeMs;
1189e5341f4fe2b7e64a55e057478946546e176358bJason Monk};
1199e5341f4fe2b7e64a55e057478946546e176358bJason Monk
1209e5341f4fe2b7e64a55e057478946546e176358bJason Monk} /* namespace uirenderer */
1219e5341f4fe2b7e64a55e057478946546e176358bJason Monk} /* namespace android */
1229e5341f4fe2b7e64a55e057478946546e176358bJason Monk
1239e5341f4fe2b7e64a55e057478946546e176358bJason Monk#endif /* TREEANIMATIONTRACKER_H_ */
1249e5341f4fe2b7e64a55e057478946546e176358bJason Monk