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 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
->SetBoolean("needs_redraw", needs_redraw_
);
222 state
->SetBoolean("needs_animate_", needs_animate_
);
223 state
->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_
);
224 state
->SetBoolean("needs_commit", needs_commit_
);
225 state
->SetBoolean("visible", visible_
);
226 state
->SetBoolean("can_start", can_start_
);
227 state
->SetBoolean("can_draw", can_draw_
);
228 state
->SetBoolean("has_pending_tree", has_pending_tree_
);
229 state
->SetBoolean("pending_tree_is_ready_for_activation",
230 pending_tree_is_ready_for_activation_
);
231 state
->SetBoolean("active_tree_needs_first_draw",
232 active_tree_needs_first_draw_
);
233 state
->SetBoolean("wait_for_active_tree_ready_to_draw",
234 wait_for_active_tree_ready_to_draw_
);
235 state
->SetBoolean("did_create_and_initialize_first_output_surface",
236 did_create_and_initialize_first_output_surface_
);
237 state
->SetBoolean("impl_latency_takes_priority",
238 impl_latency_takes_priority_
);
239 state
->SetBoolean("main_thread_is_in_high_latency_mode",
240 MainThreadIsInHighLatencyMode());
241 state
->SetBoolean("skip_begin_main_frame_to_reduce_latency",
242 skip_begin_main_frame_to_reduce_latency_
);
243 state
->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
244 skip_next_begin_main_frame_to_reduce_latency_
);
245 state
->SetBoolean("continuous_painting", continuous_painting_
);
246 state
->SetBoolean("children_need_begin_frames", children_need_begin_frames_
);
247 state
->SetBoolean("video_needs_begin_frames", video_needs_begin_frames_
);
248 state
->SetBoolean("defer_commits", defer_commits_
);
249 state
->SetBoolean("last_commit_had_no_updates", last_commit_had_no_updates_
);
250 state
->SetBoolean("did_request_swap_in_last_frame",
251 did_request_swap_in_last_frame_
);
252 state
->SetBoolean("did_perform_swap_in_last_draw",
253 did_perform_swap_in_last_draw_
);
254 state
->EndDictionary();
257 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
258 // These are all the cases where we normally cannot or do not want to draw
259 // but, if needs_redraw_ is true and we do not draw to make forward progress,
260 // we might deadlock with the main thread.
261 // This should be a superset of PendingActivationsShouldBeForced() since
262 // activation of the pending tree is blocked by drawing of the active tree and
263 // the main thread might be blocked on activation of the most recent commit.
264 if (PendingActivationsShouldBeForced())
267 // Additional states where we should abort draws.
273 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
274 // There is no output surface to trigger our activations.
275 // If we do not force activations to make forward progress, we might deadlock
276 // with the main thread.
277 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
280 // If we're not visible, we should force activation.
281 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
282 // don't checkerboard until all visible resources are done. Furthermore, if we
283 // do keep the pending tree around, when becoming visible we might activate
284 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
285 // we can simply activate on becoming invisible since we don't need to draw
286 // the active tree when we're in this state.
293 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
294 // Don't try to initialize too early.
298 // We only want to start output surface initialization after the
299 // previous commit is complete.
300 if (commit_state_
!= COMMIT_STATE_IDLE
)
303 // Make sure the BeginImplFrame from any previous OutputSurfaces
304 // are complete before creating the new OutputSurface.
305 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
308 // We want to clear the pipeline of any pending draws and activations
309 // before starting output surface initialization. This allows us to avoid
310 // weird corner cases where we abort draws or force activation while we
311 // are initializing the output surface.
312 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
315 // We need to create the output surface if we don't have one and we haven't
316 // started creating one yet.
317 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
320 bool SchedulerStateMachine::ShouldDraw() const {
321 // If we need to abort draws, we should do so ASAP since the draw could
322 // be blocking other important actions (like output surface initialization),
323 // from occurring. If we are waiting for the first draw, then perform the
324 // aborted draw to keep things moving. If we are not waiting for the first
325 // draw however, we don't want to abort for no reason.
326 if (PendingDrawsShouldBeAborted())
327 return active_tree_needs_first_draw_
;
329 // Do not draw too many times in a single frame. It's okay that we don't check
330 // this before checking for aborted draws because aborted draws do not request
332 if (request_swap_funnel_
)
335 // Don't draw if we are waiting on the first commit after a surface.
336 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
339 // Do not queue too many swaps.
340 if (pending_swaps_
>= max_pending_swaps_
)
343 // Except for the cases above, do not draw outside of the BeginImplFrame
345 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
348 // Only handle forced redraws due to timeouts on the regular deadline.
349 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
352 return needs_redraw_
;
355 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
356 // There is nothing to activate.
357 if (!has_pending_tree_
)
360 // We should not activate a second tree before drawing the first one.
361 // Even if we need to force activation of the pending tree, we should abort
362 // drawing the active tree first.
363 if (active_tree_needs_first_draw_
)
366 // If we want to force activation, do so ASAP.
367 if (PendingActivationsShouldBeForced())
370 // At this point, only activate if we are ready to activate.
371 return pending_tree_is_ready_for_activation_
;
374 bool SchedulerStateMachine::ShouldAnimate() const {
375 // Do not animate too many times in a single frame.
379 // Don't animate if we are waiting on the first commit after a surface.
380 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
383 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
384 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
387 return needs_redraw_
|| needs_animate_
;
390 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
394 // We can not perform commits if we are not visible.
398 // Do not make a new commits when it is deferred.
405 bool SchedulerStateMachine::SendingBeginMainFrameMightCauseDeadlock() const {
406 // NPAPI is the only case where the UI thread makes synchronous calls to the
407 // Renderer main thread. During that synchronous call, we may not get a
408 // SwapAck for the UI thread, which may prevent BeginMainFrame's from
409 // completing if there's enough back pressure. If the BeginMainFrame can't
410 // make progress, the Renderer can't service the UI thread's synchronous call
411 // and we have deadlock.
412 // This returns true if there's too much backpressure to finish a commit
413 // if we were to initiate a BeginMainFrame.
414 return has_pending_tree_
&& active_tree_needs_first_draw_
&&
415 pending_swaps_
>= max_pending_swaps_
;
418 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
419 if (!CouldSendBeginMainFrame())
422 // Do not send begin main frame too many times in a single frame.
423 if (send_begin_main_frame_funnel_
)
426 // Only send BeginMainFrame when there isn't another commit pending already.
427 // Other parts of the state machine indirectly defer the BeginMainFrame
428 // by transitioning to WAITING commit states rather than going
429 // immediately to IDLE.
430 if (commit_state_
!= COMMIT_STATE_IDLE
)
433 // Don't send BeginMainFrame early if we are prioritizing the active tree
434 // because of impl_latency_takes_priority_.
435 if (impl_latency_takes_priority_
&&
436 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
440 // We should not send BeginMainFrame while we are in
441 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
442 // user input arriving soon.
443 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
444 // thread isn't consuming user input.
445 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
449 // We need a new commit for the forced redraw. This honors the
450 // single commit per interval because the result will be swapped to screen.
451 // TODO(brianderson): Remove this or move it below the
452 // SendingBeginMainFrameMightCauseDeadlock check since we want to avoid
453 // ever returning true from this method if we might cause deadlock.
454 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
457 // We shouldn't normally accept commits if there isn't an OutputSurface.
458 if (!HasInitializedOutputSurface())
461 // Make sure the BeginMainFrame can finish eventually if we start it.
462 if (SendingBeginMainFrameMightCauseDeadlock())
465 if (!settings_
.main_frame_while_swap_throttled_enabled
) {
466 // SwapAck throttle the BeginMainFrames unless we just swapped to
467 // potentially improve impl-thread latency over main-thread throughput.
468 // TODO(brianderson): Remove this restriction to improve throughput or
469 // make it conditional on impl_latency_takes_priority_.
470 bool just_swapped_in_deadline
=
471 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
472 did_perform_swap_in_last_draw_
;
473 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
477 if (skip_begin_main_frame_to_reduce_latency_
)
483 bool SchedulerStateMachine::ShouldCommit() const {
484 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
487 // We must not finish the commit until the pending tree is free.
488 if (has_pending_tree_
) {
489 DCHECK(settings_
.main_frame_before_activation_enabled
);
493 // If impl-side-painting, commit to the pending tree as soon as we can.
494 if (settings_
.impl_side_painting
)
497 // If we only have an active tree, it is incorrect to replace it
498 // before we've drawn it when we aren't impl-side-painting.
499 DCHECK(!settings_
.impl_side_painting
);
500 return !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
;
641 } else if (settings_
.impl_side_painting
) {
642 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
644 commit_state_
= settings_
.main_thread_should_always_be_low_latency
645 ? COMMIT_STATE_WAITING_FOR_DRAW
649 // If we are impl-side-painting but the commit was aborted, then we behave
650 // mostly as if we are not impl-side-painting since there is no pending tree.
651 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_has_no_updates
;
653 // Update state related to forced draws.
654 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
655 forced_redraw_state_
= has_pending_tree_
656 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
657 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
660 // Update the output surface state.
661 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
662 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
663 if (has_pending_tree_
) {
664 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
666 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
667 needs_redraw_
= true;
671 // Update state if we have a new active tree to draw, or if the active tree
672 // was unchanged but we need to do a forced draw.
673 if (!has_pending_tree_
&&
674 (!commit_has_no_updates
||
675 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
676 needs_redraw_
= true;
677 active_tree_needs_first_draw_
= true;
680 // This post-commit work is common to both completed and aborted commits.
681 pending_tree_is_ready_for_activation_
= false;
683 if (continuous_painting_
)
684 needs_commit_
= true;
685 last_commit_had_no_updates_
= commit_has_no_updates
;
688 void SchedulerStateMachine::UpdateStateOnActivation() {
689 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
690 commit_state_
= settings_
.main_thread_should_always_be_low_latency
691 ? COMMIT_STATE_WAITING_FOR_DRAW
695 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
696 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
698 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
699 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
701 has_pending_tree_
= false;
702 pending_tree_is_ready_for_activation_
= false;
703 active_tree_needs_first_draw_
= true;
704 needs_redraw_
= true;
707 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
708 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
709 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
711 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
712 commit_state_
= COMMIT_STATE_IDLE
;
714 needs_redraw_
= false;
715 active_tree_needs_first_draw_
= false;
717 if (did_request_swap
) {
718 DCHECK(!request_swap_funnel_
);
719 request_swap_funnel_
= true;
720 did_request_swap_in_last_frame_
= true;
721 last_frame_number_swap_requested_
= current_frame_number_
;
725 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
726 needs_prepare_tiles_
= false;
729 void SchedulerStateMachine::UpdateStateOnBeginOutputSurfaceCreation() {
730 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
731 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
733 // The following DCHECKs make sure we are in the proper quiescent state.
734 // The pipeline should be flushed entirely before we start output
735 // surface creation to avoid complicated corner cases.
736 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
737 DCHECK(!has_pending_tree_
);
738 DCHECK(!active_tree_needs_first_draw_
);
741 void SchedulerStateMachine::UpdateStateOnInvalidateOutputSurface() {
742 DCHECK(!invalidate_output_surface_funnel_
);
743 invalidate_output_surface_funnel_
= true;
744 last_frame_number_invalidate_output_surface_performed_
=
745 current_frame_number_
;
747 // The synchronous compositor makes no guarantees about a draw coming in after
748 // an invalidate so clear any flags that would cause the compositor's pipeline
750 active_tree_needs_first_draw_
= false; // blocks commit if true
753 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
754 TRACE_EVENT_INSTANT0("cc",
755 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
756 TRACE_EVENT_SCOPE_THREAD
);
757 skip_next_begin_main_frame_to_reduce_latency_
= true;
760 bool SchedulerStateMachine::BeginFrameRequiredForChildren() const {
761 return children_need_begin_frames_
;
763 bool SchedulerStateMachine::BeginFrameNeededForVideo() const {
764 return video_needs_begin_frames_
;
767 bool SchedulerStateMachine::BeginFrameNeeded() const {
768 // We can't handle BeginFrames when output surface isn't initialized.
769 // TODO(brianderson): Support output surface creation inside a BeginFrame.
770 if (!HasInitializedOutputSurface())
773 // If we are not visible, we don't need BeginFrame messages.
777 return (BeginFrameRequiredForAction() || BeginFrameRequiredForChildren() ||
778 BeginFrameNeededForVideo() || ProactiveBeginFrameWanted());
781 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
782 bool children_need_begin_frames
) {
783 children_need_begin_frames_
= children_need_begin_frames
;
786 void SchedulerStateMachine::SetVideoNeedsBeginFrames(
787 bool video_needs_begin_frames
) {
788 video_needs_begin_frames_
= video_needs_begin_frames
;
791 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
792 defer_commits_
= defer_commits
;
795 // These are the cases where we require a BeginFrame message to make progress
796 // on requested actions.
797 bool SchedulerStateMachine::BeginFrameRequiredForAction() const {
798 // The forced draw respects our normal draw scheduling, so we need to
799 // request a BeginImplFrame for it.
800 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
803 return needs_animate_
|| needs_redraw_
|| (needs_commit_
&& !defer_commits_
);
806 // These are cases where we are very likely want a BeginFrame message in the
807 // near future. Proactively requesting the BeginImplFrame helps hide the round
808 // trip latency of the SetNeedsBeginFrame request that has to go to the
810 // This includes things like drawing soon, but might not actually have a new
811 // frame to draw when we receive the next BeginImplFrame.
812 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
813 // Do not be proactive when invisible.
817 // We should proactively request a BeginImplFrame if a commit is pending
818 // because we will want to draw if the commit completes quickly. Do not
819 // request frames when commits are disabled, because the frame requests will
820 // not provide the needed commit (and will wake up the process when it could
822 if ((commit_state_
!= COMMIT_STATE_IDLE
) && !defer_commits_
)
825 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
826 // to draw the new active tree.
827 if (has_pending_tree_
)
830 // Changing priorities may allow us to activate (given the new priorities),
831 // which may result in a new frame.
832 if (needs_prepare_tiles_
)
835 // If we just sent a swap request, it's likely that we are going to produce
836 // another frame soon. This helps avoid negative glitches in our
837 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
838 // provider and get sampled at an inopportune time, delaying the next
840 if (did_request_swap_in_last_frame_
)
843 // If the last commit was aborted because of early out (no updates), we should
844 // still want a begin frame in case there is a commit coming again.
845 if (last_commit_had_no_updates_
)
851 void SchedulerStateMachine::OnBeginImplFrame() {
852 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
853 current_frame_number_
++;
855 last_commit_had_no_updates_
= false;
856 did_request_swap_in_last_frame_
= false;
858 // Clear funnels for any actions we perform during the frame.
859 animate_funnel_
= false;
860 send_begin_main_frame_funnel_
= false;
861 invalidate_output_surface_funnel_
= false;
863 // "Drain" the PrepareTiles funnel.
864 if (prepare_tiles_funnel_
> 0)
865 prepare_tiles_funnel_
--;
867 skip_begin_main_frame_to_reduce_latency_
=
868 skip_next_begin_main_frame_to_reduce_latency_
;
869 skip_next_begin_main_frame_to_reduce_latency_
= false;
872 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
873 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
876 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
877 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
879 did_perform_swap_in_last_draw_
= false;
881 // Clear funnels for any actions we perform during the deadline.
882 request_swap_funnel_
= false;
885 void SchedulerStateMachine::OnBeginImplFrameIdle() {
886 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
889 SchedulerStateMachine::BeginImplFrameDeadlineMode
890 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
891 if (settings_
.using_synchronous_renderer_compositor
) {
892 // No deadline for synchronous compositor.
893 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
;
894 } else if (wait_for_active_tree_ready_to_draw_
) {
895 // When we are waiting for ready to draw signal, we do not wait to post a
897 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
;
898 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
899 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
900 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
901 // We have an animation or fast input path on the impl thread that wants
902 // to draw, so don't wait too long for a new active tree.
903 // If we are swap throttled we should wait until we are unblocked.
904 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
906 // The impl thread doesn't have anything it wants to draw and we are just
907 // waiting for a new active tree or we are swap throttled. In short we are
909 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
913 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
915 // TODO(brianderson): This should take into account multiple commit sources.
916 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
919 // If we just forced activation, we should end the deadline right now.
920 if (PendingActivationsShouldBeForced() && !has_pending_tree_
)
923 // SwapAck throttle the deadline since we wont draw and swap anyway.
924 if (pending_swaps_
>= max_pending_swaps_
)
927 if (active_tree_needs_first_draw_
)
933 // This is used to prioritize impl-thread draws when the main thread isn't
934 // producing anything, e.g., after an aborted commit. We also check that we
935 // don't have a pending tree -- otherwise we should give it a chance to
937 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
938 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
941 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
942 if (impl_latency_takes_priority_
)
948 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
949 // If a commit is pending before the previous commit has been drawn, we
950 // are definitely in a high latency mode.
951 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
954 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
955 // thread is in a low latency mode.
956 if (send_begin_main_frame_funnel_
&&
957 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
958 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
961 // If there's a commit in progress it must either be from the previous frame
962 // or it started after the impl thread's deadline. In either case the main
963 // thread is in high latency mode.
967 // Similarly, if there's a pending tree the main thread is in high latency
968 // mode, because either
969 // it's from the previous frame
971 // we're currently drawing the active tree and the pending tree will thus
972 // only be drawn in the next frame.
973 if (has_pending_tree_
)
976 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
977 // Even if there's a new active tree to draw at the deadline or we've just
978 // swapped it, it may have been triggered by a previous BeginImplFrame, in
979 // which case the main thread is in a high latency mode.
980 return (active_tree_needs_first_draw_
|| did_perform_swap_in_last_draw_
) &&
981 !send_begin_main_frame_funnel_
;
984 // If the active tree needs its first draw in any other state, we know the
985 // main thread is in a high latency mode.
986 return active_tree_needs_first_draw_
;
989 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
991 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
993 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
995 void SchedulerStateMachine::SetNeedsAnimate() {
996 needs_animate_
= true;
999 void SchedulerStateMachine::SetWaitForReadyToDraw() {
1000 DCHECK(settings_
.impl_side_painting
);
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 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
1019 did_perform_swap_in_last_draw_
= true;
1020 last_frame_number_swap_performed_
= current_frame_number_
;
1023 void SchedulerStateMachine::DidSwapBuffersComplete() {
1024 DCHECK_GT(pending_swaps_
, 0);
1028 void SchedulerStateMachine::SetImplLatencyTakesPriority(
1029 bool impl_latency_takes_priority
) {
1030 impl_latency_takes_priority_
= impl_latency_takes_priority
;
1033 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
1035 case INVALID_RESULT
:
1036 NOTREACHED() << "Uninitialized DrawResult.";
1038 case DRAW_ABORTED_CANT_DRAW
:
1039 case DRAW_ABORTED_CONTEXT_LOST
:
1040 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1044 consecutive_checkerboard_animations_
= 0;
1045 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
1047 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
1048 needs_redraw_
= true;
1050 // If we're already in the middle of a redraw, we don't need to
1052 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
1055 needs_commit_
= true;
1056 consecutive_checkerboard_animations_
++;
1057 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
1058 consecutive_checkerboard_animations_
>=
1059 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
1060 consecutive_checkerboard_animations_
= 0;
1061 // We need to force a draw, but it doesn't make sense to do this until
1062 // we've committed and have new textures.
1063 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1066 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1067 // It's not clear whether this missing content is because of missing
1068 // pictures (which requires a commit) or because of memory pressure
1069 // removing textures (which might not). To be safe, request a commit
1071 needs_commit_
= true;
1076 void SchedulerStateMachine::SetNeedsCommit() {
1077 needs_commit_
= true;
1080 void SchedulerStateMachine::NotifyReadyToCommit() {
1081 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1082 << AsValue()->ToString();
1083 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1084 // In main thread low latency mode, commit should happen right after
1085 // BeginFrame, meaning when this function is called, next action should be
1087 if (settings_
.main_thread_should_always_be_low_latency
)
1088 DCHECK(ShouldCommit());
1091 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1092 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1094 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1095 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1096 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1097 commit_state_
= COMMIT_STATE_IDLE
;
1100 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1101 bool commit_has_no_updates
= true;
1102 UpdateStateOnCommit(commit_has_no_updates
);
1107 void SchedulerStateMachine::DidPrepareTiles() {
1108 needs_prepare_tiles_
= false;
1109 // "Fill" the PrepareTiles funnel.
1110 prepare_tiles_funnel_
++;
1113 void SchedulerStateMachine::DidLoseOutputSurface() {
1114 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1115 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1117 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1118 needs_redraw_
= false;
1119 wait_for_active_tree_ready_to_draw_
= false;
1122 void SchedulerStateMachine::NotifyReadyToActivate() {
1123 if (has_pending_tree_
)
1124 pending_tree_is_ready_for_activation_
= true;
1127 void SchedulerStateMachine::NotifyReadyToDraw() {
1128 wait_for_active_tree_ready_to_draw_
= false;
1131 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1132 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1133 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1135 if (did_create_and_initialize_first_output_surface_
) {
1136 // TODO(boliu): See if we can remove this when impl-side painting is always
1137 // on. Does anything on the main thread need to update after recreate?
1138 needs_commit_
= true;
1140 did_create_and_initialize_first_output_surface_
= true;
1144 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1145 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1146 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1149 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1150 switch (output_surface_state_
) {
1151 case OUTPUT_SURFACE_LOST
:
1152 case OUTPUT_SURFACE_CREATING
:
1155 case OUTPUT_SURFACE_ACTIVE
:
1156 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1157 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1164 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1165 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1166 needs_commit_
? 'T' : 'F',
1167 static_cast<int>(output_surface_state_
),
1168 static_cast<int>(begin_impl_frame_state_
),
1169 static_cast<int>(commit_state_
),
1170 has_pending_tree_
? 'T' : 'F',
1171 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1172 active_tree_needs_first_draw_
? 'T' : 'F',