Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / cc / scheduler / delay_based_time_source.cc
blobb1f68bd27c94fda46cd5919eed1a76326feeeedb
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 "cc/scheduler/delay_based_time_source.h"
7 #include <algorithm>
8 #include <cmath>
9 #include <string>
11 #include "base/bind.h"
12 #include "base/location.h"
13 #include "base/logging.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/trace_event/trace_event.h"
16 #include "base/trace_event/trace_event_argument.h"
18 namespace cc {
20 namespace {
22 // kDoubleTickDivisor prevents ticks from running within the specified
23 // fraction of an interval. This helps account for jitter in the timebase as
24 // well as quick timer reactivation.
25 static const int kDoubleTickDivisor = 2;
27 // kIntervalChangeThreshold is the fraction of the interval that will trigger an
28 // immediate interval change. kPhaseChangeThreshold is the fraction of the
29 // interval that will trigger an immediate phase change. If the changes are
30 // within the thresholds, the change will take place on the next tick. If
31 // either change is outside the thresholds, the next tick will be canceled and
32 // reissued immediately.
33 static const double kIntervalChangeThreshold = 0.25;
34 static const double kPhaseChangeThreshold = 0.25;
36 } // namespace
38 // The following methods correspond to the DelayBasedTimeSource that uses
39 // the base::TimeTicks::Now as the timebase.
40 DelayBasedTimeSource::DelayBasedTimeSource(
41 base::TimeDelta interval,
42 base::SingleThreadTaskRunner* task_runner)
43 : client_(NULL),
44 last_tick_time_(base::TimeTicks() - interval),
45 current_parameters_(interval, base::TimeTicks()),
46 next_parameters_(interval, base::TimeTicks()),
47 active_(false),
48 task_runner_(task_runner),
49 weak_factory_(this) {
50 DCHECK_GT(interval.ToInternalValue(), 0);
53 DelayBasedTimeSource::~DelayBasedTimeSource() {}
55 base::TimeTicks DelayBasedTimeSource::SetActive(bool active) {
56 TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active);
57 if (active == active_)
58 return base::TimeTicks();
59 active_ = active;
61 if (!active_) {
62 weak_factory_.InvalidateWeakPtrs();
63 return base::TimeTicks();
66 PostNextTickTask(Now());
68 // Determine if there was a tick that was missed while not active.
69 base::TimeTicks last_tick_time_if_always_active =
70 current_parameters_.tick_target - current_parameters_.interval;
71 base::TimeTicks new_tick_time_threshold =
72 last_tick_time_ + current_parameters_.interval / kDoubleTickDivisor;
73 if (last_tick_time_if_always_active > new_tick_time_threshold) {
74 last_tick_time_ = last_tick_time_if_always_active;
75 return last_tick_time_;
78 return base::TimeTicks();
81 bool DelayBasedTimeSource::Active() const { return active_; }
83 base::TimeTicks DelayBasedTimeSource::LastTickTime() const {
84 return last_tick_time_;
87 base::TimeTicks DelayBasedTimeSource::NextTickTime() const {
88 return Active() ? current_parameters_.tick_target : base::TimeTicks();
91 void DelayBasedTimeSource::OnTimerFired() {
92 DCHECK(active_);
94 last_tick_time_ = current_parameters_.tick_target;
96 PostNextTickTask(Now());
98 // Fire the tick.
99 if (client_)
100 client_->OnTimerTick();
103 void DelayBasedTimeSource::SetClient(TimeSourceClient* client) {
104 client_ = client;
107 void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase,
108 base::TimeDelta interval) {
109 DCHECK_GT(interval.ToInternalValue(), 0);
110 next_parameters_.interval = interval;
111 next_parameters_.tick_target = timebase;
113 if (!active_) {
114 // If we aren't active, there's no need to reset the timer.
115 return;
118 // If the change in interval is larger than the change threshold,
119 // request an immediate reset.
120 double interval_delta =
121 std::abs((interval - current_parameters_.interval).InSecondsF());
122 double interval_change = interval_delta / interval.InSecondsF();
123 if (interval_change > kIntervalChangeThreshold) {
124 TRACE_EVENT_INSTANT0("cc", "DelayBasedTimeSource::IntervalChanged",
125 TRACE_EVENT_SCOPE_THREAD);
126 SetActive(false);
127 SetActive(true);
128 return;
131 // 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
133 // 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
135 // 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.
137 double target_delta =
138 std::abs((timebase - current_parameters_.tick_target).InSecondsF());
139 double phase_change =
140 fmod(target_delta, interval.InSecondsF()) / interval.InSecondsF();
141 if (phase_change > kPhaseChangeThreshold &&
142 phase_change < (1.0 - kPhaseChangeThreshold)) {
143 TRACE_EVENT_INSTANT0("cc", "DelayBasedTimeSource::PhaseChanged",
144 TRACE_EVENT_SCOPE_THREAD);
145 SetActive(false);
146 SetActive(true);
147 return;
151 base::TimeTicks DelayBasedTimeSource::Now() const {
152 return base::TimeTicks::Now();
155 // This code tries to achieve an average tick rate as close to interval_ as
156 // possible. To do this, it has to deal with a few basic issues:
157 // 1. PostDelayedTask can delay only at a millisecond granularity. So, 16.666
158 // has to posted as 16 or 17.
159 // 2. A delayed task may come back a bit late (a few ms), or really late
160 // (frames later)
162 // The basic idea with this scheduler here is to keep track of where we *want*
163 // to run in tick_target_. We update this with the exact interval.
165 // Then, when we post our task, we take the floor of (tick_target_ and Now()).
166 // If we started at now=0, and 60FPs (all times in milliseconds):
167 // now=0 target=16.667 PostDelayedTask(16)
169 // When our callback runs, we figure out how far off we were from that goal.
170 // Because of the flooring operation, and assuming our timer runs exactly when
171 // it should, this yields:
172 // now=16 target=16.667
174 // Since we can't post a 0.667 ms task to get to now=16, we just treat this as a
175 // tick. Then, we update target to be 33.333. We now post another task based on
176 // the difference between our target and now:
177 // now=16 tick_target=16.667 new_target=33.333 -->
178 // PostDelayedTask(floor(33.333 - 16)) --> PostDelayedTask(17)
180 // Over time, with no late tasks, this leads to us posting tasks like this:
181 // now=0 tick_target=0 new_target=16.667 -->
182 // tick(), PostDelayedTask(16)
183 // now=16 tick_target=16.667 new_target=33.333 -->
184 // tick(), PostDelayedTask(17)
185 // now=33 tick_target=33.333 new_target=50.000 -->
186 // tick(), PostDelayedTask(17)
187 // now=50 tick_target=50.000 new_target=66.667 -->
188 // tick(), PostDelayedTask(16)
190 // We treat delays in tasks differently depending on the amount of delay we
191 // encounter. Suppose we posted a task with a target=16.667:
192 // Case 1: late but not unrecoverably-so
193 // now=18 tick_target=16.667
195 // Case 2: so late we obviously missed the tick
196 // now=25.0 tick_target=16.667
198 // We treat the first case as a tick anyway, and assume the delay was unusual.
199 // Thus, we compute the new_target based on the old timebase:
200 // now=18 tick_target=16.667 new_target=33.333 -->
201 // tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15)
202 // This brings us back to 18+15 = 33, which was where we would have been if the
203 // task hadn't been late.
205 // For the really late delay, we we move to the next logical tick. The timebase
206 // is not reset.
207 // now=37 tick_target=16.667 new_target=50.000 -->
208 // tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13)
209 base::TimeTicks DelayBasedTimeSource::NextTickTarget(base::TimeTicks now) {
210 base::TimeTicks new_tick_target = now.SnappedToNextTick(
211 next_parameters_.tick_target, next_parameters_.interval);
212 DCHECK(now <= new_tick_target)
213 << "now = " << now.ToInternalValue()
214 << "; new_tick_target = " << new_tick_target.ToInternalValue()
215 << "; new_interval = " << next_parameters_.interval.InMicroseconds()
216 << "; tick_target = " << next_parameters_.tick_target.ToInternalValue();
218 // Avoid double ticks when:
219 // 1) Turning off the timer and turning it right back on.
220 // 2) Jittery data is passed to SetTimebaseAndInterval().
221 if (new_tick_target - last_tick_time_ <=
222 next_parameters_.interval / kDoubleTickDivisor)
223 new_tick_target += next_parameters_.interval;
225 return new_tick_target;
228 void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) {
229 base::TimeTicks new_tick_target = NextTickTarget(now);
231 // Post another task *before* the tick and update state
232 base::TimeDelta delay;
233 if (now <= new_tick_target)
234 delay = new_tick_target - now;
235 task_runner_->PostDelayedTask(FROM_HERE,
236 base::Bind(&DelayBasedTimeSource::OnTimerFired,
237 weak_factory_.GetWeakPtr()),
238 delay);
240 next_parameters_.tick_target = new_tick_target;
241 current_parameters_ = next_parameters_;
244 std::string DelayBasedTimeSource::TypeString() const {
245 return "DelayBasedTimeSource";
248 void DelayBasedTimeSource::AsValueInto(
249 base::trace_event::TracedValue* state) const {
250 state->SetString("type", TypeString());
251 state->SetDouble("last_tick_time_us", LastTickTime().ToInternalValue());
252 state->SetDouble("next_tick_time_us", NextTickTime().ToInternalValue());
254 state->BeginDictionary("current_parameters");
255 state->SetDouble("interval_us",
256 current_parameters_.interval.InMicroseconds());
257 state->SetDouble("tick_target_us",
258 current_parameters_.tick_target.ToInternalValue());
259 state->EndDictionary();
261 state->BeginDictionary("next_parameters");
262 state->SetDouble("interval_us", next_parameters_.interval.InMicroseconds());
263 state->SetDouble("tick_target_us",
264 next_parameters_.tick_target.ToInternalValue());
265 state->EndDictionary();
267 state->SetBoolean("active", active_);
270 } // namespace cc