OLD | NEW |
| (Empty) |
1 // Copyright 2011 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 "config.h" | |
6 | |
7 #include "CCDelayBasedTimeSource.h" | |
8 | |
9 #include "TraceEvent.h" | |
10 #include <algorithm> | |
11 #include <wtf/CurrentTime.h> | |
12 #include <wtf/MathExtras.h> | |
13 | |
14 namespace cc { | |
15 | |
16 namespace { | |
17 | |
18 // doubleTickThreshold prevents ticks from running within the specified fraction
of an interval. | |
19 // This helps account for jitter in the timebase as well as quick timer reactiva
tion. | |
20 const double doubleTickThreshold = 0.25; | |
21 | |
22 // intervalChangeThreshold is the fraction of the interval that will trigger an
immediate interval change. | |
23 // phaseChangeThreshold is the fraction of the interval that will trigger an imm
ediate phase change. | |
24 // If the changes are within the thresholds, the change will take place on the n
ext tick. | |
25 // If either change is outside the thresholds, the next tick will be canceled an
d reissued immediately. | |
26 const double intervalChangeThreshold = 0.25; | |
27 const double phaseChangeThreshold = 0.25; | |
28 | |
29 } | |
30 | |
31 | |
32 PassRefPtr<CCDelayBasedTimeSource> CCDelayBasedTimeSource::create(base::TimeDelt
a interval, CCThread* thread) | |
33 { | |
34 return adoptRef(new CCDelayBasedTimeSource(interval, thread)); | |
35 } | |
36 | |
37 CCDelayBasedTimeSource::CCDelayBasedTimeSource(base::TimeDelta interval, CCThrea
d* thread) | |
38 : m_client(0) | |
39 , m_hasTickTarget(false) | |
40 , m_currentParameters(interval, base::TimeTicks()) | |
41 , m_nextParameters(interval, base::TimeTicks()) | |
42 , m_state(STATE_INACTIVE) | |
43 , m_timer(thread, this) | |
44 { | |
45 turnOffVerifier(); | |
46 } | |
47 | |
48 CCDelayBasedTimeSource::~CCDelayBasedTimeSource() | |
49 { | |
50 } | |
51 | |
52 void CCDelayBasedTimeSource::setActive(bool active) | |
53 { | |
54 TRACE_EVENT1("cc", "CCDelayBasedTimeSource::setActive", "active", active); | |
55 if (!active) { | |
56 m_state = STATE_INACTIVE; | |
57 m_timer.stop(); | |
58 return; | |
59 } | |
60 | |
61 if (m_state == STATE_STARTING || m_state == STATE_ACTIVE) | |
62 return; | |
63 | |
64 if (!m_hasTickTarget) { | |
65 // Becoming active the first time is deferred: we post a 0-delay task. W
hen | |
66 // it runs, we use that to establish the timebase, become truly active,
and | |
67 // fire the first tick. | |
68 m_state = STATE_STARTING; | |
69 m_timer.startOneShot(0); | |
70 return; | |
71 } | |
72 | |
73 m_state = STATE_ACTIVE; | |
74 | |
75 postNextTickTask(now()); | |
76 } | |
77 | |
78 bool CCDelayBasedTimeSource::active() const | |
79 { | |
80 return m_state != STATE_INACTIVE; | |
81 } | |
82 | |
83 base::TimeTicks CCDelayBasedTimeSource::lastTickTime() | |
84 { | |
85 return m_lastTickTime; | |
86 } | |
87 | |
88 base::TimeTicks CCDelayBasedTimeSource::nextTickTime() | |
89 { | |
90 return active() ? m_currentParameters.tickTarget : base::TimeTicks(); | |
91 } | |
92 | |
93 void CCDelayBasedTimeSource::onTimerFired() | |
94 { | |
95 ASSERT(m_state != STATE_INACTIVE); | |
96 | |
97 base::TimeTicks now = this->now(); | |
98 m_lastTickTime = now; | |
99 | |
100 if (m_state == STATE_STARTING) { | |
101 setTimebaseAndInterval(now, m_currentParameters.interval); | |
102 m_state = STATE_ACTIVE; | |
103 } | |
104 | |
105 postNextTickTask(now); | |
106 | |
107 // Fire the tick | |
108 if (m_client) | |
109 m_client->onTimerTick(); | |
110 } | |
111 | |
112 void CCDelayBasedTimeSource::setClient(CCTimeSourceClient* client) | |
113 { | |
114 m_client = client; | |
115 } | |
116 | |
117 void CCDelayBasedTimeSource::setTimebaseAndInterval(base::TimeTicks timebase, ba
se::TimeDelta interval) | |
118 { | |
119 m_nextParameters.interval = interval; | |
120 m_nextParameters.tickTarget = timebase; | |
121 m_hasTickTarget = true; | |
122 | |
123 if (m_state != STATE_ACTIVE) { | |
124 // If we aren't active, there's no need to reset the timer. | |
125 return; | |
126 } | |
127 | |
128 // If the change in interval is larger than the change threshold, | |
129 // request an immediate reset. | |
130 double intervalDelta = std::abs((interval - m_currentParameters.interval).In
SecondsF()); | |
131 double intervalChange = intervalDelta / interval.InSecondsF(); | |
132 if (intervalChange > intervalChangeThreshold) { | |
133 setActive(false); | |
134 setActive(true); | |
135 return; | |
136 } | |
137 | |
138 // If the change in phase is greater than the change threshold in either | |
139 // direction, request an immediate reset. This logic might result in a false | |
140 // negative if there is a simultaneous small change in the interval and the | |
141 // fmod just happens to return something near zero. Assuming the timebase | |
142 // is very recent though, which it should be, we'll still be ok because the | |
143 // old clock and new clock just happen to line up. | |
144 double targetDelta = std::abs((timebase - m_currentParameters.tickTarget).In
SecondsF()); | |
145 double phaseChange = fmod(targetDelta, interval.InSecondsF()) / interval.InS
econdsF(); | |
146 if (phaseChange > phaseChangeThreshold && phaseChange < (1.0 - phaseChangeTh
reshold)) { | |
147 setActive(false); | |
148 setActive(true); | |
149 return; | |
150 } | |
151 } | |
152 | |
153 base::TimeTicks CCDelayBasedTimeSource::now() const | |
154 { | |
155 return base::TimeTicks::Now(); | |
156 } | |
157 | |
158 // This code tries to achieve an average tick rate as close to m_interval as pos
sible. | |
159 // To do this, it has to deal with a few basic issues: | |
160 // 1. postDelayedTask can delay only at a millisecond granularity. So, 16.666
has to | |
161 // posted as 16 or 17. | |
162 // 2. A delayed task may come back a bit late (a few ms), or really late (fram
es later) | |
163 // | |
164 // The basic idea with this scheduler here is to keep track of where we *want* t
o run in | |
165 // m_tickTarget. We update this with the exact interval. | |
166 // | |
167 // Then, when we post our task, we take the floor of (m_tickTarget and now()). I
f we | |
168 // started at now=0, and 60FPs (all times in milliseconds): | |
169 // now=0 target=16.667 postDelayedTask(16) | |
170 // | |
171 // When our callback runs, we figure out how far off we were from that goal. Bec
ause of the flooring | |
172 // operation, and assuming our timer runs exactly when it should, this yields: | |
173 // now=16 target=16.667 | |
174 // | |
175 // Since we can't post a 0.667 ms task to get to now=16, we just treat this as a
tick. Then, | |
176 // we update target to be 33.333. We now post another task based on the differen
ce between our target | |
177 // and now: | |
178 // now=16 tickTarget=16.667 newTarget=33.333 --> postDelayedTask(floor
(33.333 - 16)) --> postDelayedTask(17) | |
179 // | |
180 // Over time, with no late tasks, this leads to us posting tasks like this: | |
181 // now=0 tickTarget=0 newTarget=16.667 --> tick(), postDelayedTa
sk(16) | |
182 // now=16 tickTarget=16.667 newTarget=33.333 --> tick(), postDelayedTa
sk(17) | |
183 // now=33 tickTarget=33.333 newTarget=50.000 --> tick(), postDelayedTa
sk(17) | |
184 // now=50 tickTarget=50.000 newTarget=66.667 --> tick(), postDelayedTa
sk(16) | |
185 // | |
186 // We treat delays in tasks differently depending on the amount of delay we enco
unter. Suppose we | |
187 // posted a task with a target=16.667: | |
188 // Case 1: late but not unrecoverably-so | |
189 // now=18 tickTarget=16.667 | |
190 // | |
191 // Case 2: so late we obviously missed the tick | |
192 // now=25.0 tickTarget=16.667 | |
193 // | |
194 // We treat the first case as a tick anyway, and assume the delay was | |
195 // unusual. Thus, we compute the newTarget based on the old timebase: | |
196 // now=18 tickTarget=16.667 newTarget=33.333 --> tick(), postDelayedTa
sk(floor(33.333-18)) --> postDelayedTask(15) | |
197 // This brings us back to 18+15 = 33, which was where we would have been if the
task hadn't been late. | |
198 // | |
199 // For the really late delay, we we move to the next logical tick. The timebase
is not reset. | |
200 // now=37 tickTarget=16.667 newTarget=50.000 --> tick(), postDelayedTas
k(floor(50.000-37)) --> postDelayedTask(13) | |
201 base::TimeTicks CCDelayBasedTimeSource::nextTickTarget(base::TimeTicks now) | |
202 { | |
203 base::TimeDelta newInterval = m_nextParameters.interval; | |
204 int intervalsElapsed = static_cast<int>(floor((now - m_nextParameters.tickTa
rget).InSecondsF() / newInterval.InSecondsF())); | |
205 base::TimeTicks lastEffectiveTick = m_nextParameters.tickTarget + newInterva
l * intervalsElapsed; | |
206 base::TimeTicks newTickTarget = lastEffectiveTick + newInterval; | |
207 ASSERT(newTickTarget > now); | |
208 | |
209 // Avoid double ticks when: | |
210 // 1) Turning off the timer and turning it right back on. | |
211 // 2) Jittery data is passed to setTimebaseAndInterval(). | |
212 if (newTickTarget - m_lastTickTime <= newInterval / static_cast<int>(1.0 / d
oubleTickThreshold)) | |
213 newTickTarget += newInterval; | |
214 | |
215 return newTickTarget; | |
216 } | |
217 | |
218 void CCDelayBasedTimeSource::postNextTickTask(base::TimeTicks now) | |
219 { | |
220 base::TimeTicks newTickTarget = nextTickTarget(now); | |
221 | |
222 // Post another task *before* the tick and update state | |
223 base::TimeDelta delay = newTickTarget - now; | |
224 ASSERT(delay.InMillisecondsF() <= | |
225 m_nextParameters.interval.InMillisecondsF() * (1.0 + doubleTickThresh
old)); | |
226 m_timer.startOneShot(delay.InSecondsF()); | |
227 | |
228 m_nextParameters.tickTarget = newTickTarget; | |
229 m_currentParameters = m_nextParameters; | |
230 } | |
231 | |
232 } | |
OLD | NEW |