Commit 217667c0 authored by Sergio Villar Senin's avatar Sergio Villar Senin Committed by Commit Bot

Remove NullValue() from CalculateOverallProgress

NullValue() is represented as a NaN in a double. We could better return
a base::Optional<double> and use the undefined base::Optional as null
value. Also there is no need for the IsNull() call as we could just use
the returned value as a boolean.

Bug: 994811
Change-Id: I236fb8dbf2f0fd6ea879a6996e2a11a143406b09
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1761272
Commit-Queue: Sergio Villar <svillar@igalia.com>
Reviewed-by: default avatarYi Gu <yigu@chromium.org>
Reviewed-by: default avatarStephen McGruer <smcgruer@chromium.org>
Reviewed-by: default avatarKevin Ellis <kevers@chromium.org>
Cr-Commit-Position: refs/heads/master@{#689197}
parent cee65dca
......@@ -164,7 +164,7 @@ Timing::CalculatedTiming Timing::CalculateTimings(
base::Optional<double> progress;
const double iteration_duration = IterationDuration().InSecondsF();
const double overall_progress =
const base::Optional<double> overall_progress =
CalculateOverallProgress(current_phase, active_time, iteration_duration,
iteration_count, iteration_start);
const double simple_iteration_progress = CalculateSimpleIterationProgress(
......
......@@ -128,14 +128,15 @@ static inline double CalculateActiveTime(double active_duration,
// 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(Timing::Phase phase,
double active_time,
double iteration_duration,
double iteration_count,
double iteration_start) {
static inline base::Optional<double> CalculateOverallProgress(
Timing::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();
return base::nullopt;
// 2. Calculate an initial value for overall progress.
double overall_progress = 0;
......@@ -155,22 +156,23 @@ static inline double CalculateOverallProgress(Timing::Phase phase,
// effect.
// https://drafts.csswg.org/web-animations/#calculating-the-simple-iteration
// -progress
static inline double CalculateSimpleIterationProgress(Timing::Phase phase,
double overall_progress,
double iteration_start,
double active_time,
double active_duration,
double iteration_count) {
static inline double CalculateSimpleIterationProgress(
Timing::Phase phase,
base::Optional<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))
if (!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)
double simple_iteration_progress = std::isinf(overall_progress.value())
? fmod(iteration_start, 1.0)
: fmod(overall_progress, 1.0);
: fmod(overall_progress.value(), 1.0);
// 3. If all of the following conditions are true,
// * the simple iteration progress calculated above is zero, and
......@@ -194,7 +196,7 @@ static inline double CalculateCurrentIteration(
Timing::Phase phase,
double active_time,
double iteration_count,
double overall_progress,
base::Optional<double> overall_progress,
double simple_iteration_progress) {
// 1. If the active time is unresolved, return unresolved.
if (IsNull(active_time))
......@@ -206,15 +208,18 @@ static inline double CalculateCurrentIteration(
return std::numeric_limits<double>::infinity();
}
if (!overall_progress)
return NullValue();
// 3. If the simple iteration progress is 1.0, return floor(overall progress)
// - 1.
if (simple_iteration_progress == 1.0) {
// Safeguard for zero duration animation (crbug.com/954558).
return fmax(0, floor(overall_progress) - 1);
return fmax(0, floor(overall_progress.value()) - 1);
}
// 4. Otherwise, return floor(overall progress).
return floor(overall_progress);
return floor(overall_progress.value());
}
// https://drafts.csswg.org/web-animations/#calculating-the-directed-progress
......
......@@ -137,11 +137,11 @@ TEST(AnimationTimingCalculationsTest, IterationTime) {
TEST(AnimationTimingCalculationsTest, OverallProgress) {
// If the active time is null.
EXPECT_TRUE(IsNull(CalculateOverallProgress(Timing::kPhaseAfter,
/*active_time=*/NullValue(),
/*iteration_duration=*/1.0,
/*iteration_count=*/1.0,
/*iteration_start=*/1.0)));
EXPECT_FALSE(CalculateOverallProgress(Timing::kPhaseAfter,
/*active_time=*/NullValue(),
/*iteration_duration=*/1.0,
/*iteration_count=*/1.0,
/*iteration_start=*/1.0));
// If iteration duration is zero, calculate progress based on iteration count.
EXPECT_EQ(3, CalculateOverallProgress(Timing::kPhaseActive,
......@@ -173,13 +173,13 @@ TEST(AnimationTimingCalculationsTest, OverallProgress) {
TEST(AnimationTimingCalculationsTest, CalculateSimpleIterationProgress) {
// If the overall progress is null.
EXPECT_TRUE(
IsNull(CalculateSimpleIterationProgress(Timing::kPhaseAfter,
/*overall_progress=*/NullValue(),
/*iteration_start=*/1.0,
/*active_time=*/NullValue(),
/*active_duration=*/1.0,
/*iteration_count=*/1.0)));
EXPECT_TRUE(IsNull(
CalculateSimpleIterationProgress(Timing::kPhaseAfter,
/*overall_progress=*/base::nullopt,
/*iteration_start=*/1.0,
/*active_time=*/NullValue(),
/*active_duration=*/1.0,
/*iteration_count=*/1.0)));
// If the overall progress is infinite.
const double inf = std::numeric_limits<double>::infinity();
......@@ -213,7 +213,7 @@ TEST(AnimationTimingCalculationsTest, CurrentIteration) {
IsNull(CalculateCurrentIteration(Timing::kPhaseAfter,
/*active_time=*/NullValue(),
/*iteration_count=*/1.0,
/*overall_progress=*/NullValue(),
/*overall_progress=*/base::nullopt,
/*simple_iteration_progress=*/0)));
// If the iteration count is infinite.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment