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
|
// Copyright 2023 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/frame_sinks/external_begin_frame_source_mac.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "base/containers/contains.h"
#include "base/metrics/histogram_macros.h"
#include "base/rand_util.h"
#include "base/trace_event/trace_event.h"
namespace viz {
namespace {
// Output level for VLOG. TODO(crbug.com/40062488): Remove loggings after
// CVDisplayLinkBeginFrameSource is cleaned up.
constexpr int kOutputLevel = 4;
constexpr base::TimeDelta kMaxSupportedFrameInterval = base::Hertz(14);
constexpr auto kDeltaAlmostEqual = base::Microseconds(10);
bool AlmostEqual(base::TimeDelta a, base::TimeDelta b) {
return (a - b).magnitude() < kDeltaAlmostEqual;
}
BASE_FEATURE(kForceMacVSyncTimerForDebugging,
"ForceMacVSyncTimerForDebugging",
base::FEATURE_DISABLED_BY_DEFAULT);
// Allow CADisplayLink to handle refresh rate within the range based on the app
// work load.
BASE_FEATURE(kUseRefreshRateRange,
"UseRefreshRateRange",
base::FEATURE_DISABLED_BY_DEFAULT);
// These values are logged to UMA. Entries should not be renumbered and
// numeric values should never be reused. Please keep in sync with
// "DisplayLinkResult" in src/tools/metrics/histograms/enums.xml.
enum class DisplayLinkResult {
kSuccess = 0,
kFailedInvalidDisplayId = 1,
kFailedCreateDisplayLink = 2,
kFailedRegisterCallback = 3,
kMaxValue = kFailedRegisterCallback,
};
void RecordDisplayLinkCreateStatus(DisplayLinkResult result) {
UMA_HISTOGRAM_ENUMERATION("Viz.ExternalBeginFrameSourceMac.DisplayLink",
result);
}
// Record the delay from the system CVDisplayLink or CADisplaylink source to
// VizCompositorThread OnDisplayLinkCallback().
void RecordVSyncCallbackDelay(base::TimeDelta delay) {
UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
"Viz.BeginFrameSource.VSyncCallbackDelay", delay,
/*min=*/base::Microseconds(10),
/*max=*/base::Milliseconds(33), /*bucket_count=*/50);
}
} // namespace
///////////////////////////////////////////////////////////////////////////////
// ExternalBeginFrameSourceMac
ExternalBeginFrameSourceMac::ExternalBeginFrameSourceMac(
uint32_t restart_id,
int64_t display_id,
OutputSurface* output_surface)
: ExternalBeginFrameSource(this, restart_id),
output_surface_(output_surface) {
VLOG(kOutputLevel) << "ExternalBeginFrameSourceMac(" << this << ")"
<< "::ExternalBeginFrameSourceMac() ID:" << display_id;
if (display_id == display::kInvalidDisplayId) {
RecordDisplayLinkCreateStatus(DisplayLinkResult::kFailedInvalidDisplayId);
DLOG(ERROR)
<< "DisplayLinkMac ID is not available. "
"Switch to DelayBasedTimeSource(Timer) for BeginFrameSource.";
} else {
SetVSyncDisplayID(display_id);
}
}
ExternalBeginFrameSourceMac::~ExternalBeginFrameSourceMac() {
VLOG(kOutputLevel) << "ExternalBeginFrameSourceMac(" << this << ")"
<< "::~ExternalBeginFrameSourceMac() ID:" << display_id_;
}
void ExternalBeginFrameSourceMac::CreateDelayBasedTimeSourceIfNeeded() {
if (!time_source_) {
time_source_ = std::make_unique<DelayBasedTimeSource>(
base::SingleThreadTaskRunner::GetCurrentDefault().get());
time_source_->SetClient(this);
time_source_->SetTimebaseAndInterval(base::TimeTicks::Now(),
preferred_interval_);
}
}
void ExternalBeginFrameSourceMac::SetVSyncDisplayID(int64_t display_id) {
if (display_id_ == display_id) {
return;
}
// Forward the |display_id| to output surface for frame presentation.
output_surface_->SetVSyncDisplayID(display_id);
// Remove the current callback from display_link_mac_ or from the timer.
if (needs_begin_frames_) {
StopBeginFrame();
}
// Remove the old DisplayLinkMac.
display_link_mac_.reset();
display_id_ = display_id;
// Get DisplayLinkMac with the new CGDirectDisplayID.
if (display_id != display::kInvalidDisplayId) {
display_link_mac_ = ui::DisplayLinkMac::GetForDisplay(display_id);
}
// For debugging only. Use the timer for BeginFrameSource.
if (base::FeatureList::IsEnabled(kForceMacVSyncTimerForDebugging)) {
display_link_mac_.reset();
}
if (display_link_mac_) {
nominal_refresh_period_ = GetMinimumFrameInterval();
preferred_interval_ = nominal_refresh_period_;
VLOG(kOutputLevel) << "ExternalBeginFrameSourceMac(" << this << ")"
<< "::SetVSyncDisplayID: " << display_id_
<< ", refresh_period_: " << nominal_refresh_period_;
display_link_mac_->GetRefreshIntervalRange(
min_refresh_interval_, max_refresh_interval_, granularity_);
// Call multiple_hw_refresh_rates_callback_ to notify FrameRateDecider
// whether the supported refresh rate list will be provided. If set to
// true, there will not be a list.
if (base::FeatureList::IsEnabled(kUseRefreshRateRange)) {
hw_takes_any_refresh_rate_ =
granularity_ <= base::Milliseconds(1) &&
min_refresh_interval_ != max_refresh_interval_;
if (multiple_hw_refresh_rates_callback_) {
multiple_hw_refresh_rates_callback_.Run(hw_takes_any_refresh_rate_);
}
}
if (update_vsync_params_callback_) {
update_vsync_params_callback_.Run(display_link_mac_->GetCurrentTime(),
nominal_refresh_period_);
}
RecordDisplayLinkCreateStatus(DisplayLinkResult::kSuccess);
} else {
DisplayLinkResult display_link_result =
display_id == display::kInvalidDisplayId
? DisplayLinkResult::kFailedInvalidDisplayId
: DisplayLinkResult::kFailedCreateDisplayLink;
RecordDisplayLinkCreateStatus(display_link_result);
DLOG(ERROR) << "Fail to create DisplayLinkMac with DisplayID: "
<< display_id_ << ". Switch to DelayBasedTimeSource.";
hw_takes_any_refresh_rate_ = false;
if (multiple_hw_refresh_rates_callback_) {
multiple_hw_refresh_rates_callback_.Run(false);
}
}
if (needs_begin_frames_) {
StartBeginFrame();
}
}
void ExternalBeginFrameSourceMac::StartBeginFrame() {
if (display_link_mac_) {
DCHECK(!vsync_callback_mac_);
// Request the callback to be called on the register thread.
vsync_callback_mac_ = display_link_mac_->RegisterCallback(
base::BindRepeating(&ExternalBeginFrameSourceMac::OnDisplayLinkCallback,
weak_ptr_factory_.GetWeakPtr()));
if (vsync_callback_mac_) {
// RegisterCallback succeeded.
return;
}
// Failed. Destroy DisplayLinkMac and switch to the timer.
display_link_mac_.reset();
RecordDisplayLinkCreateStatus(DisplayLinkResult::kFailedRegisterCallback);
DLOG(ERROR) << "Fail to start CVDisplayLink callback for DisplayID: "
<< display_id_ << ". Switch to the timer";
}
// Start the timer.
CreateDelayBasedTimeSourceIfNeeded();
time_source_->SetActive(/*active=*/true);
}
void ExternalBeginFrameSourceMac::StopBeginFrame() {
if (display_link_mac_) {
DCHECK(vsync_callback_mac_);
// Remove and unregister VSyncCallbackMac.
vsync_callback_mac_.reset();
vsyncs_to_skip_ = 0;
return;
}
// Stop the timer.
DCHECK(time_source_);
time_source_->SetActive(/*active=*/false);
}
void ExternalBeginFrameSourceMac::OnNeedsBeginFrames(bool needs_begin_frames) {
if (needs_begin_frames_ == needs_begin_frames) {
return;
}
needs_begin_frames_ = needs_begin_frames;
just_started_begin_frame_ = true;
// TODO: Try to prevent constant switching between callback register and
// unregister.
if (needs_begin_frames_) {
StartBeginFrame();
} else {
StopBeginFrame();
}
}
// Called on the Viz thread.
void ExternalBeginFrameSourceMac::OnDisplayLinkCallback(
ui::VSyncParamsMac params) {
if (!needs_begin_frames_) {
return;
}
if (vsyncs_to_skip_ > 0) {
TRACE_EVENT_INSTANT0(
"viz",
"ExternalBeginFrameSourceMac::OnDisplayLinkCallback - skip_vsync",
TRACE_EVENT_SCOPE_THREAD);
vsyncs_to_skip_--;
return;
}
// Calculate the parameters.
base::TimeTicks frame_time;
base::TimeDelta interval;
auto now = base::TimeTicks::Now();
if (params.callback_times_valid) {
DCHECK(params.callback_timebase != base::TimeTicks());
DCHECK(!params.callback_interval.is_zero());
frame_time = params.callback_timebase;
interval = params.callback_interval;
} else {
// Invalid parameters should be rare. Use the default refresh rate.
frame_time = now;
interval = params.display_times_valid ? params.display_interval
: nominal_refresh_period_;
}
auto callback_delay =
params.callback_times_valid ? (now - frame_time) : base::Microseconds(0);
auto callback_timebase_to_display =
params.display_times_valid ? (params.display_timebase - frame_time)
: base::Microseconds(0);
TRACE_EVENT2("viz", "ExternalBeginFrameSourceMac::OnDisplayLinkCallback",
"callback_timebase_to_display",
callback_timebase_to_display.InMicroseconds(), "callback_delay",
callback_delay.InMicroseconds());
if (base::ShouldRecordSubsampledMetric(0.001)) {
RecordVSyncCallbackDelay(callback_delay);
}
bool display_link_frame_interval_changed =
!AlmostEqual(nominal_refresh_period_, interval);
nominal_refresh_period_ = interval;
// If the preferred frame interval is not equal to |nominal_refresh_period_|,
// vsync_subsampling_factor_ is bigger than 1.
vsyncs_to_skip_ = vsync_subsampling_factor_ - 1;
interval *= vsync_subsampling_factor_;
OnBeginFrame(begin_frame_args_generator_.GenerateBeginFrameArgs(
source_id(), frame_time, frame_time + interval, interval));
// Notify Display FrameRateDecider of the frame interval change.
if (display_link_frame_interval_changed) {
DCHECK(update_vsync_params_callback_);
VLOG(kOutputLevel) << "ExternalBeginFrameSourceMac(" << this << ")"
<< "::OnDisplayLinkCallback: " << display_id_
<< ", nominal_refresh_period_: "
<< nominal_refresh_period_;
update_vsync_params_callback_.Run(frame_time, nominal_refresh_period_);
} else if (!just_started_begin_frame_) {
// There might be delay between the system CVDisplayLink thread and
// the VizCompositorThread for the CVDisplayLink Callback. This histogram
// has accounted for the delays in the VizCompositorThread
base::TimeDelta delta = now - (last_frame_time_ + last_interval_);
RecordBeginFrameSourceAccuracy(delta);
}
just_started_begin_frame_ = false;
last_frame_time_ = frame_time;
last_interval_ = interval;
}
BeginFrameArgs ExternalBeginFrameSourceMac::GetMissedBeginFrameArgs(
BeginFrameObserver* obs) {
auto frame_time = last_begin_frame_args_.frame_time;
auto interval = last_begin_frame_args_.interval;
// Create BeginFrameArgs for now so that we don't have to wait until vsync.
if (display_link_mac_) {
base::TimeTicks now = display_link_mac_->GetCurrentTime();
if (last_begin_frame_args_.IsValid()) {
frame_time = now.SnappedToNextTick(frame_time, interval) - interval;
} else {
frame_time = now;
interval = nominal_refresh_period_ * vsync_subsampling_factor_;
}
} else {
base::TimeTicks now = base::TimeTicks::Now();
if (last_begin_frame_args_.IsValid()) {
frame_time = now.SnappedToNextTick(frame_time, interval) - interval;
} else {
frame_time = now;
interval = preferred_interval_;
}
}
// Don't create new args unless we've actually moved past the previous frame.
if (!last_begin_frame_args_.IsValid() ||
frame_time > last_begin_frame_args_.frame_time) {
last_begin_frame_args_ = begin_frame_args_generator_.GenerateBeginFrameArgs(
source_id(), frame_time, frame_time + interval, interval);
}
return ExternalBeginFrameSource::GetMissedBeginFrameArgs(obs);
}
// Timer callbacks when DisplayLink is not available.
void ExternalBeginFrameSourceMac::OnTimerTick() {
if (!needs_begin_frames_) {
return;
}
// See comments in DelayBasedBeginFrameSource::OnTimerTick regarding the
// computation of `frame_time`.
base::TimeTicks frame_time =
std::max(time_source_->LastTickTime(),
time_source_->NextTickTime() - time_source_->Interval());
auto interval = time_source_->Interval();
OnBeginFrame(begin_frame_args_generator_.GenerateBeginFrameArgs(
source_id(), frame_time, time_source_->NextTickTime(), interval));
if (last_interval_ != interval) {
DCHECK(update_vsync_params_callback_);
update_vsync_params_callback_.Run(frame_time, interval);
}
last_frame_time_ = frame_time;
last_interval_ = interval;
}
void ExternalBeginFrameSourceMac::SetPreferredInterval(
base::TimeDelta interval) {
preferred_interval_ = interval;
VLOG(kOutputLevel) << "ExternalBeginFrameSourceMac(" << this << ")"
<< "::SetPreferredInterval: ID: " << display_id_
<< ", Interval: " << interval;
if (!display_link_mac_) {
time_source_->SetTimebaseAndInterval(last_frame_time_, interval);
return;
}
// For the monitor with multitple refresh rates and CVDisplayLink
// SetPreferredInterval is supported. Just set the preferred interval without
// skipping VSyncs.
if (min_refresh_interval_ != max_refresh_interval_) {
if (base::FeatureList::IsEnabled(kUseRefreshRateRange)) {
// Request a dynamic refrate rate with a range.
display_link_mac_->SetPreferredIntervalRange(
min_refresh_interval_, max_refresh_interval_, interval);
} else {
// Request a fixed refresh rate.
display_link_mac_->SetPreferredInterval(interval);
}
nominal_refresh_period_ = interval;
vsync_subsampling_factor_ = 1;
vsyncs_to_skip_ = 0;
return;
}
// Here is for the monitor with a fixed refresh rate.
// Cap the preferred refresh interval if it's out of the range.
base::TimeDelta adjusted_interval = interval;
if (interval < nominal_refresh_period_) {
adjusted_interval = nominal_refresh_period_;
} else if (interval > kMaxSupportedFrameInterval &&
!AlmostEqual(interval, nominal_refresh_period_)) {
adjusted_interval = kMaxSupportedFrameInterval;
}
// Keep |vsyncs_to_skip_| unchanged so it will complete the whole frame
// interal.
vsync_subsampling_factor_ =
adjusted_interval.IntDiv((nominal_refresh_period_ - kDeltaAlmostEqual));
TRACE_EVENT1("gpu", "ExternalBeginFrameSourceMac::SetPreferredInterval",
"vsync_subsampling_factor", vsync_subsampling_factor_);
}
base::TimeDelta ExternalBeginFrameSourceMac::GetMinimumFrameInterval() {
if (display_link_mac_) {
auto refresh_rate = display_link_mac_->GetRefreshRate();
if (refresh_rate) {
return base::Seconds(1) / refresh_rate;
}
}
return BeginFrameArgs::DefaultInterval();
}
void ExternalBeginFrameSourceMac::SetUpdateVSyncParametersCallback(
UpdateVSyncParametersCallback callback) {
update_vsync_params_callback_ = callback;
}
void ExternalBeginFrameSourceMac::SetMultipleHWRefreshRatesCallback(
MultipleHWRefreshRatesCallback callback) {
multiple_hw_refresh_rates_callback_ = callback;
}
base::flat_set<base::TimeDelta>
ExternalBeginFrameSourceMac::GetSupportedFrameIntervals(
base::TimeDelta current_interval) {
VLOG(kOutputLevel) << "ExternalBeginFrameSourceMac(" << this << ")"
<< "::GetSupportedFrameIntervals: ID: " << display_id_;
// When CAdisplayLink will take any preferred refresh rate, return an empty
// supported_intervals list.
if (display_link_mac_ && hw_takes_any_refresh_rate_) {
return {};
}
if (nominal_refresh_period_ > kMaxSupportedFrameInterval &&
min_refresh_interval_ == max_refresh_interval_) {
VLOG(kOutputLevel) << "nominal_refresh_period_: "
<< nominal_refresh_period_;
return {nominal_refresh_period_};
}
if (granularity_.is_zero()) {
return {nominal_refresh_period_};
}
base::flat_set<base::TimeDelta> supported_intervals;
// Check if we can set various preferred intervals within the range.
if (display_link_mac_ && min_refresh_interval_ != max_refresh_interval_) {
// |max_refresh_interval_| might not be the same as
// (|min_refresh_interval_| + n*|granularity_|), so add
// |max_refresh_interval_| separately after the loop.
auto upper_bound = max_refresh_interval_ - granularity_ / 2;
for (base::TimeDelta interval = min_refresh_interval_;
interval < upper_bound; interval += granularity_) {
supported_intervals.insert(interval);
}
supported_intervals.insert(max_refresh_interval_);
return supported_intervals;
}
// Can only do fixed refresh rates. Now try to implement 2^n refresh
// rates by skipping VSyncs.
nominal_refresh_period_ = GetMinimumFrameInterval();
base::TimeDelta interval = nominal_refresh_period_;
while (interval <= kMaxSupportedFrameInterval) {
VLOG(kOutputLevel) << interval;
supported_intervals.insert(interval);
interval *= 2;
}
return supported_intervals;
}
} // namespace viz
|