[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.cc
blob7808b3f48b45f02272438eab944d169f0ba0a0aa
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 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_ready_to_draw_(false),
58 did_request_swap_in_last_frame_(false),
59 did_perform_swap_in_last_draw_(false) {}
61 const char* SchedulerStateMachine::OutputSurfaceStateToString(
62 OutputSurfaceState state) {
63 switch (state) {
64 case OUTPUT_SURFACE_ACTIVE:
65 return "OUTPUT_SURFACE_ACTIVE";
66 case OUTPUT_SURFACE_LOST:
67 return "OUTPUT_SURFACE_LOST";
68 case OUTPUT_SURFACE_CREATING:
69 return "OUTPUT_SURFACE_CREATING";
70 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
71 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT";
72 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
73 return "OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION";
75 NOTREACHED();
76 return "???";
79 const char* SchedulerStateMachine::BeginImplFrameStateToString(
80 BeginImplFrameState state) {
81 switch (state) {
82 case BEGIN_IMPL_FRAME_STATE_IDLE:
83 return "BEGIN_IMPL_FRAME_STATE_IDLE";
84 case BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING:
85 return "BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING";
86 case BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME:
87 return "BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME";
88 case BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE:
89 return "BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE";
91 NOTREACHED();
92 return "???";
95 const char* SchedulerStateMachine::BeginImplFrameDeadlineModeToString(
96 BeginImplFrameDeadlineMode mode) {
97 switch (mode) {
98 case BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE:
99 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE";
100 case BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE:
101 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE";
102 case BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR:
103 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR";
104 case BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE:
105 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE";
106 case BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW:
107 return "BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW";
109 NOTREACHED();
110 return "???";
113 const char* SchedulerStateMachine::BeginMainFrameStateToString(
114 BeginMainFrameState state) {
115 switch (state) {
116 case BEGIN_MAIN_FRAME_STATE_IDLE:
117 return "BEGIN_MAIN_FRAME_STATE_IDLE";
118 case BEGIN_MAIN_FRAME_STATE_SENT:
119 return "BEGIN_MAIN_FRAME_STATE_SENT";
120 case BEGIN_MAIN_FRAME_STATE_STARTED:
121 return "BEGIN_MAIN_FRAME_STATE_STARTED";
122 case BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT:
123 return "BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT";
124 case BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION:
125 return "BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION";
126 case BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW:
127 return "BEGIN_MAIN_FRAME_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("begin_main_frame_state",
193 BeginMainFrameStateToString(begin_main_frame_state_));
194 state->SetString("output_surface_state_",
195 OutputSurfaceStateToString(output_surface_state_));
196 state->SetString("forced_redraw_state",
197 ForcedRedrawOnTimeoutStateToString(forced_redraw_state_));
198 state->EndDictionary();
200 state->BeginDictionary("minor_state");
201 state->SetInteger("commit_count", commit_count_);
202 state->SetInteger("current_frame_number", current_frame_number_);
203 state->SetInteger("last_frame_number_animate_performed",
204 last_frame_number_animate_performed_);
205 state->SetInteger("last_frame_number_swap_performed",
206 last_frame_number_swap_performed_);
207 state->SetInteger("last_frame_number_swap_requested",
208 last_frame_number_swap_requested_);
209 state->SetInteger("last_frame_number_begin_main_frame_sent",
210 last_frame_number_begin_main_frame_sent_);
211 state->SetBoolean("funnel: animate_funnel", animate_funnel_);
212 state->SetBoolean("funnel: request_swap_funnel", request_swap_funnel_);
213 state->SetBoolean("funnel: send_begin_main_frame_funnel",
214 send_begin_main_frame_funnel_);
215 state->SetInteger("funnel: prepare_tiles_funnel", prepare_tiles_funnel_);
216 state->SetBoolean("funnel: invalidate_output_surface_funnel",
217 invalidate_output_surface_funnel_);
218 state->SetInteger("consecutive_checkerboard_animations",
219 consecutive_checkerboard_animations_);
220 state->SetInteger("max_pending_swaps_", max_pending_swaps_);
221 state->SetInteger("pending_swaps_", pending_swaps_);
222 state->SetInteger("swaps_with_current_output_surface",
223 swaps_with_current_output_surface_);
224 state->SetBoolean("needs_redraw", needs_redraw_);
225 state->SetBoolean("needs_animate_", needs_animate_);
226 state->SetBoolean("needs_prepare_tiles", needs_prepare_tiles_);
227 state->SetBoolean("needs_begin_main_frame", needs_begin_main_frame_);
228 state->SetBoolean("visible", visible_);
229 state->SetBoolean("can_start", can_start_);
230 state->SetBoolean("can_draw", can_draw_);
231 state->SetBoolean("has_pending_tree", has_pending_tree_);
232 state->SetBoolean("pending_tree_is_ready_for_activation",
233 pending_tree_is_ready_for_activation_);
234 state->SetBoolean("active_tree_needs_first_draw",
235 active_tree_needs_first_draw_);
236 state->SetBoolean("wait_for_ready_to_draw", wait_for_ready_to_draw_);
237 state->SetBoolean("did_create_and_initialize_first_output_surface",
238 did_create_and_initialize_first_output_surface_);
239 state->SetBoolean("impl_latency_takes_priority",
240 impl_latency_takes_priority_);
241 state->SetBoolean("main_thread_missed_last_deadline",
242 main_thread_missed_last_deadline_);
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 (begin_main_frame_state_ != BEGIN_MAIN_FRAME_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 (SwapThrottled())
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_begin_main_frame_)
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::SendingBeginMainFrameMightCauseDeadlock() const {
406 // NPAPI is the only case where the UI thread makes synchronous calls to the
407 // Renderer main thread. During that synchronous call, we may not get a
408 // SwapAck for the UI thread, which may prevent BeginMainFrame's from
409 // completing if there's enough back pressure. If the BeginMainFrame can't
410 // make progress, the Renderer can't service the UI thread's synchronous call
411 // and we have deadlock.
412 // This returns true if there's too much backpressure to finish a commit
413 // if we were to initiate a BeginMainFrame.
414 return has_pending_tree_ && active_tree_needs_first_draw_ && SwapThrottled();
417 bool SchedulerStateMachine::ShouldSendBeginMainFrame() const {
418 if (!CouldSendBeginMainFrame())
419 return false;
421 // Do not send begin main frame too many times in a single frame or before
422 // the first BeginFrame.
423 if (send_begin_main_frame_funnel_)
424 return false;
426 // Only send BeginMainFrame when there isn't another commit pending already.
427 // Other parts of the state machine indirectly defer the BeginMainFrame
428 // by transitioning to WAITING commit states rather than going
429 // immediately to IDLE.
430 if (begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_IDLE)
431 return false;
433 // Don't send BeginMainFrame early if we are prioritizing the active tree
434 // because of impl_latency_takes_priority_.
435 if (impl_latency_takes_priority_ &&
436 (has_pending_tree_ || active_tree_needs_first_draw_)) {
437 return false;
440 // We should not send BeginMainFrame while we are in the idle state since we
441 // might have new user input arriving soon. It's okay to send BeginMainFrame
442 // for the synchronous compositor because the main thread is always high
443 // latency in that case.
444 // TODO(brianderson): Allow sending BeginMainFrame while idle when the main
445 // thread isn't consuming user input for non-synchronous compositor.
446 if (!settings_.using_synchronous_renderer_compositor &&
447 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_IDLE) {
448 return false;
451 // We need a new commit for the forced redraw. This honors the
452 // single commit per interval because the result will be swapped to screen.
453 // TODO(brianderson): Remove this or move it below the
454 // SendingBeginMainFrameMightCauseDeadlock check since we want to avoid
455 // ever returning true from this method if we might cause deadlock.
456 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT)
457 return true;
459 // We shouldn't normally accept commits if there isn't an OutputSurface.
460 if (!HasInitializedOutputSurface())
461 return false;
463 // Make sure the BeginMainFrame can finish eventually if we start it.
464 if (SendingBeginMainFrameMightCauseDeadlock())
465 return false;
467 if (!settings_.main_frame_while_swap_throttled_enabled) {
468 // SwapAck throttle the BeginMainFrames unless we just swapped to
469 // potentially improve impl-thread latency over main-thread throughput.
470 // TODO(brianderson): Remove this restriction to improve throughput or
471 // make it conditional on impl_latency_takes_priority_.
472 bool just_swapped_in_deadline =
473 begin_impl_frame_state_ == BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE &&
474 did_perform_swap_in_last_draw_;
475 if (SwapThrottled() && !just_swapped_in_deadline)
476 return false;
479 if (skip_next_begin_main_frame_to_reduce_latency_)
480 return false;
482 return true;
485 bool SchedulerStateMachine::ShouldCommit() const {
486 if (begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT)
487 return false;
489 // We must not finish the commit until the pending tree is free.
490 if (has_pending_tree_) {
491 DCHECK(settings_.main_frame_before_activation_enabled);
492 return false;
495 // If we only have an active tree, it is incorrect to replace it
496 // before we've drawn it.
497 DCHECK_IMPLIES(settings_.commit_to_active_tree,
498 !active_tree_needs_first_draw_);
500 return true;
503 bool SchedulerStateMachine::ShouldPrepareTiles() const {
504 // PrepareTiles only really needs to be called immediately after commit
505 // and then periodically after that. Use a funnel to make sure we average
506 // one PrepareTiles per BeginImplFrame in the long run.
507 if (prepare_tiles_funnel_ > 0)
508 return false;
510 // Limiting to once per-frame is not enough, since we only want to
511 // prepare tiles _after_ draws.
512 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE)
513 return false;
515 return needs_prepare_tiles_;
518 bool SchedulerStateMachine::ShouldInvalidateOutputSurface() const {
519 // Do not invalidate too many times in a frame.
520 if (invalidate_output_surface_funnel_)
521 return false;
523 // Only the synchronous compositor requires invalidations.
524 if (!settings_.using_synchronous_renderer_compositor)
525 return false;
527 // Invalidations are only performed inside a BeginFrame.
528 if (begin_impl_frame_state_ != BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING)
529 return false;
531 // TODO(sunnyps): needs_prepare_tiles_ is needed here because PrepareTiles is
532 // called only inside the deadline / draw phase. We could remove this if we
533 // allowed PrepareTiles to happen in OnBeginImplFrame.
534 return needs_redraw_ || needs_prepare_tiles_;
537 SchedulerStateMachine::Action SchedulerStateMachine::NextAction() const {
538 if (ShouldActivatePendingTree())
539 return ACTION_ACTIVATE_SYNC_TREE;
540 if (ShouldCommit())
541 return ACTION_COMMIT;
542 if (ShouldAnimate())
543 return ACTION_ANIMATE;
544 if (ShouldDraw()) {
545 if (PendingDrawsShouldBeAborted())
546 return ACTION_DRAW_AND_SWAP_ABORT;
547 else if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
548 return ACTION_DRAW_AND_SWAP_FORCED;
549 else
550 return ACTION_DRAW_AND_SWAP_IF_POSSIBLE;
552 if (ShouldPrepareTiles())
553 return ACTION_PREPARE_TILES;
554 if (ShouldSendBeginMainFrame())
555 return ACTION_SEND_BEGIN_MAIN_FRAME;
556 if (ShouldInvalidateOutputSurface())
557 return ACTION_INVALIDATE_OUTPUT_SURFACE;
558 if (ShouldBeginOutputSurfaceCreation())
559 return ACTION_BEGIN_OUTPUT_SURFACE_CREATION;
560 return ACTION_NONE;
563 void SchedulerStateMachine::WillAnimate() {
564 DCHECK(!animate_funnel_);
565 last_frame_number_animate_performed_ = current_frame_number_;
566 animate_funnel_ = true;
567 needs_animate_ = false;
568 // TODO(skyostil): Instead of assuming this, require the client to tell us.
569 SetNeedsRedraw();
572 void SchedulerStateMachine::WillSendBeginMainFrame() {
573 DCHECK(!has_pending_tree_ || settings_.main_frame_before_activation_enabled);
574 DCHECK(visible_);
575 DCHECK(!send_begin_main_frame_funnel_);
576 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_SENT;
577 needs_begin_main_frame_ = false;
578 send_begin_main_frame_funnel_ = true;
579 last_frame_number_begin_main_frame_sent_ = current_frame_number_;
582 void SchedulerStateMachine::WillCommit(bool commit_has_no_updates) {
583 commit_count_++;
585 // Animate after commit even if we've already animated.
586 if (!commit_has_no_updates)
587 animate_funnel_ = false;
589 if (commit_has_no_updates || settings_.main_frame_before_activation_enabled) {
590 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_IDLE;
591 } else {
592 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION;
595 // If the commit was aborted, then there is no pending tree.
596 has_pending_tree_ = !commit_has_no_updates;
598 wait_for_ready_to_draw_ =
599 !commit_has_no_updates && settings_.commit_to_active_tree;
601 // Update state related to forced draws.
602 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_COMMIT) {
603 forced_redraw_state_ = has_pending_tree_
604 ? FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
605 : FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
608 // Update the output surface state.
609 DCHECK_NE(output_surface_state_, OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION);
610 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT) {
611 if (has_pending_tree_) {
612 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION;
613 } else {
614 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
618 // Update state if there's no updates heading for the active tree, but we need
619 // to do a forced draw.
620 if (commit_has_no_updates &&
621 forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW) {
622 DCHECK(!has_pending_tree_);
623 needs_redraw_ = true;
624 active_tree_needs_first_draw_ = true;
627 // This post-commit work is common to both completed and aborted commits.
628 pending_tree_is_ready_for_activation_ = false;
630 if (continuous_painting_)
631 needs_begin_main_frame_ = true;
632 last_commit_had_no_updates_ = commit_has_no_updates;
635 void SchedulerStateMachine::WillActivate() {
636 if (begin_main_frame_state_ ==
637 BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION) {
638 begin_main_frame_state_ = settings_.commit_to_active_tree
639 ? BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW
640 : BEGIN_MAIN_FRAME_STATE_IDLE;
643 if (output_surface_state_ == OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION)
644 output_surface_state_ = OUTPUT_SURFACE_ACTIVE;
646 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION)
647 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_DRAW;
649 has_pending_tree_ = false;
650 pending_tree_is_ready_for_activation_ = false;
651 active_tree_needs_first_draw_ = true;
652 needs_redraw_ = true;
655 void SchedulerStateMachine::WillDraw(bool did_request_swap) {
656 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
657 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
659 if (begin_main_frame_state_ == BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW)
660 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_IDLE;
662 needs_redraw_ = false;
663 active_tree_needs_first_draw_ = false;
665 if (did_request_swap) {
666 DCHECK(!request_swap_funnel_);
667 request_swap_funnel_ = true;
668 did_request_swap_in_last_frame_ = true;
669 last_frame_number_swap_requested_ = current_frame_number_;
673 void SchedulerStateMachine::WillPrepareTiles() {
674 needs_prepare_tiles_ = false;
677 void SchedulerStateMachine::WillBeginOutputSurfaceCreation() {
678 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_LOST);
679 output_surface_state_ = OUTPUT_SURFACE_CREATING;
681 // The following DCHECKs make sure we are in the proper quiescent state.
682 // The pipeline should be flushed entirely before we start output
683 // surface creation to avoid complicated corner cases.
684 DCHECK_EQ(begin_main_frame_state_, BEGIN_MAIN_FRAME_STATE_IDLE);
685 DCHECK(!has_pending_tree_);
686 DCHECK(!active_tree_needs_first_draw_);
689 void SchedulerStateMachine::WillInvalidateOutputSurface() {
690 DCHECK(!invalidate_output_surface_funnel_);
691 invalidate_output_surface_funnel_ = true;
692 last_frame_number_invalidate_output_surface_performed_ =
693 current_frame_number_;
695 // The synchronous compositor makes no guarantees about a draw coming in after
696 // an invalidate so clear any flags that would cause the compositor's pipeline
697 // to stall.
698 active_tree_needs_first_draw_ = false; // blocks commit if true
701 void SchedulerStateMachine::SetSkipNextBeginMainFrameToReduceLatency() {
702 TRACE_EVENT_INSTANT0("cc",
703 "Scheduler: SkipNextBeginMainFrameToReduceLatency",
704 TRACE_EVENT_SCOPE_THREAD);
705 skip_next_begin_main_frame_to_reduce_latency_ = true;
708 bool SchedulerStateMachine::BeginFrameRequiredForChildren() const {
709 return children_need_begin_frames_;
712 bool SchedulerStateMachine::BeginFrameNeededForVideo() const {
713 return video_needs_begin_frames_;
716 bool SchedulerStateMachine::BeginFrameNeeded() const {
717 // We can't handle BeginFrames when output surface isn't initialized.
718 // TODO(brianderson): Support output surface creation inside a BeginFrame.
719 if (!HasInitializedOutputSurface())
720 return false;
722 // If we are not visible, we don't need BeginFrame messages.
723 if (!visible_)
724 return false;
726 return (BeginFrameRequiredForAction() || BeginFrameRequiredForChildren() ||
727 BeginFrameNeededForVideo() || ProactiveBeginFrameWanted());
730 void SchedulerStateMachine::SetChildrenNeedBeginFrames(
731 bool children_need_begin_frames) {
732 children_need_begin_frames_ = children_need_begin_frames;
735 void SchedulerStateMachine::SetVideoNeedsBeginFrames(
736 bool video_needs_begin_frames) {
737 video_needs_begin_frames_ = video_needs_begin_frames;
740 void SchedulerStateMachine::SetDeferCommits(bool defer_commits) {
741 defer_commits_ = defer_commits;
744 // These are the cases where we require a BeginFrame message to make progress
745 // on requested actions.
746 bool SchedulerStateMachine::BeginFrameRequiredForAction() const {
747 // The forced draw respects our normal draw scheduling, so we need to
748 // request a BeginImplFrame for it.
749 if (forced_redraw_state_ == FORCED_REDRAW_STATE_WAITING_FOR_DRAW)
750 return true;
752 return needs_animate_ || needs_redraw_ ||
753 (needs_begin_main_frame_ && !defer_commits_);
756 // These are cases where we are very likely want a BeginFrame message in the
757 // near future. Proactively requesting the BeginImplFrame helps hide the round
758 // trip latency of the SetNeedsBeginFrame request that has to go to the
759 // Browser.
760 // This includes things like drawing soon, but might not actually have a new
761 // frame to draw when we receive the next BeginImplFrame.
762 bool SchedulerStateMachine::ProactiveBeginFrameWanted() const {
763 // Do not be proactive when invisible.
764 if (!visible_)
765 return false;
767 // We should proactively request a BeginImplFrame if a commit is pending
768 // because we will want to draw if the commit completes quickly. Do not
769 // request frames when commits are disabled, because the frame requests will
770 // not provide the needed commit (and will wake up the process when it could
771 // stay idle).
772 if ((begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_IDLE) &&
773 !defer_commits_)
774 return true;
776 // If the pending tree activates quickly, we'll want a BeginImplFrame soon
777 // to draw the new active tree.
778 if (has_pending_tree_)
779 return true;
781 // Changing priorities may allow us to activate (given the new priorities),
782 // which may result in a new frame.
783 if (needs_prepare_tiles_)
784 return true;
786 // If we just sent a swap request, it's likely that we are going to produce
787 // another frame soon. This helps avoid negative glitches in our
788 // SetNeedsBeginFrame requests, which may propagate to the BeginImplFrame
789 // provider and get sampled at an inopportune time, delaying the next
790 // BeginImplFrame.
791 if (did_request_swap_in_last_frame_)
792 return true;
794 // If the last commit was aborted because of early out (no updates), we should
795 // still want a begin frame in case there is a commit coming again.
796 if (last_commit_had_no_updates_)
797 return true;
799 return false;
802 void SchedulerStateMachine::OnBeginImplFrame() {
803 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING;
804 current_frame_number_++;
806 last_commit_had_no_updates_ = false;
807 did_request_swap_in_last_frame_ = false;
809 // Clear funnels for any actions we perform during the frame.
810 animate_funnel_ = false;
811 send_begin_main_frame_funnel_ = false;
812 invalidate_output_surface_funnel_ = false;
814 // "Drain" the PrepareTiles funnel.
815 if (prepare_tiles_funnel_ > 0)
816 prepare_tiles_funnel_--;
819 void SchedulerStateMachine::OnBeginImplFrameDeadlinePending() {
820 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME;
823 void SchedulerStateMachine::OnBeginImplFrameDeadline() {
824 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE;
826 did_perform_swap_in_last_draw_ = false;
828 // Clear funnels for any actions we perform during the deadline.
829 request_swap_funnel_ = false;
831 // Allow one PrepareTiles per draw for synchronous compositor.
832 if (settings_.using_synchronous_renderer_compositor) {
833 if (prepare_tiles_funnel_ > 0)
834 prepare_tiles_funnel_--;
838 void SchedulerStateMachine::OnBeginImplFrameIdle() {
839 begin_impl_frame_state_ = BEGIN_IMPL_FRAME_STATE_IDLE;
841 skip_next_begin_main_frame_to_reduce_latency_ = false;
843 // If a new or undrawn active tree is pending after the deadline,
844 // then the main thread is in a high latency mode.
845 main_thread_missed_last_deadline_ =
846 CommitPending() || has_pending_tree_ || active_tree_needs_first_draw_;
848 // If we're entering a state where we won't get BeginFrames set all the
849 // funnels so that we don't perform any actions that we shouldn't.
850 if (!BeginFrameNeeded())
851 send_begin_main_frame_funnel_ = true;
854 SchedulerStateMachine::BeginImplFrameDeadlineMode
855 SchedulerStateMachine::CurrentBeginImplFrameDeadlineMode() const {
856 if (settings_.using_synchronous_renderer_compositor) {
857 // No deadline for synchronous compositor.
858 return BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE;
859 } else if (ShouldTriggerBeginImplFrameDeadlineImmediately()) {
860 return BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE;
861 } else if (wait_for_ready_to_draw_) {
862 // When we are waiting for ready to draw signal, we do not wait to post a
863 // deadline yet.
864 return BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW;
865 } else if (needs_redraw_ && !SwapThrottled()) {
866 // We have an animation or fast input path on the impl thread that wants
867 // to draw, so don't wait too long for a new active tree.
868 // If we are swap throttled we should wait until we are unblocked.
869 return BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR;
870 } else {
871 // The impl thread doesn't have anything it wants to draw and we are just
872 // waiting for a new active tree or we are swap throttled. In short we are
873 // blocked.
874 return BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE;
878 bool SchedulerStateMachine::ShouldTriggerBeginImplFrameDeadlineImmediately()
879 const {
880 // If we just forced activation, we should end the deadline right now.
881 if (PendingActivationsShouldBeForced() && !has_pending_tree_)
882 return true;
884 // Do not trigger deadline immediately if we're waiting for READY_TO_DRAW
885 // unless it's one of the forced cases.
886 if (wait_for_ready_to_draw_)
887 return false;
889 // SwapAck throttle the deadline since we wont draw and swap anyway.
890 if (SwapThrottled())
891 return false;
893 if (active_tree_needs_first_draw_)
894 return true;
896 if (!needs_redraw_)
897 return false;
899 // This is used to prioritize impl-thread draws when the main thread isn't
900 // producing anything, e.g., after an aborted commit. We also check that we
901 // don't have a pending tree -- otherwise we should give it a chance to
902 // activate.
903 // TODO(skyostil): Revisit this when we have more accurate deadline estimates.
904 if (begin_main_frame_state_ == BEGIN_MAIN_FRAME_STATE_IDLE &&
905 !has_pending_tree_)
906 return true;
908 // Prioritize impl-thread draws in impl_latency_takes_priority_ mode.
909 if (impl_latency_takes_priority_)
910 return true;
912 return false;
915 bool SchedulerStateMachine::main_thread_missed_last_deadline() const {
916 return main_thread_missed_last_deadline_;
919 bool SchedulerStateMachine::SwapThrottled() const {
920 return pending_swaps_ >= max_pending_swaps_;
923 void SchedulerStateMachine::SetVisible(bool visible) {
924 if (visible_ == visible)
925 return;
927 visible_ = visible;
929 if (visible)
930 main_thread_missed_last_deadline_ = false;
932 // TODO(sunnyps): Change the funnel to a bool to avoid hacks like this.
933 prepare_tiles_funnel_ = 0;
934 wait_for_ready_to_draw_ = false;
937 void SchedulerStateMachine::SetCanDraw(bool can_draw) { can_draw_ = can_draw; }
939 void SchedulerStateMachine::SetNeedsRedraw() { needs_redraw_ = true; }
941 void SchedulerStateMachine::SetNeedsAnimate() {
942 needs_animate_ = true;
945 bool SchedulerStateMachine::OnlyImplSideUpdatesExpected() const {
946 bool has_impl_updates = needs_redraw_ || needs_animate_;
947 bool main_updates_expected =
948 needs_begin_main_frame_ ||
949 begin_main_frame_state_ != BEGIN_MAIN_FRAME_STATE_IDLE ||
950 has_pending_tree_;
951 return has_impl_updates && !main_updates_expected;
954 void SchedulerStateMachine::SetNeedsPrepareTiles() {
955 if (!needs_prepare_tiles_) {
956 TRACE_EVENT0("cc", "SchedulerStateMachine::SetNeedsPrepareTiles");
957 needs_prepare_tiles_ = true;
961 void SchedulerStateMachine::SetMaxSwapsPending(int max) {
962 max_pending_swaps_ = max;
965 void SchedulerStateMachine::DidSwapBuffers() {
966 pending_swaps_++;
967 swaps_with_current_output_surface_++;
969 DCHECK_LE(pending_swaps_, max_pending_swaps_);
971 did_perform_swap_in_last_draw_ = true;
972 last_frame_number_swap_performed_ = current_frame_number_;
975 void SchedulerStateMachine::DidSwapBuffersComplete() {
976 pending_swaps_--;
979 void SchedulerStateMachine::SetImplLatencyTakesPriority(
980 bool impl_latency_takes_priority) {
981 impl_latency_takes_priority_ = impl_latency_takes_priority;
984 void SchedulerStateMachine::DidDrawIfPossibleCompleted(DrawResult result) {
985 switch (result) {
986 case INVALID_RESULT:
987 NOTREACHED() << "Uninitialized DrawResult.";
988 break;
989 case DRAW_ABORTED_CANT_DRAW:
990 case DRAW_ABORTED_CONTEXT_LOST:
991 NOTREACHED() << "Invalid return value from DrawAndSwapIfPossible:"
992 << result;
993 break;
994 case DRAW_SUCCESS:
995 consecutive_checkerboard_animations_ = 0;
996 forced_redraw_state_ = FORCED_REDRAW_STATE_IDLE;
997 break;
998 case DRAW_ABORTED_CHECKERBOARD_ANIMATIONS:
999 needs_redraw_ = true;
1001 // If we're already in the middle of a redraw, we don't need to
1002 // restart it.
1003 if (forced_redraw_state_ != FORCED_REDRAW_STATE_IDLE)
1004 return;
1006 needs_begin_main_frame_ = true;
1007 consecutive_checkerboard_animations_++;
1008 if (settings_.timeout_and_draw_when_animation_checkerboards &&
1009 consecutive_checkerboard_animations_ >=
1010 settings_.maximum_number_of_failed_draws_before_draw_is_forced) {
1011 consecutive_checkerboard_animations_ = 0;
1012 // We need to force a draw, but it doesn't make sense to do this until
1013 // we've committed and have new textures.
1014 forced_redraw_state_ = FORCED_REDRAW_STATE_WAITING_FOR_COMMIT;
1016 break;
1017 case DRAW_ABORTED_MISSING_HIGH_RES_CONTENT:
1018 // It's not clear whether this missing content is because of missing
1019 // pictures (which requires a commit) or because of memory pressure
1020 // removing textures (which might not). To be safe, request a commit
1021 // anyway.
1022 needs_begin_main_frame_ = true;
1023 break;
1027 void SchedulerStateMachine::SetNeedsBeginMainFrame() {
1028 needs_begin_main_frame_ = true;
1031 void SchedulerStateMachine::NotifyReadyToCommit() {
1032 DCHECK(begin_main_frame_state_ == BEGIN_MAIN_FRAME_STATE_STARTED)
1033 << AsValue()->ToString();
1034 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT;
1035 // In commit_to_active_tree mode, commit should happen right after
1036 // BeginFrame, meaning when this function is called, next action should be
1037 // commit.
1038 if (settings_.commit_to_active_tree)
1039 DCHECK(ShouldCommit());
1042 void SchedulerStateMachine::BeginMainFrameAborted(CommitEarlyOutReason reason) {
1043 DCHECK_EQ(begin_main_frame_state_, BEGIN_MAIN_FRAME_STATE_SENT);
1044 switch (reason) {
1045 case CommitEarlyOutReason::ABORTED_OUTPUT_SURFACE_LOST:
1046 case CommitEarlyOutReason::ABORTED_NOT_VISIBLE:
1047 case CommitEarlyOutReason::ABORTED_DEFERRED_COMMIT:
1048 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_IDLE;
1049 SetNeedsBeginMainFrame();
1050 return;
1051 case CommitEarlyOutReason::FINISHED_NO_UPDATES:
1052 bool commit_has_no_updates = true;
1053 WillCommit(commit_has_no_updates);
1054 return;
1058 void SchedulerStateMachine::DidPrepareTiles() {
1059 needs_prepare_tiles_ = false;
1060 // "Fill" the PrepareTiles funnel.
1061 prepare_tiles_funnel_++;
1064 void SchedulerStateMachine::DidLoseOutputSurface() {
1065 if (output_surface_state_ == OUTPUT_SURFACE_LOST ||
1066 output_surface_state_ == OUTPUT_SURFACE_CREATING)
1067 return;
1068 output_surface_state_ = OUTPUT_SURFACE_LOST;
1069 needs_redraw_ = false;
1070 wait_for_ready_to_draw_ = false;
1073 void SchedulerStateMachine::NotifyReadyToActivate() {
1074 if (has_pending_tree_)
1075 pending_tree_is_ready_for_activation_ = true;
1078 void SchedulerStateMachine::NotifyReadyToDraw() {
1079 wait_for_ready_to_draw_ = false;
1082 void SchedulerStateMachine::DidCreateAndInitializeOutputSurface() {
1083 DCHECK_EQ(output_surface_state_, OUTPUT_SURFACE_CREATING);
1084 output_surface_state_ = OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT;
1086 if (did_create_and_initialize_first_output_surface_) {
1087 // TODO(boliu): See if we can remove this when impl-side painting is always
1088 // on. Does anything on the main thread need to update after recreate?
1089 needs_begin_main_frame_ = true;
1091 did_create_and_initialize_first_output_surface_ = true;
1092 pending_swaps_ = 0;
1093 swaps_with_current_output_surface_ = 0;
1094 main_thread_missed_last_deadline_ = false;
1097 void SchedulerStateMachine::NotifyBeginMainFrameStarted() {
1098 DCHECK_EQ(begin_main_frame_state_, BEGIN_MAIN_FRAME_STATE_SENT);
1099 begin_main_frame_state_ = BEGIN_MAIN_FRAME_STATE_STARTED;
1102 bool SchedulerStateMachine::HasInitializedOutputSurface() const {
1103 switch (output_surface_state_) {
1104 case OUTPUT_SURFACE_LOST:
1105 case OUTPUT_SURFACE_CREATING:
1106 return false;
1108 case OUTPUT_SURFACE_ACTIVE:
1109 case OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT:
1110 case OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION:
1111 return true;
1113 NOTREACHED();
1114 return false;
1117 } // namespace cc