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

Side by Side Diff: content/renderer/input/main_thread_event_queue.cc

Issue 2007413002: [POC; Not for Review] Don't use PostTask queueing between compositor and main thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve mouse move by making it non blocking Created 4 years, 7 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 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 #include "content/renderer/input/main_thread_event_queue.h" 5 #include "content/renderer/input/main_thread_event_queue.h"
6 #include "content/common/input_messages.h"
6 7
7 namespace content { 8 namespace content {
8 9
9 MainThreadEventQueue::MainThreadEventQueue(int routing_id, 10 MainThreadEventQueue::MainThreadEventQueue(
10 MainThreadEventQueueClient* client) 11 int routing_id,
11 : routing_id_(routing_id), client_(client), is_flinging_(false) {} 12 MainThreadEventQueueClient* client,
13 const base::Callback<void(const IPC::Message&)>& main_listener,
14 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner)
15 : routing_id_(routing_id),
16 client_(client),
17 is_flinging_(false),
18 main_task_runner_(main_task_runner),
19 main_listener_(main_listener) {}
12 20
13 MainThreadEventQueue::~MainThreadEventQueue() {} 21 MainThreadEventQueue::~MainThreadEventQueue() {}
14 22
15 bool MainThreadEventQueue::HandleEvent( 23 bool MainThreadEventQueue::HandleEvent(
16 const blink::WebInputEvent* event, 24 const blink::WebInputEvent* event,
17 const ui::LatencyInfo& latency, 25 const ui::LatencyInfo& latency,
18 InputEventDispatchType original_dispatch_type, 26 InputEventDispatchType original_dispatch_type,
19 InputEventAckState ack_result) { 27 InputEventAckState ack_result) {
20 DCHECK(original_dispatch_type == DISPATCH_TYPE_BLOCKING || 28 DCHECK(original_dispatch_type == DISPATCH_TYPE_BLOCKING ||
21 original_dispatch_type == DISPATCH_TYPE_NON_BLOCKING); 29 original_dispatch_type == DISPATCH_TYPE_NON_BLOCKING);
(...skipping 12 matching lines...) Expand all
34 PendingMouseWheelEvent( 42 PendingMouseWheelEvent(
35 *static_cast<const blink::WebMouseWheelEvent*>(event), latency, 43 *static_cast<const blink::WebMouseWheelEvent*>(event), latency,
36 dispatch_type); 44 dispatch_type);
37 45
38 // Adjust the |dispatchType| on the event since the compositor 46 // Adjust the |dispatchType| on the event since the compositor
39 // determined all event listeners are passive. 47 // determined all event listeners are passive.
40 if (non_blocking) { 48 if (non_blocking) {
41 modified_dispatch_type_event.event.dispatchType = 49 modified_dispatch_type_event.event.dispatchType =
42 blink::WebInputEvent::ListenersNonBlockingPassive; 50 blink::WebInputEvent::ListenersNonBlockingPassive;
43 } 51 }
44 52 QueueWheelEvent(modified_dispatch_type_event);
45 if (wheel_events_.state() == WebInputEventQueueState::ITEM_PENDING) {
46 wheel_events_.Queue(modified_dispatch_type_event);
47 } else {
48 if (non_blocking) {
49 wheel_events_.set_state(WebInputEventQueueState::ITEM_PENDING);
50 client_->SendEventToMainThread(routing_id_,
51 &modified_dispatch_type_event.event,
52 latency, dispatch_type);
53 } else {
54 // If there is nothing in the event queue and the event is
55 // blocking pass the |original_dispatch_type| to avoid
56 // having the main thread call us back as an optimization.
57 client_->SendEventToMainThread(routing_id_,
58 &modified_dispatch_type_event.event,
59 latency, original_dispatch_type);
60 }
61 }
62 } else if (blink::WebInputEvent::isTouchEventType(event->type)) { 53 } else if (blink::WebInputEvent::isTouchEventType(event->type)) {
63 PendingTouchEvent modified_dispatch_type_event = 54 PendingTouchEvent modified_dispatch_type_event =
64 PendingTouchEvent(*static_cast<const blink::WebTouchEvent*>(event), 55 PendingTouchEvent(*static_cast<const blink::WebTouchEvent*>(event),
65 latency, dispatch_type); 56 latency, dispatch_type);
66 modified_dispatch_type_event.event.dispatchedDuringFling = is_flinging_; 57 modified_dispatch_type_event.event.dispatchedDuringFling = is_flinging_;
67 58
68 // Adjust the |dispatchType| on the event since the compositor 59 // Adjust the |dispatchType| on the event since the compositor
69 // determined all event listeners are passive. 60 // determined all event listeners are passive.
70 if (non_blocking) { 61 if (non_blocking) {
71 modified_dispatch_type_event.event.dispatchType = 62 modified_dispatch_type_event.event.dispatchType =
72 blink::WebInputEvent::ListenersNonBlockingPassive; 63 blink::WebInputEvent::ListenersNonBlockingPassive;
73 } 64 }
74 65
75 if (touch_events_.state() == WebInputEventQueueState::ITEM_PENDING) { 66 QueueTouchEvent(modified_dispatch_type_event);
76 touch_events_.Queue(modified_dispatch_type_event); 67 } else if (blink::WebInputEvent::isMouseEventType(event->type)) {
77 } else { 68 PendingMouseEvent modified_dispatch_type_event =
78 if (non_blocking) { 69 PendingMouseEvent(*static_cast<const blink::WebMouseEvent*>(event),
79 touch_events_.set_state(WebInputEventQueueState::ITEM_PENDING); 70 latency, dispatch_type);
80 client_->SendEventToMainThread(routing_id_, 71 QueueMouseEvent(modified_dispatch_type_event);
81 &modified_dispatch_type_event.event,
82 latency, dispatch_type);
83 } else {
84 // If there is nothing in the event queue and the event is
85 // blocking pass the |original_dispatch_type| to avoid
86 // having the main thread call us back as an optimization.
87 client_->SendEventToMainThread(routing_id_,
88 &modified_dispatch_type_event.event,
89 latency, original_dispatch_type);
90 }
91 }
92 } else { 72 } else {
93 client_->SendEventToMainThread(routing_id_, event, latency, 73 SendEventToMainThread(event, latency, original_dispatch_type);
94 original_dispatch_type);
95 } 74 }
96 75
97 // send an ack when we are non-blocking. 76 // send an ack when we are non-blocking.
98 return non_blocking; 77 return non_blocking;
99 } 78 }
100 79
101 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type) { 80 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type) {}
tdresser 2016/05/30 14:05:32 So we'd get rid of this completely?
dtapuska 2016/06/01 18:22:53 Yes
102 if (type == blink::WebInputEvent::MouseWheel) { 81
82 void MainThreadEventQueue::SendEventToMainThread(
83 const blink::WebInputEvent* event,
84 const ui::LatencyInfo& latency,
85 InputEventDispatchType dispatch_type) {
86 IPC::Message new_msg =
87 InputMsg_HandleInputEvent(routing_id_, event, latency, dispatch_type);
88
89 main_task_runner_->PostTask(FROM_HERE, base::Bind(main_listener_, new_msg));
90 }
91
92 void MainThreadEventQueue::PopWheelEventOnMainThread() {
93 std::unique_ptr<PendingMouseWheelEvent> event;
94 {
95 base::AutoLock lock(event_queue_mutex_);
96 if (!wheel_events_.empty())
tdresser 2016/05/30 14:05:32 Should this case ever occur?
dtapuska 2016/06/01 18:22:54 No it shouldn't can add dcheck
97 event = wheel_events_.Pop();
98 }
99
100 if (event) {
tdresser 2016/05/30 14:05:32 Should this ever not be true?
dtapuska 2016/06/01 18:22:53 No it shouldn't
101 client_->HandleEventOnMainThread(routing_id_, &event->event, event->latency,
102 event->type);
103 }
104
105 {
106 base::AutoLock lock(event_queue_mutex_);
103 if (!wheel_events_.empty()) { 107 if (!wheel_events_.empty()) {
104 std::unique_ptr<PendingMouseWheelEvent> event = wheel_events_.Pop(); 108 SendWheelEventNotificationToMainThread();
105 client_->SendEventToMainThread(routing_id_, &event->event, event->latency,
106 event->type);
107 } else {
108 wheel_events_.set_state(WebInputEventQueueState::ITEM_NOT_PENDING);
109 } 109 }
110 } else if (blink::WebInputEvent::isTouchEventType(type)) {
111 if (!touch_events_.empty()) {
112 std::unique_ptr<PendingTouchEvent> event = touch_events_.Pop();
113 client_->SendEventToMainThread(routing_id_, &event->event, event->latency,
114 event->type);
115 } else {
116 touch_events_.set_state(WebInputEventQueueState::ITEM_NOT_PENDING);
117 }
118 } else {
119 NOTREACHED() << "Invalid passive event type";
120 } 110 }
121 } 111 }
122 112
113 void MainThreadEventQueue::PopTouchEventOnMainThread() {
114 std::unique_ptr<PendingTouchEvent> event;
115 size_t size;
116 {
117 base::AutoLock lock(event_queue_mutex_);
118 if (!touch_events_.empty())
119 event = touch_events_.Pop();
120 size = touch_events_.size();
121 }
122
123 LOG(ERROR) << size;
124 if (event) {
125 client_->HandleEventOnMainThread(routing_id_, &event->event, event->latency,
126 event->type);
127 }
128
129 {
130 base::AutoLock lock(event_queue_mutex_);
131 if (!touch_events_.empty()) {
132 SendTouchEventNotificationToMainThread();
133 }
134 }
135 }
136
137 void MainThreadEventQueue::PopMouseEventOnMainThread() {
138 std::unique_ptr<PendingMouseEvent> event;
139 size_t size;
140 {
141 base::AutoLock lock(event_queue_mutex_);
142 if (!mouse_events_.empty())
143 event = mouse_events_.Pop();
144 size = mouse_events_.size();
145 }
146
147 if (event) {
148 client_->HandleEventOnMainThread(routing_id_, &event->event, event->latency,
149 event->type);
150 }
151
152 {
153 base::AutoLock lock(event_queue_mutex_);
154 if (!mouse_events_.empty()) {
155 SendMouseEventNotificationToMainThread();
156 }
157 }
158 }
159
160 void MainThreadEventQueue::SendWheelEventNotificationToMainThread() {
161 main_task_runner_->PostTask(
162 FROM_HERE, base::Bind(&MainThreadEventQueue::PopWheelEventOnMainThread,
163 base::Unretained(this)));
164 }
165
166 void MainThreadEventQueue::SendTouchEventNotificationToMainThread() {
167 main_task_runner_->PostTask(
168 FROM_HERE, base::Bind(&MainThreadEventQueue::PopTouchEventOnMainThread,
169 base::Unretained(this)));
170 }
171
172 void MainThreadEventQueue::SendMouseEventNotificationToMainThread() {
173 main_task_runner_->PostTask(
174 FROM_HERE, base::Bind(&MainThreadEventQueue::PopMouseEventOnMainThread,
175 base::Unretained(this)));
176 }
177
178 void MainThreadEventQueue::QueueWheelEvent(
179 const PendingMouseWheelEvent& event) {
180 size_t pending;
181 {
182 base::AutoLock lock(event_queue_mutex_);
183 pending = wheel_events_.size();
184 wheel_events_.Queue(event);
185 }
186 if (pending == 0)
187 SendWheelEventNotificationToMainThread();
188 }
189
190 void MainThreadEventQueue::QueueTouchEvent(const PendingTouchEvent& event) {
191 size_t pending;
192 {
193 base::AutoLock lock(event_queue_mutex_);
194 pending = touch_events_.size();
195 touch_events_.Queue(event);
196 }
197 if (pending == 0)
198 SendTouchEventNotificationToMainThread();
199 }
200
201 void MainThreadEventQueue::QueueMouseEvent(const PendingMouseEvent& event) {
202 size_t pending;
203 {
204 base::AutoLock lock(event_queue_mutex_);
205 pending = mouse_events_.size();
206 mouse_events_.Queue(event);
207 }
208 if (pending == 0)
209 SendMouseEventNotificationToMainThread();
210 }
211
123 } // namespace content 212 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698