blob: b80f7e93694328ecfc48e4bdd8f5134d34fab42d [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
21#include <set>
22
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)
34 : mFinalValue(finalValue)
35 , mDeltaValue(0)
36 , mFromValue(0)
37 , mInterpolator(0)
38 , mPlayState(NEEDS_START)
John Recke45b1fd2014-04-15 09:50:16 -070039 , mStartTime(0)
Alan Viverettead2f8e32014-05-16 13:28:33 -070040 , mDelayUntil(0)
41 , mDuration(300)
42 , mStartDelay(0) {
43
John Recke45b1fd2014-04-15 09:50:16 -070044}
45
John Reckff941dc2014-05-14 16:34:14 -070046BaseRenderNodeAnimator::~BaseRenderNodeAnimator() {
John Recke45b1fd2014-04-15 09:50:16 -070047 setInterpolator(NULL);
48}
49
John Reckff941dc2014-05-14 16:34:14 -070050void BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) {
John Recke45b1fd2014-04-15 09:50:16 -070051 delete mInterpolator;
52 mInterpolator = interpolator;
53}
54
John Reckff941dc2014-05-14 16:34:14 -070055void BaseRenderNodeAnimator::setStartValue(float value) {
56 LOG_ALWAYS_FATAL_IF(mPlayState != NEEDS_START,
57 "Cannot set the start value after the animator has started!");
58 mFromValue = value;
59 mDeltaValue = (mFinalValue - mFromValue);
60 mPlayState = PENDING;
61}
62
63void BaseRenderNodeAnimator::setupStartValueIfNecessary(RenderNode* target, TreeInfo& info) {
64 if (mPlayState == NEEDS_START) {
65 setStartValue(getValue(target));
66 mPlayState = PENDING;
67 }
68}
69
Alan Viverettead2f8e32014-05-16 13:28:33 -070070void BaseRenderNodeAnimator::setDuration(nsecs_t duration) {
71 mDuration = duration;
72}
73
74void BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) {
75 mStartDelay = startDelay;
76}
77
John Reckff941dc2014-05-14 16:34:14 -070078bool BaseRenderNodeAnimator::animate(RenderNode* target, TreeInfo& info) {
Alan Viverettead2f8e32014-05-16 13:28:33 -070079 if (mPlayState == PENDING && mStartDelay > 0 && mDelayUntil == 0) {
80 mDelayUntil = info.frameTimeMs + mStartDelay;
81 return false;
82 }
83
84 if (mDelayUntil > info.frameTimeMs) {
85 return false;
86 }
87
John Recke45b1fd2014-04-15 09:50:16 -070088 if (mPlayState == PENDING) {
89 mPlayState = RUNNING;
John Reck52244ff2014-05-01 21:27:37 -070090 mStartTime = info.frameTimeMs;
John Recke45b1fd2014-04-15 09:50:16 -070091 // No interpolator was set, use the default
92 if (!mInterpolator) {
93 setInterpolator(Interpolator::createDefaultInterpolator());
94 }
John Recke45b1fd2014-04-15 09:50:16 -070095 }
96
97 float fraction = 1.0f;
98 if (mPlayState == RUNNING) {
John Reck52244ff2014-05-01 21:27:37 -070099 fraction = mDuration > 0 ? (float)(info.frameTimeMs - mStartTime) / mDuration : 1.0f;
John Recke45b1fd2014-04-15 09:50:16 -0700100 if (fraction >= 1.0f) {
101 fraction = 1.0f;
102 mPlayState = FINISHED;
103 }
104 }
105 fraction = mInterpolator->interpolate(fraction);
John Reckff941dc2014-05-14 16:34:14 -0700106 setValue(target, mFromValue + (mDeltaValue * fraction));
John Recke45b1fd2014-04-15 09:50:16 -0700107
108 if (mPlayState == FINISHED) {
John Reck52244ff2014-05-01 21:27:37 -0700109 callOnFinishedListener(info);
John Recke45b1fd2014-04-15 09:50:16 -0700110 return true;
111 }
112 return false;
113}
114
John Reckff941dc2014-05-14 16:34:14 -0700115void BaseRenderNodeAnimator::callOnFinishedListener(TreeInfo& info) {
John Reck52244ff2014-05-01 21:27:37 -0700116 if (mListener.get()) {
117 if (!info.animationHook) {
118 mListener->onAnimationFinished(this);
119 } else {
120 info.animationHook->callOnFinished(this, mListener.get());
121 }
122 }
123}
124
125/************************************************************
John Recke45b1fd2014-04-15 09:50:16 -0700126 * RenderPropertyAnimator
127 ************************************************************/
128
John Reckff941dc2014-05-14 16:34:14 -0700129struct RenderPropertyAnimator::PropertyAccessors {
130 RenderNode::DirtyPropertyMask dirtyMask;
131 GetFloatProperty getter;
132 SetFloatProperty setter;
John Reck52244ff2014-05-01 21:27:37 -0700133};
134
John Reckff941dc2014-05-14 16:34:14 -0700135// Maps RenderProperty enum to accessors
136const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
137 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
138 {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
139 {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
140 {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX },
141 {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY },
142 {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation },
143 {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX },
144 {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY },
145 {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX },
146 {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY },
147 {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ },
148 {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha },
149};
150
151RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue)
152 : BaseRenderNodeAnimator(finalValue)
153 , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) {
John Recke45b1fd2014-04-15 09:50:16 -0700154}
155
John Reckff941dc2014-05-14 16:34:14 -0700156void RenderPropertyAnimator::onAttached(RenderNode* target) {
157 if (target->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) {
158 setStartValue((target->stagingProperties().*mPropertyAccess->getter)());
159 }
160 (target->mutateStagingProperties().*mPropertyAccess->setter)(finalValue());
John Recke45b1fd2014-04-15 09:50:16 -0700161}
162
John Reckff941dc2014-05-14 16:34:14 -0700163float RenderPropertyAnimator::getValue(RenderNode* target) const {
164 return (target->properties().*mPropertyAccess->getter)();
165}
166
167void RenderPropertyAnimator::setValue(RenderNode* target, float value) {
168 (target->animatorProperties().*mPropertyAccess->setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700169}
170
John Reck52244ff2014-05-01 21:27:37 -0700171/************************************************************
172 * CanvasPropertyPrimitiveAnimator
173 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700174
John Reck52244ff2014-05-01 21:27:37 -0700175CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700176 CanvasPropertyPrimitive* property, float finalValue)
177 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700178 , mProperty(property) {
179}
180
John Reckff941dc2014-05-14 16:34:14 -0700181float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700182 return mProperty->value;
183}
184
John Reckff941dc2014-05-14 16:34:14 -0700185void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700186 mProperty->value = value;
187}
188
189/************************************************************
190 * CanvasPropertySkPaintAnimator
191 ************************************************************/
192
193CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
John Reckff941dc2014-05-14 16:34:14 -0700194 CanvasPropertyPaint* property, PaintField field, float finalValue)
195 : BaseRenderNodeAnimator(finalValue)
John Reck52244ff2014-05-01 21:27:37 -0700196 , mProperty(property)
197 , mField(field) {
198}
199
John Reckff941dc2014-05-14 16:34:14 -0700200float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const {
John Reck52244ff2014-05-01 21:27:37 -0700201 switch (mField) {
202 case STROKE_WIDTH:
203 return mProperty->value.getStrokeWidth();
204 case ALPHA:
205 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700206 }
John Reck52244ff2014-05-01 21:27:37 -0700207 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
208 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700209}
210
John Reck531ee702014-05-13 10:06:08 -0700211static uint8_t to_uint8(float value) {
212 int c = (int) (value + .5f);
213 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
214}
215
John Reckff941dc2014-05-14 16:34:14 -0700216void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) {
John Reck52244ff2014-05-01 21:27:37 -0700217 switch (mField) {
218 case STROKE_WIDTH:
219 mProperty->value.setStrokeWidth(value);
220 return;
221 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700222 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700223 return;
224 }
225 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700226}
227
228} /* namespace uirenderer */
229} /* namespace android */