FreeRDP
Loading...
Searching...
No Matches
win_dxgi.c
1
18
19#include <freerdp/config.h>
20
21#include <winpr/assert.h>
22#include <winpr/crt.h>
23#include <winpr/print.h>
24#include <winpr/library.h>
25#include <freerdp/log.h>
26
27#include "win_dxgi.h"
28
29#define TAG SERVER_TAG("shadow.win")
30
31#ifdef WITH_DXGI_1_2
32
33static D3D_DRIVER_TYPE DriverTypes[] = {
34 D3D_DRIVER_TYPE_HARDWARE,
35 D3D_DRIVER_TYPE_WARP,
36 D3D_DRIVER_TYPE_REFERENCE,
37};
38
39static UINT NumDriverTypes = ARRAYSIZE(DriverTypes);
40
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 };
43
44static UINT NumFeatureLevels = ARRAYSIZE(FeatureLevels);
45
46static HMODULE d3d11_module = nullptr;
47
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);
55
56static fnD3D11CreateDevice pfnD3D11CreateDevice = nullptr;
57
58#undef DEFINE_GUID
59#define INITGUID
60
61#include <initguid.h>
62
63/* d3d11.h GUIDs */
64
65DEFINE_GUID(IID_ID3D11DeviceChild, 0x1841e5c8, 0x16b0, 0x489b, 0xbc, 0xc8, 0x44, 0xcf, 0xb0, 0xd5,
66 0xde, 0xae);
67DEFINE_GUID(IID_ID3D11DepthStencilState, 0x03823efb, 0x8d8f, 0x4e1c, 0x9a, 0xa2, 0xf6, 0x4b, 0xb2,
68 0xcb, 0xfd, 0xf1);
69DEFINE_GUID(IID_ID3D11BlendState, 0x75b68faa, 0x347d, 0x4159, 0x8f, 0x45, 0xa0, 0x64, 0x0f, 0x01,
70 0xcd, 0x9a);
71DEFINE_GUID(IID_ID3D11RasterizerState, 0x9bb4ab81, 0xab1a, 0x4d8f, 0xb5, 0x06, 0xfc, 0x04, 0x20,
72 0x0b, 0x6e, 0xe7);
73DEFINE_GUID(IID_ID3D11Resource, 0xdc8e63f3, 0xd12b, 0x4952, 0xb4, 0x7b, 0x5e, 0x45, 0x02, 0x6a,
74 0x86, 0x2d);
75DEFINE_GUID(IID_ID3D11Buffer, 0x48570b85, 0xd1ee, 0x4fcd, 0xa2, 0x50, 0xeb, 0x35, 0x07, 0x22, 0xb0,
76 0x37);
77DEFINE_GUID(IID_ID3D11Texture1D, 0xf8fb5c27, 0xc6b3, 0x4f75, 0xa4, 0xc8, 0x43, 0x9a, 0xf2, 0xef,
78 0x56, 0x4c);
79DEFINE_GUID(IID_ID3D11Texture2D, 0x6f15aaf2, 0xd208, 0x4e89, 0x9a, 0xb4, 0x48, 0x95, 0x35, 0xd3,
80 0x4f, 0x9c);
81DEFINE_GUID(IID_ID3D11Texture3D, 0x037e866e, 0xf56d, 0x4357, 0xa8, 0xaf, 0x9d, 0xab, 0xbe, 0x6e,
82 0x25, 0x0e);
83DEFINE_GUID(IID_ID3D11View, 0x839d1216, 0xbb2e, 0x412b, 0xb7, 0xf4, 0xa9, 0xdb, 0xeb, 0xe0, 0x8e,
84 0xd1);
85DEFINE_GUID(IID_ID3D11ShaderResourceView, 0xb0e06fe0, 0x8192, 0x4e1a, 0xb1, 0xca, 0x36, 0xd7, 0x41,
86 0x47, 0x10, 0xb2);
87DEFINE_GUID(IID_ID3D11RenderTargetView, 0xdfdba067, 0x0b8d, 0x4865, 0x87, 0x5b, 0xd7, 0xb4, 0x51,
88 0x6c, 0xc1, 0x64);
89DEFINE_GUID(IID_ID3D11DepthStencilView, 0x9fdac92a, 0x1876, 0x48c3, 0xaf, 0xad, 0x25, 0xb9, 0x4f,
90 0x84, 0xa9, 0xb6);
91DEFINE_GUID(IID_ID3D11UnorderedAccessView, 0x28acf509, 0x7f5c, 0x48f6, 0x86, 0x11, 0xf3, 0x16, 0x01,
92 0x0a, 0x63, 0x80);
93DEFINE_GUID(IID_ID3D11VertexShader, 0x3b301d64, 0xd678, 0x4289, 0x88, 0x97, 0x22, 0xf8, 0x92, 0x8b,
94 0x72, 0xf3);
95DEFINE_GUID(IID_ID3D11HullShader, 0x8e5c6061, 0x628a, 0x4c8e, 0x82, 0x64, 0xbb, 0xe4, 0x5c, 0xb3,
96 0xd5, 0xdd);
97DEFINE_GUID(IID_ID3D11DomainShader, 0xf582c508, 0x0f36, 0x490c, 0x99, 0x77, 0x31, 0xee, 0xce, 0x26,
98 0x8c, 0xfa);
99DEFINE_GUID(IID_ID3D11GeometryShader, 0x38325b96, 0xeffb, 0x4022, 0xba, 0x02, 0x2e, 0x79, 0x5b,
100 0x70, 0x27, 0x5c);
101DEFINE_GUID(IID_ID3D11PixelShader, 0xea82e40d, 0x51dc, 0x4f33, 0x93, 0xd4, 0xdb, 0x7c, 0x91, 0x25,
102 0xae, 0x8c);
103DEFINE_GUID(IID_ID3D11ComputeShader, 0x4f5b196e, 0xc2bd, 0x495e, 0xbd, 0x01, 0x1f, 0xde, 0xd3, 0x8e,
104 0x49, 0x69);
105DEFINE_GUID(IID_ID3D11InputLayout, 0xe4819ddc, 0x4cf0, 0x4025, 0xbd, 0x26, 0x5d, 0xe8, 0x2a, 0x3e,
106 0x07, 0xb7);
107DEFINE_GUID(IID_ID3D11SamplerState, 0xda6fea51, 0x564c, 0x4487, 0x98, 0x10, 0xf0, 0xd0, 0xf9, 0xb4,
108 0xe3, 0xa5);
109DEFINE_GUID(IID_ID3D11Asynchronous, 0x4b35d0cd, 0x1e15, 0x4258, 0x9c, 0x98, 0x1b, 0x13, 0x33, 0xf6,
110 0xdd, 0x3b);
111DEFINE_GUID(IID_ID3D11Query, 0xd6c00747, 0x87b7, 0x425e, 0xb8, 0x4d, 0x44, 0xd1, 0x08, 0x56, 0x0a,
112 0xfd);
113DEFINE_GUID(IID_ID3D11Predicate, 0x9eb576dd, 0x9f77, 0x4d86, 0x81, 0xaa, 0x8b, 0xab, 0x5f, 0xe4,
114 0x90, 0xe2);
115DEFINE_GUID(IID_ID3D11Counter, 0x6e8c49fb, 0xa371, 0x4770, 0xb4, 0x40, 0x29, 0x08, 0x60, 0x22, 0xb7,
116 0x41);
117DEFINE_GUID(IID_ID3D11ClassInstance, 0xa6cd7faa, 0xb0b7, 0x4a2f, 0x94, 0x36, 0x86, 0x62, 0xa6, 0x57,
118 0x97, 0xcb);
119DEFINE_GUID(IID_ID3D11ClassLinkage, 0xddf57cba, 0x9543, 0x46e4, 0xa1, 0x2b, 0xf2, 0x07, 0xa0, 0xfe,
120 0x7f, 0xed);
121DEFINE_GUID(IID_ID3D11CommandList, 0xa24bc4d1, 0x769e, 0x43f7, 0x80, 0x13, 0x98, 0xff, 0x56, 0x6c,
122 0x18, 0xe2);
123DEFINE_GUID(IID_ID3D11DeviceContext, 0xc0bfa96c, 0xe089, 0x44fb, 0x8e, 0xaf, 0x26, 0xf8, 0x79, 0x61,
124 0x90, 0xda);
125DEFINE_GUID(IID_ID3D11VideoDecoder, 0x3C9C5B51, 0x995D, 0x48d1, 0x9B, 0x8D, 0xFA, 0x5C, 0xAE, 0xDE,
126 0xD6, 0x5C);
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,
130 0xD4, 0xAE, 0x6C);
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,
134 0x57, 0x20);
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,
142 0xD5, 0xE4);
143DEFINE_GUID(IID_ID3D11VideoDevice, 0x10EC4D5B, 0x975A, 0x4689, 0xB9, 0xE4, 0xD0, 0xAA, 0xC3, 0x0F,
144 0xE3, 0x33);
145DEFINE_GUID(IID_ID3D11Device, 0xdb6f6ddb, 0xac77, 0x4e88, 0x82, 0x53, 0x81, 0x9d, 0xf9, 0xbb, 0xf1,
146 0x40);
147
148/* dxgi.h GUIDs */
149
150DEFINE_GUID(IID_IDXGIObject, 0xaec22fb8, 0x76f3, 0x4639, 0x9b, 0xe0, 0x28, 0xeb, 0x43, 0xa6, 0x7a,
151 0x2e);
152DEFINE_GUID(IID_IDXGIDeviceSubObject, 0x3d3e0379, 0xf9de, 0x4d58, 0xbb, 0x6c, 0x18, 0xd6, 0x29,
153 0x92, 0xf1, 0xa6);
154DEFINE_GUID(IID_IDXGIResource, 0x035f3ab4, 0x482e, 0x4e50, 0xb4, 0x1f, 0x8a, 0x7f, 0x8b, 0xd8, 0x96,
155 0x0b);
156DEFINE_GUID(IID_IDXGIKeyedMutex, 0x9d8e1289, 0xd7b3, 0x465f, 0x81, 0x26, 0x25, 0x0e, 0x34, 0x9a,
157 0xf8, 0x5d);
158DEFINE_GUID(IID_IDXGISurface, 0xcafcb56c, 0x6ac3, 0x4889, 0xbf, 0x47, 0x9e, 0x23, 0xbb, 0xd2, 0x60,
159 0xec);
160DEFINE_GUID(IID_IDXGISurface1, 0x4AE63092, 0x6327, 0x4c1b, 0x80, 0xAE, 0xBF, 0xE1, 0x2E, 0xA3, 0x2B,
161 0x86);
162DEFINE_GUID(IID_IDXGIAdapter, 0x2411e7e1, 0x12ac, 0x4ccf, 0xbd, 0x14, 0x97, 0x98, 0xe8, 0x53, 0x4d,
163 0xc0);
164DEFINE_GUID(IID_IDXGIOutput, 0xae02eedb, 0xc735, 0x4690, 0x8d, 0x52, 0x5a, 0x8d, 0xc2, 0x02, 0x13,
165 0xaa);
166DEFINE_GUID(IID_IDXGISwapChain, 0x310d36a0, 0xd2e7, 0x4c0a, 0xaa, 0x04, 0x6a, 0x9d, 0x23, 0xb8,
167 0x88, 0x6a);
168DEFINE_GUID(IID_IDXGIFactory, 0x7b7166ec, 0x21c7, 0x44ae, 0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3,
169 0x69);
170DEFINE_GUID(IID_IDXGIDevice, 0x54ec77fa, 0x1377, 0x44e6, 0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8,
171 0x4c);
172DEFINE_GUID(IID_IDXGIFactory1, 0x770aae78, 0xf26f, 0x4dba, 0xa8, 0x29, 0x25, 0x3c, 0x83, 0xd1, 0xb3,
173 0x87);
174DEFINE_GUID(IID_IDXGIAdapter1, 0x29038f61, 0x3839, 0x4626, 0x91, 0xfd, 0x08, 0x68, 0x79, 0x01, 0x1a,
175 0x05);
176DEFINE_GUID(IID_IDXGIDevice1, 0x77db970f, 0x6276, 0x48ba, 0xba, 0x28, 0x07, 0x01, 0x43, 0xb4, 0x39,
177 0x2c);
178
179/* dxgi1_2.h GUIDs */
180
181DEFINE_GUID(IID_IDXGIDisplayControl, 0xea9dbf1a, 0xc88e, 0x4486, 0x85, 0x4a, 0x98, 0xaa, 0x01, 0x38,
182 0xf3, 0x0c);
183DEFINE_GUID(IID_IDXGIOutputDuplication, 0x191cfac3, 0xa341, 0x470d, 0xb2, 0x6e, 0xa8, 0x64, 0xf4,
184 0x28, 0x31, 0x9c);
185DEFINE_GUID(IID_IDXGISurface2, 0xaba496dd, 0xb617, 0x4cb8, 0xa8, 0x66, 0xbc, 0x44, 0xd7, 0xeb, 0x1f,
186 0xa2);
187DEFINE_GUID(IID_IDXGIResource1, 0x30961379, 0x4609, 0x4a41, 0x99, 0x8e, 0x54, 0xfe, 0x56, 0x7e,
188 0xe0, 0xc1);
189DEFINE_GUID(IID_IDXGIDevice2, 0x05008617, 0xfbfd, 0x4051, 0xa7, 0x90, 0x14, 0x48, 0x84, 0xb4, 0xf6,
190 0xa9);
191DEFINE_GUID(IID_IDXGISwapChain1, 0x790a45f7, 0x0d42, 0x4876, 0x98, 0x3a, 0x0a, 0x55, 0xcf, 0xe6,
192 0xf4, 0xaa);
193DEFINE_GUID(IID_IDXGIFactory2, 0x50c83a1c, 0xe072, 0x4c48, 0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6,
194 0xd0);
195DEFINE_GUID(IID_IDXGIAdapter2, 0x0AA1AE0A, 0xFA0E, 0x4B84, 0x86, 0x44, 0xE0, 0x5F, 0xF8, 0xE5, 0xAC,
196 0xB5);
197DEFINE_GUID(IID_IDXGIOutput1, 0x00cddea8, 0x939b, 0x4b83, 0xa3, 0x40, 0xa6, 0x85, 0x22, 0x66, 0x66,
198 0xcc);
199
200WINPR_ATTR_NODISCARD
201const char* GetDxgiErrorString(HRESULT hr)
202{
203 switch (hr)
204 {
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";
277 case E_ACCESSDENIED:
278 return "E_ACCESSDENIED";
279 }
280
281 return "DXGI_ERROR_UNKNOWN";
282}
283
284static void win_shadow_d3d11_module_init()
285{
286 if (d3d11_module)
287 return;
288
289 d3d11_module = LoadLibraryA("d3d11.dll");
290
291 if (!d3d11_module)
292 return;
293
294 pfnD3D11CreateDevice = GetProcAddressAs(d3d11_module, "D3D11CreateDevice", fnD3D11CreateDevice);
295}
296
297WINPR_ATTR_NODISCARD
298static int win_shadow_dxgi_release_frame_data(winShadowSubsystem* subsystem);
299
300/* How often a paused capture tries to duplicate the output again */
301#define WIN_SHADOW_DXGI_DENIED_RETRY_MS 250
302
303/* How often the capture tries again after a failure which does not recover by
304 * itself (no D3D11 device, no output of the adapter duplicatable at all):
305 * unlike a refused desktop, that state does not go away on its own */
306#define WIN_SHADOW_DXGI_INIT_RETRY_MS 60000
307
308/* How often the capture creates a fresh D3D11 device again after the device
309 * was removed or reset (driver crash, GPU change, TDR), and after a failure
310 * which classified itself in no other way: either state may clear soon, so
311 * retry at the same low rate as a refused desktop */
312#define WIN_SHADOW_DXGI_RESET_RETRY_MS 500
313
314/* The input desktop is one this process may not capture: the secure desktop (logon, lock
315 * screen, UAC) for a process in the user's session. DuplicateOutput says E_ACCESSDENIED,
316 * AcquireNextFrame and Map DXGI_ERROR_ACCESS_DENIED. */
317WINPR_ATTR_NODISCARD
318static BOOL win_shadow_dxgi_is_denied(HRESULT hr)
319{
320 return (hr == DXGI_ERROR_ACCESS_DENIED) || (hr == E_ACCESSDENIED);
321}
322
323/* Pause the capture: said once, then quiet until it resumes; the capture loop tries again
324 * after WIN_SHADOW_DXGI_DENIED_RETRY_MS (and at once when the input desktop changes) */
325static void win_shadow_dxgi_pause(winShadowSubsystem* subsystem, const char* what, HRESULT hr)
326{
327 WINPR_ASSERT(subsystem);
328 WINPR_ASSERT(what);
329
330 if (!subsystem->dxgiDenied)
331 WLog_WARN(TAG,
332 "%s: %s (0x%08lX): this process may not capture the current desktop (the "
333 "secure desktop?), capture paused until it can",
334 what, GetDxgiErrorString(hr), hr);
335 subsystem->dxgiDenied = TRUE;
336 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_DENIED_RETRY_MS;
337}
338
339/* A failure which classified itself in no other way must not spin the capture
340 * loop: retry at the reset rate. An earlier classification (a refused desktop,
341 * a failure which does not recover by itself) is kept. */
342static void win_shadow_dxgi_schedule_retry(winShadowSubsystem* subsystem)
343{
344 WINPR_ASSERT(subsystem);
345
346 if (GetTickCount64() >= subsystem->dxgiRetryTime)
347 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_RESET_RETRY_MS;
348}
349
350static void win_shadow_dxgi_drop_duplication(winShadowSubsystem* subsystem)
351{
352 WINPR_ASSERT(subsystem);
353
354 (void)win_shadow_dxgi_release_frame_data(subsystem);
355
356 if (subsystem->dxgiDesktopImage)
357 {
358 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
359 subsystem->dxgiDesktopImage = nullptr;
360 }
361
362 if (subsystem->dxgiOutputDuplication)
363 {
364 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
365 subsystem->dxgiOutputDuplication = nullptr;
366 }
367
368 if (subsystem->dxgiStage)
369 {
370 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
371 subsystem->dxgiStage = nullptr;
372 }
373}
374
375/* (Re)create the staging texture, the CPU readable copy of the desktop image
376 * the region copy reads from. It must hold the whole desktop image: the copy
377 * below maps it and hands its pitch to freerdp_image_copy_no_overlap(), which
378 * asserts when a row of the requested region does not fit in that pitch. */
379WINPR_ATTR_NODISCARD
380static int win_shadow_dxgi_create_stage(winShadowSubsystem* subsystem, UINT32 width, UINT32 height)
381{
382 D3D11_TEXTURE2D_DESC textureDesc = WINPR_C_ARRAY_INIT;
383 HRESULT hr;
384
385 WINPR_ASSERT(subsystem);
386 WINPR_ASSERT(subsystem->dxgiDevice);
387
388 if (subsystem->dxgiSurface)
389 {
390 if (subsystem->dxgiSurfaceMapped)
391 subsystem->dxgiSurface->lpVtbl->Unmap(subsystem->dxgiSurface);
392 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
393 subsystem->dxgiSurface = nullptr;
394 }
395 subsystem->dxgiSurfaceMapped = FALSE;
396
397 if (subsystem->dxgiStage)
398 {
399 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
400 subsystem->dxgiStage = nullptr;
401 }
402
403 textureDesc.Width = width;
404 textureDesc.Height = height;
405 textureDesc.MipLevels = 1;
406 textureDesc.ArraySize = 1;
407 textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
408 textureDesc.SampleDesc.Count = 1;
409 textureDesc.SampleDesc.Quality = 0;
410 textureDesc.Usage = D3D11_USAGE_STAGING;
411 textureDesc.BindFlags = 0;
412 textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
413 textureDesc.MiscFlags = 0;
414
415 hr = subsystem->dxgiDevice->lpVtbl->CreateTexture2D(subsystem->dxgiDevice, &textureDesc,
416 nullptr, &(subsystem->dxgiStage));
417
418 if (FAILED(hr))
419 {
420 WLog_ERR(TAG,
421 "ID3D11Device::CreateTexture2D(%" PRIu32 "x%" PRIu32 ") failure: %s (0x%08lX)",
422 width, height, GetDxgiErrorString(hr), hr);
423 return -1;
424 }
425
426 return 1;
427}
428
429WINPR_ATTR_NODISCARD
430static int win_shadow_dxgi_init_duplication(winShadowSubsystem* subsystem)
431{
432 HRESULT hr;
433 HRESULT refusedHr = E_FAIL;
434 HRESULT deniedHr = E_FAIL;
435 BOOL denied = FALSE;
436 BOOL refused = FALSE;
437 BOOL duplicated = FALSE;
438 UINT refusedIndex = 0;
439 UINT duplicatedIndex = 0;
440 IDXGIDevice* dxgiDevice = nullptr;
441 IDXGIAdapter* dxgiAdapter = nullptr;
442
443 WINPR_ASSERT(subsystem);
444
445 hr = subsystem->dxgiDevice->lpVtbl->QueryInterface(subsystem->dxgiDevice, &IID_IDXGIDevice,
446 (void**)&dxgiDevice);
447
448 if (FAILED(hr))
449 {
450 WLog_ERR(TAG, "ID3D11Device::QueryInterface(IDXGIDevice) failure: %s (0x%08lX)",
451 GetDxgiErrorString(hr), hr);
452 return -1;
453 }
454
455 hr = dxgiDevice->lpVtbl->GetParent(dxgiDevice, &IID_IDXGIAdapter, (void**)&dxgiAdapter);
456
457 if (dxgiDevice)
458 {
459 dxgiDevice->lpVtbl->Release(dxgiDevice);
460 dxgiDevice = nullptr;
461 }
462
463 if (FAILED(hr))
464 {
465 WLog_ERR(TAG, "IDXGIDevice::GetParent(IDXGIAdapter) failure: %s (0x%08lX)",
466 GetDxgiErrorString(hr), hr);
467 return -1;
468 }
469
470 /* Two passes over the adapter's outputs: the primary one first (its top
471 * left corner is the desktop origin, the monitor win_shadow_enum_monitors()
472 * describes), then the remaining attached ones. One output refusing the
473 * duplication must not end the search: an output of a virtual or an
474 * indirect display adapter does so for good while another output of the
475 * same adapter may duplicate fine, and any output does so while this
476 * process may not see the desktop. Only an adapter without a single
477 * duplicatable output fails the initialization. */
478 for (UINT pass = 0; !duplicated && (pass < 2); pass++)
479 {
480 for (UINT index = 0; !duplicated; index++)
481 {
482 IDXGIOutput* output = nullptr;
483 IDXGIOutput1* output1 = nullptr;
484 DXGI_OUTPUT_DESC desc = WINPR_C_ARRAY_INIT;
485 BOOL primary;
486
487 hr = dxgiAdapter->lpVtbl->EnumOutputs(dxgiAdapter, index, &output);
488
489 /* DXGI_ERROR_NOT_FOUND: no (more) output at this index */
490 if (FAILED(hr))
491 break;
492
493 hr = output->lpVtbl->GetDesc(output, &desc);
494
495 if (FAILED(hr))
496 {
497 WLog_WARN(TAG, "IDXGIOutput %u::GetDesc failure: %s (0x%08lX)", index,
498 GetDxgiErrorString(hr), hr);
499 output->lpVtbl->Release(output);
500 continue;
501 }
502
503 if (!desc.AttachedToDesktop)
504 {
505 output->lpVtbl->Release(output);
506 continue;
507 }
508
509 primary = (desc.DesktopCoordinates.left == 0) && (desc.DesktopCoordinates.top == 0);
510
511 /* the first pass takes the primary output, the second the others */
512 if (primary != (pass == 0))
513 {
514 output->lpVtbl->Release(output);
515 continue;
516 }
517
518 hr = output->lpVtbl->QueryInterface(output, &IID_IDXGIOutput1, (void**)&output1);
519 output->lpVtbl->Release(output);
520
521 if (FAILED(hr))
522 {
523 WLog_WARN(TAG, "IDXGIOutput %u::QueryInterface(IDXGIOutput1) failure: %s (0x%08lX)",
524 index, GetDxgiErrorString(hr), hr);
525 continue;
526 }
527
528 hr = output1->lpVtbl->DuplicateOutput(output1, (IUnknown*)subsystem->dxgiDevice,
529 &(subsystem->dxgiOutputDuplication));
530 output1->lpVtbl->Release(output1);
531
532 if (SUCCEEDED(hr))
533 {
534 duplicated = TRUE;
535 duplicatedIndex = index;
536 WLog_INFO(TAG, "duplicating output %u (%ldx%ld at %ld,%ld)", index,
537 desc.DesktopCoordinates.right - desc.DesktopCoordinates.left,
538 desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top,
539 desc.DesktopCoordinates.left, desc.DesktopCoordinates.top);
540 break;
541 }
542
543 WLog_DBG(TAG, "IDXGIOutput %u::DuplicateOutput failure: %s (0x%08lX)", index,
544 GetDxgiErrorString(hr), hr);
545
546 if (!refused)
547 {
548 refused = TRUE;
549 refusedIndex = index;
550 refusedHr = hr;
551 }
552
553 if (win_shadow_dxgi_is_denied(hr))
554 {
555 denied = TRUE;
556 deniedHr = hr;
557 }
558 }
559 }
560
561 if (dxgiAdapter)
562 {
563 dxgiAdapter->lpVtbl->Release(dxgiAdapter);
564 dxgiAdapter = nullptr;
565 }
566
567 if (!duplicated)
568 {
569 if (denied)
570 {
571 /* the outputs deny the duplication while this process may not see
572 * the desktop: the capture loop tries again from dxgiRetryTime on */
573 win_shadow_dxgi_pause(subsystem, "IDXGIOutput1::DuplicateOutput", deniedHr);
574 return -1;
575 }
576
577 /* this does not recover by itself (only virtual or indirect display
578 * adapter outputs, or none at all): the capture loop tries again at a
579 * low rate */
580 WLog_ERR(TAG, "no output of the adapter can be duplicated, next attempt in %u ms",
581 WIN_SHADOW_DXGI_INIT_RETRY_MS);
582 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_INIT_RETRY_MS;
583 return -1;
584 }
585
586 if (refused)
587 WLog_WARN(TAG,
588 "IDXGIOutput %u refused the duplication: %s (0x%08lX), duplicating output "
589 "%u instead",
590 refusedIndex, GetDxgiErrorString(refusedHr), refusedHr, duplicatedIndex);
591
592 /* the staging texture must match the current desktop size; a fatal
593 * resize error fails the duplication, the caller retries next tick */
594 if (win_shadow_check_resize(subsystem) < 0)
595 return -1;
596
597 if (win_shadow_dxgi_create_stage(subsystem, (UINT32)subsystem->width,
598 (UINT32)subsystem->height) < 0)
599 return -1;
600
601 if (subsystem->dxgiDenied)
602 {
603 WLog_INFO(TAG, "capture resumed");
604 subsystem->dxgiDenied = FALSE;
605 }
606 return 1;
607}
608
609int win_shadow_dxgi_init(winShadowSubsystem* subsystem)
610{
611 UINT i = 0;
612 HRESULT hr;
613 int status;
614 UINT DriverTypeIndex;
615 IDXGIDevice* DxgiDevice = nullptr;
616 IDXGIAdapter* DxgiAdapter = nullptr;
617 IDXGIOutput* DxgiOutput = nullptr;
618 IDXGIOutput1* DxgiOutput1 = nullptr;
619
620 win_shadow_d3d11_module_init();
621
622 if (!pfnD3D11CreateDevice)
623 {
624 /* this does not recover by itself: the capture loop tries again at a
625 * low rate */
626 WLog_ERR(TAG, "d3d11.dll did not provide D3D11CreateDevice, next attempt in %u ms",
627 WIN_SHADOW_DXGI_INIT_RETRY_MS);
628 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_INIT_RETRY_MS;
629 return -1;
630 }
631
632 for (DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
633 {
634 hr = pfnD3D11CreateDevice(nullptr, DriverTypes[DriverTypeIndex], nullptr, 0, FeatureLevels,
635 NumFeatureLevels, D3D11_SDK_VERSION, &(subsystem->dxgiDevice),
636 &(subsystem->featureLevel), &(subsystem->dxgiDeviceContext));
637
638 if (SUCCEEDED(hr))
639 break;
640 }
641
642 if (FAILED(hr))
643 {
644 /* this does not recover by itself: the capture loop tries again at a
645 * low rate */
646 WLog_ERR(TAG, "D3D11CreateDevice failure: 0x%08lX, next attempt in %u ms", hr,
647 WIN_SHADOW_DXGI_INIT_RETRY_MS);
648 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_INIT_RETRY_MS;
649 return -1;
650 }
651
652 status = win_shadow_dxgi_init_duplication(subsystem);
653
654 return status;
655}
656
657int win_shadow_dxgi_uninit(winShadowSubsystem* subsystem)
658{
659 if (subsystem->dxgiStage)
660 {
661 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
662 subsystem->dxgiStage = nullptr;
663 }
664
665 if (subsystem->dxgiDesktopImage)
666 {
667 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
668 subsystem->dxgiDesktopImage = nullptr;
669 }
670
671 if (subsystem->dxgiOutputDuplication)
672 {
673 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
674 subsystem->dxgiOutputDuplication = nullptr;
675 }
676
677 if (subsystem->dxgiDeviceContext)
678 {
679 subsystem->dxgiDeviceContext->lpVtbl->Release(subsystem->dxgiDeviceContext);
680 subsystem->dxgiDeviceContext = nullptr;
681 }
682
683 if (subsystem->dxgiDevice)
684 {
685 subsystem->dxgiDevice->lpVtbl->Release(subsystem->dxgiDevice);
686 subsystem->dxgiDevice = nullptr;
687 }
688
689 return 1;
690}
691
692/* The D3D11 device itself is gone (removed, reset, hung): a new output
693 * duplication on it can only fail, and unlike a refused desktop the device
694 * does not become usable again. Drop every object held on it and the device
695 * itself; the capture loop creates a fresh device once dxgiRetryTime has
696 * passed. */
697static void win_shadow_dxgi_reset_device(winShadowSubsystem* subsystem)
698{
699 WINPR_ASSERT(subsystem);
700
701 win_shadow_dxgi_drop_duplication(subsystem);
702
703 if (subsystem->dxgiResource)
704 {
705 subsystem->dxgiResource->lpVtbl->Release(subsystem->dxgiResource);
706 subsystem->dxgiResource = nullptr;
707 }
708
709 (void)win_shadow_dxgi_uninit(subsystem);
710
711 subsystem->dxgiRetryTime = GetTickCount64() + WIN_SHADOW_DXGI_RESET_RETRY_MS;
712}
713
714int win_shadow_dxgi_fetch_frame_data(winShadowSubsystem* subsystem, BYTE** ppDstData,
715 int* pnDstStep, int x, int y, int width, int height)
716{
717 int status;
718 HRESULT hr;
719 D3D11_BOX Box;
720 D3D11_TEXTURE2D_DESC srcDesc = WINPR_C_ARRAY_INIT;
721 DXGI_MAPPED_RECT mappedRect;
722
723 if ((width * height) < 1)
724 return 0;
725
726 {
727 /* The shadow surface may be a pixel larger than the desktop image
728 * (exclusive vs inclusive rect edges); clamp the requested region to
729 * the source texture, otherwise the out-of-bounds copy trips
730 * DXGI_ERROR_DEVICE_REMOVED and capture silently dies. */
731 subsystem->dxgiDesktopImage->lpVtbl->GetDesc(subsystem->dxgiDesktopImage, &srcDesc);
732 if (x + width > (int)srcDesc.Width)
733 width = (int)srcDesc.Width - x;
734 if (y + height > (int)srcDesc.Height)
735 height = (int)srcDesc.Height - y;
736
737 /* a desktop that shrank below the region leaves nothing to copy, and
738 * an empty box is not a valid copy source */
739 if ((width <= 0) || (height <= 0))
740 return 0;
741
742 /* The staging texture is the destination of the copy below and was
743 * created for the desktop size the duplication was set up with. A
744 * desktop resize since then (display mode change, a session connected
745 * to the console) leaves it smaller than the acquired frame while the
746 * duplication keeps delivering frames of the new size.
747 * CopySubresourceRegion clips the copy to the smaller destination
748 * without failing, so the mapped pitch stays that of the old width:
749 * shorter than one row of the requested region, which trips the
750 * stride assertion of freerdp_image_copy_no_overlap() in the caller
751 * and kills the capture thread. Follow the frame instead of dropping
752 * it: the texture holds the whole desktop image, so one recreation
753 * covers every region of this geometry. */
754 {
755 D3D11_TEXTURE2D_DESC dstDesc = WINPR_C_ARRAY_INIT;
756 BOOL fits = FALSE;
757
758 if (subsystem->dxgiStage)
759 {
760 subsystem->dxgiStage->lpVtbl->GetDesc(subsystem->dxgiStage, &dstDesc);
761 fits = ((UINT32)width <= dstDesc.Width) && ((UINT32)height <= dstDesc.Height);
762 }
763
764 if (!fits)
765 {
766 WLog_INFO(TAG,
767 "desktop is %" PRIu32 "x%" PRIu32 ", the staging texture was %" PRIu32
768 "x%" PRIu32 ": recreating it",
769 srcDesc.Width, srcDesc.Height, dstDesc.Width, dstDesc.Height);
770
771 if (win_shadow_dxgi_create_stage(subsystem, srcDesc.Width, srcDesc.Height) < 0)
772 return 0;
773 }
774 }
775 }
776
777 Box.top = y;
778 Box.left = x;
779 Box.right = x + width;
780 Box.bottom = y + height;
781 Box.front = 0;
782 Box.back = 1;
783
784 /* the destination now holds the whole frame, so this copy cannot clip it
785 * (CopySubresourceRegion reports no failure in the C vtable) */
786 subsystem->dxgiDeviceContext->lpVtbl->CopySubresourceRegion(
787 subsystem->dxgiDeviceContext, (ID3D11Resource*)subsystem->dxgiStage, 0, 0, 0, 0,
788 (ID3D11Resource*)subsystem->dxgiDesktopImage, 0, &Box);
789
790 if (subsystem->dxgiSurface)
791 {
792 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
793 subsystem->dxgiSurface = nullptr;
794 }
795
796 hr = subsystem->dxgiStage->lpVtbl->QueryInterface(subsystem->dxgiStage, &IID_IDXGISurface,
797 (void**)&(subsystem->dxgiSurface));
798
799 if (FAILED(hr))
800 {
801 WLog_ERR(TAG, "ID3D11Texture2D::QueryInterface(IDXGISurface) failure: %s 0x%08lX",
802 GetDxgiErrorString(hr), hr);
803 return -1;
804 }
805
806 hr = subsystem->dxgiSurface->lpVtbl->Map(subsystem->dxgiSurface, &mappedRect, DXGI_MAP_READ);
807
808 if (FAILED(hr))
809 {
810 if (win_shadow_dxgi_is_denied(hr))
811 {
812 win_shadow_dxgi_pause(subsystem, "IDXGISurface::Map", hr);
813 win_shadow_dxgi_drop_duplication(subsystem);
814 return 0;
815 }
816
817 WLog_ERR(TAG, "IDXGISurface::Map failure: %s 0x%08lX", GetDxgiErrorString(hr), hr);
818
819 if ((hr == DXGI_ERROR_DEVICE_REMOVED) || (hr == DXGI_ERROR_DEVICE_RESET))
820 {
821 /* the device itself is gone: a new duplication on it can only
822 * fail; drop it, the capture loop creates a fresh device once
823 * dxgiRetryTime has passed */
824 win_shadow_dxgi_reset_device(subsystem);
825 return 0;
826 }
827 else if (hr == DXGI_ERROR_ACCESS_DENIED)
828 {
829 /* The console desktop switched to the secure desktop (lock
830 * screen, UAC prompt, ...). Re-create the duplication with a
831 * bounded retry loop instead of killing the capture thread. */
832 if (subsystem->dxgiSurface)
833 {
834 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
835 subsystem->dxgiSurface = nullptr;
836 subsystem->dxgiSurfaceMapped = FALSE;
837 }
838
839 if (subsystem->dxgiStage)
840 {
841 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
842 subsystem->dxgiStage = nullptr;
843 }
844
845 if (subsystem->dxgiOutputDuplication)
846 {
847 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
848 subsystem->dxgiOutputDuplication = nullptr;
849 }
850
851 for (int attempt = 0; attempt < 240; ++attempt)
852 {
853 if (WaitForSingleObject(subsystem->base.server->StopEvent, 0) == WAIT_OBJECT_0)
854 return -1;
855
856 Sleep(500);
857 status = win_shadow_dxgi_init_duplication(subsystem);
858 if (status >= 0)
859 return 0;
860 }
861
862 return -1;
863 }
864
865 return -1;
866 }
867
868 subsystem->dxgiSurfaceMapped = TRUE;
869
870 *ppDstData = mappedRect.pBits;
871 *pnDstStep = mappedRect.Pitch;
872
873 return 1;
874}
875
876int win_shadow_dxgi_release_frame_data(winShadowSubsystem* subsystem)
877{
878 if (subsystem->dxgiSurface)
879 {
880 if (subsystem->dxgiSurfaceMapped)
881 subsystem->dxgiSurface->lpVtbl->Unmap(subsystem->dxgiSurface);
882
883 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
884 subsystem->dxgiSurface = nullptr;
885 }
886 subsystem->dxgiSurfaceMapped = FALSE;
887
888 if (subsystem->dxgiOutputDuplication)
889 {
890 if (subsystem->dxgiFrameAcquired)
891 subsystem->dxgiOutputDuplication->lpVtbl->ReleaseFrame(
892 subsystem->dxgiOutputDuplication);
893 }
894 subsystem->dxgiFrameAcquired = FALSE;
895
896 subsystem->pendingFrames = 0;
897
898 return 1;
899}
900
901/* A new duplication of the output, on the calling thread's desktop: after a
902 * switch between the user's desktop and the secure one (logon, lock screen,
903 * UAC), or when an earlier attempt failed. A device that was removed or reset
904 * in between (driver crash, GPU change, TDR) is unusable for a new duplication
905 * and is replaced by a fresh one; a healthy device is kept. */
906int win_shadow_dxgi_restart_duplication(winShadowSubsystem* subsystem)
907{
908 WINPR_ASSERT(subsystem);
909
910 if (!subsystem->dxgiDevice)
911 {
912 const int status = win_shadow_dxgi_init(subsystem);
913
914 if (status < 0)
915 {
916 /* the initialization may have created the device and failed on
917 * the frame objects afterwards */
918 if (subsystem->dxgiDevice)
919 {
920 if (FAILED(subsystem->dxgiDevice->lpVtbl->GetDeviceRemovedReason(
921 subsystem->dxgiDevice)))
922 {
923 win_shadow_dxgi_reset_device(subsystem);
924 }
925 else
926 {
927 /* a live device stays for the classified retry, the
928 * partial objects must not */
929 win_shadow_dxgi_drop_duplication(subsystem);
930 win_shadow_dxgi_schedule_retry(subsystem);
931 }
932 }
933 else
934 {
935 /* without a device a stray context may still be around */
936 (void)win_shadow_dxgi_uninit(subsystem);
937 win_shadow_dxgi_schedule_retry(subsystem);
938 }
939 }
940
941 return status;
942 }
943
944 /* the device kept from the last desktop may have died in between: a new
945 * duplication on it can only fail with DXGI_ERROR_DEVICE_REMOVED while
946 * the capture loop keeps retrying the dead device forever */
947 const HRESULT reason =
948 subsystem->dxgiDevice->lpVtbl->GetDeviceRemovedReason(subsystem->dxgiDevice);
949
950 if (FAILED(reason))
951 {
952 WLog_WARN(TAG, "D3D11 device is no longer usable: %s (0x%08lX)", GetDxgiErrorString(reason),
953 reason);
954 win_shadow_dxgi_reset_device(subsystem);
955 return -1;
956 }
957
958 win_shadow_dxgi_drop_duplication(subsystem);
959 const int status = win_shadow_dxgi_init_duplication(subsystem);
960
961 if (status < 0)
962 {
963 /* the device may have died while the outputs refused the
964 * duplication: check once more before keeping it for the retry,
965 * the device reset overrides any failure classification */
966 if (subsystem->dxgiDevice &&
967 FAILED(subsystem->dxgiDevice->lpVtbl->GetDeviceRemovedReason(subsystem->dxgiDevice)))
968 {
969 win_shadow_dxgi_reset_device(subsystem);
970 return -1;
971 }
972
973 /* a partial initialization may leave frame objects behind */
974 win_shadow_dxgi_drop_duplication(subsystem);
975 win_shadow_dxgi_schedule_retry(subsystem);
976 }
977
978 return status;
979}
980
981int win_shadow_dxgi_get_next_frame(winShadowSubsystem* subsystem)
982{
983 UINT i = 0;
984 int status;
985 HRESULT hr = 0;
986 UINT timeout = 1;
987 UINT DataBufferSize = 0;
988 BYTE* DataBuffer = nullptr;
989
990 if (subsystem->dxgiFrameAcquired)
991 {
992 win_shadow_dxgi_release_frame_data(subsystem);
993 }
994
995 if (subsystem->dxgiDesktopImage)
996 {
997 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
998 subsystem->dxgiDesktopImage = nullptr;
999 }
1000
1001 hr = subsystem->dxgiOutputDuplication->lpVtbl->AcquireNextFrame(
1002 subsystem->dxgiOutputDuplication, timeout, &(subsystem->dxgiFrameInfo),
1003 &(subsystem->dxgiResource));
1004
1005 if (SUCCEEDED(hr))
1006 {
1007 subsystem->dxgiFrameAcquired = TRUE;
1008 subsystem->pendingFrames = subsystem->dxgiFrameInfo.AccumulatedFrames;
1009 }
1010
1011 if (hr == DXGI_ERROR_WAIT_TIMEOUT)
1012 return 0;
1013
1014 if (FAILED(hr))
1015 {
1016 if (win_shadow_dxgi_is_denied(hr))
1017 {
1018 /* the duplication is no good any more: drop it, the capture loop makes a new one
1019 * (on the new input desktop when this process may open it) */
1020 win_shadow_dxgi_pause(subsystem, "IDXGIOutputDuplication::AcquireNextFrame", hr);
1021 win_shadow_dxgi_drop_duplication(subsystem);
1022 return 0;
1023 }
1024
1025 WLog_ERR(TAG, "IDXGIOutputDuplication::AcquireNextFrame failure: %s (0x%08lX)",
1026 GetDxgiErrorString(hr), hr);
1027
1028 if ((hr == DXGI_ERROR_DEVICE_REMOVED) || (hr == DXGI_ERROR_DEVICE_RESET))
1029 {
1030 /* the device is gone for good: do not ask it again (the removal
1031 * may race ahead and still report S_OK), drop it; the capture
1032 * loop creates a fresh device once dxgiRetryTime has passed */
1033 win_shadow_dxgi_reset_device(subsystem);
1034 return 0;
1035 }
1036 else if (hr == DXGI_ERROR_ACCESS_LOST)
1037 {
1038 win_shadow_dxgi_release_frame_data(subsystem);
1039
1040 if (subsystem->dxgiDesktopImage)
1041 {
1042 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
1043 subsystem->dxgiDesktopImage = nullptr;
1044 }
1045
1046 if (subsystem->dxgiStage)
1047 {
1048 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
1049 subsystem->dxgiStage = nullptr;
1050 }
1051
1052 if (subsystem->dxgiOutputDuplication)
1053 {
1054 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
1055 subsystem->dxgiOutputDuplication = nullptr;
1056 }
1057
1058 status = win_shadow_dxgi_restart_duplication(subsystem);
1059
1060 if (status < 0)
1061 {
1062 /* a device reset already scheduled the retry: defer this
1063 * frame instead of failing the capture pass */
1064 return subsystem->dxgiDevice ? -1 : 0;
1065 }
1066
1067 return 0;
1068 }
1069 else if (hr == DXGI_ERROR_ACCESS_DENIED)
1070 {
1071 /* The console desktop switched to the secure desktop (lock
1072 * screen, UAC prompt, ...); the duplication stays denied while
1073 * that desktop is active. Release it and re-create with a
1074 * bounded retry loop instead of killing the capture thread. */
1075 win_shadow_dxgi_release_frame_data(subsystem);
1076
1077 if (subsystem->dxgiDesktopImage)
1078 {
1079 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
1080 subsystem->dxgiDesktopImage = nullptr;
1081 }
1082
1083 if (subsystem->dxgiStage)
1084 {
1085 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
1086 subsystem->dxgiStage = nullptr;
1087 }
1088
1089 if (subsystem->dxgiOutputDuplication)
1090 {
1091 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
1092 subsystem->dxgiOutputDuplication = nullptr;
1093 }
1094
1095 for (int attempt = 0; attempt < 240; ++attempt)
1096 {
1097 if (WaitForSingleObject(subsystem->base.server->StopEvent, 0) == WAIT_OBJECT_0)
1098 return -1;
1099
1100 Sleep(500);
1101 status = win_shadow_dxgi_init_duplication(subsystem);
1102 if (status >= 0)
1103 return 0;
1104 }
1105
1106 return -1;
1107 }
1108 else if (hr == DXGI_ERROR_INVALID_CALL)
1109 {
1110 win_shadow_dxgi_uninit(subsystem);
1111
1112 status = win_shadow_dxgi_init(subsystem);
1113
1114 if (status < 0)
1115 return -1;
1116
1117 return 0;
1118 }
1119
1120 return -1;
1121 }
1122
1123 hr = subsystem->dxgiResource->lpVtbl->QueryInterface(
1124 subsystem->dxgiResource, &IID_ID3D11Texture2D, (void**)&(subsystem->dxgiDesktopImage));
1125
1126 if (subsystem->dxgiResource)
1127 {
1128 subsystem->dxgiResource->lpVtbl->Release(subsystem->dxgiResource);
1129 subsystem->dxgiResource = nullptr;
1130 }
1131
1132 if (FAILED(hr))
1133 {
1134 WLog_ERR(TAG, "IDXGIResource::QueryInterface(ID3D11Texture2D) failure: %s (0x%08lX)",
1135 GetDxgiErrorString(hr), hr);
1136 return -1;
1137 }
1138
1139 return 1;
1140}
1141
1142int win_shadow_dxgi_get_invalid_region(winShadowSubsystem* subsystem)
1143{
1144 HRESULT hr;
1145 POINT* pSrcPt;
1146 RECT* pDstRect;
1147 RECT* pDirtyRect;
1148 UINT numMoveRects;
1149 UINT numDirtyRects;
1150 UINT UsedBufferSize;
1151 RECTANGLE_16 invalidRect;
1152 UINT MetadataBufferSize;
1153 UINT MoveRectsBufferSize;
1154 UINT DirtyRectsBufferSize;
1155 RECT* pDirtyRectsBuffer;
1156 DXGI_OUTDUPL_MOVE_RECT* pMoveRect;
1157 DXGI_OUTDUPL_MOVE_RECT* pMoveRectBuffer;
1158 rdpShadowSurface* surface = nullptr;
1159
1160 WINPR_ASSERT(subsystem);
1161 WINPR_ASSERT(subsystem->base.server);
1162 surface = subsystem->base.server->surface;
1163 WINPR_ASSERT(surface);
1164
1165 if (subsystem->dxgiFrameInfo.AccumulatedFrames == 0)
1166 return 0;
1167
1168 if (subsystem->dxgiFrameInfo.TotalMetadataBufferSize == 0)
1169 return 0;
1170
1171 MetadataBufferSize = subsystem->dxgiFrameInfo.TotalMetadataBufferSize;
1172
1173 if (MetadataBufferSize > subsystem->MetadataBufferSize)
1174 {
1175 subsystem->MetadataBuffer = (BYTE*)realloc(subsystem->MetadataBuffer, MetadataBufferSize);
1176
1177 if (!subsystem->MetadataBuffer)
1178 return -1;
1179
1180 subsystem->MetadataBufferSize = MetadataBufferSize;
1181 }
1182
1183 /* GetFrameMoveRects */
1184
1185 UsedBufferSize = 0;
1186
1187 MoveRectsBufferSize = MetadataBufferSize - UsedBufferSize;
1188 pMoveRectBuffer = (DXGI_OUTDUPL_MOVE_RECT*)&(subsystem->MetadataBuffer[UsedBufferSize]);
1189
1190 hr = subsystem->dxgiOutputDuplication->lpVtbl->GetFrameMoveRects(
1191 subsystem->dxgiOutputDuplication, MoveRectsBufferSize, pMoveRectBuffer,
1192 &MoveRectsBufferSize);
1193
1194 if (FAILED(hr))
1195 {
1196 WLog_ERR(TAG,
1197 "IDXGIOutputDuplication::GetFrameMoveRects failure: %s (0x%08lX) Size: %u Total "
1198 "%u Used: %u",
1199 GetDxgiErrorString(hr), hr, MoveRectsBufferSize, MetadataBufferSize,
1200 UsedBufferSize);
1201 return -1;
1202 }
1203
1204 /* GetFrameDirtyRects */
1205
1206 UsedBufferSize += MoveRectsBufferSize;
1207
1208 DirtyRectsBufferSize = MetadataBufferSize - UsedBufferSize;
1209 pDirtyRectsBuffer = (RECT*)&(subsystem->MetadataBuffer[UsedBufferSize]);
1210
1211 hr = subsystem->dxgiOutputDuplication->lpVtbl->GetFrameDirtyRects(
1212 subsystem->dxgiOutputDuplication, DirtyRectsBufferSize, pDirtyRectsBuffer,
1213 &DirtyRectsBufferSize);
1214
1215 if (FAILED(hr))
1216 {
1217 WLog_ERR(TAG,
1218 "IDXGIOutputDuplication::GetFrameDirtyRects failure: %s (0x%08lX) Size: %u Total "
1219 "%u Used: %u",
1220 GetDxgiErrorString(hr), hr, DirtyRectsBufferSize, MetadataBufferSize,
1221 UsedBufferSize);
1222 return -1;
1223 }
1224
1225 numMoveRects = MoveRectsBufferSize / sizeof(DXGI_OUTDUPL_MOVE_RECT);
1226
1227 for (UINT i = 0; i < numMoveRects; i++)
1228 {
1229 pMoveRect = &pMoveRectBuffer[i];
1230 pSrcPt = &(pMoveRect->SourcePoint);
1231 pDstRect = &(pMoveRect->DestinationRect);
1232
1233 invalidRect.left = (UINT16)pDstRect->left;
1234 invalidRect.top = (UINT16)pDstRect->top;
1235 invalidRect.right = (UINT16)pDstRect->right;
1236 invalidRect.bottom = (UINT16)pDstRect->bottom;
1237
1238 region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
1239 }
1240
1241 numDirtyRects = DirtyRectsBufferSize / sizeof(RECT);
1242
1243 for (UINT i = 0; i < numDirtyRects; i++)
1244 {
1245 pDirtyRect = &pDirtyRectsBuffer[i];
1246
1247 invalidRect.left = (UINT16)pDirtyRect->left;
1248 invalidRect.top = (UINT16)pDirtyRect->top;
1249 invalidRect.right = (UINT16)pDirtyRect->right;
1250 invalidRect.bottom = (UINT16)pDirtyRect->bottom;
1251
1252 region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
1253 }
1254
1255 return 1;
1256}
1257
1258#endif