1212# include < d3d11.h>
1313#endif // defined(XP_WIN)
1414
15+ #ifndef MOZILLA_INTERNAL_API
16+ # define NS_WARNING (s )
17+ #endif
18+
1519using 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() {
6596bool 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)
0 commit comments