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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/*
* Copyright (C) 2009 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.
*/
#include "config.h"
#include "AndroidAnimation.h"
#if USE(ACCELERATED_COMPOSITING)
#include "Animation.h"
#include "GraphicsLayerAndroid.h"
#include "Timer.h"
#include "TimingFunction.h"
#include "TranslateTransformOperation.h"
#include "UnitBezier.h"
#include <wtf/CurrentTime.h>
#ifdef DEBUG
#include <cutils/log.h>
#include <wtf/text/CString.h>
#undef XLOG
#define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "AndroidAnimation", __VA_ARGS__)
#else
#undef XLOG
#define XLOG(...)
#endif // DEBUG
namespace WebCore {
static long gDebugAndroidAnimationInstances;
long AndroidAnimation::instancesCount()
{
return gDebugAndroidAnimationInstances;
}
AndroidAnimation::AndroidAnimation(AnimatedPropertyID type,
const Animation* animation,
KeyframeValueList* operations,
double beginTime)
: m_beginTime(beginTime)
, m_duration(animation->duration())
, m_finished(false)
, m_iterationCount(animation->iterationCount())
, m_direction(animation->direction())
, m_timingFunction(animation->timingFunction())
, m_type(type)
, m_operations(operations)
, m_originalLayer(0)
{
ASSERT(m_timingFunction);
if (!static_cast<int>(beginTime)) // time not set
m_beginTime = WTF::currentTime();
gDebugAndroidAnimationInstances++;
}
AndroidAnimation::AndroidAnimation(AndroidAnimation* anim)
: m_beginTime(anim->m_beginTime)
, m_duration(anim->m_duration)
, m_finished(anim->m_finished)
, m_iterationCount(anim->m_iterationCount)
, m_direction(anim->m_direction)
, m_timingFunction(anim->m_timingFunction)
, m_type(anim->m_type)
, m_operations(anim->m_operations)
, m_originalLayer(0)
{
gDebugAndroidAnimationInstances++;
}
AndroidAnimation::~AndroidAnimation()
{
gDebugAndroidAnimationInstances--;
}
double AndroidAnimation::elapsedTime(double time)
{
if (m_beginTime <= 0.000001) // overflow or not correctly set
m_beginTime = time;
m_elapsedTime = time - m_beginTime;
if (m_duration <= 0)
m_duration = 0.000001;
if (m_elapsedTime < 0) // animation not yet started.
return 0;
return m_elapsedTime;
}
bool AndroidAnimation::checkIterationsAndProgress(double time, float* finalProgress)
{
double progress = elapsedTime(time);
double dur = m_duration;
if (m_iterationCount > 0)
dur *= m_iterationCount;
if (m_duration <= 0)
return false;
// If not infinite, return false if we are done
if (m_iterationCount > 0 && progress > dur) {
*finalProgress = 1.0;
return false;
}
double fractionalTime = progress / m_duration;
int integralTime = static_cast<int>(fractionalTime);
fractionalTime -= integralTime;
if ((m_direction == Animation::AnimationDirectionAlternate) && (integralTime & 1))
fractionalTime = 1 - fractionalTime;
*finalProgress = fractionalTime;
return true;
}
double AndroidAnimation::applyTimingFunction(float from, float to, double progress,
const TimingFunction* tf)
{
double fractionalTime = progress;
double offset = from;
double scale = 1.0 / (to - from);
if (scale != 1 || offset)
fractionalTime = (fractionalTime - offset) * scale;
const TimingFunction* timingFunction = tf;
if (!timingFunction)
timingFunction = m_timingFunction.get();
if (timingFunction && timingFunction->isCubicBezierTimingFunction()) {
const CubicBezierTimingFunction* bezierFunction = static_cast<const CubicBezierTimingFunction*>(timingFunction);
UnitBezier bezier(bezierFunction->x1(),
bezierFunction->y1(),
bezierFunction->x2(),
bezierFunction->y2());
if (m_duration > 0)
fractionalTime = bezier.solve(fractionalTime, 1.0f / (200.0f * m_duration));
} else if (timingFunction && timingFunction->isStepsTimingFunction()) {
const StepsTimingFunction* stepFunction = static_cast<const StepsTimingFunction*>(timingFunction);
if (stepFunction->stepAtStart()) {
fractionalTime = (floor(stepFunction->numberOfSteps() * fractionalTime) + 1) / stepFunction->numberOfSteps();
if (fractionalTime > 1.0)
fractionalTime = 1.0;
} else {
fractionalTime = floor(stepFunction->numberOfSteps() * fractionalTime) / stepFunction->numberOfSteps();
}
}
return fractionalTime;
}
PassRefPtr<AndroidOpacityAnimation> AndroidOpacityAnimation::create(
const Animation* animation,
KeyframeValueList* operations,
double beginTime)
{
return adoptRef(new AndroidOpacityAnimation(animation, operations,
beginTime));
}
AndroidOpacityAnimation::AndroidOpacityAnimation(const Animation* animation,
KeyframeValueList* operations,
double beginTime)
: AndroidAnimation(AnimatedPropertyOpacity, animation, operations, beginTime)
{
}
AndroidOpacityAnimation::AndroidOpacityAnimation(AndroidOpacityAnimation* anim)
: AndroidAnimation(anim)
{
}
PassRefPtr<AndroidAnimation> AndroidOpacityAnimation::copy()
{
return adoptRef(new AndroidOpacityAnimation(this));
}
void AndroidAnimation::pickValues(double progress, int* start, int* end)
{
float distance = -1;
unsigned int foundAt = 0;
for (unsigned int i = 0; i < m_operations->size(); i++) {
const AnimationValue* value = m_operations->at(i);
float key = value->keyTime();
float d = progress - key;
if (distance == -1 || (d >= 0 && d < distance && i + 1 < m_operations->size())) {
distance = d;
foundAt = i;
}
}
*start = foundAt;
if (foundAt + 1 < m_operations->size())
*end = foundAt + 1;
else
*end = foundAt;
}
bool AndroidOpacityAnimation::evaluate(LayerAndroid* layer, double time)
{
float progress;
if (!checkIterationsAndProgress(time, &progress))
return false;
if (progress < 0) // we still want to be evaluated until we get progress > 0
return true;
if (progress >= 1) {
m_finished = true;
if (layer != m_originalLayer)
return false;
}
if (!m_originalLayer)
m_originalLayer = layer;
// First, we need to get the from and to values
int from, to;
pickValues(progress, &from, &to);
FloatAnimationValue* fromValue = (FloatAnimationValue*) m_operations->at(from);
FloatAnimationValue* toValue = (FloatAnimationValue*) m_operations->at(to);
XLOG("[layer %d] opacity fromValue %x, key %.2f, toValue %x, key %.2f for progress %.2f",
layer->uniqueId(),
fromValue, fromValue->keyTime(),
toValue, toValue->keyTime(), progress);
// We now have the correct two values to work with, let's compute the
// progress value
const TimingFunction* timingFunction = fromValue->timingFunction();
progress = applyTimingFunction(fromValue->keyTime(), toValue->keyTime(),
progress, timingFunction);
float value = fromValue->value() + ((toValue->value() - fromValue->value()) * progress);
layer->setOpacity(value);
XLOG("AndroidOpacityAnimation::evaluate(%p, %p, %.2f) value=%.6f", this, layer, time, value);
return true;
}
PassRefPtr<AndroidTransformAnimation> AndroidTransformAnimation::create(
const Animation* animation,
KeyframeValueList* operations,
double beginTime)
{
return adoptRef(new AndroidTransformAnimation(animation, operations, beginTime));
}
AndroidTransformAnimation::AndroidTransformAnimation(const Animation* animation,
KeyframeValueList* operations,
double beginTime)
: AndroidAnimation(AnimatedPropertyWebkitTransform, animation, operations, beginTime)
{
}
AndroidTransformAnimation::AndroidTransformAnimation(AndroidTransformAnimation* anim)
: AndroidAnimation(anim)
{
}
PassRefPtr<AndroidAnimation> AndroidTransformAnimation::copy()
{
return adoptRef(new AndroidTransformAnimation(this));
}
bool AndroidTransformAnimation::evaluate(LayerAndroid* layer, double time)
{
float progress;
bool ret = true;
if (!checkIterationsAndProgress(time, &progress)) {
m_finished = true;
ret = false;
}
if (progress < 0) // we still want to be evaluated until we get progress > 0
return true;
if (progress >= 1) {
m_finished = true;
if (layer != m_originalLayer)
return false;
}
if (!m_originalLayer)
m_originalLayer = layer;
IntSize size(layer->getSize().width(), layer->getSize().height());
TransformationMatrix matrix;
XLOG("Evaluate transforms animations, %d operations, progress %.2f for layer %d (%d, %d)"
, m_operations->size(), progress, layer->uniqueId(), size.width(), size.height());
if (!m_operations->size())
return false;
// First, we need to get the from and to values
int from, to;
pickValues(progress, &from, &to);
TransformAnimationValue* fromValue = (TransformAnimationValue*) m_operations->at(from);
TransformAnimationValue* toValue = (TransformAnimationValue*) m_operations->at(to);
XLOG("[layer %d] fromValue %x, key %.2f, toValue %x, key %.2f for progress %.2f",
layer->uniqueId(),
fromValue, fromValue->keyTime(),
toValue, toValue->keyTime(), progress);
// We now have the correct two values to work with, let's compute the
// progress value
const TimingFunction* timingFunction = fromValue->timingFunction();
float p = applyTimingFunction(fromValue->keyTime(), toValue->keyTime(),
progress, timingFunction);
XLOG("progress %.2f => %.2f from: %.2f to: %.2f", progress, p, fromValue->keyTime(),
toValue->keyTime());
progress = p;
// With both values and the progress, we also need to check out that
// the operations are compatible (i.e. we are animating the same number
// of values; if not we do a matrix blend)
TransformationMatrix transformMatrix;
bool valid = true;
unsigned int fromSize = fromValue->value()->size();
if (fromSize) {
if (toValue->value()->size() != fromSize)
valid = false;
else {
for (unsigned int j = 0; j < fromSize && valid; j++) {
if (!fromValue->value()->operations()[j]->isSameType(
*toValue->value()->operations()[j]))
valid = false;
}
}
}
if (valid) {
for (size_t i = 0; i < toValue->value()->size(); ++i)
toValue->value()->operations()[i]->blend(fromValue->value()->at(i),
progress)->apply(transformMatrix, size);
} else {
TransformationMatrix source;
fromValue->value()->apply(size, source);
toValue->value()->apply(size, transformMatrix);
transformMatrix.blend(source, progress);
}
// Set the final transform on the layer
layer->setTransform(transformMatrix);
return ret;
}
} // namespace WebCore
#endif // USE(ACCELERATED_COMPOSITING)
|