blob: 4f3a57377b269d69d71b664fcdc783d3a0a18a02 [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"
John Reck52244ff2014-05-01 21:27:37 -070023#include "RenderNode.h"
John Recke45b1fd2014-04-15 09:50:16 -070024#include "RenderProperties.h"
25
26namespace android {
27namespace uirenderer {
28
29/************************************************************
John Reckff941dc2014-05-14 16:34:14 -070030 * BaseRenderNodeAnimator
John Recke45b1fd2014-04-15 09:50:16 -070031 ************************************************************/
32
John Reckff941dc2014-05-14 16:34:14 -070033BaseRenderNodeAnimator::BaseRenderNodeAnimator(float finalValue)
John Reck8d8af3c2014-07-01 15:23:45 -070034 : mTarget(NULL)
35 , mFinalValue(finalValue)
John Reckff941dc2014-05-14 16:34:14 -070036 , mDeltaValue(0)
37 , mFromValue(0)
38 , mInterpolator(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)
44 , mStartDelay(0) {
John Recke45b1fd2014-04-15 09:50:16 -070045}
46
John Reckff941dc2014-05-14 16:34:14 -070047BaseRenderNodeAnimator::~BaseRenderNodeAnimator() {
John Reck68bfe0a2014-06-24 15:34:58 -070048 delete mInterpolator;
49}
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();
John Recke45b1fd2014-04-15 09:50:16 -070059 delete mInterpolator;
60 mInterpolator = interpolator;
61}
62
John Reckff941dc2014-05-14 16:34:14 -070063void BaseRenderNodeAnimator::setStartValue(float value) {
John Reck68bfe0a2014-06-24 15:34:58 -070064 checkMutable();
65 doSetStartValue(value);
John Reckff941dc2014-05-14 16:34:14 -070066}
67
John Reck68bfe0a2014-06-24 15:34:58 -070068void BaseRenderNodeAnimator::doSetStartValue(float value) {
69 mFromValue = value;
70 mDeltaValue = (mFinalValue - mFromValue);
71 mHasStartValue = true;
John Reckff941dc2014-05-14 16:34:14 -070072}
73
Alan Viverettead2f8e32014-05-16 13:28:33 -070074void BaseRenderNodeAnimator::setDuration(nsecs_t duration) {
John Reck68bfe0a2014-06-24 15:34:58 -070075 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070076 mDuration = duration;
77}
78
79void BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) {
John Reck68bfe0a2014-06-24 15:34:58 -070080 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070081 mStartDelay = startDelay;
82}
83
John Reck8d8af3c2014-07-01 15:23:45 -070084void BaseRenderNodeAnimator::attach(RenderNode* target) {
85 mTarget = target;
86 onAttached();
87}
88
John Reck119907c2014-08-14 09:02:01 -070089void BaseRenderNodeAnimator::pushStaging(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -070090 if (!mHasStartValue) {
John Reck8d8af3c2014-07-01 15:23:45 -070091 doSetStartValue(getValue(mTarget));
Alan Viverettead2f8e32014-05-16 13:28:33 -070092 }
John Reck68bfe0a2014-06-24 15:34:58 -070093 if (mStagingPlayState > mPlayState) {
94 mPlayState = mStagingPlayState;
95 // Oh boy, we're starting! Man the battle stations!
96 if (mPlayState == RUNNING) {
John Reck119907c2014-08-14 09:02:01 -070097 transitionToRunning(context);
John Reck4d2c4722014-08-29 10:40:56 -070098 } else if (mPlayState == FINISHED) {
99 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -0700100 }
John Recke45b1fd2014-04-15 09:50:16 -0700101 }
John Reck68bfe0a2014-06-24 15:34:58 -0700102}
103
John Reck119907c2014-08-14 09:02:01 -0700104void BaseRenderNodeAnimator::transitionToRunning(AnimationContext& context) {
105 nsecs_t frameTimeMs = context.frameTimeMs();
106 LOG_ALWAYS_FATAL_IF(frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", frameTimeMs);
John Reck68bfe0a2014-06-24 15:34:58 -0700107 if (mStartDelay < 0 || mStartDelay > 50000) {
108 ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay);
109 }
John Reck119907c2014-08-14 09:02:01 -0700110 mStartTime = frameTimeMs + mStartDelay;
John Reck68bfe0a2014-06-24 15:34:58 -0700111 if (mStartTime < 0) {
112 ALOGW("Ended up with a really weird start time of %" PRId64
113 " with frame time %" PRId64 " and start delay %" PRId64,
John Reck119907c2014-08-14 09:02:01 -0700114 mStartTime, frameTimeMs, mStartDelay);
John Reck68bfe0a2014-06-24 15:34:58 -0700115 // Set to 0 so that the animate() basically instantly finishes
116 mStartTime = 0;
117 }
118 // No interpolator was set, use the default
119 if (!mInterpolator) {
John Reck8d8af3c2014-07-01 15:23:45 -0700120 mInterpolator = Interpolator::createDefaultInterpolator();
John Reck68bfe0a2014-06-24 15:34:58 -0700121 }
122 if (mDuration < 0 || mDuration > 50000) {
123 ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
124 }
125}
126
John Reck119907c2014-08-14 09:02:01 -0700127bool BaseRenderNodeAnimator::animate(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -0700128 if (mPlayState < RUNNING) {
129 return false;
130 }
John Reck32fb6302014-07-07 09:50:32 -0700131 if (mPlayState == FINISHED) {
132 return true;
133 }
John Reck68bfe0a2014-06-24 15:34:58 -0700134
John Reck8d8af3c2014-07-01 15:23:45 -0700135 // If BaseRenderNodeAnimator is handling the delay (not typical), then
136 // because the staging properties reflect the final value, we always need
137 // to call setValue even if the animation isn't yet running or is still
138 // being delayed as we need to override the staging value
John Reck119907c2014-08-14 09:02:01 -0700139 if (mStartTime > context.frameTimeMs()) {
John Reck8d8af3c2014-07-01 15:23:45 -0700140 setValue(mTarget, mFromValue);
John Reck68bfe0a2014-06-24 15:34:58 -0700141 return false;
142 }
John Recke45b1fd2014-04-15 09:50:16 -0700143
144 float fraction = 1.0f;
John Reck68bfe0a2014-06-24 15:34:58 -0700145 if (mPlayState == RUNNING && mDuration > 0) {
John Reck119907c2014-08-14 09:02:01 -0700146 fraction = (float)(context.frameTimeMs() - mStartTime) / mDuration;
John Recke45b1fd2014-04-15 09:50:16 -0700147 }
John Reck68bfe0a2014-06-24 15:34:58 -0700148 if (fraction >= 1.0f) {
149 fraction = 1.0f;
150 mPlayState = FINISHED;
151 }
152
John Recke45b1fd2014-04-15 09:50:16 -0700153 fraction = mInterpolator->interpolate(fraction);
John Reck8d8af3c2014-07-01 15:23:45 -0700154 setValue(mTarget, mFromValue + (mDeltaValue * fraction));
John Recke45b1fd2014-04-15 09:50:16 -0700155
156 if (mPlayState == FINISHED) {
John Reck119907c2014-08-14 09:02:01 -0700157 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -0700158 return true;
159 }
John Reck68bfe0a2014-06-24 15:34:58 -0700160
John Recke45b1fd2014-04-15 09:50:16 -0700161 return false;
162}
163
John Recke2478d42014-09-03 16:46:05 -0700164void BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) {
165 if (mPlayState < FINISHED) {
166 mPlayState = FINISHED;
167 callOnFinishedListener(context);
168 }
169}
170
John Reck119907c2014-08-14 09:02:01 -0700171void BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) {
John Reck52244ff2014-05-01 21:27:37 -0700172 if (mListener.get()) {
John Reck119907c2014-08-14 09:02:01 -0700173 context.callOnFinished(this, mListener.get());
John Reck52244ff2014-05-01 21:27:37 -0700174 }
175}
176
177/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700178 * RenderPropertyAnimator
179 ************************************************************/
180
John Reckff941dc2014-05-14 16:34:14 -0700181struct RenderPropertyAnimator::PropertyAccessors {
182 RenderNode::DirtyPropertyMask dirtyMask;
183 GetFloatProperty getter;
184 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700185};
186
John Reckff941dc2014-05-14 16:34:14 -0700187// Maps RenderProperty enum to accessors
188const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
189 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
190 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
191 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
192 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
193 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
194 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
195 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
196 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
197 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
198 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
199 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
200 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
201};
202
203RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
204 : BaseRenderNodeAnimator(finalValue)
205 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700206}
207
John Reck8d8af3c2014-07-01 15:23:45 -0700208void RenderPropertyAnimator::onAttached() {
John Reck68bfe0a2014-06-24 15:34:58 -0700209 if (!mHasStartValue
John Reck8d8af3c2014-07-01 15:23:45 -0700210 && mTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
211 setStartValue((mTarget->stagingProperties().*mPropertyAccess->getter)());
John Reckff941dc2014-05-14 16:34:14 -0700212 }
John Reck8d8af3c2014-07-01 15:23:45 -0700213}
214
215void RenderPropertyAnimator::onStagingPlayStateChanged() {
216 if (mStagingPlayState == RUNNING) {
217 (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
John Reck32fb6302014-07-07 09:50:32 -0700218 } else if (mStagingPlayState == FINISHED) {
219 // We're being canceled, so make sure that whatever values the UI thread
220 // is observing for us is pushed over
221 mTarget->setPropertyFieldsDirty(dirtyMask());
John Reck8d8af3c2014-07-01 15:23:45 -0700222 }
John Recke45b1fd2014-04-15 09:50:16 -0700223}
224
John Reck22184722014-06-20 07:19:30 -0700225uint32_t RenderPropertyAnimator::dirtyMask() {
226 return mPropertyAccess->dirtyMask;
227}
228
John Reckff941dc2014-05-14 16:34:14 -0700229float RenderPropertyAnimator::getValue(RenderNode* target) const {
230 return (target->properties().*mPropertyAccess->getter)();
231}
232
233void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
234 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700235}
236
John Reck52244ff2014-05-01 21:27:37 -0700237/************************************************************
238 * CanvasPropertyPrimitiveAnimator
239 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700240
John Reck52244ff2014-05-01 21:27:37 -0700241CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700242 CanvasPropertyPrimitive* property, float finalValue)
243 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700244 , mProperty(property) {
245}
246
John Reckff941dc2014-05-14 16:34:14 -0700247float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700248 return mProperty->value;
249}
250
John Reckff941dc2014-05-14 16:34:14 -0700251void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700252 mProperty->value = value;
253}
254
John Recka7c2ea22014-08-08 13:21:00 -0700255uint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() {
256 return RenderNode::DISPLAY_LIST;
257}
258
John Reck52244ff2014-05-01 21:27:37 -0700259/************************************************************
260 * CanvasPropertySkPaintAnimator
261 ************************************************************/
262
263CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700264 CanvasPropertyPaint* property, PaintField field, float finalValue)
265 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700266 , mProperty(property)
267 , mField(field) {
268}
269
John Reckff941dc2014-05-14 16:34:14 -0700270float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700271 switch (mField) {
272 case STROKE_WIDTH:
273 return mProperty->value.getStrokeWidth();
274 case ALPHA:
275 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700276 }
John Reck52244ff2014-05-01 21:27:37 -0700277 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
278 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700279}
280
John Reck531ee702014-05-13 10:06:08 -0700281static uint8_t to_uint8(float value) {
282 int c = (int) (value + .5f);
283 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
284}
285
John Reckff941dc2014-05-14 16:34:14 -0700286void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700287 switch (mField) {
288 case STROKE_WIDTH:
289 mProperty->value.setStrokeWidth(value);
290 return;
291 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700292 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700293 return;
294 }
295 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700296}
297
John Recka7c2ea22014-08-08 13:21:00 -0700298uint32_t CanvasPropertyPaintAnimator::dirtyMask() {
299 return RenderNode::DISPLAY_LIST;
300}
301
Chris Craikaf4d04c2014-07-29 12:50:14 -0700302RevealAnimator::RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700303 float startValue, float finalValue)
304 : BaseRenderNodeAnimator(finalValue)
305 , mCenterX(centerX)
Chris Craikaf4d04c2014-07-29 12:50:14 -0700306 , mCenterY(centerY) {
John Reckd3de42c2014-07-15 14:29:33 -0700307 setStartValue(startValue);
308}
309
310float RevealAnimator::getValue(RenderNode* target) const {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700311 return target->properties().getRevealClip().getRadius();
John Reckd3de42c2014-07-15 14:29:33 -0700312}
313
314void RevealAnimator::setValue(RenderNode* target, float value) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700315 target->animatorProperties().mutableRevealClip().set(true,
John Reckd3de42c2014-07-15 14:29:33 -0700316 mCenterX, mCenterY, value);
317}
318
John Recka7c2ea22014-08-08 13:21:00 -0700319uint32_t RevealAnimator::dirtyMask() {
320 return RenderNode::GENERIC;
321}
322
John Recke45b1fd2014-04-15 09:50:16 -0700323} /* namespace uirenderer */
324} /* namespace android */