OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ | |
6 #define CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "cc/output/begin_frame_args.h" | |
13 #include "cc/scheduler/commit_earlyout_reason.h" | |
14 #include "cc/scheduler/draw_result.h" | |
15 #include "cc/scheduler/scheduler_settings.h" | |
16 | |
17 namespace base { | |
18 namespace trace_event { | |
19 class ConvertableToTraceFormat; | |
20 class TracedValue; | |
21 } | |
22 class Value; | |
23 } | |
24 | |
25 namespace cc { | |
26 | |
27 // The SchedulerStateMachine decides how to coordinate main thread activites | |
28 // like painting/running javascript with rendering and input activities on the | |
29 // impl thread. | |
30 // | |
31 // The state machine tracks internal state but is also influenced by external | |
32 // state. Internal state includes things like whether a frame has been | |
33 // requested, while external state includes things like the current time being | |
34 // near to the vblank time. | |
35 // | |
36 // The scheduler seperates "what to do next" from the updating of its internal | |
37 // state to make testing cleaner. | |
38 class SchedulerStateMachine { | |
39 public: | |
40 // settings must be valid for the lifetime of this class. | |
41 explicit SchedulerStateMachine(const SchedulerSettings& settings); | |
42 | |
43 enum OutputSurfaceState { | |
44 OUTPUT_SURFACE_ACTIVE, | |
45 OUTPUT_SURFACE_LOST, | |
46 OUTPUT_SURFACE_CREATING, | |
47 OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT, | |
48 OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION, | |
49 }; | |
50 static const char* OutputSurfaceStateToString(OutputSurfaceState state); | |
51 | |
52 // Note: BeginImplFrameState will always cycle through all the states in | |
53 // order. Whether or not it actually waits or draws, it will at least try to | |
54 // wait in BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME and try to draw in | |
55 // BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE | |
56 enum BeginImplFrameState { | |
57 BEGIN_IMPL_FRAME_STATE_IDLE, | |
58 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING, | |
59 BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME, | |
60 BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE, | |
61 }; | |
62 static const char* BeginImplFrameStateToString(BeginImplFrameState state); | |
63 | |
64 enum BeginImplFrameDeadlineMode { | |
65 BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE, | |
66 BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR, | |
67 BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE, | |
68 }; | |
69 static const char* BeginImplFrameDeadlineModeToString( | |
70 BeginImplFrameDeadlineMode mode); | |
71 | |
72 enum CommitState { | |
73 COMMIT_STATE_IDLE, | |
74 COMMIT_STATE_BEGIN_MAIN_FRAME_SENT, | |
75 COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED, | |
76 COMMIT_STATE_READY_TO_COMMIT, | |
77 COMMIT_STATE_WAITING_FOR_ACTIVATION, | |
78 COMMIT_STATE_WAITING_FOR_DRAW, | |
79 }; | |
80 static const char* CommitStateToString(CommitState state); | |
81 | |
82 enum ForcedRedrawOnTimeoutState { | |
83 FORCED_REDRAW_STATE_IDLE, | |
84 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT, | |
85 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION, | |
86 FORCED_REDRAW_STATE_WAITING_FOR_DRAW, | |
87 }; | |
88 static const char* ForcedRedrawOnTimeoutStateToString( | |
89 ForcedRedrawOnTimeoutState state); | |
90 | |
91 bool CommitPending() const { | |
92 return commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_SENT || | |
93 commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED || | |
94 commit_state_ == COMMIT_STATE_READY_TO_COMMIT; | |
95 } | |
96 CommitState commit_state() const { return commit_state_; } | |
97 | |
98 bool RedrawPending() const { return needs_redraw_; } | |
99 bool PrepareTilesPending() const { return needs_prepare_tiles_; } | |
100 | |
101 enum Action { | |
102 ACTION_NONE, | |
103 ACTION_ANIMATE, | |
104 ACTION_SEND_BEGIN_MAIN_FRAME, | |
105 ACTION_COMMIT, | |
106 ACTION_ACTIVATE_SYNC_TREE, | |
107 ACTION_DRAW_AND_SWAP_IF_POSSIBLE, | |
108 ACTION_DRAW_AND_SWAP_FORCED, | |
109 ACTION_DRAW_AND_SWAP_ABORT, | |
110 ACTION_BEGIN_OUTPUT_SURFACE_CREATION, | |
111 ACTION_PREPARE_TILES, | |
112 }; | |
113 static const char* ActionToString(Action action); | |
114 | |
115 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValue() const; | |
116 void AsValueInto(base::trace_event::TracedValue* dict) const; | |
117 | |
118 Action NextAction() const; | |
119 void UpdateState(Action action); | |
120 | |
121 // Indicates whether the impl thread needs a BeginImplFrame callback in order | |
122 // to make progress. | |
123 bool BeginFrameNeeded() const; | |
124 | |
125 // Indicates that we need to independently poll for new state and actions | |
126 // because we can't expect a BeginImplFrame. This is mostly used to avoid | |
127 // drawing repeat frames with the synchronous compositor without dropping | |
128 // necessary actions on the floor. | |
129 bool ShouldPollForAnticipatedDrawTriggers() const; | |
130 | |
131 // Indicates that the system has entered and left a BeginImplFrame callback. | |
132 // The scheduler will not draw more than once in a given BeginImplFrame | |
133 // callback nor send more than one BeginMainFrame message. | |
134 void OnBeginImplFrame(); | |
135 void OnBeginImplFrameDeadlinePending(); | |
136 void OnBeginImplFrameDeadline(); | |
137 void OnBeginImplFrameIdle(); | |
138 BeginImplFrameState begin_impl_frame_state() const { | |
139 return begin_impl_frame_state_; | |
140 } | |
141 BeginImplFrameDeadlineMode CurrentBeginImplFrameDeadlineMode() const; | |
142 | |
143 // If the main thread didn't manage to produce a new frame in time for the | |
144 // impl thread to draw, it is in a high latency mode. | |
145 bool MainThreadIsInHighLatencyMode() const; | |
146 | |
147 // PollForAnticipatedDrawTriggers is used by the synchronous compositor to | |
148 // avoid requesting BeginImplFrames when we won't actually draw but still | |
149 // need to advance our state at vsync intervals. | |
150 void DidEnterPollForAnticipatedDrawTriggers(); | |
151 void DidLeavePollForAnticipatedDrawTriggers(); | |
152 bool inside_poll_for_anticipated_draw_triggers() const { | |
153 return inside_poll_for_anticipated_draw_triggers_; | |
154 } | |
155 | |
156 // Indicates whether the LayerTreeHostImpl is visible. | |
157 void SetVisible(bool visible); | |
158 bool visible() const { return visible_; } | |
159 | |
160 // Indicates that a redraw is required, either due to the impl tree changing | |
161 // or the screen being damaged and simply needing redisplay. | |
162 void SetNeedsRedraw(); | |
163 bool needs_redraw() const { return needs_redraw_; } | |
164 | |
165 void SetNeedsAnimate(); | |
166 bool needs_animate() const { return needs_animate_; } | |
167 | |
168 // Indicates that prepare-tiles is required. This guarantees another | |
169 // PrepareTiles will occur shortly (even if no redraw is required). | |
170 void SetNeedsPrepareTiles(); | |
171 | |
172 // Sets how many swaps can be pending to the OutputSurface. | |
173 void SetMaxSwapsPending(int max); | |
174 | |
175 // If the scheduler attempted to draw and swap, this provides feedback | |
176 // regarding whether or not the swap actually occured. We might skip the | |
177 // swap when there is not damage, for example. | |
178 void DidSwapBuffers(); | |
179 | |
180 // Indicates whether a redraw is required because we are currently rendering | |
181 // with a low resolution or checkerboarded tile. | |
182 void SetSwapUsedIncompleteTile(bool used_incomplete_tile); | |
183 | |
184 // Notification from the OutputSurface that a swap has been consumed. | |
185 void DidSwapBuffersComplete(); | |
186 | |
187 // Indicates whether to prioritize impl thread latency (i.e., animation | |
188 // smoothness) over new content activation. | |
189 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority); | |
190 bool impl_latency_takes_priority() const { | |
191 return impl_latency_takes_priority_; | |
192 } | |
193 | |
194 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen. | |
195 void DidDrawIfPossibleCompleted(DrawResult result); | |
196 | |
197 // Indicates that a new commit flow needs to be performed, either to pull | |
198 // updates from the main thread to the impl, or to push deltas from the impl | |
199 // thread to main. | |
200 void SetNeedsCommit(); | |
201 | |
202 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME | |
203 // from NextAction. | |
204 // Indicates that all painting is complete. | |
205 void NotifyReadyToCommit(); | |
206 | |
207 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME | |
208 // from NextAction if the client rejects the BeginMainFrame message. | |
209 void BeginMainFrameAborted(CommitEarlyOutReason reason); | |
210 | |
211 // Set that we can create the first OutputSurface and start the scheduler. | |
212 void SetCanStart() { can_start_ = true; } | |
213 // Allow access of the can_start_ state in tests. | |
214 bool CanStartForTesting() const { return can_start_; } | |
215 | |
216 void SetSkipNextBeginMainFrameToReduceLatency(); | |
217 | |
218 // Indicates whether drawing would, at this time, make sense. | |
219 // CanDraw can be used to suppress flashes or checkerboarding | |
220 // when such behavior would be undesirable. | |
221 void SetCanDraw(bool can); | |
222 | |
223 // Indicates that scheduled BeginMainFrame is started. | |
224 void NotifyBeginMainFrameStarted(); | |
225 | |
226 // Indicates that the pending tree is ready for activation. | |
227 void NotifyReadyToActivate(); | |
228 | |
229 bool has_pending_tree() const { return has_pending_tree_; } | |
230 bool active_tree_needs_first_draw() const { | |
231 return active_tree_needs_first_draw_; | |
232 } | |
233 | |
234 void DidPrepareTiles(); | |
235 void DidLoseOutputSurface(); | |
236 void DidCreateAndInitializeOutputSurface(); | |
237 bool HasInitializedOutputSurface() const; | |
238 | |
239 // True if we need to abort draws to make forward progress. | |
240 bool PendingDrawsShouldBeAborted() const; | |
241 | |
242 bool SupportsProactiveBeginFrame() const; | |
243 | |
244 void SetContinuousPainting(bool continuous_painting) { | |
245 continuous_painting_ = continuous_painting; | |
246 } | |
247 | |
248 bool CouldSendBeginMainFrame() const; | |
249 | |
250 void SetDeferCommits(bool defer_commits); | |
251 | |
252 // TODO(zmo): This is temporary for debugging crbug.com/393331. | |
253 // We should remove it afterwards. | |
254 std::string GetStatesForDebugging() const; | |
255 | |
256 void SetChildrenNeedBeginFrames(bool children_need_begin_frames); | |
257 bool children_need_begin_frames() const { | |
258 return children_need_begin_frames_; | |
259 } | |
260 | |
261 protected: | |
262 bool BeginFrameNeededToAnimateOrDraw() const; | |
263 bool BeginFrameNeededForChildren() const; | |
264 bool ProactiveBeginFrameWanted() const; | |
265 | |
266 bool ShouldTriggerBeginImplFrameDeadlineImmediately() const; | |
267 | |
268 // True if we need to force activations to make forward progress. | |
269 bool PendingActivationsShouldBeForced() const; | |
270 | |
271 bool ShouldAnimate() const; | |
272 bool ShouldBeginOutputSurfaceCreation() const; | |
273 bool ShouldDraw() const; | |
274 bool ShouldActivatePendingTree() const; | |
275 bool ShouldSendBeginMainFrame() const; | |
276 bool ShouldCommit() const; | |
277 bool ShouldPrepareTiles() const; | |
278 | |
279 void AdvanceCurrentFrameNumber(); | |
280 | |
281 void UpdateStateOnCommit(bool commit_had_no_updates); | |
282 void UpdateStateOnActivation(); | |
283 void UpdateStateOnDraw(bool did_request_swap); | |
284 void UpdateStateOnPrepareTiles(); | |
285 | |
286 const SchedulerSettings settings_; | |
287 | |
288 OutputSurfaceState output_surface_state_; | |
289 BeginImplFrameState begin_impl_frame_state_; | |
290 CommitState commit_state_; | |
291 ForcedRedrawOnTimeoutState forced_redraw_state_; | |
292 | |
293 // These are used for tracing only. | |
294 int commit_count_; | |
295 int current_frame_number_; | |
296 int last_frame_number_animate_performed_; | |
297 int last_frame_number_swap_performed_; | |
298 int last_frame_number_swap_requested_; | |
299 int last_frame_number_begin_main_frame_sent_; | |
300 | |
301 // These are used to ensure that an action only happens once per frame, | |
302 // deadline, etc. | |
303 bool animate_funnel_; | |
304 bool perform_swap_funnel_; | |
305 bool request_swap_funnel_; | |
306 bool send_begin_main_frame_funnel_; | |
307 // prepare_tiles_funnel_ is "filled" each time PrepareTiles is called | |
308 // and "drained" on each BeginImplFrame. If the funnel gets too full, | |
309 // we start throttling ACTION_PREPARE_TILES such that we average one | |
310 // PrepareTiles per BeginImplFrame. | |
311 int prepare_tiles_funnel_; | |
312 | |
313 int consecutive_checkerboard_animations_; | |
314 int max_pending_swaps_; | |
315 int pending_swaps_; | |
316 bool needs_redraw_; | |
317 bool needs_animate_; | |
318 bool needs_prepare_tiles_; | |
319 bool needs_commit_; | |
320 bool inside_poll_for_anticipated_draw_triggers_; | |
321 bool visible_; | |
322 bool can_start_; | |
323 bool can_draw_; | |
324 bool has_pending_tree_; | |
325 bool pending_tree_is_ready_for_activation_; | |
326 bool active_tree_needs_first_draw_; | |
327 bool did_create_and_initialize_first_output_surface_; | |
328 bool impl_latency_takes_priority_; | |
329 bool skip_next_begin_main_frame_to_reduce_latency_; | |
330 bool skip_begin_main_frame_to_reduce_latency_; | |
331 bool continuous_painting_; | |
332 bool children_need_begin_frames_; | |
333 bool defer_commits_; | |
334 bool last_commit_had_no_updates_; | |
335 | |
336 private: | |
337 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine); | |
338 }; | |
339 | |
340 } // namespace cc | |
341 | |
342 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_ | |
OLD | NEW |