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/power_monitor/power_monitor.h"
15 #include "base/power_monitor/power_observer.h"
16 #include "base/time/time.h"
17 #include "cc/base/cc_export.h"
18 #include "cc/output/begin_frame_args.h"
19 #include "cc/output/vsync_parameter_observer.h"
20 #include "cc/scheduler/begin_frame_source.h"
21 #include "cc/scheduler/delay_based_time_source.h"
22 #include "cc/scheduler/draw_result.h"
23 #include "cc/scheduler/scheduler_settings.h"
24 #include "cc/scheduler/scheduler_state_machine.h"
28 class ConvertableToTraceFormat
;
30 class SingleThreadTaskRunner
;
35 class SchedulerClient
{
37 virtual BeginFrameSource
* ExternalBeginFrameSource() = 0;
38 virtual void WillBeginImplFrame(const BeginFrameArgs
& args
) = 0;
39 virtual void ScheduledActionSendBeginMainFrame() = 0;
40 virtual DrawResult
ScheduledActionDrawAndSwapIfPossible() = 0;
41 virtual DrawResult
ScheduledActionDrawAndSwapForced() = 0;
42 virtual void ScheduledActionAnimate() = 0;
43 virtual void ScheduledActionCommit() = 0;
44 virtual void ScheduledActionUpdateVisibleTiles() = 0;
45 virtual void ScheduledActionActivateSyncTree() = 0;
46 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
47 virtual void ScheduledActionManageTiles() = 0;
48 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time
) = 0;
49 virtual base::TimeDelta
DrawDurationEstimate() = 0;
50 virtual base::TimeDelta
BeginMainFrameToCommitDurationEstimate() = 0;
51 virtual base::TimeDelta
CommitToActivateDurationEstimate() = 0;
52 virtual void DidBeginImplFrameDeadline() = 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
* ConstructBackgroundFrameSource(
68 Scheduler
* scheduler
);
71 SchedulerFrameSourcesConstructor() {}
73 friend class Scheduler
;
76 class CC_EXPORT Scheduler
: public BeginFrameObserverMixIn
,
77 public base::PowerObserver
{
79 static scoped_ptr
<Scheduler
> Create(
80 SchedulerClient
* client
,
81 const SchedulerSettings
& scheduler_settings
,
82 int layer_tree_host_id
,
83 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
84 base::PowerMonitor
* power_monitor
) {
85 SchedulerFrameSourcesConstructor frame_sources_constructor
;
86 return make_scoped_ptr(new Scheduler(client
,
91 &frame_sources_constructor
));
94 ~Scheduler() override
;
96 // base::PowerObserver method.
97 void OnPowerStateChange(bool on_battery_power
) override
;
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();
111 void SetNeedsCommit();
113 void SetNeedsRedraw();
115 void SetNeedsAnimate();
117 void SetNeedsManageTiles();
119 void SetMaxSwapsPending(int max
);
120 void DidSwapBuffers();
121 void SetSwapUsedIncompleteTile(bool used_incomplete_tile
);
122 void DidSwapBuffersComplete();
124 void SetImplLatencyTakesPriority(bool impl_latency_takes_priority
);
126 void NotifyReadyToCommit();
127 void BeginMainFrameAborted(bool did_handle
);
129 void DidManageTiles();
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 ManageTilesPending() const {
143 return state_machine_
.ManageTilesPending();
145 bool MainThreadIsInHighLatencyMode() const {
146 return state_machine_
.MainThreadIsInHighLatencyMode();
148 bool BeginImplFrameDeadlinePending() const {
149 return !begin_impl_frame_deadline_task_
.IsCancelled();
152 bool WillDrawIfNeeded() const;
154 base::TimeTicks
AnticipatedDrawTime() const;
156 void NotifyBeginMainFrameStarted();
158 base::TimeTicks
LastBeginImplFrameTime();
160 scoped_refptr
<base::debug::ConvertableToTraceFormat
> AsValue() const;
161 void AsValueInto(base::debug::TracedValue
* value
) const override
;
163 void SetContinuousPainting(bool continuous_painting
) {
164 state_machine_
.SetContinuousPainting(continuous_painting
);
167 // BeginFrameObserverMixin
168 bool OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) override
;
171 Scheduler(SchedulerClient
* client
,
172 const SchedulerSettings
& scheduler_settings
,
173 int layer_tree_host_id
,
174 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
175 base::PowerMonitor
* power_monitor
,
176 SchedulerFrameSourcesConstructor
* frame_sources_constructor
);
178 // virtual for testing - Don't call these in the constructor or
180 virtual base::TimeTicks
Now() const;
182 scoped_ptr
<BeginFrameSourceMultiplexer
> frame_source_
;
183 BeginFrameSource
* primary_frame_source_
;
184 BeginFrameSource
* background_frame_source_
;
186 // Storage when frame sources are internal
187 scoped_ptr
<BeginFrameSource
> primary_frame_source_internal_
;
188 scoped_ptr
<SyntheticBeginFrameSource
> background_frame_source_internal_
;
190 VSyncParameterObserver
* vsync_observer_
;
192 const SchedulerSettings settings_
;
193 SchedulerClient
* client_
;
194 int layer_tree_host_id_
;
195 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
197 base::PowerMonitor
* power_monitor_
;
199 base::TimeDelta estimated_parent_draw_time_
;
201 bool begin_retro_frame_posted_
;
202 std::deque
<BeginFrameArgs
> begin_retro_frame_args_
;
203 BeginFrameArgs begin_impl_frame_args_
;
205 base::Closure begin_retro_frame_closure_
;
206 base::Closure begin_unthrottled_frame_closure_
;
208 base::Closure begin_impl_frame_deadline_closure_
;
209 base::Closure poll_for_draw_triggers_closure_
;
210 base::Closure advance_commit_state_closure_
;
211 base::CancelableClosure begin_impl_frame_deadline_task_
;
212 base::CancelableClosure poll_for_draw_triggers_task_
;
213 base::CancelableClosure advance_commit_state_task_
;
215 SchedulerStateMachine state_machine_
;
216 bool inside_process_scheduled_actions_
;
217 SchedulerStateMachine::Action inside_action_
;
220 base::TimeTicks
AdjustedBeginImplFrameDeadline(
221 const BeginFrameArgs
& args
,
222 base::TimeDelta draw_duration_estimate
) const;
223 void ScheduleBeginImplFrameDeadline(base::TimeTicks deadline
);
224 void SetupNextBeginFrameIfNeeded();
225 void PostBeginRetroFrameIfNeeded();
226 void SetupPollingMechanisms(bool needs_begin_frame
);
227 void DrawAndSwapIfPossible();
228 void ProcessScheduledActions();
229 bool CanCommitAndActivateBeforeDeadline() const;
230 void AdvanceCommitStateIfPossible();
231 bool IsBeginMainFrameSentOrStarted() const;
232 void BeginRetroFrame();
233 void BeginImplFrame(const BeginFrameArgs
& args
);
234 void OnBeginImplFrameDeadline();
235 void PollForAnticipatedDrawTriggers();
236 void PollToAdvanceCommitState();
237 void SetupPowerMonitoring();
238 void TeardownPowerMonitoring();
240 base::TimeDelta
EstimatedParentDrawTime() {
241 return estimated_parent_draw_time_
;
244 bool IsInsideAction(SchedulerStateMachine::Action action
) {
245 return inside_action_
== action
;
248 base::WeakPtrFactory
<Scheduler
> weak_factory_
;
250 friend class SchedulerFrameSourcesConstructor
;
251 friend class TestSchedulerFrameSourcesConstructor
;
253 DISALLOW_COPY_AND_ASSIGN(Scheduler
);
258 #endif // CC_SCHEDULER_SCHEDULER_H_