This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlassReflectionBackdrop.hpp
218 lines (191 loc) · 8.25 KB
/
GlassReflectionBackdrop.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#pragma once
#include "DCompBackdrop.hpp"
namespace AcrylicEverywhere
{
struct CGlassReflectionResources : CDCompResourcesBase
{
winrt::com_ptr<DCompPrivates::IDCompositionDesktopDevicePartner> interopDCompDevice{ nullptr };
winrt::Windows::UI::Composition::CompositionDrawingSurface drawingSurface{ nullptr };
float parallaxIntensity{ 0.1f };
float glassIntensity{ 0.8f };
HRESULT STDMETHODCALLTYPE EnsureGlassSurface() try
{
auto is_device_valid = [&]()
{
if (!interopDCompDevice) return false;
BOOL valid{ FALSE };
THROW_IF_FAILED(
interopDCompDevice.as<IDCompositionDevice>()->CheckDeviceState(
&valid
)
);
return valid == TRUE;
};
if (!is_device_valid())
{
interopDCompDevice.copy_from(
uDwmPrivates::CDesktopManager::s_pDesktopManagerInstance->GetDCompositionInteropDevice()
);
auto compositor{ interopDCompDevice.as<winrt::Windows::UI::Composition::Compositor>() };
winrt::Windows::UI::Composition::CompositionGraphicsDevice graphicsDevice{ nullptr };
THROW_IF_FAILED(
compositor.as<ABI::Windows::UI::Composition::ICompositorInterop>()->CreateGraphicsDevice(
uDwmPrivates::CDesktopManager::s_pDesktopManagerInstance->GetD2DDevice(),
reinterpret_cast<ABI::Windows::UI::Composition::ICompositionGraphicsDevice**>(winrt::put_abi(graphicsDevice))
)
);
WCHAR filePath[MAX_PATH + 1]{};
GetModuleFileName(wil::GetModuleInstanceHandle(), filePath, MAX_PATH);
PathCchRemoveFileSpec(filePath, MAX_PATH);
PathCchAppend(filePath, MAX_PATH, L"data\\glass.png");
wil::unique_hfile file{ CreateFileW(filePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0) };
THROW_LAST_ERROR_IF(!file.is_valid());
LARGE_INTEGER fileSize{};
THROW_IF_WIN32_BOOL_FALSE(GetFileSizeEx(file.get(), &fileSize));
auto buffer{ std::make_unique<BYTE[]>(static_cast<size_t>(fileSize.QuadPart)) };
THROW_IF_WIN32_BOOL_FALSE(ReadFile(file.get(), buffer.get(), static_cast<DWORD>(fileSize.QuadPart), nullptr, nullptr));
winrt::com_ptr<IStream> stream{ SHCreateMemStream(buffer.get(), static_cast<UINT>(fileSize.QuadPart)), winrt::take_ownership_from_abi };
THROW_LAST_ERROR_IF_NULL(stream);
winrt::com_ptr<IWICImagingFactory2> wicFactory{ nullptr };
wicFactory.copy_from(uDwmPrivates::CDesktopManager::s_pDesktopManagerInstance->GetWICFactory());
winrt::com_ptr<IWICBitmapDecoder> wicDecoder{ nullptr };
THROW_IF_FAILED(wicFactory->CreateDecoderFromStream(stream.get(), &GUID_VendorMicrosoft, WICDecodeMetadataCacheOnDemand, wicDecoder.put()));
winrt::com_ptr<IWICBitmapFrameDecode> wicFrame{ nullptr };
THROW_IF_FAILED(wicDecoder->GetFrame(0, wicFrame.put()));
winrt::com_ptr<IWICFormatConverter> wicConverter{ nullptr };
THROW_IF_FAILED(wicFactory->CreateFormatConverter(wicConverter.put()));
winrt::com_ptr<IWICPalette> wicPalette{ nullptr };
THROW_IF_FAILED(
wicConverter->Initialize(
wicFrame.get(),
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
wicPalette.get(),
0, WICBitmapPaletteTypeCustom
)
);
winrt::com_ptr<IWICBitmap> wicBitmap{ nullptr };
THROW_IF_FAILED(wicFactory->CreateBitmapFromSource(wicConverter.get(), WICBitmapCreateCacheOption::WICBitmapNoCache, wicBitmap.put()));
UINT width{ 0 }, height{ 0 };
THROW_IF_FAILED(wicBitmap->GetSize(&width, &height));
drawingSurface = graphicsDevice.CreateDrawingSurface(
{ static_cast<float>(width), static_cast<float>(height) },
winrt::Windows::Graphics::DirectX::DirectXPixelFormat::B8G8R8A8UIntNormalized,
winrt::Windows::Graphics::DirectX::DirectXAlphaMode::Premultiplied
);
auto drawingSurfaceInterop{ drawingSurface.as<ABI::Windows::UI::Composition::ICompositionDrawingSurfaceInterop>() };
POINT offset = { 0, 0 };
winrt::com_ptr<ID2D1DeviceContext> d2dContext{ nullptr };
THROW_IF_FAILED(
drawingSurfaceInterop->BeginDraw(nullptr, IID_PPV_ARGS(d2dContext.put()), &offset)
);
d2dContext->Clear();
winrt::com_ptr<ID2D1Bitmap1> d2dBitmap{ nullptr };
d2dContext->CreateBitmapFromWicBitmap(
wicBitmap.get(),
D2D1::BitmapProperties1(
D2D1_BITMAP_OPTIONS_NONE,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)
),
d2dBitmap.put()
);
d2dContext->DrawBitmap(d2dBitmap.get());
THROW_IF_FAILED(
drawingSurfaceInterop->EndDraw()
);
return S_OK;
}
return S_OK;
}
CATCH_RETURN()
};
struct CGlassReflectionBackdrop : CDCompBackdropBase
{
inline static CGlassReflectionResources s_sharedResources{};
winrt::Windows::Foundation::Numerics::float2 fixedOffset{};
RECT currentWindowRect{};
RECT currentMonitorRect{};
HMONITOR currentMonitor{ nullptr };
winrt::Windows::UI::Composition::CompositionSurfaceBrush glassSurfaceBrush{ nullptr };
STDMETHOD(Initialize)(
winrt::Windows::UI::Composition::VisualCollection& visualCollection
) override try
{
THROW_IF_FAILED(CDCompBackdropBase::Initialize(visualCollection));
THROW_IF_FAILED(s_sharedResources.EnsureGlassSurface());
glassSurfaceBrush = spriteVisual.Compositor().CreateSurfaceBrush(s_sharedResources.drawingSurface);
glassSurfaceBrush.Stretch(winrt::Windows::UI::Composition::CompositionStretch::None);
glassSurfaceBrush.HorizontalAlignmentRatio(0.f);
glassSurfaceBrush.VerticalAlignmentRatio(0.f);
spriteVisual.Brush(glassSurfaceBrush);
spriteVisual.Opacity(s_sharedResources.glassIntensity);
return S_OK;
}
CATCH_RETURN()
HRESULT STDMETHODCALLTYPE Update(uDwmPrivates::CTopLevelWindow* window) try
{
THROW_HR_IF_NULL(E_POINTER, s_sharedResources.drawingSurface);
RECT windowRect{};
THROW_HR_IF_NULL(E_INVALIDARG, window->GetActualWindowRect(&windowRect, false, true, false));
HWND hwnd{ window->GetWindowData()->GetHwnd() };
HMONITOR monitor{ MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST) };
THROW_LAST_ERROR_IF_NULL(monitor);
RECT monitorRect{};
MONITORINFO mi{ sizeof(MONITORINFO) };
THROW_IF_WIN32_BOOL_FALSE(GetMonitorInfoW(monitor, &mi));
auto surfaceSize{ s_sharedResources.drawingSurface.SizeInt32() };
if (currentMonitor != monitor || !EqualRect(¤tMonitorRect, &mi.rcMonitor))
{
auto scaleFactor
{
[&]()
{
float factor{1.f};
auto scaledWidth{ static_cast<float>(surfaceSize.Width) * static_cast<float>(wil::rect_height(mi.rcMonitor)) * (1 + s_sharedResources.parallaxIntensity) / static_cast<float>(surfaceSize.Height) };
factor = scaledWidth / static_cast<float>(surfaceSize.Width);
if (scaledWidth < static_cast<float>(wil::rect_width(mi.rcMonitor)) * (1 + s_sharedResources.parallaxIntensity))
{
scaledWidth = static_cast<float>(wil::rect_width(mi.rcMonitor)) * (1 + s_sharedResources.parallaxIntensity);
factor = scaledWidth / static_cast<float>(surfaceSize.Width);
}
return factor;
} ()
};
glassSurfaceBrush.Scale(
winrt::Windows::Foundation::Numerics::float2{ scaleFactor, scaleFactor }
);
winrt::Windows::Foundation::Numerics::float2 scaledSize
{
static_cast<float>(surfaceSize.Width) * scaleFactor,
static_cast<float>(surfaceSize.Height) * scaleFactor
};
fixedOffset =
{
(static_cast<float>(wil::rect_width(mi.rcMonitor)) * (1.f + s_sharedResources.parallaxIntensity) - scaledSize.x) / 2.f,
(static_cast<float>(wil::rect_height(mi.rcMonitor)) * (1.f + s_sharedResources.parallaxIntensity) - scaledSize.y) / 2.f
};
currentMonitor = monitor;
currentMonitorRect = mi.rcMonitor;
}
if ((currentWindowRect.left != windowRect.left) || (currentWindowRect.top != windowRect.top))
{
MARGINS margins{};
window->GetBorderMargins(&margins);
glassSurfaceBrush.Offset(
winrt::Windows::Foundation::Numerics::float2
{
-static_cast<float>(windowRect.left - mi.rcMonitor.left) * (1.f - s_sharedResources.parallaxIntensity) +
//static_cast<float>(IsMaximized(hwnd) ? margins.cxLeftWidth : 0.f) +
fixedOffset.x,
-static_cast<float>(windowRect.top - mi.rcMonitor.top) * (1.f - s_sharedResources.parallaxIntensity) +
//static_cast<float>(IsMaximized(hwnd) ? margins.cyTopHeight : 0.f) +
fixedOffset.y
}
);
currentWindowRect = windowRect;
}
return S_OK;
}
CATCH_RETURN()
};
}