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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 void SyntheticBeginFrameSource::AsValueInto( | 263 void SyntheticBeginFrameSource::AsValueInto( |
264 base::trace_event::TracedValue* dict) const { | 264 base::trace_event::TracedValue* dict) const { |
265 dict->SetString("type", "SyntheticBeginFrameSource"); | 265 dict->SetString("type", "SyntheticBeginFrameSource"); |
266 BeginFrameSourceBase::AsValueInto(dict); | 266 BeginFrameSourceBase::AsValueInto(dict); |
267 | 267 |
268 dict->BeginDictionary("time_source"); | 268 dict->BeginDictionary("time_source"); |
269 time_source_->AsValueInto(dict); | 269 time_source_->AsValueInto(dict); |
270 dict->EndDictionary(); | 270 dict->EndDictionary(); |
271 } | 271 } |
272 | 272 |
273 // BeginFrameSourceMultiplexer ------------------------------------------- | |
274 scoped_ptr<BeginFrameSourceMultiplexer> BeginFrameSourceMultiplexer::Create() { | |
275 return make_scoped_ptr(new BeginFrameSourceMultiplexer()); | |
276 } | |
277 | |
278 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer() | |
279 : BeginFrameSourceBase(), | |
280 active_source_(nullptr), | |
281 inside_add_observer_(false) {} | |
282 | |
283 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer( | |
284 base::TimeDelta minimum_interval) | |
285 : BeginFrameSourceBase(), | |
286 active_source_(nullptr), | |
287 inside_add_observer_(false) {} | |
288 | |
289 BeginFrameSourceMultiplexer::~BeginFrameSourceMultiplexer() { | |
290 if (active_source_ && needs_begin_frames()) | |
291 active_source_->RemoveObserver(this); | |
292 } | |
293 | |
294 void BeginFrameSourceMultiplexer::SetMinimumInterval( | |
295 base::TimeDelta new_minimum_interval) { | |
296 DEBUG_FRAMES("BeginFrameSourceMultiplexer::SetMinimumInterval", | |
297 "current minimum (us)", | |
298 minimum_interval_.InMicroseconds(), | |
299 "new minimum (us)", | |
300 new_minimum_interval.InMicroseconds()); | |
301 DCHECK_GE(new_minimum_interval.ToInternalValue(), 0); | |
302 minimum_interval_ = new_minimum_interval; | |
303 } | |
304 | |
305 void BeginFrameSourceMultiplexer::AddSource(BeginFrameSource* new_source) { | |
306 DEBUG_FRAMES("BeginFrameSourceMultiplexer::AddSource", "current active", | |
307 active_source_, "source to be added", new_source); | |
308 DCHECK(new_source); | |
309 DCHECK(!HasSource(new_source)); | |
310 | |
311 source_list_.insert(new_source); | |
312 | |
313 // If there is no active source, set the new one as the active one. | |
314 if (!active_source_) | |
315 SetActiveSource(new_source); | |
316 } | |
317 | |
318 void BeginFrameSourceMultiplexer::RemoveSource( | |
319 BeginFrameSource* existing_source) { | |
320 DEBUG_FRAMES("BeginFrameSourceMultiplexer::RemoveSource", "current active", | |
321 active_source_, "source to be removed", existing_source); | |
322 DCHECK(existing_source); | |
323 DCHECK(HasSource(existing_source)); | |
324 DCHECK_NE(existing_source, active_source_); | |
325 source_list_.erase(existing_source); | |
326 } | |
327 | |
328 void BeginFrameSourceMultiplexer::SetActiveSource( | |
329 BeginFrameSource* new_source) { | |
330 DEBUG_FRAMES("BeginFrameSourceMultiplexer::SetActiveSource", | |
331 "current active", | |
332 active_source_, | |
333 "to become active", | |
334 new_source); | |
335 | |
336 DCHECK(HasSource(new_source) || new_source == nullptr); | |
337 | |
338 if (active_source_ == new_source) | |
339 return; | |
340 | |
341 if (active_source_ && needs_begin_frames()) | |
342 active_source_->RemoveObserver(this); | |
343 | |
344 active_source_ = new_source; | |
345 | |
346 if (active_source_ && needs_begin_frames()) | |
347 active_source_->AddObserver(this); | |
348 } | |
349 | |
350 const BeginFrameSource* BeginFrameSourceMultiplexer::ActiveSource() { | |
351 return active_source_; | |
352 } | |
353 | |
354 // BeginFrameObserver support | |
355 void BeginFrameSourceMultiplexer::OnBeginFrame(const BeginFrameArgs& args) { | |
356 if (!IsIncreasing(args)) { | |
357 DEBUG_FRAMES("BeginFrameSourceMultiplexer::OnBeginFrame", | |
358 "action", | |
359 "discarding", | |
360 "new args", | |
361 args.AsValue()); | |
362 return; | |
363 } | |
364 DEBUG_FRAMES("BeginFrameSourceMultiplexer::OnBeginFrame", | |
365 "action", | |
366 "using", | |
367 "new args", | |
368 args.AsValue()); | |
369 last_begin_frame_args_ = args; | |
370 CallOnBeginFrame(args); | |
371 } | |
372 | |
373 const BeginFrameArgs& BeginFrameSourceMultiplexer::LastUsedBeginFrameArgs() | |
374 const { | |
375 return last_begin_frame_args_; | |
376 } | |
377 | |
378 void BeginFrameSourceMultiplexer::OnBeginFrameSourcePausedChanged(bool paused) { | |
379 if (paused_ == paused) | |
380 return; | |
381 paused_ = paused; | |
382 if (!inside_add_observer_) { | |
383 std::set<BeginFrameObserver*> observers(observers_); | |
384 for (auto& it : observers) | |
385 it->OnBeginFrameSourcePausedChanged(paused); | |
386 } | |
387 } | |
388 | |
389 // BeginFrameSource support | |
390 void BeginFrameSourceMultiplexer::DidFinishFrame(size_t remaining_frames) { | |
391 DEBUG_FRAMES("BeginFrameSourceMultiplexer::DidFinishFrame", | |
392 "active_source", | |
393 active_source_, | |
394 "remaining_frames", | |
395 remaining_frames); | |
396 if (active_source_) | |
397 active_source_->DidFinishFrame(remaining_frames); | |
398 } | |
399 | |
400 void BeginFrameSourceMultiplexer::AddObserver(BeginFrameObserver* obs) { | |
401 base::AutoReset<bool> reset(&inside_add_observer_, true); | |
402 BeginFrameSourceBase::AddObserver(obs); | |
403 } | |
404 | |
405 void BeginFrameSourceMultiplexer::OnNeedsBeginFramesChanged( | |
406 bool needs_begin_frames) { | |
407 if (!active_source_) | |
408 return; | |
409 if (needs_begin_frames) { | |
410 active_source_->AddObserver(this); | |
411 } else { | |
412 active_source_->RemoveObserver(this); | |
413 } | |
414 } | |
415 | |
416 // Tracing support | |
417 void BeginFrameSourceMultiplexer::AsValueInto( | |
418 base::trace_event::TracedValue* dict) const { | |
419 dict->SetString("type", "BeginFrameSourceMultiplexer"); | |
420 | |
421 dict->SetInteger("minimum_interval_us", minimum_interval_.InMicroseconds()); | |
422 | |
423 dict->BeginDictionary("last_begin_frame_args"); | |
424 last_begin_frame_args_.AsValueInto(dict); | |
425 dict->EndDictionary(); | |
426 | |
427 if (active_source_) { | |
428 dict->BeginDictionary("active_source"); | |
429 active_source_->AsValueInto(dict); | |
430 dict->EndDictionary(); | |
431 } else { | |
432 dict->SetString("active_source", "NULL"); | |
433 } | |
434 | |
435 dict->BeginArray("sources"); | |
436 for (std::set<BeginFrameSource*>::const_iterator it = source_list_.begin(); | |
437 it != source_list_.end(); | |
438 ++it) { | |
439 dict->BeginDictionary(); | |
440 (*it)->AsValueInto(dict); | |
441 dict->EndDictionary(); | |
442 } | |
443 dict->EndArray(); | |
444 } | |
445 | |
446 // protected methods | |
447 bool BeginFrameSourceMultiplexer::HasSource(BeginFrameSource* source) { | |
448 return (source_list_.find(source) != source_list_.end()); | |
449 } | |
450 | |
451 bool BeginFrameSourceMultiplexer::IsIncreasing(const BeginFrameArgs& args) { | |
452 DCHECK(args.IsValid()); | |
453 | |
454 // If the last begin frame is invalid, then any new begin frame is valid. | |
455 if (!last_begin_frame_args_.IsValid()) | |
456 return true; | |
457 | |
458 // Only allow new args have a *strictly bigger* frame_time value and statisfy | |
459 // minimum interval requirement. | |
460 return (args.frame_time >= | |
461 last_begin_frame_args_.frame_time + minimum_interval_); | |
462 } | |
463 | |
464 } // namespace cc | 273 } // namespace cc |
OLD | NEW |