Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / gfx / thebes / DeviceManagerDx.h
blob780a23e6f86fd607c0a988406674d2dbda6f41a7
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
9 #include <vector>
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"
22 #include <windows.h>
23 #include <objbase.h>
25 #include <d3d11.h>
26 #include <dxgi.h>
27 #include <dxgi1_6.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
36 #endif
38 struct ID3D11Device;
39 struct IDCompositionDevice2;
40 struct IDirectDraw7;
42 namespace mozilla {
43 class ScopedGfxFeatureReporter;
44 namespace layers {
45 class DeviceAttachmentsD3D11;
46 } // namespace layers
48 namespace gfx {
49 class FeatureState;
51 class DeviceManagerDx final {
52 public:
53 static void Init();
54 static void Shutdown();
56 DeviceManagerDx();
57 ~DeviceManagerDx();
59 static DeviceManagerDx* Get() { return sInstance; }
61 enum class DeviceFlag {
62 isHardwareWebRenderInUse,
63 disableDeviceReuse,
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();
78 bool IsWARP();
79 bool CanUseNV12();
80 bool CanUseP010();
81 bool CanUseP016();
82 bool CanUseDComp();
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
88 // need to avoid it.
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);
120 void ResetDevices();
121 void InitializeDirectDraw();
123 // Reset and reacquire the devices if a reset has happened.
124 // Returns whether a reset occurred not whether reacquiring
125 // was successful.
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);
135 private:
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);
176 bool LoadD3D11();
177 bool LoadDcomp();
178 void ReleaseD3D11() MOZ_REQUIRES(mDeviceLock);
180 // Call GetDeviceRemovedReason on each device until one returns
181 // a failure.
182 bool GetAnyDeviceRemovedReason(DeviceResetReason* aOutReason)
183 MOZ_REQUIRES(mDeviceLock);
185 void UpdateMonitorInfo();
186 std::vector<DXGI_OUTPUT_DESC1> GetOutputDescs();
188 private:
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;
222 } // namespace gfx
223 } // namespace mozilla
225 #endif // mozilla_gfx_thebes_DeviceManagerDx_h