Skip to content

Commit

Permalink
v1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ALTaleX531 committed Oct 5, 2024
1 parent a0f22c0 commit 7e94ce8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 99 deletions.
30 changes: 15 additions & 15 deletions OpenGlass/GlassFramework.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace OpenGlass::GlassFramework
int g_roundRectRadius{};

UINT g_dwOverlayTestMode{};
winrt::com_ptr<IDCompositionVisual2> g_hackVisual{ nullptr };
}

HRGN WINAPI GlassFramework::MyCreateRoundRectRgn(int x1, int y1, int x2, int y2, int w, int h)
Expand All @@ -55,21 +56,12 @@ HRESULT STDMETHODCALLTYPE GlassFramework::MyCDrawGeometryInstruction_Create(uDwm
{
if (g_capturedWindow && g_capturedWindow->GetData()->GetHwnd() != uDwm::GetShellWindowForCurrentDesktop())
{
HRGN region{ GeometryRecorder::GetRegionFromGeometry(geometry) };
RECT rgnBox{};
if (GetRgnBox(region, &rgnBox) != NULLREGION && !IsRectEmpty(&rgnBox))
{
winrt::com_ptr<uDwm::CRgnGeometryProxy> rgnGeometry{ nullptr };
uDwm::ResourceHelper::CreateGeometryFromHRGN(region, rgnGeometry.put());

auto color =
Shared::g_forceAccentColorization ?
dwmcore::Convert_D2D1_COLOR_F_sRGB_To_D2D1_COLOR_F_scRGB(g_capturedWindow->TreatAsActiveWindow() ? Shared::g_accentColor : Shared::g_accentColorInactive) :
g_capturedWindow->GetTitlebarColorizationParameters()->getArgbcolor();
color.a = g_capturedWindow->TreatAsActiveWindow() ? 0.5f : 0.0f;
RETURN_IF_FAILED(reinterpret_cast<uDwm::CSolidColorLegacyMilBrushProxy*>(brush)->Update(1.0, color));
return g_CDrawGeometryInstruction_Create_Org(brush, rgnGeometry.get(), instruction);
}
auto color =
Shared::g_forceAccentColorization ?
dwmcore::Convert_D2D1_COLOR_F_sRGB_To_D2D1_COLOR_F_scRGB(g_capturedWindow->TreatAsActiveWindow() ? Shared::g_accentColor : Shared::g_accentColorInactive) :
g_capturedWindow->GetTitlebarColorizationParameters()->getArgbcolor();
color.a = g_capturedWindow->TreatAsActiveWindow() ? 0.5f : 0.0f;
RETURN_IF_FAILED(reinterpret_cast<uDwm::CSolidColorLegacyMilBrushProxy*>(brush)->Update(1.0, color));
}

return g_CDrawGeometryInstruction_Create_Org(brush, geometry, instruction);
Expand Down Expand Up @@ -349,6 +341,10 @@ void GlassFramework::UpdateConfiguration(ConfigurationFramework::UpdateType type
Shared::g_accentColor = Utils::FromAbgr(accentColor);
Shared::g_accentColorInactive = Utils::FromAbgr(accentColorInactive);
}

Shared::g_enableFullDirty = static_cast<bool>(ConfigurationFramework::DwmGetDwordFromHKCUAndHKLM(L"EnableFullDirty"));
Shared::g_enableFullDirty ? g_hackVisual.as<IDCompositionVisualDebug>()->EnableRedrawRegions() : g_hackVisual.as<IDCompositionVisualDebug>()->DisableRedrawRegions();
LOG_IF_FAILED(uDwm::CDesktopManager::s_pDesktopManagerInstance->GetDCompDevice()->Commit());
}

auto lock = wil::EnterCriticalSection(uDwm::CDesktopManager::s_csDwmInstance);
Expand Down Expand Up @@ -380,6 +376,8 @@ HRESULT GlassFramework::Startup()
g_dwOverlayTestMode = *dwmcore::CCommonRegistryData::m_dwOverlayTestMode;
*dwmcore::CCommonRegistryData::m_dwOverlayTestMode = 0x5;

RETURN_IF_FAILED(uDwm::CDesktopManager::s_pDesktopManagerInstance->GetDCompDevice()->CreateVisual(g_hackVisual.put()));

uDwm::GetAddressFromSymbolMap("CDrawGeometryInstruction::Create", g_CDrawGeometryInstruction_Create_Org);
uDwm::GetAddressFromSymbolMap("CTopLevelWindow::UpdateNCAreaBackground", g_CTopLevelWindow_UpdateNCAreaBackground_Org);
uDwm::GetAddressFromSymbolMap("CTopLevelWindow::UpdateClientBlur", g_CTopLevelWindow_UpdateClientBlur_Org);
Expand Down Expand Up @@ -456,4 +454,6 @@ void GlassFramework::Shutdown()
VisualManager::RedrawTopLevelWindow(window);
LOG_IF_FAILED(window->ValidateVisual());
}

g_hackVisual = nullptr;
}
119 changes: 36 additions & 83 deletions OpenGlass/GlassRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ using namespace OpenGlass;

namespace OpenGlass::GlassRenderer
{
bool STDMETHODCALLTYPE MyCSolidColorLegacyMilBrush_IsOfType(
dwmcore::CSolidColorLegacyMilBrush* This,
UINT type
);
HRESULT STDMETHODCALLTYPE MyCRenderData_TryDrawCommandAsDrawList(
dwmcore::CResource* This,
dwmcore::CDrawingContext* drawingContext,
Expand All @@ -21,19 +25,6 @@ namespace OpenGlass::GlassRenderer
dwmcore::CResource** resources,
bool* succeeded
);
HRESULT STDMETHODCALLTYPE MyCRenderData_DrawSolidColorRectangle(
dwmcore::CResource* This,
dwmcore::CDrawingContext* drawingContext,
dwmcore::CResource* drawListEntryBuilder,
bool unknwon,
const D2D1_RECT_F& lprc,
const D2D1_COLOR_F& color
);
HRESULT STDMETHODCALLTYPE MyCDrawingContext_DrawSolidRectangle(
dwmcore::IDrawingContext* This,
const D2D1_RECT_F& rectangle,
const D2D1_COLOR_F& color
);
HRESULT STDMETHODCALLTYPE MyCDrawingContext_DrawGeometry(
dwmcore::IDrawingContext* This,
dwmcore::CLegacyMilBrush* brush,
Expand All @@ -53,10 +44,8 @@ namespace OpenGlass::GlassRenderer
);
void STDMETHODCALLTYPE MyCGeometry_Destructor(dwmcore::CGeometry* This);

decltype(&MyCSolidColorLegacyMilBrush_IsOfType) g_CSolidColorLegacyMilBrush_IsOfType_Org{ nullptr };
decltype(&MyCRenderData_TryDrawCommandAsDrawList) g_CRenderData_TryDrawCommandAsDrawList_Org{ nullptr };
decltype(&MyCRenderData_DrawSolidColorRectangle) g_CRenderData_DrawSolidColorRectangle_Org{ nullptr };
decltype(&MyCDrawingContext_DrawSolidRectangle) g_CDrawingContext_DrawSolidRectangle_Org{ nullptr };
decltype(&MyCDrawingContext_DrawSolidRectangle)* g_CDrawingContext_DrawSolidRectangle_Org_Address{ nullptr };
decltype(&MyCDrawingContext_DrawGeometry) g_CDrawingContext_DrawGeometry_Org{ nullptr };
decltype(&MyCDrawingContext_DrawGeometry)* g_CDrawingContext_DrawGeometry_Org_Address{ nullptr };
decltype(&MyID2D1DeviceContext_FillGeometry) g_ID2D1DeviceContext_FillGeometry_Org{ nullptr };
Expand All @@ -65,10 +54,19 @@ namespace OpenGlass::GlassRenderer
decltype(&MyCGeometry_Destructor) g_CGeometry_Destructor_Org{ nullptr };
PVOID* g_CSolidColorLegacyMilBrush_vftable{ nullptr };

std::optional<D2D1_COLOR_F> g_drawColor{};
IGlassEffect* g_glassEffectNoRef{ nullptr };
ID2D1Device* g_deviceNoRef{ nullptr };
dwmcore::IDrawingContext* g_drawingContextNoRef{ nullptr };
bool g_solidColorLegacyMilBrush{ false };
}

bool STDMETHODCALLTYPE GlassRenderer::MyCSolidColorLegacyMilBrush_IsOfType(
dwmcore::CSolidColorLegacyMilBrush* This,
UINT type
)
{
g_solidColorLegacyMilBrush = true;
return g_CSolidColorLegacyMilBrush_IsOfType_Org(This, type);
}

HRESULT STDMETHODCALLTYPE GlassRenderer::MyCRenderData_TryDrawCommandAsDrawList(
Expand All @@ -82,69 +80,22 @@ HRESULT STDMETHODCALLTYPE GlassRenderer::MyCRenderData_TryDrawCommandAsDrawList(
bool* succeeded
)
{
g_drawColor = std::nullopt;
HRESULT hr{ g_CRenderData_TryDrawCommandAsDrawList_Org(This, drawingContext, drawListCache, drawListEntryBuilder, unknwon, commandType, resources, succeeded) };
if (SUCCEEDED(hr) && g_drawColor)
{
*succeeded = false;
}
return hr;
}
HRESULT STDMETHODCALLTYPE GlassRenderer::MyCRenderData_DrawSolidColorRectangle(
dwmcore::CResource* This,
dwmcore::CDrawingContext* drawingContext,
dwmcore::CResource* drawListEntryBuilder,
bool unknwon,
const D2D1_RECT_F& lprc,
const D2D1_COLOR_F& color
)
{
if (
color.a != 1.f &&
Shared::IsBackdropAllowed()
)
{
g_drawColor = color;
g_solidColorLegacyMilBrush = false;
auto hr = g_CRenderData_TryDrawCommandAsDrawList_Org(This, drawingContext, drawListCache, drawListEntryBuilder, unknwon, commandType, resources, succeeded);

if (!g_CDrawingContext_DrawSolidRectangle_Org)
{
g_CDrawingContext_DrawSolidRectangle_Org_Address = reinterpret_cast<decltype(g_CDrawingContext_DrawSolidRectangle_Org_Address)>(&(HookHelper::vtbl_of(drawingContext->GetInterface())[2]));
g_CDrawingContext_DrawSolidRectangle_Org = HookHelper::WritePointer(g_CDrawingContext_DrawSolidRectangle_Org_Address, MyCDrawingContext_DrawSolidRectangle);
}
if (SUCCEEDED(hr) && g_solidColorLegacyMilBrush && commandType == 461)
{
if (!g_CDrawingContext_DrawGeometry_Org)
{
g_CDrawingContext_DrawGeometry_Org_Address = reinterpret_cast<decltype(g_CDrawingContext_DrawGeometry_Org_Address)>(&(HookHelper::vtbl_of(drawingContext->GetInterface())[4]));
g_CDrawingContext_DrawGeometry_Org = HookHelper::WritePointer(g_CDrawingContext_DrawGeometry_Org_Address, MyCDrawingContext_DrawGeometry);
}
*succeeded = false;

return S_OK;
}

return g_CRenderData_DrawSolidColorRectangle_Org(
This,
drawingContext,
drawListEntryBuilder,
unknwon,
lprc,
color
);
}


HRESULT STDMETHODCALLTYPE GlassRenderer::MyCDrawingContext_DrawSolidRectangle(
dwmcore::IDrawingContext* This,
const D2D1_RECT_F& rectangle,
const D2D1_COLOR_F& color
)
{
if (!g_drawColor.has_value())
{
return g_CDrawingContext_DrawSolidRectangle_Org(This, rectangle, color);
}

D2D1_COLOR_F convertedColor{ dwmcore::Convert_D2D1_COLOR_F_scRGB_To_D2D1_COLOR_F_sRGB(g_drawColor.value()) };
g_drawColor = std::nullopt;
return g_CDrawingContext_DrawSolidRectangle_Org(This, rectangle, convertedColor);
return hr;
}

HRESULT STDMETHODCALLTYPE GlassRenderer::MyCDrawingContext_DrawGeometry(
Expand All @@ -156,15 +107,13 @@ HRESULT STDMETHODCALLTYPE GlassRenderer::MyCDrawingContext_DrawGeometry(
if (
!brush ||
!geometry ||
HookHelper::vtbl_of(brush) != g_CSolidColorLegacyMilBrush_vftable ||
!g_drawColor.has_value()
HookHelper::vtbl_of(brush) != g_CSolidColorLegacyMilBrush_vftable
)
{
return g_CDrawingContext_DrawGeometry_Org(This, brush, geometry);
}

auto cleanUp = wil::scope_exit([] { g_drawColor = std::nullopt; });
auto color = dwmcore::Convert_D2D1_COLOR_F_scRGB_To_D2D1_COLOR_F_sRGB(g_drawColor.value());
auto color = dwmcore::Convert_D2D1_COLOR_F_scRGB_To_D2D1_COLOR_F_sRGB(reinterpret_cast<dwmcore::CSolidColorLegacyMilBrush*>(brush)->GetRealizedColor());

// shape is nullptr or empty
dwmcore::CShapePtr geometryShape{};
Expand Down Expand Up @@ -296,6 +245,16 @@ HRESULT STDMETHODCALLTYPE GlassRenderer::MyCDirtyRegion__Add(
const D2D1_RECT_F& lprc
)
{
if (Shared::g_enableFullDirty)
{
return g_CDirtyRegion__Add_Org(
This,
visual,
unknown,
lprc
);
}

// at high blur radius, there is no need to extend that much,
// it will only cause severe flickering
float extendAmount{ min(Shared::g_blurAmount * 3.f + 0.5f, 15.5f) };
Expand Down Expand Up @@ -379,15 +338,15 @@ void GlassRenderer::UpdateConfiguration(ConfigurationFramework::UpdateType type)
HRESULT GlassRenderer::Startup()
{
dwmcore::GetAddressFromSymbolMap("CRenderData::TryDrawCommandAsDrawList", g_CRenderData_TryDrawCommandAsDrawList_Org);
dwmcore::GetAddressFromSymbolMap("CRenderData::DrawSolidColorRectangle", g_CRenderData_DrawSolidColorRectangle_Org);
dwmcore::GetAddressFromSymbolMap("CDirtyRegion::_Add", g_CDirtyRegion__Add_Org);
dwmcore::GetAddressFromSymbolMap("CGeometry::~CGeometry", g_CGeometry_Destructor_Org);
dwmcore::GetAddressFromSymbolMap("CSolidColorLegacyMilBrush::`vftable'", g_CSolidColorLegacyMilBrush_vftable);
dwmcore::GetAddressFromSymbolMap("CSolidColorLegacyMilBrush::IsOfType", g_CSolidColorLegacyMilBrush_IsOfType_Org);

return HookHelper::Detours::Write([]()
{
HookHelper::Detours::Attach(&g_CSolidColorLegacyMilBrush_IsOfType_Org, MyCSolidColorLegacyMilBrush_IsOfType);
HookHelper::Detours::Attach(&g_CRenderData_TryDrawCommandAsDrawList_Org, MyCRenderData_TryDrawCommandAsDrawList);
HookHelper::Detours::Attach(&g_CRenderData_DrawSolidColorRectangle_Org, MyCRenderData_DrawSolidColorRectangle);
HookHelper::Detours::Attach(&g_CDirtyRegion__Add_Org, MyCDirtyRegion__Add);
HookHelper::Detours::Attach(&g_CGeometry_Destructor_Org, MyCGeometry_Destructor);
});
Expand All @@ -397,8 +356,8 @@ void GlassRenderer::Shutdown()
{
HookHelper::Detours::Write([]()
{
HookHelper::Detours::Detach(&g_CSolidColorLegacyMilBrush_IsOfType_Org, MyCSolidColorLegacyMilBrush_IsOfType);
HookHelper::Detours::Detach(&g_CRenderData_TryDrawCommandAsDrawList_Org, MyCRenderData_TryDrawCommandAsDrawList);
HookHelper::Detours::Detach(&g_CRenderData_DrawSolidColorRectangle_Org, MyCRenderData_DrawSolidColorRectangle);
HookHelper::Detours::Detach(&g_CDirtyRegion__Add_Org, MyCDirtyRegion__Add);
HookHelper::Detours::Detach(&g_CGeometry_Destructor_Org, MyCGeometry_Destructor);
});
Expand All @@ -415,12 +374,6 @@ void GlassRenderer::Shutdown()
g_CDrawingContext_DrawGeometry_Org_Address = nullptr;
g_CDrawingContext_DrawGeometry_Org = nullptr;
}
if (g_CDrawingContext_DrawSolidRectangle_Org)
{
HookHelper::WritePointer(g_CDrawingContext_DrawSolidRectangle_Org_Address, g_CDrawingContext_DrawSolidRectangle_Org);
g_CDrawingContext_DrawSolidRectangle_Org_Address = nullptr;
g_CDrawingContext_DrawSolidRectangle_Org = nullptr;
}

GlassEffectFactory::Shutdown();
ReflectionEffect::Reset();
Expand Down
2 changes: 2 additions & 0 deletions OpenGlass/Shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ namespace OpenGlass::Shared

return true;
}

inline bool g_enableFullDirty{ false };
}
10 changes: 9 additions & 1 deletion OpenGlass/dwmcoreProjection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ namespace OpenGlass::dwmcore

typedef D2D1_MATRIX_5X4_F CMILMatrix;
struct CLegacyMilBrush : CResource {};
struct CSolidColorLegacyMilBrush : CLegacyMilBrush
{
const D2D1_COLOR_F& GetRealizedColor() const
{
return *reinterpret_cast<D2D1_COLOR_F*>(reinterpret_cast<ULONG_PTR>(this) + 88);
}
};

struct CTransform : CResource {};
struct CShape
{
Expand Down Expand Up @@ -577,13 +585,13 @@ namespace OpenGlass::dwmcore
fullyUnDecoratedFunctionName == "CChannel::CombinedGeometryUpdate" ||
fullyUnDecoratedFunctionName == "CResource::GetOwningProcessId" ||
fullyUnDecoratedFunctionName == "CRenderData::TryDrawCommandAsDrawList" ||
fullyUnDecoratedFunctionName == "CRenderData::DrawSolidColorRectangle" ||
fullyUnDecoratedFunctionName == "CGeometry::GetShapeData" ||
fullyUnDecoratedFunctionName == "CGeometry::~CGeometry" ||
fullyUnDecoratedFunctionName == "CDirtyRegion::SetFullDirty" ||
fullyUnDecoratedFunctionName == "CDirtyRegion::_Add" ||
fullyUnDecoratedFunctionName == "CVisual::GetHwnd" ||
fullyUnDecoratedFunctionName == "CD2DContext::GetClip" ||
fullyUnDecoratedFunctionName == "CSolidColorLegacyMilBrush::IsOfType" ||
(
fullyUnDecoratedFunctionName.starts_with("CDrawingContext::") &&
fullyUnDecoratedFunctionName != "CDrawingContext::IsOccluded"
Expand Down

0 comments on commit 7e94ce8

Please sign in to comment.