1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "RT-Animator"
#include "Animator.h"
#include <set>
#include "RenderNode.h"
#include "RenderProperties.h"
namespace android {
namespace uirenderer {
/************************************************************
* Base animator
************************************************************/
BaseAnimator::BaseAnimator()
: mInterpolator(0)
, mPlayState(PENDING)
, mStartTime(0)
, mDuration(300) {
}
BaseAnimator::~BaseAnimator() {
setInterpolator(NULL);
}
void BaseAnimator::setInterpolator(Interpolator* interpolator) {
delete mInterpolator;
mInterpolator = interpolator;
}
void BaseAnimator::setDuration(nsecs_t duration) {
mDuration = duration;
}
bool BaseAnimator::animateFrame(TreeInfo& info) {
if (mPlayState == PENDING) {
mPlayState = RUNNING;
mStartTime = info.frameTimeMs;
// No interpolator was set, use the default
if (!mInterpolator) {
setInterpolator(Interpolator::createDefaultInterpolator());
}
onAnimationStarted();
}
float fraction = 1.0f;
if (mPlayState == RUNNING) {
fraction = mDuration > 0 ? (float)(info.frameTimeMs - mStartTime) / mDuration : 1.0f;
if (fraction >= 1.0f) {
fraction = 1.0f;
mPlayState = FINISHED;
}
}
fraction = mInterpolator->interpolate(fraction);
onAnimationUpdated(fraction);
if (mPlayState == FINISHED) {
onAnimationFinished();
callOnFinishedListener(info);
return true;
}
return false;
}
void BaseAnimator::callOnFinishedListener(TreeInfo& info) {
if (mListener.get()) {
if (!info.animationHook) {
mListener->onAnimationFinished(this);
} else {
info.animationHook->callOnFinished(this, mListener.get());
}
}
}
/************************************************************
* BaseRenderNodeAnimator
************************************************************/
BaseRenderNodeAnimator::BaseRenderNodeAnimator(
BaseRenderNodeAnimator::DeltaValueType deltaType, float delta)
: mTarget(0)
, mDeltaValueType(deltaType)
, mDeltaValue(delta)
, mFromValue(-1) {
}
bool BaseRenderNodeAnimator::animate(RenderNode* target, TreeInfo& info) {
mTarget = target;
bool finished = animateFrame(info);
mTarget = NULL;
return finished;
}
void BaseRenderNodeAnimator::onAnimationStarted() {
mFromValue = getValue();
if (mDeltaValueType == BaseRenderNodeAnimator::ABSOLUTE) {
mDeltaValue = (mDeltaValue - mFromValue);
mDeltaValueType = BaseRenderNodeAnimator::DELTA;
}
}
void BaseRenderNodeAnimator::onAnimationUpdated(float fraction) {
float value = mFromValue + (mDeltaValue * fraction);
setValue(value);
}
/************************************************************
* RenderPropertyAnimator
************************************************************/
// Maps RenderProperty enum to accessors
const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = {
{&RenderProperties::getTranslationX, &RenderProperties::setTranslationX },
{&RenderProperties::getTranslationY, &RenderProperties::setTranslationY },
{&RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ },
{&RenderProperties::getScaleX, &RenderProperties::setScaleX },
{&RenderProperties::getScaleY, &RenderProperties::setScaleY },
{&RenderProperties::getRotation, &RenderProperties::setRotation },
{&RenderProperties::getRotationX, &RenderProperties::setRotationX },
{&RenderProperties::getRotationY, &RenderProperties::setRotationY },
{&RenderProperties::getX, &RenderProperties::setX },
{&RenderProperties::getY, &RenderProperties::setY },
{&RenderProperties::getZ, &RenderProperties::setZ },
{&RenderProperties::getAlpha, &RenderProperties::setAlpha },
};
RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property,
DeltaValueType deltaType, float deltaValue)
: BaseRenderNodeAnimator(deltaType, deltaValue)
, mPropertyAccess(PROPERTY_ACCESSOR_LUT[property]) {
}
float RenderPropertyAnimator::getValue() const {
return (target()->animatorProperties().*mPropertyAccess.getter)();
}
void RenderPropertyAnimator::setValue(float value) {
(target()->animatorProperties().*mPropertyAccess.setter)(value);
}
/************************************************************
* CanvasPropertyPrimitiveAnimator
************************************************************/
CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator(
CanvasPropertyPrimitive* property, DeltaValueType deltaType, float deltaValue)
: BaseRenderNodeAnimator(deltaType, deltaValue)
, mProperty(property) {
}
float CanvasPropertyPrimitiveAnimator::getValue() const {
return mProperty->value;
}
void CanvasPropertyPrimitiveAnimator::setValue(float value) {
mProperty->value = value;
}
/************************************************************
* CanvasPropertySkPaintAnimator
************************************************************/
CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator(
CanvasPropertyPaint* property, PaintField field,
DeltaValueType deltaType, float deltaValue)
: BaseRenderNodeAnimator(deltaType, deltaValue)
, mProperty(property)
, mField(field) {
}
float CanvasPropertyPaintAnimator::getValue() const {
switch (mField) {
case STROKE_WIDTH:
return mProperty->value.getStrokeWidth();
case ALPHA:
return mProperty->value.getAlpha();
}
LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
return -1;
}
static uint8_t to_uint8(float value) {
int c = (int) (value + .5f);
return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c );
}
void CanvasPropertyPaintAnimator::setValue(float value) {
switch (mField) {
case STROKE_WIDTH:
mProperty->value.setStrokeWidth(value);
return;
case ALPHA:
mProperty->value.setAlpha(to_uint8(value));
return;
}
LOG_ALWAYS_FATAL("Unknown field %d", (int) mField);
}
} /* namespace uirenderer */
} /* namespace android */
|