File: snap_fling_curve.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (115 lines) | stat: -rw-r--r-- 4,274 bytes parent folder | download | duplicates (11)
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
// Copyright 2018 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/input/snap_fling_curve.h"

#include <algorithm>
#include <cmath>
#include "build/build_config.h"

namespace cc {
namespace {

#if BUILDFLAG(IS_ANDROID)
constexpr double kDistanceEstimatorScalar = 40;
// The delta to be scrolled in next frame is 0.9 of the delta in last frame.
constexpr double kRatio = 0.9;
#else
constexpr double kDistanceEstimatorScalar = 25;
// The delta to be scrolled in next frame is 0.92 of the delta in last frame.
constexpr double kRatio = 0.92;
#endif
constexpr auto kFrameTime = base::Milliseconds(16);
constexpr base::TimeDelta kMaximumSnapDuration = base::Seconds(5);

double GetDistanceFromDisplacement(gfx::Vector2dF displacement) {
  return std::hypot(displacement.x(), displacement.y());
}

double EstimateFramesFromDistance(double distance) {
  // We approximate scroll deltas as a geometric sequence with the ratio kRatio,
  // and the last scrolled delta should be less or equal than 1, yielding the
  // total distance as (1 - kRatio^(-n)) / (1 - (1 / kRatio)). Solving this
  // could get n as below, which is the total number of deltas in the sequence,
  // and is also the total frames needed to finish the curve.
  return std::ceil(-std::log(1 - distance * (1 - 1 / kRatio)) /
                   std::log(kRatio));
}

double CalculateFirstDelta(double distance, double frames) {
  // distance = first_delta (1 - kRatio^(frames) / (1 - kRatio)).
  // We can get the |first_delta| by solving the equation above.
  return distance * (1 - kRatio) / (1 - std::pow(kRatio, frames));
}

bool IsWithinOnePixel(gfx::Vector2dF actual, gfx::Vector2dF target) {
  return std::abs(actual.x() - target.x()) < 1 &&
         std::abs(actual.y() - target.y()) < 1;
}

}  // namespace

gfx::Vector2dF SnapFlingCurve::EstimateDisplacement(
    const gfx::Vector2dF& first_delta) {
  gfx::Vector2dF destination = first_delta;
  destination.Scale(kDistanceEstimatorScalar);
  return destination;
}

SnapFlingCurve::SnapFlingCurve(const gfx::PointF& start_offset,
                               const gfx::PointF& target_offset,
                               base::TimeTicks first_gsu_time)
    : start_offset_(start_offset),
      total_displacement_(target_offset - start_offset),
      total_distance_(GetDistanceFromDisplacement(total_displacement_)),
      start_time_(first_gsu_time),
      total_frames_(EstimateFramesFromDistance(total_distance_)),
      first_delta_(CalculateFirstDelta(total_distance_, total_frames_)),
      duration_(total_frames_ * kFrameTime),
      is_finished_(total_distance_ == 0) {
  if (is_finished_)
    return;
  ratio_x_ = total_displacement_.x() / total_distance_;
  ratio_y_ = total_displacement_.y() / total_distance_;
}

SnapFlingCurve::~SnapFlingCurve() = default;

double SnapFlingCurve::GetCurrentCurveDistance(base::TimeDelta current_time) {
  const double current_frame = current_time / kFrameTime + 1;
  const double sum =
      first_delta_ * (1 - std::pow(kRatio, current_frame)) / (1 - kRatio);
  return std::min(sum, total_distance_);
}

gfx::Vector2dF SnapFlingCurve::GetScrollDelta(base::TimeTicks time_stamp) {
  if (is_finished_)
    return gfx::Vector2dF();

  // The the snap offset may never be reached due to clamping or other factors.
  // To avoid a never ending snap curve, we force the curve to end if the time
  // has passed |duration_| or the remaining displacement is less than 1.
  base::TimeDelta current_time = time_stamp - start_time_;
  if (current_time >= std::min(duration_, kMaximumSnapDuration) ||
      IsWithinOnePixel(current_displacement_, total_displacement_)) {
    is_finished_ = true;
    return total_displacement_ - current_displacement_;
  }

  double new_distance = GetCurrentCurveDistance(current_time);
  gfx::Vector2dF new_displacement(new_distance * ratio_x_,
                                  new_distance * ratio_y_);

  return new_displacement - current_displacement_;
}

void SnapFlingCurve::UpdateCurrentOffset(const gfx::PointF& current_offset) {
  current_displacement_ = current_offset - start_offset_;
}

bool SnapFlingCurve::IsFinished() const {
  return is_finished_;
}

}  // namespace cc