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. 4 5 #include "webkit/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 WebKit::WebFloatPoint; 18 using WebKit::WebFloatSize; 19 using WebKit::WebGestureCurve; 20 using WebKit::WebGestureCurveTarget; 21 using WebKit::WebSize; 22 23 namespace { 24 25 const char* kCurveName = "TouchFlingGestureCurve"; 26 27 inline double position(double t, float* p) { 28 return p[0] * exp(-p[2] * t) - p[1] * t - p[0]; 29 } 30 31 inline double velocity(double t, float* p) { 32 return -p[0] * p[2] * exp(-p[2] * t) - p[1]; 33 } 34 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 webkit_glue { 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 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 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 123 TouchFlingGestureCurve::~TouchFlingGestureCurve() { 124 TRACE_EVENT_ASYNC_END0("input", "GestureAnimation", this); 125 } 126 127 bool TouchFlingGestureCurve::apply(double time, WebGestureCurveTarget* target) { 128 float displacement; 129 float speed; 130 if (time < 0) { 131 displacement = 0.f; 132 speed = 0.f; 133 } else if (time + time_offset_ < curve_duration_) { 134 displacement = 135 position(time + time_offset_, coefficients_) - position_offset_; 136 speed = velocity(time + time_offset_, coefficients_); 137 } else { 138 displacement = position(curve_duration_, coefficients_) - position_offset_; 139 speed = 0.f; 140 } 141 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; 150 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); 157 return true; 158 } 159 160 return false; 161 } 162 163 } // namespace webkit_glue 164