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 | |
| 7 | #include "base/auto_reset.h" |
| 8 | #include "base/bind.h" |
Brett Wilson | b02c0a2 | 2017-09-25 22:34:42 | [diff] [blame] | 9 | #include "base/containers/queue.h" |
Hans Wennborg | 3930cf3 | 2020-06-17 16:29:52 | [diff] [blame] | 10 | #include "base/logging.h" |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 11 | #include "base/metrics/histogram_functions.h" |
| 12 | #include "base/stl_util.h" |
Joone Hur | d3ae873 | 2018-04-17 18:05:09 | [diff] [blame] | 13 | #include "ui/display/manager/display_util.h" |
Mark Yacoub | d18a292 | 2020-07-07 01:14:15 | [diff] [blame^] | 14 | #include "ui/display/types/display_configuration_params.h" |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 15 | #include "ui/display/types/display_snapshot.h" |
| 16 | #include "ui/display/types/native_display_delegate.h" |
| 17 | |
kylechar | 7a067ec | 2017-01-07 01:16:28 | [diff] [blame] | 18 | namespace display { |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | // Find the next best mode after |display_mode|. If none can be found return |
| 23 | // nullptr. |
| 24 | const DisplayMode* FindNextMode(const DisplaySnapshot& display_state, |
dnicoara | a89c208 | 2015-01-05 16:49:06 | [diff] [blame] | 25 | const DisplayMode* display_mode) { |
| 26 | if (!display_mode) |
| 27 | return nullptr; |
| 28 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 29 | int best_mode_pixels = 0; |
| 30 | const DisplayMode* best_mode = nullptr; |
dnicoara | a89c208 | 2015-01-05 16:49:06 | [diff] [blame] | 31 | int current_mode_pixels = display_mode->size().GetArea(); |
dbasehore | 01e9004 | 2016-05-27 06:16:51 | [diff] [blame] | 32 | for (const std::unique_ptr<const DisplayMode>& mode : display_state.modes()) { |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 33 | int pixel_count = mode->size().GetArea(); |
| 34 | if (pixel_count < current_mode_pixels && pixel_count > best_mode_pixels) { |
dbasehore | 01e9004 | 2016-05-27 06:16:51 | [diff] [blame] | 35 | best_mode = mode.get(); |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 36 | best_mode_pixels = pixel_count; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | return best_mode; |
| 41 | } |
| 42 | |
Daniele Castagna | 4f0689b | 2019-10-30 01:16:47 | [diff] [blame] | 43 | // Samples used to define buckets used by DisplayResolution enum. |
| 44 | // The enum is used to record screen resolution statistics. |
| 45 | const int32_t kDisplayResolutionSamples[] = {1024, 1280, 1440, 1920, |
| 46 | 2560, 3840, 5120, 7680}; |
| 47 | |
| 48 | // Computes the index of the enum DisplayResolution. |
| 49 | // The index has to match the definition of the enum in enums.xml |
| 50 | int ComputeDisplayResolutionEnum(const DisplayMode* mode) { |
| 51 | if (!mode) |
| 52 | return 0; // Display is powered off |
| 53 | |
| 54 | const gfx::Size size = mode->size(); |
| 55 | uint32_t width_idx = 0; |
| 56 | uint32_t height_idx = 0; |
| 57 | for (; width_idx < base::size(kDisplayResolutionSamples); width_idx++) { |
| 58 | if (size.width() <= kDisplayResolutionSamples[width_idx]) |
| 59 | break; |
| 60 | } |
| 61 | for (; height_idx < base::size(kDisplayResolutionSamples); height_idx++) { |
| 62 | if (size.height() <= kDisplayResolutionSamples[height_idx]) |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | if (width_idx == base::size(kDisplayResolutionSamples) || |
| 67 | height_idx == base::size(kDisplayResolutionSamples)) |
| 68 | return base::size(kDisplayResolutionSamples) * |
| 69 | base::size(kDisplayResolutionSamples) + |
| 70 | 1; // Overflow bucket |
| 71 | // Computes the index of DisplayResolution, starting from 1, since 0 is used |
| 72 | // when powering off the display. |
| 73 | return width_idx * base::size(kDisplayResolutionSamples) + height_idx + 1; |
| 74 | } |
| 75 | |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 76 | } // namespace |
| 77 | |
| 78 | DisplayConfigureRequest::DisplayConfigureRequest(DisplaySnapshot* display, |
| 79 | const DisplayMode* mode, |
| 80 | const gfx::Point& origin) |
kylechar | 731f85f9 | 2016-12-01 20:50:46 | [diff] [blame] | 81 | : display(display), mode(mode), origin(origin) {} |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 82 | |
| 83 | ConfigureDisplaysTask::ConfigureDisplaysTask( |
| 84 | NativeDisplayDelegate* delegate, |
| 85 | const std::vector<DisplayConfigureRequest>& requests, |
Sylvain Defresne | 23395c7a | 2019-10-02 10:07:45 | [diff] [blame] | 86 | ResponseCallback callback) |
dnicoara | 9372a791 | 2014-12-11 01:29:06 | [diff] [blame] | 87 | : delegate_(delegate), |
|
|