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..07ecfe71620b68d00d73fac14fb74011abeecf1d |
| --- /dev/null |
| +++ b/blimp/engine/renderer/scheduler.cc |
| @@ -0,0 +1,95 @@ |
| +// 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" |
| +#include "blimp/engine/renderer/scheduler_client.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. |
| +const 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), |
| + weak_ptr_factory_(this) { |
| + DCHECK(client_); |
| + frame_tick_timer_.SetTaskRunner(std::move(task_runner)); |
| +} |
| + |
| +Scheduler::~Scheduler() = default; |
| + |
| +void Scheduler::SetNeedsMainFrame() { |
| + if (needs_main_frame_) |
| + return; |
| + |
| + needs_main_frame_ = true; |
| + ScheduleMainFrameIfNecessary(); |
| +} |
| + |
| +void Scheduler::DidSendFrameUpdateToClient() { |
| + DCHECK(!frame_ack_pending_) << "We can have only frame update in flight"; |
| + |
| + 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, weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +bool Scheduler::CanProduceMainFrames() const { |
| + return needs_main_frame_ && !frame_ack_pending_; |
| +} |
| + |
| +void Scheduler::StartMainFrame() { |
| + DCHECK(needs_main_frame_); |
| + |
| + // 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_main_frame_ = false; |
| + next_frame_time_ = base::TimeTicks::Now() + frame_delay_; |
|
Khushal
2016/10/12 21:00:48
I added this mostly to space out frames where the
|
| + client_->StartMainFrameUpdate(); |
| +} |
| + |
| +} // namespace engine |
| +} // namespace blimp |