snap_fling_curve.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include "cc/input/snap_fling_curve.h"
  5. #include <algorithm>
  6. #include <cmath>
  7. #include "build/build_config.h"
  8. namespace cc {
  9. namespace {
  10. #if BUILDFLAG(IS_ANDROID)
  11. constexpr double kDistanceEstimatorScalar = 40;
  12. // The delta to be scrolled in next frame is 0.9 of the delta in last frame.
  13. constexpr double kRatio = 0.9;
  14. #else
  15. constexpr double kDistanceEstimatorScalar = 25;
  16. // The delta to be scrolled in next frame is 0.92 of the delta in last frame.
  17. constexpr double kRatio = 0.92;
  18. #endif
  19. constexpr auto kFrameTime = base::Milliseconds(16);
  20. constexpr base::TimeDelta kMaximumSnapDuration = base::Seconds(5);
  21. double GetDistanceFromDisplacement(gfx::Vector2dF displacement) {
  22. return std::hypot(displacement.x(), displacement.y());
  23. }
  24. double EstimateFramesFromDistance(double distance) {
  25. // We approximate scroll deltas as a geometric sequence with the ratio kRatio,
  26. // and the last scrolled delta should be less or equal than 1, yielding the
  27. // total distance as (1 - kRatio^(-n)) / (1 - (1 / kRatio)). Solving this
  28. // could get n as below, which is the total number of deltas in the sequence,
  29. // and is also the total frames needed to finish the curve.
  30. return std::ceil(-std::log(1 - distance * (1 - 1 / kRatio)) /
  31. std::log(kRatio));
  32. }
  33. double CalculateFirstDelta(double distance, double frames) {
  34. // distance = first_delta (1 - kRatio^(frames) / (1 - kRatio)).
  35. // We can get the |first_delta| by solving the equation above.
  36. return distance * (1 - kRatio) / (1 - std::pow(kRatio, frames));
  37. }
  38. bool IsWithinOnePixel(gfx::Vector2dF actual, gfx::Vector2dF target) {
  39. return std::abs(actual.x() - target.x()) < 1 &&
  40. std::abs(actual.y() - target.y()) < 1;
  41. }
  42. } // namespace
  43. gfx::Vector2dF SnapFlingCurve::EstimateDisplacement(
  44. const gfx::Vector2dF& first_delta) {
  45. gfx::Vector2dF destination = first_delta;
  46. destination.Scale(kDistanceEstimatorScalar);
  47. return destination;
  48. }
  49. SnapFlingCurve::SnapFlingCurve(const gfx::PointF& start_offset,
  50. const gfx::PointF& target_offset,
  51. base::TimeTicks first_gsu_time)
  52. : start_offset_(start_offset),
  53. total_displacement_(target_offset - start_offset),
  54. total_distance_(GetDistanceFromDisplacement(total_displacement_)),
  55. start_time_(first_gsu_time),
  56. total_frames_(EstimateFramesFromDistance(total_distance_)),
  57. first_delta_(CalculateFirstDelta(total_distance_, total_frames_)),
  58. duration_(total_frames_ * kFrameTime),
  59. is_finished_(total_distance_ == 0) {
  60. if (is_finished_)
  61. return;
  62. ratio_x_ = total_displacement_.x() / total_distance_;
  63. ratio_y_ = total_displacement_.y() / total_distance_;
  64. }
  65. SnapFlingCurve::~SnapFlingCurve() = default;
  66. double SnapFlingCurve::GetCurrentCurveDistance(base::TimeDelta current_time) {
  67. const double current_frame = current_time / kFrameTime + 1;
  68. const double sum =
  69. first_delta_ * (1 - std::pow(kRatio, current_frame)) / (1 - kRatio);
  70. return std::min(sum, total_distance_);
  71. }
  72. gfx::Vector2dF SnapFlingCurve::GetScrollDelta(base::TimeTicks time_stamp) {
  73. if (is_finished_)
  74. return gfx::Vector2dF();
  75. // The the snap offset may never be reached due to clamping or other factors.
  76. // To avoid a never ending snap curve, we force the curve to end if the time
  77. // has passed |duration_| or the remaining displacement is less than 1.
  78. base::TimeDelta current_time = time_stamp - start_time_;
  79. if (current_time >= std::min(duration_, kMaximumSnapDuration) ||
  80. IsWithinOnePixel(current_displacement_, total_displacement_)) {
  81. is_finished_ = true;
  82. return total_displacement_ - current_displacement_;
  83. }
  84. double new_distance = GetCurrentCurveDistance(current_time);
  85. gfx::Vector2dF new_displacement(new_distance * ratio_x_,
  86. new_distance * ratio_y_);
  87. return new_displacement - current_displacement_;
  88. }
  89. void SnapFlingCurve::UpdateCurrentOffset(const gfx::PointF& current_offset) {
  90. current_displacement_ = current_offset - start_offset_;
  91. }
  92. bool SnapFlingCurve::IsFinished() const {
  93. return is_finished_;
  94. }
  95. } // namespace cc