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 SetContinuousPainting(bool continuous_painting
) {
138 state_machine_
.SetContinuousPainting(continuous_painting
);
141 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
);
142 void SetVideoNeedsBeginFrames(bool video_needs_begin_frames
);
144 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
147 Scheduler(SchedulerClient
* client
,
148 const SchedulerSettings
& scheduler_settings
,
149 int layer_tree_host_id
,
150 base::SingleThreadTaskRunner
* task_runner
,
151 BeginFrameSource
* external_frame_source
,
152 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_frame_source
,
153 scoped_ptr
<BackToBackBeginFrameSource
> unthrottled_frame_source
,
154 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history
);
156 // Virtual for testing.
157 virtual base::TimeTicks
Now() const;
159 const SchedulerSettings settings_
;
160 SchedulerClient
* client_
;
161 int layer_tree_host_id_
;
162 base::SingleThreadTaskRunner
* task_runner_
;
163 BeginFrameSource
* external_frame_source_
;
164 scoped_ptr
<SyntheticBeginFrameSource
> synthetic_frame_source_
;
165 scoped_ptr
<BackToBackBeginFrameSource
> unthrottled_frame_source_
;
167 scoped_ptr
<BeginFrameSourceMultiplexer
> frame_source_
;
168 bool throttle_frame_production_
;
170 base::TimeDelta authoritative_vsync_interval_
;
171 base::TimeTicks last_vsync_timebase_
;
173 scoped_ptr
<CompositorTimingHistory
> compositor_timing_history_
;
174 base::TimeDelta estimated_parent_draw_time_
;
176 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
177 SchedulerStateMachine::BeginImplFrameDeadlineMode
178 begin_impl_frame_deadline_mode_
;
179 BeginFrameTracker begin_impl_frame_tracker_
;
181 base::Closure begin_retro_frame_closure_
;
182 base::Closure begin_impl_frame_deadline_closure_
;
183 base::CancelableClosure begin_retro_frame_task_
;
184 base::CancelableClosure begin_impl_frame_deadline_task_
;
186 SchedulerStateMachine state_machine_
;
187 bool inside_process_scheduled_actions_
;
188 SchedulerStateMachine::Action inside_action_
;
191 void ScheduleBeginImplFrameDeadline();
192 void ScheduleBeginImplFrameDeadlineIfNeeded();
193 void SetupNextBeginFrameIfNeeded();
194 void PostBeginRetroFrameIfNeeded();
195 void DrawAndSwapIfPossible();
196 void DrawAndSwapForced();
197 void ProcessScheduledActions();
198 void UpdateCompositorTimingHistoryRecordingEnabled();
199 bool ShouldRecoverMainLatency(const BeginFrameArgs
& args
) const;
200 bool ShouldRecoverImplLatency(const BeginFrameArgs
& args
) const;
201 bool CanCommitAndActivateBeforeDeadline(const BeginFrameArgs
& args
) const;
202 void AdvanceCommitStateIfPossible();
203 bool IsBeginMainFrameSentOrStarted() const;
204 void BeginRetroFrame();
205 void BeginImplFrameWithDeadline(const BeginFrameArgs
& args
);
206 void BeginImplFrameSynchronous(const BeginFrameArgs
& args
);
207 void BeginImplFrame(const BeginFrameArgs
& args
);
208 void FinishImplFrame();
209 void OnBeginImplFrameDeadline();
210 void PollToAdvanceCommitState();
212 base::TimeDelta
EstimatedParentDrawTime() {
213 return estimated_parent_draw_time_
;
216 bool IsInsideAction(SchedulerStateMachine::Action action
) {
217 return inside_action_
== action
;
220 BeginFrameSource
* primary_frame_source() {
221 if (settings_
.use_external_begin_frame_source
) {
222 DCHECK(external_frame_source_
);
223 return external_frame_source_
;
225 return synthetic_frame_source_
.get();
228 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
230 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
235 #endif // CC_SCHEDULER_SCHEDULER_H_