Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | #include "PropertyValuesAnimatorSet.h" |
| 18 | #include "RenderNode.h" |
| 19 | |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | void PropertyValuesAnimatorSet::addPropertyAnimator(PropertyValuesHolder* propertyValuesHolder, |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 26 | Interpolator* interpolator, nsecs_t startDelay, nsecs_t duration, int repeatCount, |
| 27 | RepeatMode repeatMode) { |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 28 | |
| 29 | PropertyAnimator* animator = new PropertyAnimator(propertyValuesHolder, |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 30 | interpolator, startDelay, duration, repeatCount, repeatMode); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 31 | mAnimators.emplace_back(animator); |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 32 | |
| 33 | // Check whether any child animator is infinite after adding it them to the set. |
| 34 | if (repeatCount == -1) { |
| 35 | mIsInfinite = true; |
| 36 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | PropertyValuesAnimatorSet::PropertyValuesAnimatorSet() |
| 40 | : BaseRenderNodeAnimator(1.0f) { |
| 41 | setStartValue(0); |
| 42 | mLastFraction = 0.0f; |
| 43 | setInterpolator(new LinearInterpolator()); |
Doris Liu | c470466 | 2016-06-23 10:34:17 -0700 | [diff] [blame] | 44 | setListener(new PropertyAnimatorSetListener(this)); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void PropertyValuesAnimatorSet::onFinished(BaseRenderNodeAnimator* animator) { |
| 48 | if (mOneShotListener.get()) { |
| 49 | mOneShotListener->onAnimationFinished(animator); |
| 50 | mOneShotListener = nullptr; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | float PropertyValuesAnimatorSet::getValue(RenderNode* target) const { |
| 55 | return mLastFraction; |
| 56 | } |
| 57 | |
| 58 | void PropertyValuesAnimatorSet::setValue(RenderNode* target, float value) { |
| 59 | mLastFraction = value; |
| 60 | } |
| 61 | |
| 62 | void PropertyValuesAnimatorSet::onPlayTimeChanged(nsecs_t playTime) { |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 63 | if (playTime == 0 && mDuration > 0) { |
| 64 | // Reset all the animators |
| 65 | for (auto it = mAnimators.rbegin(); it != mAnimators.rend(); it++) { |
| 66 | // Note that this set may containing animators modifying the same property, so when we |
| 67 | // reset the animators, we need to make sure the animators that end the first will |
| 68 | // have the final say on what the property value should be. |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 69 | (*it)->setFraction(0, 0); |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 70 | } |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 71 | } else { |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 72 | for (auto& anim : mAnimators) { |
| 73 | anim->setCurrentPlayTime(playTime); |
| 74 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 78 | void PropertyValuesAnimatorSet::start(AnimationListener* listener) { |
| 79 | init(); |
| 80 | mOneShotListener = listener; |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 81 | mRequestId++; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 82 | BaseRenderNodeAnimator::start(); |
| 83 | } |
| 84 | |
| 85 | void PropertyValuesAnimatorSet::reverse(AnimationListener* listener) { |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 86 | init(); |
| 87 | mOneShotListener = listener; |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 88 | mRequestId++; |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 89 | BaseRenderNodeAnimator::reverse(); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Doris Liu | 718cd3e | 2016-05-17 16:50:31 -0700 | [diff] [blame] | 92 | void PropertyValuesAnimatorSet::reset() { |
| 93 | mRequestId++; |
| 94 | BaseRenderNodeAnimator::reset(); |
| 95 | } |
| 96 | |
| 97 | void PropertyValuesAnimatorSet::end() { |
| 98 | mRequestId++; |
| 99 | BaseRenderNodeAnimator::end(); |
| 100 | } |
| 101 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 102 | void PropertyValuesAnimatorSet::init() { |
| 103 | if (mInitialized) { |
| 104 | return; |
| 105 | } |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 106 | |
| 107 | // Sort the animators by their total duration. Note that all the animators in the set start at |
| 108 | // the same time, so the ones with longer total duration (which includes start delay) will |
| 109 | // be the ones that end later. |
| 110 | std::sort(mAnimators.begin(), mAnimators.end(), [](auto& a, auto&b) { |
| 111 | return a->getTotalDuration() < b->getTotalDuration(); |
| 112 | }); |
Doris Liu | c470466 | 2016-06-23 10:34:17 -0700 | [diff] [blame] | 113 | mDuration = mAnimators.empty() ? 0 : mAnimators[mAnimators.size() - 1]->getTotalDuration(); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 114 | mInitialized = true; |
| 115 | } |
| 116 | |
| 117 | uint32_t PropertyValuesAnimatorSet::dirtyMask() { |
| 118 | return RenderNode::DISPLAY_LIST; |
| 119 | } |
| 120 | |
| 121 | PropertyAnimator::PropertyAnimator(PropertyValuesHolder* holder, Interpolator* interpolator, |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 122 | nsecs_t startDelay, nsecs_t duration, int repeatCount, |
| 123 | RepeatMode repeatMode) |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 124 | : mPropertyValuesHolder(holder), mInterpolator(interpolator), mStartDelay(startDelay), |
| 125 | mDuration(duration) { |
| 126 | if (repeatCount < 0) { |
| 127 | mRepeatCount = UINT32_MAX; |
| 128 | } else { |
| 129 | mRepeatCount = repeatCount; |
| 130 | } |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 131 | mRepeatMode = repeatMode; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 132 | mTotalDuration = ((nsecs_t) mRepeatCount + 1) * mDuration + mStartDelay; |
| 133 | } |
| 134 | |
| 135 | void PropertyAnimator::setCurrentPlayTime(nsecs_t playTime) { |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 136 | if (playTime < mStartDelay) { |
| 137 | return; |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 138 | } |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 139 | |
| 140 | float currentIterationFraction; |
| 141 | long iteration; |
| 142 | if (playTime >= mTotalDuration) { |
| 143 | // Reached the end of the animation. |
| 144 | iteration = mRepeatCount; |
| 145 | currentIterationFraction = 1.0f; |
| 146 | } else { |
| 147 | // play time here is in range [mStartDelay, mTotalDuration) |
| 148 | iteration = (playTime - mStartDelay) / mDuration; |
| 149 | currentIterationFraction = ((playTime - mStartDelay) % mDuration) / (float) mDuration; |
| 150 | } |
| 151 | setFraction(currentIterationFraction, iteration); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Doris Liu | f7167e8 | 2016-08-03 17:54:28 -0700 | [diff] [blame^] | 154 | void PropertyAnimator::setFraction(float fraction, long iteration) { |
| 155 | double totalFraction = fraction + iteration; |
| 156 | // This makes sure we only set the fraction = repeatCount + 1 once. It is needed because there |
| 157 | // might be another animator modifying the same property after this animator finishes, we need |
| 158 | // to make sure we don't set conflicting values on the same property within one frame. |
| 159 | if ((mLatestFraction == mRepeatCount + 1) && (totalFraction >= mRepeatCount + 1)) { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | mLatestFraction = totalFraction; |
| 164 | // Check the play direction (i.e. reverse or restart) every other iteration, and calculate the |
| 165 | // fraction based on the play direction. |
| 166 | if (iteration % 2 && mRepeatMode == RepeatMode::Reverse) { |
| 167 | fraction = 1.0f - fraction; |
| 168 | } |
Doris Liu | c4bb185 | 2016-02-19 21:39:21 +0000 | [diff] [blame] | 169 | float interpolatedFraction = mInterpolator->interpolate(fraction); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 170 | mPropertyValuesHolder->setFraction(interpolatedFraction); |
| 171 | } |
| 172 | |
| 173 | void PropertyAnimatorSetListener::onAnimationFinished(BaseRenderNodeAnimator* animator) { |
| 174 | mSet->onFinished(animator); |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | } |