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/output/vsync_parameter_observer.h"
18 #include "cc/scheduler/begin_frame_source.h"
19 #include "cc/scheduler/delay_based_time_source.h"
20 #include "cc/scheduler/draw_result.h"
21 #include "cc/scheduler/scheduler_settings.h"
22 #include "cc/scheduler/scheduler_state_machine.h"
25 namespace trace_event
{
26 class ConvertableToTraceFormat
;
28 class SingleThreadTaskRunner
;
33 class SchedulerClient
{
35 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) = 0;
36 virtual void ScheduledActionSendBeginMainFrame() = 0;
37 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() = 0;
38 virtual DrawResult
ScheduledActionDrawAndSwapForced() = 0;
39 virtual void ScheduledActionAnimate() = 0;
40 virtual void ScheduledActionCommit() = 0;
41 virtual void ScheduledActionActivateSyncTree() = 0;
42 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
43 virtual void ScheduledActionPrepareTiles() = 0;
44 virtual void ScheduledActionInvalidateOutputSurface() = 0;
45 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) = 0;
46 virtual base::TimeDelta
DrawDurationEstimate() = 0;
47 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() = 0;
48 virtual base::TimeDelta
CommitToActivateDurationEstimate() = 0;
49 // TODO(sunnyps): Rename DidBeginImplFrameDeadline to DidFinishImplFrame.
50 virtual void DidBeginImplFrameDeadline() = 0;
51 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) = 0;
52 virtual void SendBeginMainFrameNotExpectedSoon() = 0;
55 virtual ~SchedulerClient() {}
59 // This class exists to allow tests to override the frame source construction.
60 // A virtual method can't be used as this needs to happen in the constructor
61 // (see C++ FAQ / Section 23 - http://goo.gl/fnrwom for why).
62 // This class exists solely long enough to construct the frame sources.
63 class CC_EXPORT SchedulerFrameSourcesConstructor
{
65 virtual ~SchedulerFrameSourcesConstructor() {}
66 virtual BeginFrameSource
* ConstructPrimaryFrameSource(Scheduler
* scheduler
);
67 virtual BeginFrameSource
* ConstructBackgroundFrameSource(
68 Scheduler
* scheduler
);
69 virtual BeginFrameSource
* ConstructUnthrottledFrameSource(
70 Scheduler
* scheduler
);
73 SchedulerFrameSourcesConstructor() {}
75 friend class Scheduler
;
78 class CC_EXPORT Scheduler
: public BeginFrameObserverMixIn
{
80 static scoped_ptr
<Scheduler
> Create(
81 SchedulerClient
* client
,
82 const SchedulerSettings
& scheduler_settings
,
83 int layer_tree_host_id
,
84 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
85 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
86 SchedulerFrameSourcesConstructor frame_sources_constructor
;
87 return make_scoped_ptr(new Scheduler(client
,
91 external_begin_frame_source
.Pass(),
92 &frame_sources_constructor
));
95 ~Scheduler() override
;
97 // BeginFrameObserverMixin
98 bool OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) override
;
100 void OnDrawForOutputSurface();
102 const SchedulerSettings
& settings() const { return settings_
; }
104 void CommitVSyncParameters(base::TimeTicks timebase
,
105 base::TimeDelta interval
);
106 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
);
110 void SetVisible(bool visible
);
111 void SetCanDraw(bool can_draw
);
112 void NotifyReadyToActivate();
113 void NotifyReadyToDraw();
114 void SetThrottleFrameProduction(bool throttle
);
116 void SetNeedsCommit();
118 void SetNeedsRedraw();
120 void SetNeedsAnimate();
122 void SetNeedsPrepareTiles();
124 void SetWaitForReadyToDraw();
126 void SetMaxSwapsPending(int max
);
127 void DidSwapBuffers();
128 void DidSwapBuffersComplete();
130 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
132 void NotifyReadyToCommit();
133 void BeginMainFrameAborted(CommitEarlyOutReason reason
);
135 void DidPrepareTiles();
136 void DidLoseOutputSurface();
137 void DidCreateAndInitializeOutputSurface();
139 // Tests do not want to shut down until all possible BeginMainFrames have
140 // occured to prevent flakiness.
141 bool MainFrameForTestingWillHappen() const {
142 return state_machine_
.CommitPending() ||
143 state_machine_
.CouldSendBeginMainFrame();
146 bool CommitPending() const { return state_machine_
.CommitPending(); }
147 bool RedrawPending() const { return state_machine_
.RedrawPending(); }
148 bool PrepareTilesPending() const {
149 return state_machine_
.PrepareTilesPending();
151 bool MainThreadIsInHighLatencyMode() const {
152 return state_machine_
.MainThreadIsInHighLatencyMode();
154 bool BeginImplFrameDeadlinePending() const {
155 return !begin_impl_frame_deadline_task_
.IsCancelled();
158 base::TimeTicks
AnticipatedDrawTime() const;
160 void NotifyBeginMainFrameStarted();
162 base::TimeTicks
LastBeginImplFrameTime();
164 void SetDeferCommits(bool defer_commits
);
166 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValue() const;
167 void AsValueInto(base::trace_event::TracedValue
* value
) const override
;
169 void SetContinuousPainting(bool continuous_painting
) {
170 state_machine_
.SetContinuousPainting(continuous_painting
);
173 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
175 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
178 Scheduler(SchedulerClient
* client
,
179 const SchedulerSettings
& scheduler_settings
,
180 int layer_tree_host_id
,
181 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
182 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
,
183 SchedulerFrameSourcesConstructor
* frame_sources_constructor
);
185 // virtual for testing - Don't call these in the constructor or
187 virtual base::TimeTicks
Now() const;
189 scoped_ptr
<BeginFrameSourceMultiplexer
> frame_source_
;
190 BeginFrameSource
* primary_frame_source_
;
191 BeginFrameSource
* background_frame_source_
;
192 BeginFrameSource
* unthrottled_frame_source_
;
194 // Storage when frame sources are internal
195 scoped_ptr
<BeginFrameSource
> primary_frame_source_internal_
;
196 scoped_ptr
<SyntheticBeginFrameSource
> background_frame_source_internal_
;
197 scoped_ptr
<BeginFrameSource
> unthrottled_frame_source_internal_
;
199 VSyncParameterObserver
* vsync_observer_
;
200 base::TimeDelta authoritative_vsync_interval_
;
201 base::TimeTicks last_vsync_timebase_
;
203 bool throttle_frame_production_
;
205 const SchedulerSettings settings_
;
206 SchedulerClient
* client_
;
207 int layer_tree_host_id_
;
208 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
210 base::TimeDelta estimated_parent_draw_time_
;
212 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
213 BeginFrameArgs begin_impl_frame_args_
;
214 SchedulerStateMachine::BeginImplFrameDeadlineMode
215 begin_impl_frame_deadline_mode_
;
217 base::Closure begin_retro_frame_closure_
;
218 base::Closure begin_impl_frame_deadline_closure_
;
219 base::Closure advance_commit_state_closure_
;
220 base::CancelableClosure begin_retro_frame_task_
;
221 base::CancelableClosure begin_impl_frame_deadline_task_
;
222 base::CancelableClosure advance_commit_state_task_
;
224 SchedulerStateMachine state_machine_
;
225 bool inside_process_scheduled_actions_
;
226 SchedulerStateMachine::Action inside_action_
;
229 void ScheduleBeginImplFrameDeadline();
230 void ScheduleBeginImplFrameDeadlineIfNeeded();
231 void SetupNextBeginFrameIfNeeded();
232 void PostBeginRetroFrameIfNeeded();
233 void SetupPollingMechanisms();
234 void DrawAndSwapIfPossible();
235 void ProcessScheduledActions();
236 bool CanCommitAndActivateBeforeDeadline() const;
237 void AdvanceCommitStateIfPossible();
238 bool IsBeginMainFrameSentOrStarted() const;
239 void BeginRetroFrame();
240 void BeginImplFrameWithDeadline(const BeginFrameArgs
& args
);
241 void BeginImplFrameSynchronous(const BeginFrameArgs
& args
);
242 void BeginImplFrame(const BeginFrameArgs
& args
);
243 void FinishImplFrame();
244 void OnBeginImplFrameDeadline();
245 void PollToAdvanceCommitState();
246 void UpdateActiveFrameSource();
248 base::TimeDelta
EstimatedParentDrawTime() {
249 return estimated_parent_draw_time_
;
252 bool IsInsideAction(SchedulerStateMachine::Action action
) {
253 return inside_action_
== action
;
256 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
258 friend class SchedulerFrameSourcesConstructor
;
259 friend class TestSchedulerFrameSourcesConstructor
;
261 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
266 #endif // CC_SCHEDULER_SCHEDULER_H_