Cancel Linux Aura dragging on escape key press.
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.h
blob7f0ef897f993a3d716c226332e95829f682d8bd2
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_swap_readback_result.h"
16 #include "cc/scheduler/scheduler_settings.h"
18 namespace base {
19 class Value;
22 namespace cc {
24 // The SchedulerStateMachine decides how to coordinate main thread activites
25 // like painting/running javascript with rendering and input activities on the
26 // impl thread.
28 // The state machine tracks internal state but is also influenced by external
29 // state. Internal state includes things like whether a frame has been
30 // requested, while external state includes things like the current time being
31 // near to the vblank time.
33 // The scheduler seperates "what to do next" from the updating of its internal
34 // state to make testing cleaner.
35 class CC_EXPORT SchedulerStateMachine {
36 public:
37 // settings must be valid for the lifetime of this class.
38 explicit SchedulerStateMachine(const SchedulerSettings& settings);
40 enum OutputSurfaceState {
41 OUTPUT_SURFACE_ACTIVE,
42 OUTPUT_SURFACE_LOST,
43 OUTPUT_SURFACE_CREATING,
44 OUTPUT_SURFACE_WAITING_FOR_FIRST_COMMIT,
45 OUTPUT_SURFACE_WAITING_FOR_FIRST_ACTIVATION,
47 static const char* OutputSurfaceStateToString(OutputSurfaceState state);
49 // Note: BeginImplFrameState will always cycle through all the states in
50 // order. Whether or not it actually waits or draws, it will at least try to
51 // wait in BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME and try to draw in
52 // BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE
53 enum BeginImplFrameState {
54 BEGIN_IMPL_FRAME_STATE_IDLE,
55 BEGIN_IMPL_FRAME_STATE_BEGIN_FRAME_STARTING,
56 BEGIN_IMPL_FRAME_STATE_INSIDE_BEGIN_FRAME,
57 BEGIN_IMPL_FRAME_STATE_INSIDE_DEADLINE,
59 static const char* BeginImplFrameStateToString(BeginImplFrameState state);
61 enum CommitState {
62 COMMIT_STATE_IDLE,
63 COMMIT_STATE_FRAME_IN_PROGRESS,
64 COMMIT_STATE_READY_TO_COMMIT,
65 COMMIT_STATE_WAITING_FOR_FIRST_DRAW,
67 static const char* CommitStateToString(CommitState state);
69 enum TextureState {
70 LAYER_TEXTURE_STATE_UNLOCKED,
71 LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD,
72 LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD,
74 static const char* TextureStateToString(TextureState state);
76 enum SynchronousReadbackState {
77 READBACK_STATE_IDLE,
78 READBACK_STATE_NEEDS_BEGIN_MAIN_FRAME,
79 READBACK_STATE_WAITING_FOR_COMMIT,
80 READBACK_STATE_WAITING_FOR_ACTIVATION,
81 READBACK_STATE_WAITING_FOR_DRAW_AND_READBACK,
82 READBACK_STATE_WAITING_FOR_REPLACEMENT_COMMIT,
83 READBACK_STATE_WAITING_FOR_REPLACEMENT_ACTIVATION,
85 static const char* SynchronousReadbackStateToString(
86 SynchronousReadbackState state);
88 enum ForcedRedrawOnTimeoutState {
89 FORCED_REDRAW_STATE_IDLE,
90 FORCED_REDRAW_STATE_WAITING_FOR_COMMIT,
91 FORCED_REDRAW_STATE_WAITING_FOR_ACTIVATION,
92 FORCED_REDRAW_STATE_WAITING_FOR_DRAW,
94 static const char* ForcedRedrawOnTimeoutStateToString(
95 ForcedRedrawOnTimeoutState state);
97 bool CommitPending() const {
98 return commit_state_ == COMMIT_STATE_FRAME_IN_PROGRESS ||
99 commit_state_ == COMMIT_STATE_READY_TO_COMMIT;
102 bool RedrawPending() const { return needs_redraw_; }
103 bool ManageTilesPending() const { return needs_manage_tiles_; }
105 enum Action {
106 ACTION_NONE,
107 ACTION_SEND_BEGIN_MAIN_FRAME,
108 ACTION_COMMIT,
109 ACTION_UPDATE_VISIBLE_TILES,
110 ACTION_ACTIVATE_PENDING_TREE,
111 ACTION_DRAW_AND_SWAP_IF_POSSIBLE,
112 ACTION_DRAW_AND_SWAP_FORCED,
113 ACTION_DRAW_AND_SWAP_ABORT,
114 ACTION_DRAW_AND_READBACK,
115 ACTION_BEGIN_OUTPUT_SURFACE_CREATION,
116 ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD,
117 ACTION_MANAGE_TILES,
119 static const char* ActionToString(Action action);
121 scoped_ptr<base::Value> AsValue() const;
123 Action NextAction() const;
124 void UpdateState(Action action);
126 void CheckInvariants();
128 // Indicates whether the impl thread needs a BeginImplFrame callback in order
129 // to make progress.
130 bool BeginImplFrameNeeded() const;
132 // Indicates that we need to independently poll for new state and actions
133 // because we can't expect a BeginImplFrame. This is mostly used to avoid
134 // drawing repeat frames with the synchronous compositor without dropping
135 // necessary actions on the floor.
136 bool ShouldPollForAnticipatedDrawTriggers() const;
138 // Indicates that the system has entered and left a BeginImplFrame callback.
139 // The scheduler will not draw more than once in a given BeginImplFrame
140 // callback nor send more than one BeginMainFrame message.
141 void OnBeginImplFrame(const BeginFrameArgs& args);
142 void OnBeginImplFrameDeadlinePending();
143 void OnBeginImplFrameDeadline();
144 void OnBeginImplFrameIdle();
145 bool ShouldTriggerBeginImplFrameDeadlineEarly() const;
146 BeginImplFrameState begin_impl_frame_state() const {
147 return begin_impl_frame_state_;
150 // If the main thread didn't manage to produce a new frame in time for the
151 // impl thread to draw, it is in a high latency mode.
152 bool MainThreadIsInHighLatencyMode() const;
154 // PollForAnticipatedDrawTriggers is used by the synchronous compositor to
155 // avoid requesting BeginImplFrames when we won't actually draw but still
156 // need to advance our state at vsync intervals.
157 void DidEnterPollForAnticipatedDrawTriggers();
158 void DidLeavePollForAnticipatedDrawTriggers();
159 bool inside_poll_for_anticipated_draw_triggers() const {
160 return inside_poll_for_anticipated_draw_triggers_;
163 // Indicates whether the LayerTreeHostImpl is visible.
164 void SetVisible(bool 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 // Indicates that manage-tiles is required. This guarantees another
172 // ManageTiles will occur shortly (even if no redraw is required).
173 void SetNeedsManageTiles();
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 // Indicates whether to prioritize animation smoothness over new content
180 // activation.
181 void SetSmoothnessTakesPriority(bool smoothness_takes_priority);
183 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen.
184 void DidDrawIfPossibleCompleted(DrawSwapReadbackResult::DrawResult result);
186 // Indicates that a new commit flow needs to be performed, either to pull
187 // updates from the main thread to the impl, or to push deltas from the impl
188 // thread to main.
189 void SetNeedsCommit();
191 // As SetNeedsCommit(), but ensures the BeginMainFrame will be sent even
192 // if we are not visible. After this call we expect to go through
193 // the forced commit flow and then return to waiting for a non-forced
194 // BeginMainFrame to finish.
195 void SetNeedsForcedCommitForReadback();
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 FinishCommit();
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 // If did_handle is false, then another commit will be retried soon.
205 void BeginMainFrameAborted(bool did_handle);
207 // Request exclusive access to the textures that back single buffered
208 // layers on behalf of the main thread. Upon acquisition,
209 // ACTION_DRAW_AND_SWAP_IF_POSSIBLE will not draw until the main thread
210 // releases the
211 // textures to the impl thread by committing the layers.
212 void SetMainThreadNeedsLayerTextures();
214 // Set that we can create the first OutputSurface and start the scheduler.
215 void SetCanStart() { can_start_ = true; }
217 void SetSkipBeginMainFrameToReduceLatency(bool skip);
219 // Indicates whether drawing would, at this time, make sense.
220 // CanDraw can be used to suppress flashes or checkerboarding
221 // when such behavior would be undesirable.
222 void SetCanDraw(bool can);
224 // Indicates that the pending tree is ready for activation.
225 void NotifyReadyToActivate();
227 bool has_pending_tree() const { return has_pending_tree_; }
229 void DidManageTiles();
230 void DidLoseOutputSurface();
231 void DidCreateAndInitializeOutputSurface();
232 bool HasInitializedOutputSurface() const;
234 // True if we need to abort draws to make forward progress.
235 bool PendingDrawsShouldBeAborted() const;
237 bool SupportsProactiveBeginImplFrame() const;
239 bool IsCommitStateWaiting() const;
241 protected:
242 bool BeginImplFrameNeededToDraw() const;
243 bool ProactiveBeginImplFrameWanted() const;
245 // True if we need to force activations to make forward progress.
246 bool PendingActivationsShouldBeForced() const;
248 bool ShouldBeginOutputSurfaceCreation() const;
249 bool ShouldDrawForced() const;
250 bool ShouldDraw() const;
251 bool ShouldActivatePendingTree() const;
252 bool ShouldAcquireLayerTexturesForMainThread() const;
253 bool ShouldUpdateVisibleTiles() const;
254 bool ShouldSendBeginMainFrame() const;
255 bool ShouldCommit() const;
256 bool ShouldManageTiles() const;
258 void AdvanceCurrentFrameNumber();
259 bool HasSentBeginMainFrameThisFrame() const;
260 bool HasScheduledManageTilesThisFrame() const;
261 bool HasUpdatedVisibleTilesThisFrame() const;
262 bool HasSwappedThisFrame() const;
264 void UpdateStateOnCommit(bool commit_was_aborted);
265 void UpdateStateOnActivation();
266 void UpdateStateOnDraw(bool did_swap);
267 void UpdateStateOnManageTiles();
269 const SchedulerSettings settings_;
271 OutputSurfaceState output_surface_state_;
272 BeginImplFrameState begin_impl_frame_state_;
273 CommitState commit_state_;
274 TextureState texture_state_;
275 ForcedRedrawOnTimeoutState forced_redraw_state_;
276 SynchronousReadbackState readback_state_;
278 BeginFrameArgs last_begin_impl_frame_args_;
280 int commit_count_;
281 int current_frame_number_;
282 int last_frame_number_swap_performed_;
283 int last_frame_number_begin_main_frame_sent_;
284 int last_frame_number_update_visible_tiles_was_called_;
286 // manage_tiles_funnel_ is "filled" each time ManageTiles is called
287 // and "drained" on each BeginImplFrame. If the funnel gets too full,
288 // we start throttling ACTION_MANAGE_TILES such that we average one
289 // ManageTile per BeginImplFrame.
290 int manage_tiles_funnel_;
291 int consecutive_checkerboard_animations_;
292 bool needs_redraw_;
293 bool needs_manage_tiles_;
294 bool swap_used_incomplete_tile_;
295 bool needs_commit_;
296 bool main_thread_needs_layer_textures_;
297 bool inside_poll_for_anticipated_draw_triggers_;
298 bool visible_;
299 bool can_start_;
300 bool can_draw_;
301 bool has_pending_tree_;
302 bool pending_tree_is_ready_for_activation_;
303 bool active_tree_needs_first_draw_;
304 bool draw_if_possible_failed_;
305 bool did_create_and_initialize_first_output_surface_;
306 bool smoothness_takes_priority_;
307 bool skip_begin_main_frame_to_reduce_latency_;
309 private:
310 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine);
313 } // namespace cc
315 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_