1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_SCHEDULER_SCHEDULER_H_
#define CC_SCHEDULER_SCHEDULER_H_
#include <deque>
#include <string>
#include "base/basictypes.h"
#include "base/cancelable_callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_observer.h"
#include "base/time/time.h"
#include "cc/base/cc_export.h"
#include "cc/output/begin_frame_args.h"
#include "cc/output/vsync_parameter_observer.h"
#include "cc/scheduler/begin_frame_source.h"
#include "cc/scheduler/delay_based_time_source.h"
#include "cc/scheduler/draw_result.h"
#include "cc/scheduler/scheduler_settings.h"
#include "cc/scheduler/scheduler_state_machine.h"
namespace base {
namespace debug {
class ConvertableToTraceFormat;
}
class SingleThreadTaskRunner;
}
namespace cc {
class SchedulerClient {
public:
virtual void WillBeginImplFrame(const BeginFrameArgs& args) = 0;
virtual void ScheduledActionSendBeginMainFrame() = 0;
virtual DrawResult ScheduledActionDrawAndSwapIfPossible() = 0;
virtual DrawResult ScheduledActionDrawAndSwapForced() = 0;
virtual void ScheduledActionAnimate() = 0;
virtual void ScheduledActionCommit() = 0;
virtual void ScheduledActionActivateSyncTree() = 0;
virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
virtual void ScheduledActionPrepareTiles() = 0;
virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) = 0;
virtual base::TimeDelta DrawDurationEstimate() = 0;
virtual base::TimeDelta BeginMainFrameToCommitDurationEstimate() = 0;
virtual base::TimeDelta CommitToActivateDurationEstimate() = 0;
virtual void DidBeginImplFrameDeadline() = 0;
virtual void SendBeginFramesToChildren(const BeginFrameArgs& args) = 0;
protected:
virtual ~SchedulerClient() {}
};
class Scheduler;
// This class exists to allow tests to override the frame source construction.
// A virtual method can't be used as this needs to happen in the constructor
// (see C++ FAQ / Section 23 - http://goo.gl/fnrwom for why).
// This class exists solely long enough to construct the frame sources.
class CC_EXPORT SchedulerFrameSourcesConstructor {
public:
virtual ~SchedulerFrameSourcesConstructor() {}
virtual BeginFrameSource* ConstructPrimaryFrameSource(Scheduler* scheduler);
virtual BeginFrameSource* ConstructBackgroundFrameSource(
Scheduler* scheduler);
virtual BeginFrameSource* ConstructUnthrottledFrameSource(
Scheduler* scheduler);
protected:
SchedulerFrameSourcesConstructor() {}
friend class Scheduler;
};
class CC_EXPORT Scheduler : public BeginFrameObserverMixIn,
public base::PowerObserver {
public:
static scoped_ptr<Scheduler> Create(
SchedulerClient* client,
const SchedulerSettings& scheduler_settings,
int layer_tree_host_id,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
base::PowerMonitor* power_monitor,
scoped_ptr<BeginFrameSource> external_begin_frame_source) {
SchedulerFrameSourcesConstructor frame_sources_constructor;
return make_scoped_ptr(new Scheduler(client,
scheduler_settings,
layer_tree_host_id,
task_runner,
power_monitor,
external_begin_frame_source.Pass(),
&frame_sources_constructor));
}
~Scheduler() override;
// BeginFrameObserverMixin
bool OnBeginFrameMixInDelegate(const BeginFrameArgs& args) override;
// base::PowerObserver method.
void OnPowerStateChange(bool on_battery_power) override;
const SchedulerSettings& settings() const { return settings_; }
void CommitVSyncParameters(base::TimeTicks timebase,
base::TimeDelta interval);
void SetEstimatedParentDrawTime(base::TimeDelta draw_time);
void SetCanStart();
void SetVisible(bool visible);
void SetCanDraw(bool can_draw);
void NotifyReadyToActivate();
void NotifyReadyToDraw();
void SetThrottleFrameProduction(bool throttle);
void SetNeedsCommit();
void SetNeedsRedraw();
void SetNeedsAnimate();
void SetNeedsPrepareTiles();
void SetMaxSwapsPending(int max);
void DidSwapBuffers();
void DidSwapBuffersComplete();
void SetImplLatencyTakesPriority(bool impl_latency_takes_priority);
void NotifyReadyToCommit();
void BeginMainFrameAborted(CommitEarlyOutReason reason);
void DidPrepareTiles();
void DidLoseOutputSurface();
void DidCreateAndInitializeOutputSurface();
// Tests do not want to shut down until all possible BeginMainFrames have
// occured to prevent flakiness.
bool MainFrameForTestingWillHappen() const {
return state_machine_.CommitPending() ||
state_machine_.CouldSendBeginMainFrame();
}
bool CommitPending() const { return state_machine_.CommitPending(); }
bool RedrawPending() const { return state_machine_.RedrawPending(); }
bool PrepareTilesPending() const {
return state_machine_.PrepareTilesPending();
}
bool MainThreadIsInHighLatencyMode() const {
return state_machine_.MainThreadIsInHighLatencyMode();
}
bool BeginImplFrameDeadlinePending() const {
return !begin_impl_frame_deadline_task_.IsCancelled();
}
base::TimeTicks AnticipatedDrawTime() const;
void NotifyBeginMainFrameStarted();
base::TimeTicks LastBeginImplFrameTime();
scoped_refptr<base::debug::ConvertableToTraceFormat> AsValue() const;
void AsValueInto(base::debug::TracedValue* value) const override;
void SetContinuousPainting(bool continuous_painting) {
state_machine_.SetContinuousPainting(continuous_painting);
}
void SetChildrenNeedBeginFrames(bool children_need_begin_frames);
protected:
Scheduler(SchedulerClient* client,
const SchedulerSettings& scheduler_settings,
int layer_tree_host_id,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
base::PowerMonitor* power_monitor,
scoped_ptr<BeginFrameSource> external_begin_frame_source,
SchedulerFrameSourcesConstructor* frame_sources_constructor);
// virtual for testing - Don't call these in the constructor or
// destructor!
virtual base::TimeTicks Now() const;
scoped_ptr<BeginFrameSourceMultiplexer> frame_source_;
BeginFrameSource* primary_frame_source_;
BeginFrameSource* background_frame_source_;
BeginFrameSource* unthrottled_frame_source_;
// Storage when frame sources are internal
scoped_ptr<BeginFrameSource> primary_frame_source_internal_;
scoped_ptr<SyntheticBeginFrameSource> background_frame_source_internal_;
scoped_ptr<BeginFrameSource> unthrottled_frame_source_internal_;
VSyncParameterObserver* vsync_observer_;
bool throttle_frame_production_;
const SchedulerSettings settings_;
SchedulerClient* client_;
int layer_tree_host_id_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
base::PowerMonitor* power_monitor_;
base::TimeDelta estimated_parent_draw_time_;
std::deque<BeginFrameArgs> begin_retro_frame_args_;
BeginFrameArgs begin_impl_frame_args_;
SchedulerStateMachine::BeginImplFrameDeadlineMode
begin_impl_frame_deadline_mode_;
base::Closure begin_retro_frame_closure_;
base::Closure begin_impl_frame_deadline_closure_;
base::Closure poll_for_draw_triggers_closure_;
base::Closure advance_commit_state_closure_;
base::CancelableClosure begin_retro_frame_task_;
base::CancelableClosure begin_impl_frame_deadline_task_;
base::CancelableClosure poll_for_draw_triggers_task_;
base::CancelableClosure advance_commit_state_task_;
SchedulerStateMachine state_machine_;
bool inside_process_scheduled_actions_;
SchedulerStateMachine::Action inside_action_;
private:
void ScheduleBeginImplFrameDeadline();
void RescheduleBeginImplFrameDeadlineIfNeeded();
void SetupNextBeginFrameIfNeeded();
void PostBeginRetroFrameIfNeeded();
void SetupPollingMechanisms();
void DrawAndSwapIfPossible();
void ProcessScheduledActions();
bool CanCommitAndActivateBeforeDeadline() const;
void AdvanceCommitStateIfPossible();
bool IsBeginMainFrameSentOrStarted() const;
void BeginRetroFrame();
void BeginImplFrame(const BeginFrameArgs& args);
void OnBeginImplFrameDeadline();
void PollForAnticipatedDrawTriggers();
void PollToAdvanceCommitState();
void SetupPowerMonitoring();
void TeardownPowerMonitoring();
void UpdateActiveFrameSource();
base::TimeDelta EstimatedParentDrawTime() {
return estimated_parent_draw_time_;
}
bool IsInsideAction(SchedulerStateMachine::Action action) {
return inside_action_ == action;
}
base::WeakPtrFactory<Scheduler> weak_factory_;
friend class SchedulerFrameSourcesConstructor;
friend class TestSchedulerFrameSourcesConstructor;
DISALLOW_COPY_AND_ASSIGN(Scheduler);
};
} // namespace cc
#endif // CC_SCHEDULER_SCHEDULER_H_
|