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 "base/time/time.h"
13 #include "cc/base/cc_export.h"
14 #include "cc/output/begin_frame_args.h"
15 #include "cc/scheduler/draw_swap_readback_result.h"
16 #include "cc/scheduler/scheduler_settings.h"
24 // The SchedulerStateMachine decides how to coordinate main thread activites
25 // like painting/running javascript with rendering and input activities on the
28 // The state machine tracks internal state but is also influenced by external
29 // state. Internal state includes things like whether a frame has been
30 // requested, while external state includes things like the current time being
31 // near to the vblank time.
33 // The scheduler seperates "what to do next" from the updating of its internal
34 // state to make testing cleaner.
35 class CC_EXPORT SchedulerStateMachine
{
37 // settings must be valid for the lifetime of this class.
38 explicit SchedulerStateMachine(const SchedulerSettings
& settings
);
40 enum OutputSurfaceState
{
41 OUTPUT_SURFACE_ACTIVE
,
43 OUTPUT_SURFACE_CREATING
,
44 OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
,
45 OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
,
47 static const char* OutputSurfaceStateToString(OutputSurfaceState state
);
49 // Note: BeginImplFrameState will always cycle through all the states in
50 // order. Whether or not it actually waits or draws, it will at least try to
51 // wait in BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME and try to draw in
52 // BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
53 enum BeginImplFrameState
{
54 BEGIN_IMPL_FRAME_STATE_IDLE
,
55 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING
,
56 BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME
,
57 BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
,
59 static const char* BeginImplFrameStateToString(BeginImplFrameState state
);
63 COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
,
64 COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
,
65 COMMIT_STATE_READY_TO_COMMIT
,
66 COMMIT_STATE_WAITING_FOR_ACTIVATION
,
67 COMMIT_STATE_WAITING_FOR_FIRST_DRAW
,
69 static const char* CommitStateToString(CommitState state
);
71 enum SynchronousReadbackState
{
73 READBACK_STATE_NEEDS_BEGIN_MAIN_FRAME
,
74 READBACK_STATE_WAITING_FOR_COMMIT
,
75 READBACK_STATE_WAITING_FOR_ACTIVATION
,
76 READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK
,
77 READBACK_STATE_WAITING_FOR_REPLACEMENT_COMMIT
,
78 READBACK_STATE_WAITING_FOR_REPLACEMENT_ACTIVATION
,
80 static const char* SynchronousReadbackStateToString(
81 SynchronousReadbackState state
);
83 enum ForcedRedrawOnTimeoutState
{
84 FORCED_REDRAW_STATE_IDLE
,
85 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT
,
86 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION
,
87 FORCED_REDRAW_STATE_WAITING_FOR_DRAW
,
89 static const char* ForcedRedrawOnTimeoutStateToString(
90 ForcedRedrawOnTimeoutState state
);
92 bool CommitPending() const {
93 return commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_SENT
||
94 commit_state_
== COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED
||
95 commit_state_
== COMMIT_STATE_READY_TO_COMMIT
;
97 CommitState
commit_state() const { return commit_state_
; }
99 bool RedrawPending() const { return needs_redraw_
; }
100 bool ManageTilesPending() const { return needs_manage_tiles_
; }
104 ACTION_SEND_BEGIN_MAIN_FRAME
,
106 ACTION_UPDATE_VISIBLE_TILES
,
107 ACTION_ACTIVATE_PENDING_TREE
,
108 ACTION_DRAW_AND_SWAP_IF_POSSIBLE
,
109 ACTION_DRAW_AND_SWAP_FORCED
,
110 ACTION_DRAW_AND_SWAP_ABORT
,
111 ACTION_DRAW_AND_READBACK
,
112 ACTION_BEGIN_OUTPUT_SURFACE_CREATION
,
115 static const char* ActionToString(Action action
);
117 scoped_ptr
<base::Value
> AsValue() const;
119 Action
NextAction() const;
120 void UpdateState(Action action
);
122 void CheckInvariants();
124 // Indicates whether the impl thread needs a BeginImplFrame callback in order
126 bool BeginFrameNeeded() const;
128 // Indicates that we need to independently poll for new state and actions
129 // because we can't expect a BeginImplFrame. This is mostly used to avoid
130 // drawing repeat frames with the synchronous compositor without dropping
131 // necessary actions on the floor.
132 bool ShouldPollForAnticipatedDrawTriggers() const;
134 // Indicates that the system has entered and left a BeginImplFrame callback.
135 // The scheduler will not draw more than once in a given BeginImplFrame
136 // callback nor send more than one BeginMainFrame message.
137 void OnBeginImplFrame(const BeginFrameArgs
& args
);
138 void OnBeginImplFrameDeadlinePending();
139 void OnBeginImplFrameDeadline();
140 void OnBeginImplFrameIdle();
141 bool ShouldTriggerBeginImplFrameDeadlineEarly() const;
142 BeginImplFrameState
begin_impl_frame_state() const {
143 return begin_impl_frame_state_
;
146 // If the main thread didn't manage to produce a new frame in time for the
147 // impl thread to draw, it is in a high latency mode.
148 bool MainThreadIsInHighLatencyMode() const;
150 // PollForAnticipatedDrawTriggers is used by the synchronous compositor to
151 // avoid requesting BeginImplFrames when we won't actually draw but still
152 // need to advance our state at vsync intervals.
153 void DidEnterPollForAnticipatedDrawTriggers();
154 void DidLeavePollForAnticipatedDrawTriggers();
155 bool inside_poll_for_anticipated_draw_triggers() const {
156 return inside_poll_for_anticipated_draw_triggers_
;
159 // Indicates whether the LayerTreeHostImpl is visible.
160 void SetVisible(bool visible
);
162 // Indicates that a redraw is required, either due to the impl tree changing
163 // or the screen being damaged and simply needing redisplay.
164 void SetNeedsRedraw();
165 bool needs_redraw() const { return needs_redraw_
; }
167 // Indicates that manage-tiles is required. This guarantees another
168 // ManageTiles will occur shortly (even if no redraw is required).
169 void SetNeedsManageTiles();
171 // Indicates whether a redraw is required because we are currently rendering
172 // with a low resolution or checkerboarded tile.
173 void SetSwapUsedIncompleteTile(bool used_incomplete_tile
);
175 // Indicates whether to prioritize animation smoothness over new content
177 void SetSmoothnessTakesPriority(bool smoothness_takes_priority
);
178 bool smoothness_takes_priority() const { return smoothness_takes_priority_
; }
180 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen.
181 void DidDrawIfPossibleCompleted(DrawSwapReadbackResult::DrawResult result
);
183 // Indicates that a new commit flow needs to be performed, either to pull
184 // updates from the main thread to the impl, or to push deltas from the impl
186 void SetNeedsCommit();
188 // As SetNeedsCommit(), but ensures the BeginMainFrame will be sent even
189 // if we are not visible. After this call we expect to go through
190 // the forced commit flow and then return to waiting for a non-forced
191 // BeginMainFrame to finish.
192 void SetNeedsForcedCommitForReadback();
194 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
196 // Indicates that all painting is complete.
197 void NotifyReadyToCommit();
199 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
200 // from NextAction if the client rejects the BeginMainFrame message.
201 // If did_handle is false, then another commit will be retried soon.
202 void BeginMainFrameAborted(bool did_handle
);
204 // Set that we can create the first OutputSurface and start the scheduler.
205 void SetCanStart() { can_start_
= true; }
207 void SetSkipNextBeginMainFrameToReduceLatency();
209 // Indicates whether drawing would, at this time, make sense.
210 // CanDraw can be used to suppress flashes or checkerboarding
211 // when such behavior would be undesirable.
212 void SetCanDraw(bool can
);
214 // Indicates that scheduled BeginMainFrame is started.
215 void NotifyBeginMainFrameStarted();
217 // Indicates that the pending tree is ready for activation.
218 void NotifyReadyToActivate();
220 bool has_pending_tree() const { return has_pending_tree_
; }
221 bool active_tree_needs_first_draw() const {
222 return active_tree_needs_first_draw_
;
225 void DidManageTiles();
226 void DidLoseOutputSurface();
227 void DidCreateAndInitializeOutputSurface();
228 bool HasInitializedOutputSurface() const;
230 // True if we need to abort draws to make forward progress.
231 bool PendingDrawsShouldBeAborted() const;
233 bool SupportsProactiveBeginFrame() const;
235 void SetContinuousPainting(bool continuous_painting
) {
236 continuous_painting_
= continuous_painting
;
240 bool BeginFrameNeededToDraw() const;
241 bool ProactiveBeginFrameWanted() const;
243 // True if we need to force activations to make forward progress.
244 bool PendingActivationsShouldBeForced() const;
246 bool ShouldBeginOutputSurfaceCreation() const;
247 bool ShouldDrawForced() const;
248 bool ShouldDraw() const;
249 bool ShouldActivatePendingTree() const;
250 bool ShouldUpdateVisibleTiles() const;
251 bool ShouldSendBeginMainFrame() const;
252 bool ShouldCommit() const;
253 bool ShouldManageTiles() const;
255 void AdvanceCurrentFrameNumber();
256 bool HasSentBeginMainFrameThisFrame() const;
257 bool HasScheduledManageTilesThisFrame() const;
258 bool HasUpdatedVisibleTilesThisFrame() const;
259 bool HasSwappedThisFrame() const;
261 void UpdateStateOnCommit(bool commit_was_aborted
);
262 void UpdateStateOnActivation();
263 void UpdateStateOnDraw(bool did_swap
);
264 void UpdateStateOnManageTiles();
266 const SchedulerSettings settings_
;
268 OutputSurfaceState output_surface_state_
;
269 BeginImplFrameState begin_impl_frame_state_
;
270 CommitState commit_state_
;
271 ForcedRedrawOnTimeoutState forced_redraw_state_
;
272 SynchronousReadbackState readback_state_
;
274 BeginFrameArgs begin_impl_frame_args_
;
277 int current_frame_number_
;
278 int last_frame_number_swap_performed_
;
279 int last_frame_number_begin_main_frame_sent_
;
280 int last_frame_number_update_visible_tiles_was_called_
;
282 // manage_tiles_funnel_ is "filled" each time ManageTiles is called
283 // and "drained" on each BeginImplFrame. If the funnel gets too full,
284 // we start throttling ACTION_MANAGE_TILES such that we average one
285 // ManageTile per BeginImplFrame.
286 int manage_tiles_funnel_
;
287 int consecutive_checkerboard_animations_
;
289 bool needs_manage_tiles_
;
290 bool swap_used_incomplete_tile_
;
292 bool inside_poll_for_anticipated_draw_triggers_
;
296 bool has_pending_tree_
;
297 bool pending_tree_is_ready_for_activation_
;
298 bool active_tree_needs_first_draw_
;
299 bool draw_if_possible_failed_
;
300 bool did_create_and_initialize_first_output_surface_
;
301 bool smoothness_takes_priority_
;
302 bool skip_next_begin_main_frame_to_reduce_latency_
;
303 bool skip_begin_main_frame_to_reduce_latency_
;
304 bool continuous_painting_
;
305 bool needs_back_to_back_readback_
;
308 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine
);
313 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_