Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 0987c90

Browse files
committed
Bug 1576975 - Compile VRSession.cpp into vrhost.dll r=kip
Includes changes for separating mozilla-central-specific code. Differential Revision: https://phabricator.services.mozilla.com/D43859 --HG-- extra : moz-landing-system : lando
1 parent c146e8c commit 0987c90

3 files changed

Lines changed: 100 additions & 52 deletions

File tree

gfx/vr/service/VRSession.cpp

Lines changed: 72 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,56 @@
1212
# include <d3d11.h>
1313
#endif // defined(XP_WIN)
1414

15+
#ifndef MOZILLA_INTERNAL_API
16+
# define NS_WARNING(s)
17+
#endif
18+
1519
using namespace mozilla::gfx;
1620

17-
VRSession::VRSession() : mShouldQuit(false) {}
21+
VRSession::VRSession()
22+
: mShouldQuit(false)
23+
#ifdef XP_WIN
24+
,
25+
mDevice(nullptr),
26+
mContext(nullptr),
27+
mDeviceContextState(nullptr)
28+
#endif
29+
{
30+
}
31+
32+
#ifdef XP_WIN
33+
VRSession::~VRSession() {
34+
if (mDevice != nullptr) {
35+
mDevice->Release();
36+
mDevice = nullptr;
37+
}
38+
39+
if (mContext != nullptr) {
40+
mContext->Release();
41+
mContext = nullptr;
42+
}
43+
44+
if (mDeviceContextState != nullptr) {
45+
mDeviceContextState->Release();
46+
mDeviceContextState = nullptr;
47+
}
48+
}
49+
#endif
1850

1951
#if defined(XP_WIN)
20-
bool VRSession::CreateD3DContext(RefPtr<ID3D11Device> aDevice) {
52+
bool VRSession::CreateD3DContext(ID3D11Device* aDevice) {
2153
if (!mDevice) {
2254
if (!aDevice) {
2355
NS_WARNING("VRSession::CreateD3DObjects failed to get a D3D11Device");
2456
return false;
2557
}
26-
if (FAILED(aDevice->QueryInterface(__uuidof(ID3D11Device1),
27-
getter_AddRefs(mDevice)))) {
58+
if (FAILED(aDevice->QueryInterface(IID_PPV_ARGS(&mDevice)))) {
2859
NS_WARNING("VRSession::CreateD3DObjects failed to get a D3D11Device1");
2960
return false;
3061
}
3162
}
3263
if (!mContext) {
33-
mDevice->GetImmediateContext1(getter_AddRefs(mContext));
64+
mDevice->GetImmediateContext1(&mContext);
3465
if (!mContext) {
3566
NS_WARNING(
3667
"VRSession::CreateD3DObjects failed to get an immediate context");
@@ -42,7 +73,7 @@ bool VRSession::CreateD3DContext(RefPtr<ID3D11Device> aDevice) {
4273
D3D_FEATURE_LEVEL_11_0};
4374
mDevice->CreateDeviceContextState(0, featureLevels, 2, D3D11_SDK_VERSION,
4475
__uuidof(ID3D11Device1), nullptr,
45-
getter_AddRefs(mDeviceContextState));
76+
&mDeviceContextState);
4677
}
4778
if (!mDeviceContextState) {
4879
NS_WARNING(
@@ -65,44 +96,46 @@ ID3DDeviceContextState* VRSession::GetD3DDeviceContextState() {
6596
bool VRSession::SubmitFrame(
6697
const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer) {
6798
#if defined(XP_WIN)
68-
99+
bool success = false;
69100
if (aLayer.textureType ==
70101
VRLayerTextureType::LayerTextureType_D3D10SurfaceDescriptor) {
71-
RefPtr<ID3D11Texture2D> dxTexture;
72-
HRESULT hr = mDevice->OpenSharedResource(
73-
(HANDLE)aLayer.textureHandle, __uuidof(ID3D11Texture2D),
74-
(void**)(ID3D11Texture2D**)getter_AddRefs(dxTexture));
75-
if (FAILED(hr) || !dxTexture) {
102+
ID3D11Texture2D* dxTexture = nullptr;
103+
HRESULT hr = mDevice->OpenSharedResource((HANDLE)aLayer.textureHandle,
104+
IID_PPV_ARGS(&dxTexture));
105+
if (SUCCEEDED(hr) && dxTexture != nullptr) {
106+
// Similar to LockD3DTexture in TextureD3D11.cpp
107+
IDXGIKeyedMutex* mutex = nullptr;
108+
hr = dxTexture->QueryInterface(IID_PPV_ARGS(&mutex));
109+
if (SUCCEEDED(hr)) {
110+
hr = mutex->AcquireSync(0, 1000);
111+
# ifdef MOZILLA_INTERNAL_API
112+
if (hr == WAIT_TIMEOUT) {
113+
gfxDevCrash(LogReason::D3DLockTimeout) << "D3D lock mutex timeout";
114+
} else if (hr == WAIT_ABANDONED) {
115+
gfxCriticalNote << "GFX: D3D11 lock mutex abandoned";
116+
}
117+
# endif
118+
if (SUCCEEDED(hr)) {
119+
success = SubmitFrame(aLayer, dxTexture);
120+
hr = mutex->ReleaseSync(0);
121+
if (FAILED(hr)) {
122+
NS_WARNING("Failed to unlock the texture");
123+
}
124+
} else {
125+
NS_WARNING("Failed to lock the texture");
126+
}
127+
128+
mutex->Release();
129+
mutex = nullptr;
130+
}
131+
132+
dxTexture->Release();
133+
dxTexture = nullptr;
134+
} else {
76135
NS_WARNING("Failed to open shared texture");
77-
return false;
78136
}
79137

80-
// Similar to LockD3DTexture in TextureD3D11.cpp
81-
RefPtr<IDXGIKeyedMutex> mutex;
82-
dxTexture->QueryInterface((IDXGIKeyedMutex**)getter_AddRefs(mutex));
83-
if (mutex) {
84-
HRESULT hr = mutex->AcquireSync(0, 1000);
85-
if (hr == WAIT_TIMEOUT) {
86-
gfxDevCrash(LogReason::D3DLockTimeout) << "D3D lock mutex timeout";
87-
} else if (hr == WAIT_ABANDONED) {
88-
gfxCriticalNote << "GFX: D3D11 lock mutex abandoned";
89-
}
90-
if (FAILED(hr)) {
91-
NS_WARNING("Failed to lock the texture");
92-
return false;
93-
}
94-
}
95-
bool success = SubmitFrame(aLayer, dxTexture);
96-
if (mutex) {
97-
HRESULT hr = mutex->ReleaseSync(0);
98-
if (FAILED(hr)) {
99-
NS_WARNING("Failed to unlock the texture");
100-
}
101-
}
102-
if (!success) {
103-
return false;
104-
}
105-
return true;
138+
return SUCCEEDED(hr) && success;
106139
}
107140

108141
#elif defined(XP_MACOSX)

gfx/vr/service/VRSession.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ namespace gfx {
2121
class VRSession {
2222
public:
2323
VRSession();
24+
25+
// Since this class doesn't use smartpointers for its refcounted
26+
// members (so that it can compile outside of mozilla-central),
27+
// prevent copying the addresses without increasing the refcount.
28+
VRSession(const VRSession&) = delete;
29+
VRSession& operator=(const VRSession&) = delete;
30+
31+
#ifdef XP_WIN
32+
virtual ~VRSession();
33+
#else
2434
virtual ~VRSession() = default;
35+
#endif
2536

2637
virtual bool Initialize(mozilla::gfx::VRSystemState& aSystemState) = 0;
2738
virtual void Shutdown() = 0;
@@ -41,13 +52,16 @@ class VRSession {
4152
#if defined(XP_WIN)
4253
virtual bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
4354
ID3D11Texture2D* aTexture) = 0;
44-
bool CreateD3DContext(RefPtr<ID3D11Device> aDevice);
45-
RefPtr<ID3D11Device1> mDevice;
46-
RefPtr<ID3D11DeviceContext1> mContext;
55+
bool CreateD3DContext(ID3D11Device* aDevice);
56+
4757
ID3D11Device1* GetD3DDevice();
4858
ID3D11DeviceContext1* GetD3DDeviceContext();
4959
ID3DDeviceContextState* GetD3DDeviceContextState();
50-
RefPtr<ID3DDeviceContextState> mDeviceContextState;
60+
61+
ID3D11Device1* mDevice;
62+
ID3D11DeviceContext1* mContext;
63+
ID3DDeviceContextState* mDeviceContextState;
64+
5165
#elif defined(XP_MACOSX)
5266
virtual bool SubmitFrame(const mozilla::gfx::VRLayer_Stereo_Immersive& aLayer,
5367
const VRLayerTextureHandle& aTexture) = 0;

gfx/vr/vrhost/moz.build

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

77
SOURCES += [
8+
'/gfx/vr/service/VRSession.cpp',
89
'/gfx/vr/VRShMem.cpp',
910
'vrhostapi.cpp'
1011
]
@@ -19,28 +20,28 @@ if CONFIG['NIGHTLY_BUILD']:
1920
else:
2021
DEFFILE = 'vrhost.def'
2122

22-
LOCAL_INCLUDES += [
23-
'/gfx/vr',
24-
'/gfx/vr/external_api',
25-
'/gfx/vr/service',
26-
'/ipc/chromium/src',
23+
LOCAL_INCLUDES += [
24+
'/gfx/vr',
25+
'/gfx/vr/external_api',
26+
'/gfx/vr/service',
27+
'/ipc/chromium/src',
2728
]
2829

2930
EXPORTS.vrhost = [
30-
'vrhostex.h'
31+
'vrhostex.h'
3132
]
3233

3334

3435

3536
DIRS += [
36-
'testhost'
37+
'testhost'
3738
]
3839

3940
# this is Windows-only for now
4041
DEFINES['XP_WIN'] = True
41-
# fixes "lld-link: error: undefined symbol: __imp_moz_xmalloc"
42+
# fixes "lld-link: error: undefined symbol: __imp_moz_xmalloc"
4243
DEFINES['MOZ_NO_MOZALLOC'] = True
43-
# fixes "STL code can only be used with infallible ::operator new()"
44+
# fixes "STL code can only be used with infallible ::operator new()"
4445
DisableStlWrapping()
4546

4647
# Use SharedLibrary to generate the dll

0 commit comments

Comments
 (0)