blob: da65f381b514f40cd930785cfa92f0bd0eda3da6 [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 Reck119907c2014-08-14 09:02:01 -0700164void BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) {
John Reck52244ff2014-05-01 21:27:37 -0700165 if (mListener.get()) {
John Reck119907c2014-08-14 09:02:01 -0700166 context.callOnFinished(this, mListener.get());
John Reck52244ff2014-05-01 21:27:37 -0700167 }
168}
169
170/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700171 * RenderPropertyAnimator
172 ************************************************************/
173
John Reckff941dc2014-05-14 16:34:14 -0700174struct RenderPropertyAnimator::PropertyAccessors {
175 RenderNode::DirtyPropertyMask dirtyMask;
176 GetFloatProperty getter;
177 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700178};
179
John Reckff941dc2014-05-14 16:34:14 -0700180// Maps RenderProperty enum to accessors
181const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
182 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
183 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
184 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
185 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
186 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
187 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
188 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
189 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
190 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
191 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
192 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
193 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
194};
195
196RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
197 : BaseRenderNodeAnimator(finalValue)
198 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700199}
200
John Reck8d8af3c2014-07-01 15:23:45 -0700201void RenderPropertyAnimator::onAttached() {
John Reck68bfe0a2014-06-24 15:34:58 -0700202 if (!mHasStartValue
John Reck8d8af3c2014-07-01 15:23:45 -0700203 && mTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
204 setStartValue((mTarget->stagingProperties().*mPropertyAccess->getter)());
John Reckff941dc2014-05-14 16:34:14 -0700205 }
John Reck8d8af3c2014-07-01 15:23:45 -0700206}
207
208void RenderPropertyAnimator::onStagingPlayStateChanged() {
209 if (mStagingPlayState == RUNNING) {
210 (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
John Reck32fb6302014-07-07 09:50:32 -0700211 } else if (mStagingPlayState == FINISHED) {
212 // We're being canceled, so make sure that whatever values the UI thread
213 // is observing for us is pushed over
214 mTarget->setPropertyFieldsDirty(dirtyMask());
John Reck8d8af3c2014-07-01 15:23:45 -0700215 }
John Recke45b1fd2014-04-15 09:50:16 -0700216}
217
John Reck22184722014-06-20 07:19:30 -0700218uint32_t RenderPropertyAnimator::dirtyMask() {
219 return mPropertyAccess->dirtyMask;
220}
221
John Reckff941dc2014-05-14 16:34:14 -0700222float RenderPropertyAnimator::getValue(RenderNode* target) const {
223 return (target->properties().*mPropertyAccess->getter)();
224}
225
226void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
227 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700228}
229
John Reck52244ff2014-05-01 21:27:37 -0700230/************************************************************
231 * CanvasPropertyPrimitiveAnimator
232 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700233
John Reck52244ff2014-05-01 21:27:37 -0700234CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700235 CanvasPropertyPrimitive* property, float finalValue)
236 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700237 , mProperty(property) {
238}
239
John Reckff941dc2014-05-14 16:34:14 -0700240float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700241 return mProperty->value;
242}
243
John Reckff941dc2014-05-14 16:34:14 -0700244void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700245 mProperty->value = value;
246}
247
John Recka7c2ea22014-08-08 13:21:00 -0700248uint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() {
249 return RenderNode::DISPLAY_LIST;
250}
251
John Reck52244ff2014-05-01 21:27:37 -0700252/************************************************************
253 * CanvasPropertySkPaintAnimator
254 ************************************************************/
255
256CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700257 CanvasPropertyPaint* property, PaintField field, float finalValue)
258 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700259 , mProperty(property)
260 , mField(field) {
261}
262
John Reckff941dc2014-05-14 16:34:14 -0700263float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700264 switch (mField) {
265 case STROKE_WIDTH:
266 return mProperty->value.getStrokeWidth();
267 case ALPHA:
268 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700269 }
John Reck52244ff2014-05-01 21:27:37 -0700270 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
271 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700272}
273
John Reck531ee702014-05-13 10:06:08 -0700274static uint8_t to_uint8(float value) {
275 int c = (int) (value + .5f);
276 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
277}
278
John Reckff941dc2014-05-14 16:34:14 -0700279void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700280 switch (mField) {
281 case STROKE_WIDTH:
282 mProperty->value.setStrokeWidth(value);
283 return;
284 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700285 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700286 return;
287 }
288 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700289}
290
John Recka7c2ea22014-08-08 13:21:00 -0700291uint32_t CanvasPropertyPaintAnimator::dirtyMask() {
292 return RenderNode::DISPLAY_LIST;
293}
294
Chris Craikaf4d04c2014-07-29 12:50:14 -0700295RevealAnimator::RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700296 float startValue, float finalValue)
297 : BaseRenderNodeAnimator(finalValue)
298 , mCenterX(centerX)
Chris Craikaf4d04c2014-07-29 12:50:14 -0700299 , mCenterY(centerY) {
John Reckd3de42c2014-07-15 14:29:33 -0700300 setStartValue(startValue);
301}
302
303float RevealAnimator::getValue(RenderNode* target) const {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700304 return target->properties().getRevealClip().getRadius();
John Reckd3de42c2014-07-15 14:29:33 -0700305}
306
307void RevealAnimator::setValue(RenderNode* target, float value) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700308 target->animatorProperties().mutableRevealClip().set(true,
John Reckd3de42c2014-07-15 14:29:33 -0700309 mCenterX, mCenterY, value);
310}
311
John Recka7c2ea22014-08-08 13:21:00 -0700312uint32_t RevealAnimator::dirtyMask() {
313 return RenderNode::GENERIC;
314}
315
John Recke45b1fd2014-04-15 09:50:16 -0700316} /* namespace uirenderer */
317} /* namespace android */