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