blob: b6fbf891f84d663f6b6fd8b9f14c47c35c84c304 [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)
Chris Craikd41c4d82015-01-05 15:51:13 -080035 : mTarget(nullptr)
Doris Liu8b083202016-02-19 21:46:06 +000036 , mStagingTarget(nullptr)
John Reck8d8af3c2014-07-01 15:23:45 -070037 , mFinalValue(finalValue)
John Reckff941dc2014-05-14 16:34:14 -070038 , mDeltaValue(0)
39 , mFromValue(0)
Chris Craikb9ce116d2015-08-20 15:14:06 -070040 , mStagingPlayState(PlayState::NotStarted)
41 , mPlayState(PlayState::NotStarted)
John Reck68bfe0a2014-06-24 15:34:58 -070042 , 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)
Doris Liuc4bb1852016-02-19 21:39:21 +000046 , mMayRunAsync(true)
47 , mPlayTime(0) {
John Recke45b1fd2014-04-15 09:50:16 -070048}
49
John Reckff941dc2014-05-14 16:34:14 -070050BaseRenderNodeAnimator::~BaseRenderNodeAnimator() {
John Reck68bfe0a2014-06-24 15:34:58 -070051}
52
53void BaseRenderNodeAnimator::checkMutable() {
54 // Should be impossible to hit as the Java-side also has guards for this
Chris Craikb9ce116d2015-08-20 15:14:06 -070055 LOG_ALWAYS_FATAL_IF(mStagingPlayState != PlayState::NotStarted,
John Reck68bfe0a2014-06-24 15:34:58 -070056 "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();
Chris Craik51d6a3d2014-12-22 17:16:56 -080061 mInterpolator.reset(interpolator);
John Recke45b1fd2014-04-15 09:50:16 -070062}
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) {
Doris Liu8b083202016-02-19 21:46:06 +000086 mStagingTarget = target;
John Reck8d8af3c2014-07-01 15:23:45 -070087 onAttached();
88}
89
Doris Liuc4bb1852016-02-19 21:39:21 +000090void BaseRenderNodeAnimator::start() {
91 mStagingPlayState = PlayState::Running;
92 mStagingRequests.push_back(Request::Start);
93 onStagingPlayStateChanged();
94}
95
96void BaseRenderNodeAnimator::cancel() {
97 mStagingPlayState = PlayState::Finished;
98 mStagingRequests.push_back(Request::Cancel);
99 onStagingPlayStateChanged();
100}
101
102void BaseRenderNodeAnimator::reset() {
103 mStagingPlayState = PlayState::Finished;
104 mStagingRequests.push_back(Request::Reset);
105 onStagingPlayStateChanged();
106}
107
108void BaseRenderNodeAnimator::reverse() {
109 mStagingPlayState = PlayState::Reversing;
110 mStagingRequests.push_back(Request::Reverse);
111 onStagingPlayStateChanged();
112}
113
114void BaseRenderNodeAnimator::end() {
115 mStagingPlayState = PlayState::Finished;
116 mStagingRequests.push_back(Request::End);
117 onStagingPlayStateChanged();
118}
119
120void BaseRenderNodeAnimator::resolveStagingRequest(Request request) {
121 switch (request) {
122 case Request::Start:
123 mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ?
124 mPlayTime : 0;
125 mPlayState = PlayState::Running;
Doris Liu6725d582016-08-04 13:20:17 -0700126 mPendingActionUponFinish = Action::None;
Doris Liuc4bb1852016-02-19 21:39:21 +0000127 break;
128 case Request::Reverse:
129 mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ?
130 mPlayTime : mDuration;
131 mPlayState = PlayState::Reversing;
Doris Liu6725d582016-08-04 13:20:17 -0700132 mPendingActionUponFinish = Action::None;
Doris Liuc4bb1852016-02-19 21:39:21 +0000133 break;
134 case Request::Reset:
135 mPlayTime = 0;
136 mPlayState = PlayState::Finished;
Doris Liu6725d582016-08-04 13:20:17 -0700137 mPendingActionUponFinish = Action::Reset;
Doris Liuc4bb1852016-02-19 21:39:21 +0000138 break;
139 case Request::Cancel:
140 mPlayState = PlayState::Finished;
Doris Liu6725d582016-08-04 13:20:17 -0700141 mPendingActionUponFinish = Action::None;
Doris Liuc4bb1852016-02-19 21:39:21 +0000142 break;
143 case Request::End:
144 mPlayTime = mPlayState == PlayState::Reversing ? 0 : mDuration;
145 mPlayState = PlayState::Finished;
Doris Liu6725d582016-08-04 13:20:17 -0700146 mPendingActionUponFinish = Action::End;
Doris Liuc4bb1852016-02-19 21:39:21 +0000147 break;
148 default:
149 LOG_ALWAYS_FATAL("Invalid staging request: %d", static_cast<int>(request));
150 };
151}
152
John Reck119907c2014-08-14 09:02:01 -0700153void BaseRenderNodeAnimator::pushStaging(AnimationContext& context) {
Doris Liu8b083202016-02-19 21:46:06 +0000154 if (mStagingTarget) {
155 RenderNode* oldTarget = mTarget;
156 mTarget = mStagingTarget;
157 mStagingTarget = nullptr;
158 if (oldTarget && oldTarget != mTarget) {
159 oldTarget->onAnimatorTargetChanged(this);
160 }
161 }
162
John Reck68bfe0a2014-06-24 15:34:58 -0700163 if (!mHasStartValue) {
John Reck8d8af3c2014-07-01 15:23:45 -0700164 doSetStartValue(getValue(mTarget));
Alan Viverettead2f8e32014-05-16 13:28:33 -0700165 }
Doris Liuc4bb1852016-02-19 21:39:21 +0000166
167 if (!mStagingRequests.empty()) {
Doris Liu148f57f2016-02-19 17:19:24 -0800168 // No interpolator was set, use the default
169 if (mPlayState == PlayState::NotStarted && !mInterpolator) {
170 mInterpolator.reset(Interpolator::createDefaultInterpolator());
171 }
Doris Liuc4bb1852016-02-19 21:39:21 +0000172 // Keep track of the play state and play time before they are changed when
173 // staging requests are resolved.
174 nsecs_t currentPlayTime = mPlayTime;
175 PlayState prevFramePlayState = mPlayState;
176
177 // Resolve staging requests one by one.
178 for (Request request : mStagingRequests) {
179 resolveStagingRequest(request);
Doris Liu766431a2016-02-04 22:17:11 +0000180 }
Doris Liuc4bb1852016-02-19 21:39:21 +0000181 mStagingRequests.clear();
182
183 if (mStagingPlayState == PlayState::Finished) {
John Reck4d2c4722014-08-29 10:40:56 -0700184 callOnFinishedListener(context);
Doris Liuc4bb1852016-02-19 21:39:21 +0000185 } else if (mStagingPlayState == PlayState::Running
186 || mStagingPlayState == PlayState::Reversing) {
187 bool changed = currentPlayTime != mPlayTime || prevFramePlayState != mStagingPlayState;
188 if (prevFramePlayState != mStagingPlayState) {
189 transitionToRunning(context);
190 }
191 if (changed) {
192 // Now we need to seek to the stagingPlayTime (i.e. the animation progress that was
193 // requested from UI thread). It is achieved by modifying mStartTime, such that
194 // current time - mStartTime = stagingPlayTime (or mDuration -stagingPlayTime in the
195 // case of reversing)
196 nsecs_t currentFrameTime = context.frameTimeMs();
197 if (mPlayState == PlayState::Reversing) {
198 // Reverse is not supported for animations with a start delay, so here we
199 // assume no start delay.
200 mStartTime = currentFrameTime - (mDuration - mPlayTime);
201 } else {
202 // Animation should play forward
203 if (mPlayTime == 0) {
204 // If the request is to start from the beginning, include start delay.
205 mStartTime = currentFrameTime + mStartDelay;
206 } else {
207 // If the request is to seek to a non-zero play time, then we skip start
208 // delay.
209 mStartTime = currentFrameTime - mPlayTime;
210 }
211 }
212 }
John Recke45b1fd2014-04-15 09:50:16 -0700213 }
John Recke45b1fd2014-04-15 09:50:16 -0700214 }
Doris Liu8b083202016-02-19 21:46:06 +0000215 onPushStaging();
John Reck68bfe0a2014-06-24 15:34:58 -0700216}
217
John Reck119907c2014-08-14 09:02:01 -0700218void BaseRenderNodeAnimator::transitionToRunning(AnimationContext& context) {
219 nsecs_t frameTimeMs = context.frameTimeMs();
220 LOG_ALWAYS_FATAL_IF(frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", frameTimeMs);
John Reck68bfe0a2014-06-24 15:34:58 -0700221 if (mStartDelay < 0 || mStartDelay > 50000) {
222 ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay);
223 }
John Reck119907c2014-08-14 09:02:01 -0700224 mStartTime = frameTimeMs + mStartDelay;
John Reck68bfe0a2014-06-24 15:34:58 -0700225 if (mStartTime < 0) {
226 ALOGW("Ended up with a really weird start time of %" PRId64
227 " with frame time %" PRId64 " and start delay %" PRId64,
John Reck119907c2014-08-14 09:02:01 -0700228 mStartTime, frameTimeMs, mStartDelay);
John Reck68bfe0a2014-06-24 15:34:58 -0700229 // Set to 0 so that the animate() basically instantly finishes
230 mStartTime = 0;
231 }
Doris Liu952670d2016-04-12 17:43:00 -0700232 if (mDuration < 0) {
John Reck68bfe0a2014-06-24 15:34:58 -0700233 ALOGW("Your duration is strange and confusing: %" PRId64, mDuration);
234 }
235}
236
John Reck119907c2014-08-14 09:02:01 -0700237bool BaseRenderNodeAnimator::animate(AnimationContext& context) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700238 if (mPlayState < PlayState::Running) {
John Reck68bfe0a2014-06-24 15:34:58 -0700239 return false;
240 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700241 if (mPlayState == PlayState::Finished) {
Doris Liu6725d582016-08-04 13:20:17 -0700242 if (mPendingActionUponFinish == Action::Reset) {
243 // Skip to start.
244 updatePlayTime(0);
245 } else if (mPendingActionUponFinish == Action::End) {
246 // Skip to end.
247 updatePlayTime(mDuration);
248 }
249 // Reset pending action.
250 mPendingActionUponFinish = Action ::None;
John Reck32fb6302014-07-07 09:50:32 -0700251 return true;
252 }
John Reck68bfe0a2014-06-24 15:34:58 -0700253
Doris Liu766431a2016-02-04 22:17:11 +0000254 // This should be set before setValue() so animators can query this time when setValue
255 // is called.
Doris Liuc4bb1852016-02-19 21:39:21 +0000256 nsecs_t currentPlayTime = context.frameTimeMs() - mStartTime;
257 bool finished = updatePlayTime(currentPlayTime);
258 if (finished && mPlayState != PlayState::Finished) {
259 mPlayState = PlayState::Finished;
260 callOnFinishedListener(context);
261 }
262 return finished;
263}
Doris Liu766431a2016-02-04 22:17:11 +0000264
Doris Liuc4bb1852016-02-19 21:39:21 +0000265bool BaseRenderNodeAnimator::updatePlayTime(nsecs_t playTime) {
266 mPlayTime = mPlayState == PlayState::Reversing ? mDuration - playTime : playTime;
267 onPlayTimeChanged(mPlayTime);
John Reck8d8af3c2014-07-01 15:23:45 -0700268 // If BaseRenderNodeAnimator is handling the delay (not typical), then
269 // because the staging properties reflect the final value, we always need
270 // to call setValue even if the animation isn't yet running or is still
271 // being delayed as we need to override the staging value
Doris Liuc4bb1852016-02-19 21:39:21 +0000272 if (playTime < 0) {
John Reck8d8af3c2014-07-01 15:23:45 -0700273 setValue(mTarget, mFromValue);
John Reck68bfe0a2014-06-24 15:34:58 -0700274 return false;
275 }
John Recke45b1fd2014-04-15 09:50:16 -0700276
277 float fraction = 1.0f;
Doris Liuc4bb1852016-02-19 21:39:21 +0000278 if ((mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) && mDuration > 0) {
279 fraction = mPlayTime / (float) mDuration;
John Recke45b1fd2014-04-15 09:50:16 -0700280 }
Doris Liuc4bb1852016-02-19 21:39:21 +0000281 fraction = MathUtils::clamp(fraction, 0.0f, 1.0f);
John Reck68bfe0a2014-06-24 15:34:58 -0700282
John Recke45b1fd2014-04-15 09:50:16 -0700283 fraction = mInterpolator->interpolate(fraction);
John Reck8d8af3c2014-07-01 15:23:45 -0700284 setValue(mTarget, mFromValue + (mDeltaValue * fraction));
John Recke45b1fd2014-04-15 09:50:16 -0700285
Doris Liuc4bb1852016-02-19 21:39:21 +0000286 return playTime >= mDuration;
John Recke45b1fd2014-04-15 09:50:16 -0700287}
288
Doris Liu718cd3e2016-05-17 16:50:31 -0700289nsecs_t BaseRenderNodeAnimator::getRemainingPlayTime() {
290 return mPlayState == PlayState::Reversing ? mPlayTime : mDuration - mPlayTime;
291}
292
John Recke2478d42014-09-03 16:46:05 -0700293void BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700294 if (mPlayState < PlayState::Finished) {
295 mPlayState = PlayState::Finished;
John Recke2478d42014-09-03 16:46:05 -0700296 callOnFinishedListener(context);
297 }
298}
299
John Reck119907c2014-08-14 09:02:01 -0700300void BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) {
John Reck52244ff2014-05-01 21:27:37 -0700301 if (mListener.get()) {
John Reck119907c2014-08-14 09:02:01 -0700302 context.callOnFinished(this, mListener.get());
John Reck52244ff2014-05-01 21:27:37 -0700303 }
304}
305
306/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700307 * RenderPropertyAnimator
308 ************************************************************/
309
John Reckff941dc2014-05-14 16:34:14 -0700310struct RenderPropertyAnimator::PropertyAccessors {
311 RenderNode::DirtyPropertyMask dirtyMask;
312 GetFloatProperty getter;
313 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700314};
315
John Reckff941dc2014-05-14 16:34:14 -0700316// Maps RenderProperty enum to accessors
317const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
318 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
319 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
John Reck41282b72017-01-11 11:26:43 -0800320 {RenderNode::TRANSLATION_Z, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
John Reckff941dc2014-05-14 16:34:14 -0700321 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
322 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
323 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
324 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
325 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
326 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
327 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
328 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
329 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
330};
331
332RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
333 : BaseRenderNodeAnimator(finalValue)
334 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700335}
336
John Reck8d8af3c2014-07-01 15:23:45 -0700337void RenderPropertyAnimator::onAttached() {
John Reck68bfe0a2014-06-24 15:34:58 -0700338 if (!mHasStartValue
Doris Liu8b083202016-02-19 21:46:06 +0000339 && mStagingTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
340 setStartValue((mStagingTarget->stagingProperties().*mPropertyAccess->getter)());
John Reckff941dc2014-05-14 16:34:14 -0700341 }
John Reck8d8af3c2014-07-01 15:23:45 -0700342}
343
344void RenderPropertyAnimator::onStagingPlayStateChanged() {
Chris Craikb9ce116d2015-08-20 15:14:06 -0700345 if (mStagingPlayState == PlayState::Running) {
Doris Liu8b083202016-02-19 21:46:06 +0000346 if (mStagingTarget) {
347 (mStagingTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
348 } else {
349 // In the case of start delay where stagingTarget has been sync'ed over and null'ed
350 // we delay the properties update to push staging.
351 mShouldUpdateStagingProperties = true;
352 }
Chris Craikb9ce116d2015-08-20 15:14:06 -0700353 } else if (mStagingPlayState == PlayState::Finished) {
John Reck32fb6302014-07-07 09:50:32 -0700354 // We're being canceled, so make sure that whatever values the UI thread
355 // is observing for us is pushed over
Doris Liu8b083202016-02-19 21:46:06 +0000356 mShouldSyncPropertyFields = true;
357 }
358}
359
360void RenderPropertyAnimator::onPushStaging() {
361 if (mShouldUpdateStagingProperties) {
362 (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
363 mShouldUpdateStagingProperties = false;
364 }
365
366 if (mShouldSyncPropertyFields) {
John Reck32fb6302014-07-07 09:50:32 -0700367 mTarget->setPropertyFieldsDirty(dirtyMask());
Doris Liu8b083202016-02-19 21:46:06 +0000368 mShouldSyncPropertyFields = false;
John Reck8d8af3c2014-07-01 15:23:45 -0700369 }
John Recke45b1fd2014-04-15 09:50:16 -0700370}
371
John Reck22184722014-06-20 07:19:30 -0700372uint32_t RenderPropertyAnimator::dirtyMask() {
373 return mPropertyAccess->dirtyMask;
374}
375
John Reckff941dc2014-05-14 16:34:14 -0700376float RenderPropertyAnimator::getValue(RenderNode* target) const {
377 return (target->properties().*mPropertyAccess->getter)();
378}
379
380void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
381 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700382}
383
John Reck52244ff2014-05-01 21:27:37 -0700384/************************************************************
385 * CanvasPropertyPrimitiveAnimator
386 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700387
John Reck52244ff2014-05-01 21:27:37 -0700388CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700389 CanvasPropertyPrimitive* property, float finalValue)
390 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700391 , mProperty(property) {
392}
393
Andreas Gampe64bb4132014-11-22 00:35:09 +0000394float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700395 return mProperty->value;
396}
397
Andreas Gampe64bb4132014-11-22 00:35:09 +0000398void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700399 mProperty->value = value;
400}
401
John Recka7c2ea22014-08-08 13:21:00 -0700402uint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() {
403 return RenderNode::DISPLAY_LIST;
404}
405
John Reck52244ff2014-05-01 21:27:37 -0700406/************************************************************
407 * CanvasPropertySkPaintAnimator
408 ************************************************************/
409
410CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700411 CanvasPropertyPaint* property, PaintField field, float finalValue)
412 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700413 , mProperty(property)
414 , mField(field) {
415}
416
Andreas Gampe64bb4132014-11-22 00:35:09 +0000417float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700418 switch (mField) {
419 case STROKE_WIDTH:
420 return mProperty->value.getStrokeWidth();
421 case ALPHA:
422 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700423 }
John Reck52244ff2014-05-01 21:27:37 -0700424 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
425 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700426}
427
John Reck531ee702014-05-13 10:06:08 -0700428static uint8_t to_uint8(float value) {
429 int c = (int) (value + .5f);
430 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
431}
432
Andreas Gampe64bb4132014-11-22 00:35:09 +0000433void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700434 switch (mField) {
435 case STROKE_WIDTH:
436 mProperty->value.setStrokeWidth(value);
437 return;
438 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700439 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700440 return;
441 }
442 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700443}
444
John Recka7c2ea22014-08-08 13:21:00 -0700445uint32_t CanvasPropertyPaintAnimator::dirtyMask() {
446 return RenderNode::DISPLAY_LIST;
447}
448
Chris Craikaf4d04c2014-07-29 12:50:14 -0700449RevealAnimator::RevealAnimator(int centerX, int centerY,
John Reckd3de42c2014-07-15 14:29:33 -0700450 float startValue, float finalValue)
451 : BaseRenderNodeAnimator(finalValue)
452 , mCenterX(centerX)
Chris Craikaf4d04c2014-07-29 12:50:14 -0700453 , mCenterY(centerY) {
John Reckd3de42c2014-07-15 14:29:33 -0700454 setStartValue(startValue);
455}
456
457float RevealAnimator::getValue(RenderNode* target) const {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700458 return target->properties().getRevealClip().getRadius();
John Reckd3de42c2014-07-15 14:29:33 -0700459}
460
461void RevealAnimator::setValue(RenderNode* target, float value) {
Chris Craikaf4d04c2014-07-29 12:50:14 -0700462 target->animatorProperties().mutableRevealClip().set(true,
John Reckd3de42c2014-07-15 14:29:33 -0700463 mCenterX, mCenterY, value);
464}
465
John Recka7c2ea22014-08-08 13:21:00 -0700466uint32_t RevealAnimator::dirtyMask() {
467 return RenderNode::GENERIC;
468}
469
John Recke45b1fd2014-04-15 09:50:16 -0700470} /* namespace uirenderer */
471} /* namespace android */