John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 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 | |
John Reck | ec845a2 | 2014-09-05 15:23:38 -0700 | [diff] [blame] | 23 | #include "TreeInfo.h" |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 24 | #include "renderthread/TimeLord.h" |
| 25 | #include "utils/Macros.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | class AnimationContext; |
| 31 | class AnimationListener; |
| 32 | class BaseRenderNodeAnimator; |
| 33 | class RenderNode; |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 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 | */ |
| 44 | class AnimationHandle { |
| 45 | PREVENT_COPY_AND_ASSIGN(AnimationHandle); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 46 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 47 | public: |
| 48 | AnimationContext& context() { return mContext; } |
| 49 | |
John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 50 | // Called by the RenderNode when it has internally pulsed its own animations |
| 51 | // this frame and does not need to be run again this frame. |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 52 | void notifyAnimationsRan(); |
| 53 | |
John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 54 | // Stops tracking the RenderNode and destroys the handle. The node must be |
| 55 | // re-attached to the AnimationContext to receive managed animation |
| 56 | // pulses. |
| 57 | void release(); |
| 58 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 59 | private: |
| 60 | friend class AnimationContext; |
Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 61 | explicit AnimationHandle(AnimationContext& context); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 62 | AnimationHandle(RenderNode& animatingNode, AnimationContext& context); |
| 63 | ~AnimationHandle(); |
| 64 | |
| 65 | void insertAfter(AnimationHandle* prev); |
| 66 | void removeFromList(); |
| 67 | |
| 68 | sp<RenderNode> mRenderNode; |
| 69 | |
| 70 | AnimationContext& mContext; |
| 71 | |
| 72 | AnimationHandle* mPreviousHandle; |
| 73 | AnimationHandle* mNextHandle; |
| 74 | }; |
| 75 | |
| 76 | class AnimationContext { |
| 77 | PREVENT_COPY_AND_ASSIGN(AnimationContext); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 79 | public: |
Chih-Hung Hsieh | faecb78 | 2016-07-21 11:23:06 -0700 | [diff] [blame] | 80 | ANDROID_API explicit AnimationContext(renderthread::TimeLord& clock); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 81 | ANDROID_API virtual ~AnimationContext(); |
| 82 | |
| 83 | nsecs_t frameTimeMs() { return mFrameTimeMs; } |
| 84 | bool hasAnimations() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 85 | return mCurrentFrameAnimations.mNextHandle || mNextFrameAnimations.mNextHandle; |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Will always add to the next frame list, which is swapped when |
| 89 | // startFrame() is called |
| 90 | ANDROID_API void addAnimatingRenderNode(RenderNode& node); |
| 91 | |
| 92 | // Marks the start of a frame, which will update the frame time and move all |
| 93 | // next frame animations into the current frame |
John Reck | ec845a2 | 2014-09-05 15:23:38 -0700 | [diff] [blame] | 94 | ANDROID_API virtual void startFrame(TreeInfo::TraversalMode mode); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 95 | |
| 96 | // Runs any animations still left in mCurrentFrameAnimations that were not run |
| 97 | // as part of the standard RenderNode:prepareTree pass. |
| 98 | ANDROID_API virtual void runRemainingAnimations(TreeInfo& info); |
| 99 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 100 | ANDROID_API virtual void callOnFinished(BaseRenderNodeAnimator* animator, |
| 101 | AnimationListener* listener); |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 102 | |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 103 | ANDROID_API virtual void destroy(); |
| 104 | |
Doris Liu | c82e879 | 2016-07-29 16:45:24 -0700 | [diff] [blame] | 105 | ANDROID_API virtual void pauseAnimators() {} |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 106 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 107 | private: |
| 108 | friend class AnimationHandle; |
| 109 | void addAnimationHandle(AnimationHandle* handle); |
| 110 | |
| 111 | renderthread::TimeLord& mClock; |
| 112 | |
| 113 | // Animations left to run this frame, at the end of the frame this should |
| 114 | // be null |
| 115 | AnimationHandle mCurrentFrameAnimations; |
| 116 | // Animations queued for next frame |
| 117 | AnimationHandle mNextFrameAnimations; |
| 118 | |
| 119 | nsecs_t mFrameTimeMs; |
| 120 | }; |
| 121 | |
| 122 | } /* namespace uirenderer */ |
| 123 | } /* namespace android */ |
| 124 | |
| 125 | #endif /* TREEANIMATIONTRACKER_H_ */ |