OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/engine/renderer/scheduler.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace blimp { |
| 10 namespace engine { |
| 11 namespace { |
| 12 // This is the temporary frame delay to keep pages which make animation requests |
| 13 // but don't mutate the state on the engine from running main frames |
| 14 // back-to-back. We need smarter throttling of engine updates. See |
| 15 // crbug.com/597829. |
| 16 constexpr base::TimeDelta kDefaultFrameDelay = |
| 17 base::TimeDelta::FromMilliseconds(30); |
| 18 } // namespace |
| 19 |
| 20 Scheduler::Scheduler(scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 21 SchedulerClient* client) |
| 22 : Scheduler(kDefaultFrameDelay, std::move(task_runner), client) {} |
| 23 |
| 24 Scheduler::Scheduler(base::TimeDelta frame_delay, |
| 25 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 26 SchedulerClient* client) |
| 27 : next_frame_time_(base::TimeTicks::Now()), |
| 28 frame_delay_(frame_delay), |
| 29 client_(client) { |
| 30 DCHECK(client_); |
| 31 frame_tick_timer_.SetTaskRunner(std::move(task_runner)); |
| 32 } |
| 33 |
| 34 Scheduler::~Scheduler() = default; |
| 35 |
| 36 void Scheduler::SetNeedsFrameUpdate() { |
| 37 if (needs_frame_update_) |
| 38 return; |
| 39 |
| 40 needs_frame_update_ = true; |
| 41 ScheduleMainFrameIfNecessary(); |
| 42 } |
| 43 |
| 44 void Scheduler::DidSendFrameUpdateToClient() { |
| 45 DCHECK(!frame_ack_pending_) << "We can have only frame update in flight"; |
| 46 |
| 47 frame_ack_pending_ = true; |
| 48 ScheduleMainFrameIfNecessary(); |
| 49 } |
| 50 |
| 51 void Scheduler::DidReceiveFrameUpdateAck() { |
| 52 DCHECK(frame_ack_pending_); |
| 53 |
| 54 frame_ack_pending_ = false; |
| 55 ScheduleMainFrameIfNecessary(); |
| 56 } |
| 57 |
| 58 void Scheduler::ScheduleMainFrameIfNecessary() { |
| 59 // If we can't produce main frame updates right now, don't schedule a task. |
| 60 if (!CanProduceMainFrames()) { |
| 61 return; |
| 62 } |
| 63 |
| 64 // If a task has already been scheduled, we don't need to schedule again. |
| 65 if (frame_tick_timer_.IsRunning()) |
| 66 return; |
| 67 |
| 68 base::TimeDelta delay = next_frame_time_ - base::TimeTicks::Now(); |
| 69 frame_tick_timer_.Start( |
| 70 FROM_HERE, delay, |
| 71 base::Bind(&Scheduler::StartMainFrame, base::Unretained(this))); |
| 72 } |
| 73 |
| 74 bool Scheduler::CanProduceMainFrames() const { |
| 75 return needs_frame_update_ && !frame_ack_pending_; |
| 76 } |
| 77 |
| 78 void Scheduler::StartMainFrame() { |
| 79 DCHECK(needs_frame_update_); |
| 80 |
| 81 // If an update was sent to the client since this request, we can not start |
| 82 // another frame. Early out here, we'll come back when an Ack is received from |
| 83 // the client. |
| 84 if (frame_ack_pending_) |
| 85 return; |
| 86 |
| 87 needs_frame_update_ = false; |
| 88 next_frame_time_ = base::TimeTicks::Now() + frame_delay_; |
| 89 client_->StartFrameUpdate(); |
| 90 } |
| 91 |
| 92 } // namespace engine |
| 93 } // namespace blimp |
OLD | NEW |