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 "base/logging.h" |
| 6 #include "blimp/engine/renderer/frame_scheduler.h" |
| 7 |
| 8 namespace blimp { |
| 9 namespace engine { |
| 10 namespace { |
| 11 // This is the temporary frame delay to keep pages which make animation requests |
| 12 // but don't mutate the state on the engine from running main frames |
| 13 // back-to-back. We need smarter throttling of engine updates. See |
| 14 // crbug.com/597829. |
| 15 constexpr base::TimeDelta kDefaultFrameDelay = |
| 16 base::TimeDelta::FromMilliseconds(30); |
| 17 } // namespace |
| 18 |
| 19 FrameScheduler::FrameScheduler( |
| 20 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 21 FrameSchedulerClient* client) |
| 22 : FrameScheduler(kDefaultFrameDelay, std::move(task_runner), client) {} |
| 23 |
| 24 FrameScheduler::FrameScheduler( |
| 25 base::TimeDelta frame_delay, |
| 26 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 27 FrameSchedulerClient* client) |
| 28 : next_frame_time_(base::TimeTicks::Now()), |
| 29 frame_delay_(frame_delay), |
| 30 client_(client) { |
| 31 DCHECK(client_); |
| 32 frame_tick_timer_.SetTaskRunner(std::move(task_runner)); |
| 33 } |
| 34 |
| 35 FrameScheduler::~FrameScheduler() = default; |
| 36 |
| 37 void FrameScheduler::ScheduleFrameUpdate() { |
| 38 if (needs_frame_update_) |
| 39 return; |
| 40 |
| 41 needs_frame_update_ = true; |
| 42 ScheduleFrameUpdateIfNecessary(); |
| 43 } |
| 44 |
| 45 void FrameScheduler::DidSendFrameUpdateToClient() { |
| 46 DCHECK(!frame_ack_pending_) << "We can have only frame update in flight"; |
| 47 |
| 48 frame_ack_pending_ = true; |
| 49 } |
| 50 |
| 51 void FrameScheduler::DidReceiveFrameUpdateAck() { |
| 52 DCHECK(frame_ack_pending_); |
| 53 |
| 54 frame_ack_pending_ = false; |
| 55 ScheduleFrameUpdateIfNecessary(); |
| 56 } |
| 57 |
| 58 void FrameScheduler::ScheduleFrameUpdateIfNecessary() { |
| 59 // If we can't produce main frame updates right now, don't schedule a task. |
| 60 if (!ShouldProduceFrameUpdates()) { |
| 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(&FrameScheduler::StartFrameUpdate, base::Unretained(this))); |
| 72 } |
| 73 |
| 74 bool FrameScheduler::ShouldProduceFrameUpdates() const { |
| 75 return needs_frame_update_ && !frame_ack_pending_; |
| 76 } |
| 77 |
| 78 void FrameScheduler::StartFrameUpdate() { |
| 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 |