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