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 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;
49 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) = 0;
50 virtual void SendBeginMainFrameNotExpectedSoon() = 0;
53 virtual ~SchedulerClient() {}
57 // This class exists to allow tests to override the frame source construction.
58 // A virtual method can't be used as this needs to happen in the constructor
59 // (see C++ FAQ / Section 23 - http://goo.gl/fnrwom for why).
60 // This class exists solely long enough to construct the frame sources.
61 class CC_EXPORT SchedulerFrameSourcesConstructor
{
63 virtual ~SchedulerFrameSourcesConstructor() {}
64 virtual BeginFrameSource
* ConstructPrimaryFrameSource(Scheduler
* scheduler
);
65 virtual BeginFrameSource
* ConstructBackgroundFrameSource(
66 Scheduler
* scheduler
);
67 virtual BeginFrameSource
* ConstructUnthrottledFrameSource(
68 Scheduler
* scheduler
);
71 SchedulerFrameSourcesConstructor() {}
73 friend class Scheduler
;
76 class CC_EXPORT Scheduler
: public BeginFrameObserverMixIn
{
78 static scoped_ptr
<Scheduler
> Create(
79 SchedulerClient
* client
,
80 const SchedulerSettings
& scheduler_settings
,
81 int layer_tree_host_id
,
82 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
83 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
84 SchedulerFrameSourcesConstructor frame_sources_constructor
;
85 return make_scoped_ptr(new Scheduler(client
,
89 external_begin_frame_source
.Pass(),
90 &frame_sources_constructor
));
93 ~Scheduler() override
;
95 // BeginFrameObserverMixin
96 bool OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) override
;
98 const SchedulerSettings
& settings() const { return settings_
; }
100 void CommitVSyncParameters(base::TimeTicks timebase
,
101 base::TimeDelta interval
);
102 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
);
106 void SetVisible(bool visible
);
107 void SetCanDraw(bool can_draw
);
108 void NotifyReadyToActivate();
109 void NotifyReadyToDraw();
110 void SetThrottleFrameProduction(bool throttle
);
112 void SetNeedsCommit();
114 void SetNeedsRedraw();
116 void SetNeedsAnimate();
118 void SetNeedsPrepareTiles();
120 void SetMaxSwapsPending(int max
);
121 void DidSwapBuffers();
122 void DidSwapBuffersComplete();
124 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
126 void NotifyReadyToCommit();
127 void BeginMainFrameAborted(CommitEarlyOutReason reason
);
129 void DidPrepareTiles();
130 void DidLoseOutputSurface();
131 void DidCreateAndInitializeOutputSurface();
133 // Tests do not want to shut down until all possible BeginMainFrames have
134 // occured to prevent flakiness.
135 bool MainFrameForTestingWillHappen() const {
136 return state_machine_
.CommitPending() ||
137 state_machine_
.CouldSendBeginMainFrame();
140 bool CommitPending() const { return state_machine_
.CommitPending(); }
141 bool RedrawPending() const { return state_machine_
.RedrawPending(); }
142 bool PrepareTilesPending() const {
143 return state_machine_
.PrepareTilesPending();
145 bool MainThreadIsInHighLatencyMode() const {
146 return state_machine_
.MainThreadIsInHighLatencyMode();
148 bool BeginImplFrameDeadlinePending() const {
149 return !begin_impl_frame_deadline_task_
.IsCancelled();
152 base::TimeTicks
AnticipatedDrawTime() const;
154 void NotifyBeginMainFrameStarted();
156 base::TimeTicks
LastBeginImplFrameTime();
158 void SetDeferCommits(bool defer_commits
);
160 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValue() const;
161 void AsValueInto(base::trace_event::TracedValue
* value
) const override
;
163 void SetContinuousPainting(bool continuous_painting
) {
164 state_machine_
.SetContinuousPainting(continuous_painting
);
167 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
169 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
172 Scheduler(SchedulerClient
* client
,
173 const SchedulerSettings
& scheduler_settings
,
174 int layer_tree_host_id
,
175 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
176 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
,
177 SchedulerFrameSourcesConstructor
* frame_sources_constructor
);
179 // virtual for testing - Don't call these in the constructor or
181 virtual base::TimeTicks
Now() const;
183 scoped_ptr
<BeginFrameSourceMultiplexer
> frame_source_
;
184 BeginFrameSource
* primary_frame_source_
;
185 BeginFrameSource
* background_frame_source_
;
186 BeginFrameSource
* unthrottled_frame_source_
;
188 // Storage when frame sources are internal
189 scoped_ptr
<BeginFrameSource
> primary_frame_source_internal_
;
190 scoped_ptr
<SyntheticBeginFrameSource
> background_frame_source_internal_
;
191 scoped_ptr
<BeginFrameSource
> unthrottled_frame_source_internal_
;
193 VSyncParameterObserver
* vsync_observer_
;
194 base::TimeDelta authoritative_vsync_interval_
;
195 base::TimeTicks last_vsync_timebase_
;
197 bool throttle_frame_production_
;
199 const SchedulerSettings settings_
;
200 SchedulerClient
* client_
;
201 int layer_tree_host_id_
;
202 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
204 base::TimeDelta estimated_parent_draw_time_
;
206 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
207 BeginFrameArgs begin_impl_frame_args_
;
208 SchedulerStateMachine::BeginImplFrameDeadlineMode
209 begin_impl_frame_deadline_mode_
;
211 base::Closure begin_retro_frame_closure_
;
212 base::Closure begin_impl_frame_deadline_closure_
;
213 base::Closure poll_for_draw_triggers_closure_
;
214 base::Closure advance_commit_state_closure_
;
215 base::CancelableClosure begin_retro_frame_task_
;
216 base::CancelableClosure begin_impl_frame_deadline_task_
;
217 base::CancelableClosure poll_for_draw_triggers_task_
;
218 base::CancelableClosure advance_commit_state_task_
;
220 SchedulerStateMachine state_machine_
;
221 bool inside_process_scheduled_actions_
;
222 SchedulerStateMachine::Action inside_action_
;
225 void ScheduleBeginImplFrameDeadline();
226 void RescheduleBeginImplFrameDeadlineIfNeeded();
227 void SetupNextBeginFrameIfNeeded();
228 void PostBeginRetroFrameIfNeeded();
229 void SetupPollingMechanisms();
230 void DrawAndSwapIfPossible();
231 void ProcessScheduledActions();
232 bool CanCommitAndActivateBeforeDeadline() const;
233 void AdvanceCommitStateIfPossible();
234 bool IsBeginMainFrameSentOrStarted() const;
235 void BeginRetroFrame();
236 void BeginImplFrame(const BeginFrameArgs
& args
);
237 void OnBeginImplFrameDeadline();
238 void PollForAnticipatedDrawTriggers();
239 void PollToAdvanceCommitState();
240 void UpdateActiveFrameSource();
242 base::TimeDelta
EstimatedParentDrawTime() {
243 return estimated_parent_draw_time_
;
246 bool IsInsideAction(SchedulerStateMachine::Action action
) {
247 return inside_action_
== action
;
250 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
252 friend class SchedulerFrameSourcesConstructor
;
253 friend class TestSchedulerFrameSourcesConstructor
;
255 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
260 #endif // CC_SCHEDULER_SCHEDULER_H_