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
|
// 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 "ash/display/refresh_rate_controller.h"
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "ash/shell.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ui_base_features.h"
#include "ui/compositor/compositor.h"
#include "ui/display/display_features.h"
#include "ui/display/screen.h"
#include "ui/display/types/display_snapshot.h"
#include "ui/display/util/display_util.h"
namespace ash {
namespace {
const float kMinThrottledRefreshRate = 59.f;
using DisplayStateList = display::DisplayConfigurator::DisplayStateList;
using ModeState = DisplayPerformanceModeController::ModeState;
using RefreshRateOverrideMap =
display::DisplayConfigurator::RefreshRateOverrideMap;
std::string RefreshRatesToString(const std::vector<float>& refresh_rates) {
std::vector<std::string> entries;
for (auto refresh_rate : refresh_rates) {
entries.push_back(base::NumberToString(refresh_rate));
}
return "{" + base::JoinString(entries, ", ") + "}";
}
} // namespace
RefreshRateController::RefreshRateController(
display::DisplayConfigurator* display_configurator,
PowerStatus* power_status,
DisplayPerformanceModeController* display_performance_mode_controller)
: display_configurator_(display_configurator),
power_status_(power_status),
display_performance_mode_controller_(display_performance_mode_controller),
force_throttle_(
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceRefreshRateThrottle)) {
power_status_observer_.Observe(power_status);
display_configurator_observer_.Observe(display_configurator);
current_performance_mode_ =
display_performance_mode_controller_->AddObserver(this);
// Ensure initial states are calculated.
UpdateStates();
OnDisplayConfigurationChanged(display_configurator->cached_displays());
}
RefreshRateController::~RefreshRateController() {
display_performance_mode_controller_->RemoveObserver(this);
}
void RefreshRateController::OnPowerStatusChanged() {
UpdateStates();
}
void RefreshRateController::SetGameMode(aura::Window* window,
bool game_mode_on) {
// Update the |game_window_observer_|.
if (game_mode_on) {
if (game_window_observer_.GetSource() != window) {
game_window_observer_.Reset();
// The GameModeController will always turn off game mode before the
// observed window is destroyed.
game_window_observer_.Observe(window);
}
} else {
if (game_window_observer_.GetSource() == window) {
game_window_observer_.Reset();
} else {
DCHECK(!game_window_observer_.IsObserving());
// Game mode is already off. Nothing to do.
}
}
UpdateStates();
}
void RefreshRateController::OnWindowAddedToRootWindow(aura::Window* window) {
DCHECK_EQ(window, game_window_observer_.GetSource());
// Refresh state in case the window changed displays.
UpdateStates();
}
void RefreshRateController::OnWindowDestroying(aura::Window* window) {
DCHECK_EQ(window, game_window_observer_.GetSource());
game_window_observer_.Reset();
UpdateStates();
}
void RefreshRateController::OnDisplayConfigurationChanged(
const DisplayStateList& displays) {
for (const display::DisplaySnapshot* snapshot : displays) {
if (!snapshot->current_mode()) {
continue;
}
UpdateSeamlessRefreshRates(snapshot->display_id());
}
}
void RefreshRateController::StopObservingPowerStatusForTest() {
power_status_observer_.Reset();
power_status_ = nullptr;
}
void RefreshRateController::UpdateSeamlessRefreshRates(int64_t display_id) {
// Don't attempt dynamic refresh rate adjustment with hardware mirroring
// enabled.
if (display::features::IsHardwareMirrorModeEnabled()) {
return;
}
auto callback =
base::BindOnce(&RefreshRateController::OnSeamlessRefreshRatesReceived,
weak_ptr_factory_.GetWeakPtr(), display_id);
display_configurator_->GetSeamlessRefreshRates(display_id,
std::move(callback));
}
void RefreshRateController::OnSeamlessRefreshRatesReceived(
int64_t display_id,
const std::optional<std::vector<float>>& received_refresh_rates) {
VLOG(3) << "Received refresh rates for display " << display_id << ": "
<< (received_refresh_rates
? RefreshRatesToString(*received_refresh_rates)
: "empty");
if (!received_refresh_rates || received_refresh_rates->empty()) {
// These cases could occur if there is a race between requesting the refresh
// rates and some display topology change such as removing or disabling a
// display.
display_refresh_rates_.erase(display_id);
refresh_rate_preferences_.erase(display_id);
return;
}
// Sort in ascending order.
std::vector<float> refresh_rates = received_refresh_rates.value();
std::sort(refresh_rates.begin(), refresh_rates.end());
// If the received refresh rates are equal to the last received refresh rates,
// then we're done.
auto it = display_refresh_rates_.find(display_id);
if (it != display_refresh_rates_.end() && it->second == refresh_rates) {
return;
}
// Insert the new refresh rates, possibly replacing the old ones.
display_refresh_rates_[display_id] = std::move(refresh_rates);
refresh_rate_preferences_.erase(display_id);
RefreshOverrideState();
aura::Window* window = Shell::GetRootWindowForDisplayId(display_id);
if (window) {
window->GetHost()->compositor()->SetSeamlessRefreshRates(
display_refresh_rates_[display_id]);
}
}
void RefreshRateController::OnDisplayMetricsChanged(
const display::Display& display,
uint32_t changed_metrics) {
if (game_window_observer_.IsObserving() &&
(changed_metrics & DISPLAY_METRIC_PRIMARY)) {
// Refresh state in case the window is affected by the primary display
// change.
UpdateStates();
}
}
void RefreshRateController::OnDisplayPerformanceModeChanged(
ModeState new_state) {
current_performance_mode_ = new_state;
UpdateStates();
}
void RefreshRateController::UpdateStates() {
RefreshOverrideState();
RefreshVrrState();
}
void RefreshRateController::RefreshOverrideState() {
if (!base::FeatureList::IsEnabled(
ash::features::kSeamlessRefreshRateSwitching)) {
return;
}
// Don't attempt dynamic refresh rate adjustment with hardware mirroring
// enabled.
if (display::features::IsHardwareMirrorModeEnabled()) {
return;
}
RefreshRateOverrideMap refresh_rate_overrides = GetThrottleOverrides();
// Use preferred refresh rates instead of throttled refresh rates if present.
for (const auto& it : refresh_rate_preferences_) {
if (display_refresh_rates_.contains(it.first)) {
refresh_rate_overrides[it.first] = it.second;
}
}
display_configurator_->SetRefreshRateOverrides(refresh_rate_overrides);
}
RefreshRateOverrideMap RefreshRateController::GetThrottleOverrides() {
const ThrottleState throttle_state = GetDesiredThrottleState();
if (throttle_state == ThrottleState::kDisabled) {
return {};
}
// Update the override state for each display.
RefreshRateOverrideMap refresh_rate_overrides;
for (const auto& it : display_refresh_rates_) {
// Only throttle the internal display.
if (!display::IsInternalDisplayId(it.first)) {
continue;
}
// Filter out refresh rates lower than the minimum.
std::vector<float> throttle_candidates;
for (auto refresh_rate : it.second) {
if (refresh_rate >= kMinThrottledRefreshRate) {
throttle_candidates.push_back(refresh_rate);
}
}
if (throttle_candidates.size() < 2) {
VLOG(3) << "Fewer than 2 throttle candidates for display " << it.first;
continue;
}
refresh_rate_overrides[it.first] = throttle_candidates.front();
VLOG(3) << "Request refresh rate for display " << it.first << ": "
<< refresh_rate_overrides[it.first];
}
return refresh_rate_overrides;
}
void RefreshRateController::RefreshVrrState() {
// If VRR is always on, state will not need to be refreshed.
if (::features::IsVariableRefreshRateAlwaysOn()) {
return;
}
if (!::features::IsVariableRefreshRateEnabled()) {
return;
}
// Enable VRR on the borealis-hosting display if battery saver is inactive.
if (game_window_observer_.IsObserving() &&
current_performance_mode_ != ModeState::kPowerSaver) {
display_configurator_->SetVrrEnabled(
{display::Screen::GetScreen()
->GetDisplayNearestWindow(game_window_observer_.GetSource())
.id()});
} else {
display_configurator_->SetVrrEnabled({});
}
}
RefreshRateController::ThrottleState
RefreshRateController::GetDesiredThrottleState() {
if (force_throttle_) {
return ThrottleState::kEnabled;
}
switch (current_performance_mode_) {
case ModeState::kPowerSaver:
return ThrottleState::kEnabled;
case ModeState::kHighPerformance:
return ThrottleState::kDisabled;
case ModeState::kIntelligent:
return GetDynamicThrottleState();
default:
NOTREACHED();
}
}
RefreshRateController::ThrottleState
RefreshRateController::GetDynamicThrottleState() {
// Do not throttle when Borealis is active on the internal display.
if (game_window_observer_.IsObserving() &&
display::Screen::GetScreen()
->GetDisplayNearestWindow(game_window_observer_.GetSource())
.id() == display::Display::InternalDisplayId()) {
return ThrottleState::kDisabled;
}
if (power_status_->IsMainsChargerConnected()) {
return ThrottleState::kDisabled;
}
return ThrottleState::kEnabled;
}
void RefreshRateController::OnSetPreferredRefreshRate(
aura::WindowTreeHost* host,
float preferred_refresh_rate) {
CHECK(display::Screen::HasScreen());
const int64_t display_id = display::Screen::GetScreen()
->GetDisplayNearestWindow(host->window())
.id();
// Only honor preferences for the internal display.
if (!display::IsInternalDisplayId(display_id)) {
return;
}
// No change.
const auto& it = refresh_rate_preferences_.find(display_id);
if (it != refresh_rate_preferences_.end() &&
it->second == preferred_refresh_rate) {
return;
}
if (preferred_refresh_rate) {
refresh_rate_preferences_[display_id] = preferred_refresh_rate;
} else {
refresh_rate_preferences_.erase(display_id);
}
RefreshOverrideState();
}
void RefreshRateController::OnWindowTreeHostCreated(
aura::WindowTreeHost* host) {
host->AddObserver(this);
}
} // namespace ash
|