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