-
Notifications
You must be signed in to change notification settings - Fork 84
/
App.cpp
338 lines (300 loc) · 9.5 KB
/
App.cpp
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "pch.h"
#include "App.h"
#include "CaptureSnapshot.h"
namespace winrt
{
using namespace Windows::Foundation;
using namespace Windows::Foundation::Metadata;
using namespace Windows::Graphics::Capture;
using namespace Windows::Graphics::DirectX;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;
using namespace Windows::System;
using namespace Windows::UI;
using namespace Windows::UI::Composition;
using namespace Windows::UI::Popups;
}
namespace util
{
using namespace robmikh::common::desktop;
using namespace robmikh::common::uwp;
}
App::App(winrt::ContainerVisual root, winrt::GraphicsCapturePicker capturePicker, winrt::FileSavePicker savePicker)
{
m_capturePicker = capturePicker;
m_savePicker = savePicker;
m_mainThread = winrt::DispatcherQueue::GetForCurrentThread();
WINRT_VERIFY(m_mainThread != nullptr);
m_compositor = root.Compositor();
m_root = m_compositor.CreateContainerVisual();
m_content = m_compositor.CreateSpriteVisual();
m_brush = m_compositor.CreateSurfaceBrush();
m_root.RelativeSizeAdjustment({ 1, 1 });
root.Children().InsertAtTop(m_root);
m_content.AnchorPoint({ 0.5f, 0.5f });
m_content.RelativeOffsetAdjustment({ 0.5f, 0.5f, 0 });
m_content.RelativeSizeAdjustment({ 1, 1 });
m_content.Size({ -80, -80 });
m_content.Brush(m_brush);
m_brush.HorizontalAlignmentRatio(0.5f);
m_brush.VerticalAlignmentRatio(0.5f);
m_brush.Stretch(winrt::CompositionStretch::Uniform);
auto shadow = m_compositor.CreateDropShadow();
shadow.Mask(m_brush);
m_content.Shadow(shadow);
m_root.Children().InsertAtTop(m_content);
auto d3dDevice = util::CreateD3DDevice();
auto dxgiDevice = d3dDevice.as<IDXGIDevice>();
m_device = CreateDirect3DDevice(dxgiDevice.get());
// Don't bother with a D2D device if we can't use dirty regions
if (winrt::ApiInformation::IsPropertyPresent(winrt::name_of<winrt::GraphicsCaptureSession>(), L"DirtyRegionMode"))
{
m_dirtyRegionVisualizer = std::make_shared<DirtyRegionVisualizer>(d3dDevice);
}
}
winrt::GraphicsCaptureItem App::TryStartCaptureFromWindowHandle(HWND hwnd)
{
winrt::GraphicsCaptureItem item{ nullptr };
try
{
item = util::CreateCaptureItemForWindow(hwnd);
StartCaptureFromItem(item);
}
catch (winrt::hresult_error const& error)
{
MessageBoxW(nullptr,
error.message().c_str(),
L"Win32CaptureSample",
MB_OK | MB_ICONERROR);
}
return item;
}
winrt::GraphicsCaptureItem App::TryStartCaptureFromMonitorHandle(HMONITOR hmon)
{
winrt::GraphicsCaptureItem item{ nullptr };
try
{
item = util::CreateCaptureItemForMonitor(hmon);
StartCaptureFromItem(item);
}
catch (winrt::hresult_error const& error)
{
MessageBoxW(nullptr,
error.message().c_str(),
L"Win32CaptureSample",
MB_OK | MB_ICONERROR);
}
return item;
}
winrt::IAsyncOperation<winrt::GraphicsCaptureItem> App::StartCaptureWithPickerAsync()
{
auto item = co_await m_capturePicker.PickSingleItemAsync();
if (item)
{
// We might resume on a different thread, so let's resume execution on the
// main thread. This is important because SimpleCapture uses
// Direct3D11CaptureFramePool::Create, which requires the existence of
// a DispatcherQueue. See CaptureSnapshot for an example that uses
// Direct3D11CaptureFramePool::CreateFreeThreaded, which doesn't now have this
// requirement. See the README if you're unsure of which version of 'Create' to use.
co_await wil::resume_foreground(m_mainThread);
StartCaptureFromItem(item);
}
co_return item;
}
winrt::IAsyncOperation<winrt::StorageFile> App::TakeSnapshotAsync()
{
// Use what we're currently capturing
if (m_capture == nullptr)
{
co_return nullptr;
}
auto item = m_capture->CaptureItem();
// Ask the user where they want to save the snapshot.
m_savePicker.SuggestedStartLocation(winrt::PickerLocationId::PicturesLibrary);
m_savePicker.SuggestedFileName(L"snapshot");
m_savePicker.DefaultFileExtension(L".png");
m_savePicker.FileTypeChoices().Clear();
m_savePicker.FileTypeChoices().Insert(L"PNG image", winrt::single_threaded_vector<winrt::hstring>({ L".png" }));
m_savePicker.FileTypeChoices().Insert(L"JPG image", winrt::single_threaded_vector<winrt::hstring>({ L".jpg" }));
m_savePicker.FileTypeChoices().Insert(L"JXR image", winrt::single_threaded_vector<winrt::hstring>({ L".jxr" }));
auto file = co_await m_savePicker.PickSaveFileAsync();
if (file == nullptr)
{
co_return nullptr;
}
// Decide on the pixel format depending on the image type
auto fileExtension = file.FileType();
winrt::guid fileFormatGuid = {};
winrt::BitmapPixelFormat bitmapPixelFormat;
winrt::DirectXPixelFormat pixelFormat;
if (fileExtension == L".png")
{
fileFormatGuid = winrt::BitmapEncoder::PngEncoderId();
bitmapPixelFormat = winrt::BitmapPixelFormat::Bgra8;
pixelFormat = winrt::DirectXPixelFormat::B8G8R8A8UIntNormalized;
}
else if (fileExtension == L".jpg" || fileExtension == L".jpeg")
{
fileFormatGuid = winrt::BitmapEncoder::JpegEncoderId();
bitmapPixelFormat = winrt::BitmapPixelFormat::Bgra8;
pixelFormat = winrt::DirectXPixelFormat::B8G8R8A8UIntNormalized;
}
else if (fileExtension == L".jxr")
{
fileFormatGuid = winrt::BitmapEncoder::JpegXREncoderId();
bitmapPixelFormat = winrt::BitmapPixelFormat::Rgba16;
pixelFormat = winrt::DirectXPixelFormat::R16G16B16A16Float;
}
else
{
// Unsupported
co_await wil::resume_foreground(m_mainThread);
MessageBoxW(nullptr,
L"Unsupported file format!",
L"Win32CaptureSample",
MB_OK | MB_ICONERROR);
co_return nullptr;
}
{
// Get the file stream
auto stream = co_await file.OpenAsync(winrt::FileAccessMode::ReadWrite);
// Initialize the encoder
auto encoder = co_await winrt::BitmapEncoder::CreateAsync(fileFormatGuid, stream);
// Take the snapshot
auto texture = co_await CaptureSnapshot::TakeAsync(m_device, item, pixelFormat);
// Encode the image
D3D11_TEXTURE2D_DESC desc = {};
texture->GetDesc(&desc);
auto bytes = util::CopyBytesFromTexture(texture);
encoder.SetPixelData(
bitmapPixelFormat,
winrt::BitmapAlphaMode::Premultiplied,
desc.Width,
desc.Height,
1.0,
1.0,
bytes);
co_await encoder.FlushAsync();
}
co_return file;
}
void App::StartCaptureFromItem(winrt::GraphicsCaptureItem item)
{
m_capture = std::make_unique<SimpleCapture>(m_device, m_dirtyRegionVisualizer, item, m_pixelFormat);
auto surface = m_capture->CreateSurface(m_compositor);
m_brush.Surface(surface);
m_capture->StartCapture();
}
void App::StopCapture()
{
if (m_capture)
{
m_capture->Close();
m_capture = nullptr;
m_brush.Surface(nullptr);
}
}
bool App::IsCursorEnabled()
{
if (m_capture != nullptr)
{
return m_capture->IsCursorEnabled();
}
return false;
}
void App::IsCursorEnabled(bool value)
{
if (m_capture != nullptr)
{
m_capture->IsCursorEnabled(value);
}
}
void App::PixelFormat(winrt::DirectXPixelFormat pixelFormat)
{
m_pixelFormat = pixelFormat;
if (m_capture)
{
m_capture->SetPixelFormat(pixelFormat);
}
}
bool App::IsBorderRequired()
{
if (m_capture != nullptr)
{
return m_capture->IsBorderRequired();
}
return false;
}
winrt::fire_and_forget App::IsBorderRequired(bool value)
{
if (m_capture != nullptr)
{
// Even if the user or system policy denies access, it's
// still safe to set the IsBorderRequired property. In the
// event that the policy changes, the property will be honored.
auto ignored = co_await winrt::GraphicsCaptureAccess::RequestAccessAsync(winrt::GraphicsCaptureAccessKind::Borderless);
m_capture->IsBorderRequired(value);
}
}
bool App::IncludeSecondaryWindows()
{
if (m_capture != nullptr)
{
return m_capture->IncludeSecondaryWindows();
}
return false;
}
void App::IncludeSecondaryWindows(bool value)
{
if (m_capture != nullptr)
{
m_capture->IncludeSecondaryWindows(value);
}
}
bool App::VisualizeDirtyRegions()
{
if (m_capture != nullptr)
{
return m_capture->VisualizeDirtyRegions();
}
return false;
}
void App::VisualizeDirtyRegions(bool value)
{
if (m_capture != nullptr)
{
m_capture->VisualizeDirtyRegions(value);
}
}
winrt::GraphicsCaptureDirtyRegionMode App::DirtyRegionMode()
{
if (m_capture != nullptr)
{
return m_capture->DirtyRegionMode();
}
return winrt::GraphicsCaptureDirtyRegionMode::ReportOnly;
}
void App::DirtyRegionMode(winrt::GraphicsCaptureDirtyRegionMode value)
{
if (m_capture != nullptr)
{
m_capture->DirtyRegionMode(value);
}
}
winrt::TimeSpan App::MinUpdateInterval()
{
if (m_capture != nullptr)
{
return m_capture->MinUpdateInterval();
}
return winrt::TimeSpan{ 0 };
}
void App::MinUpdateInterval(winrt::TimeSpan value)
{
if (m_capture != nullptr)
{
m_capture->MinUpdateInterval(value);
}
}