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"
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"
18 #include "nsWindowsHelpers.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
37 struct IDCompositionDevice2
;
41 class ScopedGfxFeatureReporter
;
43 class DeviceAttachmentsD3D11
;
49 class DeviceManagerDx final
{
52 static void Shutdown();
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();
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
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
);
111 void InitializeDirectDraw();
113 // Reset and reacquire the devices if a reset has happened.
114 // Returns whether a reset occurred not whether reacquiring
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
);
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
);
168 void ReleaseD3D11() MOZ_REQUIRES(mDeviceLock
);
170 // Call GetDeviceRemovedReason on each device until one returns
172 bool GetAnyDeviceRemovedReason(DeviceResetReason
* aOutReason
)
173 MOZ_REQUIRES(mDeviceLock
);
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
;
207 } // namespace mozilla
209 #endif // mozilla_gfx_thebes_DeviceManagerDx_h