File: vsync_thread_win_dxgi.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (197 lines) | stat: -rw-r--r-- 6,336 bytes parent folder | download | duplicates (4)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2024 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/vsync_thread_win_dxgi.h"

#include "base/logging.h"
#include "base/trace_event/typed_macros.h"
#include "base/win/windows_version.h"
#include "ui/gl/gl_features.h"

namespace gl {
namespace {

// Check if a DXGI adapter is stale and needs to be replaced. This can happen
// e.g. when detaching/reattaching remote desktop sessions and causes subsequent
// WaitForVSyncs on the stale adapter/output to return instantly.
bool DXGIFactoryIsCurrent(IDXGIAdapter* dxgi_adapter) {
  CHECK(dxgi_adapter);

  HRESULT hr = S_OK;
  Microsoft::WRL::ComPtr<IDXGIFactory1> dxgi_factory;
  hr = dxgi_adapter->GetParent(IID_PPV_ARGS(&dxgi_factory));
  CHECK_EQ(S_OK, hr);
  return dxgi_factory->IsCurrent();
}

// Create a new factory and find a DXGI adapter matching a LUID. This is useful
// if we have a previous adapter whose factory has become stale.
Microsoft::WRL::ComPtr<IDXGIAdapter> FindDXGIAdapterOnNewFactory(
    const LUID luid) {
  HRESULT hr = S_OK;

  Microsoft::WRL::ComPtr<IDXGIFactory1> factory;
  hr = CreateDXGIFactory1(IID_PPV_ARGS(&factory));
  CHECK_EQ(S_OK, hr);

  Microsoft::WRL::ComPtr<IDXGIAdapter1> new_adapter;
  for (uint32_t i = 0;; i++) {
    hr = factory->EnumAdapters1(i, &new_adapter);
    if (hr == DXGI_ERROR_NOT_FOUND) {
      break;
    }
    if (FAILED(hr)) {
      DLOG(ERROR) << "EnumAdapters1 failed: "
                  << logging::SystemErrorCodeToString(hr);
      return nullptr;
    }

    DXGI_ADAPTER_DESC1 new_adapter_desc;
    hr = new_adapter->GetDesc1(&new_adapter_desc);
    CHECK_EQ(S_OK, hr);

    if (new_adapter_desc.AdapterLuid.HighPart == luid.HighPart &&
        new_adapter_desc.AdapterLuid.LowPart == luid.LowPart) {
      return new_adapter;
    }
  }

  DLOG(ERROR) << "Failed to find DXGI adapter with matching LUID";
  return nullptr;
}

// Return true if |output| is on |monitor|.
bool DXGIOutputIsOnMonitor(IDXGIOutput* output, const HMONITOR monitor) {
  CHECK(output);

  DXGI_OUTPUT_DESC desc = {};
  HRESULT hr = output->GetDesc(&desc);
  CHECK_EQ(S_OK, hr);
  return desc.Monitor == monitor;
}

Microsoft::WRL::ComPtr<IDXGIOutput> DXGIOutputFromMonitor(
    HMONITOR monitor,
    IDXGIAdapter* dxgi_adapter) {
  CHECK(dxgi_adapter);

  HRESULT hr = S_OK;

  Microsoft::WRL::ComPtr<IDXGIOutput> output;
  for (uint32_t i = 0;; i++) {
    hr = dxgi_adapter->EnumOutputs(i, &output);
    if (hr == DXGI_ERROR_NOT_FOUND) {
      break;
    }
    if (FAILED(hr)) {
      DLOG(ERROR) << "EnumOutputs failed: "
                  << logging::SystemErrorCodeToString(hr);
      return nullptr;
    }

    if (DXGIOutputIsOnMonitor(output.Get(), monitor)) {
      return output;
    }
  }

  DLOG(ERROR) << "Failed to find DXGI output with matching monitor";
  return nullptr;
}

Microsoft::WRL::ComPtr<IDXGIAdapter> GetAdapter(IDXGIDevice* device) {
  CHECK(device);

  Microsoft::WRL::ComPtr<IDXGIAdapter> adapter;
  CHECK_EQ(S_OK, device->GetAdapter(&adapter));
  return adapter;
}

LUID GetLuid(IDXGIAdapter* adapter) {
  CHECK(adapter);

  DXGI_ADAPTER_DESC desc;
  HRESULT hr = adapter->GetDesc(&desc);
  CHECK_EQ(S_OK, hr);
  return desc.AdapterLuid;
}
}  // namespace

VSyncThreadWinDXGI::VSyncThreadWinDXGI(
    Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device)
    : VSyncThreadWin(),
      vsync_provider_(gfx::kNullAcceleratedWidget),
      dxgi_adapter_(GetAdapter(dxgi_device.Get())),
      original_adapter_luid_(GetLuid(dxgi_adapter_.Get())) {}

VSyncThreadWinDXGI::~VSyncThreadWinDXGI() {
  NOTREACHED();
}

gfx::VSyncProvider* VSyncThreadWinDXGI::vsync_provider() {
  return &vsync_provider_;
}

base::TimeDelta VSyncThreadWinDXGI::GetVsyncInterval() {
  base::TimeTicks vsync_timebase;
  base::TimeDelta vsync_interval;

  // This is a simplified initial approach to fix crbug.com/1456399
  // In Windows SV3 builds DWM will operate with per monitor refresh
  // rates. As a result of this, DwmGetCompositionTimingInfo is no longer
  // guaranteed to align with the primary monitor but will instead align
  // with the current highest refresh rate monitor. This can cause issues
  // in clients which may be waiting on the primary monitor's vblank as
  // the reported interval may no longer match with the vblank wait.
  // To work around this discrepancy get the VSync interval directly from
  // monitor associated with window_ or the primary monitor.
  static bool use_sv3_workaround =
      base::win::GetVersion() > base::win::Version::WIN11_22H2 &&
      base::FeatureList::IsEnabled(
          features::kUsePrimaryMonitorVSyncIntervalOnSV3);

  const bool get_vsync_params_succeeded =
      use_sv3_workaround
          ? vsync_provider_.GetVSyncIntervalIfAvailable(&vsync_interval)
          : vsync_provider_.GetVSyncParametersIfAvailable(&vsync_timebase,
                                                          &vsync_interval);
  DCHECK(get_vsync_params_succeeded);

  return vsync_interval;
}

bool VSyncThreadWinDXGI::WaitForVSyncImpl(base::TimeDelta* vsync_interval) {
  *vsync_interval = GetVsyncInterval();

  if (!dxgi_adapter_ || !DXGIFactoryIsCurrent(dxgi_adapter_.Get())) {
    TRACE_EVENT("gpu", "DXGIFactoryIsCurrent non-current factory");
    dxgi_adapter_ = FindDXGIAdapterOnNewFactory(original_adapter_luid_);
    primary_output_.Reset();
  }

  // From Raymond Chen's blog "How do I get a handle to the primary monitor?"
  // https://devblogs.microsoft.com/oldnewthing/20141106-00/?p=43683
  const HMONITOR monitor = MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY);
  if (primary_output_ &&
      !DXGIOutputIsOnMonitor(primary_output_.Get(), monitor)) {
    TRACE_EVENT("gpu", "DXGIOutputIsOnMonitor primary monitor changed");
    primary_output_.Reset();
  }

  if (!primary_output_ && dxgi_adapter_) {
    primary_output_ = DXGIOutputFromMonitor(monitor, dxgi_adapter_.Get());
  }

  // WaitForVBlank returns success on desktop occlusion and returns early
  const bool wait_succeeded =
      primary_output_ && SUCCEEDED(primary_output_->WaitForVBlank());
  if (!wait_succeeded) {
    TRACE_EVENT2("gpu", "WaitForVSyncImpl", "has adapter", !!dxgi_adapter_,
                 "has output", !!primary_output_);
    return false;
  }

  return true;
}

}  // namespace gl