Chromium Code Reviews| 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 #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" |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 | 398 |
| 399 void MainThreadEventQueue::PossiblyScheduleMainFrame() { | 399 void MainThreadEventQueue::PossiblyScheduleMainFrame() { |
| 400 if (IsRafAlignedInputDisabled()) | 400 if (IsRafAlignedInputDisabled()) |
| 401 return; | 401 return; |
| 402 bool needs_main_frame = false; | 402 bool needs_main_frame = false; |
| 403 { | 403 { |
| 404 base::AutoLock lock(shared_state_lock_); | 404 base::AutoLock lock(shared_state_lock_); |
| 405 if (!shared_state_.sent_main_frame_request_ && | 405 if (!shared_state_.sent_main_frame_request_ && |
| 406 !shared_state_.events_.empty() && | 406 !shared_state_.events_.empty() && |
| 407 IsRafAlignedEvent(shared_state_.events_.front())) { | 407 IsRafAlignedEvent(shared_state_.events_.front())) { |
| 408 needs_main_frame = !shared_state_.sent_main_frame_request_; | 408 needs_main_frame = true; |
| 409 shared_state_.sent_main_frame_request_ = false; | 409 shared_state_.sent_main_frame_request_ = true; |
| 410 } | 410 } |
| 411 } | 411 } |
| 412 if (needs_main_frame) | 412 if (needs_main_frame) |
| 413 client_->NeedsMainFrame(routing_id_); | 413 client_->NeedsMainFrame(routing_id_); |
| 414 } | 414 } |
| 415 | 415 |
| 416 void MainThreadEventQueue::DispatchEvents() { | 416 void MainThreadEventQueue::DispatchEvents() { |
| 417 std::deque<std::unique_ptr<MainThreadEventQueueTask>> events_to_process; | 417 size_t events_to_process; |
| 418 | |
| 419 // Record the queue size so that we only process | |
| 420 // that maximum number of events. | |
| 418 { | 421 { |
| 419 base::AutoLock lock(shared_state_lock_); | 422 base::AutoLock lock(shared_state_lock_); |
| 420 shared_state_.sent_post_task_ = false; | 423 shared_state_.sent_post_task_ = false; |
| 424 events_to_process = shared_state_.events_.size(); | |
| 421 | 425 |
| 422 shared_state_.events_.swap(&events_to_process); | 426 // Look at the tail of the queue and determine how many rAF |
|
mustaq
2017/04/04 16:00:22
Nit: we are not exactly "counting" rAF aligned eve
dtapuska
2017/04/04 20:02:19
Done.
| |
| 423 | 427 // aligned events remain on it. |
| 424 // Now take any raf aligned events that are at the tail of the queue | 428 for (size_t i = shared_state_.events_.size(); i > 0; --i) { |
|
mustaq
2017/04/04 16:00:23
Nit: perhaps replace with a 2+ line |while| loop?
dtapuska
2017/04/04 20:02:19
Done.
| |
| 425 // and put them back. | 429 if (IsRafAlignedEvent(shared_state_.events_.at(i - 1))) { |
| 426 while (!events_to_process.empty()) { | 430 events_to_process--; |
| 427 if (!IsRafAlignedEvent(events_to_process.back())) | 431 } else { |
| 428 break; | 432 break; |
| 429 shared_state_.events_.emplace_front(std::move(events_to_process.back())); | 433 } |
| 430 events_to_process.pop_back(); | |
| 431 } | 434 } |
| 432 } | 435 } |
| 433 while (!events_to_process.empty()) { | 436 |
| 434 in_flight_event_ = std::move(events_to_process.front()); | 437 while (events_to_process--) { |
| 435 events_to_process.pop_front(); | 438 { |
| 439 base::AutoLock lock(shared_state_lock_); | |
| 440 if (shared_state_.events_.empty()) | |
| 441 return; | |
| 442 in_flight_event_ = shared_state_.events_.Pop(); | |
| 443 } | |
| 444 | |
| 445 // Dispatching the event is outside of critical section. | |
| 436 DispatchInFlightEvent(); | 446 DispatchInFlightEvent(); |
| 437 } | 447 } |
| 438 PossiblyScheduleMainFrame(); | 448 PossiblyScheduleMainFrame(); |
| 439 } | 449 } |
| 440 | 450 |
| 441 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, | 451 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, |
| 442 blink::WebInputEventResult result, | 452 blink::WebInputEventResult result, |
| 443 InputEventAckState ack_result) { | 453 InputEventAckState ack_result) { |
| 444 if (in_flight_event_) { | 454 if (in_flight_event_) { |
| 445 in_flight_event_->EventHandled(routing_id_, renderer_scheduler_, client_, | 455 in_flight_event_->EventHandled(routing_id_, renderer_scheduler_, client_, |
| 446 type, result, ack_result); | 456 type, result, ack_result); |
| 447 } | 457 } |
| 448 } | 458 } |
| 449 | 459 |
| 450 void MainThreadEventQueue::DispatchRafAlignedInput(base::TimeTicks frame_time) { | 460 void MainThreadEventQueue::DispatchRafAlignedInput(base::TimeTicks frame_time) { |
| 451 if (IsRafAlignedInputDisabled()) | 461 if (IsRafAlignedInputDisabled()) |
| 452 return; | 462 return; |
| 453 | 463 |
| 454 std::deque<std::unique_ptr<MainThreadEventQueueTask>> events_to_process; | 464 size_t queue_size_at_start; |
| 465 | |
| 466 // Record the queue size so that we only process | |
| 467 // that maximum number of events. | |
| 455 { | 468 { |
| 456 base::AutoLock lock(shared_state_lock_); | 469 base::AutoLock lock(shared_state_lock_); |
| 457 shared_state_.sent_main_frame_request_ = false; | 470 shared_state_.sent_main_frame_request_ = false; |
| 471 queue_size_at_start = shared_state_.events_.size(); | |
| 472 } | |
| 458 | 473 |
| 459 while (!shared_state_.events_.empty()) { | 474 while (queue_size_at_start--) { |
| 475 { | |
| 476 base::AutoLock lock(shared_state_lock_); | |
| 477 | |
| 478 if (shared_state_.events_.empty()) | |
| 479 return; | |
| 480 | |
| 460 if (IsRafAlignedEvent(shared_state_.events_.front())) { | 481 if (IsRafAlignedEvent(shared_state_.events_.front())) { |
| 461 // Throttle touchmoves that are async. | 482 // Throttle touchmoves that are async. |
| 462 if (handle_raf_aligned_touch_input_ && | 483 if (handle_raf_aligned_touch_input_ && |
| 463 IsAsyncTouchMove(shared_state_.events_.front())) { | 484 IsAsyncTouchMove(shared_state_.events_.front())) { |
| 464 if (shared_state_.events_.size() == 1 && | 485 if (shared_state_.events_.size() == 1 && |
| 465 frame_time < shared_state_.last_async_touch_move_timestamp_ + | 486 frame_time < shared_state_.last_async_touch_move_timestamp_ + |
| 466 kAsyncTouchMoveInterval) { | 487 kAsyncTouchMoveInterval) { |
| 467 break; | 488 break; |
| 468 } | 489 } |
| 469 shared_state_.last_async_touch_move_timestamp_ = frame_time; | 490 shared_state_.last_async_touch_move_timestamp_ = frame_time; |
| 470 } | 491 } |
| 471 } | 492 } |
| 472 events_to_process.emplace_back(shared_state_.events_.Pop()); | 493 in_flight_event_ = shared_state_.events_.Pop(); |
| 473 } | 494 } |
| 495 | |
| 496 // Dispatching the event is outside of critical section. | |
| 497 DispatchInFlightEvent(); | |
| 474 } | 498 } |
| 475 | 499 |
| 476 while(!events_to_process.empty()) { | |
| 477 in_flight_event_ = std::move(events_to_process.front()); | |
| 478 events_to_process.pop_front(); | |
| 479 DispatchInFlightEvent(); | |
| 480 } | |
| 481 PossiblyScheduleMainFrame(); | 500 PossiblyScheduleMainFrame(); |
| 482 } | 501 } |
| 483 | 502 |
| 484 void MainThreadEventQueue::PostTaskToMainThread() { | 503 void MainThreadEventQueue::PostTaskToMainThread() { |
| 485 main_task_runner_->PostTask( | 504 main_task_runner_->PostTask( |
| 486 FROM_HERE, base::Bind(&MainThreadEventQueue::DispatchEvents, this)); | 505 FROM_HERE, base::Bind(&MainThreadEventQueue::DispatchEvents, this)); |
| 487 } | 506 } |
| 488 | 507 |
| 489 void MainThreadEventQueue::QueueEvent( | 508 void MainThreadEventQueue::QueueEvent( |
| 490 std::unique_ptr<MainThreadEventQueueTask> event) { | 509 std::unique_ptr<MainThreadEventQueueTask> event) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 529 case blink::WebInputEvent::MouseWheel: | 548 case blink::WebInputEvent::MouseWheel: |
| 530 return handle_raf_aligned_mouse_input_; | 549 return handle_raf_aligned_mouse_input_; |
| 531 case blink::WebInputEvent::TouchMove: | 550 case blink::WebInputEvent::TouchMove: |
| 532 return handle_raf_aligned_touch_input_; | 551 return handle_raf_aligned_touch_input_; |
| 533 default: | 552 default: |
| 534 return false; | 553 return false; |
| 535 } | 554 } |
| 536 } | 555 } |
| 537 | 556 |
| 538 } // namespace content | 557 } // namespace content |
| OLD | NEW |