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
);
75 enum BeginMainFrameState
{
76 BEGIN_MAIN_FRAME_STATE_IDLE
,
77 BEGIN_MAIN_FRAME_STATE_SENT
,
78 BEGIN_MAIN_FRAME_STATE_STARTED
,
79 BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT
,
80 BEGIN_MAIN_FRAME_STATE_WAITING_FOR_ACTIVATION
,
81 BEGIN_MAIN_FRAME_STATE_WAITING_FOR_DRAW
,
83 static const char* BeginMainFrameStateToString(BeginMainFrameState 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 begin_main_frame_state_
== BEGIN_MAIN_FRAME_STATE_SENT
||
96 begin_main_frame_state_
== BEGIN_MAIN_FRAME_STATE_STARTED
||
97 begin_main_frame_state_
== BEGIN_MAIN_FRAME_STATE_READY_TO_COMMIT
;
99 BeginMainFrameState
begin_main_frame_state() const {
100 return begin_main_frame_state_
;
103 bool RedrawPending() const { return needs_redraw_
; }
104 bool PrepareTilesPending() const { return needs_prepare_tiles_
; }
109 ACTION_SEND_BEGIN_MAIN_FRAME
,
111 ACTION_ACTIVATE_SYNC_TREE
,
112 ACTION_DRAW_AND_SWAP_IF_POSSIBLE
,
113 ACTION_DRAW_AND_SWAP_FORCED
,
114 ACTION_DRAW_AND_SWAP_ABORT
,
115 ACTION_BEGIN_OUTPUT_SURFACE_CREATION
,
116 ACTION_PREPARE_TILES
,
117 ACTION_INVALIDATE_OUTPUT_SURFACE
,
119 static const char* ActionToString(Action action
);
121 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValue() const;
122 void AsValueInto(base::trace_event::TracedValue
* dict
) const;
124 Action
NextAction() const;
126 void WillSendBeginMainFrame();
127 void WillCommit(bool commit_had_no_updates
);
129 void WillDraw(bool did_request_swap
);
130 void WillBeginOutputSurfaceCreation();
131 void WillPrepareTiles();
132 void WillInvalidateOutputSurface();
134 // Indicates whether the impl thread needs a BeginImplFrame callback in order
136 bool BeginFrameNeeded() const;
138 // Indicates that the system has entered and left a BeginImplFrame callback.
139 // The scheduler will not draw more than once in a given BeginImplFrame
140 // callback nor send more than one BeginMainFrame message.
141 void OnBeginImplFrame();
142 void OnBeginImplFrameDeadlinePending();
143 // Indicates that the scheduler has entered the draw phase. The scheduler
144 // will not draw more than once in a single draw phase.
145 // TODO(sunnyps): Rename OnBeginImplFrameDeadline to OnDraw or similar.
146 void OnBeginImplFrameDeadline();
147 void OnBeginImplFrameIdle();
148 BeginImplFrameState
begin_impl_frame_state() const {
149 return begin_impl_frame_state_
;
151 BeginImplFrameDeadlineMode
CurrentBeginImplFrameDeadlineMode() const;
153 // If the main thread didn't manage to produce a new frame in time for the
154 // impl thread to draw, it is in a high latency mode.
155 bool main_thread_missed_last_deadline() const;
157 bool SwapThrottled() const;
159 // Indicates whether the LayerTreeHostImpl is visible.
160 void SetVisible(bool visible
);
161 bool visible() const { return visible_
; }
163 // Indicates that a redraw is required, either due to the impl tree changing
164 // or the screen being damaged and simply needing redisplay.
165 void SetNeedsRedraw();
166 bool needs_redraw() const { return needs_redraw_
; }
168 void SetNeedsAnimate();
169 bool needs_animate() const { return needs_animate_
; }
171 bool OnlyImplSideUpdatesExpected() const;
173 // Indicates that prepare-tiles is required. This guarantees another
174 // PrepareTiles will occur shortly (even if no redraw is required).
175 void SetNeedsPrepareTiles();
177 // Sets how many swaps can be pending to the OutputSurface.
178 void SetMaxSwapsPending(int max
);
180 // If the scheduler attempted to draw and swap, this provides feedback
181 // regarding whether or not the swap actually occured. We might skip the
182 // swap when there is not damage, for example.
183 void DidSwapBuffers();
185 // Indicates whether a redraw is required because we are currently rendering
186 // with a low resolution or checkerboarded tile.
187 void SetSwapUsedIncompleteTile(bool used_incomplete_tile
);
189 // Notification from the OutputSurface that a swap has been consumed.
190 void DidSwapBuffersComplete();
192 int pending_swaps() const { return pending_swaps_
; }
194 // Indicates whether to prioritize impl thread latency (i.e., animation
195 // smoothness) over new content activation.
196 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
197 bool impl_latency_takes_priority() const {
198 return impl_latency_takes_priority_
;
201 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen.
202 void DidDrawIfPossibleCompleted(DrawResult result
);
204 // Indicates that a new begin main frame flow needs to be performed, either
205 // to pull updates from the main thread to the impl, or to push deltas from
206 // the impl thread to main.
207 void SetNeedsBeginMainFrame();
208 bool needs_begin_main_frame() const { return needs_begin_main_frame_
; }
210 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
212 // Indicates that all painting is complete.
213 void NotifyReadyToCommit();
215 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
216 // from NextAction if the client rejects the BeginMainFrame message.
217 void BeginMainFrameAborted(CommitEarlyOutReason reason
);
219 // Set that we can create the first OutputSurface and start the scheduler.
220 void SetCanStart() { can_start_
= true; }
221 // Allow access of the can_start_ state in tests.
222 bool CanStartForTesting() const { return can_start_
; }
224 // Indicates production should be skipped to recover latency.
225 void SetSkipNextBeginMainFrameToReduceLatency();
227 // Indicates whether drawing would, at this time, make sense.
228 // CanDraw can be used to suppress flashes or checkerboarding
229 // when such behavior would be undesirable.
230 void SetCanDraw(bool can
);
232 // Indicates that scheduled BeginMainFrame is started.
233 void NotifyBeginMainFrameStarted();
235 // Indicates that the pending tree is ready for activation.
236 void NotifyReadyToActivate();
238 // Indicates the active tree's visible tiles are ready to be drawn.
239 void NotifyReadyToDraw();
241 bool has_pending_tree() const { return has_pending_tree_
; }
242 bool active_tree_needs_first_draw() const {
243 return active_tree_needs_first_draw_
;
246 void DidPrepareTiles();
247 void DidLoseOutputSurface();
248 void DidCreateAndInitializeOutputSurface();
249 bool HasInitializedOutputSurface() const;
251 // True if we need to abort draws to make forward progress.
252 bool PendingDrawsShouldBeAborted() const;
254 bool CouldSendBeginMainFrame() const;
256 void SetDeferCommits(bool defer_commits
);
258 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
259 bool children_need_begin_frames() const {
260 return children_need_begin_frames_
;
263 void SetVideoNeedsBeginFrames(bool video_needs_begin_frames
);
264 bool video_needs_begin_frames() const { return video_needs_begin_frames_
; }
267 bool BeginFrameRequiredForAction() const;
268 bool BeginFrameRequiredForChildren() const;
269 bool BeginFrameNeededForVideo() const;
270 bool ProactiveBeginFrameWanted() const;
272 bool ShouldTriggerBeginImplFrameDeadlineImmediately() const;
274 // True if we need to force activations to make forward progress.
275 // TODO(sunnyps): Rename this to ShouldAbortCurrentFrame or similar.
276 bool PendingActivationsShouldBeForced() const;
278 // TODO(brianderson): Remove this once NPAPI support is removed.
279 bool SendingBeginMainFrameMightCauseDeadlock() const;
281 bool ShouldAnimate() const;
282 bool ShouldBeginOutputSurfaceCreation() const;
283 bool ShouldDraw() const;
284 bool ShouldActivatePendingTree() const;
285 bool ShouldSendBeginMainFrame() const;
286 bool ShouldCommit() const;
287 bool ShouldPrepareTiles() const;
288 bool ShouldInvalidateOutputSurface() const;
290 const SchedulerSettings settings_
;
292 OutputSurfaceState output_surface_state_
;
293 BeginImplFrameState begin_impl_frame_state_
;
294 BeginMainFrameState begin_main_frame_state_
;
295 ForcedRedrawOnTimeoutState forced_redraw_state_
;
297 // These are used for tracing only.
299 int current_frame_number_
;
300 int last_frame_number_animate_performed_
;
301 int last_frame_number_swap_performed_
;
302 int last_frame_number_swap_requested_
;
303 int last_frame_number_begin_main_frame_sent_
;
304 int last_frame_number_invalidate_output_surface_performed_
;
306 // These are used to ensure that an action only happens once per frame,
308 bool animate_funnel_
;
309 bool request_swap_funnel_
;
310 bool send_begin_main_frame_funnel_
;
311 bool invalidate_output_surface_funnel_
;
312 // prepare_tiles_funnel_ is "filled" each time PrepareTiles is called
313 // and "drained" on each BeginImplFrame. If the funnel gets too full,
314 // we start throttling ACTION_PREPARE_TILES such that we average one
315 // PrepareTiles per BeginImplFrame.
316 int prepare_tiles_funnel_
;
318 int consecutive_checkerboard_animations_
;
319 int max_pending_swaps_
;
321 int swaps_with_current_output_surface_
;
324 bool needs_prepare_tiles_
;
325 bool needs_begin_main_frame_
;
329 bool has_pending_tree_
;
330 bool pending_tree_is_ready_for_activation_
;
331 bool active_tree_needs_first_draw_
;
332 bool did_create_and_initialize_first_output_surface_
;
333 bool impl_latency_takes_priority_
;
334 bool main_thread_missed_last_deadline_
;
335 bool skip_next_begin_main_frame_to_reduce_latency_
;
336 bool children_need_begin_frames_
;
338 bool video_needs_begin_frames_
;
339 bool last_commit_had_no_updates_
;
340 bool wait_for_ready_to_draw_
;
341 bool did_request_swap_in_last_frame_
;
342 bool did_perform_swap_in_last_draw_
;
345 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine
);
350 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_