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_H_
6 #define CC_SCHEDULER_SCHEDULER_H_
11 #include "base/basictypes.h"
12 #include "base/cancelable_callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "cc/base/cc_export.h"
16 #include "cc/output/begin_frame_args.h"
17 #include "cc/scheduler/delay_based_time_source.h"
18 #include "cc/scheduler/draw_result.h"
19 #include "cc/scheduler/scheduler_settings.h"
20 #include "cc/scheduler/scheduler_state_machine.h"
24 class ConvertableToTraceFormat
;
26 class SingleThreadTaskRunner
;
31 class SchedulerClient
{
33 virtual void SetNeedsBeginFrame(bool enable
) = 0;
34 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) = 0;
35 virtual void ScheduledActionSendBeginMainFrame() = 0;
36 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() = 0;
37 virtual DrawResult
ScheduledActionDrawAndSwapForced() = 0;
38 virtual void ScheduledActionAnimate() = 0;
39 virtual void ScheduledActionCommit() = 0;
40 virtual void ScheduledActionUpdateVisibleTiles() = 0;
41 virtual void ScheduledActionActivateSyncTree() = 0;
42 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
43 virtual void ScheduledActionManageTiles() = 0;
44 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) = 0;
45 virtual base::TimeDelta
DrawDurationEstimate() = 0;
46 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() = 0;
47 virtual base::TimeDelta
CommitToActivateDurationEstimate() = 0;
48 virtual void DidBeginImplFrameDeadline() = 0;
51 virtual ~SchedulerClient() {}
54 class CC_EXPORT Scheduler
{
56 static scoped_ptr
<Scheduler
> Create(
57 SchedulerClient
* client
,
58 const SchedulerSettings
& scheduler_settings
,
59 int layer_tree_host_id
,
60 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
) {
61 return make_scoped_ptr(new Scheduler(
62 client
, scheduler_settings
, layer_tree_host_id
, task_runner
));
67 const SchedulerSettings
& settings() const { return settings_
; }
69 void CommitVSyncParameters(base::TimeTicks timebase
,
70 base::TimeDelta interval
);
71 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
);
75 void SetVisible(bool visible
);
76 void SetCanDraw(bool can_draw
);
77 void NotifyReadyToActivate();
79 void SetNeedsCommit();
81 void SetNeedsRedraw();
83 void SetNeedsAnimate();
85 void SetNeedsManageTiles();
87 void SetMaxSwapsPending(int max
);
88 void DidSwapBuffers();
89 void SetSwapUsedIncompleteTile(bool used_incomplete_tile
);
90 void DidSwapBuffersComplete();
92 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
94 void NotifyReadyToCommit();
95 void BeginMainFrameAborted(bool did_handle
);
97 void DidManageTiles();
98 void DidLoseOutputSurface();
99 void DidCreateAndInitializeOutputSurface();
101 // Tests do not want to shut down until all possible BeginMainFrames have
102 // occured to prevent flakiness.
103 bool MainFrameForTestingWillHappen() const {
104 return state_machine_
.CommitPending() ||
105 state_machine_
.CouldSendBeginMainFrame();
108 bool CommitPending() const { return state_machine_
.CommitPending(); }
109 bool RedrawPending() const { return state_machine_
.RedrawPending(); }
110 bool ManageTilesPending() const {
111 return state_machine_
.ManageTilesPending();
113 bool MainThreadIsInHighLatencyMode() const {
114 return state_machine_
.MainThreadIsInHighLatencyMode();
116 bool BeginImplFrameDeadlinePending() const {
117 return !begin_impl_frame_deadline_task_
.IsCancelled();
120 bool WillDrawIfNeeded() const;
122 base::TimeTicks
AnticipatedDrawTime() const;
124 void NotifyBeginMainFrameStarted();
126 base::TimeTicks
LastBeginImplFrameTime();
128 void BeginFrame(const BeginFrameArgs
& args
);
130 scoped_refptr
<base::debug::ConvertableToTraceFormat
> AsValue() const;
132 void SetContinuousPainting(bool continuous_painting
) {
133 state_machine_
.SetContinuousPainting(continuous_painting
);
137 class CC_EXPORT SyntheticBeginFrameSource
: public TimeSourceClient
{
139 SyntheticBeginFrameSource(Scheduler
* scheduler
,
140 base::SingleThreadTaskRunner
* task_runner
);
141 virtual ~SyntheticBeginFrameSource();
143 // Updates the phase and frequency of the timer.
144 void CommitVSyncParameters(base::TimeTicks timebase
,
145 base::TimeDelta interval
);
147 // Activates future BeginFrames and, if activating, pushes the most
148 // recently missed BeginFrame to the back of a retroactive queue.
149 void SetNeedsBeginFrame(bool needs_begin_frame
,
150 std::deque
<BeginFrameArgs
>* begin_retro_frame_args
);
152 bool IsActive() const;
154 // TimeSourceClient implementation of OnTimerTick triggers a BeginFrame.
155 virtual void OnTimerTick() OVERRIDE
;
157 void AsValueInto(base::debug::TracedValue
* dict
) const;
160 BeginFrameArgs
CreateSyntheticBeginFrameArgs(base::TimeTicks frame_time
);
162 Scheduler
* scheduler_
;
163 scoped_refptr
<DelayBasedTimeSource
> time_source_
;
166 Scheduler(SchedulerClient
* client
,
167 const SchedulerSettings
& scheduler_settings
,
168 int layer_tree_host_id
,
169 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
);
171 const SchedulerSettings settings_
;
172 SchedulerClient
* client_
;
173 int layer_tree_host_id_
;
174 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
176 base::TimeDelta vsync_interval_
;
177 base::TimeDelta estimated_parent_draw_time_
;
179 bool last_set_needs_begin_frame_
;
180 bool begin_unthrottled_frame_posted_
;
181 bool begin_retro_frame_posted_
;
182 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
183 BeginFrameArgs begin_impl_frame_args_
;
185 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_begin_frame_source_
;
187 base::Closure begin_retro_frame_closure_
;
188 base::Closure begin_unthrottled_frame_closure_
;
190 base::Closure begin_impl_frame_deadline_closure_
;
191 base::Closure poll_for_draw_triggers_closure_
;
192 base::Closure advance_commit_state_closure_
;
193 base::CancelableClosure begin_impl_frame_deadline_task_
;
194 base::CancelableClosure poll_for_draw_triggers_task_
;
195 base::CancelableClosure advance_commit_state_task_
;
197 SchedulerStateMachine state_machine_
;
198 bool inside_process_scheduled_actions_
;
199 SchedulerStateMachine::Action inside_action_
;
202 base::TimeTicks
AdjustedBeginImplFrameDeadline(
203 const BeginFrameArgs
& args
,
204 base::TimeDelta draw_duration_estimate
) const;
205 void ScheduleBeginImplFrameDeadline(base::TimeTicks deadline
);
206 void SetupNextBeginFrameIfNeeded();
207 void PostBeginRetroFrameIfNeeded();
208 void SetupNextBeginFrameWhenVSyncThrottlingEnabled(bool needs_begin_frame
);
209 void SetupNextBeginFrameWhenVSyncThrottlingDisabled(bool needs_begin_frame
);
210 void SetupPollingMechanisms(bool needs_begin_frame
);
211 void DrawAndSwapIfPossible();
212 void ProcessScheduledActions();
213 bool CanCommitAndActivateBeforeDeadline() const;
214 void AdvanceCommitStateIfPossible();
215 bool IsBeginMainFrameSentOrStarted() const;
216 void SetupSyntheticBeginFrames();
217 void BeginRetroFrame();
218 void BeginUnthrottledFrame();
219 void BeginImplFrame(const BeginFrameArgs
& args
);
220 void OnBeginImplFrameDeadline();
221 void PollForAnticipatedDrawTriggers();
222 void PollToAdvanceCommitState();
224 base::TimeDelta
VSyncInterval() { return vsync_interval_
; }
226 base::TimeDelta
EstimatedParentDrawTime() {
227 return estimated_parent_draw_time_
;
230 bool IsInsideAction(SchedulerStateMachine::Action action
) {
231 return inside_action_
== action
;
234 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
236 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
241 #endif // CC_SCHEDULER_SCHEDULER_H_