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"
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
),
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),
35 needs_animate_(false),
36 needs_manage_tiles_(false),
37 swap_used_incomplete_tile_(false),
39 inside_poll_for_anticipated_draw_triggers_(false),
43 has_pending_tree_(false),
44 pending_tree_is_ready_for_activation_(false),
45 active_tree_needs_first_draw_(false),
46 did_create_and_initialize_first_output_surface_(false),
47 impl_latency_takes_priority_(false),
48 skip_next_begin_main_frame_to_reduce_latency_(false),
49 skip_begin_main_frame_to_reduce_latency_(false),
50 continuous_painting_(false) {
53 const char* SchedulerStateMachine::OutputSurfaceStateToString(
54 OutputSurfaceState state
) {
56 case OUTPUT_SURFACE_ACTIVE
:
57 return "OUTPUT_SURFACE_ACTIVE";
58 case OUTPUT_SURFACE_LOST
:
59 return "OUTPUT_SURFACE_LOST";
60 case OUTPUT_SURFACE_CREATING
:
61 return "OUTPUT_SURFACE_CREATING";
62 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
63 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
64 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
65 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
71 const char* SchedulerStateMachine::BeginImplFrameStateToString(
72 BeginImplFrameState state
) {
74 case BEGIN_IMPL_FRAME_STATE_IDLE
:
75 return "BEGIN_IMPL_FRAME_STATE_IDLE";
76 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
:
77 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
78 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
:
79 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
80 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
:
81 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
87 const char* SchedulerStateMachine::CommitStateToString(CommitState state
) {
89 case COMMIT_STATE_IDLE
:
90 return "COMMIT_STATE_IDLE";
91 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
:
92 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
93 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
:
94 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
95 case COMMIT_STATE_READY_TO_COMMIT
:
96 return "COMMIT_STATE_READY_TO_COMMIT";
97 case COMMIT_STATE_WAITING_FOR_ACTIVATION
:
98 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
104 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
105 ForcedRedrawOnTimeoutState state
) {
107 case FORCED_REDRAW_STATE_IDLE
:
108 return "FORCED_REDRAW_STATE_IDLE";
109 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
:
110 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
111 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
:
112 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
113 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW
:
114 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
120 const char* SchedulerStateMachine::ActionToString(Action action
) {
123 return "ACTION_NONE";
125 return "ACTION_ANIMATE";
126 case ACTION_SEND_BEGIN_MAIN_FRAME
:
127 return "ACTION_SEND_BEGIN_MAIN_FRAME";
129 return "ACTION_COMMIT";
130 case ACTION_UPDATE_VISIBLE_TILES
:
131 return "ACTION_UPDATE_VISIBLE_TILES";
132 case ACTION_ACTIVATE_SYNC_TREE
:
133 return "ACTION_ACTIVATE_SYNC_TREE";
134 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
135 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
136 case ACTION_DRAW_AND_SWAP_FORCED
:
137 return "ACTION_DRAW_AND_SWAP_FORCED";
138 case ACTION_DRAW_AND_SWAP_ABORT
:
139 return "ACTION_DRAW_AND_SWAP_ABORT";
140 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
141 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
142 case ACTION_MANAGE_TILES
:
143 return "ACTION_MANAGE_TILES";
149 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
150 SchedulerStateMachine::AsValue() const {
151 scoped_refptr
<base::debug::TracedValue
> state
=
152 new base::debug::TracedValue();
153 AsValueInto(state
.get(), gfx::FrameTime::Now());
157 void SchedulerStateMachine::AsValueInto(base::debug::TracedValue
* state
,
158 base::TimeTicks now
) const {
159 state
->BeginDictionary("major_state");
160 state
->SetString("next_action", ActionToString(NextAction()));
161 state
->SetString("begin_impl_frame_state",
162 BeginImplFrameStateToString(begin_impl_frame_state_
));
163 state
->SetString("commit_state", CommitStateToString(commit_state_
));
164 state
->SetString("output_surface_state_",
165 OutputSurfaceStateToString(output_surface_state_
));
166 state
->SetString("forced_redraw_state",
167 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_
));
168 state
->EndDictionary();
170 state
->BeginDictionary("major_timestamps_in_ms");
171 state
->SetDouble("0_interval",
172 begin_impl_frame_args_
.interval
.InMicroseconds() / 1000.0L);
175 (begin_impl_frame_args_
.deadline
- now
).InMicroseconds() / 1000.0L);
177 "2_frame_time_to_now",
178 (now
- begin_impl_frame_args_
.frame_time
).InMicroseconds() / 1000.0L);
179 state
->SetDouble("3_frame_time_to_deadline",
180 (begin_impl_frame_args_
.deadline
-
181 begin_impl_frame_args_
.frame_time
).InMicroseconds() /
183 state
->SetDouble("4_now",
184 (now
- base::TimeTicks()).InMicroseconds() / 1000.0L);
187 (begin_impl_frame_args_
.frame_time
- base::TimeTicks()).InMicroseconds() /
191 (begin_impl_frame_args_
.deadline
- base::TimeTicks()).InMicroseconds() /
193 state
->EndDictionary();
195 state
->BeginDictionary("minor_state");
196 state
->SetInteger("commit_count", commit_count_
);
197 state
->SetInteger("current_frame_number", current_frame_number_
);
199 state
->SetInteger("last_frame_number_animate_performed",
200 last_frame_number_animate_performed_
);
201 state
->SetInteger("last_frame_number_swap_performed",
202 last_frame_number_swap_performed_
);
203 state
->SetInteger("last_frame_number_swap_requested",
204 last_frame_number_swap_requested_
);
205 state
->SetInteger("last_frame_number_begin_main_frame_sent",
206 last_frame_number_begin_main_frame_sent_
);
207 state
->SetInteger("last_frame_number_update_visible_tiles_was_called",
208 last_frame_number_update_visible_tiles_was_called_
);
210 state
->SetInteger("manage_tiles_funnel", manage_tiles_funnel_
);
211 state
->SetInteger("consecutive_checkerboard_animations",
212 consecutive_checkerboard_animations_
);
213 state
->SetInteger("max_pending_swaps_", max_pending_swaps_
);
214 state
->SetInteger("pending_swaps_", pending_swaps_
);
215 state
->SetBoolean("needs_redraw", needs_redraw_
);
216 state
->SetBoolean("needs_animate_", needs_animate_
);
217 state
->SetBoolean("needs_manage_tiles", needs_manage_tiles_
);
218 state
->SetBoolean("swap_used_incomplete_tile", swap_used_incomplete_tile_
);
219 state
->SetBoolean("needs_commit", needs_commit_
);
220 state
->SetBoolean("visible", visible_
);
221 state
->SetBoolean("can_start", can_start_
);
222 state
->SetBoolean("can_draw", can_draw_
);
223 state
->SetBoolean("has_pending_tree", has_pending_tree_
);
224 state
->SetBoolean("pending_tree_is_ready_for_activation",
225 pending_tree_is_ready_for_activation_
);
226 state
->SetBoolean("active_tree_needs_first_draw",
227 active_tree_needs_first_draw_
);
228 state
->SetBoolean("did_create_and_initialize_first_output_surface",
229 did_create_and_initialize_first_output_surface_
);
230 state
->SetBoolean("impl_latency_takes_priority",
231 impl_latency_takes_priority_
);
232 state
->SetBoolean("main_thread_is_in_high_latency_mode",
233 MainThreadIsInHighLatencyMode());
234 state
->SetBoolean("skip_begin_main_frame_to_reduce_latency",
235 skip_begin_main_frame_to_reduce_latency_
);
236 state
->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
237 skip_next_begin_main_frame_to_reduce_latency_
);
238 state
->SetBoolean("continuous_painting", continuous_painting_
);
239 state
->EndDictionary();
242 void SchedulerStateMachine::AdvanceCurrentFrameNumber() {
243 current_frame_number_
++;
245 // "Drain" the ManageTiles funnel.
246 if (manage_tiles_funnel_
> 0)
247 manage_tiles_funnel_
--;
249 skip_begin_main_frame_to_reduce_latency_
=
250 skip_next_begin_main_frame_to_reduce_latency_
;
251 skip_next_begin_main_frame_to_reduce_latency_
= false;
254 bool SchedulerStateMachine::HasSentBeginMainFrameThisFrame() const {
255 return current_frame_number_
==
256 last_frame_number_begin_main_frame_sent_
;
259 bool SchedulerStateMachine::HasUpdatedVisibleTilesThisFrame() const {
260 return current_frame_number_
==
261 last_frame_number_update_visible_tiles_was_called_
;
264 bool SchedulerStateMachine::HasSwappedThisFrame() const {
265 return current_frame_number_
== last_frame_number_swap_performed_
;
268 bool SchedulerStateMachine::HasRequestedSwapThisFrame() const {
269 return current_frame_number_
== last_frame_number_swap_requested_
;
272 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
273 // These are all the cases where we normally cannot or do not want to draw
274 // but, if needs_redraw_ is true and we do not draw to make forward progress,
275 // we might deadlock with the main thread.
276 // This should be a superset of PendingActivationsShouldBeForced() since
277 // activation of the pending tree is blocked by drawing of the active tree and
278 // the main thread might be blocked on activation of the most recent commit.
279 if (PendingActivationsShouldBeForced())
282 // Additional states where we should abort draws.
288 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
289 // There is no output surface to trigger our activations.
290 // If we do not force activations to make forward progress, we might deadlock
291 // with the main thread.
292 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
295 // If we're not visible, we should force activation.
296 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
297 // don't checkerboard until all visible resources are done. Furthermore, if we
298 // do keep the pending tree around, when becoming visible we might activate
299 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
300 // we can simply activate on becoming invisible since we don't need to draw
301 // the active tree when we're in this state.
308 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
309 // Don't try to initialize too early.
313 // We only want to start output surface initialization after the
314 // previous commit is complete.
315 if (commit_state_
!= COMMIT_STATE_IDLE
)
318 // Make sure the BeginImplFrame from any previous OutputSurfaces
319 // are complete before creating the new OutputSurface.
320 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
323 // We want to clear the pipline of any pending draws and activations
324 // before starting output surface initialization. This allows us to avoid
325 // weird corner cases where we abort draws or force activation while we
326 // are initializing the output surface.
327 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
330 // We need to create the output surface if we don't have one and we haven't
331 // started creating one yet.
332 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
335 bool SchedulerStateMachine::ShouldDraw() const {
336 // If we need to abort draws, we should do so ASAP since the draw could
337 // be blocking other important actions (like output surface initialization),
338 // from occuring. If we are waiting for the first draw, then perfom the
339 // aborted draw to keep things moving. If we are not waiting for the first
340 // draw however, we don't want to abort for no reason.
341 if (PendingDrawsShouldBeAborted())
342 return active_tree_needs_first_draw_
;
344 // After this line, we only want to send a swap request once per frame.
345 if (HasRequestedSwapThisFrame())
348 // Do not queue too many swaps.
349 if (pending_swaps_
>= max_pending_swaps_
)
352 // Except for the cases above, do not draw outside of the BeginImplFrame
354 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
357 // Only handle forced redraws due to timeouts on the regular deadline.
358 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
361 return needs_redraw_
;
364 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
365 // There is nothing to activate.
366 if (!has_pending_tree_
)
369 // We should not activate a second tree before drawing the first one.
370 // Even if we need to force activation of the pending tree, we should abort
371 // drawing the active tree first.
372 if (active_tree_needs_first_draw_
)
375 // If we want to force activation, do so ASAP.
376 if (PendingActivationsShouldBeForced())
379 // At this point, only activate if we are ready to activate.
380 return pending_tree_is_ready_for_activation_
;
383 bool SchedulerStateMachine::ShouldUpdateVisibleTiles() const {
384 if (!settings_
.impl_side_painting
)
386 if (HasUpdatedVisibleTilesThisFrame())
389 // We don't want to update visible tiles right after drawing.
390 if (HasRequestedSwapThisFrame())
393 // There's no reason to check for tiles if we don't have an output surface.
394 if (!HasInitializedOutputSurface())
397 // We should not check for visible tiles until we've entered the deadline so
398 // we check as late as possible and give the tiles more time to initialize.
399 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
402 // If the last swap drew with checkerboard or missing tiles, we should
403 // poll for any new visible tiles so we can be notified to draw again
405 if (swap_used_incomplete_tile_
)
411 bool SchedulerStateMachine::ShouldAnimate() const {
415 if (last_frame_number_animate_performed_
== current_frame_number_
)
418 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
419 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
422 return needs_redraw_
|| needs_animate_
;
425 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
429 // We can not perform commits if we are not visible.
436 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
437 if (!CouldSendBeginMainFrame())
440 // Only send BeginMainFrame when there isn't another commit pending already.
441 if (commit_state_
!= COMMIT_STATE_IDLE
)
444 // Don't send BeginMainFrame early if we are prioritizing the active tree
445 // because of impl_latency_takes_priority_.
446 if (impl_latency_takes_priority_
&&
447 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
451 // We want to start the first commit after we get a new output surface ASAP.
452 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
)
455 // We should not send BeginMainFrame while we are in
456 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
457 // user input arriving soon.
458 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
459 // thread isn't consuming user input.
460 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
464 // We need a new commit for the forced redraw. This honors the
465 // single commit per interval because the result will be swapped to screen.
466 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
469 // After this point, we only start a commit once per frame.
470 if (HasSentBeginMainFrameThisFrame())
473 // We shouldn't normally accept commits if there isn't an OutputSurface.
474 if (!HasInitializedOutputSurface())
477 // SwapAck throttle the BeginMainFrames unless we just swapped.
478 // TODO(brianderson): Remove this restriction to improve throughput.
479 bool just_swapped_in_deadline
=
480 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
481 HasSwappedThisFrame();
482 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
485 if (skip_begin_main_frame_to_reduce_latency_
)
491 bool SchedulerStateMachine::ShouldCommit() const {
492 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
495 // We must not finish the commit until the pending tree is free.
496 if (has_pending_tree_
) {
497 DCHECK(settings_
.main_frame_before_activation_enabled
);
501 // Prioritize drawing the previous commit before finishing the next commit.
502 if (active_tree_needs_first_draw_
)
508 bool SchedulerStateMachine::ShouldManageTiles() const {
509 // ManageTiles only really needs to be called immediately after commit
510 // and then periodically after that. Use a funnel to make sure we average
511 // one ManageTiles per BeginImplFrame in the long run.
512 if (manage_tiles_funnel_
> 0)
515 // Limiting to once per-frame is not enough, since we only want to
516 // manage tiles _after_ draws. Polling for draw triggers and
517 // begin-frame are mutually exclusive, so we limit to these two cases.
518 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
519 !inside_poll_for_anticipated_draw_triggers_
)
521 return needs_manage_tiles_
;
524 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
525 if (ShouldUpdateVisibleTiles())
526 return ACTION_UPDATE_VISIBLE_TILES
;
527 if (ShouldActivatePendingTree())
528 return ACTION_ACTIVATE_SYNC_TREE
;
530 return ACTION_COMMIT
;
532 return ACTION_ANIMATE
;
534 if (PendingDrawsShouldBeAborted())
535 return ACTION_DRAW_AND_SWAP_ABORT
;
536 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
537 return ACTION_DRAW_AND_SWAP_FORCED
;
539 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
541 if (ShouldManageTiles())
542 return ACTION_MANAGE_TILES
;
543 if (ShouldSendBeginMainFrame())
544 return ACTION_SEND_BEGIN_MAIN_FRAME
;
545 if (ShouldBeginOutputSurfaceCreation())
546 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
550 void SchedulerStateMachine::UpdateState(Action action
) {
555 case ACTION_UPDATE_VISIBLE_TILES
:
556 last_frame_number_update_visible_tiles_was_called_
=
557 current_frame_number_
;
560 case ACTION_ACTIVATE_SYNC_TREE
:
561 UpdateStateOnActivation();
565 last_frame_number_animate_performed_
= current_frame_number_
;
566 needs_animate_
= false;
567 // TODO(skyostil): Instead of assuming this, require the client to tell
572 case ACTION_SEND_BEGIN_MAIN_FRAME
:
573 DCHECK(!has_pending_tree_
||
574 settings_
.main_frame_before_activation_enabled
);
576 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
577 needs_commit_
= false;
578 last_frame_number_begin_main_frame_sent_
=
579 current_frame_number_
;
582 case ACTION_COMMIT
: {
583 bool commit_was_aborted
= false;
584 UpdateStateOnCommit(commit_was_aborted
);
588 case ACTION_DRAW_AND_SWAP_FORCED
:
589 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
590 bool did_request_swap
= true;
591 UpdateStateOnDraw(did_request_swap
);
595 case ACTION_DRAW_AND_SWAP_ABORT
: {
596 bool did_request_swap
= false;
597 UpdateStateOnDraw(did_request_swap
);
601 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
602 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
603 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
605 // The following DCHECKs make sure we are in the proper quiescent state.
606 // The pipeline should be flushed entirely before we start output
607 // surface creation to avoid complicated corner cases.
608 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
609 DCHECK(!has_pending_tree_
);
610 DCHECK(!active_tree_needs_first_draw_
);
613 case ACTION_MANAGE_TILES
:
614 UpdateStateOnManageTiles();
619 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_was_aborted
) {
622 if (commit_was_aborted
|| settings_
.main_frame_before_activation_enabled
) {
623 commit_state_
= COMMIT_STATE_IDLE
;
625 commit_state_
= settings_
.impl_side_painting
626 ? COMMIT_STATE_WAITING_FOR_ACTIVATION
630 // If we are impl-side-painting but the commit was aborted, then we behave
631 // mostly as if we are not impl-side-painting since there is no pending tree.
632 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_was_aborted
;
634 // Update state related to forced draws.
635 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
636 forced_redraw_state_
= has_pending_tree_
637 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
638 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
641 // Update the output surface state.
642 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
643 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
644 if (has_pending_tree_
) {
645 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
647 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
648 needs_redraw_
= true;
652 // Update state if we have a new active tree to draw, or if the active tree
653 // was unchanged but we need to do a forced draw.
654 if (!has_pending_tree_
&&
655 (!commit_was_aborted
||
656 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
657 needs_redraw_
= true;
658 active_tree_needs_first_draw_
= true;
661 // This post-commit work is common to both completed and aborted commits.
662 pending_tree_is_ready_for_activation_
= false;
664 if (continuous_painting_
)
665 needs_commit_
= true;
668 void SchedulerStateMachine::UpdateStateOnActivation() {
669 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
)
670 commit_state_
= COMMIT_STATE_IDLE
;
672 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
673 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
675 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
676 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
678 has_pending_tree_
= false;
679 pending_tree_is_ready_for_activation_
= false;
680 active_tree_needs_first_draw_
= true;
681 needs_redraw_
= true;
684 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
685 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
686 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
688 needs_redraw_
= false;
689 active_tree_needs_first_draw_
= false;
691 if (did_request_swap
)
692 last_frame_number_swap_requested_
= current_frame_number_
;
695 void SchedulerStateMachine::UpdateStateOnManageTiles() {
696 needs_manage_tiles_
= false;
699 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
700 TRACE_EVENT_INSTANT0("cc",
701 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
702 TRACE_EVENT_SCOPE_THREAD
);
703 skip_next_begin_main_frame_to_reduce_latency_
= true;
706 bool SchedulerStateMachine::BeginFrameNeeded() const {
707 // Proactive BeginFrames are bad for the synchronous compositor because we
708 // have to draw when we get the BeginFrame and could end up drawing many
709 // duplicate frames if our new frame isn't ready in time.
710 // To poll for state with the synchronous compositor without having to draw,
711 // we rely on ShouldPollForAnticipatedDrawTriggers instead.
712 if (!SupportsProactiveBeginFrame())
713 return BeginFrameNeededToAnimateOrDraw();
715 return BeginFrameNeededToAnimateOrDraw() || ProactiveBeginFrameWanted();
718 bool SchedulerStateMachine::ShouldPollForAnticipatedDrawTriggers() const {
719 // ShouldPollForAnticipatedDrawTriggers is what we use in place of
720 // ProactiveBeginFrameWanted when we are using the synchronous
722 if (!SupportsProactiveBeginFrame()) {
723 return !BeginFrameNeededToAnimateOrDraw() && ProactiveBeginFrameWanted();
726 // Non synchronous compositors should rely on
727 // ProactiveBeginFrameWanted to poll for state instead.
731 // Note: If SupportsProactiveBeginFrame is false, the scheduler should poll
732 // for changes in it's draw state so it can request a BeginFrame when it's
734 bool SchedulerStateMachine::SupportsProactiveBeginFrame() const {
735 // It is undesirable to proactively request BeginFrames if we are
736 // using a synchronous compositor because we *must* draw for every
737 // BeginFrame, which could cause duplicate draws.
738 return !settings_
.using_synchronous_renderer_compositor
;
741 // These are the cases where we definitely (or almost definitely) have a
742 // new frame to animate and/or draw and can draw.
743 bool SchedulerStateMachine::BeginFrameNeededToAnimateOrDraw() const {
744 // The output surface is the provider of BeginImplFrames, so we are not going
745 // to get them even if we ask for them.
746 if (!HasInitializedOutputSurface())
749 // If we can't draw, don't tick until we are notified that we can draw again.
753 // The forced draw respects our normal draw scheduling, so we need to
754 // request a BeginImplFrame for it.
755 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
758 // There's no need to produce frames if we are not visible.
762 // We need to draw a more complete frame than we did the last BeginImplFrame,
763 // so request another BeginImplFrame in anticipation that we will have
764 // additional visible tiles.
765 if (swap_used_incomplete_tile_
)
771 return needs_redraw_
;
774 // These are cases where we are very likely to draw soon, but might not
775 // actually have a new frame to draw when we receive the next BeginImplFrame.
776 // Proactively requesting the BeginImplFrame helps hide the round trip latency
777 // of the SetNeedsBeginFrame request that has to go to the Browser.
778 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
779 // The output surface is the provider of BeginImplFrames,
780 // so we are not going to get them even if we ask for them.
781 if (!HasInitializedOutputSurface())
784 // Do not be proactive when invisible.
788 // We should proactively request a BeginImplFrame if a commit is pending
789 // because we will want to draw if the commit completes quickly.
790 if (needs_commit_
|| commit_state_
!= COMMIT_STATE_IDLE
)
793 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
794 // to draw the new active tree.
795 if (has_pending_tree_
)
798 // Changing priorities may allow us to activate (given the new priorities),
799 // which may result in a new frame.
800 if (needs_manage_tiles_
)
803 // If we just sent a swap request, it's likely that we are going to produce
804 // another frame soon. This helps avoid negative glitches in our
805 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
806 // provider and get sampled at an inopportune time, delaying the next
808 if (HasRequestedSwapThisFrame())
814 void SchedulerStateMachine::OnBeginImplFrame(const BeginFrameArgs
& args
) {
815 AdvanceCurrentFrameNumber();
816 begin_impl_frame_args_
= args
;
817 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_IDLE
)
818 << AsValue()->ToString();
819 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
822 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
823 DCHECK_EQ(begin_impl_frame_state_
,
824 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
825 << AsValue()->ToString();
826 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
829 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
830 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
831 << AsValue()->ToString();
832 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
835 void SchedulerStateMachine::OnBeginImplFrameIdle() {
836 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
837 << AsValue()->ToString();
838 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
841 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineEarly() const {
842 // TODO(brianderson): This should take into account multiple commit sources.
844 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
847 // If we've lost the output surface, end the current BeginImplFrame ASAP
848 // so we can start creating the next output surface.
849 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
852 // SwapAck throttle the deadline since we wont draw and swap anyway.
853 if (pending_swaps_
>= max_pending_swaps_
)
856 if (active_tree_needs_first_draw_
)
862 // This is used to prioritize impl-thread draws when the main thread isn't
863 // producing anything, e.g., after an aborted commit. We also check that we
864 // don't have a pending tree -- otherwise we should give it a chance to
866 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
867 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
870 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
871 if (impl_latency_takes_priority_
)
877 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
878 // If a commit is pending before the previous commit has been drawn, we
879 // are definitely in a high latency mode.
880 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
883 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
884 // thread is in a low latency mode.
885 if (HasSentBeginMainFrameThisFrame() &&
886 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
887 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
890 // If there's a commit in progress it must either be from the previous frame
891 // or it started after the impl thread's deadline. In either case the main
892 // thread is in high latency mode.
896 // Similarly, if there's a pending tree the main thread is in high latency
897 // mode, because either
898 // it's from the previous frame
900 // we're currently drawing the active tree and the pending tree will thus
901 // only be drawn in the next frame.
902 if (has_pending_tree_
)
905 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
906 // Even if there's a new active tree to draw at the deadline or we've just
907 // swapped it, it may have been triggered by a previous BeginImplFrame, in
908 // which case the main thread is in a high latency mode.
909 return (active_tree_needs_first_draw_
|| HasSwappedThisFrame()) &&
910 !HasSentBeginMainFrameThisFrame();
913 // If the active tree needs its first draw in any other state, we know the
914 // main thread is in a high latency mode.
915 return active_tree_needs_first_draw_
;
918 void SchedulerStateMachine::DidEnterPollForAnticipatedDrawTriggers() {
919 AdvanceCurrentFrameNumber();
920 inside_poll_for_anticipated_draw_triggers_
= true;
923 void SchedulerStateMachine::DidLeavePollForAnticipatedDrawTriggers() {
924 inside_poll_for_anticipated_draw_triggers_
= false;
927 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
929 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
931 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
933 void SchedulerStateMachine::SetNeedsAnimate() {
934 needs_animate_
= true;
937 void SchedulerStateMachine::SetNeedsManageTiles() {
938 if (!needs_manage_tiles_
) {
940 "SchedulerStateMachine::SetNeedsManageTiles");
941 needs_manage_tiles_
= true;
945 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
946 max_pending_swaps_
= max
;
949 void SchedulerStateMachine::DidSwapBuffers() {
951 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
953 last_frame_number_swap_performed_
= current_frame_number_
;
956 void SchedulerStateMachine::SetSwapUsedIncompleteTile(
957 bool used_incomplete_tile
) {
958 swap_used_incomplete_tile_
= used_incomplete_tile
;
961 void SchedulerStateMachine::DidSwapBuffersComplete() {
962 DCHECK_GT(pending_swaps_
, 0);
966 void SchedulerStateMachine::SetImplLatencyTakesPriority(
967 bool impl_latency_takes_priority
) {
968 impl_latency_takes_priority_
= impl_latency_takes_priority
;
971 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
974 NOTREACHED() << "Uninitialized DrawResult.";
976 case DRAW_ABORTED_CANT_DRAW
:
977 case DRAW_ABORTED_CONTEXT_LOST
:
978 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
982 consecutive_checkerboard_animations_
= 0;
983 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
985 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
986 needs_redraw_
= true;
988 // If we're already in the middle of a redraw, we don't need to
990 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
993 needs_commit_
= true;
994 consecutive_checkerboard_animations_
++;
995 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
996 consecutive_checkerboard_animations_
>=
997 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
998 consecutive_checkerboard_animations_
= 0;
999 // We need to force a draw, but it doesn't make sense to do this until
1000 // we've committed and have new textures.
1001 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1004 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1005 // It's not clear whether this missing content is because of missing
1006 // pictures (which requires a commit) or because of memory pressure
1007 // removing textures (which might not). To be safe, request a commit
1009 needs_commit_
= true;
1014 void SchedulerStateMachine::SetNeedsCommit() {
1015 needs_commit_
= true;
1018 void SchedulerStateMachine::NotifyReadyToCommit() {
1019 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1020 << AsValue()->ToString();
1021 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1024 void SchedulerStateMachine::BeginMainFrameAborted(bool did_handle
) {
1025 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1027 bool commit_was_aborted
= true;
1028 UpdateStateOnCommit(commit_was_aborted
);
1030 commit_state_
= COMMIT_STATE_IDLE
;
1035 void SchedulerStateMachine::DidManageTiles() {
1036 needs_manage_tiles_
= false;
1037 // "Fill" the ManageTiles funnel.
1038 manage_tiles_funnel_
++;
1041 void SchedulerStateMachine::DidLoseOutputSurface() {
1042 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1043 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1045 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1046 needs_redraw_
= false;
1049 void SchedulerStateMachine::NotifyReadyToActivate() {
1050 if (has_pending_tree_
)
1051 pending_tree_is_ready_for_activation_
= true;
1054 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1055 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1056 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1058 if (did_create_and_initialize_first_output_surface_
) {
1059 // TODO(boliu): See if we can remove this when impl-side painting is always
1060 // on. Does anything on the main thread need to update after recreate?
1061 needs_commit_
= true;
1063 did_create_and_initialize_first_output_surface_
= true;
1067 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1068 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1069 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1072 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1073 switch (output_surface_state_
) {
1074 case OUTPUT_SURFACE_LOST
:
1075 case OUTPUT_SURFACE_CREATING
:
1078 case OUTPUT_SURFACE_ACTIVE
:
1079 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1080 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1087 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1088 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1089 needs_commit_
? 'T' : 'F',
1090 static_cast<int>(output_surface_state_
),
1091 static_cast<int>(begin_impl_frame_state_
),
1092 static_cast<int>(commit_state_
),
1093 has_pending_tree_
? 'T' : 'F',
1094 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1095 active_tree_needs_first_draw_
? 'T' : 'F',