File: TextureMapperAnimation.cpp

package info (click to toggle)
webkit2gtk 2.48.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 429,764 kB
  • sloc: cpp: 3,697,587; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,295; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (482 lines) | stat: -rw-r--r-- 18,318 bytes parent folder | download | duplicates (6)
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
 * Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "TextureMapperAnimation.h"

#if USE(TEXTURE_MAPPER)

#include "LayoutSize.h"
#include "TranslateTransformOperation.h"
#include <wtf/Scope.h>

namespace WebCore {

static RefPtr<FilterOperation> blendFunc(FilterOperation* fromOp, FilterOperation& toOp, double progress, const FloatSize&, bool blendToPassthrough = false)
{
    return toOp.blend(fromOp, progress, blendToPassthrough);
}

static FilterOperations applyFilterAnimation(const FilterOperations& from, const FilterOperations& to, double progress, const FloatSize& boxSize)
{
    // First frame of an animation.
    if (!progress)
        return from;

    // Last frame of an animation.
    if (progress == 1)
        return to;

    if (!from.isEmpty() && !to.isEmpty() && !from.operationsMatch(to))
        return to;

    size_t fromSize = from.size();
    size_t toSize = to.size();
    size_t size = std::max(fromSize, toSize);

    Vector<Ref<FilterOperation>> operations;
    operations.reserveInitialCapacity(size);

    for (size_t i = 0; i < size; i++) {
        RefPtr<FilterOperation> fromOp = (i < fromSize) ? from[i].ptr() : nullptr;
        RefPtr<FilterOperation> toOp = (i < toSize) ? to[i].ptr() : nullptr;
        RefPtr<FilterOperation> blendedOp = toOp ? blendFunc(fromOp.get(), *toOp, progress, boxSize) : (fromOp ? blendFunc(nullptr, *fromOp, progress, boxSize, true) : nullptr);
        if (blendedOp)
            operations.append(blendedOp.releaseNonNull());
        else {
            if (progress > 0.5) {
                if (toOp)
                    operations.append(toOp.releaseNonNull());
                else
                    operations.append(PassthroughFilterOperation::create());
            } else {
                if (fromOp)
                    operations.append(fromOp.releaseNonNull());
                else
                    operations.append(PassthroughFilterOperation::create());
            }
        }
    }

    return FilterOperations { WTFMove(operations) };
}

static bool shouldReverseAnimationValue(Animation::Direction direction, int loopCount)
{
    return (direction == Animation::Direction::Alternate && loopCount & 1)
        || (direction == Animation::Direction::AlternateReverse && !(loopCount & 1))
        || direction == Animation::Direction::Reverse;
}

static double normalizedAnimationValue(double runningTime, double duration, Animation::Direction direction, double iterationCount)
{
    if (!duration)
        return 0;

    const int loopCount = runningTime / duration;
    const double lastFullLoop = duration * double(loopCount);
    const double remainder = runningTime - lastFullLoop;
    // Ignore remainder when we've reached the end of animation.
    const double normalized = (loopCount == iterationCount) ? 1.0 : (remainder / duration);

    return shouldReverseAnimationValue(direction, loopCount) ? 1 - normalized : normalized;
}

static double normalizedAnimationValueForFillsForwards(double iterationCount, Animation::Direction direction)
{
    if (direction == Animation::Direction::Normal)
        return 1;
    if (direction == Animation::Direction::Reverse)
        return 0;
    return shouldReverseAnimationValue(direction, iterationCount) ? 1 : 0;
}

static float applyOpacityAnimation(float fromOpacity, float toOpacity, double progress)
{
    // Optimization: special case the edge values (0 and 1).
    if (progress == 1.0)
        return toOpacity;

    if (!progress)
        return fromOpacity;

    return fromOpacity + progress * (toOpacity - fromOpacity);
}

static TransformationMatrix applyTransformAnimation(const TransformOperations& from, const TransformOperations& to, double progress, const FloatSize& boxSize)
{
    TransformationMatrix matrix;

    // First frame of an animation.
    if (!progress) {
        from.apply(matrix, boxSize);
        return matrix;
    }

    // Last frame of an animation.
    if (progress == 1) {
        to.apply(matrix, boxSize);
        return matrix;
    }

    to.blend(from, progress, LayoutSize { boxSize }).apply(matrix, boxSize);
    return matrix;
}

static const TimingFunction& timingFunctionForAnimationValue(const AnimationValue& animationValue, const TextureMapperAnimation& animation)
{
    if (auto* function = animationValue.timingFunction())
        return *function;
    if (auto* function = animation.timingFunction())
        return *function;
    return CubicBezierTimingFunction::defaultTimingFunction();
}

static KeyframeValueList createThreadsafeKeyFrames(const KeyframeValueList& originalKeyframes, const FloatSize& boxSize)
{
    if (originalKeyframes.property() != AnimatedProperty::Transform)
        return originalKeyframes;

    // Currently translation operations are the only transform operations that store a non-fixed
    // Length. Some Lengths, in particular those for calc() operations, are not thread-safe or
    // multiprocess safe, because they maintain indices into a shared UncheckedKeyHashMap of CalculationValues.
    // This code converts all possible unsafe Length parameters to fixed Lengths, which are safe to
    // use in other threads and across IPC channels.
    KeyframeValueList keyframes = originalKeyframes;
    for (unsigned i = 0; i < keyframes.size(); i++) {
        const auto& transformValue = static_cast<const TransformAnimationValue&>(keyframes.at(i));
        for (auto& operation : transformValue.value()) {
            if (RefPtr translation = dynamicDowncast<TranslateTransformOperation>(operation)) {
                translation->setX(Length(translation->xAsFloat(boxSize), LengthType::Fixed));
                translation->setY(Length(translation->yAsFloat(boxSize), LengthType::Fixed));
                translation->setZ(Length(translation->zAsFloat(), LengthType::Fixed));
            }
        }
    }

    return keyframes;
}

TextureMapperAnimation::TextureMapperAnimation(const String& name, const KeyframeValueList& keyframes, const FloatSize& boxSize, const Animation& animation, MonotonicTime startTime, Seconds pauseTime, State state)
    : m_name(name.isSafeToSendToAnotherThread() ? name : name.isolatedCopy())
    , m_keyframes(createThreadsafeKeyFrames(keyframes, boxSize))
    , m_boxSize(boxSize)
    , m_timingFunction(animation.timingFunction()->clone())
    , m_iterationCount(animation.iterationCount())
    , m_duration(animation.duration().value_or(0))
    , m_direction(animation.direction())
    , m_fillsForwards(animation.fillsForwards())
    , m_startTime(startTime)
    , m_pauseTime(pauseTime)
    , m_totalRunningTime(0_s)
    , m_lastRefreshedTime(m_startTime)
    , m_state(state)
{
}

TextureMapperAnimation::TextureMapperAnimation(const TextureMapperAnimation& other)
    : m_name(other.m_name.isSafeToSendToAnotherThread() ? other.m_name : other.m_name.isolatedCopy())
    , m_keyframes(other.m_keyframes)
    , m_boxSize(other.m_boxSize)
    , m_timingFunction(other.m_timingFunction->clone())
    , m_iterationCount(other.m_iterationCount)
    , m_duration(other.m_duration)
    , m_direction(other.m_direction)
    , m_fillsForwards(other.m_fillsForwards)
    , m_startTime(other.m_startTime)
    , m_pauseTime(other.m_pauseTime)
    , m_totalRunningTime(other.m_totalRunningTime)
    , m_lastRefreshedTime(other.m_lastRefreshedTime)
    , m_state(other.m_state)
{
}

TextureMapperAnimation& TextureMapperAnimation::operator=(const TextureMapperAnimation& other)
{
    m_name = other.m_name.isSafeToSendToAnotherThread() ? other.m_name : other.m_name.isolatedCopy();
    m_keyframes = other.m_keyframes;
    m_boxSize = other.m_boxSize;
    m_timingFunction = other.m_timingFunction->clone();
    m_iterationCount = other.m_iterationCount;
    m_duration = other.m_duration;
    m_direction = other.m_direction;
    m_fillsForwards = other.m_fillsForwards;
    m_startTime = other.m_startTime;
    m_pauseTime = other.m_pauseTime;
    m_totalRunningTime = other.m_totalRunningTime;
    m_lastRefreshedTime = other.m_lastRefreshedTime;
    m_state = other.m_state;
    return *this;
}

void TextureMapperAnimation::apply(ApplicationResult& applicationResults, MonotonicTime time, KeepInternalState keepInternalState)
{
    MonotonicTime oldLastRefreshedTime = m_lastRefreshedTime;
    Seconds oldTotalRunningTime = m_totalRunningTime;
    State oldState = m_state;

    auto maybeRestoreInternalState = makeScopeExit([&] {
        if (keepInternalState == KeepInternalState::Yes) {
            m_lastRefreshedTime = oldLastRefreshedTime;
            m_totalRunningTime = oldTotalRunningTime;
            m_state = oldState;
        }
    });

    // Even when m_state == State::Stopped && !m_fillsForwards, we should calculate the last value to avoid a flash.

    Seconds totalRunningTime = computeTotalRunningTime(time);
    double normalizedValue = normalizedAnimationValue(totalRunningTime.seconds(), m_duration, m_direction, m_iterationCount);

    if (m_iterationCount != Animation::IterationCountInfinite && totalRunningTime.seconds() >= m_duration * m_iterationCount) {
        m_state = State::Stopped;
        m_pauseTime = 0_s;
        normalizedValue = normalizedAnimationValueForFillsForwards(m_iterationCount, m_direction);
    }

    applicationResults.hasRunningAnimations |= (m_state == State::Playing);

    if (!normalizedValue) {
        applyInternal(applicationResults, m_keyframes.at(0), m_keyframes.at(1), 0);
        return;
    }

    if (normalizedValue == 1.0) {
        applyInternal(applicationResults, m_keyframes.at(m_keyframes.size() - 2), m_keyframes.at(m_keyframes.size() - 1), 1);
        return;
    }
    if (m_keyframes.size() == 2) {
        auto& timingFunction = timingFunctionForAnimationValue(m_keyframes.at(0), *this);
        normalizedValue = timingFunction.transformProgress(normalizedValue, m_duration);
        applyInternal(applicationResults, m_keyframes.at(0), m_keyframes.at(1), normalizedValue);
        return;
    }

    for (size_t i = 0; i < m_keyframes.size() - 1; ++i) {
        const AnimationValue& from = m_keyframes.at(i);
        const AnimationValue& to = m_keyframes.at(i + 1);
        if (from.keyTime() > normalizedValue || to.keyTime() < normalizedValue)
            continue;

        normalizedValue = (normalizedValue - from.keyTime()) / (to.keyTime() - from.keyTime());
        auto& timingFunction = timingFunctionForAnimationValue(from, *this);
        normalizedValue = timingFunction.transformProgress(normalizedValue, m_duration);
        applyInternal(applicationResults, from, to, normalizedValue);
        break;
    }
}

void TextureMapperAnimation::pause(Seconds time)
{
    m_state = State::Paused;
    m_pauseTime = time;
}

void TextureMapperAnimation::resume()
{
    m_state = State::Playing;
    // FIXME: This seems wrong. m_totalRunningTime is cleared.
    // https://bugs.webkit.org/show_bug.cgi?id=183113
    m_pauseTime = 0_s;
    m_totalRunningTime = m_pauseTime;
    m_lastRefreshedTime = MonotonicTime::now();
}

Seconds TextureMapperAnimation::computeTotalRunningTime(MonotonicTime time)
{
    if (m_state == State::Paused)
        return m_pauseTime;

    MonotonicTime oldLastRefreshedTime = m_lastRefreshedTime;
    m_lastRefreshedTime = time;
    m_totalRunningTime += m_lastRefreshedTime - oldLastRefreshedTime;
    return m_totalRunningTime;
}

void TextureMapperAnimation::applyInternal(ApplicationResult& applicationResults, const AnimationValue& from, const AnimationValue& to, float progress)
{
    switch (m_keyframes.property()) {
    case AnimatedProperty::Translate:
    case AnimatedProperty::Rotate:
    case AnimatedProperty::Scale:
    case AnimatedProperty::Transform: {
        ASSERT(applicationResults.transform);
        auto transform = applyTransformAnimation(static_cast<const TransformAnimationValue&>(from).value(), static_cast<const TransformAnimationValue&>(to).value(), progress, m_boxSize);
        applicationResults.transform->multiply(transform);
        return;
    }
    case AnimatedProperty::Opacity:
        applicationResults.opacity = applyOpacityAnimation((static_cast<const FloatAnimationValue&>(from).value()), (static_cast<const FloatAnimationValue&>(to).value()), progress);
        return;
    case AnimatedProperty::Filter:
    case AnimatedProperty::WebkitBackdropFilter:
        applicationResults.filters = applyFilterAnimation(static_cast<const FilterAnimationValue&>(from).value(), static_cast<const FilterAnimationValue&>(to).value(), progress, m_boxSize);
        return;
    default:
        ASSERT_NOT_REACHED();
    }
}

void TextureMapperAnimations::add(const TextureMapperAnimation& animation)
{
    // Remove the old state if we are resuming a paused animation.
    remove(animation.name(), animation.keyframes().property());

    m_animations.append(animation);
}

void TextureMapperAnimations::remove(const String& name)
{
    m_animations.removeAllMatching([&name] (const auto& animation) {
        return animation.name() == name;
    });
}

void TextureMapperAnimations::remove(const String& name, AnimatedProperty property)
{
    m_animations.removeAllMatching([&name, property] (const auto& animation) {
        return animation.name() == name && animation.keyframes().property() == property;
    });
}

void TextureMapperAnimations::pause(const String& name, Seconds offset)
{
    for (auto& animation : m_animations) {
        if (animation.name() == name)
            animation.pause(offset);
    }
}

void TextureMapperAnimations::suspend(MonotonicTime time)
{
    // FIXME: This seems wrong. `pause` takes time offset (Seconds), not MonotonicTime.
    // https://bugs.webkit.org/show_bug.cgi?id=183112
    for (auto& animation : m_animations)
        animation.pause(time.secondsSinceEpoch());
}

void TextureMapperAnimations::resume()
{
    for (auto& animation : m_animations)
        animation.resume();
}

void TextureMapperAnimations::apply(TextureMapperAnimation::ApplicationResult& applicationResults, MonotonicTime time, TextureMapperAnimation::KeepInternalState keepInternalState)
{
    Vector<TextureMapperAnimation*> translateAnimations;
    Vector<TextureMapperAnimation*> rotateAnimations;
    Vector<TextureMapperAnimation*> scaleAnimations;
    Vector<TextureMapperAnimation*> transformAnimations;
    Vector<TextureMapperAnimation*> leafAnimations;

    for (auto& animation : m_animations) {
        switch (animation.keyframes().property()) {
        case AnimatedProperty::Translate:
            translateAnimations.append(&animation);
            break;
        case AnimatedProperty::Rotate:
            rotateAnimations.append(&animation);
            break;
        case AnimatedProperty::Scale:
            scaleAnimations.append(&animation);
            break;
        case AnimatedProperty::Transform:
            transformAnimations.append(&animation);
            break;
        default:
            leafAnimations.append(&animation);
        }
    }

    if (!translateAnimations.isEmpty() || !rotateAnimations.isEmpty() || !scaleAnimations.isEmpty() || !transformAnimations.isEmpty()) {
        applicationResults.transform = TransformationMatrix();

        if (translateAnimations.isEmpty())
            applicationResults.transform->multiply(m_translate);
        else {
            for (auto* animation : translateAnimations)
                animation->apply(applicationResults, time, keepInternalState);
        }

        if (rotateAnimations.isEmpty())
            applicationResults.transform->multiply(m_rotate);
        else {
            for (auto* animation : rotateAnimations)
                animation->apply(applicationResults, time, keepInternalState);
        }

        if (scaleAnimations.isEmpty())
            applicationResults.transform->multiply(m_scale);
        else {
            for (auto* animation : scaleAnimations)
                animation->apply(applicationResults, time, keepInternalState);
        }

        if (transformAnimations.isEmpty())
            applicationResults.transform->multiply(m_transform);
        else
            transformAnimations.last()->apply(applicationResults, time, keepInternalState);
    }

    for (auto* animation : leafAnimations)
        animation->apply(applicationResults, time, keepInternalState);
}

bool TextureMapperAnimations::hasActiveAnimationsOfType(AnimatedProperty type) const
{
    return std::any_of(m_animations.begin(), m_animations.end(), [&type](const auto& animation) {
        return animation.keyframes().property() == type;
    });
}

bool TextureMapperAnimations::hasRunningAnimations() const
{
    return std::any_of(m_animations.begin(), m_animations.end(), [](const auto& animation) {
        return animation.state() == TextureMapperAnimation::State::Playing;
    });
}

bool TextureMapperAnimations::hasRunningTransformAnimations() const
{
    return std::any_of(m_animations.begin(), m_animations.end(), [](const auto& animation) {
        switch (animation.keyframes().property()) {
        case AnimatedProperty::Translate:
        case AnimatedProperty::Rotate:
        case AnimatedProperty::Scale:
        case AnimatedProperty::Transform:
            break;
        default:
            return false;
        }

        switch (animation.state()) {
        case TextureMapperAnimation::State::Playing:
        case TextureMapperAnimation::State::Paused:
            break;
        default:
            return false;
        }

        return true;
    });
}

} // namespace WebCore

#endif // USE(TEXTURE_MAPPER)