File: gl_fence_win.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 (174 lines) | stat: -rw-r--r-- 5,734 bytes parent folder | download | duplicates (5)
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
// Copyright 2020 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/gl_fence_win.h"

#include "base/logging.h"
#include "base/notreached.h"
#include "ui/gl/gl_angle_util_win.h"

#include <d3d11_4.h>

namespace gl {

bool GLFenceWin::IsSupported() {
  Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device =
      gl::QueryD3D11DeviceObjectFromANGLE();
  if (!d3d11_device)
    return false;

  // Support for ID3D11Device5 implies support for ID3D11DeviceContext4.
  // No point in letting you create fences if you can't signal or wait on them.
  Microsoft::WRL::ComPtr<ID3D11Device5> d3d11_device5;
  HRESULT hr = d3d11_device.As(&d3d11_device5);
  if (FAILED(hr)) {
    return false;
  }

  return true;
}

std::unique_ptr<GLFenceWin> GLFenceWin::CreateForGpuFence() {
  Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device =
      gl::QueryD3D11DeviceObjectFromANGLE();
  if (!d3d11_device) {
    DLOG(ERROR) << "Unable to retrieve ID3D11Device from ANGLE";
    return nullptr;
  }

  return CreateForGpuFence(d3d11_device.Get());
}

std::unique_ptr<GLFenceWin> GLFenceWin::CreateForGpuFence(
    ID3D11Device* d3d11_device) {
  Microsoft::WRL::ComPtr<ID3D11Device5> d3d11_device5;
  HRESULT hr = d3d11_device->QueryInterface(IID_PPV_ARGS(&d3d11_device5));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to retrieve ID3D11Device5 interface "
                << logging::SystemErrorCodeToString(hr);
    return nullptr;
  }

  Microsoft::WRL::ComPtr<ID3D11Fence> d3d11_fence;
  hr = d3d11_device5->CreateFence(0, D3D11_FENCE_FLAG_SHARED,
                                  IID_PPV_ARGS(&d3d11_fence));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to create ID3D11Fence "
                << logging::SystemErrorCodeToString(hr);
    return nullptr;
  }

  HANDLE shared_handle;
  hr = d3d11_fence->CreateSharedHandle(
      nullptr, DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE, nullptr,
      &shared_handle);
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to create shared handle for DXGIResource "
                << logging::SystemErrorCodeToString(hr);
    return nullptr;
  }

  // Put the shared handle into an RAII object as quickly as possible to
  // ensure we do not leak it.
  base::win::ScopedHandle scoped_handle(shared_handle);

  Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context;
  d3d11_device5->GetImmediateContext(&d3d11_device_context);

  Microsoft::WRL::ComPtr<ID3D11DeviceContext4> d3d11_device_context4;
  hr = d3d11_device_context.As(&d3d11_device_context4);
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to retrieve ID3D11DeviceContext4 interface "
                << logging::SystemErrorCodeToString(hr);
    return nullptr;
  }

  hr = d3d11_device_context4->Signal(d3d11_fence.Get(), 1);
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to Signal D3D11 fence "
                << logging::SystemErrorCodeToString(hr);
    return nullptr;
  }

  gfx::GpuFenceHandle gpu_fence_handle;
  gpu_fence_handle.Adopt(std::move(scoped_handle));
  return std::make_unique<GLFenceWin>(std::move(d3d11_fence),
                                      std::move(gpu_fence_handle));
}

std::unique_ptr<GLFenceWin> GLFenceWin::CreateFromGpuFence(
    const gfx::GpuFence& gpu_fence) {
  Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device =
      gl::QueryD3D11DeviceObjectFromANGLE();
  return CreateFromGpuFence(d3d11_device.Get(), gpu_fence);
}

std::unique_ptr<GLFenceWin> GLFenceWin::CreateFromGpuFence(
    ID3D11Device* d3d11_device,
    const gfx::GpuFence& gpu_fence) {
  Microsoft::WRL::ComPtr<ID3D11Device5> d3d11_device5;
  HRESULT hr = d3d11_device->QueryInterface(IID_PPV_ARGS(&d3d11_device5));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to retrieve ID3D11Device5 interface "
                << logging::SystemErrorCodeToString(hr);
    return nullptr;
  }

  gfx::GpuFenceHandle gpu_fence_handle = gpu_fence.GetGpuFenceHandle().Clone();
  Microsoft::WRL::ComPtr<ID3D11Fence> d3d11_fence;
  hr = d3d11_device5->OpenSharedFence(gpu_fence_handle.Peek(),
                                      IID_PPV_ARGS(&d3d11_fence));
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to open a shared fence "
                << logging::SystemErrorCodeToString(hr);
    return nullptr;
  }

  return std::make_unique<GLFenceWin>(std::move(d3d11_fence),
                                      std::move(gpu_fence_handle));
}

GLFenceWin::GLFenceWin(Microsoft::WRL::ComPtr<ID3D11Fence> d3d11_fence,
                       gfx::GpuFenceHandle gpu_fence_handle)
    : d3d11_fence_(std::move(d3d11_fence)),
      gpu_fence_handle_(std::move(gpu_fence_handle)) {}

GLFenceWin::~GLFenceWin() = default;

bool GLFenceWin::HasCompleted() {
  NOTREACHED();
}

void GLFenceWin::ClientWait() {
  NOTREACHED();
}

void GLFenceWin::ServerWait() {
  Microsoft::WRL::ComPtr<ID3D11Device> d3d11_device;
  d3d11_fence_->GetDevice(&d3d11_device);

  Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3d11_device_context;
  d3d11_device->GetImmediateContext(&d3d11_device_context);

  Microsoft::WRL::ComPtr<ID3D11DeviceContext4> d3d11_device_context4;
  HRESULT hr = d3d11_device_context.As(&d3d11_device_context4);
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to retrieve ID3D11DeviceContext4 interface "
                << logging::SystemErrorCodeToString(hr);
    return;
  }

  hr = d3d11_device_context4->Wait(d3d11_fence_.Get(), 1);
  if (FAILED(hr)) {
    DLOG(ERROR) << "Unable to Wait on D3D11 fence "
                << logging::SystemErrorCodeToString(hr);
    return;
  }
}

std::unique_ptr<gfx::GpuFence> GLFenceWin::GetGpuFence() {
  return std::make_unique<gfx::GpuFence>(gpu_fence_handle_.Clone());
}

}  // namespace gl