blob: 512e0e24aa936af1bdc69548afdcd1295110dc9f [file] [log] [blame]
John Recke45b1fd2014-04-15 09:50:16 -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
John Recke45b1fd2014-04-15 09:50:16 -070017#include "Animator.h"
18
John Reck68bfe0a2014-06-24 15:34:58 -070019#include <inttypes.h>
John Recke45b1fd2014-04-15 09:50:16 -070020#include <set>
21
John Reck119907c2014-08-14 09:02:01 -070022#include "AnimationContext.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040023#include "Interpolator.h"
John Reck52244ff2014-05-01 21:27:37 -070024#include "RenderNode.h"
John Recke45b1fd2014-04-15 09:50:16 -070025#include "RenderProperties.h"
26
27namespace android {
28namespace uirenderer {
29
30/************************************************************
John Reckff941dc2014-05-14 16:34:14 -070031 * BaseRenderNodeAnimator
John Recke45b1fd2014-04-15 09:50:16 -070032 ************************************************************/
33
John Reckff941dc2014-05-14 16:34:14 -070034BaseRenderNodeAnimator::BaseRenderNodeAnimator(float finalValue)
Chris Craikd41c4d82015-01-05 15:51:13 -080035 : mTarget(nullptr)
John Reck8d8af3c2014-07-01 15:23:45 -070036 , mFinalValue(finalValue)
John Reckff941dc2014-05-14 16:34:14 -070037 , mDeltaValue(0)
38 , mFromValue(0)
John Reck68bfe0a2014-06-24 15:34:58 -070039 , mStagingPlayState(NOT_STARTED)
40 , mPlayState(NOT_STARTED)
41 , mHasStartValue(false)
John Recke45b1fd2014-04-15 09:50:16 -070042 , mStartTime(0)
Alan Viverettead2f8e32014-05-16 13:28:33 -070043 , mDuration(300)
Chris Craik572d9ac2014-09-12 17:40:20 -070044 , mStartDelay(0)
45 , mMayRunAsync(true) {
John Recke45b1fd2014-04-15 09:50:16 -070046}
47
John Reckff941dc2014-05-14 16:34:14 -070048BaseRenderNodeAnimator::~BaseRenderNodeAnimator() {
John Reck68bfe0a2014-06-24 15:34:58 -070049}
50
51void BaseRenderNodeAnimator::checkMutable() {
52 // Should be impossible to hit as the Java-side also has guards for this
53 LOG_ALWAYS_FATAL_IF(mStagingPlayState != NOT_STARTED,
54 "Animator has already been started!");
John Recke45b1fd2014-04-15 09:50:16 -070055}
56
John Reckff941dc2014-05-14 16:34:14 -070057void BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) {
John Reck68bfe0a2014-06-24 15:34:58 -070058 checkMutable();
Chris Craik51d6a3d2014-12-22 17:16:56 -080059 mInterpolator.reset(interpolator);
John Recke45b1fd2014-04-15 09:50:16 -070060}
61
John Reckff941dc2014-05-14 16:34:14 -070062void BaseRenderNodeAnimator::setStartValue(float value) {
John Reck68bfe0a2014-06-24 15:34:58 -070063 checkMutable();
64 doSetStartValue(value);
John Reckff941dc2014-05-14 16:34:14 -070065}
66
John Reck68bfe0a2014-06-24 15:34:58 -070067void BaseRenderNodeAnimator::doSetStartValue(float value) {
68 mFromValue = value;
69 mDeltaValue = (mFinalValue - mFromValue);
70 mHasStartValue = true;
John Reckff941dc2014-05-14 16:34:14 -070071}
72
Alan Viverettead2f8e32014-05-16 13:28:33 -070073void BaseRenderNodeAnimator::setDuration(nsecs_t duration) {
John Reck68bfe0a2014-06-24 15:34:58 -070074 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070075 mDuration = duration;
76}
77
78void BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) {
John Reck68bfe0a2014-06-24 15:34:58 -070079 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070080 mStartDelay = startDelay;
81}
82
John Reck8d8af3c2014-07-01 15:23:45 -070083void BaseRenderNodeAnimator::attach(RenderNode* target) {
84 mTarget = target;
85 onAttached();
86}
87
John Reck119907c2014-08-14 09:02:01 -070088void BaseRenderNodeAnimator::pushStaging(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -070089 if (!mHasStartValue) {
John Reck8d8af3c2014-07-01 15:23:45 -070090 doSetStartValue(getValue(mTarget));
Alan Viverettead2f8e32014-05-16 13:28:33 -070091 }
John Reck68bfe0a2014-06-24 15:34:58 -070092 if (mStagingPlayState > mPlayState) {
93 mPlayState = mStagingPlayState;
94 // Oh boy, we're starting! Man the battle stations!
95 if (mPlayState == RUNNING) {
John Reck119907c2014-08-14 09:02:01 -070096 transitionToRunning(context);
John Reck4d2c4722014-08-29 10:40:56 -070097 } else if (mPlayState == FINISHED) {
98 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -070099 }
John Recke45b1fd2014-04-15 09:50:16 -0700100 }
John Reck68bfe0a2014-06-24 15:34:58 -0700101}
102
John Reck119907c2014-08-14 09:02:01 -0700103void BaseRenderNodeAnimator::transitionToRunning(AnimationContext& context) {
104 nsecs_t frameTimeMs = context.frameTimeMs();
105 LOG_ALWAYS_FATAL_IF(frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", frameTimeMs);
John Reck68bfe0a2014-06-24 15:34:58 -0700106 if (mStartDelay < 0 || mStartDelay > 50000) {
107 ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay);
108 }
John Reck119907c2014-08-14 09:02:01 -0700109 mStartTime = frameTimeMs + mStartDelay;
John Reck68bfe0a2014-06-24 15:34:58 -0700110 if (mStartTime < 0) {
111 ALOGW("Ended up with a really weird start time of %" PRId64
112 " with frame time %" PRId64 " and start delay %" PRId64,
John Reck119907c2014-08-14 09:02:01 -0700113 mStartTime, frameTimeMs, mStartDelay);
John Reck68bfe0a2014-06-24 15:34:58 -0700114 // Set to 0 so that the animate() basically instantly finishes
115 mStartTime = 0;
116 }
117 // No interpolator was set, use the default
118 if (!mInterpolator) {
Chris Craik51d6a3d2014-12-22 17:16:56 -0800119 mInterpolator.reset(Interpolator::createDefaultInterpolator());
John Reck68bfe0a2014-06-24 15:34:58 -0700120 }
121 if (mDuration < 0 || mDuration > 50000) {
122 ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
123 }
124}
125
John Reck119907c2014-08-14 09:02:01 -0700126bool BaseRenderNodeAnimator::animate(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -0700127 if (mPlayState < RUNNING) {
128 return false;
129 }
John Reck32fb6302014-07-07 09:50:32 -0700130 if (mPlayState == FINISHED) {
131 return true;
132 }
John Reck68bfe0a2014-06-24 15:34:58 -0700133
John Reck8d8af3c2014-07-01 15:23:45 -0700134 // If BaseRenderNodeAnimator is handling the delay (not typical), then
135 // because the staging properties reflect the final value, we always need
136 // to call setValue even if the animation isn't yet running or is still
137 // being delayed as we need to override the staging value
John Reck119907c2014-08-14 09:02:01 -0700138 if (mStartTime > context.frameTimeMs()) {
John Reck8d8af3c2014-07-01 15:23:45 -0700139 setValue(mTarget, mFromValue);
John Reck68bfe0a2014-06-24 15:34:58 -0700140 return false;
141 }
John Recke45b1fd2014-04-15 09:50:16 -0700142
143 float fraction = 1.0f;
John Reck68bfe0a2014-06-24 15:34:58 -0700144 if (mPlayState == RUNNING && mDuration > 0) {
John Reck119907c2014-08-14 09:02:01 -0700145 fraction = (float)(context.frameTimeMs() - mStartTime) / mDuration;
John Recke45b1fd2014-04-15 09:50:16 -0700146 }
John Reck68bfe0a2014-06-24 15:34:58 -0700147 if (fraction >= 1.0f) {
148 fraction = 1.0f;
149 mPlayState = FINISHED;
150 }
151
John Recke45b1fd2014-04-15 09:50:16 -0700152 fraction = mInterpolator->interpolate(fraction);
John Reck8d8af3c2014-07-01 15:23:45 -0700153 setValue(mTarget, mFromValue + (mDeltaValue * fraction));
John Recke45b1fd2014-04-15 09:50:16 -0700154
155 if (mPlayState == FINISHED) {
John Reck119907c2014-08-14 09:02:01 -0700156 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -0700157 return true;
158 }
John Reck68bfe0a2014-06-24 15:34:58 -0700159
John Recke45b1fd2014-04-15 09:50:16 -0700160 return false;
161}
162
John Recke2478d42014-09-03 16:46:05 -0700163void BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) {
164 if (mPlayState < FINISHED) {
165 mPlayState = FINISHED;
166 callOnFinishedListener(context);
167 }
168}
169
John Reck119907c2014-08-14 09:02:01 -0700170void BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) {
John Reck52244ff2014-05-01 21:27:37 -0700171 if (mListener.get()) {
John Reck119907c2014-08-14 09:02:01 -0700172 context.callOnFinished(this, mListener.get());
John Reck52244ff2014-05-01 21:27:37 -0700173 }
174}
175
176/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700177 * RenderPropertyAnimator
178 ************************************************************/
179
John Reckff941dc2014-05-14 16:34:14 -0700180struct RenderPropertyAnimator::PropertyAccessors {
181 RenderNode::DirtyPropertyMask dirtyMask;
182 GetFloatProperty getter;
183 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700184};
185
John Reckff941dc2014-05-14 16:34:14 -0700186// Maps RenderProperty enum to accessors
187const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
188 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
189 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
190 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
191 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
192 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
193 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
194 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
195 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
196 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
197 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
198 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
199 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
200};
201
202RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
203 : BaseRenderNodeAnimator(finalValue)
204 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700205}
206
John Reck8d8af3c2014-07-01 15:23:45 -0700207void RenderPropertyAnimator::onAttached() {
John Reck68bfe0a2014-06-24 15:34:58 -0700208 if (!mHasStartValue
John Reck8d8af3c2014-07-01 15:23:45 -0700209 && mTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
210 setStartValue((mTarget->stagingProperties().*mPropertyAccess->getter)());
John Reckff941dc2014-05-14 16:34:14 -0700211 }
John Reck8d8af3c2014-07-01 15:23:45 -0700212}
213
214void RenderPropertyAnimator::onStagingPlayStateChanged() {
215 if (mStagingPlayState == RUNNING) {
216 (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
John Reck32fb6302014-07-07 09:50:32 -0700217 } else if (mStagingPlayState == FINISHED) {
218 // We're being canceled, so make sure that whatever values the UI thread
219 // is observing for us is pushed over
220 mTarget->setPropertyFieldsDirty(dirtyMask());
John Reck8d8af3c2014-07-01 15:23:45 -0700221 }
John Recke45b1fd2014-04-15 09:50:16 -0700222}
223
John Reck22184722014-06-20 07:19:30 -0700224uint32_t RenderPropertyAnimator::dirtyMask() {
225 return mPropertyAccess->dirtyMask;
226}
227
John Reckff941dc2014-05-14 16:34:14 -0700228float RenderPropertyAnimator::getValue(RenderNode* target) const {
229 return (target->properties().*mPropertyAccess->getter)();
230}
231
232void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
233 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700234}
235
John Reck52244ff2014-05-01 21:27:37 -0700236/************************************************************
237 * CanvasPropertyPrimitiveAnimator
238 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700239
John Reck52244ff2014-05-01 21:27:37 -0700240CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700241 CanvasPropertyPrimitive* property, float finalValue)
242 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700243 , mProperty(property) {
244}
245
Andreas Gampe64bb4132014-11-22 00:35:09 +0000246float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700247 return mProperty->value;
248}
249
Andreas Gampe64bb4132014-11-22 00:35:09 +0000250void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700251 mProperty->value = value;
252}
253
John Recka7c2ea22014-08-08 13:21:00 -0700254uint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() {
255 return RenderNode::DISPLAY_LIST;
256}
257
John Reck52244ff2014-05-01 21:27:37 -0700258/************************************************************
259 * CanvasPropertySkPaintAnimator
260 ************************************************************/
261
262CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700263 CanvasPropertyPaint* property, PaintField field, float finalValue)
264 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700265 , mProperty(property)
266 , mField(field) {
267}
268
Andreas Gampe64bb4132014-11-22 00:35:09 +0000269float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700270 switch (mField) {
271 case STROKE_WIDTH:
272 return mProperty->value.getStrokeWidth();
273 case ALPHA:
274 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700275 }
John Reck52244ff2014-05-01 21:27:37 -0700276 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
277 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700278}
279
John Reck531ee702014-05-13 10:06:08 -0700280static uint8_t to_uint8(float value) {
281 int c = (int) (value + .5f);
282 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
283}
284
Andreas Gampe64bb4132014-11-22 00:35:09 +0000285void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700286 switch (mField) {
287 case STROKE_WIDTH:
288 mProperty->value.setStrokeWidth(value);
289 return;
290 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700291 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700292 return;
293 }
294 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700295}
296
John Recka7c2ea22014-08-08 13:21:00 -0700297uint32_t CanvasPropertyPaintAnimator::dirtyMask() {
298 return RenderNode::DISPLAY_LIST;
299}
300
Chris Craikaf4d04c2014-07-29 12:50:14 -0700301RevealAnimator::RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700302 float startValue, float finalValue)
303 : BaseRenderNodeAnimator(finalValue)
304 , mCenterX(centerX)
Chris Craikaf4d04c2014-07-29 12:50:14 -0700305 , mCenterY(centerY) {
John Reckd3de42c2014-07-15 14:29:33 -0700306 setStartValue(startValue);
307}
308
309float RevealAnimator::getValue(RenderNode* target) const {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700310 return target->properties().getRevealClip().getRadius();
John Reckd3de42c2014-07-15 14:29:33 -0700311}
312
313void RevealAnimator::setValue(RenderNode* target, float value) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700314 target->animatorProperties().mutableRevealClip().set(true,
John Reckd3de42c2014-07-15 14:29:33 -0700315 mCenterX, mCenterY, value);
316}
317
John Recka7c2ea22014-08-08 13:21:00 -0700318uint32_t RevealAnimator::dirtyMask() {
319 return RenderNode::GENERIC;
320}
321
John Recke45b1fd2014-04-15 09:50:16 -0700322} /* namespace uirenderer */
323} /* namespace android */