Bug 1789309 - Annotate and fix DeviceManagerDx::mDeviceLock. r=gfx-reviewers,lsalzman

Differential Revision: https://phabricator.services.mozilla.com/D156491
This commit is contained in:
Andrew Osmond 2022-09-06 10:59:40 +00:00
Родитель 342a3bda43
Коммит cb136ac720
2 изменённых файлов: 108 добавлений и 67 удалений

Просмотреть файл

@ -142,6 +142,9 @@ bool DeviceManagerDx::LoadDcomp() {
}
void DeviceManagerDx::ReleaseD3D11() {
#ifdef DEBUG
MutexAutoLock lock(mDeviceLock);
#endif
MOZ_ASSERT(!mCompositorDevice);
MOZ_ASSERT(!mContentDevice);
MOZ_ASSERT(!mVRDevice);
@ -270,6 +273,11 @@ static inline bool ProcessOwnsCompositor() {
#endif
bool DeviceManagerDx::CreateCompositorDevices() {
MutexAutoLock lock(mDeviceLock);
return CreateCompositorDevicesLocked();
}
bool DeviceManagerDx::CreateCompositorDevicesLocked() {
MOZ_ASSERT(ProcessOwnsCompositor());
FeatureState& d3d11 = gfxConfig::GetFeature(Feature::D3D11_COMPOSITING);
@ -332,7 +340,7 @@ bool DeviceManagerDx::CreateVRDevice() {
return false;
}
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapter();
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapterLocked();
if (!adapter) {
NS_WARNING("Failed to acquire a DXGI adapter for VR");
return false;
@ -355,6 +363,11 @@ bool DeviceManagerDx::CreateVRDevice() {
}
bool DeviceManagerDx::CreateCanvasDevice() {
MutexAutoLock lock(mDeviceLock);
return CreateCanvasDeviceLocked();
}
bool DeviceManagerDx::CreateCanvasDeviceLocked() {
MOZ_ASSERT(ProcessOwnsCompositor());
if (mCanvasDevice) {
@ -365,7 +378,7 @@ bool DeviceManagerDx::CreateCanvasDevice() {
return false;
}
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapter();
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapterLocked();
if (!adapter) {
NS_WARNING("Failed to acquire a DXGI adapter for Canvas");
return false;
@ -398,6 +411,11 @@ bool DeviceManagerDx::CreateCanvasDevice() {
}
void DeviceManagerDx::CreateDirectCompositionDevice() {
MutexAutoLock lock(mDeviceLock);
CreateDirectCompositionDeviceLocked();
}
void DeviceManagerDx::CreateDirectCompositionDeviceLocked() {
if (!gfxVars::UseWebRenderDCompWin()) {
return;
}
@ -458,10 +476,12 @@ HANDLE DeviceManagerDx::CreateDCompSurfaceHandle() {
void DeviceManagerDx::ImportDeviceInfo(const D3D11DeviceStatus& aDeviceStatus) {
MOZ_ASSERT(!ProcessOwnsCompositor());
MutexAutoLock lock(mDeviceLock);
mDeviceStatus = Some(aDeviceStatus);
}
bool DeviceManagerDx::ExportDeviceInfo(D3D11DeviceStatus* aOut) {
MutexAutoLock lock(mDeviceLock);
if (mDeviceStatus) {
*aOut = mDeviceStatus.value();
return true;
@ -471,6 +491,11 @@ bool DeviceManagerDx::ExportDeviceInfo(D3D11DeviceStatus* aOut) {
}
void DeviceManagerDx::CreateContentDevices() {
MutexAutoLock lock(mDeviceLock);
CreateContentDevicesLocked();
}
void DeviceManagerDx::CreateContentDevicesLocked() {
MOZ_ASSERT(gfxConfig::IsEnabled(Feature::D3D11_COMPOSITING));
if (!LoadD3D11()) {
@ -486,7 +511,12 @@ void DeviceManagerDx::CreateContentDevices() {
}
}
IDXGIAdapter1* DeviceManagerDx::GetDXGIAdapter() {
already_AddRefed<IDXGIAdapter1> DeviceManagerDx::GetDXGIAdapter() {
MutexAutoLock lock(mDeviceLock);
return do_AddRef(GetDXGIAdapterLocked());
}
IDXGIAdapter1* DeviceManagerDx::GetDXGIAdapterLocked() {
if (mAdapter) {
return mAdapter;
}
@ -650,7 +680,7 @@ void DeviceManagerDx::CreateCompositorDevice(FeatureState& d3d11) {
return;
}
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapter();
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapterLocked();
if (!adapter) {
d3d11.SetFailed(FeatureStatus::Unavailable,
"Failed to acquire a DXGI adapter",
@ -701,15 +731,12 @@ void DeviceManagerDx::CreateCompositorDevice(FeatureState& d3d11) {
uint32_t featureLevel = device->GetFeatureLevel();
auto formatOptions = D3D11Checks::FormatOptions(device);
{
MutexAutoLock lock(mDeviceLock);
mCompositorDevice = device;
mCompositorDevice = device;
int32_t sequenceNumber = GetNextDeviceCounter();
mDeviceStatus = Some(D3D11DeviceStatus(
false, textureSharingWorks, featureLevel, DxgiAdapterDesc::From(desc),
sequenceNumber, formatOptions));
}
int32_t sequenceNumber = GetNextDeviceCounter();
mDeviceStatus = Some(D3D11DeviceStatus(
false, textureSharingWorks, featureLevel, DxgiAdapterDesc::From(desc),
sequenceNumber, formatOptions));
mCompositorDevice->SetExceptionMode(0);
}
@ -805,15 +832,12 @@ void DeviceManagerDx::CreateWARPCompositorDevice() {
int featureLevel = device->GetFeatureLevel();
auto formatOptions = D3D11Checks::FormatOptions(device);
{
MutexAutoLock lock(mDeviceLock);
mCompositorDevice = device;
mCompositorDevice = device;
int32_t sequenceNumber = GetNextDeviceCounter();
mDeviceStatus = Some(D3D11DeviceStatus(
true, textureSharingWorks, featureLevel, DxgiAdapterDesc::From(desc),
sequenceNumber, formatOptions));
}
int32_t sequenceNumber = GetNextDeviceCounter();
mDeviceStatus = Some(D3D11DeviceStatus(
true, textureSharingWorks, featureLevel, DxgiAdapterDesc::From(desc),
sequenceNumber, formatOptions));
mCompositorDevice->SetExceptionMode(0);
reporterWARP.SetSuccessful();
@ -822,7 +846,7 @@ void DeviceManagerDx::CreateWARPCompositorDevice() {
FeatureStatus DeviceManagerDx::CreateContentDevice() {
RefPtr<IDXGIAdapter1> adapter;
if (!mDeviceStatus->isWARP()) {
adapter = GetDXGIAdapter();
adapter = GetDXGIAdapterLocked();
if (!adapter) {
gfxCriticalNote << "Could not get a DXGI adapter";
return FeatureStatus::Unavailable;
@ -865,10 +889,7 @@ FeatureStatus DeviceManagerDx::CreateContentDevice() {
MOZ_ASSERT(ok);
}
{
MutexAutoLock lock(mDeviceLock);
mContentDevice = device;
}
mContentDevice = device;
mContentDevice->SetExceptionMode(0);
RefPtr<ID3D10Multithread> multi;
@ -882,15 +903,13 @@ FeatureStatus DeviceManagerDx::CreateContentDevice() {
RefPtr<ID3D11Device> DeviceManagerDx::CreateDecoderDevice(
bool aHardwareWebRender) {
bool isAMD = false;
{
MutexAutoLock lock(mDeviceLock);
if (!mDeviceStatus) {
return nullptr;
}
isAMD = mDeviceStatus->adapter().VendorId == 0x1002;
MutexAutoLock lock(mDeviceLock);
if (!mDeviceStatus) {
return nullptr;
}
bool isAMD = mDeviceStatus->adapter().VendorId == 0x1002;
bool reuseDevice = false;
if (gfxVars::ReuseDecoderDevice()) {
reuseDevice = true;
@ -924,7 +943,7 @@ RefPtr<ID3D11Device> DeviceManagerDx::CreateDecoderDevice(
return nullptr;
}
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapter();
RefPtr<IDXGIAdapter1> adapter = GetDXGIAdapterLocked();
if (!adapter) {
return nullptr;
}
@ -961,6 +980,7 @@ static HRESULT SetDebugName(T* d3d11Object, const char* debugString) {
}
RefPtr<ID3D11Device> DeviceManagerDx::CreateMediaEngineDevice() {
MutexAutoLock lock(mDeviceLock);
if (!LoadD3D11()) {
return nullptr;
}
@ -988,7 +1008,10 @@ RefPtr<ID3D11Device> DeviceManagerDx::CreateMediaEngineDevice() {
void DeviceManagerDx::ResetDevices() {
MutexAutoLock lock(mDeviceLock);
ResetDevicesLocked();
}
void DeviceManagerDx::ResetDevicesLocked() {
mAdapter = nullptr;
mCompositorAttachments = nullptr;
mCompositorDevice = nullptr;
@ -1004,8 +1027,10 @@ void DeviceManagerDx::ResetDevices() {
}
bool DeviceManagerDx::MaybeResetAndReacquireDevices() {
MutexAutoLock lock(mDeviceLock);
DeviceResetReason resetReason;
if (!HasDeviceReset(&resetReason)) {
if (!HasDeviceResetLocked(&resetReason)) {
return false;
}
@ -1016,20 +1041,20 @@ bool DeviceManagerDx::MaybeResetAndReacquireDevices() {
bool createCanvasDevice = !!mCanvasDevice;
bool createDirectCompositionDevice = !!mDirectCompositionDevice;
ResetDevices();
ResetDevicesLocked();
if (createCompositorDevice && !CreateCompositorDevices()) {
// Just stop, don't try anything more
return true;
}
if (createContentDevice) {
CreateContentDevices();
CreateContentDevicesLocked();
}
if (createCanvasDevice) {
CreateCanvasDevice();
CreateCanvasDeviceLocked();
}
if (createDirectCompositionDevice) {
CreateDirectCompositionDevice();
CreateDirectCompositionDeviceLocked();
}
return true;
@ -1079,7 +1104,10 @@ static DeviceResetReason HResultToResetReason(HRESULT hr) {
bool DeviceManagerDx::HasDeviceReset(DeviceResetReason* aOutReason) {
MutexAutoLock lock(mDeviceLock);
return HasDeviceResetLocked(aOutReason);
}
bool DeviceManagerDx::HasDeviceResetLocked(DeviceResetReason* aOutReason) {
if (mDeviceResetReason) {
if (aOutReason) {
*aOutReason = mDeviceResetReason.value();
@ -1114,10 +1142,6 @@ static inline bool DidDeviceReset(const RefPtr<ID3D11Device>& aDevice,
}
bool DeviceManagerDx::GetAnyDeviceRemovedReason(DeviceResetReason* aOutReason) {
// Caller must own the lock, since we access devices directly, and can be
// called from any thread.
mDeviceLock.AssertCurrentThreadOwns();
if (DidDeviceReset(mCompositorDevice, aOutReason) ||
DidDeviceReset(mContentDevice, aOutReason) ||
DidDeviceReset(mCanvasDevice, aOutReason)) {
@ -1214,6 +1238,7 @@ RefPtr<IDCompositionDevice2> DeviceManagerDx::GetDirectCompositionDevice() {
}
unsigned DeviceManagerDx::GetCompositorFeatureLevel() const {
MutexAutoLock lock(mDeviceLock);
if (!mDeviceStatus) {
return 0;
}

Просмотреть файл

@ -130,30 +130,45 @@ class DeviceManagerDx final {
void ForceDeviceReset(ForcedDeviceResetReason aReason);
private:
void ResetDevicesLocked() MOZ_REQUIRES(mDeviceLock);
// Device reset helpers.
bool HasDeviceResetLocked(DeviceResetReason* aOutReason = nullptr)
MOZ_REQUIRES(mDeviceLock);
// Pre-load any compositor resources that are expensive, and are needed when
// we attempt to create a compositor.
static void PreloadAttachmentsOnCompositorThread();
IDXGIAdapter1* GetDXGIAdapter();
already_AddRefed<IDXGIAdapter1> GetDXGIAdapter();
IDXGIAdapter1* GetDXGIAdapterLocked() MOZ_REQUIRES(mDeviceLock);
void DisableD3D11AfterCrash();
void CreateCompositorDevice(mozilla::gfx::FeatureState& d3d11);
bool CreateCompositorDevicesLocked() MOZ_REQUIRES(mDeviceLock);
void CreateContentDevicesLocked() MOZ_REQUIRES(mDeviceLock);
void CreateDirectCompositionDeviceLocked() MOZ_REQUIRES(mDeviceLock);
bool CreateCanvasDeviceLocked() MOZ_REQUIRES(mDeviceLock);
void CreateCompositorDevice(mozilla::gfx::FeatureState& d3d11)
MOZ_REQUIRES(mDeviceLock);
bool CreateCompositorDeviceHelper(mozilla::gfx::FeatureState& aD3d11,
IDXGIAdapter1* aAdapter,
bool aAttemptVideoSupport,
RefPtr<ID3D11Device>& aOutDevice);
RefPtr<ID3D11Device>& aOutDevice)
MOZ_REQUIRES(mDeviceLock);
void CreateWARPCompositorDevice();
bool CreateVRDevice();
void CreateWARPCompositorDevice() MOZ_REQUIRES(mDeviceLock);
bool CreateVRDevice() MOZ_REQUIRES(mDeviceLock);
mozilla::gfx::FeatureStatus CreateContentDevice();
mozilla::gfx::FeatureStatus CreateContentDevice() MOZ_REQUIRES(mDeviceLock);
bool CreateDevice(IDXGIAdapter* aAdapter, D3D_DRIVER_TYPE aDriverType,
UINT aFlags, HRESULT& aResOut,
RefPtr<ID3D11Device>& aOutDevice);
RefPtr<ID3D11Device>& aOutDevice) MOZ_REQUIRES(mDeviceLock);
bool ContentAdapterIsParentAdapter(ID3D11Device* device);
bool ContentAdapterIsParentAdapter(ID3D11Device* device)
MOZ_REQUIRES(mDeviceLock);
bool LoadD3D11();
bool LoadDcomp();
@ -161,7 +176,8 @@ class DeviceManagerDx final {
// Call GetDeviceRemovedReason on each device until one returns
// a failure.
bool GetAnyDeviceRemovedReason(DeviceResetReason* aOutReason);
bool GetAnyDeviceRemovedReason(DeviceResetReason* aOutReason)
MOZ_REQUIRES(mDeviceLock);
private:
static StaticAutoPtr<DeviceManagerDx> sInstance;
@ -173,25 +189,25 @@ class DeviceManagerDx final {
nsModuleHandle mDcompModule;
mozilla::Mutex mDeviceLock MOZ_UNANNOTATED;
nsTArray<D3D_FEATURE_LEVEL> mFeatureLevels;
RefPtr<IDXGIAdapter1> mAdapter;
RefPtr<ID3D11Device> mCompositorDevice;
RefPtr<ID3D11Device> mContentDevice;
RefPtr<ID3D11Device> mCanvasDevice;
RefPtr<ID3D11Device> mImageDevice;
RefPtr<ID3D11Device> mVRDevice;
RefPtr<ID3D11Device> mDecoderDevice;
RefPtr<IDCompositionDevice2> mDirectCompositionDevice;
RefPtr<layers::DeviceAttachmentsD3D11> mCompositorAttachments;
bool mCompositorDeviceSupportsVideo;
Maybe<D3D11DeviceStatus> mDeviceStatus;
mutable mozilla::Mutex mDeviceLock;
nsTArray<D3D_FEATURE_LEVEL> mFeatureLevels MOZ_GUARDED_BY(mDeviceLock);
RefPtr<IDXGIAdapter1> mAdapter MOZ_GUARDED_BY(mDeviceLock);
RefPtr<ID3D11Device> mCompositorDevice MOZ_GUARDED_BY(mDeviceLock);
RefPtr<ID3D11Device> mContentDevice MOZ_GUARDED_BY(mDeviceLock);
RefPtr<ID3D11Device> mCanvasDevice MOZ_GUARDED_BY(mDeviceLock);
RefPtr<ID3D11Device> mImageDevice MOZ_GUARDED_BY(mDeviceLock);
RefPtr<ID3D11Device> mVRDevice MOZ_GUARDED_BY(mDeviceLock);
RefPtr<ID3D11Device> mDecoderDevice MOZ_GUARDED_BY(mDeviceLock);
RefPtr<IDCompositionDevice2> mDirectCompositionDevice
MOZ_GUARDED_BY(mDeviceLock);
RefPtr<layers::DeviceAttachmentsD3D11> mCompositorAttachments
MOZ_GUARDED_BY(mDeviceLock);
bool mCompositorDeviceSupportsVideo MOZ_GUARDED_BY(mDeviceLock);
Maybe<D3D11DeviceStatus> mDeviceStatus MOZ_GUARDED_BY(mDeviceLock);
Maybe<DeviceResetReason> mDeviceResetReason MOZ_GUARDED_BY(mDeviceLock);
nsModuleHandle mDirectDrawDLL;
RefPtr<IDirectDraw7> mDirectDraw;
Maybe<DeviceResetReason> mDeviceResetReason;
};
} // namespace gfx