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"
16 SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings
& settings
)
17 : settings_(settings
),
18 output_surface_state_(OUTPUT_SURFACE_LOST
),
19 begin_impl_frame_state_(BEGIN_IMPL_FRAME_STATE_IDLE
),
20 commit_state_(COMMIT_STATE_IDLE
),
21 forced_redraw_state_(FORCED_REDRAW_STATE_IDLE
),
23 current_frame_number_(0),
24 last_frame_number_animate_performed_(-1),
25 last_frame_number_swap_performed_(-1),
26 last_frame_number_swap_requested_(-1),
27 last_frame_number_begin_main_frame_sent_(-1),
28 last_frame_number_invalidate_output_surface_performed_(-1),
29 animate_funnel_(false),
30 request_swap_funnel_(false),
31 send_begin_main_frame_funnel_(false),
32 invalidate_output_surface_funnel_(false),
33 prepare_tiles_funnel_(0),
34 consecutive_checkerboard_animations_(0),
35 max_pending_swaps_(1),
37 swaps_with_current_output_surface_(0),
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 video_needs_begin_frames_(false),
56 last_commit_had_no_updates_(false),
57 wait_for_active_tree_ready_to_draw_(false),
58 did_request_swap_in_last_frame_(false),
59 did_perform_swap_in_last_draw_(false) {
62 const char* SchedulerStateMachine::OutputSurfaceStateToString(
63 OutputSurfaceState state
) {
65 case OUTPUT_SURFACE_ACTIVE
:
66 return "OUTPUT_SURFACE_ACTIVE";
67 case OUTPUT_SURFACE_LOST
:
68 return "OUTPUT_SURFACE_LOST";
69 case OUTPUT_SURFACE_CREATING
:
70 return "OUTPUT_SURFACE_CREATING";
71 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
72 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
73 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
74 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
80 const char* SchedulerStateMachine::BeginImplFrameStateToString(
81 BeginImplFrameState state
) {
83 case BEGIN_IMPL_FRAME_STATE_IDLE
:
84 return "BEGIN_IMPL_FRAME_STATE_IDLE";
85 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
:
86 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
87 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
:
88 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
89 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
:
90 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
96 const char* SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
97 BeginImplFrameDeadlineMode mode
) {
99 case BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
:
100 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE";
101 case BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
:
102 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE";
103 case BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
:
104 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR";
105 case BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
:
106 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE";
107 case BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
:
108 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW";
114 const char* SchedulerStateMachine::CommitStateToString(CommitState state
) {
116 case COMMIT_STATE_IDLE
:
117 return "COMMIT_STATE_IDLE";
118 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
:
119 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
120 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
:
121 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
122 case COMMIT_STATE_READY_TO_COMMIT
:
123 return "COMMIT_STATE_READY_TO_COMMIT";
124 case COMMIT_STATE_WAITING_FOR_ACTIVATION
:
125 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
126 case COMMIT_STATE_WAITING_FOR_DRAW
:
127 return "COMMIT_STATE_WAITING_FOR_DRAW";
133 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
134 ForcedRedrawOnTimeoutState state
) {
136 case FORCED_REDRAW_STATE_IDLE
:
137 return "FORCED_REDRAW_STATE_IDLE";
138 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
:
139 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
140 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
:
141 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
142 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW
:
143 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
149 const char* SchedulerStateMachine::ActionToString(Action action
) {
152 return "ACTION_NONE";
154 return "ACTION_ANIMATE";
155 case ACTION_SEND_BEGIN_MAIN_FRAME
:
156 return "ACTION_SEND_BEGIN_MAIN_FRAME";
158 return "ACTION_COMMIT";
159 case ACTION_ACTIVATE_SYNC_TREE
:
160 return "ACTION_ACTIVATE_SYNC_TREE";
161 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
162 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
163 case ACTION_DRAW_AND_SWAP_FORCED
:
164 return "ACTION_DRAW_AND_SWAP_FORCED";
165 case ACTION_DRAW_AND_SWAP_ABORT
:
166 return "ACTION_DRAW_AND_SWAP_ABORT";
167 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
168 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
169 case ACTION_PREPARE_TILES
:
170 return "ACTION_PREPARE_TILES";
171 case ACTION_INVALIDATE_OUTPUT_SURFACE
:
172 return "ACTION_INVALIDATE_OUTPUT_SURFACE";
178 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
179 SchedulerStateMachine::AsValue() const {
180 scoped_refptr
<base::trace_event::TracedValue
> state
=
181 new base::trace_event::TracedValue();
182 AsValueInto(state
.get());
186 void SchedulerStateMachine::AsValueInto(
187 base::trace_event::TracedValue
* state
) const {
188 state
->BeginDictionary("major_state");
189 state
->SetString("next_action", ActionToString(NextAction()));
190 state
->SetString("begin_impl_frame_state",
191 BeginImplFrameStateToString(begin_impl_frame_state_
));
192 state
->SetString("commit_state", CommitStateToString(commit_state_
));
193 state
->SetString("output_surface_state_",
194 OutputSurfaceStateToString(output_surface_state_
));
195 state
->SetString("forced_redraw_state",
196 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_
));
197 state
->EndDictionary();
199 state
->BeginDictionary("minor_state");
200 state
->SetInteger("commit_count", commit_count_
);
201 state
->SetInteger("current_frame_number", current_frame_number_
);
202 state
->SetInteger("last_frame_number_animate_performed",
203 last_frame_number_animate_performed_
);
204 state
->SetInteger("last_frame_number_swap_performed",
205 last_frame_number_swap_performed_
);
206 state
->SetInteger("last_frame_number_swap_requested",
207 last_frame_number_swap_requested_
);
208 state
->SetInteger("last_frame_number_begin_main_frame_sent",
209 last_frame_number_begin_main_frame_sent_
);
210 state
->SetBoolean("funnel: animate_funnel", animate_funnel_
);
211 state
->SetBoolean("funnel: request_swap_funnel", request_swap_funnel_
);
212 state
->SetBoolean("funnel: send_begin_main_frame_funnel",
213 send_begin_main_frame_funnel_
);
214 state
->SetInteger("funnel: prepare_tiles_funnel", prepare_tiles_funnel_
);
215 state
->SetBoolean("funnel: invalidate_output_surface_funnel",
216 invalidate_output_surface_funnel_
);
217 state
->SetInteger("consecutive_checkerboard_animations",
218 consecutive_checkerboard_animations_
);
219 state
->SetInteger("max_pending_swaps_", max_pending_swaps_
);
220 state
->SetInteger("pending_swaps_", pending_swaps_
);
221 state
->SetInteger("swaps_with_current_output_surface",
222 swaps_with_current_output_surface_
);
223 state
->SetBoolean("needs_redraw", needs_redraw_
);
224 state
->SetBoolean("needs_animate_", needs_animate_
);
225 state
->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_
);
226 state
->SetBoolean("needs_commit", needs_commit_
);
227 state
->SetBoolean("visible", visible_
);
228 state
->SetBoolean("can_start", can_start_
);
229 state
->SetBoolean("can_draw", can_draw_
);
230 state
->SetBoolean("has_pending_tree", has_pending_tree_
);
231 state
->SetBoolean("pending_tree_is_ready_for_activation",
232 pending_tree_is_ready_for_activation_
);
233 state
->SetBoolean("active_tree_needs_first_draw",
234 active_tree_needs_first_draw_
);
235 state
->SetBoolean("wait_for_active_tree_ready_to_draw",
236 wait_for_active_tree_ready_to_draw_
);
237 state
->SetBoolean("did_create_and_initialize_first_output_surface",
238 did_create_and_initialize_first_output_surface_
);
239 state
->SetBoolean("impl_latency_takes_priority",
240 impl_latency_takes_priority_
);
241 state
->SetBoolean("main_thread_is_in_high_latency_mode",
242 MainThreadIsInHighLatencyMode());
243 state
->SetBoolean("skip_begin_main_frame_to_reduce_latency",
244 skip_begin_main_frame_to_reduce_latency_
);
245 state
->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
246 skip_next_begin_main_frame_to_reduce_latency_
);
247 state
->SetBoolean("continuous_painting", continuous_painting_
);
248 state
->SetBoolean("children_need_begin_frames", children_need_begin_frames_
);
249 state
->SetBoolean("video_needs_begin_frames", video_needs_begin_frames_
);
250 state
->SetBoolean("defer_commits", defer_commits_
);
251 state
->SetBoolean("last_commit_had_no_updates", last_commit_had_no_updates_
);
252 state
->SetBoolean("did_request_swap_in_last_frame",
253 did_request_swap_in_last_frame_
);
254 state
->SetBoolean("did_perform_swap_in_last_draw",
255 did_perform_swap_in_last_draw_
);
256 state
->EndDictionary();
259 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
260 // These are all the cases where we normally cannot or do not want to draw
261 // but, if needs_redraw_ is true and we do not draw to make forward progress,
262 // we might deadlock with the main thread.
263 // This should be a superset of PendingActivationsShouldBeForced() since
264 // activation of the pending tree is blocked by drawing of the active tree and
265 // the main thread might be blocked on activation of the most recent commit.
266 if (PendingActivationsShouldBeForced())
269 // Additional states where we should abort draws.
275 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
276 // There is no output surface to trigger our activations.
277 // If we do not force activations to make forward progress, we might deadlock
278 // with the main thread.
279 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
282 // If we're not visible, we should force activation.
283 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
284 // don't checkerboard until all visible resources are done. Furthermore, if we
285 // do keep the pending tree around, when becoming visible we might activate
286 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
287 // we can simply activate on becoming invisible since we don't need to draw
288 // the active tree when we're in this state.
295 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
296 // Don't try to initialize too early.
300 // We only want to start output surface initialization after the
301 // previous commit is complete.
302 if (commit_state_
!= COMMIT_STATE_IDLE
)
305 // Make sure the BeginImplFrame from any previous OutputSurfaces
306 // are complete before creating the new OutputSurface.
307 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
310 // We want to clear the pipeline of any pending draws and activations
311 // before starting output surface initialization. This allows us to avoid
312 // weird corner cases where we abort draws or force activation while we
313 // are initializing the output surface.
314 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
317 // We need to create the output surface if we don't have one and we haven't
318 // started creating one yet.
319 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
322 bool SchedulerStateMachine::ShouldDraw() const {
323 // If we need to abort draws, we should do so ASAP since the draw could
324 // be blocking other important actions (like output surface initialization),
325 // from occurring. If we are waiting for the first draw, then perform the
326 // aborted draw to keep things moving. If we are not waiting for the first
327 // draw however, we don't want to abort for no reason.
328 if (PendingDrawsShouldBeAborted())
329 return active_tree_needs_first_draw_
;
331 // Do not draw too many times in a single frame. It's okay that we don't check
332 // this before checking for aborted draws because aborted draws do not request
334 if (request_swap_funnel_
)
337 // Don't draw if we are waiting on the first commit after a surface.
338 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
341 // Do not queue too many swaps.
342 if (pending_swaps_
>= max_pending_swaps_
)
345 // Except for the cases above, do not draw outside of the BeginImplFrame
347 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
350 // Only handle forced redraws due to timeouts on the regular deadline.
351 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
354 return needs_redraw_
;
357 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
358 // There is nothing to activate.
359 if (!has_pending_tree_
)
362 // We should not activate a second tree before drawing the first one.
363 // Even if we need to force activation of the pending tree, we should abort
364 // drawing the active tree first.
365 if (active_tree_needs_first_draw_
)
368 // If we want to force activation, do so ASAP.
369 if (PendingActivationsShouldBeForced())
372 // At this point, only activate if we are ready to activate.
373 return pending_tree_is_ready_for_activation_
;
376 bool SchedulerStateMachine::ShouldAnimate() const {
377 // Do not animate too many times in a single frame.
381 // Don't animate if we are waiting on the first commit after a surface.
382 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
385 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
386 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
389 return needs_redraw_
|| needs_animate_
;
392 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
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::SendingBeginMainFrameMightCauseDeadlock() const {
408 // NPAPI is the only case where the UI thread makes synchronous calls to the
409 // Renderer main thread. During that synchronous call, we may not get a
410 // SwapAck for the UI thread, which may prevent BeginMainFrame's from
411 // completing if there's enough back pressure. If the BeginMainFrame can't
412 // make progress, the Renderer can't service the UI thread's synchronous call
413 // and we have deadlock.
414 // This returns true if there's too much backpressure to finish a commit
415 // if we were to initiate a BeginMainFrame.
416 return has_pending_tree_
&& active_tree_needs_first_draw_
&&
417 pending_swaps_
>= max_pending_swaps_
;
420 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
421 if (!CouldSendBeginMainFrame())
424 // Do not send begin main frame too many times in a single frame.
425 if (send_begin_main_frame_funnel_
)
428 // Only send BeginMainFrame when there isn't another commit pending already.
429 // Other parts of the state machine indirectly defer the BeginMainFrame
430 // by transitioning to WAITING commit states rather than going
431 // immediately to IDLE.
432 if (commit_state_
!= COMMIT_STATE_IDLE
)
435 // Don't send BeginMainFrame early if we are prioritizing the active tree
436 // because of impl_latency_takes_priority_.
437 if (impl_latency_takes_priority_
&&
438 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
442 // We should not send BeginMainFrame while we are in
443 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
444 // user input arriving soon.
445 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
446 // thread isn't consuming user input.
447 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
451 // We need a new commit for the forced redraw. This honors the
452 // single commit per interval because the result will be swapped to screen.
453 // TODO(brianderson): Remove this or move it below the
454 // SendingBeginMainFrameMightCauseDeadlock check since we want to avoid
455 // ever returning true from this method if we might cause deadlock.
456 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
459 // We shouldn't normally accept commits if there isn't an OutputSurface.
460 if (!HasInitializedOutputSurface())
463 // Make sure the BeginMainFrame can finish eventually if we start it.
464 if (SendingBeginMainFrameMightCauseDeadlock())
467 if (!settings_
.main_frame_while_swap_throttled_enabled
) {
468 // SwapAck throttle the BeginMainFrames unless we just swapped to
469 // potentially improve impl-thread latency over main-thread throughput.
470 // TODO(brianderson): Remove this restriction to improve throughput or
471 // make it conditional on impl_latency_takes_priority_.
472 bool just_swapped_in_deadline
=
473 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
474 did_perform_swap_in_last_draw_
;
475 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
479 if (skip_begin_main_frame_to_reduce_latency_
)
485 bool SchedulerStateMachine::ShouldCommit() const {
486 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
489 // We must not finish the commit until the pending tree is free.
490 if (has_pending_tree_
) {
491 DCHECK(settings_
.main_frame_before_activation_enabled
);
495 // If we only have an active tree, it is incorrect to replace it
496 // before we've drawn it.
497 DCHECK_IMPLIES(settings_
.commit_to_active_tree
,
498 !active_tree_needs_first_draw_
);
503 bool SchedulerStateMachine::ShouldPrepareTiles() const {
504 // PrepareTiles only really needs to be called immediately after commit
505 // and then periodically after that. Use a funnel to make sure we average
506 // one PrepareTiles per BeginImplFrame in the long run.
507 if (prepare_tiles_funnel_
> 0)
510 // Limiting to once per-frame is not enough, since we only want to
511 // prepare tiles _after_ draws.
512 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
515 return needs_prepare_tiles_
;
518 bool SchedulerStateMachine::ShouldInvalidateOutputSurface() const {
519 // Do not invalidate too many times in a frame.
520 if (invalidate_output_surface_funnel_
)
523 // Only the synchronous compositor requires invalidations.
524 if (!settings_
.using_synchronous_renderer_compositor
)
527 // Invalidations are only performed inside a BeginFrame.
528 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
531 // TODO(sunnyps): needs_prepare_tiles_ is needed here because PrepareTiles is
532 // called only inside the deadline / draw phase. We could remove this if we
533 // allowed PrepareTiles to happen in OnBeginImplFrame.
534 return needs_redraw_
|| needs_prepare_tiles_
;
537 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
538 if (ShouldActivatePendingTree())
539 return ACTION_ACTIVATE_SYNC_TREE
;
541 return ACTION_COMMIT
;
543 return ACTION_ANIMATE
;
545 if (PendingDrawsShouldBeAborted())
546 return ACTION_DRAW_AND_SWAP_ABORT
;
547 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
548 return ACTION_DRAW_AND_SWAP_FORCED
;
550 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
552 if (ShouldPrepareTiles())
553 return ACTION_PREPARE_TILES
;
554 if (ShouldSendBeginMainFrame())
555 return ACTION_SEND_BEGIN_MAIN_FRAME
;
556 if (ShouldInvalidateOutputSurface())
557 return ACTION_INVALIDATE_OUTPUT_SURFACE
;
558 if (ShouldBeginOutputSurfaceCreation())
559 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
563 void SchedulerStateMachine::UpdateState(Action action
) {
568 case ACTION_ACTIVATE_SYNC_TREE
:
569 UpdateStateOnActivation();
573 UpdateStateOnAnimate();
576 case ACTION_SEND_BEGIN_MAIN_FRAME
:
577 UpdateStateOnSendBeginMainFrame();
580 case ACTION_COMMIT
: {
581 bool commit_has_no_updates
= false;
582 UpdateStateOnCommit(commit_has_no_updates
);
586 case ACTION_DRAW_AND_SWAP_FORCED
:
587 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
588 bool did_request_swap
= true;
589 UpdateStateOnDraw(did_request_swap
);
593 case ACTION_DRAW_AND_SWAP_ABORT
: {
594 bool did_request_swap
= false;
595 UpdateStateOnDraw(did_request_swap
);
599 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
600 UpdateStateOnBeginOutputSurfaceCreation();
603 case ACTION_PREPARE_TILES
:
604 UpdateStateOnPrepareTiles();
607 case ACTION_INVALIDATE_OUTPUT_SURFACE
:
608 UpdateStateOnInvalidateOutputSurface();
613 void SchedulerStateMachine::UpdateStateOnAnimate() {
614 DCHECK(!animate_funnel_
);
615 last_frame_number_animate_performed_
= current_frame_number_
;
616 animate_funnel_
= true;
617 needs_animate_
= false;
618 // TODO(skyostil): Instead of assuming this, require the client to tell us.
622 void SchedulerStateMachine::UpdateStateOnSendBeginMainFrame() {
623 DCHECK(!has_pending_tree_
|| settings_
.main_frame_before_activation_enabled
);
625 DCHECK(!send_begin_main_frame_funnel_
);
626 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
627 needs_commit_
= false;
628 send_begin_main_frame_funnel_
= true;
629 last_frame_number_begin_main_frame_sent_
= current_frame_number_
;
632 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates
) {
635 // Animate after commit even if we've already animated.
636 if (!commit_has_no_updates
)
637 animate_funnel_
= false;
639 if (commit_has_no_updates
|| settings_
.main_frame_before_activation_enabled
) {
640 commit_state_
= COMMIT_STATE_IDLE
;
642 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
645 // If the commit was aborted, then there is no pending tree.
646 has_pending_tree_
= !commit_has_no_updates
;
648 // Update state related to forced draws.
649 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
650 forced_redraw_state_
= has_pending_tree_
651 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
652 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
655 // Update the output surface state.
656 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
657 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
658 if (has_pending_tree_
) {
659 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
661 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
662 needs_redraw_
= true;
666 // Update state if we have a new active tree to draw, or if the active tree
667 // was unchanged but we need to do a forced draw.
668 if (!has_pending_tree_
&&
669 (!commit_has_no_updates
||
670 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
671 needs_redraw_
= true;
672 active_tree_needs_first_draw_
= true;
675 // This post-commit work is common to both completed and aborted commits.
676 pending_tree_is_ready_for_activation_
= false;
678 if (continuous_painting_
)
679 needs_commit_
= true;
680 last_commit_had_no_updates_
= commit_has_no_updates
;
683 void SchedulerStateMachine::UpdateStateOnActivation() {
684 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
685 commit_state_
= settings_
.commit_to_active_tree
686 ? COMMIT_STATE_WAITING_FOR_DRAW
690 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
691 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
693 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
694 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
696 has_pending_tree_
= false;
697 pending_tree_is_ready_for_activation_
= false;
698 active_tree_needs_first_draw_
= true;
699 needs_redraw_
= true;
702 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
703 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
704 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
706 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
707 commit_state_
= COMMIT_STATE_IDLE
;
709 needs_redraw_
= false;
710 active_tree_needs_first_draw_
= false;
712 if (did_request_swap
) {
713 DCHECK(!request_swap_funnel_
);
714 request_swap_funnel_
= true;
715 did_request_swap_in_last_frame_
= true;
716 last_frame_number_swap_requested_
= current_frame_number_
;
720 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
721 needs_prepare_tiles_
= false;
724 void SchedulerStateMachine::UpdateStateOnBeginOutputSurfaceCreation() {
725 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
726 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
728 // The following DCHECKs make sure we are in the proper quiescent state.
729 // The pipeline should be flushed entirely before we start output
730 // surface creation to avoid complicated corner cases.
731 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
732 DCHECK(!has_pending_tree_
);
733 DCHECK(!active_tree_needs_first_draw_
);
736 void SchedulerStateMachine::UpdateStateOnInvalidateOutputSurface() {
737 DCHECK(!invalidate_output_surface_funnel_
);
738 invalidate_output_surface_funnel_
= true;
739 last_frame_number_invalidate_output_surface_performed_
=
740 current_frame_number_
;
742 // The synchronous compositor makes no guarantees about a draw coming in after
743 // an invalidate so clear any flags that would cause the compositor's pipeline
745 active_tree_needs_first_draw_
= false; // blocks commit if true
748 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
749 TRACE_EVENT_INSTANT0("cc",
750 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
751 TRACE_EVENT_SCOPE_THREAD
);
752 skip_next_begin_main_frame_to_reduce_latency_
= true;
755 bool SchedulerStateMachine::BeginFrameRequiredForChildren() const {
756 return children_need_begin_frames_
;
758 bool SchedulerStateMachine::BeginFrameNeededForVideo() const {
759 return video_needs_begin_frames_
;
762 bool SchedulerStateMachine::BeginFrameNeeded() const {
763 // We can't handle BeginFrames when output surface isn't initialized.
764 // TODO(brianderson): Support output surface creation inside a BeginFrame.
765 if (!HasInitializedOutputSurface())
768 // If we are not visible, we don't need BeginFrame messages.
772 return (BeginFrameRequiredForAction() || BeginFrameRequiredForChildren() ||
773 BeginFrameNeededForVideo() || ProactiveBeginFrameWanted());
776 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
777 bool children_need_begin_frames
) {
778 children_need_begin_frames_
= children_need_begin_frames
;
781 void SchedulerStateMachine::SetVideoNeedsBeginFrames(
782 bool video_needs_begin_frames
) {
783 video_needs_begin_frames_
= video_needs_begin_frames
;
786 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
787 defer_commits_
= defer_commits
;
790 // These are the cases where we require a BeginFrame message to make progress
791 // on requested actions.
792 bool SchedulerStateMachine::BeginFrameRequiredForAction() const {
793 // The forced draw respects our normal draw scheduling, so we need to
794 // request a BeginImplFrame for it.
795 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
798 return needs_animate_
|| needs_redraw_
|| (needs_commit_
&& !defer_commits_
);
801 // These are cases where we are very likely want a BeginFrame message in the
802 // near future. Proactively requesting the BeginImplFrame helps hide the round
803 // trip latency of the SetNeedsBeginFrame request that has to go to the
805 // This includes things like drawing soon, but might not actually have a new
806 // frame to draw when we receive the next BeginImplFrame.
807 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
808 // Do not be proactive when invisible.
812 // We should proactively request a BeginImplFrame if a commit is pending
813 // because we will want to draw if the commit completes quickly. Do not
814 // request frames when commits are disabled, because the frame requests will
815 // not provide the needed commit (and will wake up the process when it could
817 if ((commit_state_
!= COMMIT_STATE_IDLE
) && !defer_commits_
)
820 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
821 // to draw the new active tree.
822 if (has_pending_tree_
)
825 // Changing priorities may allow us to activate (given the new priorities),
826 // which may result in a new frame.
827 if (needs_prepare_tiles_
)
830 // If we just sent a swap request, it's likely that we are going to produce
831 // another frame soon. This helps avoid negative glitches in our
832 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
833 // provider and get sampled at an inopportune time, delaying the next
835 if (did_request_swap_in_last_frame_
)
838 // If the last commit was aborted because of early out (no updates), we should
839 // still want a begin frame in case there is a commit coming again.
840 if (last_commit_had_no_updates_
)
846 void SchedulerStateMachine::OnBeginImplFrame() {
847 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
848 current_frame_number_
++;
850 last_commit_had_no_updates_
= false;
851 did_request_swap_in_last_frame_
= false;
853 // Clear funnels for any actions we perform during the frame.
854 animate_funnel_
= false;
855 send_begin_main_frame_funnel_
= false;
856 invalidate_output_surface_funnel_
= false;
858 // "Drain" the PrepareTiles funnel.
859 if (prepare_tiles_funnel_
> 0)
860 prepare_tiles_funnel_
--;
862 skip_begin_main_frame_to_reduce_latency_
=
863 skip_next_begin_main_frame_to_reduce_latency_
;
864 skip_next_begin_main_frame_to_reduce_latency_
= false;
867 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
868 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
871 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
872 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
874 did_perform_swap_in_last_draw_
= false;
876 // Clear funnels for any actions we perform during the deadline.
877 request_swap_funnel_
= false;
879 // Allow one PrepareTiles per draw for synchronous compositor.
880 if (settings_
.using_synchronous_renderer_compositor
) {
881 if (prepare_tiles_funnel_
> 0)
882 prepare_tiles_funnel_
--;
886 void SchedulerStateMachine::OnBeginImplFrameIdle() {
887 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
890 SchedulerStateMachine::BeginImplFrameDeadlineMode
891 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
892 if (settings_
.using_synchronous_renderer_compositor
) {
893 // No deadline for synchronous compositor.
894 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
;
895 } else if (wait_for_active_tree_ready_to_draw_
) {
896 // When we are waiting for ready to draw signal, we do not wait to post a
898 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
;
899 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
900 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
901 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
902 // We have an animation or fast input path on the impl thread that wants
903 // to draw, so don't wait too long for a new active tree.
904 // If we are swap throttled we should wait until we are unblocked.
905 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
907 // The impl thread doesn't have anything it wants to draw and we are just
908 // waiting for a new active tree or we are swap throttled. In short we are
910 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
914 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
916 // TODO(brianderson): This should take into account multiple commit sources.
917 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
920 // If we just forced activation, we should end the deadline right now.
921 if (PendingActivationsShouldBeForced() && !has_pending_tree_
)
924 // SwapAck throttle the deadline since we wont draw and swap anyway.
925 if (pending_swaps_
>= max_pending_swaps_
)
928 if (active_tree_needs_first_draw_
)
934 // This is used to prioritize impl-thread draws when the main thread isn't
935 // producing anything, e.g., after an aborted commit. We also check that we
936 // don't have a pending tree -- otherwise we should give it a chance to
938 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
939 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
942 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
943 if (impl_latency_takes_priority_
)
949 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
950 // If a commit is pending before the previous commit has been drawn, we
951 // are definitely in a high latency mode.
952 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
955 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
956 // thread is in a low latency mode.
957 if (send_begin_main_frame_funnel_
&&
958 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
959 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
962 // If there's a commit in progress it must either be from the previous frame
963 // or it started after the impl thread's deadline. In either case the main
964 // thread is in high latency mode.
968 // Similarly, if there's a pending tree the main thread is in high latency
969 // mode, because either
970 // it's from the previous frame
972 // we're currently drawing the active tree and the pending tree will thus
973 // only be drawn in the next frame.
974 if (has_pending_tree_
)
977 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
978 // Even if there's a new active tree to draw at the deadline or we've just
979 // swapped it, it may have been triggered by a previous BeginImplFrame, in
980 // which case the main thread is in a high latency mode.
981 return (active_tree_needs_first_draw_
|| did_perform_swap_in_last_draw_
) &&
982 !send_begin_main_frame_funnel_
;
985 // If the active tree needs its first draw in any other state, we know the
986 // main thread is in a high latency mode.
987 return active_tree_needs_first_draw_
;
990 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
992 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
994 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
996 void SchedulerStateMachine::SetNeedsAnimate() {
997 needs_animate_
= true;
1000 void SchedulerStateMachine::SetWaitForReadyToDraw() {
1001 wait_for_active_tree_ready_to_draw_
= true;
1004 void SchedulerStateMachine::SetNeedsPrepareTiles() {
1005 if (!needs_prepare_tiles_
) {
1006 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
1007 needs_prepare_tiles_
= true;
1011 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
1012 max_pending_swaps_
= max
;
1015 void SchedulerStateMachine::DidSwapBuffers() {
1017 swaps_with_current_output_surface_
++;
1019 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
1021 did_perform_swap_in_last_draw_
= true;
1022 last_frame_number_swap_performed_
= current_frame_number_
;
1025 void SchedulerStateMachine::DidSwapBuffersComplete() {
1029 void SchedulerStateMachine::SetImplLatencyTakesPriority(
1030 bool impl_latency_takes_priority
) {
1031 impl_latency_takes_priority_
= impl_latency_takes_priority
;
1034 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
1036 case INVALID_RESULT
:
1037 NOTREACHED() << "Uninitialized DrawResult.";
1039 case DRAW_ABORTED_CANT_DRAW
:
1040 case DRAW_ABORTED_CONTEXT_LOST
:
1041 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1045 consecutive_checkerboard_animations_
= 0;
1046 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
1048 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
1049 needs_redraw_
= true;
1051 // If we're already in the middle of a redraw, we don't need to
1053 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
1056 needs_commit_
= true;
1057 consecutive_checkerboard_animations_
++;
1058 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
1059 consecutive_checkerboard_animations_
>=
1060 settings_
.maximum_number_of_failed_draws_before_draw_is_forced
) {
1061 consecutive_checkerboard_animations_
= 0;
1062 // We need to force a draw, but it doesn't make sense to do this until
1063 // we've committed and have new textures.
1064 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1067 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1068 // It's not clear whether this missing content is because of missing
1069 // pictures (which requires a commit) or because of memory pressure
1070 // removing textures (which might not). To be safe, request a commit
1072 needs_commit_
= true;
1077 void SchedulerStateMachine::SetNeedsCommit() {
1078 needs_commit_
= true;
1081 void SchedulerStateMachine::NotifyReadyToCommit() {
1082 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1083 << AsValue()->ToString();
1084 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1085 // In commit_to_active_tree mode, commit should happen right after
1086 // BeginFrame, meaning when this function is called, next action should be
1088 if (settings_
.commit_to_active_tree
)
1089 DCHECK(ShouldCommit());
1092 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1093 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1095 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1096 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1097 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1098 commit_state_
= COMMIT_STATE_IDLE
;
1101 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1102 bool commit_has_no_updates
= true;
1103 UpdateStateOnCommit(commit_has_no_updates
);
1108 void SchedulerStateMachine::DidPrepareTiles() {
1109 needs_prepare_tiles_
= false;
1110 // "Fill" the PrepareTiles funnel.
1111 prepare_tiles_funnel_
++;
1114 void SchedulerStateMachine::DidLoseOutputSurface() {
1115 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1116 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1118 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1119 needs_redraw_
= false;
1120 wait_for_active_tree_ready_to_draw_
= false;
1123 void SchedulerStateMachine::NotifyReadyToActivate() {
1124 if (has_pending_tree_
)
1125 pending_tree_is_ready_for_activation_
= true;
1128 void SchedulerStateMachine::NotifyReadyToDraw() {
1129 wait_for_active_tree_ready_to_draw_
= false;
1132 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1133 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1134 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1136 if (did_create_and_initialize_first_output_surface_
) {
1137 // TODO(boliu): See if we can remove this when impl-side painting is always
1138 // on. Does anything on the main thread need to update after recreate?
1139 needs_commit_
= true;
1141 did_create_and_initialize_first_output_surface_
= true;
1143 swaps_with_current_output_surface_
= 0;
1146 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1147 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1148 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1151 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1152 switch (output_surface_state_
) {
1153 case OUTPUT_SURFACE_LOST
:
1154 case OUTPUT_SURFACE_CREATING
:
1157 case OUTPUT_SURFACE_ACTIVE
:
1158 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1159 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
: