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 prepare_tiles_funnel_(0),
30 consecutive_checkerboard_animations_(0),
31 max_pending_swaps_(1),
34 needs_animate_(false),
35 needs_prepare_tiles_(false),
37 inside_poll_for_anticipated_draw_triggers_(false),
41 has_pending_tree_(false),
42 pending_tree_is_ready_for_activation_(false),
43 active_tree_needs_first_draw_(false),
44 did_commit_after_animating_(false),
45 did_create_and_initialize_first_output_surface_(false),
46 impl_latency_takes_priority_(false),
47 skip_next_begin_main_frame_to_reduce_latency_(false),
48 skip_begin_main_frame_to_reduce_latency_(false),
49 continuous_painting_(false),
50 impl_latency_takes_priority_on_battery_(false),
51 children_need_begin_frames_(false),
52 defer_commits_(false) {
55 const char* SchedulerStateMachine::OutputSurfaceStateToString(
56 OutputSurfaceState state
) {
58 case OUTPUT_SURFACE_ACTIVE
:
59 return "OUTPUT_SURFACE_ACTIVE";
60 case OUTPUT_SURFACE_LOST
:
61 return "OUTPUT_SURFACE_LOST";
62 case OUTPUT_SURFACE_CREATING
:
63 return "OUTPUT_SURFACE_CREATING";
64 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
65 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
66 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
67 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
73 const char* SchedulerStateMachine::BeginImplFrameStateToString(
74 BeginImplFrameState state
) {
76 case BEGIN_IMPL_FRAME_STATE_IDLE
:
77 return "BEGIN_IMPL_FRAME_STATE_IDLE";
78 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
:
79 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
80 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
:
81 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
82 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
:
83 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
89 const char* SchedulerStateMachine::CommitStateToString(CommitState state
) {
91 case COMMIT_STATE_IDLE
:
92 return "COMMIT_STATE_IDLE";
93 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
:
94 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
95 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
:
96 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
97 case COMMIT_STATE_READY_TO_COMMIT
:
98 return "COMMIT_STATE_READY_TO_COMMIT";
99 case COMMIT_STATE_WAITING_FOR_ACTIVATION
:
100 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
101 case COMMIT_STATE_WAITING_FOR_DRAW
:
102 return "COMMIT_STATE_WAITING_FOR_DRAW";
108 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
109 ForcedRedrawOnTimeoutState state
) {
111 case FORCED_REDRAW_STATE_IDLE
:
112 return "FORCED_REDRAW_STATE_IDLE";
113 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
:
114 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
115 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
:
116 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
117 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW
:
118 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
124 const char* SchedulerStateMachine::ActionToString(Action action
) {
127 return "ACTION_NONE";
129 return "ACTION_ANIMATE";
130 case ACTION_SEND_BEGIN_MAIN_FRAME
:
131 return "ACTION_SEND_BEGIN_MAIN_FRAME";
133 return "ACTION_COMMIT";
134 case ACTION_ACTIVATE_SYNC_TREE
:
135 return "ACTION_ACTIVATE_SYNC_TREE";
136 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
137 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
138 case ACTION_DRAW_AND_SWAP_FORCED
:
139 return "ACTION_DRAW_AND_SWAP_FORCED";
140 case ACTION_DRAW_AND_SWAP_ABORT
:
141 return "ACTION_DRAW_AND_SWAP_ABORT";
142 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
143 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
144 case ACTION_PREPARE_TILES
:
145 return "ACTION_PREPARE_TILES";
151 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
152 SchedulerStateMachine::AsValue() const {
153 scoped_refptr
<base::trace_event::TracedValue
> state
=
154 new base::trace_event::TracedValue();
155 AsValueInto(state
.get(), gfx::FrameTime::Now());
159 void SchedulerStateMachine::AsValueInto(base::trace_event::TracedValue
* state
,
160 base::TimeTicks now
) const {
161 state
->BeginDictionary("major_state");
162 state
->SetString("next_action", ActionToString(NextAction()));
163 state
->SetString("begin_impl_frame_state",
164 BeginImplFrameStateToString(begin_impl_frame_state_
));
165 state
->SetString("commit_state", CommitStateToString(commit_state_
));
166 state
->SetString("output_surface_state_",
167 OutputSurfaceStateToString(output_surface_state_
));
168 state
->SetString("forced_redraw_state",
169 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_
));
170 state
->EndDictionary();
172 state
->BeginDictionary("major_timestamps_in_ms");
173 state
->SetDouble("0_interval",
174 begin_impl_frame_args_
.interval
.InMicroseconds() / 1000.0L);
177 (begin_impl_frame_args_
.deadline
- now
).InMicroseconds() / 1000.0L);
179 "2_frame_time_to_now",
180 (now
- begin_impl_frame_args_
.frame_time
).InMicroseconds() / 1000.0L);
181 state
->SetDouble("3_frame_time_to_deadline",
182 (begin_impl_frame_args_
.deadline
-
183 begin_impl_frame_args_
.frame_time
).InMicroseconds() /
185 state
->SetDouble("4_now",
186 (now
- base::TimeTicks()).InMicroseconds() / 1000.0L);
189 (begin_impl_frame_args_
.frame_time
- base::TimeTicks()).InMicroseconds() /
193 (begin_impl_frame_args_
.deadline
- base::TimeTicks()).InMicroseconds() /
195 state
->EndDictionary();
197 state
->BeginDictionary("minor_state");
198 state
->SetInteger("commit_count", commit_count_
);
199 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_
);
210 state
->SetInteger("prepare_tiles_funnel", prepare_tiles_funnel_
);
211 state
->SetInteger("consecutive_checkerboard_animations",
212 consecutive_checkerboard_animations_
);
213 state
->SetInteger("max_pending_swaps_", max_pending_swaps_
);
214 state
->SetInteger("pending_swaps_", pending_swaps_
);
215 state
->SetBoolean("needs_redraw", needs_redraw_
);
216 state
->SetBoolean("needs_animate_", needs_animate_
);
217 state
->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_
);
218 state
->SetBoolean("needs_commit", needs_commit_
);
219 state
->SetBoolean("visible", visible_
);
220 state
->SetBoolean("can_start", can_start_
);
221 state
->SetBoolean("can_draw", can_draw_
);
222 state
->SetBoolean("has_pending_tree", has_pending_tree_
);
223 state
->SetBoolean("pending_tree_is_ready_for_activation",
224 pending_tree_is_ready_for_activation_
);
225 state
->SetBoolean("active_tree_needs_first_draw",
226 active_tree_needs_first_draw_
);
227 state
->SetBoolean("did_commit_after_animating", did_commit_after_animating_
);
228 state
->SetBoolean("did_create_and_initialize_first_output_surface",
229 did_create_and_initialize_first_output_surface_
);
230 state
->SetBoolean("impl_latency_takes_priority",
231 impl_latency_takes_priority_
);
232 state
->SetBoolean("main_thread_is_in_high_latency_mode",
233 MainThreadIsInHighLatencyMode());
234 state
->SetBoolean("skip_begin_main_frame_to_reduce_latency",
235 skip_begin_main_frame_to_reduce_latency_
);
236 state
->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
237 skip_next_begin_main_frame_to_reduce_latency_
);
238 state
->SetBoolean("continuous_painting", continuous_painting_
);
239 state
->SetBoolean("impl_latency_takes_priority_on_battery",
240 impl_latency_takes_priority_on_battery_
);
241 state
->SetBoolean("children_need_begin_frames", children_need_begin_frames_
);
242 state
->SetBoolean("defer_commits", defer_commits_
);
243 state
->EndDictionary();
246 void SchedulerStateMachine::AdvanceCurrentFrameNumber() {
247 current_frame_number_
++;
249 // "Drain" the PrepareTiles funnel.
250 if (prepare_tiles_funnel_
> 0)
251 prepare_tiles_funnel_
--;
253 skip_begin_main_frame_to_reduce_latency_
=
254 skip_next_begin_main_frame_to_reduce_latency_
;
255 skip_next_begin_main_frame_to_reduce_latency_
= false;
258 bool SchedulerStateMachine::HasAnimatedThisFrame() const {
259 return last_frame_number_animate_performed_
== current_frame_number_
;
262 bool SchedulerStateMachine::HasSentBeginMainFrameThisFrame() const {
263 return current_frame_number_
==
264 last_frame_number_begin_main_frame_sent_
;
267 bool SchedulerStateMachine::HasSwappedThisFrame() const {
268 return current_frame_number_
== last_frame_number_swap_performed_
;
271 bool SchedulerStateMachine::HasRequestedSwapThisFrame() const {
272 return current_frame_number_
== last_frame_number_swap_requested_
;
275 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
276 // These are all the cases where we normally cannot or do not want to draw
277 // but, if needs_redraw_ is true and we do not draw to make forward progress,
278 // we might deadlock with the main thread.
279 // This should be a superset of PendingActivationsShouldBeForced() since
280 // activation of the pending tree is blocked by drawing of the active tree and
281 // the main thread might be blocked on activation of the most recent commit.
282 if (PendingActivationsShouldBeForced())
285 // Additional states where we should abort draws.
291 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
292 // There is no output surface to trigger our activations.
293 // If we do not force activations to make forward progress, we might deadlock
294 // with the main thread.
295 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
298 // If we're not visible, we should force activation.
299 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
300 // don't checkerboard until all visible resources are done. Furthermore, if we
301 // do keep the pending tree around, when becoming visible we might activate
302 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
303 // we can simply activate on becoming invisible since we don't need to draw
304 // the active tree when we're in this state.
311 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
312 // Don't try to initialize too early.
316 // We only want to start output surface initialization after the
317 // previous commit is complete.
318 if (commit_state_
!= COMMIT_STATE_IDLE
)
321 // Make sure the BeginImplFrame from any previous OutputSurfaces
322 // are complete before creating the new OutputSurface.
323 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
326 // We want to clear the pipline of any pending draws and activations
327 // before starting output surface initialization. This allows us to avoid
328 // weird corner cases where we abort draws or force activation while we
329 // are initializing the output surface.
330 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
333 // We need to create the output surface if we don't have one and we haven't
334 // started creating one yet.
335 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
338 bool SchedulerStateMachine::ShouldDraw() const {
339 // If we need to abort draws, we should do so ASAP since the draw could
340 // be blocking other important actions (like output surface initialization),
341 // from occuring. If we are waiting for the first draw, then perfom the
342 // aborted draw to keep things moving. If we are not waiting for the first
343 // draw however, we don't want to abort for no reason.
344 if (PendingDrawsShouldBeAborted())
345 return active_tree_needs_first_draw_
;
347 // Don't draw if we are waiting on the first commit after a surface.
348 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
351 // If a commit has occurred after the animate call, we need to call animate
352 // again before we should draw.
353 if (did_commit_after_animating_
)
356 // After this line, we only want to send a swap request once per frame.
357 if (HasRequestedSwapThisFrame())
360 // Do not queue too many swaps.
361 if (pending_swaps_
>= max_pending_swaps_
)
364 // Except for the cases above, do not draw outside of the BeginImplFrame
366 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
369 // Only handle forced redraws due to timeouts on the regular deadline.
370 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
373 return needs_redraw_
;
376 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
377 // There is nothing to activate.
378 if (!has_pending_tree_
)
381 // We should not activate a second tree before drawing the first one.
382 // Even if we need to force activation of the pending tree, we should abort
383 // drawing the active tree first.
384 if (active_tree_needs_first_draw_
)
387 // If we want to force activation, do so ASAP.
388 if (PendingActivationsShouldBeForced())
391 // At this point, only activate if we are ready to activate.
392 return pending_tree_is_ready_for_activation_
;
395 bool SchedulerStateMachine::ShouldAnimate() const {
396 // Don't animate if we are waiting on the first commit after a surface.
397 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
400 // If a commit occurred after our last call, we need to do animation again.
401 if (HasAnimatedThisFrame() && !did_commit_after_animating_
)
404 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
405 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
408 return needs_redraw_
|| needs_animate_
;
411 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
415 // We can not perform commits if we are not visible.
419 // Do not make a new commits when it is deferred.
426 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
427 if (!CouldSendBeginMainFrame())
430 // Only send BeginMainFrame when there isn't another commit pending already.
431 if (commit_state_
!= COMMIT_STATE_IDLE
)
434 // Don't send BeginMainFrame early if we are prioritizing the active tree
435 // because of impl_latency_takes_priority_.
436 if (impl_latency_takes_priority_
&&
437 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
441 // We should not send BeginMainFrame while we are in
442 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
443 // user input arriving soon.
444 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
445 // thread isn't consuming user input.
446 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
450 // We need a new commit for the forced redraw. This honors the
451 // single commit per interval because the result will be swapped to screen.
452 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
455 // After this point, we only start a commit once per frame.
456 if (HasSentBeginMainFrameThisFrame())
459 // We shouldn't normally accept commits if there isn't an OutputSurface.
460 if (!HasInitializedOutputSurface())
463 // SwapAck throttle the BeginMainFrames unless we just swapped.
464 // TODO(brianderson): Remove this restriction to improve throughput.
465 bool just_swapped_in_deadline
=
466 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
467 HasSwappedThisFrame();
468 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
471 if (skip_begin_main_frame_to_reduce_latency_
)
477 bool SchedulerStateMachine::ShouldCommit() const {
478 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
481 // We must not finish the commit until the pending tree is free.
482 if (has_pending_tree_
) {
483 DCHECK(settings_
.main_frame_before_activation_enabled
);
487 // Prioritize drawing the previous commit before finishing the next commit.
488 if (active_tree_needs_first_draw_
)
494 bool SchedulerStateMachine::ShouldPrepareTiles() const {
495 // PrepareTiles only really needs to be called immediately after commit
496 // and then periodically after that. Use a funnel to make sure we average
497 // one PrepareTiles per BeginImplFrame in the long run.
498 if (prepare_tiles_funnel_
> 0)
501 // Limiting to once per-frame is not enough, since we only want to
502 // prepare tiles _after_ draws. Polling for draw triggers and
503 // begin-frame are mutually exclusive, so we limit to these two cases.
504 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
505 !inside_poll_for_anticipated_draw_triggers_
)
507 return needs_prepare_tiles_
;
510 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
511 if (ShouldActivatePendingTree())
512 return ACTION_ACTIVATE_SYNC_TREE
;
514 return ACTION_COMMIT
;
516 return ACTION_ANIMATE
;
518 if (PendingDrawsShouldBeAborted())
519 return ACTION_DRAW_AND_SWAP_ABORT
;
520 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
521 return ACTION_DRAW_AND_SWAP_FORCED
;
523 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
525 if (ShouldPrepareTiles())
526 return ACTION_PREPARE_TILES
;
527 if (ShouldSendBeginMainFrame())
528 return ACTION_SEND_BEGIN_MAIN_FRAME
;
529 if (ShouldBeginOutputSurfaceCreation())
530 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
534 void SchedulerStateMachine::UpdateState(Action action
) {
539 case ACTION_ACTIVATE_SYNC_TREE
:
540 UpdateStateOnActivation();
544 last_frame_number_animate_performed_
= current_frame_number_
;
545 needs_animate_
= false;
546 did_commit_after_animating_
= false;
547 // TODO(skyostil): Instead of assuming this, require the client to tell
552 case ACTION_SEND_BEGIN_MAIN_FRAME
:
553 DCHECK(!has_pending_tree_
||
554 settings_
.main_frame_before_activation_enabled
);
556 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
557 needs_commit_
= false;
558 last_frame_number_begin_main_frame_sent_
=
559 current_frame_number_
;
562 case ACTION_COMMIT
: {
563 bool commit_has_no_updates
= false;
564 UpdateStateOnCommit(commit_has_no_updates
);
568 case ACTION_DRAW_AND_SWAP_FORCED
:
569 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
570 bool did_request_swap
= true;
571 UpdateStateOnDraw(did_request_swap
);
575 case ACTION_DRAW_AND_SWAP_ABORT
: {
576 bool did_request_swap
= false;
577 UpdateStateOnDraw(did_request_swap
);
581 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
582 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
583 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
585 // The following DCHECKs make sure we are in the proper quiescent state.
586 // The pipeline should be flushed entirely before we start output
587 // surface creation to avoid complicated corner cases.
588 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
589 DCHECK(!has_pending_tree_
);
590 DCHECK(!active_tree_needs_first_draw_
);
593 case ACTION_PREPARE_TILES
:
594 UpdateStateOnPrepareTiles();
599 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates
) {
602 if (!commit_has_no_updates
&& HasAnimatedThisFrame())
603 did_commit_after_animating_
= true;
605 if (commit_has_no_updates
|| settings_
.main_frame_before_activation_enabled
) {
606 commit_state_
= COMMIT_STATE_IDLE
;
607 } else if (settings_
.impl_side_painting
) {
608 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
610 commit_state_
= settings_
.main_thread_should_always_be_low_latency
611 ? COMMIT_STATE_WAITING_FOR_DRAW
615 // If we are impl-side-painting but the commit was aborted, then we behave
616 // mostly as if we are not impl-side-painting since there is no pending tree.
617 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_has_no_updates
;
619 // Update state related to forced draws.
620 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
621 forced_redraw_state_
= has_pending_tree_
622 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
623 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
626 // Update the output surface state.
627 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
628 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
629 if (has_pending_tree_
) {
630 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
632 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
633 needs_redraw_
= true;
637 // Update state if we have a new active tree to draw, or if the active tree
638 // was unchanged but we need to do a forced draw.
639 if (!has_pending_tree_
&&
640 (!commit_has_no_updates
||
641 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
642 needs_redraw_
= true;
643 active_tree_needs_first_draw_
= true;
646 // This post-commit work is common to both completed and aborted commits.
647 pending_tree_is_ready_for_activation_
= false;
649 if (continuous_painting_
)
650 needs_commit_
= true;
653 void SchedulerStateMachine::UpdateStateOnActivation() {
654 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
655 commit_state_
= settings_
.main_thread_should_always_be_low_latency
656 ? COMMIT_STATE_WAITING_FOR_DRAW
660 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
661 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
663 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
664 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
666 has_pending_tree_
= false;
667 pending_tree_is_ready_for_activation_
= false;
668 active_tree_needs_first_draw_
= true;
669 needs_redraw_
= true;
672 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
673 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
674 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
676 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
677 commit_state_
= COMMIT_STATE_IDLE
;
679 needs_redraw_
= false;
680 active_tree_needs_first_draw_
= false;
682 if (did_request_swap
)
683 last_frame_number_swap_requested_
= current_frame_number_
;
686 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
687 needs_prepare_tiles_
= false;
690 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
691 TRACE_EVENT_INSTANT0("cc",
692 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
693 TRACE_EVENT_SCOPE_THREAD
);
694 skip_next_begin_main_frame_to_reduce_latency_
= true;
697 bool SchedulerStateMachine::BeginFrameNeededForChildren() const {
698 if (HasInitializedOutputSurface())
699 return children_need_begin_frames_
;
704 bool SchedulerStateMachine::BeginFrameNeeded() const {
705 // We can't handle BeginFrames when output surface isn't initialized.
706 // TODO(brianderson): Support output surface creation inside a BeginFrame.
707 if (!HasInitializedOutputSurface())
710 if (SupportsProactiveBeginFrame()) {
711 return (BeginFrameNeededToAnimateOrDraw() ||
712 BeginFrameNeededForChildren() ||
713 ProactiveBeginFrameWanted());
716 // Proactive BeginFrames are bad for the synchronous compositor because we
717 // have to draw when we get the BeginFrame and could end up drawing many
718 // duplicate frames if our new frame isn't ready in time.
719 // To poll for state with the synchronous compositor without having to draw,
720 // we rely on ShouldPollForAnticipatedDrawTriggers instead.
721 // Synchronous compositor doesn't have a browser.
722 DCHECK(!children_need_begin_frames_
);
723 return BeginFrameNeededToAnimateOrDraw();
726 bool SchedulerStateMachine::ShouldSetNeedsBeginFrames(
727 bool frame_source_needs_begin_frames
) const {
728 bool needs_begin_frame
= BeginFrameNeeded();
730 // Never call SetNeedsBeginFrames if the frame source has the right value.
731 if (needs_begin_frame
== frame_source_needs_begin_frames
)
734 // Always request the BeginFrame immediately if it's needed.
735 if (needs_begin_frame
)
738 // Stop requesting BeginFrames after a deadline.
739 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
742 // Stop requesting BeginFrames immediately when output surface is lost.
743 if (!HasInitializedOutputSurface())
749 bool SchedulerStateMachine::ShouldPollForAnticipatedDrawTriggers() const {
750 // ShouldPollForAnticipatedDrawTriggers is what we use in place of
751 // ProactiveBeginFrameWanted when we are using the synchronous
753 if (!SupportsProactiveBeginFrame()) {
754 return !BeginFrameNeededToAnimateOrDraw() && ProactiveBeginFrameWanted();
757 // Non synchronous compositors should rely on
758 // ProactiveBeginFrameWanted to poll for state instead.
762 // Note: If SupportsProactiveBeginFrame is false, the scheduler should poll
763 // for changes in it's draw state so it can request a BeginFrame when it's
765 bool SchedulerStateMachine::SupportsProactiveBeginFrame() const {
766 // It is undesirable to proactively request BeginFrames if we are
767 // using a synchronous compositor because we *must* draw for every
768 // BeginFrame, which could cause duplicate draws.
769 return !settings_
.using_synchronous_renderer_compositor
;
772 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
773 bool children_need_begin_frames
) {
774 DCHECK(settings_
.forward_begin_frames_to_children
);
775 children_need_begin_frames_
= children_need_begin_frames
;
778 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
779 defer_commits_
= defer_commits
;
782 // These are the cases where we definitely (or almost definitely) have a
783 // new frame to animate and/or draw and can draw.
784 bool SchedulerStateMachine::BeginFrameNeededToAnimateOrDraw() const {
785 // The forced draw respects our normal draw scheduling, so we need to
786 // request a BeginImplFrame for it.
787 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
790 return needs_animate_
|| needs_redraw_
;
793 // These are cases where we are very likely to draw soon, but might not
794 // actually have a new frame to draw when we receive the next BeginImplFrame.
795 // Proactively requesting the BeginImplFrame helps hide the round trip latency
796 // of the SetNeedsBeginFrame request that has to go to the Browser.
797 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
798 // Do not be proactive when invisible.
802 // We should proactively request a BeginImplFrame if a commit is pending
803 // because we will want to draw if the commit completes quickly.
804 if (needs_commit_
|| commit_state_
!= COMMIT_STATE_IDLE
)
807 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
808 // to draw the new active tree.
809 if (has_pending_tree_
)
812 // Changing priorities may allow us to activate (given the new priorities),
813 // which may result in a new frame.
814 if (needs_prepare_tiles_
)
817 // If we just sent a swap request, it's likely that we are going to produce
818 // another frame soon. This helps avoid negative glitches in our
819 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
820 // provider and get sampled at an inopportune time, delaying the next
822 if (HasRequestedSwapThisFrame())
828 void SchedulerStateMachine::OnBeginImplFrame(const BeginFrameArgs
& args
) {
829 AdvanceCurrentFrameNumber();
830 begin_impl_frame_args_
= args
;
831 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_IDLE
)
832 << AsValue()->ToString();
833 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
836 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
837 DCHECK_EQ(begin_impl_frame_state_
,
838 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
839 << AsValue()->ToString();
840 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
843 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
844 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
845 << AsValue()->ToString();
846 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
849 void SchedulerStateMachine::OnBeginImplFrameIdle() {
850 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
851 << AsValue()->ToString();
852 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
855 SchedulerStateMachine::BeginImplFrameDeadlineMode
856 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
857 if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
858 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
859 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
860 // We have an animation or fast input path on the impl thread that wants
861 // to draw, so don't wait too long for a new active tree.
862 // If we are swap throttled we should wait until we are unblocked.
863 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
865 // The impl thread doesn't have anything it wants to draw and we are just
866 // waiting for a new active tree or we are swap throttled. In short we are
868 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
872 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
874 // TODO(brianderson): This should take into account multiple commit sources.
876 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
879 // If we've lost the output surface, end the current BeginImplFrame ASAP
880 // so we can start creating the next output surface.
881 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
884 // SwapAck throttle the deadline since we wont draw and swap anyway.
885 if (pending_swaps_
>= max_pending_swaps_
)
888 if (active_tree_needs_first_draw_
)
894 // This is used to prioritize impl-thread draws when the main thread isn't
895 // producing anything, e.g., after an aborted commit. We also check that we
896 // don't have a pending tree -- otherwise we should give it a chance to
898 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
899 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
902 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
903 if (impl_latency_takes_priority_
)
906 // If we are on battery power and want to prioritize impl latency because
907 // we don't trust deadline tasks to execute at the right time.
908 if (impl_latency_takes_priority_on_battery_
)
914 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
915 // If a commit is pending before the previous commit has been drawn, we
916 // are definitely in a high latency mode.
917 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
920 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
921 // thread is in a low latency mode.
922 if (HasSentBeginMainFrameThisFrame() &&
923 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
924 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
927 // If there's a commit in progress it must either be from the previous frame
928 // or it started after the impl thread's deadline. In either case the main
929 // thread is in high latency mode.
933 // Similarly, if there's a pending tree the main thread is in high latency
934 // mode, because either
935 // it's from the previous frame
937 // we're currently drawing the active tree and the pending tree will thus
938 // only be drawn in the next frame.
939 if (has_pending_tree_
)
942 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
943 // Even if there's a new active tree to draw at the deadline or we've just
944 // swapped it, it may have been triggered by a previous BeginImplFrame, in
945 // which case the main thread is in a high latency mode.
946 return (active_tree_needs_first_draw_
|| HasSwappedThisFrame()) &&
947 !HasSentBeginMainFrameThisFrame();
950 // If the active tree needs its first draw in any other state, we know the
951 // main thread is in a high latency mode.
952 return active_tree_needs_first_draw_
;
955 void SchedulerStateMachine::DidEnterPollForAnticipatedDrawTriggers() {
956 AdvanceCurrentFrameNumber();
957 inside_poll_for_anticipated_draw_triggers_
= true;
960 void SchedulerStateMachine::DidLeavePollForAnticipatedDrawTriggers() {
961 inside_poll_for_anticipated_draw_triggers_
= false;
964 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
966 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
968 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
970 void SchedulerStateMachine::SetNeedsAnimate() {
971 needs_animate_
= true;
974 void SchedulerStateMachine::SetNeedsPrepareTiles() {
975 if (!needs_prepare_tiles_
) {
976 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
977 needs_prepare_tiles_
= true;
981 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
982 max_pending_swaps_
= max
;
985 void SchedulerStateMachine::DidSwapBuffers() {
987 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
989 last_frame_number_swap_performed_
= current_frame_number_
;
992 void SchedulerStateMachine::DidSwapBuffersComplete() {
993 DCHECK_GT(pending_swaps_
, 0);
997 void SchedulerStateMachine::SetImplLatencyTakesPriority(
998 bool impl_latency_takes_priority
) {
999 impl_latency_takes_priority_
= impl_latency_takes_priority
;
1002 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
1004 case INVALID_RESULT
:
1005 NOTREACHED() << "Uninitialized DrawResult.";
1007 case DRAW_ABORTED_CANT_DRAW
:
1008 case DRAW_ABORTED_CONTEXT_LOST
:
1009 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1013 consecutive_checkerboard_animations_
= 0;
1014 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
1016 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
1017 needs_redraw_
= true;
1019 // If we're already in the middle of a redraw, we don't need to
1021 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
1024 needs_commit_
= true;
1025 consecutive_checkerboard_animations_
++;
1026 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
1027 consecutive_checkerboard_animations_
>=
1028 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
1029 consecutive_checkerboard_animations_
= 0;
1030 // We need to force a draw, but it doesn't make sense to do this until
1031 // we've committed and have new textures.
1032 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1035 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1036 // It's not clear whether this missing content is because of missing
1037 // pictures (which requires a commit) or because of memory pressure
1038 // removing textures (which might not). To be safe, request a commit
1040 needs_commit_
= true;
1045 void SchedulerStateMachine::SetNeedsCommit() {
1046 needs_commit_
= true;
1049 void SchedulerStateMachine::NotifyReadyToCommit() {
1050 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1051 << AsValue()->ToString();
1052 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1053 // In main thread low latency mode, commit should happen right after
1054 // BeginFrame, meaning when this function is called, next action should be
1056 if (settings_
.main_thread_should_always_be_low_latency
)
1057 DCHECK(ShouldCommit());
1060 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1061 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1063 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1064 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1065 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1066 commit_state_
= COMMIT_STATE_IDLE
;
1069 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1070 bool commit_has_no_updates
= true;
1071 UpdateStateOnCommit(commit_has_no_updates
);
1076 void SchedulerStateMachine::DidPrepareTiles() {
1077 needs_prepare_tiles_
= false;
1078 // "Fill" the PrepareTiles funnel.
1079 prepare_tiles_funnel_
++;
1082 void SchedulerStateMachine::DidLoseOutputSurface() {
1083 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1084 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1086 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1087 needs_redraw_
= false;
1090 void SchedulerStateMachine::NotifyReadyToActivate() {
1091 if (has_pending_tree_
)
1092 pending_tree_is_ready_for_activation_
= true;
1095 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1096 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1097 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1099 if (did_create_and_initialize_first_output_surface_
) {
1100 // TODO(boliu): See if we can remove this when impl-side painting is always
1101 // on. Does anything on the main thread need to update after recreate?
1102 needs_commit_
= true;
1104 did_create_and_initialize_first_output_surface_
= true;
1108 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1109 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1110 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1113 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1114 switch (output_surface_state_
) {
1115 case OUTPUT_SURFACE_LOST
:
1116 case OUTPUT_SURFACE_CREATING
:
1119 case OUTPUT_SURFACE_ACTIVE
:
1120 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1121 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1128 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1129 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1130 needs_commit_
? 'T' : 'F',
1131 static_cast<int>(output_surface_state_
),
1132 static_cast<int>(begin_impl_frame_state_
),
1133 static_cast<int>(commit_state_
),
1134 has_pending_tree_
? 'T' : 'F',
1135 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1136 active_tree_needs_first_draw_
? 'T' : 'F',