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 children_need_begin_frames_(false),
51 defer_commits_(false) {
54 const char* SchedulerStateMachine::OutputSurfaceStateToString(
55 OutputSurfaceState state
) {
57 case OUTPUT_SURFACE_ACTIVE
:
58 return "OUTPUT_SURFACE_ACTIVE";
59 case OUTPUT_SURFACE_LOST
:
60 return "OUTPUT_SURFACE_LOST";
61 case OUTPUT_SURFACE_CREATING
:
62 return "OUTPUT_SURFACE_CREATING";
63 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
64 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
65 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
66 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
72 const char* SchedulerStateMachine::BeginImplFrameStateToString(
73 BeginImplFrameState state
) {
75 case BEGIN_IMPL_FRAME_STATE_IDLE
:
76 return "BEGIN_IMPL_FRAME_STATE_IDLE";
77 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
:
78 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
79 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
:
80 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
81 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
:
82 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
88 const char* SchedulerStateMachine::CommitStateToString(CommitState state
) {
90 case COMMIT_STATE_IDLE
:
91 return "COMMIT_STATE_IDLE";
92 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
:
93 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
94 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
:
95 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
96 case COMMIT_STATE_READY_TO_COMMIT
:
97 return "COMMIT_STATE_READY_TO_COMMIT";
98 case COMMIT_STATE_WAITING_FOR_ACTIVATION
:
99 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
100 case COMMIT_STATE_WAITING_FOR_DRAW
:
101 return "COMMIT_STATE_WAITING_FOR_DRAW";
107 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
108 ForcedRedrawOnTimeoutState state
) {
110 case FORCED_REDRAW_STATE_IDLE
:
111 return "FORCED_REDRAW_STATE_IDLE";
112 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
:
113 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
114 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
:
115 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
116 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW
:
117 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
123 const char* SchedulerStateMachine::ActionToString(Action action
) {
126 return "ACTION_NONE";
128 return "ACTION_ANIMATE";
129 case ACTION_SEND_BEGIN_MAIN_FRAME
:
130 return "ACTION_SEND_BEGIN_MAIN_FRAME";
132 return "ACTION_COMMIT";
133 case ACTION_ACTIVATE_SYNC_TREE
:
134 return "ACTION_ACTIVATE_SYNC_TREE";
135 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
:
136 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
137 case ACTION_DRAW_AND_SWAP_FORCED
:
138 return "ACTION_DRAW_AND_SWAP_FORCED";
139 case ACTION_DRAW_AND_SWAP_ABORT
:
140 return "ACTION_DRAW_AND_SWAP_ABORT";
141 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
142 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
143 case ACTION_PREPARE_TILES
:
144 return "ACTION_PREPARE_TILES";
150 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
151 SchedulerStateMachine::AsValue() const {
152 scoped_refptr
<base::trace_event::TracedValue
> state
=
153 new base::trace_event::TracedValue();
154 AsValueInto(state
.get(), gfx::FrameTime::Now());
158 void SchedulerStateMachine::AsValueInto(base::trace_event::TracedValue
* state
,
159 base::TimeTicks now
) const {
160 state
->BeginDictionary("major_state");
161 state
->SetString("next_action", ActionToString(NextAction()));
162 state
->SetString("begin_impl_frame_state",
163 BeginImplFrameStateToString(begin_impl_frame_state_
));
164 state
->SetString("commit_state", CommitStateToString(commit_state_
));
165 state
->SetString("output_surface_state_",
166 OutputSurfaceStateToString(output_surface_state_
));
167 state
->SetString("forced_redraw_state",
168 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_
));
169 state
->EndDictionary();
171 state
->BeginDictionary("major_timestamps_in_ms");
172 state
->SetDouble("0_interval",
173 begin_impl_frame_args_
.interval
.InMicroseconds() / 1000.0L);
176 (begin_impl_frame_args_
.deadline
- now
).InMicroseconds() / 1000.0L);
178 "2_frame_time_to_now",
179 (now
- begin_impl_frame_args_
.frame_time
).InMicroseconds() / 1000.0L);
180 state
->SetDouble("3_frame_time_to_deadline",
181 (begin_impl_frame_args_
.deadline
-
182 begin_impl_frame_args_
.frame_time
).InMicroseconds() /
184 state
->SetDouble("4_now",
185 (now
- base::TimeTicks()).InMicroseconds() / 1000.0L);
188 (begin_impl_frame_args_
.frame_time
- base::TimeTicks()).InMicroseconds() /
192 (begin_impl_frame_args_
.deadline
- base::TimeTicks()).InMicroseconds() /
194 state
->EndDictionary();
196 state
->BeginDictionary("minor_state");
197 state
->SetInteger("commit_count", commit_count_
);
198 state
->SetInteger("current_frame_number", current_frame_number_
);
200 state
->SetInteger("last_frame_number_animate_performed",
201 last_frame_number_animate_performed_
);
202 state
->SetInteger("last_frame_number_swap_performed",
203 last_frame_number_swap_performed_
);
204 state
->SetInteger("last_frame_number_swap_requested",
205 last_frame_number_swap_requested_
);
206 state
->SetInteger("last_frame_number_begin_main_frame_sent",
207 last_frame_number_begin_main_frame_sent_
);
209 state
->SetInteger("prepare_tiles_funnel", prepare_tiles_funnel_
);
210 state
->SetInteger("consecutive_checkerboard_animations",
211 consecutive_checkerboard_animations_
);
212 state
->SetInteger("max_pending_swaps_", max_pending_swaps_
);
213 state
->SetInteger("pending_swaps_", pending_swaps_
);
214 state
->SetBoolean("needs_redraw", needs_redraw_
);
215 state
->SetBoolean("needs_animate_", needs_animate_
);
216 state
->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_
);
217 state
->SetBoolean("needs_commit", needs_commit_
);
218 state
->SetBoolean("visible", visible_
);
219 state
->SetBoolean("can_start", can_start_
);
220 state
->SetBoolean("can_draw", can_draw_
);
221 state
->SetBoolean("has_pending_tree", has_pending_tree_
);
222 state
->SetBoolean("pending_tree_is_ready_for_activation",
223 pending_tree_is_ready_for_activation_
);
224 state
->SetBoolean("active_tree_needs_first_draw",
225 active_tree_needs_first_draw_
);
226 state
->SetBoolean("did_commit_after_animating", did_commit_after_animating_
);
227 state
->SetBoolean("did_create_and_initialize_first_output_surface",
228 did_create_and_initialize_first_output_surface_
);
229 state
->SetBoolean("impl_latency_takes_priority",
230 impl_latency_takes_priority_
);
231 state
->SetBoolean("main_thread_is_in_high_latency_mode",
232 MainThreadIsInHighLatencyMode());
233 state
->SetBoolean("skip_begin_main_frame_to_reduce_latency",
234 skip_begin_main_frame_to_reduce_latency_
);
235 state
->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
236 skip_next_begin_main_frame_to_reduce_latency_
);
237 state
->SetBoolean("continuous_painting", continuous_painting_
);
238 state
->SetBoolean("children_need_begin_frames", children_need_begin_frames_
);
239 state
->SetBoolean("defer_commits", defer_commits_
);
240 state
->EndDictionary();
243 void SchedulerStateMachine::AdvanceCurrentFrameNumber() {
244 current_frame_number_
++;
246 // "Drain" the PrepareTiles funnel.
247 if (prepare_tiles_funnel_
> 0)
248 prepare_tiles_funnel_
--;
250 skip_begin_main_frame_to_reduce_latency_
=
251 skip_next_begin_main_frame_to_reduce_latency_
;
252 skip_next_begin_main_frame_to_reduce_latency_
= false;
255 bool SchedulerStateMachine::HasAnimatedThisFrame() const {
256 return last_frame_number_animate_performed_
== current_frame_number_
;
259 bool SchedulerStateMachine::HasSentBeginMainFrameThisFrame() const {
260 return current_frame_number_
==
261 last_frame_number_begin_main_frame_sent_
;
264 bool SchedulerStateMachine::HasSwappedThisFrame() const {
265 return current_frame_number_
== last_frame_number_swap_performed_
;
268 bool SchedulerStateMachine::HasRequestedSwapThisFrame() const {
269 return current_frame_number_
== last_frame_number_swap_requested_
;
272 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
273 // These are all the cases where we normally cannot or do not want to draw
274 // but, if needs_redraw_ is true and we do not draw to make forward progress,
275 // we might deadlock with the main thread.
276 // This should be a superset of PendingActivationsShouldBeForced() since
277 // activation of the pending tree is blocked by drawing of the active tree and
278 // the main thread might be blocked on activation of the most recent commit.
279 if (PendingActivationsShouldBeForced())
282 // Additional states where we should abort draws.
288 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
289 // There is no output surface to trigger our activations.
290 // If we do not force activations to make forward progress, we might deadlock
291 // with the main thread.
292 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
295 // If we're not visible, we should force activation.
296 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
297 // don't checkerboard until all visible resources are done. Furthermore, if we
298 // do keep the pending tree around, when becoming visible we might activate
299 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
300 // we can simply activate on becoming invisible since we don't need to draw
301 // the active tree when we're in this state.
308 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
309 // Don't try to initialize too early.
313 // We only want to start output surface initialization after the
314 // previous commit is complete.
315 if (commit_state_
!= COMMIT_STATE_IDLE
)
318 // Make sure the BeginImplFrame from any previous OutputSurfaces
319 // are complete before creating the new OutputSurface.
320 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_IDLE
)
323 // We want to clear the pipline of any pending draws and activations
324 // before starting output surface initialization. This allows us to avoid
325 // weird corner cases where we abort draws or force activation while we
326 // are initializing the output surface.
327 if (active_tree_needs_first_draw_
|| has_pending_tree_
)
330 // We need to create the output surface if we don't have one and we haven't
331 // started creating one yet.
332 return output_surface_state_
== OUTPUT_SURFACE_LOST
;
335 bool SchedulerStateMachine::ShouldDraw() const {
336 // If we need to abort draws, we should do so ASAP since the draw could
337 // be blocking other important actions (like output surface initialization),
338 // from occuring. If we are waiting for the first draw, then perfom the
339 // aborted draw to keep things moving. If we are not waiting for the first
340 // draw however, we don't want to abort for no reason.
341 if (PendingDrawsShouldBeAborted())
342 return active_tree_needs_first_draw_
;
344 // Don't draw if we are waiting on the first commit after a surface.
345 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
348 // If a commit has occurred after the animate call, we need to call animate
349 // again before we should draw.
350 if (did_commit_after_animating_
)
353 // After this line, we only want to send a swap request once per frame.
354 if (HasRequestedSwapThisFrame())
357 // Do not queue too many swaps.
358 if (pending_swaps_
>= max_pending_swaps_
)
361 // Except for the cases above, do not draw outside of the BeginImplFrame
363 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
366 // Only handle forced redraws due to timeouts on the regular deadline.
367 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
370 return needs_redraw_
;
373 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
374 // There is nothing to activate.
375 if (!has_pending_tree_
)
378 // We should not activate a second tree before drawing the first one.
379 // Even if we need to force activation of the pending tree, we should abort
380 // drawing the active tree first.
381 if (active_tree_needs_first_draw_
)
384 // If we want to force activation, do so ASAP.
385 if (PendingActivationsShouldBeForced())
388 // At this point, only activate if we are ready to activate.
389 return pending_tree_is_ready_for_activation_
;
392 bool SchedulerStateMachine::ShouldAnimate() const {
393 // Don't animate if we are waiting on the first commit after a surface.
394 if (output_surface_state_
!= OUTPUT_SURFACE_ACTIVE
)
397 // If a commit occurred after our last call, we need to do animation again.
398 if (HasAnimatedThisFrame() && !did_commit_after_animating_
)
401 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
&&
402 begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
405 return needs_redraw_
|| needs_animate_
;
408 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
412 // We can not perform commits if we are not visible.
416 // Do not make a new commits when it is deferred.
423 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
424 if (!CouldSendBeginMainFrame())
427 // Only send BeginMainFrame when there isn't another commit pending already.
428 if (commit_state_
!= COMMIT_STATE_IDLE
)
431 // Don't send BeginMainFrame early if we are prioritizing the active tree
432 // because of impl_latency_takes_priority_.
433 if (impl_latency_takes_priority_
&&
434 (has_pending_tree_
|| active_tree_needs_first_draw_
)) {
438 // We should not send BeginMainFrame while we are in
439 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
440 // user input arriving soon.
441 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
442 // thread isn't consuming user input.
443 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_IDLE
&&
447 // We need a new commit for the forced redraw. This honors the
448 // single commit per interval because the result will be swapped to screen.
449 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
)
452 // After this point, we only start a commit once per frame.
453 if (HasSentBeginMainFrameThisFrame())
456 // We shouldn't normally accept commits if there isn't an OutputSurface.
457 if (!HasInitializedOutputSurface())
460 // SwapAck throttle the BeginMainFrames unless we just swapped.
461 // TODO(brianderson): Remove this restriction to improve throughput.
462 bool just_swapped_in_deadline
=
463 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
464 HasSwappedThisFrame();
465 if (pending_swaps_
>= max_pending_swaps_
&& !just_swapped_in_deadline
)
468 if (skip_begin_main_frame_to_reduce_latency_
)
474 bool SchedulerStateMachine::ShouldCommit() const {
475 if (commit_state_
!= COMMIT_STATE_READY_TO_COMMIT
)
478 // We must not finish the commit until the pending tree is free.
479 if (has_pending_tree_
) {
480 DCHECK(settings_
.main_frame_before_activation_enabled
);
484 // Prioritize drawing the previous commit before finishing the next commit.
485 if (active_tree_needs_first_draw_
)
491 bool SchedulerStateMachine::ShouldPrepareTiles() const {
492 // PrepareTiles only really needs to be called immediately after commit
493 // and then periodically after that. Use a funnel to make sure we average
494 // one PrepareTiles per BeginImplFrame in the long run.
495 if (prepare_tiles_funnel_
> 0)
498 // Limiting to once per-frame is not enough, since we only want to
499 // prepare tiles _after_ draws. Polling for draw triggers and
500 // begin-frame are mutually exclusive, so we limit to these two cases.
501 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
&&
502 !inside_poll_for_anticipated_draw_triggers_
)
504 return needs_prepare_tiles_
;
507 SchedulerStateMachine::Action
SchedulerStateMachine::NextAction() const {
508 if (ShouldActivatePendingTree())
509 return ACTION_ACTIVATE_SYNC_TREE
;
511 return ACTION_COMMIT
;
513 return ACTION_ANIMATE
;
515 if (PendingDrawsShouldBeAborted())
516 return ACTION_DRAW_AND_SWAP_ABORT
;
517 else if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
518 return ACTION_DRAW_AND_SWAP_FORCED
;
520 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE
;
522 if (ShouldPrepareTiles())
523 return ACTION_PREPARE_TILES
;
524 if (ShouldSendBeginMainFrame())
525 return ACTION_SEND_BEGIN_MAIN_FRAME
;
526 if (ShouldBeginOutputSurfaceCreation())
527 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION
;
531 void SchedulerStateMachine::UpdateState(Action action
) {
536 case ACTION_ACTIVATE_SYNC_TREE
:
537 UpdateStateOnActivation();
541 last_frame_number_animate_performed_
= current_frame_number_
;
542 needs_animate_
= false;
543 did_commit_after_animating_
= false;
544 // TODO(skyostil): Instead of assuming this, require the client to tell
549 case ACTION_SEND_BEGIN_MAIN_FRAME
:
550 DCHECK(!has_pending_tree_
||
551 settings_
.main_frame_before_activation_enabled
);
553 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
;
554 needs_commit_
= false;
555 last_frame_number_begin_main_frame_sent_
=
556 current_frame_number_
;
559 case ACTION_COMMIT
: {
560 bool commit_has_no_updates
= false;
561 UpdateStateOnCommit(commit_has_no_updates
);
565 case ACTION_DRAW_AND_SWAP_FORCED
:
566 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE
: {
567 bool did_request_swap
= true;
568 UpdateStateOnDraw(did_request_swap
);
572 case ACTION_DRAW_AND_SWAP_ABORT
: {
573 bool did_request_swap
= false;
574 UpdateStateOnDraw(did_request_swap
);
578 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION
:
579 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_LOST
);
580 output_surface_state_
= OUTPUT_SURFACE_CREATING
;
582 // The following DCHECKs make sure we are in the proper quiescent state.
583 // The pipeline should be flushed entirely before we start output
584 // surface creation to avoid complicated corner cases.
585 DCHECK_EQ(commit_state_
, COMMIT_STATE_IDLE
);
586 DCHECK(!has_pending_tree_
);
587 DCHECK(!active_tree_needs_first_draw_
);
590 case ACTION_PREPARE_TILES
:
591 UpdateStateOnPrepareTiles();
596 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates
) {
599 if (!commit_has_no_updates
&& HasAnimatedThisFrame())
600 did_commit_after_animating_
= true;
602 if (commit_has_no_updates
|| settings_
.main_frame_before_activation_enabled
) {
603 commit_state_
= COMMIT_STATE_IDLE
;
604 } else if (settings_
.impl_side_painting
) {
605 commit_state_
= COMMIT_STATE_WAITING_FOR_ACTIVATION
;
607 commit_state_
= settings_
.main_thread_should_always_be_low_latency
608 ? COMMIT_STATE_WAITING_FOR_DRAW
612 // If we are impl-side-painting but the commit was aborted, then we behave
613 // mostly as if we are not impl-side-painting since there is no pending tree.
614 has_pending_tree_
= settings_
.impl_side_painting
&& !commit_has_no_updates
;
616 // Update state related to forced draws.
617 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
) {
618 forced_redraw_state_
= has_pending_tree_
619 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
620 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
623 // Update the output surface state.
624 DCHECK_NE(output_surface_state_
, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
);
625 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
) {
626 if (has_pending_tree_
) {
627 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
;
629 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
630 needs_redraw_
= true;
634 // Update state if we have a new active tree to draw, or if the active tree
635 // was unchanged but we need to do a forced draw.
636 if (!has_pending_tree_
&&
637 (!commit_has_no_updates
||
638 forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)) {
639 needs_redraw_
= true;
640 active_tree_needs_first_draw_
= true;
643 // This post-commit work is common to both completed and aborted commits.
644 pending_tree_is_ready_for_activation_
= false;
646 if (continuous_painting_
)
647 needs_commit_
= true;
650 void SchedulerStateMachine::UpdateStateOnActivation() {
651 if (commit_state_
== COMMIT_STATE_WAITING_FOR_ACTIVATION
) {
652 commit_state_
= settings_
.main_thread_should_always_be_low_latency
653 ? COMMIT_STATE_WAITING_FOR_DRAW
657 if (output_surface_state_
== OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
)
658 output_surface_state_
= OUTPUT_SURFACE_ACTIVE
;
660 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
)
661 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_DRAW
;
663 has_pending_tree_
= false;
664 pending_tree_is_ready_for_activation_
= false;
665 active_tree_needs_first_draw_
= true;
666 needs_redraw_
= true;
669 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap
) {
670 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
671 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
673 if (commit_state_
== COMMIT_STATE_WAITING_FOR_DRAW
)
674 commit_state_
= COMMIT_STATE_IDLE
;
676 needs_redraw_
= false;
677 active_tree_needs_first_draw_
= false;
679 if (did_request_swap
)
680 last_frame_number_swap_requested_
= current_frame_number_
;
683 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
684 needs_prepare_tiles_
= false;
687 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
688 TRACE_EVENT_INSTANT0("cc",
689 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
690 TRACE_EVENT_SCOPE_THREAD
);
691 skip_next_begin_main_frame_to_reduce_latency_
= true;
694 bool SchedulerStateMachine::BeginFrameNeededForChildren() const {
695 if (HasInitializedOutputSurface())
696 return children_need_begin_frames_
;
701 bool SchedulerStateMachine::BeginFrameNeeded() const {
702 // We can't handle BeginFrames when output surface isn't initialized.
703 // TODO(brianderson): Support output surface creation inside a BeginFrame.
704 if (!HasInitializedOutputSurface())
707 if (SupportsProactiveBeginFrame()) {
708 return (BeginFrameNeededToAnimateOrDraw() ||
709 BeginFrameNeededForChildren() ||
710 ProactiveBeginFrameWanted());
713 // Proactive BeginFrames are bad for the synchronous compositor because we
714 // have to draw when we get the BeginFrame and could end up drawing many
715 // duplicate frames if our new frame isn't ready in time.
716 // To poll for state with the synchronous compositor without having to draw,
717 // we rely on ShouldPollForAnticipatedDrawTriggers instead.
718 // Synchronous compositor doesn't have a browser.
719 DCHECK(!children_need_begin_frames_
);
720 return BeginFrameNeededToAnimateOrDraw();
723 bool SchedulerStateMachine::ShouldSetNeedsBeginFrames(
724 bool frame_source_needs_begin_frames
) const {
725 bool needs_begin_frame
= BeginFrameNeeded();
727 // Never call SetNeedsBeginFrames if the frame source has the right value.
728 if (needs_begin_frame
== frame_source_needs_begin_frames
)
731 // Always request the BeginFrame immediately if it's needed.
732 if (needs_begin_frame
)
735 // Stop requesting BeginFrames after a deadline.
736 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
739 // Stop requesting BeginFrames immediately when output surface is lost.
740 if (!HasInitializedOutputSurface())
746 bool SchedulerStateMachine::ShouldPollForAnticipatedDrawTriggers() const {
747 // ShouldPollForAnticipatedDrawTriggers is what we use in place of
748 // ProactiveBeginFrameWanted when we are using the synchronous
750 if (!SupportsProactiveBeginFrame()) {
751 return !BeginFrameNeededToAnimateOrDraw() && ProactiveBeginFrameWanted();
754 // Non synchronous compositors should rely on
755 // ProactiveBeginFrameWanted to poll for state instead.
759 // Note: If SupportsProactiveBeginFrame is false, the scheduler should poll
760 // for changes in it's draw state so it can request a BeginFrame when it's
762 bool SchedulerStateMachine::SupportsProactiveBeginFrame() const {
763 // It is undesirable to proactively request BeginFrames if we are
764 // using a synchronous compositor because we *must* draw for every
765 // BeginFrame, which could cause duplicate draws.
766 return !settings_
.using_synchronous_renderer_compositor
;
769 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
770 bool children_need_begin_frames
) {
771 children_need_begin_frames_
= children_need_begin_frames
;
774 void SchedulerStateMachine::SetDeferCommits(bool defer_commits
) {
775 defer_commits_
= defer_commits
;
778 // These are the cases where we definitely (or almost definitely) have a
779 // new frame to animate and/or draw and can draw.
780 bool SchedulerStateMachine::BeginFrameNeededToAnimateOrDraw() const {
781 // The forced draw respects our normal draw scheduling, so we need to
782 // request a BeginImplFrame for it.
783 if (forced_redraw_state_
== FORCED_REDRAW_STATE_WAITING_FOR_DRAW
)
786 return needs_animate_
|| needs_redraw_
;
789 // These are cases where we are very likely to draw soon, but might not
790 // actually have a new frame to draw when we receive the next BeginImplFrame.
791 // Proactively requesting the BeginImplFrame helps hide the round trip latency
792 // of the SetNeedsBeginFrame request that has to go to the Browser.
793 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
794 // Do not be proactive when invisible.
798 // We should proactively request a BeginImplFrame if a commit is pending
799 // because we will want to draw if the commit completes quickly.
800 if (needs_commit_
|| commit_state_
!= COMMIT_STATE_IDLE
)
803 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
804 // to draw the new active tree.
805 if (has_pending_tree_
)
808 // Changing priorities may allow us to activate (given the new priorities),
809 // which may result in a new frame.
810 if (needs_prepare_tiles_
)
813 // If we just sent a swap request, it's likely that we are going to produce
814 // another frame soon. This helps avoid negative glitches in our
815 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
816 // provider and get sampled at an inopportune time, delaying the next
818 if (HasRequestedSwapThisFrame())
824 void SchedulerStateMachine::OnBeginImplFrame(const BeginFrameArgs
& args
) {
825 AdvanceCurrentFrameNumber();
826 begin_impl_frame_args_
= args
;
827 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_IDLE
)
828 << AsValue()->ToString();
829 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
;
832 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
833 DCHECK_EQ(begin_impl_frame_state_
,
834 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
)
835 << AsValue()->ToString();
836 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
;
839 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
840 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
841 << AsValue()->ToString();
842 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
;
845 void SchedulerStateMachine::OnBeginImplFrameIdle() {
846 DCHECK_EQ(begin_impl_frame_state_
, BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
)
847 << AsValue()->ToString();
848 begin_impl_frame_state_
= BEGIN_IMPL_FRAME_STATE_IDLE
;
851 SchedulerStateMachine::BeginImplFrameDeadlineMode
852 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
853 if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
854 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
;
855 } else if (needs_redraw_
&& pending_swaps_
< max_pending_swaps_
) {
856 // We have an animation or fast input path on the impl thread that wants
857 // to draw, so don't wait too long for a new active tree.
858 // If we are swap throttled we should wait until we are unblocked.
859 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
;
861 // The impl thread doesn't have anything it wants to draw and we are just
862 // waiting for a new active tree or we are swap throttled. In short we are
864 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
;
868 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
870 // TODO(brianderson): This should take into account multiple commit sources.
872 if (begin_impl_frame_state_
!= BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
)
875 // If we've lost the output surface, end the current BeginImplFrame ASAP
876 // so we can start creating the next output surface.
877 if (output_surface_state_
== OUTPUT_SURFACE_LOST
)
880 // SwapAck throttle the deadline since we wont draw and swap anyway.
881 if (pending_swaps_
>= max_pending_swaps_
)
884 if (active_tree_needs_first_draw_
)
890 // This is used to prioritize impl-thread draws when the main thread isn't
891 // producing anything, e.g., after an aborted commit. We also check that we
892 // don't have a pending tree -- otherwise we should give it a chance to
894 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
895 if (commit_state_
== COMMIT_STATE_IDLE
&& !has_pending_tree_
)
898 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
899 if (impl_latency_takes_priority_
)
905 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
906 // If a commit is pending before the previous commit has been drawn, we
907 // are definitely in a high latency mode.
908 if (CommitPending() && (active_tree_needs_first_draw_
|| has_pending_tree_
))
911 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
912 // thread is in a low latency mode.
913 if (HasSentBeginMainFrameThisFrame() &&
914 (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
||
915 begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
))
918 // If there's a commit in progress it must either be from the previous frame
919 // or it started after the impl thread's deadline. In either case the main
920 // thread is in high latency mode.
924 // Similarly, if there's a pending tree the main thread is in high latency
925 // mode, because either
926 // it's from the previous frame
928 // we're currently drawing the active tree and the pending tree will thus
929 // only be drawn in the next frame.
930 if (has_pending_tree_
)
933 if (begin_impl_frame_state_
== BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
) {
934 // Even if there's a new active tree to draw at the deadline or we've just
935 // swapped it, it may have been triggered by a previous BeginImplFrame, in
936 // which case the main thread is in a high latency mode.
937 return (active_tree_needs_first_draw_
|| HasSwappedThisFrame()) &&
938 !HasSentBeginMainFrameThisFrame();
941 // If the active tree needs its first draw in any other state, we know the
942 // main thread is in a high latency mode.
943 return active_tree_needs_first_draw_
;
946 void SchedulerStateMachine::DidEnterPollForAnticipatedDrawTriggers() {
947 AdvanceCurrentFrameNumber();
948 inside_poll_for_anticipated_draw_triggers_
= true;
951 void SchedulerStateMachine::DidLeavePollForAnticipatedDrawTriggers() {
952 inside_poll_for_anticipated_draw_triggers_
= false;
955 void SchedulerStateMachine::SetVisible(bool visible
) { visible_
= visible
; }
957 void SchedulerStateMachine::SetCanDraw(bool can_draw
) { can_draw_
= can_draw
; }
959 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_
= true; }
961 void SchedulerStateMachine::SetNeedsAnimate() {
962 needs_animate_
= true;
965 void SchedulerStateMachine::SetNeedsPrepareTiles() {
966 if (!needs_prepare_tiles_
) {
967 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
968 needs_prepare_tiles_
= true;
972 void SchedulerStateMachine::SetMaxSwapsPending(int max
) {
973 max_pending_swaps_
= max
;
976 void SchedulerStateMachine::DidSwapBuffers() {
978 DCHECK_LE(pending_swaps_
, max_pending_swaps_
);
980 last_frame_number_swap_performed_
= current_frame_number_
;
983 void SchedulerStateMachine::DidSwapBuffersComplete() {
984 DCHECK_GT(pending_swaps_
, 0);
988 void SchedulerStateMachine::SetImplLatencyTakesPriority(
989 bool impl_latency_takes_priority
) {
990 impl_latency_takes_priority_
= impl_latency_takes_priority
;
993 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result
) {
996 NOTREACHED() << "Uninitialized DrawResult.";
998 case DRAW_ABORTED_CANT_DRAW
:
999 case DRAW_ABORTED_CONTEXT_LOST
:
1000 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1004 consecutive_checkerboard_animations_
= 0;
1005 forced_redraw_state_
= FORCED_REDRAW_STATE_IDLE
;
1007 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS
:
1008 needs_redraw_
= true;
1010 // If we're already in the middle of a redraw, we don't need to
1012 if (forced_redraw_state_
!= FORCED_REDRAW_STATE_IDLE
)
1015 needs_commit_
= true;
1016 consecutive_checkerboard_animations_
++;
1017 if (settings_
.timeout_and_draw_when_animation_checkerboards
&&
1018 consecutive_checkerboard_animations_
>=
1019 settings_
.maximum_number_of_failed_draws_before_draw_is_forced_
) {
1020 consecutive_checkerboard_animations_
= 0;
1021 // We need to force a draw, but it doesn't make sense to do this until
1022 // we've committed and have new textures.
1023 forced_redraw_state_
= FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
;
1026 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT
:
1027 // It's not clear whether this missing content is because of missing
1028 // pictures (which requires a commit) or because of memory pressure
1029 // removing textures (which might not). To be safe, request a commit
1031 needs_commit_
= true;
1036 void SchedulerStateMachine::SetNeedsCommit() {
1037 needs_commit_
= true;
1040 void SchedulerStateMachine::NotifyReadyToCommit() {
1041 DCHECK(commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
)
1042 << AsValue()->ToString();
1043 commit_state_
= COMMIT_STATE_READY_TO_COMMIT
;
1044 // In main thread low latency mode, commit should happen right after
1045 // BeginFrame, meaning when this function is called, next action should be
1047 if (settings_
.main_thread_should_always_be_low_latency
)
1048 DCHECK(ShouldCommit());
1051 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason
) {
1052 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1054 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST
:
1055 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE
:
1056 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT
:
1057 commit_state_
= COMMIT_STATE_IDLE
;
1060 case CommitEarlyOutReason::FINISHED_NO_UPDATES
:
1061 bool commit_has_no_updates
= true;
1062 UpdateStateOnCommit(commit_has_no_updates
);
1067 void SchedulerStateMachine::DidPrepareTiles() {
1068 needs_prepare_tiles_
= false;
1069 // "Fill" the PrepareTiles funnel.
1070 prepare_tiles_funnel_
++;
1073 void SchedulerStateMachine::DidLoseOutputSurface() {
1074 if (output_surface_state_
== OUTPUT_SURFACE_LOST
||
1075 output_surface_state_
== OUTPUT_SURFACE_CREATING
)
1077 output_surface_state_
= OUTPUT_SURFACE_LOST
;
1078 needs_redraw_
= false;
1081 void SchedulerStateMachine::NotifyReadyToActivate() {
1082 if (has_pending_tree_
)
1083 pending_tree_is_ready_for_activation_
= true;
1086 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1087 DCHECK_EQ(output_surface_state_
, OUTPUT_SURFACE_CREATING
);
1088 output_surface_state_
= OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
;
1090 if (did_create_and_initialize_first_output_surface_
) {
1091 // TODO(boliu): See if we can remove this when impl-side painting is always
1092 // on. Does anything on the main thread need to update after recreate?
1093 needs_commit_
= true;
1095 did_create_and_initialize_first_output_surface_
= true;
1099 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1100 DCHECK_EQ(commit_state_
, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
);
1101 commit_state_
= COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
;
1104 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1105 switch (output_surface_state_
) {
1106 case OUTPUT_SURFACE_LOST
:
1107 case OUTPUT_SURFACE_CREATING
:
1110 case OUTPUT_SURFACE_ACTIVE
:
1111 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
:
1112 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
:
1119 std::string
SchedulerStateMachine::GetStatesForDebugging() const {
1120 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1121 needs_commit_
? 'T' : 'F',
1122 static_cast<int>(output_surface_state_
),
1123 static_cast<int>(begin_impl_frame_state_
),
1124 static_cast<int>(commit_state_
),
1125 has_pending_tree_
? 'T' : 'F',
1126 pending_tree_is_ready_for_activation_
? 'T' : 'F',
1127 active_tree_needs_first_draw_
? 'T' : 'F',