1 // Copyright 2013 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/renderer/input/input_handler_proxy.h"
7 #include "base/auto_reset.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/trace_event/trace_event.h"
12 #include "content/common/input/did_overscroll_params.h"
13 #include "content/common/input/web_input_event_traits.h"
14 #include "content/public/common/content_switches.h"
15 #include "content/renderer/input/input_handler_proxy_client.h"
16 #include "content/renderer/input/input_scroll_elasticity_controller.h"
17 #include "third_party/WebKit/public/platform/Platform.h"
18 #include "third_party/WebKit/public/web/WebInputEvent.h"
19 #include "ui/events/latency_info.h"
20 #include "ui/gfx/frame_time.h"
21 #include "ui/gfx/geometry/point_conversions.h"
23 using blink::WebFloatPoint
;
24 using blink::WebFloatSize
;
25 using blink::WebGestureEvent
;
26 using blink::WebInputEvent
;
27 using blink::WebMouseEvent
;
28 using blink::WebMouseWheelEvent
;
29 using blink::WebPoint
;
30 using blink::WebTouchEvent
;
31 using blink::WebTouchPoint
;
35 // Maximum time between a fling event's timestamp and the first |Animate| call
36 // for the fling curve to use the fling timestamp as the initial animation time.
37 // Two frames allows a minor delay between event creation and the first animate.
38 const double kMaxSecondsFromFlingTimestampToFirstAnimate
= 2. / 60.;
40 // Threshold for determining whether a fling scroll delta should have caused the
42 const float kScrollEpsilon
= 0.1f
;
44 // Minimum fling velocity required for the active fling and new fling for the
46 const double kMinBoostFlingSpeedSquare
= 350. * 350.;
48 // Minimum velocity for the active touch scroll to preserve (boost) an active
49 // fling for which cancellation has been deferred.
50 const double kMinBoostTouchScrollSpeedSquare
= 150 * 150.;
52 // Timeout window after which the active fling will be cancelled if no scrolls
53 // or flings of sufficient velocity relative to the current fling are received.
54 // The default value on Android native views is 40ms, but we use a slightly
55 // increased value to accomodate small IPC message delays.
56 const double kFlingBoostTimeoutDelaySeconds
= 0.045;
58 gfx::Vector2dF
ToClientScrollIncrement(const WebFloatSize
& increment
) {
59 return gfx::Vector2dF(-increment
.width
, -increment
.height
);
62 double InSecondsF(const base::TimeTicks
& time
) {
63 return (time
- base::TimeTicks()).InSecondsF();
66 bool ShouldSuppressScrollForFlingBoosting(
67 const gfx::Vector2dF
& current_fling_velocity
,
68 const WebGestureEvent
& scroll_update_event
,
69 double time_since_last_boost_event
) {
70 DCHECK_EQ(WebInputEvent::GestureScrollUpdate
, scroll_update_event
.type
);
72 gfx::Vector2dF
dx(scroll_update_event
.data
.scrollUpdate
.deltaX
,
73 scroll_update_event
.data
.scrollUpdate
.deltaY
);
74 if (gfx::DotProduct(current_fling_velocity
, dx
) <= 0)
77 if (time_since_last_boost_event
< 0.001)
80 // TODO(jdduke): Use |scroll_update_event.data.scrollUpdate.velocity{X,Y}|.
81 // The scroll must be of sufficient velocity to maintain the active fling.
82 const gfx::Vector2dF scroll_velocity
=
83 gfx::ScaleVector2d(dx
, 1. / time_since_last_boost_event
);
84 if (scroll_velocity
.LengthSquared() < kMinBoostTouchScrollSpeedSquare
)
90 bool ShouldBoostFling(const gfx::Vector2dF
& current_fling_velocity
,
91 const WebGestureEvent
& fling_start_event
) {
92 DCHECK_EQ(WebInputEvent::GestureFlingStart
, fling_start_event
.type
);
94 gfx::Vector2dF
new_fling_velocity(
95 fling_start_event
.data
.flingStart
.velocityX
,
96 fling_start_event
.data
.flingStart
.velocityY
);
98 if (gfx::DotProduct(current_fling_velocity
, new_fling_velocity
) <= 0)
101 if (current_fling_velocity
.LengthSquared() < kMinBoostFlingSpeedSquare
)
104 if (new_fling_velocity
.LengthSquared() < kMinBoostFlingSpeedSquare
)
110 WebGestureEvent
ObtainGestureScrollBegin(const WebGestureEvent
& event
) {
111 WebGestureEvent scroll_begin_event
= event
;
112 scroll_begin_event
.type
= WebInputEvent::GestureScrollBegin
;
113 scroll_begin_event
.data
.scrollBegin
.deltaXHint
= 0;
114 scroll_begin_event
.data
.scrollBegin
.deltaYHint
= 0;
115 return scroll_begin_event
;
118 void ReportInputEventLatencyUma(const WebInputEvent
& event
,
119 const ui::LatencyInfo
& latency_info
) {
120 if (!(event
.type
== WebInputEvent::GestureScrollBegin
||
121 event
.type
== WebInputEvent::GestureScrollUpdate
||
122 event
.type
== WebInputEvent::GesturePinchBegin
||
123 event
.type
== WebInputEvent::GesturePinchUpdate
||
124 event
.type
== WebInputEvent::GestureFlingStart
)) {
128 ui::LatencyInfo::LatencyMap::const_iterator it
=
129 latency_info
.latency_components
.find(std::make_pair(
130 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
, 0));
132 if (it
== latency_info
.latency_components
.end())
135 base::TimeDelta delta
= base::TimeTicks::Now() - it
->second
.event_time
;
136 for (size_t i
= 0; i
< it
->second
.event_count
; ++i
) {
137 switch (event
.type
) {
138 case blink::WebInputEvent::GestureScrollBegin
:
139 UMA_HISTOGRAM_CUSTOM_COUNTS(
140 "Event.Latency.RendererImpl.GestureScrollBegin",
141 delta
.InMicroseconds(), 1, 1000000, 100);
143 case blink::WebInputEvent::GestureScrollUpdate
:
144 UMA_HISTOGRAM_CUSTOM_COUNTS(
145 // So named for historical reasons.
146 "Event.Latency.RendererImpl.GestureScroll2",
147 delta
.InMicroseconds(), 1, 1000000, 100);
149 case blink::WebInputEvent::GesturePinchBegin
:
150 UMA_HISTOGRAM_CUSTOM_COUNTS(
151 "Event.Latency.RendererImpl.GesturePinchBegin",
152 delta
.InMicroseconds(), 1, 1000000, 100);
154 case blink::WebInputEvent::GesturePinchUpdate
:
155 UMA_HISTOGRAM_CUSTOM_COUNTS(
156 "Event.Latency.RendererImpl.GesturePinchUpdate",
157 delta
.InMicroseconds(), 1, 1000000, 100);
159 case blink::WebInputEvent::GestureFlingStart
:
160 UMA_HISTOGRAM_CUSTOM_COUNTS(
161 "Event.Latency.RendererImpl.GestureFlingStart",
162 delta
.InMicroseconds(), 1, 1000000, 100);
175 InputHandlerProxy::InputHandlerProxy(cc::InputHandler
* input_handler
,
176 InputHandlerProxyClient
* client
)
178 input_handler_(input_handler
),
179 deferred_fling_cancel_time_seconds_(0),
181 expect_scroll_update_end_(false),
183 gesture_scroll_on_impl_thread_(false),
184 gesture_pinch_on_impl_thread_(false),
185 fling_may_be_active_on_main_thread_(false),
186 disallow_horizontal_fling_scroll_(false),
187 disallow_vertical_fling_scroll_(false),
188 has_fling_animation_started_(false),
189 uma_latency_reporting_enabled_(base::TimeTicks::IsHighResolution()) {
191 input_handler_
->BindToClient(this);
192 smooth_scroll_enabled_
= base::CommandLine::ForCurrentProcess()->HasSwitch(
193 switches::kEnableSmoothScrolling
);
194 cc::ScrollElasticityHelper
* scroll_elasticity_helper
=
195 input_handler_
->CreateScrollElasticityHelper();
196 if (scroll_elasticity_helper
) {
197 scroll_elasticity_controller_
.reset(
198 new InputScrollElasticityController(scroll_elasticity_helper
));
202 InputHandlerProxy::~InputHandlerProxy() {}
204 void InputHandlerProxy::WillShutdown() {
205 scroll_elasticity_controller_
.reset();
206 input_handler_
= NULL
;
207 client_
->WillShutdown();
210 InputHandlerProxy::EventDisposition
211 InputHandlerProxy::HandleInputEventWithLatencyInfo(
212 const WebInputEvent
& event
,
213 ui::LatencyInfo
* latency_info
) {
214 DCHECK(input_handler_
);
216 if (uma_latency_reporting_enabled_
)
217 ReportInputEventLatencyUma(event
, *latency_info
);
219 TRACE_EVENT_FLOW_STEP0("input,benchmark",
221 TRACE_ID_DONT_MANGLE(latency_info
->trace_id
),
222 "HandleInputEventImpl");
224 scoped_ptr
<cc::SwapPromiseMonitor
> latency_info_swap_promise_monitor
=
225 input_handler_
->CreateLatencyInfoSwapPromiseMonitor(latency_info
);
226 InputHandlerProxy::EventDisposition disposition
= HandleInputEvent(event
);
230 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleInputEvent(
231 const WebInputEvent
& event
) {
232 DCHECK(input_handler_
);
233 TRACE_EVENT1("input,benchmark", "InputHandlerProxy::HandleInputEvent",
234 "type", WebInputEventTraits::GetName(event
.type
));
236 client_
->DidReceiveInputEvent(event
.type
);
237 if (FilterInputEventForFlingBoosting(event
))
240 switch (event
.type
) {
241 case WebInputEvent::MouseWheel
:
242 return HandleMouseWheel(static_cast<const WebMouseWheelEvent
&>(event
));
244 case WebInputEvent::GestureScrollBegin
:
245 return HandleGestureScrollBegin(
246 static_cast<const WebGestureEvent
&>(event
));
248 case WebInputEvent::GestureScrollUpdate
:
249 return HandleGestureScrollUpdate(
250 static_cast<const WebGestureEvent
&>(event
));
252 case WebInputEvent::GestureScrollEnd
:
253 return HandleGestureScrollEnd(static_cast<const WebGestureEvent
&>(event
));
255 case WebInputEvent::GesturePinchBegin
: {
256 DCHECK(!gesture_pinch_on_impl_thread_
);
257 const WebGestureEvent
& gesture_event
=
258 static_cast<const WebGestureEvent
&>(event
);
259 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
&&
260 input_handler_
->HaveWheelEventHandlersAt(
261 gfx::Point(gesture_event
.x
, gesture_event
.y
))) {
262 return DID_NOT_HANDLE
;
264 input_handler_
->PinchGestureBegin();
265 gesture_pinch_on_impl_thread_
= true;
270 case WebInputEvent::GesturePinchEnd
:
271 if (gesture_pinch_on_impl_thread_
) {
272 gesture_pinch_on_impl_thread_
= false;
273 input_handler_
->PinchGestureEnd();
276 return DID_NOT_HANDLE
;
279 case WebInputEvent::GesturePinchUpdate
: {
280 if (gesture_pinch_on_impl_thread_
) {
281 const WebGestureEvent
& gesture_event
=
282 static_cast<const WebGestureEvent
&>(event
);
283 input_handler_
->PinchGestureUpdate(
284 gesture_event
.data
.pinchUpdate
.scale
,
285 gfx::Point(gesture_event
.x
, gesture_event
.y
));
288 return DID_NOT_HANDLE
;
292 case WebInputEvent::GestureFlingStart
:
293 return HandleGestureFlingStart(
294 *static_cast<const WebGestureEvent
*>(&event
));
296 case WebInputEvent::GestureFlingCancel
:
297 if (CancelCurrentFling())
299 else if (!fling_may_be_active_on_main_thread_
)
301 return DID_NOT_HANDLE
;
303 case WebInputEvent::TouchStart
:
304 return HandleTouchStart(static_cast<const WebTouchEvent
&>(event
));
306 case WebInputEvent::MouseMove
: {
307 const WebMouseEvent
& mouse_event
=
308 static_cast<const WebMouseEvent
&>(event
);
309 // TODO(tony): Ignore when mouse buttons are down?
310 // TODO(davemoore): This should never happen, but bug #326635 showed some
311 // surprising crashes.
312 CHECK(input_handler_
);
313 input_handler_
->MouseMoveAt(gfx::Point(mouse_event
.x
, mouse_event
.y
));
314 return DID_NOT_HANDLE
;
318 if (WebInputEvent::isKeyboardEventType(event
.type
)) {
319 // Only call |CancelCurrentFling()| if a fling was active, as it will
320 // otherwise disrupt an in-progress touch scroll.
322 CancelCurrentFling();
327 return DID_NOT_HANDLE
;
330 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleMouseWheel(
331 const WebMouseWheelEvent
& wheel_event
) {
332 InputHandlerProxy::EventDisposition result
= DID_NOT_HANDLE
;
333 cc::InputHandlerScrollResult scroll_result
;
335 if (wheel_event
.scrollByPage
) {
336 // TODO(jamesr): We don't properly handle scroll by page in the compositor
337 // thread, so punt it to the main thread. http://crbug.com/236639
338 result
= DID_NOT_HANDLE
;
339 } else if (!wheel_event
.canScroll
) {
340 // Wheel events with |canScroll| == false will not trigger scrolling,
341 // only event handlers. Forward to the main thread.
342 result
= DID_NOT_HANDLE
;
343 } else if (smooth_scroll_enabled_
) {
344 cc::InputHandler::ScrollStatus scroll_status
=
345 input_handler_
->ScrollAnimated(
346 gfx::Point(wheel_event
.x
, wheel_event
.y
),
347 gfx::Vector2dF(-wheel_event
.deltaX
, -wheel_event
.deltaY
));
348 switch (scroll_status
) {
349 case cc::InputHandler::ScrollStarted
:
352 case cc::InputHandler::ScrollIgnored
:
355 result
= DID_NOT_HANDLE
;
359 cc::InputHandler::ScrollStatus scroll_status
= input_handler_
->ScrollBegin(
360 gfx::Point(wheel_event
.x
, wheel_event
.y
), cc::InputHandler::Wheel
);
361 switch (scroll_status
) {
362 case cc::InputHandler::ScrollStarted
: {
363 TRACE_EVENT_INSTANT2(
364 "input", "InputHandlerProxy::handle_input wheel scroll",
365 TRACE_EVENT_SCOPE_THREAD
, "deltaX", -wheel_event
.deltaX
, "deltaY",
366 -wheel_event
.deltaY
);
367 gfx::Point
scroll_point(wheel_event
.x
, wheel_event
.y
);
368 gfx::Vector2dF
scroll_delta(-wheel_event
.deltaX
, -wheel_event
.deltaY
);
369 scroll_result
= input_handler_
->ScrollBy(scroll_point
, scroll_delta
);
370 HandleOverscroll(scroll_point
, scroll_result
);
371 input_handler_
->ScrollEnd();
372 result
= scroll_result
.did_scroll
? DID_HANDLE
: DROP_EVENT
;
375 case cc::InputHandler::ScrollIgnored
:
376 // TODO(jamesr): This should be DROP_EVENT, but in cases where we fail
377 // to properly sync scrollability it's safer to send the event to the
378 // main thread. Change back to DROP_EVENT once we have synchronization
380 result
= DID_NOT_HANDLE
;
382 case cc::InputHandler::ScrollUnknown
:
383 case cc::InputHandler::ScrollOnMainThread
:
384 result
= DID_NOT_HANDLE
;
386 case cc::InputHandler::ScrollStatusCount
:
392 // Send the event and its disposition to the elasticity controller to update
393 // the over-scroll animation. If the event is to be handled on the main
394 // thread, the event and its disposition will be sent to the elasticity
395 // controller after being handled on the main thread.
396 if (scroll_elasticity_controller_
&& result
!= DID_NOT_HANDLE
) {
397 // Note that the call to the elasticity controller is made asynchronously,
398 // to minimize divergence between main thread and impl thread event
400 base::MessageLoop::current()->PostTask(
402 base::Bind(&InputScrollElasticityController::ObserveWheelEventAndResult
,
403 scroll_elasticity_controller_
->GetWeakPtr(), wheel_event
,
409 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleGestureScrollBegin(
410 const WebGestureEvent
& gesture_event
) {
411 DCHECK(!gesture_scroll_on_impl_thread_
);
413 DCHECK(!expect_scroll_update_end_
);
414 expect_scroll_update_end_
= true;
416 cc::InputHandler::ScrollStatus scroll_status
= input_handler_
->ScrollBegin(
417 gfx::Point(gesture_event
.x
, gesture_event
.y
),
418 cc::InputHandler::Gesture
);
419 UMA_HISTOGRAM_ENUMERATION("Renderer4.CompositorScrollHitTestResult",
421 cc::InputHandler::ScrollStatusCount
);
422 switch (scroll_status
) {
423 case cc::InputHandler::ScrollStarted
:
424 TRACE_EVENT_INSTANT0("input",
425 "InputHandlerProxy::handle_input gesture scroll",
426 TRACE_EVENT_SCOPE_THREAD
);
427 gesture_scroll_on_impl_thread_
= true;
429 case cc::InputHandler::ScrollUnknown
:
430 case cc::InputHandler::ScrollOnMainThread
:
431 return DID_NOT_HANDLE
;
432 case cc::InputHandler::ScrollIgnored
:
434 case cc::InputHandler::ScrollStatusCount
:
438 return DID_NOT_HANDLE
;
441 InputHandlerProxy::EventDisposition
442 InputHandlerProxy::HandleGestureScrollUpdate(
443 const WebGestureEvent
& gesture_event
) {
445 DCHECK(expect_scroll_update_end_
);
448 if (!gesture_scroll_on_impl_thread_
&& !gesture_pinch_on_impl_thread_
)
449 return DID_NOT_HANDLE
;
451 gfx::Point
scroll_point(gesture_event
.x
, gesture_event
.y
);
452 gfx::Vector2dF
scroll_delta(-gesture_event
.data
.scrollUpdate
.deltaX
,
453 -gesture_event
.data
.scrollUpdate
.deltaY
);
454 cc::InputHandlerScrollResult scroll_result
= input_handler_
->ScrollBy(
455 scroll_point
, scroll_delta
);
456 HandleOverscroll(scroll_point
, scroll_result
);
457 return scroll_result
.did_scroll
? DID_HANDLE
: DROP_EVENT
;
460 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleGestureScrollEnd(
461 const WebGestureEvent
& gesture_event
) {
463 DCHECK(expect_scroll_update_end_
);
464 expect_scroll_update_end_
= false;
466 input_handler_
->ScrollEnd();
467 if (!gesture_scroll_on_impl_thread_
)
468 return DID_NOT_HANDLE
;
469 gesture_scroll_on_impl_thread_
= false;
473 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleGestureFlingStart(
474 const WebGestureEvent
& gesture_event
) {
475 cc::InputHandler::ScrollStatus scroll_status
;
477 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
) {
478 scroll_status
= input_handler_
->ScrollBegin(
479 gfx::Point(gesture_event
.x
, gesture_event
.y
),
480 cc::InputHandler::NonBubblingGesture
);
482 if (!gesture_scroll_on_impl_thread_
)
483 scroll_status
= cc::InputHandler::ScrollOnMainThread
;
485 scroll_status
= input_handler_
->FlingScrollBegin();
489 expect_scroll_update_end_
= false;
492 switch (scroll_status
) {
493 case cc::InputHandler::ScrollStarted
: {
494 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
)
495 input_handler_
->ScrollEnd();
497 const float vx
= gesture_event
.data
.flingStart
.velocityX
;
498 const float vy
= gesture_event
.data
.flingStart
.velocityY
;
499 current_fling_velocity_
= gfx::Vector2dF(vx
, vy
);
500 DCHECK(!current_fling_velocity_
.IsZero());
501 fling_curve_
.reset(client_
->CreateFlingAnimationCurve(
502 gesture_event
.sourceDevice
,
503 WebFloatPoint(vx
, vy
),
505 disallow_horizontal_fling_scroll_
= !vx
;
506 disallow_vertical_fling_scroll_
= !vy
;
507 TRACE_EVENT_ASYNC_BEGIN2("input",
508 "InputHandlerProxy::HandleGestureFling::started",
514 // Note that the timestamp will only be used to kickstart the animation if
515 // its sufficiently close to the timestamp of the first call |Animate()|.
516 has_fling_animation_started_
= false;
517 fling_parameters_
.startTime
= gesture_event
.timeStampSeconds
;
518 fling_parameters_
.delta
= WebFloatPoint(vx
, vy
);
519 fling_parameters_
.point
= WebPoint(gesture_event
.x
, gesture_event
.y
);
520 fling_parameters_
.globalPoint
=
521 WebPoint(gesture_event
.globalX
, gesture_event
.globalY
);
522 fling_parameters_
.modifiers
= gesture_event
.modifiers
;
523 fling_parameters_
.sourceDevice
= gesture_event
.sourceDevice
;
524 input_handler_
->SetNeedsAnimate();
527 case cc::InputHandler::ScrollUnknown
:
528 case cc::InputHandler::ScrollOnMainThread
: {
529 TRACE_EVENT_INSTANT0("input",
530 "InputHandlerProxy::HandleGestureFling::"
531 "scroll_on_main_thread",
532 TRACE_EVENT_SCOPE_THREAD
);
533 fling_may_be_active_on_main_thread_
= true;
534 return DID_NOT_HANDLE
;
536 case cc::InputHandler::ScrollIgnored
: {
537 TRACE_EVENT_INSTANT0(
539 "InputHandlerProxy::HandleGestureFling::ignored",
540 TRACE_EVENT_SCOPE_THREAD
);
541 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
) {
542 // We still pass the curve to the main thread if there's nothing
543 // scrollable, in case something
544 // registers a handler before the curve is over.
545 return DID_NOT_HANDLE
;
549 case cc::InputHandler::ScrollStatusCount
:
553 return DID_NOT_HANDLE
;
556 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleTouchStart(
557 const blink::WebTouchEvent
& touch_event
) {
558 for (size_t i
= 0; i
< touch_event
.touchesLength
; ++i
) {
559 if (touch_event
.touches
[i
].state
!= WebTouchPoint::StatePressed
)
561 if (input_handler_
->HaveTouchEventHandlersAt(
562 gfx::Point(touch_event
.touches
[i
].position
.x
,
563 touch_event
.touches
[i
].position
.y
))) {
564 return DID_NOT_HANDLE
;
570 bool InputHandlerProxy::FilterInputEventForFlingBoosting(
571 const WebInputEvent
& event
) {
572 if (!WebInputEvent::isGestureEventType(event
.type
))
576 DCHECK(!deferred_fling_cancel_time_seconds_
);
580 const WebGestureEvent
& gesture_event
=
581 static_cast<const WebGestureEvent
&>(event
);
582 if (gesture_event
.type
== WebInputEvent::GestureFlingCancel
) {
583 if (gesture_event
.data
.flingCancel
.preventBoosting
)
586 if (current_fling_velocity_
.LengthSquared() < kMinBoostFlingSpeedSquare
)
589 TRACE_EVENT_INSTANT0("input",
590 "InputHandlerProxy::FlingBoostStart",
591 TRACE_EVENT_SCOPE_THREAD
);
592 deferred_fling_cancel_time_seconds_
=
593 event
.timeStampSeconds
+ kFlingBoostTimeoutDelaySeconds
;
597 // A fling is either inactive or is "free spinning", i.e., has yet to be
598 // interrupted by a touch gesture, in which case there is nothing to filter.
599 if (!deferred_fling_cancel_time_seconds_
)
602 // Gestures from a different source should immediately interrupt the fling.
603 if (gesture_event
.sourceDevice
!= fling_parameters_
.sourceDevice
) {
604 CancelCurrentFling();
608 switch (gesture_event
.type
) {
609 case WebInputEvent::GestureTapCancel
:
610 case WebInputEvent::GestureTapDown
:
613 case WebInputEvent::GestureScrollBegin
:
614 if (!input_handler_
->IsCurrentlyScrollingLayerAt(
615 gfx::Point(gesture_event
.x
, gesture_event
.y
),
616 fling_parameters_
.sourceDevice
== blink::WebGestureDeviceTouchpad
617 ? cc::InputHandler::NonBubblingGesture
618 : cc::InputHandler::Gesture
)) {
619 CancelCurrentFling();
623 // TODO(jdduke): Use |gesture_event.data.scrollBegin.delta{X,Y}Hint| to
624 // determine if the ScrollBegin should immediately cancel the fling.
625 ExtendBoostedFlingTimeout(gesture_event
);
628 case WebInputEvent::GestureScrollUpdate
: {
629 const double time_since_last_boost_event
=
630 event
.timeStampSeconds
- last_fling_boost_event_
.timeStampSeconds
;
631 if (ShouldSuppressScrollForFlingBoosting(current_fling_velocity_
,
633 time_since_last_boost_event
)) {
634 ExtendBoostedFlingTimeout(gesture_event
);
638 CancelCurrentFling();
642 case WebInputEvent::GestureScrollEnd
:
643 // Clear the last fling boost event *prior* to fling cancellation,
644 // preventing insertion of a synthetic GestureScrollBegin.
645 last_fling_boost_event_
= WebGestureEvent();
646 CancelCurrentFling();
649 case WebInputEvent::GestureFlingStart
: {
650 DCHECK_EQ(fling_parameters_
.sourceDevice
, gesture_event
.sourceDevice
);
653 fling_parameters_
.modifiers
== gesture_event
.modifiers
&&
654 ShouldBoostFling(current_fling_velocity_
, gesture_event
);
656 gfx::Vector2dF
new_fling_velocity(
657 gesture_event
.data
.flingStart
.velocityX
,
658 gesture_event
.data
.flingStart
.velocityY
);
659 DCHECK(!new_fling_velocity
.IsZero());
662 current_fling_velocity_
+= new_fling_velocity
;
664 current_fling_velocity_
= new_fling_velocity
;
666 WebFloatPoint
velocity(current_fling_velocity_
.x(),
667 current_fling_velocity_
.y());
668 deferred_fling_cancel_time_seconds_
= 0;
669 disallow_horizontal_fling_scroll_
= !velocity
.x
;
670 disallow_vertical_fling_scroll_
= !velocity
.y
;
671 last_fling_boost_event_
= WebGestureEvent();
672 fling_curve_
.reset(client_
->CreateFlingAnimationCurve(
673 gesture_event
.sourceDevice
,
676 fling_parameters_
.startTime
= gesture_event
.timeStampSeconds
;
677 fling_parameters_
.delta
= velocity
;
678 fling_parameters_
.point
= WebPoint(gesture_event
.x
, gesture_event
.y
);
679 fling_parameters_
.globalPoint
=
680 WebPoint(gesture_event
.globalX
, gesture_event
.globalY
);
682 TRACE_EVENT_INSTANT2("input",
683 fling_boosted
? "InputHandlerProxy::FlingBoosted"
684 : "InputHandlerProxy::FlingReplaced",
685 TRACE_EVENT_SCOPE_THREAD
,
687 current_fling_velocity_
.x(),
689 current_fling_velocity_
.y());
691 // The client expects balanced calls between a consumed GestureFlingStart
692 // and |DidStopFlinging()|. TODO(jdduke): Provide a count parameter to
693 // |DidStopFlinging()| and only send after the accumulated fling ends.
694 client_
->DidStopFlinging();
699 // All other types of gestures (taps, presses, etc...) will complete the
700 // deferred fling cancellation.
701 CancelCurrentFling();
706 void InputHandlerProxy::ExtendBoostedFlingTimeout(
707 const blink::WebGestureEvent
& event
) {
708 TRACE_EVENT_INSTANT0("input",
709 "InputHandlerProxy::ExtendBoostedFlingTimeout",
710 TRACE_EVENT_SCOPE_THREAD
);
711 deferred_fling_cancel_time_seconds_
=
712 event
.timeStampSeconds
+ kFlingBoostTimeoutDelaySeconds
;
713 last_fling_boost_event_
= event
;
716 void InputHandlerProxy::Animate(base::TimeTicks time
) {
717 if (scroll_elasticity_controller_
)
718 scroll_elasticity_controller_
->Animate(time
);
723 double monotonic_time_sec
= InSecondsF(time
);
725 if (deferred_fling_cancel_time_seconds_
&&
726 monotonic_time_sec
> deferred_fling_cancel_time_seconds_
) {
727 CancelCurrentFling();
731 client_
->DidAnimateForInput();
733 if (!has_fling_animation_started_
) {
734 has_fling_animation_started_
= true;
735 // Guard against invalid, future or sufficiently stale start times, as there
736 // are no guarantees fling event and animation timestamps are compatible.
737 if (!fling_parameters_
.startTime
||
738 monotonic_time_sec
<= fling_parameters_
.startTime
||
739 monotonic_time_sec
>= fling_parameters_
.startTime
+
740 kMaxSecondsFromFlingTimestampToFirstAnimate
) {
741 fling_parameters_
.startTime
= monotonic_time_sec
;
742 input_handler_
->SetNeedsAnimate();
747 bool fling_is_active
=
748 fling_curve_
->apply(monotonic_time_sec
- fling_parameters_
.startTime
,
751 if (disallow_vertical_fling_scroll_
&& disallow_horizontal_fling_scroll_
)
752 fling_is_active
= false;
754 if (fling_is_active
) {
755 input_handler_
->SetNeedsAnimate();
757 TRACE_EVENT_INSTANT0("input",
758 "InputHandlerProxy::animate::flingOver",
759 TRACE_EVENT_SCOPE_THREAD
);
760 CancelCurrentFling();
764 void InputHandlerProxy::MainThreadHasStoppedFlinging() {
765 fling_may_be_active_on_main_thread_
= false;
766 client_
->DidStopFlinging();
769 void InputHandlerProxy::ReconcileElasticOverscrollAndRootScroll() {
770 if (scroll_elasticity_controller_
)
771 scroll_elasticity_controller_
->ReconcileStretchAndScroll();
774 void InputHandlerProxy::HandleOverscroll(
775 const gfx::Point
& causal_event_viewport_point
,
776 const cc::InputHandlerScrollResult
& scroll_result
) {
778 if (!scroll_result
.did_overscroll_root
)
781 TRACE_EVENT2("input",
782 "InputHandlerProxy::DidOverscroll",
784 scroll_result
.unused_scroll_delta
.x(),
786 scroll_result
.unused_scroll_delta
.y());
788 DidOverscrollParams params
;
789 params
.accumulated_overscroll
= scroll_result
.accumulated_root_overscroll
;
790 params
.latest_overscroll_delta
= scroll_result
.unused_scroll_delta
;
791 params
.current_fling_velocity
=
792 ToClientScrollIncrement(current_fling_velocity_
);
793 params
.causal_event_viewport_point
= causal_event_viewport_point
;
796 static const int kFlingOverscrollThreshold
= 1;
797 disallow_horizontal_fling_scroll_
|=
798 std::abs(params
.accumulated_overscroll
.x()) >=
799 kFlingOverscrollThreshold
;
800 disallow_vertical_fling_scroll_
|=
801 std::abs(params
.accumulated_overscroll
.y()) >=
802 kFlingOverscrollThreshold
;
805 client_
->DidOverscroll(params
);
808 bool InputHandlerProxy::CancelCurrentFling() {
809 if (CancelCurrentFlingWithoutNotifyingClient()) {
810 client_
->DidStopFlinging();
816 bool InputHandlerProxy::CancelCurrentFlingWithoutNotifyingClient() {
817 bool had_fling_animation
= fling_curve_
;
818 if (had_fling_animation
&&
819 fling_parameters_
.sourceDevice
== blink::WebGestureDeviceTouchscreen
) {
820 input_handler_
->ScrollEnd();
821 TRACE_EVENT_ASYNC_END0(
823 "InputHandlerProxy::HandleGestureFling::started",
827 TRACE_EVENT_INSTANT1("input",
828 "InputHandlerProxy::CancelCurrentFling",
829 TRACE_EVENT_SCOPE_THREAD
,
830 "had_fling_animation",
831 had_fling_animation
);
832 fling_curve_
.reset();
833 has_fling_animation_started_
= false;
834 gesture_scroll_on_impl_thread_
= false;
835 current_fling_velocity_
= gfx::Vector2dF();
836 fling_parameters_
= blink::WebActiveWheelFlingParameters();
838 if (deferred_fling_cancel_time_seconds_
) {
839 deferred_fling_cancel_time_seconds_
= 0;
841 WebGestureEvent last_fling_boost_event
= last_fling_boost_event_
;
842 last_fling_boost_event_
= WebGestureEvent();
843 if (last_fling_boost_event
.type
== WebInputEvent::GestureScrollBegin
||
844 last_fling_boost_event
.type
== WebInputEvent::GestureScrollUpdate
) {
845 // Synthesize a GestureScrollBegin, as the original was suppressed.
846 HandleInputEvent(ObtainGestureScrollBegin(last_fling_boost_event
));
850 return had_fling_animation
;
853 bool InputHandlerProxy::TouchpadFlingScroll(
854 const WebFloatSize
& increment
) {
855 WebMouseWheelEvent synthetic_wheel
;
856 synthetic_wheel
.type
= WebInputEvent::MouseWheel
;
857 synthetic_wheel
.deltaX
= increment
.width
;
858 synthetic_wheel
.deltaY
= increment
.height
;
859 synthetic_wheel
.hasPreciseScrollingDeltas
= true;
860 synthetic_wheel
.x
= fling_parameters_
.point
.x
;
861 synthetic_wheel
.y
= fling_parameters_
.point
.y
;
862 synthetic_wheel
.globalX
= fling_parameters_
.globalPoint
.x
;
863 synthetic_wheel
.globalY
= fling_parameters_
.globalPoint
.y
;
864 synthetic_wheel
.modifiers
= fling_parameters_
.modifiers
;
866 InputHandlerProxy::EventDisposition disposition
=
867 HandleInputEvent(synthetic_wheel
);
868 switch (disposition
) {
874 TRACE_EVENT_INSTANT0("input",
875 "InputHandlerProxy::scrollBy::AbortFling",
876 TRACE_EVENT_SCOPE_THREAD
);
877 // If we got a DID_NOT_HANDLE, that means we need to deliver wheels on the
878 // main thread. In this case we need to schedule a commit and transfer the
879 // fling curve over to the main thread and run the rest of the wheels from
880 // there. This can happen when flinging a page that contains a scrollable
881 // subarea that we can't scroll on the thread if the fling starts outside
882 // the subarea but then is flung "under" the pointer.
883 client_
->TransferActiveWheelFlingAnimation(fling_parameters_
);
884 fling_may_be_active_on_main_thread_
= true;
885 CancelCurrentFlingWithoutNotifyingClient();
892 bool InputHandlerProxy::scrollBy(const WebFloatSize
& increment
,
893 const WebFloatSize
& velocity
) {
894 WebFloatSize clipped_increment
;
895 WebFloatSize clipped_velocity
;
896 if (!disallow_horizontal_fling_scroll_
) {
897 clipped_increment
.width
= increment
.width
;
898 clipped_velocity
.width
= velocity
.width
;
900 if (!disallow_vertical_fling_scroll_
) {
901 clipped_increment
.height
= increment
.height
;
902 clipped_velocity
.height
= velocity
.height
;
905 current_fling_velocity_
= clipped_velocity
;
907 // Early out if the increment is zero, but avoid early terimination if the
908 // velocity is still non-zero.
909 if (clipped_increment
== WebFloatSize())
910 return clipped_velocity
!= WebFloatSize();
912 TRACE_EVENT2("input",
913 "InputHandlerProxy::scrollBy",
915 clipped_increment
.width
,
917 clipped_increment
.height
);
919 bool did_scroll
= false;
921 switch (fling_parameters_
.sourceDevice
) {
922 case blink::WebGestureDeviceTouchpad
:
923 did_scroll
= TouchpadFlingScroll(clipped_increment
);
925 case blink::WebGestureDeviceTouchscreen
: {
926 clipped_increment
= ToClientScrollIncrement(clipped_increment
);
927 cc::InputHandlerScrollResult scroll_result
= input_handler_
->ScrollBy(
928 fling_parameters_
.point
, clipped_increment
);
929 HandleOverscroll(fling_parameters_
.point
, scroll_result
);
930 did_scroll
= scroll_result
.did_scroll
;
935 fling_parameters_
.cumulativeScroll
.width
+= clipped_increment
.width
;
936 fling_parameters_
.cumulativeScroll
.height
+= clipped_increment
.height
;
939 // It's possible the provided |increment| is sufficiently small as to not
940 // trigger a scroll, e.g., with a trivial time delta between fling updates.
941 // Return true in this case to prevent early fling termination.
942 if (std::abs(clipped_increment
.width
) < kScrollEpsilon
&&
943 std::abs(clipped_increment
.height
) < kScrollEpsilon
)
949 } // namespace content