Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: cc/scheduler/begin_frame_source.cc

Issue 2583483002: [cc] Adds source_id and sequence_number to BeginFrameArgs. (Closed)
Patch Set: Address Brian's comments. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/begin_frame_source.h" 5 #include "cc/scheduler/begin_frame_source.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/atomic_sequence_num.h"
9 #include "base/auto_reset.h" 10 #include "base/auto_reset.h"
10 #include "base/location.h" 11 #include "base/location.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
13 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
15 #include "base/trace_event/trace_event.h" 16 #include "base/trace_event/trace_event.h"
16 #include "base/trace_event/trace_event_argument.h" 17 #include "base/trace_event/trace_event_argument.h"
17 #include "cc/scheduler/delay_based_time_source.h" 18 #include "cc/scheduler/delay_based_time_source.h"
18 #include "cc/scheduler/scheduler.h" 19 #include "cc/scheduler/scheduler.h"
19 20
20 namespace cc { 21 namespace cc {
21 22
22 namespace { 23 namespace {
23 // kDoubleTickDivisor prevents the SyntheticBFS from sending BeginFrames too 24 // kDoubleTickDivisor prevents the SyntheticBFS from sending BeginFrames too
24 // often to an observer. 25 // often to an observer.
25 static const double kDoubleTickDivisor = 2.0; 26 static const double kDoubleTickDivisor = 2.0;
26 } 27 }
27 28
28 // BeginFrameObserverBase ----------------------------------------------- 29 // BeginFrameObserverBase -------------------------------------------------
29 BeginFrameObserverBase::BeginFrameObserverBase() 30 BeginFrameObserverBase::BeginFrameObserverBase()
30 : last_begin_frame_args_(), dropped_begin_frame_args_(0) { 31 : last_begin_frame_args_(), dropped_begin_frame_args_(0) {
31 } 32 }
32 33
33 const BeginFrameArgs& BeginFrameObserverBase::LastUsedBeginFrameArgs() const { 34 const BeginFrameArgs& BeginFrameObserverBase::LastUsedBeginFrameArgs() const {
34 return last_begin_frame_args_; 35 return last_begin_frame_args_;
35 } 36 }
37
36 void BeginFrameObserverBase::OnBeginFrame(const BeginFrameArgs& args) { 38 void BeginFrameObserverBase::OnBeginFrame(const BeginFrameArgs& args) {
37 DCHECK(args.IsValid()); 39 DCHECK(args.IsValid());
38 DCHECK(args.frame_time >= last_begin_frame_args_.frame_time); 40 DCHECK(args.frame_time >= last_begin_frame_args_.frame_time);
41 DCHECK(args.sequence_number > last_begin_frame_args_.sequence_number ||
42 args.source_id != last_begin_frame_args_.source_id);
39 bool used = OnBeginFrameDerivedImpl(args); 43 bool used = OnBeginFrameDerivedImpl(args);
40 if (used) { 44 if (used) {
41 last_begin_frame_args_ = args; 45 last_begin_frame_args_ = args;
42 } else { 46 } else {
43 ++dropped_begin_frame_args_; 47 ++dropped_begin_frame_args_;
44 } 48 }
45 } 49 }
46 50
51 // BeginFrameSource -------------------------------------------------------
52 namespace {
53 static base::StaticAtomicSequenceNumber g_next_source_id;
54 } // namespace
55
56 BeginFrameSource::BeginFrameSource() : source_id_(g_next_source_id.GetNext()) {}
57
58 uint32_t BeginFrameSource::source_id() const {
59 return source_id_;
60 }
61
62 // StubBeginFrameSource ---------------------------------------------------
47 bool StubBeginFrameSource::IsThrottled() const { 63 bool StubBeginFrameSource::IsThrottled() const {
48 return true; 64 return true;
49 } 65 }
50 66
51 // SyntheticBeginFrameSource --------------------------------------------- 67 // SyntheticBeginFrameSource ----------------------------------------------
52 SyntheticBeginFrameSource::~SyntheticBeginFrameSource() = default; 68 SyntheticBeginFrameSource::~SyntheticBeginFrameSource() = default;
53 69
54 // BackToBackBeginFrameSource -------------------------------------------- 70 // BackToBackBeginFrameSource ---------------------------------------------
55 BackToBackBeginFrameSource::BackToBackBeginFrameSource( 71 BackToBackBeginFrameSource::BackToBackBeginFrameSource(
56 std::unique_ptr<DelayBasedTimeSource> time_source) 72 std::unique_ptr<DelayBasedTimeSource> time_source)
57 : time_source_(std::move(time_source)), weak_factory_(this) { 73 : time_source_(std::move(time_source)),
74 next_sequence_number_(BeginFrameArgs::kStartingFrameNumber),
75 weak_factory_(this) {
58 time_source_->SetClient(this); 76 time_source_->SetClient(this);
59 // The time_source_ ticks immediately, so we SetActive(true) for a single 77 // The time_source_ ticks immediately, so we SetActive(true) for a single
60 // tick when we need it, and keep it as SetActive(false) otherwise. 78 // tick when we need it, and keep it as SetActive(false) otherwise.
61 time_source_->SetTimebaseAndInterval(base::TimeTicks(), base::TimeDelta()); 79 time_source_->SetTimebaseAndInterval(base::TimeTicks(), base::TimeDelta());
62 } 80 }
63 81
64 BackToBackBeginFrameSource::~BackToBackBeginFrameSource() = default; 82 BackToBackBeginFrameSource::~BackToBackBeginFrameSource() = default;
65 83
66 void BackToBackBeginFrameSource::AddObserver(BeginFrameObserver* obs) { 84 void BackToBackBeginFrameSource::AddObserver(BeginFrameObserver* obs) {
67 DCHECK(obs); 85 DCHECK(obs);
(...skipping 22 matching lines...) Expand all
90 } 108 }
91 109
92 bool BackToBackBeginFrameSource::IsThrottled() const { 110 bool BackToBackBeginFrameSource::IsThrottled() const {
93 return false; 111 return false;
94 } 112 }
95 113
96 void BackToBackBeginFrameSource::OnTimerTick() { 114 void BackToBackBeginFrameSource::OnTimerTick() {
97 base::TimeTicks frame_time = time_source_->LastTickTime(); 115 base::TimeTicks frame_time = time_source_->LastTickTime();
98 base::TimeDelta default_interval = BeginFrameArgs::DefaultInterval(); 116 base::TimeDelta default_interval = BeginFrameArgs::DefaultInterval();
99 BeginFrameArgs args = BeginFrameArgs::Create( 117 BeginFrameArgs args = BeginFrameArgs::Create(
100 BEGINFRAME_FROM_HERE, frame_time, frame_time + default_interval, 118 BEGINFRAME_FROM_HERE, source_id(), next_sequence_number_, frame_time,
101 default_interval, BeginFrameArgs::NORMAL); 119 frame_time + default_interval, default_interval, BeginFrameArgs::NORMAL);
120 next_sequence_number_++;
102 121
103 // This must happen after getting the LastTickTime() from the time source. 122 // This must happen after getting the LastTickTime() from the time source.
104 time_source_->SetActive(false); 123 time_source_->SetActive(false);
105 124
106 std::unordered_set<BeginFrameObserver*> pending_observers; 125 std::unordered_set<BeginFrameObserver*> pending_observers;
107 pending_observers.swap(pending_begin_frame_observers_); 126 pending_observers.swap(pending_begin_frame_observers_);
108 for (BeginFrameObserver* obs : pending_observers) 127 for (BeginFrameObserver* obs : pending_observers)
109 obs->OnBeginFrame(args); 128 obs->OnBeginFrame(args);
110 } 129 }
111 130
112 // DelayBasedBeginFrameSource --------------------------------------------- 131 // DelayBasedBeginFrameSource ---------------------------------------------
113 DelayBasedBeginFrameSource::DelayBasedBeginFrameSource( 132 DelayBasedBeginFrameSource::DelayBasedBeginFrameSource(
114 std::unique_ptr<DelayBasedTimeSource> time_source) 133 std::unique_ptr<DelayBasedTimeSource> time_source)
115 : time_source_(std::move(time_source)) { 134 : time_source_(std::move(time_source)),
135 next_sequence_number_(BeginFrameArgs::kStartingFrameNumber) {
116 time_source_->SetClient(this); 136 time_source_->SetClient(this);
117 } 137 }
118 138
119 DelayBasedBeginFrameSource::~DelayBasedBeginFrameSource() = default; 139 DelayBasedBeginFrameSource::~DelayBasedBeginFrameSource() = default;
120 140
121 void DelayBasedBeginFrameSource::OnUpdateVSyncParameters( 141 void DelayBasedBeginFrameSource::OnUpdateVSyncParameters(
122 base::TimeTicks timebase, 142 base::TimeTicks timebase,
123 base::TimeDelta interval) { 143 base::TimeDelta interval) {
124 if (!authoritative_interval_.is_zero()) { 144 if (!authoritative_interval_.is_zero()) {
125 interval = authoritative_interval_; 145 interval = authoritative_interval_;
126 } else if (interval.is_zero()) { 146 } else if (interval.is_zero()) {
127 // TODO(brianderson): We should not be receiving 0 intervals. 147 // TODO(brianderson): We should not be receiving 0 intervals.
128 interval = BeginFrameArgs::DefaultInterval(); 148 interval = BeginFrameArgs::DefaultInterval();
129 } 149 }
130 150
131 last_timebase_ = timebase; 151 last_timebase_ = timebase;
132 time_source_->SetTimebaseAndInterval(timebase, interval); 152 time_source_->SetTimebaseAndInterval(timebase, interval);
133 } 153 }
134 154
135 void DelayBasedBeginFrameSource::SetAuthoritativeVSyncInterval( 155 void DelayBasedBeginFrameSource::SetAuthoritativeVSyncInterval(
136 base::TimeDelta interval) { 156 base::TimeDelta interval) {
137 authoritative_interval_ = interval; 157 authoritative_interval_ = interval;
138 OnUpdateVSyncParameters(last_timebase_, interval); 158 OnUpdateVSyncParameters(last_timebase_, interval);
139 } 159 }
140 160
141 BeginFrameArgs DelayBasedBeginFrameSource::CreateBeginFrameArgs( 161 BeginFrameArgs DelayBasedBeginFrameSource::CreateBeginFrameArgs(
142 base::TimeTicks frame_time, 162 base::TimeTicks frame_time,
143 BeginFrameArgs::BeginFrameArgsType type) { 163 BeginFrameArgs::BeginFrameArgsType type) {
144 return BeginFrameArgs::Create(BEGINFRAME_FROM_HERE, frame_time, 164 uint64_t sequence_number = next_sequence_number_++;
145 time_source_->NextTickTime(), 165 return BeginFrameArgs::Create(
146 time_source_->Interval(), type); 166 BEGINFRAME_FROM_HERE, source_id(), sequence_number, frame_time,
167 time_source_->NextTickTime(), time_source_->Interval(), type);
147 } 168 }
148 169
149 void DelayBasedBeginFrameSource::AddObserver(BeginFrameObserver* obs) { 170 void DelayBasedBeginFrameSource::AddObserver(BeginFrameObserver* obs) {
150 DCHECK(obs); 171 DCHECK(obs);
151 DCHECK(observers_.find(obs) == observers_.end()); 172 DCHECK(observers_.find(obs) == observers_.end());
152 173
153 observers_.insert(obs); 174 observers_.insert(obs);
154 obs->OnBeginFrameSourcePausedChanged(false); 175 obs->OnBeginFrameSourcePausedChanged(false);
155 time_source_->SetActive(true); 176 time_source_->SetActive(true);
156 BeginFrameArgs args = CreateBeginFrameArgs( 177
157 time_source_->NextTickTime() - time_source_->Interval(), 178 // Missed args should correspond to |current_begin_frame_args_| (particularly,
158 BeginFrameArgs::MISSED); 179 // have the same sequence number) if |current_begin_frame_args_| still
180 // correspond to the last time the time source should have ticked. This may
181 // not be the case if OnTimerTick() has never run yet, the time source was
182 // inactive before AddObserver() was called, or the interval changed. In such
183 // a case, we create new args with a new sequence number.
184 base::TimeTicks last_or_missed_tick_time =
185 time_source_->NextTickTime() - time_source_->Interval();
186 if (current_begin_frame_args_.IsValid() &&
187 current_begin_frame_args_.frame_time == last_or_missed_tick_time &&
188 current_begin_frame_args_.interval == time_source_->Interval()) {
189 // Ensure that the args have the right type.
190 current_begin_frame_args_.type = BeginFrameArgs::MISSED;
191 } else {
192 // The args are not up to date and we need to create new ones with the
193 // missed tick's time and a new sequence number.
194 current_begin_frame_args_ =
195 CreateBeginFrameArgs(last_or_missed_tick_time, BeginFrameArgs::MISSED);
196 }
197
159 BeginFrameArgs last_args = obs->LastUsedBeginFrameArgs(); 198 BeginFrameArgs last_args = obs->LastUsedBeginFrameArgs();
160 if (!last_args.IsValid() || 199 if (!last_args.IsValid() ||
161 (args.frame_time > 200 (current_begin_frame_args_.frame_time >
162 last_args.frame_time + args.interval / kDoubleTickDivisor)) { 201 last_args.frame_time +
163 obs->OnBeginFrame(args); 202 current_begin_frame_args_.interval / kDoubleTickDivisor)) {
203 obs->OnBeginFrame(current_begin_frame_args_);
164 } 204 }
165 } 205 }
166 206
167 void DelayBasedBeginFrameSource::RemoveObserver(BeginFrameObserver* obs) { 207 void DelayBasedBeginFrameSource::RemoveObserver(BeginFrameObserver* obs) {
168 DCHECK(obs); 208 DCHECK(obs);
169 DCHECK(observers_.find(obs) != observers_.end()); 209 DCHECK(observers_.find(obs) != observers_.end());
170 210
171 observers_.erase(obs); 211 observers_.erase(obs);
172 if (observers_.empty()) 212 if (observers_.empty())
173 time_source_->SetActive(false); 213 time_source_->SetActive(false);
174 } 214 }
175 215
176 bool DelayBasedBeginFrameSource::IsThrottled() const { 216 bool DelayBasedBeginFrameSource::IsThrottled() const {
177 return true; 217 return true;
178 } 218 }
179 219
180 void DelayBasedBeginFrameSource::OnTimerTick() { 220 void DelayBasedBeginFrameSource::OnTimerTick() {
181 BeginFrameArgs args = CreateBeginFrameArgs(time_source_->LastTickTime(), 221 current_begin_frame_args_ = CreateBeginFrameArgs(time_source_->LastTickTime(),
182 BeginFrameArgs::NORMAL); 222 BeginFrameArgs::NORMAL);
183 std::unordered_set<BeginFrameObserver*> observers(observers_); 223 std::unordered_set<BeginFrameObserver*> observers(observers_);
184 for (auto* obs : observers) { 224 for (auto* obs : observers) {
185 BeginFrameArgs last_args = obs->LastUsedBeginFrameArgs(); 225 BeginFrameArgs last_args = obs->LastUsedBeginFrameArgs();
186 if (!last_args.IsValid() || 226 if (!last_args.IsValid() ||
187 (args.frame_time > 227 (current_begin_frame_args_.frame_time >
188 last_args.frame_time + args.interval / kDoubleTickDivisor)) 228 last_args.frame_time +
189 obs->OnBeginFrame(args); 229 current_begin_frame_args_.interval / kDoubleTickDivisor)) {
230 obs->OnBeginFrame(current_begin_frame_args_);
231 }
190 } 232 }
191 } 233 }
192 234
235 // ExternalBeginFrameSource -----------------------------------------------
193 ExternalBeginFrameSource::ExternalBeginFrameSource( 236 ExternalBeginFrameSource::ExternalBeginFrameSource(
194 ExternalBeginFrameSourceClient* client) 237 ExternalBeginFrameSourceClient* client)
195 : client_(client) { 238 : client_(client) {
196 DCHECK(client_); 239 DCHECK(client_);
197 } 240 }
198 241
199 ExternalBeginFrameSource::~ExternalBeginFrameSource() = default; 242 ExternalBeginFrameSource::~ExternalBeginFrameSource() = default;
200 243
201 void ExternalBeginFrameSource::AddObserver(BeginFrameObserver* obs) { 244 void ExternalBeginFrameSource::AddObserver(BeginFrameObserver* obs) {
202 DCHECK(obs); 245 DCHECK(obs);
203 DCHECK(observers_.find(obs) == observers_.end()); 246 DCHECK(observers_.find(obs) == observers_.end());
204 247
205 bool observers_was_empty = observers_.empty(); 248 bool observers_was_empty = observers_.empty();
206 observers_.insert(obs); 249 observers_.insert(obs);
207 obs->OnBeginFrameSourcePausedChanged(paused_); 250 obs->OnBeginFrameSourcePausedChanged(paused_);
208 if (observers_was_empty) 251 if (observers_was_empty)
209 client_->OnNeedsBeginFrames(true); 252 client_->OnNeedsBeginFrames(true);
210 253
211 // Send a MISSED begin frame if necessary. 254 // Send a MISSED begin frame if necessary.
212 if (missed_begin_frame_args_.IsValid()) { 255 if (missed_begin_frame_args_.IsValid()) {
213 BeginFrameArgs last_args = obs->LastUsedBeginFrameArgs(); 256 BeginFrameArgs last_args = obs->LastUsedBeginFrameArgs();
214 if (!last_args.IsValid() || 257 if (!last_args.IsValid() ||
215 (missed_begin_frame_args_.frame_time > last_args.frame_time)) { 258 (missed_begin_frame_args_.frame_time > last_args.frame_time)) {
259 DCHECK((missed_begin_frame_args_.source_id != last_args.source_id) ||
260 (missed_begin_frame_args_.sequence_number >
261 last_args.sequence_number));
216 obs->OnBeginFrame(missed_begin_frame_args_); 262 obs->OnBeginFrame(missed_begin_frame_args_);
217 } 263 }
218 } 264 }
219 } 265 }
220 266
221 void ExternalBeginFrameSource::RemoveObserver(BeginFrameObserver* obs) { 267 void ExternalBeginFrameSource::RemoveObserver(BeginFrameObserver* obs) {
222 DCHECK(obs); 268 DCHECK(obs);
223 DCHECK(observers_.find(obs) != observers_.end()); 269 DCHECK(observers_.find(obs) != observers_.end());
224 270
225 observers_.erase(obs); 271 observers_.erase(obs);
(...skipping 18 matching lines...) Expand all
244 290
245 void ExternalBeginFrameSource::OnBeginFrame(const BeginFrameArgs& args) { 291 void ExternalBeginFrameSource::OnBeginFrame(const BeginFrameArgs& args) {
246 missed_begin_frame_args_ = args; 292 missed_begin_frame_args_ = args;
247 missed_begin_frame_args_.type = BeginFrameArgs::MISSED; 293 missed_begin_frame_args_.type = BeginFrameArgs::MISSED;
248 std::unordered_set<BeginFrameObserver*> observers(observers_); 294 std::unordered_set<BeginFrameObserver*> observers(observers_);
249 for (auto* obs : observers) 295 for (auto* obs : observers)
250 obs->OnBeginFrame(args); 296 obs->OnBeginFrame(args);
251 } 297 }
252 298
253 } // namespace cc 299 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698