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
|
/*
* Copyright (C) 2013 Google 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:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "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 THE COPYRIGHT
* OWNER 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.
*/
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_TIMING_CALCULATIONS_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_ANIMATION_TIMING_CALCULATIONS_H_
#include "third_party/blink/renderer/core/animation/animation_effect.h"
#include "third_party/blink/renderer/core/animation/timing.h"
#include "third_party/blink/renderer/platform/animation/animation_utilities.h"
#include "third_party/blink/renderer/platform/wtf/math_extras.h"
namespace blink {
static inline double MultiplyZeroAlwaysGivesZero(double x, double y) {
DCHECK(!IsNull(x));
DCHECK(!IsNull(y));
return x && y ? x * y : 0;
}
static inline double MultiplyZeroAlwaysGivesZero(AnimationTimeDelta x,
double y) {
DCHECK(!IsNull(y));
return x.is_zero() || y == 0 ? 0 : (x * y).InSecondsF();
}
static inline bool IsWithinEpsilon(double a, double b) {
// Permit 2-bits of quantization error. Threshold based on experimentation
// with accuracy of fmod.
return std::abs(a - b) <= 2.0 * std::numeric_limits<double>::epsilon();
}
// https://drafts.csswg.org/web-animations-1/#animation-effect-phases-and-states
static inline AnimationEffect::Phase CalculatePhase(
double active_duration,
double local_time,
AnimationEffect::AnimationDirection direction,
const Timing& specified) {
DCHECK_GE(active_duration, 0);
if (IsNull(local_time))
return AnimationEffect::kPhaseNone;
double end_time = std::max(
specified.start_delay + active_duration + specified.end_delay, 0.0);
double before_active_boundary_time =
std::max(std::min(specified.start_delay, end_time), 0.0);
if (local_time < before_active_boundary_time ||
(local_time == before_active_boundary_time &&
direction == AnimationEffect::AnimationDirection::kBackwards)) {
return AnimationEffect::kPhaseBefore;
}
double active_after_boundary_time = std::max(
std::min(specified.start_delay + active_duration, end_time), 0.0);
if (local_time > active_after_boundary_time ||
(local_time == active_after_boundary_time &&
direction == AnimationEffect::AnimationDirection::kForwards)) {
return AnimationEffect::kPhaseAfter;
}
return AnimationEffect::kPhaseActive;
}
static inline double CalculateActiveTime(double active_duration,
Timing::FillMode fill_mode,
double local_time,
AnimationEffect::Phase phase,
const Timing& specified) {
DCHECK_GE(active_duration, 0);
switch (phase) {
case AnimationEffect::kPhaseBefore:
if (fill_mode == Timing::FillMode::BACKWARDS ||
fill_mode == Timing::FillMode::BOTH)
return std::max(local_time - specified.start_delay, 0.0);
return NullValue();
case AnimationEffect::kPhaseActive:
return local_time - specified.start_delay;
case AnimationEffect::kPhaseAfter:
if (fill_mode == Timing::FillMode::FORWARDS ||
fill_mode == Timing::FillMode::BOTH) {
return std::max(
0.0, std::min(active_duration, local_time - specified.start_delay));
}
return NullValue();
case AnimationEffect::kPhaseNone:
DCHECK(IsNull(local_time));
return NullValue();
default:
NOTREACHED();
return NullValue();
}
}
static inline double CalculateOffsetActiveTime(double active_duration,
double active_time,
double start_offset) {
DCHECK_GE(active_duration, 0);
DCHECK_GE(start_offset, 0);
if (IsNull(active_time))
return NullValue();
DCHECK(active_time >= 0 && active_time <= active_duration);
if (!std::isfinite(active_time))
return std::numeric_limits<double>::infinity();
return active_time + start_offset;
}
static inline bool EndsOnIterationBoundary(double iteration_count,
double iteration_start) {
DCHECK(std::isfinite(iteration_count));
return !fmod(iteration_count + iteration_start, 1);
}
// TODO(crbug.com/630915): Align this function with current Web Animations spec
// text.
static inline double CalculateIterationTime(double iteration_duration,
double repeated_duration,
double offset_active_time,
double start_offset,
AnimationEffect::Phase phase,
const Timing& specified) {
DCHECK_GT(iteration_duration, 0);
DCHECK_EQ(repeated_duration,
MultiplyZeroAlwaysGivesZero(iteration_duration,
specified.iteration_count));
if (IsNull(offset_active_time))
return NullValue();
DCHECK_GE(offset_active_time, 0);
DCHECK_LE(offset_active_time, repeated_duration + start_offset);
if (!std::isfinite(offset_active_time) ||
(offset_active_time - start_offset == repeated_duration &&
specified.iteration_count &&
EndsOnIterationBoundary(specified.iteration_count,
specified.iteration_start)))
return iteration_duration;
DCHECK(std::isfinite(offset_active_time));
double iteration_time = fmod(offset_active_time, iteration_duration);
// This implements step 3 of
// https://drafts.csswg.org/web-animations/#calculating-the-simple-iteration-progress
if (iteration_time == 0 && phase == AnimationEffect::kPhaseAfter &&
repeated_duration != 0 && offset_active_time != 0)
return iteration_duration;
return iteration_time;
}
// Calculates the overall progress, which describes the number of iterations
// that have completed (including partial iterations).
// https://drafts.csswg.org/web-animations/#calculating-the-overall-progress
static inline double CalculateOverallProgress(AnimationEffect::Phase phase,
double active_time,
double iteration_duration,
double iteration_count,
double iteration_start) {
// 1. If the active time is unresolved, return unresolved.
if (IsNull(active_time))
return NullValue();
// 2. Calculate an initial value for overall progress.
double overall_progress = 0;
if (!iteration_duration) {
if (phase != AnimationEffect::kPhaseBefore)
overall_progress = iteration_count;
} else {
overall_progress = active_time / iteration_duration;
}
return overall_progress + iteration_start;
}
// Calculates the simple iteration progress, which is a fraction of the progress
// through the current iteration that ignores transformations to the time
// introduced by the playback direction or timing functions applied to the
// effect.
// https://drafts.csswg.org/web-animations/#calculating-the-simple-iteration
// -progress
static inline double CalculateSimpleIterationProgress(
AnimationEffect::Phase phase,
double overall_progress,
double iteration_start,
double active_time,
double active_duration,
double iteration_count) {
// 1. If the overall progress is unresolved, return unresolved.
if (IsNull(overall_progress))
return NullValue();
// 2. If overall progress is infinity, let the simple iteration progress be
// iteration start % 1.0, otherwise, let the simple iteration progress be
// overall progress % 1.0.
double simple_iteration_progress = std::isinf(overall_progress)
? fmod(iteration_start, 1.0)
: fmod(overall_progress, 1.0);
// 3. If all of the following conditions are true,
// * the simple iteration progress calculated above is zero, and
// * the animation effect is in the active phase or the after phase, and
// * the active time is equal to the active duration, and
// * the iteration count is not equal to zero.
// let the simple iteration progress be 1.0.
if (IsWithinEpsilon(simple_iteration_progress, 0.0) &&
(phase == AnimationEffect::kPhaseActive ||
phase == AnimationEffect::kPhaseAfter) &&
IsWithinEpsilon(active_time, active_duration) &&
!IsWithinEpsilon(iteration_count, 0.0)) {
simple_iteration_progress = 1.0;
}
// 4. Return simple iteration progress.
return simple_iteration_progress;
}
// https://drafts.csswg.org/web-animations/#calculating-the-current-iteration
static inline double CalculateCurrentIteration(
AnimationEffect::Phase phase,
double active_time,
double iteration_count,
double overall_progress,
double simple_iteration_progress) {
// 1. If the active time is unresolved, return unresolved.
if (IsNull(active_time))
return NullValue();
// 2. If the animation effect is in the after phase and the iteration count
// is infinity, return infinity.
if (phase == AnimationEffect::kPhaseAfter && std::isinf(iteration_count)) {
return std::numeric_limits<double>::infinity();
}
// 3. If the simple iteration progress is 1.0, return floor(overall progress)
// - 1.
if (simple_iteration_progress == 1.0)
return floor(overall_progress) - 1;
// 4. Otherwise, return floor(overall progress).
return floor(overall_progress);
}
// https://drafts.csswg.org/web-animations/#calculating-the-directed-progress
static inline bool IsCurrentDirectionForwards(
double current_iteration,
Timing::PlaybackDirection direction) {
const bool current_iteration_is_even =
std::isinf(current_iteration)
? true
: IsWithinEpsilon(fmod(current_iteration, 2), 0);
switch (direction) {
case Timing::PlaybackDirection::NORMAL:
return true;
case Timing::PlaybackDirection::REVERSE:
return false;
case Timing::PlaybackDirection::ALTERNATE_NORMAL:
return current_iteration_is_even;
case Timing::PlaybackDirection::ALTERNATE_REVERSE:
return !current_iteration_is_even;
}
}
// https://drafts.csswg.org/web-animations/#calculating-the-directed-progress
static inline double CalculateDirectedProgress(
double simple_iteration_progress,
double current_iteration,
Timing::PlaybackDirection direction) {
// 1. If the simple progress is unresolved, return unresolved.
if (IsNull(simple_iteration_progress))
return NullValue();
// 2. Calculate the current direction.
bool current_direction_is_forwards =
IsCurrentDirectionForwards(current_iteration, direction);
// 3. If the current direction is forwards then return the simple iteration
// progress. Otherwise return 1 - simple iteration progress.
return current_direction_is_forwards ? simple_iteration_progress
: 1 - simple_iteration_progress;
}
// https://drafts.csswg.org/web-animations/#calculating-the-transformed-progress
static inline double CalculateTransformedProgress(
double directed_progress,
double iteration_duration,
scoped_refptr<TimingFunction> timing_function) {
if (IsNull(directed_progress))
return NullValue();
// Return the result of evaluating the animation effect’s timing function
// passing directed progress as the input progress value.
// Note that the spec calls for passing in a before flag as well, which should
// be used by the step easing functions (Possibly related to crbug/827560).
return timing_function->Evaluate(directed_progress,
AccuracyForDuration(iteration_duration));
}
} // namespace blink
#endif
|