Don't consider a Bluetooth adapter present until it has an address.
[chromium-blink-merge.git] / cc / CCDelayBasedTimeSource.cpp
blob0815f255ed51d7d761052258f6d71c621a012144
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.
5 #include "config.h"
7 #include "CCDelayBasedTimeSource.h"
9 #include "TraceEvent.h"
10 #include <algorithm>
11 #include <wtf/CurrentTime.h>
12 #include <wtf/MathExtras.h>
14 namespace WebCore {
16 namespace {
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 reactivation.
20 const double doubleTickThreshold = 0.25;
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 immediate phase change.
24 // If the changes are within the thresholds, the change will take place on the next tick.
25 // If either change is outside the thresholds, the next tick will be canceled and reissued immediately.
26 const double intervalChangeThreshold = 0.25;
27 const double phaseChangeThreshold = 0.25;
32 PassRefPtr<CCDelayBasedTimeSource> CCDelayBasedTimeSource::create(double interval, CCThread* thread)
34 return adoptRef(new CCDelayBasedTimeSource(interval, thread));
37 CCDelayBasedTimeSource::CCDelayBasedTimeSource(double intervalSeconds, CCThread* thread)
38 : m_client(0)
39 , m_hasTickTarget(false)
40 , m_lastTickTime(0)
41 , m_currentParameters(intervalSeconds, 0)
42 , m_nextParameters(intervalSeconds, 0)
43 , m_state(STATE_INACTIVE)
44 , m_timer(thread, this)
46 turnOffVerifier();
49 void CCDelayBasedTimeSource::setActive(bool active)
51 TRACE_EVENT1("cc", "CCDelayBasedTimeSource::setActive", "active", active);
52 if (!active) {
53 m_state = STATE_INACTIVE;
54 m_timer.stop();
55 return;
58 if (m_state == STATE_STARTING || m_state == STATE_ACTIVE)
59 return;
61 if (!m_hasTickTarget) {
62 // Becoming active the first time is deferred: we post a 0-delay task. When
63 // it runs, we use that to establish the timebase, become truly active, and
64 // fire the first tick.
65 m_state = STATE_STARTING;
66 m_timer.startOneShot(0);
67 return;
70 m_state = STATE_ACTIVE;
72 double now = monotonicTimeNow();
73 postNextTickTask(now);
76 double CCDelayBasedTimeSource::lastTickTime()
78 return m_lastTickTime;
81 double CCDelayBasedTimeSource::nextTickTimeIfActivated()
83 return active() ? m_currentParameters.tickTarget : nextTickTarget(monotonicTimeNow());
86 void CCDelayBasedTimeSource::onTimerFired()
88 ASSERT(m_state != STATE_INACTIVE);
90 double now = monotonicTimeNow();
91 m_lastTickTime = now;
93 if (m_state == STATE_STARTING) {
94 setTimebaseAndInterval(now, m_currentParameters.interval);
95 m_state = STATE_ACTIVE;
98 postNextTickTask(now);
100 // Fire the tick
101 if (m_client)
102 m_client->onTimerTick();
105 void CCDelayBasedTimeSource::setTimebaseAndInterval(double timebase, double intervalSeconds)
107 m_nextParameters.interval = intervalSeconds;
108 m_nextParameters.tickTarget = timebase;
109 m_hasTickTarget = true;
111 if (m_state != STATE_ACTIVE) {
112 // If we aren't active, there's no need to reset the timer.
113 return;
116 // If the change in interval is larger than the change threshold,
117 // request an immediate reset.
118 double intervalDelta = std::abs(intervalSeconds - m_currentParameters.interval);
119 double intervalChange = intervalDelta / intervalSeconds;
120 if (intervalChange > intervalChangeThreshold) {
121 setActive(false);
122 setActive(true);
123 return;
126 // 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
128 // 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
130 // 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.
132 double targetDelta = std::abs(timebase - m_currentParameters.tickTarget);
133 double phaseChange = fmod(targetDelta, intervalSeconds) / intervalSeconds;
134 if (phaseChange > phaseChangeThreshold && phaseChange < (1.0 - phaseChangeThreshold)) {
135 setActive(false);
136 setActive(true);
137 return;
141 double CCDelayBasedTimeSource::monotonicTimeNow() const
143 return monotonicallyIncreasingTime();
146 // This code tries to achieve an average tick rate as close to m_intervalMs as possible.
147 // 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
149 // posted as 16 or 17.
150 // 2. A delayed task may come back a bit late (a few ms), or really late (frames later)
152 // The basic idea with this scheduler here is to keep track of where we *want* to run in
153 // m_tickTarget. We update this with the exact interval.
155 // Then, when we post our task, we take the floor of (m_tickTarget and now()). If we
156 // started at now=0, and 60FPs:
157 // now=0 target=16.667 postDelayedTask(16)
159 // When our callback runs, we figure out how far off we were from that goal. Because of the flooring
160 // operation, and assuming our timer runs exactly when it should, this yields:
161 // now=16 target=16.667
163 // 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 difference between our target
165 // and now:
166 // now=16 tickTarget=16.667 newTarget=33.333 --> postDelayedTask(floor(33.333 - 16)) --> postDelayedTask(17)
168 // Over time, with no late tasks, this leads to us posting tasks like this:
169 // now=0 tickTarget=0 newTarget=16.667 --> tick(), postDelayedTask(16)
170 // now=16 tickTarget=16.667 newTarget=33.333 --> tick(), postDelayedTask(17)
171 // now=33 tickTarget=33.333 newTarget=50.000 --> tick(), postDelayedTask(17)
172 // now=50 tickTarget=50.000 newTarget=66.667 --> tick(), postDelayedTask(16)
174 // We treat delays in tasks differently depending on the amount of delay we encounter. Suppose we
175 // posted a task with a target=16.667:
176 // Case 1: late but not unrecoverably-so
177 // now=18 tickTarget=16.667
179 // Case 2: so late we obviously missed the tick
180 // now=25.0 tickTarget=16.667
182 // 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:
184 // now=18 tickTarget=16.667 newTarget=33.333 --> tick(), postDelayedTask(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.
187 // 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(), postDelayedTask(floor(50.000-37)) --> postDelayedTask(13)
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)
193 double newInterval = m_nextParameters.interval;
194 double intervalsElapsed = floor((now - m_nextParameters.tickTarget) / newInterval);
195 double lastEffectiveTick = m_nextParameters.tickTarget + newInterval * intervalsElapsed;
196 double newTickTarget = lastEffectiveTick + newInterval;
197 ASSERT(newTickTarget > now);
199 // Avoid double ticks when:
200 // 1) Turning off the timer and turning it right back on.
201 // 2) Jittery data is passed to setTimebaseAndInterval().
202 if (newTickTarget - m_lastTickTime <= newInterval * doubleTickThreshold)
203 newTickTarget += newInterval;
205 return newTickTarget;
208 void CCDelayBasedTimeSource::postNextTickTask(double now)
210 double newTickTarget = nextTickTarget(now);
212 // Post another task *before* the tick and update state
213 double delay = newTickTarget - now;
214 ASSERT(delay <= m_nextParameters.interval * (1.0 + doubleTickThreshold));
215 m_timer.startOneShot(delay);
217 m_nextParameters.tickTarget = newTickTarget;
218 m_currentParameters = m_nextParameters;