Bug 1381085 - Part 2: Create new D3D11Device for WebVR draft
authorKearwood "Kip" Gilbert <kgilbert@mozilla.com>
Mon, 28 Aug 2017 15:56:43 -0700
changeset 654578 60a7177943548920101c7c95c9e985aaec18cc9b
parent 654577 34f3a329acaa744a2c3c977e87d52d952808c141
child 654579 31d0a7d4eaeaa7585293523a07dde1b72c94ba36
push id76606
push userkgilbert@mozilla.com
push dateTue, 29 Aug 2017 00:07:29 +0000
bugs1381085
milestone57.0a1
Bug 1381085 - Part 2: Create new D3D11Device for WebVR MozReview-Commit-ID: EewUkNxU3BI
gfx/thebes/DeviceManagerDx.cpp
gfx/thebes/DeviceManagerDx.h
--- a/gfx/thebes/DeviceManagerDx.cpp
+++ b/gfx/thebes/DeviceManagerDx.cpp
@@ -104,16 +104,17 @@ DeviceManagerDx::LoadD3D11()
   return true;
 }
 
 void
 DeviceManagerDx::ReleaseD3D11()
 {
   MOZ_ASSERT(!mCompositorDevice);
   MOZ_ASSERT(!mContentDevice);
+  MOZ_ASSERT(!mVRDevice);
   MOZ_ASSERT(!mDecoderDevice);
 
   mD3D11Module.reset();
   sD3D11CreateDeviceFn = nullptr;
 }
 
 static inline bool
 ProcessOwnsCompositor()
@@ -164,16 +165,51 @@ DeviceManagerDx::CreateCompositorDevices
   if (!d3d11.IsEnabled()) {
     return false;
   }
 
   PreloadAttachmentsOnCompositorThread();
   return true;
 }
 
+bool
+DeviceManagerDx::CreateVRDevice()
+{
+  MOZ_ASSERT(ProcessOwnsCompositor());
+
+  if (mVRDevice) {
+    return true;
+  }
+
+  if (!LoadD3D11()) {
+    return false;
+  }
+
+  RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapter();
+  if (!adapter) {
+    NS_WARNING("Failed to acquire a DXGI adapter for VR");
+    return false;
+  }
+
+  UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
+
+  HRESULT hr;
+  if (!CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, flags, hr, mVRDevice)) {
+    gfxCriticalError() << "Crash during D3D11 device creation for VR";
+    return false;
+  }
+
+  if (FAILED(hr) || !mVRDevice) {
+    NS_WARNING("Failed to acquire a D3D11 device for VR");
+    return false;
+  }
+
+  return true;
+}
+
 void
 DeviceManagerDx::ImportDeviceInfo(const D3D11DeviceStatus& aDeviceStatus)
 {
   MOZ_ASSERT(!ProcessOwnsCompositor());
 
   mDeviceStatus = Some(aDeviceStatus);
 }
 
@@ -933,16 +969,26 @@ DeviceManagerDx::GetCompositorDevice()
 
 RefPtr<ID3D11Device>
 DeviceManagerDx::GetContentDevice()
 {
   MutexAutoLock lock(mDeviceLock);
   return mContentDevice;
 }
 
+RefPtr<ID3D11Device>
+DeviceManagerDx::GetVRDevice()
+{
+  MutexAutoLock lock(mDeviceLock);
+  if(!mVRDevice) {
+    CreateVRDevice();
+  }
+  return mVRDevice;
+}
+
 unsigned
 DeviceManagerDx::GetCompositorFeatureLevel() const
 {
   if (!mDeviceStatus) {
     return 0;
   }
   return mDeviceStatus->featureLevel();
 }
--- a/gfx/thebes/DeviceManagerDx.h
+++ b/gfx/thebes/DeviceManagerDx.h
@@ -52,16 +52,17 @@ public:
   DeviceManagerDx();
 
   static DeviceManagerDx* Get() {
     return sInstance;
   }
 
   RefPtr<ID3D11Device> GetCompositorDevice();
   RefPtr<ID3D11Device> GetContentDevice();
+  RefPtr<ID3D11Device> GetVRDevice();
   RefPtr<ID3D11Device> CreateDecoderDevice();
   RefPtr<layers::MLGDevice> GetMLGDevice();
   IDirectDraw7* GetDirectDraw();
 
   unsigned GetCompositorFeatureLevel() const;
   bool TextureSharingWorks();
   bool IsWARP();
 
@@ -117,16 +118,17 @@ private:
   bool CreateCompositorDeviceHelper(
       mozilla::gfx::FeatureState& aD3d11,
       IDXGIAdapter1* aAdapter,
       bool aAttemptVideoSupport,
       RefPtr<ID3D11Device>& aOutDevice);
 
   void CreateWARPCompositorDevice();
   void CreateMLGDevice();
+  bool CreateVRDevice();
 
   mozilla::gfx::FeatureStatus CreateContentDevice();
 
   bool CreateDevice(IDXGIAdapter* aAdapter,
                     D3D_DRIVER_TYPE aDriverType,
                     UINT aFlags,
                     HRESULT& aResOut,
                     RefPtr<ID3D11Device>& aOutDevice);
@@ -148,16 +150,17 @@ private:
   // the ref and unassign the module).
   nsModuleHandle mD3D11Module;
 
   mozilla::Mutex mDeviceLock;
   nsTArray<D3D_FEATURE_LEVEL> mFeatureLevels;
   RefPtr<IDXGIAdapter1> mAdapter;
   RefPtr<ID3D11Device> mCompositorDevice;
   RefPtr<ID3D11Device> mContentDevice;
+  RefPtr<ID3D11Device> mVRDevice;
   RefPtr<ID3D11Device> mDecoderDevice;
   RefPtr<layers::DeviceAttachmentsD3D11> mCompositorAttachments;
   RefPtr<layers::MLGDevice> mMLGDevice;
   bool mCompositorDeviceSupportsVideo;
 
   Maybe<D3D11DeviceStatus> mDeviceStatus;
 
   nsModuleHandle mDirectDrawDLL;