Chromium Code Reviews| OLD | NEW | 
|---|---|
| 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 | 
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 | 85 | 
| 86 // This is the list of the original events that were coalesced. | 86 // This is the list of the original events that were coalesced. | 
| 87 WebTouchEventWithLatencyList events_; | 87 WebTouchEventWithLatencyList events_; | 
| 88 | 88 | 
| 89 DISALLOW_COPY_AND_ASSIGN(CoalescedWebTouchEvent); | 89 DISALLOW_COPY_AND_ASSIGN(CoalescedWebTouchEvent); | 
| 90 }; | 90 }; | 
| 91 | 91 | 
| 92 TouchEventQueue::TouchEventQueue(TouchEventQueueClient* client) | 92 TouchEventQueue::TouchEventQueue(TouchEventQueueClient* client) | 
| 93 : client_(client), | 93 : client_(client), | 
| 94 dispatching_touch_ack_(false), | 94 dispatching_touch_ack_(false), | 
| 95 no_touch_move_to_renderer_(false) { | 95 no_touch_to_renderer_(false), | 
| 96 synthesized_cancel_event_(NULL) { | |
| 96 DCHECK(client); | 97 DCHECK(client); | 
| 97 } | 98 } | 
| 98 | 99 | 
| 99 TouchEventQueue::~TouchEventQueue() { | 100 TouchEventQueue::~TouchEventQueue() { | 
| 100 if (!touch_queue_.empty()) | 101 if (!touch_queue_.empty()) | 
| 101 STLDeleteElements(&touch_queue_); | 102 STLDeleteElements(&touch_queue_); | 
| 102 } | 103 } | 
| 103 | 104 | 
| 104 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) { | 105 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) { | 
| 106 latest_event_ = event; | |
| 105 // If the queueing of |event| was triggered by an ack dispatch, defer | 107 // If the queueing of |event| was triggered by an ack dispatch, defer | 
| 106 // processing the event until the dispatch has finished. | 108 // processing the event until the dispatch has finished. | 
| 107 if (touch_queue_.empty() && !dispatching_touch_ack_) { | 109 if (touch_queue_.empty() && !dispatching_touch_ack_) { | 
| 108 // There is no touch event in the queue. Forward it to the renderer | 110 // There is no touch event in the queue. Forward it to the renderer | 
| 109 // immediately. | 111 // immediately. | 
| 110 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); | 112 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); | 
| 111 if (ShouldForwardToRenderer(event.event)) | 113 if (ShouldForwardToRenderer(event.event)) | 
| 112 client_->SendTouchEventImmediately(event); | 114 client_->SendTouchEventImmediately(event); | 
| 113 else | 115 else | 
| 114 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, | 116 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, | 
| 115 ui::LatencyInfo()); | 117 ui::LatencyInfo()); | 
| 116 return; | 118 return; | 
| 117 } | 119 } | 
| 118 | 120 | 
| 119 // If the last queued touch-event was a touch-move, and the current event is | 121 // If the last queued touch-event was a touch-move, and the current event is | 
| 120 // also a touch-move, then the events can be coalesced into a single event. | 122 // also a touch-move, then the events can be coalesced into a single event. | 
| 121 if (touch_queue_.size() > 1) { | 123 if (touch_queue_.size() > 1) { | 
| 122 CoalescedWebTouchEvent* last_event = touch_queue_.back(); | 124 CoalescedWebTouchEvent* last_event = touch_queue_.back(); | 
| 123 if (last_event->CoalesceEventIfPossible(event)) | 125 if (last_event->CoalesceEventIfPossible(event)) { | 
| 126 latest_event_ = last_event->coalesced_event(); | |
| 124 return; | 127 return; | 
| 128 } | |
| 125 } | 129 } | 
| 126 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); | 130 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); | 
| 127 } | 131 } | 
| 128 | 132 | 
| 129 void TouchEventQueue::ProcessTouchAck(InputEventAckState ack_result, | 133 void TouchEventQueue::ProcessTouchAck(InputEventAckState ack_result, | 
| 130 const ui::LatencyInfo& latency_info) { | 134 const ui::LatencyInfo& latency_info) { | 
| 131 DCHECK(!dispatching_touch_ack_); | 135 DCHECK(!dispatching_touch_ack_); | 
| 132 if (touch_queue_.empty()) | 136 if (touch_queue_.empty()) | 
| 133 return; | 137 return; | 
| 134 | 138 | 
| (...skipping 26 matching lines...) Expand all Loading... | |
| 161 touch_queue_.front()->coalesced_event(); | 165 touch_queue_.front()->coalesced_event(); | 
| 162 if (ShouldForwardToRenderer(touch.event)) { | 166 if (ShouldForwardToRenderer(touch.event)) { | 
| 163 client_->SendTouchEventImmediately(touch); | 167 client_->SendTouchEventImmediately(touch); | 
| 164 break; | 168 break; | 
| 165 } | 169 } | 
| 166 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, | 170 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, | 
| 167 ui::LatencyInfo()); | 171 ui::LatencyInfo()); | 
| 168 } | 172 } | 
| 169 } | 173 } | 
| 170 | 174 | 
| 175 void TouchEventQueue::OnGestureScrollEvent( | |
| 176 const GestureEventWithLatencyInfo& gesture_event) { | |
| 177 if (gesture_event.event.type == WebKit::WebInputEvent::GestureScrollBegin) { | |
| 178 if (no_touch_to_renderer_) | |
| 179 return; | |
| 180 no_touch_to_renderer_ = true; | |
| 181 TouchEventWithLatencyInfo cancel_event = GetLatestEvent(); | |
| 182 cancel_event.event.type = WebKit::WebInputEvent::TouchCancel; | |
| 183 for (size_t i = 0; i < cancel_event.event.touchesLength; i++) | |
| 184 cancel_event.event.touches[i].state = | |
| 185 WebKit::WebTouchPoint::StateCancelled; | |
| 186 DCHECK(!synthesized_cancel_event_); | |
| 187 synthesized_cancel_event_ = new CoalescedWebTouchEvent(cancel_event); | |
| 188 touch_queue_.push_front(synthesized_cancel_event_); | |
| 
 
sadrul
2013/09/23 16:17:33
When is this cancel event actually dispatched to t
 
jdduke (slow)
2013/09/23 16:34:34
Inserting the cancel at the front... The only way
 
Yufeng Shen (Slow to review)
2013/09/23 20:49:34
How about :
1) if the touch_queue_ is empty, we en
 
 | |
| 189 } | |
| 190 | |
| 191 if (gesture_event.event.type == WebKit::WebInputEvent::GestureScrollEnd || | |
| 
 
sadrul
2013/09/23 16:17:33
else if
 
 | |
| 192 gesture_event.event.type == WebKit::WebInputEvent::GestureFlingStart) { | |
| 193 no_touch_to_renderer_ = false; | |
| 194 } | |
| 195 } | |
| 196 | |
| 171 void TouchEventQueue::FlushQueue() { | 197 void TouchEventQueue::FlushQueue() { | 
| 172 DCHECK(!dispatching_touch_ack_); | 198 DCHECK(!dispatching_touch_ack_); | 
| 173 while (!touch_queue_.empty()) | 199 while (!touch_queue_.empty()) | 
| 174 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, | 200 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, | 
| 175 ui::LatencyInfo()); | 201 ui::LatencyInfo()); | 
| 176 } | 202 } | 
| 177 | 203 | 
| 178 size_t TouchEventQueue::GetQueueSize() const { | 204 size_t TouchEventQueue::GetQueueSize() const { | 
| 179 return touch_queue_.size(); | 205 return touch_queue_.size(); | 
| 180 } | 206 } | 
| 181 | 207 | 
| 182 const TouchEventWithLatencyInfo& TouchEventQueue::GetLatestEvent() const { | 208 const TouchEventWithLatencyInfo& TouchEventQueue::GetLatestEvent() const { | 
| 183 return touch_queue_.back()->coalesced_event(); | 209 return latest_event_; | 
| 184 } | 210 } | 
| 185 | 211 | 
| 186 void TouchEventQueue::PopTouchEventToClient( | 212 void TouchEventQueue::PopTouchEventToClient( | 
| 187 InputEventAckState ack_result, | 213 InputEventAckState ack_result, | 
| 188 const ui::LatencyInfo& renderer_latency_info) { | 214 const ui::LatencyInfo& renderer_latency_info) { | 
| 189 if (touch_queue_.empty()) | 215 if (touch_queue_.empty()) | 
| 190 return; | 216 return; | 
| 191 scoped_ptr<CoalescedWebTouchEvent> acked_event(touch_queue_.front()); | 217 scoped_ptr<CoalescedWebTouchEvent> acked_event(touch_queue_.front()); | 
| 192 touch_queue_.pop_front(); | 218 touch_queue_.pop_front(); | 
| 193 | 219 | 
| 220 // Don't send the synthesized touch cancel to view. | |
| 221 if (acked_event.get() == synthesized_cancel_event_) { | |
| 222 synthesized_cancel_event_ = NULL; | |
| 223 return; | |
| 224 } | |
| 225 | |
| 194 // Note that acking the touch-event may result in multiple gestures being sent | 226 // Note that acking the touch-event may result in multiple gestures being sent | 
| 195 // to the renderer, or touch-events being queued. | 227 // to the renderer, or touch-events being queued. | 
| 196 base::AutoReset<bool> dispatching_touch_ack(&dispatching_touch_ack_, true); | 228 base::AutoReset<bool> dispatching_touch_ack(&dispatching_touch_ack_, true); | 
| 197 | 229 | 
| 198 base::TimeTicks now = base::TimeTicks::HighResNow(); | 230 base::TimeTicks now = base::TimeTicks::HighResNow(); | 
| 199 for (WebTouchEventWithLatencyList::const_iterator iter = acked_event->begin(), | 231 for (WebTouchEventWithLatencyList::const_iterator iter = acked_event->begin(), | 
| 200 end = acked_event->end(); | 232 end = acked_event->end(); | 
| 201 iter != end; ++iter) { | 233 iter != end; ++iter) { | 
| 202 ui::LatencyInfo* latency = const_cast<ui::LatencyInfo*>(&(iter->latency)); | 234 ui::LatencyInfo* latency = const_cast<ui::LatencyInfo*>(&(iter->latency)); | 
| 203 latency->AddNewLatencyFrom(renderer_latency_info); | 235 latency->AddNewLatencyFrom(renderer_latency_info); | 
| 204 latency->AddLatencyNumberWithTimestamp( | 236 latency->AddLatencyNumberWithTimestamp( | 
| 205 ui::INPUT_EVENT_LATENCY_ACKED_COMPONENT, 0, 0, now, 1); | 237 ui::INPUT_EVENT_LATENCY_ACKED_COMPONENT, 0, 0, now, 1); | 
| 206 client_->OnTouchEventAck((*iter), ack_result); | 238 client_->OnTouchEventAck((*iter), ack_result); | 
| 207 } | 239 } | 
| 208 } | 240 } | 
| 209 | 241 | 
| 210 bool TouchEventQueue::ShouldForwardToRenderer( | 242 bool TouchEventQueue::ShouldForwardToRenderer( | 
| 211 const WebKit::WebTouchEvent& event) const { | 243 const WebKit::WebTouchEvent& event) const { | 
| 244 if (no_touch_to_renderer_ && | |
| 245 event.type != WebKit::WebInputEvent::TouchCancel) | |
| 246 return false; | |
| 247 | |
| 212 // Touch press events should always be forwarded to the renderer. | 248 // Touch press events should always be forwarded to the renderer. | 
| 213 if (event.type == WebKit::WebInputEvent::TouchStart) | 249 if (event.type == WebKit::WebInputEvent::TouchStart) | 
| 214 return true; | 250 return true; | 
| 215 | 251 | 
| 216 if (event.type == WebKit::WebInputEvent::TouchMove && | |
| 217 no_touch_move_to_renderer_) | |
| 218 return false; | |
| 219 | |
| 220 for (unsigned int i = 0; i < event.touchesLength; ++i) { | 252 for (unsigned int i = 0; i < event.touchesLength; ++i) { | 
| 221 const WebKit::WebTouchPoint& point = event.touches[i]; | 253 const WebKit::WebTouchPoint& point = event.touches[i]; | 
| 222 // If a point has been stationary, then don't take it into account. | 254 // If a point has been stationary, then don't take it into account. | 
| 223 if (point.state == WebKit::WebTouchPoint::StateStationary) | 255 if (point.state == WebKit::WebTouchPoint::StateStationary) | 
| 224 continue; | 256 continue; | 
| 225 | 257 | 
| 226 if (touch_ack_states_.count(point.id) > 0) { | 258 if (touch_ack_states_.count(point.id) > 0) { | 
| 227 if (touch_ack_states_.find(point.id)->second != | 259 if (touch_ack_states_.find(point.id)->second != | 
| 228 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS) | 260 INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS) | 
| 229 return true; | 261 return true; | 
| 230 } else { | 262 } else { | 
| 231 // If the ACK status of a point is unknown, then the event should be | 263 // If the ACK status of a point is unknown, then the event should be | 
| 232 // forwarded to the renderer. | 264 // forwarded to the renderer. | 
| 233 return true; | 265 return true; | 
| 234 } | 266 } | 
| 235 } | 267 } | 
| 236 | 268 | 
| 237 return false; | 269 return false; | 
| 238 } | 270 } | 
| 239 | 271 | 
| 240 } // namespace content | 272 } // namespace content | 
| OLD | NEW |