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