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