Don't preview autofill/autocomplete until first real mouse move.
[chromium-blink-merge.git] / cc / scheduler / scheduler_state_machine.h
blobf77df28b783d5ff3f8d411221cdef6823b66904f
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 trace_event {
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::trace_event::ConvertableToTraceFormat> AsValue() const;
118 void AsValueInto(base::trace_event::TracedValue* dict,
119 base::TimeTicks now) const;
121 Action NextAction() const;
122 void UpdateState(Action action);
124 // Indicates whether the impl thread needs a BeginImplFrame callback in order
125 // to make progress.
126 bool BeginFrameNeeded() const;
128 // Indicates whether the scheduler should call
129 // SetNeedsBeginFrames(BeginFrameNeeded()) on the frame source.
130 bool ShouldSetNeedsBeginFrames(bool frame_source_needs_begin_frames) 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 BeginImplFrameState begin_impl_frame_state() const {
146 return begin_impl_frame_state_;
148 BeginImplFrameDeadlineMode CurrentBeginImplFrameDeadlineMode() const;
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);
165 bool visible() const { return visible_; }
167 // Indicates that a redraw is required, either due to the impl tree changing
168 // or the screen being damaged and simply needing redisplay.
169 void SetNeedsRedraw();
170 bool needs_redraw() const { return needs_redraw_; }
172 void SetNeedsAnimate();
173 bool needs_animate() const { return needs_animate_; }
175 // Indicates that prepare-tiles is required. This guarantees another
176 // PrepareTiles will occur shortly (even if no redraw is required).
177 void SetNeedsPrepareTiles();
179 // Sets how many swaps can be pending to the OutputSurface.
180 void SetMaxSwapsPending(int max);
182 // If the scheduler attempted to draw and swap, this provides feedback
183 // regarding whether or not the swap actually occured. We might skip the
184 // swap when there is not damage, for example.
185 void DidSwapBuffers();
187 // Indicates whether a redraw is required because we are currently rendering
188 // with a low resolution or checkerboarded tile.
189 void SetSwapUsedIncompleteTile(bool used_incomplete_tile);
191 // Notification from the OutputSurface that a swap has been consumed.
192 void DidSwapBuffersComplete();
194 // Indicates whether to prioritize impl thread latency (i.e., animation
195 // smoothness) over new content activation.
196 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority);
197 bool impl_latency_takes_priority() const {
198 return impl_latency_takes_priority_;
201 // Indicates whether ACTION_DRAW_AND_SWAP_IF_POSSIBLE drew to the screen.
202 void DidDrawIfPossibleCompleted(DrawResult result);
204 // Indicates that a new commit flow needs to be performed, either to pull
205 // updates from the main thread to the impl, or to push deltas from the impl
206 // thread to main.
207 void SetNeedsCommit();
209 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
210 // from NextAction.
211 // Indicates that all painting is complete.
212 void NotifyReadyToCommit();
214 // Call this only in response to receiving an ACTION_SEND_BEGIN_MAIN_FRAME
215 // from NextAction if the client rejects the BeginMainFrame message.
216 void BeginMainFrameAborted(CommitEarlyOutReason reason);
218 // Set that we can create the first OutputSurface and start the scheduler.
219 void SetCanStart() { can_start_ = true; }
220 // Allow access of the can_start_ state in tests.
221 bool CanStartForTesting() const { return can_start_; }
223 void SetSkipNextBeginMainFrameToReduceLatency();
225 // Indicates whether drawing would, at this time, make sense.
226 // CanDraw can be used to suppress flashes or checkerboarding
227 // when such behavior would be undesirable.
228 void SetCanDraw(bool can);
230 // Indicates that scheduled BeginMainFrame is started.
231 void NotifyBeginMainFrameStarted();
233 // Indicates that the pending tree is ready for activation.
234 void NotifyReadyToActivate();
236 bool has_pending_tree() const { return has_pending_tree_; }
237 bool active_tree_needs_first_draw() const {
238 return active_tree_needs_first_draw_;
241 void DidPrepareTiles();
242 void DidLoseOutputSurface();
243 void DidCreateAndInitializeOutputSurface();
244 bool HasInitializedOutputSurface() const;
246 // True if we need to abort draws to make forward progress.
247 bool PendingDrawsShouldBeAborted() const;
249 bool SupportsProactiveBeginFrame() const;
251 void SetContinuousPainting(bool continuous_painting) {
252 continuous_painting_ = continuous_painting;
255 bool CouldSendBeginMainFrame() const;
257 void SetImplLatencyTakesPriorityOnBattery(
258 bool impl_latency_takes_priority_on_battery) {
259 impl_latency_takes_priority_on_battery_ =
260 impl_latency_takes_priority_on_battery;
263 void SetDeferCommits(bool defer_commits);
265 // TODO(zmo): This is temporary for debugging crbug.com/393331.
266 // We should remove it afterwards.
267 std::string GetStatesForDebugging() const;
269 void SetChildrenNeedBeginFrames(bool children_need_begin_frames);
270 bool children_need_begin_frames() const {
271 return children_need_begin_frames_;
274 protected:
275 bool BeginFrameNeededToAnimateOrDraw() const;
276 bool BeginFrameNeededForChildren() const;
277 bool ProactiveBeginFrameWanted() const;
279 bool ShouldTriggerBeginImplFrameDeadlineImmediately() const;
281 // True if we need to force activations to make forward progress.
282 bool PendingActivationsShouldBeForced() const;
284 bool ShouldAnimate() const;
285 bool ShouldBeginOutputSurfaceCreation() const;
286 bool ShouldDraw() const;
287 bool ShouldActivatePendingTree() const;
288 bool ShouldSendBeginMainFrame() const;
289 bool ShouldCommit() const;
290 bool ShouldPrepareTiles() const;
292 void AdvanceCurrentFrameNumber();
293 bool HasAnimatedThisFrame() const;
294 bool HasSentBeginMainFrameThisFrame() const;
295 bool HasRequestedSwapThisFrame() const;
296 bool HasSwappedThisFrame() const;
298 void UpdateStateOnCommit(bool commit_had_no_updates);
299 void UpdateStateOnActivation();
300 void UpdateStateOnDraw(bool did_request_swap);
301 void UpdateStateOnPrepareTiles();
303 const SchedulerSettings settings_;
305 OutputSurfaceState output_surface_state_;
306 BeginImplFrameState begin_impl_frame_state_;
307 CommitState commit_state_;
308 ForcedRedrawOnTimeoutState forced_redraw_state_;
310 BeginFrameArgs begin_impl_frame_args_;
312 int commit_count_;
313 int current_frame_number_;
314 int last_frame_number_animate_performed_;
315 int last_frame_number_swap_performed_;
316 int last_frame_number_swap_requested_;
317 int last_frame_number_begin_main_frame_sent_;
319 // prepare_tiles_funnel_ is "filled" each time PrepareTiles is called
320 // and "drained" on each BeginImplFrame. If the funnel gets too full,
321 // we start throttling ACTION_PREPARE_TILES such that we average one
322 // PrepareTiles per BeginImplFrame.
323 int prepare_tiles_funnel_;
324 int consecutive_checkerboard_animations_;
325 int max_pending_swaps_;
326 int pending_swaps_;
327 bool needs_redraw_;
328 bool needs_animate_;
329 bool needs_prepare_tiles_;
330 bool needs_commit_;
331 bool inside_poll_for_anticipated_draw_triggers_;
332 bool visible_;
333 bool can_start_;
334 bool can_draw_;
335 bool has_pending_tree_;
336 bool pending_tree_is_ready_for_activation_;
337 bool active_tree_needs_first_draw_;
338 bool did_commit_after_animating_;
339 bool did_create_and_initialize_first_output_surface_;
340 bool impl_latency_takes_priority_;
341 bool skip_next_begin_main_frame_to_reduce_latency_;
342 bool skip_begin_main_frame_to_reduce_latency_;
343 bool continuous_painting_;
344 bool impl_latency_takes_priority_on_battery_;
345 bool children_need_begin_frames_;
346 bool defer_commits_;
348 private:
349 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine);
352 } // namespace cc
354 #endif // CC_SCHEDULER_SCHEDULER_STATE_MACHINE_H_