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