Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.h
blob489ece623022e3c1ad31970579a226bc250ca8fd
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/draw_result.h"
16 #include "cc/scheduler/scheduler_settings.h"
18 namespace base {
19 namespace debug {
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 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,
63 static const char* BeginImplFrameStateToString(BeginImplFrameState state);
65 enum CommitState {
66 COMMIT_STATE_IDLE,
67 COMMIT_STATE_BEGIN_MAIN_FRAME_SENT,
68 COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED,
69 COMMIT_STATE_READY_TO_COMMIT,
70 COMMIT_STATE_WAITING_FOR_ACTIVATION,
72 static const char* CommitStateToString(CommitState state);
74 enum ForcedRedrawOnTimeoutState {
75 FORCED_REDRAW_STATE_IDLE,
76 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT,
77 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION,
78 FORCED_REDRAW_STATE_WAITING_FOR_DRAW,
80 static const char* ForcedRedrawOnTimeoutStateToString(
81 ForcedRedrawOnTimeoutState state);
83 bool CommitPending() const {
84 return commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_SENT ||
85 commit_state_ == COMMIT_STATE_BEGIN_MAIN_FRAME_STARTED ||
86 commit_state_ == COMMIT_STATE_READY_TO_COMMIT;
88 CommitState commit_state() const { return commit_state_; }
90 bool RedrawPending() const { return needs_redraw_; }
91 bool ManageTilesPending() const { return needs_manage_tiles_; }
93 enum Action {
94 ACTION_NONE,
95 ACTION_ANIMATE,
96 ACTION_SEND_BEGIN_MAIN_FRAME,
97 ACTION_COMMIT,
98 ACTION_ACTIVATE_SYNC_TREE,
99 ACTION_DRAW_AND_SWAP_IF_POSSIBLE,
100 ACTION_DRAW_AND_SWAP_FORCED,
101 ACTION_DRAW_AND_SWAP_ABORT,
102 ACTION_BEGIN_OUTPUT_SURFACE_CREATION,
103 ACTION_MANAGE_TILES,
105 static const char* ActionToString(Action action);
107 scoped_refptr<base::debug::ConvertableToTraceFormat> AsValue() const;
108 void AsValueInto(base::debug::TracedValue* dict, base::TimeTicks now) const;
110 Action NextAction() const;
111 void UpdateState(Action action);
113 // Indicates whether the impl thread needs a BeginImplFrame callback in order
114 // to make progress.
115 bool BeginFrameNeeded() const;
117 // Indicates that we need to independently poll for new state and actions
118 // because we can't expect a BeginImplFrame. This is mostly used to avoid
119 // drawing repeat frames with the synchronous compositor without dropping
120 // necessary actions on the floor.
121 bool ShouldPollForAnticipatedDrawTriggers() const;
123 // Indicates that the system has entered and left a BeginImplFrame callback.
124 // The scheduler will not draw more than once in a given BeginImplFrame
125 // callback nor send more than one BeginMainFrame message.
126 void OnBeginImplFrame(const BeginFrameArgs& args);
127 void OnBeginImplFrameDeadlinePending();
128 void OnBeginImplFrameDeadline();
129 void OnBeginImplFrameIdle();
130 bool ShouldTriggerBeginImplFrameDeadlineEarly() const;
131 BeginImplFrameState begin_impl_frame_state() const {
132 return begin_impl_frame_state_;
135 // If the main thread didn't manage to produce a new frame in time for the
136 // impl thread to draw, it is in a high latency mode.
137 bool MainThreadIsInHighLatencyMode() const;
139 // PollForAnticipatedDrawTriggers is used by the synchronous compositor to
140 // avoid requesting BeginImplFrames when we won't actually draw but still
141 // need to advance our state at vsync intervals.
142 void DidEnterPollForAnticipatedDrawTriggers();
143 void DidLeavePollForAnticipatedDrawTriggers();
144 bool inside_poll_for_anticipated_draw_triggers() const {
145 return inside_poll_for_anticipated_draw_triggers_;
148 // Indicates whether the LayerTreeHostImpl is visible.
149 void SetVisible(bool visible);
151 // Indicates that a redraw is required, either due to the impl tree changing
152 // or the screen being damaged and simply needing redisplay.
153 void SetNeedsRedraw();
154 bool needs_redraw() const { return needs_redraw_; }
156 void SetNeedsAnimate();
157 bool needs_animate() const { return needs_animate_; }
159 // Indicates that manage-tiles is required. This guarantees another
160 // ManageTiles will occur shortly (even if no redraw is required).
161 void SetNeedsManageTiles();
163 // Sets how many swaps can be pending to the OutputSurface.
164 void SetMaxSwapsPending(int max);
166 // If the scheduler attempted to draw and swap, this provides feedback
167 // regarding whether or not the swap actually occured. We might skip the
168 // swap when there is not damage, for example.
169 void DidSwapBuffers();
171 // Indicates whether a redraw is required because we are currently rendering
172 // with a low resolution or checkerboarded tile.
173 void SetSwapUsedIncompleteTile(bool used_incomplete_tile);
175 // Notification from the OutputSurface that a swap has been consumed.
176 void DidSwapBuffersComplete();
178 // Indicates whether to prioritize impl thread latency (i.e., animation
179 // smoothness) over new content activation.
180 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority);
181 bool impl_latency_takes_priority() const {
182 return impl_latency_takes_priority_;
185 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen.
186 void DidDrawIfPossibleCompleted(DrawResult result);
188 // Indicates that a new commit flow needs to be performed, either to pull
189 // updates from the main thread to the impl, or to push deltas from the impl
190 // thread to main.
191 void SetNeedsCommit();
193 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
194 // from NextAction.
195 // Indicates that all painting is complete.
196 void NotifyReadyToCommit();
198 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
199 // from NextAction if the client rejects the BeginMainFrame message.
200 // If did_handle is false, then another commit will be retried soon.
201 void BeginMainFrameAborted(bool did_handle);
203 // Set that we can create the first OutputSurface and start the scheduler.
204 void SetCanStart() { can_start_ = true; }
206 void SetSkipNextBeginMainFrameToReduceLatency();
208 // Indicates whether drawing would, at this time, make sense.
209 // CanDraw can be used to suppress flashes or checkerboarding
210 // when such behavior would be undesirable.
211 void SetCanDraw(bool can);
213 // Indicates that scheduled BeginMainFrame is started.
214 void NotifyBeginMainFrameStarted();
216 // Indicates that the pending tree is ready for activation.
217 void NotifyReadyToActivate();
219 bool has_pending_tree() const { return has_pending_tree_; }
220 bool active_tree_needs_first_draw() const {
221 return active_tree_needs_first_draw_;
224 void DidManageTiles();
225 void DidLoseOutputSurface();
226 void DidCreateAndInitializeOutputSurface();
227 bool HasInitializedOutputSurface() const;
229 // True if we need to abort draws to make forward progress.
230 bool PendingDrawsShouldBeAborted() const;
232 bool SupportsProactiveBeginFrame() const;
234 void SetContinuousPainting(bool continuous_painting) {
235 continuous_painting_ = continuous_painting;
238 bool CouldSendBeginMainFrame() const;
240 void SetImplLatencyTakesPriorityOnBattery(
241 bool impl_latency_takes_priority_on_battery) {
242 impl_latency_takes_priority_on_battery_ =
243 impl_latency_takes_priority_on_battery;
246 // TODO(zmo): This is temporary for debugging crbug.com/393331.
247 // We should remove it afterwards.
248 std::string GetStatesForDebugging() const;
250 protected:
251 bool BeginFrameNeededToAnimateOrDraw() const;
252 bool ProactiveBeginFrameWanted() const;
254 // True if we need to force activations to make forward progress.
255 bool PendingActivationsShouldBeForced() const;
257 bool ShouldAnimate() const;
258 bool ShouldBeginOutputSurfaceCreation() const;
259 bool ShouldDrawForced() const;
260 bool ShouldDraw() const;
261 bool ShouldActivatePendingTree() const;
262 bool ShouldSendBeginMainFrame() const;
263 bool ShouldCommit() const;
264 bool ShouldManageTiles() const;
266 void AdvanceCurrentFrameNumber();
267 bool HasAnimatedThisFrame() const;
268 bool HasSentBeginMainFrameThisFrame() const;
269 bool HasRequestedSwapThisFrame() const;
270 bool HasSwappedThisFrame() const;
272 void UpdateStateOnCommit(bool commit_was_aborted);
273 void UpdateStateOnActivation();
274 void UpdateStateOnDraw(bool did_request_swap);
275 void UpdateStateOnManageTiles();
277 const SchedulerSettings settings_;
279 OutputSurfaceState output_surface_state_;
280 BeginImplFrameState begin_impl_frame_state_;
281 CommitState commit_state_;
282 ForcedRedrawOnTimeoutState forced_redraw_state_;
284 BeginFrameArgs begin_impl_frame_args_;
286 int commit_count_;
287 int current_frame_number_;
288 int last_frame_number_animate_performed_;
289 int last_frame_number_swap_performed_;
290 int last_frame_number_swap_requested_;
291 int last_frame_number_begin_main_frame_sent_;
293 // manage_tiles_funnel_ is "filled" each time ManageTiles is called
294 // and "drained" on each BeginImplFrame. If the funnel gets too full,
295 // we start throttling ACTION_MANAGE_TILES such that we average one
296 // ManageTile per BeginImplFrame.
297 int manage_tiles_funnel_;
298 int consecutive_checkerboard_animations_;
299 int max_pending_swaps_;
300 int pending_swaps_;
301 bool needs_redraw_;
302 bool needs_animate_;
303 bool needs_manage_tiles_;
304 bool needs_commit_;
305 bool inside_poll_for_anticipated_draw_triggers_;
306 bool visible_;
307 bool can_start_;
308 bool can_draw_;
309 bool has_pending_tree_;
310 bool pending_tree_is_ready_for_activation_;
311 bool active_tree_needs_first_draw_;
312 bool did_commit_after_animating_;
313 bool did_create_and_initialize_first_output_surface_;
314 bool impl_latency_takes_priority_;
315 bool skip_next_begin_main_frame_to_reduce_latency_;
316 bool skip_begin_main_frame_to_reduce_latency_;
317 bool continuous_painting_;
318 bool impl_latency_takes_priority_on_battery_;
320 private:
321 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine);
324 } // namespace cc
326 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_