| 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 "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
| 10 #include "base/logging.h" |
| 10 #include <algorithm> | 11 #include <algorithm> |
| 11 #include <wtf/CurrentTime.h> | 12 #include <wtf/CurrentTime.h> |
| 12 #include <wtf/MathExtras.h> | 13 #include <wtf/MathExtras.h> |
| 13 | 14 |
| 14 namespace cc { | 15 namespace cc { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // doubleTickThreshold prevents ticks from running within the specified fraction
of an interval. | 19 // 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 // This helps account for jitter in the timebase as well as quick timer reactiva
tion. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 return m_lastTickTime; | 84 return m_lastTickTime; |
| 84 } | 85 } |
| 85 | 86 |
| 86 base::TimeTicks CCDelayBasedTimeSource::nextTickTime() | 87 base::TimeTicks CCDelayBasedTimeSource::nextTickTime() |
| 87 { | 88 { |
| 88 return active() ? m_currentParameters.tickTarget : base::TimeTicks(); | 89 return active() ? m_currentParameters.tickTarget : base::TimeTicks(); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void CCDelayBasedTimeSource::onTimerFired() | 92 void CCDelayBasedTimeSource::onTimerFired() |
| 92 { | 93 { |
| 93 ASSERT(m_state != STATE_INACTIVE); | 94 DCHECK(m_state != STATE_INACTIVE); |
| 94 | 95 |
| 95 base::TimeTicks now = this->now(); | 96 base::TimeTicks now = this->now(); |
| 96 m_lastTickTime = now; | 97 m_lastTickTime = now; |
| 97 | 98 |
| 98 if (m_state == STATE_STARTING) { | 99 if (m_state == STATE_STARTING) { |
| 99 setTimebaseAndInterval(now, m_currentParameters.interval); | 100 setTimebaseAndInterval(now, m_currentParameters.interval); |
| 100 m_state = STATE_ACTIVE; | 101 m_state = STATE_ACTIVE; |
| 101 } | 102 } |
| 102 | 103 |
| 103 postNextTickTask(now); | 104 postNextTickTask(now); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 // This brings us back to 18+15 = 33, which was where we would have been if the
task hadn't been late. | 196 // This brings us back to 18+15 = 33, which was where we would have been if the
task hadn't been late. |
| 196 // | 197 // |
| 197 // For the really late delay, we we move to the next logical tick. The timebase
is not reset. | 198 // For the really late delay, we we move to the next logical tick. The timebase
is not reset. |
| 198 // now=37 tickTarget=16.667 newTarget=50.000 --> tick(), postDelayedTas
k(floor(50.000-37)) --> postDelayedTask(13) | 199 // now=37 tickTarget=16.667 newTarget=50.000 --> tick(), postDelayedTas
k(floor(50.000-37)) --> postDelayedTask(13) |
| 199 base::TimeTicks CCDelayBasedTimeSource::nextTickTarget(base::TimeTicks now) | 200 base::TimeTicks CCDelayBasedTimeSource::nextTickTarget(base::TimeTicks now) |
| 200 { | 201 { |
| 201 base::TimeDelta newInterval = m_nextParameters.interval; | 202 base::TimeDelta newInterval = m_nextParameters.interval; |
| 202 int intervalsElapsed = static_cast<int>(floor((now - m_nextParameters.tickTa
rget).InSecondsF() / newInterval.InSecondsF())); | 203 int intervalsElapsed = static_cast<int>(floor((now - m_nextParameters.tickTa
rget).InSecondsF() / newInterval.InSecondsF())); |
| 203 base::TimeTicks lastEffectiveTick = m_nextParameters.tickTarget + newInterva
l * intervalsElapsed; | 204 base::TimeTicks lastEffectiveTick = m_nextParameters.tickTarget + newInterva
l * intervalsElapsed; |
| 204 base::TimeTicks newTickTarget = lastEffectiveTick + newInterval; | 205 base::TimeTicks newTickTarget = lastEffectiveTick + newInterval; |
| 205 ASSERT(newTickTarget > now); | 206 DCHECK(newTickTarget > now); |
| 206 | 207 |
| 207 // Avoid double ticks when: | 208 // Avoid double ticks when: |
| 208 // 1) Turning off the timer and turning it right back on. | 209 // 1) Turning off the timer and turning it right back on. |
| 209 // 2) Jittery data is passed to setTimebaseAndInterval(). | 210 // 2) Jittery data is passed to setTimebaseAndInterval(). |
| 210 if (newTickTarget - m_lastTickTime <= newInterval / static_cast<int>(1.0 / d
oubleTickThreshold)) | 211 if (newTickTarget - m_lastTickTime <= newInterval / static_cast<int>(1.0 / d
oubleTickThreshold)) |
| 211 newTickTarget += newInterval; | 212 newTickTarget += newInterval; |
| 212 | 213 |
| 213 return newTickTarget; | 214 return newTickTarget; |
| 214 } | 215 } |
| 215 | 216 |
| 216 void CCDelayBasedTimeSource::postNextTickTask(base::TimeTicks now) | 217 void CCDelayBasedTimeSource::postNextTickTask(base::TimeTicks now) |
| 217 { | 218 { |
| 218 base::TimeTicks newTickTarget = nextTickTarget(now); | 219 base::TimeTicks newTickTarget = nextTickTarget(now); |
| 219 | 220 |
| 220 // Post another task *before* the tick and update state | 221 // Post another task *before* the tick and update state |
| 221 base::TimeDelta delay = newTickTarget - now; | 222 base::TimeDelta delay = newTickTarget - now; |
| 222 ASSERT(delay.InMillisecondsF() <= | 223 DCHECK(delay.InMillisecondsF() <= |
| 223 m_nextParameters.interval.InMillisecondsF() * (1.0 + doubleTickThresh
old)); | 224 m_nextParameters.interval.InMillisecondsF() * (1.0 + doubleTickThresh
old)); |
| 224 m_timer.startOneShot(delay.InSecondsF()); | 225 m_timer.startOneShot(delay.InSecondsF()); |
| 225 | 226 |
| 226 m_nextParameters.tickTarget = newTickTarget; | 227 m_nextParameters.tickTarget = newTickTarget; |
| 227 m_currentParameters = m_nextParameters; | 228 m_currentParameters = m_nextParameters; |
| 228 } | 229 } |
| 229 | 230 |
| 230 } // namespace cc | 231 } // namespace cc |
| OLD | NEW |