| John Reck | 68bfe0a | 2014-06-24 15:34:58 -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 "AnimatorManager.h" | 
|  | 17 |  | 
|  | 18 | #include <algorithm> | 
|  | 19 |  | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 20 | #include "AnimationContext.h" | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 21 | #include "Animator.h" | 
| Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 22 | #include "DamageAccumulator.h" | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 23 | #include "RenderNode.h" | 
|  | 24 |  | 
|  | 25 | namespace android { | 
|  | 26 | namespace uirenderer { | 
|  | 27 |  | 
|  | 28 | using namespace std; | 
|  | 29 |  | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 30 | static void detach(sp<BaseRenderNodeAnimator>& animator) { | 
| John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 31 | animator->detach(); | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 32 | } | 
|  | 33 |  | 
| Nader Jawad | 528b0d2 | 2022-05-10 13:58:36 -0700 | [diff] [blame] | 34 | AnimatorManager::AnimatorManager(RenderNode& parent) | 
|  | 35 | : mParent(parent), mAnimationHandle(nullptr), mCancelAllAnimators(false) {} | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 36 |  | 
|  | 37 | AnimatorManager::~AnimatorManager() { | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 38 | for_each(mNewAnimators.begin(), mNewAnimators.end(), detach); | 
|  | 39 | for_each(mAnimators.begin(), mAnimators.end(), detach); | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 40 | } | 
|  | 41 |  | 
|  | 42 | void AnimatorManager::addAnimator(const sp<BaseRenderNodeAnimator>& animator) { | 
| Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 43 | RenderNode* stagingTarget = animator->stagingTarget(); | 
|  | 44 | if (stagingTarget == &mParent) { | 
|  | 45 | return; | 
|  | 46 | } | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 47 | mNewAnimators.emplace_back(animator.get()); | 
| Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 48 | // If the animator is already attached to other RenderNode, remove it from that RenderNode's | 
|  | 49 | // new animator list. This ensures one animator only ends up in one newAnimatorList during one | 
|  | 50 | // frame, even when it's added multiple times to multiple targets. | 
|  | 51 | if (stagingTarget) { | 
|  | 52 | stagingTarget->removeAnimator(animator); | 
|  | 53 | } | 
|  | 54 | animator->attach(&mParent); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | void AnimatorManager::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) { | 
|  | 58 | mNewAnimators.erase(std::remove(mNewAnimators.begin(), mNewAnimators.end(), animator), | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 59 | mNewAnimators.end()); | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 62 | void AnimatorManager::setAnimationHandle(AnimationHandle* handle) { | 
|  | 63 | LOG_ALWAYS_FATAL_IF(mAnimationHandle && handle, "Already have an AnimationHandle!"); | 
|  | 64 | mAnimationHandle = handle; | 
| John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 65 | LOG_ALWAYS_FATAL_IF(!mAnimationHandle && mAnimators.size(), | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 66 | "Lost animation handle on %p (%s) with outstanding animators!", &mParent, | 
|  | 67 | mParent.getName()); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 70 | void AnimatorManager::pushStaging() { | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 71 | if (mNewAnimators.size()) { | 
| John Reck | d2080d5 | 2017-09-25 14:22:40 -0700 | [diff] [blame] | 72 | if (CC_UNLIKELY(!mAnimationHandle)) { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 73 | ALOGW("Trying to start new animators on %p (%s) without an animation handle!", &mParent, | 
|  | 74 | mParent.getName()); | 
| John Reck | d2080d5 | 2017-09-25 14:22:40 -0700 | [diff] [blame] | 75 | return; | 
|  | 76 | } | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 77 |  | 
| Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 78 | // Only add new animators that are not already in the mAnimators list | 
|  | 79 | for (auto& anim : mNewAnimators) { | 
|  | 80 | if (anim->target() != &mParent) { | 
|  | 81 | mAnimators.push_back(std::move(anim)); | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 82 | } | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 83 | } | 
|  | 84 | mNewAnimators.clear(); | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 85 | } | 
| Nader Jawad | 528b0d2 | 2022-05-10 13:58:36 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | if (mCancelAllAnimators) { | 
|  | 88 | for (auto& animator : mAnimators) { | 
|  | 89 | animator->forceEndNow(mAnimationHandle->context()); | 
|  | 90 | } | 
|  | 91 | mCancelAllAnimators = false; | 
|  | 92 | } else { | 
|  | 93 | for (auto& animator : mAnimators) { | 
|  | 94 | animator->pushStaging(mAnimationHandle->context()); | 
|  | 95 | } | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 96 | } | 
|  | 97 | } | 
|  | 98 |  | 
| Doris Liu | 8b08320 | 2016-02-19 21:46:06 +0000 | [diff] [blame] | 99 | void AnimatorManager::onAnimatorTargetChanged(BaseRenderNodeAnimator* animator) { | 
|  | 100 | LOG_ALWAYS_FATAL_IF(animator->target() == &mParent, "Target has not been changed"); | 
|  | 101 | mAnimators.erase(std::remove(mAnimators.begin(), mAnimators.end(), animator), mAnimators.end()); | 
|  | 102 | } | 
|  | 103 |  | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 104 | class AnimateFunctor { | 
|  | 105 | public: | 
| John Reck | 9e066cb | 2016-02-29 13:40:52 -0800 | [diff] [blame] | 106 | AnimateFunctor(TreeInfo& info, AnimationContext& context, uint32_t* outDirtyMask) | 
|  | 107 | : mInfo(info), mContext(context), mDirtyMask(outDirtyMask) {} | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 108 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 109 | bool operator()(sp<BaseRenderNodeAnimator>& animator) { | 
| John Reck | 9e066cb | 2016-02-29 13:40:52 -0800 | [diff] [blame] | 110 | *mDirtyMask |= animator->dirtyMask(); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 111 | bool remove = animator->animate(mContext); | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 112 | if (remove) { | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 113 | animator->detach(); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 114 | } else { | 
|  | 115 | if (animator->isRunning()) { | 
|  | 116 | mInfo.out.hasAnimations = true; | 
|  | 117 | } | 
| John Reck | f5945a0 | 2014-09-05 15:57:47 -0700 | [diff] [blame] | 118 | if (CC_UNLIKELY(!animator->mayRunAsync())) { | 
|  | 119 | mInfo.out.requiresUiRedraw = true; | 
|  | 120 | } | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 121 | } | 
|  | 122 | return remove; | 
|  | 123 | } | 
| John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 124 |  | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 125 | private: | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 126 | TreeInfo& mInfo; | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 127 | AnimationContext& mContext; | 
| John Reck | 9e066cb | 2016-02-29 13:40:52 -0800 | [diff] [blame] | 128 | uint32_t* mDirtyMask; | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 129 | }; | 
|  | 130 |  | 
| John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 131 | uint32_t AnimatorManager::animate(TreeInfo& info) { | 
|  | 132 | if (!mAnimators.size()) return 0; | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 133 |  | 
|  | 134 | // TODO: Can we target this better? For now treat it like any other staging | 
|  | 135 | // property push and just damage self before and after animators are run | 
|  | 136 |  | 
|  | 137 | mParent.damageSelf(info); | 
|  | 138 | info.damageAccumulator->popTransform(); | 
|  | 139 |  | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 140 | uint32_t dirty = animateCommon(info); | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 141 |  | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 142 | info.damageAccumulator->pushTransform(&mParent); | 
|  | 143 | mParent.damageSelf(info); | 
| John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 144 |  | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 145 | return dirty; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | void AnimatorManager::animateNoDamage(TreeInfo& info) { | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 149 | animateCommon(info); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | uint32_t AnimatorManager::animateCommon(TreeInfo& info) { | 
| George Burgess IV | a483173 | 2017-01-10 15:33:57 -0800 | [diff] [blame] | 153 | uint32_t dirtyMask = 0; | 
| John Reck | 9e066cb | 2016-02-29 13:40:52 -0800 | [diff] [blame] | 154 | AnimateFunctor functor(info, mAnimationHandle->context(), &dirtyMask); | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 155 | auto newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 156 | mAnimators.erase(newEnd, mAnimators.end()); | 
|  | 157 | mAnimationHandle->notifyAnimationsRan(); | 
| John Reck | 49dec43 | 2015-07-23 15:33:12 -0700 | [diff] [blame] | 158 | mParent.mProperties.updateMatrix(); | 
| John Reck | 9e066cb | 2016-02-29 13:40:52 -0800 | [diff] [blame] | 159 | return dirtyMask; | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 160 | } | 
|  | 161 |  | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 162 | static void endStagingAnimator(sp<BaseRenderNodeAnimator>& animator) { | 
|  | 163 | animator->cancel(); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 164 | if (animator->listener()) { | 
| Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 165 | animator->listener()->onAnimationFinished(animator.get()); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 166 | } | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
| John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 169 | void AnimatorManager::endAllStagingAnimators() { | 
|  | 170 | ALOGD("endAllStagingAnimators on %p (%s)", &mParent, mParent.getName()); | 
|  | 171 | // This works because this state can only happen on the UI thread, | 
|  | 172 | // which means we're already on the right thread to invoke listeners | 
|  | 173 | for_each(mNewAnimators.begin(), mNewAnimators.end(), endStagingAnimator); | 
|  | 174 | mNewAnimators.clear(); | 
|  | 175 | } | 
|  | 176 |  | 
|  | 177 | class EndActiveAnimatorsFunctor { | 
|  | 178 | public: | 
| Chih-Hung Hsieh | c6baf56 | 2016-04-27 11:29:23 -0700 | [diff] [blame] | 179 | explicit EndActiveAnimatorsFunctor(AnimationContext& context) : mContext(context) {} | 
| John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 180 |  | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 181 | void operator()(sp<BaseRenderNodeAnimator>& animator) { animator->forceEndNow(mContext); } | 
| John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 182 |  | 
|  | 183 | private: | 
|  | 184 | AnimationContext& mContext; | 
|  | 185 | }; | 
|  | 186 |  | 
|  | 187 | void AnimatorManager::endAllActiveAnimators() { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 188 | ALOGD("endAllActiveAnimators on %p (%s) with handle %p", &mParent, mParent.getName(), | 
|  | 189 | mAnimationHandle); | 
| John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 190 | EndActiveAnimatorsFunctor functor(mAnimationHandle->context()); | 
|  | 191 | for_each(mAnimators.begin(), mAnimators.end(), functor); | 
|  | 192 | mAnimators.clear(); | 
|  | 193 | mAnimationHandle->release(); | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| Nader Jawad | 528b0d2 | 2022-05-10 13:58:36 -0700 | [diff] [blame] | 196 | void AnimatorManager::forceEndAnimators() { | 
|  | 197 | mCancelAllAnimators = true; | 
|  | 198 | } | 
|  | 199 |  | 
| John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 200 | } /* namespace uirenderer */ | 
|  | 201 | } /* namespace android */ |