Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / input / gesture_event_queue.h
bloba0a8146d38dcb96c427ef632bac9f0cffe498a7d
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 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_
8 #include <deque>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/renderer_host/event_with_latency_info.h"
14 #include "content/browser/renderer_host/input/tap_suppression_controller.h"
15 #include "content/browser/renderer_host/input/touchpad_tap_suppression_controller.h"
16 #include "content/browser/renderer_host/input/touchscreen_tap_suppression_controller.h"
17 #include "content/common/content_export.h"
18 #include "content/common/input/input_event_ack_state.h"
19 #include "third_party/WebKit/public/web/WebInputEvent.h"
20 #include "ui/gfx/transform.h"
22 namespace content {
23 class GestureEventQueueTest;
24 class InputRouter;
25 class MockRenderWidgetHost;
27 // Interface with which the GestureEventQueue can forward gesture events, and
28 // dispatch gesture event responses.
29 class CONTENT_EXPORT GestureEventQueueClient {
30 public:
31 virtual ~GestureEventQueueClient() {}
33 virtual void SendGestureEventImmediately(
34 const GestureEventWithLatencyInfo& event) = 0;
36 virtual void OnGestureEventAck(
37 const GestureEventWithLatencyInfo& event,
38 InputEventAckState ack_result) = 0;
41 // Maintains WebGestureEvents in a queue before forwarding them to the renderer
42 // to apply a sequence of filters on them:
43 // 1. Zero-velocity fling-starts from touchpad are filtered.
44 // 2. The sequence is filtered for bounces. A bounce is when the finger lifts
45 // from the screen briefly during an in-progress scroll. Ifco this happens,
46 // non-GestureScrollUpdate events are queued until the de-bounce interval
47 // passes or another GestureScrollUpdate event occurs.
48 // 3. Unnecessary GestureFlingCancel events are filtered. These are
49 // GestureFlingCancels that have no corresponding GestureFlingStart in the
50 // queue.
51 // 4. Taps immediately after a GestureFlingCancel (caused by the same tap) are
52 // filtered.
53 // 5. Whenever possible, events in the queue are coalesced to have as few events
54 // as possible and therefore maximize the chance that the event stream can be
55 // handled entirely by the compositor thread.
56 // Events in the queue are forwarded to the renderer one by one; i.e., each
57 // event is sent after receiving the ACK for previous one. The only exception is
58 // that if a GestureScrollUpdate is followed by a GesturePinchUpdate, they are
59 // sent together.
60 // TODO(rjkroege): Possibly refactor into a filter chain:
61 // http://crbug.com/148443.
62 class CONTENT_EXPORT GestureEventQueue {
63 public:
64 struct CONTENT_EXPORT Config {
65 Config();
67 // Controls touchpad-related tap suppression, disabled by default.
68 TapSuppressionController::Config touchpad_tap_suppression_config;
70 // Controls touchscreen-related tap suppression, disabled by default.
71 TapSuppressionController::Config touchscreen_tap_suppression_config;
73 // Determines whether non-scroll gesture events are "debounced" during an
74 // active scroll sequence, suppressing brief scroll interruptions.
75 // Zero by default (disabled).
76 base::TimeDelta debounce_interval;
79 // Both |client| and |touchpad_client| must outlive the GestureEventQueue.
80 GestureEventQueue(GestureEventQueueClient* client,
81 TouchpadTapSuppressionControllerClient* touchpad_client,
82 const Config& config);
83 ~GestureEventQueue();
85 // Returns |true| if the caller should immediately forward the provided
86 // |GestureEventWithLatencyInfo| argument to the renderer.
87 // If this function returns false, then the event may be queued and forwared
88 // at a later point.
89 bool ShouldForward(const GestureEventWithLatencyInfo&);
91 // Indicates that the caller has received an acknowledgement from the renderer
92 // with state |ack_result| and event |type|. May send events if the queue is
93 // not empty.
94 void ProcessGestureAck(InputEventAckState ack_result,
95 blink::WebInputEvent::Type type,
96 const ui::LatencyInfo& latency);
98 // Sets the state of the |fling_in_progress_| field to indicate that a fling
99 // is definitely not in progress.
100 void FlingHasBeenHalted();
102 // Returns the |TouchpadTapSuppressionController| instance.
103 TouchpadTapSuppressionController* GetTouchpadTapSuppressionController();
105 void ForwardGestureEvent(const GestureEventWithLatencyInfo& gesture_event);
107 // Whether the queue is expecting a gesture event ack.
108 bool ExpectingGestureAck() const;
110 bool empty() const {
111 return coalesced_gesture_events_.empty() &&
112 debouncing_deferral_queue_.empty();
115 void set_debounce_interval_time_ms_for_testing(int interval_ms) {
116 debounce_interval_ = base::TimeDelta::FromMilliseconds(interval_ms);
119 private:
120 friend class GestureEventQueueTest;
121 friend class MockRenderWidgetHost;
123 // TODO(mohsen): There are a bunch of ShouldForward.../ShouldDiscard...
124 // methods that are getting confusing. This should be somehow fixed. Maybe
125 // while refactoring GEQ: http://crbug.com/148443.
127 // Inovked on the expiration of the debounce interval to release
128 // deferred events.
129 void SendScrollEndingEventsNow();
131 // Returns |true| if the given GestureFlingCancel should be discarded
132 // as unnecessary.
133 bool ShouldDiscardFlingCancelEvent(
134 const GestureEventWithLatencyInfo& gesture_event) const;
136 // Returns |true| if the only event in the queue is the current event and
137 // hence that event should be handled now.
138 bool ShouldHandleEventNow() const;
140 // Merge or append a GestureScrollUpdate or GesturePinchUpdate into
141 // the coalescing queue.
142 void MergeOrInsertScrollAndPinchEvent(
143 const GestureEventWithLatencyInfo& gesture_event);
145 // Sub-filter for removing zero-velocity fling-starts from touchpad.
146 bool ShouldForwardForZeroVelocityFlingStart(
147 const GestureEventWithLatencyInfo& gesture_event) const;
149 // Sub-filter for removing bounces from in-progress scrolls.
150 bool ShouldForwardForBounceReduction(
151 const GestureEventWithLatencyInfo& gesture_event);
153 // Sub-filter for removing unnecessary GestureFlingCancels.
154 bool ShouldForwardForGFCFiltering(
155 const GestureEventWithLatencyInfo& gesture_event) const;
157 // Sub-filter for suppressing taps immediately after a GestureFlingCancel.
158 bool ShouldForwardForTapSuppression(
159 const GestureEventWithLatencyInfo& gesture_event);
161 // Puts the events in a queue to forward them one by one; i.e., forward them
162 // whenever ACK for previous event is received. This queue also tries to
163 // coalesce events as much as possible.
164 bool ShouldForwardForCoalescing(
165 const GestureEventWithLatencyInfo& gesture_event);
167 // Whether the event_in_queue is GesturePinchUpdate or
168 // GestureScrollUpdate and it has the same modifiers as the
169 // new event.
170 bool ShouldTryMerging(
171 const GestureEventWithLatencyInfo& new_event,
172 const GestureEventWithLatencyInfo& event_in_queue)const;
174 // Returns the transform matrix corresponding to the gesture event.
175 // Assumes the gesture event sent is either GestureScrollUpdate or
176 // GesturePinchUpdate. Returns the identity matrix otherwise.
177 gfx::Transform GetTransformForEvent(
178 const GestureEventWithLatencyInfo& gesture_event) const;
180 // The number of sent events for which we're awaiting an ack. These events
181 // remain at the head of the queue until ack'ed.
182 size_t EventsInFlightCount() const;
184 // The receiver of all forwarded gesture events.
185 GestureEventQueueClient* client_;
187 // True if a GestureFlingStart is in progress on the renderer or
188 // queued without a subsequent queued GestureFlingCancel event.
189 bool fling_in_progress_;
191 // True if a GestureScrollUpdate sequence is in progress.
192 bool scrolling_in_progress_;
194 // True if two related gesture events were sent before without waiting
195 // for an ACK, so the next gesture ACK should be ignored.
196 bool ignore_next_ack_;
198 // An object tracking the state of touchpad on the delivery of mouse events to
199 // the renderer to filter mouse immediately after a touchpad fling canceling
200 // tap.
201 // TODO(mohsen): Move touchpad tap suppression out of GestureEventQueue since
202 // GEQ is meant to only be used for touchscreen gesture events.
203 TouchpadTapSuppressionController touchpad_tap_suppression_controller_;
205 // An object tracking the state of touchscreen on the delivery of gesture tap
206 // events to the renderer to filter taps immediately after a touchscreen fling
207 // canceling tap.
208 TouchscreenTapSuppressionController touchscreen_tap_suppression_controller_;
210 typedef std::deque<GestureEventWithLatencyInfo> GestureQueue;
212 // Queue of coalesced gesture events not yet sent to the renderer. If
213 // |ignore_next_ack_| is false, then the event at the front of the queue has
214 // been sent and is awaiting an ACK, and all other events have yet to be sent.
215 // If |ignore_next_ack_| is true, then the two events at the front of the
216 // queue have been sent, and the second is awaiting an ACK. All other events
217 // have yet to be sent.
218 GestureQueue coalesced_gesture_events_;
220 // Timer to release a previously deferred gesture event.
221 base::OneShotTimer<GestureEventQueue> debounce_deferring_timer_;
223 // Queue of events that have been deferred for debounce.
224 GestureQueue debouncing_deferral_queue_;
226 // Time window in which to debounce scroll/fling ends. Note that an interval
227 // of zero effectively disables debouncing.
228 base::TimeDelta debounce_interval_;
230 DISALLOW_COPY_AND_ASSIGN(GestureEventQueue);
233 } // namespace content
235 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURE_EVENT_QUEUE_H_