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
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/display/manager/update_display_configuration_task.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "display_configurator.h"
#include "ui/display/manager/configure_displays_task.h"
#include "ui/display/manager/display_layout_manager.h"
#include "ui/display/manager/util/display_manager_util.h"
#include "ui/display/types/display_snapshot.h"
#include "ui/display/types/native_display_delegate.h"
namespace display {
namespace {
// Move all internal panel displays to the front of the display list. Otherwise,
// the list remains in order.
void MoveInternalDisplaysToTheFront(
std::vector<raw_ptr<DisplaySnapshot, VectorExperimental>>& displays) {
DisplayConfigurator::DisplayStateList sorted_displays;
// First pass for internal panels.
for (DisplaySnapshot* display : displays) {
if (display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL)
sorted_displays.push_back(display);
}
// Second pass for the rest.
for (DisplaySnapshot* display : displays) {
if (display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL)
continue;
sorted_displays.push_back(display);
}
displays.swap(sorted_displays);
}
bool ResolveOverrides(
const DisplayConfigurator::RefreshRateOverrideMap& refresh_rate_overrides,
std::vector<DisplayConfigureRequest>& requests) {
for (auto& request : requests) {
if (!request.mode) {
continue;
}
auto override_it =
refresh_rate_overrides.find(request.display->display_id());
if (override_it == refresh_rate_overrides.end()) {
continue;
}
request.mode = std::make_unique<const DisplayMode>(
request.mode->size(), request.mode->is_interlaced(),
override_it->second, /*vsync_rate_min=*/std::nullopt);
}
return true;
}
} // namespace
UpdateDisplayConfigurationTask::UpdateDisplayConfigurationTask(
NativeDisplayDelegate* delegate,
DisplayLayoutManager* layout_manager,
MultipleDisplayState new_display_state,
chromeos::DisplayPowerState new_power_state,
int power_flags,
const base::flat_set<int64_t>& new_vrr_state,
const DisplayConfigurator::RefreshRateOverrideMap& refresh_rate_overrides,
bool force_configure,
ConfigurationType configuration_type,
ResponseCallback callback)
: delegate_(delegate),
layout_manager_(layout_manager),
new_display_state_(new_display_state),
new_power_state_(new_power_state),
power_flags_(power_flags),
new_vrr_state_(new_vrr_state),
refresh_rate_overrides_(refresh_rate_overrides),
force_configure_(force_configure),
configuration_type_(configuration_type),
callback_(std::move(callback)),
requesting_displays_(false) {
delegate_->AddObserver(this);
}
UpdateDisplayConfigurationTask::~UpdateDisplayConfigurationTask() {
delegate_->RemoveObserver(this);
}
void UpdateDisplayConfigurationTask::Run() {
requesting_displays_ = true;
delegate_->GetDisplays(
base::BindOnce(&UpdateDisplayConfigurationTask::OnDisplaysUpdated,
weak_ptr_factory_.GetWeakPtr()));
}
void UpdateDisplayConfigurationTask::OnConfigurationChanged() {}
void UpdateDisplayConfigurationTask::OnDisplaySnapshotsInvalidated() {
cached_displays_.clear();
if (!requesting_displays_ && weak_ptr_factory_.HasWeakPtrs()) {
// This task has already been run and getting the displays request is not in
// flight. We need to re-run it to get updated displays snapshots.
weak_ptr_factory_.InvalidateWeakPtrs();
Run();
}
}
void UpdateDisplayConfigurationTask::OnDisplaysUpdated(
const std::vector<raw_ptr<DisplaySnapshot, VectorExperimental>>& displays) {
cached_displays_ = displays;
MoveInternalDisplaysToTheFront(cached_displays_);
requesting_displays_ = false;
// If the user hasn't requested a display state, update it using the requested
// power state.
if (new_display_state_ == MULTIPLE_DISPLAY_STATE_INVALID)
new_display_state_ = ChooseDisplayState();
VLOG(1) << "OnDisplaysUpdated: new_display_state="
<< MultipleDisplayStateToString(new_display_state_)
<< " new_power_state=" << DisplayPowerStateToString(new_power_state_)
<< " flags=" << power_flags_
<< " new_vrr_state=" << VrrStateToString(new_vrr_state_)
<< " refresh_rate_overrides="
<< RefreshRateOverrideToString(refresh_rate_overrides_)
<< " force_configure=" << force_configure_
<< " display_count=" << cached_displays_.size();
if (ShouldConfigure()) {
EnterState(base::BindOnce(&UpdateDisplayConfigurationTask::OnStateEntered,
weak_ptr_factory_.GetWeakPtr()));
} else {
// If we don't have to configure then we're sticking with the old
// configuration. Update it such that it reflects in the reported value.
new_power_state_ = layout_manager_->GetPowerState();
FinishConfiguration(true);
}
}
void UpdateDisplayConfigurationTask::EnterState(
ConfigureDisplaysTask::ResponseCallback callback) {
VLOG(2) << "EnterState";
std::vector<DisplayConfigureRequest> requests;
if (!layout_manager_->GetDisplayLayout(cached_displays_, new_display_state_,
new_power_state_, new_vrr_state_,
&requests)) {
std::move(callback).Run(ConfigureDisplaysTask::ERROR);
return;
}
if (!ResolveOverrides(refresh_rate_overrides_, requests)) {
std::move(callback).Run(ConfigureDisplaysTask::ERROR);
return;
}
if (!requests.empty()) {
configure_task_ = std::make_unique<ConfigureDisplaysTask>(
delegate_, requests, std::move(callback), configuration_type_);
configure_task_->Run();
} else {
VLOG(2) << "No displays";
std::move(callback).Run(ConfigureDisplaysTask::SUCCESS);
}
}
void UpdateDisplayConfigurationTask::OnStateEntered(
ConfigureDisplaysTask::Status status) {
bool success = status != ConfigureDisplaysTask::ERROR;
if (new_display_state_ == MULTIPLE_DISPLAY_STATE_MULTI_MIRROR &&
status == ConfigureDisplaysTask::PARTIAL_SUCCESS)
success = false;
if (layout_manager_->GetSoftwareMirroringController()) {
bool enable_software_mirroring = false;
if (!success && new_display_state_ == MULTIPLE_DISPLAY_STATE_MULTI_MIRROR) {
if (layout_manager_->GetDisplayState() !=
MULTIPLE_DISPLAY_STATE_MULTI_EXTENDED ||
layout_manager_->GetPowerState() != new_power_state_ ||
force_configure_) {
new_display_state_ = MULTIPLE_DISPLAY_STATE_MULTI_EXTENDED;
EnterState(base::BindOnce(
&UpdateDisplayConfigurationTask::OnEnableSoftwareMirroring,
weak_ptr_factory_.GetWeakPtr()));
return;
}
success = layout_manager_->GetDisplayState() ==
MULTIPLE_DISPLAY_STATE_MULTI_EXTENDED;
enable_software_mirroring = success;
if (success)
new_display_state_ = MULTIPLE_DISPLAY_STATE_MULTI_EXTENDED;
}
layout_manager_->GetSoftwareMirroringController()->SetSoftwareMirroring(
enable_software_mirroring);
}
FinishConfiguration(success);
}
void UpdateDisplayConfigurationTask::OnEnableSoftwareMirroring(
ConfigureDisplaysTask::Status status) {
bool success = status != ConfigureDisplaysTask::ERROR;
layout_manager_->GetSoftwareMirroringController()->SetSoftwareMirroring(
success);
FinishConfiguration(success);
}
void UpdateDisplayConfigurationTask::FinishConfiguration(bool success) {
base::UmaHistogramBoolean(
"DisplayManager.UpdateDisplayConfigurationTask.Success", success);
std::move(callback_).Run(success, cached_displays_,
cached_unassociated_displays_, new_display_state_,
new_power_state_);
}
bool UpdateDisplayConfigurationTask::ShouldForceDpms() const {
return new_power_state_ != chromeos::DISPLAY_POWER_ALL_OFF &&
(layout_manager_->GetPowerState() != new_power_state_ ||
(power_flags_ & DisplayConfigurator::kSetDisplayPowerForceProbe));
}
bool UpdateDisplayConfigurationTask::ShouldConfigure() const {
if (force_configure_)
return true;
if (cached_displays_.size() == 1 &&
cached_displays_[0]->type() == DISPLAY_CONNECTION_TYPE_INTERNAL)
return true;
if (!(power_flags_ &
DisplayConfigurator::kSetDisplayPowerOnlyIfSingleInternalDisplay))
return true;
if (new_display_state_ != layout_manager_->GetDisplayState())
return true;
// Compare refresh rate overrides with current states.
if (ShouldConfigureRefreshRate()) {
return true;
}
if (ShouldConfigureVrr()) {
return true;
}
return false;
}
MultipleDisplayState UpdateDisplayConfigurationTask::ChooseDisplayState()
const {
int num_displays = cached_displays_.size();
int num_on_displays =
GetDisplayPower(cached_displays_, new_power_state_, nullptr);
if (num_displays == 0)
return MULTIPLE_DISPLAY_STATE_HEADLESS;
if (num_displays == 1 || num_on_displays == 1) {
// If only one display is currently turned on, return the "single" state
// so that its native mode will be used.
return MULTIPLE_DISPLAY_STATE_SINGLE;
}
// Try to use the saved configuration; otherwise, default to extended.
DisplayConfigurator::StateController* state_controller =
layout_manager_->GetStateController();
if (!state_controller)
return MULTIPLE_DISPLAY_STATE_MULTI_EXTENDED;
return state_controller->GetStateForDisplayIds(cached_displays_);
}
bool UpdateDisplayConfigurationTask::ShouldConfigureVrr() const {
for (const DisplaySnapshot* display : cached_displays_) {
if (!display->IsVrrCapable()) {
continue;
}
if (new_vrr_state_.contains(display->display_id()) !=
display->IsVrrEnabled()) {
return true;
}
}
return false;
}
bool UpdateDisplayConfigurationTask::ShouldConfigureRefreshRate() const {
for (const DisplaySnapshot* display : cached_displays_) {
// TODO b/334104991: Refresh rate override is only enabled for internal
// displays.
if (display->type() != DISPLAY_CONNECTION_TYPE_INTERNAL) {
continue;
}
// No mode means display isn't turned on. Refresh rate override should
// not affect whether a display is enabled.
if (!display->current_mode() || !display->native_mode()) {
continue;
}
// Target refresh rate is the native mode's refresh rate, unless an override
// is specified.
float target_refresh_rate = display->native_mode()->refresh_rate();
auto it = refresh_rate_overrides_.find(display->display_id());
if (it != refresh_rate_overrides_.end()) {
target_refresh_rate = it->second;
}
// If the target refresh rate doesn't match the current refresh rate, then
// a configuration is needed.
if (display->current_mode()->refresh_rate() != target_refresh_rate) {
return true;
}
}
// Checked all displays, and none of them require a refresh rate override.
return false;
}
} // namespace display
|