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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
// Copyright 2022 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/gfx/win/d3d_shared_fence.h"
#include "base/debug/alias.h"
#include "base/logging.h"
#include "base/notreached.h"
#include <dxgi1_6.h>
namespace gfx {
namespace {
Microsoft::WRL::ComPtr<ID3D11DeviceContext4> GetDeviceContext4(
ID3D11Device* d3d11_device) {
Microsoft::WRL::ComPtr<ID3D11DeviceContext> context;
d3d11_device->GetImmediateContext(&context);
Microsoft::WRL::ComPtr<ID3D11DeviceContext4> context4;
HRESULT hr = context.As(&context4);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to get ID3D11DeviceContext4: "
<< logging::SystemErrorCodeToString(hr);
return nullptr;
}
return context4;
}
base::win::ScopedHandle DuplicateSharedHandle(HANDLE shared_handle) {
const base::ProcessHandle process = ::GetCurrentProcess();
HANDLE duplicated_handle = INVALID_HANDLE_VALUE;
const BOOL result =
::DuplicateHandle(process, shared_handle, process, &duplicated_handle, 0,
FALSE, DUPLICATE_SAME_ACCESS);
if (!result) {
const DWORD last_error = ::GetLastError();
base::debug::Alias(&last_error);
NOTREACHED();
}
return base::win::ScopedHandle(duplicated_handle);
}
} // namespace
// static
scoped_refptr<D3DSharedFence> D3DSharedFence::CreateForD3D11(
Microsoft::WRL::ComPtr<ID3D11Device> d3d11_signal_device) {
Microsoft::WRL::ComPtr<ID3D11Device5> d3d11_device5;
HRESULT hr = d3d11_signal_device.As(&d3d11_device5);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to get ID3D11Device5: "
<< 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) << "CreateFence failed with error "
<< logging::SystemErrorCodeToString(hr);
return nullptr;
}
return CreateFromD3D11Fence(std::move(d3d11_signal_device),
std::move(d3d11_fence),
/*fence_value=*/0);
}
// static
scoped_refptr<D3DSharedFence> D3DSharedFence::CreateFromD3D11Fence(
Microsoft::WRL::ComPtr<ID3D11Device> d3d11_signal_device,
Microsoft::WRL::ComPtr<ID3D11Fence> d3d11_fence,
uint64_t fence_value) {
HANDLE shared_handle = nullptr;
HRESULT hr = d3d11_fence->CreateSharedHandle(nullptr, GENERIC_ALL, nullptr,
&shared_handle);
if (FAILED(hr)) {
DLOG(ERROR) << "Unable to create shared handle for D3D11Fence: "
<< logging::SystemErrorCodeToString(hr);
return nullptr;
}
auto fence = base::WrapRefCounted(new D3DSharedFence(
base::win::ScopedHandle(shared_handle), DXGIHandleToken()));
fence->d3d11_signal_device_ = std::move(d3d11_signal_device);
fence->d3d11_signal_fence_ = std::move(d3d11_fence);
fence->fence_value_ = fence_value;
return fence;
}
// static
bool D3DSharedFence::IsSupported(ID3D11Device* d3d11_device) {
DCHECK(d3d11_device);
// Check for ID3D11Device5:
Microsoft::WRL::ComPtr<ID3D11Device5> d3d11_device5;
HRESULT hr = d3d11_device->QueryInterface(IID_PPV_ARGS(&d3d11_device5));
if (FAILED(hr)) {
DVLOG(1) << "Failed to get ID3D11Device5: "
<< logging::SystemErrorCodeToString(hr);
return false;
}
// Check for IDXGIAdapter4:
Microsoft::WRL::ComPtr<IDXGIDevice> dxgi_device;
hr = d3d11_device->QueryInterface(IID_PPV_ARGS(&dxgi_device));
CHECK_EQ(hr, S_OK);
Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter;
hr = dxgi_device->GetAdapter(&dxgi_adapter);
CHECK_EQ(hr, S_OK);
Microsoft::WRL::ComPtr<IDXGIAdapter4> dxgi_adapter4;
hr = dxgi_adapter.As(&dxgi_adapter4);
if (FAILED(hr)) {
DVLOG(1) << "Failed to get IDXGIAdapter4: "
<< logging::SystemErrorCodeToString(hr);
return false;
}
DXGI_ADAPTER_DESC3 adapter_desc;
hr = dxgi_adapter4->GetDesc3(&adapter_desc);
CHECK_EQ(hr, S_OK);
// The adapter must support monitored fences.
return adapter_desc.Flags & DXGI_ADAPTER_FLAG3_SUPPORT_MONITORED_FENCES;
}
// static
scoped_refptr<D3DSharedFence> D3DSharedFence::CreateFromScopedHandle(
base::win::ScopedHandle fence_handle,
const DXGIHandleToken& fence_token) {
return base::WrapRefCounted(
new D3DSharedFence(std::move(fence_handle), fence_token));
}
scoped_refptr<D3DSharedFence> D3DSharedFence::CreateFromUnownedHandle(
HANDLE shared_handle) {
return base::WrapRefCounted(new D3DSharedFence(
DuplicateSharedHandle(shared_handle), DXGIHandleToken()));
}
D3DSharedFence::D3DSharedFence(base::win::ScopedHandle shared_handle,
const DXGIHandleToken& dxgi_token)
: shared_handle_(std::move(shared_handle)),
dxgi_token_(dxgi_token),
d3d11_wait_fence_map_(kMaxD3D11FenceMapSize) {}
D3DSharedFence::~D3DSharedFence() = default;
HANDLE D3DSharedFence::GetSharedHandle() const {
return shared_handle_.get();
}
base::win::ScopedHandle D3DSharedFence::CloneSharedHandle() {
return DuplicateSharedHandle(shared_handle_.get());
}
uint64_t D3DSharedFence::GetFenceValue() const {
return fence_value_;
}
const DXGIHandleToken& D3DSharedFence::GetDXGIHandleToken() const {
return dxgi_token_;
}
Microsoft::WRL::ComPtr<ID3D11Device> D3DSharedFence::GetD3D11Device() const {
return d3d11_signal_device_;
}
bool D3DSharedFence::IsSameFenceAsHandle(HANDLE shared_handle) const {
return CompareObjectHandles(shared_handle_.Get(), shared_handle);
}
void D3DSharedFence::Update(uint64_t fence_value) {
if (fence_value > fence_value_) {
fence_value_ = fence_value;
}
}
bool D3DSharedFence::WaitD3D11(
Microsoft::WRL::ComPtr<ID3D11Device> d3d11_wait_device) {
// Skip wait if passed in device is the same as signaling device.
if (d3d11_wait_device == d3d11_signal_device_) {
return true;
}
auto it = d3d11_wait_fence_map_.Get(d3d11_wait_device);
if (it == d3d11_wait_fence_map_.end()) {
Microsoft::WRL::ComPtr<ID3D11Device5> d3d11_device5;
HRESULT hr = d3d11_wait_device.As(&d3d11_device5);
if (FAILED(hr)) {
DLOG(ERROR) << "Failed to get ID3D11Device5: "
<< logging::SystemErrorCodeToString(hr);
return false;
}
Microsoft::WRL::ComPtr<ID3D11Fence> d3d11_fence;
hr = d3d11_device5->OpenSharedFence(shared_handle_.get(),
IID_PPV_ARGS(&d3d11_fence));
if (FAILED(hr)) {
DLOG(ERROR) << "OpenSharedFence failed: "
<< logging::SystemErrorCodeToString(hr);
return false;
}
it = d3d11_wait_fence_map_.Put(d3d11_wait_device, std::move(d3d11_fence));
}
const Microsoft::WRL::ComPtr<ID3D11Fence>& fence = it->second;
// Skip wait if we're already past the wait value.
if (fence->GetCompletedValue() >= fence_value_) {
return true;
}
Microsoft::WRL::ComPtr<ID3D11DeviceContext4> context4 =
GetDeviceContext4(d3d11_wait_device.Get());
if (!context4) {
return false;
}
HRESULT hr = context4->Wait(fence.Get(), fence_value_);
if (FAILED(hr)) {
DLOG(ERROR) << "D3D11 fence wait failed: "
<< logging::SystemErrorCodeToString(hr);
return false;
}
return true;
}
bool D3DSharedFence::IncrementAndSignalD3D11() {
CHECK(d3d11_signal_device_)
<< "D3D11 fence is expected to be signaled externally";
DCHECK(d3d11_signal_fence_);
Microsoft::WRL::ComPtr<ID3D11DeviceContext4> context4 =
GetDeviceContext4(d3d11_signal_device_.Get());
if (!context4) {
return false;
}
HRESULT hr = context4->Signal(d3d11_signal_fence_.Get(), fence_value_ + 1);
if (FAILED(hr)) {
DLOG(ERROR) << "D3D11 fence signal failed: "
<< logging::SystemErrorCodeToString(hr);
return false;
}
fence_value_++;
return true;
}
} // namespace gfx
|