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/. */
6 #ifndef mozilla_gfx_thebes_DeviceManagerDx_h
7 #define mozilla_gfx_thebes_DeviceManagerDx_h
11 #include "gfxPlatform.h"
12 #include "gfxTelemetry.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"
20 #include "nsWindowsHelpers.h"
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
39 struct IDCompositionDevice2
;
43 class ScopedGfxFeatureReporter
;
45 class DeviceAttachmentsD3D11
;
51 class DeviceManagerDx final
{
54 static void Shutdown();
59 static DeviceManagerDx
* Get() { return sInstance
; }
61 enum class DeviceFlag
{
62 isHardwareWebRenderInUse
,
65 using DeviceFlagSet
= EnumSet
<DeviceFlag
, uint8_t>;
66 RefPtr
<ID3D11Device
> GetCompositorDevice();
67 RefPtr
<ID3D11Device
> GetContentDevice();
68 RefPtr
<ID3D11Device
> GetCanvasDevice();
69 RefPtr
<ID3D11Device
> GetImageDevice();
70 RefPtr
<IDCompositionDevice2
> GetDirectCompositionDevice();
71 RefPtr
<ID3D11Device
> GetVRDevice();
72 RefPtr
<ID3D11Device
> CreateDecoderDevice(DeviceFlagSet aFlags
);
73 RefPtr
<ID3D11Device
> CreateMediaEngineDevice();
74 IDirectDraw7
* GetDirectDraw();
76 unsigned GetCompositorFeatureLevel() const;
77 bool TextureSharingWorks();
84 // Returns true if we can create a texture with
85 // D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX and also
86 // upload texture data during the CreateTexture2D
87 // call. This crashes on some devices, so we might
89 bool CanInitializeKeyedMutexTextures();
91 // Enumerate and return all outputs on the current adapter.
92 nsTArray
<DXGI_OUTPUT_DESC1
> EnumerateOutputs();
94 // find the IDXGIOutput with a description.Monitor matching
95 // 'monitor'; returns false if not found or some error occurred.
96 bool GetOutputFromMonitor(HMONITOR monitor
, RefPtr
<IDXGIOutput
>* aOutOutput
);
98 void PostUpdateMonitorInfo();
99 bool SystemHDREnabled();
101 // Check if the current adapter supports hardware stretching
102 void CheckHardwareStretchingSupport(HwStretchingSupport
& aRv
);
104 bool CreateCompositorDevices();
105 void CreateContentDevices();
106 void CreateDirectCompositionDevice();
107 bool CreateCanvasDevice();
109 static HANDLE
CreateDCompSurfaceHandle();
111 void GetCompositorDevices(
112 RefPtr
<ID3D11Device
>* aOutDevice
,
113 RefPtr
<layers::DeviceAttachmentsD3D11
>* aOutAttachments
);
115 void ImportDeviceInfo(const D3D11DeviceStatus
& aDeviceStatus
);
117 // Returns whether the device info was exported.
118 bool ExportDeviceInfo(D3D11DeviceStatus
* aOut
);
121 void InitializeDirectDraw();
123 // Reset and reacquire the devices if a reset has happened.
124 // Returns whether a reset occurred not whether reacquiring
126 bool MaybeResetAndReacquireDevices();
128 // Device reset helpers.
129 bool HasDeviceReset(DeviceResetReason
* aOutReason
= nullptr);
131 // Note: these set the cached device reset reason, which will be picked up
132 // on the next frame.
133 void ForceDeviceReset(ForcedDeviceResetReason aReason
);
136 void ResetDevicesLocked() MOZ_REQUIRES(mDeviceLock
);
138 // Device reset helpers.
139 bool HasDeviceResetLocked(DeviceResetReason
* aOutReason
= nullptr)
140 MOZ_REQUIRES(mDeviceLock
);
142 // Pre-load any compositor resources that are expensive, and are needed when
143 // we attempt to create a compositor.
144 static void PreloadAttachmentsOnCompositorThread();
146 already_AddRefed
<IDXGIAdapter1
> GetDXGIAdapter();
147 IDXGIAdapter1
* GetDXGIAdapterLocked() MOZ_REQUIRES(mDeviceLock
);
149 void DisableD3D11AfterCrash();
151 bool CreateCompositorDevicesLocked() MOZ_REQUIRES(mDeviceLock
);
152 void CreateContentDevicesLocked() MOZ_REQUIRES(mDeviceLock
);
153 void CreateDirectCompositionDeviceLocked() MOZ_REQUIRES(mDeviceLock
);
154 bool CreateCanvasDeviceLocked() MOZ_REQUIRES(mDeviceLock
);
156 void CreateCompositorDevice(mozilla::gfx::FeatureState
& d3d11
)
157 MOZ_REQUIRES(mDeviceLock
);
158 bool CreateCompositorDeviceHelper(mozilla::gfx::FeatureState
& aD3d11
,
159 IDXGIAdapter1
* aAdapter
,
160 bool aAttemptVideoSupport
,
161 RefPtr
<ID3D11Device
>& aOutDevice
)
162 MOZ_REQUIRES(mDeviceLock
);
164 void CreateWARPCompositorDevice() MOZ_REQUIRES(mDeviceLock
);
165 bool CreateVRDevice() MOZ_REQUIRES(mDeviceLock
);
167 mozilla::gfx::FeatureStatus
CreateContentDevice() MOZ_REQUIRES(mDeviceLock
);
169 bool CreateDevice(IDXGIAdapter
* aAdapter
, D3D_DRIVER_TYPE aDriverType
,
170 UINT aFlags
, HRESULT
& aResOut
,
171 RefPtr
<ID3D11Device
>& aOutDevice
) MOZ_REQUIRES(mDeviceLock
);
173 bool ContentAdapterIsParentAdapter(ID3D11Device
* device
)
174 MOZ_REQUIRES(mDeviceLock
);
178 void ReleaseD3D11() MOZ_REQUIRES(mDeviceLock
);
180 // Call GetDeviceRemovedReason on each device until one returns
182 bool GetAnyDeviceRemovedReason(DeviceResetReason
* aOutReason
)
183 MOZ_REQUIRES(mDeviceLock
);
185 void UpdateMonitorInfo();
186 std::vector
<DXGI_OUTPUT_DESC1
> GetOutputDescs();
189 static StaticAutoPtr
<DeviceManagerDx
> sInstance
;
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
;
196 nsModuleHandle mDcompModule
;
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 Maybe
<D3D11DeviceStatus
> mDeviceStatus
MOZ_GUARDED_BY(mDeviceLock
);
214 Maybe
<DeviceResetReason
> mDeviceResetReason
MOZ_GUARDED_BY(mDeviceLock
);
215 RefPtr
<Runnable
> mUpdateMonitorInfoRunnable
MOZ_GUARDED_BY(mDeviceLock
);
216 Maybe
<bool> mSystemHdrEnabled
MOZ_GUARDED_BY(mDeviceLock
);
218 nsModuleHandle mDirectDrawDLL
;
219 RefPtr
<IDirectDraw7
> mDirectDraw
;
223 } // namespace mozilla
225 #endif // mozilla_gfx_thebes_DeviceManagerDx_h