VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/nt/ntDisplay.cpp@ 107381

Last change on this file since 107381 was 107025, checked in by vboxsync, 3 months ago

ValKit: A tool to keep all guest displays filled with a uniform color, simplify to have all displays filled with blue. bugref:10714

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: ntDisplay.cpp 107025 2024-11-18 13:16:33Z vboxsync $ */
2/** @file
3 * Test cases for Display device and DirectX 3D rendering - NT.
4 */
5
6/*
7 * Copyright (C) 2007-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41
42#include <d3d11.h>
43
44#include <iprt/win/setupapi.h>
45#include <devguid.h>
46
47#include <iprt/initterm.h>
48#include <iprt/getopt.h>
49#include <iprt/message.h>
50#include <iprt/stream.h>
51#include <iprt/string.h>
52#include <iprt/thread.h>
53#include <iprt/errcore.h>
54
55
56/*********************************************************************************************************************************
57* Global Variables *
58*********************************************************************************************************************************/
59/** How chatty we should be. */
60static uint32_t g_cVerbosity = 0;
61static HINSTANCE g_hInstance;
62
63NTSTATUS SetDisplayDeviceState(bool bEnable)
64{
65 HDEVINFO hDevs = NULL;
66 SP_DEVINFO_DATA DevInfo;
67 NTSTATUS rcNt = (NTSTATUS)0;
68
69 hDevs = SetupDiGetClassDevs(&GUID_DEVCLASS_DISPLAY, NULL, NULL, DIGCF_PRESENT | DIGCF_PROFILE);
70
71 if (hDevs == INVALID_HANDLE_VALUE)
72 {
73 rcNt = GetLastError();
74 RTMsgError("SetupDiGetClassDevs failed: %#x\n", rcNt);
75 return rcNt;
76 }
77
78 RT_ZERO(DevInfo);
79 DevInfo.cbSize = sizeof(SP_DEVINFO_DATA);
80
81 if (SetupDiEnumDeviceInfo(hDevs, 0, &DevInfo))
82 {
83 SP_PROPCHANGE_PARAMS PropChangeParams;
84
85 RT_ZERO(PropChangeParams);
86 PropChangeParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
87 PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
88 PropChangeParams.StateChange = bEnable ? DICS_ENABLE : DICS_DISABLE;
89 PropChangeParams.Scope = DICS_FLAG_CONFIGSPECIFIC;
90 PropChangeParams.HwProfile = 0;
91
92 if (SetupDiSetClassInstallParams(hDevs, &DevInfo, &PropChangeParams.ClassInstallHeader, sizeof(PropChangeParams)))
93 {
94 if (SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevs, &DevInfo))
95 {
96 if (g_cVerbosity >= 1)
97 RTMsgInfo("debug: device %s\n", bEnable ? "enabled" : "disabled");
98 }
99 else
100 {
101 rcNt = GetLastError();
102 RTMsgError("SetupDiCallClassInstaller failed: %#x\n", rcNt);
103 }
104 }
105 else
106 {
107 rcNt = GetLastError();
108 RTMsgError("SetupDiSetClassInstallParams failed: %#x\n", rcNt);
109 }
110 }
111
112 SetupDiDestroyDeviceInfoList(hDevs);
113
114 return rcNt;
115}
116
117bool CheckDXFeatureLevel()
118{
119 IDXGIAdapter *pAdapter = NULL; /* Default adapter. */
120 static const D3D_FEATURE_LEVEL aFeatureLevels[] =
121 {
122 D3D_FEATURE_LEVEL_11_1,
123 D3D_FEATURE_LEVEL_11_0,
124 D3D_FEATURE_LEVEL_10_1,
125 D3D_FEATURE_LEVEL_10_0,
126 D3D_FEATURE_LEVEL_9_3,
127 D3D_FEATURE_LEVEL_9_2,
128 D3D_FEATURE_LEVEL_9_1
129 };
130 UINT Flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
131 D3D_FEATURE_LEVEL FeatureLevel = D3D_FEATURE_LEVEL_9_1;
132 ID3D11Device *pDevice = NULL;
133 ID3D11DeviceContext *pImmediateContext = NULL;
134 bool fResult = false;
135 HRESULT hr = S_OK;
136
137 hr = D3D11CreateDevice(pAdapter,
138 D3D_DRIVER_TYPE_HARDWARE,
139 NULL,
140 Flags,
141 aFeatureLevels,
142 RT_ELEMENTS(aFeatureLevels),
143 D3D11_SDK_VERSION,
144 &pDevice,
145 &FeatureLevel,
146 &pImmediateContext);
147
148 if (FAILED(hr))
149 {
150 RTMsgError("D3D11CreateDevice failed with 0x%X\n", hr);
151 return false;
152 }
153
154 if (FeatureLevel == D3D_FEATURE_LEVEL_11_1)
155 {
156 RTMsgInfo("D3D_FEATURE_LEVEL_11_1 is supported\n");
157 fResult = true;
158 }
159 else
160 {
161 RTMsgError("D3D_FEATURE_LEVEL_11_1 is not supported, only 0x%X\n", FeatureLevel);
162 }
163
164 pDevice->Release();
165 pImmediateContext->Release();
166
167 return fResult;
168}
169
170#define WM_PRIV_RECREATE_ALL_WINDOWS WM_USER
171
172#define DISPLAYS_NUM_MAX 64
173static int g_cHWnd = 0;
174static HWND g_aHWnd[DISPLAYS_NUM_MAX];
175static HWND g_hWndPrimary = NULL;
176static HBRUSH g_ahBrush[3];
177static WNDCLASSEX g_WindowClass;
178
179LRESULT CALLBACK WindowProcGDIFullScreen(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
180{
181 switch(message)
182 {
183 case WM_DISPLAYCHANGE:
184 RTPrintf("WM_DISPLAYCHANGE: bpp %d, w %d, h %d\n", wParam, LOWORD(lParam), HIWORD(lParam));
185 if (g_hWndPrimary == hWnd)
186 PostMessage(g_hWndPrimary, WM_PRIV_RECREATE_ALL_WINDOWS, 0, 0);
187 break;
188
189 case WM_PAINT:
190 {
191 PAINTSTRUCT ps;
192 HDC hdc;
193
194 hdc = BeginPaint(hWnd, &ps);
195
196 if (RT_LIKELY(hdc))
197 {
198 RECT r;
199
200 SelectObject(hdc, g_ahBrush[0]);
201 r = ps.rcPaint;
202 PatBlt(hdc, r.left, r.top, r.right - r.left, r.bottom - r.top, PATCOPY);
203
204 EndPaint(hWnd, &ps);
205 }
206 }
207 break;
208
209 case WM_KEYDOWN:
210 if (wParam == VK_ESCAPE)
211 PostQuitMessage(0);
212 break;
213 }
214
215 return DefWindowProc(hWnd, message, wParam, lParam);
216}
217
218
219void CreateWindowForEachDisplay()
220{
221 DISPLAY_DEVICE dev;
222 HWND hWndPrimaryNew = NULL;
223 int k;
224
225 RT_ZERO(g_aHWnd);
226 g_cHWnd = 0;
227
228 dev.cb = sizeof(dev);
229 for(k = 0; k < DISPLAYS_NUM_MAX && EnumDisplayDevices(NULL, k, &dev, 0); k++, g_cHWnd++)
230 {
231 RTPrintf("%d: %s 0x%x %s%s\n", k + 1, dev.DeviceName, dev.StateFlags,
232 (dev.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) ? "PRIMARY" : "",
233 (dev.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) ? " ATTACHED" : "");
234
235 DEVMODE mode = {};
236 if (EnumDisplaySettings(dev.DeviceName, ENUM_CURRENT_SETTINGS, &mode))
237 {
238 int x, y, w, h;
239 HWND hWnd;
240
241 x = mode.dmPosition.x;
242 y = mode.dmPosition.y;
243 w = mode.dmPelsWidth;
244 h = mode.dmPelsHeight;
245 RTPrintf("%s: (%d,%d)-(%dx%d)\n", mode.dmDeviceName, x, y, w, h);
246
247 hWnd = CreateWindowEx(WS_EX_TOPMOST,
248 TEXT("WindowClassGDIFullScreen"),
249 TEXT("Fullscreen GDI test"),
250 WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
251 x, y, w, h,
252 NULL,
253 NULL,
254 g_hInstance,
255 NULL);
256
257 g_aHWnd[k] = hWnd;
258
259 RTPrintf("DISPLAY%d window 0x%x\n", k + 1, hWnd);
260
261 if (dev.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
262 {
263 hWndPrimaryNew = hWnd;
264 }
265 }
266 else
267 {
268 g_aHWnd[k] = NULL;
269 RTPrintf("DISPLAY%d is disabled, skipped\n", k + 1);
270 }
271 }
272
273 g_hWndPrimary = hWndPrimaryNew;
274
275 for (k = 0; k < g_cHWnd; k++)
276 {
277 if (g_aHWnd[k])
278 {
279 RTPrintf("DISPLAY%d Show & Update for Window 0x%x\n", k, g_aHWnd[k]);
280 ShowWindow(g_aHWnd[k], TRUE);
281 UpdateWindow(g_aHWnd[k]);
282 }
283 }
284}
285
286bool ShowFullScreenWindows()
287{
288 g_WindowClass.cbSize = sizeof(WNDCLASSEX);
289 g_WindowClass.style = CS_HREDRAW | CS_VREDRAW;
290 g_WindowClass.lpfnWndProc = WindowProcGDIFullScreen;
291 g_WindowClass.hInstance = g_hInstance;
292 g_WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
293 g_WindowClass.hbrBackground = NULL;
294 g_WindowClass.lpszClassName = TEXT("WindowClassGDIFullScreen");
295
296 RegisterClassEx(&g_WindowClass);
297
298 g_ahBrush[0] = CreateSolidBrush(RGB(0, 0, 255));
299 g_ahBrush[1] = CreateSolidBrush(RGB(0, 255, 0));
300 g_ahBrush[2] = CreateSolidBrush(RGB(255, 0, 0));
301
302 PostMessage(NULL, WM_PRIV_RECREATE_ALL_WINDOWS, 0, 0);
303
304 while(TRUE)
305 {
306 MSG msg;
307
308 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
309 {
310 if (msg.message == WM_PRIV_RECREATE_ALL_WINDOWS)
311 {
312 g_hWndPrimary = NULL;
313
314 for(int k = 0; k < g_cHWnd; k++)
315 {
316 if (RT_LIKELY(g_aHWnd[k]))
317 {
318 DestroyWindow(g_aHWnd[k]);
319 g_aHWnd[k] = NULL;
320 }
321 }
322
323 CreateWindowForEachDisplay();
324 continue;
325 }
326
327 if (msg.message == WM_QUIT)
328 break;
329
330 TranslateMessage(&msg);
331 DispatchMessage(&msg);
332 }
333// RenderFrame();
334 }
335
336 return true;
337}
338
339int main(int argc, char **argv)
340{
341 /*
342 * Init IPRT.
343 */
344 int rc = RTR3InitExe(argc, &argv, 0);
345 if (RT_FAILURE(rc))
346 return RTMsgInitFailure(rc);
347
348 g_hInstance = GetModuleHandle(NULL);
349
350 /*
351 * Parse arguments.
352 */
353 static const RTGETOPTDEF s_aOptions[] =
354 {
355 { "--enable", 'e', RTGETOPT_REQ_UINT32 },
356 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
357 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
358 };
359
360 RTGETOPTSTATE State;
361 RTGetOptInit(&State, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0);
362 RTGETOPTUNION ValueUnion;
363 int chOpt;
364 while ((chOpt = RTGetOpt(&State, &ValueUnion)) != 0)
365 {
366 switch (chOpt)
367 {
368 case 'e': SetDisplayDeviceState(RT_BOOL(ValueUnion.u32)); break;
369 case 'q': g_cVerbosity = 0; break;
370 case 'v': g_cVerbosity += 1; break;
371 case 'h':
372 RTPrintf("usage: ntDisplay.exe [-e|--enable <0 or 1>]\n");
373 return 0;
374
375 default:
376 return RTGetOptPrintError(chOpt, &ValueUnion);
377 }
378 }
379
380 ShowFullScreenWindows();
381
382 return !CheckDXFeatureLevel();
383}
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