-
Notifications
You must be signed in to change notification settings - Fork 37
/
wcap_screen_capture.h
631 lines (528 loc) · 23.8 KB
/
wcap_screen_capture.h
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#pragma once
#include "wcap.h"
#include <d3d11.h>
#include <dwmapi.h>
#define WINDOWS_FOUNDATION_UNIVERSALAPICONTRACT_VERSION 0xc0000
#include <windows.graphics.capture.h>
typedef struct IGraphicsCaptureItemInterop IGraphicsCaptureItemInterop;
//
// interface
//
typedef struct
{
// public
ID3D11Texture2D* Texture;
uint64_t Time;
RECT Rect;
// private
uint32_t Width;
uint32_t Height;
IUnknown* NextFrame;
}
ScreenCaptureFrame;
typedef struct ScreenCapture ScreenCapture;
// return true to continue capture, or false stop any further catpures
typedef bool ScreenCapture_OnFrameCallback(ScreenCapture* Capture, ScreenCaptureFrame* Frame);
typedef struct ScreenCapture
{
IGraphicsCaptureItemInterop* ItemInterop;
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePoolStatics* FramePoolStatics;
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePoolStatics2* FramePoolStatics2;
IInspectable* Controller; // actually IDispatcherQueueController
__x_ABI_CWindows_CGraphics_CDirectX_CDirect3D11_CIDirect3DDevice* Device;
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem* Item;
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool* FramePool;
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession* Session;
ScreenCapture_OnFrameCallback* OnFrame;
__FITypedEventHandler_2_Windows__CGraphics__CCapture__CDirect3D11CaptureFramePool_IInspectable OnFrameHandler;
__FITypedEventHandler_2_Windows__CGraphics__CCapture__CGraphicsCaptureItem_IInspectable OnCloseHandler;
EventRegistrationToken OnFrameToken;
EventRegistrationToken OnCloseToken;
__x_ABI_CWindows_CGraphics_CSizeInt32 CurrentSize;
RECT Rect;
HWND Window;
bool OnlyClientArea;
bool RestoreWindowCornerPreference;
DWM_WINDOW_CORNER_PREFERENCE WindowCornerPreference;
}
ScreenCapture;
static bool ScreenCapture_IsSupported(void);
static bool ScreenCapture_CanHideMouseCursor(void);
static bool ScreenCapture_CanHideRecordingBorder(void);
static bool ScreenCapture_CanDisableRoundedCorners(void);
// if OnFrame is NULL then you need to periodically call GetFrame/ReleaseFrame manually
// if OnFrame is not NULL and CallbackOnThread is false then callback will be invoked from message processing loop on the same thread as Create call
// if OnFrame is not NULL and CallbackOnThread is true, then callback will be invoked on backgroud thread
static void ScreenCapture_Create(ScreenCapture* Capture, ScreenCapture_OnFrameCallback* OnFrame, bool CallbackOnThread);
static void ScreenCapture_Release(ScreenCapture* Capture);
static bool ScreenCapture_CreateForWindow(ScreenCapture* Capture, ID3D11Device* Device, HWND Window, bool OnlyClientArea, bool DisableRoundedCorners);
static bool ScreenCapture_CreateForMonitor(ScreenCapture* Capture, ID3D11Device* Device, HMONITOR Monitor, const RECT* Rect);
static void ScreenCapture_Start(ScreenCapture* Capture, bool WithMouseCursor, bool WithRecordingBorder);
static void ScreenCapture_Stop(ScreenCapture* Capture);
static bool ScreenCapture_GetFrame(ScreenCapture* Capture, ScreenCaptureFrame* Frame);
static void ScreenCapture_ReleaseFrame(ScreenCapture* Capture, ScreenCaptureFrame* Frame);
//
// implementation
//
#include <dxgi.h>
#include <roapi.h>
#ifndef SCREEN_CAPTURE_BUFFER_COUNT
#define SCREEN_CAPTURE_BUFFER_COUNT 2
#endif
#ifndef SCREEN_CAPTURE_BUFFER_FORMAT
#define SCREEN_CAPTURE_BUFFER_FORMAT DirectXPixelFormat_B8G8R8A8UIntNormalized // same as DXGI_FORMAT_B8G8R8A8_UNORM
#endif
// this really should be just these three includes, but Microsoft decided to not do proper COM headers that support C :(
//#include <dispatcherqueue.h>
//#include <windows.graphics.capture.interop.h>
//#include <windows.graphics.directx.direct3d11.interop.h>
typedef enum {
DQTAT_COM_NONE = 0,
DQTAT_COM_ASTA = 1,
DQTAT_COM_STA = 2,
} DISPATCHERQUEUE_THREAD_APARTMENTTYPE;
typedef enum {
DQTYPE_THREAD_DEDICATED = 1,
DQTYPE_THREAD_CURRENT = 2,
} DISPATCHERQUEUE_THREAD_TYPE;
typedef struct {
DWORD dwSize;
DISPATCHERQUEUE_THREAD_TYPE threadType;
DISPATCHERQUEUE_THREAD_APARTMENTTYPE apartmentType;
} DispatcherQueueOptions;
struct IGraphicsCaptureItemInteropVtbl
{
STDMETHOD(QueryInterface)(IGraphicsCaptureItemInterop* This, const GUID* Riid, void** Object);
STDMETHOD_(ULONG, AddRef)(IGraphicsCaptureItemInterop* This);
STDMETHOD_(ULONG, Release)(IGraphicsCaptureItemInterop* This);
STDMETHOD(CreateForWindow)(IGraphicsCaptureItemInterop* This, HWND Window, const GUID* Riid, void** Result);
STDMETHOD(CreateForMonitor)(IGraphicsCaptureItemInterop* This, HMONITOR Monitor, const GUID* Riid, void** Result);
};
struct IGraphicsCaptureItemInterop
{
struct IGraphicsCaptureItemInteropVtbl* lpVtbl;
};
typedef struct IDirect3DDxgiInterfaceAccess IDirect3DDxgiInterfaceAccess;
struct IDirect3DDxgiInterfaceAccessVtbl
{
STDMETHOD(QueryInterface)(IDirect3DDxgiInterfaceAccess* This, const GUID* Riid, void** Object);
STDMETHOD_(ULONG, AddRef)(IDirect3DDxgiInterfaceAccess* This);
STDMETHOD_(ULONG, Release)(IDirect3DDxgiInterfaceAccess* This);
STDMETHOD(GetInterface)(IDirect3DDxgiInterfaceAccess* This, const GUID* Riid, void** Object);
};
struct IDirect3DDxgiInterfaceAccess
{
struct IDirect3DDxgiInterfaceAccessVtbl* lpVtbl;
};
// from ntdll.dll
extern __declspec(dllimport) LONG WINAPI RtlGetVersion(RTL_OSVERSIONINFOW*);
// from CoreMessaging.dll
extern __declspec(dllimport) HRESULT WINAPI CreateDispatcherQueueController(DispatcherQueueOptions, IInspectable**);
// from d3d11.dll
extern __declspec(dllimport) HRESULT WINAPI CreateDirect3D11DeviceFromDXGIDevice(IDXGIDevice*, IInspectable**);
DEFINE_GUID(IID_IClosable, 0x30d5a829, 0x7fa4, 0x4026, 0x83, 0xbb, 0xd7, 0x5b, 0xae, 0x4e, 0xa9, 0x9e);
DEFINE_GUID(IID_IGraphicsCaptureSession2, 0x2c39ae40, 0x7d2e, 0x5044, 0x80, 0x4e, 0x8b, 0x67, 0x99, 0xd4, 0xcf, 0x9e);
DEFINE_GUID(IID_IGraphicsCaptureSession3, 0xf2cdd966, 0x22ae, 0x5ea1, 0x95, 0x96, 0x3a, 0x28, 0x93, 0x44, 0xc3, 0xbe);
DEFINE_GUID(IID_IGraphicsCaptureItemInterop, 0x3628e81b, 0x3cac, 0x4c60, 0xb7, 0xf4, 0x23, 0xce, 0x0e, 0x0c, 0x33, 0x56);
DEFINE_GUID(IID_IGraphicsCaptureItem, 0x79c3f95b, 0x31f7, 0x4ec2, 0xa4, 0x64, 0x63, 0x2e, 0xf5, 0xd3, 0x07, 0x60);
DEFINE_GUID(IID_IGraphicsCaptureItemHandler, 0xe9c610c0, 0xa68c, 0x5bd9, 0x80, 0x21, 0x85, 0x89, 0x34, 0x6e, 0xee, 0xe2);
DEFINE_GUID(IID_IDirect3D11CaptureFramePoolStatics, 0x7784056a, 0x67aa, 0x4d53, 0xae, 0x54, 0x10, 0x88, 0xd5, 0xa8, 0xca, 0x21);
DEFINE_GUID(IID_IDirect3D11CaptureFramePoolStatics2, 0x589b103f, 0x6bbc, 0x5df5, 0xa9, 0x91, 0x02, 0xe2, 0x8b, 0x3b, 0x66, 0xd5);
DEFINE_GUID(IID_IDirect3D11CaptureFramePoolHandler, 0x51a947f7, 0x79cf, 0x5a3e, 0xa3, 0xa5, 0x12, 0x89, 0xcf, 0xa6, 0xdf, 0xe8);
DEFINE_GUID(IID_IDirect3DDxgiInterfaceAccess, 0xa9b3d012, 0x3df2, 0x4ee3, 0xb8, 0xd1, 0x86, 0x95, 0xf4, 0x57, 0xd3, 0xc1);
static HRESULT STDMETHODCALLTYPE ScreenCapture__FrameQueryInterface(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CDirect3D11CaptureFramePool_IInspectable* This, REFIID Riid, void** Object)
{
if (Object == NULL)
{
return E_POINTER;
}
if (IsEqualGUID(Riid, &IID_IDirect3D11CaptureFramePoolHandler) ||
IsEqualGUID(Riid, &IID_IAgileObject) ||
IsEqualGUID(Riid, &IID_IUnknown))
{
*Object = This;
return S_OK;
}
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE ScreenCapture__FrameAddRef(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CDirect3D11CaptureFramePool_IInspectable* This)
{
return 1;
}
static ULONG STDMETHODCALLTYPE ScreenCapture__FrameRelease(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CDirect3D11CaptureFramePool_IInspectable* This)
{
return 1;
}
static HRESULT STDMETHODCALLTYPE ScreenCapture__FrameInvoke(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CDirect3D11CaptureFramePool_IInspectable* This, __x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool* Sender, IInspectable* Args)
{
ScreenCapture* Capture = CONTAINING_RECORD(This, ScreenCapture, OnFrameHandler);
ScreenCaptureFrame Frame;
if (ScreenCapture_GetFrame(Capture, &Frame))
{
bool ContinueCapture = Capture->OnFrame(Capture, &Frame);
ScreenCapture_ReleaseFrame(Capture, &Frame);
if (!ContinueCapture)
{
ScreenCapture_Stop(Capture);
}
}
return S_OK;
}
static HRESULT STDMETHODCALLTYPE ScreenCapture__CloseQueryInterface(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CGraphicsCaptureItem_IInspectable* This, REFIID Riid, void** Object)
{
if (Object == NULL)
{
return E_POINTER;
}
if (IsEqualGUID(Riid, &IID_IGraphicsCaptureItemHandler) ||
IsEqualGUID(Riid, &IID_IAgileObject) ||
IsEqualGUID(Riid, &IID_IUnknown))
{
*Object = This;
return S_OK;
}
return E_NOINTERFACE;
}
static ULONG STDMETHODCALLTYPE ScreenCapture__CloseAddRef(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CGraphicsCaptureItem_IInspectable* This)
{
return 1;
}
static ULONG STDMETHODCALLTYPE ScreenCapture__CloseRelease(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CGraphicsCaptureItem_IInspectable* This)
{
return 1;
}
static HRESULT STDMETHODCALLTYPE ScreenCapture__CloseInvoke(__FITypedEventHandler_2_Windows__CGraphics__CCapture__CGraphicsCaptureItem_IInspectable* This, __x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem* Sender, IInspectable* Args)
{
ScreenCapture* Capture = CONTAINING_RECORD(This, ScreenCapture, OnCloseHandler);
Capture->OnFrame(Capture, NULL);
return S_OK;
}
static __FITypedEventHandler_2_Windows__CGraphics__CCapture__CDirect3D11CaptureFramePool_IInspectableVtbl ScreenCapture__OnFrameHandlerVtbl =
{
.QueryInterface = ScreenCapture__FrameQueryInterface,
.AddRef = ScreenCapture__FrameAddRef,
.Release = ScreenCapture__FrameRelease,
.Invoke = ScreenCapture__FrameInvoke,
};
static __FITypedEventHandler_2_Windows__CGraphics__CCapture__CGraphicsCaptureItem_IInspectableVtbl ScreenCapture__OnCloseHandlerVtbl =
{
.QueryInterface = ScreenCapture__CloseQueryInterface,
.AddRef = ScreenCapture__CloseAddRef,
.Release = ScreenCapture__CloseRelease,
.Invoke = ScreenCapture__CloseInvoke,
};
static RECT ScreenCapture__GetRect(ScreenCapture* Capture, uint32_t Width, uint32_t Height)
{
if (Capture->Window) // capturing window
{
RECT WindowRect;
if (FAILED(DwmGetWindowAttribute(Capture->Window, DWMWA_EXTENDED_FRAME_BOUNDS, &WindowRect, sizeof(WindowRect))))
{
return (RECT) { 0 };
}
if (Capture->OnlyClientArea)
{
RECT ClientRect;
GetClientRect(Capture->Window, &ClientRect);
POINT TopLeft = { 0, 0 };
ClientToScreen(Capture->Window, &TopLeft);
RECT Rect;
Rect.left = max(0, TopLeft.x - WindowRect.left);
Rect.top = max(0, TopLeft.y - WindowRect.top);
Rect.right = Rect.left + min((uint32_t)ClientRect.right, Width - Rect.left);
Rect.bottom = Rect.top + min((uint32_t)ClientRect.bottom, Height - Rect.top);
return Rect;
}
else // whole window size
{
return (RECT)
{
.left = 0,
.top = 0,
.right = WindowRect.right - WindowRect.left,
.bottom = WindowRect.bottom - WindowRect.top,
};
}
}
else // capturing monitor, or region on it
{
return Capture->Rect;
}
}
static bool ScreenCapture__CreateFramePool(ScreenCapture* Capture, __x_ABI_CWindows_CGraphics_CSizeInt32 Size, __x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool** FramePool)
{
if (Capture->FramePoolStatics2)
{
return SUCCEEDED(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePoolStatics2_CreateFreeThreaded(Capture->FramePoolStatics2, Capture->Device, SCREEN_CAPTURE_BUFFER_FORMAT, SCREEN_CAPTURE_BUFFER_COUNT, Size, FramePool));
}
else
{
return SUCCEEDED(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePoolStatics_Create(Capture->FramePoolStatics, Capture->Device, SCREEN_CAPTURE_BUFFER_FORMAT, SCREEN_CAPTURE_BUFFER_COUNT, Size, FramePool));
}
}
bool ScreenCapture_IsSupported(void)
{
RTL_OSVERSIONINFOW Version = { sizeof(Version) };
RtlGetVersion(&Version);
// available since Windows 10 version 1903, May 2019 Update (19H1), build 10.0.18362.0
return Version.dwMajorVersion > 10 || (Version.dwMajorVersion == 10 && Version.dwBuildNumber >= 18362);
}
bool ScreenCapture_CanHideMouseCursor(void)
{
RTL_OSVERSIONINFOW Version = { sizeof(Version) };
RtlGetVersion(&Version);
// available since Windows 10 version 2004, May 2020 Update (20H1), build 10.0.19041.0
return Version.dwMajorVersion > 10 || (Version.dwMajorVersion == 10 && Version.dwBuildNumber >= 19041);
}
bool ScreenCapture_CanHideRecordingBorder(void)
{
RTL_OSVERSIONINFOW Version = { sizeof(Version) };
RtlGetVersion(&Version);
// available since Windows 11, build 10.0.22000.0
return Version.dwMajorVersion > 10 || (Version.dwMajorVersion == 10 && Version.dwBuildNumber >= 22000);
}
bool ScreenCapture_CanDisableRoundedCorners(void)
{
RTL_OSVERSIONINFOW Version = { sizeof(Version) };
RtlGetVersion(&Version);
// available since Windows 11, build 10.0.22000.0
return Version.dwMajorVersion > 10 || (Version.dwMajorVersion == 10 && Version.dwBuildNumber >= 22000);
}
void ScreenCapture_Create(ScreenCapture* Capture, ScreenCapture_OnFrameCallback* OnFrame, bool CallbackOnThread)
{
HR(RoInitialize(RO_INIT_SINGLETHREADED));
typedef struct
{
DWORD Flags;
DWORD Length;
DWORD Padding1;
DWORD Padding2;
LPCWCHAR Ptr;
}
StaticHSTRING;
#define STATIC_HSTRING(Str) (HSTRING)&(StaticHSTRING){ 1, ARRAYSIZE(Str) - 1, 0, 0, Str }
HSTRING GraphicsCaptureItem = STATIC_HSTRING(RuntimeClass_Windows_Graphics_Capture_GraphicsCaptureItem);
HSTRING Direct3D11CaptureFramePool = STATIC_HSTRING(RuntimeClass_Windows_Graphics_Capture_Direct3D11CaptureFramePool);
#undef STATIC_HSTRING
HR(RoGetActivationFactory(GraphicsCaptureItem, &IID_IGraphicsCaptureItemInterop, (void**)&Capture->ItemInterop));
if (OnFrame && CallbackOnThread)
{
HR(RoGetActivationFactory(Direct3D11CaptureFramePool, &IID_IDirect3D11CaptureFramePoolStatics2, (void**)&Capture->FramePoolStatics2));
Capture->FramePoolStatics = NULL;
}
else
{
HR(RoGetActivationFactory(Direct3D11CaptureFramePool, &IID_IDirect3D11CaptureFramePoolStatics, (void**)&Capture->FramePoolStatics));
Capture->FramePoolStatics2 = NULL;
}
if (OnFrame && !CallbackOnThread)
{
// create dispatcher queue that will call callbacks on main thread as part of message loop
DispatcherQueueOptions Options =
{
.dwSize = sizeof(Options),
.threadType = DQTYPE_THREAD_CURRENT,
.apartmentType = DQTAT_COM_NONE,
};
HR(CreateDispatcherQueueController(Options, &Capture->Controller));
}
Capture->OnFrameHandler.lpVtbl = &ScreenCapture__OnFrameHandlerVtbl;
Capture->OnCloseHandler.lpVtbl = &ScreenCapture__OnCloseHandlerVtbl;
Capture->OnFrame = OnFrame;
}
void ScreenCapture_Release(ScreenCapture* Capture)
{
if (Capture->Controller)
{
IInspectable_Release(Capture->Controller);
}
if (Capture->FramePoolStatics)
{
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePoolStatics_Release(Capture->FramePoolStatics);
}
else
{
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePoolStatics2_Release(Capture->FramePoolStatics2);
}
Capture->ItemInterop->lpVtbl->Release(Capture->ItemInterop);
RoUninitialize();
}
bool ScreenCapture_CreateForWindow(ScreenCapture* Capture, ID3D11Device* Device, HWND Window, bool OnlyClientArea, bool DisableRoundedCorners)
{
IDXGIDevice* DxgiDevice;
HR(ID3D11Device_QueryInterface(Device, &IID_IDXGIDevice, (LPVOID*)&DxgiDevice));
HR(CreateDirect3D11DeviceFromDXGIDevice(DxgiDevice, (IInspectable**)&Capture->Device));
IDXGIDevice_Release(DxgiDevice);
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem* Item;
if (SUCCEEDED(Capture->ItemInterop->lpVtbl->CreateForWindow(Capture->ItemInterop, Window, &IID_IGraphicsCaptureItem, (void**)&Item)))
{
__x_ABI_CWindows_CGraphics_CSizeInt32 Size;
HR(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem_get_Size(Item, &Size));
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool* FramePool;
if (ScreenCapture__CreateFramePool(Capture, Size, &FramePool))
{
Capture->Item = Item;
Capture->FramePool = FramePool;
Capture->OnlyClientArea = OnlyClientArea;
Capture->Window = Window;
RECT Rect = ScreenCapture__GetRect(Capture, Size.Width, Size.Height);
Capture->CurrentSize.Width = Rect.right - Rect.left;
Capture->CurrentSize.Height = Rect.bottom - Rect.top;
Capture->Rect = Rect;
Capture->RestoreWindowCornerPreference = false;
if (DisableRoundedCorners)
{
if (SUCCEEDED(DwmGetWindowAttribute(Window, DWMWA_WINDOW_CORNER_PREFERENCE, &Capture->WindowCornerPreference, sizeof(Capture->WindowCornerPreference))))
{
DWM_WINDOW_CORNER_PREFERENCE Preference = DWMWCP_DONOTROUND;
if (SUCCEEDED(DwmSetWindowAttribute(Window, DWMWA_WINDOW_CORNER_PREFERENCE, &Preference, sizeof(Preference))))
{
Capture->RestoreWindowCornerPreference = true;
}
}
}
return true;
}
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem_Release(Item);
}
__x_ABI_CWindows_CGraphics_CDirectX_CDirect3D11_CIDirect3DDevice_Release(Capture->Device);
return false;
}
bool ScreenCapture_CreateForMonitor(ScreenCapture* Capture, ID3D11Device* Device, HMONITOR Monitor, const RECT* Rect)
{
IDXGIDevice* DxgiDevice;
HR(ID3D11Device_QueryInterface(Device, &IID_IDXGIDevice, (LPVOID*)&DxgiDevice));
HR(CreateDirect3D11DeviceFromDXGIDevice(DxgiDevice, (IInspectable**)&Capture->Device));
IDXGIDevice_Release(DxgiDevice);
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem* Item;
if (SUCCEEDED(Capture->ItemInterop->lpVtbl->CreateForMonitor(Capture->ItemInterop, Monitor, &IID_IGraphicsCaptureItem, (void**)&Item)))
{
__x_ABI_CWindows_CGraphics_CSizeInt32 Size;
HR(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem_get_Size(Item, &Size));
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool* FramePool;
if (ScreenCapture__CreateFramePool(Capture, Size, &FramePool))
{
Capture->Item = Item;
Capture->FramePool = FramePool;
Capture->Window = NULL;
Capture->CurrentSize = Size;
Capture->Rect = Rect ? *Rect : (RECT) { 0, 0, Size.Width, Size.Height };
Capture->RestoreWindowCornerPreference = false;
return true;
}
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem_Release(Item);
}
__x_ABI_CWindows_CGraphics_CDirectX_CDirect3D11_CIDirect3DDevice_Release(Capture->Device);
return false;
}
void ScreenCapture_Start(ScreenCapture* Capture, bool WithMouseCursor, bool WithRecordingBorder)
{
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession* Session;
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool_CreateCaptureSession(Capture->FramePool, Capture->Item, &Session));
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession2* Session2;
if (SUCCEEDED(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession_QueryInterface(Session, &IID_IGraphicsCaptureSession2, (void**)&Session2)))
{
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession2_put_IsCursorCaptureEnabled(Session2, (boolean)WithMouseCursor);
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession2_Release(Session2);
}
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession3* Session3;
if (SUCCEEDED(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession_QueryInterface(Session, &IID_IGraphicsCaptureSession3, (void**)&Session3)))
{
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession3_put_IsBorderRequired(Session3, (boolean)WithRecordingBorder);
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession3_Release(Session3);
}
if (Capture->OnFrame)
{
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool_add_FrameArrived(Capture->FramePool, &Capture->OnFrameHandler, &Capture->OnFrameToken));
HR(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem_add_Closed(Capture->Item, &Capture->OnCloseHandler, &Capture->OnCloseToken));
}
HR(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession_StartCapture(Session));
Capture->Session = Session;
}
void ScreenCapture_Stop(ScreenCapture* Capture)
{
__x_ABI_CWindows_CFoundation_CIClosable* Closable;
if (Capture->OnFrameToken.value)
{
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool_remove_FrameArrived(Capture->FramePool, Capture->OnFrameToken));
Capture->OnFrameToken.value = 0;
}
if (Capture->OnCloseToken.value)
{
HR(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem_remove_Closed(Capture->Item, Capture->OnCloseToken));
Capture->OnCloseToken.value = 0;
}
if (Capture->Session)
{
HR(__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession_QueryInterface(Capture->Session, &IID_IClosable, (void**)&Closable));
HR(__x_ABI_CWindows_CFoundation_CIClosable_Close(Closable));
__x_ABI_CWindows_CFoundation_CIClosable_Release(Closable);
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureSession_Release(Capture->Session);
Capture->Session = NULL;
}
if (Capture->Item)
{
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool_QueryInterface(Capture->FramePool, &IID_IClosable, (void**)&Closable));
HR(__x_ABI_CWindows_CFoundation_CIClosable_Close(Closable));
__x_ABI_CWindows_CFoundation_CIClosable_Release(Closable);
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool_Release(Capture->FramePool);
__x_ABI_CWindows_CGraphics_CCapture_CIGraphicsCaptureItem_Release(Capture->Item);
Capture->Item = NULL;
}
if (Capture->Device)
{
__x_ABI_CWindows_CGraphics_CDirectX_CDirect3D11_CIDirect3DDevice_Release(Capture->Device);
Capture->Device = NULL;
}
if (Capture->RestoreWindowCornerPreference)
{
DwmSetWindowAttribute(Capture->Window, DWMWA_WINDOW_CORNER_PREFERENCE, &Capture->WindowCornerPreference, sizeof(Capture->WindowCornerPreference));
}
}
bool ScreenCapture_GetFrame(ScreenCapture* Capture, ScreenCaptureFrame* Frame)
{
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFrame* NextFrame;
if (SUCCEEDED(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool_TryGetNextFrame(Capture->FramePool, &NextFrame)) && NextFrame != NULL)
{
__x_ABI_CWindows_CFoundation_CTimeSpan Time;
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFrame_get_SystemRelativeTime(NextFrame, &Time));
__x_ABI_CWindows_CGraphics_CSizeInt32 Size;
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFrame_get_ContentSize(NextFrame, &Size));
__x_ABI_CWindows_CGraphics_CDirectX_CDirect3D11_CIDirect3DSurface* Surface;
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFrame_get_Surface(NextFrame, &Surface));
IDirect3DDxgiInterfaceAccess* Access;
HR(__x_ABI_CWindows_CGraphics_CDirectX_CDirect3D11_CIDirect3DSurface_QueryInterface(Surface, &IID_IDirect3DDxgiInterfaceAccess, (void**)&Access));
__x_ABI_CWindows_CGraphics_CDirectX_CDirect3D11_CIDirect3DSurface_Release(Surface);
ID3D11Texture2D* Texture;
HR(Access->lpVtbl->GetInterface(Access, &IID_ID3D11Texture2D, (void**)&Texture));
Access->lpVtbl->Release(Access);
D3D11_TEXTURE2D_DESC TextureDesc;
ID3D11Texture2D_GetDesc(Texture, &TextureDesc);
RECT Rect = ScreenCapture__GetRect(Capture, min((uint32_t)Size.Width, TextureDesc.Width), min((uint32_t)Size.Height, TextureDesc.Height));
if (Rect.right > Rect.left && Rect.bottom > Rect.top)
{
*Frame = (ScreenCaptureFrame)
{
.Texture = Texture,
.Time = Time.Duration,
.Rect = Rect,
.Width = Size.Width,
.Height = Size.Height,
.NextFrame = (IUnknown*)NextFrame,
};
return true;
}
__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFrame_Release(NextFrame);
}
return false;
}
void ScreenCapture_ReleaseFrame(ScreenCapture* Capture, ScreenCaptureFrame* Frame)
{
ID3D11Texture2D_Release(Frame->Texture);
IUnknown_Release(Frame->NextFrame);
if (Capture->CurrentSize.Width != Frame->Width || Capture->CurrentSize.Height != Frame->Height)
{
Capture->CurrentSize = (__x_ABI_CWindows_CGraphics_CSizeInt32){ Frame->Width, Frame->Height };
HR(__x_ABI_CWindows_CGraphics_CCapture_CIDirect3D11CaptureFramePool_Recreate(Capture->FramePool, Capture->Device, SCREEN_CAPTURE_BUFFER_FORMAT, SCREEN_CAPTURE_BUFFER_COUNT, Capture->CurrentSize));
}
}