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/scheduler_settings.h"
23 // The SchedulerStateMachine decides how to coordinate main thread activites
24 // like painting/running javascript with rendering and input activities on the
27 // The state machine tracks internal state but is also influenced by external
28 // state. Internal state includes things like whether a frame has been
29 // requested, while external state includes things like the current time being
30 // near to the vblank time.
32 // The scheduler seperates "what to do next" from the updating of its internal
33 // state to make testing cleaner.
34 class CC_EXPORT SchedulerStateMachine
{
36 // settings must be valid for the lifetime of this class.
37 explicit SchedulerStateMachine(const SchedulerSettings
& settings
);
39 enum OutputSurfaceState
{
40 OUTPUT_SURFACE_ACTIVE
,
42 OUTPUT_SURFACE_CREATING
,
43 OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT
,
44 OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION
,
46 static const char* OutputSurfaceStateToString(OutputSurfaceState state
);
50 COMMIT_STATE_FRAME_IN_PROGRESS
,
51 COMMIT_STATE_READY_TO_COMMIT
,
52 COMMIT_STATE_WAITING_FOR_FIRST_DRAW
,
53 COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW
,
55 static const char* CommitStateToString(CommitState state
);
58 LAYER_TEXTURE_STATE_UNLOCKED
,
59 LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD
,
60 LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD
,
62 static const char* TextureStateToString(TextureState state
);
64 bool CommitPending() const {
65 return commit_state_
== COMMIT_STATE_FRAME_IN_PROGRESS
||
66 commit_state_
== COMMIT_STATE_READY_TO_COMMIT
;
69 bool RedrawPending() const { return needs_redraw_
; }
73 ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD
,
75 ACTION_UPDATE_VISIBLE_TILES
,
76 ACTION_ACTIVATE_PENDING_TREE
,
77 ACTION_DRAW_IF_POSSIBLE
,
79 ACTION_DRAW_AND_SWAP_ABORT
,
80 ACTION_BEGIN_OUTPUT_SURFACE_CREATION
,
81 ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD
,
83 static const char* ActionToString(Action action
);
85 scoped_ptr
<base::Value
> AsValue() const;
87 Action
NextAction() const;
88 void UpdateState(Action action
);
90 // Indicates whether the main thread needs a begin frame callback in order to
92 bool BeginFrameNeededToDrawByImplThread() const;
93 bool ProactiveBeginFrameWantedByImplThread() const;
95 // Indicates that the system has entered and left a BeginFrame callback.
96 // The scheduler will not draw more than once in a given BeginFrame
97 // callback nor send more than one BeginFrame message.
98 void DidEnterBeginFrame(const BeginFrameArgs
& args
);
99 void DidLeaveBeginFrame();
100 bool inside_begin_frame() const { return inside_begin_frame_
; }
102 // Indicates whether the LayerTreeHostImpl is visible.
103 void SetVisible(bool visible
);
105 // Indicates that a redraw is required, either due to the impl tree changing
106 // or the screen being damaged and simply needing redisplay.
107 void SetNeedsRedraw();
109 // As SetNeedsRedraw(), but ensures the draw will definitely happen even if
110 // we are not visible.
111 void SetNeedsForcedRedraw();
113 // Indicates that a redraw is required because we are currently rendering
114 // with a low resolution or checkerboarded tile.
115 void DidSwapUseIncompleteTile();
117 // Indicates whether ACTION_DRAW_IF_POSSIBLE drew to the screen or not.
118 void DidDrawIfPossibleCompleted(bool success
);
120 // Indicates that a new commit flow needs to be performed, either to pull
121 // updates from the main thread to the impl, or to push deltas from the impl
123 void SetNeedsCommit();
125 // As SetNeedsCommit(), but ensures the begin frame will be sent to the main
126 // thread even if we are not visible. After this call we expect to go through
127 // the forced commit flow and then return to waiting for a non-forced
128 // begin frame to finish.
129 void SetNeedsForcedCommit();
131 // Call this only in response to receiving an
132 // ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD from NextAction.
133 // Indicates that all painting is complete.
136 // Call this only in response to receiving an
137 // ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD from NextAction if the client
138 // rejects the begin frame message. If did_handle is false, then
139 // another commit will be retried soon.
140 void BeginFrameAbortedByMainThread(bool did_handle
);
142 // Request exclusive access to the textures that back single buffered
143 // layers on behalf of the main thread. Upon acquisition,
144 // ACTION_DRAW_IF_POSSIBLE will not draw until the main thread releases the
145 // textures to the impl thread by committing the layers.
146 void SetMainThreadNeedsLayerTextures();
148 // Set that we can create the first OutputSurface and start the scheduler.
149 void SetCanStart() { can_start_
= true; }
151 // Indicates whether drawing would, at this time, make sense.
152 // CanDraw can be used to supress flashes or checkerboarding
153 // when such behavior would be undesirable.
154 void SetCanDraw(bool can
);
156 // Indicates that the pending tree is ready for activation.
157 void NotifyReadyToActivate();
159 bool has_pending_tree() const { return has_pending_tree_
; }
161 void DidLoseOutputSurface();
162 void DidCreateAndInitializeOutputSurface();
163 bool HasInitializedOutputSurface() const;
165 // Exposed for testing purposes.
166 void SetMaximumNumberOfFailedDrawsBeforeDrawIsForced(int num_draws
);
168 // True if we need to abort draws to make forward progress.
169 bool PendingDrawsShouldBeAborted() const;
172 // True if we need to force activations to make forward progress.
173 bool PendingActivationsShouldBeForced() const;
175 bool ShouldBeginOutputSurfaceCreation() const;
176 bool ShouldDrawForced() const;
177 bool ShouldDraw() const;
178 bool ShouldActivatePendingTree() const;
179 bool ShouldAcquireLayerTexturesForMainThread() const;
180 bool ShouldUpdateVisibleTiles() const;
181 bool ShouldSendBeginFrameToMainThread() const;
182 bool ShouldCommit() const;
184 bool HasDrawnThisFrame() const;
185 bool HasActivatedPendingTreeThisFrame() const;
186 bool HasUpdatedVisibleTilesThisFrame() const;
187 bool HasSentBeginFrameToMainThreadThisFrame() const;
189 void UpdateStateOnCommit(bool commit_was_aborted
);
190 void UpdateStateOnActivation();
191 void UpdateStateOnDraw(bool did_swap
);
193 const SchedulerSettings settings_
;
195 OutputSurfaceState output_surface_state_
;
196 CommitState commit_state_
;
199 int current_frame_number_
;
200 int last_frame_number_where_begin_frame_sent_to_main_thread_
;
201 int last_frame_number_where_draw_was_called_
;
202 int last_frame_number_where_update_visible_tiles_was_called_
;
203 int consecutive_failed_draws_
;
204 int maximum_number_of_failed_draws_before_draw_is_forced_
;
206 bool swap_used_incomplete_tile_
;
207 bool needs_forced_redraw_
;
208 bool needs_forced_redraw_after_next_commit_
;
210 bool needs_forced_commit_
;
211 bool expect_immediate_begin_frame_for_main_thread_
;
212 bool main_thread_needs_layer_textures_
;
213 bool inside_begin_frame_
;
214 BeginFrameArgs last_begin_frame_args_
;
218 bool has_pending_tree_
;
219 bool pending_tree_is_ready_for_activation_
;
220 bool active_tree_has_been_drawn_
;
221 bool draw_if_possible_failed_
;
222 TextureState texture_state_
;
223 bool did_create_and_initialize_first_output_surface_
;
226 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine
);
231 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_