Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Joone Hur | d3ae873 | 2018-04-17 18:05:09 | [diff] [blame] | 5 | #include "ui/display/manager/configure_displays_task.h" |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 6 | |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 7 | #include <cstddef> |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 8 | #include <string> |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 9 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 10 | #include "base/containers/flat_set.h" |
Avi Drissman | c149c16 | 2023-01-12 02:16:59 | [diff] [blame] | 11 | #include "base/functional/bind.h" |
Hans Wennborg | 3930cf3 | 2020-06-17 16:29:52 | [diff] [blame] | 12 | #include "base/logging.h" |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 13 | #include "base/metrics/histogram_functions.h" |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 15 | #include "base/metrics/sparse_histogram.h" |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 16 | #include "base/numerics/safe_conversions.h" |
Drew Davenport | 3e89c1b0 | 2024-05-09 22:12:08 | [diff] [blame^] | 17 | #include "base/strings/string_number_conversions.h" |
Gil Dekel | a6690b7 | 2023-05-02 18:59:04 | [diff] [blame] | 18 | #include "ui/display/manager/util/display_manager_util.h" |
Mark Yacoub | d18a292 | 2020-07-07 01:14:15 | [diff] [blame] | 19 | #include "ui/display/types/display_configuration_params.h" |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 20 | #include "ui/display/types/display_constants.h" |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 21 | #include "ui/display/types/display_mode.h" |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 22 | #include "ui/display/types/display_snapshot.h" |
| 23 | #include "ui/display/types/native_display_delegate.h" |
| 24 | |
kylechar | 7a067ec | 2017-01-07 01:16:28 | [diff] [blame] | 25 | namespace display { |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 29 | // The epsilon by which a refresh rate value may drift. For example: |
| 30 | // 239.76Hz --> 240Hz. This value was chosen with the consideration of the |
| 31 | // refresh rate value drifts presented in the "Video Formats—Video ID Code and |
| 32 | // Aspect Ratios" table on p.40 of the CTA-861-G standard. |
| 33 | constexpr float kRefreshRateEpsilon = 0.5f; |
| 34 | |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 35 | // Because we do not offer hardware mirroring, the maximal number of external |
| 36 | // displays that can be configured is limited by the number of available CRTCs, |
| 37 | // which is usually three. Since the lifetime of the UMA using this value is one |
| 38 | // year (exp. Nov. 2021), five buckets are more than enough for |
| 39 | // its histogram (between 0 to 4 external monitors). |
| 40 | constexpr int kMaxDisplaysCount = 5; |
| 41 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 42 | // Consolidates the UMA name prefix creation to one location, since it is used |
| 43 | // in many different call-sites. |
| 44 | const std::string GetUmaNamePrefixForRequest( |
| 45 | const DisplayConfigureRequest& request) { |
| 46 | return request.display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL |
| 47 | ? std::string("ConfigureDisplays.Internal.Modeset.") |
| 48 | : std::string("ConfigureDisplays.External.Modeset."); |
| 49 | } |
| 50 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 51 | // Find the next best mode that is smaller than |request->mode|. The next best |
| 52 | // mode is found by comparing resolutions, and if those are similar, comparing |
| 53 | // refresh rates. If no mode is found, return nullptr. |
| 54 | const DisplayMode* FindNextMode(const DisplayConfigureRequest& request) { |
| 55 | DCHECK(request.mode); |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 56 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 57 | // Internal displays are restricted to their native mode. We do not |
| 58 | // attempt to downgrade their modes upon failure. |
| 59 | if (request.display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL) |
dnicoara | a89c208 | 2015-01-05 16:49:06 | [diff] [blame] | 60 | return nullptr; |
| 61 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 62 | if (request.display->modes().size() <= 1) |
| 63 | return nullptr; |
| 64 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 65 | const DisplayMode* best_mode = nullptr; |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 66 | for (const auto& mode : request.display->modes()) { |
| 67 | if (*mode < *request.mode && (!best_mode || *mode > *best_mode)) |
dbasehore | 01e9004 | 2016-05-27 06:16:51 | [diff] [blame] | 68 | best_mode = mode.get(); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return best_mode; |
| 72 | } |
| 73 | |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 74 | void LogIfInvalidRequestForInternalDisplay( |
| 75 | const DisplayConfigureRequest& request) { |
| 76 | if (request.display->type() != DISPLAY_CONNECTION_TYPE_INTERNAL) |
| 77 | return; |
| 78 | |
| 79 | if (request.mode == nullptr) |
| 80 | return; |
| 81 | |
| 82 | if (request.mode == request.display->native_mode()) |
| 83 | return; |
| 84 | |
| 85 | LOG(ERROR) << "A mode other than the preferred mode was requested for the " |
| 86 | "internal display: preferred=" |
| 87 | << request.display->native_mode()->ToString() |
| 88 | << " vs. requested=" << request.mode->ToString() |
| 89 | << ". Current mode=" |
| 90 | << (request.display->current_mode() |
| 91 | ? request.display->current_mode()->ToString() |
| 92 | : "nullptr (disabled)") |
| 93 | << "."; |
| 94 | } |
| 95 | |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 96 | // Samples used to define buckets used by DisplayResolution enum. |
| 97 | // The enum is used to record screen resolution statistics. |
| 98 | const int32_t kDisplayResolutionSamples[] = {1024, 1280, 1440, 1920, |
| 99 | 2560, 3840, 5120, 7680}; |
| 100 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 101 | void UpdateResolutionUma(const DisplayConfigureRequest& request, |
| 102 | const std::string& uma_name) { |
| 103 | // Display is powered off. |
| 104 | if (!request.mode) |
| 105 | return; |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 106 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 107 | // First, compute the index of the enum DisplayResolution. |
| 108 | // The index has to match the definition of the enum in enums.xml. |
| 109 | const uint32_t samples_list_size = std::size(kDisplayResolutionSamples); |
| 110 | const gfx::Size size = request.mode->size(); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 111 | uint32_t width_idx = 0; |
| 112 | uint32_t height_idx = 0; |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 113 | for (; width_idx < samples_list_size; width_idx++) { |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 114 | if (size.width() <= kDisplayResolutionSamples[width_idx]) |
| 115 | break; |
| 116 | } |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 117 | for (; height_idx < samples_list_size; height_idx++) { |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 118 | if (size.height() <= kDisplayResolutionSamples[height_idx]) |
| 119 | break; |
| 120 | } |
| 121 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 122 | int display_resolution_index = 0; |
| 123 | if (width_idx == samples_list_size || height_idx == samples_list_size) { |
| 124 | // Check if we are in the overflow bucket. |
| 125 | display_resolution_index = samples_list_size * samples_list_size + 1; |
| 126 | } else { |
| 127 | // Compute the index of DisplayResolution, starting from 1, since 0 is used |
| 128 | // when powering off the display. |
| 129 | display_resolution_index = width_idx * samples_list_size + height_idx + 1; |
| 130 | } |
| 131 | |
| 132 | base::UmaHistogramExactLinear(uma_name, display_resolution_index, |
| 133 | samples_list_size * samples_list_size + 2); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 134 | } |
| 135 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 136 | // A list of common refresh rates that are used to help fit approximate refresh |
| 137 | // rate values into one of the common refresh rate bins. |
| 138 | constexpr float kCommonDisplayRefreshRates[] = { |
| 139 | 24.0, 25.0, 30.0, 45.0, 48.0, 50.0, 60.0, 75.0, |
| 140 | 90.0, 100.0, 120.0, 144.0, 165.0, 200.0, 240.0}; |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 141 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 142 | void UpdateRefreshRateUma(const DisplayConfigureRequest& request, |
| 143 | const std::string& uma_name) { |
| 144 | // Display is powered off. |
| 145 | if (!request.mode) |
| 146 | return; |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 147 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 148 | base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( |
| 149 | uma_name, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 150 | |
| 151 | // Check if the refresh value is within an epsilon from one of the common |
| 152 | // refresh rate values. |
Gil Dekel | a7ca9fc | 2022-05-16 19:25:37 | [diff] [blame] | 153 | for (float common_rate : kCommonDisplayRefreshRates) { |
| 154 | const bool is_within_epsilon = std::abs(request.mode->refresh_rate() - |
| 155 | common_rate) < kRefreshRateEpsilon; |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 156 | if (is_within_epsilon) { |
Gil Dekel | a7ca9fc | 2022-05-16 19:25:37 | [diff] [blame] | 157 | histogram->Add(common_rate); |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Since this is not a common refresh rate value, report it as is. |
| 163 | histogram->Add(request.mode->refresh_rate()); |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 164 | } |
| 165 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 166 | void UpdateAttemptSucceededUma( |
| 167 | const std::vector<DisplayConfigureRequest>& requests, |
| 168 | bool display_success) { |
| 169 | for (const auto& request : requests) { |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 170 | const std::string uma_name_prefix = GetUmaNamePrefixForRequest(request); |
| 171 | base::UmaHistogramBoolean(uma_name_prefix + "AttemptSucceeded", |
| 172 | display_success); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 173 | |
| 174 | VLOG(2) << "Configured status=" << display_success |
| 175 | << " display=" << request.display->display_id() |
| 176 | << " origin=" << request.origin.ToString() |
Andrew Wolfers | 4eb4c30 | 2023-02-14 21:37:33 | [diff] [blame] | 177 | << " mode=" << (request.mode ? request.mode->ToString() : "null") |
| 178 | << " enable_vrr=" << request.enable_vrr; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 179 | } |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 180 | } |
| 181 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 182 | void UpdateFinalStatusUma( |
Gil Dekel | 81fc8d5 | 2023-07-26 19:40:47 | [diff] [blame] | 183 | const std::vector<RequestAndStatusList>& requests_and_statuses, |
| 184 | ConfigureDisplaysTask::Status status) { |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 185 | int mst_external_displays = 0; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 186 | size_t total_external_displays = requests_and_statuses.size(); |
| 187 | for (auto& request_and_status : requests_and_statuses) { |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 188 | const DisplayConfigureRequest* request = request_and_status.first; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 189 | |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 190 | // Is this display SST (single-stream vs. MST multi-stream). |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 191 | const bool sst_display = request->display->base_connector_id() && |
| 192 | request->display->path_topology().empty(); |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 193 | if (!sst_display) |
| 194 | mst_external_displays++; |
| 195 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 196 | if (request->display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL) |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 197 | total_external_displays--; |
| 198 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 199 | const std::string uma_name_prefix = GetUmaNamePrefixForRequest(*request); |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 200 | if (request_and_status.second) { |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 201 | UpdateResolutionUma(*request, uma_name_prefix + "Success.Resolution"); |
| 202 | UpdateRefreshRateUma(*request, uma_name_prefix + "Success.RefreshRate"); |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 203 | } |
| 204 | base::UmaHistogramBoolean(uma_name_prefix + "FinalStatus", |
| 205 | request_and_status.second); |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 206 | } |
| 207 | |
Gil Dekel | 81fc8d5 | 2023-07-26 19:40:47 | [diff] [blame] | 208 | base::UmaHistogramEnumeration("ConfigureDisplays.Modeset.FinalTaskStatus", |
| 209 | status); |
| 210 | |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 211 | base::UmaHistogramExactLinear( |
| 212 | "ConfigureDisplays.Modeset.TotalExternalDisplaysCount", |
| 213 | base::checked_cast<int>(total_external_displays), kMaxDisplaysCount); |
| 214 | |
| 215 | base::UmaHistogramExactLinear( |
| 216 | "ConfigureDisplays.Modeset.MstExternalDisplaysCount", |
| 217 | mst_external_displays, kMaxDisplaysCount); |
| 218 | |
| 219 | if (total_external_displays > 0) { |
| 220 | const int mst_displays_percentage = |
| 221 | 100.0 * mst_external_displays / total_external_displays; |
| 222 | UMA_HISTOGRAM_PERCENTAGE( |
| 223 | "ConfigureDisplays.Modeset.MstExternalDisplaysPercentage", |
| 224 | mst_displays_percentage); |
| 225 | } |
| 226 | } |
| 227 | |
Drew Davenport | 3e89c1b0 | 2024-05-09 22:12:08 | [diff] [blame^] | 228 | // After a successful configuration, the DisplaySnapshot associated with a |
| 229 | // request needs to have its state updated to reflect the new configuration. |
| 230 | void UpdateSnapshotAfterConfiguration(const DisplayConfigureRequest& request) { |
| 231 | request.display->set_current_mode(request.mode); |
| 232 | request.display->set_origin(request.origin); |
| 233 | if (request.display->IsVrrCapable()) { |
| 234 | request.display->set_variable_refresh_rate_state( |
| 235 | request.enable_vrr ? display::kVrrEnabled : display::kVrrDisabled); |
| 236 | } |
| 237 | } |
| 238 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 239 | } // namespace |
| 240 | |
| 241 | DisplayConfigureRequest::DisplayConfigureRequest(DisplaySnapshot* display, |
| 242 | const DisplayMode* mode, |
Andrew Wolfers | 4eb4c30 | 2023-02-14 21:37:33 | [diff] [blame] | 243 | const gfx::Point& origin, |
| 244 | bool enable_vrr) |
| 245 | : display(display), mode(mode), origin(origin), enable_vrr(enable_vrr) {} |
| 246 | |
| 247 | DisplayConfigureRequest::DisplayConfigureRequest(DisplaySnapshot* display, |
| 248 | const DisplayMode* mode, |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 249 | const gfx::Point& origin) |
Drew Davenport | 3e89c1b0 | 2024-05-09 22:12:08 | [diff] [blame^] | 250 | : DisplayConfigureRequest(display, |
| 251 | mode, |
| 252 | origin, |
| 253 | /*enable_vrr=*/false) {} |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 254 | |
| 255 | ConfigureDisplaysTask::ConfigureDisplaysTask( |
| 256 | NativeDisplayDelegate* delegate, |
| 257 | const std::vector<DisplayConfigureRequest>& requests, |
Drew Davenport | 7636a46 | 2022-07-21 20:14:30 | [diff] [blame] | 258 | ResponseCallback callback, |
| 259 | ConfigurationType configuration_type) |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 260 | : delegate_(delegate), |
| 261 | requests_(requests), |
Drew Davenport | 7636a46 | 2022-07-21 20:14:30 | [diff] [blame] | 262 | configuration_type_(configuration_type), |
Sylvain Defresne | 23395c7a | 2019-10-02 10:07:45 | [diff] [blame] | 263 | callback_(std::move(callback)), |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 264 | task_status_(SUCCESS) { |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 265 | delegate_->AddObserver(this); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 266 | } |
| 267 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 268 | ConfigureDisplaysTask::RequestToOriginalMode::RequestToOriginalMode( |
| 269 | DisplayConfigureRequest* request, |
| 270 | const DisplayMode* original_mode) |
| 271 | : request(request), original_mode(original_mode) {} |
| 272 | |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 273 | ConfigureDisplaysTask::~ConfigureDisplaysTask() { |
| 274 | delegate_->RemoveObserver(this); |
| 275 | } |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 276 | |
| 277 | void ConfigureDisplaysTask::Run() { |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 278 | DCHECK(!requests_.empty()); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 279 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 280 | const bool is_first_attempt = pending_display_group_requests_.empty(); |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 281 | std::vector<display::DisplayConfigurationParams> config_requests; |
| 282 | for (const auto& request : requests_) { |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 283 | LogIfInvalidRequestForInternalDisplay(request); |
| 284 | |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 285 | config_requests.emplace_back(request.display->display_id(), request.origin, |
Andrew Wolfers | 4eb4c30 | 2023-02-14 21:37:33 | [diff] [blame] | 286 | request.mode, request.enable_vrr); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 287 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 288 | if (is_first_attempt) { |
| 289 | const std::string uma_name_prefix = GetUmaNamePrefixForRequest(request); |
| 290 | UpdateResolutionUma(request, |
| 291 | uma_name_prefix + "OriginalRequest.Resolution"); |
| 292 | } |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 293 | } |
| 294 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 295 | const auto& on_configured = |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame] | 296 | is_first_attempt ? &ConfigureDisplaysTask::OnFirstAttemptConfigured |
| 297 | : &ConfigureDisplaysTask::OnRetryConfigured; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 298 | |
Drew Davenport | 5686c95 | 2024-03-08 16:47:50 | [diff] [blame] | 299 | display::ModesetFlags modeset_flags{display::ModesetFlag::kTestModeset}; |
Drew Davenport | 7636a46 | 2022-07-21 20:14:30 | [diff] [blame] | 300 | if (configuration_type_ == kConfigurationTypeSeamless) |
Drew Davenport | 5686c95 | 2024-03-08 16:47:50 | [diff] [blame] | 301 | modeset_flags.Put(display::ModesetFlag::kSeamlessModeset); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 302 | delegate_->Configure( |
| 303 | config_requests, |
Gil Dekel | cd4c2c0 | 2022-07-19 18:01:37 | [diff] [blame] | 304 | base::BindOnce(on_configured, weak_ptr_factory_.GetWeakPtr()), |
Drew Davenport | 7636a46 | 2022-07-21 20:14:30 | [diff] [blame] | 305 | modeset_flags); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 306 | } |
| 307 | |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 308 | void ConfigureDisplaysTask::OnConfigurationChanged() {} |
| 309 | |
| 310 | void ConfigureDisplaysTask::OnDisplaySnapshotsInvalidated() { |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 311 | // From now on, don't access |requests_[index]->display|; they're invalid. |
| 312 | task_status_ = ERROR; |
| 313 | weak_ptr_factory_.InvalidateWeakPtrs(); |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 314 | std::move(callback_).Run(task_status_); |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 315 | } |
| 316 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 317 | void ConfigureDisplaysTask::OnFirstAttemptConfigured(bool config_success) { |
| 318 | UpdateAttemptSucceededUma(requests_, config_success); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 319 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 320 | if (!config_success) { |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 321 | // Partition |requests_| into smaller groups via |
| 322 | // |pending_display_group_requests_|, update the task's state, and initiate |
| 323 | // the retry logic. The next time |delegate_|->Configure() terminates |
| 324 | // OnRetryConfigured() will be executed instead. |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 325 | PartitionRequests(); |
| 326 | DCHECK(!pending_display_group_requests_.empty()); |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 327 | // Prep the first group |
| 328 | for (const auto& pair : pending_display_group_requests_.front()) |
| 329 | pair.request->mode = pair.original_mode; |
Gil Dekel | 42f7aba | 2021-01-08 18:59:55 | [diff] [blame] | 330 | task_status_ = PARTIAL_SUCCESS; |
| 331 | Run(); |
| 332 | return; |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 333 | } |
| 334 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 335 | // This code execute only when the first modeset attempt fully succeeds. |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 336 | // Submit the current |requests_| for modeset. |
| 337 | std::vector<display::DisplayConfigurationParams> config_requests; |
| 338 | for (const auto& request : requests_) { |
| 339 | final_requests_status_.emplace_back(&request, true); |
| 340 | |
| 341 | config_requests.emplace_back(request.display->display_id(), request.origin, |
Andrew Wolfers | 4eb4c30 | 2023-02-14 21:37:33 | [diff] [blame] | 342 | request.mode, request.enable_vrr); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 343 | } |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 344 | |
Drew Davenport | 5686c95 | 2024-03-08 16:47:50 | [diff] [blame] | 345 | display::ModesetFlags modeset_flags{display::ModesetFlag::kCommitModeset}; |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 346 | if (configuration_type_ == kConfigurationTypeSeamless) |
Drew Davenport | 5686c95 | 2024-03-08 16:47:50 | [diff] [blame] | 347 | modeset_flags.Put(display::ModesetFlag::kSeamlessModeset); |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 348 | delegate_->Configure(config_requests, |
| 349 | base::BindOnce(&ConfigureDisplaysTask::OnConfigured, |
| 350 | weak_ptr_factory_.GetWeakPtr()), |
| 351 | modeset_flags); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 352 | } |
| 353 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 354 | void ConfigureDisplaysTask::OnRetryConfigured(bool config_success) { |
| 355 | UpdateAttemptSucceededUma(requests_, config_success); |
| 356 | |
| 357 | if (!config_success) { |
| 358 | // If one of the largest display request can be downgraded, try again. |
| 359 | // Otherwise this configuration task is a failure. |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 360 | if (DowngradeDisplayRequestGroup()) { |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 361 | Run(); |
| 362 | return; |
| 363 | } else { |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 364 | // Disable all displays in the current group, since we failed to find an |
| 365 | // alternative mode. Note that we skip modeset if the latest (or a |
| 366 | // single) pending group fails. There is no point in disabling displays |
| 367 | // that are already disabled from previous attempts and failed to change |
| 368 | // mode. |
| 369 | for (const auto& pair : pending_display_group_requests_.front()) |
| 370 | pair.request->mode = nullptr; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 371 | task_status_ = ERROR; |
| 372 | } |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 373 | } else { |
| 374 | // This configuration attempt passed test-modeset. Cache it so we can use it |
| 375 | // to modeset the displays once we are done testing, or if no other future |
| 376 | // attempts succeed. |
| 377 | last_successful_config_parameters_.clear(); |
| 378 | for (const auto& request : requests_) { |
| 379 | last_successful_config_parameters_.emplace_back( |
Andrew Wolfers | 4eb4c30 | 2023-02-14 21:37:33 | [diff] [blame] | 380 | request.display->display_id(), request.origin, request.mode, |
| 381 | request.enable_vrr); |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 382 | } |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // This code executes only when this display group request fully succeeds or |
| 386 | // fails to modeset. Update the final status of this group. |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 387 | for (const auto& pair : pending_display_group_requests_.front()) |
| 388 | final_requests_status_.emplace_back(pair.request, config_success); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 389 | |
| 390 | // Subsequent modeset attempts will be done on the next pending display group, |
| 391 | // if one exists. |
| 392 | pending_display_group_requests_.pop(); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 393 | if (!pending_display_group_requests_.empty()) { |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 394 | // Prep the next group |
| 395 | for (const auto& pair : pending_display_group_requests_.front()) |
| 396 | pair.request->mode = pair.original_mode; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 397 | Run(); |
| 398 | return; |
| 399 | } |
| 400 | |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 401 | if (task_status_ == ERROR) { |
| 402 | LOG(WARNING) << "One or more of the connected display groups failed to " |
| 403 | "pass test-modeset entirely and will be disabled."; |
| 404 | |
| 405 | if (last_successful_config_parameters_.empty()) { |
| 406 | LOG(ERROR) << "Display configuration failed. No modeset was attempted."; |
| 407 | |
Gil Dekel | 81fc8d5 | 2023-07-26 19:40:47 | [diff] [blame] | 408 | UpdateFinalStatusUma(final_requests_status_, task_status_); |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 409 | std::move(callback_).Run(task_status_); |
| 410 | return; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Configure the displays using the last successful configuration parameter |
| 415 | // list. |
Drew Davenport | 5686c95 | 2024-03-08 16:47:50 | [diff] [blame] | 416 | display::ModesetFlags modeset_flags{display::ModesetFlag::kCommitModeset}; |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 417 | if (configuration_type_ == kConfigurationTypeSeamless) |
Drew Davenport | 5686c95 | 2024-03-08 16:47:50 | [diff] [blame] | 418 | modeset_flags.Put(display::ModesetFlag::kSeamlessModeset); |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 419 | delegate_->Configure(last_successful_config_parameters_, |
| 420 | base::BindOnce(&ConfigureDisplaysTask::OnConfigured, |
| 421 | weak_ptr_factory_.GetWeakPtr()), |
| 422 | modeset_flags); |
| 423 | } |
| 424 | |
| 425 | void ConfigureDisplaysTask::OnConfigured(bool config_success) { |
| 426 | if (config_success) { |
| 427 | for (const DisplayConfigureRequest& request : requests_) { |
Drew Davenport | 3e89c1b0 | 2024-05-09 22:12:08 | [diff] [blame^] | 428 | UpdateSnapshotAfterConfiguration(request); |
Gil Dekel | 8f7fa018 | 2022-07-22 17:45:14 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
Gil Dekel | 81fc8d5 | 2023-07-26 19:40:47 | [diff] [blame] | 432 | UpdateFinalStatusUma(final_requests_status_, task_status_); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 433 | std::move(callback_).Run(task_status_); |
| 434 | } |
| 435 | |
| 436 | void ConfigureDisplaysTask::PartitionRequests() { |
| 437 | pending_display_group_requests_ = PartitionedRequestsQueue(); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 438 | |
Mark Yacoub | 100f2df | 2022-04-19 18:01:14 | [diff] [blame] | 439 | base::flat_set<uint64_t> handled_connectors; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 440 | for (size_t i = 0; i < requests_.size(); ++i) { |
| 441 | uint64_t connector_id = requests_[i].display->base_connector_id(); |
| 442 | if (handled_connectors.find(connector_id) != handled_connectors.end()) |
| 443 | continue; |
| 444 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 445 | std::vector<ConfigureDisplaysTask::RequestToOriginalMode> request_group; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 446 | for (size_t j = i; j < requests_.size(); ++j) { |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 447 | if (connector_id == requests_[j].display->base_connector_id()) { |
| 448 | // Disable all requests in preparation increment connector retries after |
| 449 | // mapping them to their original request. |
| 450 | request_group.emplace_back(&requests_[j], requests_[j].mode); |
| 451 | requests_[j].mode = nullptr; |
| 452 | } |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 453 | } |
| 454 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 455 | handled_connectors.insert(connector_id); |
Mark Yacoub | 100f2df | 2022-04-19 18:01:14 | [diff] [blame] | 456 | pending_display_group_requests_.push(request_group); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 460 | bool ConfigureDisplaysTask::DowngradeDisplayRequestGroup() { |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 461 | auto cmp = [](DisplayConfigureRequest* lhs, DisplayConfigureRequest* rhs) { |
| 462 | return *lhs->mode < *rhs->mode; |
| 463 | }; |
| 464 | std::priority_queue<DisplayConfigureRequest*, |
| 465 | std::vector<DisplayConfigureRequest*>, decltype(cmp)> |
| 466 | sorted_requests(cmp); |
| 467 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 468 | for (const auto& pair : pending_display_group_requests_.front()) { |
| 469 | if (pair.request->display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL) |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 470 | continue; |
| 471 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 472 | if (!pair.request->mode) |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 473 | continue; |
| 474 | |
Gil Dekel | 472925e | 2022-05-16 18:55:27 | [diff] [blame] | 475 | sorted_requests.push(pair.request); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // Fail if there are no viable candidates to downgrade |
| 479 | if (sorted_requests.empty()) |
| 480 | return false; |
| 481 | |
| 482 | while (!sorted_requests.empty()) { |
| 483 | DisplayConfigureRequest* next_request = sorted_requests.top(); |
| 484 | sorted_requests.pop(); |
| 485 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 486 | const DisplayMode* next_mode = FindNextMode(*next_request); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 487 | if (next_mode) { |
| 488 | next_request->mode = next_mode; |
| 489 | return true; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
kylechar | 7a067ec | 2017-01-07 01:16:28 | [diff] [blame] | 496 | } // namespace display |