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/location.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/trace_event/trace_event.h"
15 #include "content/common/input/did_overscroll_params.h"
16 #include "content/common/input/web_input_event_traits.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/renderer/input/input_handler_proxy_client.h"
19 #include "content/renderer/input/input_scroll_elasticity_controller.h"
20 #include "third_party/WebKit/public/platform/Platform.h"
21 #include "third_party/WebKit/public/web/WebInputEvent.h"
22 #include "ui/events/latency_info.h"
23 #include "ui/gfx/geometry/point_conversions.h"
25 using blink::WebFloatPoint
;
26 using blink::WebFloatSize
;
27 using blink::WebGestureEvent
;
28 using blink::WebInputEvent
;
29 using blink::WebMouseEvent
;
30 using blink::WebMouseWheelEvent
;
31 using blink::WebPoint
;
32 using blink::WebTouchEvent
;
33 using blink::WebTouchPoint
;
37 // Maximum time between a fling event's timestamp and the first |Animate| call
38 // for the fling curve to use the fling timestamp as the initial animation time.
39 // Two frames allows a minor delay between event creation and the first animate.
40 const double kMaxSecondsFromFlingTimestampToFirstAnimate
= 2. / 60.;
42 // Threshold for determining whether a fling scroll delta should have caused the
44 const float kScrollEpsilon
= 0.1f
;
46 // Minimum fling velocity required for the active fling and new fling for the
48 const double kMinBoostFlingSpeedSquare
= 350. * 350.;
50 // Minimum velocity for the active touch scroll to preserve (boost) an active
51 // fling for which cancellation has been deferred.
52 const double kMinBoostTouchScrollSpeedSquare
= 150 * 150.;
54 // Timeout window after which the active fling will be cancelled if no scrolls
55 // or flings of sufficient velocity relative to the current fling are received.
56 // The default value on Android native views is 40ms, but we use a slightly
57 // increased value to accomodate small IPC message delays.
58 const double kFlingBoostTimeoutDelaySeconds
= 0.045;
60 gfx::Vector2dF
ToClientScrollIncrement(const WebFloatSize
& increment
) {
61 return gfx::Vector2dF(-increment
.width
, -increment
.height
);
64 double InSecondsF(const base::TimeTicks
& time
) {
65 return (time
- base::TimeTicks()).InSecondsF();
68 bool ShouldSuppressScrollForFlingBoosting(
69 const gfx::Vector2dF
& current_fling_velocity
,
70 const WebGestureEvent
& scroll_update_event
,
71 double time_since_last_boost_event
) {
72 DCHECK_EQ(WebInputEvent::GestureScrollUpdate
, scroll_update_event
.type
);
74 gfx::Vector2dF
dx(scroll_update_event
.data
.scrollUpdate
.deltaX
,
75 scroll_update_event
.data
.scrollUpdate
.deltaY
);
76 if (gfx::DotProduct(current_fling_velocity
, dx
) <= 0)
79 if (time_since_last_boost_event
< 0.001)
82 // TODO(jdduke): Use |scroll_update_event.data.scrollUpdate.velocity{X,Y}|.
83 // The scroll must be of sufficient velocity to maintain the active fling.
84 const gfx::Vector2dF scroll_velocity
=
85 gfx::ScaleVector2d(dx
, 1. / time_since_last_boost_event
);
86 if (scroll_velocity
.LengthSquared() < kMinBoostTouchScrollSpeedSquare
)
92 bool ShouldBoostFling(const gfx::Vector2dF
& current_fling_velocity
,
93 const WebGestureEvent
& fling_start_event
) {
94 DCHECK_EQ(WebInputEvent::GestureFlingStart
, fling_start_event
.type
);
96 gfx::Vector2dF
new_fling_velocity(
97 fling_start_event
.data
.flingStart
.velocityX
,
98 fling_start_event
.data
.flingStart
.velocityY
);
100 if (gfx::DotProduct(current_fling_velocity
, new_fling_velocity
) <= 0)
103 if (current_fling_velocity
.LengthSquared() < kMinBoostFlingSpeedSquare
)
106 if (new_fling_velocity
.LengthSquared() < kMinBoostFlingSpeedSquare
)
112 WebGestureEvent
ObtainGestureScrollBegin(const WebGestureEvent
& event
) {
113 WebGestureEvent scroll_begin_event
= event
;
114 scroll_begin_event
.type
= WebInputEvent::GestureScrollBegin
;
115 scroll_begin_event
.data
.scrollBegin
.deltaXHint
= 0;
116 scroll_begin_event
.data
.scrollBegin
.deltaYHint
= 0;
117 return scroll_begin_event
;
120 void ReportInputEventLatencyUma(const WebInputEvent
& event
,
121 const ui::LatencyInfo
& latency_info
) {
122 if (!(event
.type
== WebInputEvent::GestureScrollBegin
||
123 event
.type
== WebInputEvent::GestureScrollUpdate
||
124 event
.type
== WebInputEvent::GesturePinchBegin
||
125 event
.type
== WebInputEvent::GesturePinchUpdate
||
126 event
.type
== WebInputEvent::GestureFlingStart
)) {
130 ui::LatencyInfo::LatencyMap::const_iterator it
=
131 latency_info
.latency_components
.find(std::make_pair(
132 ui::INPUT_EVENT_LATENCY_ORIGINAL_COMPONENT
, 0));
134 if (it
== latency_info
.latency_components
.end())
137 base::TimeDelta delta
= base::TimeTicks::Now() - it
->second
.event_time
;
138 for (size_t i
= 0; i
< it
->second
.event_count
; ++i
) {
139 switch (event
.type
) {
140 case blink::WebInputEvent::GestureScrollBegin
:
141 UMA_HISTOGRAM_CUSTOM_COUNTS(
142 "Event.Latency.RendererImpl.GestureScrollBegin",
143 delta
.InMicroseconds(), 1, 1000000, 100);
145 case blink::WebInputEvent::GestureScrollUpdate
:
146 UMA_HISTOGRAM_CUSTOM_COUNTS(
147 // So named for historical reasons.
148 "Event.Latency.RendererImpl.GestureScroll2",
149 delta
.InMicroseconds(), 1, 1000000, 100);
151 case blink::WebInputEvent::GesturePinchBegin
:
152 UMA_HISTOGRAM_CUSTOM_COUNTS(
153 "Event.Latency.RendererImpl.GesturePinchBegin",
154 delta
.InMicroseconds(), 1, 1000000, 100);
156 case blink::WebInputEvent::GesturePinchUpdate
:
157 UMA_HISTOGRAM_CUSTOM_COUNTS(
158 "Event.Latency.RendererImpl.GesturePinchUpdate",
159 delta
.InMicroseconds(), 1, 1000000, 100);
161 case blink::WebInputEvent::GestureFlingStart
:
162 UMA_HISTOGRAM_CUSTOM_COUNTS(
163 "Event.Latency.RendererImpl.GestureFlingStart",
164 delta
.InMicroseconds(), 1, 1000000, 100);
177 InputHandlerProxy::InputHandlerProxy(cc::InputHandler
* input_handler
,
178 InputHandlerProxyClient
* client
)
180 input_handler_(input_handler
),
181 deferred_fling_cancel_time_seconds_(0),
183 expect_scroll_update_end_(false),
185 gesture_scroll_on_impl_thread_(false),
186 gesture_pinch_on_impl_thread_(false),
187 fling_may_be_active_on_main_thread_(false),
188 disallow_horizontal_fling_scroll_(false),
189 disallow_vertical_fling_scroll_(false),
190 has_fling_animation_started_(false),
191 uma_latency_reporting_enabled_(base::TimeTicks::IsHighResolution()) {
193 input_handler_
->BindToClient(this);
194 smooth_scroll_enabled_
= base::CommandLine::ForCurrentProcess()->HasSwitch(
195 switches::kEnableSmoothScrolling
);
196 cc::ScrollElasticityHelper
* scroll_elasticity_helper
=
197 input_handler_
->CreateScrollElasticityHelper();
198 if (scroll_elasticity_helper
) {
199 scroll_elasticity_controller_
.reset(
200 new InputScrollElasticityController(scroll_elasticity_helper
));
204 InputHandlerProxy::~InputHandlerProxy() {}
206 void InputHandlerProxy::WillShutdown() {
207 scroll_elasticity_controller_
.reset();
208 input_handler_
= NULL
;
209 client_
->WillShutdown();
212 InputHandlerProxy::EventDisposition
213 InputHandlerProxy::HandleInputEventWithLatencyInfo(
214 const WebInputEvent
& event
,
215 ui::LatencyInfo
* latency_info
) {
216 DCHECK(input_handler_
);
218 if (uma_latency_reporting_enabled_
)
219 ReportInputEventLatencyUma(event
, *latency_info
);
221 TRACE_EVENT_FLOW_STEP0("input,benchmark",
223 TRACE_ID_DONT_MANGLE(latency_info
->trace_id
),
224 "HandleInputEventImpl");
226 scoped_ptr
<cc::SwapPromiseMonitor
> latency_info_swap_promise_monitor
=
227 input_handler_
->CreateLatencyInfoSwapPromiseMonitor(latency_info
);
228 InputHandlerProxy::EventDisposition disposition
= HandleInputEvent(event
);
232 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleInputEvent(
233 const WebInputEvent
& event
) {
234 DCHECK(input_handler_
);
235 TRACE_EVENT1("input,benchmark", "InputHandlerProxy::HandleInputEvent",
236 "type", WebInputEventTraits::GetName(event
.type
));
238 if (FilterInputEventForFlingBoosting(event
))
241 switch (event
.type
) {
242 case WebInputEvent::MouseWheel
:
243 return HandleMouseWheel(static_cast<const WebMouseWheelEvent
&>(event
));
245 case WebInputEvent::GestureScrollBegin
:
246 return HandleGestureScrollBegin(
247 static_cast<const WebGestureEvent
&>(event
));
249 case WebInputEvent::GestureScrollUpdate
:
250 return HandleGestureScrollUpdate(
251 static_cast<const WebGestureEvent
&>(event
));
253 case WebInputEvent::GestureScrollEnd
:
254 return HandleGestureScrollEnd(static_cast<const WebGestureEvent
&>(event
));
256 case WebInputEvent::GesturePinchBegin
: {
257 DCHECK(!gesture_pinch_on_impl_thread_
);
258 const WebGestureEvent
& gesture_event
=
259 static_cast<const WebGestureEvent
&>(event
);
260 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
&&
261 input_handler_
->HaveWheelEventHandlersAt(
262 gfx::Point(gesture_event
.x
, gesture_event
.y
))) {
263 return DID_NOT_HANDLE
;
265 input_handler_
->PinchGestureBegin();
266 gesture_pinch_on_impl_thread_
= true;
271 case WebInputEvent::GesturePinchEnd
:
272 if (gesture_pinch_on_impl_thread_
) {
273 gesture_pinch_on_impl_thread_
= false;
274 input_handler_
->PinchGestureEnd();
277 return DID_NOT_HANDLE
;
280 case WebInputEvent::GesturePinchUpdate
: {
281 if (gesture_pinch_on_impl_thread_
) {
282 const WebGestureEvent
& gesture_event
=
283 static_cast<const WebGestureEvent
&>(event
);
284 if (gesture_event
.data
.pinchUpdate
.zoomDisabled
)
286 input_handler_
->PinchGestureUpdate(
287 gesture_event
.data
.pinchUpdate
.scale
,
288 gfx::Point(gesture_event
.x
, gesture_event
.y
));
291 return DID_NOT_HANDLE
;
295 case WebInputEvent::GestureFlingStart
:
296 return HandleGestureFlingStart(
297 *static_cast<const WebGestureEvent
*>(&event
));
299 case WebInputEvent::GestureFlingCancel
:
300 if (CancelCurrentFling())
302 else if (!fling_may_be_active_on_main_thread_
)
304 return DID_NOT_HANDLE
;
306 case WebInputEvent::TouchStart
:
307 return HandleTouchStart(static_cast<const WebTouchEvent
&>(event
));
309 case WebInputEvent::MouseMove
: {
310 const WebMouseEvent
& mouse_event
=
311 static_cast<const WebMouseEvent
&>(event
);
312 // TODO(tony): Ignore when mouse buttons are down?
313 // TODO(davemoore): This should never happen, but bug #326635 showed some
314 // surprising crashes.
315 CHECK(input_handler_
);
316 input_handler_
->MouseMoveAt(gfx::Point(mouse_event
.x
, mouse_event
.y
));
317 return DID_NOT_HANDLE
;
321 if (WebInputEvent::isKeyboardEventType(event
.type
)) {
322 // Only call |CancelCurrentFling()| if a fling was active, as it will
323 // otherwise disrupt an in-progress touch scroll.
325 CancelCurrentFling();
330 return DID_NOT_HANDLE
;
333 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleMouseWheel(
334 const WebMouseWheelEvent
& wheel_event
) {
335 InputHandlerProxy::EventDisposition result
= DID_NOT_HANDLE
;
336 cc::InputHandlerScrollResult scroll_result
;
338 // TODO(ccameron): The rail information should be pushed down into
340 gfx::Vector2dF
scroll_delta(
341 wheel_event
.railsMode
!= WebInputEvent::RailsModeVertical
342 ? -wheel_event
.deltaX
344 wheel_event
.railsMode
!= WebInputEvent::RailsModeHorizontal
345 ? -wheel_event
.deltaY
348 if (wheel_event
.scrollByPage
) {
349 // TODO(jamesr): We don't properly handle scroll by page in the compositor
350 // thread, so punt it to the main thread. http://crbug.com/236639
351 result
= DID_NOT_HANDLE
;
352 } else if (!wheel_event
.canScroll
) {
353 // Wheel events with |canScroll| == false will not trigger scrolling,
354 // only event handlers. Forward to the main thread.
355 result
= DID_NOT_HANDLE
;
356 } else if (smooth_scroll_enabled_
) {
357 cc::InputHandler::ScrollStatus scroll_status
=
358 input_handler_
->ScrollAnimated(gfx::Point(wheel_event
.x
, wheel_event
.y
),
360 switch (scroll_status
) {
361 case cc::InputHandler::SCROLL_STARTED
:
364 case cc::InputHandler::SCROLL_IGNORED
:
367 result
= DID_NOT_HANDLE
;
371 cc::InputHandler::ScrollStatus scroll_status
= input_handler_
->ScrollBegin(
372 gfx::Point(wheel_event
.x
, wheel_event
.y
), cc::InputHandler::WHEEL
);
373 switch (scroll_status
) {
374 case cc::InputHandler::SCROLL_STARTED
: {
375 TRACE_EVENT_INSTANT2("input",
376 "InputHandlerProxy::handle_input wheel scroll",
377 TRACE_EVENT_SCOPE_THREAD
, "deltaX",
378 scroll_delta
.x(), "deltaY", scroll_delta
.y());
379 gfx::Point
scroll_point(wheel_event
.x
, wheel_event
.y
);
380 scroll_result
= input_handler_
->ScrollBy(scroll_point
, scroll_delta
);
381 HandleOverscroll(scroll_point
, scroll_result
);
382 input_handler_
->ScrollEnd();
383 result
= scroll_result
.did_scroll
? DID_HANDLE
: DROP_EVENT
;
386 case cc::InputHandler::SCROLL_IGNORED
:
387 // TODO(jamesr): This should be DROP_EVENT, but in cases where we fail
388 // to properly sync scrollability it's safer to send the event to the
389 // main thread. Change back to DROP_EVENT once we have synchronization
391 result
= DID_NOT_HANDLE
;
393 case cc::InputHandler::SCROLL_UNKNOWN
:
394 case cc::InputHandler::SCROLL_ON_MAIN_THREAD
:
395 result
= DID_NOT_HANDLE
;
397 case cc::InputHandler::ScrollStatusCount
:
403 // Send the event and its disposition to the elasticity controller to update
404 // the over-scroll animation. If the event is to be handled on the main
405 // thread, the event and its disposition will be sent to the elasticity
406 // controller after being handled on the main thread.
407 if (scroll_elasticity_controller_
&& result
!= DID_NOT_HANDLE
) {
408 // Note that the call to the elasticity controller is made asynchronously,
409 // to minimize divergence between main thread and impl thread event
411 base::ThreadTaskRunnerHandle::Get()->PostTask(
413 base::Bind(&InputScrollElasticityController::ObserveWheelEventAndResult
,
414 scroll_elasticity_controller_
->GetWeakPtr(), wheel_event
,
420 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleGestureScrollBegin(
421 const WebGestureEvent
& gesture_event
) {
422 DCHECK(!gesture_scroll_on_impl_thread_
);
424 DCHECK(!expect_scroll_update_end_
);
425 expect_scroll_update_end_
= true;
427 cc::InputHandler::ScrollStatus scroll_status
= input_handler_
->ScrollBegin(
428 gfx::Point(gesture_event
.x
, gesture_event
.y
), cc::InputHandler::GESTURE
);
429 UMA_HISTOGRAM_ENUMERATION("Renderer4.CompositorScrollHitTestResult",
431 cc::InputHandler::ScrollStatusCount
);
432 switch (scroll_status
) {
433 case cc::InputHandler::SCROLL_STARTED
:
434 TRACE_EVENT_INSTANT0("input",
435 "InputHandlerProxy::handle_input gesture scroll",
436 TRACE_EVENT_SCOPE_THREAD
);
437 gesture_scroll_on_impl_thread_
= true;
439 case cc::InputHandler::SCROLL_UNKNOWN
:
440 case cc::InputHandler::SCROLL_ON_MAIN_THREAD
:
441 return DID_NOT_HANDLE
;
442 case cc::InputHandler::SCROLL_IGNORED
:
444 case cc::InputHandler::ScrollStatusCount
:
448 return DID_NOT_HANDLE
;
451 InputHandlerProxy::EventDisposition
452 InputHandlerProxy::HandleGestureScrollUpdate(
453 const WebGestureEvent
& gesture_event
) {
455 DCHECK(expect_scroll_update_end_
);
458 if (!gesture_scroll_on_impl_thread_
&& !gesture_pinch_on_impl_thread_
)
459 return DID_NOT_HANDLE
;
461 gfx::Point
scroll_point(gesture_event
.x
, gesture_event
.y
);
462 gfx::Vector2dF
scroll_delta(-gesture_event
.data
.scrollUpdate
.deltaX
,
463 -gesture_event
.data
.scrollUpdate
.deltaY
);
464 cc::InputHandlerScrollResult scroll_result
= input_handler_
->ScrollBy(
465 scroll_point
, scroll_delta
);
466 HandleOverscroll(scroll_point
, scroll_result
);
467 return scroll_result
.did_scroll
? DID_HANDLE
: DROP_EVENT
;
470 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleGestureScrollEnd(
471 const WebGestureEvent
& gesture_event
) {
473 DCHECK(expect_scroll_update_end_
);
474 expect_scroll_update_end_
= false;
476 input_handler_
->ScrollEnd();
477 if (!gesture_scroll_on_impl_thread_
)
478 return DID_NOT_HANDLE
;
479 gesture_scroll_on_impl_thread_
= false;
483 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleGestureFlingStart(
484 const WebGestureEvent
& gesture_event
) {
485 cc::InputHandler::ScrollStatus scroll_status
;
487 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
) {
488 scroll_status
= input_handler_
->ScrollBegin(
489 gfx::Point(gesture_event
.x
, gesture_event
.y
),
490 cc::InputHandler::NON_BUBBLING_GESTURE
);
492 if (!gesture_scroll_on_impl_thread_
)
493 scroll_status
= cc::InputHandler::SCROLL_ON_MAIN_THREAD
;
495 scroll_status
= input_handler_
->FlingScrollBegin();
499 expect_scroll_update_end_
= false;
502 switch (scroll_status
) {
503 case cc::InputHandler::SCROLL_STARTED
: {
504 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
)
505 input_handler_
->ScrollEnd();
507 const float vx
= gesture_event
.data
.flingStart
.velocityX
;
508 const float vy
= gesture_event
.data
.flingStart
.velocityY
;
509 current_fling_velocity_
= gfx::Vector2dF(vx
, vy
);
510 DCHECK(!current_fling_velocity_
.IsZero());
511 fling_curve_
.reset(client_
->CreateFlingAnimationCurve(
512 gesture_event
.sourceDevice
,
513 WebFloatPoint(vx
, vy
),
515 disallow_horizontal_fling_scroll_
= !vx
;
516 disallow_vertical_fling_scroll_
= !vy
;
517 TRACE_EVENT_ASYNC_BEGIN2("input",
518 "InputHandlerProxy::HandleGestureFling::started",
524 // Note that the timestamp will only be used to kickstart the animation if
525 // its sufficiently close to the timestamp of the first call |Animate()|.
526 has_fling_animation_started_
= false;
527 fling_parameters_
.startTime
= gesture_event
.timeStampSeconds
;
528 fling_parameters_
.delta
= WebFloatPoint(vx
, vy
);
529 fling_parameters_
.point
= WebPoint(gesture_event
.x
, gesture_event
.y
);
530 fling_parameters_
.globalPoint
=
531 WebPoint(gesture_event
.globalX
, gesture_event
.globalY
);
532 fling_parameters_
.modifiers
= gesture_event
.modifiers
;
533 fling_parameters_
.sourceDevice
= gesture_event
.sourceDevice
;
534 input_handler_
->SetNeedsAnimate();
537 case cc::InputHandler::SCROLL_UNKNOWN
:
538 case cc::InputHandler::SCROLL_ON_MAIN_THREAD
: {
539 TRACE_EVENT_INSTANT0("input",
540 "InputHandlerProxy::HandleGestureFling::"
541 "scroll_on_main_thread",
542 TRACE_EVENT_SCOPE_THREAD
);
543 gesture_scroll_on_impl_thread_
= false;
544 fling_may_be_active_on_main_thread_
= true;
545 return DID_NOT_HANDLE
;
547 case cc::InputHandler::SCROLL_IGNORED
: {
548 TRACE_EVENT_INSTANT0(
550 "InputHandlerProxy::HandleGestureFling::ignored",
551 TRACE_EVENT_SCOPE_THREAD
);
552 gesture_scroll_on_impl_thread_
= false;
553 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
) {
554 // We still pass the curve to the main thread if there's nothing
555 // scrollable, in case something
556 // registers a handler before the curve is over.
557 return DID_NOT_HANDLE
;
561 case cc::InputHandler::ScrollStatusCount
:
565 return DID_NOT_HANDLE
;
568 InputHandlerProxy::EventDisposition
InputHandlerProxy::HandleTouchStart(
569 const blink::WebTouchEvent
& touch_event
) {
570 for (size_t i
= 0; i
< touch_event
.touchesLength
; ++i
) {
571 if (touch_event
.touches
[i
].state
!= WebTouchPoint::StatePressed
)
573 if (input_handler_
->DoTouchEventsBlockScrollAt(
574 gfx::Point(touch_event
.touches
[i
].position
.x
,
575 touch_event
.touches
[i
].position
.y
))) {
576 // TODO(rbyers): We should consider still sending the touch events to
577 // main asynchronously (crbug.com/455539).
578 return DID_NOT_HANDLE
;
584 bool InputHandlerProxy::FilterInputEventForFlingBoosting(
585 const WebInputEvent
& event
) {
586 if (!WebInputEvent::isGestureEventType(event
.type
))
590 DCHECK(!deferred_fling_cancel_time_seconds_
);
594 const WebGestureEvent
& gesture_event
=
595 static_cast<const WebGestureEvent
&>(event
);
596 if (gesture_event
.type
== WebInputEvent::GestureFlingCancel
) {
597 if (gesture_event
.data
.flingCancel
.preventBoosting
)
600 if (current_fling_velocity_
.LengthSquared() < kMinBoostFlingSpeedSquare
)
603 TRACE_EVENT_INSTANT0("input",
604 "InputHandlerProxy::FlingBoostStart",
605 TRACE_EVENT_SCOPE_THREAD
);
606 deferred_fling_cancel_time_seconds_
=
607 event
.timeStampSeconds
+ kFlingBoostTimeoutDelaySeconds
;
611 // A fling is either inactive or is "free spinning", i.e., has yet to be
612 // interrupted by a touch gesture, in which case there is nothing to filter.
613 if (!deferred_fling_cancel_time_seconds_
)
616 // Gestures from a different source should immediately interrupt the fling.
617 if (gesture_event
.sourceDevice
!= fling_parameters_
.sourceDevice
) {
618 CancelCurrentFling();
622 switch (gesture_event
.type
) {
623 case WebInputEvent::GestureTapCancel
:
624 case WebInputEvent::GestureTapDown
:
627 case WebInputEvent::GestureScrollBegin
:
628 if (!input_handler_
->IsCurrentlyScrollingLayerAt(
629 gfx::Point(gesture_event
.x
, gesture_event
.y
),
630 fling_parameters_
.sourceDevice
== blink::WebGestureDeviceTouchpad
631 ? cc::InputHandler::NON_BUBBLING_GESTURE
632 : cc::InputHandler::GESTURE
)) {
633 CancelCurrentFling();
637 // TODO(jdduke): Use |gesture_event.data.scrollBegin.delta{X,Y}Hint| to
638 // determine if the ScrollBegin should immediately cancel the fling.
639 ExtendBoostedFlingTimeout(gesture_event
);
642 case WebInputEvent::GestureScrollUpdate
: {
643 const double time_since_last_boost_event
=
644 event
.timeStampSeconds
- last_fling_boost_event_
.timeStampSeconds
;
645 if (ShouldSuppressScrollForFlingBoosting(current_fling_velocity_
,
647 time_since_last_boost_event
)) {
648 ExtendBoostedFlingTimeout(gesture_event
);
652 CancelCurrentFling();
656 case WebInputEvent::GestureScrollEnd
:
657 // Clear the last fling boost event *prior* to fling cancellation,
658 // preventing insertion of a synthetic GestureScrollBegin.
659 last_fling_boost_event_
= WebGestureEvent();
660 CancelCurrentFling();
663 case WebInputEvent::GestureFlingStart
: {
664 DCHECK_EQ(fling_parameters_
.sourceDevice
, gesture_event
.sourceDevice
);
667 fling_parameters_
.modifiers
== gesture_event
.modifiers
&&
668 ShouldBoostFling(current_fling_velocity_
, gesture_event
);
670 gfx::Vector2dF
new_fling_velocity(
671 gesture_event
.data
.flingStart
.velocityX
,
672 gesture_event
.data
.flingStart
.velocityY
);
673 DCHECK(!new_fling_velocity
.IsZero());
676 current_fling_velocity_
+= new_fling_velocity
;
678 current_fling_velocity_
= new_fling_velocity
;
680 WebFloatPoint
velocity(current_fling_velocity_
.x(),
681 current_fling_velocity_
.y());
682 deferred_fling_cancel_time_seconds_
= 0;
683 disallow_horizontal_fling_scroll_
= !velocity
.x
;
684 disallow_vertical_fling_scroll_
= !velocity
.y
;
685 last_fling_boost_event_
= WebGestureEvent();
686 fling_curve_
.reset(client_
->CreateFlingAnimationCurve(
687 gesture_event
.sourceDevice
,
690 fling_parameters_
.startTime
= gesture_event
.timeStampSeconds
;
691 fling_parameters_
.delta
= velocity
;
692 fling_parameters_
.point
= WebPoint(gesture_event
.x
, gesture_event
.y
);
693 fling_parameters_
.globalPoint
=
694 WebPoint(gesture_event
.globalX
, gesture_event
.globalY
);
696 TRACE_EVENT_INSTANT2("input",
697 fling_boosted
? "InputHandlerProxy::FlingBoosted"
698 : "InputHandlerProxy::FlingReplaced",
699 TRACE_EVENT_SCOPE_THREAD
,
701 current_fling_velocity_
.x(),
703 current_fling_velocity_
.y());
705 // The client expects balanced calls between a consumed GestureFlingStart
706 // and |DidStopFlinging()|. TODO(jdduke): Provide a count parameter to
707 // |DidStopFlinging()| and only send after the accumulated fling ends.
708 client_
->DidStopFlinging();
713 // All other types of gestures (taps, presses, etc...) will complete the
714 // deferred fling cancellation.
715 CancelCurrentFling();
720 void InputHandlerProxy::ExtendBoostedFlingTimeout(
721 const blink::WebGestureEvent
& event
) {
722 TRACE_EVENT_INSTANT0("input",
723 "InputHandlerProxy::ExtendBoostedFlingTimeout",
724 TRACE_EVENT_SCOPE_THREAD
);
725 deferred_fling_cancel_time_seconds_
=
726 event
.timeStampSeconds
+ kFlingBoostTimeoutDelaySeconds
;
727 last_fling_boost_event_
= event
;
730 void InputHandlerProxy::Animate(base::TimeTicks time
) {
731 if (scroll_elasticity_controller_
)
732 scroll_elasticity_controller_
->Animate(time
);
737 double monotonic_time_sec
= InSecondsF(time
);
739 if (deferred_fling_cancel_time_seconds_
&&
740 monotonic_time_sec
> deferred_fling_cancel_time_seconds_
) {
741 CancelCurrentFling();
745 client_
->DidAnimateForInput();
747 if (!has_fling_animation_started_
) {
748 has_fling_animation_started_
= true;
749 // Guard against invalid, future or sufficiently stale start times, as there
750 // are no guarantees fling event and animation timestamps are compatible.
751 if (!fling_parameters_
.startTime
||
752 monotonic_time_sec
<= fling_parameters_
.startTime
||
753 monotonic_time_sec
>= fling_parameters_
.startTime
+
754 kMaxSecondsFromFlingTimestampToFirstAnimate
) {
755 fling_parameters_
.startTime
= monotonic_time_sec
;
756 input_handler_
->SetNeedsAnimate();
761 bool fling_is_active
=
762 fling_curve_
->apply(monotonic_time_sec
- fling_parameters_
.startTime
,
765 if (disallow_vertical_fling_scroll_
&& disallow_horizontal_fling_scroll_
)
766 fling_is_active
= false;
768 if (fling_is_active
) {
769 input_handler_
->SetNeedsAnimate();
771 TRACE_EVENT_INSTANT0("input",
772 "InputHandlerProxy::animate::flingOver",
773 TRACE_EVENT_SCOPE_THREAD
);
774 CancelCurrentFling();
778 void InputHandlerProxy::MainThreadHasStoppedFlinging() {
779 fling_may_be_active_on_main_thread_
= false;
780 client_
->DidStopFlinging();
783 void InputHandlerProxy::ReconcileElasticOverscrollAndRootScroll() {
784 if (scroll_elasticity_controller_
)
785 scroll_elasticity_controller_
->ReconcileStretchAndScroll();
788 void InputHandlerProxy::HandleOverscroll(
789 const gfx::Point
& causal_event_viewport_point
,
790 const cc::InputHandlerScrollResult
& scroll_result
) {
792 if (!scroll_result
.did_overscroll_root
)
795 TRACE_EVENT2("input",
796 "InputHandlerProxy::DidOverscroll",
798 scroll_result
.unused_scroll_delta
.x(),
800 scroll_result
.unused_scroll_delta
.y());
802 DidOverscrollParams params
;
803 params
.accumulated_overscroll
= scroll_result
.accumulated_root_overscroll
;
804 params
.latest_overscroll_delta
= scroll_result
.unused_scroll_delta
;
805 params
.current_fling_velocity
=
806 ToClientScrollIncrement(current_fling_velocity_
);
807 params
.causal_event_viewport_point
= causal_event_viewport_point
;
810 static const int kFlingOverscrollThreshold
= 1;
811 disallow_horizontal_fling_scroll_
|=
812 std::abs(params
.accumulated_overscroll
.x()) >=
813 kFlingOverscrollThreshold
;
814 disallow_vertical_fling_scroll_
|=
815 std::abs(params
.accumulated_overscroll
.y()) >=
816 kFlingOverscrollThreshold
;
819 client_
->DidOverscroll(params
);
822 bool InputHandlerProxy::CancelCurrentFling() {
823 if (CancelCurrentFlingWithoutNotifyingClient()) {
824 client_
->DidStopFlinging();
830 bool InputHandlerProxy::CancelCurrentFlingWithoutNotifyingClient() {
831 bool had_fling_animation
= fling_curve_
;
832 if (had_fling_animation
&&
833 fling_parameters_
.sourceDevice
== blink::WebGestureDeviceTouchscreen
) {
834 input_handler_
->ScrollEnd();
835 TRACE_EVENT_ASYNC_END0(
837 "InputHandlerProxy::HandleGestureFling::started",
841 TRACE_EVENT_INSTANT1("input",
842 "InputHandlerProxy::CancelCurrentFling",
843 TRACE_EVENT_SCOPE_THREAD
,
844 "had_fling_animation",
845 had_fling_animation
);
846 fling_curve_
.reset();
847 has_fling_animation_started_
= false;
848 gesture_scroll_on_impl_thread_
= false;
849 current_fling_velocity_
= gfx::Vector2dF();
850 fling_parameters_
= blink::WebActiveWheelFlingParameters();
852 if (deferred_fling_cancel_time_seconds_
) {
853 deferred_fling_cancel_time_seconds_
= 0;
855 WebGestureEvent last_fling_boost_event
= last_fling_boost_event_
;
856 last_fling_boost_event_
= WebGestureEvent();
857 if (last_fling_boost_event
.type
== WebInputEvent::GestureScrollBegin
||
858 last_fling_boost_event
.type
== WebInputEvent::GestureScrollUpdate
) {
859 // Synthesize a GestureScrollBegin, as the original was suppressed.
860 HandleInputEvent(ObtainGestureScrollBegin(last_fling_boost_event
));
864 return had_fling_animation
;
867 bool InputHandlerProxy::TouchpadFlingScroll(
868 const WebFloatSize
& increment
) {
869 WebMouseWheelEvent synthetic_wheel
;
870 synthetic_wheel
.type
= WebInputEvent::MouseWheel
;
871 synthetic_wheel
.deltaX
= increment
.width
;
872 synthetic_wheel
.deltaY
= increment
.height
;
873 synthetic_wheel
.hasPreciseScrollingDeltas
= true;
874 synthetic_wheel
.x
= fling_parameters_
.point
.x
;
875 synthetic_wheel
.y
= fling_parameters_
.point
.y
;
876 synthetic_wheel
.globalX
= fling_parameters_
.globalPoint
.x
;
877 synthetic_wheel
.globalY
= fling_parameters_
.globalPoint
.y
;
878 synthetic_wheel
.modifiers
= fling_parameters_
.modifiers
;
880 InputHandlerProxy::EventDisposition disposition
=
881 HandleInputEvent(synthetic_wheel
);
882 switch (disposition
) {
888 TRACE_EVENT_INSTANT0("input",
889 "InputHandlerProxy::scrollBy::AbortFling",
890 TRACE_EVENT_SCOPE_THREAD
);
891 // If we got a DID_NOT_HANDLE, that means we need to deliver wheels on the
892 // main thread. In this case we need to schedule a commit and transfer the
893 // fling curve over to the main thread and run the rest of the wheels from
894 // there. This can happen when flinging a page that contains a scrollable
895 // subarea that we can't scroll on the thread if the fling starts outside
896 // the subarea but then is flung "under" the pointer.
897 client_
->TransferActiveWheelFlingAnimation(fling_parameters_
);
898 fling_may_be_active_on_main_thread_
= true;
899 CancelCurrentFlingWithoutNotifyingClient();
906 bool InputHandlerProxy::scrollBy(const WebFloatSize
& increment
,
907 const WebFloatSize
& velocity
) {
908 WebFloatSize clipped_increment
;
909 WebFloatSize clipped_velocity
;
910 if (!disallow_horizontal_fling_scroll_
) {
911 clipped_increment
.width
= increment
.width
;
912 clipped_velocity
.width
= velocity
.width
;
914 if (!disallow_vertical_fling_scroll_
) {
915 clipped_increment
.height
= increment
.height
;
916 clipped_velocity
.height
= velocity
.height
;
919 current_fling_velocity_
= clipped_velocity
;
921 // Early out if the increment is zero, but avoid early terimination if the
922 // velocity is still non-zero.
923 if (clipped_increment
== WebFloatSize())
924 return clipped_velocity
!= WebFloatSize();
926 TRACE_EVENT2("input",
927 "InputHandlerProxy::scrollBy",
929 clipped_increment
.width
,
931 clipped_increment
.height
);
933 bool did_scroll
= false;
935 switch (fling_parameters_
.sourceDevice
) {
936 case blink::WebGestureDeviceTouchpad
:
937 did_scroll
= TouchpadFlingScroll(clipped_increment
);
939 case blink::WebGestureDeviceTouchscreen
: {
940 clipped_increment
= ToClientScrollIncrement(clipped_increment
);
941 cc::InputHandlerScrollResult scroll_result
= input_handler_
->ScrollBy(
942 fling_parameters_
.point
, clipped_increment
);
943 HandleOverscroll(fling_parameters_
.point
, scroll_result
);
944 did_scroll
= scroll_result
.did_scroll
;
949 fling_parameters_
.cumulativeScroll
.width
+= clipped_increment
.width
;
950 fling_parameters_
.cumulativeScroll
.height
+= clipped_increment
.height
;
953 // It's possible the provided |increment| is sufficiently small as to not
954 // trigger a scroll, e.g., with a trivial time delta between fling updates.
955 // Return true in this case to prevent early fling termination.
956 if (std::abs(clipped_increment
.width
) < kScrollEpsilon
&&
957 std::abs(clipped_increment
.height
) < kScrollEpsilon
)
963 } // namespace content