1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/service/display/display_scheduler.h"
#include <algorithm>
#include <utility>
#include <vector>
#include "base/auto_reset.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/notimplemented.h"
#include "base/task/delay_policy.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "components/viz/common/features.h"
#include "components/viz/service/performance_hint/hint_session.h"
namespace viz {
namespace {
base::TimeDelta ComputeAdpfTarget(const BeginFrameArgs& args) {
if (args.possible_deadlines) {
const auto& deadline = args.possible_deadlines->GetPreferredDeadline();
// Arbitrarily use 75% of the deadline for CPU work.
return deadline.latch_delta * 3 / 4;
}
return base::Milliseconds(12);
}
bool DrawImmediatelyWhenInteractive() {
return features::ShouldDrawImmediatelyWhenInteractive();
}
bool AdpfCanUseSetThreads() {
#if BUILDFLAG(IS_ANDROID)
return android_get_device_api_level() >= __ANDROID_API_U__ &&
base::FeatureList::IsEnabled(features::kEnableADPFSetThreads);
#else
return false;
#endif
}
} // namespace
class DisplayScheduler::BeginFrameObserver : public BeginFrameObserverBase {
public:
explicit BeginFrameObserver(DisplayScheduler* scheduler)
: scheduler_(scheduler) {
// The DisplayScheduler handles animate_only BeginFrames as if they were
// normal BeginFrames: Clients won't commit a CompositorFrame but will still
// acknowledge when they have completed the BeginFrame via BeginFrameAcks
// and the DisplayScheduler will still indicate when all clients have
// finished via DisplayObserver::OnDisplayDidFinishFrame.
wants_animate_only_begin_frames_ = true;
}
// BeginFrameObserverBase implementation.
void OnBeginFrameSourcePausedChanged(bool paused) override {
// TODO(crbug.com/40663506): DisplayScheduler doesn't handle
// BeginFrameSource pause but it can happen on WebXR.
if (paused) {
NOTIMPLEMENTED();
}
}
bool OnBeginFrameDerivedImpl(const BeginFrameArgs& args) override {
return scheduler_->OnBeginFrame(args);
}
private:
const raw_ptr<DisplayScheduler> scheduler_;
};
DisplayScheduler::DisplayScheduler(BeginFrameSource* begin_frame_source,
base::SingleThreadTaskRunner* task_runner,
PendingSwapParams pending_swap_params,
HintSessionFactory* hint_session_factory,
bool wait_for_all_surfaces_before_draw)
: begin_frame_observer_(std::make_unique<BeginFrameObserver>(this)),
begin_frame_source_(begin_frame_source),
task_runner_(task_runner),
inside_surface_damaged_(false),
visible_(false),
output_surface_lost_(false),
inside_begin_frame_deadline_interval_(false),
needs_draw_(false),
has_pending_surfaces_(false),
next_swap_id_(1),
pending_swaps_(0),
pending_swap_params_(std::move(pending_swap_params)),
wait_for_all_surfaces_before_draw_(wait_for_all_surfaces_before_draw),
observing_begin_frame_source_(false),
hint_session_factory_(hint_session_factory) {
begin_frame_deadline_timer_.SetTaskRunner(task_runner);
begin_frame_deadline_closure_ = base::BindRepeating(
&DisplayScheduler::OnBeginFrameDeadline, weak_ptr_factory_.GetWeakPtr());
session_states_.emplace_back(HintSession::SessionType::kAnimation);
if (base::FeatureList::IsEnabled(
features::kEnableADPFSeparateRendererMainSession)) {
session_states_.emplace_back(HintSession::SessionType::kRendererMain);
}
}
DisplayScheduler::~DisplayScheduler() {
// It is possible for DisplayScheduler to be destroyed while there's an
// in-flight swap. So always mark the gpu as not busy during destruction.
begin_frame_source_->SetIsGpuBusy(false);
StopObservingBeginFrames();
}
DisplayScheduler::AdpfSessionState::AdpfSessionState(
HintSession::SessionType type)
: type(type) {}
DisplayScheduler::AdpfSessionState::AdpfSessionState(AdpfSessionState&&) =
default;
DisplayScheduler::AdpfSessionState::~AdpfSessionState() = default;
void DisplayScheduler::SetDamageTracker(DisplayDamageTracker* damage_tracker) {
DisplaySchedulerBase::SetDamageTracker(damage_tracker);
damage_tracker->SetDisplayBeginFrameSourceId(
begin_frame_source_->source_id());
}
void DisplayScheduler::SetVisible(bool visible) {
if (visible_ == visible) {
return;
}
visible_ = visible;
// If going invisible, we'll stop observing begin frames once we try
// to draw and fail.
MaybeStartObservingBeginFrames();
ScheduleBeginFrameDeadline();
}
void DisplayScheduler::OnRootFrameMissing(bool missing) {
MaybeStartObservingBeginFrames();
ScheduleBeginFrameDeadline();
}
void DisplayScheduler::OnDisplayDamaged(SurfaceId surface_id) {
// We may cause a new BeginFrame to be run inside this method, but to help
// avoid being reentrant to the caller of SurfaceDamaged, track when this is
// happening with |inside_surface_damaged_|.
base::AutoReset<bool> auto_reset(&inside_surface_damaged_, true);
needs_draw_ = true;
MaybeStartObservingBeginFrames();
UpdateHasPendingSurfaces();
ScheduleBeginFrameDeadline();
}
void DisplayScheduler::OnPendingSurfacesChanged() {
if (UpdateHasPendingSurfaces())
ScheduleBeginFrameDeadline();
}
base::TimeDelta DisplayScheduler::GetDeadlineOffset(
base::TimeDelta interval) const {
return BeginFrameArgs::DefaultEstimatedDisplayDrawTime(interval);
}
// This is used to force an immediate swap before a resize.
void DisplayScheduler::ForceImmediateSwapIfPossible() {
TRACE_EVENT0("viz", "DisplayScheduler::ForceImmediateSwapIfPossible");
bool in_begin = inside_begin_frame_deadline_interval_;
bool did_draw = AttemptDrawAndSwap();
if (in_begin)
DidFinishFrame(did_draw);
}
bool DisplayScheduler::UpdateHasPendingSurfaces() {
// If we're not currently inside a deadline interval, we will call
// UpdateHasPendingSurfaces() again during OnBeginFrameImpl().
if (!inside_begin_frame_deadline_interval_ || !client_)
return false;
bool old_value = has_pending_surfaces_;
has_pending_surfaces_ =
damage_tracker_->HasPendingSurfaces(current_begin_frame_args_);
return has_pending_surfaces_ != old_value;
}
void DisplayScheduler::OutputSurfaceLost() {
TRACE_EVENT0("viz", "DisplayScheduler::OutputSurfaceLost");
output_surface_lost_ = true;
ScheduleBeginFrameDeadline();
}
void DisplayScheduler::MaybeCreateHintSessions(
base::flat_set<base::PlatformThreadId> animation_thread_ids,
base::flat_set<base::PlatformThreadId> renderer_main_thread_ids) {
if (!hint_session_factory_)
return;
if (!renderer_main_thread_ids.empty() &&
!base::FeatureList::IsEnabled(
features::kEnableADPFSeparateRendererMainSession)) {
animation_thread_ids.insert(renderer_main_thread_ids.begin(),
renderer_main_thread_ids.end());
renderer_main_thread_ids.clear();
}
const auto adpf_target = ComputeAdpfTarget(current_begin_frame_args_);
for (auto& state : session_states_) {
auto thread_ids = state.type == HintSession::SessionType::kAnimation
? animation_thread_ids
: renderer_main_thread_ids;
// Use SetThreads whenever possible - it's cheaper than recreating the
// session.
if (state.hint_session && state.thread_ids != thread_ids &&
AdpfCanUseSetThreads()) {
state.thread_ids = std::move(thread_ids);
state.hint_session->SetThreads(hint_session_factory_->GetSessionThreadIds(
state.thread_ids, state.type));
continue;
}
// If SetThreads cannot be used, (re)create the ADPF session - this is a
// more a expensive operation.
if ((!state.create_session_for_current_thread_ids_failed &&
!state.hint_session) ||
state.thread_ids != thread_ids) {
state.hint_session.reset();
state.thread_ids = std::move(thread_ids);
state.hint_session = hint_session_factory_->CreateSession(
state.thread_ids, adpf_target, state.type);
state.create_session_for_current_thread_ids_failed = !state.hint_session;
}
}
}
void DisplayScheduler::ReportFrameTime(
base::TimeDelta frame_time,
base::flat_set<base::PlatformThreadId> animation_thread_ids,
base::flat_set<base::PlatformThreadId> renderer_main_thread_ids,
base::TimeTicks draw_start,
HintSession::BoostType boost_type) {
MaybeCreateHintSessions(std::move(animation_thread_ids),
std::move(renderer_main_thread_ids));
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES("Compositing.Display.AdpfHintUs",
frame_time, base::Microseconds(1),
base::Milliseconds(50), 50);
for (const auto& state : session_states_) {
if (state.hint_session) {
state.hint_session->ReportCpuCompletionTime(frame_time, draw_start,
boost_type);
}
}
}
bool DisplayScheduler::DrawAndSwap() {
TRACE_EVENT0("viz", "DisplayScheduler::DrawAndSwap");
DCHECK_LT(pending_swaps_,
std::max(pending_swap_params_.max_pending_swaps,
pending_swap_params_.max_pending_swaps_120hz.value_or(0)));
DCHECK(!output_surface_lost_);
DrawAndSwapParams params{current_begin_frame_args_.frame_time,
current_frame_display_time(), MaxPendingSwaps()};
if (current_begin_frame_args_.possible_deadlines) {
params.choreographer_vsync_id =
current_begin_frame_args_.possible_deadlines->GetPreferredDeadline()
.vsync_id;
}
bool success = client_ && client_->DrawAndSwap(params);
if (!success)
return false;
needs_draw_ = false;
return true;
}
bool DisplayScheduler::OnBeginFrame(const BeginFrameArgs& args) {
base::TimeTicks now = base::TimeTicks::Now();
TRACE_EVENT2("viz", "DisplayScheduler::BeginFrame", "args", args.AsValue(),
"now", now);
if (inside_surface_damaged_) {
// Repost this so that we don't run a missed BeginFrame on the same
// callstack. Otherwise we end up running unexpected scheduler actions
// immediately while inside some other action (such as submitting a
// CompositorFrame for a SurfaceFactory).
DCHECK_EQ(args.type, BeginFrameArgs::MISSED);
DCHECK(missed_begin_frame_task_.IsCancelled());
missed_begin_frame_task_.Reset(
base::BindOnce(base::IgnoreResult(&DisplayScheduler::OnBeginFrame),
// The CancelableOnceCallback will not run after it is
// destroyed, which happens when |this| is destroyed.
base::Unretained(this), args));
task_runner_->PostTask(FROM_HERE, missed_begin_frame_task_.callback());
return true;
}
// Save the |BeginFrameArgs| as the callback (missed_begin_frame_task_) can be
// destroyed if we StopObservingBeginFrames(), and it would take the |args|
// with it. Instead save the args and cancel the |missed_begin_frame_task_|.
BeginFrameArgs save_args = args;
// If we get another BeginFrame before a posted missed frame, just drop the
// missed frame. Also if this was the missed frame, drop the Callback inside
// it.
missed_begin_frame_task_.Cancel();
// Although this always runs "in sequence" with the VizCompositorThread, it
// may run on another thread (see use of `RunOrPostTask` in
// `ExternalBeginFrameSourceWin`). To keep that other thread responsive, run
// the continuation on the VizCompositorThread if a potentially expensive call
// to `OnBeginFrameDeadline()` is needed.
CHECK(task_runner_->RunsTasksInCurrentSequence());
if (inside_begin_frame_deadline_interval_ &&
!task_runner_->BelongsToCurrentThread()) {
task_runner_->PostTask(
FROM_HERE, base::BindOnce(&DisplayScheduler::OnBeginFrameContinuation,
weak_ptr_factory_.GetWeakPtr(), save_args));
} else {
OnBeginFrameContinuation(save_args);
}
return true;
}
void DisplayScheduler::OnBeginFrameContinuation(const BeginFrameArgs& args) {
// If we get another BeginFrame before the previous deadline,
// synchronously trigger the previous deadline before progressing.
if (inside_begin_frame_deadline_interval_) {
OnBeginFrameDeadline();
}
// Schedule the deadline.
current_begin_frame_args_ = args;
const auto adpf_target = ComputeAdpfTarget(args);
for (auto& state : session_states_) {
if (state.hint_session) {
state.hint_session->UpdateTargetDuration(adpf_target);
}
}
current_begin_frame_args_.deadline -=
BeginFrameArgs::DefaultEstimatedDisplayDrawTime(args.interval);
inside_begin_frame_deadline_interval_ = true;
UpdateHasPendingSurfaces();
ScheduleBeginFrameDeadline();
}
int DisplayScheduler::MaxPendingSwaps() const {
// Interval for 72, 90, and 120hz with some delta for margin of error.
constexpr base::TimeDelta k72HzInterval = base::Microseconds(14000);
constexpr base::TimeDelta k90HzInterval = base::Microseconds(11500);
constexpr base::TimeDelta k120HzInterval = base::Microseconds(8500);
int param_max_pending_swaps;
if (current_begin_frame_args_.interval < k120HzInterval &&
pending_swap_params_.max_pending_swaps_120hz) {
param_max_pending_swaps =
pending_swap_params_.max_pending_swaps_120hz.value();
} else if (current_begin_frame_args_.interval < k90HzInterval &&
pending_swap_params_.max_pending_swaps_90hz) {
param_max_pending_swaps =
pending_swap_params_.max_pending_swaps_90hz.value();
} else if (current_begin_frame_args_.interval < k72HzInterval &&
pending_swap_params_.max_pending_swaps_72hz) {
param_max_pending_swaps =
pending_swap_params_.max_pending_swaps_72hz.value();
} else {
param_max_pending_swaps = pending_swap_params_.max_pending_swaps;
}
if (!current_begin_frame_args_.possible_deadlines) {
return param_max_pending_swaps;
}
// Estimate the max pending swap based on the frame rate and presentation
// time.
const auto& deadline =
current_begin_frame_args_.possible_deadlines->GetPreferredDeadline();
int64_t total_time_nanos = deadline.present_delta.InNanoseconds();
int64_t interval_nanos = current_begin_frame_args_.interval.InNanoseconds();
// Assuming no frames are dropped, then:
// * A new frame is started every `interval_nanos`.
// * A buffer is returned after its present time is passed.
// This gives us the formula that number of pending swaps needed (ie the
// max) is the number of new frames that can be started before the buffer
// for a frame is returned: total_time_nanos / interval_nanos.
// However present time is generally not an exact multiple of interval, and
// here the 0.8 constant is chosen to bias rounding up.
int deadline_max_pending_swaps =
(total_time_nanos + 0.8 * interval_nanos) / interval_nanos;
return std::clamp(deadline_max_pending_swaps, 1, param_max_pending_swaps);
}
void DisplayScheduler::SetNeedsOneBeginFrame(bool needs_draw) {
// If we are not currently observing BeginFrames because needs_draw_ is false,
// we will stop observing again after one BeginFrame in AttemptDrawAndSwap().
StartObservingBeginFrames();
if (needs_draw)
needs_draw_ = true;
}
void DisplayScheduler::MaybeStartObservingBeginFrames() {
if (ShouldDraw())
StartObservingBeginFrames();
}
void DisplayScheduler::StartObservingBeginFrames() {
if (!observing_begin_frame_source_) {
begin_frame_source_->AddObserver(begin_frame_observer_.get());
observing_begin_frame_source_ = true;
}
}
void DisplayScheduler::StopObservingBeginFrames() {
if (observing_begin_frame_source_) {
begin_frame_source_->RemoveObserver(begin_frame_observer_.get());
observing_begin_frame_source_ = false;
// A missed BeginFrame may be queued, so drop that too if we're going to
// stop listening.
missed_begin_frame_task_.Cancel();
}
}
bool DisplayScheduler::ShouldDraw() const {
// Note: When any of these cases becomes true, MaybeStartObservingBeginFrames
// must be called to ensure the draw will happen.
return needs_draw_ && !output_surface_lost_ && visible_ &&
!damage_tracker_->root_frame_missing();
}
// static
base::TimeTicks DisplayScheduler::DesiredBeginFrameDeadlineTime(
BeginFrameDeadlineMode deadline_mode,
BeginFrameArgs begin_frame_args) {
switch (deadline_mode) {
case BeginFrameDeadlineMode::kImmediate:
return base::TimeTicks();
case BeginFrameDeadlineMode::kRegular:
return begin_frame_args.deadline;
case BeginFrameDeadlineMode::kLate:
return begin_frame_args.frame_time + begin_frame_args.interval;
case BeginFrameDeadlineMode::kNone:
return base::TimeTicks::Max();
default:
NOTREACHED();
}
}
DisplayScheduler::BeginFrameDeadlineMode
DisplayScheduler::AdjustedBeginFrameDeadlineMode() const {
BeginFrameDeadlineMode mode = DesiredBeginFrameDeadlineMode();
// In blocking mode, late and regular deadline should not apply. Wait
// indefinitely instead.
if (wait_for_all_surfaces_before_draw_ &&
(mode == BeginFrameDeadlineMode::kRegular ||
mode == BeginFrameDeadlineMode::kLate)) {
return BeginFrameDeadlineMode::kNone;
}
return mode;
}
DisplayScheduler::BeginFrameDeadlineMode
DisplayScheduler::DesiredBeginFrameDeadlineMode() const {
if (output_surface_lost_) {
TRACE_EVENT_INSTANT0("viz", "Lost output surface",
TRACE_EVENT_SCOPE_THREAD);
return BeginFrameDeadlineMode::kImmediate;
}
const int max_pending_swaps = MaxPendingSwaps();
if (pending_swaps_ >= max_pending_swaps) {
TRACE_EVENT_INSTANT2("viz", "Swap throttled", TRACE_EVENT_SCOPE_THREAD,
"pending_swaps", pending_swaps_, "max_pending_swaps",
max_pending_swaps);
return BeginFrameDeadlineMode::kLate;
}
if (damage_tracker_->root_frame_missing()) {
TRACE_EVENT_INSTANT0("viz", "Root frame missing", TRACE_EVENT_SCOPE_THREAD);
return BeginFrameDeadlineMode::kLate;
}
// Only wait if we actually have pending surfaces and we're not forcing draw
// due to an ongoing interaction.
bool wait_for_pending_surfaces =
has_pending_surfaces_ && !(DrawImmediatelyWhenInteractive() &&
damage_tracker_->HasDamageDueToInteraction());
bool all_surfaces_ready =
!wait_for_pending_surfaces && damage_tracker_->IsRootSurfaceValid() &&
!damage_tracker_->expecting_root_surface_damage_because_of_resize();
// When no draw is needed, only allow an early deadline in full-pipe mode.
// This way, we can unblock the BeginFrame in full-pipe mode if no draw is
// necessary, but accommodate damage as a result of missed BeginFrames from
// clients otherwise.
bool allow_early_deadline_without_draw = wait_for_all_surfaces_before_draw_;
if (all_surfaces_ready &&
(needs_draw_ || allow_early_deadline_without_draw)) {
TRACE_EVENT_INSTANT0("viz", "All active surfaces ready",
TRACE_EVENT_SCOPE_THREAD);
return BeginFrameDeadlineMode::kImmediate;
}
if (!needs_draw_) {
TRACE_EVENT_INSTANT0("viz", "No damage yet", TRACE_EVENT_SCOPE_THREAD);
return BeginFrameDeadlineMode::kLate;
}
// TODO(mithro): Be smarter about resize deadlines.
if (damage_tracker_->expecting_root_surface_damage_because_of_resize()) {
TRACE_EVENT_INSTANT0("viz", "Entire display damaged",
TRACE_EVENT_SCOPE_THREAD);
return BeginFrameDeadlineMode::kLate;
}
TRACE_EVENT_INSTANT0("viz", "More damage expected soon",
TRACE_EVENT_SCOPE_THREAD);
return BeginFrameDeadlineMode::kRegular;
}
void DisplayScheduler::ScheduleBeginFrameDeadline() {
TRACE_EVENT0("viz", "DisplayScheduler::ScheduleBeginFrameDeadline");
// We need to wait for the next BeginFrame before scheduling a deadline.
if (!inside_begin_frame_deadline_interval_) {
TRACE_EVENT_INSTANT0("viz", "Waiting for next BeginFrame",
TRACE_EVENT_SCOPE_THREAD);
DCHECK(!begin_frame_deadline_timer_.IsRunning());
return;
}
// Determine the deadline we want to use.
BeginFrameDeadlineMode deadline_mode = AdjustedBeginFrameDeadlineMode();
base::TimeTicks desired_deadline =
DesiredBeginFrameDeadlineTime(deadline_mode, current_begin_frame_args_);
// Avoid re-scheduling the deadline if it's already correctly scheduled.
if (begin_frame_deadline_timer_.IsRunning() &&
desired_deadline == begin_frame_deadline_task_time_) {
TRACE_EVENT_INSTANT0("viz", "Using existing deadline",
TRACE_EVENT_SCOPE_THREAD);
return;
}
// Schedule the deadline.
begin_frame_deadline_task_time_ = desired_deadline;
begin_frame_deadline_timer_.Stop();
if (begin_frame_deadline_task_time_ == base::TimeTicks::Max()) {
TRACE_EVENT_INSTANT0("viz", "Using infinite deadline",
TRACE_EVENT_SCOPE_THREAD);
return;
}
begin_frame_deadline_timer_.Start(FROM_HERE, desired_deadline,
begin_frame_deadline_closure_,
base::subtle::DelayPolicy::kPrecise);
TRACE_EVENT2("viz", "Using new deadline", "deadline_mode", deadline_mode,
"desired_deadline", desired_deadline);
}
bool DisplayScheduler::AttemptDrawAndSwap() {
inside_begin_frame_deadline_interval_ = false;
begin_frame_deadline_timer_.Stop();
begin_frame_deadline_task_time_ = base::TimeTicks();
if (ShouldDraw()) {
if (pending_swaps_ < MaxPendingSwaps())
return DrawAndSwap();
} else {
// We are going idle, so reset expectations.
// TODO(eseckler): Should we avoid going idle if
// |expecting_root_surface_damage_because_of_resize_| is true?
damage_tracker_->reset_expecting_root_surface_damage_because_of_resize();
StopObservingBeginFrames();
}
return false;
}
void DisplayScheduler::OnBeginFrameDeadline() {
TRACE_EVENT0("viz,input.scrolling", "DisplayScheduler::OnBeginFrameDeadline");
DCHECK(inside_begin_frame_deadline_interval_);
bool did_draw = AttemptDrawAndSwap();
DidFinishFrame(did_draw);
}
void DisplayScheduler::DidFinishFrame(bool did_draw) {
DCHECK(begin_frame_source_);
begin_frame_source_->DidFinishFrame(begin_frame_observer_.get());
BeginFrameAck ack(current_begin_frame_args_, did_draw);
if (client_)
client_->DidFinishFrame(ack);
damage_tracker_->DidFinishFrame();
}
void DisplayScheduler::DidSwapBuffers() {
pending_swaps_++;
if (pending_swaps_ >= MaxPendingSwaps())
begin_frame_source_->SetIsGpuBusy(true);
uint32_t swap_id = next_swap_id_++;
TRACE_EVENT_ASYNC_BEGIN0("viz", "DisplayScheduler:pending_swaps", swap_id);
}
void DisplayScheduler::DidReceiveSwapBuffersAck() {
uint32_t swap_id = next_swap_id_ - pending_swaps_;
pending_swaps_--;
// It is important to call this after updating |pending_swaps_| above to
// ensure any callback from BeginFrameSource observes the correct swap
// throttled state.
begin_frame_source_->SetIsGpuBusy(false);
TRACE_EVENT_ASYNC_END0("viz", "DisplayScheduler:pending_swaps", swap_id);
ScheduleBeginFrameDeadline();
}
} // namespace viz
|