Revert of Class for allocating a chunk of memory for RenderPass (patchset #31 id...
[chromium-blink-merge.git] / content / child / touch_fling_gesture_curve.cc
blobcc94bd6212838ff01b1c5c91a214233a4a331f9b
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.
5 #include "content/child/touch_fling_gesture_curve.h"
7 #include <cmath>
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;
21 using blink::WebSize;
23 namespace {
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) {
36 DCHECK(p[0]);
37 DCHECK(p[2]);
38 return -log((v + p[1]) / (-p[0] * p[2])) / p[2];
41 } // namespace
44 namespace content {
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,
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);
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());
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",
120 kCurveName);
123 TouchFlingGestureCurve::~TouchFlingGestureCurve() {
124 TRACE_EVENT_ASYNC_END0("input", "GestureAnimation", this);
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;
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;
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;
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);
160 return false;
163 } // namespace content