dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 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 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 10 | #include "base/auto_reset.h" |
| 11 | #include "base/bind.h" |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 12 | #include "base/containers/flat_set.h" |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 13 | #include "base/containers/queue.h" |
Hans Wennborg | 3930cf3 | 2020-06-17 16:29:52 | [diff] [blame] | 14 | #include "base/logging.h" |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 15 | #include "base/metrics/histogram_functions.h" |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 16 | #include "base/metrics/histogram_macros.h" |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 17 | #include "base/metrics/sparse_histogram.h" |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 18 | #include "base/numerics/safe_conversions.h" |
Mitsuru Oshima | 96ff454 | 2022-05-02 17:31:32 | [diff] [blame] | 19 | #include "ui/display/manager/display_manager_util.h" |
Mark Yacoub | d18a292 | 2020-07-07 01:14:15 | [diff] [blame] | 20 | #include "ui/display/types/display_configuration_params.h" |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 21 | #include "ui/display/types/display_constants.h" |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 22 | #include "ui/display/types/display_mode.h" |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 23 | #include "ui/display/types/display_snapshot.h" |
| 24 | #include "ui/display/types/native_display_delegate.h" |
| 25 | |
kylechar | 7a067ec | 2017-01-07 01:16:28 | [diff] [blame] | 26 | namespace display { |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 30 | // The epsilon by which a refresh rate value may drift. For example: |
| 31 | // 239.76Hz --> 240Hz. This value was chosen with the consideration of the |
| 32 | // refresh rate value drifts presented in the "Video Formats—Video ID Code and |
| 33 | // Aspect Ratios" table on p.40 of the CTA-861-G standard. |
| 34 | constexpr float kRefreshRateEpsilon = 0.5f; |
| 35 | |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 36 | // Because we do not offer hardware mirroring, the maximal number of external |
| 37 | // displays that can be configured is limited by the number of available CRTCs, |
| 38 | // which is usually three. Since the lifetime of the UMA using this value is one |
| 39 | // year (exp. Nov. 2021), five buckets are more than enough for |
| 40 | // its histogram (between 0 to 4 external monitors). |
| 41 | constexpr int kMaxDisplaysCount = 5; |
| 42 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 43 | // Consolidates the UMA name prefix creation to one location, since it is used |
| 44 | // in many different call-sites. |
| 45 | const std::string GetUmaNamePrefixForRequest( |
| 46 | const DisplayConfigureRequest& request) { |
| 47 | return request.display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL |
| 48 | ? std::string("ConfigureDisplays.Internal.Modeset.") |
| 49 | : std::string("ConfigureDisplays.External.Modeset."); |
| 50 | } |
| 51 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 52 | // Find the next best mode that is smaller than |request->mode|. The next best |
| 53 | // mode is found by comparing resolutions, and if those are similar, comparing |
| 54 | // refresh rates. If no mode is found, return nullptr. |
| 55 | const DisplayMode* FindNextMode(const DisplayConfigureRequest& request) { |
| 56 | DCHECK(request.mode); |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 57 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 58 | // Internal displays are restricted to their native mode. We do not |
| 59 | // attempt to downgrade their modes upon failure. |
| 60 | if (request.display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL) |
dnicoara | a89c208 | 2015-01-05 16:49:06 | [diff] [blame] | 61 | return nullptr; |
| 62 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 63 | if (request.display->modes().size() <= 1) |
| 64 | return nullptr; |
| 65 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 66 | const DisplayMode* best_mode = nullptr; |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 67 | for (const auto& mode : request.display->modes()) { |
| 68 | if (*mode < *request.mode && (!best_mode || *mode > *best_mode)) |
dbasehore | 01e9004 | 2016-05-27 06:16:51 | [diff] [blame] | 69 | best_mode = mode.get(); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return best_mode; |
| 73 | } |
| 74 | |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 75 | void LogIfInvalidRequestForInternalDisplay( |
| 76 | const DisplayConfigureRequest& request) { |
| 77 | if (request.display->type() != DISPLAY_CONNECTION_TYPE_INTERNAL) |
| 78 | return; |
| 79 | |
| 80 | if (request.mode == nullptr) |
| 81 | return; |
| 82 | |
| 83 | if (request.mode == request.display->native_mode()) |
| 84 | return; |
| 85 | |
| 86 | LOG(ERROR) << "A mode other than the preferred mode was requested for the " |
| 87 | "internal display: preferred=" |
| 88 | << request.display->native_mode()->ToString() |
| 89 | << " vs. requested=" << request.mode->ToString() |
| 90 | << ". Current mode=" |
| 91 | << (request.display->current_mode() |
| 92 | ? request.display->current_mode()->ToString() |
| 93 | : "nullptr (disabled)") |
| 94 | << "."; |
| 95 | } |
| 96 | |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 97 | // Samples used to define buckets used by DisplayResolution enum. |
| 98 | // The enum is used to record screen resolution statistics. |
| 99 | const int32_t kDisplayResolutionSamples[] = {1024, 1280, 1440, 1920, |
| 100 | 2560, 3840, 5120, 7680}; |
| 101 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 102 | void UpdateResolutionUma(const DisplayConfigureRequest& request, |
| 103 | const std::string& uma_name) { |
| 104 | // Display is powered off. |
| 105 | if (!request.mode) |
| 106 | return; |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 107 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 108 | // First, compute the index of the enum DisplayResolution. |
| 109 | // The index has to match the definition of the enum in enums.xml. |
| 110 | const uint32_t samples_list_size = std::size(kDisplayResolutionSamples); |
| 111 | const gfx::Size size = request.mode->size(); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 112 | uint32_t width_idx = 0; |
| 113 | uint32_t height_idx = 0; |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 114 | for (; width_idx < samples_list_size; width_idx++) { |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 115 | if (size.width() <= kDisplayResolutionSamples[width_idx]) |
| 116 | break; |
| 117 | } |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 118 | for (; height_idx < samples_list_size; height_idx++) { |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 119 | if (size.height() <= kDisplayResolutionSamples[height_idx]) |
| 120 | break; |
| 121 | } |
| 122 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 123 | int display_resolution_index = 0; |
| 124 | if (width_idx == samples_list_size || height_idx == samples_list_size) { |
| 125 | // Check if we are in the overflow bucket. |
| 126 | display_resolution_index = samples_list_size * samples_list_size + 1; |
| 127 | } else { |
| 128 | // Compute the index of DisplayResolution, starting from 1, since 0 is used |
| 129 | // when powering off the display. |
| 130 | display_resolution_index = width_idx * samples_list_size + height_idx + 1; |
| 131 | } |
| 132 | |
| 133 | base::UmaHistogramExactLinear(uma_name, display_resolution_index, |
| 134 | samples_list_size * samples_list_size + 2); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 135 | } |
| 136 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 137 | // A list of common refresh rates that are used to help fit approximate refresh |
| 138 | // rate values into one of the common refresh rate bins. |
| 139 | constexpr float kCommonDisplayRefreshRates[] = { |
| 140 | 24.0, 25.0, 30.0, 45.0, 48.0, 50.0, 60.0, 75.0, |
| 141 | 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] | 142 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 143 | void UpdateRefreshRateUma(const DisplayConfigureRequest& request, |
| 144 | const std::string& uma_name) { |
| 145 | // Display is powered off. |
| 146 | if (!request.mode) |
| 147 | return; |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 148 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 149 | base::HistogramBase* histogram = base::SparseHistogram::FactoryGet( |
| 150 | uma_name, base::HistogramBase::kUmaTargetedHistogramFlag); |
| 151 | |
| 152 | // Check if the refresh value is within an epsilon from one of the common |
| 153 | // refresh rate values. |
| 154 | for (size_t i = 0; i < std::size(kCommonDisplayRefreshRates); ++i) { |
| 155 | const bool is_within_epsilon = |
| 156 | std::abs(request.mode->refresh_rate() - kCommonDisplayRefreshRates[i]) < |
| 157 | kRefreshRateEpsilon; |
| 158 | if (is_within_epsilon) { |
| 159 | histogram->Add(kCommonDisplayRefreshRates[i]); |
| 160 | return; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Since this is not a common refresh rate value, report it as is. |
| 165 | histogram->Add(request.mode->refresh_rate()); |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 166 | } |
| 167 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 168 | void UpdateAttemptSucceededUma( |
| 169 | const std::vector<DisplayConfigureRequest>& requests, |
| 170 | bool display_success) { |
| 171 | for (const auto& request : requests) { |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 172 | const std::string uma_name_prefix = GetUmaNamePrefixForRequest(request); |
| 173 | base::UmaHistogramBoolean(uma_name_prefix + "AttemptSucceeded", |
| 174 | display_success); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 175 | |
| 176 | VLOG(2) << "Configured status=" << display_success |
| 177 | << " display=" << request.display->display_id() |
| 178 | << " origin=" << request.origin.ToString() |
| 179 | << " mode=" << (request.mode ? request.mode->ToString() : "null"); |
| 180 | } |
Gil Dekel | 20dc530 | 2020-12-01 20:03:41 | [diff] [blame] | 181 | } |
| 182 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 183 | void UpdateFinalStatusUma( |
| 184 | const std::vector<RequestAndStatusList>& requests_and_statuses) { |
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) { |
| 188 | const DisplayConfigureRequest& request = request_and_status.first; |
| 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 | c8b8c4ed | 2022-05-04 20:30:38 | [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 | c8b8c4ed | 2022-05-04 20:30:38 | [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 | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 199 | const std::string uma_name_prefix = GetUmaNamePrefixForRequest(request); |
| 200 | if (request_and_status.second) { |
| 201 | UpdateResolutionUma(request, uma_name_prefix + "Success.Resolution"); |
| 202 | UpdateRefreshRateUma(request, uma_name_prefix + "Success.RefreshRate"); |
| 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 | |
| 208 | base::UmaHistogramExactLinear( |
| 209 | "ConfigureDisplays.Modeset.TotalExternalDisplaysCount", |
| 210 | base::checked_cast<int>(total_external_displays), kMaxDisplaysCount); |
| 211 | |
| 212 | base::UmaHistogramExactLinear( |
| 213 | "ConfigureDisplays.Modeset.MstExternalDisplaysCount", |
| 214 | mst_external_displays, kMaxDisplaysCount); |
| 215 | |
| 216 | if (total_external_displays > 0) { |
| 217 | const int mst_displays_percentage = |
| 218 | 100.0 * mst_external_displays / total_external_displays; |
| 219 | UMA_HISTOGRAM_PERCENTAGE( |
| 220 | "ConfigureDisplays.Modeset.MstExternalDisplaysPercentage", |
| 221 | mst_displays_percentage); |
| 222 | } |
| 223 | } |
| 224 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 225 | } // namespace |
| 226 | |
| 227 | DisplayConfigureRequest::DisplayConfigureRequest(DisplaySnapshot* display, |
| 228 | const DisplayMode* mode, |
| 229 | const gfx::Point& origin) |
kylechar | 731f85f9 | 2016-12-01 20:50:46 | [diff] [blame] | 230 | : display(display), mode(mode), origin(origin) {} |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 231 | |
| 232 | ConfigureDisplaysTask::ConfigureDisplaysTask( |
| 233 | NativeDisplayDelegate* delegate, |
| 234 | const std::vector<DisplayConfigureRequest>& requests, |
Sylvain Defresne | 23395c7a | 2019-10-02 10:07:45 | [diff] [blame] | 235 | ResponseCallback callback) |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 236 | : delegate_(delegate), |
| 237 | requests_(requests), |
Sylvain Defresne | 23395c7a | 2019-10-02 10:07:45 | [diff] [blame] | 238 | callback_(std::move(callback)), |
Jeremy Roman | 47d432e | 2019-08-20 14:24:00 | [diff] [blame] | 239 | task_status_(SUCCESS) { |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 240 | delegate_->AddObserver(this); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 241 | } |
| 242 | |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 243 | ConfigureDisplaysTask::~ConfigureDisplaysTask() { |
| 244 | delegate_->RemoveObserver(this); |
| 245 | } |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 246 | |
| 247 | void ConfigureDisplaysTask::Run() { |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 248 | DCHECK(!requests_.empty()); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 249 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 250 | const bool is_first_attempt = pending_display_group_requests_.empty(); |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 251 | std::vector<display::DisplayConfigurationParams> config_requests; |
| 252 | for (const auto& request : requests_) { |
Gil Dekel | bbc4035 | 2021-01-05 18:33:56 | [diff] [blame] | 253 | LogIfInvalidRequestForInternalDisplay(request); |
| 254 | |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 255 | config_requests.emplace_back(request.display->display_id(), request.origin, |
| 256 | request.mode); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 257 | |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 258 | if (is_first_attempt) { |
| 259 | const std::string uma_name_prefix = GetUmaNamePrefixForRequest(request); |
| 260 | UpdateResolutionUma(request, |
| 261 | uma_name_prefix + "OriginalRequest.Resolution"); |
| 262 | } |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 263 | } |
| 264 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 265 | const auto& on_configured = |
Gil Dekel | c8b8c4ed | 2022-05-04 20:30:38 | [diff] [blame^] | 266 | is_first_attempt ? &ConfigureDisplaysTask::OnFirstAttemptConfigured |
| 267 | : &ConfigureDisplaysTask::OnRetryConfigured; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 268 | |
| 269 | delegate_->Configure( |
| 270 | config_requests, |
| 271 | base::BindOnce(on_configured, weak_ptr_factory_.GetWeakPtr())); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 272 | } |
| 273 | |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 274 | void ConfigureDisplaysTask::OnConfigurationChanged() {} |
| 275 | |
| 276 | void ConfigureDisplaysTask::OnDisplaySnapshotsInvalidated() { |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 277 | // From now on, don't access |requests_[index]->display|; they're invalid. |
| 278 | task_status_ = ERROR; |
| 279 | weak_ptr_factory_.InvalidateWeakPtrs(); |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 280 | std::move(callback_).Run(task_status_); |
afakhry | 4e92e8c | 2017-04-20 17:04:59 | [diff] [blame] | 281 | } |
| 282 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 283 | void ConfigureDisplaysTask::OnFirstAttemptConfigured(bool config_success) { |
| 284 | UpdateAttemptSucceededUma(requests_, config_success); |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 285 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 286 | if (!config_success) { |
| 287 | // Partition |requests_| into smaller groups, update the task's state, and |
| 288 | // initiate the retry logic. The next time |delegate_|->Configure() |
| 289 | // terminates OnRetryConfigured() will be executed instead. |
| 290 | PartitionRequests(); |
| 291 | DCHECK(!pending_display_group_requests_.empty()); |
| 292 | requests_ = pending_display_group_requests_.front(); |
Gil Dekel | 42f7aba | 2021-01-08 18:59:55 | [diff] [blame] | 293 | task_status_ = PARTIAL_SUCCESS; |
| 294 | Run(); |
| 295 | return; |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 296 | } |
| 297 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 298 | // This code execute only when the first modeset attempt fully succeeds. |
| 299 | // Update the displays' status and report success. |
| 300 | for (const auto& request : requests_) { |
| 301 | request.display->set_current_mode(request.mode); |
| 302 | request.display->set_origin(request.origin); |
| 303 | final_requests_status_.emplace_back(std::make_pair(request, true)); |
| 304 | } |
Gil Dekel | 788edb6 | 2020-11-25 23:41:28 | [diff] [blame] | 305 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 306 | UpdateFinalStatusUma(final_requests_status_); |
Gil Dekel | 3369eec | 2020-10-06 19:53:10 | [diff] [blame] | 307 | std::move(callback_).Run(task_status_); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 308 | } |
| 309 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 310 | void ConfigureDisplaysTask::OnRetryConfigured(bool config_success) { |
| 311 | UpdateAttemptSucceededUma(requests_, config_success); |
| 312 | |
| 313 | if (!config_success) { |
| 314 | // If one of the largest display request can be downgraded, try again. |
| 315 | // Otherwise this configuration task is a failure. |
| 316 | if (DowngradeLargestRequestWithAlternativeModes()) { |
| 317 | Run(); |
| 318 | return; |
| 319 | } else { |
| 320 | task_status_ = ERROR; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // This code executes only when this display group request fully succeeds or |
| 325 | // fails to modeset. Update the final status of this group. |
| 326 | for (const auto& request : requests_) { |
| 327 | final_requests_status_.emplace_back( |
| 328 | std::make_pair(request, config_success)); |
| 329 | if (config_success) { |
| 330 | request.display->set_current_mode(request.mode); |
| 331 | request.display->set_origin(request.origin); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // Subsequent modeset attempts will be done on the next pending display group, |
| 336 | // if one exists. |
| 337 | pending_display_group_requests_.pop(); |
| 338 | requests_.clear(); |
| 339 | if (!pending_display_group_requests_.empty()) { |
| 340 | requests_ = pending_display_group_requests_.front(); |
| 341 | Run(); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // No more display groups to retry. |
| 346 | UpdateFinalStatusUma(final_requests_status_); |
| 347 | std::move(callback_).Run(task_status_); |
| 348 | } |
| 349 | |
| 350 | void ConfigureDisplaysTask::PartitionRequests() { |
| 351 | pending_display_group_requests_ = PartitionedRequestsQueue(); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 352 | |
Mark Yacoub | 100f2df | 2022-04-19 18:01:14 | [diff] [blame] | 353 | // PartitionRequests occurs when the first modeset fails and we start by |
| 354 | // modesetting the groups of connectors one after the other. When doing this, |
| 355 | // we must start by resetting the state and the allocation of resources to |
| 356 | // turn off any displays hogging the resources. |
| 357 | std::vector<DisplayConfigureRequest> disable_requests; |
| 358 | for (const DisplayConfigureRequest& request : requests_) |
| 359 | disable_requests.emplace_back(request.display, nullptr, gfx::Point()); |
| 360 | pending_display_group_requests_.push(disable_requests); |
| 361 | |
| 362 | base::flat_set<uint64_t> handled_connectors; |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 363 | for (size_t i = 0; i < requests_.size(); ++i) { |
| 364 | uint64_t connector_id = requests_[i].display->base_connector_id(); |
| 365 | if (handled_connectors.find(connector_id) != handled_connectors.end()) |
| 366 | continue; |
| 367 | |
| 368 | std::vector<DisplayConfigureRequest> request_group; |
| 369 | for (size_t j = i; j < requests_.size(); ++j) { |
| 370 | if (connector_id == requests_[j].display->base_connector_id()) |
| 371 | request_group.push_back(requests_[j]); |
| 372 | } |
| 373 | |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 374 | handled_connectors.insert(connector_id); |
Mark Yacoub | 100f2df | 2022-04-19 18:01:14 | [diff] [blame] | 375 | pending_display_group_requests_.push(request_group); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | bool ConfigureDisplaysTask::DowngradeLargestRequestWithAlternativeModes() { |
| 380 | auto cmp = [](DisplayConfigureRequest* lhs, DisplayConfigureRequest* rhs) { |
| 381 | return *lhs->mode < *rhs->mode; |
| 382 | }; |
| 383 | std::priority_queue<DisplayConfigureRequest*, |
| 384 | std::vector<DisplayConfigureRequest*>, decltype(cmp)> |
| 385 | sorted_requests(cmp); |
| 386 | |
| 387 | for (auto& request : requests_) { |
| 388 | if (request.display->type() == DISPLAY_CONNECTION_TYPE_INTERNAL) |
| 389 | continue; |
| 390 | |
| 391 | if (!request.mode) |
| 392 | continue; |
| 393 | |
| 394 | sorted_requests.push(&request); |
| 395 | } |
| 396 | |
| 397 | // Fail if there are no viable candidates to downgrade |
| 398 | if (sorted_requests.empty()) |
| 399 | return false; |
| 400 | |
| 401 | while (!sorted_requests.empty()) { |
| 402 | DisplayConfigureRequest* next_request = sorted_requests.top(); |
| 403 | sorted_requests.pop(); |
| 404 | |
Gil Dekel | 66a97bc | 2022-04-29 04:21:50 | [diff] [blame] | 405 | const DisplayMode* next_mode = FindNextMode(*next_request); |
Gil Dekel | 36b941dd | 2021-01-23 00:37:23 | [diff] [blame] | 406 | if (next_mode) { |
| 407 | next_request->mode = next_mode; |
| 408 | return true; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | return false; |
| 413 | } |
| 414 | |
kylechar | 7a067ec | 2017-01-07 01:16:28 | [diff] [blame] | 415 | } // namespace display |