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),
38 needs_animate_(false),
39 needs_prepare_tiles_(false),
44 has_pending_tree_(false),
45 pending_tree_is_ready_for_activation_(false),
46 active_tree_needs_first_draw_(false),
47 did_create_and_initialize_first_output_surface_(false),
48 impl_latency_takes_priority_(false),
49 skip_next_begin_main_frame_to_reduce_latency_(false),
50 skip_begin_main_frame_to_reduce_latency_(false),
51 continuous_painting_(false),
52 children_need_begin_frames_(false),
53 defer_commits_(false),
54 video_needs_begin_frames_(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("video_needs_begin_frames", video_needs_begin_frames_
);
247 state
->SetBoolean("defer_commits", defer_commits_
);
248 state
->SetBoolean("last_commit_had_no_updates", last_commit_had_no_updates_
);
249 state
->SetBoolean("did_request_swap_in_last_frame",
250 did_request_swap_in_last_frame_
);
251 state
->SetBoolean("did_perform_swap_in_last_draw",
252 did_perform_swap_in_last_draw_
);
253 state
->EndDictionary();
256 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
257 // These are all the cases where we normally cannot or do not want to draw
258 // but, if needs_redraw_ is true and we do not draw to make forward progress,
259 // we might deadlock with the main thread.
260 // This should be a superset of PendingActivationsShouldBeForced() since
261 // activation of the pending tree is blocked by drawing of the active tree and
262 // the main thread might be blocked on activation of the most recent commit.
263 if (PendingActivationsShouldBeForced())
266 // Additional states where we should abort draws.
272 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
273 // There is no output surface to trigger our activations.
274 // If we do not force activations to make forward progress, we might deadlock
275 // with the main thread.
276 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
279 // If we're not visible, we should force activation.
280 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
281 // don't checkerboard until all visible resources are done. Furthermore, if we
282 // do keep the pending tree around, when becoming visible we might activate
283 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
284 // we can simply activate on becoming invisible since we don't need to draw
285 // the active tree when we're in this state.
292 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
293 // Don't try to initialize too early.
297 // We only want to start output surface initialization after the
298 // previous commit is complete.
299 if (commit_state_
!= COMMIT_STATE_IDLE
)
302 // Make sure the BeginImplFrame from any previous OutputSurfaces
303 // are complete before creating the new OutputSurface.
304 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
307 // We want to clear the pipeline of any pending draws and activations
308 // before starting output surface initialization. This allows us to avoid
309 // weird corner cases where we abort draws or force activation while we
310 // are initializing the output surface.
311 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
314 // We need to create the output surface if we don't have one and we haven't
315 // started creating one yet.
316 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
319 bool SchedulerStateMachine::ShouldDraw() const {
320 // If we need to abort draws, we should do so ASAP since the draw could
321 // be blocking other important actions (like output surface initialization),
322 // from occurring. If we are waiting for the first draw, then perform the
323 // aborted draw to keep things moving. If we are not waiting for the first
324 // draw however, we don't want to abort for no reason.
325 if (PendingDrawsShouldBeAborted())
326 return active_tree_needs_first_draw_
;
328 // Do not draw too many times in a single frame. It's okay that we don't check
329 // this before checking for aborted draws because aborted draws do not request
331 if (request_swap_funnel_
)
334 // Don't draw if we are waiting on the first commit after a surface.
335 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
338 // Do not queue too many swaps.
339 if (pending_swaps_
>= max_pending_swaps_
)
342 // Except for the cases above, do not draw outside of the BeginImplFrame
344 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
347 // Only handle forced redraws due to timeouts on the regular deadline.
348 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
351 return needs_redraw_
;
354 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
355 // There is nothing to activate.
356 if (!has_pending_tree_
)
359 // We should not activate a second tree before drawing the first one.
360 // Even if we need to force activation of the pending tree, we should abort
361 // drawing the active tree first.
362 if (active_tree_needs_first_draw_
)
365 // If we want to force activation, do so ASAP.
366 if (PendingActivationsShouldBeForced())
369 // At this point, only activate if we are ready to activate.
370 return pending_tree_is_ready_for_activation_
;
373 bool SchedulerStateMachine::ShouldAnimate() const {
374 // Do not animate too many times in a single frame.
378 // Don't animate if we are waiting on the first commit after a surface.
379 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
382 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
383 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
386 return needs_redraw_
|| needs_animate_
;
389 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
393 // We can not perform commits if we are not visible.
397 // Do not make a new commits when it is deferred.
404 bool SchedulerStateMachine::SendingBeginMainFrameMightCauseDeadlock() const {
405 // NPAPI is the only case where the UI thread makes synchronous calls to the
406 // Renderer main thread. During that synchronous call, we may not get a
407 // SwapAck for the UI thread, which may prevent BeginMainFrame's from
408 // completing if there's enough back pressure. If the BeginMainFrame can't
409 // make progress, the Renderer can't service the UI thread's synchronous call
410 // and we have deadlock.
411 // This returns true if there's too much backpressure to finish a commit
412 // if we were to initiate a BeginMainFrame.
413 return has_pending_tree_
&& active_tree_needs_first_draw_
&&
414 pending_swaps_
>= max_pending_swaps_
;
417 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
418 if (!CouldSendBeginMainFrame())
421 // Do not send begin main frame too many times in a single frame.
422 if (send_begin_main_frame_funnel_
)
425 // Only send BeginMainFrame when there isn't another commit pending already.
426 // Other parts of the state machine indirectly defer the BeginMainFrame
427 // by transitioning to WAITING commit states rather than going
428 // immediately to IDLE.
429 if (commit_state_
!= COMMIT_STATE_IDLE
)
432 // Don't send BeginMainFrame early if we are prioritizing the active tree
433 // because of impl_latency_takes_priority_.
434 if (impl_latency_takes_priority_
&&
435 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
439 // We should not send BeginMainFrame while we are in
440 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
441 // user input arriving soon.
442 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
443 // thread isn't consuming user input.
444 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
448 // We need a new commit for the forced redraw. This honors the
449 // single commit per interval because the result will be swapped to screen.
450 // TODO(brianderson): Remove this or move it below the
451 // SendingBeginMainFrameMightCauseDeadlock check since we want to avoid
452 // ever returning true from this method if we might cause deadlock.
453 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
456 // We shouldn't normally accept commits if there isn't an OutputSurface.
457 if (!HasInitializedOutputSurface())
460 // Make sure the BeginMainFrame can finish eventually if we start it.
461 if (SendingBeginMainFrameMightCauseDeadlock())
464 if (!settings_
.main_frame_while_swap_throttled_enabled
) {
465 // SwapAck throttle the BeginMainFrames unless we just swapped to
466 // potentially improve impl-thread latency over main-thread throughput.
467 // TODO(brianderson): Remove this restriction to improve throughput or
468 // make it conditional on impl_latency_takes_priority_.
469 bool just_swapped_in_deadline
=
470 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
471 did_perform_swap_in_last_draw_
;
472 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
476 if (skip_begin_main_frame_to_reduce_latency_
)
482 bool SchedulerStateMachine::ShouldCommit() const {
483 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
486 // We must not finish the commit until the pending tree is free.
487 if (has_pending_tree_
) {
488 DCHECK(settings_
.main_frame_before_activation_enabled
);
492 // If impl-side-painting, commit to the pending tree as soon as we can.
493 if (settings_
.impl_side_painting
)
496 // If we only have an active tree, it is incorrect to replace it
497 // before we've drawn it when we aren't impl-side-painting.
498 DCHECK(!settings_
.impl_side_painting
);
499 return !active_tree_needs_first_draw_
;
502 bool SchedulerStateMachine::ShouldPrepareTiles() const {
503 // PrepareTiles only really needs to be called immediately after commit
504 // and then periodically after that. Use a funnel to make sure we average
505 // one PrepareTiles per BeginImplFrame in the long run.
506 if (prepare_tiles_funnel_
> 0)
509 // Limiting to once per-frame is not enough, since we only want to
510 // prepare tiles _after_ draws.
511 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
514 return needs_prepare_tiles_
;
517 bool SchedulerStateMachine::ShouldInvalidateOutputSurface() const {
518 // Do not invalidate too many times in a frame.
519 if (invalidate_output_surface_funnel_
)
522 // Only the synchronous compositor requires invalidations.
523 if (!settings_
.using_synchronous_renderer_compositor
)
526 // Invalidations are only performed inside a BeginFrame.
527 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
530 // TODO(sunnyps): needs_prepare_tiles_ is needed here because PrepareTiles is
531 // called only inside the deadline / draw phase. We could remove this if we
532 // allowed PrepareTiles to happen in OnBeginImplFrame.
533 return needs_redraw_
|| needs_prepare_tiles_
;
536 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
537 if (ShouldActivatePendingTree())
538 return ACTION_ACTIVATE_SYNC_TREE
;
540 return ACTION_COMMIT
;
542 return ACTION_ANIMATE
;
544 if (PendingDrawsShouldBeAborted())
545 return ACTION_DRAW_AND_SWAP_ABORT
;
546 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
547 return ACTION_DRAW_AND_SWAP_FORCED
;
549 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
551 if (ShouldPrepareTiles())
552 return ACTION_PREPARE_TILES
;
553 if (ShouldSendBeginMainFrame())
554 return ACTION_SEND_BEGIN_MAIN_FRAME
;
555 if (ShouldInvalidateOutputSurface())
556 return ACTION_INVALIDATE_OUTPUT_SURFACE
;
557 if (ShouldBeginOutputSurfaceCreation())
558 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
562 void SchedulerStateMachine::UpdateState(Action action
) {
567 case ACTION_ACTIVATE_SYNC_TREE
:
568 UpdateStateOnActivation();
572 UpdateStateOnAnimate();
575 case ACTION_SEND_BEGIN_MAIN_FRAME
:
576 UpdateStateOnSendBeginMainFrame();
579 case ACTION_COMMIT
: {
580 bool commit_has_no_updates
= false;
581 UpdateStateOnCommit(commit_has_no_updates
);
585 case ACTION_DRAW_AND_SWAP_FORCED
:
586 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
587 bool did_request_swap
= true;
588 UpdateStateOnDraw(did_request_swap
);
592 case ACTION_DRAW_AND_SWAP_ABORT
: {
593 bool did_request_swap
= false;
594 UpdateStateOnDraw(did_request_swap
);
598 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
599 UpdateStateOnBeginOutputSurfaceCreation();
602 case ACTION_PREPARE_TILES
:
603 UpdateStateOnPrepareTiles();
606 case ACTION_INVALIDATE_OUTPUT_SURFACE
:
607 UpdateStateOnInvalidateOutputSurface();
612 void SchedulerStateMachine::UpdateStateOnAnimate() {
613 DCHECK(!animate_funnel_
);
614 last_frame_number_animate_performed_
= current_frame_number_
;
615 animate_funnel_
= true;
616 needs_animate_
= false;
617 // TODO(skyostil): Instead of assuming this, require the client to tell us.
621 void SchedulerStateMachine::UpdateStateOnSendBeginMainFrame() {
622 DCHECK(!has_pending_tree_
|| settings_
.main_frame_before_activation_enabled
);
624 DCHECK(!send_begin_main_frame_funnel_
);
625 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
626 needs_commit_
= false;
627 send_begin_main_frame_funnel_
= true;
628 last_frame_number_begin_main_frame_sent_
= current_frame_number_
;
631 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates
) {
634 // Animate after commit even if we've already animated.
635 if (!commit_has_no_updates
)
636 animate_funnel_
= false;
638 if (commit_has_no_updates
|| settings_
.main_frame_before_activation_enabled
) {
639 commit_state_
= COMMIT_STATE_IDLE
;
640 } else if (settings_
.impl_side_painting
) {
641 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
643 commit_state_
= settings_
.main_thread_should_always_be_low_latency
644 ? COMMIT_STATE_WAITING_FOR_DRAW
648 // If we are impl-side-painting but the commit was aborted, then we behave
649 // mostly as if we are not impl-side-painting since there is no pending tree.
650 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_has_no_updates
;
652 // Update state related to forced draws.
653 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
654 forced_redraw_state_
= has_pending_tree_
655 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
656 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
659 // Update the output surface state.
660 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
661 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
662 if (has_pending_tree_
) {
663 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
665 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
666 needs_redraw_
= true;
670 // Update state if we have a new active tree to draw, or if the active tree
671 // was unchanged but we need to do a forced draw.
672 if (!has_pending_tree_
&&
673 (!commit_has_no_updates
||
674 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
675 needs_redraw_
= true;
676 active_tree_needs_first_draw_
= true;
679 // This post-commit work is common to both completed and aborted commits.
680 pending_tree_is_ready_for_activation_
= false;
682 if (continuous_painting_
)
683 needs_commit_
= true;
684 last_commit_had_no_updates_
= commit_has_no_updates
;
687 void SchedulerStateMachine::UpdateStateOnActivation() {
688 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
689 commit_state_
= settings_
.main_thread_should_always_be_low_latency
690 ? COMMIT_STATE_WAITING_FOR_DRAW
694 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
695 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
697 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
698 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
700 has_pending_tree_
= false;
701 pending_tree_is_ready_for_activation_
= false;
702 active_tree_needs_first_draw_
= true;
703 needs_redraw_
= true;
706 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
707 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
708 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
710 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
711 commit_state_
= COMMIT_STATE_IDLE
;
713 needs_redraw_
= false;
714 active_tree_needs_first_draw_
= false;
716 if (did_request_swap
) {
717 DCHECK(!request_swap_funnel_
);
718 request_swap_funnel_
= true;
719 did_request_swap_in_last_frame_
= true;
720 last_frame_number_swap_requested_
= current_frame_number_
;
724 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
725 needs_prepare_tiles_
= false;
728 void SchedulerStateMachine::UpdateStateOnBeginOutputSurfaceCreation() {
729 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
730 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
732 // The following DCHECKs make sure we are in the proper quiescent state.
733 // The pipeline should be flushed entirely before we start output
734 // surface creation to avoid complicated corner cases.
735 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
736 DCHECK(!has_pending_tree_
);
737 DCHECK(!active_tree_needs_first_draw_
);
740 void SchedulerStateMachine::UpdateStateOnInvalidateOutputSurface() {
741 DCHECK(!invalidate_output_surface_funnel_
);
742 invalidate_output_surface_funnel_
= true;
743 last_frame_number_invalidate_output_surface_performed_
=
744 current_frame_number_
;
746 // The synchronous compositor makes no guarantees about a draw coming in after
747 // an invalidate so clear any flags that would cause the compositor's pipeline
749 active_tree_needs_first_draw_
= false; // blocks commit if true
752 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
753 TRACE_EVENT_INSTANT0("cc",
754 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
755 TRACE_EVENT_SCOPE_THREAD
);
756 skip_next_begin_main_frame_to_reduce_latency_
= true;
759 bool SchedulerStateMachine::BeginFrameRequiredForChildren() const {
760 return children_need_begin_frames_
;
762 bool SchedulerStateMachine::BeginFrameNeededForVideo() const {
763 return video_needs_begin_frames_
;
766 bool SchedulerStateMachine::BeginFrameNeeded() const {
767 // We can't handle BeginFrames when output surface isn't initialized.
768 // TODO(brianderson): Support output surface creation inside a BeginFrame.
769 if (!HasInitializedOutputSurface())
772 // If we are not visible, we don't need BeginFrame messages.
776 return (BeginFrameRequiredForAction() || BeginFrameRequiredForChildren() ||
777 BeginFrameNeededForVideo() || ProactiveBeginFrameWanted());
780 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
781 bool children_need_begin_frames
) {
782 children_need_begin_frames_
= children_need_begin_frames
;
785 void SchedulerStateMachine::SetVideoNeedsBeginFrames(
786 bool video_needs_begin_frames
) {
787 video_needs_begin_frames_
= video_needs_begin_frames
;
790 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
791 defer_commits_
= defer_commits
;
794 // These are the cases where we require a BeginFrame message to make progress
795 // on requested actions.
796 bool SchedulerStateMachine::BeginFrameRequiredForAction() const {
797 // The forced draw respects our normal draw scheduling, so we need to
798 // request a BeginImplFrame for it.
799 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
802 return needs_animate_
|| needs_redraw_
|| (needs_commit_
&& !defer_commits_
);
805 // These are cases where we are very likely want a BeginFrame message in the
806 // near future. Proactively requesting the BeginImplFrame helps hide the round
807 // trip latency of the SetNeedsBeginFrame request that has to go to the
809 // This includes things like drawing soon, but might not actually have a new
810 // frame to draw when we receive the next BeginImplFrame.
811 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
812 // Do not be proactive when invisible.
816 // We should proactively request a BeginImplFrame if a commit is pending
817 // because we will want to draw if the commit completes quickly. Do not
818 // request frames when commits are disabled, because the frame requests will
819 // not provide the needed commit (and will wake up the process when it could
821 if ((commit_state_
!= COMMIT_STATE_IDLE
) && !defer_commits_
)
824 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
825 // to draw the new active tree.
826 if (has_pending_tree_
)
829 // Changing priorities may allow us to activate (given the new priorities),
830 // which may result in a new frame.
831 if (needs_prepare_tiles_
)
834 // If we just sent a swap request, it's likely that we are going to produce
835 // another frame soon. This helps avoid negative glitches in our
836 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
837 // provider and get sampled at an inopportune time, delaying the next
839 if (did_request_swap_in_last_frame_
)
842 // If the last commit was aborted because of early out (no updates), we should
843 // still want a begin frame in case there is a commit coming again.
844 if (last_commit_had_no_updates_
)
850 void SchedulerStateMachine::OnBeginImplFrame() {
851 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
852 current_frame_number_
++;
854 last_commit_had_no_updates_
= false;
855 did_request_swap_in_last_frame_
= false;
857 // Clear funnels for any actions we perform during the frame.
858 animate_funnel_
= false;
859 send_begin_main_frame_funnel_
= false;
860 invalidate_output_surface_funnel_
= false;
862 // "Drain" the PrepareTiles funnel.
863 if (prepare_tiles_funnel_
> 0)
864 prepare_tiles_funnel_
--;
866 skip_begin_main_frame_to_reduce_latency_
=
867 skip_next_begin_main_frame_to_reduce_latency_
;
868 skip_next_begin_main_frame_to_reduce_latency_
= false;
871 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
872 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
875 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
876 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
878 did_perform_swap_in_last_draw_
= false;
880 // Clear funnels for any actions we perform during the deadline.
881 request_swap_funnel_
= false;
884 void SchedulerStateMachine::OnBeginImplFrameIdle() {
885 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
888 SchedulerStateMachine::BeginImplFrameDeadlineMode
889 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
890 if (settings_
.using_synchronous_renderer_compositor
) {
891 // No deadline for synchronous compositor.
892 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
;
893 } else if (wait_for_active_tree_ready_to_draw_
) {
894 // When we are waiting for ready to draw signal, we do not wait to post a
896 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
;
897 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
898 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
899 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
900 // We have an animation or fast input path on the impl thread that wants
901 // to draw, so don't wait too long for a new active tree.
902 // If we are swap throttled we should wait until we are unblocked.
903 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
905 // The impl thread doesn't have anything it wants to draw and we are just
906 // waiting for a new active tree or we are swap throttled. In short we are
908 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
912 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
914 // TODO(brianderson): This should take into account multiple commit sources.
915 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
918 // If we just forced activation, we should end the deadline right now.
919 if (PendingActivationsShouldBeForced() && !has_pending_tree_
)
922 // SwapAck throttle the deadline since we wont draw and swap anyway.
923 if (pending_swaps_
>= max_pending_swaps_
)
926 if (active_tree_needs_first_draw_
)
932 // This is used to prioritize impl-thread draws when the main thread isn't
933 // producing anything, e.g., after an aborted commit. We also check that we
934 // don't have a pending tree -- otherwise we should give it a chance to
936 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
937 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
940 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
941 if (impl_latency_takes_priority_
)
947 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
948 // If a commit is pending before the previous commit has been drawn, we
949 // are definitely in a high latency mode.
950 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
953 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
954 // thread is in a low latency mode.
955 if (send_begin_main_frame_funnel_
&&
956 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
957 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
960 // If there's a commit in progress it must either be from the previous frame
961 // or it started after the impl thread's deadline. In either case the main
962 // thread is in high latency mode.
966 // Similarly, if there's a pending tree the main thread is in high latency
967 // mode, because either
968 // it's from the previous frame
970 // we're currently drawing the active tree and the pending tree will thus
971 // only be drawn in the next frame.
972 if (has_pending_tree_
)
975 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
976 // Even if there's a new active tree to draw at the deadline or we've just
977 // swapped it, it may have been triggered by a previous BeginImplFrame, in
978 // which case the main thread is in a high latency mode.
979 return (active_tree_needs_first_draw_
|| did_perform_swap_in_last_draw_
) &&
980 !send_begin_main_frame_funnel_
;
983 // If the active tree needs its first draw in any other state, we know the
984 // main thread is in a high latency mode.
985 return active_tree_needs_first_draw_
;
988 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
990 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
992 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
994 void SchedulerStateMachine::SetNeedsAnimate() {
995 needs_animate_
= true;
998 void SchedulerStateMachine::SetWaitForReadyToDraw() {
999 DCHECK(settings_
.impl_side_painting
);
1000 wait_for_active_tree_ready_to_draw_
= true;
1003 void SchedulerStateMachine::SetNeedsPrepareTiles() {
1004 if (!needs_prepare_tiles_
) {
1005 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
1006 needs_prepare_tiles_
= true;
1010 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
1011 max_pending_swaps_
= max
;
1014 void SchedulerStateMachine::DidSwapBuffers() {
1016 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
1018 did_perform_swap_in_last_draw_
= true;
1019 last_frame_number_swap_performed_
= current_frame_number_
;
1022 void SchedulerStateMachine::DidSwapBuffersComplete() {
1023 DCHECK_GT(pending_swaps_
, 0);
1027 void SchedulerStateMachine::SetImplLatencyTakesPriority(
1028 bool impl_latency_takes_priority
) {
1029 impl_latency_takes_priority_
= impl_latency_takes_priority
;
1032 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
1034 case INVALID_RESULT
:
1035 NOTREACHED() << "Uninitialized DrawResult.";
1037 case DRAW_ABORTED_CANT_DRAW
:
1038 case DRAW_ABORTED_CONTEXT_LOST
:
1039 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1043 consecutive_checkerboard_animations_
= 0;
1044 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
1046 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
1047 needs_redraw_
= true;
1049 // If we're already in the middle of a redraw, we don't need to
1051 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
1054 needs_commit_
= true;
1055 consecutive_checkerboard_animations_
++;
1056 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
1057 consecutive_checkerboard_animations_
>=
1058 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
1059 consecutive_checkerboard_animations_
= 0;
1060 // We need to force a draw, but it doesn't make sense to do this until
1061 // we've committed and have new textures.
1062 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1065 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1066 // It's not clear whether this missing content is because of missing
1067 // pictures (which requires a commit) or because of memory pressure
1068 // removing textures (which might not). To be safe, request a commit
1070 needs_commit_
= true;
1075 void SchedulerStateMachine::SetNeedsCommit() {
1076 needs_commit_
= true;
1079 void SchedulerStateMachine::NotifyReadyToCommit() {
1080 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1081 << AsValue()->ToString();
1082 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1083 // In main thread low latency mode, commit should happen right after
1084 // BeginFrame, meaning when this function is called, next action should be
1086 if (settings_
.main_thread_should_always_be_low_latency
)
1087 DCHECK(ShouldCommit());
1090 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1091 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1093 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1094 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1095 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1096 commit_state_
= COMMIT_STATE_IDLE
;
1099 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1100 bool commit_has_no_updates
= true;
1101 UpdateStateOnCommit(commit_has_no_updates
);
1106 void SchedulerStateMachine::DidPrepareTiles() {
1107 needs_prepare_tiles_
= false;
1108 // "Fill" the PrepareTiles funnel.
1109 prepare_tiles_funnel_
++;
1112 void SchedulerStateMachine::DidLoseOutputSurface() {
1113 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1114 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1116 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1117 needs_redraw_
= false;
1118 wait_for_active_tree_ready_to_draw_
= false;
1121 void SchedulerStateMachine::NotifyReadyToActivate() {
1122 if (has_pending_tree_
)
1123 pending_tree_is_ready_for_activation_
= true;
1126 void SchedulerStateMachine::NotifyReadyToDraw() {
1127 wait_for_active_tree_ready_to_draw_
= false;
1130 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1131 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1132 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1134 if (did_create_and_initialize_first_output_surface_
) {
1135 // TODO(boliu): See if we can remove this when impl-side painting is always
1136 // on. Does anything on the main thread need to update after recreate?
1137 needs_commit_
= true;
1139 did_create_and_initialize_first_output_surface_
= true;
1143 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1144 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1145 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1148 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1149 switch (output_surface_state_
) {
1150 case OUTPUT_SURFACE_LOST
:
1151 case OUTPUT_SURFACE_CREATING
:
1154 case OUTPUT_SURFACE_ACTIVE
:
1155 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1156 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1163 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1164 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1165 needs_commit_
? 'T' : 'F',
1166 static_cast<int>(output_surface_state_
),
1167 static_cast<int>(begin_impl_frame_state_
),
1168 static_cast<int>(commit_state_
),
1169 has_pending_tree_
? 'T' : 'F',
1170 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1171 active_tree_needs_first_draw_
? 'T' : 'F',