VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxDispIf.cpp@ 44550

Last change on this file since 44550 was 44550, checked in by vboxsync, 12 years ago

VBoxTray/wddm: autoresize retries

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.8 KB
Line 
1/** @file
2 * VBoxTray - Display Settings Interface abstraction for XPDM & WDDM
3 */
4
5/*
6 * Copyright (C) 2006-2012 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17#include "VBoxTray.h"
18
19#include <iprt/log.h>
20#include <iprt/err.h>
21#include <iprt/assert.h>
22
23#include <malloc.h>
24
25#ifdef VBOX_WITH_WDDM
26#include <iprt/asm.h>
27#endif
28
29/* display driver interface abstraction for XPDM & WDDM
30 * with WDDM we can not use ExtEscape to communicate with our driver
31 * because we do not have XPDM display driver any more, i.e. escape requests are handled by cdd
32 * that knows nothing about us */
33DWORD VBoxDispIfInit(PVBOXDISPIF pIf)
34{
35 pIf->enmMode = VBOXDISPIF_MODE_XPDM;
36 return NO_ERROR;
37}
38
39#ifdef VBOX_WITH_WDDM
40static void vboxDispIfWddmTerm(PCVBOXDISPIF pIf);
41static DWORD vboxDispIfWddmInit(PCVBOXDISPIF pIf);
42#endif
43
44DWORD VBoxDispIfTerm(PVBOXDISPIF pIf)
45{
46#ifdef VBOX_WITH_WDDM
47 if (pIf->enmMode == VBOXDISPIF_MODE_WDDM)
48 {
49 vboxDispIfWddmTerm(pIf);
50 }
51#endif
52
53 pIf->enmMode = VBOXDISPIF_MODE_UNKNOWN;
54 return NO_ERROR;
55}
56
57static DWORD vboxDispIfEscapeXPDM(PCVBOXDISPIF pIf, PVBOXDISPIFESCAPE pEscape, int cbData, int iDirection)
58{
59 HDC hdc = GetDC(HWND_DESKTOP);
60 VOID *pvData = cbData ? VBOXDISPIFESCAPE_DATA(pEscape, VOID) : NULL;
61 int iRet = ExtEscape(hdc, pEscape->escapeCode,
62 iDirection >= 0 ? cbData : 0,
63 iDirection >= 0 ? (LPSTR)pvData : NULL,
64 iDirection <= 0 ? cbData : 0,
65 iDirection <= 0 ? (LPSTR)pvData : NULL);
66 ReleaseDC(HWND_DESKTOP, hdc);
67 if (iRet > 0)
68 return VINF_SUCCESS;
69 else if (iRet == 0)
70 return ERROR_NOT_SUPPORTED;
71 /* else */
72 return ERROR_GEN_FAILURE;
73}
74
75#ifdef VBOX_WITH_WDDM
76static DWORD vboxDispIfSwitchToWDDM(PVBOXDISPIF pIf)
77{
78 DWORD err = NO_ERROR;
79 OSVERSIONINFO OSinfo;
80 OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
81 GetVersionEx (&OSinfo);
82 bool bSupported = true;
83
84 if (OSinfo.dwMajorVersion >= 6)
85 {
86 Log((__FUNCTION__": this is vista and up\n"));
87 HMODULE hUser = GetModuleHandle("USER32");
88 if (hUser)
89 {
90 *(uintptr_t *)&pIf->modeData.wddm.pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
91 Log((__FUNCTION__": VBoxDisplayInit: pfnChangeDisplaySettingsEx = %p\n", pIf->modeData.wddm.pfnChangeDisplaySettingsEx));
92 bSupported &= !!(pIf->modeData.wddm.pfnChangeDisplaySettingsEx);
93
94 *(uintptr_t *)&pIf->modeData.wddm.pfnEnumDisplayDevices = (uintptr_t)GetProcAddress(hUser, "EnumDisplayDevicesA");
95 Log((__FUNCTION__": VBoxDisplayInit: pfnEnumDisplayDevices = %p\n", pIf->modeData.wddm.pfnEnumDisplayDevices));
96 bSupported &= !!(pIf->modeData.wddm.pfnEnumDisplayDevices);
97
98 /* this is vista and up */
99 HMODULE hGdi32 = GetModuleHandle("gdi32");
100 if (hGdi32 != NULL)
101 {
102 pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc = (PFND3DKMT_OPENADAPTERFROMHDC)GetProcAddress(hGdi32, "D3DKMTOpenAdapterFromHdc");
103 Log((__FUNCTION__"pfnD3DKMTOpenAdapterFromHdc = %p\n", pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc));
104 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc);
105
106 pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName = (PFND3DKMT_OPENADAPTERFROMGDIDISPLAYNAME)GetProcAddress(hGdi32, "D3DKMTOpenAdapterFromGdiDisplayName");
107 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName = %p\n", pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName));
108 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromGdiDisplayName);
109
110 pIf->modeData.wddm.pfnD3DKMTCloseAdapter = (PFND3DKMT_CLOSEADAPTER)GetProcAddress(hGdi32, "D3DKMTCloseAdapter");
111 Log((__FUNCTION__": pfnD3DKMTCloseAdapter = %p\n", pIf->modeData.wddm.pfnD3DKMTCloseAdapter));
112 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTCloseAdapter);
113
114 pIf->modeData.wddm.pfnD3DKMTEscape = (PFND3DKMT_ESCAPE)GetProcAddress(hGdi32, "D3DKMTEscape");
115 Log((__FUNCTION__": pfnD3DKMTEscape = %p\n", pIf->modeData.wddm.pfnD3DKMTEscape));
116 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTCloseAdapter);
117
118 pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn = (PFND3DKMT_INVALIDATEACTIVEVIDPN)GetProcAddress(hGdi32, "D3DKMTInvalidateActiveVidPn");
119 Log((__FUNCTION__": pfnD3DKMTInvalidateActiveVidPn = %p\n", pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn));
120 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn);
121
122 pIf->modeData.wddm.pfnD3DKMTPollDisplayChildren = (PFND3DKMT_POLLDISPLAYCHILDREN)GetProcAddress(hGdi32, "D3DKMTPollDisplayChildren");
123 Log((__FUNCTION__": pfnD3DKMTPollDisplayChildren = %p\n", pIf->modeData.wddm.pfnD3DKMTPollDisplayChildren));
124 bSupported &= !!(pIf->modeData.wddm.pfnD3DKMTPollDisplayChildren);
125
126 if (!bSupported)
127 {
128 Log((__FUNCTION__": one of pfnD3DKMT function pointers failed to initialize\n"));
129 err = ERROR_NOT_SUPPORTED;
130 }
131 }
132 else
133 {
134 Log((__FUNCTION__": GetModuleHandle(gdi32) failed, err(%d)\n", GetLastError()));
135 err = ERROR_NOT_SUPPORTED;
136 }
137
138 }
139 else
140 {
141 Log((__FUNCTION__": GetModuleHandle(USER32) failed, err(%d)\n", GetLastError()));
142 err = ERROR_NOT_SUPPORTED;
143 }
144 }
145 else
146 {
147 Log((__FUNCTION__": can not switch to VBOXDISPIF_MODE_WDDM, because os is not Vista or upper\n"));
148 err = ERROR_NOT_SUPPORTED;
149 }
150
151 if (err == ERROR_SUCCESS)
152 {
153 err = vboxDispIfWddmInit(pIf);
154 }
155
156 return err;
157}
158
159static DWORD vboxDispIfWDDMAdpHdcCreate(int iDisplay, HDC *phDc, DISPLAY_DEVICE *pDev)
160{
161 DWORD winEr = ERROR_INVALID_STATE;
162 memset(pDev, 0, sizeof (*pDev));
163 pDev->cb = sizeof (*pDev);
164
165 for (int i = 0; ; ++i)
166 {
167 if (EnumDisplayDevices(NULL, /* LPCTSTR lpDevice */ i, /* DWORD iDevNum */
168 pDev, 0 /* DWORD dwFlags*/))
169 {
170 if (i == iDisplay || (iDisplay < 0 && pDev->StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE))
171 {
172 HDC hDc = CreateDC(NULL, pDev->DeviceName, NULL, NULL);
173 if (hDc)
174 {
175 *phDc = hDc;
176 return NO_ERROR;
177 }
178 else
179 {
180 winEr = GetLastError();
181 Log(("CreateDC failed %d", winEr));
182 break;
183 }
184 }
185 Log(("display data no match display(%d): i(%d), flags(%d)", iDisplay, i, pDev->StateFlags));
186 }
187 else
188 {
189 winEr = GetLastError();
190 Log(("EnumDisplayDevices failed %d", winEr));
191 break;
192 }
193 }
194
195 Log(("vboxDispIfWDDMAdpHdcCreate failure branch %d", winEr));
196 return winEr;
197}
198
199
200typedef DECLCALLBACK(BOOLEAN) FNVBOXDISPIFWDDM_ADAPTEROP(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, DISPLAY_DEVICE *pDev, PVOID pContext);
201typedef FNVBOXDISPIFWDDM_ADAPTEROP *PFNVBOXDISPIFWDDM_ADAPTEROP;
202static DWORD vboxDispIfWDDMAdapterOp(PCVBOXDISPIF pIf, int iDisplay, PFNVBOXDISPIFWDDM_ADAPTEROP pfnOp, PVOID pContext)
203{
204 D3DKMT_OPENADAPTERFROMHDC OpenAdapterData = {0};
205 DISPLAY_DEVICE DDev;
206 DWORD err = vboxDispIfWDDMAdpHdcCreate(iDisplay, &OpenAdapterData.hDc, &DDev);
207 Assert(err == NO_ERROR);
208 if (err == NO_ERROR)
209 {
210 NTSTATUS Status = pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc(&OpenAdapterData);
211 Assert(!Status);
212 if (!Status)
213 {
214 BOOLEAN bCloseAdapter = pfnOp(pIf, OpenAdapterData.hAdapter, &DDev, pContext);
215
216 if (bCloseAdapter)
217 {
218 D3DKMT_CLOSEADAPTER ClosaAdapterData = {0};
219 ClosaAdapterData.hAdapter = OpenAdapterData.hAdapter;
220 Status = pIf->modeData.wddm.pfnD3DKMTCloseAdapter(&ClosaAdapterData);
221 if (Status)
222 {
223 Log((__FUNCTION__": pfnD3DKMTCloseAdapter failed, Status (0x%x)\n", Status));
224 }
225 }
226 }
227 else
228 {
229 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName failed, Status (0x%x)\n", Status));
230 err = ERROR_GEN_FAILURE;
231 }
232
233 DeleteDC(OpenAdapterData.hDc);
234 }
235 else
236 Log((__FUNCTION__": vboxDispIfWDDMAdpHdcCreate failed, winEr (%d)\n", err));
237
238 return err;
239}
240
241typedef struct
242{
243 NTSTATUS Status;
244 PVBOXDISPIFESCAPE pEscape;
245 int cbData;
246 D3DDDI_ESCAPEFLAGS EscapeFlags;
247} VBOXDISPIFWDDM_ESCAPEOP_CONTEXT, *PVBOXDISPIFWDDM_ESCAPEOP_CONTEXT;
248
249DECLCALLBACK(BOOLEAN) vboxDispIfEscapeWDDMOp(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, DISPLAY_DEVICE *pDev, PVOID pContext)
250{
251 PVBOXDISPIFWDDM_ESCAPEOP_CONTEXT pCtx = (PVBOXDISPIFWDDM_ESCAPEOP_CONTEXT)pContext;
252
253 D3DKMT_ESCAPE EscapeData = {0};
254 EscapeData.hAdapter = hAdapter;
255 //EscapeData.hDevice = NULL;
256 EscapeData.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
257 EscapeData.Flags = pCtx->EscapeFlags;
258 EscapeData.pPrivateDriverData = pCtx->pEscape;
259 EscapeData.PrivateDriverDataSize = VBOXDISPIFESCAPE_SIZE(pCtx->cbData);
260 //EscapeData.hContext = NULL;
261
262 pCtx->Status = pIf->modeData.wddm.pfnD3DKMTEscape(&EscapeData);
263
264 return TRUE;
265}
266
267static DWORD vboxDispIfEscapeWDDM(PCVBOXDISPIF pIf, PVBOXDISPIFESCAPE pEscape, int cbData, BOOL fHwAccess)
268{
269 VBOXDISPIFWDDM_ESCAPEOP_CONTEXT Ctx = {0};
270 Ctx.pEscape = pEscape;
271 Ctx.cbData = cbData;
272 if (fHwAccess)
273 Ctx.EscapeFlags.HardwareAccess = 1;
274 DWORD err = vboxDispIfWDDMAdapterOp(pIf, -1 /* iDisplay, -1 means primary */, vboxDispIfEscapeWDDMOp, &Ctx);
275 if (err == NO_ERROR)
276 {
277 if (!Ctx.Status)
278 err = NO_ERROR;
279 else
280 {
281 if (Ctx.Status == 0xC00000BBL) /* not supported */
282 err = ERROR_NOT_SUPPORTED;
283 else
284 err = ERROR_GEN_FAILURE;
285 Log((__FUNCTION__": pfnD3DKMTEscape failed, Status (0x%x)\n", Ctx.Status));
286 }
287 }
288 else
289 Log((__FUNCTION__": vboxDispIfWDDMAdapterOp failed, err (%d)\n", err));
290
291 return err;
292}
293
294typedef struct
295{
296 NTSTATUS Status;
297 VBOXWDDM_RECOMMENDVIDPN_SCREEN_INFO Info;
298} VBOXDISPIFWDDM_RESIZEOP_CONTEXT, *PVBOXDISPIFWDDM_RESIZEOP_CONTEXT;
299
300DECLCALLBACK(BOOLEAN) vboxDispIfResizeWDDMOp(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, DISPLAY_DEVICE *pDev, PVOID pContext)
301{
302 PVBOXDISPIFWDDM_RESIZEOP_CONTEXT pCtx = (PVBOXDISPIFWDDM_RESIZEOP_CONTEXT)pContext;
303 D3DKMT_INVALIDATEACTIVEVIDPN IAVidPnData = {0};
304 uint32_t cbData = VBOXWDDM_RECOMMENDVIDPN_SIZE(1);
305 PVBOXWDDM_RECOMMENDVIDPN pData = (PVBOXWDDM_RECOMMENDVIDPN)malloc(cbData);
306 if (pData)
307 {
308 memset(pData, 0, cbData);
309 pData->cScreenInfos = 1;
310 memcpy(&pData->aScreenInfos[0], &pCtx->Info, sizeof (VBOXWDDM_RECOMMENDVIDPN_SCREEN_INFO));
311
312 IAVidPnData.hAdapter = hAdapter;
313 IAVidPnData.pPrivateDriverData = pData;
314 IAVidPnData.PrivateDriverDataSize = cbData;
315
316 pCtx->Status = pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn(&IAVidPnData);
317 Assert(!pCtx->Status);
318 if (pCtx->Status)
319 Log((__FUNCTION__": pfnD3DKMTInvalidateActiveVidPn failed, Status (0x%x)\n", pCtx->Status));
320
321 free(pData);
322 }
323 else
324 {
325 Log((__FUNCTION__": malloc failed\n"));
326 pCtx->Status = -1;
327 }
328
329 return TRUE;
330}
331
332static DWORD vboxDispIfResizeWDDM(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
333{
334 VBOXDISPIFWDDM_RESIZEOP_CONTEXT Ctx = {0};
335 Ctx.Info.Id = Id;
336 Ctx.Info.Width = Width;
337 Ctx.Info.Height = Height;
338 Ctx.Info.BitsPerPixel = BitsPerPixel;
339 DWORD err = vboxDispIfWDDMAdapterOp(pIf, -1, /* (int)Id - always say -1 to use primary display since the display does not really matter here */
340 vboxDispIfResizeWDDMOp, &Ctx);
341 if (err == NO_ERROR)
342 {
343 if (!Ctx.Status)
344 err = NO_ERROR;
345 else
346 {
347 if (Ctx.Status == 0xC00000BBL) /* not supported */
348 err = ERROR_NOT_SUPPORTED;
349 else
350 err = ERROR_GEN_FAILURE;
351 Log((__FUNCTION__": vboxDispIfResizeWDDMOp failed, Status (0x%x)\n", Ctx.Status));
352 }
353 }
354 else
355 Log((__FUNCTION__": vboxDispIfWDDMAdapterOp failed, err (%d)\n", err));
356
357 return err;
358}
359#endif
360
361DWORD VBoxDispIfEscape(PCVBOXDISPIF pIf, PVBOXDISPIFESCAPE pEscape, int cbData)
362{
363 switch (pIf->enmMode)
364 {
365 case VBOXDISPIF_MODE_XPDM_NT4:
366 case VBOXDISPIF_MODE_XPDM:
367 return vboxDispIfEscapeXPDM(pIf, pEscape, cbData, 1);
368#ifdef VBOX_WITH_WDDM
369 case VBOXDISPIF_MODE_WDDM:
370 return vboxDispIfEscapeWDDM(pIf, pEscape, cbData, TRUE /* BOOL fHwAccess */);
371#endif
372 default:
373 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
374 return ERROR_INVALID_PARAMETER;
375 }
376}
377
378DWORD VBoxDispIfEscapeInOut(PCVBOXDISPIF const pIf, PVBOXDISPIFESCAPE pEscape, int cbData)
379{
380 switch (pIf->enmMode)
381 {
382 case VBOXDISPIF_MODE_XPDM_NT4:
383 case VBOXDISPIF_MODE_XPDM:
384 return vboxDispIfEscapeXPDM(pIf, pEscape, cbData, 0);
385#ifdef VBOX_WITH_WDDM
386 case VBOXDISPIF_MODE_WDDM:
387 return vboxDispIfEscapeWDDM(pIf, pEscape, cbData, TRUE /* BOOL fHwAccess */);
388#endif
389 default:
390 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
391 return ERROR_INVALID_PARAMETER;
392 }
393}
394
395static DWORD vboxDispIfResizeXPDM(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
396{
397 return ERROR_NOT_SUPPORTED;
398}
399
400DWORD VBoxDispIfResize(PCVBOXDISPIF const pIf, ULONG Id, DWORD Width, DWORD Height, DWORD BitsPerPixel)
401{
402 switch (pIf->enmMode)
403 {
404 case VBOXDISPIF_MODE_XPDM_NT4:
405 return ERROR_NOT_SUPPORTED;
406 case VBOXDISPIF_MODE_XPDM:
407 return vboxDispIfResizeXPDM(pIf, Id, Width, Height, BitsPerPixel);
408#ifdef VBOX_WITH_WDDM
409 case VBOXDISPIF_MODE_WDDM:
410 return vboxDispIfResizeWDDM(pIf, Id, Width, Height, BitsPerPixel);
411#endif
412 default:
413 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
414 return ERROR_INVALID_PARAMETER;
415 }
416}
417
418
419#ifdef VBOX_WITH_WDDM
420/**/
421#define VBOXRR_TIMER_ID 1234
422
423typedef struct VBOXRR
424{
425 HANDLE hThread;
426 DWORD idThread;
427 HANDLE hEvent;
428 HWND hWnd;
429 CRITICAL_SECTION CritSect;
430 UINT_PTR idTimer;
431 PCVBOXDISPIF pIf;
432 DISPLAY_DEVICE *paDisplayDevices;
433 DEVMODE *paDeviceModes;
434 UINT cDevModes;
435} VBOXRR, *PVBOXRR;
436
437static VBOXRR g_VBoxRr = {0};
438
439#define VBOX_E_INSUFFICIENT_BUFFER HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER)
440#define VBOX_E_NOT_SUPPORTED HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED)
441
442static void vboxRrRetryStopLocked()
443{
444 PVBOXRR pMon = &g_VBoxRr;
445 if (pMon->pIf)
446 {
447 if (pMon->paDisplayDevices)
448 {
449 free(pMon->paDisplayDevices);
450 pMon->paDisplayDevices = NULL;
451 }
452
453 if (pMon->paDeviceModes)
454 {
455 free(pMon->paDeviceModes);
456 pMon->paDeviceModes = NULL;
457 }
458
459 if (pMon->idTimer)
460 {
461 KillTimer(pMon->hWnd, pMon->idTimer);
462 pMon->idTimer = 0;
463 }
464
465 pMon->cDevModes = 0;
466 pMon->pIf = NULL;
467 }
468}
469
470static void VBoxRrRetryStop()
471{
472 PVBOXRR pMon = &g_VBoxRr;
473 EnterCriticalSection(&pMon->CritSect);
474 vboxRrRetryStopLocked();
475 LeaveCriticalSection(&pMon->CritSect);
476}
477
478static DWORD vboxDispIfWddmValidateFixResize(PCVBOXDISPIF const pIf, DISPLAY_DEVICE *paDisplayDevices, DEVMODE *paDeviceModes, UINT cDevModes);
479
480static void vboxRrRetryReschedule()
481{
482}
483
484static void VBoxRrRetrySchedule(PCVBOXDISPIF const pIf, DISPLAY_DEVICE *paDisplayDevices, DEVMODE *paDeviceModes, UINT cDevModes)
485{
486 PVBOXRR pMon = &g_VBoxRr;
487 EnterCriticalSection(&pMon->CritSect);
488 vboxRrRetryStopLocked();
489
490 pMon->pIf = pIf;
491 if (cDevModes)
492 {
493 pMon->paDisplayDevices = (DISPLAY_DEVICE*)malloc(sizeof (*paDisplayDevices) * cDevModes);
494 Assert(pMon->paDisplayDevices);
495 if (!pMon->paDisplayDevices)
496 {
497 Log(("malloc failed!"));
498 vboxRrRetryStopLocked();
499 LeaveCriticalSection(&pMon->CritSect);
500 return;
501 }
502 memcpy(pMon->paDisplayDevices, paDisplayDevices, sizeof (*paDisplayDevices) * cDevModes);
503
504 pMon->paDeviceModes = (DEVMODE*)malloc(sizeof (*paDeviceModes) * cDevModes);
505 Assert(pMon->paDeviceModes);
506 if (!pMon->paDeviceModes)
507 {
508 Log(("malloc failed!"));
509 vboxRrRetryStopLocked();
510 LeaveCriticalSection(&pMon->CritSect);
511 return;
512 }
513 memcpy(pMon->paDeviceModes, paDeviceModes, sizeof (*paDeviceModes) * cDevModes);
514 }
515 pMon->cDevModes = cDevModes;
516
517 pMon->idTimer = SetTimer(pMon->hWnd, VBOXRR_TIMER_ID, 1000, (TIMERPROC)NULL);
518 Assert(pMon->idTimer);
519 if (!pMon->idTimer)
520 {
521 Log(("SetTimer failed!, err %d", GetLastError()));
522 vboxRrRetryStopLocked();
523 }
524
525 LeaveCriticalSection(&pMon->CritSect);
526}
527
528static void vboxRrRetryPerform()
529{
530 PVBOXRR pMon = &g_VBoxRr;
531 EnterCriticalSection(&pMon->CritSect);
532 if (pMon->pIf)
533 {
534 DWORD dwErr = vboxDispIfWddmValidateFixResize(pMon->pIf, pMon->paDisplayDevices, pMon->paDeviceModes, pMon->cDevModes);
535 if (ERROR_RETRY != dwErr)
536 VBoxRrRetryStop();
537 else
538 vboxRrRetryReschedule();
539 }
540 LeaveCriticalSection(&pMon->CritSect);
541}
542
543static LRESULT CALLBACK vboxRrWndProc(HWND hwnd,
544 UINT uMsg,
545 WPARAM wParam,
546 LPARAM lParam
547)
548{
549 switch(uMsg)
550 {
551 case WM_DISPLAYCHANGE:
552 {
553 VBoxRrRetryStop();
554 return 0;
555 }
556 case WM_TIMER:
557 {
558 if (wParam == VBOXRR_TIMER_ID)
559 {
560 vboxRrRetryPerform();
561 return 0;
562 }
563 break;
564 }
565 case WM_CLOSE:
566 Log((__FUNCTION__": got WM_CLOSE for hwnd(0x%x)", hwnd));
567 return 0;
568 case WM_DESTROY:
569 Log((__FUNCTION__": got WM_DESTROY for hwnd(0x%x)", hwnd));
570 return 0;
571 case WM_NCHITTEST:
572 Log((__FUNCTION__": got WM_NCHITTEST for hwnd(0x%x)\n", hwnd));
573 return HTNOWHERE;
574 default:
575 break;
576 }
577
578 return DefWindowProc(hwnd, uMsg, wParam, lParam);
579}
580
581#define VBOXRRWND_NAME "VBoxRrWnd"
582
583static HRESULT vboxRrWndCreate(HWND *phWnd)
584{
585 HRESULT hr = S_OK;
586 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
587 /* Register the Window Class. */
588 WNDCLASS wc;
589 if (!GetClassInfo(hInstance, VBOXRRWND_NAME, &wc))
590 {
591 wc.style = 0;//CS_OWNDC;
592 wc.lpfnWndProc = vboxRrWndProc;
593 wc.cbClsExtra = 0;
594 wc.cbWndExtra = 0;
595 wc.hInstance = hInstance;
596 wc.hIcon = NULL;
597 wc.hCursor = NULL;
598 wc.hbrBackground = NULL;
599 wc.lpszMenuName = NULL;
600 wc.lpszClassName = VBOXRRWND_NAME;
601 if (!RegisterClass(&wc))
602 {
603 DWORD winErr = GetLastError();
604 Log((__FUNCTION__": RegisterClass failed, winErr(%d)\n", winErr));
605 hr = E_FAIL;
606 }
607 }
608
609 if (hr == S_OK)
610 {
611 HWND hWnd = CreateWindowEx (WS_EX_TOOLWINDOW,
612 VBOXRRWND_NAME, VBOXRRWND_NAME,
613 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
614 -100, -100,
615 10, 10,
616 NULL, //GetDesktopWindow() /* hWndParent */,
617 NULL /* hMenu */,
618 hInstance,
619 NULL /* lpParam */);
620 Assert(hWnd);
621 if (hWnd)
622 {
623 *phWnd = hWnd;
624 }
625 else
626 {
627 DWORD winErr = GetLastError();
628 Log((__FUNCTION__": CreateWindowEx failed, winErr(%d)\n", winErr));
629 hr = E_FAIL;
630 }
631 }
632
633 return hr;
634}
635
636static HRESULT vboxRrWndDestroy(HWND hWnd)
637{
638 BOOL bResult = DestroyWindow(hWnd);
639 if (bResult)
640 return S_OK;
641
642 DWORD winErr = GetLastError();
643 Log((__FUNCTION__": DestroyWindow failed, winErr(%d) for hWnd(0x%x)\n", winErr, hWnd));
644
645 return HRESULT_FROM_WIN32(winErr);
646}
647
648static HRESULT vboxRrWndInit()
649{
650 PVBOXRR pMon = &g_VBoxRr;
651 return vboxRrWndCreate(&pMon->hWnd);
652}
653
654HRESULT vboxRrWndTerm()
655{
656 PVBOXRR pMon = &g_VBoxRr;
657 HRESULT tmpHr = vboxRrWndDestroy(pMon->hWnd);
658 Assert(tmpHr == S_OK);
659
660 HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
661 UnregisterClass(VBOXRRWND_NAME, hInstance);
662
663 return S_OK;
664}
665
666#define WM_VBOXRR_INIT_QUIT (WM_APP+2)
667
668HRESULT vboxRrRun()
669{
670 PVBOXRR pMon = &g_VBoxRr;
671 MSG Msg;
672
673 HRESULT hr = S_FALSE;
674
675 PeekMessage(&Msg,
676 NULL /* HWND hWnd */,
677 WM_USER /* UINT wMsgFilterMin */,
678 WM_USER /* UINT wMsgFilterMax */,
679 PM_NOREMOVE);
680
681 do
682 {
683 BOOL bResult = GetMessage(&Msg,
684 0 /*HWND hWnd*/,
685 0 /*UINT wMsgFilterMin*/,
686 0 /*UINT wMsgFilterMax*/
687 );
688
689 if(!bResult) /* WM_QUIT was posted */
690 {
691 hr = S_FALSE;
692 VBoxRrRetryStop();
693 break;
694 }
695
696 if(bResult == -1) /* error occurred */
697 {
698 DWORD winEr = GetLastError();
699 hr = HRESULT_FROM_WIN32(winEr);
700 Assert(0);
701 /* just ensure we never return success in this case */
702 Assert(hr != S_OK);
703 Assert(hr != S_FALSE);
704 if (hr == S_OK || hr == S_FALSE)
705 hr = E_FAIL;
706 VBoxRrRetryStop();
707 break;
708 }
709
710 switch (Msg.message)
711 {
712 case WM_VBOXRR_INIT_QUIT:
713 case WM_CLOSE:
714 {
715 VBoxRrRetryStop();
716 PostQuitMessage(0);
717 break;
718 }
719 default:
720 TranslateMessage(&Msg);
721 DispatchMessage(&Msg);
722 break;
723 }
724 } while (1);
725 return 0;
726}
727
728static DWORD WINAPI vboxRrRunnerThread(void *pvUser)
729{
730 PVBOXRR pMon = &g_VBoxRr;
731
732 BOOL bRc = SetEvent(pMon->hEvent);
733 if (!bRc)
734 {
735 DWORD winErr = GetLastError();
736 Log((__FUNCTION__": SetEvent failed, winErr = (%d)", winErr));
737 HRESULT tmpHr = HRESULT_FROM_WIN32(winErr);
738 Assert(tmpHr != S_OK);
739 }
740
741 HRESULT hr = vboxRrWndInit();
742 Assert(hr == S_OK);
743 if (hr == S_OK)
744 {
745 hr = vboxRrRun();
746 Assert(hr == S_OK);
747
748 vboxRrWndTerm();
749 }
750
751 return 0;
752}
753
754HRESULT VBoxRrInit()
755{
756 HRESULT hr = E_FAIL;
757 PVBOXRR pMon = &g_VBoxRr;
758 memset(pMon, 0, sizeof (*pMon));
759
760 InitializeCriticalSection(&pMon->CritSect);
761
762 pMon->hEvent = CreateEvent(NULL, /* LPSECURITY_ATTRIBUTES lpEventAttributes*/
763 TRUE, /* BOOL bManualReset*/
764 FALSE, /* BOOL bInitialState */
765 NULL /* LPCTSTR lpName */
766 );
767 if (pMon->hEvent)
768 {
769 pMon->hThread = CreateThread(NULL /* LPSECURITY_ATTRIBUTES lpThreadAttributes */,
770 0 /* SIZE_T dwStackSize */,
771 vboxRrRunnerThread,
772 pMon,
773 0 /* DWORD dwCreationFlags */,
774 &pMon->idThread);
775 if (pMon->hThread)
776 {
777 DWORD dwResult = WaitForSingleObject(pMon->hEvent, INFINITE);
778 if (dwResult == WAIT_OBJECT_0)
779 return S_OK;
780 else
781 {
782 Log(("WaitForSingleObject failed!"));
783 hr = E_FAIL;
784 }
785 }
786 else
787 {
788 DWORD winErr = GetLastError();
789 Log((__FUNCTION__": CreateThread failed, winErr = (%d)", winErr));
790 hr = HRESULT_FROM_WIN32(winErr);
791 Assert(hr != S_OK);
792 }
793 CloseHandle(pMon->hEvent);
794 }
795 else
796 {
797 DWORD winErr = GetLastError();
798 Log((__FUNCTION__": CreateEvent failed, winErr = (%d)", winErr));
799 hr = HRESULT_FROM_WIN32(winErr);
800 Assert(hr != S_OK);
801 }
802
803 DeleteCriticalSection(&pMon->CritSect);
804
805 return hr;
806}
807
808VOID VBoxRrTerm()
809{
810 HRESULT hr;
811 PVBOXRR pMon = &g_VBoxRr;
812 if (!pMon->hThread)
813 return;
814
815 BOOL bResult = PostThreadMessage(pMon->idThread, WM_VBOXRR_INIT_QUIT, 0, 0);
816 DWORD winErr;
817 if (bResult
818 || (winErr = GetLastError()) == ERROR_INVALID_THREAD_ID) /* <- could be that the thread is terminated */
819 {
820 DWORD dwErr = WaitForSingleObject(pMon->hThread, INFINITE);
821 if (dwErr == WAIT_OBJECT_0)
822 {
823 hr = S_OK;
824 }
825 else
826 {
827 winErr = GetLastError();
828 hr = HRESULT_FROM_WIN32(winErr);
829 }
830 }
831 else
832 {
833 hr = HRESULT_FROM_WIN32(winErr);
834 }
835
836 DeleteCriticalSection(&pMon->CritSect);
837
838 CloseHandle(pMon->hThread);
839 pMon->hThread = 0;
840 CloseHandle(pMon->hEvent);
841 pMon->hThread = 0;
842}
843/**/
844
845typedef struct VBOXDISPIF_WDDM_INTERNAL
846{
847 PCVBOXDISPIF pIf;
848} VBOXDISPIF_WDDM_INTERNAL, *PVBOXDISPIF_WDDM_INTERNAL;
849
850static VBOXDISPIF_WDDM_INTERNAL g_VBoxDispIfWddm;
851
852static BOOL vboxDispIfWddmValidateResize(DISPLAY_DEVICE *paDisplayDevices, DEVMODE *paDeviceModes, UINT cDevModes)
853{
854 DISPLAY_DEVICE DisplayDevice;
855 int i = 0;
856 UINT cMatched = 0;
857 DEVMODE CurDevMode, RegDevMode;
858 for (int i = 0; ; ++i)
859 {
860 ZeroMemory(&DisplayDevice, sizeof(DISPLAY_DEVICE));
861 DisplayDevice.cb = sizeof(DISPLAY_DEVICE);
862
863 if (!EnumDisplayDevices (NULL, i, &DisplayDevice, 0))
864 break;
865
866 Log(("VBoxTray: vboxDispIfValidateResize: [%d(%d)] %s\n", i, cMatched, DisplayDevice.DeviceName));
867
868 BOOL bFetchDevice = FALSE;
869
870 if (DisplayDevice.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
871 {
872 Log(("VBoxTray: vboxDispIfValidateResize: Found primary device. err %d\n", GetLastError ()));
873 bFetchDevice = TRUE;
874 }
875 else if (!(DisplayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER))
876 {
877
878 Log(("VBoxTray: vboxDispIfValidateResize: Found secondary device. err %d\n", GetLastError ()));
879 bFetchDevice = TRUE;
880 }
881
882 if (bFetchDevice)
883 {
884 if (cMatched >= cDevModes)
885 {
886 Log(("VBoxTray: vboxDispIfValidateResize: %d >= %d\n", cDevModes, cMatched));
887 return FALSE;
888 }
889
890 /* First try to get the video mode stored in registry (ENUM_REGISTRY_SETTINGS).
891 * A secondary display could be not active at the moment and would not have
892 * a current video mode (ENUM_CURRENT_SETTINGS).
893 */
894 ZeroMemory(&RegDevMode, sizeof(RegDevMode));
895 RegDevMode.dmSize = sizeof(DEVMODE);
896 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
897 ENUM_REGISTRY_SETTINGS, &RegDevMode))
898 {
899 Log(("VBoxTray: vboxDispIfValidateResize: EnumDisplaySettings error %d\n", GetLastError ()));
900 return FALSE;
901 }
902
903 /* with Win8 WDDM Display-only driver, it seems like sometimes we get an auto-resize setting being stored in registry, although current settings do not match */
904 ZeroMemory(&CurDevMode, sizeof(CurDevMode));
905 CurDevMode.dmSize = sizeof(CurDevMode);
906 if (!EnumDisplaySettings((LPSTR)DisplayDevice.DeviceName,
907 ENUM_CURRENT_SETTINGS, &CurDevMode))
908 {
909 /* ENUM_CURRENT_SETTINGS returns FALSE when the display is not active:
910 * for example a disabled secondary display */
911 Log(("VBoxTray: vboxDispIfValidateResize: EnumDisplaySettings(ENUM_CURRENT_SETTINGS) error %d\n", GetLastError ()));
912 return FALSE;
913 }
914
915 /* No ENUM_REGISTRY_SETTINGS yet. Seen on Vista after installation.
916 * Get the current video mode then.
917 */
918 if ( RegDevMode.dmPelsWidth != 0
919 && RegDevMode.dmPelsHeight == 0)
920 {
921 if (CurDevMode.dmBitsPerPel != RegDevMode.dmBitsPerPel
922 || CurDevMode.dmPelsWidth != RegDevMode.dmPelsWidth
923 || CurDevMode.dmPelsHeight != RegDevMode.dmPelsHeight
924 || CurDevMode.dmPosition.x != RegDevMode.dmPosition.x
925 || CurDevMode.dmPosition.y != RegDevMode.dmPosition.y)
926 {
927 Log(("VBoxTray: vboxDispIfValidateResize: current settings do not match registry settings, trating as no-match"));
928 return FALSE;
929 }
930 }
931
932 UINT j = 0;
933 for (; j < cDevModes; ++j)
934 {
935 if (!strncmp(DisplayDevice.DeviceName, paDisplayDevices[j].DeviceName, RT_ELEMENTS(CurDevMode.dmDeviceName)))
936 {
937 if (paDeviceModes[j].dmBitsPerPel != CurDevMode.dmBitsPerPel
938 || (paDeviceModes[j].dmPelsWidth & 0xfff8) != (CurDevMode.dmPelsWidth & 0xfff8)
939 || (paDeviceModes[j].dmPelsHeight & 0xfff8) != (CurDevMode.dmPelsHeight & 0xfff8)
940 || (paDeviceModes[j].dmPosition.x & 0xfff8) != (CurDevMode.dmPosition.x & 0xfff8)
941 || (paDeviceModes[j].dmPosition.y & 0xfff8) != (CurDevMode.dmPosition.y & 0xfff8)
942 || (paDisplayDevices[j].StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != (DisplayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP))
943 {
944 return FALSE;
945 }
946 break;
947 }
948 }
949
950 if (j == cDevModes)
951 return FALSE;
952
953 ++cMatched;
954 }
955 }
956
957 return cMatched == cDevModes;
958}
959
960static DWORD vboxDispIfWddmValidateFixResize(PCVBOXDISPIF const pIf, DISPLAY_DEVICE *paDisplayDevices, DEVMODE *paDeviceModes, UINT cDevModes)
961{
962 if (vboxDispIfWddmValidateResize(paDisplayDevices, paDeviceModes, cDevModes))
963 return NO_ERROR;
964
965 LONG status = DISP_CHANGE_SUCCESSFUL;
966
967 /* now try to resize in a "regular" way */
968 /* Assign the new rectangles to displays. */
969 for (UINT i = 0; i < cDevModes; i++)
970 {
971 /* On Vista one must specify DM_BITSPERPEL.
972 * Note that the current mode dmBitsPerPel is already in the DEVMODE structure.
973 */
974 paDeviceModes[i].dmFields = DM_POSITION | DM_PELSHEIGHT | DM_PELSWIDTH | DM_BITSPERPEL;
975
976 Log(("VBoxTray: ResizeDisplayDevice: pfnChangeDisplaySettingsEx %x: %dx%dx%d at %d,%d\n",
977 pIf->modeData.wddm.pfnChangeDisplaySettingsEx,
978 paDeviceModes[i].dmPelsWidth,
979 paDeviceModes[i].dmPelsHeight,
980 paDeviceModes[i].dmBitsPerPel,
981 paDeviceModes[i].dmPosition.x,
982 paDeviceModes[i].dmPosition.y));
983
984 /* the miniport might have been adjusted the display mode stuff,
985 * adjust the paDeviceModes[i] by picking the closest available one */
986// DEVMODE AdjustedMode = paDeviceModes[i];
987// vboxDispIfAdjustMode(&paDisplayDevices[i], &AdjustedMode);
988
989 LONG tmpStatus = pIf->modeData.wddm.pfnChangeDisplaySettingsEx((LPSTR)paDisplayDevices[i].DeviceName,
990 &paDeviceModes[i], NULL, CDS_NORESET | CDS_UPDATEREGISTRY, NULL);
991 Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettingsEx position status %d, err %d\n", tmpStatus, GetLastError ()));
992 if (tmpStatus != DISP_CHANGE_SUCCESSFUL)
993 {
994 status = tmpStatus;
995 }
996 }
997
998 /* A second call to ChangeDisplaySettings updates the monitor. */
999 LONG tmpStatus = pIf->modeData.wddm.pfnChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
1000 Log(("VBoxTray: ResizeDisplayDevice: ChangeDisplaySettings update status %d\n", status));
1001 if (tmpStatus == DISP_CHANGE_SUCCESSFUL)
1002 {
1003 if (status == DISP_CHANGE_SUCCESSFUL)
1004 return NO_ERROR;
1005 }
1006 else
1007 {
1008 if (status == DISP_CHANGE_SUCCESSFUL)
1009 status = tmpStatus;
1010 }
1011
1012 return status == DISP_CHANGE_FAILED ? ERROR_RETRY : ERROR_GEN_FAILURE;
1013}
1014
1015static DWORD vboxDispIfWddmInit(PCVBOXDISPIF pIf)
1016{
1017 memset(&g_VBoxDispIfWddm, 0, sizeof (g_VBoxDispIfWddm));
1018 g_VBoxDispIfWddm.pIf = pIf;
1019 HRESULT hr = VBoxRrInit();
1020 if (SUCCEEDED(hr))
1021 {
1022 return ERROR_SUCCESS;
1023 }
1024 return ERROR_GEN_FAILURE;
1025}
1026
1027static void vboxDispIfWddmTerm(PCVBOXDISPIF pIf)
1028{
1029 VBoxRrTerm();
1030 memset(&g_VBoxDispIfWddm, 0, sizeof (g_VBoxDispIfWddm));
1031}
1032
1033typedef struct VBOXDISPIF_REINITMODES_OP
1034{
1035 VBOXDISPIFESCAPE_REINITVIDEOMODESBYMASK EscData;
1036} VBOXDISPIF_REINITMODES_OP, *PVBOXDISPIF_REINITMODES_OP;
1037
1038static DECLCALLBACK(BOOLEAN) vboxDispIfReninitModesWDDMOp(PCVBOXDISPIF pIf, D3DKMT_HANDLE hAdapter, DISPLAY_DEVICE *pDev, PVOID pContext)
1039{
1040 PVBOXDISPIF_REINITMODES_OP pData = (PVBOXDISPIF_REINITMODES_OP)pContext;
1041 VBOXDISPIFWDDM_ESCAPEOP_CONTEXT Ctx = {0};
1042 Ctx.pEscape = &pData->EscData.EscapeHdr;
1043 Ctx.cbData = sizeof (pData->EscData) - sizeof (pData->EscData.EscapeHdr);
1044// Ctx.EscapeFlags.HardwareAccess = 0;
1045 DWORD err = vboxDispIfWDDMAdapterOp(pIf, -1 /* iDisplay, -1 means primary */, vboxDispIfEscapeWDDMOp, &Ctx);
1046 if (err == NO_ERROR)
1047 {
1048 if (!Ctx.Status)
1049 err = NO_ERROR;
1050 else
1051 {
1052 if (Ctx.Status == 0xC00000BBL) /* not supported */
1053 err = ERROR_NOT_SUPPORTED;
1054 else
1055 err = ERROR_GEN_FAILURE;
1056 Log((__FUNCTION__": pfnD3DKMTEscape failed, Status (0x%x)\n", Ctx.Status));
1057 }
1058 }
1059 else
1060 Log((__FUNCTION__": vboxDispIfWDDMAdapterOp failed, err (%d)\n", err));
1061
1062 D3DKMT_POLLDISPLAYCHILDREN PollData = {0};
1063 PollData.hAdapter = hAdapter;
1064 PollData.NonDestructiveOnly = 1;
1065 NTSTATUS Status = pIf->modeData.wddm.pfnD3DKMTPollDisplayChildren(&PollData);
1066 if (Status != 0)
1067 {
1068 Log((__FUNCTION__": pfnD3DKMTPollDisplayChildren failed, Status (0x%x)\n", Status));
1069 }
1070 return TRUE;
1071}
1072
1073static DWORD vboxDispIfReninitModesWDDM(PCVBOXDISPIF const pIf, uint8_t *pScreenIdMask, BOOL fReconnectDisplaysOnChange)
1074{
1075 VBOXDISPIF_REINITMODES_OP OpData = {0};
1076 OpData.EscData.EscapeHdr.escapeCode = VBOXESC_REINITVIDEOMODESBYMASK;
1077 if (fReconnectDisplaysOnChange)
1078 OpData.EscData.EscapeHdr.u32CmdSpecific = VBOXWDDM_REINITVIDEOMODESBYMASK_F_RECONNECT_DISPLAYS_ON_CHANGE;
1079
1080 memcpy(OpData.EscData.ScreenMask, pScreenIdMask, sizeof (OpData.EscData.ScreenMask));
1081
1082 DWORD err = vboxDispIfWDDMAdapterOp(pIf, -1 /* iDisplay, -1 means primary */, vboxDispIfReninitModesWDDMOp, &OpData);
1083 return err;
1084}
1085
1086DWORD vboxDispIfCancelPendingResizeWDDM(PCVBOXDISPIF const pIf)
1087{
1088 VBoxRrRetryStop();
1089 return NO_ERROR;
1090}
1091
1092static DWORD vboxDispIfAdjustMode(DISPLAY_DEVICE *pDisplayDevice, DEVMODE *pDeviceMode)
1093{
1094 DEVMODE CurMode;
1095 DEVMODE BestMatchMode;
1096 DWORD i = 0;
1097 int64_t diffWH = INT64_MAX;
1098 int diffBpp = INT32_MAX;
1099 for (; ; ++i)
1100 {
1101 CurMode.dmSize = sizeof (CurMode);
1102 CurMode.dmDriverExtra = 0;
1103
1104 if (!EnumDisplaySettings(pDisplayDevice->DeviceName, i, &CurMode))
1105 break;
1106
1107 if (CurMode.dmPelsWidth == pDeviceMode->dmPelsWidth
1108 && CurMode.dmPelsHeight == pDeviceMode->dmPelsHeight
1109 && CurMode.dmBitsPerPel == pDeviceMode->dmBitsPerPel)
1110 {
1111 Log(("Exact match found"));
1112 *pDeviceMode = CurMode;
1113 return NO_ERROR;
1114 }
1115
1116 int diffCurrW = RT_ABS((int)(CurMode.dmPelsWidth - pDeviceMode->dmPelsWidth));
1117 int diffCurrH = RT_ABS((int)(CurMode.dmPelsHeight - pDeviceMode->dmPelsHeight));
1118 int diffCurrBpp = RT_ABS((int)(CurMode.dmBitsPerPel - pDeviceMode->dmBitsPerPel)
1119 - 1 /* <- to make higher bpp take precedence over lower ones */
1120 );
1121
1122 int64_t diffCurrHW = (int64_t)diffCurrW*diffCurrW + (int64_t)diffCurrH*diffCurrH;
1123
1124 if (i == 0
1125 || diffCurrHW < diffWH
1126 || (diffCurrHW == diffWH && diffCurrBpp < diffBpp))
1127 {
1128 /* first run */
1129 BestMatchMode = CurMode;
1130 diffWH = diffCurrHW;
1131 diffBpp = diffCurrBpp;
1132 continue;
1133 }
1134 }
1135
1136 if (i == 0)
1137 {
1138 Log(("No modes found!"));
1139 return NO_ERROR;
1140 }
1141
1142 *pDeviceMode = BestMatchMode;
1143 return NO_ERROR;
1144}
1145
1146static DWORD vboxDispIfAdjustModeValues(PCVBOXDISPIF const pIf, DISPLAY_DEVICE *pDisplayDevice, DEVMODE *pDeviceMode)
1147{
1148 VBOXDISPIFESCAPE_ADJUSTVIDEOMODES Data = {0};
1149 Data.EscapeHdr.escapeCode = VBOXESC_REINITVIDEOMODESBYMASK;
1150 Data.EscapeHdr.u32CmdSpecific = 1;
1151 Data.aScreenInfos[0].Mode.Id =
1152 Data.aScreenInfos[0].Mode.Width = pDeviceMode->dmPelsWidth;
1153 Data.aScreenInfos[0].Mode.Height = pDeviceMode->dmPelsHeight;
1154 Data.aScreenInfos[0].Mode.BitsPerPixel = pDeviceMode->dmBitsPerPel;
1155 DWORD err = vboxDispIfEscapeWDDM(pIf, &Data.EscapeHdr, sizeof (Data) - sizeof (Data.EscapeHdr), TRUE);
1156 if (err != NO_ERROR)
1157 {
1158 Log((__FUNCTION__": VBoxDispIfEscape failed with err (%d)\n", err));
1159 }
1160 return err;
1161}
1162
1163DWORD vboxDispIfResizeModesWDDM(PCVBOXDISPIF const pIf, UINT iChangedMode, DISPLAY_DEVICE *paDisplayDevices, DEVMODE *paDeviceModes, UINT cDevModes)
1164{
1165 UINT cbVidPnInfo = VBOXWDDM_RECOMMENDVIDPN_SIZE(cDevModes);
1166 PVBOXWDDM_RECOMMENDVIDPN pVidPnInfo = (PVBOXWDDM_RECOMMENDVIDPN)alloca(cbVidPnInfo);
1167 pVidPnInfo->cScreenInfos = cDevModes;
1168 D3DKMT_HANDLE hAdapter = NULL;
1169 NTSTATUS Status;
1170 DWORD winEr = NO_ERROR;
1171 UINT i = 0;
1172
1173 VBoxRrRetryStop();
1174
1175 for (; i < cDevModes; i++)
1176 {
1177 PVBOXWDDM_RECOMMENDVIDPN_SCREEN_INFO pInfo = &pVidPnInfo->aScreenInfos[i];
1178 D3DKMT_OPENADAPTERFROMHDC OpenAdapterData = {0};
1179 OpenAdapterData.hDc = CreateDC(NULL, paDisplayDevices[i].DeviceName, NULL, NULL);
1180 if (!OpenAdapterData.hDc)
1181 {
1182 winEr = GetLastError();
1183 Log(("WARNING: Failed to get dc for display device %s, winEr %d\n", paDisplayDevices[i].DeviceName, winEr));
1184 break;
1185 }
1186
1187 Status = pIf->modeData.wddm.pfnD3DKMTOpenAdapterFromHdc(&OpenAdapterData);
1188 Assert(!Status);
1189 if (Status)
1190 {
1191 winEr = ERROR_GEN_FAILURE;
1192 Log(("WARNING: Failed to open adapter from dc, Status 0x%x\n", Status));
1193 break;
1194 }
1195
1196 pInfo->Id = OpenAdapterData.VidPnSourceId;
1197 pInfo->Width = paDeviceModes[i].dmPelsWidth;
1198 pInfo->Height = paDeviceModes[i].dmPelsHeight;
1199 pInfo->BitsPerPixel = paDeviceModes[i].dmBitsPerPel;
1200
1201 if (!hAdapter)
1202 {
1203 hAdapter = OpenAdapterData.hAdapter;
1204 }
1205 else
1206 {
1207 D3DKMT_CLOSEADAPTER ClosaAdapterData = {0};
1208 ClosaAdapterData.hAdapter = OpenAdapterData.hAdapter;
1209 Status = pIf->modeData.wddm.pfnD3DKMTCloseAdapter(&ClosaAdapterData);
1210 if (Status)
1211 Log(("WARNING: Failed to close adapter, Status 0x%x\n", Status));
1212 }
1213 }
1214
1215 BOOL fAbleToInvalidateVidPn = FALSE;
1216
1217 if (winEr == NO_ERROR)
1218 {
1219 Assert(hAdapter);
1220
1221 D3DKMT_INVALIDATEACTIVEVIDPN IAVidPnData = {0};
1222 IAVidPnData.hAdapter = hAdapter;
1223 IAVidPnData.pPrivateDriverData = pVidPnInfo;
1224 IAVidPnData.PrivateDriverDataSize = cbVidPnInfo;
1225
1226 DWORD winEr = NO_ERROR;
1227 Status = pIf->modeData.wddm.pfnD3DKMTInvalidateActiveVidPn(&IAVidPnData);
1228 Assert(!Status);
1229 if (Status)
1230 {
1231 Log((__FUNCTION__": pfnD3DKMTInvalidateActiveVidPn failed, Status (0x%x)\n", Status));
1232 winEr = ERROR_GEN_FAILURE;
1233 }
1234 else
1235 {
1236 fAbleToInvalidateVidPn = TRUE;
1237 }
1238 }
1239
1240 if (hAdapter)
1241 {
1242 D3DKMT_CLOSEADAPTER ClosaAdapterData = {0};
1243 ClosaAdapterData.hAdapter = hAdapter;
1244 Status = pIf->modeData.wddm.pfnD3DKMTCloseAdapter(&ClosaAdapterData);
1245 if (Status)
1246 Log(("WARNING: Failed to close adapter[2], Status 0x%x\n", Status));
1247 }
1248
1249// for (i = 0; i < cDevModes; i++)
1250// {
1251// vboxDispIfAdjustMode(&paDisplayDevices[i], &paDeviceModes[i]);
1252// }
1253
1254 if (fAbleToInvalidateVidPn)
1255 {
1256 Log(("Invalidating VidPn Worked!\n"));
1257 winEr = vboxDispIfWddmValidateFixResize(pIf, paDisplayDevices, paDeviceModes, cDevModes);
1258 }
1259 else
1260 {
1261 Log(("Falling back to monitor mode reinit\n"));
1262 /* fallback impl needed for display-only driver
1263 * since D3DKMTInvalidateActiveVidPn is not available for WDDM > 1.0:
1264 * make the driver invalidate VidPn,
1265 * which is done by emulating a monitor re-plug currently */
1266 uint8_t ScreenMask[VBOXWDDM_SCREENMASK_SIZE] = {0};
1267 ASMBitSet(ScreenMask, iChangedMode);
1268 vboxDispIfReninitModesWDDM(pIf, ScreenMask, TRUE);
1269
1270 winEr = vboxDispIfWddmValidateFixResize(pIf, paDisplayDevices, paDeviceModes, cDevModes);
1271
1272 Assert(winEr == NO_ERROR);
1273 }
1274
1275 if (winEr == ERROR_RETRY)
1276 {
1277 VBoxRrRetrySchedule(pIf, paDisplayDevices, paDeviceModes, cDevModes);
1278 /* just pretend everything is fine so far */
1279 winEr = NO_ERROR;
1280 }
1281
1282 return winEr;
1283}
1284#endif /* VBOX_WITH_WDDM */
1285
1286DWORD VBoxDispIfResizeModes(PCVBOXDISPIF const pIf, UINT iChangedMode, DISPLAY_DEVICE *paDisplayDevices, DEVMODE *paDeviceModes, UINT cDevModes)
1287{
1288 switch (pIf->enmMode)
1289 {
1290 case VBOXDISPIF_MODE_XPDM_NT4:
1291 return ERROR_NOT_SUPPORTED;
1292 case VBOXDISPIF_MODE_XPDM:
1293 return ERROR_NOT_SUPPORTED;
1294#ifdef VBOX_WITH_WDDM
1295 case VBOXDISPIF_MODE_WDDM:
1296 return vboxDispIfResizeModesWDDM(pIf, iChangedMode, paDisplayDevices, paDeviceModes, cDevModes);
1297#endif
1298 default:
1299 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
1300 return ERROR_INVALID_PARAMETER;
1301 }
1302}
1303
1304DWORD VBoxDispIfCancelPendingResize(PCVBOXDISPIF const pIf)
1305{
1306 switch (pIf->enmMode)
1307 {
1308 case VBOXDISPIF_MODE_XPDM_NT4:
1309 return NO_ERROR;
1310 case VBOXDISPIF_MODE_XPDM:
1311 return NO_ERROR;
1312#ifdef VBOX_WITH_WDDM
1313 case VBOXDISPIF_MODE_WDDM:
1314 return vboxDispIfCancelPendingResizeWDDM(pIf);
1315#endif
1316 default:
1317 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
1318 return ERROR_INVALID_PARAMETER;
1319 }
1320}
1321
1322DWORD VBoxDispIfReninitModes(PCVBOXDISPIF const pIf, uint8_t *pScreenIdMask, BOOL fReconnectDisplaysOnChange)
1323{
1324 switch (pIf->enmMode)
1325 {
1326 case VBOXDISPIF_MODE_XPDM_NT4:
1327 return ERROR_NOT_SUPPORTED;
1328 case VBOXDISPIF_MODE_XPDM:
1329 return ERROR_NOT_SUPPORTED;
1330#ifdef VBOX_WITH_WDDM
1331 case VBOXDISPIF_MODE_WDDM:
1332 return vboxDispIfReninitModesWDDM(pIf, pScreenIdMask, fReconnectDisplaysOnChange);
1333#endif
1334 default:
1335 Log((__FUNCTION__": unknown mode (%d)\n", pIf->enmMode));
1336 return ERROR_INVALID_PARAMETER;
1337 }
1338}
1339
1340static DWORD vboxDispIfSwitchToXPDM_NT4(PVBOXDISPIF pIf)
1341{
1342 return NO_ERROR;
1343}
1344
1345static DWORD vboxDispIfSwitchToXPDM(PVBOXDISPIF pIf)
1346{
1347 DWORD err = NO_ERROR;
1348 AssertBreakpoint();
1349 OSVERSIONINFO OSinfo;
1350 OSinfo.dwOSVersionInfoSize = sizeof (OSinfo);
1351 GetVersionEx (&OSinfo);
1352 if (OSinfo.dwMajorVersion >= 5)
1353 {
1354 HMODULE hUser = GetModuleHandle("USER32");
1355 if (NULL != hUser)
1356 {
1357 bool bSupported = true;
1358 *(uintptr_t *)&pIf->modeData.xpdm.pfnChangeDisplaySettingsEx = (uintptr_t)GetProcAddress(hUser, "ChangeDisplaySettingsExA");
1359 Log((__FUNCTION__": pfnChangeDisplaySettingsEx = %p\n", pIf->modeData.xpdm.pfnChangeDisplaySettingsEx));
1360 bSupported &= !!(pIf->modeData.xpdm.pfnChangeDisplaySettingsEx);
1361
1362 if (!bSupported)
1363 {
1364 Log((__FUNCTION__": pfnChangeDisplaySettingsEx function pointer failed to initialize\n"));
1365 err = ERROR_NOT_SUPPORTED;
1366 }
1367 }
1368 else
1369 {
1370 Log((__FUNCTION__": failed to get USER32 handle, err (%d)\n", GetLastError()));
1371 err = ERROR_NOT_SUPPORTED;
1372 }
1373 }
1374 else
1375 {
1376 Log((__FUNCTION__": can not switch to VBOXDISPIF_MODE_XPDM, because os is not >= w2k\n"));
1377 err = ERROR_NOT_SUPPORTED;
1378 }
1379
1380 return err;
1381}
1382
1383DWORD VBoxDispIfSwitchMode(PVBOXDISPIF pIf, VBOXDISPIF_MODE enmMode, VBOXDISPIF_MODE *penmOldMode)
1384{
1385 /* @todo: may need to addd synchronization in case we want to change modes dynamically
1386 * i.e. currently the mode is supposed to be initialized once on service initialization */
1387 if (penmOldMode)
1388 *penmOldMode = pIf->enmMode;
1389
1390 if (enmMode == pIf->enmMode)
1391 return NO_ERROR;
1392
1393#ifdef VBOX_WITH_WDDM
1394 if (pIf->enmMode == VBOXDISPIF_MODE_WDDM)
1395 {
1396 vboxDispIfWddmTerm(pIf);
1397 }
1398#endif
1399
1400 DWORD err = NO_ERROR;
1401 switch (enmMode)
1402 {
1403 case VBOXDISPIF_MODE_XPDM_NT4:
1404 Log((__FUNCTION__": request to switch to VBOXDISPIF_MODE_XPDM_NT4\n"));
1405 err = vboxDispIfSwitchToXPDM_NT4(pIf);
1406 if (err == NO_ERROR)
1407 {
1408 Log((__FUNCTION__": successfully switched to XPDM_NT4 mode\n"));
1409 pIf->enmMode = VBOXDISPIF_MODE_XPDM_NT4;
1410 }
1411 else
1412 Log((__FUNCTION__": failed to switch to XPDM_NT4 mode, err (%d)\n", err));
1413 break;
1414 case VBOXDISPIF_MODE_XPDM:
1415 Log((__FUNCTION__": request to switch to VBOXDISPIF_MODE_XPDM\n"));
1416 err = vboxDispIfSwitchToXPDM(pIf);
1417 if (err == NO_ERROR)
1418 {
1419 Log((__FUNCTION__": successfully switched to XPDM mode\n"));
1420 pIf->enmMode = VBOXDISPIF_MODE_XPDM;
1421 }
1422 else
1423 Log((__FUNCTION__": failed to switch to XPDM mode, err (%d)\n", err));
1424 break;
1425#ifdef VBOX_WITH_WDDM
1426 case VBOXDISPIF_MODE_WDDM:
1427 {
1428 Log((__FUNCTION__": request to switch to VBOXDISPIF_MODE_WDDM\n"));
1429 err = vboxDispIfSwitchToWDDM(pIf);
1430 if (err == NO_ERROR)
1431 {
1432 Log((__FUNCTION__": successfully switched to WDDM mode\n"));
1433 pIf->enmMode = VBOXDISPIF_MODE_WDDM;
1434 }
1435 else
1436 Log((__FUNCTION__": failed to switch to WDDM mode, err (%d)\n", err));
1437 break;
1438 }
1439#endif
1440 default:
1441 err = ERROR_INVALID_PARAMETER;
1442 break;
1443 }
1444 return err;
1445}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette