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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/animation/scroll_offset_animation_curve.h"
#include <algorithm>
#include <cmath>
#include <utility>
#include "base/check_op.h"
#include "base/memory/ptr_util.h"
#include "cc/base/features.h"
#include "ui/gfx/animation/keyframe/timing_function.h"
#include "ui/gfx/animation/tween.h"
const double kConstantDuration = 9.0;
const double kDurationDivisor = 60.0;
struct CubicBezierPoints {
double x1;
double y1;
double x2;
double y2;
};
// See `ui/gfx/animation/keyframe/timing_function.cc`
static constexpr CubicBezierPoints kEaseInOutControlPoints{
.x1 = 0.42,
.y1 = 0,
.x2 = 0.58,
.y2 = 1,
};
CubicBezierPoints GetCubicBezierPointsForProgrammaticScroll() {
return {
.x1 = features::kCubicBezierX1.Get(),
.y1 = features::kCubicBezierY1.Get(),
.x2 = features::kCubicBezierX2.Get(),
.y2 = features::kCubicBezierY2.Get(),
};
}
const double kInverseDeltaRampStartPx = 120.0;
const double kInverseDeltaRampEndPx = 480.0;
const double kInverseDeltaMinDuration = 6.0;
const double kInverseDeltaMaxDuration = 12.0;
const double kInverseDeltaSlope =
(kInverseDeltaMinDuration - kInverseDeltaMaxDuration) /
(kInverseDeltaRampEndPx - kInverseDeltaRampStartPx);
const double kInverseDeltaOffset =
kInverseDeltaMaxDuration - kInverseDeltaRampStartPx * kInverseDeltaSlope;
using gfx::CubicBezierTimingFunction;
using gfx::LinearTimingFunction;
using gfx::TimingFunction;
namespace cc {
namespace {
const double kEpsilon = 0.01f;
static float MaximumDimension(const gfx::Vector2dF& delta) {
return std::abs(delta.x()) > std::abs(delta.y()) ? delta.x() : delta.y();
}
std::unique_ptr<TimingFunction> EaseInOutWithInitialSlope(
const CubicBezierPoints& control_points,
double slope) {
// Clamp slope to a sane value.
slope = std::clamp(slope, -1000.0, 1000.0);
// Scale the first control point with `slope`.
return CubicBezierTimingFunction::Create(
control_points.x1, control_points.x1 * slope, control_points.x2,
control_points.y2);
}
base::TimeDelta VelocityBasedDurationBound(gfx::Vector2dF old_delta,
double velocity,
gfx::Vector2dF new_delta) {
double new_delta_max_dimension = MaximumDimension(new_delta);
// If we are already at the target, stop animating.
if (std::abs(new_delta_max_dimension) < kEpsilon)
return base::TimeDelta();
// Guard against division by zero.
if (std::abs(velocity) < kEpsilon) {
return base::TimeDelta::Max();
}
// Estimate how long it will take to reach the new target at our present
// velocity, with some fudge factor to account for the "ease out".
double bound = (new_delta_max_dimension / velocity) * 2.5f;
// If bound < 0 we are moving in the opposite direction.
return bound < 0 ? base::TimeDelta::Max() : base::Seconds(bound);
}
} // namespace
std::optional<double>
ScrollOffsetAnimationCurve::animation_duration_for_testing_;
ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
const gfx::PointF& target_value,
AnimationType animation_type,
ScrollType scroll_type,
std::optional<DurationBehavior> duration_behavior)
: target_value_(target_value),
animation_type_(animation_type),
scroll_type_(scroll_type),
duration_behavior_(duration_behavior),
has_set_initial_value_(false) {
DCHECK_EQ(animation_type == AnimationType::kEaseInOut,
duration_behavior.has_value());
switch (animation_type) {
case AnimationType::kEaseInOut:
timing_function_ = GetEasingFunction(/*slope=*/std::nullopt);
break;
case AnimationType::kLinear:
timing_function_ = LinearTimingFunction::Create();
break;
}
}
ScrollOffsetAnimationCurve::ScrollOffsetAnimationCurve(
const gfx::PointF& target_value,
std::unique_ptr<TimingFunction> timing_function,
AnimationType animation_type,
ScrollType scroll_type,
std::optional<DurationBehavior> duration_behavior)
: target_value_(target_value),
timing_function_(std::move(timing_function)),
animation_type_(animation_type),
scroll_type_(scroll_type),
duration_behavior_(duration_behavior),
has_set_initial_value_(false) {
DCHECK_EQ(animation_type == AnimationType::kEaseInOut,
duration_behavior.has_value());
}
ScrollOffsetAnimationCurve::~ScrollOffsetAnimationCurve() = default;
base::TimeDelta ScrollOffsetAnimationCurve::EaseInOutSegmentDuration(
const gfx::Vector2dF& delta,
DurationBehavior duration_behavior,
base::TimeDelta delayed_by) {
double duration = kConstantDuration;
if (!animation_duration_for_testing_) {
switch (duration_behavior) {
case DurationBehavior::kConstant:
duration = kConstantDuration;
break;
case DurationBehavior::kDeltaBased: {
CHECK_EQ(scroll_type_, ScrollType::kProgrammatic);
duration =
std::min<double>(std::sqrt(std::abs(MaximumDimension(delta))),
features::kMaxAnimtionDuration.Get().InSecondsF() *
kDurationDivisor);
break;
}
case DurationBehavior::kInverseDelta:
duration = kInverseDeltaOffset +
std::abs(MaximumDimension(delta)) * kInverseDeltaSlope;
duration = std::clamp(duration, kInverseDeltaMinDuration,
kInverseDeltaMaxDuration);
break;
}
duration /= kDurationDivisor;
} else {
duration = animation_duration_for_testing_.value();
}
base::TimeDelta delay_adjusted_duration =
base::Seconds(duration) - delayed_by;
return (delay_adjusted_duration >= base::TimeDelta())
? delay_adjusted_duration
: base::TimeDelta();
}
base::TimeDelta ScrollOffsetAnimationCurve::EaseInOutBoundedSegmentDuration(
const gfx::Vector2dF& new_delta,
base::TimeDelta t,
base::TimeDelta delayed_by) {
gfx::Vector2dF old_delta = target_value_ - initial_value_;
double velocity = CalculateVelocity(t);
// Use the velocity-based duration bound when it is less than the constant
// segment duration. This minimizes the "rubber-band" bouncing effect when
// |velocity| is large and |new_delta| is small.
return std::min(EaseInOutSegmentDuration(
new_delta, duration_behavior_.value(), delayed_by),
VelocityBasedDurationBound(old_delta, velocity, new_delta));
}
base::TimeDelta ScrollOffsetAnimationCurve::SegmentDuration(
const gfx::Vector2dF& delta,
base::TimeDelta delayed_by,
std::optional<double> velocity) {
switch (animation_type_) {
case AnimationType::kEaseInOut:
DCHECK(duration_behavior_.has_value());
return EaseInOutSegmentDuration(delta, duration_behavior_.value(),
delayed_by);
case AnimationType::kLinear:
DCHECK(velocity.has_value());
return LinearSegmentDuration(delta, delayed_by, velocity.value());
}
}
// static
base::TimeDelta ScrollOffsetAnimationCurve::LinearSegmentDuration(
const gfx::Vector2dF& delta,
base::TimeDelta delayed_by,
float velocity) {
double duration_in_seconds =
(animation_duration_for_testing_.has_value())
? animation_duration_for_testing_.value()
: std::abs(MaximumDimension(delta) / velocity);
base::TimeDelta delay_adjusted_duration =
base::Seconds(duration_in_seconds) - delayed_by;
return (delay_adjusted_duration >= base::TimeDelta())
? delay_adjusted_duration
: base::TimeDelta();
}
void ScrollOffsetAnimationCurve::SetInitialValue(
const gfx::PointF& initial_value,
base::TimeDelta delayed_by,
float velocity) {
initial_value_ = initial_value;
has_set_initial_value_ = true;
gfx::Vector2dF delta = target_value_ - initial_value;
total_animation_duration_ = SegmentDuration(delta, delayed_by, velocity);
}
bool ScrollOffsetAnimationCurve::HasSetInitialValue() const {
return has_set_initial_value_;
}
void ScrollOffsetAnimationCurve::ApplyAdjustment(
const gfx::Vector2dF& adjustment) {
initial_value_ = initial_value_ + adjustment;
target_value_ = target_value_ + adjustment;
}
gfx::PointF ScrollOffsetAnimationCurve::GetValue(base::TimeDelta t) const {
const base::TimeDelta duration = total_animation_duration_ - last_retarget_;
t -= last_retarget_;
if (duration.is_zero() || (t >= duration))
return target_value_;
if (t <= base::TimeDelta())
return initial_value_;
const double progress = timing_function_->GetValue(
t / duration, TimingFunction::LimitDirection::RIGHT);
return gfx::PointF(gfx::Tween::FloatValueBetween(progress, initial_value_.x(),
target_value_.x()),
gfx::Tween::FloatValueBetween(progress, initial_value_.y(),
target_value_.y()));
}
base::TimeDelta ScrollOffsetAnimationCurve::Duration() const {
return total_animation_duration_;
}
int ScrollOffsetAnimationCurve::Type() const {
return AnimationCurve::SCROLL_OFFSET;
}
const char* ScrollOffsetAnimationCurve::TypeName() const {
return "ScrollOffset";
}
std::unique_ptr<gfx::AnimationCurve> ScrollOffsetAnimationCurve::Clone() const {
return CloneToScrollOffsetAnimationCurve();
}
void ScrollOffsetAnimationCurve::Tick(
base::TimeDelta t,
int property_id,
gfx::KeyframeModel* keyframe_model,
gfx::TimingFunction::LimitDirection unused) const {
if (target_) {
target_->OnScrollOffsetAnimated(GetValue(t), property_id, keyframe_model);
}
}
std::unique_ptr<ScrollOffsetAnimationCurve>
ScrollOffsetAnimationCurve::CloneToScrollOffsetAnimationCurve() const {
std::unique_ptr<TimingFunction> timing_function(
static_cast<TimingFunction*>(timing_function_->Clone().release()));
std::unique_ptr<ScrollOffsetAnimationCurve> curve_clone =
base::WrapUnique(new ScrollOffsetAnimationCurve(
target_value_, std::move(timing_function), animation_type_,
scroll_type_, duration_behavior_));
curve_clone->initial_value_ = initial_value_;
curve_clone->total_animation_duration_ = total_animation_duration_;
curve_clone->last_retarget_ = last_retarget_;
curve_clone->has_set_initial_value_ = has_set_initial_value_;
return curve_clone;
}
void ScrollOffsetAnimationCurve::SetAnimationDurationForTesting(
base::TimeDelta duration) {
animation_duration_for_testing_ = duration.InSecondsF();
}
double ScrollOffsetAnimationCurve::CalculateVelocity(base::TimeDelta t) {
base::TimeDelta duration = total_animation_duration_ - last_retarget_;
const double slope =
timing_function_->Velocity((t - last_retarget_) / duration);
gfx::Vector2dF delta = target_value_ - initial_value_;
// TimingFunction::Velocity just gives the slope of the curve. Convert it to
// units of pixels per second.
return slope * (MaximumDimension(delta) / duration.InSecondsF());
}
std::unique_ptr<TimingFunction> ScrollOffsetAnimationCurve::GetEasingFunction(
std::optional<double> slope) {
CubicBezierPoints control_points = kEaseInOutControlPoints;
if (scroll_type_ == ScrollType::kProgrammatic) {
control_points = GetCubicBezierPointsForProgrammaticScroll();
}
if (slope) {
return EaseInOutWithInitialSlope(control_points, *slope);
}
return CubicBezierTimingFunction::Create(control_points.x1, control_points.y1,
control_points.x2,
control_points.y2);
}
void ScrollOffsetAnimationCurve::UpdateTarget(base::TimeDelta t,
const gfx::PointF& new_target) {
DCHECK_NE(animation_type_, AnimationType::kLinear)
<< "UpdateTarget is not supported on linear scroll animations.";
// UpdateTarget is still called for linear animations occasionally. This is
// tracked via crbug.com/1164008.
if (animation_type_ == AnimationType::kLinear)
return;
// If the new UpdateTarget actually happened before the previous one, keep
// |t| as the most recent, but reduce the duration of any generated
// animation.
base::TimeDelta delayed_by = std::max(base::TimeDelta(), last_retarget_ - t);
t = std::max(t, last_retarget_);
if (animation_type_ == AnimationType::kEaseInOut &&
std::abs(MaximumDimension(target_value_ - new_target)) < kEpsilon) {
// Don't update the animation if the new target is the same as the old one.
// This is done for EaseInOut-style animation curves, since the duration is
// inversely proportional to the distance, and it may cause an animation
// that is longer than the one currently running.
// Specifically avoid doing this for Impulse-style animation curves since
// its duration is directly proportional to the distance, and we don't want
// to drop user input.
target_value_ = new_target;
return;
}
gfx::PointF current_position = GetValue(t);
gfx::Vector2dF new_delta = new_target - current_position;
// We are already at or very close to the new target. Stop animating.
if (std::abs(MaximumDimension(new_delta)) < kEpsilon) {
last_retarget_ = t;
total_animation_duration_ = t;
target_value_ = new_target;
return;
}
// The last segment was of zero duration.
base::TimeDelta old_duration = total_animation_duration_ - last_retarget_;
if (old_duration.is_zero()) {
DCHECK_EQ(t, last_retarget_);
total_animation_duration_ = SegmentDuration(new_delta, delayed_by);
target_value_ = new_target;
return;
}
const base::TimeDelta new_duration =
EaseInOutBoundedSegmentDuration(new_delta, t, delayed_by);
if (new_duration.InSecondsF() < kEpsilon) {
// The duration is (close to) 0, so stop the animation.
target_value_ = new_target;
total_animation_duration_ = t;
return;
}
// Adjust the slope of the new animation in order to preserve the velocity of
// the old animation.
double velocity = CalculateVelocity(t);
double new_slope =
velocity * (new_duration.InSecondsF() / MaximumDimension(new_delta));
timing_function_ = GetEasingFunction(new_slope);
initial_value_ = current_position;
target_value_ = new_target;
total_animation_duration_ = t + new_duration;
last_retarget_ = t;
}
const ScrollOffsetAnimationCurve*
ScrollOffsetAnimationCurve::ToScrollOffsetAnimationCurve(
const AnimationCurve* c) {
DCHECK_EQ(ScrollOffsetAnimationCurve::SCROLL_OFFSET, c->Type());
return static_cast<const ScrollOffsetAnimationCurve*>(c);
}
ScrollOffsetAnimationCurve*
ScrollOffsetAnimationCurve::ToScrollOffsetAnimationCurve(AnimationCurve* c) {
DCHECK_EQ(ScrollOffsetAnimationCurve::SCROLL_OFFSET, c->Type());
return static_cast<ScrollOffsetAnimationCurve*>(c);
}
} // namespace cc
|