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.
4 
5 #include "cc/input/snap_fling_curve.h"
6 
7 #include <algorithm>
8 #include <cmath>
9 #include "build/build_config.h"
10 
11 namespace cc {
12 namespace {
13 
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;
18 #else
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;
22 #endif
23 constexpr double kMsPerFrame = 16;
24 constexpr base::TimeDelta kMaximumSnapDuration =
25     base::TimeDelta::FromSecondsD(5);
26 
GetDistanceFromDisplacement(gfx::Vector2dF displacement)27 double GetDistanceFromDisplacement(gfx::Vector2dF displacement) {
28   return std::hypot(displacement.x(), displacement.y());
29 }
30 
EstimateFramesFromDistance(double distance)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)) /
38                    std::log(kRatio));
39 }
40 
CalculateFirstDelta(double distance,double frames)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));
45 }
46 
IsWithinOnePixel(gfx::Vector2dF actual,gfx::Vector2dF target)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;
50 }
51 
52 }  // namespace
53 
EstimateDisplacement(const gfx::Vector2dF & first_delta)54 gfx::Vector2dF SnapFlingCurve::EstimateDisplacement(
55     const gfx::Vector2dF& first_delta) {
56   gfx::Vector2dF destination = first_delta;
57   destination.Scale(kDistanceEstimatorScalar);
58   return destination;
59 }
60 
SnapFlingCurve(const gfx::Vector2dF & start_offset,const gfx::Vector2dF & target_offset,base::TimeTicks first_gsu_time)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) {
72   if (is_finished_)
73     return;
74   ratio_x_ = total_displacement_.x() / total_distance_;
75   ratio_y_ = total_displacement_.y() / total_distance_;
76 }
77 
78 SnapFlingCurve::~SnapFlingCurve() = default;
79 
GetCurrentCurveDistance(base::TimeDelta current_time)80 double SnapFlingCurve::GetCurrentCurveDistance(base::TimeDelta current_time) {
81   double current_frame = current_time.InMillisecondsF() / kMsPerFrame + 1;
82   double sum =
83       first_delta_ * (1 - std::pow(kRatio, current_frame)) / (1 - kRatio);
84   return sum <= total_distance_ ? sum : total_distance_;
85 }
86 
GetScrollDelta(base::TimeTicks time_stamp)87 gfx::Vector2dF SnapFlingCurve::GetScrollDelta(base::TimeTicks time_stamp) {
88   if (is_finished_)
89     return gfx::Vector2dF();
90 
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_)) {
97     is_finished_ = true;
98     return total_displacement_ - current_displacement_;
99   }
100 
101   double new_distance = GetCurrentCurveDistance(current_time);
102   gfx::Vector2dF new_displacement(new_distance * ratio_x_,
103                                   new_distance * ratio_y_);
104 
105   return new_displacement - current_displacement_;
106 }
107 
UpdateCurrentOffset(const gfx::Vector2dF & current_offset)108 void SnapFlingCurve::UpdateCurrentOffset(const gfx::Vector2dF& current_offset) {
109   current_displacement_ = current_offset - start_offset_;
110 }
111 
IsFinished() const112 bool SnapFlingCurve::IsFinished() const {
113   return is_finished_;
114 }
115 
116 }  // namespace cc
117