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