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 if (skip_begin_main_frame_to_reduce_latency_
)
446 bool SchedulerStateMachine::ShouldCommit() const {
447 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
450 // We must not finish the commit until the pending tree is free.
451 if (has_pending_tree_
) {
452 DCHECK(settings_
.main_frame_before_activation_enabled
);
456 // Prioritize drawing the previous commit before finishing the next commit.
457 if (active_tree_needs_first_draw_
)
463 bool SchedulerStateMachine::ShouldPrepareTiles() const {
464 // PrepareTiles only really needs to be called immediately after commit
465 // and then periodically after that. Use a funnel to make sure we average
466 // one PrepareTiles per BeginImplFrame in the long run.
467 if (prepare_tiles_funnel_
> 0)
470 // Limiting to once per-frame is not enough, since we only want to
471 // prepare tiles _after_ draws.
472 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
475 return needs_prepare_tiles_
;
478 bool SchedulerStateMachine::ShouldInvalidateOutputSurface() const {
479 // Do not invalidate too many times in a frame.
480 if (invalidate_output_surface_funnel_
)
483 // Only the synchronous compositor requires invalidations.
484 if (!settings_
.using_synchronous_renderer_compositor
)
487 // Invalidations are only performed inside a BeginFrame.
488 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
491 // TODO(sunnyps): needs_prepare_tiles_ is needed here because PrepareTiles is
492 // called only inside the deadline / draw phase. We could remove this if we
493 // allowed PrepareTiles to happen in OnBeginImplFrame.
494 return needs_redraw_
|| needs_prepare_tiles_
;
497 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
498 if (ShouldActivatePendingTree())
499 return ACTION_ACTIVATE_SYNC_TREE
;
501 return ACTION_COMMIT
;
503 return ACTION_ANIMATE
;
505 if (PendingDrawsShouldBeAborted())
506 return ACTION_DRAW_AND_SWAP_ABORT
;
507 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
508 return ACTION_DRAW_AND_SWAP_FORCED
;
510 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
512 if (ShouldPrepareTiles())
513 return ACTION_PREPARE_TILES
;
514 if (ShouldSendBeginMainFrame())
515 return ACTION_SEND_BEGIN_MAIN_FRAME
;
516 if (ShouldInvalidateOutputSurface())
517 return ACTION_INVALIDATE_OUTPUT_SURFACE
;
518 if (ShouldBeginOutputSurfaceCreation())
519 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
523 void SchedulerStateMachine::UpdateState(Action action
) {
528 case ACTION_ACTIVATE_SYNC_TREE
:
529 UpdateStateOnActivation();
533 UpdateStateOnAnimate();
536 case ACTION_SEND_BEGIN_MAIN_FRAME
:
537 UpdateStateOnSendBeginMainFrame();
540 case ACTION_COMMIT
: {
541 bool commit_has_no_updates
= false;
542 UpdateStateOnCommit(commit_has_no_updates
);
546 case ACTION_DRAW_AND_SWAP_FORCED
:
547 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
548 bool did_request_swap
= true;
549 UpdateStateOnDraw(did_request_swap
);
553 case ACTION_DRAW_AND_SWAP_ABORT
: {
554 bool did_request_swap
= false;
555 UpdateStateOnDraw(did_request_swap
);
559 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
560 UpdateStateOnBeginOutputSurfaceCreation();
563 case ACTION_PREPARE_TILES
:
564 UpdateStateOnPrepareTiles();
567 case ACTION_INVALIDATE_OUTPUT_SURFACE
:
568 UpdateStateOnInvalidateOutputSurface();
573 void SchedulerStateMachine::UpdateStateOnAnimate() {
574 DCHECK(!animate_funnel_
);
575 last_frame_number_animate_performed_
= current_frame_number_
;
576 animate_funnel_
= true;
577 needs_animate_
= false;
578 // TODO(skyostil): Instead of assuming this, require the client to tell us.
582 void SchedulerStateMachine::UpdateStateOnSendBeginMainFrame() {
583 DCHECK(!has_pending_tree_
|| settings_
.main_frame_before_activation_enabled
);
585 DCHECK(!send_begin_main_frame_funnel_
);
586 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
587 needs_commit_
= false;
588 send_begin_main_frame_funnel_
= true;
589 last_frame_number_begin_main_frame_sent_
= current_frame_number_
;
592 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates
) {
595 // Animate after commit even if we've already animated.
596 if (!commit_has_no_updates
)
597 animate_funnel_
= false;
599 if (commit_has_no_updates
|| settings_
.main_frame_before_activation_enabled
) {
600 commit_state_
= COMMIT_STATE_IDLE
;
601 } else if (settings_
.impl_side_painting
) {
602 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
604 commit_state_
= settings_
.main_thread_should_always_be_low_latency
605 ? COMMIT_STATE_WAITING_FOR_DRAW
609 // If we are impl-side-painting but the commit was aborted, then we behave
610 // mostly as if we are not impl-side-painting since there is no pending tree.
611 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_has_no_updates
;
613 // Update state related to forced draws.
614 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
615 forced_redraw_state_
= has_pending_tree_
616 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
617 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
620 // Update the output surface state.
621 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
622 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
623 if (has_pending_tree_
) {
624 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
626 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
627 needs_redraw_
= true;
631 // Update state if we have a new active tree to draw, or if the active tree
632 // was unchanged but we need to do a forced draw.
633 if (!has_pending_tree_
&&
634 (!commit_has_no_updates
||
635 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
636 needs_redraw_
= true;
637 active_tree_needs_first_draw_
= true;
640 // This post-commit work is common to both completed and aborted commits.
641 pending_tree_is_ready_for_activation_
= false;
643 if (continuous_painting_
)
644 needs_commit_
= true;
645 last_commit_had_no_updates_
= commit_has_no_updates
;
648 void SchedulerStateMachine::UpdateStateOnActivation() {
649 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
650 commit_state_
= settings_
.main_thread_should_always_be_low_latency
651 ? COMMIT_STATE_WAITING_FOR_DRAW
655 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
656 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
658 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
659 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
661 has_pending_tree_
= false;
662 pending_tree_is_ready_for_activation_
= false;
663 active_tree_needs_first_draw_
= true;
664 needs_redraw_
= true;
667 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
668 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
669 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
671 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
672 commit_state_
= COMMIT_STATE_IDLE
;
674 needs_redraw_
= false;
675 active_tree_needs_first_draw_
= false;
677 if (did_request_swap
) {
678 DCHECK(!request_swap_funnel_
);
679 request_swap_funnel_
= true;
680 did_request_swap_in_last_frame_
= true;
681 last_frame_number_swap_requested_
= current_frame_number_
;
685 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
686 needs_prepare_tiles_
= false;
689 void SchedulerStateMachine::UpdateStateOnBeginOutputSurfaceCreation() {
690 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
691 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
693 // The following DCHECKs make sure we are in the proper quiescent state.
694 // The pipeline should be flushed entirely before we start output
695 // surface creation to avoid complicated corner cases.
696 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
697 DCHECK(!has_pending_tree_
);
698 DCHECK(!active_tree_needs_first_draw_
);
701 void SchedulerStateMachine::UpdateStateOnInvalidateOutputSurface() {
702 DCHECK(!invalidate_output_surface_funnel_
);
703 invalidate_output_surface_funnel_
= true;
704 last_frame_number_invalidate_output_surface_performed_
=
705 current_frame_number_
;
708 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
709 TRACE_EVENT_INSTANT0("cc",
710 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
711 TRACE_EVENT_SCOPE_THREAD
);
712 skip_next_begin_main_frame_to_reduce_latency_
= true;
715 bool SchedulerStateMachine::BeginFrameRequiredForChildren() const {
716 return children_need_begin_frames_
;
719 bool SchedulerStateMachine::BeginFrameNeeded() const {
720 // We can't handle BeginFrames when output surface isn't initialized.
721 // TODO(brianderson): Support output surface creation inside a BeginFrame.
722 if (!HasInitializedOutputSurface())
725 // If we are not visible, we don't need BeginFrame messages.
729 return (BeginFrameRequiredForAction() || BeginFrameRequiredForChildren() ||
730 ProactiveBeginFrameWanted());
733 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
734 bool children_need_begin_frames
) {
735 children_need_begin_frames_
= children_need_begin_frames
;
738 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
739 defer_commits_
= defer_commits
;
742 // These are the cases where we require a BeginFrame message to make progress
743 // on requested actions.
744 bool SchedulerStateMachine::BeginFrameRequiredForAction() const {
745 // The forced draw respects our normal draw scheduling, so we need to
746 // request a BeginImplFrame for it.
747 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
750 return needs_animate_
|| needs_redraw_
|| (needs_commit_
&& !defer_commits_
);
753 // These are cases where we are very likely want a BeginFrame message in the
754 // near future. Proactively requesting the BeginImplFrame helps hide the round
755 // trip latency of the SetNeedsBeginFrame request that has to go to the
757 // This includes things like drawing soon, but might not actually have a new
758 // frame to draw when we receive the next BeginImplFrame.
759 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
760 // Do not be proactive when invisible.
764 // We should proactively request a BeginImplFrame if a commit is pending
765 // because we will want to draw if the commit completes quickly. Do not
766 // request frames when commits are disabled, because the frame requests will
767 // not provide the needed commit (and will wake up the process when it could
769 if ((commit_state_
!= COMMIT_STATE_IDLE
) && !defer_commits_
)
772 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
773 // to draw the new active tree.
774 if (has_pending_tree_
)
777 // Changing priorities may allow us to activate (given the new priorities),
778 // which may result in a new frame.
779 if (needs_prepare_tiles_
)
782 // If we just sent a swap request, it's likely that we are going to produce
783 // another frame soon. This helps avoid negative glitches in our
784 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
785 // provider and get sampled at an inopportune time, delaying the next
787 if (did_request_swap_in_last_frame_
)
790 // If the last commit was aborted because of early out (no updates), we should
791 // still want a begin frame in case there is a commit coming again.
792 if (last_commit_had_no_updates_
)
798 void SchedulerStateMachine::OnBeginImplFrame() {
799 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
800 current_frame_number_
++;
802 last_commit_had_no_updates_
= false;
803 did_request_swap_in_last_frame_
= false;
805 // Clear funnels for any actions we perform during the frame.
806 animate_funnel_
= false;
807 send_begin_main_frame_funnel_
= false;
808 invalidate_output_surface_funnel_
= false;
810 // "Drain" the PrepareTiles funnel.
811 if (prepare_tiles_funnel_
> 0)
812 prepare_tiles_funnel_
--;
814 skip_begin_main_frame_to_reduce_latency_
=
815 skip_next_begin_main_frame_to_reduce_latency_
;
816 skip_next_begin_main_frame_to_reduce_latency_
= false;
819 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
820 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
823 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
824 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
826 did_perform_swap_in_last_draw_
= false;
828 // Clear funnels for any actions we perform during the deadline.
829 request_swap_funnel_
= false;
832 void SchedulerStateMachine::OnBeginImplFrameIdle() {
833 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
836 SchedulerStateMachine::BeginImplFrameDeadlineMode
837 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
838 if (settings_
.using_synchronous_renderer_compositor
) {
839 // No deadline for synchronous compositor.
840 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
;
841 } else if (wait_for_active_tree_ready_to_draw_
) {
842 // When we are waiting for ready to draw signal, we do not wait to post a
844 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
;
845 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
846 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
847 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
848 // We have an animation or fast input path on the impl thread that wants
849 // to draw, so don't wait too long for a new active tree.
850 // If we are swap throttled we should wait until we are unblocked.
851 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
853 // The impl thread doesn't have anything it wants to draw and we are just
854 // waiting for a new active tree or we are swap throttled. In short we are
856 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
860 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
862 // TODO(brianderson): This should take into account multiple commit sources.
863 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
866 // If things are being aborted, end the current BeginImplFrame ASAP so we can
867 // unblock creating the next output surface.
868 if (PendingDrawsShouldBeAborted())
871 // SwapAck throttle the deadline since we wont draw and swap anyway.
872 if (pending_swaps_
>= max_pending_swaps_
)
875 if (active_tree_needs_first_draw_
)
881 // This is used to prioritize impl-thread draws when the main thread isn't
882 // producing anything, e.g., after an aborted commit. We also check that we
883 // don't have a pending tree -- otherwise we should give it a chance to
885 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
886 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
889 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
890 if (impl_latency_takes_priority_
)
896 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
897 // If a commit is pending before the previous commit has been drawn, we
898 // are definitely in a high latency mode.
899 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
902 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
903 // thread is in a low latency mode.
904 if (send_begin_main_frame_funnel_
&&
905 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
906 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
909 // If there's a commit in progress it must either be from the previous frame
910 // or it started after the impl thread's deadline. In either case the main
911 // thread is in high latency mode.
915 // Similarly, if there's a pending tree the main thread is in high latency
916 // mode, because either
917 // it's from the previous frame
919 // we're currently drawing the active tree and the pending tree will thus
920 // only be drawn in the next frame.
921 if (has_pending_tree_
)
924 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
925 // Even if there's a new active tree to draw at the deadline or we've just
926 // swapped it, it may have been triggered by a previous BeginImplFrame, in
927 // which case the main thread is in a high latency mode.
928 return (active_tree_needs_first_draw_
|| did_perform_swap_in_last_draw_
) &&
929 !send_begin_main_frame_funnel_
;
932 // If the active tree needs its first draw in any other state, we know the
933 // main thread is in a high latency mode.
934 return active_tree_needs_first_draw_
;
937 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
939 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
941 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
943 void SchedulerStateMachine::SetNeedsAnimate() {
944 needs_animate_
= true;
947 void SchedulerStateMachine::SetWaitForReadyToDraw() {
948 DCHECK(settings_
.impl_side_painting
);
949 wait_for_active_tree_ready_to_draw_
= true;
952 void SchedulerStateMachine::SetNeedsPrepareTiles() {
953 if (!needs_prepare_tiles_
) {
954 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
955 needs_prepare_tiles_
= true;
959 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
960 max_pending_swaps_
= max
;
963 void SchedulerStateMachine::DidSwapBuffers() {
965 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
967 did_perform_swap_in_last_draw_
= true;
968 last_frame_number_swap_performed_
= current_frame_number_
;
971 void SchedulerStateMachine::DidSwapBuffersComplete() {
972 DCHECK_GT(pending_swaps_
, 0);
976 void SchedulerStateMachine::SetImplLatencyTakesPriority(
977 bool impl_latency_takes_priority
) {
978 impl_latency_takes_priority_
= impl_latency_takes_priority
;
981 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
984 NOTREACHED() << "Uninitialized DrawResult.";
986 case DRAW_ABORTED_CANT_DRAW
:
987 case DRAW_ABORTED_CONTEXT_LOST
:
988 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
992 consecutive_checkerboard_animations_
= 0;
993 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
995 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
996 needs_redraw_
= true;
998 // If we're already in the middle of a redraw, we don't need to
1000 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
1003 needs_commit_
= true;
1004 consecutive_checkerboard_animations_
++;
1005 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
1006 consecutive_checkerboard_animations_
>=
1007 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
1008 consecutive_checkerboard_animations_
= 0;
1009 // We need to force a draw, but it doesn't make sense to do this until
1010 // we've committed and have new textures.
1011 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1014 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1015 // It's not clear whether this missing content is because of missing
1016 // pictures (which requires a commit) or because of memory pressure
1017 // removing textures (which might not). To be safe, request a commit
1019 needs_commit_
= true;
1024 void SchedulerStateMachine::SetNeedsCommit() {
1025 needs_commit_
= true;
1028 void SchedulerStateMachine::NotifyReadyToCommit() {
1029 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1030 << AsValue()->ToString();
1031 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1032 // In main thread low latency mode, commit should happen right after
1033 // BeginFrame, meaning when this function is called, next action should be
1035 if (settings_
.main_thread_should_always_be_low_latency
)
1036 DCHECK(ShouldCommit());
1039 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1040 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1042 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1043 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1044 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1045 commit_state_
= COMMIT_STATE_IDLE
;
1048 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1049 bool commit_has_no_updates
= true;
1050 UpdateStateOnCommit(commit_has_no_updates
);
1055 void SchedulerStateMachine::DidPrepareTiles() {
1056 needs_prepare_tiles_
= false;
1057 // "Fill" the PrepareTiles funnel.
1058 prepare_tiles_funnel_
++;
1061 void SchedulerStateMachine::DidLoseOutputSurface() {
1062 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1063 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1065 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1066 needs_redraw_
= false;
1067 wait_for_active_tree_ready_to_draw_
= false;
1070 void SchedulerStateMachine::NotifyReadyToActivate() {
1071 if (has_pending_tree_
)
1072 pending_tree_is_ready_for_activation_
= true;
1075 void SchedulerStateMachine::NotifyReadyToDraw() {
1076 wait_for_active_tree_ready_to_draw_
= false;
1079 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1080 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1081 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1083 if (did_create_and_initialize_first_output_surface_
) {
1084 // TODO(boliu): See if we can remove this when impl-side painting is always
1085 // on. Does anything on the main thread need to update after recreate?
1086 needs_commit_
= true;
1088 did_create_and_initialize_first_output_surface_
= true;
1092 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1093 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1094 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1097 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1098 switch (output_surface_state_
) {
1099 case OUTPUT_SURFACE_LOST
:
1100 case OUTPUT_SURFACE_CREATING
:
1103 case OUTPUT_SURFACE_ACTIVE
:
1104 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1105 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1112 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1113 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1114 needs_commit_
? 'T' : 'F',
1115 static_cast<int>(output_surface_state_
),
1116 static_cast<int>(begin_impl_frame_state_
),
1117 static_cast<int>(commit_state_
),
1118 has_pending_tree_
? 'T' : 'F',
1119 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1120 active_tree_needs_first_draw_
? 'T' : 'F',