FreeRDP
Loading...
Searching...
No Matches
win_dxgi.c
1
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
200const char* GetDxgiErrorString(HRESULT hr)
201{
202 switch (hr)
203 {
204 case DXGI_STATUS_OCCLUDED:
205 return "DXGI_STATUS_OCCLUDED";
206 case DXGI_STATUS_CLIPPED:
207 return "DXGI_STATUS_CLIPPED";
208 case DXGI_STATUS_NO_REDIRECTION:
209 return "DXGI_STATUS_NO_REDIRECTION";
210 case DXGI_STATUS_NO_DESKTOP_ACCESS:
211 return "DXGI_STATUS_NO_DESKTOP_ACCESS";
212 case DXGI_STATUS_GRAPHICS_VIDPN_SOURCE_IN_USE:
213 return "DXGI_STATUS_GRAPHICS_VIDPN_SOURCE_IN_USE";
214 case DXGI_STATUS_MODE_CHANGED:
215 return "DXGI_STATUS_MODE_CHANGED";
216 case DXGI_STATUS_MODE_CHANGE_IN_PROGRESS:
217 return "DXGI_STATUS_MODE_CHANGE_IN_PROGRESS";
218 case DXGI_ERROR_INVALID_CALL:
219 return "DXGI_ERROR_INVALID_CALL";
220 case DXGI_ERROR_NOT_FOUND:
221 return "DXGI_ERROR_NOT_FOUND";
222 case DXGI_ERROR_MORE_DATA:
223 return "DXGI_ERROR_MORE_DATA";
224 case DXGI_ERROR_UNSUPPORTED:
225 return "DXGI_ERROR_UNSUPPORTED";
226 case DXGI_ERROR_DEVICE_REMOVED:
227 return "DXGI_ERROR_DEVICE_REMOVED";
228 case DXGI_ERROR_DEVICE_HUNG:
229 return "DXGI_ERROR_DEVICE_HUNG";
230 case DXGI_ERROR_DEVICE_RESET:
231 return "DXGI_ERROR_DEVICE_RESET";
232 case DXGI_ERROR_WAS_STILL_DRAWING:
233 return "DXGI_ERROR_WAS_STILL_DRAWING";
234 case DXGI_ERROR_FRAME_STATISTICS_DISJOINT:
235 return "DXGI_ERROR_FRAME_STATISTICS_DISJOINT";
236 case DXGI_ERROR_GRAPHICS_VIDPN_SOURCE_IN_USE:
237 return "DXGI_ERROR_GRAPHICS_VIDPN_SOURCE_IN_USE";
238 case DXGI_ERROR_DRIVER_INTERNAL_ERROR:
239 return "DXGI_ERROR_DRIVER_INTERNAL_ERROR";
240 case DXGI_ERROR_NONEXCLUSIVE:
241 return "DXGI_ERROR_NONEXCLUSIVE";
242 case DXGI_ERROR_NOT_CURRENTLY_AVAILABLE:
243 return "DXGI_ERROR_NOT_CURRENTLY_AVAILABLE";
244 case DXGI_ERROR_REMOTE_CLIENT_DISCONNECTED:
245 return "DXGI_ERROR_REMOTE_CLIENT_DISCONNECTED";
246 case DXGI_ERROR_REMOTE_OUTOFMEMORY:
247 return "DXGI_ERROR_REMOTE_OUTOFMEMORY";
248 case DXGI_ERROR_ACCESS_LOST:
249 return "DXGI_ERROR_ACCESS_LOST";
250 case DXGI_ERROR_WAIT_TIMEOUT:
251 return "DXGI_ERROR_WAIT_TIMEOUT";
252 case DXGI_ERROR_SESSION_DISCONNECTED:
253 return "DXGI_ERROR_SESSION_DISCONNECTED";
254 case DXGI_ERROR_RESTRICT_TO_OUTPUT_STALE:
255 return "DXGI_ERROR_RESTRICT_TO_OUTPUT_STALE";
256 case DXGI_ERROR_CANNOT_PROTECT_CONTENT:
257 return "DXGI_ERROR_CANNOT_PROTECT_CONTENT";
258 case DXGI_ERROR_ACCESS_DENIED:
259 return "DXGI_ERROR_ACCESS_DENIED";
260 case DXGI_ERROR_NAME_ALREADY_EXISTS:
261 return "DXGI_ERROR_NAME_ALREADY_EXISTS";
262 case DXGI_ERROR_SDK_COMPONENT_MISSING:
263 return "DXGI_ERROR_SDK_COMPONENT_MISSING";
264 case DXGI_STATUS_UNOCCLUDED:
265 return "DXGI_STATUS_UNOCCLUDED";
266 case DXGI_STATUS_DDA_WAS_STILL_DRAWING:
267 return "DXGI_STATUS_DDA_WAS_STILL_DRAWING";
268 case DXGI_ERROR_MODE_CHANGE_IN_PROGRESS:
269 return "DXGI_ERROR_MODE_CHANGE_IN_PROGRESS";
270 case DXGI_DDI_ERR_WASSTILLDRAWING:
271 return "DXGI_DDI_ERR_WASSTILLDRAWING";
272 case DXGI_DDI_ERR_UNSUPPORTED:
273 return "DXGI_DDI_ERR_UNSUPPORTED";
274 case DXGI_DDI_ERR_NONEXCLUSIVE:
275 return "DXGI_DDI_ERR_NONEXCLUSIVE";
276 case 0x80070005:
277 return "DXGI_ERROR_ACCESS_DENIED";
278 }
279
280 return "DXGI_ERROR_UNKNOWN";
281}
282
283static void win_shadow_d3d11_module_init()
284{
285 if (d3d11_module)
286 return;
287
288 d3d11_module = LoadLibraryA("d3d11.dll");
289
290 if (!d3d11_module)
291 return;
292
293 pfnD3D11CreateDevice = GetProcAddressAs(d3d11_module, "D3D11CreateDevice", fnD3D11CreateDevice);
294}
295
296int win_shadow_dxgi_init_duplication(winShadowSubsystem* subsystem)
297{
298 HRESULT hr;
299 UINT dTop, i = 0;
300 IDXGIOutput* pOutput;
301 DXGI_OUTPUT_DESC outputDesc = WINPR_C_ARRAY_INIT;
302 DXGI_OUTPUT_DESC* pOutputDesc;
303 D3D11_TEXTURE2D_DESC textureDesc;
304 IDXGIDevice* dxgiDevice = nullptr;
305 IDXGIAdapter* dxgiAdapter = nullptr;
306 IDXGIOutput* dxgiOutput = nullptr;
307 IDXGIOutput1* dxgiOutput1 = nullptr;
308
309 hr = subsystem->dxgiDevice->lpVtbl->QueryInterface(subsystem->dxgiDevice, &IID_IDXGIDevice,
310 (void**)&dxgiDevice);
311
312 if (FAILED(hr))
313 {
314 WLog_ERR(TAG, "ID3D11Device::QueryInterface(IDXGIDevice) failure: %s (0x%08lX)",
315 GetDxgiErrorString(hr), hr);
316 return -1;
317 }
318
319 hr = dxgiDevice->lpVtbl->GetParent(dxgiDevice, &IID_IDXGIAdapter, (void**)&dxgiAdapter);
320
321 if (dxgiDevice)
322 {
323 dxgiDevice->lpVtbl->Release(dxgiDevice);
324 dxgiDevice = nullptr;
325 }
326
327 if (FAILED(hr))
328 {
329 WLog_ERR(TAG, "IDXGIDevice::GetParent(IDXGIAdapter) failure: %s (0x%08lX)",
330 GetDxgiErrorString(hr), hr);
331 return -1;
332 }
333
334 pOutput = nullptr;
335
336 while (dxgiAdapter->lpVtbl->EnumOutputs(dxgiAdapter, i, &pOutput) != DXGI_ERROR_NOT_FOUND)
337 {
338 if (!pOutput)
339 break;
340
341 pOutputDesc = &outputDesc;
342
343 hr = pOutput->lpVtbl->GetDesc(pOutput, pOutputDesc);
344
345 if (FAILED(hr))
346 {
347 WLog_ERR(TAG, "IDXGIOutput::GetDesc failure: %s (0x%08lX)", GetDxgiErrorString(hr), hr);
348 return -1;
349 }
350
351 if (pOutputDesc->AttachedToDesktop)
352 dTop = i;
353
354 pOutput->lpVtbl->Release(pOutput);
355 pOutput = nullptr;
356 i++;
357 }
358
359 dTop = 0; /* screen id */
360
361 hr = dxgiAdapter->lpVtbl->EnumOutputs(dxgiAdapter, dTop, &dxgiOutput);
362
363 if (dxgiAdapter)
364 {
365 dxgiAdapter->lpVtbl->Release(dxgiAdapter);
366 dxgiAdapter = nullptr;
367 }
368
369 if (FAILED(hr))
370 {
371 WLog_ERR(TAG, "IDXGIAdapter::EnumOutputs failure: %s (0x%08lX)", GetDxgiErrorString(hr),
372 hr);
373 return -1;
374 }
375
376 hr = dxgiOutput->lpVtbl->QueryInterface(dxgiOutput, &IID_IDXGIOutput1, (void**)&dxgiOutput1);
377
378 if (dxgiOutput)
379 {
380 dxgiOutput->lpVtbl->Release(dxgiOutput);
381 dxgiOutput = nullptr;
382 }
383
384 if (FAILED(hr))
385 {
386 WLog_ERR(TAG, "IDXGIOutput::QueryInterface(IDXGIOutput1) failure: %s (0x%08lX)",
387 GetDxgiErrorString(hr), hr);
388 return -1;
389 }
390
391 for (int attempt = 0;; ++attempt)
392 {
393 hr = dxgiOutput1->lpVtbl->DuplicateOutput(dxgiOutput1, (IUnknown*)subsystem->dxgiDevice,
394 &(subsystem->dxgiOutputDuplication));
395
396 /* DXGI_ERROR_ACCESS_DENIED is transient while the display mode
397 * changes (e.g. a display settings switch driven by a remote
398 * display control channel): retry on the same output for a
399 * bounded window instead of failing the capture outright. */
400 if (hr != DXGI_ERROR_ACCESS_DENIED || attempt >= 60)
401 break;
402
403 if (WaitForSingleObject(subsystem->base.server->StopEvent, 0) == WAIT_OBJECT_0)
404 break;
405
406 Sleep(250);
407 }
408
409 if (dxgiOutput1)
410 {
411 dxgiOutput1->lpVtbl->Release(dxgiOutput1);
412 dxgiOutput1 = nullptr;
413 }
414
415 if (FAILED(hr))
416 {
417 WLog_ERR(TAG, "IDXGIOutput1::DuplicateOutput failure: %s (0x%08lX)", GetDxgiErrorString(hr),
418 hr);
419 return -1;
420 }
421
422 /* the staging texture must match the current desktop size; a fatal
423 * resize error fails the duplication, the caller retries next tick */
424 if (win_shadow_check_resize(subsystem) < 0)
425 return -1;
426
427 textureDesc.Width = subsystem->width;
428 textureDesc.Height = subsystem->height;
429 textureDesc.MipLevels = 1;
430 textureDesc.ArraySize = 1;
431 textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
432 textureDesc.SampleDesc.Count = 1;
433 textureDesc.SampleDesc.Quality = 0;
434 textureDesc.Usage = D3D11_USAGE_STAGING;
435 textureDesc.BindFlags = 0;
436 textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
437 textureDesc.MiscFlags = 0;
438
439 hr = subsystem->dxgiDevice->lpVtbl->CreateTexture2D(subsystem->dxgiDevice, &textureDesc,
440 nullptr, &(subsystem->dxgiStage));
441
442 if (FAILED(hr))
443 {
444 WLog_ERR(TAG, "ID3D11Device::CreateTexture2D failure: %s (0x%08lX)", GetDxgiErrorString(hr),
445 hr);
446 return -1;
447 }
448
449 return 1;
450}
451
452int win_shadow_dxgi_init(winShadowSubsystem* subsystem)
453{
454 UINT i = 0;
455 HRESULT hr;
456 int status;
457 UINT DriverTypeIndex;
458 IDXGIDevice* DxgiDevice = nullptr;
459 IDXGIAdapter* DxgiAdapter = nullptr;
460 IDXGIOutput* DxgiOutput = nullptr;
461 IDXGIOutput1* DxgiOutput1 = nullptr;
462
463 win_shadow_d3d11_module_init();
464
465 if (!pfnD3D11CreateDevice)
466 return -1;
467
468 for (DriverTypeIndex = 0; DriverTypeIndex < NumDriverTypes; ++DriverTypeIndex)
469 {
470 hr = pfnD3D11CreateDevice(nullptr, DriverTypes[DriverTypeIndex], nullptr, 0, FeatureLevels,
471 NumFeatureLevels, D3D11_SDK_VERSION, &(subsystem->dxgiDevice),
472 &(subsystem->featureLevel), &(subsystem->dxgiDeviceContext));
473
474 if (SUCCEEDED(hr))
475 break;
476 }
477
478 if (FAILED(hr))
479 {
480 WLog_ERR(TAG, "D3D11CreateDevice failure: 0x%08lX", hr);
481 return -1;
482 }
483
484 status = win_shadow_dxgi_init_duplication(subsystem);
485
486 return status;
487}
488
489int win_shadow_dxgi_uninit(winShadowSubsystem* subsystem)
490{
491 if (subsystem->dxgiStage)
492 {
493 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
494 subsystem->dxgiStage = nullptr;
495 }
496
497 if (subsystem->dxgiDesktopImage)
498 {
499 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
500 subsystem->dxgiDesktopImage = nullptr;
501 }
502
503 if (subsystem->dxgiOutputDuplication)
504 {
505 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
506 subsystem->dxgiOutputDuplication = nullptr;
507 }
508
509 if (subsystem->dxgiDeviceContext)
510 {
511 subsystem->dxgiDeviceContext->lpVtbl->Release(subsystem->dxgiDeviceContext);
512 subsystem->dxgiDeviceContext = nullptr;
513 }
514
515 if (subsystem->dxgiDevice)
516 {
517 subsystem->dxgiDevice->lpVtbl->Release(subsystem->dxgiDevice);
518 subsystem->dxgiDevice = nullptr;
519 }
520
521 return 1;
522}
523
524int win_shadow_dxgi_fetch_frame_data(winShadowSubsystem* subsystem, BYTE** ppDstData,
525 int* pnDstStep, int x, int y, int width, int height)
526{
527 int status;
528 HRESULT hr;
529 D3D11_BOX Box;
530 DXGI_MAPPED_RECT mappedRect;
531
532 if ((width * height) < 1)
533 return 0;
534
535 {
536 /* The shadow surface may be a pixel larger than the desktop image
537 * (exclusive vs inclusive rect edges); clamp the requested region to
538 * the source texture, otherwise the out-of-bounds copy trips
539 * DXGI_ERROR_DEVICE_REMOVED and capture silently dies. */
540 D3D11_TEXTURE2D_DESC srcDesc;
541 subsystem->dxgiDesktopImage->lpVtbl->GetDesc(subsystem->dxgiDesktopImage, &srcDesc);
542 if (x + width > (int)srcDesc.Width)
543 width = (int)srcDesc.Width - x;
544 if (y + height > (int)srcDesc.Height)
545 height = (int)srcDesc.Height - y;
546 }
547
548 Box.top = y;
549 Box.left = x;
550 Box.right = x + width;
551 Box.bottom = y + height;
552 Box.front = 0;
553 Box.back = 1;
554
555 subsystem->dxgiDeviceContext->lpVtbl->CopySubresourceRegion(
556 subsystem->dxgiDeviceContext, (ID3D11Resource*)subsystem->dxgiStage, 0, 0, 0, 0,
557 (ID3D11Resource*)subsystem->dxgiDesktopImage, 0, &Box);
558
559 if (subsystem->dxgiSurface)
560 {
561 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
562 subsystem->dxgiSurface = nullptr;
563 }
564
565 hr = subsystem->dxgiStage->lpVtbl->QueryInterface(subsystem->dxgiStage, &IID_IDXGISurface,
566 (void**)&(subsystem->dxgiSurface));
567
568 if (FAILED(hr))
569 {
570 WLog_ERR(TAG, "ID3D11Texture2D::QueryInterface(IDXGISurface) failure: %s 0x%08lX",
571 GetDxgiErrorString(hr), hr);
572 return -1;
573 }
574
575 hr = subsystem->dxgiSurface->lpVtbl->Map(subsystem->dxgiSurface, &mappedRect, DXGI_MAP_READ);
576
577 if (FAILED(hr))
578 {
579 WLog_ERR(TAG, "IDXGISurface::Map failure: %s 0x%08lX", GetDxgiErrorString(hr), hr);
580
581 if (hr == DXGI_ERROR_DEVICE_REMOVED)
582 {
583 win_shadow_dxgi_uninit(subsystem);
584
585 status = win_shadow_dxgi_init(subsystem);
586
587 if (status < 0)
588 return -1;
589
590 return 0;
591 }
592 else if (hr == DXGI_ERROR_ACCESS_DENIED)
593 {
594 /* The console desktop switched to the secure desktop (lock
595 * screen, UAC prompt, ...). Re-create the duplication with a
596 * bounded retry loop instead of killing the capture thread. */
597 if (subsystem->dxgiSurface)
598 {
599 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
600 subsystem->dxgiSurface = nullptr;
601 subsystem->dxgiSurfaceMapped = FALSE;
602 }
603
604 if (subsystem->dxgiStage)
605 {
606 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
607 subsystem->dxgiStage = nullptr;
608 }
609
610 if (subsystem->dxgiOutputDuplication)
611 {
612 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
613 subsystem->dxgiOutputDuplication = nullptr;
614 }
615
616 for (int attempt = 0; attempt < 240; ++attempt)
617 {
618 if (WaitForSingleObject(subsystem->base.server->StopEvent, 0) == WAIT_OBJECT_0)
619 return -1;
620
621 Sleep(500);
622 status = win_shadow_dxgi_init_duplication(subsystem);
623 if (status >= 0)
624 return 0;
625 }
626
627 return -1;
628 }
629
630 return -1;
631 }
632
633 subsystem->dxgiSurfaceMapped = TRUE;
634
635 *ppDstData = mappedRect.pBits;
636 *pnDstStep = mappedRect.Pitch;
637
638 return 1;
639}
640
641int win_shadow_dxgi_release_frame_data(winShadowSubsystem* subsystem)
642{
643 if (subsystem->dxgiSurface)
644 {
645 if (subsystem->dxgiSurfaceMapped)
646 {
647 subsystem->dxgiSurface->lpVtbl->Unmap(subsystem->dxgiSurface);
648 subsystem->dxgiSurfaceMapped = FALSE;
649 }
650
651 subsystem->dxgiSurface->lpVtbl->Release(subsystem->dxgiSurface);
652 subsystem->dxgiSurface = nullptr;
653 }
654
655 if (subsystem->dxgiOutputDuplication)
656 {
657 if (subsystem->dxgiFrameAcquired)
658 {
659 subsystem->dxgiOutputDuplication->lpVtbl->ReleaseFrame(
660 subsystem->dxgiOutputDuplication);
661 subsystem->dxgiFrameAcquired = FALSE;
662 }
663 }
664
665 subsystem->pendingFrames = 0;
666
667 return 1;
668}
669
670/* A new duplication of the output, on the calling thread's desktop: after a
671 * switch between the user's desktop and the secure one (logon, lock screen,
672 * UAC), or when an earlier attempt failed. The device is kept when there is
673 * one. */
674int win_shadow_dxgi_restart_duplication(winShadowSubsystem* subsystem)
675{
676 WINPR_ASSERT(subsystem);
677
678 if (!subsystem->dxgiDevice)
679 return win_shadow_dxgi_init(subsystem);
680
681 (void)win_shadow_dxgi_release_frame_data(subsystem);
682
683 if (subsystem->dxgiDesktopImage)
684 {
685 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
686 subsystem->dxgiDesktopImage = nullptr;
687 }
688
689 if (subsystem->dxgiOutputDuplication)
690 {
691 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
692 subsystem->dxgiOutputDuplication = nullptr;
693 }
694
695 if (subsystem->dxgiStage)
696 {
697 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
698 subsystem->dxgiStage = nullptr;
699 }
700
701 return win_shadow_dxgi_init_duplication(subsystem);
702}
703
704int win_shadow_dxgi_get_next_frame(winShadowSubsystem* subsystem)
705{
706 UINT i = 0;
707 int status;
708 HRESULT hr = 0;
709 UINT timeout = 1;
710 UINT DataBufferSize = 0;
711 BYTE* DataBuffer = nullptr;
712
713 if (subsystem->dxgiFrameAcquired)
714 {
715 win_shadow_dxgi_release_frame_data(subsystem);
716 }
717
718 if (subsystem->dxgiDesktopImage)
719 {
720 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
721 subsystem->dxgiDesktopImage = nullptr;
722 }
723
724 hr = subsystem->dxgiOutputDuplication->lpVtbl->AcquireNextFrame(
725 subsystem->dxgiOutputDuplication, timeout, &(subsystem->dxgiFrameInfo),
726 &(subsystem->dxgiResource));
727
728 if (SUCCEEDED(hr))
729 {
730 subsystem->dxgiFrameAcquired = TRUE;
731 subsystem->pendingFrames = subsystem->dxgiFrameInfo.AccumulatedFrames;
732 }
733
734 if (hr == DXGI_ERROR_WAIT_TIMEOUT)
735 return 0;
736
737 if (FAILED(hr))
738 {
739 WLog_ERR(TAG, "IDXGIOutputDuplication::AcquireNextFrame failure: %s (0x%08lX)",
740 GetDxgiErrorString(hr), hr);
741
742 if (hr == DXGI_ERROR_ACCESS_LOST)
743 {
744 win_shadow_dxgi_release_frame_data(subsystem);
745
746 if (subsystem->dxgiDesktopImage)
747 {
748 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
749 subsystem->dxgiDesktopImage = nullptr;
750 }
751
752 if (subsystem->dxgiStage)
753 {
754 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
755 subsystem->dxgiStage = nullptr;
756 }
757
758 if (subsystem->dxgiOutputDuplication)
759 {
760 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
761 subsystem->dxgiOutputDuplication = nullptr;
762 }
763
764 status = win_shadow_dxgi_init_duplication(subsystem);
765
766 if (status < 0)
767 return -1;
768
769 return 0;
770 }
771 else if (hr == DXGI_ERROR_ACCESS_DENIED)
772 {
773 /* The console desktop switched to the secure desktop (lock
774 * screen, UAC prompt, ...); the duplication stays denied while
775 * that desktop is active. Release it and re-create with a
776 * bounded retry loop instead of killing the capture thread. */
777 win_shadow_dxgi_release_frame_data(subsystem);
778
779 if (subsystem->dxgiDesktopImage)
780 {
781 subsystem->dxgiDesktopImage->lpVtbl->Release(subsystem->dxgiDesktopImage);
782 subsystem->dxgiDesktopImage = nullptr;
783 }
784
785 if (subsystem->dxgiStage)
786 {
787 subsystem->dxgiStage->lpVtbl->Release(subsystem->dxgiStage);
788 subsystem->dxgiStage = nullptr;
789 }
790
791 if (subsystem->dxgiOutputDuplication)
792 {
793 subsystem->dxgiOutputDuplication->lpVtbl->Release(subsystem->dxgiOutputDuplication);
794 subsystem->dxgiOutputDuplication = nullptr;
795 }
796
797 for (int attempt = 0; attempt < 240; ++attempt)
798 {
799 if (WaitForSingleObject(subsystem->base.server->StopEvent, 0) == WAIT_OBJECT_0)
800 return -1;
801
802 Sleep(500);
803 status = win_shadow_dxgi_init_duplication(subsystem);
804 if (status >= 0)
805 return 0;
806 }
807
808 return -1;
809 }
810 else if (hr == DXGI_ERROR_INVALID_CALL)
811 {
812 win_shadow_dxgi_uninit(subsystem);
813
814 status = win_shadow_dxgi_init(subsystem);
815
816 if (status < 0)
817 return -1;
818
819 return 0;
820 }
821
822 return -1;
823 }
824
825 hr = subsystem->dxgiResource->lpVtbl->QueryInterface(
826 subsystem->dxgiResource, &IID_ID3D11Texture2D, (void**)&(subsystem->dxgiDesktopImage));
827
828 if (subsystem->dxgiResource)
829 {
830 subsystem->dxgiResource->lpVtbl->Release(subsystem->dxgiResource);
831 subsystem->dxgiResource = nullptr;
832 }
833
834 if (FAILED(hr))
835 {
836 WLog_ERR(TAG, "IDXGIResource::QueryInterface(ID3D11Texture2D) failure: %s (0x%08lX)",
837 GetDxgiErrorString(hr), hr);
838 return -1;
839 }
840
841 return 1;
842}
843
844int win_shadow_dxgi_get_invalid_region(winShadowSubsystem* subsystem)
845{
846 HRESULT hr;
847 POINT* pSrcPt;
848 RECT* pDstRect;
849 RECT* pDirtyRect;
850 UINT numMoveRects;
851 UINT numDirtyRects;
852 UINT UsedBufferSize;
853 RECTANGLE_16 invalidRect;
854 UINT MetadataBufferSize;
855 UINT MoveRectsBufferSize;
856 UINT DirtyRectsBufferSize;
857 RECT* pDirtyRectsBuffer;
858 DXGI_OUTDUPL_MOVE_RECT* pMoveRect;
859 DXGI_OUTDUPL_MOVE_RECT* pMoveRectBuffer;
860 rdpShadowSurface* surface = nullptr;
861
862 WINPR_ASSERT(subsystem);
863 WINPR_ASSERT(subsystem->base.server);
864 surface = subsystem->base.server->surface;
865 WINPR_ASSERT(surface);
866
867 if (subsystem->dxgiFrameInfo.AccumulatedFrames == 0)
868 return 0;
869
870 if (subsystem->dxgiFrameInfo.TotalMetadataBufferSize == 0)
871 return 0;
872
873 MetadataBufferSize = subsystem->dxgiFrameInfo.TotalMetadataBufferSize;
874
875 if (MetadataBufferSize > subsystem->MetadataBufferSize)
876 {
877 subsystem->MetadataBuffer = (BYTE*)realloc(subsystem->MetadataBuffer, MetadataBufferSize);
878
879 if (!subsystem->MetadataBuffer)
880 return -1;
881
882 subsystem->MetadataBufferSize = MetadataBufferSize;
883 }
884
885 /* GetFrameMoveRects */
886
887 UsedBufferSize = 0;
888
889 MoveRectsBufferSize = MetadataBufferSize - UsedBufferSize;
890 pMoveRectBuffer = (DXGI_OUTDUPL_MOVE_RECT*)&(subsystem->MetadataBuffer[UsedBufferSize]);
891
892 hr = subsystem->dxgiOutputDuplication->lpVtbl->GetFrameMoveRects(
893 subsystem->dxgiOutputDuplication, MoveRectsBufferSize, pMoveRectBuffer,
894 &MoveRectsBufferSize);
895
896 if (FAILED(hr))
897 {
898 WLog_ERR(TAG,
899 "IDXGIOutputDuplication::GetFrameMoveRects failure: %s (0x%08lX) Size: %u Total "
900 "%u Used: %u",
901 GetDxgiErrorString(hr), hr, MoveRectsBufferSize, MetadataBufferSize,
902 UsedBufferSize);
903 return -1;
904 }
905
906 /* GetFrameDirtyRects */
907
908 UsedBufferSize += MoveRectsBufferSize;
909
910 DirtyRectsBufferSize = MetadataBufferSize - UsedBufferSize;
911 pDirtyRectsBuffer = (RECT*)&(subsystem->MetadataBuffer[UsedBufferSize]);
912
913 hr = subsystem->dxgiOutputDuplication->lpVtbl->GetFrameDirtyRects(
914 subsystem->dxgiOutputDuplication, DirtyRectsBufferSize, pDirtyRectsBuffer,
915 &DirtyRectsBufferSize);
916
917 if (FAILED(hr))
918 {
919 WLog_ERR(TAG,
920 "IDXGIOutputDuplication::GetFrameDirtyRects failure: %s (0x%08lX) Size: %u Total "
921 "%u Used: %u",
922 GetDxgiErrorString(hr), hr, DirtyRectsBufferSize, MetadataBufferSize,
923 UsedBufferSize);
924 return -1;
925 }
926
927 numMoveRects = MoveRectsBufferSize / sizeof(DXGI_OUTDUPL_MOVE_RECT);
928
929 for (UINT i = 0; i < numMoveRects; i++)
930 {
931 pMoveRect = &pMoveRectBuffer[i];
932 pSrcPt = &(pMoveRect->SourcePoint);
933 pDstRect = &(pMoveRect->DestinationRect);
934
935 invalidRect.left = (UINT16)pDstRect->left;
936 invalidRect.top = (UINT16)pDstRect->top;
937 invalidRect.right = (UINT16)pDstRect->right;
938 invalidRect.bottom = (UINT16)pDstRect->bottom;
939
940 region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
941 }
942
943 numDirtyRects = DirtyRectsBufferSize / sizeof(RECT);
944
945 for (UINT i = 0; i < numDirtyRects; i++)
946 {
947 pDirtyRect = &pDirtyRectsBuffer[i];
948
949 invalidRect.left = (UINT16)pDirtyRect->left;
950 invalidRect.top = (UINT16)pDirtyRect->top;
951 invalidRect.right = (UINT16)pDirtyRect->right;
952 invalidRect.bottom = (UINT16)pDirtyRect->bottom;
953
954 region16_union_rect(&(surface->invalidRegion), &(surface->invalidRegion), &invalidRect);
955 }
956
957 return 1;
958}
959
960#endif