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"
23 class SingleThreadTaskRunner
;
28 class SchedulerClient
{
30 virtual void SetNeedsBeginFrame(bool enable
) = 0;
31 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) = 0;
32 virtual void ScheduledActionSendBeginMainFrame() = 0;
33 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() = 0;
34 virtual DrawResult
ScheduledActionDrawAndSwapForced() = 0;
35 virtual void ScheduledActionAnimate() = 0;
36 virtual void ScheduledActionCommit() = 0;
37 virtual void ScheduledActionUpdateVisibleTiles() = 0;
38 virtual void ScheduledActionActivateSyncTree() = 0;
39 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
40 virtual void ScheduledActionManageTiles() = 0;
41 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) = 0;
42 virtual base::TimeDelta
DrawDurationEstimate() = 0;
43 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() = 0;
44 virtual base::TimeDelta
CommitToActivateDurationEstimate() = 0;
45 virtual void DidBeginImplFrameDeadline() = 0;
48 virtual ~SchedulerClient() {}
51 class CC_EXPORT Scheduler
{
53 static scoped_ptr
<Scheduler
> Create(
54 SchedulerClient
* client
,
55 const SchedulerSettings
& scheduler_settings
,
56 int layer_tree_host_id
,
57 const scoped_refptr
<base::SingleThreadTaskRunner
>& impl_task_runner
) {
58 return make_scoped_ptr(new Scheduler(
59 client
, scheduler_settings
, layer_tree_host_id
, impl_task_runner
));
64 const SchedulerSettings
& settings() const { return settings_
; }
66 void CommitVSyncParameters(base::TimeTicks timebase
,
67 base::TimeDelta interval
);
68 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
);
72 void SetVisible(bool visible
);
73 void SetCanDraw(bool can_draw
);
74 void NotifyReadyToActivate();
76 void SetNeedsCommit();
78 void SetNeedsRedraw();
80 void SetNeedsAnimate();
82 void SetNeedsManageTiles();
84 void SetMaxSwapsPending(int max
);
85 void DidSwapBuffers();
86 void SetSwapUsedIncompleteTile(bool used_incomplete_tile
);
87 void DidSwapBuffersComplete();
89 void SetSmoothnessTakesPriority(bool smoothness_takes_priority
);
91 void NotifyReadyToCommit();
92 void BeginMainFrameAborted(bool did_handle
);
94 void DidManageTiles();
95 void DidLoseOutputSurface();
96 void DidCreateAndInitializeOutputSurface();
98 bool CommitPending() const { return state_machine_
.CommitPending(); }
99 bool RedrawPending() const { return state_machine_
.RedrawPending(); }
100 bool ManageTilesPending() const {
101 return state_machine_
.ManageTilesPending();
103 bool MainThreadIsInHighLatencyMode() const {
104 return state_machine_
.MainThreadIsInHighLatencyMode();
106 bool BeginImplFrameDeadlinePending() const {
107 return !begin_impl_frame_deadline_task_
.IsCancelled();
110 bool WillDrawIfNeeded() const;
112 base::TimeTicks
AnticipatedDrawTime() const;
114 void NotifyBeginMainFrameStarted();
116 base::TimeTicks
LastBeginImplFrameTime();
117 base::TimeDelta
VSyncInterval() { return vsync_interval_
; }
118 base::TimeDelta
EstimatedParentDrawTime() {
119 return estimated_parent_draw_time_
;
122 void BeginFrame(const BeginFrameArgs
& args
);
123 void PostBeginRetroFrame();
124 void BeginRetroFrame();
125 void BeginUnthrottledFrame();
127 void BeginImplFrame(const BeginFrameArgs
& args
);
128 void OnBeginImplFrameDeadline();
129 void PollForAnticipatedDrawTriggers();
130 void PollToAdvanceCommitState();
132 scoped_ptr
<base::Value
> AsValue() const;
134 bool IsInsideAction(SchedulerStateMachine::Action action
) {
135 return inside_action_
== action
;
138 bool IsBeginMainFrameSent() const;
139 void SetContinuousPainting(bool continuous_painting
) {
140 state_machine_
.SetContinuousPainting(continuous_painting
);
144 class CC_EXPORT SyntheticBeginFrameSource
: public TimeSourceClient
{
146 SyntheticBeginFrameSource(Scheduler
* scheduler
,
147 base::SingleThreadTaskRunner
* task_runner
);
148 virtual ~SyntheticBeginFrameSource();
150 // Updates the phase and frequency of the timer.
151 void CommitVSyncParameters(base::TimeTicks timebase
,
152 base::TimeDelta interval
);
154 // Activates future BeginFrames and, if activating, pushes the most
155 // recently missed BeginFrame to the back of a retroactive queue.
156 void SetNeedsBeginFrame(bool needs_begin_frame
,
157 std::deque
<BeginFrameArgs
>* begin_retro_frame_args
);
159 bool IsActive() const;
161 // TimeSourceClient implementation of OnTimerTick triggers a BeginFrame.
162 virtual void OnTimerTick() OVERRIDE
;
164 scoped_ptr
<base::Value
> AsValue() const;
167 BeginFrameArgs
CreateSyntheticBeginFrameArgs(base::TimeTicks frame_time
);
169 Scheduler
* scheduler_
;
170 scoped_refptr
<DelayBasedTimeSource
> time_source_
;
174 SchedulerClient
* client
,
175 const SchedulerSettings
& scheduler_settings
,
176 int layer_tree_host_id
,
177 const scoped_refptr
<base::SingleThreadTaskRunner
>& impl_task_runner
);
179 const SchedulerSettings settings_
;
180 SchedulerClient
* client_
;
181 int layer_tree_host_id_
;
182 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner_
;
184 base::TimeDelta vsync_interval_
;
185 base::TimeDelta estimated_parent_draw_time_
;
187 bool last_set_needs_begin_frame_
;
188 bool begin_unthrottled_frame_posted_
;
189 bool begin_retro_frame_posted_
;
190 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
191 BeginFrameArgs begin_impl_frame_args_
;
193 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_begin_frame_source_
;
195 base::Closure begin_retro_frame_closure_
;
196 base::Closure begin_unthrottled_frame_closure_
;
198 base::Closure begin_impl_frame_deadline_closure_
;
199 base::Closure poll_for_draw_triggers_closure_
;
200 base::Closure advance_commit_state_closure_
;
201 base::CancelableClosure begin_impl_frame_deadline_task_
;
202 base::CancelableClosure poll_for_draw_triggers_task_
;
203 base::CancelableClosure advance_commit_state_task_
;
205 SchedulerStateMachine state_machine_
;
206 bool inside_process_scheduled_actions_
;
207 SchedulerStateMachine::Action inside_action_
;
210 base::TimeTicks
AdjustedBeginImplFrameDeadline(
211 const BeginFrameArgs
& args
,
212 base::TimeDelta draw_duration_estimate
) const;
213 void ScheduleBeginImplFrameDeadline(base::TimeTicks deadline
);
214 void SetupNextBeginFrameIfNeeded();
215 void PostBeginRetroFrameIfNeeded();
216 void SetupNextBeginFrameWhenVSyncThrottlingEnabled(bool needs_begin_frame
);
217 void SetupNextBeginFrameWhenVSyncThrottlingDisabled(bool needs_begin_frame
);
218 void SetupPollingMechanisms(bool needs_begin_frame
);
219 void DrawAndSwapIfPossible();
220 void ProcessScheduledActions();
221 bool CanCommitAndActivateBeforeDeadline() const;
222 void AdvanceCommitStateIfPossible();
223 bool IsBeginMainFrameSentOrStarted() const;
224 void SetupSyntheticBeginFrames();
226 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
228 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
233 #endif // CC_SCHEDULER_SCHEDULER_H_