This source file includes following definitions.
- Create
- Now
- Create
- weak_factory_
- SetActive
- Active
- LastTickTime
- NextTickTime
- OnTimerFired
- SetClient
- SetTimebaseAndInterval
- Now
- NextTickTarget
- PostNextTickTask
#include "cc/scheduler/delay_based_time_source.h"
#include <algorithm>
#include <cmath>
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
namespace cc {
namespace {
static const int kDoubleTickDivisor = 2;
static const double kIntervalChangeThreshold = 0.25;
static const double kPhaseChangeThreshold = 0.25;
}
scoped_refptr<DelayBasedTimeSourceHighRes> DelayBasedTimeSourceHighRes::Create(
base::TimeDelta interval,
base::SingleThreadTaskRunner* task_runner) {
return make_scoped_refptr(
new DelayBasedTimeSourceHighRes(interval, task_runner));
}
DelayBasedTimeSourceHighRes::DelayBasedTimeSourceHighRes(
base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner)
: DelayBasedTimeSource(interval, task_runner) {}
DelayBasedTimeSourceHighRes::~DelayBasedTimeSourceHighRes() {}
base::TimeTicks DelayBasedTimeSourceHighRes::Now() const {
return base::TimeTicks::HighResNow();
}
scoped_refptr<DelayBasedTimeSource> DelayBasedTimeSource::Create(
base::TimeDelta interval,
base::SingleThreadTaskRunner* task_runner) {
return make_scoped_refptr(new DelayBasedTimeSource(interval, task_runner));
}
DelayBasedTimeSource::DelayBasedTimeSource(
base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner)
: client_(NULL),
last_tick_time_(base::TimeTicks() - interval),
current_parameters_(interval, base::TimeTicks()),
next_parameters_(interval, base::TimeTicks()),
active_(false),
task_runner_(task_runner),
weak_factory_(this) {}
DelayBasedTimeSource::~DelayBasedTimeSource() {}
base::TimeTicks DelayBasedTimeSource::SetActive(bool active) {
TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active);
if (active == active_)
return base::TimeTicks();
active_ = active;
if (!active_) {
weak_factory_.InvalidateWeakPtrs();
return base::TimeTicks();
}
PostNextTickTask(Now());
base::TimeTicks last_tick_time_if_always_active =
current_parameters_.tick_target - current_parameters_.interval;
base::TimeTicks new_tick_time_threshold =
last_tick_time_ + current_parameters_.interval / kDoubleTickDivisor;
if (last_tick_time_if_always_active > new_tick_time_threshold) {
last_tick_time_ = last_tick_time_if_always_active;
return last_tick_time_;
}
return base::TimeTicks();
}
bool DelayBasedTimeSource::Active() const { return active_; }
base::TimeTicks DelayBasedTimeSource::LastTickTime() { return last_tick_time_; }
base::TimeTicks DelayBasedTimeSource::NextTickTime() {
return Active() ? current_parameters_.tick_target : base::TimeTicks();
}
void DelayBasedTimeSource::OnTimerFired() {
DCHECK(active_);
last_tick_time_ = current_parameters_.tick_target;
PostNextTickTask(Now());
if (client_)
client_->OnTimerTick();
}
void DelayBasedTimeSource::SetClient(TimeSourceClient* client) {
client_ = client;
}
void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase,
base::TimeDelta interval) {
next_parameters_.interval = interval;
next_parameters_.tick_target = timebase;
if (!active_) {
return;
}
double interval_delta =
std::abs((interval - current_parameters_.interval).InSecondsF());
double interval_change = interval_delta / interval.InSecondsF();
if (interval_change > kIntervalChangeThreshold) {
TRACE_EVENT_INSTANT0("cc", "DelayBasedTimeSource::IntervalChanged",
TRACE_EVENT_SCOPE_THREAD);
SetActive(false);
SetActive(true);
return;
}
double target_delta =
std::abs((timebase - current_parameters_.tick_target).InSecondsF());
double phase_change =
fmod(target_delta, interval.InSecondsF()) / interval.InSecondsF();
if (phase_change > kPhaseChangeThreshold &&
phase_change < (1.0 - kPhaseChangeThreshold)) {
TRACE_EVENT_INSTANT0("cc", "DelayBasedTimeSource::PhaseChanged",
TRACE_EVENT_SCOPE_THREAD);
SetActive(false);
SetActive(true);
return;
}
}
base::TimeTicks DelayBasedTimeSource::Now() const {
return base::TimeTicks::Now();
}
base::TimeTicks DelayBasedTimeSource::NextTickTarget(base::TimeTicks now) {
base::TimeDelta new_interval = next_parameters_.interval;
base::TimeDelta interval_offset = base::TimeDelta::FromInternalValue(
(next_parameters_.tick_target - now).ToInternalValue() %
new_interval.ToInternalValue());
if (interval_offset.ToInternalValue() != 0 &&
next_parameters_.tick_target < now) {
interval_offset += new_interval;
}
base::TimeTicks new_tick_target = now + interval_offset;
DCHECK(now <= new_tick_target)
<< "now = " << now.ToInternalValue()
<< "; new_tick_target = " << new_tick_target.ToInternalValue()
<< "; new_interval = " << new_interval.InMicroseconds()
<< "; tick_target = " << next_parameters_.tick_target.ToInternalValue()
<< "; interval_offset = " << interval_offset.ToInternalValue();
if (new_tick_target - last_tick_time_ <= new_interval / kDoubleTickDivisor)
new_tick_target += new_interval;
return new_tick_target;
}
void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) {
base::TimeTicks new_tick_target = NextTickTarget(now);
base::TimeDelta delay;
if (now <= new_tick_target)
delay = new_tick_target - now;
task_runner_->PostDelayedTask(FROM_HERE,
base::Bind(&DelayBasedTimeSource::OnTimerFired,
weak_factory_.GetWeakPtr()),
delay);
next_parameters_.tick_target = new_tick_target;
current_parameters_ = next_parameters_;
}
}