1 // Copyright 2014 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 "content/child/touch_fling_gesture_curve.h"
6
7 #include <cmath>
8
9 #include "base/debug/trace_event.h"
10 #include "base/logging.h"
11 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
12 #include "third_party/WebKit/public/platform/WebFloatSize.h"
13 #include "third_party/WebKit/public/platform/WebGestureCurve.h"
14 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h"
15 #include "third_party/WebKit/public/platform/WebSize.h"
16
17 using blink::WebFloatPoint;
18 using blink::WebFloatSize;
19 using blink::WebGestureCurve;
20 using blink::WebGestureCurveTarget;
21 using blink::WebSize;
22
23 namespace {
24
25 const char* kCurveName = "TouchFlingGestureCurve";
26
position(double t,float * p)27 inline double position(double t, float* p) {
28 return p[0] * exp(-p[2] * t) - p[1] * t - p[0];
29 }
30
velocity(double t,float * p)31 inline double velocity(double t, float* p) {
32 return -p[0] * p[2] * exp(-p[2] * t) - p[1];
33 }
34
timeAtVelocity(double v,float * p)35 inline double timeAtVelocity(double v, float* p) {
36 DCHECK(p[0]);
37 DCHECK(p[2]);
38 return -log((v + p[1]) / (-p[0] * p[2])) / p[2];
39 }
40
41 } // namespace
42
43
44 namespace content {
45
46 // This curve implementation is based on the notion of a single, absolute
47 // curve, which starts at a large velocity and smoothly decreases to
48 // zero. For a given input velocity, we find where on the curve this
49 // velocity occurs, and start the animation at this point---denoted by
50 // (time_offset_, position_offset_).
51 //
52 // This has the effect of automatically determining an animation duration
53 // that scales with input velocity, as faster initial velocities start
54 // earlier on the curve and thus take longer to reach the end. No
55 // complicated time scaling is required.
56 //
57 // Since the starting velocity is implicitly determined by our starting
58 // point, we only store the relative magnitude and direction of both
59 // initial x- and y-velocities, and use this to scale the computed
60 // displacement at any point in time. This guarantees that fling
61 // trajectories are straight lines when viewed in x-y space. Initial
62 // velocities that lie outside the max velocity are constrained to start
63 // at zero (and thus are implicitly scaled).
64 //
65 // The curve is modelled as a 4th order polynomial, starting at t = 0,
66 // and ending at t = curve_duration_. Attempts to generate
67 // position/velocity estimates outside this range are undefined.
68
Create(const WebFloatPoint & initial_velocity,float p0,float p1,float p2,const WebSize & cumulative_scroll)69 WebGestureCurve* TouchFlingGestureCurve::Create(
70 const WebFloatPoint& initial_velocity,
71 float p0,
72 float p1,
73 float p2,
74 const WebSize& cumulative_scroll) {
75 return new TouchFlingGestureCurve(initial_velocity, p0, p1, p2,
76 cumulative_scroll);
77 }
78
TouchFlingGestureCurve(const WebFloatPoint & initial_velocity,float alpha,float beta,float gamma,const WebSize & cumulative_scroll)79 TouchFlingGestureCurve::TouchFlingGestureCurve(
80 const WebFloatPoint& initial_velocity,
81 float alpha,
82 float beta,
83 float gamma,
84 const WebSize& cumulative_scroll)
85 : cumulative_scroll_(WebFloatSize(cumulative_scroll.width,
86 cumulative_scroll.height)) {
87 DCHECK(initial_velocity != WebFloatPoint());
88
89 coefficients_[0] = alpha;
90 coefficients_[1] = beta;
91 coefficients_[2] = gamma;
92
93 // Curve ends when velocity reaches zero.
94 curve_duration_ = timeAtVelocity(0, coefficients_);
95 DCHECK(curve_duration_ > 0);
96
97 float max_start_velocity = std::max(fabs(initial_velocity.x),
98 fabs(initial_velocity.y));
99
100 // Force max_start_velocity to lie in the range v(0) to v(curve_duration),
101 // and assume that the curve parameters define a monotonically decreasing
102 // velocity, or else bisection search may fail.
103 if (max_start_velocity > velocity(0, coefficients_))
104 max_start_velocity = velocity(0, coefficients_);
105
106 if (max_start_velocity < 0)
107 max_start_velocity = 0;
108
109 // We keep track of relative magnitudes and directions of the
110 // velocity/displacement components here.
111 displacement_ratio_ = WebFloatPoint(initial_velocity.x / max_start_velocity,
112 initial_velocity.y / max_start_velocity);
113
114 // Compute time-offset for start velocity.
115 time_offset_ = timeAtVelocity(max_start_velocity, coefficients_);
116
117 // Compute curve position at offset time
118 position_offset_ = position(time_offset_, coefficients_);
119 TRACE_EVENT_ASYNC_BEGIN1("input", "GestureAnimation", this, "curve",
120 kCurveName);
121 }
122
~TouchFlingGestureCurve()123 TouchFlingGestureCurve::~TouchFlingGestureCurve() {
124 TRACE_EVENT_ASYNC_END0("input", "GestureAnimation", this);
125 }
126
apply(double time,WebGestureCurveTarget * target)127 bool TouchFlingGestureCurve::apply(double time, WebGestureCurveTarget* target) {
128 // If the fling has yet to start, simply return and report true to prevent
129 // fling termination.
130 if (time <= 0)
131 return true;
132
133 float displacement;
134 float speed;
135 if (time + time_offset_ < curve_duration_) {
136 displacement =
137 position(time + time_offset_, coefficients_) - position_offset_;
138 speed = velocity(time + time_offset_, coefficients_);
139 } else {
140 displacement = position(curve_duration_, coefficients_) - position_offset_;
141 speed = 0.f;
142 }
143
144 // Keep track of integer portion of scroll thus far, and prepare increment.
145 WebFloatSize scroll(displacement * displacement_ratio_.x,
146 displacement * displacement_ratio_.y);
147 WebFloatSize scroll_increment(scroll.width - cumulative_scroll_.width,
148 scroll.height - cumulative_scroll_.height);
149 WebFloatSize scroll_velocity(speed * displacement_ratio_.x,
150 speed * displacement_ratio_.y);
151 cumulative_scroll_ = scroll;
152
153 if (time + time_offset_ < curve_duration_ ||
154 scroll_increment != WebFloatSize()) {
155 // scrollBy() could delete this curve if the animation is over, so don't
156 // touch any member variables after making that call.
157 return target->scrollBy(scroll_increment, scroll_velocity);
158 }
159
160 return false;
161 }
162
163 } // namespace content
164