- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / input / touch_event_queue.cc
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.
4
5 #include "content/browser/renderer_host/input/touch_event_queue.h"
6
7 #include "base/auto_reset.h"
8 #include "base/debug/trace_event.h"
9 #include "base/stl_util.h"
10
11 namespace content {
12
13 typedef std::vector<TouchEventWithLatencyInfo> WebTouchEventWithLatencyList;
14
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
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
19 // the Client receives the event with their original timestamp.
20 class CoalescedWebTouchEvent {
21  public:
22   explicit CoalescedWebTouchEvent(const TouchEventWithLatencyInfo& event)
23       : coalesced_event_(event),
24         ignore_ack_(false) {
25     events_.push_back(event);
26     TRACE_EVENT_ASYNC_BEGIN0(
27         "input", "TouchEventQueue::QueueEvent", this);
28   }
29
30   ~CoalescedWebTouchEvent() {
31     TRACE_EVENT_ASYNC_END0(
32         "input", "TouchEventQueue::QueueEvent", this);
33   }
34
35   // Coalesces the event with the existing event if possible. Returns whether
36   // the event was coalesced.
37   bool CoalesceEventIfPossible(
38       const TouchEventWithLatencyInfo& event_with_latency) {
39     if (ignore_ack_)
40       return false;
41
42     if (!coalesced_event_.CanCoalesceWith(event_with_latency))
43       return false;
44
45     TRACE_EVENT_INSTANT0(
46         "input", "TouchEventQueue::MoveCoalesced", TRACE_EVENT_SCOPE_THREAD);
47     coalesced_event_.CoalesceWith(event_with_latency);
48     events_.push_back(event_with_latency);
49     return true;
50   }
51
52   const TouchEventWithLatencyInfo& coalesced_event() const {
53     return coalesced_event_;
54   }
55
56   WebTouchEventWithLatencyList::iterator begin() {
57     return events_.begin();
58   }
59
60   WebTouchEventWithLatencyList::iterator end() {
61     return events_.end();
62   }
63
64   size_t size() const { return events_.size(); }
65
66   bool ignore_ack() const { return ignore_ack_; }
67   void set_ignore_ack(bool value) { ignore_ack_ = value; }
68
69  private:
70   // This is the event that is forwarded to the renderer.
71   TouchEventWithLatencyInfo coalesced_event_;
72
73   // This is the list of the original events that were coalesced.
74   WebTouchEventWithLatencyList events_;
75
76   // If |ignore_ack_| is true, don't send this touch event to client
77   // when the event is acked.
78   bool ignore_ack_;
79
80   DISALLOW_COPY_AND_ASSIGN(CoalescedWebTouchEvent);
81 };
82
83 TouchEventQueue::TouchEventQueue(TouchEventQueueClient* client)
84     : client_(client),
85       dispatching_touch_ack_(NULL),
86       no_touch_to_renderer_(false) {
87   DCHECK(client);
88 }
89
90 TouchEventQueue::~TouchEventQueue() {
91   if (!touch_queue_.empty())
92     STLDeleteElements(&touch_queue_);
93 }
94
95 void TouchEventQueue::QueueEvent(const TouchEventWithLatencyInfo& event) {
96   // If the queueing of |event| was triggered by an ack dispatch, defer
97   // processing the event until the dispatch has finished.
98   if (touch_queue_.empty() && !dispatching_touch_ack_) {
99     // There is no touch event in the queue. Forward it to the renderer
100     // immediately.
101     touch_queue_.push_back(new CoalescedWebTouchEvent(event));
102     TryForwardNextEventToRenderer();
103     return;
104   }
105
106   // If the last queued touch-event was a touch-move, and the current event is
107   // also a touch-move, then the events can be coalesced into a single event.
108   if (touch_queue_.size() > 1) {
109     CoalescedWebTouchEvent* last_event = touch_queue_.back();
110     if (last_event->CoalesceEventIfPossible(event))
111       return;
112   }
113   touch_queue_.push_back(new CoalescedWebTouchEvent(event));
114 }
115
116 void TouchEventQueue::ProcessTouchAck(InputEventAckState ack_result,
117                                       const ui::LatencyInfo& latency_info) {
118   DCHECK(!dispatching_touch_ack_);
119   if (touch_queue_.empty())
120     return;
121
122   // Update the ACK status for each touch point in the ACKed event.
123   const WebKit::WebTouchEvent& event =
124       touch_queue_.front()->coalesced_event().event;
125   if (event.type == WebKit::WebInputEvent::TouchEnd ||
126       event.type == WebKit::WebInputEvent::TouchCancel) {
127     // The points have been released. Erase the ACK states.
128     for (unsigned i = 0; i < event.touchesLength; ++i) {
129       const WebKit::WebTouchPoint& point = event.touches[i];
130       if (point.state == WebKit::WebTouchPoint::StateReleased ||
131           point.state == WebKit::WebTouchPoint::StateCancelled)
132         touch_ack_states_.erase(point.id);
133     }
134   } else if (event.type == WebKit::WebInputEvent::TouchStart) {
135     for (unsigned i = 0; i < event.touchesLength; ++i) {
136       const WebKit::WebTouchPoint& point = event.touches[i];
137       if (point.state == WebKit::WebTouchPoint::StatePressed)
138         touch_ack_states_[point.id] = ack_result;
139     }
140   }
141
142   PopTouchEventToClient(ack_result, latency_info);
143   TryForwardNextEventToRenderer();
144 }
145
146 void TouchEventQueue::TryForwardNextEventToRenderer() {
147   DCHECK(!dispatching_touch_ack_);
148   // If there are queued touch events, then try to forward them to the renderer
149   // immediately, or ACK the events back to the client if appropriate.
150   while (!touch_queue_.empty()) {
151     const TouchEventWithLatencyInfo& touch =
152         touch_queue_.front()->coalesced_event();
153     if (ShouldForwardToRenderer(touch.event)) {
154       client_->SendTouchEventImmediately(touch);
155       break;
156     }
157     PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS,
158                           ui::LatencyInfo());
159   }
160 }
161
162 void TouchEventQueue::OnGestureScrollEvent(
163     const GestureEventWithLatencyInfo& gesture_event) {
164   WebKit::WebInputEvent::Type type = gesture_event.event.type;
165   if (type == WebKit::WebInputEvent::GestureScrollBegin) {
166     // We assume the scroll event are generated synchronously from
167     // dispatching a touch event ack, so that we can fake a cancel
168     // event that has the correct touch ids as the touch event that
169     // is being acked. If not, we don't do the touch-cancel optimization.
170     if (no_touch_to_renderer_ || !dispatching_touch_ack_)
171       return;
172     no_touch_to_renderer_ = true;
173     // Fake a TouchCancel to cancel the touch points of the touch event
174     // that is currently being acked.
175     TouchEventWithLatencyInfo cancel_event =
176         dispatching_touch_ack_->coalesced_event();
177     cancel_event.event.type = WebKit::WebInputEvent::TouchCancel;
178     for (size_t i = 0; i < cancel_event.event.touchesLength; i++)
179       cancel_event.event.touches[i].state =
180           WebKit::WebTouchPoint::StateCancelled;
181     CoalescedWebTouchEvent* coalesced_cancel_event =
182         new CoalescedWebTouchEvent(cancel_event);
183     // Ignore the ack of the touch cancel so when it is acked, it won't get
184     // sent to gesture recognizer.
185     coalesced_cancel_event->set_ignore_ack(true);
186     // |dispatching_touch_ack_| is non-null when we reach here, meaning we
187     // are in the scope of PopTouchEventToClient() and that no touch event
188     // in the queue is waiting for ack from renderer. So we can just insert
189     // the touch cancel at the beginning of the queue.
190     touch_queue_.push_front(coalesced_cancel_event);
191   } else if (type == WebKit::WebInputEvent::GestureScrollEnd ||
192              type == WebKit::WebInputEvent::GestureFlingStart) {
193     no_touch_to_renderer_ = false;
194   }
195 }
196
197 void TouchEventQueue::FlushQueue() {
198   DCHECK(!dispatching_touch_ack_);
199   while (!touch_queue_.empty())
200     PopTouchEventToClient(INPUT_EVENT_ACK_STATE_NOT_CONSUMED,
201                           ui::LatencyInfo());
202 }
203
204 size_t TouchEventQueue::GetQueueSize() const {
205   return touch_queue_.size();
206 }
207
208 const TouchEventWithLatencyInfo& TouchEventQueue::GetLatestEvent() const {
209   return touch_queue_.back()->coalesced_event();
210 }
211
212 void TouchEventQueue::PopTouchEventToClient(
213     InputEventAckState ack_result,
214     const ui::LatencyInfo& renderer_latency_info) {
215   if (touch_queue_.empty())
216     return;
217   scoped_ptr<CoalescedWebTouchEvent> acked_event(touch_queue_.front());
218   touch_queue_.pop_front();
219
220   if (acked_event->ignore_ack())
221     return;
222
223   // Note that acking the touch-event may result in multiple gestures being sent
224   // to the renderer, or touch-events being queued.
225   base::AutoReset<CoalescedWebTouchEvent*>
226       dispatching_touch_ack(&dispatching_touch_ack_, acked_event.get());
227
228   for (WebTouchEventWithLatencyList::iterator iter = acked_event->begin(),
229        end = acked_event->end();
230        iter != end; ++iter) {
231     iter->latency.AddNewLatencyFrom(renderer_latency_info);
232     client_->OnTouchEventAck((*iter), ack_result);
233   }
234 }
235
236 bool TouchEventQueue::ShouldForwardToRenderer(
237     const WebKit::WebTouchEvent& event) const {
238   if (no_touch_to_renderer_ &&
239       event.type != WebKit::WebInputEvent::TouchCancel)
240     return false;
241
242   // Touch press events should always be forwarded to the renderer.
243   if (event.type == WebKit::WebInputEvent::TouchStart)
244     return true;
245
246   for (unsigned int i = 0; i < event.touchesLength; ++i) {
247     const WebKit::WebTouchPoint& point = event.touches[i];
248     // If a point has been stationary, then don't take it into account.
249     if (point.state == WebKit::WebTouchPoint::StateStationary)
250       continue;
251
252     if (touch_ack_states_.count(point.id) > 0) {
253       if (touch_ack_states_.find(point.id)->second !=
254           INPUT_EVENT_ACK_STATE_NO_CONSUMER_EXISTS)
255         return true;
256     } else {
257       // If the ACK status of a point is unknown, then the event should be
258       // forwarded to the renderer.
259       return true;
260     }
261   }
262
263   return false;
264 }
265
266 }  // namespace content