1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/input/snap_fling_curve.h"
9 #include "build/build_config.h"
14 #if defined(OS_ANDROID)
15 constexpr double kDistanceEstimatorScalar = 40;
16 // The delta to be scrolled in next frame is 0.9 of the delta in last frame.
17 constexpr double kRatio = 0.9;
19 constexpr double kDistanceEstimatorScalar = 25;
20 // The delta to be scrolled in next frame is 0.92 of the delta in last frame.
21 constexpr double kRatio = 0.92;
23 constexpr double kMsPerFrame = 16;
24 constexpr base::TimeDelta kMaximumSnapDuration =
25 base::TimeDelta::FromSecondsD(5);
27 double GetDistanceFromDisplacement(gfx::Vector2dF displacement) {
28 return std::hypot(displacement.x(), displacement.y());
31 double EstimateFramesFromDistance(double distance) {
32 // We approximate scroll deltas as a geometric sequence with the ratio kRatio,
33 // and the last scrolled delta should be less or equal than 1, yielding the
34 // total distance as (1 - kRatio^(-n)) / (1 - (1 / kRatio)). Solving this
35 // could get n as below, which is the total number of deltas in the sequence,
36 // and is also the total frames needed to finish the curve.
37 return std::ceil(-std::log(1 - distance * (1 - 1 / kRatio)) /
41 double CalculateFirstDelta(double distance, double frames) {
42 // distance = first_delta (1 - kRatio^(frames) / (1 - kRatio)).
43 // We can get the |first_delta| by solving the equation above.
44 return distance * (1 - kRatio) / (1 - std::pow(kRatio, frames));
47 bool IsWithinOnePixel(gfx::Vector2dF actual, gfx::Vector2dF target) {
48 return std::abs(actual.x() - target.x()) < 1 &&
49 std::abs(actual.y() - target.y()) < 1;
54 gfx::Vector2dF SnapFlingCurve::EstimateDisplacement(
55 const gfx::Vector2dF& first_delta) {
56 gfx::Vector2dF destination = first_delta;
57 destination.Scale(kDistanceEstimatorScalar);
61 SnapFlingCurve::SnapFlingCurve(const gfx::Vector2dF& start_offset,
62 const gfx::Vector2dF& target_offset,
63 base::TimeTicks first_gsu_time)
64 : start_offset_(start_offset),
65 total_displacement_(target_offset - start_offset),
66 total_distance_(GetDistanceFromDisplacement(total_displacement_)),
67 start_time_(first_gsu_time),
68 total_frames_(EstimateFramesFromDistance(total_distance_)),
69 first_delta_(CalculateFirstDelta(total_distance_, total_frames_)),
70 duration_(base::TimeDelta::FromMilliseconds(total_frames_ * kMsPerFrame)),
71 is_finished_(total_distance_ == 0) {
74 ratio_x_ = total_displacement_.x() / total_distance_;
75 ratio_y_ = total_displacement_.y() / total_distance_;
78 SnapFlingCurve::~SnapFlingCurve() = default;
80 double SnapFlingCurve::GetCurrentCurveDistance(base::TimeDelta current_time) {
81 double current_frame = current_time.InMillisecondsF() / kMsPerFrame + 1;
83 first_delta_ * (1 - std::pow(kRatio, current_frame)) / (1 - kRatio);
84 return sum <= total_distance_ ? sum : total_distance_;
87 gfx::Vector2dF SnapFlingCurve::GetScrollDelta(base::TimeTicks time_stamp) {
89 return gfx::Vector2dF();
91 // The the snap offset may never be reached due to clamping or other factors.
92 // To avoid a never ending snap curve, we force the curve to end if the time
93 // has passed |duration_| or the remaining displacement is less than 1.
94 base::TimeDelta current_time = time_stamp - start_time_;
95 if (current_time >= std::min(duration_, kMaximumSnapDuration) ||
96 IsWithinOnePixel(current_displacement_, total_displacement_)) {
98 return total_displacement_ - current_displacement_;
101 double new_distance = GetCurrentCurveDistance(current_time);
102 gfx::Vector2dF new_displacement(new_distance * ratio_x_,
103 new_distance * ratio_y_);
105 return new_displacement - current_displacement_;
108 void SnapFlingCurve::UpdateCurrentOffset(const gfx::Vector2dF& current_offset) {
109 current_displacement_ = current_offset - start_offset_;
112 bool SnapFlingCurve::IsFinished() const {