Chromium Code Reviews| Index: blimp/engine/renderer/scheduler.cc |
| diff --git a/blimp/engine/renderer/scheduler.cc b/blimp/engine/renderer/scheduler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c063e6022deee7ea72748db49937c6f07838e86b |
| --- /dev/null |
| +++ b/blimp/engine/renderer/scheduler.cc |
| @@ -0,0 +1,93 @@ |
| +// Copyright 2016 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 "blimp/engine/renderer/scheduler.h" |
| + |
| +#include "base/logging.h" |
| + |
| +namespace blimp { |
| +namespace engine { |
| +namespace { |
| +// This is the temporary frame delay to keep pages which make animation requests |
| +// but don't mutate the state on the engine from running main frames |
| +// back-to-back. We need smarter throttling of engine updates. See |
| +// crbug.com/597829. |
| +constexpr base::TimeDelta kDefaultFrameDelay = |
| + base::TimeDelta::FromMilliseconds(30); |
| +} // namespace |
| + |
| +Scheduler::Scheduler(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| + SchedulerClient* client) |
| + : Scheduler(kDefaultFrameDelay, std::move(task_runner), client) {} |
| + |
| +Scheduler::Scheduler(base::TimeDelta frame_delay, |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| + SchedulerClient* client) |
| + : next_frame_time_(base::TimeTicks::Now()), |
| + frame_delay_(frame_delay), |
| + client_(client) { |
| + DCHECK(client_); |
| + frame_tick_timer_.SetTaskRunner(std::move(task_runner)); |
| +} |
| + |
| +Scheduler::~Scheduler() = default; |
| + |
| +void Scheduler::SetNeedsFrameUpdate() { |
| + if (needs_frame_update_) |
| + return; |
| + |
| + needs_frame_update_ = true; |
| + ScheduleMainFrameIfNecessary(); |
| +} |
| + |
| +void Scheduler::DidSendFrameUpdateToClient() { |
| + DCHECK(!frame_ack_pending_) << "We can have only frame update in flight"; |
|
Wez
2016/10/19 22:01:34
nit: "...only one frame..."
That said, I'd recomm
Khushal
2016/10/20 01:29:33
The reason why this is an error is because the Sch
|
| + |
| + frame_ack_pending_ = true; |
| + ScheduleMainFrameIfNecessary(); |
| +} |
| + |
| +void Scheduler::DidReceiveFrameUpdateAck() { |
| + DCHECK(frame_ack_pending_); |
| + |
| + frame_ack_pending_ = false; |
| + ScheduleMainFrameIfNecessary(); |
| +} |
| + |
| +void Scheduler::ScheduleMainFrameIfNecessary() { |
| + // If we can't produce main frame updates right now, don't schedule a task. |
| + if (!CanProduceMainFrames()) { |
| + return; |
| + } |
| + |
| + // If a task has already been scheduled, we don't need to schedule again. |
| + if (frame_tick_timer_.IsRunning()) |
| + return; |
| + |
| + base::TimeDelta delay = next_frame_time_ - base::TimeTicks::Now(); |
| + frame_tick_timer_.Start( |
| + FROM_HERE, delay, |
| + base::Bind(&Scheduler::StartMainFrame, base::Unretained(this))); |
| +} |
| + |
| +bool Scheduler::CanProduceMainFrames() const { |
| + return needs_frame_update_ && !frame_ack_pending_; |
|
Wez
2016/10/19 22:01:34
This logic doesn't seem to match the function name
Khushal
2016/10/20 01:29:33
I just renamed it to ShouldProduceMainFrames, that
|
| +} |
| + |
| +void Scheduler::StartMainFrame() { |
| + DCHECK(needs_frame_update_); |
| + |
| + // If an update was sent to the client since this request, we can not start |
| + // another frame. Early out here, we'll come back when an Ack is received from |
| + // the client. |
| + if (frame_ack_pending_) |
| + return; |
| + |
| + needs_frame_update_ = false; |
| + next_frame_time_ = base::TimeTicks::Now() + frame_delay_; |
|
Wez
2016/10/19 22:15:27
nit: Is this the desired next-frame-time, bearing
Khushal
2016/10/20 01:29:33
Yeah, but if it is triggered in response to DidRec
Wez
2016/10/20 23:53:27
Acknowledged.
|
| + client_->StartFrameUpdate(); |
| +} |
| + |
| +} // namespace engine |
| +} // namespace blimp |