blob: e3258e3c1a48f7e23f29210a83a6326ae3f81945 [file] [log] [blame]
Doris Liu766431a2016-02-04 22:17:11 +00001/*
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 Liuc4bb1852016-02-19 21:39:21 +000020#include <algorithm>
21
Doris Liu766431a2016-02-04 22:17:11 +000022namespace android {
23namespace uirenderer {
24
25void PropertyValuesAnimatorSet::addPropertyAnimator(PropertyValuesHolder* propertyValuesHolder,
Doris Liuf7167e82016-08-03 17:54:28 -070026 Interpolator* interpolator, nsecs_t startDelay, nsecs_t duration, int repeatCount,
27 RepeatMode repeatMode) {
Doris Liu766431a2016-02-04 22:17:11 +000028
29 PropertyAnimator* animator = new PropertyAnimator(propertyValuesHolder,
Doris Liuf7167e82016-08-03 17:54:28 -070030 interpolator, startDelay, duration, repeatCount, repeatMode);
Doris Liu766431a2016-02-04 22:17:11 +000031 mAnimators.emplace_back(animator);
Doris Liu718cd3e2016-05-17 16:50:31 -070032
33 // Check whether any child animator is infinite after adding it them to the set.
34 if (repeatCount == -1) {
35 mIsInfinite = true;
36 }
Doris Liu766431a2016-02-04 22:17:11 +000037}
38
39PropertyValuesAnimatorSet::PropertyValuesAnimatorSet()
40 : BaseRenderNodeAnimator(1.0f) {
41 setStartValue(0);
42 mLastFraction = 0.0f;
43 setInterpolator(new LinearInterpolator());
Doris Liuc4704662016-06-23 10:34:17 -070044 setListener(new PropertyAnimatorSetListener(this));
Doris Liu766431a2016-02-04 22:17:11 +000045}
46
47void PropertyValuesAnimatorSet::onFinished(BaseRenderNodeAnimator* animator) {
48 if (mOneShotListener.get()) {
Doris Liu679fe6a2016-10-07 11:09:21 -070049 sp<AnimationListener> listener = std::move(mOneShotListener);
50 // Set the listener to nullptr before the onAnimationFinished callback, rather than after,
51 // for two reasons:
52 // 1) We need to prevent changes to mOneShotListener during the onAnimationFinished
53 // callback (specifically in AnimationListenerBridge::onAnimationFinished(...) from
54 // triggering dtor of the bridge and potentially unsafely re-entering
55 // AnimationListenerBridge::onAnimationFinished(...).
56 // 2) It's possible that there are changes to the listener during the callback, therefore
57 // we need to reset the listener before the callback rather than afterwards.
Doris Liu766431a2016-02-04 22:17:11 +000058 mOneShotListener = nullptr;
Doris Liu679fe6a2016-10-07 11:09:21 -070059 listener->onAnimationFinished(animator);
Doris Liu766431a2016-02-04 22:17:11 +000060 }
61}
62
63float PropertyValuesAnimatorSet::getValue(RenderNode* target) const {
64 return mLastFraction;
65}
66
67void PropertyValuesAnimatorSet::setValue(RenderNode* target, float value) {
68 mLastFraction = value;
69}
70
71void PropertyValuesAnimatorSet::onPlayTimeChanged(nsecs_t playTime) {
Doris Liuc4bb1852016-02-19 21:39:21 +000072 if (playTime == 0 && mDuration > 0) {
73 // Reset all the animators
74 for (auto it = mAnimators.rbegin(); it != mAnimators.rend(); it++) {
75 // Note that this set may containing animators modifying the same property, so when we
76 // reset the animators, we need to make sure the animators that end the first will
77 // have the final say on what the property value should be.
Doris Liuf7167e82016-08-03 17:54:28 -070078 (*it)->setFraction(0, 0);
Doris Liuc4bb1852016-02-19 21:39:21 +000079 }
Doris Liuf7167e82016-08-03 17:54:28 -070080 } else {
Doris Liuc4bb1852016-02-19 21:39:21 +000081 for (auto& anim : mAnimators) {
82 anim->setCurrentPlayTime(playTime);
83 }
Doris Liu766431a2016-02-04 22:17:11 +000084 }
85}
86
Doris Liu766431a2016-02-04 22:17:11 +000087void PropertyValuesAnimatorSet::start(AnimationListener* listener) {
88 init();
89 mOneShotListener = listener;
Doris Liu718cd3e2016-05-17 16:50:31 -070090 mRequestId++;
Doris Liu766431a2016-02-04 22:17:11 +000091 BaseRenderNodeAnimator::start();
92}
93
94void PropertyValuesAnimatorSet::reverse(AnimationListener* listener) {
Doris Liuc4bb1852016-02-19 21:39:21 +000095 init();
96 mOneShotListener = listener;
Doris Liu718cd3e2016-05-17 16:50:31 -070097 mRequestId++;
Doris Liuc4bb1852016-02-19 21:39:21 +000098 BaseRenderNodeAnimator::reverse();
Doris Liu766431a2016-02-04 22:17:11 +000099}
100
Doris Liu718cd3e2016-05-17 16:50:31 -0700101void PropertyValuesAnimatorSet::reset() {
102 mRequestId++;
103 BaseRenderNodeAnimator::reset();
104}
105
106void PropertyValuesAnimatorSet::end() {
107 mRequestId++;
108 BaseRenderNodeAnimator::end();
109}
110
Doris Liu766431a2016-02-04 22:17:11 +0000111void PropertyValuesAnimatorSet::init() {
112 if (mInitialized) {
113 return;
114 }
Doris Liuc4bb1852016-02-19 21:39:21 +0000115
116 // Sort the animators by their total duration. Note that all the animators in the set start at
117 // the same time, so the ones with longer total duration (which includes start delay) will
118 // be the ones that end later.
119 std::sort(mAnimators.begin(), mAnimators.end(), [](auto& a, auto&b) {
120 return a->getTotalDuration() < b->getTotalDuration();
121 });
Doris Liuc4704662016-06-23 10:34:17 -0700122 mDuration = mAnimators.empty() ? 0 : mAnimators[mAnimators.size() - 1]->getTotalDuration();
Doris Liu766431a2016-02-04 22:17:11 +0000123 mInitialized = true;
124}
125
126uint32_t PropertyValuesAnimatorSet::dirtyMask() {
127 return RenderNode::DISPLAY_LIST;
128}
129
130PropertyAnimator::PropertyAnimator(PropertyValuesHolder* holder, Interpolator* interpolator,
Doris Liuf7167e82016-08-03 17:54:28 -0700131 nsecs_t startDelay, nsecs_t duration, int repeatCount,
132 RepeatMode repeatMode)
Doris Liu766431a2016-02-04 22:17:11 +0000133 : mPropertyValuesHolder(holder), mInterpolator(interpolator), mStartDelay(startDelay),
134 mDuration(duration) {
135 if (repeatCount < 0) {
136 mRepeatCount = UINT32_MAX;
137 } else {
138 mRepeatCount = repeatCount;
139 }
Doris Liuf7167e82016-08-03 17:54:28 -0700140 mRepeatMode = repeatMode;
Doris Liu766431a2016-02-04 22:17:11 +0000141 mTotalDuration = ((nsecs_t) mRepeatCount + 1) * mDuration + mStartDelay;
142}
143
144void PropertyAnimator::setCurrentPlayTime(nsecs_t playTime) {
Doris Liuf7167e82016-08-03 17:54:28 -0700145 if (playTime < mStartDelay) {
146 return;
Doris Liu766431a2016-02-04 22:17:11 +0000147 }
Doris Liuf7167e82016-08-03 17:54:28 -0700148
149 float currentIterationFraction;
150 long iteration;
151 if (playTime >= mTotalDuration) {
152 // Reached the end of the animation.
153 iteration = mRepeatCount;
154 currentIterationFraction = 1.0f;
155 } else {
156 // play time here is in range [mStartDelay, mTotalDuration)
157 iteration = (playTime - mStartDelay) / mDuration;
158 currentIterationFraction = ((playTime - mStartDelay) % mDuration) / (float) mDuration;
159 }
160 setFraction(currentIterationFraction, iteration);
Doris Liu766431a2016-02-04 22:17:11 +0000161}
162
Doris Liuf7167e82016-08-03 17:54:28 -0700163void PropertyAnimator::setFraction(float fraction, long iteration) {
164 double totalFraction = fraction + iteration;
165 // This makes sure we only set the fraction = repeatCount + 1 once. It is needed because there
166 // might be another animator modifying the same property after this animator finishes, we need
167 // to make sure we don't set conflicting values on the same property within one frame.
Doris Liu18e08a02016-08-08 12:47:20 -0700168 if ((mLatestFraction == mRepeatCount + 1.0) && (totalFraction >= mRepeatCount + 1.0)) {
Doris Liuf7167e82016-08-03 17:54:28 -0700169 return;
170 }
171
172 mLatestFraction = totalFraction;
173 // Check the play direction (i.e. reverse or restart) every other iteration, and calculate the
174 // fraction based on the play direction.
175 if (iteration % 2 && mRepeatMode == RepeatMode::Reverse) {
176 fraction = 1.0f - fraction;
177 }
Doris Liuc4bb1852016-02-19 21:39:21 +0000178 float interpolatedFraction = mInterpolator->interpolate(fraction);
Doris Liu766431a2016-02-04 22:17:11 +0000179 mPropertyValuesHolder->setFraction(interpolatedFraction);
180}
181
182void PropertyAnimatorSetListener::onAnimationFinished(BaseRenderNodeAnimator* animator) {
183 mSet->onFinished(animator);
184}
185
186}
187}