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( ...@@ -164,7 +164,7 @@ Timing::CalculatedTiming Timing::CalculateTimings(
base::Optional<double> progress; base::Optional<double> progress;
const double iteration_duration = IterationDuration().InSecondsF(); const double iteration_duration = IterationDuration().InSecondsF();
const double overall_progress = const base::Optional<double> overall_progress =
CalculateOverallProgress(current_phase, active_time, iteration_duration, CalculateOverallProgress(current_phase, active_time, iteration_duration,
iteration_count, iteration_start); iteration_count, iteration_start);
const double simple_iteration_progress = CalculateSimpleIterationProgress( const double simple_iteration_progress = CalculateSimpleIterationProgress(
......
...@@ -128,14 +128,15 @@ static inline double CalculateActiveTime(double active_duration, ...@@ -128,14 +128,15 @@ static inline double CalculateActiveTime(double active_duration,
// Calculates the overall progress, which describes the number of iterations // Calculates the overall progress, which describes the number of iterations
// that have completed (including partial iterations). // that have completed (including partial iterations).
// https://drafts.csswg.org/web-animations/#calculating-the-overall-progress // https://drafts.csswg.org/web-animations/#calculating-the-overall-progress
static inline double CalculateOverallProgress(Timing::Phase phase, static inline base::Optional<double> CalculateOverallProgress(
double active_time, Timing::Phase phase,
double iteration_duration, double active_time,
double iteration_count, double iteration_duration,
double iteration_start) { double iteration_count,
double iteration_start) {
// 1. If the active time is unresolved, return unresolved. // 1. If the active time is unresolved, return unresolved.
if (IsNull(active_time)) if (IsNull(active_time))
return NullValue(); return base::nullopt;
// 2. Calculate an initial value for overall progress. // 2. Calculate an initial value for overall progress.
double overall_progress = 0; double overall_progress = 0;
...@@ -155,22 +156,23 @@ static inline double CalculateOverallProgress(Timing::Phase phase, ...@@ -155,22 +156,23 @@ static inline double CalculateOverallProgress(Timing::Phase phase,
// effect. // effect.
// https://drafts.csswg.org/web-animations/#calculating-the-simple-iteration // https://drafts.csswg.org/web-animations/#calculating-the-simple-iteration
// -progress // -progress
static inline double CalculateSimpleIterationProgress(Timing::Phase phase, static inline double CalculateSimpleIterationProgress(
double overall_progress, Timing::Phase phase,
double iteration_start, base::Optional<double> overall_progress,
double active_time, double iteration_start,
double active_duration, double active_time,
double iteration_count) { double active_duration,
double iteration_count) {
// 1. If the overall progress is unresolved, return unresolved. // 1. If the overall progress is unresolved, return unresolved.
if (IsNull(overall_progress)) if (!overall_progress)
return NullValue(); return NullValue();
// 2. If overall progress is infinity, let the simple iteration progress be // 2. If overall progress is infinity, let the simple iteration progress be
// iteration start % 1.0, otherwise, let the simple iteration progress be // iteration start % 1.0, otherwise, let the simple iteration progress be
// overall progress % 1.0. // 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(iteration_start, 1.0)
: fmod(overall_progress, 1.0); : fmod(overall_progress.value(), 1.0);
// 3. If all of the following conditions are true, // 3. If all of the following conditions are true,
// * the simple iteration progress calculated above is zero, and // * the simple iteration progress calculated above is zero, and
...@@ -194,7 +196,7 @@ static inline double CalculateCurrentIteration( ...@@ -194,7 +196,7 @@ static inline double CalculateCurrentIteration(
Timing::Phase phase, Timing::Phase phase,
double active_time, double active_time,
double iteration_count, double iteration_count,
double overall_progress, base::Optional<double> overall_progress,
double simple_iteration_progress) { double simple_iteration_progress) {
// 1. If the active time is unresolved, return unresolved. // 1. If the active time is unresolved, return unresolved.
if (IsNull(active_time)) if (IsNull(active_time))
...@@ -206,15 +208,18 @@ static inline double CalculateCurrentIteration( ...@@ -206,15 +208,18 @@ static inline double CalculateCurrentIteration(
return std::numeric_limits<double>::infinity(); return std::numeric_limits<double>::infinity();
} }
if (!overall_progress)
return NullValue();
// 3. If the simple iteration progress is 1.0, return floor(overall progress) // 3. If the simple iteration progress is 1.0, return floor(overall progress)
// - 1. // - 1.
if (simple_iteration_progress == 1.0) { if (simple_iteration_progress == 1.0) {
// Safeguard for zero duration animation (crbug.com/954558). // 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). // 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 // https://drafts.csswg.org/web-animations/#calculating-the-directed-progress
......
...@@ -137,11 +137,11 @@ TEST(AnimationTimingCalculationsTest, IterationTime) { ...@@ -137,11 +137,11 @@ TEST(AnimationTimingCalculationsTest, IterationTime) {
TEST(AnimationTimingCalculationsTest, OverallProgress) { TEST(AnimationTimingCalculationsTest, OverallProgress) {
// If the active time is null. // If the active time is null.
EXPECT_TRUE(IsNull(CalculateOverallProgress(Timing::kPhaseAfter, EXPECT_FALSE(CalculateOverallProgress(Timing::kPhaseAfter,
/*active_time=*/NullValue(), /*active_time=*/NullValue(),
/*iteration_duration=*/1.0, /*iteration_duration=*/1.0,
/*iteration_count=*/1.0, /*iteration_count=*/1.0,
/*iteration_start=*/1.0))); /*iteration_start=*/1.0));
// If iteration duration is zero, calculate progress based on iteration count. // If iteration duration is zero, calculate progress based on iteration count.
EXPECT_EQ(3, CalculateOverallProgress(Timing::kPhaseActive, EXPECT_EQ(3, CalculateOverallProgress(Timing::kPhaseActive,
...@@ -173,13 +173,13 @@ TEST(AnimationTimingCalculationsTest, OverallProgress) { ...@@ -173,13 +173,13 @@ TEST(AnimationTimingCalculationsTest, OverallProgress) {
TEST(AnimationTimingCalculationsTest, CalculateSimpleIterationProgress) { TEST(AnimationTimingCalculationsTest, CalculateSimpleIterationProgress) {
// If the overall progress is null. // If the overall progress is null.
EXPECT_TRUE( EXPECT_TRUE(IsNull(
IsNull(CalculateSimpleIterationProgress(Timing::kPhaseAfter, CalculateSimpleIterationProgress(Timing::kPhaseAfter,
/*overall_progress=*/NullValue(), /*overall_progress=*/base::nullopt,
/*iteration_start=*/1.0, /*iteration_start=*/1.0,
/*active_time=*/NullValue(), /*active_time=*/NullValue(),
/*active_duration=*/1.0, /*active_duration=*/1.0,
/*iteration_count=*/1.0))); /*iteration_count=*/1.0)));
// If the overall progress is infinite. // If the overall progress is infinite.
const double inf = std::numeric_limits<double>::infinity(); const double inf = std::numeric_limits<double>::infinity();
...@@ -213,7 +213,7 @@ TEST(AnimationTimingCalculationsTest, CurrentIteration) { ...@@ -213,7 +213,7 @@ TEST(AnimationTimingCalculationsTest, CurrentIteration) {
IsNull(CalculateCurrentIteration(Timing::kPhaseAfter, IsNull(CalculateCurrentIteration(Timing::kPhaseAfter,
/*active_time=*/NullValue(), /*active_time=*/NullValue(),
/*iteration_count=*/1.0, /*iteration_count=*/1.0,
/*overall_progress=*/NullValue(), /*overall_progress=*/base::nullopt,
/*simple_iteration_progress=*/0))); /*simple_iteration_progress=*/0)));
// If the iteration count is infinite. // 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