OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "cc/scheduler/begin_frame_source.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/trace_event/trace_event.h" |
| 12 #include "base/trace_event/trace_event_argument.h" |
| 13 #include "cc/scheduler/delay_based_time_source.h" |
| 14 #include "cc/scheduler/scheduler.h" |
| 15 #include "ui/gfx/frame_time.h" |
| 16 |
| 17 #ifdef NDEBUG |
| 18 #define DEBUG_FRAMES(...) |
| 19 #else |
| 20 #define DEBUG_FRAMES(name, arg1_name, arg1_val, arg2_name, arg2_val) \ |
| 21 TRACE_EVENT2(TRACE_DISABLED_BY_DEFAULT("cc.debug.scheduler.frames"), \ |
| 22 name, \ |
| 23 arg1_name, \ |
| 24 arg1_val, \ |
| 25 arg2_name, \ |
| 26 arg2_val); |
| 27 #endif |
| 28 |
| 29 namespace cc { |
| 30 |
| 31 // BeginFrameObserverMixIn ----------------------------------------------- |
| 32 BeginFrameObserverMixIn::BeginFrameObserverMixIn() |
| 33 : last_begin_frame_args_(), dropped_begin_frame_args_(0) { |
| 34 } |
| 35 |
| 36 const BeginFrameArgs BeginFrameObserverMixIn::LastUsedBeginFrameArgs() const { |
| 37 return last_begin_frame_args_; |
| 38 } |
| 39 void BeginFrameObserverMixIn::OnBeginFrame(const BeginFrameArgs& args) { |
| 40 DEBUG_FRAMES("BeginFrameObserverMixIn::OnBeginFrame", |
| 41 "last args", |
| 42 last_begin_frame_args_.AsValue(), |
| 43 "new args", |
| 44 args.AsValue()); |
| 45 DCHECK(args.IsValid()); |
| 46 DCHECK(args.frame_time >= last_begin_frame_args_.frame_time); |
| 47 bool used = OnBeginFrameMixInDelegate(args); |
| 48 if (used) { |
| 49 last_begin_frame_args_ = args; |
| 50 } else { |
| 51 ++dropped_begin_frame_args_; |
| 52 } |
| 53 } |
| 54 |
| 55 void BeginFrameObserverMixIn::AsValueInto( |
| 56 base::trace_event::TracedValue* dict) const { |
| 57 dict->BeginDictionary("last_begin_frame_args_"); |
| 58 last_begin_frame_args_.AsValueInto(dict); |
| 59 dict->EndDictionary(); |
| 60 dict->SetInteger("dropped_begin_frame_args_", dropped_begin_frame_args_); |
| 61 } |
| 62 |
| 63 // BeginFrameSourceMixIn ------------------------------------------------------ |
| 64 BeginFrameSourceMixIn::BeginFrameSourceMixIn() |
| 65 : observer_(NULL), |
| 66 needs_begin_frames_(false), |
| 67 inside_as_value_into_(false) { |
| 68 DCHECK(!observer_); |
| 69 DCHECK_EQ(inside_as_value_into_, false); |
| 70 } |
| 71 |
| 72 bool BeginFrameSourceMixIn::NeedsBeginFrames() const { |
| 73 return needs_begin_frames_; |
| 74 } |
| 75 |
| 76 void BeginFrameSourceMixIn::SetNeedsBeginFrames(bool needs_begin_frames) { |
| 77 DEBUG_FRAMES("BeginFrameSourceMixIn::SetNeedsBeginFrames", |
| 78 "current state", |
| 79 needs_begin_frames_, |
| 80 "new state", |
| 81 needs_begin_frames); |
| 82 if (needs_begin_frames_ != needs_begin_frames) { |
| 83 needs_begin_frames_ = needs_begin_frames; |
| 84 OnNeedsBeginFramesChange(needs_begin_frames); |
| 85 } |
| 86 } |
| 87 |
| 88 void BeginFrameSourceMixIn::AddObserver(BeginFrameObserver* obs) { |
| 89 DEBUG_FRAMES("BeginFrameSourceMixIn::AddObserver", |
| 90 "current observer", |
| 91 observer_, |
| 92 "to add observer", |
| 93 obs); |
| 94 DCHECK(!observer_); |
| 95 observer_ = obs; |
| 96 } |
| 97 |
| 98 void BeginFrameSourceMixIn::RemoveObserver(BeginFrameObserver* obs) { |
| 99 DEBUG_FRAMES("BeginFrameSourceMixIn::RemoveObserver", |
| 100 "current observer", |
| 101 observer_, |
| 102 "to remove observer", |
| 103 obs); |
| 104 DCHECK_EQ(observer_, obs); |
| 105 observer_ = NULL; |
| 106 } |
| 107 |
| 108 void BeginFrameSourceMixIn::CallOnBeginFrame(const BeginFrameArgs& args) { |
| 109 DEBUG_FRAMES("BeginFrameSourceMixIn::CallOnBeginFrame", |
| 110 "current observer", |
| 111 observer_, |
| 112 "args", |
| 113 args.AsValue()); |
| 114 if (observer_) { |
| 115 return observer_->OnBeginFrame(args); |
| 116 } |
| 117 } |
| 118 |
| 119 // Tracing support |
| 120 void BeginFrameSourceMixIn::AsValueInto( |
| 121 base::trace_event::TracedValue* dict) const { |
| 122 // As the observer might try to trace the source, prevent an infinte loop |
| 123 // from occuring. |
| 124 if (inside_as_value_into_) { |
| 125 dict->SetString("observer", "<loop detected>"); |
| 126 return; |
| 127 } |
| 128 |
| 129 if (observer_) { |
| 130 base::AutoReset<bool> prevent_loops( |
| 131 const_cast<bool*>(&inside_as_value_into_), true); |
| 132 dict->BeginDictionary("observer"); |
| 133 observer_->AsValueInto(dict); |
| 134 dict->EndDictionary(); |
| 135 } else { |
| 136 dict->SetString("observer", "NULL"); |
| 137 } |
| 138 dict->SetBoolean("needs_begin_frames", NeedsBeginFrames()); |
| 139 } |
| 140 |
| 141 // BackToBackBeginFrameSourceMixIn -------------------------------------------- |
| 142 scoped_ptr<BackToBackBeginFrameSource> BackToBackBeginFrameSource::Create( |
| 143 base::SingleThreadTaskRunner* task_runner) { |
| 144 return make_scoped_ptr(new BackToBackBeginFrameSource(task_runner)); |
| 145 } |
| 146 |
| 147 BackToBackBeginFrameSource::BackToBackBeginFrameSource( |
| 148 base::SingleThreadTaskRunner* task_runner) |
| 149 : BeginFrameSourceMixIn(), |
| 150 task_runner_(task_runner), |
| 151 send_begin_frame_posted_(false), |
| 152 weak_factory_(this) { |
| 153 DCHECK(task_runner); |
| 154 DCHECK_EQ(needs_begin_frames_, false); |
| 155 DCHECK_EQ(send_begin_frame_posted_, false); |
| 156 } |
| 157 |
| 158 BackToBackBeginFrameSource::~BackToBackBeginFrameSource() { |
| 159 } |
| 160 |
| 161 base::TimeTicks BackToBackBeginFrameSource::Now() { |
| 162 return gfx::FrameTime::Now(); |
| 163 } |
| 164 |
| 165 void BackToBackBeginFrameSource::OnNeedsBeginFramesChange( |
| 166 bool needs_begin_frames) { |
| 167 if (!needs_begin_frames) |
| 168 return; |
| 169 |
| 170 if (send_begin_frame_posted_) |
| 171 return; |
| 172 |
| 173 send_begin_frame_posted_ = true; |
| 174 task_runner_->PostTask(FROM_HERE, |
| 175 base::Bind(&BackToBackBeginFrameSource::BeginFrame, |
| 176 weak_factory_.GetWeakPtr())); |
| 177 } |
| 178 |
| 179 void BackToBackBeginFrameSource::BeginFrame() { |
| 180 send_begin_frame_posted_ = false; |
| 181 |
| 182 if (!needs_begin_frames_) |
| 183 return; |
| 184 |
| 185 base::TimeTicks now = Now(); |
| 186 BeginFrameArgs args = BeginFrameArgs::Create( |
| 187 BEGINFRAME_FROM_HERE, now, now + BeginFrameArgs::DefaultInterval(), |
| 188 BeginFrameArgs::DefaultInterval(), BeginFrameArgs::NORMAL); |
| 189 CallOnBeginFrame(args); |
| 190 } |
| 191 |
| 192 // BeginFrameSource support |
| 193 |
| 194 void BackToBackBeginFrameSource::DidFinishFrame(size_t remaining_frames) { |
| 195 if (remaining_frames == 0) { |
| 196 OnNeedsBeginFramesChange(NeedsBeginFrames()); |
| 197 } |
| 198 } |
| 199 |
| 200 // Tracing support |
| 201 void BackToBackBeginFrameSource::AsValueInto( |
| 202 base::trace_event::TracedValue* dict) const { |
| 203 dict->SetString("type", "BackToBackBeginFrameSource"); |
| 204 BeginFrameSourceMixIn::AsValueInto(dict); |
| 205 dict->SetBoolean("send_begin_frame_posted_", send_begin_frame_posted_); |
| 206 } |
| 207 |
| 208 // SyntheticBeginFrameSource --------------------------------------------- |
| 209 scoped_ptr<SyntheticBeginFrameSource> SyntheticBeginFrameSource::Create( |
| 210 base::SingleThreadTaskRunner* task_runner, |
| 211 base::TimeTicks initial_vsync_timebase, |
| 212 base::TimeDelta initial_vsync_interval) { |
| 213 scoped_refptr<DelayBasedTimeSource> time_source; |
| 214 if (gfx::FrameTime::TimestampsAreHighRes()) { |
| 215 time_source = DelayBasedTimeSourceHighRes::Create(initial_vsync_interval, |
| 216 task_runner); |
| 217 } else { |
| 218 time_source = |
| 219 DelayBasedTimeSource::Create(initial_vsync_interval, task_runner); |
| 220 } |
| 221 |
| 222 return make_scoped_ptr(new SyntheticBeginFrameSource(time_source)); |
| 223 } |
| 224 |
| 225 SyntheticBeginFrameSource::SyntheticBeginFrameSource( |
| 226 scoped_refptr<DelayBasedTimeSource> time_source) |
| 227 : BeginFrameSourceMixIn(), time_source_(time_source) { |
| 228 time_source_->SetActive(false); |
| 229 time_source_->SetClient(this); |
| 230 } |
| 231 |
| 232 SyntheticBeginFrameSource::~SyntheticBeginFrameSource() { |
| 233 if (NeedsBeginFrames()) |
| 234 time_source_->SetActive(false); |
| 235 } |
| 236 |
| 237 void SyntheticBeginFrameSource::OnUpdateVSyncParameters( |
| 238 base::TimeTicks new_vsync_timebase, |
| 239 base::TimeDelta new_vsync_interval) { |
| 240 time_source_->SetTimebaseAndInterval(new_vsync_timebase, new_vsync_interval); |
| 241 } |
| 242 |
| 243 BeginFrameArgs SyntheticBeginFrameSource::CreateBeginFrameArgs( |
| 244 base::TimeTicks frame_time, |
| 245 BeginFrameArgs::BeginFrameArgsType type) { |
| 246 base::TimeTicks deadline = time_source_->NextTickTime(); |
| 247 return BeginFrameArgs::Create(BEGINFRAME_FROM_HERE, frame_time, deadline, |
| 248 time_source_->Interval(), type); |
| 249 } |
| 250 |
| 251 // TimeSourceClient support |
| 252 void SyntheticBeginFrameSource::OnTimerTick() { |
| 253 CallOnBeginFrame(CreateBeginFrameArgs(time_source_->LastTickTime(), |
| 254 BeginFrameArgs::NORMAL)); |
| 255 } |
| 256 |
| 257 // BeginFrameSourceMixIn support |
| 258 void SyntheticBeginFrameSource::OnNeedsBeginFramesChange( |
| 259 bool needs_begin_frames) { |
| 260 base::TimeTicks missed_tick_time = |
| 261 time_source_->SetActive(needs_begin_frames); |
| 262 if (!missed_tick_time.is_null()) { |
| 263 CallOnBeginFrame( |
| 264 CreateBeginFrameArgs(missed_tick_time, BeginFrameArgs::MISSED)); |
| 265 } |
| 266 } |
| 267 |
| 268 // Tracing support |
| 269 void SyntheticBeginFrameSource::AsValueInto( |
| 270 base::trace_event::TracedValue* dict) const { |
| 271 dict->SetString("type", "SyntheticBeginFrameSource"); |
| 272 BeginFrameSourceMixIn::AsValueInto(dict); |
| 273 |
| 274 dict->BeginDictionary("time_source"); |
| 275 time_source_->AsValueInto(dict); |
| 276 dict->EndDictionary(); |
| 277 } |
| 278 |
| 279 // BeginFrameSourceMultiplexer ------------------------------------------- |
| 280 scoped_ptr<BeginFrameSourceMultiplexer> BeginFrameSourceMultiplexer::Create() { |
| 281 return make_scoped_ptr(new BeginFrameSourceMultiplexer()); |
| 282 } |
| 283 |
| 284 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer() |
| 285 : BeginFrameSourceMixIn(), |
| 286 minimum_interval_(base::TimeDelta()), |
| 287 active_source_(NULL), |
| 288 source_list_() { |
| 289 } |
| 290 |
| 291 BeginFrameSourceMultiplexer::BeginFrameSourceMultiplexer( |
| 292 base::TimeDelta minimum_interval) |
| 293 : BeginFrameSourceMixIn(), |
| 294 minimum_interval_(minimum_interval), |
| 295 active_source_(NULL), |
| 296 source_list_() { |
| 297 } |
| 298 |
| 299 BeginFrameSourceMultiplexer::~BeginFrameSourceMultiplexer() { |
| 300 } |
| 301 |
| 302 void BeginFrameSourceMultiplexer::SetMinimumInterval( |
| 303 base::TimeDelta new_minimum_interval) { |
| 304 DEBUG_FRAMES("BeginFrameSourceMultiplexer::SetMinimumInterval", |
| 305 "current minimum (us)", |
| 306 minimum_interval_.InMicroseconds(), |
| 307 "new minimum (us)", |
| 308 new_minimum_interval.InMicroseconds()); |
| 309 DCHECK_GE(new_minimum_interval.ToInternalValue(), 0); |
| 310 minimum_interval_ = new_minimum_interval; |
| 311 } |
| 312 |
| 313 void BeginFrameSourceMultiplexer::AddSource(BeginFrameSource* new_source) { |
| 314 DEBUG_FRAMES("BeginFrameSourceMultiplexer::AddSource", |
| 315 "current active", |
| 316 active_source_, |
| 317 "source to remove", |
| 318 new_source); |
| 319 DCHECK(new_source); |
| 320 DCHECK(!HasSource(new_source)); |
| 321 |
| 322 source_list_.insert(new_source); |
| 323 |
| 324 // If there is no active source, set the new one as the active one. |
| 325 if (!active_source_) |
| 326 SetActiveSource(new_source); |
| 327 } |
| 328 |
| 329 void BeginFrameSourceMultiplexer::RemoveSource( |
| 330 BeginFrameSource* existing_source) { |
| 331 DEBUG_FRAMES("BeginFrameSourceMultiplexer::RemoveSource", |
| 332 "current active", |
| 333 active_source_, |
| 334 "source to remove", |
| 335 existing_source); |
| 336 DCHECK(existing_source); |
| 337 DCHECK(HasSource(existing_source)); |
| 338 DCHECK_NE(existing_source, active_source_); |
| 339 source_list_.erase(existing_source); |
| 340 } |
| 341 |
| 342 void BeginFrameSourceMultiplexer::SetActiveSource( |
| 343 BeginFrameSource* new_source) { |
| 344 DEBUG_FRAMES("BeginFrameSourceMultiplexer::SetActiveSource", |
| 345 "current active", |
| 346 active_source_, |
| 347 "to become active", |
| 348 new_source); |
| 349 |
| 350 DCHECK(HasSource(new_source) || new_source == NULL); |
| 351 |
| 352 bool needs_begin_frames = NeedsBeginFrames(); |
| 353 if (active_source_) { |
| 354 if (needs_begin_frames) |
| 355 SetNeedsBeginFrames(false); |
| 356 |
| 357 // Technically we shouldn't need to remove observation, but this prevents |
| 358 // the case where SetNeedsBeginFrames message gets to the source after a |
| 359 // message has already been sent. |
| 360 active_source_->RemoveObserver(this); |
| 361 active_source_ = NULL; |
| 362 } |
| 363 DCHECK(!active_source_); |
| 364 active_source_ = new_source; |
| 365 |
| 366 if (active_source_) { |
| 367 active_source_->AddObserver(this); |
| 368 |
| 369 if (needs_begin_frames) { |
| 370 SetNeedsBeginFrames(true); |
| 371 } |
| 372 } |
| 373 } |
| 374 |
| 375 const BeginFrameSource* BeginFrameSourceMultiplexer::ActiveSource() { |
| 376 return active_source_; |
| 377 } |
| 378 |
| 379 // BeginFrameObserver support |
| 380 void BeginFrameSourceMultiplexer::OnBeginFrame(const BeginFrameArgs& args) { |
| 381 if (!IsIncreasing(args)) { |
| 382 DEBUG_FRAMES("BeginFrameSourceMultiplexer::OnBeginFrame", |
| 383 "action", |
| 384 "discarding", |
| 385 "new args", |
| 386 args.AsValue()); |
| 387 return; |
| 388 } |
| 389 DEBUG_FRAMES("BeginFrameSourceMultiplexer::OnBeginFrame", |
| 390 "action", |
| 391 "using", |
| 392 "new args", |
| 393 args.AsValue()); |
| 394 CallOnBeginFrame(args); |
| 395 } |
| 396 |
| 397 const BeginFrameArgs BeginFrameSourceMultiplexer::LastUsedBeginFrameArgs() |
| 398 const { |
| 399 if (observer_) |
| 400 return observer_->LastUsedBeginFrameArgs(); |
| 401 else |
| 402 return BeginFrameArgs(); |
| 403 } |
| 404 |
| 405 // BeginFrameSource support |
| 406 void BeginFrameSourceMultiplexer::OnNeedsBeginFramesChange( |
| 407 bool needs_begin_frames) { |
| 408 DEBUG_FRAMES("BeginFrameSourceMultiplexer::OnNeedsBeginFramesChange", |
| 409 "active_source", active_source_, "needs_begin_frames", |
| 410 needs_begin_frames); |
| 411 if (active_source_) { |
| 412 active_source_->SetNeedsBeginFrames(needs_begin_frames); |
| 413 } else { |
| 414 DCHECK(!needs_begin_frames); |
| 415 } |
| 416 } |
| 417 |
| 418 void BeginFrameSourceMultiplexer::DidFinishFrame(size_t remaining_frames) { |
| 419 DEBUG_FRAMES("BeginFrameSourceMultiplexer::DidFinishFrame", |
| 420 "active_source", |
| 421 active_source_, |
| 422 "remaining_frames", |
| 423 remaining_frames); |
| 424 if (active_source_) { |
| 425 active_source_->DidFinishFrame(remaining_frames); |
| 426 } |
| 427 } |
| 428 |
| 429 // Tracing support |
| 430 void BeginFrameSourceMultiplexer::AsValueInto( |
| 431 base::trace_event::TracedValue* dict) const { |
| 432 dict->SetString("type", "BeginFrameSourceMultiplexer"); |
| 433 |
| 434 dict->SetInteger("minimum_interval_us", minimum_interval_.InMicroseconds()); |
| 435 if (observer_) { |
| 436 dict->BeginDictionary("last_begin_frame_args"); |
| 437 observer_->LastUsedBeginFrameArgs().AsValueInto(dict); |
| 438 dict->EndDictionary(); |
| 439 } |
| 440 |
| 441 if (active_source_) { |
| 442 dict->BeginDictionary("active_source"); |
| 443 active_source_->AsValueInto(dict); |
| 444 dict->EndDictionary(); |
| 445 } else { |
| 446 dict->SetString("active_source", "NULL"); |
| 447 } |
| 448 |
| 449 dict->BeginArray("sources"); |
| 450 for (std::set<BeginFrameSource*>::const_iterator it = source_list_.begin(); |
| 451 it != source_list_.end(); |
| 452 ++it) { |
| 453 dict->BeginDictionary(); |
| 454 (*it)->AsValueInto(dict); |
| 455 dict->EndDictionary(); |
| 456 } |
| 457 dict->EndArray(); |
| 458 } |
| 459 |
| 460 // protected methods |
| 461 bool BeginFrameSourceMultiplexer::HasSource(BeginFrameSource* source) { |
| 462 return (source_list_.find(source) != source_list_.end()); |
| 463 } |
| 464 |
| 465 bool BeginFrameSourceMultiplexer::IsIncreasing(const BeginFrameArgs& args) { |
| 466 DCHECK(args.IsValid()); |
| 467 if (!observer_) |
| 468 return false; |
| 469 |
| 470 // If the last begin frame is invalid, then any new begin frame is valid. |
| 471 if (!observer_->LastUsedBeginFrameArgs().IsValid()) |
| 472 return true; |
| 473 |
| 474 // Only allow new args have a *strictly bigger* frame_time value and statisfy |
| 475 // minimum interval requirement. |
| 476 return (args.frame_time >= |
| 477 observer_->LastUsedBeginFrameArgs().frame_time + minimum_interval_); |
| 478 } |
| 479 |
| 480 } // namespace cc |
OLD | NEW |