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 "cc/scheduler/delay_based_time_source.h" | 5 #include "cc/scheduler/delay_based_time_source.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 | 13 |
14 namespace cc { | 14 namespace cc { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // kDoubleTickThreshold prevents ticks from running within the specified | 18 // kDoubleTickDivisor prevents ticks from running within the specified |
19 // fraction of an interval. This helps account for jitter in the timebase as | 19 // fraction of an interval. This helps account for jitter in the timebase as |
20 // well as quick timer reactivation. | 20 // well as quick timer reactivation. |
21 static const double kDoubleTickThreshold = 0.25; | 21 static const int kDoubleTickDivisor = 4; |
22 | 22 |
23 // kIntervalChangeThreshold is the fraction of the interval that will trigger an | 23 // kIntervalChangeThreshold is the fraction of the interval that will trigger an |
24 // immediate interval change. kPhaseChangeThreshold is the fraction of the | 24 // immediate interval change. kPhaseChangeThreshold is the fraction of the |
25 // interval that will trigger an immediate phase change. If the changes are | 25 // interval that will trigger an immediate phase change. If the changes are |
26 // within the thresholds, the change will take place on the next tick. If | 26 // within the thresholds, the change will take place on the next tick. If |
27 // either change is outside the thresholds, the next tick will be canceled and | 27 // either change is outside the thresholds, the next tick will be canceled and |
28 // reissued immediately. | 28 // reissued immediately. |
29 static const double kIntervalChangeThreshold = 0.25; | 29 static const double kIntervalChangeThreshold = 0.25; |
30 static const double kPhaseChangeThreshold = 0.25; | 30 static const double kPhaseChangeThreshold = 0.25; |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 scoped_refptr<DelayBasedTimeSource> DelayBasedTimeSource::Create( | 34 scoped_refptr<DelayBasedTimeSource> DelayBasedTimeSource::Create( |
35 base::TimeDelta interval, | 35 base::TimeDelta interval, |
36 base::SingleThreadTaskRunner* task_runner) { | 36 base::SingleThreadTaskRunner* task_runner) { |
37 return make_scoped_refptr(new DelayBasedTimeSource(interval, task_runner)); | 37 return make_scoped_refptr(new DelayBasedTimeSource(interval, task_runner)); |
38 } | 38 } |
39 | 39 |
40 DelayBasedTimeSource::DelayBasedTimeSource( | 40 DelayBasedTimeSource::DelayBasedTimeSource( |
41 base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner) | 41 base::TimeDelta interval, base::SingleThreadTaskRunner* task_runner) |
42 : client_(NULL), | 42 : client_(NULL), |
43 has_tick_target_(false), | 43 last_tick_time_(base::TimeTicks() - interval), |
44 current_parameters_(interval, base::TimeTicks()), | 44 current_parameters_(interval, base::TimeTicks()), |
45 next_parameters_(interval, base::TimeTicks()), | 45 next_parameters_(interval, base::TimeTicks()), |
46 state_(STATE_INACTIVE), | 46 active_(false), |
47 task_runner_(task_runner), | 47 task_runner_(task_runner), |
48 weak_factory_(this) {} | 48 weak_factory_(this) {} |
49 | 49 |
50 DelayBasedTimeSource::~DelayBasedTimeSource() {} | 50 DelayBasedTimeSource::~DelayBasedTimeSource() {} |
51 | 51 |
52 void DelayBasedTimeSource::SetActive(bool active) { | 52 void DelayBasedTimeSource::SetActive(bool active) { |
53 TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active); | 53 TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active); |
54 if (!active) { | 54 if (active == active_) |
55 state_ = STATE_INACTIVE; | 55 return; |
| 56 active_ = active; |
| 57 |
| 58 if (!active_) { |
56 weak_factory_.InvalidateWeakPtrs(); | 59 weak_factory_.InvalidateWeakPtrs(); |
57 return; | 60 return; |
58 } | 61 } |
59 | 62 |
60 if (state_ == STATE_STARTING || state_ == STATE_ACTIVE) | |
61 return; | |
62 | |
63 if (!has_tick_target_) { | |
64 // Becoming active the first time is deferred: we post a 0-delay task. | |
65 // When it runs, we use that to establish the timebase, become truly | |
66 // active, and fire the first tick. | |
67 state_ = STATE_STARTING; | |
68 task_runner_->PostTask(FROM_HERE, | |
69 base::Bind(&DelayBasedTimeSource::OnTimerFired, | |
70 weak_factory_.GetWeakPtr())); | |
71 return; | |
72 } | |
73 | |
74 state_ = STATE_ACTIVE; | |
75 | |
76 PostNextTickTask(Now()); | 63 PostNextTickTask(Now()); |
77 } | 64 } |
78 | 65 |
79 bool DelayBasedTimeSource::Active() const { return state_ != STATE_INACTIVE; } | 66 bool DelayBasedTimeSource::Active() const { return active_; } |
80 | 67 |
81 base::TimeTicks DelayBasedTimeSource::LastTickTime() { return last_tick_time_; } | 68 base::TimeTicks DelayBasedTimeSource::LastTickTime() { return last_tick_time_; } |
82 | 69 |
83 base::TimeTicks DelayBasedTimeSource::NextTickTime() { | 70 base::TimeTicks DelayBasedTimeSource::NextTickTime() { |
84 return Active() ? current_parameters_.tick_target : base::TimeTicks(); | 71 return Active() ? current_parameters_.tick_target : base::TimeTicks(); |
85 } | 72 } |
86 | 73 |
87 void DelayBasedTimeSource::OnTimerFired() { | 74 void DelayBasedTimeSource::OnTimerFired() { |
88 DCHECK(state_ != STATE_INACTIVE); | 75 DCHECK(active_); |
89 | 76 |
90 base::TimeTicks now = this->Now(); | 77 last_tick_time_ = current_parameters_.tick_target; |
91 last_tick_time_ = now; | |
92 | 78 |
93 if (state_ == STATE_STARTING) { | 79 PostNextTickTask(Now()); |
94 SetTimebaseAndInterval(now, current_parameters_.interval); | |
95 state_ = STATE_ACTIVE; | |
96 } | |
97 | |
98 PostNextTickTask(now); | |
99 | 80 |
100 // Fire the tick. | 81 // Fire the tick. |
101 if (client_) | 82 if (client_) |
102 client_->OnTimerTick(); | 83 client_->OnTimerTick(); |
103 } | 84 } |
104 | 85 |
105 void DelayBasedTimeSource::SetClient(TimeSourceClient* client) { | 86 void DelayBasedTimeSource::SetClient(TimeSourceClient* client) { |
106 client_ = client; | 87 client_ = client; |
107 } | 88 } |
108 | 89 |
109 void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase, | 90 void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase, |
110 base::TimeDelta interval) { | 91 base::TimeDelta interval) { |
111 next_parameters_.interval = interval; | 92 next_parameters_.interval = interval; |
112 next_parameters_.tick_target = timebase; | 93 next_parameters_.tick_target = timebase; |
113 has_tick_target_ = true; | |
114 | 94 |
115 if (state_ != STATE_ACTIVE) { | 95 if (!active_) { |
116 // If we aren't active, there's no need to reset the timer. | 96 // If we aren't active, there's no need to reset the timer. |
117 return; | 97 return; |
118 } | 98 } |
119 | 99 |
120 // If the change in interval is larger than the change threshold, | 100 // If the change in interval is larger than the change threshold, |
121 // request an immediate reset. | 101 // request an immediate reset. |
122 double interval_delta = | 102 double interval_delta = |
123 std::abs((interval - current_parameters_.interval).InSecondsF()); | 103 std::abs((interval - current_parameters_.interval).InSecondsF()); |
124 double interval_change = interval_delta / interval.InSecondsF(); | 104 double interval_change = interval_delta / interval.InSecondsF(); |
125 if (interval_change > kIntervalChangeThreshold) { | 105 if (interval_change > kIntervalChangeThreshold) { |
| 106 TRACE_EVENT_INSTANT0("cc", "DelayBasedTimeSource::IntervalChanged", |
| 107 TRACE_EVENT_SCOPE_THREAD); |
126 SetActive(false); | 108 SetActive(false); |
127 SetActive(true); | 109 SetActive(true); |
128 return; | 110 return; |
129 } | 111 } |
130 | 112 |
131 // If the change in phase is greater than the change threshold in either | 113 // If the change in phase is greater than the change threshold in either |
132 // direction, request an immediate reset. This logic might result in a false | 114 // direction, request an immediate reset. This logic might result in a false |
133 // negative if there is a simultaneous small change in the interval and the | 115 // negative if there is a simultaneous small change in the interval and the |
134 // fmod just happens to return something near zero. Assuming the timebase | 116 // fmod just happens to return something near zero. Assuming the timebase |
135 // is very recent though, which it should be, we'll still be ok because the | 117 // is very recent though, which it should be, we'll still be ok because the |
136 // old clock and new clock just happen to line up. | 118 // old clock and new clock just happen to line up. |
137 double target_delta = | 119 double target_delta = |
138 std::abs((timebase - current_parameters_.tick_target).InSecondsF()); | 120 std::abs((timebase - current_parameters_.tick_target).InSecondsF()); |
139 double phase_change = | 121 double phase_change = |
140 fmod(target_delta, interval.InSecondsF()) / interval.InSecondsF(); | 122 fmod(target_delta, interval.InSecondsF()) / interval.InSecondsF(); |
141 if (phase_change > kPhaseChangeThreshold && | 123 if (phase_change > kPhaseChangeThreshold && |
142 phase_change < (1.0 - kPhaseChangeThreshold)) { | 124 phase_change < (1.0 - kPhaseChangeThreshold)) { |
| 125 TRACE_EVENT_INSTANT0("cc", "DelayBasedTimeSource::PhaseChanged", |
| 126 TRACE_EVENT_SCOPE_THREAD); |
143 SetActive(false); | 127 SetActive(false); |
144 SetActive(true); | 128 SetActive(true); |
145 return; | 129 return; |
146 } | 130 } |
147 } | 131 } |
148 | 132 |
149 base::TimeTicks DelayBasedTimeSource::Now() const { | 133 base::TimeTicks DelayBasedTimeSource::Now() const { |
150 return base::TimeTicks::Now(); | 134 return base::TimeTicks::Now(); |
151 } | 135 } |
152 | 136 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 // now=18 tick_target=16.667 new_target=33.333 --> | 182 // now=18 tick_target=16.667 new_target=33.333 --> |
199 // tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15) | 183 // tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15) |
200 // This brings us back to 18+15 = 33, which was where we would have been if the | 184 // This brings us back to 18+15 = 33, which was where we would have been if the |
201 // task hadn't been late. | 185 // task hadn't been late. |
202 // | 186 // |
203 // For the really late delay, we we move to the next logical tick. The timebase | 187 // For the really late delay, we we move to the next logical tick. The timebase |
204 // is not reset. | 188 // is not reset. |
205 // now=37 tick_target=16.667 new_target=50.000 --> | 189 // now=37 tick_target=16.667 new_target=50.000 --> |
206 // tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13) | 190 // tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13) |
207 base::TimeTicks DelayBasedTimeSource::NextTickTarget(base::TimeTicks now) { | 191 base::TimeTicks DelayBasedTimeSource::NextTickTarget(base::TimeTicks now) { |
| 192 const base::TimeDelta epsilon(base::TimeDelta::FromMicroseconds(1)); |
208 base::TimeDelta new_interval = next_parameters_.interval; | 193 base::TimeDelta new_interval = next_parameters_.interval; |
209 int intervals_elapsed = | 194 int intervals_elapsed = |
210 static_cast<int>(floor((now - next_parameters_.tick_target).InSecondsF() / | 195 (now - next_parameters_.tick_target + new_interval - epsilon) / |
211 new_interval.InSecondsF())); | 196 new_interval; |
212 base::TimeTicks last_effective_tick = | 197 base::TimeTicks new_tick_target = |
213 next_parameters_.tick_target + new_interval * intervals_elapsed; | 198 next_parameters_.tick_target + new_interval * intervals_elapsed; |
214 base::TimeTicks new_tick_target = last_effective_tick + new_interval; | 199 DCHECK(now <= new_tick_target) |
215 DCHECK(now < new_tick_target) | |
216 << "now = " << now.ToInternalValue() | 200 << "now = " << now.ToInternalValue() |
217 << "; new_tick_target = " << new_tick_target.ToInternalValue() | 201 << "; new_tick_target = " << new_tick_target.ToInternalValue() |
218 << "; new_interval = " << new_interval.InMicroseconds() | 202 << "; new_interval = " << new_interval.InMicroseconds() |
219 << "; tick_target = " << next_parameters_.tick_target.ToInternalValue() | 203 << "; tick_target = " << next_parameters_.tick_target.ToInternalValue() |
220 << "; intervals_elapsed = " << intervals_elapsed | 204 << "; intervals_elapsed = " << intervals_elapsed; |
221 << "; last_effective_tick = " << last_effective_tick.ToInternalValue(); | |
222 | 205 |
223 // Avoid double ticks when: | 206 // Avoid double ticks when: |
224 // 1) Turning off the timer and turning it right back on. | 207 // 1) Turning off the timer and turning it right back on. |
225 // 2) Jittery data is passed to SetTimebaseAndInterval(). | 208 // 2) Jittery data is passed to SetTimebaseAndInterval(). |
226 if (new_tick_target - last_tick_time_ <= | 209 if (new_tick_target - last_tick_time_ <= new_interval / kDoubleTickDivisor) |
227 new_interval / static_cast<int>(1.0 / kDoubleTickThreshold)) | |
228 new_tick_target += new_interval; | 210 new_tick_target += new_interval; |
229 | 211 |
230 return new_tick_target; | 212 return new_tick_target; |
231 } | 213 } |
232 | 214 |
233 void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) { | 215 void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) { |
234 base::TimeTicks new_tick_target = NextTickTarget(now); | 216 base::TimeTicks new_tick_target = NextTickTarget(now); |
235 | 217 |
236 // Post another task *before* the tick and update state | 218 // Post another task *before* the tick and update state |
237 base::TimeDelta delay = new_tick_target - now; | 219 base::TimeDelta delay; |
238 DCHECK(delay.InMillisecondsF() <= | 220 if (now <= new_tick_target) |
239 next_parameters_.interval.InMillisecondsF() * | 221 delay = new_tick_target - now; |
240 (1.0 + kDoubleTickThreshold)); | |
241 task_runner_->PostDelayedTask(FROM_HERE, | 222 task_runner_->PostDelayedTask(FROM_HERE, |
242 base::Bind(&DelayBasedTimeSource::OnTimerFired, | 223 base::Bind(&DelayBasedTimeSource::OnTimerFired, |
243 weak_factory_.GetWeakPtr()), | 224 weak_factory_.GetWeakPtr()), |
244 delay); | 225 delay); |
245 | 226 |
246 next_parameters_.tick_target = new_tick_target; | 227 next_parameters_.tick_target = new_tick_target; |
247 current_parameters_ = next_parameters_; | 228 current_parameters_ = next_parameters_; |
248 } | 229 } |
249 | 230 |
250 } // namespace cc | 231 } // namespace cc |
OLD | NEW |