/
AnimationEffect.cpp
346 lines (286 loc) · 12.9 KB
/
AnimationEffect.cpp
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
/*
* Copyright (C) 2017-2018 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "AnimationEffect.h"
#include "CSSAnimation.h"
#include "CommonAtomStrings.h"
#include "FillMode.h"
#include "JSComputedEffectTiming.h"
#include "ScriptExecutionContext.h"
#include "WebAnimation.h"
#include "WebAnimationUtilities.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(AnimationEffect);
AnimationEffect::AnimationEffect() = default;
AnimationEffect::~AnimationEffect() = default;
void AnimationEffect::setAnimation(WebAnimation* animation)
{
if (m_animation == animation)
return;
m_animation = animation;
if (animation)
animation->updateRelevance();
}
EffectTiming AnimationEffect::getBindingsTiming() const
{
if (auto* styleOriginatedAnimation = dynamicDowncast<StyleOriginatedAnimation>(animation()))
styleOriginatedAnimation->flushPendingStyleChanges();
EffectTiming timing;
timing.delay = secondsToWebAnimationsAPITime(m_timing.delay);
timing.endDelay = secondsToWebAnimationsAPITime(m_timing.endDelay);
timing.fill = m_timing.fill;
timing.iterationStart = m_timing.iterationStart;
timing.iterations = m_timing.iterations;
if (m_timing.iterationDuration == 0_s)
timing.duration = autoAtom();
else
timing.duration = secondsToWebAnimationsAPITime(m_timing.iterationDuration);
timing.direction = m_timing.direction;
timing.easing = m_timing.timingFunction->cssText();
return timing;
}
std::optional<Seconds> AnimationEffect::localTime(std::optional<Seconds> startTime) const
{
// 4.5.4. Local time
// https://drafts.csswg.org/web-animations-1/#local-time-section
// The local time of an animation effect at a given moment is based on the first matching condition from the following:
// If the animation effect is associated with an animation, the local time is the current time of the animation.
// Otherwise, the local time is unresolved.
if (m_animation)
return m_animation->currentTime(startTime);
return std::nullopt;
}
double AnimationEffect::playbackRate() const
{
return m_animation ? m_animation->playbackRate() : 1;
}
BasicEffectTiming AnimationEffect::getBasicTiming(std::optional<Seconds> startTime) const
{
return m_timing.getBasicTiming(localTime(startTime), playbackRate());
}
ComputedEffectTiming AnimationEffect::getBindingsComputedTiming() const
{
if (auto* styleOriginatedAnimation = dynamicDowncast<StyleOriginatedAnimation>(animation()))
styleOriginatedAnimation->flushPendingStyleChanges();
return getComputedTiming();
}
ComputedEffectTiming AnimationEffect::getComputedTiming(std::optional<Seconds> startTime) const
{
auto localTime = this->localTime(startTime);
auto resolvedTiming = m_timing.resolve(localTime, playbackRate());
ComputedEffectTiming computedTiming;
computedTiming.delay = secondsToWebAnimationsAPITime(m_timing.delay);
computedTiming.endDelay = secondsToWebAnimationsAPITime(m_timing.endDelay);
computedTiming.fill = m_timing.fill == FillMode::Auto ? FillMode::None : m_timing.fill;
computedTiming.iterationStart = m_timing.iterationStart;
computedTiming.iterations = m_timing.iterations;
computedTiming.duration = secondsToWebAnimationsAPITime(m_timing.iterationDuration);
computedTiming.direction = m_timing.direction;
computedTiming.easing = m_timing.timingFunction->cssText();
computedTiming.endTime = secondsToWebAnimationsAPITime(m_timing.endTime);
computedTiming.activeDuration = secondsToWebAnimationsAPITime(m_timing.activeDuration);
if (localTime)
computedTiming.localTime = secondsToWebAnimationsAPITime(*localTime);
computedTiming.simpleIterationProgress = resolvedTiming.simpleIterationProgress;
computedTiming.progress = resolvedTiming.transformedProgress;
computedTiming.currentIteration = resolvedTiming.currentIteration;
computedTiming.phase = resolvedTiming.phase;
return computedTiming;
}
ExceptionOr<void> AnimationEffect::bindingsUpdateTiming(std::optional<OptionalEffectTiming> timing)
{
auto retVal = updateTiming(timing);
if (!retVal.hasException() && timing) {
if (auto* cssAnimation = dynamicDowncast<CSSAnimation>(animation()))
cssAnimation->effectTimingWasUpdatedUsingBindings(*timing);
}
return retVal;
}
ExceptionOr<void> AnimationEffect::updateTiming(std::optional<OptionalEffectTiming> timing)
{
// 6.5.4. Updating the timing of an AnimationEffect
// https://drafts.csswg.org/web-animations/#updating-animationeffect-timing
// To update the timing properties of an animation effect, effect, from an EffectTiming or OptionalEffectTiming object, input, perform the following steps:
if (!timing)
return { };
// 1. If the iterationStart member of input is present and less than zero, throw a TypeError and abort this procedure.
if (timing->iterationStart) {
if (timing->iterationStart.value() < 0)
return Exception { ExceptionCode::TypeError };
}
// 2. If the iterations member of input is present, and less than zero or is the value NaN, throw a TypeError and abort this procedure.
if (timing->iterations) {
if (timing->iterations.value() < 0 || std::isnan(timing->iterations.value()))
return Exception { ExceptionCode::TypeError };
}
// 3. If the duration member of input is present, and less than zero or is the value NaN, throw a TypeError and abort this procedure.
// FIXME: should it not throw an exception on a string other than "auto"?
if (timing->duration) {
if (std::holds_alternative<double>(timing->duration.value())) {
auto durationAsDouble = std::get<double>(timing->duration.value());
if (durationAsDouble < 0 || std::isnan(durationAsDouble))
return Exception { ExceptionCode::TypeError };
} else {
if (std::get<String>(timing->duration.value()) != autoAtom())
return Exception { ExceptionCode::TypeError };
}
}
// 4. If the easing member of input is present but cannot be parsed using the <timing-function> production [CSS-EASING-1], throw a TypeError and abort this procedure.
if (!timing->easing.isNull()) {
auto timingFunctionResult = TimingFunction::createFromCSSText(timing->easing);
if (timingFunctionResult.hasException())
return timingFunctionResult.releaseException();
m_timing.timingFunction = timingFunctionResult.returnValue();
}
// 5. Assign each member present in input to the corresponding timing property of effect as follows:
//
// delay → start delay
// endDelay → end delay
// fill → fill mode
// iterationStart → iteration start
// iterations → iteration count
// duration → iteration duration
// direction → playback direction
// easing → timing function
if (timing->delay)
m_timing.delay = Seconds::fromMilliseconds(timing->delay.value());
if (timing->endDelay)
m_timing.endDelay = Seconds::fromMilliseconds(timing->endDelay.value());
if (timing->fill)
m_timing.fill = timing->fill.value();
if (timing->iterationStart)
m_timing.iterationStart = timing->iterationStart.value();
if (timing->iterations)
m_timing.iterations = timing->iterations.value();
if (timing->duration)
m_timing.iterationDuration = std::holds_alternative<double>(timing->duration.value()) ? Seconds::fromMilliseconds(std::get<double>(timing->duration.value())) : 0_s;
if (timing->direction)
m_timing.direction = timing->direction.value();
updateStaticTimingProperties();
if (m_animation)
m_animation->effectTimingDidChange();
return { };
}
void AnimationEffect::updateStaticTimingProperties()
{
m_timing.updateComputedProperties();
}
ExceptionOr<void> AnimationEffect::setIterationStart(double iterationStart)
{
// https://drafts.csswg.org/web-animations-1/#dom-animationeffecttiming-iterationstart
// If an attempt is made to set this attribute to a value less than zero, a TypeError must
// be thrown and the value of the iterationStart attribute left unchanged.
if (iterationStart < 0)
return Exception { ExceptionCode::TypeError };
if (m_timing.iterationStart == iterationStart)
return { };
m_timing.iterationStart = iterationStart;
return { };
}
ExceptionOr<void> AnimationEffect::setIterations(double iterations)
{
// https://drafts.csswg.org/web-animations-1/#dom-animationeffecttiming-iterations
// If an attempt is made to set this attribute to a value less than zero or a NaN value, a
// TypeError must be thrown and the value of the iterations attribute left unchanged.
if (iterations < 0 || std::isnan(iterations))
return Exception { ExceptionCode::TypeError };
if (m_timing.iterations == iterations)
return { };
m_timing.iterations = iterations;
return { };
}
void AnimationEffect::setDelay(const Seconds& delay)
{
if (m_timing.delay == delay)
return;
m_timing.delay = delay;
}
void AnimationEffect::setEndDelay(const Seconds& endDelay)
{
if (m_timing.endDelay == endDelay)
return;
m_timing.endDelay = endDelay;
}
void AnimationEffect::setFill(FillMode fill)
{
if (m_timing.fill == fill)
return;
m_timing.fill = fill;
}
void AnimationEffect::setIterationDuration(const Seconds& duration)
{
if (m_timing.iterationDuration == duration)
return;
m_timing.iterationDuration = duration;
}
void AnimationEffect::setDirection(PlaybackDirection direction)
{
if (m_timing.direction == direction)
return;
m_timing.direction = direction;
}
void AnimationEffect::setTimingFunction(const RefPtr<TimingFunction>& timingFunction)
{
m_timing.timingFunction = timingFunction;
}
std::optional<double> AnimationEffect::progressUntilNextStep(double iterationProgress) const
{
RefPtr stepsTimingFunction = dynamicDowncast<StepsTimingFunction>(m_timing.timingFunction);
if (!stepsTimingFunction)
return std::nullopt;
auto numberOfSteps = stepsTimingFunction->numberOfSteps();
auto nextStepProgress = ceil(iterationProgress * numberOfSteps) / numberOfSteps;
return nextStepProgress - iterationProgress;
}
Seconds AnimationEffect::timeToNextTick(const BasicEffectTiming& timing) const
{
switch (timing.phase) {
case AnimationEffectPhase::Before:
// The effect is in its "before" phase, in this case we can wait until it enters its "active" phase.
return delay() - *timing.localTime;
case AnimationEffectPhase::Active: {
if (!ticksContinuouslyWhileActive())
return endTime() - *timing.localTime;
if (auto iterationProgress = getComputedTiming().simpleIterationProgress) {
// In case we're in a range that uses a steps() timing function, we can compute the time until the next step starts.
if (auto progressUntilNextStep = this->progressUntilNextStep(*iterationProgress))
return iterationDuration() * *progressUntilNextStep;
}
// Other effects that continuously tick in the "active" phase will need to update their animated
// progress at the immediate next opportunity.
return 0_s;
}
case AnimationEffectPhase::After:
// The effect is in its after phase, which means it will no longer update its progress, so it doens't need a tick.
return Seconds::infinity();
case AnimationEffectPhase::Idle:
ASSERT_NOT_REACHED();
return Seconds::infinity();
}
ASSERT_NOT_REACHED();
return Seconds::infinity();
}
} // namespace WebCore