blob: 7221295a4c846a7febe9d3e1ff517fc3f202063c [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
20#include "RenderNode.h"
21
22namespace android {
23namespace uirenderer {
24
25using namespace std;
26
27static void unref(BaseRenderNodeAnimator* animator) {
John Reck8d8af3c2014-07-01 15:23:45 -070028 animator->detach();
John Reck68bfe0a2014-06-24 15:34:58 -070029 animator->decStrong(0);
30}
31
32AnimatorManager::AnimatorManager(RenderNode& parent)
33 : mParent(parent) {
34}
35
36AnimatorManager::~AnimatorManager() {
37 for_each(mNewAnimators.begin(), mNewAnimators.end(), unref);
38 for_each(mAnimators.begin(), mAnimators.end(), unref);
39}
40
41void AnimatorManager::addAnimator(const sp<BaseRenderNodeAnimator>& animator) {
42 animator->incStrong(0);
John Reck8d8af3c2014-07-01 15:23:45 -070043 animator->attach(&mParent);
John Reck68bfe0a2014-06-24 15:34:58 -070044 mNewAnimators.push_back(animator.get());
45}
46
47template<typename T>
48static void move_all(T& source, T& dest) {
49 dest.reserve(source.size() + dest.size());
50 for (typename T::iterator it = source.begin(); it != source.end(); it++) {
51 dest.push_back(*it);
52 }
53 source.clear();
54}
55
56void AnimatorManager::pushStaging(TreeInfo& info) {
57 if (mNewAnimators.size()) {
58 // Since this is a straight move, we don't need to inc/dec the ref count
59 move_all(mNewAnimators, mAnimators);
60 }
61 for (vector<BaseRenderNodeAnimator*>::iterator it = mAnimators.begin(); it != mAnimators.end(); it++) {
John Reck8d8af3c2014-07-01 15:23:45 -070062 (*it)->pushStaging(info);
John Reck68bfe0a2014-06-24 15:34:58 -070063 }
64}
65
66class AnimateFunctor {
67public:
68 AnimateFunctor(RenderNode& target, TreeInfo& info)
John Recka7c2ea22014-08-08 13:21:00 -070069 : dirtyMask(0), mTarget(target), mInfo(info) {}
John Reck68bfe0a2014-06-24 15:34:58 -070070
71 bool operator() (BaseRenderNodeAnimator* animator) {
John Recka7c2ea22014-08-08 13:21:00 -070072 dirtyMask |= animator->dirtyMask();
John Reck8d8af3c2014-07-01 15:23:45 -070073 bool remove = animator->animate(mInfo);
John Reck68bfe0a2014-06-24 15:34:58 -070074 if (remove) {
75 animator->decStrong(0);
76 }
77 return remove;
78 }
John Recka7c2ea22014-08-08 13:21:00 -070079
80 uint32_t dirtyMask;
81
John Reck68bfe0a2014-06-24 15:34:58 -070082private:
83 RenderNode& mTarget;
84 TreeInfo& mInfo;
85};
86
John Recka7c2ea22014-08-08 13:21:00 -070087uint32_t AnimatorManager::animate(TreeInfo& info) {
88 if (!mAnimators.size()) return 0;
John Reck68bfe0a2014-06-24 15:34:58 -070089
90 // TODO: Can we target this better? For now treat it like any other staging
91 // property push and just damage self before and after animators are run
92
93 mParent.damageSelf(info);
94 info.damageAccumulator->popTransform();
95
96 AnimateFunctor functor(mParent, info);
97 std::vector< BaseRenderNodeAnimator* >::iterator newEnd;
98 newEnd = std::remove_if(mAnimators.begin(), mAnimators.end(), functor);
99 mAnimators.erase(newEnd, mAnimators.end());
100
101 mParent.mProperties.updateMatrix();
102 info.damageAccumulator->pushTransform(&mParent);
103 mParent.damageSelf(info);
John Recka7c2ea22014-08-08 13:21:00 -0700104
105 return functor.dirtyMask;
John Reck68bfe0a2014-06-24 15:34:58 -0700106}
107
108} /* namespace uirenderer */
109} /* namespace android */