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 #ifndef CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_
6 #define CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "cc/base/cc_export.h"
13 #include "cc/output/begin_frame_args.h"
14 #include "cc/scheduler/commit_earlyout_reason.h"
15 #include "cc/scheduler/draw_result.h"
16 #include "cc/scheduler/scheduler_settings.h"
19 namespace trace_event
{
20 class ConvertableToTraceFormat
;
28 // The SchedulerStateMachine decides how to coordinate main thread activites
29 // like painting/running javascript with rendering and input activities on the
32 // The state machine tracks internal state but is also influenced by external
33 // state. Internal state includes things like whether a frame has been
34 // requested, while external state includes things like the current time being
35 // near to the vblank time.
37 // The scheduler seperates "what to do next" from the updating of its internal
38 // state to make testing cleaner.
39 class CC_EXPORT SchedulerStateMachine
{
41 // settings must be valid for the lifetime of this class.
42 explicit SchedulerStateMachine(const SchedulerSettings
& settings
);
44 enum OutputSurfaceState
{
45 OUTPUT_SURFACE_ACTIVE
,
47 OUTPUT_SURFACE_CREATING
,
48 OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
,
49 OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
,
51 static const char* OutputSurfaceStateToString(OutputSurfaceState state
);
53 // Note: BeginImplFrameState does not cycle through these states in a fixed
54 // order on all platforms. It's up to the scheduler to set these correctly.
55 // TODO(sunnyps): Rename the states to IDLE, ANIMATE, WAITING_FOR_DEADLINE and
57 enum BeginImplFrameState
{
58 BEGIN_IMPL_FRAME_STATE_IDLE
,
59 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
,
60 BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
,
61 BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
,
63 static const char* BeginImplFrameStateToString(BeginImplFrameState state
);
65 enum BeginImplFrameDeadlineMode
{
66 BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE
,
67 BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE
,
68 BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR
,
69 BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE
,
70 BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW
,
72 static const char* BeginImplFrameDeadlineModeToString(
73 BeginImplFrameDeadlineMode mode
);
77 COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
,
78 COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
,
79 COMMIT_STATE_READY_TO_COMMIT
,
80 COMMIT_STATE_WAITING_FOR_ACTIVATION
,
81 COMMIT_STATE_WAITING_FOR_DRAW
,
83 static const char* CommitStateToString(CommitState state
);
85 enum ForcedRedrawOnTimeoutState
{
86 FORCED_REDRAW_STATE_IDLE
,
87 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
,
88 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
,
89 FORCED_REDRAW_STATE_WAITING_FOR_DRAW
,
91 static const char* ForcedRedrawOnTimeoutStateToString(
92 ForcedRedrawOnTimeoutState state
);
94 bool CommitPending() const {
95 return commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
96 commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
||
97 commit_state_
== COMMIT_STATE_READY_TO_COMMIT
;
99 CommitState
commit_state() const { return commit_state_
; }
101 bool RedrawPending() const { return needs_redraw_
; }
102 bool PrepareTilesPending() const { return needs_prepare_tiles_
; }
107 ACTION_SEND_BEGIN_MAIN_FRAME
,
109 ACTION_ACTIVATE_SYNC_TREE
,
110 ACTION_DRAW_AND_SWAP_IF_POSSIBLE
,
111 ACTION_DRAW_AND_SWAP_FORCED
,
112 ACTION_DRAW_AND_SWAP_ABORT
,
113 ACTION_BEGIN_OUTPUT_SURFACE_CREATION
,
114 ACTION_PREPARE_TILES
,
115 ACTION_INVALIDATE_OUTPUT_SURFACE
,
117 static const char* ActionToString(Action action
);
119 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValue() const;
120 void AsValueInto(base::trace_event::TracedValue
* dict
) const;
122 Action
NextAction() const;
123 void UpdateState(Action action
);
125 // Indicates whether the impl thread needs a BeginImplFrame callback in order
127 bool BeginFrameNeeded() const;
129 // Indicates that the system has entered and left a BeginImplFrame callback.
130 // The scheduler will not draw more than once in a given BeginImplFrame
131 // callback nor send more than one BeginMainFrame message.
132 void OnBeginImplFrame();
133 void OnBeginImplFrameDeadlinePending();
134 // Indicates that the scheduler has entered the draw phase. The scheduler
135 // will not draw more than once in a single draw phase.
136 // TODO(sunnyps): Rename OnBeginImplFrameDeadline to OnDraw or similar.
137 void OnBeginImplFrameDeadline();
138 void OnBeginImplFrameIdle();
139 BeginImplFrameState
begin_impl_frame_state() const {
140 return begin_impl_frame_state_
;
142 BeginImplFrameDeadlineMode
CurrentBeginImplFrameDeadlineMode() const;
144 // If the main thread didn't manage to produce a new frame in time for the
145 // impl thread to draw, it is in a high latency mode.
146 bool MainThreadIsInHighLatencyMode() const;
148 // Indicates whether the LayerTreeHostImpl is visible.
149 void SetVisible(bool visible
);
150 bool visible() const { return visible_
; }
152 // Indicates that a redraw is required, either due to the impl tree changing
153 // or the screen being damaged and simply needing redisplay.
154 void SetNeedsRedraw();
155 bool needs_redraw() const { return needs_redraw_
; }
157 void SetNeedsAnimate();
158 bool needs_animate() const { return needs_animate_
; }
160 // Indicates that prepare-tiles is required. This guarantees another
161 // PrepareTiles will occur shortly (even if no redraw is required).
162 void SetNeedsPrepareTiles();
164 // Make deadline wait for ReadyToDraw signal.
165 void SetWaitForReadyToDraw();
167 // Sets how many swaps can be pending to the OutputSurface.
168 void SetMaxSwapsPending(int max
);
170 // If the scheduler attempted to draw and swap, this provides feedback
171 // regarding whether or not the swap actually occured. We might skip the
172 // swap when there is not damage, for example.
173 void DidSwapBuffers();
175 // Indicates whether a redraw is required because we are currently rendering
176 // with a low resolution or checkerboarded tile.
177 void SetSwapUsedIncompleteTile(bool used_incomplete_tile
);
179 // Notification from the OutputSurface that a swap has been consumed.
180 void DidSwapBuffersComplete();
182 int pending_swaps() const { return pending_swaps_
; }
184 // Indicates whether to prioritize impl thread latency (i.e., animation
185 // smoothness) over new content activation.
186 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
187 bool impl_latency_takes_priority() const {
188 return impl_latency_takes_priority_
;
191 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen.
192 void DidDrawIfPossibleCompleted(DrawResult result
);
194 // Indicates that a new commit flow needs to be performed, either to pull
195 // updates from the main thread to the impl, or to push deltas from the impl
197 void SetNeedsCommit();
199 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
201 // Indicates that all painting is complete.
202 void NotifyReadyToCommit();
204 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
205 // from NextAction if the client rejects the BeginMainFrame message.
206 void BeginMainFrameAborted(CommitEarlyOutReason reason
);
208 // Set that we can create the first OutputSurface and start the scheduler.
209 void SetCanStart() { can_start_
= true; }
210 // Allow access of the can_start_ state in tests.
211 bool CanStartForTesting() const { return can_start_
; }
213 void SetSkipNextBeginMainFrameToReduceLatency();
215 // Indicates whether drawing would, at this time, make sense.
216 // CanDraw can be used to suppress flashes or checkerboarding
217 // when such behavior would be undesirable.
218 void SetCanDraw(bool can
);
220 // Indicates that scheduled BeginMainFrame is started.
221 void NotifyBeginMainFrameStarted();
223 // Indicates that the pending tree is ready for activation.
224 void NotifyReadyToActivate();
226 // Indicates the active tree's visible tiles are ready to be drawn.
227 void NotifyReadyToDraw();
229 bool has_pending_tree() const { return has_pending_tree_
; }
230 bool active_tree_needs_first_draw() const {
231 return active_tree_needs_first_draw_
;
234 void DidPrepareTiles();
235 void DidLoseOutputSurface();
236 void DidCreateAndInitializeOutputSurface();
237 bool HasInitializedOutputSurface() const;
239 // True if we need to abort draws to make forward progress.
240 bool PendingDrawsShouldBeAborted() const;
242 void SetContinuousPainting(bool continuous_painting
) {
243 continuous_painting_
= continuous_painting
;
246 bool CouldSendBeginMainFrame() const;
248 void SetDeferCommits(bool defer_commits
);
250 // TODO(zmo): This is temporary for debugging crbug.com/393331.
251 // We should remove it afterwards.
252 std::string
GetStatesForDebugging() const;
254 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
255 bool children_need_begin_frames() const {
256 return children_need_begin_frames_
;
259 void SetVideoNeedsBeginFrames(bool video_needs_begin_frames
);
260 bool video_needs_begin_frames() const { return video_needs_begin_frames_
; }
263 bool BeginFrameRequiredForAction() const;
264 bool BeginFrameRequiredForChildren() const;
265 bool BeginFrameNeededForVideo() const;
266 bool ProactiveBeginFrameWanted() const;
268 bool ShouldTriggerBeginImplFrameDeadlineImmediately() const;
270 // True if we need to force activations to make forward progress.
271 bool PendingActivationsShouldBeForced() const;
273 // TODO(brianderson): Remove this once NPAPI support is removed.
274 bool SendingBeginMainFrameMightCauseDeadlock() const;
276 bool ShouldAnimate() const;
277 bool ShouldBeginOutputSurfaceCreation() const;
278 bool ShouldDraw() const;
279 bool ShouldActivatePendingTree() const;
280 bool ShouldSendBeginMainFrame() const;
281 bool ShouldCommit() const;
282 bool ShouldPrepareTiles() const;
283 bool ShouldInvalidateOutputSurface() const;
285 void UpdateStateOnAnimate();
286 void UpdateStateOnSendBeginMainFrame();
287 void UpdateStateOnCommit(bool commit_had_no_updates
);
288 void UpdateStateOnActivation();
289 void UpdateStateOnDraw(bool did_request_swap
);
290 void UpdateStateOnBeginOutputSurfaceCreation();
291 void UpdateStateOnPrepareTiles();
292 void UpdateStateOnInvalidateOutputSurface();
294 const SchedulerSettings settings_
;
296 OutputSurfaceState output_surface_state_
;
297 BeginImplFrameState begin_impl_frame_state_
;
298 CommitState commit_state_
;
299 ForcedRedrawOnTimeoutState forced_redraw_state_
;
301 // These are used for tracing only.
303 int current_frame_number_
;
304 int last_frame_number_animate_performed_
;
305 int last_frame_number_swap_performed_
;
306 int last_frame_number_swap_requested_
;
307 int last_frame_number_begin_main_frame_sent_
;
308 int last_frame_number_invalidate_output_surface_performed_
;
310 // These are used to ensure that an action only happens once per frame,
312 bool animate_funnel_
;
313 bool request_swap_funnel_
;
314 bool send_begin_main_frame_funnel_
;
315 bool invalidate_output_surface_funnel_
;
316 // prepare_tiles_funnel_ is "filled" each time PrepareTiles is called
317 // and "drained" on each BeginImplFrame. If the funnel gets too full,
318 // we start throttling ACTION_PREPARE_TILES such that we average one
319 // PrepareTiles per BeginImplFrame.
320 int prepare_tiles_funnel_
;
322 int consecutive_checkerboard_animations_
;
323 int max_pending_swaps_
;
325 int swaps_with_current_output_surface_
;
328 bool needs_prepare_tiles_
;
333 bool has_pending_tree_
;
334 bool pending_tree_is_ready_for_activation_
;
335 bool active_tree_needs_first_draw_
;
336 bool did_create_and_initialize_first_output_surface_
;
337 bool impl_latency_takes_priority_
;
338 bool skip_next_begin_main_frame_to_reduce_latency_
;
339 bool skip_begin_main_frame_to_reduce_latency_
;
340 bool continuous_painting_
;
341 bool children_need_begin_frames_
;
343 bool video_needs_begin_frames_
;
344 bool last_commit_had_no_updates_
;
345 bool wait_for_active_tree_ready_to_draw_
;
346 bool did_request_swap_in_last_frame_
;
347 bool did_perform_swap_in_last_draw_
;
350 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine
);
355 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_