OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/scheduler/begin_frame_source.h" | 5 #include "cc/scheduler/begin_frame_source.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/trace_event/trace_event.h" | 11 #include "base/trace_event/trace_event.h" |
12 #include "base/trace_event/trace_event_argument.h" | 12 #include "base/trace_event/trace_event_argument.h" |
13 #include "cc/scheduler/delay_based_time_source.h" | 13 #include "cc/scheduler/delay_based_time_source.h" |
14 #include "cc/scheduler/scheduler.h" | 14 #include "cc/scheduler/scheduler.h" |
15 | 15 |
16 #ifdef NDEBUG | 16 #ifdef NDEBUG |
17 #define DEBUG_FRAMES(...) | 17 #define DEBUG_FRAMES(...) |
18 #else | 18 #else |
19 #define DEBUG_FRAMES(name, arg1_name, arg1_val, arg2_name, arg2_val) \ | 19 #define DEBUG_FRAMES(name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
20 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"), \ | 20 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"), \ |
21 name, \ | 21 name, \ |
22 arg1_name, \ | 22 arg1_name, \ |
23 arg1_val, \ | 23 arg1_val, \ |
24 arg2_name, \ | 24 arg2_name, \ |
25 arg2_val); | 25 arg2_val); |
26 #endif | 26 #endif |
27 | 27 |
28 namespace cc { | 28 namespace cc { |
29 | 29 |
30 // BeginFrameObserverMixIn ----------------------------------------------- | 30 // BeginFrameObserverBase ----------------------------------------------- |
31 BeginFrameObserverMixIn::BeginFrameObserverMixIn() | 31 BeginFrameObserverBase::BeginFrameObserverBase() |
32 : last_begin_frame_args_(), dropped_begin_frame_args_(0) { | 32 : last_begin_frame_args_(), dropped_begin_frame_args_(0) { |
33 } | 33 } |
34 | 34 |
35 const BeginFrameArgs BeginFrameObserverMixIn::LastUsedBeginFrameArgs() const { | 35 const BeginFrameArgs BeginFrameObserverBase::LastUsedBeginFrameArgs() const { |
36 return last_begin_frame_args_; | 36 return last_begin_frame_args_; |
37 } | 37 } |
38 void BeginFrameObserverMixIn::OnBeginFrame(const BeginFrameArgs& args) { | 38 void BeginFrameObserverBase::OnBeginFrame(const BeginFrameArgs& args) { |
39 DEBUG_FRAMES("BeginFrameObserverMixIn::OnBeginFrame", | 39 DEBUG_FRAMES("BeginFrameObserverBase::OnBeginFrame", |
40 "last args", | 40 "last args", |
41 last_begin_frame_args_.AsValue(), | 41 last_begin_frame_args_.AsValue(), |
42 "new args", | 42 "new args", |
43 args.AsValue()); | 43 args.AsValue()); |
44 DCHECK(args.IsValid()); | 44 DCHECK(args.IsValid()); |
45 DCHECK(args.frame_time >= last_begin_frame_args_.frame_time); | 45 DCHECK(args.frame_time >= last_begin_frame_args_.frame_time); |
46 bool used = OnBeginFrameMixInDelegate(args); | 46 bool used = OnBeginFrameDerivedImpl(args); |
47 if (used) { | 47 if (used) { |
48 last_begin_frame_args_ = args; | 48 last_begin_frame_args_ = args; |
49 } else { | 49 } else { |
50 ++dropped_begin_frame_args_; | 50 ++dropped_begin_frame_args_; |
51 } | 51 } |
52 } | 52 } |
53 | 53 |
54 void BeginFrameObserverMixIn::AsValueInto( | 54 void BeginFrameObserverBase::AsValueInto( |
55 base::trace_event::TracedValue* dict) const { | 55 base::trace_event::TracedValue* dict) const { |
56 dict->BeginDictionary("last_begin_frame_args_"); | 56 dict->BeginDictionary("last_begin_frame_args_"); |
57 last_begin_frame_args_.AsValueInto(dict); | 57 last_begin_frame_args_.AsValueInto(dict); |
58 dict->EndDictionary(); | 58 dict->EndDictionary(); |
59 dict->SetInteger("dropped_begin_frame_args_", dropped_begin_frame_args_); | 59 dict->SetInteger("dropped_begin_frame_args_", dropped_begin_frame_args_); |
60 } | 60 } |
61 | 61 |
62 // BeginFrameSourceMixIn ------------------------------------------------------ | 62 // BeginFrameSourceBase ------------------------------------------------------ |
63 BeginFrameSourceMixIn::BeginFrameSourceMixIn() | 63 BeginFrameSourceBase::BeginFrameSourceBase() |
64 : observer_(NULL), | 64 : observer_(NULL), |
65 needs_begin_frames_(false), | 65 needs_begin_frames_(false), |
66 inside_as_value_into_(false) { | 66 inside_as_value_into_(false) { |
67 DCHECK(!observer_); | 67 DCHECK(!observer_); |
68 DCHECK_EQ(inside_as_value_into_, false); | 68 DCHECK_EQ(inside_as_value_into_, false); |
69 } | 69 } |
70 | 70 |
71 bool BeginFrameSourceMixIn::NeedsBeginFrames() const { | 71 bool BeginFrameSourceBase::NeedsBeginFrames() const { |
72 return needs_begin_frames_; | 72 return needs_begin_frames_; |
73 } | 73 } |
74 | 74 |
75 void BeginFrameSourceMixIn::SetNeedsBeginFrames(bool needs_begin_frames) { | 75 void BeginFrameSourceBase::SetNeedsBeginFrames(bool needs_begin_frames) { |
76 DEBUG_FRAMES("BeginFrameSourceMixIn::SetNeedsBeginFrames", | 76 DEBUG_FRAMES("BeginFrameSourceBase::SetNeedsBeginFrames", |
77 "current state", | 77 "current state", |
78 needs_begin_frames_, | 78 needs_begin_frames_, |
79 "new state", | 79 "new state", |
80 needs_begin_frames); | 80 needs_begin_frames); |
81 if (needs_begin_frames_ != needs_begin_frames) { | 81 if (needs_begin_frames_ != needs_begin_frames) { |
82 needs_begin_frames_ = needs_begin_frames; | 82 needs_begin_frames_ = needs_begin_frames; |
83 OnNeedsBeginFramesChange(needs_begin_frames); | 83 OnNeedsBeginFramesChange(needs_begin_frames); |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 void BeginFrameSourceMixIn::AddObserver(BeginFrameObserver* obs) { | 87 void BeginFrameSourceBase::AddObserver(BeginFrameObserver* obs) { |
88 DEBUG_FRAMES("BeginFrameSourceMixIn::AddObserver", | 88 DEBUG_FRAMES("BeginFrameSourceBase::AddObserver", |
89 "current observer", | 89 "current observer", |
90 observer_, | 90 observer_, |
91 "to add observer", | 91 "to add observer", |
92 obs); | 92 obs); |
93 DCHECK(!observer_); | 93 DCHECK(!observer_); |
94 observer_ = obs; | 94 observer_ = obs; |
95 } | 95 } |
96 | 96 |
97 void BeginFrameSourceMixIn::RemoveObserver(BeginFrameObserver* obs) { | 97 void BeginFrameSourceBase::RemoveObserver(BeginFrameObserver* obs) { |
98 DEBUG_FRAMES("BeginFrameSourceMixIn::RemoveObserver", | 98 DEBUG_FRAMES("BeginFrameSourceBase::RemoveObserver", |
99 "current observer", | 99 "current observer", |
100 observer_, | 100 observer_, |
101 "to remove observer", | 101 "to remove observer", |
102 obs); | 102 obs); |
103 DCHECK_EQ(observer_, obs); | 103 DCHECK_EQ(observer_, obs); |
104 observer_ = NULL; | 104 observer_ = NULL; |
105 } | 105 } |
106 | 106 |
107 void BeginFrameSourceMixIn::CallOnBeginFrame(const BeginFrameArgs& args) { | 107 void BeginFrameSourceBase::CallOnBeginFrame(const BeginFrameArgs& args) { |
108 DEBUG_FRAMES("BeginFrameSourceMixIn::CallOnBeginFrame", | 108 DEBUG_FRAMES("BeginFrameSourceBase::CallOnBeginFrame", |
109 "current observer", | 109 "current observer", |
110 observer_, | 110 observer_, |
111 "args", | 111 "args", |
112 args.AsValue()); | 112 args.AsValue()); |
113 if (observer_) { | 113 if (observer_) { |
114 return observer_->OnBeginFrame(args); | 114 return observer_->OnBeginFrame(args); |
115 } | 115 } |
116 } | 116 } |
117 | 117 |
118 // Tracing support | 118 // Tracing support |
119 void BeginFrameSourceMixIn::AsValueInto( | 119 void BeginFrameSourceBase::AsValueInto( |
120 base::trace_event::TracedValue* dict) const { | 120 base::trace_event::TracedValue* dict) const { |
121 // As the observer might try to trace the source, prevent an infinte loop | 121 // As the observer might try to trace the source, prevent an infinte loop |
122 // from occuring. | 122 // from occuring. |
123 if (inside_as_value_into_) { | 123 if (inside_as_value_into_) { |
124 dict->SetString("observer", "<loop detected>"); | 124 dict->SetString("observer", "<loop detected>"); |
125 return; | 125 return; |
126 } | 126 } |
127 | 127 |
128 if (observer_) { | 128 if (observer_) { |
129 base::AutoReset<bool> prevent_loops( | 129 base::AutoReset<bool> prevent_loops( |
130 const_cast<bool*>(&inside_as_value_into_), true); | 130 const_cast<bool*>(&inside_as_value_into_), true); |
131 dict->BeginDictionary("observer"); | 131 dict->BeginDictionary("observer"); |
132 observer_->AsValueInto(dict); | 132 observer_->AsValueInto(dict); |
133 dict->EndDictionary(); | 133 dict->EndDictionary(); |
134 } else { | 134 } else { |
135 dict->SetString("observer", "NULL"); | 135 dict->SetString("observer", "NULL"); |
136 } | 136 } |
137 dict->SetBoolean("needs_begin_frames", NeedsBeginFrames()); | 137 dict->SetBoolean("needs_begin_frames", NeedsBeginFrames()); |
138 } | 138 } |
139 | 139 |
140 // BackToBackBeginFrameSourceMixIn -------------------------------------------- | 140 // BackToBackBeginFrameSource -------------------------------------------- |
141 scoped_ptr<BackToBackBeginFrameSource> BackToBackBeginFrameSource::Create( | 141 scoped_ptr<BackToBackBeginFrameSource> BackToBackBeginFrameSource::Create( |
142 base::SingleThreadTaskRunner* task_runner) { | 142 base::SingleThreadTaskRunner* task_runner) { |
143 return make_scoped_ptr(new BackToBackBeginFrameSource(task_runner)); | 143 return make_scoped_ptr(new BackToBackBeginFrameSource(task_runner)); |
144 } | 144 } |
145 | 145 |
146 BackToBackBeginFrameSource::BackToBackBeginFrameSource( | 146 BackToBackBeginFrameSource::BackToBackBeginFrameSource( |
147 base::SingleThreadTaskRunner* task_runner) | 147 base::SingleThreadTaskRunner* task_runner) |
148 : BeginFrameSourceMixIn(), | 148 : BeginFrameSourceBase(), |
149 task_runner_(task_runner), | 149 task_runner_(task_runner), |
150 send_begin_frame_posted_(false), | 150 send_begin_frame_posted_(false), |
151 weak_factory_(this) { | 151 weak_factory_(this) { |
152 DCHECK(task_runner); | 152 DCHECK(task_runner); |
153 DCHECK_EQ(needs_begin_frames_, false); | 153 DCHECK_EQ(needs_begin_frames_, false); |
154 DCHECK_EQ(send_begin_frame_posted_, false); | 154 DCHECK_EQ(send_begin_frame_posted_, false); |
155 } | 155 } |
156 | 156 |
157 BackToBackBeginFrameSource::~BackToBackBeginFrameSource() { | 157 BackToBackBeginFrameSource::~BackToBackBeginFrameSource() { |
158 } | 158 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 void BackToBackBeginFrameSource::DidFinishFrame(size_t remaining_frames) { | 193 void BackToBackBeginFrameSource::DidFinishFrame(size_t remaining_frames) { |
194 if (remaining_frames == 0) { | 194 if (remaining_frames == 0) { |
195 OnNeedsBeginFramesChange(NeedsBeginFrames()); | 195 OnNeedsBeginFramesChange(NeedsBeginFrames()); |
196 } | 196 } |
197 } | 197 } |
198 | 198 |
199 // Tracing support | 199 // Tracing support |
200 void BackToBackBeginFrameSource::AsValueInto( | 200 void BackToBackBeginFrameSource::AsValueInto( |
201 base::trace_event::TracedValue* dict) const { | 201 base::trace_event::TracedValue* dict) const { |
202 dict->SetString("type", "BackToBackBeginFrameSource"); | 202 dict->SetString("type", "BackToBackBeginFrameSource"); |
203 BeginFrameSourceMixIn::AsValueInto(dict); | 203 BeginFrameSourceBase::AsValueInto(dict); |
204 dict->SetBoolean("send_begin_frame_posted_", send_begin_frame_posted_); | 204 dict->SetBoolean("send_begin_frame_posted_", send_begin_frame_posted_); |
205 } | 205 } |
206 | 206 |
207 // SyntheticBeginFrameSource --------------------------------------------- | 207 // SyntheticBeginFrameSource --------------------------------------------- |
208 scoped_ptr<SyntheticBeginFrameSource> SyntheticBeginFrameSource::Create( | 208 scoped_ptr<SyntheticBeginFrameSource> SyntheticBeginFrameSource::Create( |
209 base::SingleThreadTaskRunner* task_runner, | 209 base::SingleThreadTaskRunner* task_runner, |
210 base::TimeTicks initial_vsync_timebase, | 210 base::TimeTicks initial_vsync_timebase, |
211 base::TimeDelta initial_vsync_interval) { | 211 base::TimeDelta initial_vsync_interval) { |
212 scoped_refptr<DelayBasedTimeSource> time_source; | 212 scoped_refptr<DelayBasedTimeSource> time_source; |
213 time_source = | 213 time_source = |
214 DelayBasedTimeSource::Create(initial_vsync_interval, task_runner); | 214 DelayBasedTimeSource::Create(initial_vsync_interval, task_runner); |
215 return make_scoped_ptr(new SyntheticBeginFrameSource(time_source)); | 215 return make_scoped_ptr(new SyntheticBeginFrameSource(time_source)); |
216 } | 216 } |
217 | 217 |
218 SyntheticBeginFrameSource::SyntheticBeginFrameSource( | 218 SyntheticBeginFrameSource::SyntheticBeginFrameSource( |
219 scoped_refptr<DelayBasedTimeSource> time_source) | 219 scoped_refptr<DelayBasedTimeSource> time_source) |
220 : BeginFrameSourceMixIn(), time_source_(time_source) { | 220 : BeginFrameSourceBase(), time_source_(time_source) { |
221 time_source_->SetActive(false); | 221 time_source_->SetActive(false); |
222 time_source_->SetClient(this); | 222 time_source_->SetClient(this); |
223 } | 223 } |
224 | 224 |
225 SyntheticBeginFrameSource::~SyntheticBeginFrameSource() { | 225 SyntheticBeginFrameSource::~SyntheticBeginFrameSource() { |
226 if (NeedsBeginFrames()) | 226 if (NeedsBeginFrames()) |
227 time_source_->SetActive(false); | 227 time_source_->SetActive(false); |
228 } | 228 } |
229 | 229 |
230 void SyntheticBeginFrameSource::OnUpdateVSyncParameters( | 230 void SyntheticBeginFrameSource::OnUpdateVSyncParameters( |
231 base::TimeTicks new_vsync_timebase, | 231 base::TimeTicks new_vsync_timebase, |
232 base::TimeDelta new_vsync_interval) { | 232 base::TimeDelta new_vsync_interval) { |
233 time_source_->SetTimebaseAndInterval(new_vsync_timebase, new_vsync_interval); | 233 time_source_->SetTimebaseAndInterval(new_vsync_timebase, new_vsync_interval); |
234 } | 234 } |
235 | 235 |
236 BeginFrameArgs SyntheticBeginFrameSource::CreateBeginFrameArgs( | 236 BeginFrameArgs SyntheticBeginFrameSource::CreateBeginFrameArgs( |
237 base::TimeTicks frame_time, | 237 base::TimeTicks frame_time, |
238 BeginFrameArgs::BeginFrameArgsType type) { | 238 BeginFrameArgs::BeginFrameArgsType type) { |
239 base::TimeTicks deadline = time_source_->NextTickTime(); | 239 base::TimeTicks deadline = time_source_->NextTickTime(); |
240 return BeginFrameArgs::Create(BEGINFRAME_FROM_HERE, frame_time, deadline, | 240 return BeginFrameArgs::Create(BEGINFRAME_FROM_HERE, frame_time, deadline, |
241 time_source_->Interval(), type); | 241 time_source_->Interval(), type); |
242 } | 242 } |
243 | 243 |
244 // TimeSourceClient support | 244 // TimeSourceClient support |
245 void SyntheticBeginFrameSource::OnTimerTick() { | 245 void SyntheticBeginFrameSource::OnTimerTick() { |
246 CallOnBeginFrame(CreateBeginFrameArgs(time_source_->LastTickTime(), | 246 CallOnBeginFrame(CreateBeginFrameArgs(time_source_->LastTickTime(), |
247 BeginFrameArgs::NORMAL)); | 247 BeginFrameArgs::NORMAL)); |
248 } | 248 } |
249 | 249 |
250 // BeginFrameSourceMixIn support | 250 // BeginFrameSourceBase support |
251 void SyntheticBeginFrameSource::OnNeedsBeginFramesChange( | 251 void SyntheticBeginFrameSource::OnNeedsBeginFramesChange( |
252 bool needs_begin_frames) { | 252 bool needs_begin_frames) { |
253 base::TimeTicks missed_tick_time = | 253 base::TimeTicks missed_tick_time = |
254 time_source_->SetActive(needs_begin_frames); | 254 time_source_->SetActive(needs_begin_frames); |
255 if (!missed_tick_time.is_null()) { | 255 if (!missed_tick_time.is_null()) { |
256 CallOnBeginFrame( | 256 CallOnBeginFrame( |
257 CreateBeginFrameArgs(missed_tick_time, BeginFrameArgs::MISSED)); | 257 CreateBeginFrameArgs(missed_tick_time, BeginFrameArgs::MISSED)); |
258 } | 258 } |
259 } | 259 } |
260 | 260 |
261 // Tracing support | 261 // Tracing support |
262 void SyntheticBeginFrameSource::AsValueInto( | 262 void SyntheticBeginFrameSource::AsValueInto( |
263 base::trace_event::TracedValue* dict) const { | 263 base::trace_event::TracedValue* dict) const { |
264 dict->SetString("type", "SyntheticBeginFrameSource"); | 264 dict->SetString("type", "SyntheticBeginFrameSource"); |
265 BeginFrameSourceMixIn::AsValueInto(dict); | 265 BeginFrameSourceBase::AsValueInto(dict); |
266 | 266 |
267 dict->BeginDictionary("time_source"); | 267 dict->BeginDictionary("time_source"); |
268 time_source_->AsValueInto(dict); | 268 time_source_->AsValueInto(dict); |
269 dict->EndDictionary(); | 269 dict->EndDictionary(); |
270 } | 270 } |
271 | 271 |
272 // BeginFrameSourceMultiplexer ------------------------------------------- | 272 // BeginFrameSourceMultiplexer ------------------------------------------- |
273 scoped_ptr<BeginFrameSourceMultiplexer> BeginFrameSourceMultiplexer::Create() { | 273 scoped_ptr<BeginFrameSourceMultiplexer> BeginFrameSourceMultiplexer::Create() { |
274 return make_scoped_ptr(new BeginFrameSourceMultiplexer()); | 274 return make_scoped_ptr(new BeginFrameSourceMultiplexer()); |
275 } | 275 } |
276 | 276 |
277 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer() | 277 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer() |
278 : BeginFrameSourceMixIn(), | 278 : BeginFrameSourceBase(), |
279 minimum_interval_(base::TimeDelta()), | 279 minimum_interval_(base::TimeDelta()), |
280 active_source_(NULL), | 280 active_source_(NULL), |
281 source_list_() { | 281 source_list_() { |
282 } | 282 } |
283 | 283 |
284 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer( | 284 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer( |
285 base::TimeDelta minimum_interval) | 285 base::TimeDelta minimum_interval) |
286 : BeginFrameSourceMixIn(), | 286 : BeginFrameSourceBase(), |
287 minimum_interval_(minimum_interval), | 287 minimum_interval_(minimum_interval), |
288 active_source_(NULL), | 288 active_source_(NULL), |
289 source_list_() { | 289 source_list_() { |
290 } | 290 } |
291 | 291 |
292 BeginFrameSourceMultiplexer::~BeginFrameSourceMultiplexer() { | 292 BeginFrameSourceMultiplexer::~BeginFrameSourceMultiplexer() { |
293 if (active_source_) { | 293 if (active_source_) { |
294 active_source_->SetNeedsBeginFrames(false); | 294 active_source_->SetNeedsBeginFrames(false); |
295 active_source_->RemoveObserver(this); | 295 active_source_->RemoveObserver(this); |
296 } | 296 } |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 if (!observer_->LastUsedBeginFrameArgs().IsValid()) | 462 if (!observer_->LastUsedBeginFrameArgs().IsValid()) |
463 return true; | 463 return true; |
464 | 464 |
465 // Only allow new args have a *strictly bigger* frame_time value and statisfy | 465 // Only allow new args have a *strictly bigger* frame_time value and statisfy |
466 // minimum interval requirement. | 466 // minimum interval requirement. |
467 return (args.frame_time >= | 467 return (args.frame_time >= |
468 observer_->LastUsedBeginFrameArgs().frame_time + minimum_interval_); | 468 observer_->LastUsedBeginFrameArgs().frame_time + minimum_interval_); |
469 } | 469 } |
470 | 470 |
471 } // namespace cc | 471 } // namespace cc |
OLD | NEW |