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/auto_reset.h" |
| 6 #include "base/logging.h" |
| 7 #include "blimp/engine/renderer/frame_scheduler.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 FrameScheduler::FrameScheduler( |
| 21 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 22 FrameSchedulerClient* client) |
| 23 : FrameScheduler(kDefaultFrameDelay, std::move(task_runner), client) {} |
| 24 |
| 25 FrameScheduler::FrameScheduler( |
| 26 base::TimeDelta frame_delay, |
| 27 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 28 FrameSchedulerClient* client) |
| 29 : next_frame_time_(base::TimeTicks::Now()), |
| 30 frame_delay_(frame_delay), |
| 31 client_(client) { |
| 32 DCHECK(client_); |
| 33 frame_tick_timer_.SetTaskRunner(std::move(task_runner)); |
| 34 } |
| 35 |
| 36 FrameScheduler::~FrameScheduler() = default; |
| 37 |
| 38 void FrameScheduler::ScheduleFrameUpdate() { |
| 39 if (needs_frame_update_) |
| 40 return; |
| 41 |
| 42 needs_frame_update_ = true; |
| 43 ScheduleFrameUpdateIfNecessary(); |
| 44 } |
| 45 |
| 46 void FrameScheduler::DidSendFrameUpdateToClient() { |
| 47 DCHECK(in_frame_update_); |
| 48 DCHECK(!frame_ack_pending_) << "We can have only frame update in flight"; |
| 49 |
| 50 frame_ack_pending_ = true; |
| 51 } |
| 52 |
| 53 void FrameScheduler::DidReceiveFrameUpdateAck() { |
| 54 DCHECK(frame_ack_pending_); |
| 55 |
| 56 frame_ack_pending_ = false; |
| 57 ScheduleFrameUpdateIfNecessary(); |
| 58 } |
| 59 |
| 60 void FrameScheduler::ScheduleFrameUpdateIfNecessary() { |
| 61 // If we can't produce main frame updates right now, don't schedule a task. |
| 62 if (!ShouldProduceFrameUpdates()) { |
| 63 return; |
| 64 } |
| 65 |
| 66 // If a task has already been scheduled, we don't need to schedule again. |
| 67 if (frame_tick_timer_.IsRunning()) |
| 68 return; |
| 69 |
| 70 base::TimeDelta delay = next_frame_time_ - base::TimeTicks::Now(); |
| 71 frame_tick_timer_.Start( |
| 72 FROM_HERE, delay, |
| 73 base::Bind(&FrameScheduler::StartFrameUpdate, base::Unretained(this))); |
| 74 } |
| 75 |
| 76 bool FrameScheduler::ShouldProduceFrameUpdates() const { |
| 77 return needs_frame_update_ && !frame_ack_pending_; |
| 78 } |
| 79 |
| 80 void FrameScheduler::StartFrameUpdate() { |
| 81 DCHECK(!in_frame_update_); |
| 82 DCHECK(needs_frame_update_); |
| 83 |
| 84 // If an update was sent to the client since this request, we can not start |
| 85 // another frame. Early out here, we'll come back when an Ack is received from |
| 86 // the client. |
| 87 if (frame_ack_pending_) |
| 88 return; |
| 89 |
| 90 needs_frame_update_ = false; |
| 91 next_frame_time_ = base::TimeTicks::Now() + frame_delay_; |
| 92 { |
| 93 base::AutoReset<bool> in_frame_update(&in_frame_update_, true); |
| 94 client_->StartFrameUpdate(); |
| 95 } |
| 96 } |
| 97 |
| 98 } // namespace engine |
| 99 } // namespace blimp |
OLD | NEW |