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/begin_frame_tracker.h"
20 #include "cc/scheduler/delay_based_time_source.h"
21 #include "cc/scheduler/draw_result.h"
22 #include "cc/scheduler/scheduler_settings.h"
23 #include "cc/scheduler/scheduler_state_machine.h"
26 namespace trace_event
{
27 class ConvertableToTraceFormat
;
29 class SingleThreadTaskRunner
;
34 class SchedulerClient
{
36 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) = 0;
37 virtual void ScheduledActionSendBeginMainFrame() = 0;
38 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() = 0;
39 virtual DrawResult
ScheduledActionDrawAndSwapForced() = 0;
40 virtual void ScheduledActionAnimate() = 0;
41 virtual void ScheduledActionCommit() = 0;
42 virtual void ScheduledActionActivateSyncTree() = 0;
43 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
44 virtual void ScheduledActionPrepareTiles() = 0;
45 virtual void ScheduledActionInvalidateOutputSurface() = 0;
46 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) = 0;
47 virtual base::TimeDelta
DrawDurationEstimate() = 0;
48 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() = 0;
49 virtual base::TimeDelta
CommitToActivateDurationEstimate() = 0;
50 virtual void DidFinishImplFrame() = 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
* 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 void OnDrawForOutputSurface();
100 const SchedulerSettings
& settings() const { return settings_
; }
102 void CommitVSyncParameters(base::TimeTicks timebase
,
103 base::TimeDelta interval
);
104 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
);
108 void SetVisible(bool visible
);
109 void SetCanDraw(bool can_draw
);
110 void NotifyReadyToActivate();
111 void NotifyReadyToDraw();
112 void SetThrottleFrameProduction(bool throttle
);
114 void SetNeedsCommit();
116 void SetNeedsRedraw();
118 void SetNeedsAnimate();
120 void SetNeedsPrepareTiles();
122 void SetWaitForReadyToDraw();
124 void SetMaxSwapsPending(int max
);
125 void DidSwapBuffers();
126 void DidSwapBuffersComplete();
128 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
130 void NotifyReadyToCommit();
131 void BeginMainFrameAborted(CommitEarlyOutReason reason
);
133 void DidPrepareTiles();
134 void DidLoseOutputSurface();
135 void DidCreateAndInitializeOutputSurface();
137 // Tests do not want to shut down until all possible BeginMainFrames have
138 // occured to prevent flakiness.
139 bool MainFrameForTestingWillHappen() const {
140 return state_machine_
.CommitPending() ||
141 state_machine_
.CouldSendBeginMainFrame();
144 bool CommitPending() const { return state_machine_
.CommitPending(); }
145 bool RedrawPending() const { return state_machine_
.RedrawPending(); }
146 bool PrepareTilesPending() const {
147 return state_machine_
.PrepareTilesPending();
149 bool MainThreadIsInHighLatencyMode() const {
150 return state_machine_
.MainThreadIsInHighLatencyMode();
152 bool BeginImplFrameDeadlinePending() const {
153 return !begin_impl_frame_deadline_task_
.IsCancelled();
156 base::TimeTicks
AnticipatedDrawTime() const;
158 void NotifyBeginMainFrameStarted();
160 base::TimeTicks
LastBeginImplFrameTime();
162 void SetDeferCommits(bool defer_commits
);
164 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValue() const;
165 void AsValueInto(base::trace_event::TracedValue
* value
) const override
;
167 void SetContinuousPainting(bool continuous_painting
) {
168 state_machine_
.SetContinuousPainting(continuous_painting
);
171 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
172 void SetVideoNeedsBeginFrames(bool video_needs_begin_frames
);
174 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
177 Scheduler(SchedulerClient
* client
,
178 const SchedulerSettings
& scheduler_settings
,
179 int layer_tree_host_id
,
180 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
181 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
,
182 SchedulerFrameSourcesConstructor
* frame_sources_constructor
);
184 // virtual for testing - Don't call these in the constructor or
186 virtual base::TimeTicks
Now() const;
188 scoped_ptr
<BeginFrameSourceMultiplexer
> frame_source_
;
189 BeginFrameSource
* primary_frame_source_
;
190 BeginFrameSource
* unthrottled_frame_source_
;
192 // Storage when frame sources are internal
193 scoped_ptr
<BeginFrameSource
> primary_frame_source_internal_
;
194 scoped_ptr
<BeginFrameSource
> unthrottled_frame_source_internal_
;
196 VSyncParameterObserver
* vsync_observer_
;
197 base::TimeDelta authoritative_vsync_interval_
;
198 base::TimeTicks last_vsync_timebase_
;
200 bool throttle_frame_production_
;
202 const SchedulerSettings settings_
;
203 SchedulerClient
* client_
;
204 int layer_tree_host_id_
;
205 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
207 base::TimeDelta estimated_parent_draw_time_
;
209 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
210 SchedulerStateMachine::BeginImplFrameDeadlineMode
211 begin_impl_frame_deadline_mode_
;
212 BeginFrameTracker begin_impl_frame_tracker_
;
214 base::Closure begin_retro_frame_closure_
;
215 base::Closure begin_impl_frame_deadline_closure_
;
216 base::Closure advance_commit_state_closure_
;
217 base::CancelableClosure begin_retro_frame_task_
;
218 base::CancelableClosure begin_impl_frame_deadline_task_
;
219 base::CancelableClosure advance_commit_state_task_
;
221 SchedulerStateMachine state_machine_
;
222 bool inside_process_scheduled_actions_
;
223 SchedulerStateMachine::Action inside_action_
;
226 void ScheduleBeginImplFrameDeadline();
227 void ScheduleBeginImplFrameDeadlineIfNeeded();
228 void SetupNextBeginFrameIfNeeded();
229 void PostBeginRetroFrameIfNeeded();
230 void SetupPollingMechanisms();
231 void DrawAndSwapIfPossible();
232 void ProcessScheduledActions();
233 bool CanCommitAndActivateBeforeDeadline() const;
234 void AdvanceCommitStateIfPossible();
235 bool IsBeginMainFrameSentOrStarted() const;
236 void BeginRetroFrame();
237 void BeginImplFrameWithDeadline(const BeginFrameArgs
& args
);
238 void BeginImplFrameSynchronous(const BeginFrameArgs
& args
);
239 void BeginImplFrame(const BeginFrameArgs
& args
);
240 void FinishImplFrame();
241 void OnBeginImplFrameDeadline();
242 void PollToAdvanceCommitState();
244 base::TimeDelta
EstimatedParentDrawTime() {
245 return estimated_parent_draw_time_
;
248 bool IsInsideAction(SchedulerStateMachine::Action action
) {
249 return inside_action_
== action
;
252 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
254 friend class SchedulerFrameSourcesConstructor
;
255 friend class TestSchedulerFrameSourcesConstructor
;
257 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
262 #endif // CC_SCHEDULER_SCHEDULER_H_