DeviceManagerDx.h (7915B)
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef mozilla_gfx_thebes_DeviceManagerDx_h 7 #define mozilla_gfx_thebes_DeviceManagerDx_h 8 9 #include <set> 10 11 #include "gfxPlatform.h" 12 #include "gfxTelemetry.h" 13 #include "gfxTypes.h" 14 #include "mozilla/Maybe.h" 15 #include "mozilla/Mutex.h" 16 #include "mozilla/RefPtr.h" 17 #include "mozilla/StaticPtr.h" 18 #include "mozilla/gfx/GraphicsMessages.h" 19 #include "nsTArray.h" 20 #include "nsWindowsHelpers.h" 21 22 #include <windows.h> 23 #include <objbase.h> 24 25 #include <d3d11.h> 26 #include <dxgi.h> 27 #include <dxgi1_6.h> 28 29 // This header is available in the June 2010 SDK and in the Win8 SDK 30 #include <d3dcommon.h> 31 // Win 8.0 SDK types we'll need when building using older sdks. 32 #if !defined(D3D_FEATURE_LEVEL_11_1) // defined in the 8.0 SDK only 33 # define D3D_FEATURE_LEVEL_11_1 static_cast<D3D_FEATURE_LEVEL>(0xb100) 34 # define D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION 2048 35 # define D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION 4096 36 #endif 37 38 struct ID3D11Device; 39 struct IDCompositionDevice2; 40 struct IDirectDraw7; 41 42 namespace mozilla { 43 class ScopedGfxFeatureReporter; 44 namespace layers { 45 class DeviceAttachmentsD3D11; 46 } // namespace layers 47 48 namespace gfx { 49 class FeatureState; 50 51 class DeviceManagerDx final { 52 public: 53 static void Init(); 54 static void Shutdown(); 55 56 DeviceManagerDx(); 57 ~DeviceManagerDx(); 58 59 static DeviceManagerDx* Get() { return sInstance; } 60 61 enum class DeviceFlag { 62 isHardwareWebRenderInUse, 63 }; 64 using DeviceFlagSet = EnumSet<DeviceFlag, uint8_t>; 65 RefPtr<ID3D11Device> GetCompositorDevice(); 66 RefPtr<ID3D11Device> GetContentDevice(); 67 RefPtr<ID3D11Device> GetCanvasDevice(); 68 RefPtr<ID3D11Device> GetImageDevice(); 69 RefPtr<IDCompositionDevice2> GetDirectCompositionDevice(); 70 RefPtr<ID3D11Device> GetVRDevice(); 71 RefPtr<ID3D11Device> CreateDecoderDevice(DeviceFlagSet aFlags); 72 RefPtr<ID3D11Device> CreateMediaEngineDevice(); 73 IDirectDraw7* GetDirectDraw(); 74 75 unsigned GetCompositorFeatureLevel() const; 76 bool TextureSharingWorks(); 77 bool IsWARP(); 78 bool IsWARPLocked() MOZ_REQUIRES(mDeviceLock); 79 bool CanUseNV12(); 80 bool CanUseP010(); 81 bool CanUseP016(); 82 bool CanUseDComp(); 83 bool CanUseDCompositionTexture(); 84 85 // Returns true if we can create a texture with 86 // D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX and also 87 // upload texture data during the CreateTexture2D 88 // call. This crashes on some devices, so we might 89 // need to avoid it. 90 bool CanInitializeKeyedMutexTextures(); 91 92 // Enumerate and return all outputs on the current adapter. 93 nsTArray<DXGI_OUTPUT_DESC1> EnumerateOutputs(); 94 95 // find the IDXGIOutput with a description.Monitor matching 96 // 'monitor'; returns false if not found or some error occurred. 97 bool GetOutputFromMonitor(HMONITOR monitor, RefPtr<IDXGIOutput>* aOutOutput); 98 99 void PostUpdateMonitorInfo(); 100 void UpdateMonitorInfo(); 101 bool SystemHDREnabled(); 102 bool WindowHDREnabled(HWND aWindow); 103 bool MonitorHDREnabled(HMONITOR aMonitor); 104 105 // Check if the current adapter supports hardware stretching 106 void CheckHardwareStretchingSupport(HwStretchingSupport& aRv); 107 108 bool CreateCompositorDevices(); 109 void CreateContentDevices(); 110 void CreateDirectCompositionDevice(); 111 bool CreateCanvasDevice(); 112 113 static HANDLE CreateDCompSurfaceHandle(); 114 115 void GetCompositorDevices( 116 RefPtr<ID3D11Device>* aOutDevice, 117 RefPtr<layers::DeviceAttachmentsD3D11>* aOutAttachments); 118 119 void ImportDeviceInfo(const D3D11DeviceStatus& aDeviceStatus); 120 121 // Returns whether the device info was exported. 122 bool ExportDeviceInfo(D3D11DeviceStatus* aOut); 123 124 void ResetDevices(); 125 126 // Reset and reacquire the devices if a reset has happened. 127 // Returns whether a reset occurred not whether reacquiring 128 // was successful. 129 bool MaybeResetAndReacquireDevices(); 130 131 // Device reset helpers. 132 bool HasDeviceReset(DeviceResetReason* aOutReason = nullptr); 133 134 // Note: these set the cached device reset reason, which will be picked up 135 // on the next frame. 136 void ForceDeviceReset(ForcedDeviceResetReason aReason); 137 138 private: 139 void ResetDevicesLocked() MOZ_REQUIRES(mDeviceLock); 140 141 // Device reset helpers. 142 bool HasDeviceResetLocked(DeviceResetReason* aOutReason = nullptr) 143 MOZ_REQUIRES(mDeviceLock); 144 145 // Pre-load any compositor resources that are expensive, and are needed when 146 // we attempt to create a compositor. 147 static void PreloadAttachmentsOnCompositorThread(); 148 149 already_AddRefed<IDXGIAdapter1> GetDXGIAdapter(); 150 IDXGIAdapter1* GetDXGIAdapterLocked() MOZ_REQUIRES(mDeviceLock); 151 152 void DisableD3D11AfterCrash(); 153 154 bool CreateCompositorDevicesLocked() MOZ_REQUIRES(mDeviceLock); 155 void CreateContentDevicesLocked() MOZ_REQUIRES(mDeviceLock); 156 void CreateDirectCompositionDeviceLocked() MOZ_REQUIRES(mDeviceLock); 157 bool CreateCanvasDeviceLocked() MOZ_REQUIRES(mDeviceLock); 158 159 void CreateCompositorDevice(mozilla::gfx::FeatureState& d3d11) 160 MOZ_REQUIRES(mDeviceLock); 161 bool CreateCompositorDeviceHelper(mozilla::gfx::FeatureState& aD3d11, 162 IDXGIAdapter1* aAdapter, 163 bool aAttemptVideoSupport, 164 RefPtr<ID3D11Device>& aOutDevice) 165 MOZ_REQUIRES(mDeviceLock); 166 167 void CreateWARPCompositorDevice() MOZ_REQUIRES(mDeviceLock); 168 bool CreateVRDevice() MOZ_REQUIRES(mDeviceLock); 169 170 mozilla::gfx::FeatureStatus CreateContentDevice() MOZ_REQUIRES(mDeviceLock); 171 172 bool CreateDevice(IDXGIAdapter* aAdapter, D3D_DRIVER_TYPE aDriverType, 173 UINT aFlags, HRESULT& aResOut, 174 RefPtr<ID3D11Device>& aOutDevice) MOZ_REQUIRES(mDeviceLock); 175 176 bool ContentAdapterIsParentAdapter(ID3D11Device* device) 177 MOZ_REQUIRES(mDeviceLock); 178 179 bool LoadD3D11(); 180 bool LoadDcomp(); 181 void ReleaseD3D11() MOZ_REQUIRES(mDeviceLock); 182 183 // Call GetDeviceRemovedReason on each device until one returns 184 // a failure. 185 bool GetAnyDeviceRemovedReason(DeviceResetReason* aOutReason) 186 MOZ_REQUIRES(mDeviceLock); 187 188 private: 189 static StaticAutoPtr<DeviceManagerDx> sInstance; 190 191 // This is assigned during device creation. Afterwards, it is released if 192 // devices failed, and "forgotten" if devices succeeded (meaning, we leak 193 // the ref and unassign the module). 194 nsModuleHandle mD3D11Module; 195 196 nsModuleHandle mDcompModule; 197 198 mutable mozilla::Mutex mDeviceLock; 199 nsTArray<D3D_FEATURE_LEVEL> mFeatureLevels MOZ_GUARDED_BY(mDeviceLock); 200 RefPtr<IDXGIAdapter1> mAdapter MOZ_GUARDED_BY(mDeviceLock); 201 RefPtr<IDXGIFactory1> mFactory MOZ_GUARDED_BY(mDeviceLock); 202 RefPtr<ID3D11Device> mCompositorDevice MOZ_GUARDED_BY(mDeviceLock); 203 RefPtr<ID3D11Device> mContentDevice MOZ_GUARDED_BY(mDeviceLock); 204 RefPtr<ID3D11Device> mCanvasDevice MOZ_GUARDED_BY(mDeviceLock); 205 RefPtr<ID3D11Device> mImageDevice MOZ_GUARDED_BY(mDeviceLock); 206 RefPtr<ID3D11Device> mVRDevice MOZ_GUARDED_BY(mDeviceLock); 207 RefPtr<ID3D11Device> mDecoderDevice MOZ_GUARDED_BY(mDeviceLock); 208 RefPtr<IDCompositionDevice2> mDirectCompositionDevice 209 MOZ_GUARDED_BY(mDeviceLock); 210 RefPtr<layers::DeviceAttachmentsD3D11> mCompositorAttachments 211 MOZ_GUARDED_BY(mDeviceLock); 212 bool mCompositorDeviceSupportsVideo MOZ_GUARDED_BY(mDeviceLock); 213 bool mSupportsDCompositionTexture MOZ_GUARDED_BY(mDeviceLock); 214 Maybe<D3D11DeviceStatus> mDeviceStatus MOZ_GUARDED_BY(mDeviceLock); 215 Maybe<DeviceResetReason> mDeviceResetReason MOZ_GUARDED_BY(mDeviceLock); 216 RefPtr<Runnable> mUpdateMonitorInfoRunnable MOZ_GUARDED_BY(mDeviceLock); 217 Maybe<bool> mSystemHdrEnabled MOZ_GUARDED_BY(mDeviceLock); 218 std::set<HMONITOR> mHdrMonitors MOZ_GUARDED_BY(mDeviceLock); 219 }; 220 221 } // namespace gfx 222 } // namespace mozilla 223 224 #endif // mozilla_gfx_thebes_DeviceManagerDx_h