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
|
// 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/test/test_native_display_delegate.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "base/task/single_thread_task_runner.h"
#include "ui/display/manager/test/action_logger.h"
#include "ui/display/types/display_mode.h"
#include "ui/display/types/display_snapshot.h"
#include "ui/display/types/native_display_observer.h"
#include "ui/gfx/geometry/size.h"
namespace display::test {
std::string GetModesetFlag(display::ModesetFlags modeset_flags) {
std::string flags_str;
if (modeset_flags.Has(display::ModesetFlag::kTestModeset)) {
flags_str = base::StrCat({flags_str, kTestModesetStr, ","});
}
if (modeset_flags.Has(display::ModesetFlag::kCommitModeset)) {
flags_str = base::StrCat({flags_str, kCommitModesetStr, ","});
}
if (modeset_flags.Has(display::ModesetFlag::kSeamlessModeset)) {
flags_str = base::StrCat({flags_str, kSeamlessModesetStr, ","});
}
// Remove trailing comma.
if (!flags_str.empty())
flags_str.resize(flags_str.size() - 1);
return flags_str;
}
TestNativeDisplayDelegate::TestNativeDisplayDelegate(ActionLogger* log)
: max_configurable_pixels_(0),
get_hdcp_expectation_(true),
set_hdcp_expectation_(true),
hdcp_state_(HDCP_STATE_UNDESIRED),
content_protection_method_(CONTENT_PROTECTION_METHOD_NONE),
run_async_(false),
log_(log) {}
TestNativeDisplayDelegate::~TestNativeDisplayDelegate() = default;
const std::vector<raw_ptr<DisplaySnapshot, VectorExperimental>>
TestNativeDisplayDelegate::GetOutputs() const {
std::vector<raw_ptr<DisplaySnapshot, VectorExperimental>> outputs;
for (const auto& output : outputs_) {
outputs.push_back(output.get());
}
return outputs;
}
void TestNativeDisplayDelegate::SetOutputs(
std::vector<std::unique_ptr<DisplaySnapshot>> outputs) {
std::move(begin(outputs_), end(outputs_),
std::back_inserter(cached_outputs_));
outputs_ = std::move(outputs);
}
void TestNativeDisplayDelegate::Initialize() {
log_->AppendAction(kInit);
}
void TestNativeDisplayDelegate::TakeDisplayControl(
DisplayControlCallback callback) {
log_->AppendAction(kTakeDisplayControl);
std::move(callback).Run(true);
}
void TestNativeDisplayDelegate::RelinquishDisplayControl(
DisplayControlCallback callback) {
log_->AppendAction(kRelinquishDisplayControl);
std::move(callback).Run(true);
}
void TestNativeDisplayDelegate::GetDisplays(GetDisplaysCallback callback) {
// This mimics the behavior of Ozone DRM when new display state arrives.
observers_.Notify(&NativeDisplayObserver::OnDisplaySnapshotsInvalidated);
cached_outputs_.clear();
if (run_async_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), GetOutputs()));
} else {
std::move(callback).Run(GetOutputs());
}
}
bool TestNativeDisplayDelegate::Configure(
const display::DisplayConfigurationParams& display_config_params) {
log_->AppendAction(GetCrtcAction(display_config_params));
if (max_configurable_pixels_ == 0)
return true;
else if (max_configurable_pixels_ < 0)
return false;
if (display_config_params.mode) {
return display_config_params.mode->size().GetArea() <=
max_configurable_pixels_;
}
return true;
}
bool TestNativeDisplayDelegate::IsConfigurationWithinSystemBandwidth(
const std::vector<display::DisplayConfigurationParams>& config_requests) {
if (system_bandwidth_limit_ == 0)
return true;
// We need a copy of the current state to account for current configuration.
// But we can't overwrite it yet because we may fail to configure
base::flat_map<int64_t, int> requested_ids_with_bandwidth =
display_id_to_used_system_bw_;
for (const DisplayConfigurationParams& config : config_requests) {
requested_ids_with_bandwidth[config.id] =
config.mode ? config.mode->size().GetArea() : 0;
}
int requested_bandwidth = 0;
for (const auto& it : requested_ids_with_bandwidth) {
requested_bandwidth += it.second;
}
return requested_bandwidth <= system_bandwidth_limit_;
}
void TestNativeDisplayDelegate::SaveCurrentConfigSystemBandwidth(
const std::vector<display::DisplayConfigurationParams>& config_requests) {
// On a successful configuration, we update the current state to reflect the
// current system usage.
for (const DisplayConfigurationParams& config : config_requests) {
display_id_to_used_system_bw_[config.id] =
config.mode ? config.mode->size().GetArea() : 0;
}
}
void TestNativeDisplayDelegate::Configure(
const std::vector<display::DisplayConfigurationParams>& config_requests,
ConfigureCallback callback,
display::ModesetFlags modeset_flags) {
log_->AppendAction(GetModesetFlag(modeset_flags));
bool config_success = true;
for (const auto& config : config_requests)
config_success &= Configure(config);
config_success &= IsConfigurationWithinSystemBandwidth(config_requests);
if (config_success)
SaveCurrentConfigSystemBandwidth(config_requests);
std::string config_outcome = "outcome: ";
config_outcome += config_success ? "success" : "failure";
log_->AppendAction(config_outcome);
if (run_async_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), config_requests, config_success));
} else {
std::move(callback).Run(config_requests, config_success);
}
}
void TestNativeDisplayDelegate::SetHdcpKeyProp(
int64_t display_id,
const std::string& key,
SetHdcpKeyPropCallback callback) {
log_->AppendAction(GetSetHdcpKeyPropAction(display_id, true));
if (run_async_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), true));
} else {
std::move(callback).Run(true);
}
}
void TestNativeDisplayDelegate::GetHDCPState(const DisplaySnapshot& output,
GetHDCPStateCallback callback) {
if (run_async_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), get_hdcp_expectation_,
hdcp_state_, content_protection_method_));
} else {
std::move(callback).Run(get_hdcp_expectation_, hdcp_state_,
content_protection_method_);
}
}
void TestNativeDisplayDelegate::SetHDCPState(
const DisplaySnapshot& output,
HDCPState state,
ContentProtectionMethod protection_method,
SetHDCPStateCallback callback) {
if (run_async_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&TestNativeDisplayDelegate::DoSetHDCPState,
base::Unretained(this), output.display_id(), state,
protection_method, std::move(callback)));
} else {
DoSetHDCPState(output.display_id(), state, protection_method,
std::move(callback));
}
}
void TestNativeDisplayDelegate::DoSetHDCPState(
int64_t display_id,
HDCPState state,
ContentProtectionMethod protection_method,
SetHDCPStateCallback callback) {
log_->AppendAction(
GetSetHDCPStateAction(display_id, state, protection_method));
switch (state) {
case HDCP_STATE_ENABLED:
NOTREACHED();
case HDCP_STATE_DESIRED:
hdcp_state_ =
set_hdcp_expectation_ ? HDCP_STATE_ENABLED : HDCP_STATE_DESIRED;
break;
case HDCP_STATE_UNDESIRED:
if (set_hdcp_expectation_)
hdcp_state_ = HDCP_STATE_UNDESIRED;
break;
}
content_protection_method_ = set_hdcp_expectation_
? protection_method
: CONTENT_PROTECTION_METHOD_NONE;
std::move(callback).Run(set_hdcp_expectation_);
}
void TestNativeDisplayDelegate::SetColorCalibration(
int64_t display_id,
const ColorCalibration& calibration) {
log_->AppendAction(SetColorCalibrationAction(display_id, calibration));
}
void TestNativeDisplayDelegate::SetColorTemperatureAdjustment(
int64_t display_id,
const ColorTemperatureAdjustment& cta) {
log_->AppendAction(SetColorTemperatureAdjustmentAction(display_id, cta));
}
void TestNativeDisplayDelegate::SetGammaAdjustment(
int64_t display_id,
const GammaAdjustment& gamma) {
log_->AppendAction(SetGammaAdjustmentAction(display_id, gamma));
}
void TestNativeDisplayDelegate::SetPrivacyScreen(
int64_t display_id,
bool enabled,
SetPrivacyScreenCallback callback) {
log_->AppendAction(SetPrivacyScreenAction(display_id, enabled));
std::move(callback).Run(true);
}
void TestNativeDisplayDelegate::GetSeamlessRefreshRates(
int64_t display_id,
GetSeamlessRefreshRatesCallback callback) const {
const DisplaySnapshot* snapshot = nullptr;
for (const auto& output : outputs_) {
if (output->display_id() == display_id) {
snapshot = output.get();
break;
}
}
// Return nullopt if there is no snapshot with this display_id.
std::optional<std::vector<float>> result;
if (snapshot) {
// Return empty vector if there is no current mode.
std::vector<float> refresh_rates;
if (snapshot->current_mode()) {
for (auto& mode : snapshot->modes()) {
// If a mode has the same size as the currently configured mode, then
// include that mode's refresh rate.
if (mode->size() == snapshot->current_mode()->size()) {
refresh_rates.push_back(mode->refresh_rate());
}
}
}
result.emplace(refresh_rates);
}
if (run_async_) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), result));
} else {
std::move(callback).Run(result);
}
}
void TestNativeDisplayDelegate::AddObserver(NativeDisplayObserver* observer) {
observers_.AddObserver(observer);
}
void TestNativeDisplayDelegate::RemoveObserver(
NativeDisplayObserver* observer) {
observers_.RemoveObserver(observer);
}
FakeDisplayController* TestNativeDisplayDelegate::GetFakeDisplayController() {
return nullptr;
}
} // namespace display::test
|