Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: content/browser/renderer_host/input/touch_event_queue.cc

Issue 23856016: Send touch cancel to renderer when scrolling starts (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix the issue that TouchCancel may never be sent Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/renderer_host/input/touch_event_queue.h" 5 #include "content/browser/renderer_host/input/touch_event_queue.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 10
11 namespace content { 11 namespace content {
12 12
13 typedef std::vector<TouchEventWithLatencyInfo> WebTouchEventWithLatencyList; 13 typedef std::vector<TouchEventWithLatencyInfo> WebTouchEventWithLatencyList;
14 14
15 // This class represents a single coalesced touch event. However, it also keeps 15 // This class represents a single coalesced touch event. However, it also keeps
16 // track of all the original touch-events that were coalesced into a single 16 // track of all the original touch-events that were coalesced into a single
17 // event. The coalesced event is forwarded to the renderer, while the original 17 // event. The coalesced event is forwarded to the renderer, while the original
18 // touch-events are sent to the Client (on ACK for the coalesced event) so that 18 // touch-events are sent to the Client (on ACK for the coalesced event) so that
19 // the Client receives the event with their original timestamp. 19 // the Client receives the event with their original timestamp.
20 class CoalescedWebTouchEvent { 20 class CoalescedWebTouchEvent {
21 public: 21 public:
22 explicit CoalescedWebTouchEvent(const TouchEventWithLatencyInfo& event) 22 explicit CoalescedWebTouchEvent(const TouchEventWithLatencyInfo& event)
23 : coalesced_event_(event) { 23 : coalesced_event_(event),
24 ignore_ack_(false),
25 has_been_sent_to_renderer_(false) {
24 events_.push_back(event); 26 events_.push_back(event);
25 TRACE_EVENT_ASYNC_BEGIN0( 27 TRACE_EVENT_ASYNC_BEGIN0(
26 "input", "TouchEventQueue::QueueEvent", this); 28 "input", "TouchEventQueue::QueueEvent", this);
27 } 29 }
28 30
29 ~CoalescedWebTouchEvent() { 31 ~CoalescedWebTouchEvent() {
30 TRACE_EVENT_ASYNC_END0( 32 TRACE_EVENT_ASYNC_END0(
31 "input", "TouchEventQueue::QueueEvent", this); 33 "input", "TouchEventQueue::QueueEvent", this);
32 } 34 }
33 35
34 // Coalesces the event with the existing event if possible. Returns whether 36 // Coalesces the event with the existing event if possible. Returns whether
35 // the event was coalesced. 37 // the event was coalesced.
36 bool CoalesceEventIfPossible( 38 bool CoalesceEventIfPossible(
37 const TouchEventWithLatencyInfo& event_with_latency) { 39 const TouchEventWithLatencyInfo& event_with_latency) {
38 if (coalesced_event_.event.type == WebKit::WebInputEvent::TouchMove && 40 if (coalesced_event_.event.type == WebKit::WebInputEvent::TouchMove &&
39 event_with_latency.event.type == WebKit::WebInputEvent::TouchMove && 41 event_with_latency.event.type == WebKit::WebInputEvent::TouchMove &&
40 coalesced_event_.event.modifiers == 42 coalesced_event_.event.modifiers ==
41 event_with_latency.event.modifiers && 43 event_with_latency.event.modifiers &&
42 coalesced_event_.event.touchesLength == 44 coalesced_event_.event.touchesLength ==
43 event_with_latency.event.touchesLength) { 45 event_with_latency.event.touchesLength) {
sadrul 2013/09/27 15:13:51 You should check that has_been_sent_to_renderer_ i
Yufeng Shen (Slow to review) 2013/09/30 20:43:14 Done.
44 TRACE_EVENT_INSTANT0( 46 TRACE_EVENT_INSTANT0(
45 "input", "TouchEventQueue::MoveCoalesced", TRACE_EVENT_SCOPE_THREAD); 47 "input", "TouchEventQueue::MoveCoalesced", TRACE_EVENT_SCOPE_THREAD);
46 events_.push_back(event_with_latency); 48 events_.push_back(event_with_latency);
47 // The WebTouchPoints include absolute position information. So it is 49 // The WebTouchPoints include absolute position information. So it is
48 // sufficient to simply replace the previous event with the new event. 50 // sufficient to simply replace the previous event with the new event.
49 // However, it is necessary to make sure that all the points have the 51 // However, it is necessary to make sure that all the points have the
50 // correct state, i.e. the touch-points that moved in the last event, but 52 // correct state, i.e. the touch-points that moved in the last event, but
51 // didn't change in the current event, will have Stationary state. It is 53 // didn't change in the current event, will have Stationary state. It is
52 // necessary to change them back to Moved state. 54 // necessary to change them back to Moved state.
53 const WebKit::WebTouchEvent last_event = coalesced_event_.event; 55 const WebKit::WebTouchEvent last_event = coalesced_event_.event;
(...skipping 18 matching lines...) Expand all
72 WebTouchEventWithLatencyList::const_iterator begin() const { 74 WebTouchEventWithLatencyList::const_iterator begin() const {
73 return events_.begin(); 75 return events_.begin();
74 } 76 }
75 77
76 WebTouchEventWithLatencyList::const_iterator end() const { 78 WebTouchEventWithLatencyList::const_iterator end() const {
77 return events_.end(); 79 return events_.end();
78 } 80 }
79 81
80 size_t size() const { return events_.size(); } 82 size_t size() const { return events_.size(); }
81 83
84 bool ignore_ack() const { return ignore_ack_; }
85 void set_ignore_ack(bool value) { ignore_ack_ = value; }
86
87 bool has_been_sent_to_renderer() const { return has_been_sent_to_renderer_; }
88 void set_has_been_sent_to_renderer(bool value) {
89 has_been_sent_to_renderer_ = value;
90 }
91
82 private: 92 private:
83 // This is the event that is forwarded to the renderer. 93 // This is the event that is forwarded to the renderer.
84 TouchEventWithLatencyInfo coalesced_event_; 94 TouchEventWithLatencyInfo coalesced_event_;
85 95
86 // This is the list of the original events that were coalesced. 96 // This is the list of the original events that were coalesced.
87 WebTouchEventWithLatencyList events_; 97 WebTouchEventWithLatencyList events_;
88 98
99 // If |ignore_ack_| is true, don't send this touch event to client
100 // when the event is acked.
101 bool ignore_ack_;
102
103 // Whether the touch event has been sent to renderer.
104 bool has_been_sent_to_renderer_;
105
89 DISALLOW_COPY_AND_ASSIGN(CoalescedWebTouchEvent); 106 DISALLOW_COPY_AND_ASSIGN(CoalescedWebTouchEvent);
90 }; 107 };
91 108
92 TouchEventQueue::TouchEventQueue(TouchEventQueueClient* client) 109 TouchEventQueue::TouchEventQueue(TouchEventQueueClient* client)
93 : client_(client), 110 : client_(client),
94 dispatching_touch_ack_(false), 111 dispatching_touch_ack_(NULL),
95 no_touch_move_to_renderer_(false) { 112 no_touch_to_renderer_(false) {
96 DCHECK(client); 113 DCHECK(client);
97 } 114 }
98 115
99 TouchEventQueue::~TouchEventQueue() { 116 TouchEventQueue::~TouchEventQueue() {
100 if (!touch_queue_.empty()) 117 if (!touch_queue_.empty())
101 STLDeleteElements(&touch_queue_); 118 STLDeleteElements(&touch_queue_);
102 } 119 }
103 120
104 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) { 121 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) {
105 // If the queueing of |event| was triggered by an ack dispatch, defer 122 // If the queueing of |event| was triggered by an ack dispatch, defer
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 PopTouchEventToClient(ack_result, latency_info); 168 PopTouchEventToClient(ack_result, latency_info);
152 TryForwardNextEventToRenderer(); 169 TryForwardNextEventToRenderer();
153 } 170 }
154 171
155 void TouchEventQueue::TryForwardNextEventToRenderer() { 172 void TouchEventQueue::TryForwardNextEventToRenderer() {
156 // If there are queued touch events, then try to forward them to the renderer 173 // If there are queued touch events, then try to forward them to the renderer
157 // immediately, or ACK the events back to the client if appropriate. 174 // immediately, or ACK the events back to the client if appropriate.
158 while (!touch_queue_.empty()) { 175 while (!touch_queue_.empty()) {
159 const TouchEventWithLatencyInfo& touch = 176 const TouchEventWithLatencyInfo& touch =
160 touch_queue_.front()->coalesced_event(); 177 touch_queue_.front()->coalesced_event();
178 if (touch_queue_.front()->has_been_sent_to_renderer())
jdduke (slow) 2013/09/30 19:21:51 You can remove this flag (or make it a DCHECK(!...
Yufeng Shen (Slow to review) 2013/09/30 20:43:14 removed.
179 break;
161 if (ShouldForwardToRenderer(touch.event)) { 180 if (ShouldForwardToRenderer(touch.event)) {
162 client_->SendTouchEventImmediately(touch); 181 client_->SendTouchEventImmediately(touch);
182 touch_queue_.front()->set_has_been_sent_to_renderer(true);
163 break; 183 break;
164 } 184 }
165 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, 185 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS,
166 ui::LatencyInfo()); 186 ui::LatencyInfo());
167 } 187 }
168 } 188 }
169 189
190 void TouchEventQueue::OnGestureScrollEvent(
191 const GestureEventWithLatencyInfo& gesture_event) {
192 WebKit::WebInputEvent::Type type = gesture_event.event.type;
193 if (type == WebKit::WebInputEvent::GestureScrollBegin) {
194 // We assume the scroll event are generated synchronously from
195 // dispatching a touch event ack, so that we can fake a cancel
196 // event that has the correct touch ids as the touch event that
197 // is being acked. If not, we don't do the touch-cancel optimization.
198 if (no_touch_to_renderer_ || !dispatching_touch_ack_)
199 return;
200 no_touch_to_renderer_ = true;
201 // Fake a TouchCancel to cancel the touch points of the touch event
202 // that is currently being acked.
203 TouchEventWithLatencyInfo cancel_event =
204 dispatching_touch_ack_->coalesced_event();
205 cancel_event.event.type = WebKit::WebInputEvent::TouchCancel;
206 for (size_t i = 0; i < cancel_event.event.touchesLength; i++)
207 cancel_event.event.touches[i].state =
208 WebKit::WebTouchPoint::StateCancelled;
209 CoalescedWebTouchEvent* coalesced_cancel_event =
210 new CoalescedWebTouchEvent(cancel_event);
211 // Ignore the ack of the touch cancel so when it is acked, it won't get
212 // sent to gesture recognizer.
213 coalesced_cancel_event->set_ignore_ack(true);
214 // If the touch event queue is empty, or the first touch event in the queue
215 // has not been sent to renderer (i.e. not waiting for ack), we insert the
216 // touch cancel at the beginning of the queue to be the first to send.
217 if (touch_queue_.empty() ||
218 !touch_queue_.front()->has_been_sent_to_renderer()) {
219 touch_queue_.push_front(coalesced_cancel_event);
220 } else {
221 // If there there is touch event already sent to the renderer and waiting
222 // for ack, we insert the touch cancel after that touch event.
sadrul 2013/09/27 15:13:51 + "This cancel event will be dispatched to the ren
Yufeng Shen (Slow to review) 2013/09/30 20:43:14 see the comments below.
223 TouchQueue::iterator it = touch_queue_.begin();
224 it++;
225 touch_queue_.insert(it, coalesced_cancel_event);
226 }
227 TryForwardNextEventToRenderer();
jdduke (slow) 2013/09/30 19:21:51 I believe you can remove this call. |dispatching_
sadrul 2013/09/30 20:33:24 Client::OnTouchEventAck can cause another touch-ev
Yufeng Shen (Slow to review) 2013/09/30 20:43:14 Right. So all the complication comes from that I w
sadrul 2013/10/01 11:32:45 Thanks for the detailed explanation. This makes se
228 } else if (type == WebKit::WebInputEvent::GestureScrollEnd ||
229 type == WebKit::WebInputEvent::GestureFlingStart) {
230 no_touch_to_renderer_ = false;
231 }
232 }
233
170 void TouchEventQueue::FlushQueue() { 234 void TouchEventQueue::FlushQueue() {
171 DCHECK(!dispatching_touch_ack_); 235 DCHECK(!dispatching_touch_ack_);
172 while (!touch_queue_.empty()) 236 while (!touch_queue_.empty())
173 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, 237 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED,
174 ui::LatencyInfo()); 238 ui::LatencyInfo());
175 } 239 }
176 240
177 size_t TouchEventQueue::GetQueueSize() const { 241 size_t TouchEventQueue::GetQueueSize() const {
178 return touch_queue_.size(); 242 return touch_queue_.size();
179 } 243 }
180 244
181 const TouchEventWithLatencyInfo& TouchEventQueue::GetLatestEvent() const { 245 const TouchEventWithLatencyInfo& TouchEventQueue::GetLatestEvent() const {
182 return touch_queue_.back()->coalesced_event(); 246 return touch_queue_.back()->coalesced_event();
183 } 247 }
184 248
185 void TouchEventQueue::PopTouchEventToClient( 249 void TouchEventQueue::PopTouchEventToClient(
186 InputEventAckState ack_result, 250 InputEventAckState ack_result,
187 const ui::LatencyInfo& renderer_latency_info) { 251 const ui::LatencyInfo& renderer_latency_info) {
188 if (touch_queue_.empty()) 252 if (touch_queue_.empty())
189 return; 253 return;
190 scoped_ptr<CoalescedWebTouchEvent> acked_event(touch_queue_.front()); 254 scoped_ptr<CoalescedWebTouchEvent> acked_event(touch_queue_.front());
191 touch_queue_.pop_front(); 255 touch_queue_.pop_front();
192 256
257 if (acked_event->ignore_ack())
258 return;
259
193 // Note that acking the touch-event may result in multiple gestures being sent 260 // Note that acking the touch-event may result in multiple gestures being sent
194 // to the renderer, or touch-events being queued. 261 // to the renderer, or touch-events being queued.
195 base::AutoReset<bool> dispatching_touch_ack(&dispatching_touch_ack_, true); 262 base::AutoReset<CoalescedWebTouchEvent*>
263 dispatching_touch_ack(&dispatching_touch_ack_, acked_event.get());
sadrul 2013/09/27 15:13:51 Hm, this would only work if the acked touch-event
jdduke (slow) 2013/09/30 19:21:51 Yup, that's generally the case on Android, and oth
196 264
197 base::TimeTicks now = base::TimeTicks::HighResNow(); 265 base::TimeTicks now = base::TimeTicks::HighResNow();
198 for (WebTouchEventWithLatencyList::const_iterator iter = acked_event->begin(), 266 for (WebTouchEventWithLatencyList::const_iterator iter = acked_event->begin(),
199 end = acked_event->end(); 267 end = acked_event->end();
200 iter != end; ++iter) { 268 iter != end; ++iter) {
201 ui::LatencyInfo* latency = const_cast<ui::LatencyInfo*>(&(iter->latency)); 269 ui::LatencyInfo* latency = const_cast<ui::LatencyInfo*>(&(iter->latency));
202 latency->AddNewLatencyFrom(renderer_latency_info); 270 latency->AddNewLatencyFrom(renderer_latency_info);
203 latency->AddLatencyNumberWithTimestamp( 271 latency->AddLatencyNumberWithTimestamp(
204 ui::INPUT_EVENT_LATENCY_ACKED_COMPONENT, 0, 0, now, 1); 272 ui::INPUT_EVENT_LATENCY_ACKED_COMPONENT, 0, 0, now, 1);
205 client_->OnTouchEventAck((*iter), ack_result); 273 client_->OnTouchEventAck((*iter), ack_result);
206 } 274 }
207 } 275 }
208 276
209 bool TouchEventQueue::ShouldForwardToRenderer( 277 bool TouchEventQueue::ShouldForwardToRenderer(
210 const WebKit::WebTouchEvent& event) const { 278 const WebKit::WebTouchEvent& event) const {
279 if (no_touch_to_renderer_ &&
280 event.type != WebKit::WebInputEvent::TouchCancel)
281 return false;
282
211 // Touch press events should always be forwarded to the renderer. 283 // Touch press events should always be forwarded to the renderer.
212 if (event.type == WebKit::WebInputEvent::TouchStart) 284 if (event.type == WebKit::WebInputEvent::TouchStart)
213 return true; 285 return true;
214 286
215 if (event.type == WebKit::WebInputEvent::TouchMove &&
216 no_touch_move_to_renderer_)
217 return false;
218
219 for (unsigned int i = 0; i < event.touchesLength; ++i) { 287 for (unsigned int i = 0; i < event.touchesLength; ++i) {
220 const WebKit::WebTouchPoint& point = event.touches[i]; 288 const WebKit::WebTouchPoint& point = event.touches[i];
221 // If a point has been stationary, then don't take it into account. 289 // If a point has been stationary, then don't take it into account.
222 if (point.state == WebKit::WebTouchPoint::StateStationary) 290 if (point.state == WebKit::WebTouchPoint::StateStationary)
223 continue; 291 continue;
224 292
225 if (touch_ack_states_.count(point.id) > 0) { 293 if (touch_ack_states_.count(point.id) > 0) {
226 if (touch_ack_states_.find(point.id)->second != 294 if (touch_ack_states_.find(point.id)->second !=
227 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS) 295 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS)
228 return true; 296 return true;
229 } else { 297 } else {
230 // If the ACK status of a point is unknown, then the event should be 298 // If the ACK status of a point is unknown, then the event should be
231 // forwarded to the renderer. 299 // forwarded to the renderer.
232 return true; 300 return true;
233 } 301 }
234 } 302 }
235 303
236 return false; 304 return false;
237 } 305 }
238 306
239 } // namespace content 307 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698