blob: 24ed6cd51de03309930fe25cb63df393a1cd8055 [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
17#define LOG_TAG "RT-Animator"
18
19#include "Animator.h"
20
John Reck68bfe0a2014-06-24 15:34:58 -070021#include <inttypes.h>
John Recke45b1fd2014-04-15 09:50:16 -070022#include <set>
23
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)
45 , mStartDelay(0) {
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
90void BaseRenderNodeAnimator::pushStaging(TreeInfo& info) {
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) {
98 transitionToRunning(info);
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
103void BaseRenderNodeAnimator::transitionToRunning(TreeInfo& info) {
104 LOG_ALWAYS_FATAL_IF(info.frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", info.frameTimeMs);
105 if (mStartDelay < 0 || mStartDelay > 50000) {
106 ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay);
107 }
108 mStartTime = info.frameTimeMs + mStartDelay;
109 if (mStartTime < 0) {
110 ALOGW("Ended up with a really weird start time of %" PRId64
111 " with frame time %" PRId64 " and start delay %" PRId64,
112 mStartTime, info.frameTimeMs, mStartDelay);
113 // Set to 0 so that the animate() basically instantly finishes
114 mStartTime = 0;
115 }
116 // No interpolator was set, use the default
117 if (!mInterpolator) {
John Reck8d8af3c2014-07-01 15:23:45 -0700118 mInterpolator = Interpolator::createDefaultInterpolator();
John Reck68bfe0a2014-06-24 15:34:58 -0700119 }
120 if (mDuration < 0 || mDuration > 50000) {
121 ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
122 }
123}
124
John Reck8d8af3c2014-07-01 15:23:45 -0700125bool BaseRenderNodeAnimator::animate(TreeInfo& info) {
John Reck68bfe0a2014-06-24 15:34:58 -0700126 if (mPlayState < RUNNING) {
127 return false;
128 }
John Reck32fb6302014-07-07 09:50:32 -0700129 if (mPlayState == FINISHED) {
130 return true;
131 }
John Reck68bfe0a2014-06-24 15:34:58 -0700132
John Reck8d8af3c2014-07-01 15:23:45 -0700133 // If BaseRenderNodeAnimator is handling the delay (not typical), then
134 // because the staging properties reflect the final value, we always need
135 // to call setValue even if the animation isn't yet running or is still
136 // being delayed as we need to override the staging value
John Reck68bfe0a2014-06-24 15:34:58 -0700137 if (mStartTime > info.frameTimeMs) {
138 info.out.hasAnimations |= true;
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) {
145 fraction = (float)(info.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 Reck52244ff2014-05-01 21:27:37 -0700156 callOnFinishedListener(info);
John Recke45b1fd2014-04-15 09:50:16 -0700157 return true;
158 }
John Reck68bfe0a2014-06-24 15:34:58 -0700159
160 info.out.hasAnimations |= true;
John Recke45b1fd2014-04-15 09:50:16 -0700161 return false;
162}
163
John Reckff941dc2014-05-14 16:34:14 -0700164void BaseRenderNodeAnimator::callOnFinishedListener(TreeInfo& info) {
John Reck52244ff2014-05-01 21:27:37 -0700165 if (mListener.get()) {
166 if (!info.animationHook) {
167 mListener->onAnimationFinished(this);
168 } else {
169 info.animationHook->callOnFinished(this, mListener.get());
170 }
171 }
172}
173
174/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700175 * RenderPropertyAnimator
176 ************************************************************/
177
John Reckff941dc2014-05-14 16:34:14 -0700178struct RenderPropertyAnimator::PropertyAccessors {
179 RenderNode::DirtyPropertyMask dirtyMask;
180 GetFloatProperty getter;
181 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700182};
183
John Reckff941dc2014-05-14 16:34:14 -0700184// Maps RenderProperty enum to accessors
185const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
186 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
187 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
188 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
189 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
190 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
191 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
192 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
193 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
194 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
195 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
196 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
197 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
198};
199
200RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
201 : BaseRenderNodeAnimator(finalValue)
202 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700203}
204
John Reck8d8af3c2014-07-01 15:23:45 -0700205void RenderPropertyAnimator::onAttached() {
John Reck68bfe0a2014-06-24 15:34:58 -0700206 if (!mHasStartValue
John Reck8d8af3c2014-07-01 15:23:45 -0700207 && mTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
208 setStartValue((mTarget->stagingProperties().*mPropertyAccess->getter)());
John Reckff941dc2014-05-14 16:34:14 -0700209 }
John Reck8d8af3c2014-07-01 15:23:45 -0700210}
211
212void RenderPropertyAnimator::onStagingPlayStateChanged() {
213 if (mStagingPlayState == RUNNING) {
214 (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
John Reck32fb6302014-07-07 09:50:32 -0700215 } else if (mStagingPlayState == FINISHED) {
216 // We're being canceled, so make sure that whatever values the UI thread
217 // is observing for us is pushed over
218 mTarget->setPropertyFieldsDirty(dirtyMask());
John Reck8d8af3c2014-07-01 15:23:45 -0700219 }
John Recke45b1fd2014-04-15 09:50:16 -0700220}
221
John Reck22184722014-06-20 07:19:30 -0700222uint32_t RenderPropertyAnimator::dirtyMask() {
223 return mPropertyAccess->dirtyMask;
224}
225
John Reckff941dc2014-05-14 16:34:14 -0700226float RenderPropertyAnimator::getValue(RenderNode* target) const {
227 return (target->properties().*mPropertyAccess->getter)();
228}
229
230void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
231 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700232}
233
John Reck52244ff2014-05-01 21:27:37 -0700234/************************************************************
235 * CanvasPropertyPrimitiveAnimator
236 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700237
John Reck52244ff2014-05-01 21:27:37 -0700238CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700239 CanvasPropertyPrimitive* property, float finalValue)
240 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700241 , mProperty(property) {
242}
243
John Reckff941dc2014-05-14 16:34:14 -0700244float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700245 return mProperty->value;
246}
247
John Reckff941dc2014-05-14 16:34:14 -0700248void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700249 mProperty->value = value;
250}
251
252/************************************************************
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 Reckd3de42c2014-07-15 14:29:33 -0700291RevealAnimator::RevealAnimator(int centerX, int centerY, bool inverseClip,
292 float startValue, float finalValue)
293 : BaseRenderNodeAnimator(finalValue)
294 , mCenterX(centerX)
295 , mCenterY(centerY)
296 , mInverseClip(inverseClip) {
297 setStartValue(startValue);
298}
299
300float RevealAnimator::getValue(RenderNode* target) const {
301 return target->properties().getRevealClip().radius();
302}
303
304void RevealAnimator::setValue(RenderNode* target, float value) {
305 target->animatorProperties().mutableRevealClip().set(true, mInverseClip,
306 mCenterX, mCenterY, value);
307}
308
John Recke45b1fd2014-04-15 09:50:16 -0700309} /* namespace uirenderer */
310} /* namespace android */