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 ScheduleFrameUpdateIfNecessary(); | |
Wez
2016/10/21 01:40:35
Wait... this call will never have any effect, sure
Khushal
2016/10/21 03:13:56
Woops. :P Will remove.
Khushal
2016/10/24 22:49:09
Done.
| |
50 } | |
51 | |
52 void FrameScheduler::DidReceiveFrameUpdateAck() { | |
53 DCHECK(frame_ack_pending_); | |
54 | |
55 frame_ack_pending_ = false; | |
56 ScheduleFrameUpdateIfNecessary(); | |
57 } | |
58 | |
59 void FrameScheduler::ScheduleFrameUpdateIfNecessary() { | |
60 // If we can't produce main frame updates right now, don't schedule a task. | |
61 if (!ShouldProduceFrameUpdates()) { | |
62 return; | |
63 } | |
64 | |
65 // If a task has already been scheduled, we don't need to schedule again. | |
66 if (frame_tick_timer_.IsRunning()) | |
67 return; | |
68 | |
69 base::TimeDelta delay = next_frame_time_ - base::TimeTicks::Now(); | |
70 frame_tick_timer_.Start( | |
71 FROM_HERE, delay, | |
72 base::Bind(&FrameScheduler::StartFrameUpdate, base::Unretained(this))); | |
73 } | |
74 | |
75 bool FrameScheduler::ShouldProduceFrameUpdates() const { | |
76 return needs_frame_update_ && !frame_ack_pending_; | |
77 } | |
78 | |
79 void FrameScheduler::StartFrameUpdate() { | |
80 DCHECK(needs_frame_update_); | |
81 | |
82 // If an update was sent to the client since this request, we can not start | |
83 // another frame. Early out here, we'll come back when an Ack is received from | |
84 // the client. | |
Wez
2016/10/20 23:53:27
It's really not clear from this comment under whic
Khushal
2016/10/21 00:03:11
Hmm, so StartFrameUpdate won't necessarily result
Wez
2016/10/21 01:40:35
Hmmm. Resetting the timer in DidSendFrameUpdateToC
Khushal
2016/10/21 03:13:56
Nope, that's not possible. DidSendFrameUpdateToCli
Khushal
2016/10/24 22:49:09
Done.
| |
85 if (frame_ack_pending_) | |
86 return; | |
87 | |
88 needs_frame_update_ = false; | |
89 next_frame_time_ = base::TimeTicks::Now() + frame_delay_; | |
90 client_->StartFrameUpdate(); | |
91 } | |
92 | |
93 } // namespace engine | |
94 } // namespace blimp | |
OLD | NEW |