Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.cc
blob8a1af521fbe9657c46740d52f917e73b1caae2dc
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"
15 namespace cc {
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),
23 commit_count_(0),
24 current_frame_number_(0),
25 last_frame_number_animate_performed_(-1),
26 last_frame_number_swap_performed_(-1),
27 last_frame_number_swap_requested_(-1),
28 last_frame_number_begin_main_frame_sent_(-1),
29 last_frame_number_invalidate_output_surface_performed_(-1),
30 animate_funnel_(false),
31 request_swap_funnel_(false),
32 send_begin_main_frame_funnel_(false),
33 invalidate_output_surface_funnel_(false),
34 prepare_tiles_funnel_(0),
35 consecutive_checkerboard_animations_(0),
36 max_pending_swaps_(1),
37 pending_swaps_(0),
38 needs_redraw_(false),
39 needs_animate_(false),
40 needs_prepare_tiles_(false),
41 needs_commit_(false),
42 visible_(false),
43 can_start_(false),
44 can_draw_(false),
45 has_pending_tree_(false),
46 pending_tree_is_ready_for_activation_(false),
47 active_tree_needs_first_draw_(false),
48 did_create_and_initialize_first_output_surface_(false),
49 impl_latency_takes_priority_(false),
50 skip_next_begin_main_frame_to_reduce_latency_(false),
51 skip_begin_main_frame_to_reduce_latency_(false),
52 continuous_painting_(false),
53 children_need_begin_frames_(false),
54 defer_commits_(false),
55 video_needs_begin_frames_(false),
56 last_commit_had_no_updates_(false),
57 wait_for_active_tree_ready_to_draw_(false),
58 did_request_swap_in_last_frame_(false),
59 did_perform_swap_in_last_draw_(false) {
62 const char* SchedulerStateMachine::OutputSurfaceStateToString(
63 OutputSurfaceState state) {
64 switch (state) {
65 case OUTPUT_SURFACE_ACTIVE:
66 return "OUTPUT_SURFACE_ACTIVE";
67 case OUTPUT_SURFACE_LOST:
68 return "OUTPUT_SURFACE_LOST";
69 case OUTPUT_SURFACE_CREATING:
70 return "OUTPUT_SURFACE_CREATING";
71 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
72 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
73 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
74 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
76 NOTREACHED();
77 return "???";
80 const char* SchedulerStateMachine::BeginImplFrameStateToString(
81 BeginImplFrameState state) {
82 switch (state) {
83 case BEGIN_IMPL_FRAME_STATE_IDLE:
84 return "BEGIN_IMPL_FRAME_STATE_IDLE";
85 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING:
86 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
87 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME:
88 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
89 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE:
90 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
92 NOTREACHED();
93 return "???";
96 const char* SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
97 BeginImplFrameDeadlineMode mode) {
98 switch (mode) {
99 case BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE:
100 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE";
101 case BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE:
102 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE";
103 case BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR:
104 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR";
105 case BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE:
106 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE";
107 case BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW:
108 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW";
110 NOTREACHED();
111 return "???";
114 const char* SchedulerStateMachine::CommitStateToString(CommitState state) {
115 switch (state) {
116 case COMMIT_STATE_IDLE:
117 return "COMMIT_STATE_IDLE";
118 case COMMIT_STATE_BEGIN_MAIN_FRAME_SENT:
119 return "COMMIT_STATE_BEGIN_MAIN_FRAME_SENT";
120 case COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED:
121 return "COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED";
122 case COMMIT_STATE_READY_TO_COMMIT:
123 return "COMMIT_STATE_READY_TO_COMMIT";
124 case COMMIT_STATE_WAITING_FOR_ACTIVATION:
125 return "COMMIT_STATE_WAITING_FOR_ACTIVATION";
126 case COMMIT_STATE_WAITING_FOR_DRAW:
127 return "COMMIT_STATE_WAITING_FOR_DRAW";
129 NOTREACHED();
130 return "???";
133 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
134 ForcedRedrawOnTimeoutState state) {
135 switch (state) {
136 case FORCED_REDRAW_STATE_IDLE:
137 return "FORCED_REDRAW_STATE_IDLE";
138 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT:
139 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
140 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION:
141 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
142 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW:
143 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
145 NOTREACHED();
146 return "???";
149 const char* SchedulerStateMachine::ActionToString(Action action) {
150 switch (action) {
151 case ACTION_NONE:
152 return "ACTION_NONE";
153 case ACTION_ANIMATE:
154 return "ACTION_ANIMATE";
155 case ACTION_SEND_BEGIN_MAIN_FRAME:
156 return "ACTION_SEND_BEGIN_MAIN_FRAME";
157 case ACTION_COMMIT:
158 return "ACTION_COMMIT";
159 case ACTION_ACTIVATE_SYNC_TREE:
160 return "ACTION_ACTIVATE_SYNC_TREE";
161 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE:
162 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
163 case ACTION_DRAW_AND_SWAP_FORCED:
164 return "ACTION_DRAW_AND_SWAP_FORCED";
165 case ACTION_DRAW_AND_SWAP_ABORT:
166 return "ACTION_DRAW_AND_SWAP_ABORT";
167 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION:
168 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
169 case ACTION_PREPARE_TILES:
170 return "ACTION_PREPARE_TILES";
171 case ACTION_INVALIDATE_OUTPUT_SURFACE:
172 return "ACTION_INVALIDATE_OUTPUT_SURFACE";
174 NOTREACHED();
175 return "???";
178 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
179 SchedulerStateMachine::AsValue() const {
180 scoped_refptr<base::trace_event::TracedValue> state =
181 new base::trace_event::TracedValue();
182 AsValueInto(state.get());
183 return state;
186 void SchedulerStateMachine::AsValueInto(
187 base::trace_event::TracedValue* state) const {
188 state->BeginDictionary("major_state");
189 state->SetString("next_action", ActionToString(NextAction()));
190 state->SetString("begin_impl_frame_state",
191 BeginImplFrameStateToString(begin_impl_frame_state_));
192 state->SetString("commit_state", CommitStateToString(commit_state_));
193 state->SetString("output_surface_state_",
194 OutputSurfaceStateToString(output_surface_state_));
195 state->SetString("forced_redraw_state",
196 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_));
197 state->EndDictionary();
199 state->BeginDictionary("minor_state");
200 state->SetInteger("commit_count", commit_count_);
201 state->SetInteger("current_frame_number", current_frame_number_);
202 state->SetInteger("last_frame_number_animate_performed",
203 last_frame_number_animate_performed_);
204 state->SetInteger("last_frame_number_swap_performed",
205 last_frame_number_swap_performed_);
206 state->SetInteger("last_frame_number_swap_requested",
207 last_frame_number_swap_requested_);
208 state->SetInteger("last_frame_number_begin_main_frame_sent",
209 last_frame_number_begin_main_frame_sent_);
210 state->SetBoolean("funnel: animate_funnel", animate_funnel_);
211 state->SetBoolean("funnel: request_swap_funnel", request_swap_funnel_);
212 state->SetBoolean("funnel: send_begin_main_frame_funnel",
213 send_begin_main_frame_funnel_);
214 state->SetInteger("funnel: prepare_tiles_funnel", prepare_tiles_funnel_);
215 state->SetBoolean("funnel: invalidate_output_surface_funnel",
216 invalidate_output_surface_funnel_);
217 state->SetInteger("consecutive_checkerboard_animations",
218 consecutive_checkerboard_animations_);
219 state->SetInteger("max_pending_swaps_", max_pending_swaps_);
220 state->SetInteger("pending_swaps_", pending_swaps_);
221 state->SetBoolean("needs_redraw", needs_redraw_);
222 state->SetBoolean("needs_animate_", needs_animate_);
223 state->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_);
224 state->SetBoolean("needs_commit", needs_commit_);
225 state->SetBoolean("visible", visible_);
226 state->SetBoolean("can_start", can_start_);
227 state->SetBoolean("can_draw", can_draw_);
228 state->SetBoolean("has_pending_tree", has_pending_tree_);
229 state->SetBoolean("pending_tree_is_ready_for_activation",
230 pending_tree_is_ready_for_activation_);
231 state->SetBoolean("active_tree_needs_first_draw",
232 active_tree_needs_first_draw_);
233 state->SetBoolean("wait_for_active_tree_ready_to_draw",
234 wait_for_active_tree_ready_to_draw_);
235 state->SetBoolean("did_create_and_initialize_first_output_surface",
236 did_create_and_initialize_first_output_surface_);
237 state->SetBoolean("impl_latency_takes_priority",
238 impl_latency_takes_priority_);
239 state->SetBoolean("main_thread_is_in_high_latency_mode",
240 MainThreadIsInHighLatencyMode());
241 state->SetBoolean("skip_begin_main_frame_to_reduce_latency",
242 skip_begin_main_frame_to_reduce_latency_);
243 state->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
244 skip_next_begin_main_frame_to_reduce_latency_);
245 state->SetBoolean("continuous_painting", continuous_painting_);
246 state->SetBoolean("children_need_begin_frames", children_need_begin_frames_);
247 state->SetBoolean("video_needs_begin_frames", video_needs_begin_frames_);
248 state->SetBoolean("defer_commits", defer_commits_);
249 state->SetBoolean("last_commit_had_no_updates", last_commit_had_no_updates_);
250 state->SetBoolean("did_request_swap_in_last_frame",
251 did_request_swap_in_last_frame_);
252 state->SetBoolean("did_perform_swap_in_last_draw",
253 did_perform_swap_in_last_draw_);
254 state->EndDictionary();
257 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
258 // These are all the cases where we normally cannot or do not want to draw
259 // but, if needs_redraw_ is true and we do not draw to make forward progress,
260 // we might deadlock with the main thread.
261 // This should be a superset of PendingActivationsShouldBeForced() since
262 // activation of the pending tree is blocked by drawing of the active tree and
263 // the main thread might be blocked on activation of the most recent commit.
264 if (PendingActivationsShouldBeForced())
265 return true;
267 // Additional states where we should abort draws.
268 if (!can_draw_)
269 return true;
270 return false;
273 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
274 // There is no output surface to trigger our activations.
275 // If we do not force activations to make forward progress, we might deadlock
276 // with the main thread.
277 if (output_surface_state_ == OUTPUT_SURFACE_LOST)
278 return true;
280 // If we're not visible, we should force activation.
281 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
282 // don't checkerboard until all visible resources are done. Furthermore, if we
283 // do keep the pending tree around, when becoming visible we might activate
284 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
285 // we can simply activate on becoming invisible since we don't need to draw
286 // the active tree when we're in this state.
287 if (!visible_)
288 return true;
290 return false;
293 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
294 // Don't try to initialize too early.
295 if (!can_start_)
296 return false;
298 // We only want to start output surface initialization after the
299 // previous commit is complete.
300 if (commit_state_ != COMMIT_STATE_IDLE)
301 return false;
303 // Make sure the BeginImplFrame from any previous OutputSurfaces
304 // are complete before creating the new OutputSurface.
305 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_IDLE)
306 return false;
308 // We want to clear the pipeline of any pending draws and activations
309 // before starting output surface initialization. This allows us to avoid
310 // weird corner cases where we abort draws or force activation while we
311 // are initializing the output surface.
312 if (active_tree_needs_first_draw_ || has_pending_tree_)
313 return false;
315 // We need to create the output surface if we don't have one and we haven't
316 // started creating one yet.
317 return output_surface_state_ == OUTPUT_SURFACE_LOST;
320 bool SchedulerStateMachine::ShouldDraw() const {
321 // If we need to abort draws, we should do so ASAP since the draw could
322 // be blocking other important actions (like output surface initialization),
323 // from occurring. If we are waiting for the first draw, then perform the
324 // aborted draw to keep things moving. If we are not waiting for the first
325 // draw however, we don't want to abort for no reason.
326 if (PendingDrawsShouldBeAborted())
327 return active_tree_needs_first_draw_;
329 // Do not draw too many times in a single frame. It's okay that we don't check
330 // this before checking for aborted draws because aborted draws do not request
331 // a swap.
332 if (request_swap_funnel_)
333 return false;
335 // Don't draw if we are waiting on the first commit after a surface.
336 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE)
337 return false;
339 // Do not queue too many swaps.
340 if (pending_swaps_ >= max_pending_swaps_)
341 return false;
343 // Except for the cases above, do not draw outside of the BeginImplFrame
344 // deadline.
345 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
346 return false;
348 // Only handle forced redraws due to timeouts on the regular deadline.
349 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
350 return true;
352 return needs_redraw_;
355 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
356 // There is nothing to activate.
357 if (!has_pending_tree_)
358 return false;
360 // We should not activate a second tree before drawing the first one.
361 // Even if we need to force activation of the pending tree, we should abort
362 // drawing the active tree first.
363 if (active_tree_needs_first_draw_)
364 return false;
366 // If we want to force activation, do so ASAP.
367 if (PendingActivationsShouldBeForced())
368 return true;
370 // At this point, only activate if we are ready to activate.
371 return pending_tree_is_ready_for_activation_;
374 bool SchedulerStateMachine::ShouldAnimate() const {
375 // Do not animate too many times in a single frame.
376 if (animate_funnel_)
377 return false;
379 // Don't animate if we are waiting on the first commit after a surface.
380 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE)
381 return false;
383 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING &&
384 begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
385 return false;
387 return needs_redraw_ || needs_animate_;
390 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
391 if (!needs_commit_)
392 return false;
394 // We can not perform commits if we are not visible.
395 if (!visible_)
396 return false;
398 // Do not make a new commits when it is deferred.
399 if (defer_commits_)
400 return false;
402 return true;
405 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
406 if (!CouldSendBeginMainFrame())
407 return false;
409 // Do not send begin main frame too many times in a single frame.
410 if (send_begin_main_frame_funnel_)
411 return false;
413 // Only send BeginMainFrame when there isn't another commit pending already.
414 if (commit_state_ != COMMIT_STATE_IDLE)
415 return false;
417 // Don't send BeginMainFrame early if we are prioritizing the active tree
418 // because of impl_latency_takes_priority_.
419 if (impl_latency_takes_priority_ &&
420 (has_pending_tree_ || active_tree_needs_first_draw_)) {
421 return false;
424 // We should not send BeginMainFrame while we are in
425 // BEGIN_IMPL_FRAME_STATE_IDLE since we might have new
426 // user input arriving soon.
427 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
428 // thread isn't consuming user input.
429 if (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_IDLE &&
430 BeginFrameNeeded())
431 return false;
433 // We need a new commit for the forced redraw. This honors the
434 // single commit per interval because the result will be swapped to screen.
435 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT)
436 return true;
438 // We shouldn't normally accept commits if there isn't an OutputSurface.
439 if (!HasInitializedOutputSurface())
440 return false;
442 // SwapAck throttle the BeginMainFrames unless we just swapped.
443 // TODO(brianderson): Remove this restriction to improve throughput.
444 bool just_swapped_in_deadline =
445 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE &&
446 did_perform_swap_in_last_draw_;
447 if (pending_swaps_ >= max_pending_swaps_ && !just_swapped_in_deadline)
448 return false;
450 if (skip_begin_main_frame_to_reduce_latency_)
451 return false;
453 return true;
456 bool SchedulerStateMachine::ShouldCommit() const {
457 if (commit_state_ != COMMIT_STATE_READY_TO_COMMIT)
458 return false;
460 // We must not finish the commit until the pending tree is free.
461 if (has_pending_tree_) {
462 DCHECK(settings_.main_frame_before_activation_enabled);
463 return false;
466 // Prioritize drawing the previous commit before finishing the next commit.
467 if (active_tree_needs_first_draw_)
468 return false;
470 return true;
473 bool SchedulerStateMachine::ShouldPrepareTiles() const {
474 // PrepareTiles only really needs to be called immediately after commit
475 // and then periodically after that. Use a funnel to make sure we average
476 // one PrepareTiles per BeginImplFrame in the long run.
477 if (prepare_tiles_funnel_ > 0)
478 return false;
480 // Limiting to once per-frame is not enough, since we only want to
481 // prepare tiles _after_ draws.
482 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
483 return false;
485 return needs_prepare_tiles_;
488 bool SchedulerStateMachine::ShouldInvalidateOutputSurface() const {
489 // Do not invalidate too many times in a frame.
490 if (invalidate_output_surface_funnel_)
491 return false;
493 // Only the synchronous compositor requires invalidations.
494 if (!settings_.using_synchronous_renderer_compositor)
495 return false;
497 // Invalidations are only performed inside a BeginFrame.
498 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING)
499 return false;
501 // TODO(sunnyps): needs_prepare_tiles_ is needed here because PrepareTiles is
502 // called only inside the deadline / draw phase. We could remove this if we
503 // allowed PrepareTiles to happen in OnBeginImplFrame.
504 return needs_redraw_ || needs_prepare_tiles_;
507 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const {
508 if (ShouldActivatePendingTree())
509 return ACTION_ACTIVATE_SYNC_TREE;
510 if (ShouldCommit())
511 return ACTION_COMMIT;
512 if (ShouldAnimate())
513 return ACTION_ANIMATE;
514 if (ShouldDraw()) {
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;
519 else
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 (ShouldInvalidateOutputSurface())
527 return ACTION_INVALIDATE_OUTPUT_SURFACE;
528 if (ShouldBeginOutputSurfaceCreation())
529 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION;
530 return ACTION_NONE;
533 void SchedulerStateMachine::UpdateState(Action action) {
534 switch (action) {
535 case ACTION_NONE:
536 return;
538 case ACTION_ACTIVATE_SYNC_TREE:
539 UpdateStateOnActivation();
540 return;
542 case ACTION_ANIMATE:
543 UpdateStateOnAnimate();
544 return;
546 case ACTION_SEND_BEGIN_MAIN_FRAME:
547 UpdateStateOnSendBeginMainFrame();
548 return;
550 case ACTION_COMMIT: {
551 bool commit_has_no_updates = false;
552 UpdateStateOnCommit(commit_has_no_updates);
553 return;
556 case ACTION_DRAW_AND_SWAP_FORCED:
557 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE: {
558 bool did_request_swap = true;
559 UpdateStateOnDraw(did_request_swap);
560 return;
563 case ACTION_DRAW_AND_SWAP_ABORT: {
564 bool did_request_swap = false;
565 UpdateStateOnDraw(did_request_swap);
566 return;
569 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION:
570 UpdateStateOnBeginOutputSurfaceCreation();
571 return;
573 case ACTION_PREPARE_TILES:
574 UpdateStateOnPrepareTiles();
575 return;
577 case ACTION_INVALIDATE_OUTPUT_SURFACE:
578 UpdateStateOnInvalidateOutputSurface();
579 return;
583 void SchedulerStateMachine::UpdateStateOnAnimate() {
584 DCHECK(!animate_funnel_);
585 last_frame_number_animate_performed_ = current_frame_number_;
586 animate_funnel_ = true;
587 needs_animate_ = false;
588 // TODO(skyostil): Instead of assuming this, require the client to tell us.
589 SetNeedsRedraw();
592 void SchedulerStateMachine::UpdateStateOnSendBeginMainFrame() {
593 DCHECK(!has_pending_tree_ || settings_.main_frame_before_activation_enabled);
594 DCHECK(visible_);
595 DCHECK(!send_begin_main_frame_funnel_);
596 commit_state_ = COMMIT_STATE_BEGIN_MAIN_FRAME_SENT;
597 needs_commit_ = false;
598 send_begin_main_frame_funnel_ = true;
599 last_frame_number_begin_main_frame_sent_ = current_frame_number_;
602 void SchedulerStateMachine::UpdateStateOnCommit(bool commit_has_no_updates) {
603 commit_count_++;
605 // Animate after commit even if we've already animated.
606 if (!commit_has_no_updates)
607 animate_funnel_ = false;
609 if (commit_has_no_updates || settings_.main_frame_before_activation_enabled) {
610 commit_state_ = COMMIT_STATE_IDLE;
611 } else if (settings_.impl_side_painting) {
612 commit_state_ = COMMIT_STATE_WAITING_FOR_ACTIVATION;
613 } else {
614 commit_state_ = settings_.main_thread_should_always_be_low_latency
615 ? COMMIT_STATE_WAITING_FOR_DRAW
616 : COMMIT_STATE_IDLE;
619 // If we are impl-side-painting but the commit was aborted, then we behave
620 // mostly as if we are not impl-side-painting since there is no pending tree.
621 has_pending_tree_ = settings_.impl_side_painting && !commit_has_no_updates;
623 // Update state related to forced draws.
624 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) {
625 forced_redraw_state_ = has_pending_tree_
626 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
627 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
630 // Update the output surface state.
631 DCHECK_NE(output_surface_state_, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION);
632 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT) {
633 if (has_pending_tree_) {
634 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION;
635 } else {
636 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
637 needs_redraw_ = true;
641 // Update state if we have a new active tree to draw, or if the active tree
642 // was unchanged but we need to do a forced draw.
643 if (!has_pending_tree_ &&
644 (!commit_has_no_updates ||
645 forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)) {
646 needs_redraw_ = true;
647 active_tree_needs_first_draw_ = true;
650 // This post-commit work is common to both completed and aborted commits.
651 pending_tree_is_ready_for_activation_ = false;
653 if (continuous_painting_)
654 needs_commit_ = true;
655 last_commit_had_no_updates_ = commit_has_no_updates;
658 void SchedulerStateMachine::UpdateStateOnActivation() {
659 if (commit_state_ == COMMIT_STATE_WAITING_FOR_ACTIVATION) {
660 commit_state_ = settings_.main_thread_should_always_be_low_latency
661 ? COMMIT_STATE_WAITING_FOR_DRAW
662 : COMMIT_STATE_IDLE;
665 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION)
666 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
668 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION)
669 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
671 has_pending_tree_ = false;
672 pending_tree_is_ready_for_activation_ = false;
673 active_tree_needs_first_draw_ = true;
674 needs_redraw_ = true;
677 void SchedulerStateMachine::UpdateStateOnDraw(bool did_request_swap) {
678 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
679 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
681 if (commit_state_ == COMMIT_STATE_WAITING_FOR_DRAW)
682 commit_state_ = COMMIT_STATE_IDLE;
684 needs_redraw_ = false;
685 active_tree_needs_first_draw_ = false;
687 if (did_request_swap) {
688 DCHECK(!request_swap_funnel_);
689 request_swap_funnel_ = true;
690 did_request_swap_in_last_frame_ = true;
691 last_frame_number_swap_requested_ = current_frame_number_;
695 void SchedulerStateMachine::UpdateStateOnPrepareTiles() {
696 needs_prepare_tiles_ = false;
699 void SchedulerStateMachine::UpdateStateOnBeginOutputSurfaceCreation() {
700 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST);
701 output_surface_state_ = OUTPUT_SURFACE_CREATING;
703 // The following DCHECKs make sure we are in the proper quiescent state.
704 // The pipeline should be flushed entirely before we start output
705 // surface creation to avoid complicated corner cases.
706 DCHECK_EQ(commit_state_, COMMIT_STATE_IDLE);
707 DCHECK(!has_pending_tree_);
708 DCHECK(!active_tree_needs_first_draw_);
711 void SchedulerStateMachine::UpdateStateOnInvalidateOutputSurface() {
712 DCHECK(!invalidate_output_surface_funnel_);
713 invalidate_output_surface_funnel_ = true;
714 last_frame_number_invalidate_output_surface_performed_ =
715 current_frame_number_;
717 // The synchronous compositor makes no guarantees about a draw coming in after
718 // an invalidate so clear any flags that would cause the compositor's pipeline
719 // to stall.
720 active_tree_needs_first_draw_ = false; // blocks commit if true
723 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
724 TRACE_EVENT_INSTANT0("cc",
725 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
726 TRACE_EVENT_SCOPE_THREAD);
727 skip_next_begin_main_frame_to_reduce_latency_ = true;
730 bool SchedulerStateMachine::BeginFrameRequiredForChildren() const {
731 return children_need_begin_frames_;
733 bool SchedulerStateMachine::BeginFrameNeededForVideo() const {
734 return video_needs_begin_frames_;
737 bool SchedulerStateMachine::BeginFrameNeeded() const {
738 // We can't handle BeginFrames when output surface isn't initialized.
739 // TODO(brianderson): Support output surface creation inside a BeginFrame.
740 if (!HasInitializedOutputSurface())
741 return false;
743 // If we are not visible, we don't need BeginFrame messages.
744 if (!visible_)
745 return false;
747 return (BeginFrameRequiredForAction() || BeginFrameRequiredForChildren() ||
748 BeginFrameNeededForVideo() || ProactiveBeginFrameWanted());
751 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
752 bool children_need_begin_frames) {
753 children_need_begin_frames_ = children_need_begin_frames;
756 void SchedulerStateMachine::SetVideoNeedsBeginFrames(
757 bool video_needs_begin_frames) {
758 video_needs_begin_frames_ = video_needs_begin_frames;
761 void SchedulerStateMachine::SetDeferCommits(bool defer_commits) {
762 defer_commits_ = defer_commits;
765 // These are the cases where we require a BeginFrame message to make progress
766 // on requested actions.
767 bool SchedulerStateMachine::BeginFrameRequiredForAction() const {
768 // The forced draw respects our normal draw scheduling, so we need to
769 // request a BeginImplFrame for it.
770 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
771 return true;
773 return needs_animate_ || needs_redraw_ || (needs_commit_ && !defer_commits_);
776 // These are cases where we are very likely want a BeginFrame message in the
777 // near future. Proactively requesting the BeginImplFrame helps hide the round
778 // trip latency of the SetNeedsBeginFrame request that has to go to the
779 // Browser.
780 // This includes things like drawing soon, but might not actually have a new
781 // frame to draw when we receive the next BeginImplFrame.
782 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
783 // Do not be proactive when invisible.
784 if (!visible_)
785 return false;
787 // We should proactively request a BeginImplFrame if a commit is pending
788 // because we will want to draw if the commit completes quickly. Do not
789 // request frames when commits are disabled, because the frame requests will
790 // not provide the needed commit (and will wake up the process when it could
791 // stay idle).
792 if ((commit_state_ != COMMIT_STATE_IDLE) && !defer_commits_)
793 return true;
795 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
796 // to draw the new active tree.
797 if (has_pending_tree_)
798 return true;
800 // Changing priorities may allow us to activate (given the new priorities),
801 // which may result in a new frame.
802 if (needs_prepare_tiles_)
803 return true;
805 // If we just sent a swap request, it's likely that we are going to produce
806 // another frame soon. This helps avoid negative glitches in our
807 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
808 // provider and get sampled at an inopportune time, delaying the next
809 // BeginImplFrame.
810 if (did_request_swap_in_last_frame_)
811 return true;
813 // If the last commit was aborted because of early out (no updates), we should
814 // still want a begin frame in case there is a commit coming again.
815 if (last_commit_had_no_updates_)
816 return true;
818 return false;
821 void SchedulerStateMachine::OnBeginImplFrame() {
822 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING;
823 current_frame_number_++;
825 last_commit_had_no_updates_ = false;
826 did_request_swap_in_last_frame_ = false;
828 // Clear funnels for any actions we perform during the frame.
829 animate_funnel_ = false;
830 send_begin_main_frame_funnel_ = false;
831 invalidate_output_surface_funnel_ = false;
833 // "Drain" the PrepareTiles funnel.
834 if (prepare_tiles_funnel_ > 0)
835 prepare_tiles_funnel_--;
837 skip_begin_main_frame_to_reduce_latency_ =
838 skip_next_begin_main_frame_to_reduce_latency_;
839 skip_next_begin_main_frame_to_reduce_latency_ = false;
842 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
843 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME;
846 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
847 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE;
849 did_perform_swap_in_last_draw_ = false;
851 // Clear funnels for any actions we perform during the deadline.
852 request_swap_funnel_ = false;
855 void SchedulerStateMachine::OnBeginImplFrameIdle() {
856 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_IDLE;
859 SchedulerStateMachine::BeginImplFrameDeadlineMode
860 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
861 if (settings_.using_synchronous_renderer_compositor) {
862 // No deadline for synchronous compositor.
863 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE;
864 } else if (wait_for_active_tree_ready_to_draw_) {
865 // When we are waiting for ready to draw signal, we do not wait to post a
866 // deadline yet.
867 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW;
868 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
869 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE;
870 } else if (needs_redraw_ && pending_swaps_ < max_pending_swaps_) {
871 // We have an animation or fast input path on the impl thread that wants
872 // to draw, so don't wait too long for a new active tree.
873 // If we are swap throttled we should wait until we are unblocked.
874 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR;
875 } else {
876 // The impl thread doesn't have anything it wants to draw and we are just
877 // waiting for a new active tree or we are swap throttled. In short we are
878 // blocked.
879 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE;
883 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
884 const {
885 // TODO(brianderson): This should take into account multiple commit sources.
886 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME)
887 return false;
889 // If things are being aborted, end the current BeginImplFrame ASAP so we can
890 // unblock creating the next output surface.
891 if (PendingDrawsShouldBeAborted())
892 return true;
894 // SwapAck throttle the deadline since we wont draw and swap anyway.
895 if (pending_swaps_ >= max_pending_swaps_)
896 return false;
898 if (active_tree_needs_first_draw_)
899 return true;
901 if (!needs_redraw_)
902 return false;
904 // This is used to prioritize impl-thread draws when the main thread isn't
905 // producing anything, e.g., after an aborted commit. We also check that we
906 // don't have a pending tree -- otherwise we should give it a chance to
907 // activate.
908 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
909 if (commit_state_ == COMMIT_STATE_IDLE && !has_pending_tree_)
910 return true;
912 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
913 if (impl_latency_takes_priority_)
914 return true;
916 return false;
919 bool SchedulerStateMachine::MainThreadIsInHighLatencyMode() const {
920 // If a commit is pending before the previous commit has been drawn, we
921 // are definitely in a high latency mode.
922 if (CommitPending() && (active_tree_needs_first_draw_ || has_pending_tree_))
923 return true;
925 // If we just sent a BeginMainFrame and haven't hit the deadline yet, the main
926 // thread is in a low latency mode.
927 if (send_begin_main_frame_funnel_ &&
928 (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING ||
929 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME))
930 return false;
932 // If there's a commit in progress it must either be from the previous frame
933 // or it started after the impl thread's deadline. In either case the main
934 // thread is in high latency mode.
935 if (CommitPending())
936 return true;
938 // Similarly, if there's a pending tree the main thread is in high latency
939 // mode, because either
940 // it's from the previous frame
941 // or
942 // we're currently drawing the active tree and the pending tree will thus
943 // only be drawn in the next frame.
944 if (has_pending_tree_)
945 return true;
947 if (begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE) {
948 // Even if there's a new active tree to draw at the deadline or we've just
949 // swapped it, it may have been triggered by a previous BeginImplFrame, in
950 // which case the main thread is in a high latency mode.
951 return (active_tree_needs_first_draw_ || did_perform_swap_in_last_draw_) &&
952 !send_begin_main_frame_funnel_;
955 // If the active tree needs its first draw in any other state, we know the
956 // main thread is in a high latency mode.
957 return active_tree_needs_first_draw_;
960 void SchedulerStateMachine::SetVisible(bool visible) { visible_ = visible; }
962 void SchedulerStateMachine::SetCanDraw(bool can_draw) { can_draw_ = can_draw; }
964 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; }
966 void SchedulerStateMachine::SetNeedsAnimate() {
967 needs_animate_ = true;
970 void SchedulerStateMachine::SetWaitForReadyToDraw() {
971 DCHECK(settings_.impl_side_painting);
972 wait_for_active_tree_ready_to_draw_ = true;
975 void SchedulerStateMachine::SetNeedsPrepareTiles() {
976 if (!needs_prepare_tiles_) {
977 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
978 needs_prepare_tiles_ = true;
982 void SchedulerStateMachine::SetMaxSwapsPending(int max) {
983 max_pending_swaps_ = max;
986 void SchedulerStateMachine::DidSwapBuffers() {
987 pending_swaps_++;
988 DCHECK_LE(pending_swaps_, max_pending_swaps_);
990 did_perform_swap_in_last_draw_ = true;
991 last_frame_number_swap_performed_ = current_frame_number_;
994 void SchedulerStateMachine::DidSwapBuffersComplete() {
995 DCHECK_GT(pending_swaps_, 0);
996 pending_swaps_--;
999 void SchedulerStateMachine::SetImplLatencyTakesPriority(
1000 bool impl_latency_takes_priority) {
1001 impl_latency_takes_priority_ = impl_latency_takes_priority;
1004 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result) {
1005 switch (result) {
1006 case INVALID_RESULT:
1007 NOTREACHED() << "Uninitialized DrawResult.";
1008 break;
1009 case DRAW_ABORTED_CANT_DRAW:
1010 case DRAW_ABORTED_CONTEXT_LOST:
1011 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
1012 << result;
1013 break;
1014 case DRAW_SUCCESS:
1015 consecutive_checkerboard_animations_ = 0;
1016 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
1017 break;
1018 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS:
1019 needs_redraw_ = true;
1021 // If we're already in the middle of a redraw, we don't need to
1022 // restart it.
1023 if (forced_redraw_state_ != FORCED_REDRAW_STATE_IDLE)
1024 return;
1026 needs_commit_ = true;
1027 consecutive_checkerboard_animations_++;
1028 if (settings_.timeout_and_draw_when_animation_checkerboards &&
1029 consecutive_checkerboard_animations_ >=
1030 settings_.maximum_number_of_failed_draws_before_draw_is_forced_) {
1031 consecutive_checkerboard_animations_ = 0;
1032 // We need to force a draw, but it doesn't make sense to do this until
1033 // we've committed and have new textures.
1034 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_COMMIT;
1036 break;
1037 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT:
1038 // It's not clear whether this missing content is because of missing
1039 // pictures (which requires a commit) or because of memory pressure
1040 // removing textures (which might not). To be safe, request a commit
1041 // anyway.
1042 needs_commit_ = true;
1043 break;
1047 void SchedulerStateMachine::SetNeedsCommit() {
1048 needs_commit_ = true;
1051 void SchedulerStateMachine::NotifyReadyToCommit() {
1052 DCHECK(commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED)
1053 << AsValue()->ToString();
1054 commit_state_ = COMMIT_STATE_READY_TO_COMMIT;
1055 // In main thread low latency mode, commit should happen right after
1056 // BeginFrame, meaning when this function is called, next action should be
1057 // commit.
1058 if (settings_.main_thread_should_always_be_low_latency)
1059 DCHECK(ShouldCommit());
1062 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason) {
1063 DCHECK_EQ(commit_state_, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT);
1064 switch (reason) {
1065 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST:
1066 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE:
1067 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT:
1068 commit_state_ = COMMIT_STATE_IDLE;
1069 SetNeedsCommit();
1070 return;
1071 case CommitEarlyOutReason::FINISHED_NO_UPDATES:
1072 bool commit_has_no_updates = true;
1073 UpdateStateOnCommit(commit_has_no_updates);
1074 return;
1078 void SchedulerStateMachine::DidPrepareTiles() {
1079 needs_prepare_tiles_ = false;
1080 // "Fill" the PrepareTiles funnel.
1081 prepare_tiles_funnel_++;
1084 void SchedulerStateMachine::DidLoseOutputSurface() {
1085 if (output_surface_state_ == OUTPUT_SURFACE_LOST ||
1086 output_surface_state_ == OUTPUT_SURFACE_CREATING)
1087 return;
1088 output_surface_state_ = OUTPUT_SURFACE_LOST;
1089 needs_redraw_ = false;
1090 wait_for_active_tree_ready_to_draw_ = false;
1093 void SchedulerStateMachine::NotifyReadyToActivate() {
1094 if (has_pending_tree_)
1095 pending_tree_is_ready_for_activation_ = true;
1098 void SchedulerStateMachine::NotifyReadyToDraw() {
1099 wait_for_active_tree_ready_to_draw_ = false;
1102 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1103 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING);
1104 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT;
1106 if (did_create_and_initialize_first_output_surface_) {
1107 // TODO(boliu): See if we can remove this when impl-side painting is always
1108 // on. Does anything on the main thread need to update after recreate?
1109 needs_commit_ = true;
1111 did_create_and_initialize_first_output_surface_ = true;
1112 pending_swaps_ = 0;
1115 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1116 DCHECK_EQ(commit_state_, COMMIT_STATE_BEGIN_MAIN_FRAME_SENT);
1117 commit_state_ = COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED;
1120 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1121 switch (output_surface_state_) {
1122 case OUTPUT_SURFACE_LOST:
1123 case OUTPUT_SURFACE_CREATING:
1124 return false;
1126 case OUTPUT_SURFACE_ACTIVE:
1127 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
1128 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
1129 return true;
1131 NOTREACHED();
1132 return false;
1135 std::string SchedulerStateMachine::GetStatesForDebugging() const {
1136 return base::StringPrintf("%c %d %d %d %c %c %c %d %d",
1137 needs_commit_ ? 'T' : 'F',
1138 static_cast<int>(output_surface_state_),
1139 static_cast<int>(begin_impl_frame_state_),
1140 static_cast<int>(commit_state_),
1141 has_pending_tree_ ? 'T' : 'F',
1142 pending_tree_is_ready_for_activation_ ? 'T' : 'F',
1143 active_tree_needs_first_draw_ ? 'T' : 'F',
1144 max_pending_swaps_,
1145 pending_swaps_);
1148 } // namespace cc