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

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

Issue 2813683002: Allow MainThreadEventQueue to call the RenderWidget directly. (Closed)
Patch Set: Fix tdresser's comments Created 3 years, 8 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 6
7 #include "base/metrics/field_trial.h" 7 #include "base/metrics/field_trial.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "content/common/input/event_with_latency_info.h" 10 #include "content/common/input/event_with_latency_info.h"
11 #include "content/common/input_messages.h" 11 #include "content/common/input_messages.h"
12 #include "content/renderer/render_widget.h"
12 13
13 namespace content { 14 namespace content {
14 15
15 namespace { 16 namespace {
16 17
17 const size_t kTenSeconds = 10 * 1000 * 1000; 18 const size_t kTenSeconds = 10 * 1000 * 1000;
18 19
19 class QueuedClosure : public MainThreadEventQueueTask { 20 class QueuedClosure : public MainThreadEventQueueTask {
20 public: 21 public:
21 QueuedClosure(const base::Closure& closure) : closure_(closure) {} 22 QueuedClosure(const base::Closure& closure) : closure_(closure) {}
22 23
23 ~QueuedClosure() override {} 24 ~QueuedClosure() override {}
24 25
25 FilterResult FilterNewEvent( 26 FilterResult FilterNewEvent(
26 const MainThreadEventQueueTask& other_task) override { 27 const MainThreadEventQueueTask& other_task) override {
27 return other_task.IsWebInputEvent() ? FilterResult::KeepIterating 28 return other_task.IsWebInputEvent() ? FilterResult::KeepIterating
28 : FilterResult::StopIterating; 29 : FilterResult::StopIterating;
29 } 30 }
30 31
31 bool IsWebInputEvent() const override { return false; } 32 bool IsWebInputEvent() const override { return false; }
32 33
33 void Dispatch(int routing_id, MainThreadEventQueueClient*) override { 34 void Dispatch(MainThreadEventQueue*) override { closure_.Run(); }
34 closure_.Run();
35 }
36
37 void EventHandled(int routing_id,
38 blink::scheduler::RendererScheduler* renderer_scheduler,
39 MainThreadEventQueueClient* client,
40 blink::WebInputEvent::Type type,
41 blink::WebInputEventResult result,
42 InputEventAckState ack_result) override {}
43 35
44 private: 36 private:
45 base::Closure closure_; 37 base::Closure closure_;
46 }; 38 };
47 39
40 // Time interval at which touchmove events will be skipped during rAF signal.
tdresser 2017/04/18 13:45:05 Should this say "touchmove events during scroll wi
dtapuska 2017/04/18 14:16:03 Done.
41 const base::TimeDelta kAsyncTouchMoveInterval =
42 base::TimeDelta::FromMilliseconds(200);
43
44 } // namespace
45
48 class QueuedWebInputEvent : public ScopedWebInputEventWithLatencyInfo, 46 class QueuedWebInputEvent : public ScopedWebInputEventWithLatencyInfo,
49 public MainThreadEventQueueTask { 47 public MainThreadEventQueueTask {
50 public: 48 public:
51 QueuedWebInputEvent(ui::WebScopedInputEvent event, 49 QueuedWebInputEvent(ui::WebScopedInputEvent event,
52 const ui::LatencyInfo& latency, 50 const ui::LatencyInfo& latency,
53 InputEventDispatchType dispatch_type, 51 InputEventDispatchType dispatch_type,
54 bool originally_cancelable) 52 bool originally_cancelable)
55 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency), 53 : ScopedWebInputEventWithLatencyInfo(std::move(event), latency),
56 dispatch_type_(dispatch_type), 54 dispatch_type_(dispatch_type),
57 non_blocking_coalesced_count_(0), 55 non_blocking_coalesced_count_(0),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 90
93 // The newest event (|other_item|) always wins when updating fields. 91 // The newest event (|other_item|) always wins when updating fields.
94 dispatch_type_ = other_event.dispatch_type_; 92 dispatch_type_ = other_event.dispatch_type_;
95 originally_cancelable_ = other_event.originally_cancelable_; 93 originally_cancelable_ = other_event.originally_cancelable_;
96 94
97 return FilterResult::CoalescedEvent; 95 return FilterResult::CoalescedEvent;
98 } 96 }
99 97
100 bool IsWebInputEvent() const override { return true; } 98 bool IsWebInputEvent() const override { return true; }
101 99
102 void Dispatch(int routing_id, MainThreadEventQueueClient* client) override { 100 void Dispatch(MainThreadEventQueue* queue) override {
103 // Report the coalesced count only for continuous events; otherwise 101 // Report the coalesced count only for continuous events; otherwise
104 // the zero value would be dominated by non-continuous events. 102 // the zero value would be dominated by non-continuous events.
105 base::TimeTicks now = base::TimeTicks::Now(); 103 base::TimeTicks now = base::TimeTicks::Now();
106 if (IsContinuousEvent()) { 104 if (IsContinuousEvent()) {
107 UMA_HISTOGRAM_CUSTOM_COUNTS( 105 UMA_HISTOGRAM_CUSTOM_COUNTS(
108 "Event.MainThreadEventQueue.Continuous.QueueingTime", 106 "Event.MainThreadEventQueue.Continuous.QueueingTime",
109 (now - creationTimestamp()).InMicroseconds(), 1, kTenSeconds, 50); 107 (now - creationTimestamp()).InMicroseconds(), 1, kTenSeconds, 50);
110 108
111 UMA_HISTOGRAM_CUSTOM_COUNTS( 109 UMA_HISTOGRAM_CUSTOM_COUNTS(
112 "Event.MainThreadEventQueue.Continuous.FreshnessTime", 110 "Event.MainThreadEventQueue.Continuous.FreshnessTime",
113 (now - lastCoalescedTimestamp()).InMicroseconds(), 1, kTenSeconds, 111 (now - lastCoalescedTimestamp()).InMicroseconds(), 1, kTenSeconds,
114 50); 112 50);
115 113
116 UMA_HISTOGRAM_COUNTS_1000("Event.MainThreadEventQueue.CoalescedCount", 114 UMA_HISTOGRAM_COUNTS_1000("Event.MainThreadEventQueue.CoalescedCount",
117 coalescedCount()); 115 coalescedCount());
118 } else { 116 } else {
119 UMA_HISTOGRAM_CUSTOM_COUNTS( 117 UMA_HISTOGRAM_CUSTOM_COUNTS(
120 "Event.MainThreadEventQueue.NonContinuous.QueueingTime", 118 "Event.MainThreadEventQueue.NonContinuous.QueueingTime",
121 (now - creationTimestamp()).InMicroseconds(), 1, kTenSeconds, 50); 119 (now - creationTimestamp()).InMicroseconds(), 1, kTenSeconds, 50);
122 } 120 }
123 121
124 InputEventDispatchType dispatch_type = dispatchType(); 122 InputEventAckState ack_result = queue->HandleEventOnMainThread(
125 if (!blockingCoalescedEventIds().empty()) { 123 coalesced_event(), latencyInfo(), dispatchType());
126 switch (dispatch_type) { 124 for (const auto id : blockingCoalescedEventIds())
127 case DISPATCH_TYPE_BLOCKING: 125 queue->SendInputEventAck(event(), ack_result, id);
128 dispatch_type = DISPATCH_TYPE_BLOCKING_NOTIFY_MAIN;
129 break;
130 case DISPATCH_TYPE_NON_BLOCKING:
131 dispatch_type = DISPATCH_TYPE_NON_BLOCKING_NOTIFY_MAIN;
132 break;
133 default:
134 NOTREACHED();
135 }
136 }
137 client->HandleEventOnMainThread(routing_id, &coalesced_event(),
138 latencyInfo(), dispatch_type);
139 }
140
141 void EventHandled(int routing_id,
142 blink::scheduler::RendererScheduler* renderer_scheduler,
143 MainThreadEventQueueClient* client,
144 blink::WebInputEvent::Type type,
145 blink::WebInputEventResult result,
146 InputEventAckState ack_result) override {
147 for (const auto id : blockingCoalescedEventIds()) {
148 client->SendInputEventAck(routing_id, type, ack_result, id);
149 if (renderer_scheduler) {
150 renderer_scheduler->DidHandleInputEventOnMainThread(event(), result);
151 }
152 }
153 } 126 }
154 127
155 bool originallyCancelable() const { return originally_cancelable_; } 128 bool originallyCancelable() const { return originally_cancelable_; }
156 129
157 private: 130 private:
158 FilterResult HandleTouchScrollStartQueued() { 131 FilterResult HandleTouchScrollStartQueued() {
159 // A TouchScrollStart will queued after this touch move which will make all 132 // A TouchScrollStart will queued after this touch move which will make all
160 // previous touch moves that are queued uncancelable. 133 // previous touch moves that are queued uncancelable.
161 switch (event().GetType()) { 134 switch (event().GetType()) {
162 case blink::WebInputEvent::kTouchMove: { 135 case blink::WebInputEvent::kTouchMove: {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 base::TimeTicks creation_timestamp_; 186 base::TimeTicks creation_timestamp_;
214 base::TimeTicks last_coalesced_timestamp_; 187 base::TimeTicks last_coalesced_timestamp_;
215 188
216 // Whether the received event was originally cancelable or not. The compositor 189 // Whether the received event was originally cancelable or not. The compositor
217 // input handler can change the event based on presence of event handlers so 190 // input handler can change the event based on presence of event handlers so
218 // this is the state at which the renderer received the event from the 191 // this is the state at which the renderer received the event from the
219 // browser. 192 // browser.
220 bool originally_cancelable_; 193 bool originally_cancelable_;
221 }; 194 };
222 195
223 // Time interval at which touchmove events will be skipped during rAF signal.
224 const base::TimeDelta kAsyncTouchMoveInterval =
225 base::TimeDelta::FromMilliseconds(200);
226
227 bool IsAsyncTouchMove(
228 const std::unique_ptr<MainThreadEventQueueTask>& queued_item) {
229 if (!queued_item->IsWebInputEvent())
230 return false;
231 const QueuedWebInputEvent* event =
232 static_cast<const QueuedWebInputEvent*>(queued_item.get());
233 if (event->event().GetType() != blink::WebInputEvent::kTouchMove)
234 return false;
235 const blink::WebTouchEvent& touch_event =
236 static_cast<const blink::WebTouchEvent&>(event->event());
237 return touch_event.moved_beyond_slop_region && !event->originallyCancelable();
238 }
239
240 } // namespace
241
242 MainThreadEventQueue::SharedState::SharedState() 196 MainThreadEventQueue::SharedState::SharedState()
243 : sent_main_frame_request_(false), sent_post_task_(false) {} 197 : sent_main_frame_request_(false), sent_post_task_(false) {}
244 198
245 MainThreadEventQueue::SharedState::~SharedState() {} 199 MainThreadEventQueue::SharedState::~SharedState() {}
246 200
247 MainThreadEventQueue::MainThreadEventQueue( 201 MainThreadEventQueue::MainThreadEventQueue(
248 int routing_id,
249 MainThreadEventQueueClient* client, 202 MainThreadEventQueueClient* client,
250 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner, 203 const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
251 blink::scheduler::RendererScheduler* renderer_scheduler) 204 blink::scheduler::RendererScheduler* renderer_scheduler)
252 : routing_id_(routing_id), 205 : client_(client),
253 client_(client),
254 last_touch_start_forced_nonblocking_due_to_fling_(false), 206 last_touch_start_forced_nonblocking_due_to_fling_(false),
255 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled( 207 enable_fling_passive_listener_flag_(base::FeatureList::IsEnabled(
256 features::kPassiveEventListenersDueToFling)), 208 features::kPassiveEventListenersDueToFling)),
257 enable_non_blocking_due_to_main_thread_responsiveness_flag_( 209 enable_non_blocking_due_to_main_thread_responsiveness_flag_(
258 base::FeatureList::IsEnabled( 210 base::FeatureList::IsEnabled(
259 features::kMainThreadBusyScrollIntervention)), 211 features::kMainThreadBusyScrollIntervention)),
260 handle_raf_aligned_touch_input_( 212 handle_raf_aligned_touch_input_(
261 base::FeatureList::IsEnabled(features::kRafAlignedTouchInputEvents)), 213 base::FeatureList::IsEnabled(features::kRafAlignedTouchInputEvents)),
262 handle_raf_aligned_mouse_input_( 214 handle_raf_aligned_mouse_input_(
263 base::FeatureList::IsEnabled(features::kRafAlignedMouseInputEvents)), 215 base::FeatureList::IsEnabled(features::kRafAlignedMouseInputEvents)),
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 needs_post_task = !shared_state_.sent_post_task_; 336 needs_post_task = !shared_state_.sent_post_task_;
385 shared_state_.sent_post_task_ = true; 337 shared_state_.sent_post_task_ = true;
386 } 338 }
387 339
388 if (needs_post_task) 340 if (needs_post_task)
389 PostTaskToMainThread(); 341 PostTaskToMainThread();
390 } 342 }
391 343
392 void MainThreadEventQueue::DispatchInFlightEvent() { 344 void MainThreadEventQueue::DispatchInFlightEvent() {
393 if (in_flight_event_) { 345 if (in_flight_event_) {
394 in_flight_event_->Dispatch(routing_id_, client_); 346 in_flight_event_->Dispatch(this);
395 in_flight_event_.reset(); 347 in_flight_event_.reset();
396 } 348 }
397 } 349 }
398 350
399 void MainThreadEventQueue::PossiblyScheduleMainFrame() { 351 void MainThreadEventQueue::PossiblyScheduleMainFrame() {
400 if (IsRafAlignedInputDisabled()) 352 if (IsRafAlignedInputDisabled())
401 return; 353 return;
402 bool needs_main_frame = false; 354 bool needs_main_frame = false;
403 { 355 {
404 base::AutoLock lock(shared_state_lock_); 356 base::AutoLock lock(shared_state_lock_);
405 if (!shared_state_.sent_main_frame_request_ && 357 if (!shared_state_.sent_main_frame_request_ &&
406 !shared_state_.events_.empty() && 358 !shared_state_.events_.empty() &&
407 IsRafAlignedEvent(shared_state_.events_.front())) { 359 IsRafAlignedEvent(shared_state_.events_.front())) {
408 needs_main_frame = true; 360 needs_main_frame = true;
409 shared_state_.sent_main_frame_request_ = true; 361 shared_state_.sent_main_frame_request_ = true;
410 } 362 }
411 } 363 }
412 if (needs_main_frame) 364 if (needs_main_frame)
413 client_->NeedsMainFrame(routing_id_); 365 SetNeedsMainFrame();
414 } 366 }
415 367
416 void MainThreadEventQueue::DispatchEvents() { 368 void MainThreadEventQueue::DispatchEvents() {
417 size_t events_to_process; 369 size_t events_to_process;
418 370
419 // Record the queue size so that we only process 371 // Record the queue size so that we only process
420 // that maximum number of events. 372 // that maximum number of events.
421 { 373 {
422 base::AutoLock lock(shared_state_lock_); 374 base::AutoLock lock(shared_state_lock_);
423 shared_state_.sent_post_task_ = false; 375 shared_state_.sent_post_task_ = false;
(...skipping 13 matching lines...) Expand all
437 return; 389 return;
438 in_flight_event_ = shared_state_.events_.Pop(); 390 in_flight_event_ = shared_state_.events_.Pop();
439 } 391 }
440 392
441 // Dispatching the event is outside of critical section. 393 // Dispatching the event is outside of critical section.
442 DispatchInFlightEvent(); 394 DispatchInFlightEvent();
443 } 395 }
444 PossiblyScheduleMainFrame(); 396 PossiblyScheduleMainFrame();
445 } 397 }
446 398
447 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, 399 static bool IsAsyncTouchMove(
448 blink::WebInputEventResult result, 400 const std::unique_ptr<MainThreadEventQueueTask>& queued_item) {
449 InputEventAckState ack_result) { 401 if (!queued_item->IsWebInputEvent())
450 if (in_flight_event_) { 402 return false;
451 in_flight_event_->EventHandled(routing_id_, renderer_scheduler_, client_, 403 const QueuedWebInputEvent* event =
452 type, result, ack_result); 404 static_cast<const QueuedWebInputEvent*>(queued_item.get());
453 } 405 if (event->event().GetType() != blink::WebInputEvent::kTouchMove)
406 return false;
407 const blink::WebTouchEvent& touch_event =
408 static_cast<const blink::WebTouchEvent&>(event->event());
409 return touch_event.moved_beyond_slop_region && !event->originallyCancelable();
454 } 410 }
455 411
456 void MainThreadEventQueue::DispatchRafAlignedInput(base::TimeTicks frame_time) { 412 void MainThreadEventQueue::DispatchRafAlignedInput(base::TimeTicks frame_time) {
457 if (IsRafAlignedInputDisabled()) 413 if (IsRafAlignedInputDisabled())
458 return; 414 return;
459 415
460 size_t queue_size_at_start; 416 size_t queue_size_at_start;
461 417
462 // Record the queue size so that we only process 418 // Record the queue size so that we only process
463 // that maximum number of events. 419 // that maximum number of events.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 } else { 475 } else {
520 needs_main_frame = !shared_state_.sent_main_frame_request_; 476 needs_main_frame = !shared_state_.sent_main_frame_request_;
521 shared_state_.sent_main_frame_request_ = true; 477 shared_state_.sent_main_frame_request_ = true;
522 } 478 }
523 } 479 }
524 } 480 }
525 481
526 if (needs_post_task) 482 if (needs_post_task)
527 PostTaskToMainThread(); 483 PostTaskToMainThread();
528 if (needs_main_frame) 484 if (needs_main_frame)
529 client_->NeedsMainFrame(routing_id_); 485 SetNeedsMainFrame();
530 } 486 }
531 487
532 bool MainThreadEventQueue::IsRafAlignedInputDisabled() const { 488 bool MainThreadEventQueue::IsRafAlignedInputDisabled() const {
533 return !handle_raf_aligned_mouse_input_ && !handle_raf_aligned_touch_input_; 489 return !handle_raf_aligned_mouse_input_ && !handle_raf_aligned_touch_input_;
534 } 490 }
535 491
536 bool MainThreadEventQueue::IsRafAlignedEvent( 492 bool MainThreadEventQueue::IsRafAlignedEvent(
537 const std::unique_ptr<MainThreadEventQueueTask>& item) const { 493 const std::unique_ptr<MainThreadEventQueueTask>& item) const {
538 if (!item->IsWebInputEvent()) 494 if (!item->IsWebInputEvent())
539 return false; 495 return false;
540 const QueuedWebInputEvent* event = 496 const QueuedWebInputEvent* event =
541 static_cast<const QueuedWebInputEvent*>(item.get()); 497 static_cast<const QueuedWebInputEvent*>(item.get());
542 switch (event->event().GetType()) { 498 switch (event->event().GetType()) {
543 case blink::WebInputEvent::kMouseMove: 499 case blink::WebInputEvent::kMouseMove:
544 case blink::WebInputEvent::kMouseWheel: 500 case blink::WebInputEvent::kMouseWheel:
545 return handle_raf_aligned_mouse_input_; 501 return handle_raf_aligned_mouse_input_;
546 case blink::WebInputEvent::kTouchMove: 502 case blink::WebInputEvent::kTouchMove:
547 return handle_raf_aligned_touch_input_; 503 return handle_raf_aligned_touch_input_;
548 default: 504 default:
549 return false; 505 return false;
550 } 506 }
551 } 507 }
552 508
509 InputEventAckState MainThreadEventQueue::HandleEventOnMainThread(
510 const blink::WebCoalescedInputEvent& event,
511 const ui::LatencyInfo& latency,
512 InputEventDispatchType dispatch_type) {
513 if (client_)
514 return client_->HandleInputEvent(event, latency, dispatch_type);
515 return INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
516 }
517
518 void MainThreadEventQueue::SendInputEventAck(const blink::WebInputEvent& event,
519 InputEventAckState ack_result,
520 uint32_t touch_event_id) {
521 DCHECK(main_task_runner_->BelongsToCurrentThread());
522 if (!client_)
523 return;
524 client_->SendInputEventAck(event.GetType(), ack_result, touch_event_id);
525 if (renderer_scheduler_) {
526 renderer_scheduler_->DidHandleInputEventOnMainThread(
527 event, ack_result == INPUT_EVENT_ACK_STATE_CONSUMED
528 ? blink::WebInputEventResult::kHandledApplication
529 : blink::WebInputEventResult::kNotHandled);
530 }
531 }
532
533 void MainThreadEventQueue::SetNeedsMainFrame() {
534 if (main_task_runner_->BelongsToCurrentThread()) {
535 if (client_)
536 client_->SetNeedsMainFrame();
537 return;
538 }
539
540 main_task_runner_->PostTask(
541 FROM_HERE, base::Bind(&MainThreadEventQueue::SetNeedsMainFrame, this));
542 }
543
544 void MainThreadEventQueue::ClearClient() {
545 DCHECK(main_task_runner_->BelongsToCurrentThread());
546 client_ = nullptr;
547 }
548
553 } // namespace content 549 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698