Add a simple scheduler for animation frames

This CL adds a simple scheduler for animation frames that holds a given interval
in a given phase.  The scheduler can be told the estimated duration of the
BeginFrame callback and can adjust the callback to hit a given vsync interval.

This CL also makes ganesh_app use this scheduler to animate.

R=jamesr@chromium.org, ernstm@chromium.org

Review URL: https://codereview.chromium.org/731893002
This commit is contained in:
Adam Barth
2014-11-17 11:24:07 -08:00
parent 9fe3014a74
commit ad91aac5ad
7 changed files with 295 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
# Copyright 2014 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.
static_library("scheduler") {
sources = [
"scheduler.cc",
"scheduler.h",
"time_interval.cc",
"time_interval.h",
"timer.cc",
"timer.h",
]
deps = [
"//base",
]
}

View File

@@ -0,0 +1,45 @@
// Copyright 2014 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.
#include "sky/scheduler/scheduler.h"
namespace sky {
Scheduler::Client::~Client() {
}
Scheduler::Scheduler(Client* client,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: client_(client), timer_(this, task_runner) {
}
Scheduler::~Scheduler() {
}
void Scheduler::SetNeedsFrame() {
timer_.SetEnabled(true);
}
void Scheduler::UpdateFrameDuration(base::TimeDelta estimate) {
frame_duration_ = estimate;
UpdateTimerInterval();
}
void Scheduler::UpdateVSync(const TimeInterval& vsync) {
vsync_ = vsync;
UpdateTimerInterval();
}
void Scheduler::UpdateTimerInterval() {
TimeInterval interval = vsync_;
interval.base -= frame_duration_;
timer_.SetInterval(interval);
}
void Scheduler::OnTimerTick(base::TimeTicks now) {
timer_.SetEnabled(false);
client_->BeginFrame(now, vsync_.NextAfter(now));
// We might be deleted here.
}
}

View File

@@ -0,0 +1,45 @@
// Copyright 2014 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 SKY_SCHEDULER_SCHEDULER_H_
#define SKY_SCHEDULER_SCHEDULER_H_
#include "base/single_thread_task_runner.h"
#include "base/time/time.h"
#include "sky/scheduler/timer.h"
namespace sky {
class Scheduler : public Timer::Client {
public:
class Client {
public:
virtual void BeginFrame(base::TimeTicks frame_time,
base::TimeTicks deadline) = 0;
protected:
virtual ~Client();
};
Scheduler(Client* client,
scoped_refptr<base::SingleThreadTaskRunner> task_runner);
~Scheduler();
void UpdateFrameDuration(base::TimeDelta estimate);
void UpdateVSync(const TimeInterval& vsync);
void SetNeedsFrame();
private:
void UpdateTimerInterval();
void OnTimerTick(base::TimeTicks now) override;
Client* client_;
Timer timer_;
TimeInterval vsync_;
base::TimeDelta frame_duration_;
};
}
#endif // SKY_SCHEDULER_SCHEDULER_H_

View File

@@ -0,0 +1,17 @@
// Copyright 2014 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.
#include "sky/scheduler/time_interval.h"
#include <cstdlib>
namespace sky {
base::TimeTicks TimeInterval::NextAfter(base::TimeTicks when) {
int64 offset = std::abs((when - base).ToInternalValue());
base::TimeDelta excess =
base::TimeDelta::FromInternalValue(offset % duration.ToInternalValue());
return when + duration - excess;
}
}

View File

@@ -0,0 +1,24 @@
// Copyright 2014 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 SKY_SCHEDULER_TIME_INTERVAL_H_
#define SKY_SCHEDULER_TIME_INTERVAL_H_
#include "base/time/time.h"
namespace sky {
struct TimeInterval {
TimeInterval() {}
TimeInterval(base::TimeTicks base, base::TimeDelta duration)
: base(base), duration(duration) {}
base::TimeTicks NextAfter(base::TimeTicks when);
base::TimeTicks base;
base::TimeDelta duration;
};
}
#endif // SKY_SCHEDULER_TIME_INTERVAL_H_

View File

@@ -0,0 +1,94 @@
// Copyright 2014 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.
#include "sky/scheduler/timer.h"
#include <cstdlib>
#include "base/bind.h"
#include "base/tracked_objects.h"
namespace sky {
// We're willing to slop around 1/4 of a tick duration to avoid trashing our
// client with irregular ticks.
static const int64 kTickSlop = 4;
Timer::Client::~Client() {
}
Timer::Timer(Client* client,
scoped_refptr<base::SingleThreadTaskRunner> task_runner)
: client_(client),
task_runner_(task_runner),
enabled_(false),
weak_factory_(this) {
DCHECK(client_);
}
Timer::~Timer() {
}
void Timer::SetEnabled(bool enabled) {
enabled_ = enabled;
if (enabled_ && current_target_.is_null())
ScheduleNextTick(base::TimeTicks::Now());
}
void Timer::SetInterval(const TimeInterval& interval) {
interval_ = interval;
// We don't have a tick scheduled, so there's no need to reschedule it.
if (current_target_.is_null())
return;
base::TimeTicks now = base::TimeTicks::Now();
base::TimeTicks new_target = NextTickTarget(now);
base::TimeDelta delta = base::TimeDelta::FromInternalValue(
std::abs((new_target - current_target_).ToInternalValue()));
if (delta * kTickSlop < interval_.duration)
return;
current_target_ = base::TimeTicks();
weak_factory_.InvalidateWeakPtrs();
PostTickTask(now, new_target);
}
base::TimeTicks Timer::NextTickTarget(base::TimeTicks now) {
base::TimeTicks target = interval_.NextAfter(now);
// If we're targeting a time that's too soon since the last tick, we push out
// the target to the next tick.
if ((target - last_tick_) * kTickSlop < interval_.duration)
target += interval_.duration;
return target;
}
void Timer::ScheduleNextTick(base::TimeTicks now) {
PostTickTask(now, NextTickTarget(now));
}
void Timer::PostTickTask(base::TimeTicks now, base::TimeTicks target) {
DCHECK(current_target_.is_null());
current_target_ = target;
task_runner_->PostDelayedTask(
FROM_HERE, base::Bind(&Timer::OnTimerFired, weak_factory_.GetWeakPtr()),
current_target_ - now);
}
void Timer::OnTimerFired() {
current_target_ = base::TimeTicks();
if (!enabled_)
return;
base::TimeTicks now = base::TimeTicks::Now();
ScheduleNextTick(now);
last_tick_ = now;
client_->OnTimerTick(now);
// We might be deleted here.
}
}

View File

@@ -0,0 +1,52 @@
// Copyright 2014 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 SKY_SCHEDULER_TIMER_H_
#define SKY_SCHEDULER_TIMER_H_
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/time/time.h"
#include "sky/scheduler/time_interval.h"
namespace sky {
class Timer {
public:
class Client {
public:
virtual void OnTimerTick(base::TimeTicks now) = 0;
protected:
virtual ~Client();
};
Timer(Client* client,
scoped_refptr<base::SingleThreadTaskRunner> task_runner);
~Timer();
void SetInterval(const TimeInterval& parameters);
void SetEnabled(bool enabled);
private:
base::TimeTicks NextTickTarget(base::TimeTicks now);
void ScheduleNextTick(base::TimeTicks now);
void PostTickTask(base::TimeTicks now, base::TimeTicks target);
void OnTimerFired();
Client* client_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
TimeInterval interval_;
base::TimeTicks last_tick_;
base::TimeTicks current_target_;
bool enabled_;
base::WeakPtrFactory<Timer> weak_factory_;
};
}
#endif // SKY_SCHEDULER_TIMER_H_