No bug - tagging 2af34b4c9adf8c8defd3251b569af9c38cc0a429 with FIREFOX_BETA_124_BASE...
[gecko.git] / gfx / thebes / DeviceManagerDx.h
blob9d127af3585b05c57de843d014e126a60fea2285
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 "gfxPlatform.h"
10 #include "gfxTelemetry.h"
11 #include "gfxTypes.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/Mutex.h"
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/StaticPtr.h"
16 #include "mozilla/gfx/GraphicsMessages.h"
17 #include "nsTArray.h"
18 #include "nsWindowsHelpers.h"
20 #include <windows.h>
21 #include <objbase.h>
23 #include <d3d11.h>
24 #include <dxgi.h>
25 #include <dxgi1_6.h>
27 // This header is available in the June 2010 SDK and in the Win8 SDK
28 #include <d3dcommon.h>
29 // Win 8.0 SDK types we'll need when building using older sdks.
30 #if !defined(D3D_FEATURE_LEVEL_11_1) // defined in the 8.0 SDK only
31 # define D3D_FEATURE_LEVEL_11_1 static_cast<D3D_FEATURE_LEVEL>(0xb100)
32 # define D3D_FL9_1_REQ_TEXTURE2D_U_OR_V_DIMENSION 2048
33 # define D3D_FL9_3_REQ_TEXTURE2D_U_OR_V_DIMENSION 4096
34 #endif
36 struct ID3D11Device;
37 struct IDCompositionDevice2;
38 struct IDirectDraw7;
40 namespace mozilla {
41 class ScopedGfxFeatureReporter;
42 namespace layers {
43 class DeviceAttachmentsD3D11;
44 } // namespace layers
46 namespace gfx {
47 class FeatureState;
49 class DeviceManagerDx final {
50 public:
51 static void Init();
52 static void Shutdown();
54 DeviceManagerDx();
55 ~DeviceManagerDx();
57 static DeviceManagerDx* Get() { return sInstance; }
59 RefPtr<ID3D11Device> GetCompositorDevice();
60 RefPtr<ID3D11Device> GetContentDevice();
61 RefPtr<ID3D11Device> GetCanvasDevice();
62 RefPtr<ID3D11Device> GetImageDevice();
63 RefPtr<IDCompositionDevice2> GetDirectCompositionDevice();
64 RefPtr<ID3D11Device> GetVRDevice();
65 RefPtr<ID3D11Device> CreateDecoderDevice(bool aHardwareWebRender);
66 RefPtr<ID3D11Device> CreateMediaEngineDevice();
67 IDirectDraw7* GetDirectDraw();
69 unsigned GetCompositorFeatureLevel() const;
70 bool TextureSharingWorks();
71 bool IsWARP();
72 bool CanUseNV12();
73 bool CanUseP010();
74 bool CanUseP016();
75 bool CanUseDComp();
77 // Returns true if we can create a texture with
78 // D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX and also
79 // upload texture data during the CreateTexture2D
80 // call. This crashes on some devices, so we might
81 // need to avoid it.
82 bool CanInitializeKeyedMutexTextures();
84 // Enumerate and return all outputs on the current adapter.
85 nsTArray<DXGI_OUTPUT_DESC1> EnumerateOutputs();
87 // find the IDXGIOutput with a description.Monitor matching
88 // 'monitor'; returns false if not found or some error occurred.
89 bool GetOutputFromMonitor(HMONITOR monitor, RefPtr<IDXGIOutput>* aOutOutput);
91 // Check if the current adapter supports hardware stretching
92 void CheckHardwareStretchingSupport(HwStretchingSupport& aRv);
94 bool CreateCompositorDevices();
95 void CreateContentDevices();
96 void CreateDirectCompositionDevice();
97 bool CreateCanvasDevice();
99 static HANDLE CreateDCompSurfaceHandle();
101 void GetCompositorDevices(
102 RefPtr<ID3D11Device>* aOutDevice,
103 RefPtr<layers::DeviceAttachmentsD3D11>* aOutAttachments);
105 void ImportDeviceInfo(const D3D11DeviceStatus& aDeviceStatus);
107 // Returns whether the device info was exported.
108 bool ExportDeviceInfo(D3D11DeviceStatus* aOut);
110 void ResetDevices();
111 void InitializeDirectDraw();
113 // Reset and reacquire the devices if a reset has happened.
114 // Returns whether a reset occurred not whether reacquiring
115 // was successful.
116 bool MaybeResetAndReacquireDevices();
118 // Device reset helpers.
119 bool HasDeviceReset(DeviceResetReason* aOutReason = nullptr);
121 // Note: these set the cached device reset reason, which will be picked up
122 // on the next frame.
123 void ForceDeviceReset(ForcedDeviceResetReason aReason);
125 private:
126 void ResetDevicesLocked() MOZ_REQUIRES(mDeviceLock);
128 // Device reset helpers.
129 bool HasDeviceResetLocked(DeviceResetReason* aOutReason = nullptr)
130 MOZ_REQUIRES(mDeviceLock);
132 // Pre-load any compositor resources that are expensive, and are needed when
133 // we attempt to create a compositor.
134 static void PreloadAttachmentsOnCompositorThread();
136 already_AddRefed<IDXGIAdapter1> GetDXGIAdapter();
137 IDXGIAdapter1* GetDXGIAdapterLocked() MOZ_REQUIRES(mDeviceLock);
139 void DisableD3D11AfterCrash();
141 bool CreateCompositorDevicesLocked() MOZ_REQUIRES(mDeviceLock);
142 void CreateContentDevicesLocked() MOZ_REQUIRES(mDeviceLock);
143 void CreateDirectCompositionDeviceLocked() MOZ_REQUIRES(mDeviceLock);
144 bool CreateCanvasDeviceLocked() MOZ_REQUIRES(mDeviceLock);
146 void CreateCompositorDevice(mozilla::gfx::FeatureState& d3d11)
147 MOZ_REQUIRES(mDeviceLock);
148 bool CreateCompositorDeviceHelper(mozilla::gfx::FeatureState& aD3d11,
149 IDXGIAdapter1* aAdapter,
150 bool aAttemptVideoSupport,
151 RefPtr<ID3D11Device>& aOutDevice)
152 MOZ_REQUIRES(mDeviceLock);
154 void CreateWARPCompositorDevice() MOZ_REQUIRES(mDeviceLock);
155 bool CreateVRDevice() MOZ_REQUIRES(mDeviceLock);
157 mozilla::gfx::FeatureStatus CreateContentDevice() MOZ_REQUIRES(mDeviceLock);
159 bool CreateDevice(IDXGIAdapter* aAdapter, D3D_DRIVER_TYPE aDriverType,
160 UINT aFlags, HRESULT& aResOut,
161 RefPtr<ID3D11Device>& aOutDevice) MOZ_REQUIRES(mDeviceLock);
163 bool ContentAdapterIsParentAdapter(ID3D11Device* device)
164 MOZ_REQUIRES(mDeviceLock);
166 bool LoadD3D11();
167 bool LoadDcomp();
168 void ReleaseD3D11() MOZ_REQUIRES(mDeviceLock);
170 // Call GetDeviceRemovedReason on each device until one returns
171 // a failure.
172 bool GetAnyDeviceRemovedReason(DeviceResetReason* aOutReason)
173 MOZ_REQUIRES(mDeviceLock);
175 private:
176 static StaticAutoPtr<DeviceManagerDx> sInstance;
178 // This is assigned during device creation. Afterwards, it is released if
179 // devices failed, and "forgotten" if devices succeeded (meaning, we leak
180 // the ref and unassign the module).
181 nsModuleHandle mD3D11Module;
183 nsModuleHandle mDcompModule;
185 mutable mozilla::Mutex mDeviceLock;
186 nsTArray<D3D_FEATURE_LEVEL> mFeatureLevels MOZ_GUARDED_BY(mDeviceLock);
187 RefPtr<IDXGIAdapter1> mAdapter MOZ_GUARDED_BY(mDeviceLock);
188 RefPtr<ID3D11Device> mCompositorDevice MOZ_GUARDED_BY(mDeviceLock);
189 RefPtr<ID3D11Device> mContentDevice MOZ_GUARDED_BY(mDeviceLock);
190 RefPtr<ID3D11Device> mCanvasDevice MOZ_GUARDED_BY(mDeviceLock);
191 RefPtr<ID3D11Device> mImageDevice MOZ_GUARDED_BY(mDeviceLock);
192 RefPtr<ID3D11Device> mVRDevice MOZ_GUARDED_BY(mDeviceLock);
193 RefPtr<ID3D11Device> mDecoderDevice MOZ_GUARDED_BY(mDeviceLock);
194 RefPtr<IDCompositionDevice2> mDirectCompositionDevice
195 MOZ_GUARDED_BY(mDeviceLock);
196 RefPtr<layers::DeviceAttachmentsD3D11> mCompositorAttachments
197 MOZ_GUARDED_BY(mDeviceLock);
198 bool mCompositorDeviceSupportsVideo MOZ_GUARDED_BY(mDeviceLock);
199 Maybe<D3D11DeviceStatus> mDeviceStatus MOZ_GUARDED_BY(mDeviceLock);
200 Maybe<DeviceResetReason> mDeviceResetReason MOZ_GUARDED_BY(mDeviceLock);
202 nsModuleHandle mDirectDrawDLL;
203 RefPtr<IDirectDraw7> mDirectDraw;
206 } // namespace gfx
207 } // namespace mozilla
209 #endif // mozilla_gfx_thebes_DeviceManagerDx_h