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