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 "cc/base/cc_export.h"
12 #include "cc/scheduler/scheduler_settings.h"
16 // The SchedulerStateMachine decides how to coordinate main thread activites
17 // like painting/running javascript with rendering and input activities on the
20 // The state machine tracks internal state but is also influenced by external
21 // state. Internal state includes things like whether a frame has been
22 // requested, while external state includes things like the current time being
23 // near to the vblank time.
25 // The scheduler seperates "what to do next" from the updating of its internal
26 // state to make testing cleaner.
27 class CC_EXPORT SchedulerStateMachine
{
29 // settings must be valid for the lifetime of this class.
30 explicit SchedulerStateMachine(const SchedulerSettings
& settings
);
34 COMMIT_STATE_FRAME_IN_PROGRESS
,
35 COMMIT_STATE_READY_TO_COMMIT
,
36 COMMIT_STATE_WAITING_FOR_FIRST_DRAW
,
37 COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW
,
41 LAYER_TEXTURE_STATE_UNLOCKED
,
42 LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD
,
43 LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD
,
46 enum OutputSurfaceState
{
47 OUTPUT_SURFACE_ACTIVE
,
49 OUTPUT_SURFACE_RECREATING
,
52 bool CommitPending() const {
53 return commit_state_
== COMMIT_STATE_FRAME_IN_PROGRESS
||
54 commit_state_
== COMMIT_STATE_READY_TO_COMMIT
;
57 bool RedrawPending() const { return needs_redraw_
; }
63 ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS
,
64 ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED
,
65 ACTION_DRAW_IF_POSSIBLE
,
67 ACTION_BEGIN_OUTPUT_SURFACE_RECREATION
,
68 ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD
,
70 Action
NextAction() const;
71 void UpdateState(Action action
);
73 // Indicates whether the scheduler needs a vsync callback in order to make
75 bool VSyncCallbackNeeded() const;
77 // Indicates that the system has entered and left a vsync callback.
78 // The scheduler will not draw more than once in a given vsync callback.
82 // Indicates whether the LayerTreeHostImpl is visible.
83 void SetVisible(bool visible
);
85 // Indicates that a redraw is required, either due to the impl tree changing
86 // or the screen being damaged and simply needing redisplay.
87 void SetNeedsRedraw();
89 // As SetNeedsRedraw(), but ensures the draw will definitely happen even if
90 // we are not visible.
91 void SetNeedsForcedRedraw();
93 // Indicates that a redraw is required because we are currently rendering
94 // with a low resolution or checkerboarded tile.
95 void DidSwapUseIncompleteTile();
97 // Indicates whether ACTION_DRAW_IF_POSSIBLE drew to the screen or not.
98 void DidDrawIfPossibleCompleted(bool success
);
100 // Indicates that a new commit flow needs to be performed, either to pull
101 // updates from the main thread to the impl, or to push deltas from the impl
103 void SetNeedsCommit();
105 // As SetNeedsCommit(), but ensures the BeginFrame will definitely happen even
106 // if we are not visible. After this call we expect to go through the forced
107 // commit flow and then return to waiting for a non-forced BeginFrame to
109 void SetNeedsForcedCommit();
111 // Call this only in response to receiving an ACTION_BEGIN_FRAME
112 // from NextAction. Indicates that all painting is complete.
113 void BeginFrameComplete();
115 // Call this only in response to receiving an ACTION_BEGIN_FRAME
116 // from NextAction if the client rejects the BeginFrame message.
117 void BeginFrameAborted();
119 // Request exclusive access to the textures that back single buffered
120 // layers on behalf of the main thread. Upon acquisition,
121 // ACTION_DRAW_IF_POSSIBLE will not draw until the main thread releases the
122 // textures to the impl thread by committing the layers.
123 void SetMainThreadNeedsLayerTextures();
125 // Indicates whether we can successfully begin a frame at this time.
126 void SetCanBeginFrame(bool can
) { can_begin_frame_
= can
; }
128 // Indicates whether drawing would, at this time, make sense.
129 // CanDraw can be used to supress flashes or checkerboarding
130 // when such behavior would be undesirable.
131 void SetCanDraw(bool can
);
133 // Indicates whether or not there is a pending tree. This influences
134 // whether or not we can succesfully commit at this time. If the
135 // last commit is still being processed (but not blocking), it may not
136 // be possible to take another commit yet. This overrides force commit,
137 // as a commit is already still in flight.
138 void SetHasPendingTree(bool has_pending_tree
);
139 bool has_pending_tree() const { return has_pending_tree_
; }
141 void DidLoseOutputSurface();
142 void DidRecreateOutputSurface();
144 // Exposed for testing purposes.
145 void SetMaximumNumberOfFailedDrawsBeforeDrawIsForced(int num_draws
);
147 // False if drawing is not being prevented, true if drawing won't happen
148 // for some reason, such as not being visible.
149 bool DrawSuspendedUntilCommit() const;
151 std::string
ToString();
154 bool ShouldDrawForced() const;
155 bool ScheduledToDraw() const;
156 bool ShouldDraw() const;
157 bool ShouldAttemptTreeActivation() const;
158 bool ShouldAcquireLayerTexturesForMainThread() const;
159 bool ShouldCheckForCompletedTileUploads() const;
160 bool HasDrawnThisFrame() const;
161 bool HasAttemptedTreeActivationThisFrame() const;
162 bool HasCheckedForCompletedTileUploadsThisFrame() const;
164 const SchedulerSettings settings_
;
166 CommitState commit_state_
;
168 int current_frame_number_
;
169 int last_frame_number_where_draw_was_called_
;
170 int last_frame_number_where_tree_activation_attempted_
;
171 int last_frame_number_where_check_for_completed_tile_uploads_called_
;
172 int consecutive_failed_draws_
;
173 int maximum_number_of_failed_draws_before_draw_is_forced_
;
175 bool swap_used_incomplete_tile_
;
176 bool needs_forced_redraw_
;
177 bool needs_forced_redraw_after_next_commit_
;
179 bool needs_forced_commit_
;
180 bool expect_immediate_begin_frame_
;
181 bool main_thread_needs_layer_textures_
;
184 bool can_begin_frame_
;
186 bool has_pending_tree_
;
187 bool draw_if_possible_failed_
;
188 TextureState texture_state_
;
189 OutputSurfaceState output_surface_state_
;
191 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine
);
196 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_