| 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 | #include "AnimationContext.h" | 
|  | 17 |  | 
|  | 18 | #include "Animator.h" | 
|  | 19 | #include "RenderNode.h" | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 20 | #include "renderthread/TimeLord.h" | 
|  | 21 |  | 
|  | 22 | namespace android { | 
|  | 23 | namespace uirenderer { | 
|  | 24 |  | 
|  | 25 | AnimationContext::AnimationContext(renderthread::TimeLord& clock) | 
|  | 26 | : mClock(clock) | 
|  | 27 | , mCurrentFrameAnimations(*this) | 
|  | 28 | , mNextFrameAnimations(*this) | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 29 | , mFrameTimeMs(0) {} | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 30 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 31 | AnimationContext::~AnimationContext() {} | 
| John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | void AnimationContext::destroy() { | 
| John Reck | ec845a2 | 2014-09-05 15:23:38 -0700 | [diff] [blame] | 34 | startFrame(TreeInfo::MODE_RT_ONLY); | 
| John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 35 | while (mCurrentFrameAnimations.mNextHandle) { | 
|  | 36 | AnimationHandle* current = mCurrentFrameAnimations.mNextHandle; | 
|  | 37 | AnimatorManager& animators = current->mRenderNode->animators(); | 
| John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 38 | animators.endAllActiveAnimators(); | 
| John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 39 | LOG_ALWAYS_FATAL_IF(mCurrentFrameAnimations.mNextHandle == current, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 40 | "endAllAnimators failed to remove from current frame list!"); | 
| John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 41 | } | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
|  | 44 | void AnimationContext::addAnimatingRenderNode(RenderNode& node) { | 
|  | 45 | if (!node.animators().hasAnimationHandle()) { | 
|  | 46 | AnimationHandle* handle = new AnimationHandle(node, *this); | 
|  | 47 | addAnimationHandle(handle); | 
|  | 48 | } | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | void AnimationContext::addAnimationHandle(AnimationHandle* handle) { | 
|  | 52 | handle->insertAfter(&mNextFrameAnimations); | 
|  | 53 | } | 
|  | 54 |  | 
| Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 55 | void AnimationContext::startFrame(TreeInfo::TraversalMode mode) { | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 56 | LOG_ALWAYS_FATAL_IF(mCurrentFrameAnimations.mNextHandle, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 57 | "Missed running animations last frame!"); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 58 | AnimationHandle* head = mNextFrameAnimations.mNextHandle; | 
|  | 59 | if (head) { | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 60 | mNextFrameAnimations.mNextHandle = nullptr; | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 61 | mCurrentFrameAnimations.mNextHandle = head; | 
|  | 62 | head->mPreviousHandle = &mCurrentFrameAnimations; | 
|  | 63 | } | 
| John Reck | 501ff9a | 2016-06-17 12:57:12 -0700 | [diff] [blame] | 64 | mFrameTimeMs = ns2ms(mClock.latestVsync()); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
|  | 67 | void AnimationContext::runRemainingAnimations(TreeInfo& info) { | 
|  | 68 | while (mCurrentFrameAnimations.mNextHandle) { | 
|  | 69 | AnimationHandle* current = mCurrentFrameAnimations.mNextHandle; | 
|  | 70 | AnimatorManager& animators = current->mRenderNode->animators(); | 
|  | 71 | animators.pushStaging(); | 
|  | 72 | animators.animateNoDamage(info); | 
|  | 73 | LOG_ALWAYS_FATAL_IF(mCurrentFrameAnimations.mNextHandle == current, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 74 | "Animate failed to remove from current frame list!"); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 75 | } | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | void AnimationContext::callOnFinished(BaseRenderNodeAnimator* animator, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 79 | AnimationListener* listener) { | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 80 | listener->onAnimationFinished(animator); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | AnimationHandle::AnimationHandle(AnimationContext& context) | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 84 | : mContext(context), mPreviousHandle(nullptr), mNextHandle(nullptr) {} | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 85 |  | 
|  | 86 | AnimationHandle::AnimationHandle(RenderNode& animatingNode, AnimationContext& context) | 
|  | 87 | : mRenderNode(&animatingNode) | 
|  | 88 | , mContext(context) | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 89 | , mPreviousHandle(nullptr) | 
|  | 90 | , mNextHandle(nullptr) { | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 91 | mRenderNode->animators().setAnimationHandle(this); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | AnimationHandle::~AnimationHandle() { | 
|  | 95 | LOG_ALWAYS_FATAL_IF(mPreviousHandle || mNextHandle, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 96 | "AnimationHandle destroyed while still animating!"); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
|  | 99 | void AnimationHandle::notifyAnimationsRan() { | 
|  | 100 | removeFromList(); | 
|  | 101 | if (mRenderNode->animators().hasAnimators()) { | 
|  | 102 | mContext.addAnimationHandle(this); | 
|  | 103 | } else { | 
| John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 104 | release(); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 105 | } | 
|  | 106 | } | 
|  | 107 |  | 
| John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 108 | void AnimationHandle::release() { | 
|  | 109 | LOG_ALWAYS_FATAL_IF(mRenderNode->animators().hasAnimators(), | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 110 | "Releasing the handle for an RenderNode with outstanding animators!"); | 
| John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 111 | removeFromList(); | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 112 | mRenderNode->animators().setAnimationHandle(nullptr); | 
| John Reck | d0cd9db | 2014-08-28 08:43:39 -0700 | [diff] [blame] | 113 | delete this; | 
|  | 114 | } | 
|  | 115 |  | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 116 | void AnimationHandle::insertAfter(AnimationHandle* prev) { | 
|  | 117 | removeFromList(); | 
|  | 118 | mNextHandle = prev->mNextHandle; | 
|  | 119 | if (mNextHandle) { | 
|  | 120 | mNextHandle->mPreviousHandle = this; | 
|  | 121 | } | 
|  | 122 | prev->mNextHandle = this; | 
|  | 123 | mPreviousHandle = prev; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | void AnimationHandle::removeFromList() { | 
|  | 127 | if (mPreviousHandle) { | 
|  | 128 | mPreviousHandle->mNextHandle = mNextHandle; | 
|  | 129 | } | 
|  | 130 | if (mNextHandle) { | 
|  | 131 | mNextHandle->mPreviousHandle = mPreviousHandle; | 
|  | 132 | } | 
| Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 133 | mPreviousHandle = nullptr; | 
|  | 134 | mNextHandle = nullptr; | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
|  | 137 | } /* namespace uirenderer */ | 
|  | 138 | } /* namespace android */ |