24 #include <d3dcompiler.h>
25 #include <dispatcherqueue.h>
26 #include <windows.foundation.h>
27 #include <windows.graphics.capture.h>
28 #include <windows.graphics.capture.interop.h>
29 #include <windows.graphics.directx.direct3d11.h>
30 #if HAVE_IDIRECT3DDXGIINTERFACEACCESS
31 #include <windows.graphics.directx.direct3d11.interop.h>
51 #include <condition_variable>
58 #include <type_traits>
60 using namespace ABI::Windows::System;
61 using namespace ABI::Windows::Foundation;
62 using namespace ABI::Windows::Graphics::Capture;
63 using namespace ABI::Windows::Graphics::DirectX::Direct3D11;
65 using Microsoft::WRL::ComPtr;
66 using ABI::Windows::Graphics::SizeInt32;
67 using ABI::Windows::Foundation::TimeSpan;
68 using ABI::Windows::Graphics::DirectX::DirectXPixelFormat;
70 #define TIMESPAN_RES 10000000
71 #define TIMESPAN_RES64 INT64_C(10000000)
73 #define CAPTURE_POOL_SIZE 2
79 #define CCTX(ctx) static_cast<GfxCaptureContext*>(ctx)
85 HRESULT (WINAPI *RoInitialize)(RO_INIT_TYPE initType);
86 void (WINAPI *RoUninitialize)(void);
87 HRESULT (WINAPI *RoGetActivationFactory)(HSTRING activatableClassId, REFIID
iid,
void **factory);
88 HRESULT (WINAPI *WindowsCreateStringReference)(PCWSTR sourceString, UINT32 length, HSTRING_HEADER *hstringHeader, HSTRING *
string);
91 HRESULT (WINAPI *DwmGetWindowAttribute)(HWND hwnd, DWORD dwAttribute, PVOID
pvAttribute, DWORD cbAttribute);
94 HRESULT (WINAPI *CreateDirect3D11DeviceFromDXGIDevice)(IDXGIDevice *dxgiDevice, IInspectable **
graphicsDevice);
100 DPI_AWARENESS_CONTEXT (WINAPI *SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT dpiContext);
106 HRESULT (WINAPI *D3DCompile)(LPCVOID pSrcData, SIZE_T
SrcDataSize, LPCSTR pSourceName,
const D3D10_SHADER_MACRO *pDefines, ID3DInclude *pInclude,
107 LPCSTR pEntrypoint, LPCSTR
pTarget, UINT Flags1, UINT Flags2, ID3DBlob **ppCode, ID3DBlob **ppErrorMsgs);
120 EventRegistrationToken frame_arrived_token { 0 };
121 EventRegistrationToken closed_token { 0 };
125 bool window_closed {
false };
126 uint64_t frame_seq { 0 };
128 SizeInt32 cap_size { 0, 0 };
129 RECT client_area_offsets { 0, 0, 0, 0 };
142 std::unique_ptr<GfxCaptureContextWgc>
wgc;
143 std::unique_ptr<GfxCaptureContextD3D>
d3d;
146 DWORD wgc_thread_id { 0 };
149 volatile int wgc_thread_init_res { INT_MAX };
151 volatile int wgc_thread_res { 0 };
154 HWND capture_hwnd {
nullptr };
155 HMONITOR capture_hmonitor {
nullptr };
169 template <
typename T>
171 HSTRING_HEADER hsheader = { 0 };
174 HRESULT hr =
ctx->fn.WindowsCreateStringReference(clsid, (UINT32)wcslen(clsid), &hsheader, &hs);
178 return ctx->fn.RoGetActivationFactory(hs, IID_PPV_ARGS(factory));
181 #define CHECK_HR(fcall, action) \
183 HRESULT fhr = fcall; \
185 av_log(avctx, AV_LOG_ERROR, #fcall " failed: 0x%08lX\n", fhr); \
189 #define CHECK_HR_RET(...) CHECK_HR((__VA_ARGS__), return AVERROR_EXTERNAL)
190 #define CHECK_HR_FAIL(...) CHECK_HR((__VA_ARGS__), ret = AVERROR_EXTERNAL; goto fail)
191 #define CHECK_HR_LOG(...) CHECK_HR((__VA_ARGS__), (void)0)
200 std::lock_guard
lock(wgctx->frame_arrived_mutex);
201 wgctx->frame_seq += 1;
203 wgctx->frame_arrived_cond.notify_one();
208 std::lock_guard
lock(wgctx->frame_arrived_mutex);
209 wgctx->window_closed =
true;
211 wgctx->frame_arrived_cond.notify_one();
218 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
220 if (wgctx->closed_token.value && wgctx->capture_item) {
221 CHECK_HR_LOG(wgctx->capture_item->remove_Closed(wgctx->closed_token));
222 wgctx->closed_token.value = 0;
225 if (wgctx->frame_arrived_token.value && wgctx->frame_pool) {
226 CHECK_HR_LOG(wgctx->frame_pool->remove_FrameArrived(wgctx->frame_arrived_token));
227 wgctx->frame_arrived_token.value = 0;
230 if (wgctx->capture_session) {
231 ComPtr<IClosable> closable;
232 if (SUCCEEDED(wgctx->capture_session.As(&closable))) {
239 if (wgctx->frame_pool) {
240 ComPtr<IClosable> closable;
241 if (SUCCEEDED(wgctx->frame_pool.As(&closable))) {
248 wgctx->capture_session.Reset();
249 wgctx->frame_pool.Reset();
250 wgctx->capture_item.Reset();
251 wgctx->d3d_device.Reset();
258 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
260 if (!
ctx->capture_hwnd) {
261 wgctx->client_area_offsets.left = 0;
262 wgctx->client_area_offsets.top = 0;
263 wgctx->client_area_offsets.right = 0;
264 wgctx->client_area_offsets.bottom = 0;
268 RECT client_rect = {};
269 RECT frame_bounds = {};
270 RECT window_rect = {};
272 if (IsIconic(
ctx->capture_hwnd)) {
277 if (!GetClientRect(
ctx->capture_hwnd, &client_rect)) {
283 if (!MapWindowPoints(
ctx->capture_hwnd,
nullptr, (POINT*)&client_rect, 2) && GetLastError()) {
288 if (FAILED(
ctx->fn.DwmGetWindowAttribute(
ctx->capture_hwnd, DWMWA_EXTENDED_FRAME_BOUNDS, &frame_bounds,
sizeof(window_rect))))
291 if (!GetWindowRect(
ctx->capture_hwnd, &window_rect))
294 if (wgctx->cap_size.Width == frame_bounds.right - frame_bounds.left ||
295 wgctx->cap_size.Height == frame_bounds.bottom - frame_bounds.top) {
297 }
else if (wgctx->cap_size.Width == window_rect.right - window_rect.left ||
298 wgctx->cap_size.Height == window_rect.bottom - window_rect.top) {
300 frame_bounds = window_rect;
302 if ((frame_bounds.top == frame_bounds.bottom || frame_bounds.left == frame_bounds.right) &&
303 (window_rect.top == window_rect.bottom || window_rect.left == window_rect.right))
308 av_log(avctx,
AV_LOG_VERBOSE,
"Failed to get valid window rect, client area may be inaccurate\n");
312 wgctx->client_area_offsets.left =
FFMAX(client_rect.left - frame_bounds.left, 0);
313 wgctx->client_area_offsets.top =
FFMAX(client_rect.top - frame_bounds.top, 0);
314 wgctx->client_area_offsets.right =
FFMAX(frame_bounds.right - client_rect.right, 0);
315 wgctx->client_area_offsets.bottom =
FFMAX(frame_bounds.bottom - client_rect.bottom, 0);
317 av_log(avctx,
AV_LOG_DEBUG,
"Client area offsets: left=%ld top=%ld right=%ld bottom=%ld\n",
318 wgctx->client_area_offsets.left, wgctx->client_area_offsets.top,
319 wgctx->client_area_offsets.right, wgctx->client_area_offsets.bottom);
328 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
331 ComPtr<IDirect3D11CaptureFramePoolStatics2> frame_pool_statics;
332 ComPtr<ID3D11Device> d3d11_device =
ctx->device_hwctx->device;
333 ComPtr<ID3D10Multithread> d3d10_multithread;
334 ComPtr<IDXGIDevice> dxgi_device;
335 ComPtr<IGraphicsCaptureSession2> session2;
336 ComPtr<IGraphicsCaptureSession3> session3;
337 ComPtr<IGraphicsCaptureSession5> session5;
339 DirectXPixelFormat fmt = DirectXPixelFormat::DirectXPixelFormat_B8G8R8A8UIntNormalized;
341 fmt = DirectXPixelFormat::DirectXPixelFormat_R16G16B16A16Float;
343 CHECK_HR_RET(wgctx->capture_item->get_Size(&wgctx->cap_size));
349 d3d10_multithread->SetMultithreadProtected(TRUE);
352 CHECK_HR_RET(
ctx->fn.CreateDirect3D11DeviceFromDXGIDevice(dxgi_device.Get(), &wgctx->d3d_device));
354 CHECK_HR_RET(get_activation_factory<IDirect3D11CaptureFramePoolStatics2>(
ctx, RuntimeClass_Windows_Graphics_Capture_Direct3D11CaptureFramePool, &frame_pool_statics));
356 CHECK_HR_RET(wgctx->frame_pool->CreateCaptureSession(wgctx->capture_item.Get(), &wgctx->capture_session));
358 if (SUCCEEDED(wgctx->capture_session.As(&session2))) {
359 if (FAILED(session2->put_IsCursorCaptureEnabled(cctx->
capture_cursor))) {
366 if (SUCCEEDED(wgctx->capture_session.As(&session3))) {
368 if (FAILED(session3->put_IsBorderRequired(cctx->
display_border))) {
375 if (SUCCEEDED(wgctx->capture_session.As(&session5))) {
377 if (FAILED(session5->put_MinUpdateInterval(ivl))) {
378 av_log(avctx,
AV_LOG_WARNING,
"Failed setting minimum update interval, framerate may be limited\n");
381 av_log(avctx,
AV_LOG_WARNING,
"Setting minimum update interval unavailable, framerate may be limited\n");
384 wgctx->window_closed = 0;
387 create_cb_handler<ITypedEventHandler<GraphicsCaptureItem*,IInspectable*>, IGraphicsCaptureItem*, IInspectable*>(
388 [avctx,
ctx](
auto,
auto) {
389 av_log(avctx, AV_LOG_INFO,
"Capture item closed\n");
390 wgc_closed_handler(ctx->wgc);
392 }).Get(), &wgctx->closed_token));
395 create_cb_handler<ITypedEventHandler<Direct3D11CaptureFramePool*,IInspectable*>, IDirect3D11CaptureFramePool*, IInspectable*>(
396 [avctx,
ctx](
auto,
auto) {
397 av_log(avctx, AV_LOG_TRACE,
"Frame arrived\n");
398 wgc_frame_arrived_handler(ctx->wgc);
400 }).Get(), &wgctx->frame_arrived_token));
409 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
413 ComPtr<IGraphicsCaptureItemInterop> capture_item_interop;
414 CHECK_HR_RET(get_activation_factory<IGraphicsCaptureItemInterop>(
ctx, RuntimeClass_Windows_Graphics_Capture_GraphicsCaptureItem, &capture_item_interop));
416 if (
ctx->capture_hmonitor) {
417 hr = capture_item_interop->CreateForMonitor(
ctx->capture_hmonitor, IID_PPV_ARGS(&wgctx->capture_item));
419 av_log(avctx,
AV_LOG_ERROR,
"Failed to setup graphics capture for monitor (0x%08lX)\n", hr);
422 }
else if (
ctx->capture_hwnd) {
423 hr = capture_item_interop->CreateForWindow(
ctx->capture_hwnd, IID_PPV_ARGS(&wgctx->capture_item));
425 av_log(avctx,
AV_LOG_ERROR,
"Failed to setup graphics capture for window (0x%08lX)\n", hr);
436 hr =
ctx->wgc->capture_session->StartCapture();
438 av_log(avctx,
AV_LOG_ERROR,
"Failed to start graphics capture session (0x%08lX)\n", hr);
449 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
451 ComPtr<IDirect3DSurface> capture_surface;
452 ComPtr<IDirect3DDxgiInterfaceAccess> dxgi_interface_access;
453 ComPtr<ID3D11Texture2D> frame_texture;
456 CHECK_HR_RET(wgctx->frame_pool->TryGetNextFrame(&capture_frame));
464 DirectXPixelFormat fmt = DirectXPixelFormat::DirectXPixelFormat_B8G8R8A8UIntNormalized;
466 fmt = DirectXPixelFormat::DirectXPixelFormat_R16G16B16A16Float;
483 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
487 PeekMessage(&msg,
nullptr, 0, 0, PM_NOREMOVE);
489 DispatcherQueueOptions
options = { 0 };
490 options.dwSize =
sizeof(DispatcherQueueOptions);
491 options.threadType = DISPATCHERQUEUE_THREAD_TYPE::DQTYPE_THREAD_CURRENT;
492 options.apartmentType = DISPATCHERQUEUE_THREAD_APARTMENTTYPE::DQTAT_COM_NONE;
495 CHECK_HR_RET(wgctx->dispatcher_queue_controller->get_DispatcherQueue(&wgctx->dispatcher_queue));
508 ctx->fn.RoUninitialize();
518 ctx->wgc = std::make_unique<GfxCaptureContextWgc>();
520 ctx->fn.SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
522 hr =
ctx->fn.RoInitialize(RO_INIT_MULTITHREADED);
552 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
553 ComPtr<IAsyncAction> async;
558 while (BOOL res = GetMessage(&msg,
NULL, 0, 0)) {
569 if (FAILED(wgctx->dispatcher_queue_controller->ShutdownQueueAsync(&async))) {
573 async->put_Completed(create_cb_handler<IAsyncActionCompletedHandler, IAsyncAction*, AsyncStatus>(
575 PostThreadMessage(
ctx->wgc_thread_id, WM_QUIT, 0, 0);
584 TranslateMessage(&msg);
585 DispatchMessage(&msg);
589 av_log(avctx,
AV_LOG_ERROR,
"WGC Thread message loop ended without proper shutdown\n");
604 static const wchar_t name_prefix[] =
L"wgc_winrt@0x";
605 wchar_t thread_name[
FF_ARRAY_ELEMS(name_prefix) +
sizeof(
void*) * 2] = { 0 };
606 swprintf(thread_name,
FF_ARRAY_ELEMS(thread_name),
L"%ls%" PRIxPTR, name_prefix, (uintptr_t)avctx);
607 ctx->fn.SetThreadDescription(GetCurrentThread(), thread_name);
609 std::lock_guard init_lock(
ctx->wgc_thread_init_mutex);
610 ctx->wgc_thread_id = GetCurrentThreadId();
614 }
catch (
const std::bad_alloc &) {
616 }
catch (
const std::exception &e) {
617 av_log(avctx,
AV_LOG_ERROR,
"unhandled exception in WGC thread init: %s\n", e.what());
624 ctx->wgc_thread_init_cond.notify_all();
625 if (
ctx->wgc_thread_init_res < 0) {
626 ctx->wgc_thread_res =
ctx->wgc_thread_init_res;
635 }
catch (
const std::bad_alloc &) {
637 }
catch (
const std::exception &e) {
638 av_log(avctx,
AV_LOG_ERROR,
"unhandled exception in WGC thread worker: %s\n", e.what());
645 std::lock_guard uninit_lock(
ctx->wgc_thread_uninit_mutex);
648 ctx->wgc_thread_res =
ret;
661 if (
ctx->wgc_thread.joinable()) {
665 ctx->wgc_thread.join();
666 ret =
ctx->wgc_thread_res;
668 ctx->wgc_thread_id = 0;
679 if (
ctx->wgc_thread.joinable() ||
ctx->wgc_thread_id) {
684 std::unique_lock wgc_lock(
ctx->wgc_thread_init_mutex);
685 ctx->wgc_thread_init_res = INT_MAX;
689 }
catch (
const std::system_error &e) {
694 if (!
ctx->wgc_thread_init_cond.wait_for(wgc_lock, std::chrono::seconds(1), [&]() {
695 return ctx->wgc_thread_init_res != INT_MAX;
701 return ctx->wgc_thread_init_res;
704 template <
typename F>
709 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
711 std::lock_guard uninit_lock(
ctx->wgc_thread_uninit_mutex);
719 std::condition_variable
cond;
724 auto cbdata =
ctx->wgc_thread_cb_data ?
725 std::static_pointer_cast<CBData>(
ctx->wgc_thread_cb_data) :
726 std::make_shared<CBData>();
727 ctx->wgc_thread_cb_data = cbdata;
729 cbdata->done = cbdata->cancel =
false;
734 create_cb_handler<IDispatcherQueueHandler>(
735 [
cb = std::forward<F>(
cb), cbdata]() {
737 std::lock_guard lock(cbdata->mutex);
743 } catch (const std::bad_alloc &) {
744 cbdata->ret = AVERROR(ENOMEM);
746 cbdata->ret = AVERROR_BUG;
752 cbdata->cond.notify_one();
760 std::unique_lock cblock(cbdata->mutex);
761 if (!cbdata->cond.wait_for(cblock, std::chrono::seconds(1), [&]() { return cbdata->done; })) {
762 cbdata->cancel =
true;
779 std::string pat(pattern);
781 auto flags = std::regex::ECMAScript | std::regex::optimize;
782 if (pat.rfind(
"(?i)", 0) == 0 || pat.rfind(
"(?I)", 0) == 0) {
784 flags |= std::regex::icase;
785 }
else if(pat.rfind(
"(?c)", 0) == 0 || pat.rfind(
"(?C)", 0) == 0) {
791 }
catch (
const std::regex_error &e) {
792 av_log(avctx,
AV_LOG_ERROR,
"Failed to compile regex '%s': %s\n", pat.c_str(), e.what());
803 int utf8size = WideCharToMultiByte(CP_UTF8, 0, in, -1,
nullptr, 0,
nullptr,
nullptr);
808 out->resize(utf8size - 1);
810 if (WideCharToMultiByte(CP_UTF8, 0, in, -1,
out->data(), utf8size,
nullptr,
nullptr) != utf8size)
821 if (!GetWindowThreadProcessId(hwnd, &pid))
824 handle_ptr_t proc(OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid));
828 std::wstring image_name;
829 DWORD image_name_size = 512;
832 DWORD
len = image_name_size;
833 image_name.resize(
len);
834 if (QueryFullProcessImageNameW(proc.get(), 0, image_name.data(), &
len)) {
835 image_name.resize(
len);
838 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
839 image_name_size *= 2;
845 if (image_name.empty())
848 const wchar_t *
base = image_name.c_str();
849 size_t pos = image_name.find_last_of(
L"\\/");
850 if (
pos != std::string::npos)
874 av_log(avctx, AV_LOG_DEBUG,
"Found capture monitor: %d\n", cctx->monitor_idx);
875 ctx->capture_hmonitor = hmonitor;
880 if (EnumDisplayMonitors(
NULL,
NULL,
cb->proc,
cb->lparam) || !
ctx->capture_hmonitor)
884 std::regex text_regex;
888 std::regex class_regex;
892 std::regex exe_regex;
896 std::string window_text;
897 std::wstring window_text_w;
898 std::string window_class;
899 std::wstring window_class_w;
900 std::string window_exe;
903 if (!GetWindowRect(hwnd, &
r) ||
r.right <=
r.left ||
r.bottom <=
r.top || !IsWindowVisible(hwnd))
906 window_text_w.resize(GetWindowTextLengthW(hwnd) + 1);
907 int len = GetWindowTextW(hwnd, window_text_w.data(), (
int)window_text_w.size());
909 window_text_w.resize(
len);
916 window_class_w.resize(256);
917 len = GetClassNameW(hwnd, window_class_w.data(), (
int)window_class_w.size());
919 window_class_w.resize(
len);
921 window_class.clear();
923 window_class.clear();
929 hwnd, window_text.c_str(), window_class.c_str(), window_exe.c_str());
932 if (window_text.empty() || !std::regex_search(window_text, text_regex))
937 if (window_class.empty() || !std::regex_search(window_class, class_regex))
942 if (window_exe.empty() || !std::regex_search(window_exe, exe_regex))
947 window_text.c_str(), window_class.c_str(), window_exe.c_str());
948 ctx->capture_hwnd = hwnd;
951 if (EnumWindows(
cb->proc,
cb->lparam) || !
ctx->capture_hwnd)
955 ctx->capture_hmonitor = MonitorFromWindow(
ctx->capture_hwnd, MONITOR_DEFAULTTONEAREST);
957 if (!
ctx->capture_hmonitor) {
991 *
out =
reinterpret_cast<T>(GetProcAddress(hModule.get(), lpProcName));
999 #define LOAD_DLL(handle, name) \
1000 handle = hmodule_ptr_t(LoadLibraryExW(L##name, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32)); \
1002 av_log(avctx, AV_LOG_ERROR, "Failed opening " #name "\n"); \
1003 return AVERROR(ENOSYS); \
1006 #define LOAD_FUNC(handle, name) \
1007 GetProcAddressTyped(handle, #name, &ctx->fn.name); \
1008 if (!ctx->fn.name) { \
1009 av_log(avctx, AV_LOG_ERROR, "Failed loading " #name "\n"); \
1010 return AVERROR(ENOSYS); \
1014 LOAD_DLL(
ctx->fn.graphicscapture_handle,
"graphicscapture.dll");
1019 LOAD_DLL(
ctx->fn.coremsg_handle,
"coremessaging.dll");
1021 LOAD_DLL(
ctx->fn.kernel32_handle,
"kernel32.dll");
1022 LOAD_DLL(
ctx->fn.d3dcompiler_handle,
"d3dcompiler_47.dll");
1026 LOAD_FUNC(
ctx->fn.combase_handle, RoGetActivationFactory);
1027 LOAD_FUNC(
ctx->fn.combase_handle, WindowsCreateStringReference);
1029 LOAD_FUNC(
ctx->fn.dwmapi_handle, DwmGetWindowAttribute);
1031 LOAD_FUNC(
ctx->fn.d3d11_handle, CreateDirect3D11DeviceFromDXGIDevice);
1033 LOAD_FUNC(
ctx->fn.coremsg_handle, CreateDispatcherQueueController);
1035 LOAD_FUNC(
ctx->fn.user32_handle, SetThreadDpiAwarenessContext);
1037 LOAD_FUNC(
ctx->fn.kernel32_handle, SetThreadDescription);
1052 ctx->d3d = std::make_unique<GfxCaptureContextD3D>();
1056 ctx->fn.RoUninitialize =
nullptr;
1074 if (!
ctx->frames_ref)
1086 ctx->frames_hwctx->BindFlags = D3D11_BIND_RENDER_TARGET;
1104 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
1125 cap_w -= wgctx->client_area_offsets.left + wgctx->client_area_offsets.right;
1126 cap_h -= wgctx->client_area_offsets.top + wgctx->client_area_offsets.bottom;
1146 std::unique_ptr<GfxCaptureContextD3D> &d3dctx =
ctx->d3d;
1149 ComPtr<ID3DBlob> vs_blob, ps_blob, err_blob;
1150 CD3D11_SAMPLER_DESC sampler_desc(CD3D11_DEFAULT{});
1151 UINT
flags = D3DCOMPILE_OPTIMIZATION_LEVEL3;
1153 hr =
ctx->fn.D3DCompile(
render_shader_src,
sizeof(
render_shader_src) - 1,
NULL,
NULL,
NULL,
"main_vs",
"vs_4_0",
flags, 0, &vs_blob, &err_blob);
1156 av_log(avctx,
AV_LOG_ERROR,
"Failed compiling vertex shader: %.*s\n", (
int)err_blob->GetBufferSize(), (
char*)err_blob->GetBufferPointer());
1163 const char *ps_entry =
"main_ps_bicubic";
1165 ps_entry =
"main_ps";
1166 sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
1169 hr =
ctx->fn.D3DCompile(
render_shader_src,
sizeof(
render_shader_src) - 1,
NULL,
NULL,
NULL, ps_entry,
"ps_4_0",
flags, 0, &ps_blob, &err_blob);
1172 av_log(avctx,
AV_LOG_ERROR,
"Failed compiling pixel shader: %.*s\n", (
int)err_blob->GetBufferSize(), (
char*)err_blob->GetBufferPointer());
1179 CHECK_HR_RET(
ctx->device_hwctx->device->CreateVertexShader(vs_blob->GetBufferPointer(), vs_blob->GetBufferSize(),
NULL, &d3dctx->vertex_shader));
1180 CHECK_HR_RET(
ctx->device_hwctx->device->CreatePixelShader(ps_blob->GetBufferPointer(), ps_blob->GetBufferSize(),
NULL, &d3dctx->pixel_shader));
1182 CHECK_HR_RET(
ctx->device_hwctx->device->CreateSamplerState(&sampler_desc, &d3dctx->sampler_state));
1184 D3D11_BUFFER_DESC cb_desc = { 0 };
1185 cb_desc.ByteWidth = 48;
1186 cb_desc.Usage = D3D11_USAGE_DYNAMIC;
1187 cb_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
1188 cb_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
1190 CHECK_HR_RET(
ctx->device_hwctx->device->CreateBuffer(&cb_desc,
NULL, &d3dctx->shader_cb));
1192 CHECK_HR_RET(
ctx->device_hwctx->device->CreateDeferredContext(0, &d3dctx->deferred_ctx));
1215 if (!
ctx->device_ref)
1253 std::lock_guard wgc_lock(
ctx->wgc_thread_uninit_mutex);
1259 outlink->
w =
ctx->frames_ctx->width;
1260 outlink->
h =
ctx->frames_ctx->height;
1274 std::unique_ptr<GfxCaptureContextD3D> &d3dctx =
ctx->d3d;
1275 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
1278 ID3D11DeviceContext *dev_ctx =
ctx->device_hwctx->device_context;
1279 ComPtr<ID3D11DeviceContext> &def_ctx = d3dctx->deferred_ctx;
1281 D3D11_TEXTURE2D_DESC dst_tex_desc;
1282 reinterpret_cast<ID3D11Texture2D*
>(
frame->data[0])->GetDesc(&dst_tex_desc);
1284 D3D11_TEXTURE2D_DESC src_tex_desc;
1285 src_tex->GetDesc(&src_tex_desc);
1287 D3D11_RENDER_TARGET_VIEW_DESC target_desc = {};
1288 target_desc.Format = dst_tex_desc.Format;
1290 if (dst_tex_desc.ArraySize > 1) {
1291 target_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
1292 target_desc.Texture2DArray.ArraySize = 1;
1293 target_desc.Texture2DArray.FirstArraySlice = (uintptr_t)
frame->data[1];
1294 target_desc.Texture2DArray.MipSlice = 0;
1296 target_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
1297 target_desc.Texture2D.MipSlice = 0;
1300 ComPtr<ID3D11RenderTargetView> rtv;
1302 reinterpret_cast<ID3D11Resource*
>(
frame->data[0]), &target_desc, &rtv));
1304 ComPtr<ID3D11ShaderResourceView> srv;
1305 CHECK_HR_RET(dev->CreateShaderResourceView(src_tex.Get(),
nullptr, &srv));
1313 crop_left += wgctx->client_area_offsets.left;
1314 crop_top += wgctx->client_area_offsets.top;
1315 crop_right += wgctx->client_area_offsets.right;
1316 crop_bottom += wgctx->client_area_offsets.bottom;
1325 int cropped_w = wgctx->cap_size.Width - crop_left - crop_right;
1326 int cropped_h = wgctx->cap_size.Height - crop_top - crop_bottom;
1328 D3D11_VIEWPORT viewport = { 0 };
1329 viewport.MinDepth = 0.f;
1330 viewport.MaxDepth = 1.f;
1334 viewport.Width = (
float)cropped_w;
1335 viewport.Height = (
float)cropped_h;
1338 viewport.Width = dst_tex_desc.Width;
1339 viewport.Height = dst_tex_desc.Height;
1342 float scale =
FFMIN(dst_tex_desc.Width / (
float)cropped_w,
1343 dst_tex_desc.Height / (
float)cropped_h);
1344 viewport.Width = cropped_w *
scale;
1345 viewport.Height = cropped_h *
scale;
1353 def_ctx->RSSetViewports(1, &viewport);
1355 D3D11_MAPPED_SUBRESOURCE
map;
1356 CHECK_HR_RET(def_ctx->Map(d3dctx->shader_cb.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &
map));
1358 float *cb_f =
static_cast<float*
>(
map.pData);
1359 uint32_t *cb_u =
static_cast<uint32_t*
>(
map.pData);
1360 cb_f[0] = (
float)cropped_w;
1361 cb_f[1] = (
float)cropped_h;
1362 cb_f[2] = viewport.Width;
1363 cb_f[3] = viewport.Height;
1364 cb_f[4] = crop_left / (
float)src_tex_desc.Width;
1365 cb_f[5] = crop_top / (
float)src_tex_desc.Height;
1366 cb_f[6] = (crop_left + cropped_w) / (
float)src_tex_desc.Width;
1367 cb_f[7] = (crop_top + cropped_h) / (
float)src_tex_desc.Height;
1372 def_ctx->Unmap(d3dctx->shader_cb.Get(), 0);
1374 def_ctx->OMSetRenderTargets(1, rtv.GetAddressOf(),
nullptr);
1376 const float clear_color[4] = {0.f, 0.f, 0.f, 1.f};
1377 def_ctx->ClearRenderTargetView(rtv.Get(), clear_color);
1379 def_ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
1380 def_ctx->VSSetShader(d3dctx->vertex_shader.Get(),
nullptr, 0);
1381 def_ctx->VSSetConstantBuffers(0, 1, d3dctx->shader_cb.GetAddressOf());
1382 def_ctx->PSSetShader(d3dctx->pixel_shader.Get(),
nullptr, 0);
1383 def_ctx->PSSetSamplers(0, 1, d3dctx->sampler_state.GetAddressOf());
1384 def_ctx->PSSetShaderResources(0, 1, srv.GetAddressOf());
1385 def_ctx->PSSetConstantBuffers(0, 1, d3dctx->shader_cb.GetAddressOf());
1387 def_ctx->Draw(3, 0);
1389 ComPtr<ID3D11CommandList> cmd_list;
1390 CHECK_HR_RET(def_ctx->FinishCommandList(FALSE, &cmd_list));
1391 dev_ctx->ExecuteCommandList(cmd_list.Get(), FALSE);
1406 ComPtr<IDirect3D11CaptureFrame> capture_frame;
1407 ComPtr<IDirect3DSurface> capture_surface;
1408 ComPtr<IDirect3DDxgiInterfaceAccess> dxgi_interface_access;
1409 ComPtr<ID3D11Texture2D> frame_texture;
1410 TimeSpan frame_time = { 0 };
1416 CHECK_HR_RET(capture_frame->get_SystemRelativeTime(&frame_time));
1418 CHECK_HR_RET(capture_frame->get_Surface(&capture_surface));
1419 CHECK_HR_RET(capture_surface.As(&dxgi_interface_access));
1420 CHECK_HR_RET(dxgi_interface_access->GetInterface(IID_PPV_ARGS(&frame_texture)));
1429 frame->pts = frame_time.Duration;
1454 if (!
ctx->first_pts)
1466 std::unique_ptr<GfxCaptureContextWgc> &wgctx =
ctx->wgc;
1468 std::lock_guard wgc_lock(
ctx->wgc_thread_uninit_mutex);
1478 uint64_t last_seq = wgctx->frame_seq;
1484 std::unique_lock
frame_lock(wgctx->frame_arrived_mutex);
1486 if (wgctx->window_closed && wgctx->frame_seq == last_seq) {
1491 if (!wgctx->frame_arrived_cond.wait_for(
frame_lock, std::chrono::seconds(1), [&]() {
1492 return wgctx->frame_seq != last_seq || wgctx->window_closed;
1504 }
catch (
const std::exception &e) {
1515 }
catch (
const std::bad_alloc&) {
1517 }
catch (
const std::exception &e) {
1530 }
catch (
const std::bad_alloc&) {
1532 }
catch (
const std::exception &e) {
1547 }
catch (
const std::bad_alloc&) {
1549 }
catch (
const std::exception &e) {
1550 av_log(avctx,
AV_LOG_ERROR,
"unhandled exception during config_props: %s\n", e.what());