Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.h
blob238c08c42d166c47acd80c4a682fd225cbf6d033
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_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "cc/base/cc_export.h"
13 #include "cc/output/begin_frame_args.h"
14 #include "cc/scheduler/commit_earlyout_reason.h"
15 #include "cc/scheduler/draw_result.h"
16 #include "cc/scheduler/scheduler_settings.h"
18 namespace base {
19 namespace trace_event {
20 class ConvertableToTraceFormat;
21 class TracedValue;
23 class Value;
26 namespace cc {
28 // The SchedulerStateMachine decides how to coordinate main thread activites
29 // like painting/running javascript with rendering and input activities on the
30 // impl thread.
32 // The state machine tracks internal state but is also influenced by external
33 // state. Internal state includes things like whether a frame has been
34 // requested, while external state includes things like the current time being
35 // near to the vblank time.
37 // The scheduler seperates "what to do next" from the updating of its internal
38 // state to make testing cleaner.
39 class CC_EXPORT SchedulerStateMachine {
40 public:
41 // settings must be valid for the lifetime of this class.
42 explicit SchedulerStateMachine(const SchedulerSettings& settings);
44 enum OutputSurfaceState {
45 OUTPUT_SURFACE_ACTIVE,
46 OUTPUT_SURFACE_LOST,
47 OUTPUT_SURFACE_CREATING,
48 OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT,
49 OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION,
51 static const char* OutputSurfaceStateToString(OutputSurfaceState state);
53 // Note: BeginImplFrameState does not cycle through these states in a fixed
54 // order on all platforms. It's up to the scheduler to set these correctly.
55 // TODO(sunnyps): Rename the states to IDLE, ANIMATE, WAITING_FOR_DEADLINE and
56 // DRAW.
57 enum BeginImplFrameState {
58 BEGIN_IMPL_FRAME_STATE_IDLE,
59 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING,
60 BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME,
61 BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE,
63 static const char* BeginImplFrameStateToString(BeginImplFrameState state);
65 enum BeginImplFrameDeadlineMode {
66 BEGIN_IMPL_FRAME_DEADLINE_MODE_NONE,
67 BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE,
68 BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR,
69 BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE,
70 BEGIN_IMPL_FRAME_DEADLINE_MODE_BLOCKED_ON_READY_TO_DRAW,
72 static const char* BeginImplFrameDeadlineModeToString(
73 BeginImplFrameDeadlineMode mode);
75 enum CommitState {
76 COMMIT_STATE_IDLE,
77 COMMIT_STATE_BEGIN_MAIN_FRAME_SENT,
78 COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED,
79 COMMIT_STATE_READY_TO_COMMIT,
80 COMMIT_STATE_WAITING_FOR_ACTIVATION,
81 COMMIT_STATE_WAITING_FOR_DRAW,
83 static const char* CommitStateToString(CommitState state);
85 enum ForcedRedrawOnTimeoutState {
86 FORCED_REDRAW_STATE_IDLE,
87 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT,
88 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION,
89 FORCED_REDRAW_STATE_WAITING_FOR_DRAW,
91 static const char* ForcedRedrawOnTimeoutStateToString(
92 ForcedRedrawOnTimeoutState state);
94 bool CommitPending() const {
95 return commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_SENT ||
96 commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED ||
97 commit_state_ == COMMIT_STATE_READY_TO_COMMIT;
99 CommitState commit_state() const { return commit_state_; }
101 bool RedrawPending() const { return needs_redraw_; }
102 bool PrepareTilesPending() const { return needs_prepare_tiles_; }
104 enum Action {
105 ACTION_NONE,
106 ACTION_ANIMATE,
107 ACTION_SEND_BEGIN_MAIN_FRAME,
108 ACTION_COMMIT,
109 ACTION_ACTIVATE_SYNC_TREE,
110 ACTION_DRAW_AND_SWAP_IF_POSSIBLE,
111 ACTION_DRAW_AND_SWAP_FORCED,
112 ACTION_DRAW_AND_SWAP_ABORT,
113 ACTION_BEGIN_OUTPUT_SURFACE_CREATION,
114 ACTION_PREPARE_TILES,
115 ACTION_INVALIDATE_OUTPUT_SURFACE,
117 static const char* ActionToString(Action action);
119 scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsValue() const;
120 void AsValueInto(base::trace_event::TracedValue* dict) const;
122 Action NextAction() const;
123 void UpdateState(Action action);
125 // Indicates whether the impl thread needs a BeginImplFrame callback in order
126 // to make progress.
127 bool BeginFrameNeeded() const;
129 // Indicates that the system has entered and left a BeginImplFrame callback.
130 // The scheduler will not draw more than once in a given BeginImplFrame
131 // callback nor send more than one BeginMainFrame message.
132 void OnBeginImplFrame();
133 void OnBeginImplFrameDeadlinePending();
134 // Indicates that the scheduler has entered the draw phase. The scheduler
135 // will not draw more than once in a single draw phase.
136 // TODO(sunnyps): Rename OnBeginImplFrameDeadline to OnDraw or similar.
137 void OnBeginImplFrameDeadline();
138 void OnBeginImplFrameIdle();
139 BeginImplFrameState begin_impl_frame_state() const {
140 return begin_impl_frame_state_;
142 BeginImplFrameDeadlineMode CurrentBeginImplFrameDeadlineMode() const;
144 // If the main thread didn't manage to produce a new frame in time for the
145 // impl thread to draw, it is in a high latency mode.
146 bool MainThreadIsInHighLatencyMode() const;
148 // Indicates whether the LayerTreeHostImpl is visible.
149 void SetVisible(bool visible);
150 bool visible() const { return visible_; }
152 // Indicates that a redraw is required, either due to the impl tree changing
153 // or the screen being damaged and simply needing redisplay.
154 void SetNeedsRedraw();
155 bool needs_redraw() const { return needs_redraw_; }
157 void SetNeedsAnimate();
158 bool needs_animate() const { return needs_animate_; }
160 // Indicates that prepare-tiles is required. This guarantees another
161 // PrepareTiles will occur shortly (even if no redraw is required).
162 void SetNeedsPrepareTiles();
164 // Make deadline wait for ReadyToDraw signal.
165 void SetWaitForReadyToDraw();
167 // Sets how many swaps can be pending to the OutputSurface.
168 void SetMaxSwapsPending(int max);
170 // If the scheduler attempted to draw and swap, this provides feedback
171 // regarding whether or not the swap actually occured. We might skip the
172 // swap when there is not damage, for example.
173 void DidSwapBuffers();
175 // Indicates whether a redraw is required because we are currently rendering
176 // with a low resolution or checkerboarded tile.
177 void SetSwapUsedIncompleteTile(bool used_incomplete_tile);
179 // Notification from the OutputSurface that a swap has been consumed.
180 void DidSwapBuffersComplete();
182 // Indicates whether to prioritize impl thread latency (i.e., animation
183 // smoothness) over new content activation.
184 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority);
185 bool impl_latency_takes_priority() const {
186 return impl_latency_takes_priority_;
189 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen.
190 void DidDrawIfPossibleCompleted(DrawResult result);
192 // Indicates that a new commit flow needs to be performed, either to pull
193 // updates from the main thread to the impl, or to push deltas from the impl
194 // thread to main.
195 void SetNeedsCommit();
197 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
198 // from NextAction.
199 // Indicates that all painting is complete.
200 void NotifyReadyToCommit();
202 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
203 // from NextAction if the client rejects the BeginMainFrame message.
204 void BeginMainFrameAborted(CommitEarlyOutReason reason);
206 // Set that we can create the first OutputSurface and start the scheduler.
207 void SetCanStart() { can_start_ = true; }
208 // Allow access of the can_start_ state in tests.
209 bool CanStartForTesting() const { return can_start_; }
211 void SetSkipNextBeginMainFrameToReduceLatency();
213 // Indicates whether drawing would, at this time, make sense.
214 // CanDraw can be used to suppress flashes or checkerboarding
215 // when such behavior would be undesirable.
216 void SetCanDraw(bool can);
218 // Indicates that scheduled BeginMainFrame is started.
219 void NotifyBeginMainFrameStarted();
221 // Indicates that the pending tree is ready for activation.
222 void NotifyReadyToActivate();
224 // Indicates the active tree's visible tiles are ready to be drawn.
225 void NotifyReadyToDraw();
227 bool has_pending_tree() const { return has_pending_tree_; }
228 bool active_tree_needs_first_draw() const {
229 return active_tree_needs_first_draw_;
232 void DidPrepareTiles();
233 void DidLoseOutputSurface();
234 void DidCreateAndInitializeOutputSurface();
235 bool HasInitializedOutputSurface() const;
237 // True if we need to abort draws to make forward progress.
238 bool PendingDrawsShouldBeAborted() const;
240 void SetContinuousPainting(bool continuous_painting) {
241 continuous_painting_ = continuous_painting;
244 bool CouldSendBeginMainFrame() const;
246 void SetDeferCommits(bool defer_commits);
248 // TODO(zmo): This is temporary for debugging crbug.com/393331.
249 // We should remove it afterwards.
250 std::string GetStatesForDebugging() const;
252 void SetChildrenNeedBeginFrames(bool children_need_begin_frames);
253 bool children_need_begin_frames() const {
254 return children_need_begin_frames_;
257 void SetVideoNeedsBeginFrames(bool video_needs_begin_frames);
258 bool video_needs_begin_frames() const { return video_needs_begin_frames_; }
260 protected:
261 bool BeginFrameRequiredForAction() const;
262 bool BeginFrameRequiredForChildren() const;
263 bool BeginFrameNeededForVideo() const;
264 bool ProactiveBeginFrameWanted() const;
266 bool ShouldTriggerBeginImplFrameDeadlineImmediately() const;
268 // True if we need to force activations to make forward progress.
269 bool PendingActivationsShouldBeForced() const;
271 // TODO(brianderson): Remove this once NPAPI support is removed.
272 bool SendingBeginMainFrameMightCauseDeadlock() const;
274 bool ShouldAnimate() const;
275 bool ShouldBeginOutputSurfaceCreation() const;
276 bool ShouldDraw() const;
277 bool ShouldActivatePendingTree() const;
278 bool ShouldSendBeginMainFrame() const;
279 bool ShouldCommit() const;
280 bool ShouldPrepareTiles() const;
281 bool ShouldInvalidateOutputSurface() const;
283 void UpdateStateOnAnimate();
284 void UpdateStateOnSendBeginMainFrame();
285 void UpdateStateOnCommit(bool commit_had_no_updates);
286 void UpdateStateOnActivation();
287 void UpdateStateOnDraw(bool did_request_swap);
288 void UpdateStateOnBeginOutputSurfaceCreation();
289 void UpdateStateOnPrepareTiles();
290 void UpdateStateOnInvalidateOutputSurface();
292 const SchedulerSettings settings_;
294 OutputSurfaceState output_surface_state_;
295 BeginImplFrameState begin_impl_frame_state_;
296 CommitState commit_state_;
297 ForcedRedrawOnTimeoutState forced_redraw_state_;
299 // These are used for tracing only.
300 int commit_count_;
301 int current_frame_number_;
302 int last_frame_number_animate_performed_;
303 int last_frame_number_swap_performed_;
304 int last_frame_number_swap_requested_;
305 int last_frame_number_begin_main_frame_sent_;
306 int last_frame_number_invalidate_output_surface_performed_;
308 // These are used to ensure that an action only happens once per frame,
309 // deadline, etc.
310 bool animate_funnel_;
311 bool request_swap_funnel_;
312 bool send_begin_main_frame_funnel_;
313 bool invalidate_output_surface_funnel_;
314 // prepare_tiles_funnel_ is "filled" each time PrepareTiles is called
315 // and "drained" on each BeginImplFrame. If the funnel gets too full,
316 // we start throttling ACTION_PREPARE_TILES such that we average one
317 // PrepareTiles per BeginImplFrame.
318 int prepare_tiles_funnel_;
320 int consecutive_checkerboard_animations_;
321 int max_pending_swaps_;
322 int pending_swaps_;
323 bool needs_redraw_;
324 bool needs_animate_;
325 bool needs_prepare_tiles_;
326 bool needs_commit_;
327 bool visible_;
328 bool can_start_;
329 bool can_draw_;
330 bool has_pending_tree_;
331 bool pending_tree_is_ready_for_activation_;
332 bool active_tree_needs_first_draw_;
333 bool did_create_and_initialize_first_output_surface_;
334 bool impl_latency_takes_priority_;
335 bool skip_next_begin_main_frame_to_reduce_latency_;
336 bool skip_begin_main_frame_to_reduce_latency_;
337 bool continuous_painting_;
338 bool children_need_begin_frames_;
339 bool defer_commits_;
340 bool video_needs_begin_frames_;
341 bool last_commit_had_no_updates_;
342 bool wait_for_active_tree_ready_to_draw_;
343 bool did_request_swap_in_last_frame_;
344 bool did_perform_swap_in_last_draw_;
346 private:
347 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine);
350 } // namespace cc
352 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_