Skip to content

Commit

Permalink
重写部分功能的逻辑,优化注册表读入,修复渲染问题,引入动画
Browse files Browse the repository at this point in the history
  • Loading branch information
ALTaleX531 committed Aug 5, 2023
1 parent cb959e9 commit 336a7e9
Show file tree
Hide file tree
Showing 27 changed files with 2,147 additions and 1,250 deletions.
174 changes: 141 additions & 33 deletions TFMain/DXHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
#include "Hooking.hpp"
#include "DXHelper.hpp"

namespace TranslucentFlyouts::DXHelper
{
using namespace std;
using namespace D2D1;
using namespace std;
using namespace wil;
using namespace D2D1;
using namespace TranslucentFlyouts;
using namespace TranslucentFlyouts::DXHelper;

LazyDX::InternalHook LazyDX::g_internalHook{};
}
LazyDX::InternalHook LazyDX::g_internalHook{};

BOOL WINAPI TranslucentFlyouts::DXHelper::LazyDX::InternalHook::FreeLibrary(
BOOL WINAPI LazyDX::InternalHook::FreeLibrary(
HMODULE hLibModule
)
{
Expand Down Expand Up @@ -50,7 +50,7 @@ BOOL WINAPI TranslucentFlyouts::DXHelper::LazyDX::InternalHook::FreeLibrary(
return actualFreeLibrary(hLibModule);
}

void TranslucentFlyouts::DXHelper::LazyDX::NotifyDeviceLost()
void LazyDX::NotifyDeviceLost()
{
auto& dxList{g_internalHook.dxList};
for (auto it = dxList.begin(); it != dxList.end(); it++)
Expand All @@ -61,7 +61,7 @@ void TranslucentFlyouts::DXHelper::LazyDX::NotifyDeviceLost()
}
}

TranslucentFlyouts::DXHelper::LazyDX::InternalHook::InternalHook()
LazyDX::InternalHook::InternalHook()
{
actualFreeLibrary = reinterpret_cast<decltype(actualFreeLibrary)>(DetourFindFunction("kernel32.dll", "FreeLibrary"));
if (!actualFreeLibrary)
Expand All @@ -73,13 +73,13 @@ TranslucentFlyouts::DXHelper::LazyDX::InternalHook::InternalHook()
StartupHook();
}

TranslucentFlyouts::DXHelper::LazyDX::InternalHook::~InternalHook()
LazyDX::InternalHook::~InternalHook()
{
ShutdownHook();
actualFreeLibrary = nullptr;
}

void TranslucentFlyouts::DXHelper::LazyDX::InternalHook::StartupHook()
void LazyDX::InternalHook::StartupHook()
{
if (!hooked)
{
Expand All @@ -88,7 +88,7 @@ void TranslucentFlyouts::DXHelper::LazyDX::InternalHook::StartupHook()
}
}

void TranslucentFlyouts::DXHelper::LazyDX::InternalHook::ShutdownHook()
void LazyDX::InternalHook::ShutdownHook()
{
if (hooked)
{
Expand All @@ -97,12 +97,12 @@ void TranslucentFlyouts::DXHelper::LazyDX::InternalHook::ShutdownHook()
}
}

TranslucentFlyouts::DXHelper::LazyDX::LazyDX()
LazyDX::LazyDX()
{
g_internalHook.dxList.push_back(this);
}

TranslucentFlyouts::DXHelper::LazyDX::~LazyDX()
LazyDX::~LazyDX()
{
auto& dxList{g_internalHook.dxList};
for (auto it = dxList.begin(); it != dxList.end();)
Expand All @@ -121,13 +121,13 @@ TranslucentFlyouts::DXHelper::LazyDX::~LazyDX()

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

TranslucentFlyouts::DXHelper::LazyD2D& TranslucentFlyouts::DXHelper::LazyD2D::GetInstance()
LazyD2D& LazyD2D::GetInstance()
{
static LazyD2D instance{};
return instance;
}

bool TranslucentFlyouts::DXHelper::LazyD2D::EnsureInitialized()
bool LazyD2D::EnsureInitialized()
{
auto& lazyD2D{GetInstance()};

Expand All @@ -142,22 +142,28 @@ bool TranslucentFlyouts::DXHelper::LazyD2D::EnsureInitialized()
return (lazyD2D.m_dcRT && lazyD2D.m_factory ? true : false);
}

TranslucentFlyouts::DXHelper::LazyD2D::LazyD2D()
LazyD2D::LazyD2D()
{
CreateDeviceIndependentResources();
CreateDeviceResources();
}

void TranslucentFlyouts::DXHelper::LazyD2D::CreateDeviceIndependentResources()
LazyD2D::~LazyD2D()
{
DestroyDeviceIndependentResources();
DestroyDeviceResources();
}

void LazyD2D::CreateDeviceIndependentResources()
{
try
{
wil::com_ptr<ID2D1Factory> factory{nullptr};
com_ptr<ID2D1Factory> factory{nullptr};
THROW_IF_FAILED(
D2D1CreateFactory<ID2D1Factory>(
D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_MULTI_THREADED,
&factory
)
)
);

factory.copy_to(&m_factory);
Expand All @@ -167,7 +173,7 @@ void TranslucentFlyouts::DXHelper::LazyD2D::CreateDeviceIndependentResources()
LOG_CAUGHT_EXCEPTION();
}
}
void TranslucentFlyouts::DXHelper::LazyD2D::CreateDeviceResources()
void LazyD2D::CreateDeviceResources()
{
try
{
Expand All @@ -184,7 +190,7 @@ void TranslucentFlyouts::DXHelper::LazyD2D::CreateDeviceResources()
)
};

wil::com_ptr<ID2D1DCRenderTarget> dcRT{nullptr};
com_ptr<ID2D1DCRenderTarget> dcRT{nullptr};
THROW_IF_FAILED(
m_factory->CreateDCRenderTarget(
&properties,
Expand All @@ -193,29 +199,131 @@ void TranslucentFlyouts::DXHelper::LazyD2D::CreateDeviceResources()
);

dcRT->SetAntialiasMode(D2D1_ANTIALIAS_MODE::D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);

dcRT.copy_to(&m_dcRT);
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
}
}
void TranslucentFlyouts::DXHelper::LazyD2D::DestroyDeviceIndependentResources()
void LazyD2D::DestroyDeviceIndependentResources()
{
m_factory.reset();
}
void TranslucentFlyouts::DXHelper::LazyD2D::DestroyDeviceResources()
void LazyD2D::DestroyDeviceResources()
{
m_dcRT.reset();
}

D2D1::ColorF TranslucentFlyouts::DXHelper::COLORREF2ColorF(COLORREF color, std::byte alpha)
/* ======================================================================================== */

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

bool LazyDComposition::EnsureInitialized()
{
auto& lazyDComp{GetInstance()};

if (lazyDComp.m_dcompDevice)
{
try
{
BOOL valid{FALSE};
THROW_IF_FAILED(
lazyDComp.m_dcompDevice.query<IDCompositionDevice>()->CheckDeviceState(&valid)
);

if (!valid)
{
LazyDX::NotifyDeviceLost();
}
}
CATCH_LOG()
}

return (lazyDComp.m_dcompDevice ? true : false);
}

LazyDComposition::LazyDComposition()
{
return ColorF(
static_cast<float>(GetRValue(color)) / 255.f,
static_cast<float>(GetGValue(color)) / 255.f,
static_cast<float>(GetBValue(color)) / 255.f,
static_cast<float>(std::to_integer<int>(alpha)) / 255.f
);
}
CreateDeviceIndependentResources();
CreateDeviceResources();
}

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

void LazyDComposition::CreateDeviceIndependentResources()
{
}
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(),
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::DestroyDeviceResources()
{
m_dcompDevice.reset();
m_d3dDevice.reset();
m_dxgiDevice.reset();
}

ColorF TranslucentFlyouts::DXHelper::MakeColorF(DWORD argb)
{
auto a{static_cast<float>(argb >> 24) / 255.f};
auto r{static_cast<float>(argb >> 16 & 0xff) / 255.f};
auto g{static_cast<float>(argb >> 8 & 0xff) / 255.f};
auto b{static_cast<float>(argb & 0xff) / 255.f};

return ColorF{
r,
g,
b,
a
};
}
29 changes: 27 additions & 2 deletions TFMain/DXHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace TranslucentFlyouts
public:
static bool EnsureInitialized();
static LazyD2D& GetInstance();
~LazyD2D() noexcept = default;
~LazyD2D() noexcept;
LazyD2D(const LazyD2D&) = delete;
LazyD2D& operator=(const LazyD2D&) = delete;

Expand All @@ -80,6 +80,31 @@ namespace TranslucentFlyouts
wil::com_ptr<ID2D1Factory> m_factory{nullptr};
};

D2D1::ColorF COLORREF2ColorF(COLORREF color, std::byte alpha);
class LazyDComposition : public LazyDX
{
public:
static bool EnsureInitialized();
static LazyDComposition& GetInstance();
~LazyDComposition() noexcept;
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;
private:
LazyDComposition();

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

D2D1::ColorF MakeColorF(DWORD argb);
}
}
Loading

0 comments on commit 336a7e9

Please sign in to comment.