blob: 8bf2107b70bb360b64691c78f900a65be1c4515a [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)
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 delete mInterpolator;
50}
51
52void BaseRenderNodeAnimator::checkMutable() {
53 // Should be impossible to hit as the Java-side also has guards for this
54 LOG_ALWAYS_FATAL_IF(mStagingPlayState != NOT_STARTED,
55 "Animator has already been started!");
John Recke45b1fd2014-04-15 09:50:16 -070056}
57
John Reckff941dc2014-05-14 16:34:14 -070058void BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) {
John Reck68bfe0a2014-06-24 15:34:58 -070059 checkMutable();
John Recke45b1fd2014-04-15 09:50:16 -070060 delete mInterpolator;
61 mInterpolator = interpolator;
62}
63
John Reckff941dc2014-05-14 16:34:14 -070064void BaseRenderNodeAnimator::setStartValue(float value) {
John Reck68bfe0a2014-06-24 15:34:58 -070065 checkMutable();
66 doSetStartValue(value);
John Reckff941dc2014-05-14 16:34:14 -070067}
68
John Reck68bfe0a2014-06-24 15:34:58 -070069void BaseRenderNodeAnimator::doSetStartValue(float value) {
70 mFromValue = value;
71 mDeltaValue = (mFinalValue - mFromValue);
72 mHasStartValue = true;
John Reckff941dc2014-05-14 16:34:14 -070073}
74
Alan Viverettead2f8e32014-05-16 13:28:33 -070075void BaseRenderNodeAnimator::setDuration(nsecs_t duration) {
John Reck68bfe0a2014-06-24 15:34:58 -070076 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070077 mDuration = duration;
78}
79
80void BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) {
John Reck68bfe0a2014-06-24 15:34:58 -070081 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070082 mStartDelay = startDelay;
83}
84
John Reck8d8af3c2014-07-01 15:23:45 -070085void BaseRenderNodeAnimator::attach(RenderNode* target) {
86 mTarget = target;
87 onAttached();
88}
89
John Reck119907c2014-08-14 09:02:01 -070090void BaseRenderNodeAnimator::pushStaging(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -070091 if (!mHasStartValue) {
John Reck8d8af3c2014-07-01 15:23:45 -070092 doSetStartValue(getValue(mTarget));
Alan Viverettead2f8e32014-05-16 13:28:33 -070093 }
John Reck68bfe0a2014-06-24 15:34:58 -070094 if (mStagingPlayState > mPlayState) {
95 mPlayState = mStagingPlayState;
96 // Oh boy, we're starting! Man the battle stations!
97 if (mPlayState == RUNNING) {
John Reck119907c2014-08-14 09:02:01 -070098 transitionToRunning(context);
John Reck4d2c4722014-08-29 10:40:56 -070099 } else if (mPlayState == FINISHED) {
100 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -0700101 }
John Recke45b1fd2014-04-15 09:50:16 -0700102 }
John Reck68bfe0a2014-06-24 15:34:58 -0700103}
104
John Reck119907c2014-08-14 09:02:01 -0700105void BaseRenderNodeAnimator::transitionToRunning(AnimationContext& context) {
106 nsecs_t frameTimeMs = context.frameTimeMs();
107 LOG_ALWAYS_FATAL_IF(frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", frameTimeMs);
John Reck68bfe0a2014-06-24 15:34:58 -0700108 if (mStartDelay < 0 || mStartDelay > 50000) {
109 ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay);
110 }
John Reck119907c2014-08-14 09:02:01 -0700111 mStartTime = frameTimeMs + mStartDelay;
John Reck68bfe0a2014-06-24 15:34:58 -0700112 if (mStartTime < 0) {
113 ALOGW("Ended up with a really weird start time of %" PRId64
114 " with frame time %" PRId64 " and start delay %" PRId64,
John Reck119907c2014-08-14 09:02:01 -0700115 mStartTime, frameTimeMs, mStartDelay);
John Reck68bfe0a2014-06-24 15:34:58 -0700116 // Set to 0 so that the animate() basically instantly finishes
117 mStartTime = 0;
118 }
119 // No interpolator was set, use the default
120 if (!mInterpolator) {
John Reck8d8af3c2014-07-01 15:23:45 -0700121 mInterpolator = Interpolator::createDefaultInterpolator();
John Reck68bfe0a2014-06-24 15:34:58 -0700122 }
123 if (mDuration < 0 || mDuration > 50000) {
124 ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
125 }
126}
127
John Reck119907c2014-08-14 09:02:01 -0700128bool BaseRenderNodeAnimator::animate(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -0700129 if (mPlayState < RUNNING) {
130 return false;
131 }
John Reck32fb6302014-07-07 09:50:32 -0700132 if (mPlayState == FINISHED) {
133 return true;
134 }
John Reck68bfe0a2014-06-24 15:34:58 -0700135
John Reck8d8af3c2014-07-01 15:23:45 -0700136 // If BaseRenderNodeAnimator is handling the delay (not typical), then
137 // because the staging properties reflect the final value, we always need
138 // to call setValue even if the animation isn't yet running or is still
139 // being delayed as we need to override the staging value
John Reck119907c2014-08-14 09:02:01 -0700140 if (mStartTime > context.frameTimeMs()) {
John Reck8d8af3c2014-07-01 15:23:45 -0700141 setValue(mTarget, mFromValue);
John Reck68bfe0a2014-06-24 15:34:58 -0700142 return false;
143 }
John Recke45b1fd2014-04-15 09:50:16 -0700144
145 float fraction = 1.0f;
John Reck68bfe0a2014-06-24 15:34:58 -0700146 if (mPlayState == RUNNING && mDuration > 0) {
John Reck119907c2014-08-14 09:02:01 -0700147 fraction = (float)(context.frameTimeMs() - mStartTime) / mDuration;
John Recke45b1fd2014-04-15 09:50:16 -0700148 }
John Reck68bfe0a2014-06-24 15:34:58 -0700149 if (fraction >= 1.0f) {
150 fraction = 1.0f;
151 mPlayState = FINISHED;
152 }
153
John Recke45b1fd2014-04-15 09:50:16 -0700154 fraction = mInterpolator->interpolate(fraction);
John Reck8d8af3c2014-07-01 15:23:45 -0700155 setValue(mTarget, mFromValue + (mDeltaValue * fraction));
John Recke45b1fd2014-04-15 09:50:16 -0700156
157 if (mPlayState == FINISHED) {
John Reck119907c2014-08-14 09:02:01 -0700158 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -0700159 return true;
160 }
John Reck68bfe0a2014-06-24 15:34:58 -0700161
John Recke45b1fd2014-04-15 09:50:16 -0700162 return false;
163}
164
John Recke2478d42014-09-03 16:46:05 -0700165void BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) {
166 if (mPlayState < FINISHED) {
167 mPlayState = FINISHED;
168 callOnFinishedListener(context);
169 }
170}
171
John Reck119907c2014-08-14 09:02:01 -0700172void BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) {
John Reck52244ff2014-05-01 21:27:37 -0700173 if (mListener.get()) {
John Reck119907c2014-08-14 09:02:01 -0700174 context.callOnFinished(this, mListener.get());
John Reck52244ff2014-05-01 21:27:37 -0700175 }
176}
177
178/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700179 * RenderPropertyAnimator
180 ************************************************************/
181
John Reckff941dc2014-05-14 16:34:14 -0700182struct RenderPropertyAnimator::PropertyAccessors {
183 RenderNode::DirtyPropertyMask dirtyMask;
184 GetFloatProperty getter;
185 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700186};
187
John Reckff941dc2014-05-14 16:34:14 -0700188// Maps RenderProperty enum to accessors
189const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
190 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
191 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
192 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
193 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
194 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
195 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
196 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
197 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
198 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
199 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
200 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
201 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
202};
203
204RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
205 : BaseRenderNodeAnimator(finalValue)
206 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700207}
208
John Reck8d8af3c2014-07-01 15:23:45 -0700209void RenderPropertyAnimator::onAttached() {
John Reck68bfe0a2014-06-24 15:34:58 -0700210 if (!mHasStartValue
John Reck8d8af3c2014-07-01 15:23:45 -0700211 && mTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
212 setStartValue((mTarget->stagingProperties().*mPropertyAccess->getter)());
John Reckff941dc2014-05-14 16:34:14 -0700213 }
John Reck8d8af3c2014-07-01 15:23:45 -0700214}
215
216void RenderPropertyAnimator::onStagingPlayStateChanged() {
217 if (mStagingPlayState == RUNNING) {
218 (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
John Reck32fb6302014-07-07 09:50:32 -0700219 } else if (mStagingPlayState == FINISHED) {
220 // We're being canceled, so make sure that whatever values the UI thread
221 // is observing for us is pushed over
222 mTarget->setPropertyFieldsDirty(dirtyMask());
John Reck8d8af3c2014-07-01 15:23:45 -0700223 }
John Recke45b1fd2014-04-15 09:50:16 -0700224}
225
John Reck22184722014-06-20 07:19:30 -0700226uint32_t RenderPropertyAnimator::dirtyMask() {
227 return mPropertyAccess->dirtyMask;
228}
229
John Reckff941dc2014-05-14 16:34:14 -0700230float RenderPropertyAnimator::getValue(RenderNode* target) const {
231 return (target->properties().*mPropertyAccess->getter)();
232}
233
234void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
235 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700236}
237
John Reck52244ff2014-05-01 21:27:37 -0700238/************************************************************
239 * CanvasPropertyPrimitiveAnimator
240 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700241
John Reck52244ff2014-05-01 21:27:37 -0700242CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700243 CanvasPropertyPrimitive* property, float finalValue)
244 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700245 , mProperty(property) {
246}
247
John Reckff941dc2014-05-14 16:34:14 -0700248float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700249 return mProperty->value;
250}
251
John Reckff941dc2014-05-14 16:34:14 -0700252void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700253 mProperty->value = value;
254}
255
John Recka7c2ea22014-08-08 13:21:00 -0700256uint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() {
257 return RenderNode::DISPLAY_LIST;
258}
259
John Reck52244ff2014-05-01 21:27:37 -0700260/************************************************************
261 * CanvasPropertySkPaintAnimator
262 ************************************************************/
263
264CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700265 CanvasPropertyPaint* property, PaintField field, float finalValue)
266 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700267 , mProperty(property)
268 , mField(field) {
269}
270
John Reckff941dc2014-05-14 16:34:14 -0700271float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700272 switch (mField) {
273 case STROKE_WIDTH:
274 return mProperty->value.getStrokeWidth();
275 case ALPHA:
276 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700277 }
John Reck52244ff2014-05-01 21:27:37 -0700278 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
279 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700280}
281
John Reck531ee702014-05-13 10:06:08 -0700282static uint8_t to_uint8(float value) {
283 int c = (int) (value + .5f);
284 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
285}
286
John Reckff941dc2014-05-14 16:34:14 -0700287void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700288 switch (mField) {
289 case STROKE_WIDTH:
290 mProperty->value.setStrokeWidth(value);
291 return;
292 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700293 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700294 return;
295 }
296 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700297}
298
John Recka7c2ea22014-08-08 13:21:00 -0700299uint32_t CanvasPropertyPaintAnimator::dirtyMask() {
300 return RenderNode::DISPLAY_LIST;
301}
302
Chris Craikaf4d04c2014-07-29 12:50:14 -0700303RevealAnimator::RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700304 float startValue, float finalValue)
305 : BaseRenderNodeAnimator(finalValue)
306 , mCenterX(centerX)
Chris Craikaf4d04c2014-07-29 12:50:14 -0700307 , mCenterY(centerY) {
John Reckd3de42c2014-07-15 14:29:33 -0700308 setStartValue(startValue);
309}
310
311float RevealAnimator::getValue(RenderNode* target) const {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700312 return target->properties().getRevealClip().getRadius();
John Reckd3de42c2014-07-15 14:29:33 -0700313}
314
315void RevealAnimator::setValue(RenderNode* target, float value) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700316 target->animatorProperties().mutableRevealClip().set(true,
John Reckd3de42c2014-07-15 14:29:33 -0700317 mCenterX, mCenterY, value);
318}
319
John Recka7c2ea22014-08-08 13:21:00 -0700320uint32_t RevealAnimator::dirtyMask() {
321 return RenderNode::GENERIC;
322}
323
John Recke45b1fd2014-04-15 09:50:16 -0700324} /* namespace uirenderer */
325} /* namespace android */