blob: 7ee039aee056e7fac0346232fee8843d897ed21e [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 "base/time/time.h"
13#include "cc/base/cc_export.h"
14#include "cc/output/begin_frame_args.h"
15#include "cc/scheduler/draw_result.h"
16#include "cc/scheduler/scheduler_settings.h"
17
18namespace base {
19namespace debug {
20class ConvertableToTraceFormat;
21class TracedValue;
22}
23class Value;
24}
25
26namespace cc {
27
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.
31//
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.
36//
37// The scheduler seperates "what to do next" from the updating of its internal
38// state to make testing cleaner.
39class CC_EXPORT SchedulerStateMachine {
40 public:
41 // settings must be valid for the lifetime of this class.
42 explicit SchedulerStateMachine(const SchedulerSettings& settings);
43
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,
50 };
51 static const char* OutputSurfaceStateToString(OutputSurfaceState state);
52
53 // Note: BeginImplFrameState will always cycle through all the states in
54 // order. Whether or not it actually waits or draws, it will at least try to
55 // wait in BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME and try to draw in
56 // BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
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,
62 };
63 static const char* BeginImplFrameStateToString(BeginImplFrameState state);
64
James Robinsond2015d92014-12-08 13:45:40 -080065 enum BeginImplFrameDeadlineMode {
66 BEGIN_IMPL_FRAME_DEADLINE_MODE_IMMEDIATE,
67 BEGIN_IMPL_FRAME_DEADLINE_MODE_REGULAR,
68 BEGIN_IMPL_FRAME_DEADLINE_MODE_LATE,
69 };
70 static const char* BeginImplFrameDeadlineModeToString(
71 BeginImplFrameDeadlineMode mode);
72
James Robinson646469d2014-10-03 15:33:28 -070073 enum CommitState {
74 COMMIT_STATE_IDLE,
75 COMMIT_STATE_BEGIN_MAIN_FRAME_SENT,
76 COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED,
77 COMMIT_STATE_READY_TO_COMMIT,
78 COMMIT_STATE_WAITING_FOR_ACTIVATION,
James Robinson6a64b812014-12-03 13:38:42 -080079 COMMIT_STATE_WAITING_FOR_DRAW,
James Robinson646469d2014-10-03 15:33:28 -070080 };
81 static const char* CommitStateToString(CommitState state);
82
83 enum ForcedRedrawOnTimeoutState {
84 FORCED_REDRAW_STATE_IDLE,
85 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT,
86 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION,
87 FORCED_REDRAW_STATE_WAITING_FOR_DRAW,
88 };
89 static const char* ForcedRedrawOnTimeoutStateToString(
90 ForcedRedrawOnTimeoutState state);
91
92 bool CommitPending() const {
93 return commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_SENT ||
94 commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED ||
95 commit_state_ == COMMIT_STATE_READY_TO_COMMIT;
96 }
97 CommitState commit_state() const { return commit_state_; }
98
99 bool RedrawPending() const { return needs_redraw_; }
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100100 bool PrepareTilesPending() const { return needs_prepare_tiles_; }
James Robinson646469d2014-10-03 15:33:28 -0700101
102 enum Action {
103 ACTION_NONE,
104 ACTION_ANIMATE,
105 ACTION_SEND_BEGIN_MAIN_FRAME,
106 ACTION_COMMIT,
James Robinson646469d2014-10-03 15:33:28 -0700107 ACTION_ACTIVATE_SYNC_TREE,
108 ACTION_DRAW_AND_SWAP_IF_POSSIBLE,
109 ACTION_DRAW_AND_SWAP_FORCED,
110 ACTION_DRAW_AND_SWAP_ABORT,
111 ACTION_BEGIN_OUTPUT_SURFACE_CREATION,
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100112 ACTION_PREPARE_TILES,
James Robinson646469d2014-10-03 15:33:28 -0700113 };
114 static const char* ActionToString(Action action);
115
116 scoped_refptr<base::debug::ConvertableToTraceFormat> AsValue() const;
117 void AsValueInto(base::debug::TracedValue* dict, base::TimeTicks now) const;
118
119 Action NextAction() const;
120 void UpdateState(Action action);
121
122 // Indicates whether the impl thread needs a BeginImplFrame callback in order
123 // to make progress.
124 bool BeginFrameNeeded() const;
125
126 // Indicates that we need to independently poll for new state and actions
127 // because we can't expect a BeginImplFrame. This is mostly used to avoid
128 // drawing repeat frames with the synchronous compositor without dropping
129 // necessary actions on the floor.
130 bool ShouldPollForAnticipatedDrawTriggers() const;
131
132 // Indicates that the system has entered and left a BeginImplFrame callback.
133 // The scheduler will not draw more than once in a given BeginImplFrame
134 // callback nor send more than one BeginMainFrame message.
135 void OnBeginImplFrame(const BeginFrameArgs& args);
136 void OnBeginImplFrameDeadlinePending();
137 void OnBeginImplFrameDeadline();
138 void OnBeginImplFrameIdle();
James Robinson646469d2014-10-03 15:33:28 -0700139 BeginImplFrameState begin_impl_frame_state() const {
140 return begin_impl_frame_state_;
141 }
James Robinsond2015d92014-12-08 13:45:40 -0800142 BeginImplFrameDeadlineMode CurrentBeginImplFrameDeadlineMode() const;
James Robinson646469d2014-10-03 15:33:28 -0700143
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;
147
148 // PollForAnticipatedDrawTriggers is used by the synchronous compositor to
149 // avoid requesting BeginImplFrames when we won't actually draw but still
150 // need to advance our state at vsync intervals.
151 void DidEnterPollForAnticipatedDrawTriggers();
152 void DidLeavePollForAnticipatedDrawTriggers();
153 bool inside_poll_for_anticipated_draw_triggers() const {
154 return inside_poll_for_anticipated_draw_triggers_;
155 }
156
157 // Indicates whether the LayerTreeHostImpl is visible.
158 void SetVisible(bool 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
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100168 // Indicates that prepare-tiles is required. This guarantees another
169 // PrepareTiles will occur shortly (even if no redraw is required).
170 void SetNeedsPrepareTiles();
James Robinson646469d2014-10-03 15:33:28 -0700171
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 // If did_handle is false, then another commit will be retried soon.
210 void BeginMainFrameAborted(bool did_handle);
211
212 // Set that we can create the first OutputSurface and start the scheduler.
213 void SetCanStart() { can_start_ = true; }
214
215 void SetSkipNextBeginMainFrameToReduceLatency();
216
217 // Indicates whether drawing would, at this time, make sense.
218 // CanDraw can be used to suppress flashes or checkerboarding
219 // when such behavior would be undesirable.
220 void SetCanDraw(bool can);
221
222 // Indicates that scheduled BeginMainFrame is started.
223 void NotifyBeginMainFrameStarted();
224
225 // Indicates that the pending tree is ready for activation.
226 void NotifyReadyToActivate();
227
228 bool has_pending_tree() const { return has_pending_tree_; }
229 bool active_tree_needs_first_draw() const {
230 return active_tree_needs_first_draw_;
231 }
232
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100233 void DidPrepareTiles();
James Robinson646469d2014-10-03 15:33:28 -0700234 void DidLoseOutputSurface();
235 void DidCreateAndInitializeOutputSurface();
236 bool HasInitializedOutputSurface() const;
237
238 // True if we need to abort draws to make forward progress.
239 bool PendingDrawsShouldBeAborted() const;
240
241 bool SupportsProactiveBeginFrame() const;
242
243 void SetContinuousPainting(bool continuous_painting) {
244 continuous_painting_ = continuous_painting;
245 }
246
247 bool CouldSendBeginMainFrame() const;
248
James Robinson80d418c2014-10-16 16:00:02 -0700249 void SetImplLatencyTakesPriorityOnBattery(
250 bool impl_latency_takes_priority_on_battery) {
251 impl_latency_takes_priority_on_battery_ =
252 impl_latency_takes_priority_on_battery;
253 }
254
James Robinson646469d2014-10-03 15:33:28 -0700255 // TODO(zmo): This is temporary for debugging crbug.com/393331.
256 // We should remove it afterwards.
257 std::string GetStatesForDebugging() const;
258
James Robinsonc4c1c592014-11-21 18:27:04 -0800259 void SetChildrenNeedBeginFrames(bool children_need_begin_frames);
260 bool children_need_begin_frames() const {
261 return children_need_begin_frames_;
262 }
263
James Robinson646469d2014-10-03 15:33:28 -0700264 protected:
265 bool BeginFrameNeededToAnimateOrDraw() const;
James Robinsonc4c1c592014-11-21 18:27:04 -0800266 bool BeginFrameNeededForChildren() const;
James Robinson646469d2014-10-03 15:33:28 -0700267 bool ProactiveBeginFrameWanted() const;
268
James Robinsond2015d92014-12-08 13:45:40 -0800269 bool ShouldTriggerBeginImplFrameDeadlineImmediately() const;
270
James Robinson646469d2014-10-03 15:33:28 -0700271 // True if we need to force activations to make forward progress.
272 bool PendingActivationsShouldBeForced() const;
273
274 bool ShouldAnimate() const;
275 bool ShouldBeginOutputSurfaceCreation() const;
276 bool ShouldDrawForced() const;
277 bool ShouldDraw() const;
278 bool ShouldActivatePendingTree() const;
James Robinson646469d2014-10-03 15:33:28 -0700279 bool ShouldSendBeginMainFrame() const;
280 bool ShouldCommit() const;
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100281 bool ShouldPrepareTiles() const;
James Robinson646469d2014-10-03 15:33:28 -0700282
283 void AdvanceCurrentFrameNumber();
James Robinson53b77582014-10-28 17:00:48 -0700284 bool HasAnimatedThisFrame() const;
James Robinson646469d2014-10-03 15:33:28 -0700285 bool HasSentBeginMainFrameThisFrame() const;
James Robinson646469d2014-10-03 15:33:28 -0700286 bool HasRequestedSwapThisFrame() const;
287 bool HasSwappedThisFrame() const;
288
289 void UpdateStateOnCommit(bool commit_was_aborted);
290 void UpdateStateOnActivation();
291 void UpdateStateOnDraw(bool did_request_swap);
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100292 void UpdateStateOnPrepareTiles();
James Robinson646469d2014-10-03 15:33:28 -0700293
294 const SchedulerSettings settings_;
295
296 OutputSurfaceState output_surface_state_;
297 BeginImplFrameState begin_impl_frame_state_;
298 CommitState commit_state_;
299 ForcedRedrawOnTimeoutState forced_redraw_state_;
300
301 BeginFrameArgs begin_impl_frame_args_;
302
303 int commit_count_;
304 int current_frame_number_;
305 int last_frame_number_animate_performed_;
306 int last_frame_number_swap_performed_;
307 int last_frame_number_swap_requested_;
308 int last_frame_number_begin_main_frame_sent_;
James Robinson646469d2014-10-03 15:33:28 -0700309
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100310 // prepare_tiles_funnel_ is "filled" each time PrepareTiles is called
James Robinson646469d2014-10-03 15:33:28 -0700311 // and "drained" on each BeginImplFrame. If the funnel gets too full,
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100312 // we start throttling ACTION_PREPARE_TILES such that we average one
313 // PrepareTiles per BeginImplFrame.
314 int prepare_tiles_funnel_;
James Robinson646469d2014-10-03 15:33:28 -0700315 int consecutive_checkerboard_animations_;
316 int max_pending_swaps_;
317 int pending_swaps_;
318 bool needs_redraw_;
319 bool needs_animate_;
Przemyslaw Pietrzkiewiczea77f0b2014-12-10 15:35:08 +0100320 bool needs_prepare_tiles_;
James Robinson646469d2014-10-03 15:33:28 -0700321 bool needs_commit_;
322 bool inside_poll_for_anticipated_draw_triggers_;
323 bool visible_;
324 bool can_start_;
325 bool can_draw_;
326 bool has_pending_tree_;
327 bool pending_tree_is_ready_for_activation_;
328 bool active_tree_needs_first_draw_;
James Robinson53b77582014-10-28 17:00:48 -0700329 bool did_commit_after_animating_;
James Robinson646469d2014-10-03 15:33:28 -0700330 bool did_create_and_initialize_first_output_surface_;
331 bool impl_latency_takes_priority_;
332 bool skip_next_begin_main_frame_to_reduce_latency_;
333 bool skip_begin_main_frame_to_reduce_latency_;
334 bool continuous_painting_;
James Robinson80d418c2014-10-16 16:00:02 -0700335 bool impl_latency_takes_priority_on_battery_;
James Robinsonc4c1c592014-11-21 18:27:04 -0800336 bool children_need_begin_frames_;
James Robinson646469d2014-10-03 15:33:28 -0700337
338 private:
339 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine);
340};
341
342} // namespace cc
343
344#endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_