Skip to content

Commit

Permalink
修复设备在每次渲染都重复创建
Browse files Browse the repository at this point in the history
修复自定义渲染和动画无法一起使用的问题
  • Loading branch information
ALTaleX531 committed Aug 6, 2023
1 parent 1e81f4d commit 6168e09
Show file tree
Hide file tree
Showing 4 changed files with 369 additions and 264 deletions.
127 changes: 86 additions & 41 deletions TFMain/DXHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ BOOL WINAPI LazyDX::InternalHook::FreeLibrary(

auto f = [](PTP_CALLBACK_INSTANCE pci, PVOID)
{
// Wait for 10ms so that Kernel32!FreeLibrary can return safely to LazyD2D::FreeLibrary,
// Wait for 100ms so that Kernel32!FreeLibrary can return safely to LazyD2D::FreeLibrary,
// then go back to its caller, ending the function call
DisassociateCurrentThreadFromCallback(pci);
FreeLibraryWhenCallbackReturns(pci, HINST_THISCOMPONENT);
Sleep(10);
Sleep(100);
};
if (TrySubmitThreadpoolCallback(f, nullptr, nullptr))
{
Expand All @@ -56,8 +56,13 @@ void LazyDX::NotifyDeviceLost()
for (auto it = dxList.begin(); it != dxList.end(); it++)
{
auto& lazyDX{*it};
lazyDX->DestroyDeviceResources();
lazyDX->CreateDeviceResources();

try
{
lazyDX->DestroyDeviceResources();
lazyDX->CreateDeviceResources();
}
CATCH_LOG()
}
}

Expand Down Expand Up @@ -131,15 +136,7 @@ bool LazyD2D::EnsureInitialized()
{
auto& lazyD2D{GetInstance()};

if (lazyD2D.m_dcRT)
{
if (FAILED(lazyD2D.m_dcRT->Flush()))
{
LazyDX::NotifyDeviceLost();
}
}

return (lazyD2D.m_dcRT && lazyD2D.m_factory ? true : false);
return (LazyDComposition::EnsureInitialized() && lazyD2D.m_dcRT && lazyD2D.m_factory ? true : false);
}

LazyD2D::LazyD2D()
Expand Down Expand Up @@ -206,17 +203,88 @@ void LazyD2D::CreateDeviceResources()
LOG_CAUGHT_EXCEPTION();
}
}
void LazyD2D::DestroyDeviceIndependentResources()
void LazyD2D::DestroyDeviceIndependentResources() noexcept
{
m_factory.reset();
}
void LazyD2D::DestroyDeviceResources()
void LazyD2D::DestroyDeviceResources() noexcept
{
m_dcRT.reset();
}

/* ======================================================================================== */

LazyD3D& LazyD3D::GetInstance()
{
static LazyD3D instance{};
return instance;
}

bool LazyD3D::EnsureInitialized()
{
auto& lazyD3D{GetInstance()};

return (lazyD3D.m_d3dDevice && lazyD3D.m_dxgiDevice ? true : false);
}

LazyD3D::LazyD3D()
{
CreateDeviceIndependentResources();
CreateDeviceResources();
}

LazyD3D::~LazyD3D()
{
DestroyDeviceIndependentResources();
DestroyDeviceResources();
}

void LazyD3D::CreateDeviceIndependentResources()
{
}
void LazyD3D::CreateDeviceResources()
{
try
{
com_ptr<IDXGIDevice3> dxgiDevice{nullptr};
com_ptr<ID3D11Device> d3dDevice{nullptr};

THROW_IF_FAILED(
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
nullptr,
0,
D3D11_SDK_VERSION,
&d3dDevice,
nullptr,
nullptr
)
);
d3dDevice.query_to(&dxgiDevice);

d3dDevice.copy_to(&m_d3dDevice);
dxgiDevice.copy_to(&m_dxgiDevice);
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
}
}
void LazyD3D::DestroyDeviceIndependentResources() noexcept
{
}

void LazyD3D::DestroyDeviceResources() noexcept
{
m_d3dDevice.reset();
m_dxgiDevice.reset();
}

/* ======================================================================================== */

LazyDComposition& LazyDComposition::GetInstance()
{
static LazyDComposition instance{};
Expand Down Expand Up @@ -266,51 +334,28 @@ void LazyDComposition::CreateDeviceResources()
{
try
{
com_ptr<IDXGIDevice3> dxgiDevice{nullptr};
com_ptr<ID3D11Device> d3dDevice{nullptr};
com_ptr<IDCompositionDesktopDevice> dcompDevice{nullptr};

THROW_IF_FAILED(
D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
D3D11_CREATE_DEVICE_BGRA_SUPPORT,
nullptr,
0,
D3D11_SDK_VERSION,
&d3dDevice,
nullptr,
nullptr
)
);
d3dDevice.query_to(&dxgiDevice);

THROW_IF_FAILED(
DCompositionCreateDevice3(
dxgiDevice.get(),
m_lazyD3D.GetDxgiDevice().get(),
IID_PPV_ARGS(&dcompDevice)
)
);

dcompDevice.copy_to(&m_dcompDevice);
d3dDevice.copy_to(&m_d3dDevice);
dxgiDevice.copy_to(&m_dxgiDevice);
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
}
}
void LazyDComposition::DestroyDeviceIndependentResources()
void LazyDComposition::DestroyDeviceIndependentResources() noexcept
{
}

void LazyDComposition::DestroyDeviceResources()
void LazyDComposition::DestroyDeviceResources() noexcept
{
m_dcompDevice.reset();
m_d3dDevice.reset();
m_dxgiDevice.reset();
}

ColorF TranslucentFlyouts::DXHelper::MakeColorF(DWORD argb)
Expand Down
40 changes: 30 additions & 10 deletions TFMain/DXHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace TranslucentFlyouts

virtual void CreateDeviceIndependentResources() = 0;
virtual void CreateDeviceResources() = 0;
virtual void DestroyDeviceIndependentResources() = 0;
virtual void DestroyDeviceResources() = 0;
virtual void DestroyDeviceIndependentResources() noexcept = 0;
virtual void DestroyDeviceResources() noexcept = 0;
private:
struct InternalHook
{
Expand All @@ -57,6 +57,29 @@ namespace TranslucentFlyouts
static InternalHook g_internalHook;
};

class LazyD3D : public LazyDX
{
public:
static bool EnsureInitialized();
static LazyD3D& GetInstance();
~LazyD3D() noexcept;
LazyD3D(const LazyD3D&) = delete;
LazyD3D& operator=(const LazyD3D&) = delete;

wil::com_ptr<IDXGIDevice3> GetDxgiDevice() const { return m_dxgiDevice; };
wil::com_ptr<ID3D11Device> GetD3DDevice() const { return m_d3dDevice; };
protected:
void CreateDeviceIndependentResources() override;
void CreateDeviceResources() override;
void DestroyDeviceIndependentResources() noexcept override;
void DestroyDeviceResources() noexcept override;
private:
LazyD3D();

wil::com_ptr<IDXGIDevice3> m_dxgiDevice{nullptr};
wil::com_ptr<ID3D11Device> m_d3dDevice{nullptr};
};

class LazyD2D : public LazyDX
{
public:
Expand All @@ -71,8 +94,8 @@ namespace TranslucentFlyouts
protected:
void CreateDeviceIndependentResources() override;
void CreateDeviceResources() override;
void DestroyDeviceIndependentResources() override;
void DestroyDeviceResources() override;
void DestroyDeviceIndependentResources() noexcept override;
void DestroyDeviceResources() noexcept override;
private:
LazyD2D();

Expand All @@ -89,19 +112,16 @@ namespace TranslucentFlyouts
LazyDComposition(const LazyDComposition&) = delete;
LazyDComposition& operator=(const LazyDComposition&) = delete;

wil::com_ptr<IDXGIDevice3> GetDxgiDevice() const { return m_dxgiDevice; };
wil::com_ptr<ID3D11Device> GetD3DDevice() const { return m_d3dDevice; };
wil::com_ptr<IDCompositionDesktopDevice> GetDCompositionDevice() const { return m_dcompDevice; };
protected:
void CreateDeviceIndependentResources() override;
void CreateDeviceResources() override;
void DestroyDeviceIndependentResources() override;
void DestroyDeviceResources() override;
void DestroyDeviceIndependentResources() noexcept override;
void DestroyDeviceResources() noexcept override;
private:
LazyDComposition();

wil::com_ptr<IDXGIDevice3> m_dxgiDevice{nullptr};
wil::com_ptr<ID3D11Device> m_d3dDevice{nullptr};
LazyD3D& m_lazyD3D{LazyD3D::GetInstance()};
wil::com_ptr<IDCompositionDesktopDevice> m_dcompDevice{nullptr};
};

Expand Down
2 changes: 1 addition & 1 deletion TFMain/EffectHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace TranslucentFlyouts
ACCENT_ENABLE_BORDER_TOP = 1 << 6,
ACCENT_ENABLE_BORDER_RIGHT = 1 << 7,
ACCENT_ENABLE_BORDER_BOTTOM = 1 << 8,
ACCENT_ENABLE_BLUR_RECT = 1 << 9, // DwmpUpdateAccentBlurRect, it is confliected with ACCENT_ENABLE_GRADIENT_COLOR when using ACCENT_ENABLE_BLURBEHIND
ACCENT_ENABLE_BLUR_RECT = 1 << 9, // DwmpUpdateAccentBlurRect, it is conflicted with ACCENT_ENABLE_GRADIENT_COLOR when using ACCENT_ENABLE_BLURBEHIND
ACCENT_ENABLE_BORDER = ACCENT_ENABLE_BORDER_LEFT | ACCENT_ENABLE_BORDER_TOP | ACCENT_ENABLE_BORDER_RIGHT | ACCENT_ENABLE_BORDER_BOTTOM
};

Expand Down
Loading

0 comments on commit 6168e09

Please sign in to comment.