Clean up D3D11 device creation. (bug 1292680, r=mattwoodrow)
authorDavid Anderson <danderson@mozilla.com>
Sat, 13 Aug 2016 12:04:03 -0700
changeset 309373 b065da4afcf193e6d2ecd0897d9556a53258988a
parent 309372 f50b8c335e195a6c54f144a678fd0b7913d8dd8a
child 309374 6e3fb17d70ad6b8b478f2be3c9a7268fc45b83be
push id30561
push userkwierso@gmail.com
push dateMon, 15 Aug 2016 21:20:49 +0000
treeherdermozilla-central@91a319101587 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattwoodrow
bugs1292680
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Clean up D3D11 device creation. (bug 1292680, r=mattwoodrow)
gfx/thebes/DeviceManagerD3D11.cpp
gfx/thebes/DeviceManagerD3D11.h
--- a/gfx/thebes/DeviceManagerD3D11.cpp
+++ b/gfx/thebes/DeviceManagerD3D11.cpp
@@ -206,45 +206,29 @@ DeviceManagerD3D11::GetDXGIAdapter()
   }
 
   // We leak this module everywhere, we might as well do so here as well.
   dxgiModule.disown();
   return mAdapter;
 }
 
 bool
-DeviceManagerD3D11::AttemptD3D11DeviceCreationHelperInner(
-  IDXGIAdapter1* aAdapter, bool aAttemptVideoSupport, RefPtr<ID3D11Device>& aOutDevice, HRESULT& aResOut)
+DeviceManagerD3D11::AttemptD3D11DeviceCreationHelper(
+  FeatureState& aD3d11, IDXGIAdapter1* aAdapter, bool aAttemptVideoSupport, RefPtr<ID3D11Device>& aOutDevice)
 {
   // Use D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS
   // to prevent bug 1092260. IE 11 also uses this flag.
   UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS;
   if (aAttemptVideoSupport) {
     flags |= D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
   }
 
-  MOZ_SEH_TRY {
-    aResOut =
-      sD3D11CreateDeviceFn(
-        aAdapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, flags,
-        mFeatureLevels.Elements(), mFeatureLevels.Length(),
-        D3D11_SDK_VERSION, getter_AddRefs(aOutDevice), nullptr, nullptr);
-  } MOZ_SEH_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
-    return false;
-  }
-  return true;
-}
-
-bool
-DeviceManagerD3D11::AttemptD3D11DeviceCreationHelper(
-  FeatureState& aD3d11, IDXGIAdapter1* aAdapter, bool aAttemptVideoSupport, RefPtr<ID3D11Device>& aOutDevice)
-{
   HRESULT hr;
   RefPtr<ID3D11Device> device;
-  if (!AttemptD3D11DeviceCreationHelperInner(aAdapter, aAttemptVideoSupport, device, hr)) {
+  if (!CreateDevice(aAdapter, D3D_DRIVER_TYPE_UNKNOWN, flags, hr, device)) {
     if (!aAttemptVideoSupport) {
       gfxCriticalError() << "Crash during D3D11 device creation";
       aD3d11.SetFailed(FeatureStatus::CrashedInHandler, "Crashed trying to acquire a D3D11 device",
                        NS_LITERAL_CSTRING("FEATURE_FAILURE_D3D11_DEVICE1"));
     }
     return false;
   }
 
@@ -313,107 +297,92 @@ DeviceManagerD3D11::AttemptD3D11DeviceCr
   // It seems like this may only happen when we're using the NVIDIA gpu
   D3D11Checks::WarnOnAdapterMismatch(mCompositorDevice);
 
   mCompositorDevice->SetExceptionMode(0);
   mIsWARP = false;
 }
 
 bool
-DeviceManagerD3D11::AttemptWARPDeviceCreationHelper(
-  ScopedGfxFeatureReporter& aReporterWARP,
-  RefPtr<ID3D11Device>& aOutDevice,
-  HRESULT& aResOut)
+DeviceManagerD3D11::CreateDevice(IDXGIAdapter* aAdapter,
+                                 D3D_DRIVER_TYPE aDriverType,
+                                 UINT aFlags,
+                                 HRESULT& aResOut,
+                                 RefPtr<ID3D11Device>& aOutDevice)
 {
   MOZ_SEH_TRY {
-    aResOut =
-      sD3D11CreateDeviceFn(
-        nullptr, D3D_DRIVER_TYPE_WARP, nullptr,
-        // Use D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS
-        // to prevent bug 1092260. IE 11 also uses this flag.
-        D3D11_CREATE_DEVICE_BGRA_SUPPORT,
-        mFeatureLevels.Elements(), mFeatureLevels.Length(),
-        D3D11_SDK_VERSION, getter_AddRefs(aOutDevice), nullptr, nullptr);
-
-    aReporterWARP.SetSuccessful();
+    aResOut = sD3D11CreateDeviceFn(
+      aAdapter, aDriverType, nullptr,
+      aFlags,
+      mFeatureLevels.Elements(), mFeatureLevels.Length(),
+      D3D11_SDK_VERSION, getter_AddRefs(aOutDevice), nullptr, nullptr);
   } MOZ_SEH_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
     return false;
   }
   return true;
 }
 
 void
 DeviceManagerD3D11::AttemptWARPDeviceCreation()
 {
   ScopedGfxFeatureReporter reporterWARP("D3D11-WARP", gfxPrefs::LayersD3D11ForceWARP());
   FeatureState& d3d11 = gfxConfig::GetFeature(Feature::D3D11_COMPOSITING);
 
   HRESULT hr;
   RefPtr<ID3D11Device> device;
-  if (!AttemptWARPDeviceCreationHelper(reporterWARP, device, hr)) {
+
+  // Use D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS
+  // to prevent bug 1092260. IE 11 also uses this flag.
+  UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
+  if (!CreateDevice(nullptr, D3D_DRIVER_TYPE_WARP, flags, hr, device)) {
     gfxCriticalError() << "Exception occurred initializing WARP D3D11 device!";
     d3d11.SetFailed(FeatureStatus::CrashedInHandler, "Crashed creating a D3D11 WARP device",
                      NS_LITERAL_CSTRING("FEATURE_FAILURE_D3D11_WARP_DEVICE"));
-    return;
   }
 
   if (FAILED(hr) || !device) {
     // This should always succeed... in theory.
     gfxCriticalError() << "Failed to initialize WARP D3D11 device! " << hexa(hr);
     d3d11.SetFailed(FeatureStatus::Failed, "Failed to create a D3D11 WARP device",
                     NS_LITERAL_CSTRING("FEATURE_FAILURE_D3D11_WARP_DEVICE2"));
     return;
   }
 
   {
     MutexAutoLock lock(mDeviceLock);
     mCompositorDevice = device;
   }
+  reporterWARP.SetSuccessful();
 
   // Only test for texture sharing on Windows 8 since it puts the device into
   // an unusable state if used on Windows 7
   if (IsWin8OrLater()) {
     mTextureSharingWorks = D3D11Checks::DoesTextureSharingWork(mCompositorDevice);
   }
   mCompositorDevice->SetExceptionMode(0);
   mIsWARP = true;
 }
 
-bool
-DeviceManagerD3D11::AttemptD3D11ContentDeviceCreationHelper(
-  IDXGIAdapter1* aAdapter, RefPtr<ID3D11Device>& aOutDevice, HRESULT& aResOut)
-{
-  MOZ_SEH_TRY {
-    aResOut =
-      sD3D11CreateDeviceFn(
-        aAdapter, mIsWARP ? D3D_DRIVER_TYPE_WARP : D3D_DRIVER_TYPE_UNKNOWN,
-        nullptr, D3D11_CREATE_DEVICE_BGRA_SUPPORT,
-        mFeatureLevels.Elements(), mFeatureLevels.Length(),
-        D3D11_SDK_VERSION, getter_AddRefs(aOutDevice), nullptr, nullptr);
-
-  } MOZ_SEH_EXCEPT (EXCEPTION_EXECUTE_HANDLER) {
-    return false;
-  }
-  return true;
-}
-
 FeatureStatus
 DeviceManagerD3D11::AttemptD3D11ContentDeviceCreation()
 {
   RefPtr<IDXGIAdapter1> adapter;
   if (!mIsWARP) {
     adapter = GetDXGIAdapter();
     if (!adapter) {
       return FeatureStatus::Unavailable;
     }
   }
 
   HRESULT hr;
   RefPtr<ID3D11Device> device;
-  if (!AttemptD3D11ContentDeviceCreationHelper(adapter, device, hr)) {
+
+  UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
+  D3D_DRIVER_TYPE type = mIsWARP ? D3D_DRIVER_TYPE_WARP : D3D_DRIVER_TYPE_UNKNOWN;
+  if (!CreateDevice(adapter, type, flags, hr, device)) {
     gfxCriticalNote << "Recovered from crash while creating a D3D11 content device";
     gfxWindowsPlatform::RecordContentDeviceFailure(TelemetryDeviceCode::Content);
     return FeatureStatus::CrashedInHandler;
   }
 
   if (FAILED(hr) || !device) {
     gfxCriticalNote << "Failed to create a D3D11 content device: " << hexa(hr);
     gfxWindowsPlatform::RecordContentDeviceFailure(TelemetryDeviceCode::Content);
@@ -449,34 +418,16 @@ DeviceManagerD3D11::AttemptD3D11ContentD
   RefPtr<ID3D10Multithread> multi;
   hr = mContentDevice->QueryInterface(__uuidof(ID3D10Multithread), getter_AddRefs(multi));
   if (SUCCEEDED(hr) && multi) {
     multi->SetMultithreadProtected(TRUE);
   }
   return FeatureStatus::Available;
 }
 
-bool
-DeviceManagerD3D11::CreateD3D11DecoderDeviceHelper(
-  IDXGIAdapter1* aAdapter, RefPtr<ID3D11Device>& aDevice, HRESULT& aResOut)
-{
-  MOZ_SEH_TRY{
-    aResOut =
-      sD3D11CreateDeviceFn(
-        aAdapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr,
-        D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS | D3D11_CREATE_DEVICE_VIDEO_SUPPORT,
-        mFeatureLevels.Elements(), mFeatureLevels.Length(),
-        D3D11_SDK_VERSION, getter_AddRefs(aDevice), nullptr, nullptr);
-
-  } MOZ_SEH_EXCEPT(EXCEPTION_EXECUTE_HANDLER) {
-    return false;
-  }
-  return true;
-}
-
 RefPtr<ID3D11Device>
 DeviceManagerD3D11::CreateDecoderDevice()
 {
   if (mCompositorDevice && mCompositorDeviceSupportsVideo && !mDecoderDevice) {
     mDecoderDevice = mCompositorDevice;
 
     RefPtr<ID3D10Multithread> multi;
     mDecoderDevice->QueryInterface(__uuidof(ID3D10Multithread), getter_AddRefs(multi));
@@ -495,20 +446,22 @@ DeviceManagerD3D11::CreateDecoderDevice(
     return nullptr;
   }
 
   RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapter();
   if (!adapter) {
     return nullptr;
   }
 
+  HRESULT hr;
   RefPtr<ID3D11Device> device;
 
-  HRESULT hr;
-  if (!CreateD3D11DecoderDeviceHelper(adapter, device, hr)) {
+  UINT flags = D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS |
+               D3D11_CREATE_DEVICE_VIDEO_SUPPORT;
+  if (!CreateDevice(adapter, D3D_DRIVER_TYPE_UNKNOWN, flags, hr, device)) {
     return nullptr;
   }
   if (FAILED(hr) || !device || !D3D11Checks::DoesDeviceWork()) {
     return nullptr;
   }
 
   RefPtr<ID3D10Multithread> multi;
   device->QueryInterface(__uuidof(ID3D10Multithread), getter_AddRefs(multi));
--- a/gfx/thebes/DeviceManagerD3D11.h
+++ b/gfx/thebes/DeviceManagerD3D11.h
@@ -63,43 +63,31 @@ public:
   bool GetAnyDeviceRemovedReason(DeviceResetReason* aOutReason);
 
 private:
   IDXGIAdapter1 *GetDXGIAdapter();
 
   void DisableD3D11AfterCrash();
 
   void AttemptD3D11DeviceCreation(mozilla::gfx::FeatureState& d3d11);
-  bool AttemptD3D11DeviceCreationHelperInner(
-      IDXGIAdapter1* aAdapter,
-      bool aAttemptVideoSupport,
-      RefPtr<ID3D11Device>& aOutDevice,
-      HRESULT& aResOut);
   bool AttemptD3D11DeviceCreationHelper(
       mozilla::gfx::FeatureState& aD3d11,
       IDXGIAdapter1* aAdapter,
       bool aAttemptVideoSupport,
       RefPtr<ID3D11Device>& aOutDevice);
 
   void AttemptWARPDeviceCreation();
-  bool AttemptWARPDeviceCreationHelper(
-      mozilla::ScopedGfxFeatureReporter& aReporterWARP,
-      RefPtr<ID3D11Device>& aOutDevice,
-      HRESULT& aResOut);
 
   mozilla::gfx::FeatureStatus AttemptD3D11ContentDeviceCreation();
-  bool AttemptD3D11ContentDeviceCreationHelper(
-      IDXGIAdapter1* aAdapter,
-      RefPtr<ID3D11Device>& aOutDevice,
-      HRESULT& aResOut);
 
-  // Create a D3D11 device to be used for DXVA decoding.
-  bool CreateD3D11DecoderDeviceHelper(
-      IDXGIAdapter1* aAdapter, RefPtr<ID3D11Device>& aDevice,
-      HRESULT& aResOut);
+  bool CreateDevice(IDXGIAdapter* aAdapter,
+                    D3D_DRIVER_TYPE aDriverType,
+                    UINT aFlags,
+                    HRESULT& aResOut,
+                    RefPtr<ID3D11Device>& aOutDevice);
 
   bool ContentAdapterIsParentAdapter(ID3D11Device* device);
 
 private:
   static StaticAutoPtr<DeviceManagerD3D11> sInstance;
 
   mozilla::Mutex mDeviceLock;
   nsTArray<D3D_FEATURE_LEVEL> mFeatureLevels;