blob: a033f86e7d0802b92deb81c15824821fc7e68edf [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 Recke45b1fd2014-04-15 09:50:16 -070030 * Base animator
31 ************************************************************/
32
33BaseAnimator::BaseAnimator()
34 : mInterpolator(0)
35 , mPlayState(PENDING)
36 , mStartTime(0)
37 , mDuration(300) {
38
39}
40
41BaseAnimator::~BaseAnimator() {
42 setInterpolator(NULL);
43}
44
45void BaseAnimator::setInterpolator(Interpolator* interpolator) {
46 delete mInterpolator;
47 mInterpolator = interpolator;
48}
49
50void BaseAnimator::setDuration(nsecs_t duration) {
51 mDuration = duration;
52}
53
John Reck52244ff2014-05-01 21:27:37 -070054bool BaseAnimator::animateFrame(TreeInfo& info) {
John Recke45b1fd2014-04-15 09:50:16 -070055 if (mPlayState == PENDING) {
56 mPlayState = RUNNING;
John Reck52244ff2014-05-01 21:27:37 -070057 mStartTime = info.frameTimeMs;
John Recke45b1fd2014-04-15 09:50:16 -070058 // No interpolator was set, use the default
59 if (!mInterpolator) {
60 setInterpolator(Interpolator::createDefaultInterpolator());
61 }
62 onAnimationStarted();
63 }
64
65 float fraction = 1.0f;
66 if (mPlayState == RUNNING) {
John Reck52244ff2014-05-01 21:27:37 -070067 fraction = mDuration > 0 ? (float)(info.frameTimeMs - mStartTime) / mDuration : 1.0f;
John Recke45b1fd2014-04-15 09:50:16 -070068 if (fraction >= 1.0f) {
69 fraction = 1.0f;
70 mPlayState = FINISHED;
71 }
72 }
73 fraction = mInterpolator->interpolate(fraction);
74 onAnimationUpdated(fraction);
75
76 if (mPlayState == FINISHED) {
77 onAnimationFinished();
John Reck52244ff2014-05-01 21:27:37 -070078 callOnFinishedListener(info);
John Recke45b1fd2014-04-15 09:50:16 -070079 return true;
80 }
81 return false;
82}
83
John Reck52244ff2014-05-01 21:27:37 -070084void BaseAnimator::callOnFinishedListener(TreeInfo& info) {
85 if (mListener.get()) {
86 if (!info.animationHook) {
87 mListener->onAnimationFinished(this);
88 } else {
89 info.animationHook->callOnFinished(this, mListener.get());
90 }
91 }
92}
93
94/************************************************************
95 * BaseRenderNodeAnimator
96 ************************************************************/
97
98BaseRenderNodeAnimator::BaseRenderNodeAnimator(
99 BaseRenderNodeAnimator::DeltaValueType deltaType, float delta)
100 : mTarget(0)
101 , mDeltaValueType(deltaType)
102 , mDeltaValue(delta)
103 , mFromValue(-1) {
104}
105
106bool BaseRenderNodeAnimator::animate(RenderNode* target, TreeInfo& info) {
107 mTarget = target;
108 bool finished = animateFrame(info);
109 mTarget = NULL;
110 return finished;
111}
112
113void BaseRenderNodeAnimator::onAnimationStarted() {
114 mFromValue = getValue();
115
116 if (mDeltaValueType == BaseRenderNodeAnimator::ABSOLUTE) {
117 mDeltaValue = (mDeltaValue - mFromValue);
118 mDeltaValueType = BaseRenderNodeAnimator::DELTA;
119 }
120}
121
122void BaseRenderNodeAnimator::onAnimationUpdated(float fraction) {
123 float value = mFromValue + (mDeltaValue * fraction);
124 setValue(value);
125}
126
John Recke45b1fd2014-04-15 09:50:16 -0700127/************************************************************
128 * RenderPropertyAnimator
129 ************************************************************/
130
John Reck52244ff2014-05-01 21:27:37 -0700131// Maps RenderProperty enum to accessors
132const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
133 {&RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
134 {&RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
135 {&RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
136 {&RenderProperties::getScaleX, &RenderProperties::setScaleX },
137 {&RenderProperties::getScaleY, &RenderProperties::setScaleY },
138 {&RenderProperties::getRotation, &RenderProperties::setRotation },
139 {&RenderProperties::getRotationX, &RenderProperties::setRotationX },
140 {&RenderProperties::getRotationY, &RenderProperties::setRotationY },
141 {&RenderProperties::getX, &RenderProperties::setX },
142 {&RenderProperties::getY, &RenderProperties::setY },
143 {&RenderProperties::getZ, &RenderProperties::setZ },
144 {&RenderProperties::getAlpha, &RenderProperties::setAlpha },
145};
146
147RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property,
148 DeltaValueType deltaType, float deltaValue)
149 : BaseRenderNodeAnimator(deltaType, deltaValue)
150 , mPropertyAccess(PROPERTY_ACCESSOR_LUT[property]) {
John Recke45b1fd2014-04-15 09:50:16 -0700151}
152
John Reck52244ff2014-05-01 21:27:37 -0700153float RenderPropertyAnimator::getValue() const {
154 return (target()->animatorProperties().*mPropertyAccess.getter)();
John Recke45b1fd2014-04-15 09:50:16 -0700155}
156
John Reck52244ff2014-05-01 21:27:37 -0700157void RenderPropertyAnimator::setValue(float value) {
158 (target()->animatorProperties().*mPropertyAccess.setter)(value);
John Recke45b1fd2014-04-15 09:50:16 -0700159}
160
John Reck52244ff2014-05-01 21:27:37 -0700161/************************************************************
162 * CanvasPropertyPrimitiveAnimator
163 ************************************************************/
John Recke45b1fd2014-04-15 09:50:16 -0700164
John Reck52244ff2014-05-01 21:27:37 -0700165CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
166 CanvasPropertyPrimitive* property, DeltaValueType deltaType, float deltaValue)
167 : BaseRenderNodeAnimator(deltaType, deltaValue)
168 , mProperty(property) {
169}
170
171float CanvasPropertyPrimitiveAnimator::getValue() const {
172 return mProperty->value;
173}
174
175void CanvasPropertyPrimitiveAnimator::setValue(float value) {
176 mProperty->value = value;
177}
178
179/************************************************************
180 * CanvasPropertySkPaintAnimator
181 ************************************************************/
182
183CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
184 CanvasPropertyPaint* property, PaintField field,
185 DeltaValueType deltaType, float deltaValue)
186 : BaseRenderNodeAnimator(deltaType, deltaValue)
187 , mProperty(property)
188 , mField(field) {
189}
190
191float CanvasPropertyPaintAnimator::getValue() const {
192 switch (mField) {
193 case STROKE_WIDTH:
194 return mProperty->value.getStrokeWidth();
195 case ALPHA:
196 return mProperty->value.getAlpha();
John Recke45b1fd2014-04-15 09:50:16 -0700197 }
John Reck52244ff2014-05-01 21:27:37 -0700198 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
199 return -1;
John Recke45b1fd2014-04-15 09:50:16 -0700200}
201
John Reck531ee702014-05-13 10:06:08 -0700202static uint8_t to_uint8(float value) {
203 int c = (int) (value + .5f);
204 return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
205}
206
John Reck52244ff2014-05-01 21:27:37 -0700207void CanvasPropertyPaintAnimator::setValue(float value) {
208 switch (mField) {
209 case STROKE_WIDTH:
210 mProperty->value.setStrokeWidth(value);
211 return;
212 case ALPHA:
John Reck531ee702014-05-13 10:06:08 -0700213 mProperty->value.setAlpha(to_uint8(value));
John Reck52244ff2014-05-01 21:27:37 -0700214 return;
215 }
216 LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
John Recke45b1fd2014-04-15 09:50:16 -0700217}
218
219} /* namespace uirenderer */
220} /* namespace android */