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