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/begin_frame_source.h"
18 #include "cc/scheduler/begin_frame_tracker.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 CompositorTimingHistory
;
35 class SchedulerClient
{
37 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) = 0;
38 virtual void ScheduledActionSendBeginMainFrame() = 0;
39 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() = 0;
40 virtual DrawResult
ScheduledActionDrawAndSwapForced() = 0;
41 virtual void ScheduledActionAnimate() = 0;
42 virtual void ScheduledActionCommit() = 0;
43 virtual void ScheduledActionActivateSyncTree() = 0;
44 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
45 virtual void ScheduledActionPrepareTiles() = 0;
46 virtual void ScheduledActionInvalidateOutputSurface() = 0;
47 virtual void DidFinishImplFrame() = 0;
48 virtual void SendBeginFramesToChildren(const BeginFrameArgs
& args
) = 0;
49 virtual void SendBeginMainFrameNotExpectedSoon() = 0;
52 virtual ~SchedulerClient() {}
55 class CC_EXPORT Scheduler
: public BeginFrameObserverBase
{
57 static scoped_ptr
<Scheduler
> Create(
58 SchedulerClient
* client
,
59 const SchedulerSettings
& scheduler_settings
,
60 int layer_tree_host_id
,
61 base::SingleThreadTaskRunner
* task_runner
,
62 BeginFrameSource
* external_frame_source
,
63 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history
);
65 ~Scheduler() override
;
67 // BeginFrameObserverBase
68 bool OnBeginFrameDerivedImpl(const BeginFrameArgs
& args
) override
;
70 void OnDrawForOutputSurface();
72 const SchedulerSettings
& settings() const { return settings_
; }
74 void CommitVSyncParameters(base::TimeTicks timebase
,
75 base::TimeDelta interval
);
76 void SetEstimatedParentDrawTime(base::TimeDelta draw_time
);
80 void SetVisible(bool visible
);
81 void SetCanDraw(bool can_draw
);
82 void NotifyReadyToActivate();
83 void NotifyReadyToDraw();
84 void SetThrottleFrameProduction(bool throttle
);
86 void SetNeedsBeginMainFrame();
88 void SetNeedsRedraw();
90 void SetNeedsAnimate();
92 void SetNeedsPrepareTiles();
94 void SetMaxSwapsPending(int max
);
95 void DidSwapBuffers();
96 void DidSwapBuffersComplete();
98 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
100 void NotifyReadyToCommit();
101 void BeginMainFrameAborted(CommitEarlyOutReason reason
);
104 void WillPrepareTiles();
105 void DidPrepareTiles();
106 void DidLoseOutputSurface();
107 void DidCreateAndInitializeOutputSurface();
109 // Tests do not want to shut down until all possible BeginMainFrames have
110 // occured to prevent flakiness.
111 bool MainFrameForTestingWillHappen() const {
112 return state_machine_
.CommitPending() ||
113 state_machine_
.CouldSendBeginMainFrame();
116 bool CommitPending() const { return state_machine_
.CommitPending(); }
117 bool RedrawPending() const { return state_machine_
.RedrawPending(); }
118 bool PrepareTilesPending() const {
119 return state_machine_
.PrepareTilesPending();
121 bool BeginImplFrameDeadlinePending() const {
122 return !begin_impl_frame_deadline_task_
.IsCancelled();
124 bool ImplLatencyTakesPriority() const {
125 return state_machine_
.impl_latency_takes_priority();
128 void NotifyBeginMainFrameStarted();
130 base::TimeTicks
LastBeginImplFrameTime();
132 void SetDeferCommits(bool defer_commits
);
134 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> AsValue() const;
135 void AsValueInto(base::trace_event::TracedValue
* value
) const override
;
137 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
138 void SetVideoNeedsBeginFrames(bool video_needs_begin_frames
);
140 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
143 Scheduler(SchedulerClient
* client
,
144 const SchedulerSettings
& scheduler_settings
,
145 int layer_tree_host_id
,
146 base::SingleThreadTaskRunner
* task_runner
,
147 BeginFrameSource
* external_frame_source
,
148 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_frame_source
,
149 scoped_ptr
<BackToBackBeginFrameSource
> unthrottled_frame_source
,
150 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history
);
152 // Virtual for testing.
153 virtual base::TimeTicks
Now() const;
155 const SchedulerSettings settings_
;
156 SchedulerClient
* client_
;
157 int layer_tree_host_id_
;
158 base::SingleThreadTaskRunner
* task_runner_
;
159 BeginFrameSource
* external_frame_source_
;
160 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_frame_source_
;
161 scoped_ptr
<BackToBackBeginFrameSource
> unthrottled_frame_source_
;
163 scoped_ptr
<BeginFrameSourceMultiplexer
> frame_source_
;
164 bool throttle_frame_production_
;
166 base::TimeDelta authoritative_vsync_interval_
;
167 base::TimeTicks last_vsync_timebase_
;
169 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history_
;
170 base::TimeDelta estimated_parent_draw_time_
;
172 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
173 SchedulerStateMachine::BeginImplFrameDeadlineMode
174 begin_impl_frame_deadline_mode_
;
175 BeginFrameTracker begin_impl_frame_tracker_
;
177 base::Closure begin_retro_frame_closure_
;
178 base::Closure begin_impl_frame_deadline_closure_
;
179 base::CancelableClosure begin_retro_frame_task_
;
180 base::CancelableClosure begin_impl_frame_deadline_task_
;
182 SchedulerStateMachine state_machine_
;
183 bool inside_process_scheduled_actions_
;
184 SchedulerStateMachine::Action inside_action_
;
187 void ScheduleBeginImplFrameDeadline();
188 void ScheduleBeginImplFrameDeadlineIfNeeded();
189 void SetupNextBeginFrameIfNeeded();
190 void PostBeginRetroFrameIfNeeded();
191 void DrawAndSwapIfPossible();
192 void DrawAndSwapForced();
193 void ProcessScheduledActions();
194 void UpdateCompositorTimingHistoryRecordingEnabled();
195 bool ShouldRecoverMainLatency(const BeginFrameArgs
& args
) const;
196 bool ShouldRecoverImplLatency(const BeginFrameArgs
& args
) const;
197 bool CanCommitAndActivateBeforeDeadline(const BeginFrameArgs
& args
) const;
198 void AdvanceCommitStateIfPossible();
199 bool IsBeginMainFrameSentOrStarted() const;
200 void BeginRetroFrame();
201 void BeginImplFrameWithDeadline(const BeginFrameArgs
& args
);
202 void BeginImplFrameSynchronous(const BeginFrameArgs
& args
);
203 void BeginImplFrame(const BeginFrameArgs
& args
);
204 void FinishImplFrame();
205 void OnBeginImplFrameDeadline();
206 void PollToAdvanceCommitState();
208 base::TimeDelta
EstimatedParentDrawTime() {
209 return estimated_parent_draw_time_
;
212 bool IsInsideAction(SchedulerStateMachine::Action action
) {
213 return inside_action_
== action
;
216 BeginFrameSource
* primary_frame_source() {
217 if (settings_
.use_external_begin_frame_source
) {
218 DCHECK(external_frame_source_
);
219 return external_frame_source_
;
221 return synthetic_frame_source_
.get();
224 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
226 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
231 #endif // CC_SCHEDULER_SCHEDULER_H_