blob: 4826d5a0c8da947f7ae928bc19281a4446e6bf7e [file] [log] [blame]
John Reck68bfe0a2014-06-24 15:34:58 -07001/*
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 Reck119907c2014-08-14 09:02:01 -070020#include "AnimationContext.h"
John Reck1bcacfd2017-11-03 10:12:19 -070021#include "Animator.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040022#include "DamageAccumulator.h"
John Reck68bfe0a2014-06-24 15:34:58 -070023#include "RenderNode.h"
24
25namespace android {
26namespace uirenderer {
27
28using namespace std;
29
Doris Liuc4bb1852016-02-19 21:39:21 +000030static void detach(sp<BaseRenderNodeAnimator>& animator) {
John Reck8d8af3c2014-07-01 15:23:45 -070031 animator->detach();
John Reck68bfe0a2014-06-24 15:34:58 -070032}
33
John Reck1bcacfd2017-11-03 10:12:19 -070034AnimatorManager::AnimatorManager(RenderNode& parent) : mParent(parent), mAnimationHandle(nullptr) {}
John Reck68bfe0a2014-06-24 15:34:58 -070035
36AnimatorManager::~AnimatorManager() {
Doris Liuc4bb1852016-02-19 21:39:21 +000037 for_each(mNewAnimators.begin(), mNewAnimators.end(), detach);
38 for_each(mAnimators.begin(), mAnimators.end(), detach);
John Reck68bfe0a2014-06-24 15:34:58 -070039}
40
41void AnimatorManager::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
Doris Liu8b083202016-02-19 21:46:06 +000042 RenderNode* stagingTarget = animator->stagingTarget();
43 if (stagingTarget == &mParent) {
44 return;
45 }
Doris Liuc4bb1852016-02-19 21:39:21 +000046 mNewAnimators.emplace_back(animator.get());
Doris Liu8b083202016-02-19 21:46:06 +000047 // 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
56void AnimatorManager::removeAnimator(const sp<BaseRenderNodeAnimator>& animator) {
57 mNewAnimators.erase(std::remove(mNewAnimators.begin(), mNewAnimators.end(), animator),
John Reck1bcacfd2017-11-03 10:12:19 -070058 mNewAnimators.end());
John Reck68bfe0a2014-06-24 15:34:58 -070059}
60
John Reck119907c2014-08-14 09:02:01 -070061void AnimatorManager::setAnimationHandle(AnimationHandle* handle) {
62 LOG_ALWAYS_FATAL_IF(mAnimationHandle && handle, "Already have an AnimationHandle!");
63 mAnimationHandle = handle;
John Recke2478d42014-09-03 16:46:05 -070064 LOG_ALWAYS_FATAL_IF(!mAnimationHandle && mAnimators.size(),
John Reck1bcacfd2017-11-03 10:12:19 -070065 "Lost animation handle on %p (%s) with outstanding animators!", &mParent,
66 mParent.getName());
John Reck119907c2014-08-14 09:02:01 -070067}
68
John Reck119907c2014-08-14 09:02:01 -070069void AnimatorManager::pushStaging() {
John Reck68bfe0a2014-06-24 15:34:58 -070070 if (mNewAnimators.size()) {
John Reckd2080d52017-09-25 14:22:40 -070071 if (CC_UNLIKELY(!mAnimationHandle)) {
John Reck1bcacfd2017-11-03 10:12:19 -070072 ALOGW("Trying to start new animators on %p (%s) without an animation handle!", &mParent,
73 mParent.getName());
John Reckd2080d52017-09-25 14:22:40 -070074 return;
75 }
Doris Liuc4bb1852016-02-19 21:39:21 +000076
Doris Liu8b083202016-02-19 21:46:06 +000077 // 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 Liuc4bb1852016-02-19 21:39:21 +000081 }
Doris Liuc4bb1852016-02-19 21:39:21 +000082 }
83 mNewAnimators.clear();
John Reck68bfe0a2014-06-24 15:34:58 -070084 }
Doris Liuc4bb1852016-02-19 21:39:21 +000085 for (auto& animator : mAnimators) {
86 animator->pushStaging(mAnimationHandle->context());
John Reck68bfe0a2014-06-24 15:34:58 -070087 }
88}
89
Doris Liu8b083202016-02-19 21:46:06 +000090void 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 Reck68bfe0a2014-06-24 15:34:58 -070095class AnimateFunctor {
96public:
John Reck9e066cb2016-02-29 13:40:52 -080097 AnimateFunctor(TreeInfo& info, AnimationContext& context, uint32_t* outDirtyMask)
98 : mInfo(info), mContext(context), mDirtyMask(outDirtyMask) {}
John Reck68bfe0a2014-06-24 15:34:58 -070099
John Reck1bcacfd2017-11-03 10:12:19 -0700100 bool operator()(sp<BaseRenderNodeAnimator>& animator) {
John Reck9e066cb2016-02-29 13:40:52 -0800101 *mDirtyMask |= animator->dirtyMask();
John Reck119907c2014-08-14 09:02:01 -0700102 bool remove = animator->animate(mContext);
John Reck68bfe0a2014-06-24 15:34:58 -0700103 if (remove) {
Doris Liuc4bb1852016-02-19 21:39:21 +0000104 animator->detach();
John Reck119907c2014-08-14 09:02:01 -0700105 } else {
106 if (animator->isRunning()) {
107 mInfo.out.hasAnimations = true;
108 }
John Reckf5945a02014-09-05 15:57:47 -0700109 if (CC_UNLIKELY(!animator->mayRunAsync())) {
110 mInfo.out.requiresUiRedraw = true;
111 }
John Reck68bfe0a2014-06-24 15:34:58 -0700112 }
113 return remove;
114 }
John Recka7c2ea22014-08-08 13:21:00 -0700115
John Reck68bfe0a2014-06-24 15:34:58 -0700116private:
John Reck68bfe0a2014-06-24 15:34:58 -0700117 TreeInfo& mInfo;
John Reck119907c2014-08-14 09:02:01 -0700118 AnimationContext& mContext;
John Reck9e066cb2016-02-29 13:40:52 -0800119 uint32_t* mDirtyMask;
John Reck68bfe0a2014-06-24 15:34:58 -0700120};
121
John Recka7c2ea22014-08-08 13:21:00 -0700122uint32_t AnimatorManager::animate(TreeInfo& info) {
123 if (!mAnimators.size()) return 0;
John Reck68bfe0a2014-06-24 15:34:58 -0700124
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 Reck119907c2014-08-14 09:02:01 -0700131 uint32_t dirty = animateCommon(info);
John Reck68bfe0a2014-06-24 15:34:58 -0700132
John Reck68bfe0a2014-06-24 15:34:58 -0700133 info.damageAccumulator->pushTransform(&mParent);
134 mParent.damageSelf(info);
John Recka7c2ea22014-08-08 13:21:00 -0700135
John Reck119907c2014-08-14 09:02:01 -0700136 return dirty;
137}
138
139void AnimatorManager::animateNoDamage(TreeInfo& info) {
John Reck119907c2014-08-14 09:02:01 -0700140 animateCommon(info);
141}
142
143uint32_t AnimatorManager::animateCommon(TreeInfo& info) {
George Burgess IVa4831732017-01-10 15:33:57 -0800144 uint32_t dirtyMask = 0;
John Reck9e066cb2016-02-29 13:40:52 -0800145 AnimateFunctor functor(info, mAnimationHandle->context(), &dirtyMask);
Doris Liuc4bb1852016-02-19 21:39:21 +0000146 auto newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor);
John Reck119907c2014-08-14 09:02:01 -0700147 mAnimators.erase(newEnd, mAnimators.end());
148 mAnimationHandle->notifyAnimationsRan();
John Reck49dec432015-07-23 15:33:12 -0700149 mParent.mProperties.updateMatrix();
John Reck9e066cb2016-02-29 13:40:52 -0800150 return dirtyMask;
John Reck68bfe0a2014-06-24 15:34:58 -0700151}
152
Doris Liuc4bb1852016-02-19 21:39:21 +0000153static void endStagingAnimator(sp<BaseRenderNodeAnimator>& animator) {
154 animator->cancel();
John Reck119907c2014-08-14 09:02:01 -0700155 if (animator->listener()) {
Doris Liuc4bb1852016-02-19 21:39:21 +0000156 animator->listener()->onAnimationFinished(animator.get());
John Reck119907c2014-08-14 09:02:01 -0700157 }
John Reck119907c2014-08-14 09:02:01 -0700158}
159
John Recke2478d42014-09-03 16:46:05 -0700160void 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
168class EndActiveAnimatorsFunctor {
169public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -0700170 explicit EndActiveAnimatorsFunctor(AnimationContext& context) : mContext(context) {}
John Recke2478d42014-09-03 16:46:05 -0700171
John Reck1bcacfd2017-11-03 10:12:19 -0700172 void operator()(sp<BaseRenderNodeAnimator>& animator) { animator->forceEndNow(mContext); }
John Recke2478d42014-09-03 16:46:05 -0700173
174private:
175 AnimationContext& mContext;
176};
177
178void AnimatorManager::endAllActiveAnimators() {
John Reck1bcacfd2017-11-03 10:12:19 -0700179 ALOGD("endAllActiveAnimators on %p (%s) with handle %p", &mParent, mParent.getName(),
180 mAnimationHandle);
John Recke2478d42014-09-03 16:46:05 -0700181 EndActiveAnimatorsFunctor functor(mAnimationHandle->context());
182 for_each(mAnimators.begin(), mAnimators.end(), functor);
183 mAnimators.clear();
184 mAnimationHandle->release();
John Reck119907c2014-08-14 09:02:01 -0700185}
186
John Reck68bfe0a2014-06-24 15:34:58 -0700187} /* namespace uirenderer */
188} /* namespace android */