| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) { | 103 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) { |
| 104 // If the queueing of |event| was triggered by an ack dispatch, defer | 104 // If the queueing of |event| was triggered by an ack dispatch, defer |
| 105 // processing the event until the dispatch has finished. | 105 // processing the event until the dispatch has finished. |
| 106 if (touch_queue_.empty() && !dispatching_touch_ack_) { | 106 if (touch_queue_.empty() && !dispatching_touch_ack_) { |
| 107 // There is no touch event in the queue. Forward it to the renderer | 107 // There is no touch event in the queue. Forward it to the renderer |
| 108 // immediately. | 108 // immediately. |
| 109 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); | 109 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); |
| 110 if (ShouldForwardToRenderer(event.event)) | 110 if (ShouldForwardToRenderer(event.event)) |
| 111 client_->SendTouchEventImmediately(event); | 111 client_->SendTouchEventImmediately(event); |
| 112 else | 112 else |
| 113 PopTouchEventWithAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); | 113 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, |
| 114 ui::LatencyInfo()); |
| 114 return; | 115 return; |
| 115 } | 116 } |
| 116 | 117 |
| 117 // If the last queued touch-event was a touch-move, and the current event is | 118 // If the last queued touch-event was a touch-move, and the current event is |
| 118 // also a touch-move, then the events can be coalesced into a single event. | 119 // also a touch-move, then the events can be coalesced into a single event. |
| 119 if (touch_queue_.size() > 1) { | 120 if (touch_queue_.size() > 1) { |
| 120 CoalescedWebTouchEvent* last_event = touch_queue_.back(); | 121 CoalescedWebTouchEvent* last_event = touch_queue_.back(); |
| 121 if (last_event->CoalesceEventIfPossible(event)) | 122 if (last_event->CoalesceEventIfPossible(event)) |
| 122 return; | 123 return; |
| 123 } | 124 } |
| 124 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); | 125 touch_queue_.push_back(new CoalescedWebTouchEvent(event)); |
| 125 } | 126 } |
| 126 | 127 |
| 127 void TouchEventQueue::ProcessTouchAck(InputEventAckState ack_result) { | 128 void TouchEventQueue::ProcessTouchAck(InputEventAckState ack_result, |
| 129 const ui::LatencyInfo& latency_info) { |
| 128 DCHECK(!dispatching_touch_ack_); | 130 DCHECK(!dispatching_touch_ack_); |
| 129 if (touch_queue_.empty()) | 131 if (touch_queue_.empty()) |
| 130 return; | 132 return; |
| 131 | 133 |
| 132 // Update the ACK status for each touch point in the ACKed event. | 134 // Update the ACK status for each touch point in the ACKed event. |
| 133 const WebKit::WebTouchEvent& event = | 135 const WebKit::WebTouchEvent& event = |
| 134 touch_queue_.front()->coalesced_event().event; | 136 touch_queue_.front()->coalesced_event().event; |
| 135 if (event.type == WebKit::WebInputEvent::TouchEnd || | 137 if (event.type == WebKit::WebInputEvent::TouchEnd || |
| 136 event.type == WebKit::WebInputEvent::TouchCancel) { | 138 event.type == WebKit::WebInputEvent::TouchCancel) { |
| 137 // The points have been released. Erase the ACK states. | 139 // The points have been released. Erase the ACK states. |
| 138 for (unsigned i = 0; i < event.touchesLength; ++i) { | 140 for (unsigned i = 0; i < event.touchesLength; ++i) { |
| 139 const WebKit::WebTouchPoint& point = event.touches[i]; | 141 const WebKit::WebTouchPoint& point = event.touches[i]; |
| 140 if (point.state == WebKit::WebTouchPoint::StateReleased || | 142 if (point.state == WebKit::WebTouchPoint::StateReleased || |
| 141 point.state == WebKit::WebTouchPoint::StateCancelled) | 143 point.state == WebKit::WebTouchPoint::StateCancelled) |
| 142 touch_ack_states_.erase(point.id); | 144 touch_ack_states_.erase(point.id); |
| 143 } | 145 } |
| 144 } else if (event.type == WebKit::WebInputEvent::TouchStart) { | 146 } else if (event.type == WebKit::WebInputEvent::TouchStart) { |
| 145 for (unsigned i = 0; i < event.touchesLength; ++i) { | 147 for (unsigned i = 0; i < event.touchesLength; ++i) { |
| 146 const WebKit::WebTouchPoint& point = event.touches[i]; | 148 const WebKit::WebTouchPoint& point = event.touches[i]; |
| 147 if (point.state == WebKit::WebTouchPoint::StatePressed) | 149 if (point.state == WebKit::WebTouchPoint::StatePressed) |
| 148 touch_ack_states_[point.id] = ack_result; | 150 touch_ack_states_[point.id] = ack_result; |
| 149 } | 151 } |
| 150 } | 152 } |
| 151 | 153 |
| 152 PopTouchEventWithAck(ack_result); | 154 PopTouchEventToClient(ack_result, latency_info); |
| 153 | 155 |
| 154 // If there are queued touch events, then try to forward them to the renderer | 156 // If there are queued touch events, then try to forward them to the renderer |
| 155 // immediately, or ACK the events back to the client if appropriate. | 157 // immediately, or ACK the events back to the client if appropriate. |
| 156 while (!touch_queue_.empty()) { | 158 while (!touch_queue_.empty()) { |
| 157 const TouchEventWithLatencyInfo& touch = | 159 const TouchEventWithLatencyInfo& touch = |
| 158 touch_queue_.front()->coalesced_event(); | 160 touch_queue_.front()->coalesced_event(); |
| 159 if (ShouldForwardToRenderer(touch.event)) { | 161 if (ShouldForwardToRenderer(touch.event)) { |
| 160 client_->SendTouchEventImmediately(touch); | 162 client_->SendTouchEventImmediately(touch); |
| 161 break; | 163 break; |
| 162 } | 164 } |
| 163 PopTouchEventWithAck(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS); | 165 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS, |
| 166 ui::LatencyInfo()); |
| 164 } | 167 } |
| 165 } | 168 } |
| 166 | 169 |
| 167 void TouchEventQueue::FlushQueue() { | 170 void TouchEventQueue::FlushQueue() { |
| 168 DCHECK(!dispatching_touch_ack_); | 171 DCHECK(!dispatching_touch_ack_); |
| 169 while (!touch_queue_.empty()) | 172 while (!touch_queue_.empty()) |
| 170 PopTouchEventWithAck(INPUT_EVENT_ACK_STATE_NOT_CONSUMED); | 173 PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, |
| 174 ui::LatencyInfo()); |
| 171 } | 175 } |
| 172 | 176 |
| 173 size_t TouchEventQueue::GetQueueSize() const { | 177 size_t TouchEventQueue::GetQueueSize() const { |
| 174 return touch_queue_.size(); | 178 return touch_queue_.size(); |
| 175 } | 179 } |
| 176 | 180 |
| 177 const TouchEventWithLatencyInfo& TouchEventQueue::GetLatestEvent() const { | 181 const TouchEventWithLatencyInfo& TouchEventQueue::GetLatestEvent() const { |
| 178 return touch_queue_.back()->coalesced_event(); | 182 return touch_queue_.back()->coalesced_event(); |
| 179 } | 183 } |
| 180 | 184 |
| 181 void TouchEventQueue::PopTouchEventWithAck(InputEventAckState ack_result) { | 185 void TouchEventQueue::PopTouchEventToClient( |
| 186 InputEventAckState ack_result, |
| 187 const ui::LatencyInfo& renderer_latency_info) { |
| 182 if (touch_queue_.empty()) | 188 if (touch_queue_.empty()) |
| 183 return; | 189 return; |
| 184 scoped_ptr<CoalescedWebTouchEvent> acked_event(touch_queue_.front()); | 190 scoped_ptr<CoalescedWebTouchEvent> acked_event(touch_queue_.front()); |
| 185 touch_queue_.pop_front(); | 191 touch_queue_.pop_front(); |
| 186 | 192 |
| 187 // Note that acking the touch-event may result in multiple gestures being sent | 193 // Note that acking the touch-event may result in multiple gestures being sent |
| 188 // to the renderer, or touch-events being queued. | 194 // to the renderer, or touch-events being queued. |
| 189 base::AutoReset<bool> dispatching_touch_ack(&dispatching_touch_ack_, true); | 195 base::AutoReset<bool> dispatching_touch_ack(&dispatching_touch_ack_, true); |
| 190 | 196 |
| 191 base::TimeTicks now = base::TimeTicks::HighResNow(); | 197 base::TimeTicks now = base::TimeTicks::HighResNow(); |
| 192 for (WebTouchEventWithLatencyList::const_iterator iter = acked_event->begin(), | 198 for (WebTouchEventWithLatencyList::const_iterator iter = acked_event->begin(), |
| 193 end = acked_event->end(); | 199 end = acked_event->end(); |
| 194 iter != end; ++iter) { | 200 iter != end; ++iter) { |
| 195 ui::LatencyInfo* latency = const_cast<ui::LatencyInfo*>(&(iter->latency)); | 201 ui::LatencyInfo* latency = const_cast<ui::LatencyInfo*>(&(iter->latency)); |
| 202 latency->AddNewLatencyFrom(renderer_latency_info); |
| 196 latency->AddLatencyNumberWithTimestamp( | 203 latency->AddLatencyNumberWithTimestamp( |
| 197 ui::INPUT_EVENT_LATENCY_ACKED_COMPONENT, 0, 0, now, 1); | 204 ui::INPUT_EVENT_LATENCY_ACKED_COMPONENT, 0, 0, now, 1); |
| 198 client_->OnTouchEventAck((*iter), ack_result); | 205 client_->OnTouchEventAck((*iter), ack_result); |
| 199 } | 206 } |
| 200 } | 207 } |
| 201 | 208 |
| 202 bool TouchEventQueue::ShouldForwardToRenderer( | 209 bool TouchEventQueue::ShouldForwardToRenderer( |
| 203 const WebKit::WebTouchEvent& event) const { | 210 const WebKit::WebTouchEvent& event) const { |
| 204 // Touch press events should always be forwarded to the renderer. | 211 // Touch press events should always be forwarded to the renderer. |
| 205 if (event.type == WebKit::WebInputEvent::TouchStart) | 212 if (event.type == WebKit::WebInputEvent::TouchStart) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 219 // If the ACK status of a point is unknown, then the event should be | 226 // If the ACK status of a point is unknown, then the event should be |
| 220 // forwarded to the renderer. | 227 // forwarded to the renderer. |
| 221 return true; | 228 return true; |
| 222 } | 229 } |
| 223 } | 230 } |
| 224 | 231 |
| 225 return false; | 232 return false; |
| 226 } | 233 } |
| 227 | 234 |
| 228 } // namespace content | 235 } // namespace content |
| OLD | NEW |