19#include <freerdp/config.h>
21#include <winpr/assert.h>
23#include <winpr/print.h>
24#include <winpr/library.h>
25#include <freerdp/log.h>
29#define TAG SERVER_TAG("shadow.win")
33static D3D_DRIVER_TYPE DriverTypes[] = {
34 D3D_DRIVER_TYPE_HARDWARE,
36 D3D_DRIVER_TYPE_REFERENCE,
39static UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
41static D3D_FEATURE_LEVEL FeatureLevels[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1,
42 D3D_FEATURE_LEVEL_10_0, D3D_FEATURE_LEVEL_9_1 };
44static UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
46static HMODULE d3d11_module =
nullptr;
48typedef HRESULT(WINAPI* fnD3D11CreateDevice)(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType,
49 HMODULE Software, UINT Flags,
50 CONST D3D_FEATURE_LEVEL* pFeatureLevels,
51 UINT FeatureLevels, UINT SDKVersion,
52 ID3D11Device** ppDevice,
53 D3D_FEATURE_LEVEL* pFeatureLevel,
54 ID3D11DeviceContext** ppImmediateContext);
56static fnD3D11CreateDevice pfnD3D11CreateDevice =
nullptr;
65DEFINE_GUID(IID_ID3D11DeviceChild, 0x1841e5c8, 0x16b0, 0x489b, 0xbc, 0xc8, 0x44, 0xcf, 0xb0, 0xd5,
67DEFINE_GUID(IID_ID3D11DepthStencilState, 0x03823efb, 0x8d8f, 0x4e1c, 0x9a, 0xa2, 0xf6, 0x4b, 0xb2,
69DEFINE_GUID(IID_ID3D11BlendState, 0x75b68faa, 0x347d, 0x4159, 0x8f, 0x45, 0xa0, 0x64, 0x0f, 0x01,
71DEFINE_GUID(IID_ID3D11RasterizerState, 0x9bb4ab81, 0xab1a, 0x4d8f, 0xb5, 0x06, 0xfc, 0x04, 0x20,
73DEFINE_GUID(IID_ID3D11Resource, 0xdc8e63f3, 0xd12b, 0x4952, 0xb4, 0x7b, 0x5e, 0x45, 0x02, 0x6a,
75DEFINE_GUID(IID_ID3D11Buffer, 0x48570b85, 0xd1ee, 0x4fcd, 0xa2, 0x50, 0xeb, 0x35, 0x07, 0x22, 0xb0,
77DEFINE_GUID(IID_ID3D11Texture1D, 0xf8fb5c27, 0xc6b3, 0x4f75, 0xa4, 0xc8, 0x43, 0x9a, 0xf2, 0xef,
79DEFINE_GUID(IID_ID3D11Texture2D, 0x6f15aaf2, 0xd208, 0x4e89, 0x9a, 0xb4, 0x48, 0x95, 0x35, 0xd3,
81DEFINE_GUID(IID_ID3D11Texture3D, 0x037e866e, 0xf56d, 0x4357, 0xa8, 0xaf, 0x9d, 0xab, 0xbe, 0x6e,
83DEFINE_GUID(IID_ID3D11View, 0x839d1216, 0xbb2e, 0x412b, 0xb7, 0xf4, 0xa9, 0xdb, 0xeb, 0xe0, 0x8e,
85DEFINE_GUID(IID_ID3D11ShaderResourceView, 0xb0e06fe0, 0x8192, 0x4e1a, 0xb1, 0xca, 0x36, 0xd7, 0x41,
87DEFINE_GUID(IID_ID3D11RenderTargetView, 0xdfdba067, 0x0b8d, 0x4865, 0x87, 0x5b, 0xd7, 0xb4, 0x51,
89DEFINE_GUID(IID_ID3D11DepthStencilView, 0x9fdac92a, 0x1876, 0x48c3, 0xaf, 0xad, 0x25, 0xb9, 0x4f,
91DEFINE_GUID(IID_ID3D11UnorderedAccessView, 0x28acf509, 0x7f5c, 0x48f6, 0x86, 0x11, 0xf3, 0x16, 0x01,
93DEFINE_GUID(IID_ID3D11VertexShader, 0x3b301d64, 0xd678, 0x4289, 0x88, 0x97, 0x22, 0xf8, 0x92, 0x8b,
95DEFINE_GUID(IID_ID3D11HullShader, 0x8e5c6061, 0x628a, 0x4c8e, 0x82, 0x64, 0xbb, 0xe4, 0x5c, 0xb3,
97DEFINE_GUID(IID_ID3D11DomainShader, 0xf582c508, 0x0f36, 0x490c, 0x99, 0x77, 0x31, 0xee, 0xce, 0x26,
99DEFINE_GUID(IID_ID3D11GeometryShader, 0x38325b96, 0xeffb, 0x4022, 0xba, 0x02, 0x2e, 0x79, 0x5b,
101DEFINE_GUID(IID_ID3D11PixelShader, 0xea82e40d, 0x51dc, 0x4f33, 0x93, 0xd4, 0xdb, 0x7c, 0x91, 0x25,
103DEFINE_GUID(IID_ID3D11ComputeShader, 0x4f5b196e, 0xc2bd, 0x495e, 0xbd, 0x01, 0x1f, 0xde, 0xd3, 0x8e,
105DEFINE_GUID(IID_ID3D11InputLayout, 0xe4819ddc, 0x4cf0, 0x4025, 0xbd, 0x26, 0x5d, 0xe8, 0x2a, 0x3e,
107DEFINE_GUID(IID_ID3D11SamplerState, 0xda6fea51, 0x564c, 0x4487, 0x98, 0x10, 0xf0, 0xd0, 0xf9, 0xb4,
109DEFINE_GUID(IID_ID3D11Asynchronous, 0x4b35d0cd, 0x1e15, 0x4258, 0x9c, 0x98, 0x1b, 0x13, 0x33, 0xf6,
111DEFINE_GUID(IID_ID3D11Query, 0xd6c00747, 0x87b7, 0x425e, 0xb8, 0x4d, 0x44, 0xd1, 0x08, 0x56, 0x0a,
113DEFINE_GUID(IID_ID3D11Predicate, 0x9eb576dd, 0x9f77, 0x4d86, 0x81, 0xaa, 0x8b, 0xab, 0x5f, 0xe4,
115DEFINE_GUID(IID_ID3D11Counter, 0x6e8c49fb, 0xa371, 0x4770, 0xb4, 0x40, 0x29, 0x08, 0x60, 0x22, 0xb7,
117DEFINE_GUID(IID_ID3D11ClassInstance, 0xa6cd7faa, 0xb0b7, 0x4a2f, 0x94, 0x36, 0x86, 0x62, 0xa6, 0x57,
119DEFINE_GUID(IID_ID3D11ClassLinkage, 0xddf57cba, 0x9543, 0x46e4, 0xa1, 0x2b, 0xf2, 0x07, 0xa0, 0xfe,
121DEFINE_GUID(IID_ID3D11CommandList, 0xa24bc4d1, 0x769e, 0x43f7, 0x80, 0x13, 0x98, 0xff, 0x56, 0x6c,
123DEFINE_GUID(IID_ID3D11DeviceContext, 0xc0bfa96c, 0xe089, 0x44fb, 0x8e, 0xaf, 0x26, 0xf8, 0x79, 0x61,
125DEFINE_GUID(IID_ID3D11VideoDecoder, 0x3C9C5B51, 0x995D, 0x48d1, 0x9B, 0x8D, 0xFA, 0x5C, 0xAE, 0xDE,
127DEFINE_GUID(IID_ID3D11VideoProcessorEnumerator, 0x31627037, 0x53AB, 0x4200, 0x90, 0x61, 0x05, 0xFA,
128 0xA9, 0xAB, 0x45, 0xF9);
129DEFINE_GUID(IID_ID3D11VideoProcessor, 0x1D7B0652, 0x185F, 0x41c6, 0x85, 0xCE, 0x0C, 0x5B, 0xE3,
131DEFINE_GUID(IID_ID3D11AuthenticatedChannel, 0x3015A308, 0xDCBD, 0x47aa, 0xA7, 0x47, 0x19, 0x24,
132 0x86, 0xD1, 0x4D, 0x4A);
133DEFINE_GUID(IID_ID3D11CryptoSession, 0x9B32F9AD, 0xBDCC, 0x40a6, 0xA3, 0x9D, 0xD5, 0xC8, 0x65, 0x84,
135DEFINE_GUID(IID_ID3D11VideoDecoderOutputView, 0xC2931AEA, 0x2A85, 0x4f20, 0x86, 0x0F, 0xFB, 0xA1,
136 0xFD, 0x25, 0x6E, 0x18);
137DEFINE_GUID(IID_ID3D11VideoProcessorInputView, 0x11EC5A5F, 0x51DC, 0x4945, 0xAB, 0x34, 0x6E, 0x8C,
138 0x21, 0x30, 0x0E, 0xA5);
139DEFINE_GUID(IID_ID3D11VideoProcessorOutputView, 0xA048285E, 0x25A9, 0x4527, 0xBD, 0x93, 0xD6, 0x8B,
140 0x68, 0xC4, 0x42, 0x54);
141DEFINE_GUID(IID_ID3D11VideoContext, 0x61F21C45, 0x3C0E, 0x4a74, 0x9C, 0xEA, 0x67, 0x10, 0x0D, 0x9A,
143DEFINE_GUID(IID_ID3D11VideoDevice, 0x10EC4D5B, 0x975A, 0x4689, 0xB9, 0xE4, 0xD0, 0xAA, 0xC3, 0x0F,
145DEFINE_GUID(IID_ID3D11Device, 0xdb6f6ddb, 0xac77, 0x4e88, 0x82, 0x53, 0x81, 0x9d, 0xf9, 0xbb, 0xf1,
150DEFINE_GUID(IID_IDXGIObject, 0xaec22fb8, 0x76f3, 0x4639, 0x9b, 0xe0, 0x28, 0xeb, 0x43, 0xa6, 0x7a,
152DEFINE_GUID(IID_IDXGIDeviceSubObject, 0x3d3e0379, 0xf9de, 0x4d58, 0xbb, 0x6c, 0x18, 0xd6, 0x29,
154DEFINE_GUID(IID_IDXGIResource, 0x035f3ab4, 0x482e, 0x4e50, 0xb4, 0x1f, 0x8a, 0x7f, 0x8b, 0xd8, 0x96,
156DEFINE_GUID(IID_IDXGIKeyedMutex, 0x9d8e1289, 0xd7b3, 0x465f, 0x81, 0x26, 0x25, 0x0e, 0x34, 0x9a,
158DEFINE_GUID(IID_IDXGISurface, 0xcafcb56c, 0x6ac3, 0x4889, 0xbf, 0x47, 0x9e, 0x23, 0xbb, 0xd2, 0x60,
160DEFINE_GUID(IID_IDXGISurface1, 0x4AE63092, 0x6327, 0x4c1b, 0x80, 0xAE, 0xBF, 0xE1, 0x2E, 0xA3, 0x2B,
162DEFINE_GUID(IID_IDXGIAdapter, 0x2411e7e1, 0x12ac, 0x4ccf, 0xbd, 0x14, 0x97, 0x98, 0xe8, 0x53, 0x4d,
164DEFINE_GUID(IID_IDXGIOutput, 0xae02eedb, 0xc735, 0x4690, 0x8d, 0x52, 0x5a, 0x8d, 0xc2, 0x02, 0x13,
166DEFINE_GUID(IID_IDXGISwapChain, 0x310d36a0, 0xd2e7, 0x4c0a, 0xaa, 0x04, 0x6a, 0x9d, 0x23, 0xb8,
168DEFINE_GUID(IID_IDXGIFactory, 0x7b7166ec, 0x21c7, 0x44ae, 0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3,
170DEFINE_GUID(IID_IDXGIDevice, 0x54ec77fa, 0x1377, 0x44e6, 0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8,
172DEFINE_GUID(IID_IDXGIFactory1, 0x770aae78, 0xf26f, 0x4dba, 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3,
174DEFINE_GUID(IID_IDXGIAdapter1, 0x29038f61, 0x3839, 0x4626, 0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a,
176DEFINE_GUID(IID_IDXGIDevice1, 0x77db970f, 0x6276, 0x48ba, 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39,
181DEFINE_GUID(IID_IDXGIDisplayControl, 0xea9dbf1a, 0xc88e, 0x4486, 0x85, 0x4a, 0x98, 0xaa, 0x01, 0x38,
183DEFINE_GUID(IID_IDXGIOutputDuplication, 0x191cfac3, 0xa341, 0x470d, 0xb2, 0x6e, 0xa8, 0x64, 0xf4,
185DEFINE_GUID(IID_IDXGISurface2, 0xaba496dd, 0xb617, 0x4cb8, 0xa8, 0x66, 0xbc, 0x44, 0xd7, 0xeb, 0x1f,
187DEFINE_GUID(IID_IDXGIResource1, 0x30961379, 0x4609, 0x4a41, 0x99, 0x8e, 0x54, 0xfe, 0x56, 0x7e,
189DEFINE_GUID(IID_IDXGIDevice2, 0x05008617, 0xfbfd, 0x4051, 0xa7, 0x90, 0x14, 0x48, 0x84, 0xb4, 0xf6,
191DEFINE_GUID(IID_IDXGISwapChain1, 0x790a45f7, 0x0d42, 0x4876, 0x98, 0x3a, 0x0a, 0x55, 0xcf, 0xe6,
193DEFINE_GUID(IID_IDXGIFactory2, 0x50c83a1c, 0xe072, 0x4c48, 0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6,
195DEFINE_GUID(IID_IDXGIAdapter2, 0x0AA1AE0A, 0xFA0E, 0x4B84, 0x86, 0x44, 0xE0, 0x5F, 0xF8, 0xE5, 0xAC,
197DEFINE_GUID(IID_IDXGIOutput1, 0x00cddea8, 0x939b, 0x4b83, 0xa3, 0x40, 0xa6, 0x85, 0x22, 0x66, 0x66,
201const char* GetDxgiErrorString(HRESULT hr)
205 case DXGI_STATUS_OCCLUDED:
206 return "DXGI_STATUS_OCCLUDED";
207 case DXGI_STATUS_CLIPPED:
208 return "DXGI_STATUS_CLIPPED";
209 case DXGI_STATUS_NO_REDIRECTION:
210 return "DXGI_STATUS_NO_REDIRECTION";
211 case DXGI_STATUS_NO_DESKTOP_ACCESS:
212 return "DXGI_STATUS_NO_DESKTOP_ACCESS";
213 case DXGI_STATUS_GRAPHICS_VIDPN_SOURCE_IN_USE:
214 return "DXGI_STATUS_GRAPHICS_VIDPN_SOURCE_IN_USE";
215 case DXGI_STATUS_MODE_CHANGED:
216 return "DXGI_STATUS_MODE_CHANGED";
217 case DXGI_STATUS_MODE_CHANGE_IN_PROGRESS:
218 return "DXGI_STATUS_MODE_CHANGE_IN_PROGRESS";
219 case DXGI_ERROR_INVALID_CALL:
220 return "DXGI_ERROR_INVALID_CALL";
221 case DXGI_ERROR_NOT_FOUND:
222 return "DXGI_ERROR_NOT_FOUND";
223 case DXGI_ERROR_MORE_DATA:
224 return "DXGI_ERROR_MORE_DATA";
225 case DXGI_ERROR_UNSUPPORTED:
226 return "DXGI_ERROR_UNSUPPORTED";
227 case DXGI_ERROR_DEVICE_REMOVED:
228 return "DXGI_ERROR_DEVICE_REMOVED";
229 case DXGI_ERROR_DEVICE_HUNG:
230 return "DXGI_ERROR_DEVICE_HUNG";
231 case DXGI_ERROR_DEVICE_RESET:
232 return "DXGI_ERROR_DEVICE_RESET";
233 case DXGI_ERROR_WAS_STILL_DRAWING:
234 return "DXGI_ERROR_WAS_STILL_DRAWING";
235 case DXGI_ERROR_FRAME_STATISTICS_DISJOINT:
236 return "DXGI_ERROR_FRAME_STATISTICS_DISJOINT";
237 case DXGI_ERROR_GRAPHICS_VIDPN_SOURCE_IN_USE:
238 return "DXGI_ERROR_GRAPHICS_VIDPN_SOURCE_IN_USE";
239 case DXGI_ERROR_DRIVER_INTERNAL_ERROR:
240 return "DXGI_ERROR_DRIVER_INTERNAL_ERROR";
241 case DXGI_ERROR_NONEXCLUSIVE:
242 return "DXGI_ERROR_NONEXCLUSIVE";
243 case DXGI_ERROR_NOT_CURRENTLY_AVAILABLE:
244 return "DXGI_ERROR_NOT_CURRENTLY_AVAILABLE";
245 case DXGI_ERROR_REMOTE_CLIENT_DISCONNECTED:
246 return "DXGI_ERROR_REMOTE_CLIENT_DISCONNECTED";
247 case DXGI_ERROR_REMOTE_OUTOFMEMORY:
248 return "DXGI_ERROR_REMOTE_OUTOFMEMORY";
249 case DXGI_ERROR_ACCESS_LOST:
250 return "DXGI_ERROR_ACCESS_LOST";
251 case DXGI_ERROR_WAIT_TIMEOUT:
252 return "DXGI_ERROR_WAIT_TIMEOUT";
253 case DXGI_ERROR_SESSION_DISCONNECTED:
254 return "DXGI_ERROR_SESSION_DISCONNECTED";
255 case DXGI_ERROR_RESTRICT_TO_OUTPUT_STALE:
256 return "DXGI_ERROR_RESTRICT_TO_OUTPUT_STALE";
257 case DXGI_ERROR_CANNOT_PROTECT_CONTENT:
258 return "DXGI_ERROR_CANNOT_PROTECT_CONTENT";
259 case DXGI_ERROR_ACCESS_DENIED:
260 return "DXGI_ERROR_ACCESS_DENIED";
261 case DXGI_ERROR_NAME_ALREADY_EXISTS:
262 return "DXGI_ERROR_NAME_ALREADY_EXISTS";
263 case DXGI_ERROR_SDK_COMPONENT_MISSING:
264 return "DXGI_ERROR_SDK_COMPONENT_MISSING";
265 case DXGI_STATUS_UNOCCLUDED:
266 return "DXGI_STATUS_UNOCCLUDED";
267 case DXGI_STATUS_DDA_WAS_STILL_DRAWING:
268 return "DXGI_STATUS_DDA_WAS_STILL_DRAWING";
269 case DXGI_ERROR_MODE_CHANGE_IN_PROGRESS:
270 return "DXGI_ERROR_MODE_CHANGE_IN_PROGRESS";
271 case DXGI_DDI_ERR_WASSTILLDRAWING:
272 return "DXGI_DDI_ERR_WASSTILLDRAWING";
273 case DXGI_DDI_ERR_UNSUPPORTED:
274 return "DXGI_DDI_ERR_UNSUPPORTED";
275 case DXGI_DDI_ERR_NONEXCLUSIVE:
276 return "DXGI_DDI_ERR_NONEXCLUSIVE";
278 return "E_ACCESSDENIED";
281 return "DXGI_ERROR_UNKNOWN";
284static void win_shadow_d3d11_module_init()
289 d3d11_module = LoadLibraryA(
"d3d11.dll");
294 pfnD3D11CreateDevice = GetProcAddressAs(d3d11_module,
"D3D11CreateDevice", fnD3D11CreateDevice);
298static int win_shadow_dxgi_release_frame_data(winShadowSubsystem* subsystem);
301#define WIN_SHADOW_DXGI_DENIED_RETRY_MS 250
306#define WIN_SHADOW_DXGI_INIT_RETRY_MS 60000
312static BOOL win_shadow_dxgi_is_denied(HRESULT hr)
314 return (hr == DXGI_ERROR_ACCESS_DENIED) || (hr == E_ACCESSDENIED);
319static void win_shadow_dxgi_pause(winShadowSubsystem* subsystem,
const char* what, HRESULT hr)
321 WINPR_ASSERT(subsystem);
324 if (!subsystem->dxgiDenied)
326 "%s: %s (0x%08lX): this process may not capture the current desktop (the "
327 "secure desktop?), capture paused until it can",
328 what, GetDxgiErrorString(hr), hr);
329 subsystem->dxgiDenied = TRUE;
330 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_DENIED_RETRY_MS;
333static void win_shadow_dxgi_drop_duplication(winShadowSubsystem* subsystem)
335 WINPR_ASSERT(subsystem);
337 (void)win_shadow_dxgi_release_frame_data(subsystem);
339 if (subsystem->dxgiDesktopImage)
341 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
342 subsystem->dxgiDesktopImage =
nullptr;
345 if (subsystem->dxgiOutputDuplication)
347 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
348 subsystem->dxgiOutputDuplication =
nullptr;
351 if (subsystem->dxgiStage)
353 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
354 subsystem->dxgiStage =
nullptr;
359static int win_shadow_dxgi_init_duplication(winShadowSubsystem* subsystem)
362 HRESULT refusedHr = E_FAIL;
363 HRESULT deniedHr = E_FAIL;
365 BOOL refused = FALSE;
366 BOOL duplicated = FALSE;
367 UINT refusedIndex = 0;
368 UINT duplicatedIndex = 0;
369 D3D11_TEXTURE2D_DESC textureDesc;
370 IDXGIDevice* dxgiDevice =
nullptr;
371 IDXGIAdapter* dxgiAdapter =
nullptr;
373 WINPR_ASSERT(subsystem);
375 hr = subsystem->dxgiDevice->lpVtbl->QueryInterface(subsystem->dxgiDevice, &IID_IDXGIDevice,
376 (
void**)&dxgiDevice);
380 WLog_ERR(TAG,
"ID3D11Device::QueryInterface(IDXGIDevice) failure: %s (0x%08lX)",
381 GetDxgiErrorString(hr), hr);
385 hr = dxgiDevice->lpVtbl->GetParent(dxgiDevice, &IID_IDXGIAdapter, (
void**)&dxgiAdapter);
389 dxgiDevice->lpVtbl->Release(dxgiDevice);
390 dxgiDevice =
nullptr;
395 WLog_ERR(TAG,
"IDXGIDevice::GetParent(IDXGIAdapter) failure: %s (0x%08lX)",
396 GetDxgiErrorString(hr), hr);
408 for (UINT pass = 0; !duplicated && (pass < 2); pass++)
410 for (UINT index = 0; !duplicated; index++)
412 IDXGIOutput* output =
nullptr;
413 IDXGIOutput1* output1 =
nullptr;
414 DXGI_OUTPUT_DESC desc = WINPR_C_ARRAY_INIT;
417 hr = dxgiAdapter->lpVtbl->EnumOutputs(dxgiAdapter, index, &output);
423 hr = output->lpVtbl->GetDesc(output, &desc);
427 WLog_WARN(TAG,
"IDXGIOutput %u::GetDesc failure: %s (0x%08lX)", index,
428 GetDxgiErrorString(hr), hr);
429 output->lpVtbl->Release(output);
433 if (!desc.AttachedToDesktop)
435 output->lpVtbl->Release(output);
439 primary = (desc.DesktopCoordinates.left == 0) && (desc.DesktopCoordinates.top == 0);
442 if (primary != (pass == 0))
444 output->lpVtbl->Release(output);
448 hr = output->lpVtbl->QueryInterface(output, &IID_IDXGIOutput1, (
void**)&output1);
449 output->lpVtbl->Release(output);
453 WLog_WARN(TAG,
"IDXGIOutput %u::QueryInterface(IDXGIOutput1) failure: %s (0x%08lX)",
454 index, GetDxgiErrorString(hr), hr);
458 hr = output1->lpVtbl->DuplicateOutput(output1, (IUnknown*)subsystem->dxgiDevice,
459 &(subsystem->dxgiOutputDuplication));
460 output1->lpVtbl->Release(output1);
465 duplicatedIndex = index;
466 WLog_INFO(TAG,
"duplicating output %u (%ldx%ld at %ld,%ld)", index,
467 desc.DesktopCoordinates.right - desc.DesktopCoordinates.left,
468 desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top,
469 desc.DesktopCoordinates.left, desc.DesktopCoordinates.top);
473 WLog_DBG(TAG,
"IDXGIOutput %u::DuplicateOutput failure: %s (0x%08lX)", index,
474 GetDxgiErrorString(hr), hr);
479 refusedIndex = index;
483 if (win_shadow_dxgi_is_denied(hr))
493 dxgiAdapter->lpVtbl->Release(dxgiAdapter);
494 dxgiAdapter =
nullptr;
503 win_shadow_dxgi_pause(subsystem,
"IDXGIOutput1::DuplicateOutput", deniedHr);
510 WLog_ERR(TAG,
"no output of the adapter can be duplicated, next attempt in %u ms",
511 WIN_SHADOW_DXGI_INIT_RETRY_MS);
512 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_INIT_RETRY_MS;
518 "IDXGIOutput %u refused the duplication: %s (0x%08lX), duplicating output "
520 refusedIndex, GetDxgiErrorString(refusedHr), refusedHr, duplicatedIndex);
524 if (win_shadow_check_resize(subsystem) < 0)
527 textureDesc.Width = subsystem->width;
528 textureDesc.Height = subsystem->height;
529 textureDesc.MipLevels = 1;
530 textureDesc.ArraySize = 1;
531 textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
532 textureDesc.SampleDesc.Count = 1;
533 textureDesc.SampleDesc.Quality = 0;
534 textureDesc.Usage = D3D11_USAGE_STAGING;
535 textureDesc.BindFlags = 0;
536 textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
537 textureDesc.MiscFlags = 0;
539 hr = subsystem->dxgiDevice->lpVtbl->CreateTexture2D(subsystem->dxgiDevice, &textureDesc,
540 nullptr, &(subsystem->dxgiStage));
544 WLog_ERR(TAG,
"ID3D11Device::CreateTexture2D failure: %s (0x%08lX)", GetDxgiErrorString(hr),
549 if (subsystem->dxgiDenied)
551 WLog_INFO(TAG,
"capture resumed");
552 subsystem->dxgiDenied = FALSE;
557int win_shadow_dxgi_init(winShadowSubsystem* subsystem)
562 UINT DriverTypeIndex;
563 IDXGIDevice* DxgiDevice =
nullptr;
564 IDXGIAdapter* DxgiAdapter =
nullptr;
565 IDXGIOutput* DxgiOutput =
nullptr;
566 IDXGIOutput1* DxgiOutput1 =
nullptr;
568 win_shadow_d3d11_module_init();
570 if (!pfnD3D11CreateDevice)
574 WLog_ERR(TAG,
"d3d11.dll did not provide D3D11CreateDevice, next attempt in %u ms",
575 WIN_SHADOW_DXGI_INIT_RETRY_MS);
576 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_INIT_RETRY_MS;
580 for (DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
582 hr = pfnD3D11CreateDevice(
nullptr, DriverTypes[DriverTypeIndex],
nullptr, 0, FeatureLevels,
583 NumFeatureLevels, D3D11_SDK_VERSION, &(subsystem->dxgiDevice),
584 &(subsystem->featureLevel), &(subsystem->dxgiDeviceContext));
594 WLog_ERR(TAG,
"D3D11CreateDevice failure: 0x%08lX, next attempt in %u ms", hr,
595 WIN_SHADOW_DXGI_INIT_RETRY_MS);
596 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_INIT_RETRY_MS;
600 status = win_shadow_dxgi_init_duplication(subsystem);
605int win_shadow_dxgi_uninit(winShadowSubsystem* subsystem)
607 if (subsystem->dxgiStage)
609 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
610 subsystem->dxgiStage =
nullptr;
613 if (subsystem->dxgiDesktopImage)
615 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
616 subsystem->dxgiDesktopImage =
nullptr;
619 if (subsystem->dxgiOutputDuplication)
621 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
622 subsystem->dxgiOutputDuplication =
nullptr;
625 if (subsystem->dxgiDeviceContext)
627 subsystem->dxgiDeviceContext->lpVtbl->Release(subsystem->dxgiDeviceContext);
628 subsystem->dxgiDeviceContext =
nullptr;
631 if (subsystem->dxgiDevice)
633 subsystem->dxgiDevice->lpVtbl->Release(subsystem->dxgiDevice);
634 subsystem->dxgiDevice =
nullptr;
640int win_shadow_dxgi_fetch_frame_data(winShadowSubsystem* subsystem, BYTE** ppDstData,
641 int* pnDstStep,
int x,
int y,
int width,
int height)
646 DXGI_MAPPED_RECT mappedRect;
648 if ((width * height) < 1)
656 D3D11_TEXTURE2D_DESC srcDesc;
657 subsystem->dxgiDesktopImage->lpVtbl->GetDesc(subsystem->dxgiDesktopImage, &srcDesc);
658 if (x + width > (
int)srcDesc.Width)
659 width = (int)srcDesc.Width - x;
660 if (y + height > (
int)srcDesc.Height)
661 height = (int)srcDesc.Height - y;
666 Box.right = x + width;
667 Box.bottom = y + height;
671 subsystem->dxgiDeviceContext->lpVtbl->CopySubresourceRegion(
672 subsystem->dxgiDeviceContext, (ID3D11Resource*)subsystem->dxgiStage, 0, 0, 0, 0,
673 (ID3D11Resource*)subsystem->dxgiDesktopImage, 0, &Box);
675 if (subsystem->dxgiSurface)
677 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
678 subsystem->dxgiSurface =
nullptr;
681 hr = subsystem->dxgiStage->lpVtbl->QueryInterface(subsystem->dxgiStage, &IID_IDXGISurface,
682 (
void**)&(subsystem->dxgiSurface));
686 WLog_ERR(TAG,
"ID3D11Texture2D::QueryInterface(IDXGISurface) failure: %s 0x%08lX",
687 GetDxgiErrorString(hr), hr);
691 hr = subsystem->dxgiSurface->lpVtbl->Map(subsystem->dxgiSurface, &mappedRect, DXGI_MAP_READ);
695 if (win_shadow_dxgi_is_denied(hr))
697 win_shadow_dxgi_pause(subsystem,
"IDXGISurface::Map", hr);
698 win_shadow_dxgi_drop_duplication(subsystem);
702 WLog_ERR(TAG,
"IDXGISurface::Map failure: %s 0x%08lX", GetDxgiErrorString(hr), hr);
704 if (hr == DXGI_ERROR_DEVICE_REMOVED)
706 win_shadow_dxgi_uninit(subsystem);
708 status = win_shadow_dxgi_init(subsystem);
715 else if (hr == DXGI_ERROR_ACCESS_DENIED)
720 if (subsystem->dxgiSurface)
722 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
723 subsystem->dxgiSurface =
nullptr;
724 subsystem->dxgiSurfaceMapped = FALSE;
727 if (subsystem->dxgiStage)
729 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
730 subsystem->dxgiStage =
nullptr;
733 if (subsystem->dxgiOutputDuplication)
735 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
736 subsystem->dxgiOutputDuplication =
nullptr;
739 for (
int attempt = 0; attempt < 240; ++attempt)
741 if (WaitForSingleObject(subsystem->base.server->StopEvent, 0) == WAIT_OBJECT_0)
745 status = win_shadow_dxgi_init_duplication(subsystem);
756 subsystem->dxgiSurfaceMapped = TRUE;
758 *ppDstData = mappedRect.pBits;
759 *pnDstStep = mappedRect.Pitch;
764int win_shadow_dxgi_release_frame_data(winShadowSubsystem* subsystem)
766 if (subsystem->dxgiSurface)
768 if (subsystem->dxgiSurfaceMapped)
770 subsystem->dxgiSurface->lpVtbl->Unmap(subsystem->dxgiSurface);
771 subsystem->dxgiSurfaceMapped = FALSE;
774 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
775 subsystem->dxgiSurface =
nullptr;
778 if (subsystem->dxgiOutputDuplication)
780 if (subsystem->dxgiFrameAcquired)
782 subsystem->dxgiOutputDuplication->lpVtbl->ReleaseFrame(
783 subsystem->dxgiOutputDuplication);
784 subsystem->dxgiFrameAcquired = FALSE;
788 subsystem->pendingFrames = 0;
797int win_shadow_dxgi_restart_duplication(winShadowSubsystem* subsystem)
799 WINPR_ASSERT(subsystem);
801 if (!subsystem->dxgiDevice)
802 return win_shadow_dxgi_init(subsystem);
804 win_shadow_dxgi_drop_duplication(subsystem);
805 return win_shadow_dxgi_init_duplication(subsystem);
808int win_shadow_dxgi_get_next_frame(winShadowSubsystem* subsystem)
814 UINT DataBufferSize = 0;
815 BYTE* DataBuffer =
nullptr;
817 if (subsystem->dxgiFrameAcquired)
819 win_shadow_dxgi_release_frame_data(subsystem);
822 if (subsystem->dxgiDesktopImage)
824 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
825 subsystem->dxgiDesktopImage =
nullptr;
828 hr = subsystem->dxgiOutputDuplication->lpVtbl->AcquireNextFrame(
829 subsystem->dxgiOutputDuplication, timeout, &(subsystem->dxgiFrameInfo),
830 &(subsystem->dxgiResource));
834 subsystem->dxgiFrameAcquired = TRUE;
835 subsystem->pendingFrames = subsystem->dxgiFrameInfo.AccumulatedFrames;
838 if (hr == DXGI_ERROR_WAIT_TIMEOUT)
843 if (win_shadow_dxgi_is_denied(hr))
847 win_shadow_dxgi_pause(subsystem,
"IDXGIOutputDuplication::AcquireNextFrame", hr);
848 win_shadow_dxgi_drop_duplication(subsystem);
852 WLog_ERR(TAG,
"IDXGIOutputDuplication::AcquireNextFrame failure: %s (0x%08lX)",
853 GetDxgiErrorString(hr), hr);
855 if (hr == DXGI_ERROR_ACCESS_LOST)
857 win_shadow_dxgi_release_frame_data(subsystem);
859 if (subsystem->dxgiDesktopImage)
861 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
862 subsystem->dxgiDesktopImage =
nullptr;
865 if (subsystem->dxgiStage)
867 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
868 subsystem->dxgiStage =
nullptr;
871 if (subsystem->dxgiOutputDuplication)
873 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
874 subsystem->dxgiOutputDuplication =
nullptr;
877 status = win_shadow_dxgi_init_duplication(subsystem);
884 else if (hr == DXGI_ERROR_ACCESS_DENIED)
890 win_shadow_dxgi_release_frame_data(subsystem);
892 if (subsystem->dxgiDesktopImage)
894 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
895 subsystem->dxgiDesktopImage =
nullptr;
898 if (subsystem->dxgiStage)
900 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
901 subsystem->dxgiStage =
nullptr;
904 if (subsystem->dxgiOutputDuplication)
906 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
907 subsystem->dxgiOutputDuplication =
nullptr;
910 for (
int attempt = 0; attempt < 240; ++attempt)
912 if (WaitForSingleObject(subsystem->base.server->StopEvent, 0) == WAIT_OBJECT_0)
916 status = win_shadow_dxgi_init_duplication(subsystem);
923 else if (hr == DXGI_ERROR_INVALID_CALL)
925 win_shadow_dxgi_uninit(subsystem);
927 status = win_shadow_dxgi_init(subsystem);
938 hr = subsystem->dxgiResource->lpVtbl->QueryInterface(
939 subsystem->dxgiResource, &IID_ID3D11Texture2D, (
void**)&(subsystem->dxgiDesktopImage));
941 if (subsystem->dxgiResource)
943 subsystem->dxgiResource->lpVtbl->Release(subsystem->dxgiResource);
944 subsystem->dxgiResource =
nullptr;
949 WLog_ERR(TAG,
"IDXGIResource::QueryInterface(ID3D11Texture2D) failure: %s (0x%08lX)",
950 GetDxgiErrorString(hr), hr);
957int win_shadow_dxgi_get_invalid_region(winShadowSubsystem* subsystem)
967 UINT MetadataBufferSize;
968 UINT MoveRectsBufferSize;
969 UINT DirtyRectsBufferSize;
970 RECT* pDirtyRectsBuffer;
971 DXGI_OUTDUPL_MOVE_RECT* pMoveRect;
972 DXGI_OUTDUPL_MOVE_RECT* pMoveRectBuffer;
973 rdpShadowSurface* surface =
nullptr;
975 WINPR_ASSERT(subsystem);
976 WINPR_ASSERT(subsystem->base.server);
977 surface = subsystem->base.server->surface;
978 WINPR_ASSERT(surface);
980 if (subsystem->dxgiFrameInfo.AccumulatedFrames == 0)
983 if (subsystem->dxgiFrameInfo.TotalMetadataBufferSize == 0)
986 MetadataBufferSize = subsystem->dxgiFrameInfo.TotalMetadataBufferSize;
988 if (MetadataBufferSize > subsystem->MetadataBufferSize)
990 subsystem->MetadataBuffer = (BYTE*)realloc(subsystem->MetadataBuffer, MetadataBufferSize);
992 if (!subsystem->MetadataBuffer)
995 subsystem->MetadataBufferSize = MetadataBufferSize;
1002 MoveRectsBufferSize = MetadataBufferSize - UsedBufferSize;
1003 pMoveRectBuffer = (DXGI_OUTDUPL_MOVE_RECT*)&(subsystem->MetadataBuffer[UsedBufferSize]);
1005 hr = subsystem->dxgiOutputDuplication->lpVtbl->GetFrameMoveRects(
1006 subsystem->dxgiOutputDuplication, MoveRectsBufferSize, pMoveRectBuffer,
1007 &MoveRectsBufferSize);
1012 "IDXGIOutputDuplication::GetFrameMoveRects failure: %s (0x%08lX) Size: %u Total "
1014 GetDxgiErrorString(hr), hr, MoveRectsBufferSize, MetadataBufferSize,
1021 UsedBufferSize += MoveRectsBufferSize;
1023 DirtyRectsBufferSize = MetadataBufferSize - UsedBufferSize;
1024 pDirtyRectsBuffer = (RECT*)&(subsystem->MetadataBuffer[UsedBufferSize]);
1026 hr = subsystem->dxgiOutputDuplication->lpVtbl->GetFrameDirtyRects(
1027 subsystem->dxgiOutputDuplication, DirtyRectsBufferSize, pDirtyRectsBuffer,
1028 &DirtyRectsBufferSize);
1033 "IDXGIOutputDuplication::GetFrameDirtyRects failure: %s (0x%08lX) Size: %u Total "
1035 GetDxgiErrorString(hr), hr, DirtyRectsBufferSize, MetadataBufferSize,
1040 numMoveRects = MoveRectsBufferSize /
sizeof(DXGI_OUTDUPL_MOVE_RECT);
1042 for (UINT i = 0; i < numMoveRects; i++)
1044 pMoveRect = &pMoveRectBuffer[i];
1045 pSrcPt = &(pMoveRect->SourcePoint);
1046 pDstRect = &(pMoveRect->DestinationRect);
1048 invalidRect.left = (UINT16)pDstRect->left;
1049 invalidRect.top = (UINT16)pDstRect->top;
1050 invalidRect.right = (UINT16)pDstRect->right;
1051 invalidRect.bottom = (UINT16)pDstRect->bottom;
1053 region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
1056 numDirtyRects = DirtyRectsBufferSize /
sizeof(RECT);
1058 for (UINT i = 0; i < numDirtyRects; i++)
1060 pDirtyRect = &pDirtyRectsBuffer[i];
1062 invalidRect.left = (UINT16)pDirtyRect->left;
1063 invalidRect.top = (UINT16)pDirtyRect->top;
1064 invalidRect.right = (UINT16)pDirtyRect->right;
1065 invalidRect.bottom = (UINT16)pDirtyRect->bottom;
1067 region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);