File: hdr_metadata_helper_win.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (144 lines) | stat: -rw-r--r-- 5,059 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2019 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/gl/hdr_metadata_helper_win.h"
#include "ui/gl/gpu_switching_manager.h"

namespace {

// Magic constants to convert to fixed point.
// https://docs.microsoft.com/en-us/windows/win32/api/dxgi1_5/ns-dxgi1_5-dxgi_hdr_metadata_hdr10
static constexpr int kPrimariesFixedPoint = 50000;
static constexpr int kMinLuminanceFixedPoint = 10000;

}  // namespace

namespace gl {

HDRMetadataHelperWin::HDRMetadataHelperWin(
    Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device)
    : d3d11_device_(std::move(d3d11_device)) {
  UpdateDisplayMetadata();
  ui::GpuSwitchingManager::GetInstance()->AddObserver(this);
}

HDRMetadataHelperWin::~HDRMetadataHelperWin() {
  ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this);
}

absl::optional<DXGI_HDR_METADATA_HDR10>
HDRMetadataHelperWin::GetDisplayMetadata() {
  return hdr_metadata_;
}

void HDRMetadataHelperWin::UpdateDisplayMetadata() {
  hdr_metadata_.reset();

  if (!d3d11_device_)
    return;

  Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device;
  if (FAILED(d3d11_device_.As(&dxgi_device)))
    return;

  Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter;
  if (FAILED(dxgi_device->GetAdapter(&dxgi_adapter)))
    return;

  Microsoft::WRL::ComPtr<IDXGIFactory> dxgi_factory;
  if (FAILED(dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory))))
    return;

  DXGI_OUTPUT_DESC1 desc_best{};
  bool found_monitor = false;

  // Enumerate all the monitors attached to all the adapters.  Pick the
  // brightest monitor as the one we want, which makes no sense really.
  // TODO(liberato): figure out what monitor we're actually using, or get that
  // from the renderer.
  Microsoft::WRL::ComPtr<IDXGIAdapter> adapter;
  for (unsigned int i = 0;
       dxgi_factory->EnumAdapters(i, &adapter) != DXGI_ERROR_NOT_FOUND; i++) {
    Microsoft::WRL::ComPtr<IDXGIOutput> output;
    for (unsigned int u = 0;
         adapter->EnumOutputs(u, &output) != DXGI_ERROR_NOT_FOUND; u++) {
      Microsoft::WRL::ComPtr<IDXGIOutput6> output6;
      if (FAILED(output.As(&output6)))
        continue;

      DXGI_OUTPUT_DESC1 desc1{};
      if (FAILED(output6->GetDesc1(&desc1)))
        continue;

      if (desc_best.MaxLuminance < desc1.MaxLuminance) {
        desc_best = desc1;
        found_monitor = true;
      }
    }
  }

  if (!found_monitor)
    return;

  DXGI_HDR_METADATA_HDR10 metadata{};

  auto& primary_r = desc_best.RedPrimary;
  metadata.RedPrimary[0] = primary_r[0] * kPrimariesFixedPoint;
  metadata.RedPrimary[1] = primary_r[1] * kPrimariesFixedPoint;
  auto& primary_g = desc_best.GreenPrimary;
  metadata.GreenPrimary[0] = primary_g[0] * kPrimariesFixedPoint;
  metadata.GreenPrimary[1] = primary_g[1] * kPrimariesFixedPoint;
  auto& primary_b = desc_best.BluePrimary;
  metadata.BluePrimary[0] = primary_b[0] * kPrimariesFixedPoint;
  metadata.BluePrimary[1] = primary_b[1] * kPrimariesFixedPoint;
  auto& white_point = desc_best.WhitePoint;
  metadata.WhitePoint[0] = white_point[0] * kPrimariesFixedPoint;
  metadata.WhitePoint[1] = white_point[1] * kPrimariesFixedPoint;
  metadata.MaxMasteringLuminance = desc_best.MaxLuminance;
  metadata.MinMasteringLuminance =
      desc_best.MinLuminance * kMinLuminanceFixedPoint;
  // It's unclear how to set these properly, so this is a guess.
  // Also note that these are not fixed-point.
  metadata.MaxContentLightLevel = desc_best.MaxFullFrameLuminance;
  metadata.MaxFrameAverageLightLevel = desc_best.MaxFullFrameLuminance;

  hdr_metadata_ = metadata;
}

// static
DXGI_HDR_METADATA_HDR10 HDRMetadataHelperWin::HDRMetadataToDXGI(
    const gfx::HDRMetadata& hdr_metadata) {
  DXGI_HDR_METADATA_HDR10 metadata{};

  const auto smpte_st_2086 =
      hdr_metadata.smpte_st_2086.value_or(gfx::HdrMetadataSmpteSt2086());
  const auto& primaries = smpte_st_2086.primaries;
  metadata.RedPrimary[0] = primaries.fRX * kPrimariesFixedPoint;
  metadata.RedPrimary[1] = primaries.fRY * kPrimariesFixedPoint;
  metadata.GreenPrimary[0] = primaries.fGX * kPrimariesFixedPoint;
  metadata.GreenPrimary[1] = primaries.fGY * kPrimariesFixedPoint;
  metadata.BluePrimary[0] = primaries.fBX * kPrimariesFixedPoint;
  metadata.BluePrimary[1] = primaries.fBY * kPrimariesFixedPoint;
  metadata.WhitePoint[0] = primaries.fWX * kPrimariesFixedPoint;
  metadata.WhitePoint[1] = primaries.fWY * kPrimariesFixedPoint;
  metadata.MaxMasteringLuminance = smpte_st_2086.luminance_max;
  metadata.MinMasteringLuminance =
      smpte_st_2086.luminance_min * kMinLuminanceFixedPoint;

  const auto cta_861_3 =
      hdr_metadata.cta_861_3.value_or(gfx::HdrMetadataCta861_3());
  metadata.MaxContentLightLevel = cta_861_3.max_content_light_level;
  metadata.MaxFrameAverageLightLevel = cta_861_3.max_frame_average_light_level;

  return metadata;
}

void HDRMetadataHelperWin::OnDisplayAdded() {
  UpdateDisplayMetadata();
}

void HDRMetadataHelperWin::OnDisplayRemoved() {
  UpdateDisplayMetadata();
}
}  // namespace gl