blob: 52e232e8f253f16c94481f3ba6ae41a73de3ba32 [file] [log] [blame]
dnicoara9372a7912014-12-11 01:29:061// 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 Hurd3ae8732018-04-17 18:05:095#include "ui/display/manager/configure_displays_task.h"
dnicoara9372a7912014-12-11 01:29:066
7#include "base/auto_reset.h"
8#include "base/bind.h"
Brett Wilsonb02c0a22017-09-25 22:34:429#include "base/containers/queue.h"
Hans Wennborg3930cf32020-06-17 16:29:5210#include "base/logging.h"
Daniele Castagna4f0689b2019-10-30 01:16:4711#include "base/metrics/histogram_functions.h"
12#include "base/stl_util.h"
Joone Hurd3ae8732018-04-17 18:05:0913#include "ui/display/manager/display_util.h"
Mark Yacoubd18a2922020-07-07 01:14:1514#include "ui/display/types/display_configuration_params.h"
dnicoara9372a7912014-12-11 01:29:0615#include "ui/display/types/display_snapshot.h"
16#include "ui/display/types/native_display_delegate.h"
17
kylechar7a067ec2017-01-07 01:16:2818namespace display {
dnicoara9372a7912014-12-11 01:29:0619
20namespace {
21
22// Find the next best mode after |display_mode|. If none can be found return
23// nullptr.
24const DisplayMode* FindNextMode(const DisplaySnapshot& display_state,
dnicoaraa89c2082015-01-05 16:49:0625 const DisplayMode* display_mode) {
26 if (!display_mode)
27 return nullptr;
28
dnicoara9372a7912014-12-11 01:29:0629 int best_mode_pixels = 0;
30 const DisplayMode* best_mode = nullptr;
dnicoaraa89c2082015-01-05 16:49:0631 int current_mode_pixels = display_mode->size().GetArea();
dbasehore01e90042016-05-27 06:16:5132 for (const std::unique_ptr<const DisplayMode>& mode : display_state.modes()) {
dnicoara9372a7912014-12-11 01:29:0633 int pixel_count = mode->size().GetArea();
34 if (pixel_count < current_mode_pixels && pixel_count > best_mode_pixels) {
dbasehore01e90042016-05-27 06:16:5135 best_mode = mode.get();
dnicoara9372a7912014-12-11 01:29:0636 best_mode_pixels = pixel_count;
37 }
38 }
39
40 return best_mode;
41}
42
Daniele Castagna4f0689b2019-10-30 01:16:4743// Samples used to define buckets used by DisplayResolution enum.
44// The enum is used to record screen resolution statistics.
45const 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
50int 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
dnicoara9372a7912014-12-11 01:29:0676} // namespace
77
78DisplayConfigureRequest::DisplayConfigureRequest(DisplaySnapshot* display,
79 const DisplayMode* mode,
80 const gfx::Point& origin)
kylechar731f85f92016-12-01 20:50:4681 : display(display), mode(mode), origin(origin) {}
dnicoara9372a7912014-12-11 01:29:0682
83ConfigureDisplaysTask::ConfigureDisplaysTask(
84 NativeDisplayDelegate* delegate,
85 const std::vector<DisplayConfigureRequest>& requests,
Sylvain Defresne23395c7a2019-10-02 10:07:4586 ResponseCallback callback)
dnicoara9372a7912014-12-11 01:29:0687 : delegate_(delegate),