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

Side by Side Diff: cc/scheduler/scheduler_state_machine.cc

Issue 1139613003: cc: Avoid deadlock with the UI thread and NPAPI in more cases (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address Sunny's comments Created 5 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 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 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 "cc/scheduler/scheduler_state_machine.h" 5 #include "cc/scheduler/scheduler_state_machine.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/trace_event/trace_event.h" 10 #include "base/trace_event/trace_event.h"
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 if (!visible_) 395 if (!visible_)
396 return false; 396 return false;
397 397
398 // Do not make a new commits when it is deferred. 398 // Do not make a new commits when it is deferred.
399 if (defer_commits_) 399 if (defer_commits_)
400 return false; 400 return false;
401 401
402 return true; 402 return true;
403 } 403 }
404 404
405 bool SchedulerStateMachine::SendingBeginMainFrameMightCauseDeadlock() const {
406 // NPAPI is the only case where the UI thread makes synchronous calls to the
407 // Renderer main thread. During that synchronous call, we may not get a
408 // SwapAck for the UI thread, which may prevent BeginMainFrame's from
409 // completing if there's enough back pressure. If the BeginMainFrame can't
410 // make progress, the Renderer can't service the UI thread's synchronous call
411 // and we have deadlock.
412 // This returns true if there's too much backpressure to finish a commit
413 // if we were to initiate a BeginMainFrame.
414 return has_pending_tree_ && active_tree_needs_first_draw_ &&
415 pending_swaps_ >= max_pending_swaps_;
416 }
417
405 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const { 418 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
406 if (!CouldSendBeginMainFrame()) 419 if (!CouldSendBeginMainFrame())
407 return false; 420 return false;
408 421
409 // Do not send begin main frame too many times in a single frame. 422 // Do not send begin main frame too many times in a single frame.
410 if (send_begin_main_frame_funnel_) 423 if (send_begin_main_frame_funnel_)
411 return false; 424 return false;
412 425
413 // Only send BeginMainFrame when there isn't another commit pending already. 426 // Only send BeginMainFrame when there isn't another commit pending already.
427 // Other parts of the state machine indirectly defer the BeginMainFrame
428 // by transitioning to WAITING commit states rather than going
429 // immediately to IDLE.
414 if (commit_state_ != COMMIT_STATE_IDLE) 430 if (commit_state_ != COMMIT_STATE_IDLE)
415 return false; 431 return false;
416 432
417 // Don't send BeginMainFrame early if we are prioritizing the active tree 433 // Don't send BeginMainFrame early if we are prioritizing the active tree
418 // because of impl_latency_takes_priority_. 434 // because of impl_latency_takes_priority_.
419 if (impl_latency_takes_priority_ && 435 if (impl_latency_takes_priority_ &&
420 (has_pending_tree_ || active_tree_needs_first_draw_)) { 436 (has_pending_tree_ || active_tree_needs_first_draw_)) {
421 return false; 437 return false;
422 } 438 }
423 439
424 // We should not send BeginMainFrame while we are in 440 // We should not send BeginMainFrame while we are in
425 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new 441 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
426 // user input arriving soon. 442 // user input arriving soon.
427 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main 443 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
428 // thread isn't consuming user input. 444 // thread isn't consuming user input.
429 if (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_IDLE && 445 if (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_IDLE &&
430 BeginFrameNeeded()) 446 BeginFrameNeeded())
431 return false; 447 return false;
432 448
433 // We need a new commit for the forced redraw. This honors the 449 // We need a new commit for the forced redraw. This honors the
434 // single commit per interval because the result will be swapped to screen. 450 // single commit per interval because the result will be swapped to screen.
451 // TODO(brianderson): Remove this or move it below the
452 // SendingBeginMainFrameMightCauseDeadlock check since we want to avoid
453 // ever returning true from this method if we might cause deadlock.
435 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) 454 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT)
436 return true; 455 return true;
437 456
438 // We shouldn't normally accept commits if there isn't an OutputSurface. 457 // We shouldn't normally accept commits if there isn't an OutputSurface.
439 if (!HasInitializedOutputSurface()) 458 if (!HasInitializedOutputSurface())
440 return false; 459 return false;
441 460
442 // SwapAck throttle the BeginMainFrames unless we just swapped. 461 // Make sure the BeginMainFrame can finish eventually if we start it.
443 // TODO(brianderson): Remove this restriction to improve throughput. 462 if (SendingBeginMainFrameMightCauseDeadlock())
444 bool just_swapped_in_deadline =
445 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE &&
446 did_perform_swap_in_last_draw_;
447 if (pending_swaps_ >= max_pending_swaps_ && !just_swapped_in_deadline)
448 return false; 463 return false;
449 464
465 if (!settings_.main_frame_while_swap_throttled_enabled) {
466 // SwapAck throttle the BeginMainFrames unless we just swapped to
467 // potentially improve impl-thread latency over main-thread throughput.
468 // TODO(brianderson): Remove this restriction to improve throughput or
469 // make it conditional on impl_latency_takes_priority_.
470 bool just_swapped_in_deadline =
471 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE &&
472 did_perform_swap_in_last_draw_;
473 if (pending_swaps_ >= max_pending_swaps_ && !just_swapped_in_deadline)
474 return false;
475 }
476
450 if (skip_begin_main_frame_to_reduce_latency_) 477 if (skip_begin_main_frame_to_reduce_latency_)
451 return false; 478 return false;
452 479
453 return true; 480 return true;
454 } 481 }
455 482
456 bool SchedulerStateMachine::ShouldCommit() const { 483 bool SchedulerStateMachine::ShouldCommit() const {
457 if (commit_state_ != COMMIT_STATE_READY_TO_COMMIT) 484 if (commit_state_ != COMMIT_STATE_READY_TO_COMMIT)
458 return false; 485 return false;
459 486
460 // We must not finish the commit until the pending tree is free. 487 // We must not finish the commit until the pending tree is free.
461 if (has_pending_tree_) { 488 if (has_pending_tree_) {
462 DCHECK(settings_.main_frame_before_activation_enabled); 489 DCHECK(settings_.main_frame_before_activation_enabled);
463 return false; 490 return false;
464 } 491 }
465 492
466 // Prioritize drawing the previous commit before finishing the next commit. 493 // If impl-side-painting, commit to the pending tree as soon as we can.
467 if (active_tree_needs_first_draw_) 494 if (settings_.impl_side_painting)
468 return false; 495 return true;
469 496
470 return true; 497 // If we only have an active tree, it is incorrect to replace it
498 // before we've drawn it when we aren't impl-side-painting.
499 DCHECK(!settings_.impl_side_painting);
500 return !active_tree_needs_first_draw_;
471 } 501 }
472 502
473 bool SchedulerStateMachine::ShouldPrepareTiles() const { 503 bool SchedulerStateMachine::ShouldPrepareTiles() const {
474 // PrepareTiles only really needs to be called immediately after commit 504 // PrepareTiles only really needs to be called immediately after commit
475 // and then periodically after that. Use a funnel to make sure we average 505 // and then periodically after that. Use a funnel to make sure we average
476 // one PrepareTiles per BeginImplFrame in the long run. 506 // one PrepareTiles per BeginImplFrame in the long run.
477 if (prepare_tiles_funnel_ > 0) 507 if (prepare_tiles_funnel_ > 0)
478 return false; 508 return false;
479 509
480 // Limiting to once per-frame is not enough, since we only want to 510 // Limiting to once per-frame is not enough, since we only want to
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 static_cast<int>(begin_impl_frame_state_), 1168 static_cast<int>(begin_impl_frame_state_),
1139 static_cast<int>(commit_state_), 1169 static_cast<int>(commit_state_),
1140 has_pending_tree_ ? 'T' : 'F', 1170 has_pending_tree_ ? 'T' : 'F',
1141 pending_tree_is_ready_for_activation_ ? 'T' : 'F', 1171 pending_tree_is_ready_for_activation_ ? 'T' : 'F',
1142 active_tree_needs_first_draw_ ? 'T' : 'F', 1172 active_tree_needs_first_draw_ ? 'T' : 'F',
1143 max_pending_swaps_, 1173 max_pending_swaps_,
1144 pending_swaps_); 1174 pending_swaps_);
1145 } 1175 }
1146 1176
1147 } // namespace cc 1177 } // namespace cc
OLDNEW
« no previous file with comments | « cc/scheduler/scheduler_state_machine.h ('k') | cc/scheduler/scheduler_state_machine_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698