OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ | 5 #ifndef CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ |
6 #define CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ | 6 #define CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include "base/memory/ref_counted.h" | |
9 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
10 #include "content/common/input/event_with_latency_info.h" | 11 #include "content/common/input/event_with_latency_info.h" |
11 #include "content/common/input/input_event_ack_state.h" | 12 #include "content/common/input/input_event_ack_state.h" |
12 #include "content/common/input/input_event_dispatch_type.h" | 13 #include "content/common/input/input_event_dispatch_type.h" |
13 #include "content/common/input/web_input_event_queue.h" | 14 #include "content/common/input/web_input_event_queue.h" |
14 #include "content/common/input/web_input_event_traits.h" | 15 #include "content/common/input/web_input_event_traits.h" |
15 #include "third_party/WebKit/public/web/WebInputEvent.h" | 16 #include "third_party/WebKit/public/web/WebInputEvent.h" |
16 #include "ui/events/latency_info.h" | 17 #include "ui/events/latency_info.h" |
17 | 18 |
18 namespace content { | 19 namespace content { |
19 | 20 |
20 template <typename BaseClass, typename BaseType> | 21 class EventWithDispatchType : public ScopedWebInputEventWithLatencyInfo { |
21 class EventWithDispatchType : public BaseClass { | |
22 public: | 22 public: |
23 EventWithDispatchType(const BaseType& e, | 23 EventWithDispatchType(const blink::WebInputEvent& event, |
24 const ui::LatencyInfo& l, | 24 const ui::LatencyInfo& latency, |
25 InputEventDispatchType t) | 25 InputEventDispatchType dispatch_type); |
26 : BaseClass(e, l), type(t) {} | 26 ~EventWithDispatchType(); |
27 bool CanCoalesceWith(const EventWithDispatchType& other) const | |
28 WARN_UNUSED_RESULT; | |
29 void CoalesceWith(const EventWithDispatchType& other); | |
27 | 30 |
28 InputEventDispatchType type; | 31 const std::deque<uint32_t>& eventsToAck() const { return eventsToAck_; } |
29 std::deque<uint32_t> eventsToAck; | 32 InputEventDispatchType dispatchType() const { return dispatch_type_; } |
30 | 33 |
31 bool CanCoalesceWith(const EventWithDispatchType& other) const | 34 private: |
32 WARN_UNUSED_RESULT { | 35 InputEventDispatchType dispatch_type_; |
33 return other.type == type && BaseClass::CanCoalesceWith(other); | 36 std::deque<uint32_t> eventsToAck_; |
tdresser
2016/07/20 20:52:27
Make it clear this is touch only.
dtapuska
2016/07/27 05:29:00
Acknowledged.
| |
34 } | |
35 | |
36 void CoalesceWith(const EventWithDispatchType& other) { | |
37 // If we are blocking and are coalescing touch, make sure to keep | |
38 // the touch ids that need to be acked. | |
39 if (type == DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN) { | |
40 // We should only have blocking touch events that need coalescing. | |
41 DCHECK(blink::WebInputEvent::isTouchEventType(other.event.type)); | |
42 eventsToAck.push_back( | |
43 WebInputEventTraits::GetUniqueTouchEventId(other.event)); | |
44 } | |
45 BaseClass::CoalesceWith(other); | |
46 } | |
47 }; | 37 }; |
48 | 38 |
49 using PendingMouseWheelEvent = | |
50 EventWithDispatchType<MouseWheelEventWithLatencyInfo, | |
51 blink::WebMouseWheelEvent>; | |
52 | |
53 using PendingTouchEvent = | |
54 EventWithDispatchType<TouchEventWithLatencyInfo, blink::WebTouchEvent>; | |
55 | |
56 class CONTENT_EXPORT MainThreadEventQueueClient { | 39 class CONTENT_EXPORT MainThreadEventQueueClient { |
57 public: | 40 public: |
58 // Send an |event| that was previously queued (possibly | 41 // Handle an |event| that was previously queued (possibly |
59 // coalesced with another event) to the |routing_id|'s | 42 // coalesced with another event) to the |routing_id|'s |
60 // channel. Implementors must implement this callback. | 43 // channel. Implementors must implement this callback. |
61 virtual void SendEventToMainThread(int routing_id, | 44 virtual void HandleEventOnMainThread( |
62 const blink::WebInputEvent* event, | 45 int routing_id, |
63 const ui::LatencyInfo& latency, | 46 const blink::WebInputEvent* event, |
64 InputEventDispatchType dispatch_type) = 0; | 47 const ui::LatencyInfo& latency, |
48 InputEventDispatchType dispatch_type) = 0; | |
65 | 49 |
66 virtual void SendInputEventAck(int routing_id, | 50 virtual void SendInputEventAck(int routing_id, |
67 blink::WebInputEvent::Type type, | 51 blink::WebInputEvent::Type type, |
68 InputEventAckState ack_result, | 52 InputEventAckState ack_result, |
69 uint32_t touch_event_id) = 0; | 53 uint32_t touch_event_id) = 0; |
70 }; | 54 }; |
71 | 55 |
72 // MainThreadEventQueue implements a series of queues (one touch | 56 // MainThreadEventQueue implements a series of queues (one touch |
73 // and one mouse wheel) for events that need to be queued between | 57 // and one mouse wheel) for events that need to be queued between |
tdresser
2016/07/20 20:52:27
Update comment.
| |
74 // the compositor and main threads. When an event is sent | 58 // the compositor and main threads. When an event is sent |
75 // from the compositor to main it can either be sent directly if no | 59 // from the compositor to main it can either be sent directly if no |
76 // outstanding events of that type are in flight; or it needs to | 60 // outstanding events of that type are in flight; or it needs to |
77 // wait in a queue until the main thread has finished processing | 61 // wait in a queue until the main thread has finished processing |
78 // the in-flight event. This class tracks the state and queues | 62 // the in-flight event. This class tracks the state and queues |
79 // for the event types. Methods on this class should only be called | 63 // for the event types. Methods on this class should only be called |
80 // from the compositor thread. | 64 // from the compositor thread. |
81 // | 65 // |
82 // Below some example flows are how the code behaves. | 66 // Below some example flows are how the code behaves. |
83 // Legend: B=Browser, C=Compositor, M=Main Thread, NB=Non-blocking | 67 // Legend: B=Browser, C=Compositor, M=Main Thread, NB=Non-blocking |
(...skipping 14 matching lines...) Expand all Loading... | |
98 // Non-blocking followed by blocking event sent to main thread. | 82 // Non-blocking followed by blocking event sent to main thread. |
99 // B C M | 83 // B C M |
100 // ---(NB)--> | 84 // ---(NB)--> |
101 // ---(PT)--> | 85 // ---(PT)--> |
102 // ---(BL)--> | 86 // ---(BL)--> |
103 // <--(PT)--- // Notify from NB event. | 87 // <--(PT)--- // Notify from NB event. |
104 // ---(PT)--> // Release blocking event. | 88 // ---(PT)--> // Release blocking event. |
105 // <--(PT)--- // Notify from BL event. | 89 // <--(PT)--- // Notify from BL event. |
106 // <-------(ACK)------ | 90 // <-------(ACK)------ |
107 // | 91 // |
108 class CONTENT_EXPORT MainThreadEventQueue { | 92 class CONTENT_EXPORT MainThreadEventQueue |
93 : public base::RefCountedThreadSafe<MainThreadEventQueue> { | |
109 public: | 94 public: |
110 MainThreadEventQueue(int routing_id, MainThreadEventQueueClient* client); | 95 MainThreadEventQueue( |
111 ~MainThreadEventQueue(); | 96 int routing_id, |
97 MainThreadEventQueueClient* client, | |
98 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner); | |
112 | 99 |
113 // Called once the compositor has handled |event| and indicated that it is | 100 // Called once the compositor has handled |event| and indicated that it is |
114 // a non-blocking event to be queued to the main thread. | 101 // a non-blocking event to be queued to the main thread. |
115 bool HandleEvent(const blink::WebInputEvent* event, | 102 bool HandleEvent(const blink::WebInputEvent* event, |
116 const ui::LatencyInfo& latency, | 103 const ui::LatencyInfo& latency, |
117 InputEventDispatchType dispatch_type, | 104 InputEventDispatchType dispatch_type, |
118 InputEventAckState ack_result); | 105 InputEventAckState ack_result); |
119 | 106 |
120 // Call once the main thread has handled an outstanding |type| event | 107 // Call once the main thread has handled an outstanding |type| event |
121 // in flight. | 108 // in flight. |
122 void EventHandled(blink::WebInputEvent::Type type, | 109 void EventHandled(blink::WebInputEvent::Type type, |
123 InputEventAckState ack_result); | 110 InputEventAckState ack_result); |
124 | 111 |
125 void set_is_flinging(bool is_flinging) { is_flinging_ = is_flinging; } | 112 void set_is_flinging(bool is_flinging) { is_flinging_ = is_flinging; } |
126 | 113 |
127 private: | 114 private: |
115 friend class base::RefCountedThreadSafe<MainThreadEventQueue>; | |
116 ~MainThreadEventQueue(); | |
117 void QueueEvent(std::unique_ptr<EventWithDispatchType>&& event); | |
118 void SendEventNotificationToMainThread(); | |
119 void PopEventOnMainThread(); | |
120 void SendEventToMainThread(const blink::WebInputEvent* event, | |
121 const ui::LatencyInfo& latency, | |
122 InputEventDispatchType original_dispatch_type); | |
123 | |
128 friend class MainThreadEventQueueTest; | 124 friend class MainThreadEventQueueTest; |
129 int routing_id_; | 125 int routing_id_; |
130 MainThreadEventQueueClient* client_; | 126 MainThreadEventQueueClient* client_; |
131 WebInputEventQueue<PendingMouseWheelEvent> wheel_events_; | 127 WebInputEventQueue<EventWithDispatchType> events_; |
132 WebInputEventQueue<PendingTouchEvent> touch_events_; | 128 std::unique_ptr<EventWithDispatchType> in_flight_event_; |
133 bool is_flinging_; | 129 bool is_flinging_; |
tdresser
2016/07/20 20:52:27
Do we still need is_flinging?
dtapuska
2016/07/27 05:29:00
yes sorry that was missing code.
| |
134 | 130 base::Lock event_queue_mutex_; |
135 // TODO(dtapuska): These can be removed when the queues are dequeued | 131 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
136 // on the main thread. See crbug.com/624021 | |
137 std::unique_ptr<PendingMouseWheelEvent> in_flight_wheel_event_; | |
138 std::unique_ptr<PendingTouchEvent> in_flight_touch_event_; | |
139 | 132 |
140 DISALLOW_COPY_AND_ASSIGN(MainThreadEventQueue); | 133 DISALLOW_COPY_AND_ASSIGN(MainThreadEventQueue); |
141 }; | 134 }; |
142 | 135 |
143 } // namespace content | 136 } // namespace content |
144 | 137 |
145 #endif // CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ | 138 #endif // CONTENT_RENDERER_INPUT_MAIN_THREAD_EVENT_QUEUE_H_ |
OLD | NEW |