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