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/format_macros.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/trace_event/trace_event.h"
11 #include "base/trace_event/trace_event_argument.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 prepare_tiles_funnel_(0),
30 consecutive_checkerboard_animations_(0),
31 max_pending_swaps_(1),
34 needs_animate_(false),
35 needs_prepare_tiles_(false),
37 inside_poll_for_anticipated_draw_triggers_(false),
41 has_pending_tree_(false),
42 pending_tree_is_ready_for_activation_(false),
43 active_tree_needs_first_draw_(false),
44 did_commit_after_animating_(false),
45 did_create_and_initialize_first_output_surface_(false),
46 impl_latency_takes_priority_(false),
47 skip_next_begin_main_frame_to_reduce_latency_(false),
48 skip_begin_main_frame_to_reduce_latency_(false),
49 continuous_painting_(false),
50 children_need_begin_frames_(false),
51 defer_commits_(false) {
54 const char* SchedulerStateMachine::OutputSurfaceStateToString(
55 OutputSurfaceState state
) {
57 case OUTPUT_SURFACE_ACTIVE
:
58 return "OUTPUT_SURFACE_ACTIVE";
59 case OUTPUT_SURFACE_LOST
:
60 return "OUTPUT_SURFACE_LOST";
61 case OUTPUT_SURFACE_CREATING
:
62 return "OUTPUT_SURFACE_CREATING";
63 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
64 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
65 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
66 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
72 const char* SchedulerStateMachine::BeginImplFrameStateToString(
73 BeginImplFrameState state
) {
75 case BEGIN_IMPL_FRAME_STATE_IDLE
:
76 return "BEGIN_IMPL_FRAME_STATE_IDLE";
77 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
:
78 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
79 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
:
80 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
81 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
:
82 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
88 const char* SchedulerStateMachine::CommitStateToString(CommitState state
) {
90 case COMMIT_STATE_IDLE
:
91 return "COMMIT_STATE_IDLE";
92 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
:
93 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
94 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
:
95 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
96 case COMMIT_STATE_READY_TO_COMMIT
:
97 return "COMMIT_STATE_READY_TO_COMMIT";
98 case COMMIT_STATE_WAITING_FOR_ACTIVATION
:
99 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
100 case COMMIT_STATE_WAITING_FOR_DRAW
:
101 return "COMMIT_STATE_WAITING_FOR_DRAW";
107 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
108 ForcedRedrawOnTimeoutState state
) {
110 case FORCED_REDRAW_STATE_IDLE
:
111 return "FORCED_REDRAW_STATE_IDLE";
112 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
:
113 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
114 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
:
115 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
116 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW
:
117 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
123 const char* SchedulerStateMachine::ActionToString(Action action
) {
126 return "ACTION_NONE";
128 return "ACTION_ANIMATE";
129 case ACTION_SEND_BEGIN_MAIN_FRAME
:
130 return "ACTION_SEND_BEGIN_MAIN_FRAME";
132 return "ACTION_COMMIT";
133 case ACTION_ACTIVATE_SYNC_TREE
:
134 return "ACTION_ACTIVATE_SYNC_TREE";
135 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
136 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
137 case ACTION_DRAW_AND_SWAP_FORCED
:
138 return "ACTION_DRAW_AND_SWAP_FORCED";
139 case ACTION_DRAW_AND_SWAP_ABORT
:
140 return "ACTION_DRAW_AND_SWAP_ABORT";
141 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
142 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
143 case ACTION_PREPARE_TILES
:
144 return "ACTION_PREPARE_TILES";
150 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
151 SchedulerStateMachine::AsValue() const {
152 scoped_refptr
<base::trace_event::TracedValue
> state
=
153 new base::trace_event::TracedValue();
154 AsValueInto(state
.get());
158 void SchedulerStateMachine::AsValueInto(
159 base::trace_event::TracedValue
* state
) const {
160 state
->BeginDictionary("major_state");
161 state
->SetString("next_action", ActionToString(NextAction()));
162 state
->SetString("begin_impl_frame_state",
163 BeginImplFrameStateToString(begin_impl_frame_state_
));
164 state
->SetString("commit_state", CommitStateToString(commit_state_
));
165 state
->SetString("output_surface_state_",
166 OutputSurfaceStateToString(output_surface_state_
));
167 state
->SetString("forced_redraw_state",
168 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_
));
169 state
->EndDictionary();
171 state
->BeginDictionary("minor_state");
172 state
->SetInteger("commit_count", commit_count_
);
173 state
->SetInteger("current_frame_number", current_frame_number_
);
175 state
->SetInteger("last_frame_number_animate_performed",
176 last_frame_number_animate_performed_
);
177 state
->SetInteger("last_frame_number_swap_performed",
178 last_frame_number_swap_performed_
);
179 state
->SetInteger("last_frame_number_swap_requested",
180 last_frame_number_swap_requested_
);
181 state
->SetInteger("last_frame_number_begin_main_frame_sent",
182 last_frame_number_begin_main_frame_sent_
);
184 state
->SetInteger("prepare_tiles_funnel", prepare_tiles_funnel_
);
185 state
->SetInteger("consecutive_checkerboard_animations",
186 consecutive_checkerboard_animations_
);
187 state
->SetInteger("max_pending_swaps_", max_pending_swaps_
);
188 state
->SetInteger("pending_swaps_", pending_swaps_
);
189 state
->SetBoolean("needs_redraw", needs_redraw_
);
190 state
->SetBoolean("needs_animate_", needs_animate_
);
191 state
->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_
);
192 state
->SetBoolean("needs_commit", needs_commit_
);
193 state
->SetBoolean("visible", visible_
);
194 state
->SetBoolean("can_start", can_start_
);
195 state
->SetBoolean("can_draw", can_draw_
);
196 state
->SetBoolean("has_pending_tree", has_pending_tree_
);
197 state
->SetBoolean("pending_tree_is_ready_for_activation",
198 pending_tree_is_ready_for_activation_
);
199 state
->SetBoolean("active_tree_needs_first_draw",
200 active_tree_needs_first_draw_
);
201 state
->SetBoolean("did_commit_after_animating", did_commit_after_animating_
);
202 state
->SetBoolean("did_create_and_initialize_first_output_surface",
203 did_create_and_initialize_first_output_surface_
);
204 state
->SetBoolean("impl_latency_takes_priority",
205 impl_latency_takes_priority_
);
206 state
->SetBoolean("main_thread_is_in_high_latency_mode",
207 MainThreadIsInHighLatencyMode());
208 state
->SetBoolean("skip_begin_main_frame_to_reduce_latency",
209 skip_begin_main_frame_to_reduce_latency_
);
210 state
->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
211 skip_next_begin_main_frame_to_reduce_latency_
);
212 state
->SetBoolean("continuous_painting", continuous_painting_
);
213 state
->SetBoolean("children_need_begin_frames", children_need_begin_frames_
);
214 state
->SetBoolean("defer_commits", defer_commits_
);
215 state
->EndDictionary();
218 void SchedulerStateMachine::AdvanceCurrentFrameNumber() {
219 current_frame_number_
++;
221 // "Drain" the PrepareTiles funnel.
222 if (prepare_tiles_funnel_
> 0)
223 prepare_tiles_funnel_
--;
225 skip_begin_main_frame_to_reduce_latency_
=
226 skip_next_begin_main_frame_to_reduce_latency_
;
227 skip_next_begin_main_frame_to_reduce_latency_
= false;
230 bool SchedulerStateMachine::HasAnimatedThisFrame() const {
231 return last_frame_number_animate_performed_
== current_frame_number_
;
234 bool SchedulerStateMachine::HasSentBeginMainFrameThisFrame() const {
235 return current_frame_number_
==
236 last_frame_number_begin_main_frame_sent_
;
239 bool SchedulerStateMachine::HasSwappedThisFrame() const {
240 return current_frame_number_
== last_frame_number_swap_performed_
;
243 bool SchedulerStateMachine::HasRequestedSwapThisFrame() const {
244 return current_frame_number_
== last_frame_number_swap_requested_
;
247 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
248 // These are all the cases where we normally cannot or do not want to draw
249 // but, if needs_redraw_ is true and we do not draw to make forward progress,
250 // we might deadlock with the main thread.
251 // This should be a superset of PendingActivationsShouldBeForced() since
252 // activation of the pending tree is blocked by drawing of the active tree and
253 // the main thread might be blocked on activation of the most recent commit.
254 if (PendingActivationsShouldBeForced())
257 // Additional states where we should abort draws.
263 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
264 // There is no output surface to trigger our activations.
265 // If we do not force activations to make forward progress, we might deadlock
266 // with the main thread.
267 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
270 // If we're not visible, we should force activation.
271 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
272 // don't checkerboard until all visible resources are done. Furthermore, if we
273 // do keep the pending tree around, when becoming visible we might activate
274 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
275 // we can simply activate on becoming invisible since we don't need to draw
276 // the active tree when we're in this state.
283 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
284 // Don't try to initialize too early.
288 // We only want to start output surface initialization after the
289 // previous commit is complete.
290 if (commit_state_
!= COMMIT_STATE_IDLE
)
293 // Make sure the BeginImplFrame from any previous OutputSurfaces
294 // are complete before creating the new OutputSurface.
295 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
298 // We want to clear the pipline of any pending draws and activations
299 // before starting output surface initialization. This allows us to avoid
300 // weird corner cases where we abort draws or force activation while we
301 // are initializing the output surface.
302 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
305 // We need to create the output surface if we don't have one and we haven't
306 // started creating one yet.
307 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
310 bool SchedulerStateMachine::ShouldDraw() const {
311 // If we need to abort draws, we should do so ASAP since the draw could
312 // be blocking other important actions (like output surface initialization),
313 // from occuring. If we are waiting for the first draw, then perfom the
314 // aborted draw to keep things moving. If we are not waiting for the first
315 // draw however, we don't want to abort for no reason.
316 if (PendingDrawsShouldBeAborted())
317 return active_tree_needs_first_draw_
;
319 // Don't draw if we are waiting on the first commit after a surface.
320 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
323 // If a commit has occurred after the animate call, we need to call animate
324 // again before we should draw.
325 if (did_commit_after_animating_
)
328 // After this line, we only want to send a swap request once per frame.
329 if (HasRequestedSwapThisFrame())
332 // Do not queue too many swaps.
333 if (pending_swaps_
>= max_pending_swaps_
)
336 // Except for the cases above, do not draw outside of the BeginImplFrame
338 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
341 // Only handle forced redraws due to timeouts on the regular deadline.
342 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
345 return needs_redraw_
;
348 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
349 // There is nothing to activate.
350 if (!has_pending_tree_
)
353 // We should not activate a second tree before drawing the first one.
354 // Even if we need to force activation of the pending tree, we should abort
355 // drawing the active tree first.
356 if (active_tree_needs_first_draw_
)
359 // If we want to force activation, do so ASAP.
360 if (PendingActivationsShouldBeForced())
363 // At this point, only activate if we are ready to activate.
364 return pending_tree_is_ready_for_activation_
;
367 bool SchedulerStateMachine::ShouldAnimate() const {
368 // Don't animate if we are waiting on the first commit after a surface.
369 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
372 // If a commit occurred after our last call, we need to do animation again.
373 if (HasAnimatedThisFrame() && !did_commit_after_animating_
)
376 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
377 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
380 return needs_redraw_
|| needs_animate_
;
383 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
387 // We can not perform commits if we are not visible.
391 // Do not make a new commits when it is deferred.
398 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
399 if (!CouldSendBeginMainFrame())
402 // Only send BeginMainFrame when there isn't another commit pending already.
403 if (commit_state_
!= COMMIT_STATE_IDLE
)
406 // Don't send BeginMainFrame early if we are prioritizing the active tree
407 // because of impl_latency_takes_priority_.
408 if (impl_latency_takes_priority_
&&
409 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
413 // We should not send BeginMainFrame while we are in
414 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
415 // user input arriving soon.
416 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
417 // thread isn't consuming user input.
418 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
422 // We need a new commit for the forced redraw. This honors the
423 // single commit per interval because the result will be swapped to screen.
424 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
427 // After this point, we only start a commit once per frame.
428 if (HasSentBeginMainFrameThisFrame())
431 // We shouldn't normally accept commits if there isn't an OutputSurface.
432 if (!HasInitializedOutputSurface())
435 // SwapAck throttle the BeginMainFrames unless we just swapped.
436 // TODO(brianderson): Remove this restriction to improve throughput.
437 bool just_swapped_in_deadline
=
438 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
439 HasSwappedThisFrame();
440 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
443 if (skip_begin_main_frame_to_reduce_latency_
)
449 bool SchedulerStateMachine::ShouldCommit() const {
450 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
453 // We must not finish the commit until the pending tree is free.
454 if (has_pending_tree_
) {
455 DCHECK(settings_
.main_frame_before_activation_enabled
);
459 // Prioritize drawing the previous commit before finishing the next commit.
460 if (active_tree_needs_first_draw_
)
466 bool SchedulerStateMachine::ShouldPrepareTiles() const {
467 // PrepareTiles only really needs to be called immediately after commit
468 // and then periodically after that. Use a funnel to make sure we average
469 // one PrepareTiles per BeginImplFrame in the long run.
470 if (prepare_tiles_funnel_
> 0)
473 // Limiting to once per-frame is not enough, since we only want to
474 // prepare tiles _after_ draws. Polling for draw triggers and
475 // begin-frame are mutually exclusive, so we limit to these two cases.
476 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
477 !inside_poll_for_anticipated_draw_triggers_
)
479 return needs_prepare_tiles_
;
482 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
483 if (ShouldActivatePendingTree())
484 return ACTION_ACTIVATE_SYNC_TREE
;
486 return ACTION_COMMIT
;
488 return ACTION_ANIMATE
;
490 if (PendingDrawsShouldBeAborted())
491 return ACTION_DRAW_AND_SWAP_ABORT
;
492 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
493 return ACTION_DRAW_AND_SWAP_FORCED
;
495 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
497 if (ShouldPrepareTiles())
498 return ACTION_PREPARE_TILES
;
499 if (ShouldSendBeginMainFrame())
500 return ACTION_SEND_BEGIN_MAIN_FRAME
;
501 if (ShouldBeginOutputSurfaceCreation())
502 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
506 void SchedulerStateMachine::UpdateState(Action action
) {
511 case ACTION_ACTIVATE_SYNC_TREE
:
512 UpdateStateOnActivation();
516 last_frame_number_animate_performed_
= current_frame_number_
;
517 needs_animate_
= false;
518 did_commit_after_animating_
= false;
519 // TODO(skyostil): Instead of assuming this, require the client to tell
524 case ACTION_SEND_BEGIN_MAIN_FRAME
:
525 DCHECK(!has_pending_tree_
||
526 settings_
.main_frame_before_activation_enabled
);
528 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
529 needs_commit_
= false;
530 last_frame_number_begin_main_frame_sent_
=
531 current_frame_number_
;
534 case ACTION_COMMIT
: {
535 bool commit_has_no_updates
= false;
536 UpdateStateOnCommit(commit_has_no_updates
);
540 case ACTION_DRAW_AND_SWAP_FORCED
:
541 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
542 bool did_request_swap
= true;
543 UpdateStateOnDraw(did_request_swap
);
547 case ACTION_DRAW_AND_SWAP_ABORT
: {
548 bool did_request_swap
= false;
549 UpdateStateOnDraw(did_request_swap
);
553 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
554 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
555 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
557 // The following DCHECKs make sure we are in the proper quiescent state.
558 // The pipeline should be flushed entirely before we start output
559 // surface creation to avoid complicated corner cases.
560 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
561 DCHECK(!has_pending_tree_
);
562 DCHECK(!active_tree_needs_first_draw_
);
565 case ACTION_PREPARE_TILES
:
566 UpdateStateOnPrepareTiles();
571 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates
) {
574 if (!commit_has_no_updates
&& HasAnimatedThisFrame())
575 did_commit_after_animating_
= true;
577 if (commit_has_no_updates
|| settings_
.main_frame_before_activation_enabled
) {
578 commit_state_
= COMMIT_STATE_IDLE
;
579 } else if (settings_
.impl_side_painting
) {
580 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
582 commit_state_
= settings_
.main_thread_should_always_be_low_latency
583 ? COMMIT_STATE_WAITING_FOR_DRAW
587 // If we are impl-side-painting but the commit was aborted, then we behave
588 // mostly as if we are not impl-side-painting since there is no pending tree.
589 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_has_no_updates
;
591 // Update state related to forced draws.
592 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
593 forced_redraw_state_
= has_pending_tree_
594 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
595 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
598 // Update the output surface state.
599 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
600 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
601 if (has_pending_tree_
) {
602 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
604 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
605 needs_redraw_
= true;
609 // Update state if we have a new active tree to draw, or if the active tree
610 // was unchanged but we need to do a forced draw.
611 if (!has_pending_tree_
&&
612 (!commit_has_no_updates
||
613 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
614 needs_redraw_
= true;
615 active_tree_needs_first_draw_
= true;
618 // This post-commit work is common to both completed and aborted commits.
619 pending_tree_is_ready_for_activation_
= false;
621 if (continuous_painting_
)
622 needs_commit_
= true;
625 void SchedulerStateMachine::UpdateStateOnActivation() {
626 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
627 commit_state_
= settings_
.main_thread_should_always_be_low_latency
628 ? COMMIT_STATE_WAITING_FOR_DRAW
632 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
633 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
635 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
636 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
638 has_pending_tree_
= false;
639 pending_tree_is_ready_for_activation_
= false;
640 active_tree_needs_first_draw_
= true;
641 needs_redraw_
= true;
644 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
645 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
646 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
648 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
649 commit_state_
= COMMIT_STATE_IDLE
;
651 needs_redraw_
= false;
652 active_tree_needs_first_draw_
= false;
654 if (did_request_swap
)
655 last_frame_number_swap_requested_
= current_frame_number_
;
658 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
659 needs_prepare_tiles_
= false;
662 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
663 TRACE_EVENT_INSTANT0("cc",
664 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
665 TRACE_EVENT_SCOPE_THREAD
);
666 skip_next_begin_main_frame_to_reduce_latency_
= true;
669 bool SchedulerStateMachine::BeginFrameNeededForChildren() const {
670 if (HasInitializedOutputSurface())
671 return children_need_begin_frames_
;
676 bool SchedulerStateMachine::BeginFrameNeeded() const {
677 // We can't handle BeginFrames when output surface isn't initialized.
678 // TODO(brianderson): Support output surface creation inside a BeginFrame.
679 if (!HasInitializedOutputSurface())
682 if (SupportsProactiveBeginFrame()) {
683 return (BeginFrameNeededToAnimateOrDraw() ||
684 BeginFrameNeededForChildren() ||
685 ProactiveBeginFrameWanted());
688 // Proactive BeginFrames are bad for the synchronous compositor because we
689 // have to draw when we get the BeginFrame and could end up drawing many
690 // duplicate frames if our new frame isn't ready in time.
691 // To poll for state with the synchronous compositor without having to draw,
692 // we rely on ShouldPollForAnticipatedDrawTriggers instead.
693 // Synchronous compositor doesn't have a browser.
694 DCHECK(!children_need_begin_frames_
);
695 return BeginFrameNeededToAnimateOrDraw();
698 bool SchedulerStateMachine::ShouldPollForAnticipatedDrawTriggers() const {
699 // ShouldPollForAnticipatedDrawTriggers is what we use in place of
700 // ProactiveBeginFrameWanted when we are using the synchronous
702 if (!SupportsProactiveBeginFrame()) {
703 return !BeginFrameNeededToAnimateOrDraw() && ProactiveBeginFrameWanted();
706 // Non synchronous compositors should rely on
707 // ProactiveBeginFrameWanted to poll for state instead.
711 // Note: If SupportsProactiveBeginFrame is false, the scheduler should poll
712 // for changes in it's draw state so it can request a BeginFrame when it's
714 bool SchedulerStateMachine::SupportsProactiveBeginFrame() const {
715 // It is undesirable to proactively request BeginFrames if we are
716 // using a synchronous compositor because we *must* draw for every
717 // BeginFrame, which could cause duplicate draws.
718 return !settings_
.using_synchronous_renderer_compositor
;
721 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
722 bool children_need_begin_frames
) {
723 children_need_begin_frames_
= children_need_begin_frames
;
726 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
727 defer_commits_
= defer_commits
;
730 // These are the cases where we definitely (or almost definitely) have a
731 // new frame to animate and/or draw and can draw.
732 bool SchedulerStateMachine::BeginFrameNeededToAnimateOrDraw() const {
733 // The forced draw respects our normal draw scheduling, so we need to
734 // request a BeginImplFrame for it.
735 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
738 return needs_animate_
|| needs_redraw_
;
741 // These are cases where we are very likely to draw soon, but might not
742 // actually have a new frame to draw when we receive the next BeginImplFrame.
743 // Proactively requesting the BeginImplFrame helps hide the round trip latency
744 // of the SetNeedsBeginFrame request that has to go to the Browser.
745 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
746 // Do not be proactive when invisible.
750 // We should proactively request a BeginImplFrame if a commit is pending
751 // because we will want to draw if the commit completes quickly.
752 if (needs_commit_
|| commit_state_
!= COMMIT_STATE_IDLE
)
755 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
756 // to draw the new active tree.
757 if (has_pending_tree_
)
760 // Changing priorities may allow us to activate (given the new priorities),
761 // which may result in a new frame.
762 if (needs_prepare_tiles_
)
765 // If we just sent a swap request, it's likely that we are going to produce
766 // another frame soon. This helps avoid negative glitches in our
767 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
768 // provider and get sampled at an inopportune time, delaying the next
770 if (HasRequestedSwapThisFrame())
776 void SchedulerStateMachine::OnBeginImplFrame() {
777 AdvanceCurrentFrameNumber();
778 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_IDLE
)
779 << AsValue()->ToString();
780 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
783 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
784 DCHECK_EQ(begin_impl_frame_state_
,
785 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
786 << AsValue()->ToString();
787 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
790 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
791 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
792 << AsValue()->ToString();
793 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
796 void SchedulerStateMachine::OnBeginImplFrameIdle() {
797 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
798 << AsValue()->ToString();
799 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
802 SchedulerStateMachine::BeginImplFrameDeadlineMode
803 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
804 if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
805 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
806 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
807 // We have an animation or fast input path on the impl thread that wants
808 // to draw, so don't wait too long for a new active tree.
809 // If we are swap throttled we should wait until we are unblocked.
810 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
812 // The impl thread doesn't have anything it wants to draw and we are just
813 // waiting for a new active tree or we are swap throttled. In short we are
815 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
819 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
821 // TODO(brianderson): This should take into account multiple commit sources.
823 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
826 // If we've lost the output surface, end the current BeginImplFrame ASAP
827 // so we can start creating the next output surface.
828 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
831 // SwapAck throttle the deadline since we wont draw and swap anyway.
832 if (pending_swaps_
>= max_pending_swaps_
)
835 if (active_tree_needs_first_draw_
)
841 // This is used to prioritize impl-thread draws when the main thread isn't
842 // producing anything, e.g., after an aborted commit. We also check that we
843 // don't have a pending tree -- otherwise we should give it a chance to
845 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
846 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
849 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
850 if (impl_latency_takes_priority_
)
856 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
857 // If a commit is pending before the previous commit has been drawn, we
858 // are definitely in a high latency mode.
859 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
862 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
863 // thread is in a low latency mode.
864 if (HasSentBeginMainFrameThisFrame() &&
865 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
866 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
869 // If there's a commit in progress it must either be from the previous frame
870 // or it started after the impl thread's deadline. In either case the main
871 // thread is in high latency mode.
875 // Similarly, if there's a pending tree the main thread is in high latency
876 // mode, because either
877 // it's from the previous frame
879 // we're currently drawing the active tree and the pending tree will thus
880 // only be drawn in the next frame.
881 if (has_pending_tree_
)
884 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
885 // Even if there's a new active tree to draw at the deadline or we've just
886 // swapped it, it may have been triggered by a previous BeginImplFrame, in
887 // which case the main thread is in a high latency mode.
888 return (active_tree_needs_first_draw_
|| HasSwappedThisFrame()) &&
889 !HasSentBeginMainFrameThisFrame();
892 // If the active tree needs its first draw in any other state, we know the
893 // main thread is in a high latency mode.
894 return active_tree_needs_first_draw_
;
897 void SchedulerStateMachine::DidEnterPollForAnticipatedDrawTriggers() {
898 AdvanceCurrentFrameNumber();
899 inside_poll_for_anticipated_draw_triggers_
= true;
902 void SchedulerStateMachine::DidLeavePollForAnticipatedDrawTriggers() {
903 inside_poll_for_anticipated_draw_triggers_
= false;
906 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
908 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
910 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
912 void SchedulerStateMachine::SetNeedsAnimate() {
913 needs_animate_
= true;
916 void SchedulerStateMachine::SetNeedsPrepareTiles() {
917 if (!needs_prepare_tiles_
) {
918 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
919 needs_prepare_tiles_
= true;
923 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
924 max_pending_swaps_
= max
;
927 void SchedulerStateMachine::DidSwapBuffers() {
929 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
931 last_frame_number_swap_performed_
= current_frame_number_
;
934 void SchedulerStateMachine::DidSwapBuffersComplete() {
935 DCHECK_GT(pending_swaps_
, 0);
939 void SchedulerStateMachine::SetImplLatencyTakesPriority(
940 bool impl_latency_takes_priority
) {
941 impl_latency_takes_priority_
= impl_latency_takes_priority
;
944 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
947 NOTREACHED() << "Uninitialized DrawResult.";
949 case DRAW_ABORTED_CANT_DRAW
:
950 case DRAW_ABORTED_CONTEXT_LOST
:
951 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
955 consecutive_checkerboard_animations_
= 0;
956 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
958 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
959 needs_redraw_
= true;
961 // If we're already in the middle of a redraw, we don't need to
963 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
966 needs_commit_
= true;
967 consecutive_checkerboard_animations_
++;
968 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
969 consecutive_checkerboard_animations_
>=
970 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
971 consecutive_checkerboard_animations_
= 0;
972 // We need to force a draw, but it doesn't make sense to do this until
973 // we've committed and have new textures.
974 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
977 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
978 // It's not clear whether this missing content is because of missing
979 // pictures (which requires a commit) or because of memory pressure
980 // removing textures (which might not). To be safe, request a commit
982 needs_commit_
= true;
987 void SchedulerStateMachine::SetNeedsCommit() {
988 needs_commit_
= true;
991 void SchedulerStateMachine::NotifyReadyToCommit() {
992 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
993 << AsValue()->ToString();
994 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
995 // In main thread low latency mode, commit should happen right after
996 // BeginFrame, meaning when this function is called, next action should be
998 if (settings_
.main_thread_should_always_be_low_latency
)
999 DCHECK(ShouldCommit());
1002 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1003 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1005 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1006 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1007 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1008 commit_state_
= COMMIT_STATE_IDLE
;
1011 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1012 bool commit_has_no_updates
= true;
1013 UpdateStateOnCommit(commit_has_no_updates
);
1018 void SchedulerStateMachine::DidPrepareTiles() {
1019 needs_prepare_tiles_
= false;
1020 // "Fill" the PrepareTiles funnel.
1021 prepare_tiles_funnel_
++;
1024 void SchedulerStateMachine::DidLoseOutputSurface() {
1025 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1026 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1028 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1029 needs_redraw_
= false;
1032 void SchedulerStateMachine::NotifyReadyToActivate() {
1033 if (has_pending_tree_
)
1034 pending_tree_is_ready_for_activation_
= true;
1037 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1038 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1039 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1041 if (did_create_and_initialize_first_output_surface_
) {
1042 // TODO(boliu): See if we can remove this when impl-side painting is always
1043 // on. Does anything on the main thread need to update after recreate?
1044 needs_commit_
= true;
1046 did_create_and_initialize_first_output_surface_
= true;
1050 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1051 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1052 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1055 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1056 switch (output_surface_state_
) {
1057 case OUTPUT_SURFACE_LOST
:
1058 case OUTPUT_SURFACE_CREATING
:
1061 case OUTPUT_SURFACE_ACTIVE
:
1062 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1063 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1070 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1071 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1072 needs_commit_
? 'T' : 'F',
1073 static_cast<int>(output_surface_state_
),
1074 static_cast<int>(begin_impl_frame_state_
),
1075 static_cast<int>(commit_state_
),
1076 has_pending_tree_
? 'T' : 'F',
1077 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1078 active_tree_needs_first_draw_
? 'T' : 'F',