Change DtmfSenderHandler to handle events on the signaling thread.
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.cc
blob2b8210d2d2f85de8dbb86a381abff5f6f8266be0
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
3 // found in the LICENSE file.
5 #include "cc/scheduler/scheduler_state_machine.h"
7 #include "base/debug/trace_event.h"
8 #include "base/debug/trace_event_argument.h"
9 #include "base/format_macros.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "ui/gfx/frame_time.h"
15 namespace cc {
17 SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings& settings)
18 : settings_(settings),
19 output_surface_state_(OUTPUT_SURFACE_LOST),
20 begin_impl_frame_state_(BEGIN_IMPL_FRAME_STATE_IDLE),
21 commit_state_(COMMIT_STATE_IDLE),
22 forced_redraw_state_(FORCED_REDRAW_STATE_IDLE),
23 commit_count_(0),
24 current_frame_number_(0),
25 last_frame_number_animate_performed_(-1),
26 last_frame_number_swap_performed_(-1),
27 last_frame_number_swap_requested_(-1),
28 last_frame_number_begin_main_frame_sent_(-1),
29 last_frame_number_update_visible_tiles_was_called_(-1),
30 manage_tiles_funnel_(0),
31 consecutive_checkerboard_animations_(0),
32 max_pending_swaps_(1),
33 pending_swaps_(0),
34 needs_redraw_(false),
35 needs_animate_(false),
36 needs_manage_tiles_(false),
37 swap_used_incomplete_tile_(false),
38 needs_commit_(false),
39 inside_poll_for_anticipated_draw_triggers_(false),
40 visible_(false),
41 can_start_(false),
42 can_draw_(false),
43 has_pending_tree_(false),
44 pending_tree_is_ready_for_activation_(false),
45 active_tree_needs_first_draw_(false),
46 did_commit_after_animating_(false),
47 did_create_and_initialize_first_output_surface_(false),
48 impl_latency_takes_priority_(false),
49 skip_next_begin_main_frame_to_reduce_latency_(false),
50 skip_begin_main_frame_to_reduce_latency_(false),
51 continuous_painting_(false),
52 impl_latency_takes_priority_on_battery_(false) {
55 const char* SchedulerStateMachine::OutputSurfaceStateToString(
56 OutputSurfaceState state) {
57 switch (state) {
58 case OUTPUT_SURFACE_ACTIVE:
59 return "OUTPUT_SURFACE_ACTIVE";
60 case OUTPUT_SURFACE_LOST:
61 return "OUTPUT_SURFACE_LOST";
62 case OUTPUT_SURFACE_CREATING:
63 return "OUTPUT_SURFACE_CREATING";
64 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
65 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
66 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
67 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
69 NOTREACHED();
70 return "???";
73 const char* SchedulerStateMachine::BeginImplFrameStateToString(
74 BeginImplFrameState state) {
75 switch (state) {
76 case BEGIN_IMPL_FRAME_STATE_IDLE:
77 return "BEGIN_IMPL_FRAME_STATE_IDLE";
78 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING:
79 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
80 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME:
81 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
82 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE:
83 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
85 NOTREACHED();
86 return "???";
89 const char* SchedulerStateMachine::CommitStateToString(CommitState state) {
90 switch (state) {
91 case COMMIT_STATE_IDLE:
92 return "COMMIT_STATE_IDLE";
93 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT:
94 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
95 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED:
96 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
97 case COMMIT_STATE_READY_TO_COMMIT:
98 return "COMMIT_STATE_READY_TO_COMMIT";
99 case COMMIT_STATE_WAITING_FOR_ACTIVATION:
100 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
102 NOTREACHED();
103 return "???";
106 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
107 ForcedRedrawOnTimeoutState state) {
108 switch (state) {
109 case FORCED_REDRAW_STATE_IDLE:
110 return "FORCED_REDRAW_STATE_IDLE";
111 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT:
112 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
113 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION:
114 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
115 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW:
116 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
118 NOTREACHED();
119 return "???";
122 const char* SchedulerStateMachine::ActionToString(Action action) {
123 switch (action) {
124 case ACTION_NONE:
125 return "ACTION_NONE";
126 case ACTION_ANIMATE:
127 return "ACTION_ANIMATE";
128 case ACTION_SEND_BEGIN_MAIN_FRAME:
129 return "ACTION_SEND_BEGIN_MAIN_FRAME";
130 case ACTION_COMMIT:
131 return "ACTION_COMMIT";
132 case ACTION_UPDATE_VISIBLE_TILES:
133 return "ACTION_UPDATE_VISIBLE_TILES";
134 case ACTION_ACTIVATE_SYNC_TREE:
135 return "ACTION_ACTIVATE_SYNC_TREE";
136 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE:
137 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
138 case ACTION_DRAW_AND_SWAP_FORCED:
139 return "ACTION_DRAW_AND_SWAP_FORCED";
140 case ACTION_DRAW_AND_SWAP_ABORT:
141 return "ACTION_DRAW_AND_SWAP_ABORT";
142 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION:
143 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
144 case ACTION_MANAGE_TILES:
145 return "ACTION_MANAGE_TILES";
147 NOTREACHED();
148 return "???";
151 scoped_refptr<base::debug::ConvertableToTraceFormat>
152 SchedulerStateMachine::AsValue() const {
153 scoped_refptr<base::debug::TracedValue> state =
154 new base::debug::TracedValue();
155 AsValueInto(state.get(), gfx::FrameTime::Now());
156 return state;
159 void SchedulerStateMachine::AsValueInto(base::debug::TracedValue* state,
160 base::TimeTicks now) const {
161 state->BeginDictionary("major_state");
162 state->SetString("next_action", ActionToString(NextAction()));
163 state->SetString("begin_impl_frame_state",
164 BeginImplFrameStateToString(begin_impl_frame_state_));
165 state->SetString("commit_state", CommitStateToString(commit_state_));
166 state->SetString("output_surface_state_",
167 OutputSurfaceStateToString(output_surface_state_));
168 state->SetString("forced_redraw_state",
169 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_));
170 state->EndDictionary();
172 state->BeginDictionary("major_timestamps_in_ms");
173 state->SetDouble("0_interval",
174 begin_impl_frame_args_.interval.InMicroseconds() / 1000.0L);
175 state->SetDouble(
176 "1_now_to_deadline",
177 (begin_impl_frame_args_.deadline - now).InMicroseconds() / 1000.0L);
178 state->SetDouble(
179 "2_frame_time_to_now",
180 (now - begin_impl_frame_args_.frame_time).InMicroseconds() / 1000.0L);
181 state->SetDouble("3_frame_time_to_deadline",
182 (begin_impl_frame_args_.deadline -
183 begin_impl_frame_args_.frame_time).InMicroseconds() /
184 1000.0L);
185 state->SetDouble("4_now",
186 (now - base::TimeTicks()).InMicroseconds() / 1000.0L);
187 state->SetDouble(
188 "5_frame_time",
189 (begin_impl_frame_args_.frame_time - base::TimeTicks()).InMicroseconds() /
190 1000.0L);
191 state->SetDouble(
192 "6_deadline",
193 (begin_impl_frame_args_.deadline - base::TimeTicks()).InMicroseconds() /
194 1000.0L);
195 state->EndDictionary();
197 state->BeginDictionary("minor_state");
198 state->SetInteger("commit_count", commit_count_);
199 state->SetInteger("current_frame_number", current_frame_number_);
201 state->SetInteger("last_frame_number_animate_performed",
202 last_frame_number_animate_performed_);
203 state->SetInteger("last_frame_number_swap_performed",
204 last_frame_number_swap_performed_);
205 state->SetInteger("last_frame_number_swap_requested",
206 last_frame_number_swap_requested_);
207 state->SetInteger("last_frame_number_begin_main_frame_sent",
208 last_frame_number_begin_main_frame_sent_);
209 state->SetInteger("last_frame_number_update_visible_tiles_was_called",
210 last_frame_number_update_visible_tiles_was_called_);
212 state->SetInteger("manage_tiles_funnel", manage_tiles_funnel_);
213 state->SetInteger("consecutive_checkerboard_animations",
214 consecutive_checkerboard_animations_);
215 state->SetInteger("max_pending_swaps_", max_pending_swaps_);
216 state->SetInteger("pending_swaps_", pending_swaps_);
217 state->SetBoolean("needs_redraw", needs_redraw_);
218 state->SetBoolean("needs_animate_", needs_animate_);
219 state->SetBoolean("needs_manage_tiles", needs_manage_tiles_);
220 state->SetBoolean("swap_used_incomplete_tile", swap_used_incomplete_tile_);
221 state->SetBoolean("needs_commit", needs_commit_);
222 state->SetBoolean("visible", visible_);
223 state->SetBoolean("can_start", can_start_);
224 state->SetBoolean("can_draw", can_draw_);
225 state->SetBoolean("has_pending_tree", has_pending_tree_);
226 state->SetBoolean("pending_tree_is_ready_for_activation",
227 pending_tree_is_ready_for_activation_);
228 state->SetBoolean("active_tree_needs_first_draw",
229 active_tree_needs_first_draw_);
230 state->SetBoolean("did_commit_after_animating", did_commit_after_animating_);
231 state->SetBoolean("did_create_and_initialize_first_output_surface",
232 did_create_and_initialize_first_output_surface_);
233 state->SetBoolean("impl_latency_takes_priority",
234 impl_latency_takes_priority_);
235 state->SetBoolean("main_thread_is_in_high_latency_mode",
236 MainThreadIsInHighLatencyMode());
237 state->SetBoolean("skip_begin_main_frame_to_reduce_latency",
238 skip_begin_main_frame_to_reduce_latency_);
239 state->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
240 skip_next_begin_main_frame_to_reduce_latency_);
241 state->SetBoolean("continuous_painting", continuous_painting_);
242 state->SetBoolean("impl_latency_takes_priority_on_battery",
243 impl_latency_takes_priority_on_battery_);
244 state->EndDictionary();
247 void SchedulerStateMachine::AdvanceCurrentFrameNumber() {
248 current_frame_number_++;
250 // "Drain" the ManageTiles funnel.
251 if (manage_tiles_funnel_ > 0)
252 manage_tiles_funnel_--;
254 skip_begin_main_frame_to_reduce_latency_ =
255 skip_next_begin_main_frame_to_reduce_latency_;
256 skip_next_begin_main_frame_to_reduce_latency_ = false;
259 bool SchedulerStateMachine::HasAnimatedThisFrame() const {
260 return last_frame_number_animate_performed_ == current_frame_number_;
263 bool SchedulerStateMachine::HasSentBeginMainFrameThisFrame() const {
264 return current_frame_number_ ==
265 last_frame_number_begin_main_frame_sent_;
268 bool SchedulerStateMachine::HasUpdatedVisibleTilesThisFrame() const {
269 return current_frame_number_ ==
270 last_frame_number_update_visible_tiles_was_called_;
273 bool SchedulerStateMachine::HasSwappedThisFrame() const {
274 return current_frame_number_ == last_frame_number_swap_performed_;
277 bool SchedulerStateMachine::HasRequestedSwapThisFrame() const {
278 return current_frame_number_ == last_frame_number_swap_requested_;
281 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
282 // These are all the cases where we normally cannot or do not want to draw
283 // but, if needs_redraw_ is true and we do not draw to make forward progress,
284 // we might deadlock with the main thread.
285 // This should be a superset of PendingActivationsShouldBeForced() since
286 // activation of the pending tree is blocked by drawing of the active tree and
287 // the main thread might be blocked on activation of the most recent commit.
288 if (PendingActivationsShouldBeForced())
289 return true;
291 // Additional states where we should abort draws.
292 if (!can_draw_)
293 return true;
294 return false;
297 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
298 // There is no output surface to trigger our activations.
299 // If we do not force activations to make forward progress, we might deadlock
300 // with the main thread.
301 if (output_surface_state_ == OUTPUT_SURFACE_LOST)
302 return true;
304 // If we're not visible, we should force activation.
305 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
306 // don't checkerboard until all visible resources are done. Furthermore, if we
307 // do keep the pending tree around, when becoming visible we might activate
308 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
309 // we can simply activate on becoming invisible since we don't need to draw
310 // the active tree when we're in this state.
311 if (!visible_)
312 return true;
314 return false;
317 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
318 // Don't try to initialize too early.
319 if (!can_start_)
320 return false;
322 // We only want to start output surface initialization after the
323 // previous commit is complete.
324 if (commit_state_ != COMMIT_STATE_IDLE)
325 return false;
327 // Make sure the BeginImplFrame from any previous OutputSurfaces
328 // are complete before creating the new OutputSurface.
329 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_IDLE)
330 return false;
332 // We want to clear the pipline of any pending draws and activations
333 // before starting output surface initialization. This allows us to avoid
334 // weird corner cases where we abort draws or force activation while we
335 // are initializing the output surface.
336 if (active_tree_needs_first_draw_ || has_pending_tree_)
337 return false;
339 // We need to create the output surface if we don't have one and we haven't
340 // started creating one yet.
341 return output_surface_state_ == OUTPUT_SURFACE_LOST;
344 bool SchedulerStateMachine::ShouldDraw() const {
345 // If we need to abort draws, we should do so ASAP since the draw could
346 // be blocking other important actions (like output surface initialization),
347 // from occuring. If we are waiting for the first draw, then perfom the
348 // aborted draw to keep things moving. If we are not waiting for the first
349 // draw however, we don't want to abort for no reason.
350 if (PendingDrawsShouldBeAborted())
351 return active_tree_needs_first_draw_;
353 // If a commit has occurred after the animate call, we need to call animate
354 // again before we should draw.
355 if (did_commit_after_animating_)
356 return false;
358 // After this line, we only want to send a swap request once per frame.
359 if (HasRequestedSwapThisFrame())
360 return false;
362 // Do not queue too many swaps.
363 if (pending_swaps_ >= max_pending_swaps_)
364 return false;
366 // Except for the cases above, do not draw outside of the BeginImplFrame
367 // deadline.
368 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
369 return false;
371 // Only handle forced redraws due to timeouts on the regular deadline.
372 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
373 return true;
375 return needs_redraw_;
378 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
379 // There is nothing to activate.
380 if (!has_pending_tree_)
381 return false;
383 // We should not activate a second tree before drawing the first one.
384 // Even if we need to force activation of the pending tree, we should abort
385 // drawing the active tree first.
386 if (active_tree_needs_first_draw_)
387 return false;
389 // If we want to force activation, do so ASAP.
390 if (PendingActivationsShouldBeForced())
391 return true;
393 // At this point, only activate if we are ready to activate.
394 return pending_tree_is_ready_for_activation_;
397 bool SchedulerStateMachine::ShouldUpdateVisibleTiles() const {
398 if (!settings_.impl_side_painting)
399 return false;
400 if (HasUpdatedVisibleTilesThisFrame())
401 return false;
403 // We don't want to update visible tiles right after drawing.
404 if (HasRequestedSwapThisFrame())
405 return false;
407 // There's no reason to check for tiles if we don't have an output surface.
408 if (!HasInitializedOutputSurface())
409 return false;
411 // We should not check for visible tiles until we've entered the deadline so
412 // we check as late as possible and give the tiles more time to initialize.
413 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
414 return false;
416 // If the last swap drew with checkerboard or missing tiles, we should
417 // poll for any new visible tiles so we can be notified to draw again
418 // when there are.
419 if (swap_used_incomplete_tile_)
420 return true;
422 return false;
425 bool SchedulerStateMachine::ShouldAnimate() const {
426 if (!can_draw_)
427 return false;
429 // If a commit occurred after our last call, we need to do animation again.
430 if (HasAnimatedThisFrame() && !did_commit_after_animating_)
431 return false;
433 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING &&
434 begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
435 return false;
437 return needs_redraw_ || needs_animate_;
440 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
441 if (!needs_commit_)
442 return false;
444 // We can not perform commits if we are not visible.
445 if (!visible_)
446 return false;
448 return true;
451 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
452 if (!CouldSendBeginMainFrame())
453 return false;
455 // Only send BeginMainFrame when there isn't another commit pending already.
456 if (commit_state_ != COMMIT_STATE_IDLE)
457 return false;
459 // Don't send BeginMainFrame early if we are prioritizing the active tree
460 // because of impl_latency_takes_priority_.
461 if (impl_latency_takes_priority_ &&
462 (has_pending_tree_ || active_tree_needs_first_draw_)) {
463 return false;
466 // We want to start the first commit after we get a new output surface ASAP.
467 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT)
468 return true;
470 // We should not send BeginMainFrame while we are in
471 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
472 // user input arriving soon.
473 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
474 // thread isn't consuming user input.
475 if (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_IDLE &&
476 BeginFrameNeeded())
477 return false;
479 // We need a new commit for the forced redraw. This honors the
480 // single commit per interval because the result will be swapped to screen.
481 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT)
482 return true;
484 // After this point, we only start a commit once per frame.
485 if (HasSentBeginMainFrameThisFrame())
486 return false;
488 // We shouldn't normally accept commits if there isn't an OutputSurface.
489 if (!HasInitializedOutputSurface())
490 return false;
492 // SwapAck throttle the BeginMainFrames unless we just swapped.
493 // TODO(brianderson): Remove this restriction to improve throughput.
494 bool just_swapped_in_deadline =
495 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE &&
496 HasSwappedThisFrame();
497 if (pending_swaps_ >= max_pending_swaps_ && !just_swapped_in_deadline)
498 return false;
500 if (skip_begin_main_frame_to_reduce_latency_)
501 return false;
503 return true;
506 bool SchedulerStateMachine::ShouldCommit() const {
507 if (commit_state_ != COMMIT_STATE_READY_TO_COMMIT)
508 return false;
510 // We must not finish the commit until the pending tree is free.
511 if (has_pending_tree_) {
512 DCHECK(settings_.main_frame_before_activation_enabled);
513 return false;
516 // Prioritize drawing the previous commit before finishing the next commit.
517 if (active_tree_needs_first_draw_)
518 return false;
520 return true;
523 bool SchedulerStateMachine::ShouldManageTiles() const {
524 // ManageTiles only really needs to be called immediately after commit
525 // and then periodically after that. Use a funnel to make sure we average
526 // one ManageTiles per BeginImplFrame in the long run.
527 if (manage_tiles_funnel_ > 0)
528 return false;
530 // Limiting to once per-frame is not enough, since we only want to
531 // manage tiles _after_ draws. Polling for draw triggers and
532 // begin-frame are mutually exclusive, so we limit to these two cases.
533 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE &&
534 !inside_poll_for_anticipated_draw_triggers_)
535 return false;
536 return needs_manage_tiles_;
539 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const {
540 if (ShouldUpdateVisibleTiles())
541 return ACTION_UPDATE_VISIBLE_TILES;
542 if (ShouldActivatePendingTree())
543 return ACTION_ACTIVATE_SYNC_TREE;
544 if (ShouldCommit())
545 return ACTION_COMMIT;
546 if (ShouldAnimate())
547 return ACTION_ANIMATE;
548 if (ShouldDraw()) {
549 if (PendingDrawsShouldBeAborted())
550 return ACTION_DRAW_AND_SWAP_ABORT;
551 else if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
552 return ACTION_DRAW_AND_SWAP_FORCED;
553 else
554 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE;
556 if (ShouldManageTiles())
557 return ACTION_MANAGE_TILES;
558 if (ShouldSendBeginMainFrame())
559 return ACTION_SEND_BEGIN_MAIN_FRAME;
560 if (ShouldBeginOutputSurfaceCreation())
561 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION;
562 return ACTION_NONE;
565 void SchedulerStateMachine::UpdateState(Action action) {
566 switch (action) {
567 case ACTION_NONE:
568 return;
570 case ACTION_UPDATE_VISIBLE_TILES:
571 last_frame_number_update_visible_tiles_was_called_ =
572 current_frame_number_;
573 return;
575 case ACTION_ACTIVATE_SYNC_TREE:
576 UpdateStateOnActivation();
577 return;
579 case ACTION_ANIMATE:
580 last_frame_number_animate_performed_ = current_frame_number_;
581 needs_animate_ = false;
582 did_commit_after_animating_ = false;
583 // TODO(skyostil): Instead of assuming this, require the client to tell
584 // us.
585 SetNeedsRedraw();
586 return;
588 case ACTION_SEND_BEGIN_MAIN_FRAME:
589 DCHECK(!has_pending_tree_ ||
590 settings_.main_frame_before_activation_enabled);
591 DCHECK(visible_);
592 commit_state_ = COMMIT_STATE_BEGIN_MAIN_FRAME_SENT;
593 needs_commit_ = false;
594 last_frame_number_begin_main_frame_sent_ =
595 current_frame_number_;
596 return;
598 case ACTION_COMMIT: {
599 bool commit_was_aborted = false;
600 UpdateStateOnCommit(commit_was_aborted);
601 return;
604 case ACTION_DRAW_AND_SWAP_FORCED:
605 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE: {
606 bool did_request_swap = true;
607 UpdateStateOnDraw(did_request_swap);
608 return;
611 case ACTION_DRAW_AND_SWAP_ABORT: {
612 bool did_request_swap = false;
613 UpdateStateOnDraw(did_request_swap);
614 return;
617 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION:
618 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST);
619 output_surface_state_ = OUTPUT_SURFACE_CREATING;
621 // The following DCHECKs make sure we are in the proper quiescent state.
622 // The pipeline should be flushed entirely before we start output
623 // surface creation to avoid complicated corner cases.
624 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE);
625 DCHECK(!has_pending_tree_);
626 DCHECK(!active_tree_needs_first_draw_);
627 return;
629 case ACTION_MANAGE_TILES:
630 UpdateStateOnManageTiles();
631 return;
635 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_was_aborted) {
636 commit_count_++;
638 if (!commit_was_aborted && HasAnimatedThisFrame())
639 did_commit_after_animating_ = true;
641 if (commit_was_aborted || settings_.main_frame_before_activation_enabled) {
642 commit_state_ = COMMIT_STATE_IDLE;
643 } else {
644 commit_state_ = settings_.impl_side_painting
645 ? COMMIT_STATE_WAITING_FOR_ACTIVATION
646 : COMMIT_STATE_IDLE;
649 // If we are impl-side-painting but the commit was aborted, then we behave
650 // mostly as if we are not impl-side-painting since there is no pending tree.
651 has_pending_tree_ = settings_.impl_side_painting && !commit_was_aborted;
653 // Update state related to forced draws.
654 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) {
655 forced_redraw_state_ = has_pending_tree_
656 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
657 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
660 // Update the output surface state.
661 DCHECK_NE(output_surface_state_, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION);
662 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT) {
663 if (has_pending_tree_) {
664 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION;
665 } else {
666 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
667 needs_redraw_ = true;
671 // Update state if we have a new active tree to draw, or if the active tree
672 // was unchanged but we need to do a forced draw.
673 if (!has_pending_tree_ &&
674 (!commit_was_aborted ||
675 forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)) {
676 needs_redraw_ = true;
677 active_tree_needs_first_draw_ = true;
680 // This post-commit work is common to both completed and aborted commits.
681 pending_tree_is_ready_for_activation_ = false;
683 if (continuous_painting_)
684 needs_commit_ = true;
687 void SchedulerStateMachine::UpdateStateOnActivation() {
688 if (commit_state_ == COMMIT_STATE_WAITING_FOR_ACTIVATION)
689 commit_state_ = COMMIT_STATE_IDLE;
691 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION)
692 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
694 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION)
695 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
697 has_pending_tree_ = false;
698 pending_tree_is_ready_for_activation_ = false;
699 active_tree_needs_first_draw_ = true;
700 needs_redraw_ = true;
703 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap) {
704 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
705 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
707 needs_redraw_ = false;
708 active_tree_needs_first_draw_ = false;
710 if (did_request_swap)
711 last_frame_number_swap_requested_ = current_frame_number_;
714 void SchedulerStateMachine::UpdateStateOnManageTiles() {
715 needs_manage_tiles_ = false;
718 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
719 TRACE_EVENT_INSTANT0("cc",
720 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
721 TRACE_EVENT_SCOPE_THREAD);
722 skip_next_begin_main_frame_to_reduce_latency_ = true;
725 bool SchedulerStateMachine::BeginFrameNeeded() const {
726 // Proactive BeginFrames are bad for the synchronous compositor because we
727 // have to draw when we get the BeginFrame and could end up drawing many
728 // duplicate frames if our new frame isn't ready in time.
729 // To poll for state with the synchronous compositor without having to draw,
730 // we rely on ShouldPollForAnticipatedDrawTriggers instead.
731 if (!SupportsProactiveBeginFrame())
732 return BeginFrameNeededToAnimateOrDraw();
734 return BeginFrameNeededToAnimateOrDraw() || ProactiveBeginFrameWanted();
737 bool SchedulerStateMachine::ShouldPollForAnticipatedDrawTriggers() const {
738 // ShouldPollForAnticipatedDrawTriggers is what we use in place of
739 // ProactiveBeginFrameWanted when we are using the synchronous
740 // compositor.
741 if (!SupportsProactiveBeginFrame()) {
742 return !BeginFrameNeededToAnimateOrDraw() && ProactiveBeginFrameWanted();
745 // Non synchronous compositors should rely on
746 // ProactiveBeginFrameWanted to poll for state instead.
747 return false;
750 // Note: If SupportsProactiveBeginFrame is false, the scheduler should poll
751 // for changes in it's draw state so it can request a BeginFrame when it's
752 // actually ready.
753 bool SchedulerStateMachine::SupportsProactiveBeginFrame() const {
754 // It is undesirable to proactively request BeginFrames if we are
755 // using a synchronous compositor because we *must* draw for every
756 // BeginFrame, which could cause duplicate draws.
757 return !settings_.using_synchronous_renderer_compositor;
760 // These are the cases where we definitely (or almost definitely) have a
761 // new frame to animate and/or draw and can draw.
762 bool SchedulerStateMachine::BeginFrameNeededToAnimateOrDraw() const {
763 // The output surface is the provider of BeginImplFrames, so we are not going
764 // to get them even if we ask for them.
765 if (!HasInitializedOutputSurface())
766 return false;
768 // If we can't draw, don't tick until we are notified that we can draw again.
769 if (!can_draw_)
770 return false;
772 // The forced draw respects our normal draw scheduling, so we need to
773 // request a BeginImplFrame for it.
774 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
775 return true;
777 // There's no need to produce frames if we are not visible.
778 if (!visible_)
779 return false;
781 // We need to draw a more complete frame than we did the last BeginImplFrame,
782 // so request another BeginImplFrame in anticipation that we will have
783 // additional visible tiles.
784 if (swap_used_incomplete_tile_)
785 return true;
787 if (needs_animate_)
788 return true;
790 return needs_redraw_;
793 // These are cases where we are very likely to draw soon, but might not
794 // actually have a new frame to draw when we receive the next BeginImplFrame.
795 // Proactively requesting the BeginImplFrame helps hide the round trip latency
796 // of the SetNeedsBeginFrame request that has to go to the Browser.
797 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
798 // The output surface is the provider of BeginImplFrames,
799 // so we are not going to get them even if we ask for them.
800 if (!HasInitializedOutputSurface())
801 return false;
803 // Do not be proactive when invisible.
804 if (!visible_)
805 return false;
807 // We should proactively request a BeginImplFrame if a commit is pending
808 // because we will want to draw if the commit completes quickly.
809 if (needs_commit_ || commit_state_ != COMMIT_STATE_IDLE)
810 return true;
812 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
813 // to draw the new active tree.
814 if (has_pending_tree_)
815 return true;
817 // Changing priorities may allow us to activate (given the new priorities),
818 // which may result in a new frame.
819 if (needs_manage_tiles_)
820 return true;
822 // If we just sent a swap request, it's likely that we are going to produce
823 // another frame soon. This helps avoid negative glitches in our
824 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
825 // provider and get sampled at an inopportune time, delaying the next
826 // BeginImplFrame.
827 if (HasRequestedSwapThisFrame())
828 return true;
830 return false;
833 void SchedulerStateMachine::OnBeginImplFrame(const BeginFrameArgs& args) {
834 AdvanceCurrentFrameNumber();
835 begin_impl_frame_args_ = args;
836 DCHECK_EQ(begin_impl_frame_state_, BEGIN_IMPL_FRAME_STATE_IDLE)
837 << AsValue()->ToString();
838 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING;
841 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
842 DCHECK_EQ(begin_impl_frame_state_,
843 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING)
844 << AsValue()->ToString();
845 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME;
848 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
849 DCHECK_EQ(begin_impl_frame_state_, BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME)
850 << AsValue()->ToString();
851 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE;
854 void SchedulerStateMachine::OnBeginImplFrameIdle() {
855 DCHECK_EQ(begin_impl_frame_state_, BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
856 << AsValue()->ToString();
857 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_IDLE;
860 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineEarly() const {
861 // TODO(brianderson): This should take into account multiple commit sources.
863 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME)
864 return false;
866 // If we've lost the output surface, end the current BeginImplFrame ASAP
867 // so we can start creating the next output surface.
868 if (output_surface_state_ == OUTPUT_SURFACE_LOST)
869 return true;
871 // SwapAck throttle the deadline since we wont draw and swap anyway.
872 if (pending_swaps_ >= max_pending_swaps_)
873 return false;
875 if (active_tree_needs_first_draw_)
876 return true;
878 if (!needs_redraw_)
879 return false;
881 // This is used to prioritize impl-thread draws when the main thread isn't
882 // producing anything, e.g., after an aborted commit. We also check that we
883 // don't have a pending tree -- otherwise we should give it a chance to
884 // activate.
885 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
886 if (commit_state_ == COMMIT_STATE_IDLE && !has_pending_tree_)
887 return true;
889 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
890 if (impl_latency_takes_priority_)
891 return true;
893 // If we are on battery power and want to prioritize impl latency because
894 // we don't trust deadline tasks to execute at the right time.
895 if (impl_latency_takes_priority_on_battery_)
896 return true;
898 return false;
901 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
902 // If a commit is pending before the previous commit has been drawn, we
903 // are definitely in a high latency mode.
904 if (CommitPending() && (active_tree_needs_first_draw_ || has_pending_tree_))
905 return true;
907 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
908 // thread is in a low latency mode.
909 if (HasSentBeginMainFrameThisFrame() &&
910 (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING ||
911 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME))
912 return false;
914 // If there's a commit in progress it must either be from the previous frame
915 // or it started after the impl thread's deadline. In either case the main
916 // thread is in high latency mode.
917 if (CommitPending())
918 return true;
920 // Similarly, if there's a pending tree the main thread is in high latency
921 // mode, because either
922 // it's from the previous frame
923 // or
924 // we're currently drawing the active tree and the pending tree will thus
925 // only be drawn in the next frame.
926 if (has_pending_tree_)
927 return true;
929 if (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE) {
930 // Even if there's a new active tree to draw at the deadline or we've just
931 // swapped it, it may have been triggered by a previous BeginImplFrame, in
932 // which case the main thread is in a high latency mode.
933 return (active_tree_needs_first_draw_ || HasSwappedThisFrame()) &&
934 !HasSentBeginMainFrameThisFrame();
937 // If the active tree needs its first draw in any other state, we know the
938 // main thread is in a high latency mode.
939 return active_tree_needs_first_draw_;
942 void SchedulerStateMachine::DidEnterPollForAnticipatedDrawTriggers() {
943 AdvanceCurrentFrameNumber();
944 inside_poll_for_anticipated_draw_triggers_ = true;
947 void SchedulerStateMachine::DidLeavePollForAnticipatedDrawTriggers() {
948 inside_poll_for_anticipated_draw_triggers_ = false;
951 void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; }
953 void SchedulerStateMachine::SetCanDraw(bool can_draw) { can_draw_ = can_draw; }
955 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; }
957 void SchedulerStateMachine::SetNeedsAnimate() {
958 needs_animate_ = true;
961 void SchedulerStateMachine::SetNeedsManageTiles() {
962 if (!needs_manage_tiles_) {
963 TRACE_EVENT0("cc",
964 "SchedulerStateMachine::SetNeedsManageTiles");
965 needs_manage_tiles_ = true;
969 void SchedulerStateMachine::SetMaxSwapsPending(int max) {
970 max_pending_swaps_ = max;
973 void SchedulerStateMachine::DidSwapBuffers() {
974 pending_swaps_++;
975 DCHECK_LE(pending_swaps_, max_pending_swaps_);
977 last_frame_number_swap_performed_ = current_frame_number_;
980 void SchedulerStateMachine::SetSwapUsedIncompleteTile(
981 bool used_incomplete_tile) {
982 swap_used_incomplete_tile_ = used_incomplete_tile;
985 void SchedulerStateMachine::DidSwapBuffersComplete() {
986 DCHECK_GT(pending_swaps_, 0);
987 pending_swaps_--;
990 void SchedulerStateMachine::SetImplLatencyTakesPriority(
991 bool impl_latency_takes_priority) {
992 impl_latency_takes_priority_ = impl_latency_takes_priority;
995 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result) {
996 switch (result) {
997 case INVALID_RESULT:
998 NOTREACHED() << "Uninitialized DrawResult.";
999 break;
1000 case DRAW_ABORTED_CANT_DRAW:
1001 case DRAW_ABORTED_CONTEXT_LOST:
1002 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1003 << result;
1004 break;
1005 case DRAW_SUCCESS:
1006 consecutive_checkerboard_animations_ = 0;
1007 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
1008 break;
1009 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS:
1010 needs_redraw_ = true;
1012 // If we're already in the middle of a redraw, we don't need to
1013 // restart it.
1014 if (forced_redraw_state_ != FORCED_REDRAW_STATE_IDLE)
1015 return;
1017 needs_commit_ = true;
1018 consecutive_checkerboard_animations_++;
1019 if (settings_.timeout_and_draw_when_animation_checkerboards &&
1020 consecutive_checkerboard_animations_ >=
1021 settings_.maximum_number_of_failed_draws_before_draw_is_forced_) {
1022 consecutive_checkerboard_animations_ = 0;
1023 // We need to force a draw, but it doesn't make sense to do this until
1024 // we've committed and have new textures.
1025 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_COMMIT;
1027 break;
1028 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT:
1029 // It's not clear whether this missing content is because of missing
1030 // pictures (which requires a commit) or because of memory pressure
1031 // removing textures (which might not). To be safe, request a commit
1032 // anyway.
1033 needs_commit_ = true;
1034 break;
1038 void SchedulerStateMachine::SetNeedsCommit() {
1039 needs_commit_ = true;
1042 void SchedulerStateMachine::NotifyReadyToCommit() {
1043 DCHECK(commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED)
1044 << AsValue()->ToString();
1045 commit_state_ = COMMIT_STATE_READY_TO_COMMIT;
1048 void SchedulerStateMachine::BeginMainFrameAborted(bool did_handle) {
1049 DCHECK_EQ(commit_state_, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT);
1050 if (did_handle) {
1051 bool commit_was_aborted = true;
1052 UpdateStateOnCommit(commit_was_aborted);
1053 } else {
1054 commit_state_ = COMMIT_STATE_IDLE;
1055 SetNeedsCommit();
1059 void SchedulerStateMachine::DidManageTiles() {
1060 needs_manage_tiles_ = false;
1061 // "Fill" the ManageTiles funnel.
1062 manage_tiles_funnel_++;
1065 void SchedulerStateMachine::DidLoseOutputSurface() {
1066 if (output_surface_state_ == OUTPUT_SURFACE_LOST ||
1067 output_surface_state_ == OUTPUT_SURFACE_CREATING)
1068 return;
1069 output_surface_state_ = OUTPUT_SURFACE_LOST;
1070 needs_redraw_ = false;
1073 void SchedulerStateMachine::NotifyReadyToActivate() {
1074 if (has_pending_tree_)
1075 pending_tree_is_ready_for_activation_ = true;
1078 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1079 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING);
1080 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT;
1082 if (did_create_and_initialize_first_output_surface_) {
1083 // TODO(boliu): See if we can remove this when impl-side painting is always
1084 // on. Does anything on the main thread need to update after recreate?
1085 needs_commit_ = true;
1087 did_create_and_initialize_first_output_surface_ = true;
1088 pending_swaps_ = 0;
1091 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1092 DCHECK_EQ(commit_state_, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT);
1093 commit_state_ = COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED;
1096 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1097 switch (output_surface_state_) {
1098 case OUTPUT_SURFACE_LOST:
1099 case OUTPUT_SURFACE_CREATING:
1100 return false;
1102 case OUTPUT_SURFACE_ACTIVE:
1103 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
1104 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
1105 return true;
1107 NOTREACHED();
1108 return false;
1111 std::string SchedulerStateMachine::GetStatesForDebugging() const {
1112 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1113 needs_commit_ ? 'T' : 'F',
1114 static_cast<int>(output_surface_state_),
1115 static_cast<int>(begin_impl_frame_state_),
1116 static_cast<int>(commit_state_),
1117 has_pending_tree_ ? 'T' : 'F',
1118 pending_tree_is_ready_for_activation_ ? 'T' : 'F',
1119 active_tree_needs_first_draw_ ? 'T' : 'F',
1120 max_pending_swaps_,
1121 pending_swaps_);
1124 } // namespace cc