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 virtual void DidFinishImplFrame() = 0;
50 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) = 0;
51 virtual void SendBeginMainFrameNotExpectedSoon() = 0;
54 virtual ~SchedulerClient() {}
58 // This class exists to allow tests to override the frame source construction.
59 // A virtual method can't be used as this needs to happen in the constructor
60 // (see C++ FAQ / Section 23 - http://goo.gl/fnrwom for why).
61 // This class exists solely long enough to construct the frame sources.
62 class CC_EXPORT SchedulerFrameSourcesConstructor
{
64 virtual ~SchedulerFrameSourcesConstructor() {}
65 virtual BeginFrameSource
* ConstructPrimaryFrameSource(Scheduler
* scheduler
);
66 virtual BeginFrameSource
* ConstructUnthrottledFrameSource(
67 Scheduler
* scheduler
);
70 SchedulerFrameSourcesConstructor() {}
72 friend class Scheduler
;
75 class CC_EXPORT Scheduler
: public BeginFrameObserverMixIn
{
77 static scoped_ptr
<Scheduler
> Create(
78 SchedulerClient
* client
,
79 const SchedulerSettings
& scheduler_settings
,
80 int layer_tree_host_id
,
81 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
82 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
) {
83 SchedulerFrameSourcesConstructor frame_sources_constructor
;
84 return make_scoped_ptr(new Scheduler(client
,
88 external_begin_frame_source
.Pass(),
89 &frame_sources_constructor
));
92 ~Scheduler() override
;
94 // BeginFrameObserverMixin
95 bool OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) override
;
97 void OnDrawForOutputSurface();
99 const SchedulerSettings
& settings() const { return settings_
; }
101 void CommitVSyncParameters(base::TimeTicks timebase
,
102 base::TimeDelta interval
);
103 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
);
107 void SetVisible(bool visible
);
108 void SetCanDraw(bool can_draw
);
109 void NotifyReadyToActivate();
110 void NotifyReadyToDraw();
111 void SetThrottleFrameProduction(bool throttle
);
113 void SetNeedsCommit();
115 void SetNeedsRedraw();
117 void SetNeedsAnimate();
119 void SetNeedsPrepareTiles();
121 void SetWaitForReadyToDraw();
123 void SetMaxSwapsPending(int max
);
124 void DidSwapBuffers();
125 void DidSwapBuffersComplete();
127 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
129 void NotifyReadyToCommit();
130 void BeginMainFrameAborted(CommitEarlyOutReason reason
);
132 void DidPrepareTiles();
133 void DidLoseOutputSurface();
134 void DidCreateAndInitializeOutputSurface();
136 // Tests do not want to shut down until all possible BeginMainFrames have
137 // occured to prevent flakiness.
138 bool MainFrameForTestingWillHappen() const {
139 return state_machine_
.CommitPending() ||
140 state_machine_
.CouldSendBeginMainFrame();
143 bool CommitPending() const { return state_machine_
.CommitPending(); }
144 bool RedrawPending() const { return state_machine_
.RedrawPending(); }
145 bool PrepareTilesPending() const {
146 return state_machine_
.PrepareTilesPending();
148 bool MainThreadIsInHighLatencyMode() const {
149 return state_machine_
.MainThreadIsInHighLatencyMode();
151 bool BeginImplFrameDeadlinePending() const {
152 return !begin_impl_frame_deadline_task_
.IsCancelled();
155 base::TimeTicks
AnticipatedDrawTime() const;
157 void NotifyBeginMainFrameStarted();
159 base::TimeTicks
LastBeginImplFrameTime();
161 void SetDeferCommits(bool defer_commits
);
163 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValue() const;
164 void AsValueInto(base::trace_event::TracedValue
* value
) const override
;
166 void SetContinuousPainting(bool continuous_painting
) {
167 state_machine_
.SetContinuousPainting(continuous_painting
);
170 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
171 void SetVideoNeedsBeginFrames(bool video_needs_begin_frames
);
173 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
176 Scheduler(SchedulerClient
* client
,
177 const SchedulerSettings
& scheduler_settings
,
178 int layer_tree_host_id
,
179 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
180 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
,
181 SchedulerFrameSourcesConstructor
* frame_sources_constructor
);
183 // virtual for testing - Don't call these in the constructor or
185 virtual base::TimeTicks
Now() const;
187 scoped_ptr
<BeginFrameSourceMultiplexer
> frame_source_
;
188 BeginFrameSource
* primary_frame_source_
;
189 BeginFrameSource
* unthrottled_frame_source_
;
191 // Storage when frame sources are internal
192 scoped_ptr
<BeginFrameSource
> primary_frame_source_internal_
;
193 scoped_ptr
<BeginFrameSource
> unthrottled_frame_source_internal_
;
195 VSyncParameterObserver
* vsync_observer_
;
196 base::TimeDelta authoritative_vsync_interval_
;
197 base::TimeTicks last_vsync_timebase_
;
199 bool throttle_frame_production_
;
201 const SchedulerSettings settings_
;
202 SchedulerClient
* client_
;
203 int layer_tree_host_id_
;
204 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
206 base::TimeDelta estimated_parent_draw_time_
;
208 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
209 BeginFrameArgs begin_impl_frame_args_
;
210 SchedulerStateMachine::BeginImplFrameDeadlineMode
211 begin_impl_frame_deadline_mode_
;
213 base::Closure begin_retro_frame_closure_
;
214 base::Closure begin_impl_frame_deadline_closure_
;
215 base::Closure advance_commit_state_closure_
;
216 base::CancelableClosure begin_retro_frame_task_
;
217 base::CancelableClosure begin_impl_frame_deadline_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 ScheduleBeginImplFrameDeadlineIfNeeded();
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 BeginImplFrameWithDeadline(const BeginFrameArgs
& args
);
237 void BeginImplFrameSynchronous(const BeginFrameArgs
& args
);
238 void BeginImplFrame();
239 void FinishImplFrame();
240 void OnBeginImplFrameDeadline();
241 void PollToAdvanceCommitState();
243 base::TimeDelta
EstimatedParentDrawTime() {
244 return estimated_parent_draw_time_
;
247 bool IsInsideAction(SchedulerStateMachine::Action action
) {
248 return inside_action_
== action
;
251 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
253 friend class SchedulerFrameSourcesConstructor
;
254 friend class TestSchedulerFrameSourcesConstructor
;
256 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
261 #endif // CC_SCHEDULER_SCHEDULER_H_