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 | |
| 20 | #include "RenderNode.h" |
| 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | using namespace std; |
| 26 | |
| 27 | static void unref(BaseRenderNodeAnimator* animator) { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 28 | animator->detach(); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 29 | animator->decStrong(0); |
| 30 | } |
| 31 | |
| 32 | AnimatorManager::AnimatorManager(RenderNode& parent) |
| 33 | : mParent(parent) { |
| 34 | } |
| 35 | |
| 36 | AnimatorManager::~AnimatorManager() { |
| 37 | for_each(mNewAnimators.begin(), mNewAnimators.end(), unref); |
| 38 | for_each(mAnimators.begin(), mAnimators.end(), unref); |
| 39 | } |
| 40 | |
| 41 | void AnimatorManager::addAnimator(const sp<BaseRenderNodeAnimator>& animator) { |
| 42 | animator->incStrong(0); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 43 | animator->attach(&mParent); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 44 | mNewAnimators.push_back(animator.get()); |
| 45 | } |
| 46 | |
| 47 | template<typename T> |
| 48 | static 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 | |
| 56 | void 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 Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 62 | (*it)->pushStaging(info); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | class AnimateFunctor { |
| 67 | public: |
| 68 | AnimateFunctor(RenderNode& target, TreeInfo& info) |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 69 | : dirtyMask(0), mTarget(target), mInfo(info) {} |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 70 | |
| 71 | bool operator() (BaseRenderNodeAnimator* animator) { |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 72 | dirtyMask |= animator->dirtyMask(); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 73 | bool remove = animator->animate(mInfo); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 74 | if (remove) { |
| 75 | animator->decStrong(0); |
| 76 | } |
| 77 | return remove; |
| 78 | } |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 79 | |
| 80 | uint32_t dirtyMask; |
| 81 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 82 | private: |
| 83 | RenderNode& mTarget; |
| 84 | TreeInfo& mInfo; |
| 85 | }; |
| 86 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 87 | uint32_t AnimatorManager::animate(TreeInfo& info) { |
| 88 | if (!mAnimators.size()) return 0; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 89 | |
| 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 Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 104 | |
| 105 | return functor.dirtyMask; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } /* namespace uirenderer */ |
| 109 | } /* namespace android */ |