blob: a1bbc06089ff4f0d16c9eeeeff8c33e66f334903 [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)
John Reck8d8af3c2014-07-01 15:23:45 -070035 : mTarget(NULL)
36 , mFinalValue(finalValue)
John Reckff941dc2014-05-14 16:34:14 -070037 , mDeltaValue(0)
38 , mFromValue(0)
39 , mInterpolator(0)
John Reck68bfe0a2014-06-24 15:34:58 -070040 , mStagingPlayState(NOT_STARTED)
41 , mPlayState(NOT_STARTED)
42 , mHasStartValue(false)
John Recke45b1fd2014-04-15 09:50:16 -070043 , mStartTime(0)
Alan Viverettead2f8e32014-05-16 13:28:33 -070044 , mDuration(300)
Chris Craik572d9ac2014-09-12 17:40:20 -070045 , mStartDelay(0)
46 , mMayRunAsync(true) {
John Recke45b1fd2014-04-15 09:50:16 -070047}
48
John Reckff941dc2014-05-14 16:34:14 -070049BaseRenderNodeAnimator::~BaseRenderNodeAnimator() {
John Reck68bfe0a2014-06-24 15:34:58 -070050 delete mInterpolator;
51}
52
53void BaseRenderNodeAnimator::checkMutable() {
54 // Should be impossible to hit as the Java-side also has guards for this
55 LOG_ALWAYS_FATAL_IF(mStagingPlayState != NOT_STARTED,
56 "Animator has already been started!");
John Recke45b1fd2014-04-15 09:50:16 -070057}
58
John Reckff941dc2014-05-14 16:34:14 -070059void BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) {
John Reck68bfe0a2014-06-24 15:34:58 -070060 checkMutable();
John Recke45b1fd2014-04-15 09:50:16 -070061 delete mInterpolator;
62 mInterpolator = interpolator;
63}
64
John Reckff941dc2014-05-14 16:34:14 -070065void BaseRenderNodeAnimator::setStartValue(float value) {
John Reck68bfe0a2014-06-24 15:34:58 -070066 checkMutable();
67 doSetStartValue(value);
John Reckff941dc2014-05-14 16:34:14 -070068}
69
John Reck68bfe0a2014-06-24 15:34:58 -070070void BaseRenderNodeAnimator::doSetStartValue(float value) {
71 mFromValue = value;
72 mDeltaValue = (mFinalValue - mFromValue);
73 mHasStartValue = true;
John Reckff941dc2014-05-14 16:34:14 -070074}
75
Alan Viverettead2f8e32014-05-16 13:28:33 -070076void BaseRenderNodeAnimator::setDuration(nsecs_t duration) {
John Reck68bfe0a2014-06-24 15:34:58 -070077 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070078 mDuration = duration;
79}
80
81void BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) {
John Reck68bfe0a2014-06-24 15:34:58 -070082 checkMutable();
Alan Viverettead2f8e32014-05-16 13:28:33 -070083 mStartDelay = startDelay;
84}
85
John Reck8d8af3c2014-07-01 15:23:45 -070086void BaseRenderNodeAnimator::attach(RenderNode* target) {
87 mTarget = target;
88 onAttached();
89}
90
John Reck119907c2014-08-14 09:02:01 -070091void BaseRenderNodeAnimator::pushStaging(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -070092 if (!mHasStartValue) {
John Reck8d8af3c2014-07-01 15:23:45 -070093 doSetStartValue(getValue(mTarget));
Alan Viverettead2f8e32014-05-16 13:28:33 -070094 }
John Reck68bfe0a2014-06-24 15:34:58 -070095 if (mStagingPlayState > mPlayState) {
96 mPlayState = mStagingPlayState;
97 // Oh boy, we're starting! Man the battle stations!
98 if (mPlayState == RUNNING) {
John Reck119907c2014-08-14 09:02:01 -070099 transitionToRunning(context);
John Reck4d2c4722014-08-29 10:40:56 -0700100 } else if (mPlayState == FINISHED) {
101 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -0700102 }
John Recke45b1fd2014-04-15 09:50:16 -0700103 }
John Reck68bfe0a2014-06-24 15:34:58 -0700104}
105
John Reck119907c2014-08-14 09:02:01 -0700106void BaseRenderNodeAnimator::transitionToRunning(AnimationContext& context) {
107 nsecs_t frameTimeMs = context.frameTimeMs();
108 LOG_ALWAYS_FATAL_IF(frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", frameTimeMs);
John Reck68bfe0a2014-06-24 15:34:58 -0700109 if (mStartDelay < 0 || mStartDelay > 50000) {
110 ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay);
111 }
John Reck119907c2014-08-14 09:02:01 -0700112 mStartTime = frameTimeMs + mStartDelay;
John Reck68bfe0a2014-06-24 15:34:58 -0700113 if (mStartTime < 0) {
114 ALOGW("Ended up with a really weird start time of %" PRId64
115 " with frame time %" PRId64 " and start delay %" PRId64,
John Reck119907c2014-08-14 09:02:01 -0700116 mStartTime, frameTimeMs, mStartDelay);
John Reck68bfe0a2014-06-24 15:34:58 -0700117 // Set to 0 so that the animate() basically instantly finishes
118 mStartTime = 0;
119 }
120 // No interpolator was set, use the default
121 if (!mInterpolator) {
John Reck8d8af3c2014-07-01 15:23:45 -0700122 mInterpolator = Interpolator::createDefaultInterpolator();
John Reck68bfe0a2014-06-24 15:34:58 -0700123 }
124 if (mDuration < 0 || mDuration > 50000) {
125 ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
126 }
127}
128
John Reck119907c2014-08-14 09:02:01 -0700129bool BaseRenderNodeAnimator::animate(AnimationContext& context) {
John Reck68bfe0a2014-06-24 15:34:58 -0700130 if (mPlayState < RUNNING) {
131 return false;
132 }
John Reck32fb6302014-07-07 09:50:32 -0700133 if (mPlayState == FINISHED) {
134 return true;
135 }
John Reck68bfe0a2014-06-24 15:34:58 -0700136
John Reck8d8af3c2014-07-01 15:23:45 -0700137 // If BaseRenderNodeAnimator is handling the delay (not typical), then
138 // because the staging properties reflect the final value, we always need
139 // to call setValue even if the animation isn't yet running or is still
140 // being delayed as we need to override the staging value
John Reck119907c2014-08-14 09:02:01 -0700141 if (mStartTime > context.frameTimeMs()) {
John Reck8d8af3c2014-07-01 15:23:45 -0700142 setValue(mTarget, mFromValue);
John Reck68bfe0a2014-06-24 15:34:58 -0700143 return false;
144 }
John Recke45b1fd2014-04-15 09:50:16 -0700145
146 float fraction = 1.0f;
John Reck68bfe0a2014-06-24 15:34:58 -0700147 if (mPlayState == RUNNING && mDuration > 0) {
John Reck119907c2014-08-14 09:02:01 -0700148 fraction = (float)(context.frameTimeMs() - mStartTime) / mDuration;
John Recke45b1fd2014-04-15 09:50:16 -0700149 }
John Reck68bfe0a2014-06-24 15:34:58 -0700150 if (fraction >= 1.0f) {
151 fraction = 1.0f;
152 mPlayState = FINISHED;
153 }
154
John Recke45b1fd2014-04-15 09:50:16 -0700155 fraction = mInterpolator->interpolate(fraction);
John Reck8d8af3c2014-07-01 15:23:45 -0700156 setValue(mTarget, mFromValue + (mDeltaValue * fraction));
John Recke45b1fd2014-04-15 09:50:16 -0700157
158 if (mPlayState == FINISHED) {
John Reck119907c2014-08-14 09:02:01 -0700159 callOnFinishedListener(context);
John Recke45b1fd2014-04-15 09:50:16 -0700160 return true;
161 }
John Reck68bfe0a2014-06-24 15:34:58 -0700162
John Recke45b1fd2014-04-15 09:50:16 -0700163 return false;
164}
165
John Recke2478d42014-09-03 16:46:05 -0700166void BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) {
167 if (mPlayState < FINISHED) {
168 mPlayState = FINISHED;
169 callOnFinishedListener(context);
170 }
171}
172
John Reck119907c2014-08-14 09:02:01 -0700173void BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) {
John Reck52244ff2014-05-01 21:27:37 -0700174 if (mListener.get()) {
John Reck119907c2014-08-14 09:02:01 -0700175 context.callOnFinished(this, mListener.get());
John Reck52244ff2014-05-01 21:27:37 -0700176 }
177}
178
179/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700180 * RenderPropertyAnimator
181 ************************************************************/
182
John Reckff941dc2014-05-14 16:34:14 -0700183struct RenderPropertyAnimator::PropertyAccessors {
184 RenderNode::DirtyPropertyMask dirtyMask;
185 GetFloatProperty getter;
186 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700187};
188
John Reckff941dc2014-05-14 16:34:14 -0700189// Maps RenderProperty enum to accessors
190const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
191 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
192 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
193 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
194 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
195 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
196 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
197 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
198 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
199 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
200 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
201 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
202 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
203};
204
205RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
206 : BaseRenderNodeAnimator(finalValue)
207 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700208}
209
John Reck8d8af3c2014-07-01 15:23:45 -0700210void RenderPropertyAnimator::onAttached() {
John Reck68bfe0a2014-06-24 15:34:58 -0700211 if (!mHasStartValue
John Reck8d8af3c2014-07-01 15:23:45 -0700212 && mTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
213 setStartValue((mTarget->stagingProperties().*mPropertyAccess->getter)());
John Reckff941dc2014-05-14 16:34:14 -0700214 }
John Reck8d8af3c2014-07-01 15:23:45 -0700215}
216
217void RenderPropertyAnimator::onStagingPlayStateChanged() {
218 if (mStagingPlayState == RUNNING) {
219 (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
John Reck32fb6302014-07-07 09:50:32 -0700220 } else if (mStagingPlayState == FINISHED) {
221 // We're being canceled, so make sure that whatever values the UI thread
222 // is observing for us is pushed over
223 mTarget->setPropertyFieldsDirty(dirtyMask());
John Reck8d8af3c2014-07-01 15:23:45 -0700224 }
John Recke45b1fd2014-04-15 09:50:16 -0700225}
226
John Reck22184722014-06-20 07:19:30 -0700227uint32_t RenderPropertyAnimator::dirtyMask() {
228 return mPropertyAccess->dirtyMask;
229}
230
John Reckff941dc2014-05-14 16:34:14 -0700231float RenderPropertyAnimator::getValue(RenderNode* target) const {
232 return (target->properties().*mPropertyAccess->getter)();
233}
234
235void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
236 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700237}
238
John Reck52244ff2014-05-01 21:27:37 -0700239/************************************************************
240 * CanvasPropertyPrimitiveAnimator
241 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700242
John Reck52244ff2014-05-01 21:27:37 -0700243CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700244 CanvasPropertyPrimitive* property, float finalValue)
245 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700246 , mProperty(property) {
247}
248
Andreas Gampe64bb4132014-11-22 00:35:09 +0000249float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700250 return mProperty->value;
251}
252
Andreas Gampe64bb4132014-11-22 00:35:09 +0000253void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700254 mProperty->value = value;
255}
256
John Recka7c2ea22014-08-08 13:21:00 -0700257uint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() {
258 return RenderNode::DISPLAY_LIST;
259}
260
John Reck52244ff2014-05-01 21:27:37 -0700261/************************************************************
262 * CanvasPropertySkPaintAnimator
263 ************************************************************/
264
265CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700266 CanvasPropertyPaint* property, PaintField field, float finalValue)
267 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700268 , mProperty(property)
269 , mField(field) {
270}
271
Andreas Gampe64bb4132014-11-22 00:35:09 +0000272float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700273 switch (mField) {
274 case STROKE_WIDTH:
275 return mProperty->value.getStrokeWidth();
276 case ALPHA:
277 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700278 }
John Reck52244ff2014-05-01 21:27:37 -0700279 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
280 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700281}
282
John Reck531ee702014-05-13 10:06:08 -0700283static uint8_t to_uint8(float value) {
284 int c = (int) (value + .5f);
285 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
286}
287
Andreas Gampe64bb4132014-11-22 00:35:09 +0000288void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700289 switch (mField) {
290 case STROKE_WIDTH:
291 mProperty->value.setStrokeWidth(value);
292 return;
293 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700294 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700295 return;
296 }
297 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700298}
299
John Recka7c2ea22014-08-08 13:21:00 -0700300uint32_t CanvasPropertyPaintAnimator::dirtyMask() {
301 return RenderNode::DISPLAY_LIST;
302}
303
Chris Craikaf4d04c2014-07-29 12:50:14 -0700304RevealAnimator::RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700305 float startValue, float finalValue)
306 : BaseRenderNodeAnimator(finalValue)
307 , mCenterX(centerX)
Chris Craikaf4d04c2014-07-29 12:50:14 -0700308 , mCenterY(centerY) {
John Reckd3de42c2014-07-15 14:29:33 -0700309 setStartValue(startValue);
310}
311
312float RevealAnimator::getValue(RenderNode* target) const {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700313 return target->properties().getRevealClip().getRadius();
John Reckd3de42c2014-07-15 14:29:33 -0700314}
315
316void RevealAnimator::setValue(RenderNode* target, float value) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700317 target->animatorProperties().mutableRevealClip().set(true,
John Reckd3de42c2014-07-15 14:29:33 -0700318 mCenterX, mCenterY, value);
319}
320
John Recka7c2ea22014-08-08 13:21:00 -0700321uint32_t RevealAnimator::dirtyMask() {
322 return RenderNode::GENERIC;
323}
324
John Recke45b1fd2014-04-15 09:50:16 -0700325} /* namespace uirenderer */
326} /* namespace android */