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