OLD | NEW |
1 // Copyright 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ | 5 #ifndef CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ |
6 #define CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ | 6 #define CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 // requested, while external state includes things like the current time being | 24 // requested, while external state includes things like the current time being |
25 // near to the vblank time. | 25 // near to the vblank time. |
26 // | 26 // |
27 // The scheduler seperates "what to do next" from the updating of its internal | 27 // The scheduler seperates "what to do next" from the updating of its internal |
28 // state to make testing cleaner. | 28 // state to make testing cleaner. |
29 class CC_EXPORT SchedulerStateMachine { | 29 class CC_EXPORT SchedulerStateMachine { |
30 public: | 30 public: |
31 // settings must be valid for the lifetime of this class. | 31 // settings must be valid for the lifetime of this class. |
32 explicit SchedulerStateMachine(const SchedulerSettings& settings); | 32 explicit SchedulerStateMachine(const SchedulerSettings& settings); |
33 | 33 |
| 34 enum OutputSurfaceState { |
| 35 OUTPUT_SURFACE_ACTIVE, |
| 36 OUTPUT_SURFACE_LOST, |
| 37 OUTPUT_SURFACE_CREATING, |
| 38 OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT, |
| 39 OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION, |
| 40 }; |
| 41 |
| 42 // Note: BeginFrameState will always cycle through all the states in order. |
| 43 // Whether or not it actually waits or draws, it will at least try to wait in |
| 44 // BEGIN_FRAME_STATE_DEADLINE_PENDING and try to draw in |
| 45 // BEGIN_FRAME_STATE_INSIDE_DEADLINE |
| 46 enum BeginFrameState { |
| 47 BEGIN_FRAME_STATE_IDLE, |
| 48 BEGIN_FRAME_STATE_INSIDE_BEGIN_FRAME, |
| 49 BEGIN_FRAME_STATE_DEADLINE_PENDING, |
| 50 BEGIN_FRAME_STATE_INSIDE_DEADLINE, |
| 51 }; |
| 52 |
34 enum CommitState { | 53 enum CommitState { |
35 COMMIT_STATE_IDLE, | 54 COMMIT_STATE_IDLE, |
36 COMMIT_STATE_FRAME_IN_PROGRESS, | 55 COMMIT_STATE_FRAME_IN_PROGRESS, |
37 COMMIT_STATE_READY_TO_COMMIT, | 56 COMMIT_STATE_READY_TO_COMMIT, |
| 57 COMMIT_STATE_WAITING_FOR_ACTIVATION, |
38 COMMIT_STATE_WAITING_FOR_FIRST_DRAW, | 58 COMMIT_STATE_WAITING_FOR_FIRST_DRAW, |
39 COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW, | |
40 }; | 59 }; |
41 | 60 |
42 enum TextureState { | 61 enum TextureState { |
43 LAYER_TEXTURE_STATE_UNLOCKED, | 62 LAYER_TEXTURE_STATE_UNLOCKED, |
44 LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD, | 63 LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD, |
45 LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD, | 64 LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD, |
46 }; | 65 }; |
47 | 66 |
48 enum OutputSurfaceState { | 67 enum SynchronousReadbackState { |
49 OUTPUT_SURFACE_ACTIVE, | 68 READBACK_STATE_IDLE, |
50 OUTPUT_SURFACE_LOST, | 69 READBACK_STATE_FORCED_COMMIT_REQUESTED, |
51 OUTPUT_SURFACE_CREATING, | 70 READBACK_STATE_FORCED_COMMIT_PENDING, |
| 71 READBACK_STATE_WAITING_FOR_ACTIVATION, |
| 72 READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK, |
| 73 READBACK_STATE_REPLACEMENT_COMMIT_PENDING, |
| 74 READBACK_STATE_REPLACEMENT_COMMIT_ACTIVATING, |
| 75 }; |
| 76 |
| 77 enum ForcedRedrawOnTimeoutState { |
| 78 FORCED_REDRAW_STATE_IDLE, |
| 79 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT, |
| 80 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION, |
| 81 FORCED_REDRAW_STATE_WAITING_FOR_DRAW, |
52 }; | 82 }; |
53 | 83 |
54 bool CommitPending() const { | 84 bool CommitPending() const { |
55 return commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS || | 85 return commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS; |
56 commit_state_ == COMMIT_STATE_READY_TO_COMMIT; | |
57 } | 86 } |
58 | 87 |
59 bool RedrawPending() const { return needs_redraw_; } | 88 bool RedrawPending() const { return needs_redraw_; } |
60 | 89 |
61 enum Action { | 90 enum Action { |
62 ACTION_NONE, | 91 ACTION_NONE, |
63 ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD, | 92 ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD, |
64 ACTION_COMMIT, | 93 ACTION_COMMIT, |
65 ACTION_UPDATE_VISIBLE_TILES, | 94 ACTION_UPDATE_VISIBLE_TILES, |
66 ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED, | 95 ACTION_ACTIVATE_PENDING_TREE, |
67 ACTION_DRAW_IF_POSSIBLE, | 96 ACTION_DRAW_AND_SWAP_IF_POSSIBLE, |
68 ACTION_DRAW_FORCED, | 97 ACTION_DRAW_AND_SWAP_FORCED, |
| 98 ACTION_DRAW_AND_SWAP_ABORT, |
| 99 ACTION_DRAW_AND_READBACK, |
69 ACTION_BEGIN_OUTPUT_SURFACE_CREATION, | 100 ACTION_BEGIN_OUTPUT_SURFACE_CREATION, |
70 ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD, | 101 ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD, |
71 }; | 102 }; |
72 Action NextAction() const; | 103 Action NextAction() const; |
73 void UpdateState(Action action); | 104 void UpdateState(Action action); |
| 105 void AdvanceBeginFrameStateWhenNoActionsRemain(); |
74 | 106 |
75 // Indicates whether the main thread needs a begin frame callback in order to | 107 // Indicates whether the main thread needs a begin frame callback in order to |
76 // make progress. | 108 // make progress. |
| 109 bool BeginFrameNeededByImplThread() const; |
77 bool BeginFrameNeededToDrawByImplThread() const; | 110 bool BeginFrameNeededToDrawByImplThread() const; |
78 bool ProactiveBeginFrameWantedByImplThread() const; | 111 bool BeginFrameProactivelyNeededByImplThread() const; |
79 | 112 |
80 // Indicates that the system has entered and left a BeginFrame callback. | 113 // Indicates that the system has entered and left a BeginFrame callback. |
81 // The scheduler will not draw more than once in a given BeginFrame | 114 // The scheduler will not draw more than once in a given BeginFrame |
82 // callback nor send more than one BeginFrame message. | 115 // callback nor send more than one BeginFrame message. |
83 void DidEnterBeginFrame(const BeginFrameArgs& args); | 116 void OnBeginFrame(const BeginFrameArgs& args); |
84 void DidLeaveBeginFrame(); | 117 void OnBeginFrameDeadline(); |
85 bool inside_begin_frame() const { return inside_begin_frame_; } | 118 bool ShouldTriggerBeginFrameDeadlineEarly() const; |
| 119 bool InsideBeginFrame() const; |
86 | 120 |
87 // Indicates whether the LayerTreeHostImpl is visible. | 121 // Indicates whether the LayerTreeHostImpl is visible. |
88 void SetVisible(bool visible); | 122 void SetVisible(bool visible); |
89 | 123 |
90 // Indicates that a redraw is required, either due to the impl tree changing | 124 // Indicates that a redraw is required, either due to the impl tree changing |
91 // or the screen being damaged and simply needing redisplay. | 125 // or the screen being damaged and simply needing redisplay. |
92 void SetNeedsRedraw(); | 126 void SetNeedsRedraw(); |
93 | 127 bool needs_redraw() const { return needs_redraw_; } |
94 // As SetNeedsRedraw(), but ensures the draw will definitely happen even if | |
95 // we are not visible. | |
96 void SetNeedsForcedRedraw(); | |
97 | 128 |
98 // Indicates that a redraw is required because we are currently rendering | 129 // Indicates that a redraw is required because we are currently rendering |
99 // with a low resolution or checkerboarded tile. | 130 // with a low resolution or checkerboarded tile. |
100 void DidSwapUseIncompleteTile(); | 131 void DidSwapUseIncompleteTile(); |
101 | 132 |
102 // Indicates whether ACTION_DRAW_IF_POSSIBLE drew to the screen or not. | 133 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen. |
103 void DidDrawIfPossibleCompleted(bool success); | 134 void DidDrawIfPossibleCompleted(bool success); |
104 | 135 |
105 // Indicates that a new commit flow needs to be performed, either to pull | 136 // Indicates that a new commit flow needs to be performed, either to pull |
106 // updates from the main thread to the impl, or to push deltas from the impl | 137 // updates from the main thread to the impl, or to push deltas from the impl |
107 // thread to main. | 138 // thread to main. |
108 void SetNeedsCommit(); | 139 void SetNeedsCommit(); |
109 | 140 |
110 // As SetNeedsCommit(), but ensures the begin frame will be sent to the main | 141 // As SetNeedsCommit(), but ensures the begin frame will be sent to the main |
111 // thread even if we are not visible. After this call we expect to go through | 142 // thread even if we are not visible. After this call we expect to go through |
112 // the forced commit flow and then return to waiting for a non-forced | 143 // the forced commit flow and then return to waiting for a non-forced |
113 // begin frame to finish. | 144 // begin frame to finish. |
114 void SetNeedsForcedCommit(); | 145 void SetNeedsForcedCommitForReadback(); |
115 | 146 |
116 // Call this only in response to receiving an | 147 // Call this only in response to receiving an |
117 // ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD from NextAction. | 148 // ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD from NextAction. |
118 // Indicates that all painting is complete. | 149 // Indicates that all painting is complete. |
119 void FinishCommit(); | 150 void FinishCommit(); |
120 | 151 |
121 // Call this only in response to receiving an | 152 // Call this only in response to receiving an |
122 // ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD from NextAction if the client | 153 // ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD from NextAction if the client |
123 // rejects the begin frame message. If did_handle is false, then | 154 // rejects the begin frame message. If did_handle is false, then |
124 // another commit will be retried soon. | 155 // another commit will be retried soon. |
125 void BeginFrameAbortedByMainThread(bool did_handle); | 156 void BeginFrameAbortedByMainThread(bool did_handle); |
126 | 157 |
127 // Request exclusive access to the textures that back single buffered | 158 // Request exclusive access to the textures that back single buffered |
128 // layers on behalf of the main thread. Upon acquisition, | 159 // layers on behalf of the main thread. Upon acquisition, |
129 // ACTION_DRAW_IF_POSSIBLE will not draw until the main thread releases the | 160 // ACTION_DRAW_AND_SWAP_IF_POSSIBLE will not draw until the main thread |
130 // textures to the impl thread by committing the layers. | 161 // releases the textures to the impl thread by committing the layers. |
131 void SetMainThreadNeedsLayerTextures(); | 162 void SetMainThreadNeedsLayerTextures(); |
132 | 163 |
133 // Set that we can create the first OutputSurface and start the scheduler. | 164 // Set that we can create the first OutputSurface and start the scheduler. |
134 void SetCanStart() { can_start_ = true; } | 165 void SetCanStart() { can_start_ = true; } |
135 | 166 |
136 // Indicates whether drawing would, at this time, make sense. | 167 // Indicates whether drawing would, at this time, make sense. |
137 // CanDraw can be used to supress flashes or checkerboarding | 168 // CanDraw can be used to supress flashes or checkerboarding |
138 // when such behavior would be undesirable. | 169 // when such behavior would be undesirable. |
139 void SetCanDraw(bool can); | 170 void SetCanDraw(bool can_draw); |
140 | 171 |
141 // Indicates whether or not there is a pending tree. This influences | 172 // Indicates that the pending tree is ready for activation. |
142 // whether or not we can succesfully commit at this time. If the | 173 void NotifyReadyToActivate(); |
| 174 |
| 175 // Indicates whether or not there is a pending tree (which may be null) |
| 176 // and whether the active tree is null or not. This |
| 177 // influences whether or not we can succesfully commit at this time. If the |
143 // last commit is still being processed (but not blocking), it may not | 178 // last commit is still being processed (but not blocking), it may not |
144 // be possible to take another commit yet. This overrides force commit, | 179 // be possible to take another commit yet. This overrides force commit, |
145 // as a commit is already still in flight. | 180 // as a commit is already still in flight. |
146 void SetHasPendingTree(bool has_pending_tree); | 181 void SetHasTrees(bool has_pending_tree, bool active_tree_is_null); |
147 bool has_pending_tree() const { return has_pending_tree_; } | 182 bool has_pending_tree() const { return has_pending_tree_; } |
148 | 183 |
149 void DidLoseOutputSurface(); | 184 void DidLoseOutputSurface(); |
150 void DidCreateAndInitializeOutputSurface(); | 185 void DidCreateAndInitializeOutputSurface(); |
151 bool HasInitializedOutputSurface() const; | 186 bool HasInitializedOutputSurface() const; |
152 | 187 |
153 // Exposed for testing purposes. | 188 // Exposed for testing purposes. |
154 void SetMaximumNumberOfFailedDrawsBeforeDrawIsForced(int num_draws); | 189 void SetMaximumNumberOfFailedDrawsBeforeDrawIsForced(int num_draws); |
155 | 190 |
156 // False if drawing is not being prevented, true if drawing won't happen | 191 // False if drawing is not being prevented, true if drawing won't happen |
157 // for some reason, such as not being visible. | 192 // for some reason, such as not being visible. |
158 bool DrawSuspendedUntilCommit() const; | 193 bool DrawSuspendedUntilCommit() const; |
159 | 194 |
160 std::string ToString(); | 195 std::string ToString(); |
161 | 196 |
162 protected: | 197 protected: |
163 bool ShouldDrawForced() const; | 198 bool ShouldDrawForced() const; |
164 bool ScheduledToDraw() const; | 199 bool ScheduledToDraw() const; |
165 bool ShouldDraw() const; | 200 bool ShouldDraw() const; |
166 bool ShouldAttemptTreeActivation() const; | 201 bool ShouldDrawInternal(BeginFrameState begin_frame_state) const; |
| 202 bool ShouldActivatePendingTree() const; |
167 bool ShouldAcquireLayerTexturesForMainThread() const; | 203 bool ShouldAcquireLayerTexturesForMainThread() const; |
168 bool ShouldUpdateVisibleTiles() const; | 204 bool ShouldUpdateVisibleTiles() const; |
| 205 bool ShouldSendBeginFrameToMainThread() const; |
169 bool HasDrawnThisFrame() const; | 206 bool HasDrawnThisFrame() const; |
170 bool HasAttemptedTreeActivationThisFrame() const; | 207 bool HasDrawnThisDrawAttempt() const; |
171 bool HasUpdatedVisibleTilesThisFrame() const; | 208 bool HasUpdatedVisibleTilesThisDrawAttempt() const; |
172 void SetPostCommitFlags(); | 209 bool HasSentBeginFrameToMainThreadThisFrame() const; |
| 210 bool HasActivatedPendingTreeThisDrawAttempt() const; |
| 211 |
| 212 void HandleCommitInternal(bool commit_was_aborted); |
| 213 void UpdateStateOnDraw(bool did_swap); |
173 | 214 |
174 const SchedulerSettings settings_; | 215 const SchedulerSettings settings_; |
175 | 216 |
| 217 OutputSurfaceState output_surface_state_; |
| 218 BeginFrameState begin_frame_state_; |
176 CommitState commit_state_; | 219 CommitState commit_state_; |
| 220 TextureState texture_state_; |
| 221 SynchronousReadbackState readback_state_; |
| 222 ForcedRedrawOnTimeoutState forced_redraw_state_; |
| 223 |
| 224 BeginFrameArgs last_begin_frame_args_; |
| 225 |
177 int commit_count_; | 226 int commit_count_; |
| 227 int begin_frame_count_; |
| 228 int draw_attempt_count_; |
| 229 int last_begin_frame_count_swap_performed_; |
| 230 int last_draw_attempt_count_draw_was_called_; |
| 231 int last_begin_frame_count_begin_frame_sent_to_main_thread_; |
| 232 int last_draw_attempt_count_tree_activation_attempted_; |
| 233 int last_draw_attempt_count_completed_tile_uploads_checked_; |
| 234 int last_draw_attempt_count_update_visible_tiles_was_called_; |
178 | 235 |
179 int current_frame_number_; | |
180 int last_frame_number_where_begin_frame_sent_to_main_thread_; | |
181 int last_frame_number_where_draw_was_called_; | |
182 int last_frame_number_where_tree_activation_attempted_; | |
183 int last_frame_number_where_update_visible_tiles_was_called_; | |
184 int consecutive_failed_draws_; | 236 int consecutive_failed_draws_; |
185 int maximum_number_of_failed_draws_before_draw_is_forced_; | 237 int maximum_number_of_failed_draws_before_draw_is_forced_; |
| 238 |
186 bool needs_redraw_; | 239 bool needs_redraw_; |
187 bool swap_used_incomplete_tile_; | 240 bool swap_used_incomplete_tile_; |
188 bool needs_forced_redraw_; | |
189 bool needs_forced_redraw_after_next_commit_; | |
190 bool needs_redraw_after_next_commit_; | |
191 bool needs_commit_; | 241 bool needs_commit_; |
192 bool needs_forced_commit_; | |
193 bool expect_immediate_begin_frame_for_main_thread_; | |
194 bool main_thread_needs_layer_textures_; | 242 bool main_thread_needs_layer_textures_; |
195 bool inside_begin_frame_; | 243 bool active_tree_has_been_drawn_; |
196 BeginFrameArgs last_begin_frame_args_; | 244 bool active_tree_is_null_; |
197 bool visible_; | 245 bool visible_; |
198 bool can_start_; | 246 bool can_start_; |
199 bool can_draw_; | 247 bool can_draw_; |
200 bool has_pending_tree_; | 248 bool has_pending_tree_; |
| 249 bool pending_tree_is_ready_for_activation_; |
201 bool draw_if_possible_failed_; | 250 bool draw_if_possible_failed_; |
202 TextureState texture_state_; | |
203 OutputSurfaceState output_surface_state_; | |
204 bool did_create_and_initialize_first_output_surface_; | 251 bool did_create_and_initialize_first_output_surface_; |
205 | 252 |
206 private: | 253 private: |
207 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine); | 254 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine); |
208 }; | 255 }; |
209 | 256 |
210 } // namespace cc | 257 } // namespace cc |
211 | 258 |
212 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ | 259 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ |
OLD | NEW |