| 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 // Don't process rAF aligned events at tail of queue. |
| 423 | 427 while (events_to_process > 0 && |
| 424 // Now take any raf aligned events that are at the tail of the queue | 428 IsRafAlignedEvent(shared_state_.events_.at(events_to_process - 1))) { |
| 425 // and put them back. | 429 --events_to_process; |
| 426 while (!events_to_process.empty()) { | |
| 427 if (!IsRafAlignedEvent(events_to_process.back())) | |
| 428 break; | |
| 429 shared_state_.events_.emplace_front(std::move(events_to_process.back())); | |
| 430 events_to_process.pop_back(); | |
| 431 } | 430 } |
| 432 } | 431 } |
| 433 while (!events_to_process.empty()) { | 432 |
| 434 in_flight_event_ = std::move(events_to_process.front()); | 433 while (events_to_process--) { |
| 435 events_to_process.pop_front(); | 434 { |
| 435 base::AutoLock lock(shared_state_lock_); |
| 436 if (shared_state_.events_.empty()) |
| 437 return; |
| 438 in_flight_event_ = shared_state_.events_.Pop(); |
| 439 } |
| 440 |
| 441 // Dispatching the event is outside of critical section. |
| 436 DispatchInFlightEvent(); | 442 DispatchInFlightEvent(); |
| 437 } | 443 } |
| 438 PossiblyScheduleMainFrame(); | 444 PossiblyScheduleMainFrame(); |
| 439 } | 445 } |
| 440 | 446 |
| 441 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, | 447 void MainThreadEventQueue::EventHandled(blink::WebInputEvent::Type type, |
| 442 blink::WebInputEventResult result, | 448 blink::WebInputEventResult result, |
| 443 InputEventAckState ack_result) { | 449 InputEventAckState ack_result) { |
| 444 if (in_flight_event_) { | 450 if (in_flight_event_) { |
| 445 in_flight_event_->EventHandled(routing_id_, renderer_scheduler_, client_, | 451 in_flight_event_->EventHandled(routing_id_, renderer_scheduler_, client_, |
| 446 type, result, ack_result); | 452 type, result, ack_result); |
| 447 } | 453 } |
| 448 } | 454 } |
| 449 | 455 |
| 450 void MainThreadEventQueue::DispatchRafAlignedInput(base::TimeTicks frame_time) { | 456 void MainThreadEventQueue::DispatchRafAlignedInput(base::TimeTicks frame_time) { |
| 451 if (IsRafAlignedInputDisabled()) | 457 if (IsRafAlignedInputDisabled()) |
| 452 return; | 458 return; |
| 453 | 459 |
| 454 std::deque<std::unique_ptr<MainThreadEventQueueTask>> events_to_process; | 460 size_t queue_size_at_start; |
| 461 |
| 462 // Record the queue size so that we only process |
| 463 // that maximum number of events. |
| 455 { | 464 { |
| 456 base::AutoLock lock(shared_state_lock_); | 465 base::AutoLock lock(shared_state_lock_); |
| 457 shared_state_.sent_main_frame_request_ = false; | 466 shared_state_.sent_main_frame_request_ = false; |
| 467 queue_size_at_start = shared_state_.events_.size(); |
| 468 } |
| 458 | 469 |
| 459 while (!shared_state_.events_.empty()) { | 470 while (queue_size_at_start--) { |
| 471 { |
| 472 base::AutoLock lock(shared_state_lock_); |
| 473 |
| 474 if (shared_state_.events_.empty()) |
| 475 return; |
| 476 |
| 460 if (IsRafAlignedEvent(shared_state_.events_.front())) { | 477 if (IsRafAlignedEvent(shared_state_.events_.front())) { |
| 461 // Throttle touchmoves that are async. | 478 // Throttle touchmoves that are async. |
| 462 if (handle_raf_aligned_touch_input_ && | 479 if (handle_raf_aligned_touch_input_ && |
| 463 IsAsyncTouchMove(shared_state_.events_.front())) { | 480 IsAsyncTouchMove(shared_state_.events_.front())) { |
| 464 if (shared_state_.events_.size() == 1 && | 481 if (shared_state_.events_.size() == 1 && |
| 465 frame_time < shared_state_.last_async_touch_move_timestamp_ + | 482 frame_time < shared_state_.last_async_touch_move_timestamp_ + |
| 466 kAsyncTouchMoveInterval) { | 483 kAsyncTouchMoveInterval) { |
| 467 break; | 484 break; |
| 468 } | 485 } |
| 469 shared_state_.last_async_touch_move_timestamp_ = frame_time; | 486 shared_state_.last_async_touch_move_timestamp_ = frame_time; |
| 470 } | 487 } |
| 471 } | 488 } |
| 472 events_to_process.emplace_back(shared_state_.events_.Pop()); | 489 in_flight_event_ = shared_state_.events_.Pop(); |
| 473 } | 490 } |
| 491 |
| 492 // Dispatching the event is outside of critical section. |
| 493 DispatchInFlightEvent(); |
| 474 } | 494 } |
| 475 | 495 |
| 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(); | 496 PossiblyScheduleMainFrame(); |
| 482 } | 497 } |
| 483 | 498 |
| 484 void MainThreadEventQueue::PostTaskToMainThread() { | 499 void MainThreadEventQueue::PostTaskToMainThread() { |
| 485 main_task_runner_->PostTask( | 500 main_task_runner_->PostTask( |
| 486 FROM_HERE, base::Bind(&MainThreadEventQueue::DispatchEvents, this)); | 501 FROM_HERE, base::Bind(&MainThreadEventQueue::DispatchEvents, this)); |
| 487 } | 502 } |
| 488 | 503 |
| 489 void MainThreadEventQueue::QueueEvent( | 504 void MainThreadEventQueue::QueueEvent( |
| 490 std::unique_ptr<MainThreadEventQueueTask> event) { | 505 std::unique_ptr<MainThreadEventQueueTask> event) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 529 case blink::WebInputEvent::MouseWheel: | 544 case blink::WebInputEvent::MouseWheel: |
| 530 return handle_raf_aligned_mouse_input_; | 545 return handle_raf_aligned_mouse_input_; |
| 531 case blink::WebInputEvent::TouchMove: | 546 case blink::WebInputEvent::TouchMove: |
| 532 return handle_raf_aligned_touch_input_; | 547 return handle_raf_aligned_touch_input_; |
| 533 default: | 548 default: |
| 534 return false; | 549 return false; |
| 535 } | 550 } |
| 536 } | 551 } |
| 537 | 552 |
| 538 } // namespace content | 553 } // namespace content |
| OLD | NEW |