1 // Copyright (c) 2012 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.
5 #include "webkit/child/touch_fling_gesture_curve.h"
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"
17 using blink::WebFloatPoint
;
18 using blink::WebFloatSize
;
19 using blink::WebGestureCurve
;
20 using blink::WebGestureCurveTarget
;
25 const char* kCurveName
= "TouchFlingGestureCurve";
27 inline double position(double t
, float* p
) {
28 return p
[0] * exp(-p
[2] * t
) - p
[1] * t
- p
[0];
31 inline double velocity(double t
, float* p
) {
32 return -p
[0] * p
[2] * exp(-p
[2] * t
) - p
[1];
35 inline double timeAtVelocity(double v
, float* p
) {
38 return -log((v
+ p
[1]) / (-p
[0] * p
[2])) / p
[2];
44 namespace webkit_glue
{
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_).
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.
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).
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.
69 WebGestureCurve
* TouchFlingGestureCurve::Create(
70 const WebFloatPoint
& initial_velocity
,
74 const WebSize
& cumulative_scroll
) {
75 return new TouchFlingGestureCurve(initial_velocity
, p0
, p1
, p2
,
79 TouchFlingGestureCurve::TouchFlingGestureCurve(
80 const WebFloatPoint
& initial_velocity
,
84 const WebSize
& cumulative_scroll
)
85 : cumulative_scroll_(WebFloatSize(cumulative_scroll
.width
,
86 cumulative_scroll
.height
)) {
87 DCHECK(initial_velocity
!= WebFloatPoint());
89 coefficients_
[0] = alpha
;
90 coefficients_
[1] = beta
;
91 coefficients_
[2] = gamma
;
93 // Curve ends when velocity reaches zero.
94 curve_duration_
= timeAtVelocity(0, coefficients_
);
95 DCHECK(curve_duration_
> 0);
97 float max_start_velocity
= std::max(fabs(initial_velocity
.x
),
98 fabs(initial_velocity
.y
));
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_
);
106 if (max_start_velocity
< 0)
107 max_start_velocity
= 0;
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
);
114 // Compute time-offset for start velocity.
115 time_offset_
= timeAtVelocity(max_start_velocity
, coefficients_
);
117 // Compute curve position at offset time
118 position_offset_
= position(time_offset_
, coefficients_
);
119 TRACE_EVENT_ASYNC_BEGIN1("input", "GestureAnimation", this, "curve",
123 TouchFlingGestureCurve::~TouchFlingGestureCurve() {
124 TRACE_EVENT_ASYNC_END0("input", "GestureAnimation", this);
127 bool TouchFlingGestureCurve::apply(double time
, WebGestureCurveTarget
* target
) {
133 } else if (time
+ time_offset_
< curve_duration_
) {
135 position(time
+ time_offset_
, coefficients_
) - position_offset_
;
136 speed
= velocity(time
+ time_offset_
, coefficients_
);
138 displacement
= position(curve_duration_
, coefficients_
) - position_offset_
;
142 // Keep track of integer portion of scroll thus far, and prepare increment.
143 WebFloatSize
scroll(displacement
* displacement_ratio_
.x
,
144 displacement
* displacement_ratio_
.y
);
145 WebFloatSize
scroll_increment(scroll
.width
- cumulative_scroll_
.width
,
146 scroll
.height
- cumulative_scroll_
.height
);
147 WebFloatSize
scroll_velocity(speed
* displacement_ratio_
.x
,
148 speed
* displacement_ratio_
.y
);
149 cumulative_scroll_
= scroll
;
151 if (time
+ time_offset_
< curve_duration_
||
152 scroll_increment
!= WebFloatSize()) {
153 target
->notifyCurrentFlingVelocity(scroll_velocity
);
154 // scrollBy() could delete this curve if the animation is over, so don't
155 // touch any member variables after making that call.
156 target
->scrollBy(scroll_increment
);
163 } // namespace webkit_glue