File: display_info.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 4,310 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/display/win/display_info.h"

#include <string.h>

#include "base/compiler_specific.h"
#include "base/hash/hash.h"
#include "base/metrics/histogram_functions.h"
#include "base/rand_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/display/win/display_config_helper.h"
#include "ui/display/win/screen_win_headless.h"

namespace display::win::internal {

namespace {

// Return a string view from a fixed-length array representing a string, up
// until the first nul terminator, if any.
template <size_t N>
std::wstring_view FixedArrayToStringView(
    const std::wstring_view::value_type (&str)[N]) {
  return std::wstring_view(str, UNSAFE_TODO(::wcsnlen(str, N)));
}

}  // namespace

DisplayInfo::DisplayInfo(
    const MONITORINFOEX& monitor_info,
    float device_scale_factor,
    float sdr_white_level,
    Display::Rotation rotation,
    float display_frequency,
    const gfx::Vector2dF& pixels_per_inch,
    DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY output_technology,
    const std::string& label)
    : id_(DisplayIdFromMonitorInfo(monitor_info)),
      screen_rect_(monitor_info.rcMonitor),
      screen_work_rect_(monitor_info.rcWork),
      device_scale_factor_(device_scale_factor),
      sdr_white_level_(sdr_white_level),
      rotation_(rotation),
      display_frequency_(display_frequency),
      pixels_per_inch_(pixels_per_inch),
      output_technology_(output_technology),
      label_(label),
      device_name_(FixedArrayToStringView(monitor_info.szDevice)) {}

DisplayInfo::DisplayInfo(
    int64_t id,
    const MONITORINFOEX& monitor_info,
    float device_scale_factor,
    float sdr_white_level,
    Display::Rotation rotation,
    float display_frequency,
    const gfx::Vector2dF& pixels_per_inch,
    DISPLAYCONFIG_VIDEO_OUTPUT_TECHNOLOGY output_technology,
    const std::string& label)
    : id_(id),
      screen_rect_(monitor_info.rcMonitor),
      screen_work_rect_(monitor_info.rcWork),
      device_scale_factor_(device_scale_factor),
      sdr_white_level_(sdr_white_level),
      rotation_(rotation),
      display_frequency_(display_frequency),
      pixels_per_inch_(pixels_per_inch),
      output_technology_(output_technology),
      label_(label),
      device_name_(FixedArrayToStringView(monitor_info.szDevice)) {
  CHECK(VerifyHeadlessDisplayDeviceName(id, monitor_info));
}

DisplayInfo::DisplayInfo(const DisplayInfo& other) = default;

DisplayInfo::~DisplayInfo() = default;

// static
int64_t DisplayInfo::DisplayIdFromMonitorInfo(const MONITORINFOEX& monitor) {
  // Derive a display ID from the adapter ID and per-adapter monitor ID.
  // This seems to be broadly available, unique for each monitor of the device,
  // and stable across display configuration changes, but not device restarts.
  std::optional<DISPLAYCONFIG_PATH_INFO> config_path =
      GetDisplayConfigPathInfo(monitor);
  // Record if DISPLAYCONFIG_PATH_INFO is available or not.
  // TODO(anandrv): Remove the non-subsampled metric when there is enough data
  // from the subsampled metric.
  base::UmaHistogramBoolean("Windows.LegacyDisplayIdAlgorithm",
                            !config_path.has_value());
  if (base::ShouldRecordSubsampledMetric(0.01)) {
    base::UmaHistogramBoolean("Windows.LegacyDisplayIdAlgorithm2",
                              !config_path.has_value());
  }

  if (config_path.has_value()) {
    return static_cast<int64_t>(base::PersistentHash(base::StringPrintf(
        "%lu/%li/%u", config_path->targetInfo.adapterId.LowPart,
        config_path->targetInfo.adapterId.HighPart,
        config_path->targetInfo.id)));
  }
  // MONITORINFOEX::szDevice is a plausible backup with some notable drawbacks.
  // This value (e.g. "\\.\DISPLAY1") may change when adding/removing displays,
  // and even be reassigned between physical monitors during those changes,
  // which can cause subtle unexpected behavior.
  return static_cast<int64_t>(base::PersistentHash(
      base::WideToUTF8(FixedArrayToStringView(monitor.szDevice))));
}

bool DisplayInfo::operator==(const DisplayInfo& rhs) const = default;

}  // namespace display::win::internal