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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "FenceD3D11.h"
#include <d3d11.h>
#include <d3d11_3.h>
#include <d3d11_4.h>
#include "mozilla/gfx/Logging.h"
namespace mozilla {
namespace layers {
RefPtr<ID3D11Device> mDevice;
/* static */
RefPtr<FenceD3D11> FenceD3D11::Create(ID3D11Device* aDevice) {
MOZ_ASSERT(aDevice);
if (!aDevice) {
return nullptr;
}
RefPtr<ID3D11Device5> d3d11_5;
auto hr =
aDevice->QueryInterface(__uuidof(ID3D11Device5), getter_AddRefs(d3d11_5));
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device5: " << gfx::hexa(hr);
return nullptr;
}
RefPtr<ID3D11Fence> fenceD3D11;
d3d11_5->CreateFence(0, D3D11_FENCE_FLAG_SHARED,
IID_PPV_ARGS((ID3D11Fence**)getter_AddRefs(fenceD3D11)));
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Fence creation failed: " << gfx::hexa(hr);
return nullptr;
}
HANDLE sharedHandle = nullptr;
hr = fenceD3D11->CreateSharedHandle(nullptr, GENERIC_ALL, nullptr,
&sharedHandle);
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Fence shared handle creation failed "
<< gfx::hexa(hr);
return nullptr;
}
RefPtr<gfx::FileHandleWrapper> handle =
new gfx::FileHandleWrapper(UniqueFileHandle(sharedHandle));
RefPtr<FenceD3D11> fence = new FenceD3D11(handle);
fence->mDevice = aDevice;
fence->mSignalFence = fenceD3D11;
return fence;
}
/* static */
RefPtr<FenceD3D11> FenceD3D11::CreateFromHandle(
RefPtr<gfx::FileHandleWrapper> aHandle) {
// Opening shared handle is deferred.
return new FenceD3D11(aHandle);
}
/* static */
bool FenceD3D11::IsSupported(ID3D11Device* aDevice) {
RefPtr<ID3D11Device5> d3d11_5;
auto hr =
aDevice->QueryInterface(__uuidof(ID3D11Device5), getter_AddRefs(d3d11_5));
if (FAILED(hr)) {
return false;
}
return true;
}
FenceD3D11::FenceD3D11(RefPtr<gfx::FileHandleWrapper>& aHandle)
: mHandle(aHandle) {
MOZ_ASSERT(mHandle);
}
FenceD3D11::~FenceD3D11() {}
gfx::FenceInfo FenceD3D11::GetFenceInfo() const {
return gfx::FenceInfo(mHandle, mFenceValue);
}
bool FenceD3D11::IncrementAndSignal() {
MOZ_ASSERT(mDevice);
MOZ_ASSERT(mSignalFence);
if (!mDevice || !mSignalFence) {
return false;
}
RefPtr<ID3D11DeviceContext> context;
mDevice->GetImmediateContext(getter_AddRefs(context));
RefPtr<ID3D11DeviceContext4> context4;
auto hr = context->QueryInterface(__uuidof(ID3D11DeviceContext4),
getter_AddRefs(context4));
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Failed to get D3D11DeviceContext4: "
<< gfx::hexa(hr);
return false;
}
hr = context4->Signal(mSignalFence, mFenceValue + 1);
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Signal fence failed: " << gfx::hexa(hr);
return false;
}
mFenceValue++;
return true;
}
void FenceD3D11::Update(uint64_t aFenceValue) {
MOZ_ASSERT(!mDevice);
MOZ_ASSERT(!mSignalFence);
if (mFenceValue > aFenceValue) {
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
return;
}
mFenceValue = aFenceValue;
}
bool FenceD3D11::Wait(ID3D11Device* aDevice) {
MOZ_ASSERT(aDevice);
if (!aDevice) {
return false;
}
// Skip wait if passed device is the same as signaling device.
if (mDevice == aDevice) {
return true;
}
RefPtr<ID3D11Fence> fence;
auto it = mWaitFenceMap.find(aDevice);
if (it == mWaitFenceMap.end()) {
RefPtr<ID3D11Device5> d3d11_5;
auto hr = aDevice->QueryInterface(__uuidof(ID3D11Device5),
getter_AddRefs(d3d11_5));
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Failed to get ID3D11Device5: " << gfx::hexa(hr);
return false;
}
hr = d3d11_5->OpenSharedFence(mHandle->GetHandle(), __uuidof(ID3D11Fence),
(void**)(ID3D11Fence**)getter_AddRefs(fence));
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Opening fence shared handle failed "
<< gfx::hexa(hr);
return false;
}
mWaitFenceMap.emplace(aDevice, fence);
} else {
fence = it->second;
}
if (!fence) {
MOZ_ASSERT_UNREACHABLE("unexpected to be called");
return false;
}
RefPtr<ID3D11DeviceContext> context;
aDevice->GetImmediateContext(getter_AddRefs(context));
RefPtr<ID3D11DeviceContext4> context4;
auto hr = context->QueryInterface(__uuidof(ID3D11DeviceContext4),
getter_AddRefs(context4));
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Failed to get D3D11DeviceContext4: "
<< gfx::hexa(hr);
return false;
}
hr = context4->Wait(fence, mFenceValue);
if (FAILED(hr)) {
gfxCriticalNoteOnce << "Failed to wait fence: " << gfx::hexa(hr);
return false;
}
return true;
}
} // namespace layers
} // namespace mozilla
|