[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.cc
blobb86b5503f0091b87cfdfbd4c5af0a1b44a8e9c7e
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"
14 namespace cc {
16 SchedulerStateMachine::SchedulerStateMachine(const SchedulerSettings& settings)
17 : settings_(settings),
18 output_surface_state_(OUTPUT_SURFACE_LOST),
19 begin_impl_frame_state_(BEGIN_IMPL_FRAME_STATE_IDLE),
20 begin_main_frame_state_(BEGIN_MAIN_FRAME_STATE_IDLE),
21 forced_redraw_state_(FORCED_REDRAW_STATE_IDLE),
22 commit_count_(0),
23 current_frame_number_(0),
24 last_frame_number_animate_performed_(-1),
25 last_frame_number_swap_performed_(-1),
26 last_frame_number_swap_requested_(-1),
27 last_frame_number_begin_main_frame_sent_(-1),
28 last_frame_number_invalidate_output_surface_performed_(-1),
29 animate_funnel_(false),
30 request_swap_funnel_(false),
31 send_begin_main_frame_funnel_(true),
32 invalidate_output_surface_funnel_(false),
33 prepare_tiles_funnel_(0),
34 consecutive_checkerboard_animations_(0),
35 max_pending_swaps_(1),
36 pending_swaps_(0),
37 swaps_with_current_output_surface_(0),
38 needs_redraw_(false),
39 needs_animate_(false),
40 needs_prepare_tiles_(false),
41 needs_begin_main_frame_(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 main_thread_missed_last_deadline_(false),
51 skip_next_begin_main_frame_to_reduce_latency_(false),
52 children_need_begin_frames_(false),
53 defer_commits_(false),
54 video_needs_begin_frames_(false),
55 last_commit_had_no_updates_(false),
56 wait_for_ready_to_draw_(false),
57 did_request_swap_in_last_frame_(false),
58 did_perform_swap_in_last_draw_(false) {}
60 const char* SchedulerStateMachine::OutputSurfaceStateToString(
61 OutputSurfaceState state) {
62 switch (state) {
63 case OUTPUT_SURFACE_ACTIVE:
64 return "OUTPUT_SURFACE_ACTIVE";
65 case OUTPUT_SURFACE_LOST:
66 return "OUTPUT_SURFACE_LOST";
67 case OUTPUT_SURFACE_CREATING:
68 return "OUTPUT_SURFACE_CREATING";
69 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
70 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
71 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
72 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
74 NOTREACHED();
75 return "???";
78 const char* SchedulerStateMachine::BeginImplFrameStateToString(
79 BeginImplFrameState state) {
80 switch (state) {
81 case BEGIN_IMPL_FRAME_STATE_IDLE:
82 return "BEGIN_IMPL_FRAME_STATE_IDLE";
83 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING:
84 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
85 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME:
86 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
87 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE:
88 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
90 NOTREACHED();
91 return "???";
94 const char* SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
95 BeginImplFrameDeadlineMode mode) {
96 switch (mode) {
97 case BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE:
98 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE";
99 case BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE:
100 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE";
101 case BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR:
102 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR";
103 case BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE:
104 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE";
105 case BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW:
106 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW";
108 NOTREACHED();
109 return "???";
112 const char* SchedulerStateMachine::BeginMainFrameStateToString(
113 BeginMainFrameState state) {
114 switch (state) {
115 case BEGIN_MAIN_FRAME_STATE_IDLE:
116 return "BEGIN_MAIN_FRAME_STATE_IDLE";
117 case BEGIN_MAIN_FRAME_STATE_SENT:
118 return "BEGIN_MAIN_FRAME_STATE_SENT";
119 case BEGIN_MAIN_FRAME_STATE_STARTED:
120 return "BEGIN_MAIN_FRAME_STATE_STARTED";
121 case BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT:
122 return "BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT";
123 case BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION:
124 return "BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION";
125 case BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW:
126 return "BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW";
128 NOTREACHED();
129 return "???";
132 const char* SchedulerStateMachine::ForcedRedrawOnTimeoutStateToString(
133 ForcedRedrawOnTimeoutState state) {
134 switch (state) {
135 case FORCED_REDRAW_STATE_IDLE:
136 return "FORCED_REDRAW_STATE_IDLE";
137 case FORCED_REDRAW_STATE_WAITING_FOR_COMMIT:
138 return "FORCED_REDRAW_STATE_WAITING_FOR_COMMIT";
139 case FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION:
140 return "FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION";
141 case FORCED_REDRAW_STATE_WAITING_FOR_DRAW:
142 return "FORCED_REDRAW_STATE_WAITING_FOR_DRAW";
144 NOTREACHED();
145 return "???";
148 const char* SchedulerStateMachine::ActionToString(Action action) {
149 switch (action) {
150 case ACTION_NONE:
151 return "ACTION_NONE";
152 case ACTION_ANIMATE:
153 return "ACTION_ANIMATE";
154 case ACTION_SEND_BEGIN_MAIN_FRAME:
155 return "ACTION_SEND_BEGIN_MAIN_FRAME";
156 case ACTION_COMMIT:
157 return "ACTION_COMMIT";
158 case ACTION_ACTIVATE_SYNC_TREE:
159 return "ACTION_ACTIVATE_SYNC_TREE";
160 case ACTION_DRAW_AND_SWAP_IF_POSSIBLE:
161 return "ACTION_DRAW_AND_SWAP_IF_POSSIBLE";
162 case ACTION_DRAW_AND_SWAP_FORCED:
163 return "ACTION_DRAW_AND_SWAP_FORCED";
164 case ACTION_DRAW_AND_SWAP_ABORT:
165 return "ACTION_DRAW_AND_SWAP_ABORT";
166 case ACTION_BEGIN_OUTPUT_SURFACE_CREATION:
167 return "ACTION_BEGIN_OUTPUT_SURFACE_CREATION";
168 case ACTION_PREPARE_TILES:
169 return "ACTION_PREPARE_TILES";
170 case ACTION_INVALIDATE_OUTPUT_SURFACE:
171 return "ACTION_INVALIDATE_OUTPUT_SURFACE";
173 NOTREACHED();
174 return "???";
177 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
178 SchedulerStateMachine::AsValue() const {
179 scoped_refptr<base::trace_event::TracedValue> state =
180 new base::trace_event::TracedValue();
181 AsValueInto(state.get());
182 return state;
185 void SchedulerStateMachine::AsValueInto(
186 base::trace_event::TracedValue* state) const {
187 state->BeginDictionary("major_state");
188 state->SetString("next_action", ActionToString(NextAction()));
189 state->SetString("begin_impl_frame_state",
190 BeginImplFrameStateToString(begin_impl_frame_state_));
191 state->SetString("begin_main_frame_state",
192 BeginMainFrameStateToString(begin_main_frame_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->SetInteger("swaps_with_current_output_surface",
222 swaps_with_current_output_surface_);
223 state->SetBoolean("needs_redraw", needs_redraw_);
224 state->SetBoolean("needs_animate_", needs_animate_);
225 state->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_);
226 state->SetBoolean("needs_begin_main_frame", needs_begin_main_frame_);
227 state->SetBoolean("visible", visible_);
228 state->SetBoolean("can_start", can_start_);
229 state->SetBoolean("can_draw", can_draw_);
230 state->SetBoolean("has_pending_tree", has_pending_tree_);
231 state->SetBoolean("pending_tree_is_ready_for_activation",
232 pending_tree_is_ready_for_activation_);
233 state->SetBoolean("active_tree_needs_first_draw",
234 active_tree_needs_first_draw_);
235 state->SetBoolean("wait_for_ready_to_draw", wait_for_ready_to_draw_);
236 state->SetBoolean("did_create_and_initialize_first_output_surface",
237 did_create_and_initialize_first_output_surface_);
238 state->SetBoolean("impl_latency_takes_priority",
239 impl_latency_takes_priority_);
240 state->SetBoolean("main_thread_missed_last_deadline",
241 main_thread_missed_last_deadline_);
242 state->SetBoolean("skip_next_begin_main_frame_to_reduce_latency",
243 skip_next_begin_main_frame_to_reduce_latency_);
244 state->SetBoolean("children_need_begin_frames", children_need_begin_frames_);
245 state->SetBoolean("video_needs_begin_frames", video_needs_begin_frames_);
246 state->SetBoolean("defer_commits", defer_commits_);
247 state->SetBoolean("last_commit_had_no_updates", last_commit_had_no_updates_);
248 state->SetBoolean("did_request_swap_in_last_frame",
249 did_request_swap_in_last_frame_);
250 state->SetBoolean("did_perform_swap_in_last_draw",
251 did_perform_swap_in_last_draw_);
252 state->EndDictionary();
255 bool SchedulerStateMachine::PendingDrawsShouldBeAborted() const {
256 // These are all the cases where we normally cannot or do not want to draw
257 // but, if needs_redraw_ is true and we do not draw to make forward progress,
258 // we might deadlock with the main thread.
259 // This should be a superset of PendingActivationsShouldBeForced() since
260 // activation of the pending tree is blocked by drawing of the active tree and
261 // the main thread might be blocked on activation of the most recent commit.
262 if (PendingActivationsShouldBeForced())
263 return true;
265 // Additional states where we should abort draws.
266 if (!can_draw_)
267 return true;
268 return false;
271 bool SchedulerStateMachine::PendingActivationsShouldBeForced() const {
272 // There is no output surface to trigger our activations.
273 // If we do not force activations to make forward progress, we might deadlock
274 // with the main thread.
275 if (output_surface_state_ == OUTPUT_SURFACE_LOST)
276 return true;
278 // If we're not visible, we should force activation.
279 // Since we set RequiresHighResToDraw when becoming visible, we ensure that we
280 // don't checkerboard until all visible resources are done. Furthermore, if we
281 // do keep the pending tree around, when becoming visible we might activate
282 // prematurely causing RequiresHighResToDraw flag to be reset. In all cases,
283 // we can simply activate on becoming invisible since we don't need to draw
284 // the active tree when we're in this state.
285 if (!visible_)
286 return true;
288 return false;
291 bool SchedulerStateMachine::ShouldBeginOutputSurfaceCreation() const {
292 if (!visible_)
293 return false;
295 // Don't try to initialize too early.
296 if (!can_start_)
297 return false;
299 // We only want to start output surface initialization after the
300 // previous commit is complete.
301 if (begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_IDLE)
302 return false;
304 // Make sure the BeginImplFrame from any previous OutputSurfaces
305 // are complete before creating the new OutputSurface.
306 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_IDLE)
307 return false;
309 // We want to clear the pipeline of any pending draws and activations
310 // before starting output surface initialization. This allows us to avoid
311 // weird corner cases where we abort draws or force activation while we
312 // are initializing the output surface.
313 if (active_tree_needs_first_draw_ || has_pending_tree_)
314 return false;
316 // We need to create the output surface if we don't have one and we haven't
317 // started creating one yet.
318 return output_surface_state_ == OUTPUT_SURFACE_LOST;
321 bool SchedulerStateMachine::ShouldDraw() const {
322 // If we need to abort draws, we should do so ASAP since the draw could
323 // be blocking other important actions (like output surface initialization),
324 // from occurring. If we are waiting for the first draw, then perform the
325 // aborted draw to keep things moving. If we are not waiting for the first
326 // draw however, we don't want to abort for no reason.
327 if (PendingDrawsShouldBeAborted())
328 return active_tree_needs_first_draw_;
330 // Do not draw too many times in a single frame. It's okay that we don't check
331 // this before checking for aborted draws because aborted draws do not request
332 // a swap.
333 if (request_swap_funnel_)
334 return false;
336 // Don't draw if we are waiting on the first commit after a surface.
337 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE)
338 return false;
340 // Do not queue too many swaps.
341 if (SwapThrottled())
342 return false;
344 // Except for the cases above, do not draw outside of the BeginImplFrame
345 // deadline.
346 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
347 return false;
349 // Only handle forced redraws due to timeouts on the regular deadline.
350 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
351 return true;
353 return needs_redraw_;
356 bool SchedulerStateMachine::ShouldActivatePendingTree() const {
357 // There is nothing to activate.
358 if (!has_pending_tree_)
359 return false;
361 // We should not activate a second tree before drawing the first one.
362 // Even if we need to force activation of the pending tree, we should abort
363 // drawing the active tree first.
364 if (active_tree_needs_first_draw_)
365 return false;
367 // If we want to force activation, do so ASAP.
368 if (PendingActivationsShouldBeForced())
369 return true;
371 // At this point, only activate if we are ready to activate.
372 return pending_tree_is_ready_for_activation_;
375 bool SchedulerStateMachine::ShouldAnimate() const {
376 // Do not animate too many times in a single frame.
377 if (animate_funnel_)
378 return false;
380 // Don't animate if we are waiting on the first commit after a surface.
381 if (output_surface_state_ != OUTPUT_SURFACE_ACTIVE)
382 return false;
384 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING &&
385 begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
386 return false;
388 return needs_redraw_ || needs_animate_;
391 bool SchedulerStateMachine::CouldSendBeginMainFrame() const {
392 if (!needs_begin_main_frame_)
393 return false;
395 // We can not perform commits if we are not visible.
396 if (!visible_)
397 return false;
399 // Do not make a new commits when it is deferred.
400 if (defer_commits_)
401 return false;
403 return true;
406 bool SchedulerStateMachine::SendingBeginMainFrameMightCauseDeadlock() const {
407 // NPAPI is the only case where the UI thread makes synchronous calls to the
408 // Renderer main thread. During that synchronous call, we may not get a
409 // SwapAck for the UI thread, which may prevent BeginMainFrame's from
410 // completing if there's enough back pressure. If the BeginMainFrame can't
411 // make progress, the Renderer can't service the UI thread's synchronous call
412 // and we have deadlock.
413 // This returns true if there's too much backpressure to finish a commit
414 // if we were to initiate a BeginMainFrame.
415 return has_pending_tree_ && active_tree_needs_first_draw_ && SwapThrottled();
418 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
419 if (!CouldSendBeginMainFrame())
420 return false;
422 // Do not send begin main frame too many times in a single frame or before
423 // the first BeginFrame.
424 if (send_begin_main_frame_funnel_)
425 return false;
427 // Only send BeginMainFrame when there isn't another commit pending already.
428 // Other parts of the state machine indirectly defer the BeginMainFrame
429 // by transitioning to WAITING commit states rather than going
430 // immediately to IDLE.
431 if (begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_IDLE)
432 return false;
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_)) {
438 return false;
441 // We should not send BeginMainFrame while we are in the idle state since we
442 // might have new user input arriving soon. It's okay to send BeginMainFrame
443 // for the synchronous compositor because the main thread is always high
444 // latency in that case.
445 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
446 // thread isn't consuming user input for non-synchronous compositor.
447 if (!settings_.using_synchronous_renderer_compositor &&
448 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_IDLE) {
449 return false;
452 // We need a new commit for the forced redraw. This honors the
453 // single commit per interval because the result will be swapped to screen.
454 // TODO(brianderson): Remove this or move it below the
455 // SendingBeginMainFrameMightCauseDeadlock check since we want to avoid
456 // ever returning true from this method if we might cause deadlock.
457 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT)
458 return true;
460 // We shouldn't normally accept commits if there isn't an OutputSurface.
461 if (!HasInitializedOutputSurface())
462 return false;
464 // Make sure the BeginMainFrame can finish eventually if we start it.
465 if (SendingBeginMainFrameMightCauseDeadlock())
466 return false;
468 if (!settings_.main_frame_while_swap_throttled_enabled) {
469 // SwapAck throttle the BeginMainFrames unless we just swapped to
470 // potentially improve impl-thread latency over main-thread throughput.
471 // TODO(brianderson): Remove this restriction to improve throughput or
472 // make it conditional on impl_latency_takes_priority_.
473 bool just_swapped_in_deadline =
474 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE &&
475 did_perform_swap_in_last_draw_;
476 if (SwapThrottled() && !just_swapped_in_deadline)
477 return false;
480 if (skip_next_begin_main_frame_to_reduce_latency_)
481 return false;
483 return true;
486 bool SchedulerStateMachine::ShouldCommit() const {
487 if (begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT)
488 return false;
490 // We must not finish the commit until the pending tree is free.
491 if (has_pending_tree_) {
492 DCHECK(settings_.main_frame_before_activation_enabled);
493 return false;
496 // If we only have an active tree, it is incorrect to replace it
497 // before we've drawn it.
498 DCHECK_IMPLIES(settings_.commit_to_active_tree,
499 !active_tree_needs_first_draw_);
501 return true;
504 bool SchedulerStateMachine::ShouldPrepareTiles() const {
505 // PrepareTiles only really needs to be called immediately after commit
506 // and then periodically after that. Use a funnel to make sure we average
507 // one PrepareTiles per BeginImplFrame in the long run.
508 if (prepare_tiles_funnel_ > 0)
509 return false;
511 // Limiting to once per-frame is not enough, since we only want to
512 // prepare tiles _after_ draws.
513 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
514 return false;
516 return needs_prepare_tiles_;
519 bool SchedulerStateMachine::ShouldInvalidateOutputSurface() const {
520 // Do not invalidate too many times in a frame.
521 if (invalidate_output_surface_funnel_)
522 return false;
524 // Only the synchronous compositor requires invalidations.
525 if (!settings_.using_synchronous_renderer_compositor)
526 return false;
528 // Invalidations are only performed inside a BeginFrame.
529 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING)
530 return false;
532 // TODO(sunnyps): needs_prepare_tiles_ is needed here because PrepareTiles is
533 // called only inside the deadline / draw phase. We could remove this if we
534 // allowed PrepareTiles to happen in OnBeginImplFrame.
535 return needs_redraw_ || needs_prepare_tiles_;
538 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const {
539 if (ShouldActivatePendingTree())
540 return ACTION_ACTIVATE_SYNC_TREE;
541 if (ShouldCommit())
542 return ACTION_COMMIT;
543 if (ShouldAnimate())
544 return ACTION_ANIMATE;
545 if (ShouldDraw()) {
546 if (PendingDrawsShouldBeAborted())
547 return ACTION_DRAW_AND_SWAP_ABORT;
548 else if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
549 return ACTION_DRAW_AND_SWAP_FORCED;
550 else
551 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE;
553 if (ShouldPrepareTiles())
554 return ACTION_PREPARE_TILES;
555 if (ShouldSendBeginMainFrame())
556 return ACTION_SEND_BEGIN_MAIN_FRAME;
557 if (ShouldInvalidateOutputSurface())
558 return ACTION_INVALIDATE_OUTPUT_SURFACE;
559 if (ShouldBeginOutputSurfaceCreation())
560 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION;
561 return ACTION_NONE;
564 void SchedulerStateMachine::WillAnimate() {
565 DCHECK(!animate_funnel_);
566 last_frame_number_animate_performed_ = current_frame_number_;
567 animate_funnel_ = true;
568 needs_animate_ = false;
569 // TODO(skyostil): Instead of assuming this, require the client to tell us.
570 SetNeedsRedraw();
573 void SchedulerStateMachine::WillSendBeginMainFrame() {
574 DCHECK(!has_pending_tree_ || settings_.main_frame_before_activation_enabled);
575 DCHECK(visible_);
576 DCHECK(!send_begin_main_frame_funnel_);
577 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_SENT;
578 needs_begin_main_frame_ = false;
579 send_begin_main_frame_funnel_ = true;
580 last_frame_number_begin_main_frame_sent_ = current_frame_number_;
583 void SchedulerStateMachine::WillCommit(bool commit_has_no_updates) {
584 commit_count_++;
586 // Animate after commit even if we've already animated.
587 if (!commit_has_no_updates)
588 animate_funnel_ = false;
590 if (commit_has_no_updates || settings_.main_frame_before_activation_enabled) {
591 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_IDLE;
592 } else {
593 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION;
596 // If the commit was aborted, then there is no pending tree.
597 has_pending_tree_ = !commit_has_no_updates;
599 wait_for_ready_to_draw_ =
600 !commit_has_no_updates && settings_.commit_to_active_tree;
602 // Update state related to forced draws.
603 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) {
604 forced_redraw_state_ = has_pending_tree_
605 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
606 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
609 // Update the output surface state.
610 DCHECK_NE(output_surface_state_, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION);
611 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT) {
612 if (has_pending_tree_) {
613 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION;
614 } else {
615 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
619 // Update state if there's no updates heading for the active tree, but we need
620 // to do a forced draw.
621 if (commit_has_no_updates &&
622 forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW) {
623 DCHECK(!has_pending_tree_);
624 needs_redraw_ = true;
625 active_tree_needs_first_draw_ = true;
628 // This post-commit work is common to both completed and aborted commits.
629 pending_tree_is_ready_for_activation_ = false;
631 last_commit_had_no_updates_ = commit_has_no_updates;
634 void SchedulerStateMachine::WillActivate() {
635 if (begin_main_frame_state_ ==
636 BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION) {
637 begin_main_frame_state_ = settings_.commit_to_active_tree
638 ? BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW
639 : BEGIN_MAIN_FRAME_STATE_IDLE;
642 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION)
643 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
645 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION)
646 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
648 has_pending_tree_ = false;
649 pending_tree_is_ready_for_activation_ = false;
650 active_tree_needs_first_draw_ = true;
651 needs_redraw_ = true;
654 void SchedulerStateMachine::WillDraw(bool did_request_swap) {
655 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
656 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
658 if (begin_main_frame_state_ == BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW)
659 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_IDLE;
661 needs_redraw_ = false;
662 active_tree_needs_first_draw_ = false;
664 if (did_request_swap) {
665 DCHECK(!request_swap_funnel_);
666 request_swap_funnel_ = true;
667 did_request_swap_in_last_frame_ = true;
668 last_frame_number_swap_requested_ = current_frame_number_;
672 void SchedulerStateMachine::WillPrepareTiles() {
673 needs_prepare_tiles_ = false;
676 void SchedulerStateMachine::WillBeginOutputSurfaceCreation() {
677 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST);
678 output_surface_state_ = OUTPUT_SURFACE_CREATING;
680 // The following DCHECKs make sure we are in the proper quiescent state.
681 // The pipeline should be flushed entirely before we start output
682 // surface creation to avoid complicated corner cases.
683 DCHECK_EQ(begin_main_frame_state_, BEGIN_MAIN_FRAME_STATE_IDLE);
684 DCHECK(!has_pending_tree_);
685 DCHECK(!active_tree_needs_first_draw_);
688 void SchedulerStateMachine::WillInvalidateOutputSurface() {
689 DCHECK(!invalidate_output_surface_funnel_);
690 invalidate_output_surface_funnel_ = true;
691 last_frame_number_invalidate_output_surface_performed_ =
692 current_frame_number_;
694 // The synchronous compositor makes no guarantees about a draw coming in after
695 // an invalidate so clear any flags that would cause the compositor's pipeline
696 // to stall.
697 active_tree_needs_first_draw_ = false; // blocks commit if true
700 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
701 TRACE_EVENT_INSTANT0("cc",
702 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
703 TRACE_EVENT_SCOPE_THREAD);
704 skip_next_begin_main_frame_to_reduce_latency_ = true;
707 bool SchedulerStateMachine::BeginFrameRequiredForChildren() const {
708 return children_need_begin_frames_;
711 bool SchedulerStateMachine::BeginFrameNeededForVideo() const {
712 return video_needs_begin_frames_;
715 bool SchedulerStateMachine::BeginFrameNeeded() const {
716 // We can't handle BeginFrames when output surface isn't initialized.
717 // TODO(brianderson): Support output surface creation inside a BeginFrame.
718 if (!HasInitializedOutputSurface())
719 return false;
721 // If we are not visible, we don't need BeginFrame messages.
722 if (!visible_)
723 return false;
725 return (BeginFrameRequiredForAction() || BeginFrameRequiredForChildren() ||
726 BeginFrameNeededForVideo() || ProactiveBeginFrameWanted());
729 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
730 bool children_need_begin_frames) {
731 children_need_begin_frames_ = children_need_begin_frames;
734 void SchedulerStateMachine::SetVideoNeedsBeginFrames(
735 bool video_needs_begin_frames) {
736 video_needs_begin_frames_ = video_needs_begin_frames;
739 void SchedulerStateMachine::SetDeferCommits(bool defer_commits) {
740 defer_commits_ = defer_commits;
743 // These are the cases where we require a BeginFrame message to make progress
744 // on requested actions.
745 bool SchedulerStateMachine::BeginFrameRequiredForAction() const {
746 // The forced draw respects our normal draw scheduling, so we need to
747 // request a BeginImplFrame for it.
748 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
749 return true;
751 return needs_animate_ || needs_redraw_ ||
752 (needs_begin_main_frame_ && !defer_commits_);
755 // These are cases where we are very likely want a BeginFrame message in the
756 // near future. Proactively requesting the BeginImplFrame helps hide the round
757 // trip latency of the SetNeedsBeginFrame request that has to go to the
758 // Browser.
759 // This includes things like drawing soon, but might not actually have a new
760 // frame to draw when we receive the next BeginImplFrame.
761 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
762 // Do not be proactive when invisible.
763 if (!visible_)
764 return false;
766 // We should proactively request a BeginImplFrame if a commit is pending
767 // because we will want to draw if the commit completes quickly. Do not
768 // request frames when commits are disabled, because the frame requests will
769 // not provide the needed commit (and will wake up the process when it could
770 // stay idle).
771 if ((begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_IDLE) &&
772 !defer_commits_)
773 return true;
775 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
776 // to draw the new active tree.
777 if (has_pending_tree_)
778 return true;
780 // Changing priorities may allow us to activate (given the new priorities),
781 // which may result in a new frame.
782 if (needs_prepare_tiles_)
783 return true;
785 // If we just sent a swap request, it's likely that we are going to produce
786 // another frame soon. This helps avoid negative glitches in our
787 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
788 // provider and get sampled at an inopportune time, delaying the next
789 // BeginImplFrame.
790 if (did_request_swap_in_last_frame_)
791 return true;
793 // If the last commit was aborted because of early out (no updates), we should
794 // still want a begin frame in case there is a commit coming again.
795 if (last_commit_had_no_updates_)
796 return true;
798 return false;
801 void SchedulerStateMachine::OnBeginImplFrame() {
802 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING;
803 current_frame_number_++;
805 last_commit_had_no_updates_ = false;
806 did_request_swap_in_last_frame_ = false;
808 // Clear funnels for any actions we perform during the frame.
809 animate_funnel_ = false;
810 send_begin_main_frame_funnel_ = false;
811 invalidate_output_surface_funnel_ = false;
813 // "Drain" the PrepareTiles funnel.
814 if (prepare_tiles_funnel_ > 0)
815 prepare_tiles_funnel_--;
818 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
819 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME;
822 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
823 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE;
825 did_perform_swap_in_last_draw_ = false;
827 // Clear funnels for any actions we perform during the deadline.
828 request_swap_funnel_ = false;
830 // Allow one PrepareTiles per draw for synchronous compositor.
831 if (settings_.using_synchronous_renderer_compositor) {
832 if (prepare_tiles_funnel_ > 0)
833 prepare_tiles_funnel_--;
837 void SchedulerStateMachine::OnBeginImplFrameIdle() {
838 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_IDLE;
840 skip_next_begin_main_frame_to_reduce_latency_ = false;
842 // If a new or undrawn active tree is pending after the deadline,
843 // then the main thread is in a high latency mode.
844 main_thread_missed_last_deadline_ =
845 CommitPending() || has_pending_tree_ || active_tree_needs_first_draw_;
847 // If we're entering a state where we won't get BeginFrames set all the
848 // funnels so that we don't perform any actions that we shouldn't.
849 if (!BeginFrameNeeded())
850 send_begin_main_frame_funnel_ = true;
853 SchedulerStateMachine::BeginImplFrameDeadlineMode
854 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
855 if (settings_.using_synchronous_renderer_compositor) {
856 // No deadline for synchronous compositor.
857 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE;
858 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
859 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE;
860 } else if (wait_for_ready_to_draw_) {
861 // When we are waiting for ready to draw signal, we do not wait to post a
862 // deadline yet.
863 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW;
864 } else if (needs_redraw_ && !SwapThrottled()) {
865 // We have an animation or fast input path on the impl thread that wants
866 // to draw, so don't wait too long for a new active tree.
867 // If we are swap throttled we should wait until we are unblocked.
868 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR;
869 } else {
870 // The impl thread doesn't have anything it wants to draw and we are just
871 // waiting for a new active tree or we are swap throttled. In short we are
872 // blocked.
873 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE;
877 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
878 const {
879 // If we just forced activation, we should end the deadline right now.
880 if (PendingActivationsShouldBeForced() && !has_pending_tree_)
881 return true;
883 // Do not trigger deadline immediately if we're waiting for READY_TO_DRAW
884 // unless it's one of the forced cases.
885 if (wait_for_ready_to_draw_)
886 return false;
888 // SwapAck throttle the deadline since we wont draw and swap anyway.
889 if (SwapThrottled())
890 return false;
892 if (active_tree_needs_first_draw_)
893 return true;
895 if (!needs_redraw_)
896 return false;
898 // This is used to prioritize impl-thread draws when the main thread isn't
899 // producing anything, e.g., after an aborted commit. We also check that we
900 // don't have a pending tree -- otherwise we should give it a chance to
901 // activate.
902 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
903 if (begin_main_frame_state_ == BEGIN_MAIN_FRAME_STATE_IDLE &&
904 !has_pending_tree_)
905 return true;
907 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
908 if (impl_latency_takes_priority_)
909 return true;
911 return false;
914 bool SchedulerStateMachine::main_thread_missed_last_deadline() const {
915 return main_thread_missed_last_deadline_;
918 bool SchedulerStateMachine::SwapThrottled() const {
919 return pending_swaps_ >= max_pending_swaps_;
922 void SchedulerStateMachine::SetVisible(bool visible) {
923 if (visible_ == visible)
924 return;
926 visible_ = visible;
928 if (visible)
929 main_thread_missed_last_deadline_ = false;
931 // TODO(sunnyps): Change the funnel to a bool to avoid hacks like this.
932 prepare_tiles_funnel_ = 0;
933 wait_for_ready_to_draw_ = false;
936 void SchedulerStateMachine::SetCanDraw(bool can_draw) { can_draw_ = can_draw; }
938 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; }
940 void SchedulerStateMachine::SetNeedsAnimate() {
941 needs_animate_ = true;
944 bool SchedulerStateMachine::OnlyImplSideUpdatesExpected() const {
945 bool has_impl_updates = needs_redraw_ || needs_animate_;
946 bool main_updates_expected =
947 needs_begin_main_frame_ ||
948 begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_IDLE ||
949 has_pending_tree_;
950 return has_impl_updates && !main_updates_expected;
953 void SchedulerStateMachine::SetNeedsPrepareTiles() {
954 if (!needs_prepare_tiles_) {
955 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
956 needs_prepare_tiles_ = true;
960 void SchedulerStateMachine::SetMaxSwapsPending(int max) {
961 max_pending_swaps_ = max;
964 void SchedulerStateMachine::DidSwapBuffers() {
965 pending_swaps_++;
966 swaps_with_current_output_surface_++;
968 DCHECK_LE(pending_swaps_, max_pending_swaps_);
970 did_perform_swap_in_last_draw_ = true;
971 last_frame_number_swap_performed_ = current_frame_number_;
974 void SchedulerStateMachine::DidSwapBuffersComplete() {
975 pending_swaps_--;
978 void SchedulerStateMachine::SetImplLatencyTakesPriority(
979 bool impl_latency_takes_priority) {
980 impl_latency_takes_priority_ = impl_latency_takes_priority;
983 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result) {
984 switch (result) {
985 case INVALID_RESULT:
986 NOTREACHED() << "Uninitialized DrawResult.";
987 break;
988 case DRAW_ABORTED_CANT_DRAW:
989 case DRAW_ABORTED_CONTEXT_LOST:
990 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
991 << result;
992 break;
993 case DRAW_SUCCESS:
994 consecutive_checkerboard_animations_ = 0;
995 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
996 break;
997 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS:
998 needs_redraw_ = true;
1000 // If we're already in the middle of a redraw, we don't need to
1001 // restart it.
1002 if (forced_redraw_state_ != FORCED_REDRAW_STATE_IDLE)
1003 return;
1005 needs_begin_main_frame_ = true;
1006 consecutive_checkerboard_animations_++;
1007 if (settings_.timeout_and_draw_when_animation_checkerboards &&
1008 consecutive_checkerboard_animations_ >=
1009 settings_.maximum_number_of_failed_draws_before_draw_is_forced) {
1010 consecutive_checkerboard_animations_ = 0;
1011 // We need to force a draw, but it doesn't make sense to do this until
1012 // we've committed and have new textures.
1013 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_COMMIT;
1015 break;
1016 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT:
1017 // It's not clear whether this missing content is because of missing
1018 // pictures (which requires a commit) or because of memory pressure
1019 // removing textures (which might not). To be safe, request a commit
1020 // anyway.
1021 needs_begin_main_frame_ = true;
1022 break;
1026 void SchedulerStateMachine::SetNeedsBeginMainFrame() {
1027 needs_begin_main_frame_ = true;
1030 void SchedulerStateMachine::NotifyReadyToCommit() {
1031 DCHECK(begin_main_frame_state_ == BEGIN_MAIN_FRAME_STATE_STARTED)
1032 << AsValue()->ToString();
1033 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT;
1034 // In commit_to_active_tree mode, commit should happen right after
1035 // BeginFrame, meaning when this function is called, next action should be
1036 // commit.
1037 if (settings_.commit_to_active_tree)
1038 DCHECK(ShouldCommit());
1041 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason) {
1042 DCHECK_EQ(begin_main_frame_state_, BEGIN_MAIN_FRAME_STATE_SENT);
1043 switch (reason) {
1044 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST:
1045 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE:
1046 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT:
1047 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_IDLE;
1048 SetNeedsBeginMainFrame();
1049 return;
1050 case CommitEarlyOutReason::FINISHED_NO_UPDATES:
1051 bool commit_has_no_updates = true;
1052 WillCommit(commit_has_no_updates);
1053 return;
1057 void SchedulerStateMachine::DidPrepareTiles() {
1058 needs_prepare_tiles_ = false;
1059 // "Fill" the PrepareTiles funnel.
1060 prepare_tiles_funnel_++;
1063 void SchedulerStateMachine::DidLoseOutputSurface() {
1064 if (output_surface_state_ == OUTPUT_SURFACE_LOST ||
1065 output_surface_state_ == OUTPUT_SURFACE_CREATING)
1066 return;
1067 output_surface_state_ = OUTPUT_SURFACE_LOST;
1068 needs_redraw_ = false;
1069 wait_for_ready_to_draw_ = false;
1072 void SchedulerStateMachine::NotifyReadyToActivate() {
1073 if (has_pending_tree_)
1074 pending_tree_is_ready_for_activation_ = true;
1077 void SchedulerStateMachine::NotifyReadyToDraw() {
1078 wait_for_ready_to_draw_ = false;
1081 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1082 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING);
1083 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT;
1085 if (did_create_and_initialize_first_output_surface_) {
1086 // TODO(boliu): See if we can remove this when impl-side painting is always
1087 // on. Does anything on the main thread need to update after recreate?
1088 needs_begin_main_frame_ = true;
1090 did_create_and_initialize_first_output_surface_ = true;
1091 pending_swaps_ = 0;
1092 swaps_with_current_output_surface_ = 0;
1093 main_thread_missed_last_deadline_ = false;
1096 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1097 DCHECK_EQ(begin_main_frame_state_, BEGIN_MAIN_FRAME_STATE_SENT);
1098 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_STARTED;
1101 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1102 switch (output_surface_state_) {
1103 case OUTPUT_SURFACE_LOST:
1104 case OUTPUT_SURFACE_CREATING:
1105 return false;
1107 case OUTPUT_SURFACE_ACTIVE:
1108 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
1109 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
1110 return true;
1112 NOTREACHED();
1113 return false;
1116 } // namespace cc