summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/layers/AndroidAnimation.cpp
blob: 0ef84b9bb0c139c37e63aae01f29374b58523818 (plain)
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
/*
 * 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.
 */

#define LOG_TAG "AndroidAnimation"
#define LOG_NDEBUG 1

#include "config.h"
#include "AndroidAnimation.h"

#if USE(ACCELERATED_COMPOSITING)

#include "AndroidLog.h"
#include "Animation.h"
#include "GraphicsLayerAndroid.h"
#include "Timer.h"
#include "TimingFunction.h"
#include "TranslateTransformOperation.h"
#include "UnitBezier.h"

namespace WebCore {

static int gUniqueId;

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_fillsBackwards(animation->fillsBackwards())
    , m_fillsForwards(animation->fillsForwards())
    , m_iterationCount(animation->iterationCount())
    , m_direction(animation->direction())
    , m_timingFunction(animation->timingFunction())
    , m_type(type)
    , m_operations(operations)
    , m_uniqueId(++gUniqueId)
    , m_hasFinished(false)
{
    ASSERT(m_timingFunction);

    gDebugAndroidAnimationInstances++;
}

AndroidAnimation::~AndroidAnimation()
{
    gDebugAndroidAnimationInstances--;
}

void AndroidAnimation::suggestBeginTime(double time)
{
    if (m_beginTime <= 0.000001) // overflow or not yet set
        m_beginTime = time;
}

double AndroidAnimation::elapsedTime(double time)
{
    double elapsedTime = (m_beginTime < 0.000001) ? 0 : time - m_beginTime;

    if (m_duration <= 0)
      m_duration = 0.000001;

    if (elapsedTime < 0) // animation not yet started.
        return 0;

    return 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;
        if (!m_hasFinished) {
            // first time past duration, continue with progress 1.0 so the
            // element's final position lines up with it's last keyframe
            m_hasFinished = true;
            return true;
        }

        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;
}

bool AndroidAnimation::evaluate(LayerAndroid* layer, double time)
{
    float progress;
    if (!checkIterationsAndProgress(time, &progress)
        && !(m_fillsBackwards || m_fillsForwards))
        return false;

    if (progress < 0) {
        // The animation hasn't started yet
        if (m_fillsBackwards || m_beginTime <= 0.000001) {
            // in this case we want to apply the initial keyframe to the layer
            applyForProgress(layer, 0);
        }
        // we still want to be evaluated until we get progress > 0
        return true;
    }

    if (progress > 1) {
        if (!m_fillsForwards)
            return false;
        progress = 1;
    }

    if (!m_operations->size())
        return false;

    applyForProgress(layer, progress);

    return true;
}

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)
{
}

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;
}

void AndroidOpacityAnimation::applyForProgress(LayerAndroid* layer, float progress)
{
    // 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);

    ALOGV("[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);
}

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)
{
}

void AndroidTransformAnimation::applyForProgress(LayerAndroid* layer, float progress)
{
    // 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);

    ALOGV("[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);
    ALOGV("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;
            }
        }
    }

    IntSize size(layer->getSize().width(), layer->getSize().height());
    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);
}

} // namespace WebCore

#endif // USE(ACCELERATED_COMPOSITING)