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 last_frame_number_invalidate_output_surface_performed_(-1),
30 animate_funnel_(false),
31 request_swap_funnel_(false),
32 send_begin_main_frame_funnel_(false),
33 invalidate_output_surface_funnel_(false),
34 prepare_tiles_funnel_(0),
35 consecutive_checkerboard_animations_(0),
36 max_pending_swaps_(1),
39 needs_animate_(false),
40 needs_prepare_tiles_(false),
45 has_pending_tree_(false),
46 pending_tree_is_ready_for_activation_(false),
47 active_tree_needs_first_draw_(false),
48 did_create_and_initialize_first_output_surface_(false),
49 impl_latency_takes_priority_(false),
50 skip_next_begin_main_frame_to_reduce_latency_(false),
51 skip_begin_main_frame_to_reduce_latency_(false),
52 continuous_painting_(false),
53 children_need_begin_frames_(false),
54 defer_commits_(false),
55 last_commit_had_no_updates_(false),
56 wait_for_active_tree_ready_to_draw_(false),
57 did_request_swap_in_last_frame_(false),
58 did_perform_swap_in_last_draw_(false) {
61 const char* SchedulerStateMachine::OutputSurfaceStateToString(
62 OutputSurfaceState state
) {
64 case OUTPUT_SURFACE_ACTIVE
:
65 return "OUTPUT_SURFACE_ACTIVE";
66 case OUTPUT_SURFACE_LOST
:
67 return "OUTPUT_SURFACE_LOST";
68 case OUTPUT_SURFACE_CREATING
:
69 return "OUTPUT_SURFACE_CREATING";
70 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
71 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
72 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
73 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
79 const char* SchedulerStateMachine::BeginImplFrameStateToString(
80 BeginImplFrameState state
) {
82 case BEGIN_IMPL_FRAME_STATE_IDLE
:
83 return "BEGIN_IMPL_FRAME_STATE_IDLE";
84 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
:
85 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
86 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
:
87 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
88 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
:
89 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
95 const char* SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
96 BeginImplFrameDeadlineMode mode
) {
98 case BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
:
99 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE";
100 case BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
:
101 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE";
102 case BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
:
103 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR";
104 case BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
:
105 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE";
106 case BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
:
107 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW";
113 const char* SchedulerStateMachine::CommitStateToString(CommitState state
) {
115 case COMMIT_STATE_IDLE
:
116 return "COMMIT_STATE_IDLE";
117 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
:
118 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
119 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
:
120 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
121 case COMMIT_STATE_READY_TO_COMMIT
:
122 return "COMMIT_STATE_READY_TO_COMMIT";
123 case COMMIT_STATE_WAITING_FOR_ACTIVATION
:
124 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
125 case COMMIT_STATE_WAITING_FOR_DRAW
:
126 return "COMMIT_STATE_WAITING_FOR_DRAW";
132 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
133 ForcedRedrawOnTimeoutState state
) {
135 case FORCED_REDRAW_STATE_IDLE
:
136 return "FORCED_REDRAW_STATE_IDLE";
137 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
:
138 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
139 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
:
140 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
141 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW
:
142 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
148 const char* SchedulerStateMachine::ActionToString(Action action
) {
151 return "ACTION_NONE";
153 return "ACTION_ANIMATE";
154 case ACTION_SEND_BEGIN_MAIN_FRAME
:
155 return "ACTION_SEND_BEGIN_MAIN_FRAME";
157 return "ACTION_COMMIT";
158 case ACTION_ACTIVATE_SYNC_TREE
:
159 return "ACTION_ACTIVATE_SYNC_TREE";
160 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
161 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
162 case ACTION_DRAW_AND_SWAP_FORCED
:
163 return "ACTION_DRAW_AND_SWAP_FORCED";
164 case ACTION_DRAW_AND_SWAP_ABORT
:
165 return "ACTION_DRAW_AND_SWAP_ABORT";
166 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
167 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
168 case ACTION_PREPARE_TILES
:
169 return "ACTION_PREPARE_TILES";
170 case ACTION_INVALIDATE_OUTPUT_SURFACE
:
171 return "ACTION_INVALIDATE_OUTPUT_SURFACE";
177 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
178 SchedulerStateMachine::AsValue() const {
179 scoped_refptr
<base::trace_event::TracedValue
> state
=
180 new base::trace_event::TracedValue();
181 AsValueInto(state
.get());
185 void SchedulerStateMachine::AsValueInto(
186 base::trace_event::TracedValue
* state
) const {
187 state
->BeginDictionary("major_state");
188 state
->SetString("next_action", ActionToString(NextAction()));
189 state
->SetString("begin_impl_frame_state",
190 BeginImplFrameStateToString(begin_impl_frame_state_
));
191 state
->SetString("commit_state", CommitStateToString(commit_state_
));
192 state
->SetString("output_surface_state_",
193 OutputSurfaceStateToString(output_surface_state_
));
194 state
->SetString("forced_redraw_state",
195 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_
));
196 state
->EndDictionary();
198 state
->BeginDictionary("minor_state");
199 state
->SetInteger("commit_count", commit_count_
);
200 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
->SetBoolean("funnel: animate_funnel", animate_funnel_
);
210 state
->SetBoolean("funnel: request_swap_funnel", request_swap_funnel_
);
211 state
->SetBoolean("funnel: send_begin_main_frame_funnel",
212 send_begin_main_frame_funnel_
);
213 state
->SetInteger("funnel: prepare_tiles_funnel", prepare_tiles_funnel_
);
214 state
->SetBoolean("funnel: invalidate_output_surface_funnel",
215 invalidate_output_surface_funnel_
);
216 state
->SetInteger("consecutive_checkerboard_animations",
217 consecutive_checkerboard_animations_
);
218 state
->SetInteger("max_pending_swaps_", max_pending_swaps_
);
219 state
->SetInteger("pending_swaps_", pending_swaps_
);
220 state
->SetBoolean("needs_redraw", needs_redraw_
);
221 state
->SetBoolean("needs_animate_", needs_animate_
);
222 state
->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_
);
223 state
->SetBoolean("needs_commit", needs_commit_
);
224 state
->SetBoolean("visible", visible_
);
225 state
->SetBoolean("can_start", can_start_
);
226 state
->SetBoolean("can_draw", can_draw_
);
227 state
->SetBoolean("has_pending_tree", has_pending_tree_
);
228 state
->SetBoolean("pending_tree_is_ready_for_activation",
229 pending_tree_is_ready_for_activation_
);
230 state
->SetBoolean("active_tree_needs_first_draw",
231 active_tree_needs_first_draw_
);
232 state
->SetBoolean("wait_for_active_tree_ready_to_draw",
233 wait_for_active_tree_ready_to_draw_
);
234 state
->SetBoolean("did_create_and_initialize_first_output_surface",
235 did_create_and_initialize_first_output_surface_
);
236 state
->SetBoolean("impl_latency_takes_priority",
237 impl_latency_takes_priority_
);
238 state
->SetBoolean("main_thread_is_in_high_latency_mode",
239 MainThreadIsInHighLatencyMode());
240 state
->SetBoolean("skip_begin_main_frame_to_reduce_latency",
241 skip_begin_main_frame_to_reduce_latency_
);
242 state
->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
243 skip_next_begin_main_frame_to_reduce_latency_
);
244 state
->SetBoolean("continuous_painting", continuous_painting_
);
245 state
->SetBoolean("children_need_begin_frames", children_need_begin_frames_
);
246 state
->SetBoolean("defer_commits", defer_commits_
);
247 state
->SetBoolean("last_commit_had_no_updates", last_commit_had_no_updates_
);
248 state
->SetBoolean("did_request_swap_in_last_frame",
249 did_request_swap_in_last_frame_
);
250 state
->SetBoolean("did_perform_swap_in_last_draw",
251 did_perform_swap_in_last_draw_
);
252 state
->EndDictionary();
255 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
256 // These are all the cases where we normally cannot or do not want to draw
257 // but, if needs_redraw_ is true and we do not draw to make forward progress,
258 // we might deadlock with the main thread.
259 // This should be a superset of PendingActivationsShouldBeForced() since
260 // activation of the pending tree is blocked by drawing of the active tree and
261 // the main thread might be blocked on activation of the most recent commit.
262 if (PendingActivationsShouldBeForced())
265 // Additional states where we should abort draws.
271 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
272 // There is no output surface to trigger our activations.
273 // If we do not force activations to make forward progress, we might deadlock
274 // with the main thread.
275 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
278 // If we're not visible, we should force activation.
279 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
280 // don't checkerboard until all visible resources are done. Furthermore, if we
281 // do keep the pending tree around, when becoming visible we might activate
282 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
283 // we can simply activate on becoming invisible since we don't need to draw
284 // the active tree when we're in this state.
291 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
292 // Don't try to initialize too early.
296 // We only want to start output surface initialization after the
297 // previous commit is complete.
298 if (commit_state_
!= COMMIT_STATE_IDLE
)
301 // Make sure the BeginImplFrame from any previous OutputSurfaces
302 // are complete before creating the new OutputSurface.
303 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
306 // We want to clear the pipline of any pending draws and activations
307 // before starting output surface initialization. This allows us to avoid
308 // weird corner cases where we abort draws or force activation while we
309 // are initializing the output surface.
310 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
313 // We need to create the output surface if we don't have one and we haven't
314 // started creating one yet.
315 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
318 bool SchedulerStateMachine::ShouldDraw() const {
319 // If we need to abort draws, we should do so ASAP since the draw could
320 // be blocking other important actions (like output surface initialization),
321 // from occuring. If we are waiting for the first draw, then perfom the
322 // aborted draw to keep things moving. If we are not waiting for the first
323 // draw however, we don't want to abort for no reason.
324 if (PendingDrawsShouldBeAborted())
325 return active_tree_needs_first_draw_
;
327 // Do not draw too many times in a single frame. It's okay that we don't check
328 // this before checking for aborted draws because aborted draws do not request
330 if (request_swap_funnel_
)
333 // Don't draw if we are waiting on the first commit after a surface.
334 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
337 // Do not queue too many swaps.
338 if (pending_swaps_
>= max_pending_swaps_
)
341 // Except for the cases above, do not draw outside of the BeginImplFrame
343 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
346 // Only handle forced redraws due to timeouts on the regular deadline.
347 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
350 return needs_redraw_
;
353 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
354 // There is nothing to activate.
355 if (!has_pending_tree_
)
358 // We should not activate a second tree before drawing the first one.
359 // Even if we need to force activation of the pending tree, we should abort
360 // drawing the active tree first.
361 if (active_tree_needs_first_draw_
)
364 // If we want to force activation, do so ASAP.
365 if (PendingActivationsShouldBeForced())
368 // At this point, only activate if we are ready to activate.
369 return pending_tree_is_ready_for_activation_
;
372 bool SchedulerStateMachine::ShouldAnimate() const {
373 // Do not animate too many times in a single frame.
377 // Don't animate if we are waiting on the first commit after a surface.
378 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
381 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
382 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
385 return needs_redraw_
|| needs_animate_
;
388 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
389 // Do not send begin main frame too many times in a single frame.
390 if (send_begin_main_frame_funnel_
)
396 // We can not perform commits if we are not visible.
400 // Do not make a new commits when it is deferred.
407 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
408 if (!CouldSendBeginMainFrame())
411 // Only send BeginMainFrame when there isn't another commit pending already.
412 if (commit_state_
!= COMMIT_STATE_IDLE
)
415 // Don't send BeginMainFrame early if we are prioritizing the active tree
416 // because of impl_latency_takes_priority_.
417 if (impl_latency_takes_priority_
&&
418 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
422 // We should not send BeginMainFrame while we are in
423 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
424 // user input arriving soon.
425 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
426 // thread isn't consuming user input.
427 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
431 // We need a new commit for the forced redraw. This honors the
432 // single commit per interval because the result will be swapped to screen.
433 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
436 // We shouldn't normally accept commits if there isn't an OutputSurface.
437 if (!HasInitializedOutputSurface())
440 // SwapAck throttle the BeginMainFrames unless we just swapped.
441 // TODO(brianderson): Remove this restriction to improve throughput.
442 bool just_swapped_in_deadline
=
443 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
444 did_perform_swap_in_last_draw_
;
445 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
448 if (skip_begin_main_frame_to_reduce_latency_
)
454 bool SchedulerStateMachine::ShouldCommit() const {
455 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
458 // We must not finish the commit until the pending tree is free.
459 if (has_pending_tree_
) {
460 DCHECK(settings_
.main_frame_before_activation_enabled
);
464 // Prioritize drawing the previous commit before finishing the next commit.
465 if (active_tree_needs_first_draw_
)
471 bool SchedulerStateMachine::ShouldPrepareTiles() const {
472 // PrepareTiles only really needs to be called immediately after commit
473 // and then periodically after that. Use a funnel to make sure we average
474 // one PrepareTiles per BeginImplFrame in the long run.
475 if (prepare_tiles_funnel_
> 0)
478 // Limiting to once per-frame is not enough, since we only want to
479 // prepare tiles _after_ draws.
480 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
483 return needs_prepare_tiles_
;
486 bool SchedulerStateMachine::ShouldInvalidateOutputSurface() const {
487 // Do not invalidate too many times in a frame.
488 if (invalidate_output_surface_funnel_
)
491 // Only the synchronous compositor requires invalidations.
492 if (!settings_
.using_synchronous_renderer_compositor
)
495 // Invalidations are only performed inside a BeginFrame.
496 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
499 // TODO(sunnyps): needs_prepare_tiles_ is needed here because PrepareTiles is
500 // called only inside the deadline / draw phase. We could remove this if we
501 // allowed PrepareTiles to happen in OnBeginImplFrame.
502 return needs_redraw_
|| needs_prepare_tiles_
;
505 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
506 if (ShouldActivatePendingTree())
507 return ACTION_ACTIVATE_SYNC_TREE
;
509 return ACTION_COMMIT
;
511 return ACTION_ANIMATE
;
513 if (PendingDrawsShouldBeAborted())
514 return ACTION_DRAW_AND_SWAP_ABORT
;
515 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
516 return ACTION_DRAW_AND_SWAP_FORCED
;
518 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
520 if (ShouldPrepareTiles())
521 return ACTION_PREPARE_TILES
;
522 if (ShouldSendBeginMainFrame())
523 return ACTION_SEND_BEGIN_MAIN_FRAME
;
524 if (ShouldInvalidateOutputSurface())
525 return ACTION_INVALIDATE_OUTPUT_SURFACE
;
526 if (ShouldBeginOutputSurfaceCreation())
527 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
531 void SchedulerStateMachine::UpdateState(Action action
) {
536 case ACTION_ACTIVATE_SYNC_TREE
:
537 UpdateStateOnActivation();
541 UpdateStateOnAnimate();
544 case ACTION_SEND_BEGIN_MAIN_FRAME
:
545 UpdateStateOnSendBeginMainFrame();
548 case ACTION_COMMIT
: {
549 bool commit_has_no_updates
= false;
550 UpdateStateOnCommit(commit_has_no_updates
);
554 case ACTION_DRAW_AND_SWAP_FORCED
:
555 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
556 bool did_request_swap
= true;
557 UpdateStateOnDraw(did_request_swap
);
561 case ACTION_DRAW_AND_SWAP_ABORT
: {
562 bool did_request_swap
= false;
563 UpdateStateOnDraw(did_request_swap
);
567 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
568 UpdateStateOnBeginOutputSurfaceCreation();
571 case ACTION_PREPARE_TILES
:
572 UpdateStateOnPrepareTiles();
575 case ACTION_INVALIDATE_OUTPUT_SURFACE
:
576 UpdateStateOnInvalidateOutputSurface();
581 void SchedulerStateMachine::UpdateStateOnAnimate() {
582 DCHECK(!animate_funnel_
);
583 last_frame_number_animate_performed_
= current_frame_number_
;
584 animate_funnel_
= true;
585 needs_animate_
= false;
586 // TODO(skyostil): Instead of assuming this, require the client to tell us.
590 void SchedulerStateMachine::UpdateStateOnSendBeginMainFrame() {
591 DCHECK(!has_pending_tree_
|| settings_
.main_frame_before_activation_enabled
);
593 DCHECK(!send_begin_main_frame_funnel_
);
594 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
595 needs_commit_
= false;
596 send_begin_main_frame_funnel_
= true;
597 last_frame_number_begin_main_frame_sent_
= current_frame_number_
;
600 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates
) {
603 // Animate after commit even if we've already animated.
604 if (!commit_has_no_updates
)
605 animate_funnel_
= false;
607 if (commit_has_no_updates
|| settings_
.main_frame_before_activation_enabled
) {
608 commit_state_
= COMMIT_STATE_IDLE
;
609 } else if (settings_
.impl_side_painting
) {
610 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
612 commit_state_
= settings_
.main_thread_should_always_be_low_latency
613 ? COMMIT_STATE_WAITING_FOR_DRAW
617 // If we are impl-side-painting but the commit was aborted, then we behave
618 // mostly as if we are not impl-side-painting since there is no pending tree.
619 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_has_no_updates
;
621 // Update state related to forced draws.
622 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
623 forced_redraw_state_
= has_pending_tree_
624 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
625 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
628 // Update the output surface state.
629 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
630 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
631 if (has_pending_tree_
) {
632 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
634 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
635 needs_redraw_
= true;
639 // Update state if we have a new active tree to draw, or if the active tree
640 // was unchanged but we need to do a forced draw.
641 if (!has_pending_tree_
&&
642 (!commit_has_no_updates
||
643 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
644 needs_redraw_
= true;
645 active_tree_needs_first_draw_
= true;
648 // This post-commit work is common to both completed and aborted commits.
649 pending_tree_is_ready_for_activation_
= false;
651 if (continuous_painting_
)
652 needs_commit_
= true;
653 last_commit_had_no_updates_
= commit_has_no_updates
;
656 void SchedulerStateMachine::UpdateStateOnActivation() {
657 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
658 commit_state_
= settings_
.main_thread_should_always_be_low_latency
659 ? COMMIT_STATE_WAITING_FOR_DRAW
663 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
664 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
666 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
667 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
669 has_pending_tree_
= false;
670 pending_tree_is_ready_for_activation_
= false;
671 active_tree_needs_first_draw_
= true;
672 needs_redraw_
= true;
675 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
676 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
677 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
679 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
680 commit_state_
= COMMIT_STATE_IDLE
;
682 needs_redraw_
= false;
683 active_tree_needs_first_draw_
= false;
685 if (did_request_swap
) {
686 DCHECK(!request_swap_funnel_
);
687 request_swap_funnel_
= true;
688 did_request_swap_in_last_frame_
= true;
689 last_frame_number_swap_requested_
= current_frame_number_
;
693 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
694 needs_prepare_tiles_
= false;
697 void SchedulerStateMachine::UpdateStateOnBeginOutputSurfaceCreation() {
698 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
699 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
701 // The following DCHECKs make sure we are in the proper quiescent state.
702 // The pipeline should be flushed entirely before we start output
703 // surface creation to avoid complicated corner cases.
704 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
705 DCHECK(!has_pending_tree_
);
706 DCHECK(!active_tree_needs_first_draw_
);
709 void SchedulerStateMachine::UpdateStateOnInvalidateOutputSurface() {
710 DCHECK(!invalidate_output_surface_funnel_
);
711 invalidate_output_surface_funnel_
= true;
712 last_frame_number_invalidate_output_surface_performed_
=
713 current_frame_number_
;
716 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
717 TRACE_EVENT_INSTANT0("cc",
718 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
719 TRACE_EVENT_SCOPE_THREAD
);
720 skip_next_begin_main_frame_to_reduce_latency_
= true;
723 bool SchedulerStateMachine::BeginFrameNeededForChildren() const {
724 return children_need_begin_frames_
;
727 bool SchedulerStateMachine::BeginFrameNeeded() const {
728 // We can't handle BeginFrames when output surface isn't initialized.
729 // TODO(brianderson): Support output surface creation inside a BeginFrame.
730 if (!HasInitializedOutputSurface())
732 return (BeginFrameNeededToAnimateOrDraw() || BeginFrameNeededForChildren() ||
733 ProactiveBeginFrameWanted());
736 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
737 bool children_need_begin_frames
) {
738 children_need_begin_frames_
= children_need_begin_frames
;
741 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
742 defer_commits_
= defer_commits
;
745 // These are the cases where we definitely (or almost definitely) have a
746 // new frame to animate and/or draw and can draw.
747 bool SchedulerStateMachine::BeginFrameNeededToAnimateOrDraw() const {
748 // The forced draw respects our normal draw scheduling, so we need to
749 // request a BeginImplFrame for it.
750 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
753 // TODO(mithro): Remove background animation ticking. crbug.com/371747
757 // Only background tick for animations - not draws, which will never happen.
761 return needs_redraw_
;
764 // These are cases where we are very likely to draw soon, but might not
765 // actually have a new frame to draw when we receive the next BeginImplFrame.
766 // Proactively requesting the BeginImplFrame helps hide the round trip latency
767 // of the SetNeedsBeginFrame request that has to go to the Browser.
768 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
769 // Do not be proactive when invisible.
773 // We should proactively request a BeginImplFrame if a commit is pending
774 // because we will want to draw if the commit completes quickly. Do not
775 // request frames when commits are disabled, because the frame requests will
776 // not provide the needed commit (and will wake up the process when it could
778 if ((needs_commit_
|| commit_state_
!= COMMIT_STATE_IDLE
) && !defer_commits_
)
781 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
782 // to draw the new active tree.
783 if (has_pending_tree_
)
786 // Changing priorities may allow us to activate (given the new priorities),
787 // which may result in a new frame.
788 if (needs_prepare_tiles_
)
791 // If we just sent a swap request, it's likely that we are going to produce
792 // another frame soon. This helps avoid negative glitches in our
793 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
794 // provider and get sampled at an inopportune time, delaying the next
796 if (did_request_swap_in_last_frame_
)
799 // If the last commit was aborted because of early out (no updates), we should
800 // still want a begin frame in case there is a commit coming again.
801 if (last_commit_had_no_updates_
)
807 void SchedulerStateMachine::OnBeginImplFrame() {
808 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
809 current_frame_number_
++;
811 last_commit_had_no_updates_
= false;
812 did_request_swap_in_last_frame_
= false;
814 // Clear funnels for any actions we perform during the frame.
815 animate_funnel_
= false;
816 send_begin_main_frame_funnel_
= false;
817 invalidate_output_surface_funnel_
= false;
819 // "Drain" the PrepareTiles funnel.
820 if (prepare_tiles_funnel_
> 0)
821 prepare_tiles_funnel_
--;
823 skip_begin_main_frame_to_reduce_latency_
=
824 skip_next_begin_main_frame_to_reduce_latency_
;
825 skip_next_begin_main_frame_to_reduce_latency_
= false;
828 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
829 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
832 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
833 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
835 did_perform_swap_in_last_draw_
= false;
837 // Clear funnels for any actions we perform during the deadline.
838 request_swap_funnel_
= false;
841 void SchedulerStateMachine::OnBeginImplFrameIdle() {
842 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
845 SchedulerStateMachine::BeginImplFrameDeadlineMode
846 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
847 if (settings_
.using_synchronous_renderer_compositor
) {
848 // No deadline for synchronous compositor.
849 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
;
850 } else if (wait_for_active_tree_ready_to_draw_
) {
851 // When we are waiting for ready to draw signal, we do not wait to post a
853 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
;
854 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
855 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
856 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
857 // We have an animation or fast input path on the impl thread that wants
858 // to draw, so don't wait too long for a new active tree.
859 // If we are swap throttled we should wait until we are unblocked.
860 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
862 // The impl thread doesn't have anything it wants to draw and we are just
863 // waiting for a new active tree or we are swap throttled. In short we are
865 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
869 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
871 // TODO(brianderson): This should take into account multiple commit sources.
872 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
875 // If we've lost the output surface, end the current BeginImplFrame ASAP
876 // so we can start creating the next output surface.
877 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
880 // SwapAck throttle the deadline since we wont draw and swap anyway.
881 if (pending_swaps_
>= max_pending_swaps_
)
884 if (active_tree_needs_first_draw_
)
890 // This is used to prioritize impl-thread draws when the main thread isn't
891 // producing anything, e.g., after an aborted commit. We also check that we
892 // don't have a pending tree -- otherwise we should give it a chance to
894 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
895 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
898 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
899 if (impl_latency_takes_priority_
)
905 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
906 // If a commit is pending before the previous commit has been drawn, we
907 // are definitely in a high latency mode.
908 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
911 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
912 // thread is in a low latency mode.
913 if (send_begin_main_frame_funnel_
&&
914 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
915 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
918 // If there's a commit in progress it must either be from the previous frame
919 // or it started after the impl thread's deadline. In either case the main
920 // thread is in high latency mode.
924 // Similarly, if there's a pending tree the main thread is in high latency
925 // mode, because either
926 // it's from the previous frame
928 // we're currently drawing the active tree and the pending tree will thus
929 // only be drawn in the next frame.
930 if (has_pending_tree_
)
933 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
934 // Even if there's a new active tree to draw at the deadline or we've just
935 // swapped it, it may have been triggered by a previous BeginImplFrame, in
936 // which case the main thread is in a high latency mode.
937 return (active_tree_needs_first_draw_
|| did_perform_swap_in_last_draw_
) &&
938 !send_begin_main_frame_funnel_
;
941 // If the active tree needs its first draw in any other state, we know the
942 // main thread is in a high latency mode.
943 return active_tree_needs_first_draw_
;
946 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
948 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
950 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
952 void SchedulerStateMachine::SetNeedsAnimate() {
953 needs_animate_
= true;
956 void SchedulerStateMachine::SetWaitForReadyToDraw() {
957 DCHECK(settings_
.impl_side_painting
);
958 wait_for_active_tree_ready_to_draw_
= true;
961 void SchedulerStateMachine::SetNeedsPrepareTiles() {
962 if (!needs_prepare_tiles_
) {
963 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
964 needs_prepare_tiles_
= true;
968 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
969 max_pending_swaps_
= max
;
972 void SchedulerStateMachine::DidSwapBuffers() {
974 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
976 did_perform_swap_in_last_draw_
= true;
977 last_frame_number_swap_performed_
= current_frame_number_
;
980 void SchedulerStateMachine::DidSwapBuffersComplete() {
981 DCHECK_GT(pending_swaps_
, 0);
985 void SchedulerStateMachine::SetImplLatencyTakesPriority(
986 bool impl_latency_takes_priority
) {
987 impl_latency_takes_priority_
= impl_latency_takes_priority
;
990 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
993 NOTREACHED() << "Uninitialized DrawResult.";
995 case DRAW_ABORTED_CANT_DRAW
:
996 case DRAW_ABORTED_CONTEXT_LOST
:
997 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1001 consecutive_checkerboard_animations_
= 0;
1002 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
1004 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
1005 needs_redraw_
= true;
1007 // If we're already in the middle of a redraw, we don't need to
1009 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
1012 needs_commit_
= true;
1013 consecutive_checkerboard_animations_
++;
1014 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
1015 consecutive_checkerboard_animations_
>=
1016 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
1017 consecutive_checkerboard_animations_
= 0;
1018 // We need to force a draw, but it doesn't make sense to do this until
1019 // we've committed and have new textures.
1020 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1023 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1024 // It's not clear whether this missing content is because of missing
1025 // pictures (which requires a commit) or because of memory pressure
1026 // removing textures (which might not). To be safe, request a commit
1028 needs_commit_
= true;
1033 void SchedulerStateMachine::SetNeedsCommit() {
1034 needs_commit_
= true;
1037 void SchedulerStateMachine::NotifyReadyToCommit() {
1038 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1039 << AsValue()->ToString();
1040 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1041 // In main thread low latency mode, commit should happen right after
1042 // BeginFrame, meaning when this function is called, next action should be
1044 if (settings_
.main_thread_should_always_be_low_latency
)
1045 DCHECK(ShouldCommit());
1048 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1049 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1051 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1052 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1053 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1054 commit_state_
= COMMIT_STATE_IDLE
;
1057 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1058 bool commit_has_no_updates
= true;
1059 UpdateStateOnCommit(commit_has_no_updates
);
1064 void SchedulerStateMachine::DidPrepareTiles() {
1065 needs_prepare_tiles_
= false;
1066 // "Fill" the PrepareTiles funnel.
1067 prepare_tiles_funnel_
++;
1070 void SchedulerStateMachine::DidLoseOutputSurface() {
1071 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1072 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1074 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1075 needs_redraw_
= false;
1076 wait_for_active_tree_ready_to_draw_
= false;
1079 void SchedulerStateMachine::NotifyReadyToActivate() {
1080 if (has_pending_tree_
)
1081 pending_tree_is_ready_for_activation_
= true;
1084 void SchedulerStateMachine::NotifyReadyToDraw() {
1085 wait_for_active_tree_ready_to_draw_
= false;
1088 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1089 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1090 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1092 if (did_create_and_initialize_first_output_surface_
) {
1093 // TODO(boliu): See if we can remove this when impl-side painting is always
1094 // on. Does anything on the main thread need to update after recreate?
1095 needs_commit_
= true;
1097 did_create_and_initialize_first_output_surface_
= true;
1101 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1102 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1103 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1106 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1107 switch (output_surface_state_
) {
1108 case OUTPUT_SURFACE_LOST
:
1109 case OUTPUT_SURFACE_CREATING
:
1112 case OUTPUT_SURFACE_ACTIVE
:
1113 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1114 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1121 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1122 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1123 needs_commit_
? 'T' : 'F',
1124 static_cast<int>(output_surface_state_
),
1125 static_cast<int>(begin_impl_frame_state_
),
1126 static_cast<int>(commit_state_
),
1127 has_pending_tree_
? 'T' : 'F',
1128 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1129 active_tree_needs_first_draw_
? 'T' : 'F',