| 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 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
| 16 #include "base/trace_event/trace_event_argument.h" | 16 #include "base/trace_event/trace_event_argument.h" |
| 17 #include "cc/output/begin_frame_args.h" |
| 17 | 18 |
| 18 namespace cc { | 19 namespace cc { |
| 19 | 20 |
| 20 // The following methods correspond to the DelayBasedTimeSource that uses | 21 // The following methods correspond to the DelayBasedTimeSource that uses |
| 21 // the base::TimeTicks::Now as the timebase. | 22 // the base::TimeTicks::Now as the timebase. |
| 22 DelayBasedTimeSource::DelayBasedTimeSource( | 23 DelayBasedTimeSource::DelayBasedTimeSource( |
| 23 base::TimeDelta interval, | |
| 24 base::SingleThreadTaskRunner* task_runner) | 24 base::SingleThreadTaskRunner* task_runner) |
| 25 : client_(nullptr), | 25 : client_(nullptr), |
| 26 active_(false), | 26 active_(false), |
| 27 timebase_(base::TimeTicks()), | 27 timebase_(base::TimeTicks()), |
| 28 interval_(interval), | 28 interval_(BeginFrameArgs::DefaultInterval()), |
| 29 last_tick_time_(base::TimeTicks() - interval), | 29 last_tick_time_(base::TimeTicks() - interval_), |
| 30 next_tick_time_(base::TimeTicks()), | 30 next_tick_time_(base::TimeTicks()), |
| 31 task_runner_(task_runner), | 31 task_runner_(task_runner), |
| 32 weak_factory_(this) { | 32 weak_factory_(this) {} |
| 33 DCHECK_GT(interval, base::TimeDelta()); | |
| 34 } | |
| 35 | 33 |
| 36 DelayBasedTimeSource::~DelayBasedTimeSource() {} | 34 DelayBasedTimeSource::~DelayBasedTimeSource() {} |
| 37 | 35 |
| 38 void DelayBasedTimeSource::SetActive(bool active) { | 36 void DelayBasedTimeSource::SetActive(bool active) { |
| 39 TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active); | 37 TRACE_EVENT1("cc", "DelayBasedTimeSource::SetActive", "active", active); |
| 40 | 38 |
| 41 if (active == active_) | 39 if (active == active_) |
| 42 return; | 40 return; |
| 43 | 41 |
| 44 active_ = active; | 42 active_ = active; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 if (client_) | 75 if (client_) |
| 78 client_->OnTimerTick(); | 76 client_->OnTimerTick(); |
| 79 } | 77 } |
| 80 | 78 |
| 81 void DelayBasedTimeSource::SetClient(DelayBasedTimeSourceClient* client) { | 79 void DelayBasedTimeSource::SetClient(DelayBasedTimeSourceClient* client) { |
| 82 client_ = client; | 80 client_ = client; |
| 83 } | 81 } |
| 84 | 82 |
| 85 void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase, | 83 void DelayBasedTimeSource::SetTimebaseAndInterval(base::TimeTicks timebase, |
| 86 base::TimeDelta interval) { | 84 base::TimeDelta interval) { |
| 87 DCHECK_GT(interval, base::TimeDelta()); | |
| 88 interval_ = interval; | 85 interval_ = interval; |
| 89 timebase_ = timebase; | 86 timebase_ = timebase; |
| 90 } | 87 } |
| 91 | 88 |
| 92 base::TimeTicks DelayBasedTimeSource::Now() const { | 89 base::TimeTicks DelayBasedTimeSource::Now() const { |
| 93 return base::TimeTicks::Now(); | 90 return base::TimeTicks::Now(); |
| 94 } | 91 } |
| 95 | 92 |
| 96 // This code tries to achieve an average tick rate as close to interval_ as | 93 // This code tries to achieve an average tick rate as close to interval_ as |
| 97 // possible. To do this, it has to deal with a few basic issues: | 94 // possible. To do this, it has to deal with a few basic issues: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // now=18 tick_target=16.667 new_target=33.333 --> | 138 // now=18 tick_target=16.667 new_target=33.333 --> |
| 142 // tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15) | 139 // tick(), PostDelayedTask(floor(33.333-18)) --> PostDelayedTask(15) |
| 143 // This brings us back to 18+15 = 33, which was where we would have been if the | 140 // This brings us back to 18+15 = 33, which was where we would have been if the |
| 144 // task hadn't been late. | 141 // task hadn't been late. |
| 145 // | 142 // |
| 146 // For the really late delay, we we move to the next logical tick. The timebase | 143 // For the really late delay, we we move to the next logical tick. The timebase |
| 147 // is not reset. | 144 // is not reset. |
| 148 // now=37 tick_target=16.667 new_target=50.000 --> | 145 // now=37 tick_target=16.667 new_target=50.000 --> |
| 149 // tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13) | 146 // tick(), PostDelayedTask(floor(50.000-37)) --> PostDelayedTask(13) |
| 150 void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) { | 147 void DelayBasedTimeSource::PostNextTickTask(base::TimeTicks now) { |
| 151 next_tick_time_ = now.SnappedToNextTick(timebase_, interval_); | 148 if (interval_.is_zero()) { |
| 152 if (next_tick_time_ == now) | 149 next_tick_time_ = now; |
| 153 next_tick_time_ += interval_; | 150 } else { |
| 154 DCHECK_GT(next_tick_time_, now); | 151 next_tick_time_ = now.SnappedToNextTick(timebase_, interval_); |
| 152 if (next_tick_time_ == now) |
| 153 next_tick_time_ += interval_; |
| 154 DCHECK_GT(next_tick_time_, now); |
| 155 } |
| 155 tick_closure_.Reset(base::Bind(&DelayBasedTimeSource::OnTimerTick, | 156 tick_closure_.Reset(base::Bind(&DelayBasedTimeSource::OnTimerTick, |
| 156 weak_factory_.GetWeakPtr())); | 157 weak_factory_.GetWeakPtr())); |
| 157 task_runner_->PostDelayedTask(FROM_HERE, tick_closure_.callback(), | 158 task_runner_->PostDelayedTask(FROM_HERE, tick_closure_.callback(), |
| 158 next_tick_time_ - now); | 159 next_tick_time_ - now); |
| 159 } | 160 } |
| 160 | 161 |
| 161 std::string DelayBasedTimeSource::TypeString() const { | 162 std::string DelayBasedTimeSource::TypeString() const { |
| 162 return "DelayBasedTimeSource"; | 163 return "DelayBasedTimeSource"; |
| 163 } | 164 } |
| 164 | 165 |
| 165 void DelayBasedTimeSource::AsValueInto( | 166 void DelayBasedTimeSource::AsValueInto( |
| 166 base::trace_event::TracedValue* state) const { | 167 base::trace_event::TracedValue* state) const { |
| 167 state->SetString("type", TypeString()); | 168 state->SetString("type", TypeString()); |
| 168 state->SetDouble("last_tick_time_us", LastTickTime().ToInternalValue()); | 169 state->SetDouble("last_tick_time_us", LastTickTime().ToInternalValue()); |
| 169 state->SetDouble("next_tick_time_us", NextTickTime().ToInternalValue()); | 170 state->SetDouble("next_tick_time_us", NextTickTime().ToInternalValue()); |
| 170 state->SetDouble("interval_us", interval_.InMicroseconds()); | 171 state->SetDouble("interval_us", interval_.InMicroseconds()); |
| 171 state->SetDouble("timebase_us", timebase_.ToInternalValue()); | 172 state->SetDouble("timebase_us", timebase_.ToInternalValue()); |
| 172 state->SetBoolean("active", active_); | 173 state->SetBoolean("active", active_); |
| 173 } | 174 } |
| 174 | 175 |
| 175 } // namespace cc | 176 } // namespace cc |
| OLD | NEW |