VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPVidModes.cpp@ 46851

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

ga/win/graphics: fix win version support

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 48.4 KB
Line 
1/* $Id: VBoxMPVidModes.cpp 46851 2013-06-27 16:55:20Z vboxsync $ */
2
3/** @file
4 * VBox Miniport video modes related functions
5 */
6
7/*
8 * Copyright (C) 2011-2013 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include "VBoxMPCommon.h"
20
21#if _MSC_VER >= 1400 /* bird: MS fixed swprintf to be standard-conforming... */
22#define _INC_SWPRINTF_INL_
23extern "C" int __cdecl swprintf(wchar_t *, const wchar_t *, ...);
24#endif
25#include <wchar.h>
26#include <VBox/Hardware/VBoxVideoVBE.h>
27
28#ifdef VBOX_WITH_WDDM
29# define VBOX_WITHOUT_24BPP_MODES
30#endif
31
32/* Custom video modes which are being read from registry at driver startup. */
33static VIDEO_MODE_INFORMATION g_CustomVideoModes[VBOX_VIDEO_MAX_SCREENS] = { 0 };
34
35#ifdef VBOX_XPDM_MINIPORT
36/* Standart video modes list.
37 * Additional space is reserved for custom video modes for VBOX_VIDEO_MAX_SCREENS guest monitors.
38 * The custom video mode index is alternating for each mode set and 2 indexes are needed for each custom mode.
39 */
40static VIDEO_MODE_INFORMATION g_VideoModes[VBOXMP_MAX_VIDEO_MODES + VBOX_VIDEO_MAX_SCREENS * 2] = { 0 };
41
42/* Number of available video modes, set by VBoxMPCmnBuildVideoModesTable. */
43static uint32_t g_NumVideoModes = 0;
44#endif
45
46static BOOLEAN
47VBoxMPValidateVideoModeParamsGuest(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, uint32_t xres, uint32_t yres, uint32_t bpp)
48{
49 switch (bpp)
50 {
51 case 32:
52 break;
53 case 24:
54#ifdef VBOX_WITHOUT_24BPP_MODES
55 return FALSE;
56#else
57 break;
58#endif
59 case 16:
60 break;
61 case 8:
62#ifndef VBOX_WITH_8BPP_MODES
63 return FALSE;
64#else
65 break;
66#endif
67 default:
68 WARN(("Unexpected bpp (%d)", bpp));
69 return FALSE;
70 }
71 return TRUE;
72}
73
74/* Fills given video mode BPP related fields */
75static void
76VBoxFillVidModeBPP(VIDEO_MODE_INFORMATION *pMode, ULONG bitsR, ULONG bitsG, ULONG bitsB,
77 ULONG maskR, ULONG maskG, ULONG maskB)
78{
79 pMode->NumberRedBits = bitsR;
80 pMode->NumberGreenBits = bitsG;
81 pMode->NumberBlueBits = bitsB;
82 pMode->RedMask = maskR;
83 pMode->GreenMask = maskG;
84 pMode->BlueMask = maskB;
85}
86
87/* Fills given video mode structure */
88static void
89VBoxFillVidModeInfo(VIDEO_MODE_INFORMATION *pMode, ULONG xres, ULONG yres, ULONG bpp, ULONG index, ULONG yoffset)
90{
91 LOGF(("%dx%d:%d (idx=%d, yoffset=%d)", xres, yres, bpp, index, yoffset));
92
93 memset(pMode, 0, sizeof(VIDEO_MODE_INFORMATION));
94
95 /*Common entries*/
96 pMode->Length = sizeof(VIDEO_MODE_INFORMATION);
97 pMode->ModeIndex = index;
98 pMode->VisScreenWidth = xres;
99 pMode->VisScreenHeight = yres - yoffset;
100 pMode->ScreenStride = xres * ((bpp + 7) / 8);
101 pMode->NumberOfPlanes = 1;
102 pMode->BitsPerPlane = bpp;
103 pMode->Frequency = 60;
104 pMode->XMillimeter = 320;
105 pMode->YMillimeter = 240;
106 pMode->VideoMemoryBitmapWidth = xres;
107 pMode->VideoMemoryBitmapHeight = yres - yoffset;
108 pMode->DriverSpecificAttributeFlags = 0;
109 pMode->AttributeFlags = VIDEO_MODE_GRAPHICS | VIDEO_MODE_COLOR | VIDEO_MODE_NO_OFF_SCREEN;
110
111 /*BPP related entries*/
112 switch (bpp)
113 {
114#ifdef VBOX_WITH_8BPP_MODES
115 case 8:
116 VBoxFillVidModeBPP(pMode, 6, 6, 6, 0, 0, 0);
117
118 pMode->AttributeFlags |= VIDEO_MODE_PALETTE_DRIVEN | VIDEO_MODE_MANAGED_PALETTE;
119 break;
120#endif
121 case 16:
122 VBoxFillVidModeBPP(pMode, 5, 6, 5, 0xF800, 0x7E0, 0x1F);
123 break;
124 case 24:
125 case 32:
126 VBoxFillVidModeBPP(pMode, 8, 8, 8, 0xFF0000, 0xFF00, 0xFF);
127 break;
128 default:
129 Assert(0);
130 break;
131 }
132}
133
134void VBoxMPCmnInitCustomVideoModes(PVBOXMP_DEVEXT pExt)
135{
136 VBOXMPCMNREGISTRY Registry;
137 VP_STATUS rc;
138 int iMode;
139
140 LOGF_ENTER();
141
142 rc = VBoxMPCmnRegInit(pExt, &Registry);
143 VBOXMP_WARN_VPS(rc);
144
145 /* Initialize all custom modes to the 800x600x32 */
146 VBoxFillVidModeInfo(&g_CustomVideoModes[0], 800, 600, 32, 0, 0);
147 for (iMode=1; iMode<RT_ELEMENTS(g_CustomVideoModes); ++iMode)
148 {
149 g_CustomVideoModes[iMode] = g_CustomVideoModes[0];
150 }
151
152 /* Read stored custom resolution info from registry */
153 for (iMode=0; iMode<VBoxCommonFromDeviceExt(pExt)->cDisplays; ++iMode)
154 {
155 uint32_t CustomXRes = 0, CustomYRes = 0, CustomBPP = 0;
156
157 if (iMode==0)
158 {
159 /*First name without a suffix*/
160 rc = VBoxMPCmnRegQueryDword(Registry, L"CustomXRes", &CustomXRes);
161 VBOXMP_WARN_VPS_NOBP(rc);
162 rc = VBoxMPCmnRegQueryDword(Registry, L"CustomYRes", &CustomYRes);
163 VBOXMP_WARN_VPS_NOBP(rc);
164 rc = VBoxMPCmnRegQueryDword(Registry, L"CustomBPP", &CustomBPP);
165 VBOXMP_WARN_VPS_NOBP(rc);
166 }
167 else
168 {
169 wchar_t keyname[32];
170 swprintf(keyname, L"CustomXRes%d", iMode);
171 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomXRes);
172 VBOXMP_WARN_VPS_NOBP(rc);
173 swprintf(keyname, L"CustomYRes%d", iMode);
174 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomYRes);
175 VBOXMP_WARN_VPS_NOBP(rc);
176 swprintf(keyname, L"CustomBPP%d", iMode);
177 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomBPP);
178 VBOXMP_WARN_VPS_NOBP(rc);
179 }
180
181 LOG(("got stored custom resolution[%d] %dx%dx%d", iMode, CustomXRes, CustomYRes, CustomBPP));
182
183 if (CustomXRes || CustomYRes || CustomBPP)
184 {
185 if (CustomXRes == 0)
186 {
187 CustomXRes = g_CustomVideoModes[iMode].VisScreenWidth;
188 }
189 if (CustomYRes == 0)
190 {
191 CustomYRes = g_CustomVideoModes[iMode].VisScreenHeight;
192 }
193 if (CustomBPP == 0)
194 {
195 CustomBPP = g_CustomVideoModes[iMode].BitsPerPlane;
196 }
197
198 if (VBoxMPValidateVideoModeParamsGuest(pExt, iMode, CustomXRes, CustomYRes, CustomBPP))
199 {
200 VBoxFillVidModeInfo(&g_CustomVideoModes[iMode], CustomXRes, CustomYRes, CustomBPP, 0, 0);
201 }
202 }
203 }
204
205 rc = VBoxMPCmnRegFini(Registry);
206 VBOXMP_WARN_VPS(rc);
207 LOGF_LEAVE();
208}
209
210VIDEO_MODE_INFORMATION *VBoxMPCmnGetCustomVideoModeInfo(ULONG ulIndex)
211{
212 return (ulIndex<RT_ELEMENTS(g_CustomVideoModes)) ? &g_CustomVideoModes[ulIndex] : NULL;
213}
214
215#ifdef VBOX_XPDM_MINIPORT
216VIDEO_MODE_INFORMATION* VBoxMPCmnGetVideoModeInfo(ULONG ulIndex)
217{
218 return (ulIndex<RT_ELEMENTS(g_VideoModes)) ? &g_VideoModes[ulIndex] : NULL;
219}
220#endif
221
222static bool VBoxMPVideoModesMatch(const PVIDEO_MODE_INFORMATION pMode1, const PVIDEO_MODE_INFORMATION pMode2)
223{
224 return pMode1->VisScreenHeight == pMode2->VisScreenHeight
225 && pMode1->VisScreenWidth == pMode2->VisScreenWidth
226 && pMode1->BitsPerPlane == pMode2->BitsPerPlane;
227}
228
229static int
230VBoxMPFindVideoMode(const PVIDEO_MODE_INFORMATION pModesTable, int cModes, const PVIDEO_MODE_INFORMATION pMode)
231{
232 for (int i = 0; i < cModes; ++i)
233 {
234 if (VBoxMPVideoModesMatch(pMode, &pModesTable[i]))
235 {
236 return i;
237 }
238 }
239 return -1;
240}
241
242/* Helper function to dynamically build our table of standard video
243 * modes. We take the amount of VRAM and create modes with standard
244 * geometries until we've either reached the maximum number of modes
245 * or the available VRAM does not allow for additional modes.
246 * We also check registry for manually added video modes.
247 * Returns number of modes added to the table.
248 */
249static uint32_t
250VBoxMPFillModesTable(PVBOXMP_DEVEXT pExt, int iDisplay, PVIDEO_MODE_INFORMATION pModesTable, size_t tableSize,
251 int32_t *pPrefModeIdx)
252{
253 /* the resolution matrix */
254 struct
255 {
256 uint16_t xRes;
257 uint16_t yRes;
258 } resolutionMatrix[] =
259 {
260 /* standard modes */
261 { 640, 480 },
262 { 800, 600 },
263 { 1024, 768 },
264 { 1152, 864 },
265 { 1280, 960 },
266 { 1280, 1024 },
267 { 1400, 1050 },
268 { 1600, 1200 },
269 { 1920, 1440 },
270#ifndef VBOX_WITH_WDDM
271 /* multi screen modes with 1280x1024 */
272 { 2560, 1024 },
273 { 3840, 1024 },
274 { 5120, 1024 },
275 /* multi screen modes with 1600x1200 */
276 { 3200, 1200 },
277 { 4800, 1200 },
278 { 6400, 1200 },
279#endif
280 };
281
282#ifdef VBOX_XPDM_MINIPORT
283 ULONG vramSize = pExt->pPrimary->u.primary.ulMaxFrameBufferSize;
284#else
285 ULONG vramSize = vboxWddmVramCpuVisibleSegmentSize(pExt);
286 /* at least two surfaces will be needed: primary & shadow */
287 vramSize /= 2 * pExt->u.primary.commonInfo.cDisplays;
288#endif
289
290 uint32_t iMode=0, iPrefIdx=0;
291 /* there are 4 color depths: 8, 16, 24 and 32bpp and we reserve 50% of the modes for other sources */
292 size_t maxModesPerColorDepth = VBOXMP_MAX_VIDEO_MODES / 2 / 4;
293
294 /* Always add 800x600 video modes. Windows XP+ needs at least 800x600 resolution
295 * and fallbacks to 800x600x4bpp VGA mode if the driver did not report suitable modes.
296 * This resolution could be rejected by a low resolution host (netbooks, etc).
297 */
298#ifdef VBOX_WITH_8BPP_MODES
299 int bytesPerPixel=1;
300#else
301 int bytesPerPixel=2;
302#endif
303 for (; bytesPerPixel<=4; bytesPerPixel++)
304 {
305 int bitsPerPixel = 8*bytesPerPixel;
306
307 if (800*600*bytesPerPixel > (LONG)vramSize)
308 {
309 /* we don't have enough VRAM for this mode */
310 continue;
311 }
312
313 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iMode, 800, 600, bitsPerPixel))
314 continue;
315
316 VBoxFillVidModeInfo(&pModesTable[iMode], 800, 600, bitsPerPixel, iMode+1, 0);
317
318 if (32==bitsPerPixel)
319 {
320 iPrefIdx = iMode;
321 }
322 ++iMode;
323 }
324
325 /* Query yoffset from the host */
326 ULONG yOffset = VBoxGetHeightReduction();
327
328 /* Iterate through our static resolution table and add supported video modes for different bpp's */
329#ifdef VBOX_WITH_8BPP_MODES
330 bytesPerPixel=1;
331#else
332 bytesPerPixel=2;
333#endif
334 for (; bytesPerPixel<=4; bytesPerPixel++)
335 {
336 int bitsPerPixel = 8*bytesPerPixel;
337 size_t cAdded, resIndex;
338
339 for (cAdded=0, resIndex=0; resIndex<RT_ELEMENTS(resolutionMatrix) && cAdded<maxModesPerColorDepth; resIndex++)
340 {
341 if (resolutionMatrix[resIndex].xRes * resolutionMatrix[resIndex].yRes * bytesPerPixel > (LONG)vramSize)
342 {
343 /* we don't have enough VRAM for this mode */
344 continue;
345 }
346
347 if (yOffset == 0 && resolutionMatrix[resIndex].xRes == 800 && resolutionMatrix[resIndex].yRes == 600)
348 {
349 /* this mode was already added */
350 continue;
351 }
352
353 if (
354#ifdef VBOX_WDDM_MINIPORT
355 /* 1024x768 resolution is a minimal resolutions for win8 to make most metro apps run.
356 * For small host display resolutions, host will dislike the mode 1024x768 and above
357 * if the framebuffer window requires scrolling to fit the guest resolution.
358 * So add 1024x768 resolution for win8 guest to allow user switch to it */
359 ((VBoxQueryWinVersion() != WIN8 && VBoxQueryWinVersion() != WIN8_1) || resolutionMatrix[resIndex].xRes != 1024 || resolutionMatrix[resIndex].yRes != 768) &&
360#endif
361 !VBoxLikesVideoMode(iDisplay, resolutionMatrix[resIndex].xRes, resolutionMatrix[resIndex].yRes - yOffset, bitsPerPixel))
362 {
363 /* host doesn't like this mode */
364 continue;
365 }
366
367 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, resolutionMatrix[resIndex].xRes, resolutionMatrix[resIndex].yRes, bitsPerPixel))
368 {
369 /* guest does not like this mode */
370 continue;
371 }
372
373 /* Sanity check, we shouldn't ever get here */
374 if (iMode >= tableSize)
375 {
376 WARN(("video modes table overflow!"));
377 break;
378 }
379
380 VBoxFillVidModeInfo(&pModesTable[iMode], resolutionMatrix[resIndex].xRes, resolutionMatrix[resIndex].yRes, bitsPerPixel, iMode+1, yOffset);
381 ++iMode;
382 ++cAdded;
383 }
384 }
385
386 /* Check registry for manually added modes, up to 128 entries is supported
387 * Give up on the first error encountered.
388 */
389 VBOXMPCMNREGISTRY Registry;
390 int fPrefSet=0;
391 VP_STATUS rc;
392
393 rc = VBoxMPCmnRegInit(pExt, &Registry);
394 VBOXMP_WARN_VPS(rc);
395
396 for (int curKey=0; curKey<128; curKey++)
397 {
398 if (iMode>=tableSize)
399 {
400 WARN(("ignoring possible custom mode(s), table is full!"));
401 break;
402 }
403
404 wchar_t keyname[24];
405 uint32_t xres, yres, bpp = 0;
406
407 swprintf(keyname, L"CustomMode%dWidth", curKey);
408 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &xres);
409 VBOXMP_CHECK_VPS_BREAK(rc);
410
411 swprintf(keyname, L"CustomMode%dHeight", curKey);
412 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &yres);
413 VBOXMP_CHECK_VPS_BREAK(rc);
414
415 swprintf(keyname, L"CustomMode%dBPP", curKey);
416 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &bpp);
417 VBOXMP_CHECK_VPS_BREAK(rc);
418
419 LOG(("got custom mode[%u]=%ux%u:%u", curKey, xres, yres, bpp));
420
421 /* round down width to be a multiple of 8 if necessary */
422 if (!VBoxCommonFromDeviceExt(pExt)->fAnyX)
423 {
424 xres &= 0xFFF8;
425 }
426
427 if ( (xres > (1 << 16))
428 || (yres > (1 << 16))
429 || ( (bpp != 16)
430 && (bpp != 24)
431 && (bpp != 32)))
432 {
433 /* incorrect values */
434 break;
435 }
436
437 /* does it fit within our VRAM? */
438 if (xres * yres * (bpp / 8) > vramSize)
439 {
440 /* we don't have enough VRAM for this mode */
441 break;
442 }
443
444 if (!VBoxLikesVideoMode(iDisplay, xres, yres, bpp))
445 {
446 /* host doesn't like this mode */
447 break;
448 }
449
450 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, xres, yres, bpp))
451 {
452 /* guest does not like this mode */
453 continue;
454 }
455
456 LOG(("adding video mode from registry."));
457
458 VBoxFillVidModeInfo(&pModesTable[iMode], xres, yres, bpp, iMode+1, yOffset);
459
460 if (!fPrefSet)
461 {
462 fPrefSet = 1;
463 iPrefIdx = iMode;
464 }
465#ifdef VBOX_WDDM_MINIPORT
466 /*check if the same mode has been added to the table already*/
467 int foundIdx = VBoxMPFindVideoMode(pModesTable, iMode, &pModesTable[iMode]);
468
469 if (foundIdx>=0)
470 {
471 if (iPrefIdx==iMode)
472 {
473 iPrefIdx=foundIdx;
474 }
475 }
476 else
477#endif
478 {
479 ++iMode;
480 }
481 }
482
483 rc = VBoxMPCmnRegFini(Registry);
484 VBOXMP_WARN_VPS(rc);
485
486 if (pPrefModeIdx)
487 {
488 *pPrefModeIdx = iPrefIdx;
489 }
490
491 return iMode;
492}
493
494/* Returns if we're in the first mode change, ie doesn't have valid video mode set yet */
495static BOOLEAN VBoxMPIsStartingUp(PVBOXMP_DEVEXT pExt, uint32_t iDisplay)
496{
497#ifdef VBOX_XPDM_MINIPORT
498 return (pExt->CurrentMode == 0);
499#elif defined(VBOX_WDDM_WIN8)
500 return FALSE;
501#else /* VBOX_WDDM_MINIPORT && !VBOX_WDDM_MINIPORT */
502 return (!VBoxCommonFromDeviceExt(pExt)->cDisplays
503 || !pExt->aSources[iDisplay].pPrimaryAllocation);
504#endif
505}
506
507#ifdef VBOX_WDDM_MINIPORT
508static const uint32_t g_aVBoxVidModesSupportedBpps[] = {
509 32
510#ifndef VBOX_WITHOUT_24BPP_MODES
511 , 24
512#endif
513 , 16
514#ifdef VBOX_WITH_8BPP_MODES
515 , 8
516#endif
517};
518DECLINLINE(BOOLEAN) VBoxMPIsSupportedBpp(uint32_t bpp)
519{
520 for (int i = 0; i < RT_ELEMENTS(g_aVBoxVidModesSupportedBpps); ++i)
521 {
522 if (bpp == g_aVBoxVidModesSupportedBpps[i])
523 return TRUE;
524 }
525 return FALSE;
526}
527
528DECLINLINE(uint32_t) VBoxMPAdjustBpp(uint32_t bpp)
529{
530 if (VBoxMPIsSupportedBpp(bpp))
531 return bpp;
532 Assert(g_aVBoxVidModesSupportedBpps[0] == 32);
533 return g_aVBoxVidModesSupportedBpps[0];
534}
535#endif
536/* Updates missing video mode params with current values,
537 * Checks if resulting mode is liked by the host and fits into VRAM.
538 * Returns TRUE if resulting mode could be used.
539 */
540static BOOLEAN
541VBoxMPValidateVideoModeParams(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, uint32_t &xres, uint32_t &yres, uint32_t &bpp)
542{
543 /* Make sure all important video mode values are set */
544 if (VBoxMPIsStartingUp(pExt, iDisplay))
545 {
546 /* Use stored custom values only if nothing was read from host. */
547 xres = xres ? xres:g_CustomVideoModes[iDisplay].VisScreenWidth;
548 yres = yres ? yres:g_CustomVideoModes[iDisplay].VisScreenHeight;
549 bpp = bpp ? bpp :g_CustomVideoModes[iDisplay].BitsPerPlane;
550 }
551 else
552 {
553 /* Use current values for field which weren't read from host. */
554#ifdef VBOX_XPDM_MINIPORT
555 xres = xres ? xres:pExt->CurrentModeWidth;
556 yres = yres ? yres:pExt->CurrentModeHeight;
557 bpp = bpp ? bpp :pExt->CurrentModeBPP;
558#else
559 PVBOXWDDM_ALLOC_DATA pAllocData = pExt->aSources[iDisplay].pPrimaryAllocation ?
560 &pExt->aSources[iDisplay].pPrimaryAllocation->AllocData
561 : &pExt->aSources[iDisplay].AllocData;
562 xres = xres ? xres:pAllocData->SurfDesc.width;
563 yres = yres ? yres:pAllocData->SurfDesc.height;
564 /* VBox WDDM driver does not allow 24 modes since OS could choose the 24bit mode as default in that case,
565 * the pExt->aSources[iDisplay].AllocData.SurfDesc.bpp could be initially 24 though,
566 * i.e. when driver occurs the current mode on driver load via DxgkCbAcquirePostDisplayOwnership
567 * and until driver reports the supported modes
568 * This is true for Win8 Display-Only driver currently since DxgkCbAcquirePostDisplayOwnership is only used by it
569 *
570 * This is why we need to adjust the current mode bpp to the value we actually report as supported */
571 bpp = bpp ? bpp : VBoxMPAdjustBpp(pAllocData->SurfDesc.bpp);
572#endif
573 }
574
575 /* Round down width to be a multiple of 8 if necessary */
576 if (!VBoxCommonFromDeviceExt(pExt)->fAnyX)
577 {
578 xres &= 0xFFF8;
579 }
580
581 /* We always need bpp to be set */
582 if (!bpp)
583 {
584 bpp=32;
585 }
586
587 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, xres, yres, bpp))
588 {
589 WARN(("GUEST does not like special mode %dx%d:%d for display %d", xres, yres, bpp, iDisplay));
590 return FALSE;
591 }
592
593 /* Check if host likes this mode */
594 if (!VBoxLikesVideoMode(iDisplay, xres, yres, bpp))
595 {
596 WARN_NOBP(("HOST does not like special mode %dx%d:%d for display %d", xres, yres, bpp, iDisplay));
597 return FALSE;
598 }
599
600#ifdef VBOX_XPDM_MINIPORT
601 ULONG vramSize = pExt->pPrimary->u.primary.ulMaxFrameBufferSize;
602#else
603 ULONG vramSize = vboxWddmVramCpuVisibleSegmentSize(pExt);
604 vramSize /= pExt->u.primary.commonInfo.cDisplays;
605# ifdef VBOX_WDDM_WIN8
606 if (!g_VBoxDisplayOnly)
607# endif
608 {
609 /* at least two surfaces will be needed: primary & shadow */
610 vramSize /= 2;
611 }
612#endif
613
614 /* Check that values are valid and mode fits into VRAM */
615 if (!xres || !yres
616 || !((bpp == 16)
617#ifdef VBOX_WITH_8BPP_MODES
618 || (bpp == 8)
619#endif
620 || (bpp == 24)
621 || (bpp == 32)))
622 {
623 LOG(("invalid params for special mode %dx%d:%d", xres, yres, bpp));
624 return FALSE;
625 }
626
627
628
629 if ((xres * yres * (bpp / 8) >= vramSize))
630 {
631 /* Store values of last reported release log message to avoid log flooding. */
632 static uint32_t s_xresNoVRAM=0, s_yresNoVRAM=0, s_bppNoVRAM=0;
633
634 LOG(("not enough VRAM for video mode %dx%dx%dbpp. Available: %d bytes. Required: more than %d bytes.",
635 xres, yres, bpp, vramSize, xres * yres * (bpp / 8)));
636
637 s_xresNoVRAM = xres;
638 s_yresNoVRAM = yres;
639 s_bppNoVRAM = bpp;
640
641 return FALSE;
642 }
643
644 return TRUE;
645}
646
647/* Checks if there's a pending video mode change hint,
648 * and fills pPendingMode with associated info.
649 * returns TRUE if there's a pending change. Otherwise returns FALSE.
650 */
651static BOOLEAN
652VBoxMPCheckPendingVideoMode(PVBOXMP_DEVEXT pExt, PVIDEO_MODE_INFORMATION pPendingMode)
653{
654 uint32_t xres=0, yres=0, bpp=0, display=0;
655
656 /* Check if there's a pending display change request for this display */
657 if (VBoxQueryDisplayRequest(&xres, &yres, &bpp, &display) && (xres || yres || bpp))
658 {
659 if (display>RT_ELEMENTS(g_CustomVideoModes))
660 {
661 /*display = RT_ELEMENTS(g_CustomVideoModes) - 1;*/
662 WARN(("VBoxQueryDisplayRequest returned invalid display number %d", display));
663 return FALSE;
664 }
665 }
666 else
667 {
668 LOG(("no pending request"));
669 return FALSE;
670 }
671
672 /* Correct video mode params and check if host likes it */
673 if (VBoxMPValidateVideoModeParams(pExt, display, xres, yres, bpp))
674 {
675 VBoxFillVidModeInfo(pPendingMode, xres, yres, bpp, display, 0);
676 return TRUE;
677 }
678
679 return FALSE;
680}
681
682/* Save custom mode info to registry */
683static void VBoxMPRegSaveModeInfo(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, PVIDEO_MODE_INFORMATION pMode)
684{
685 VBOXMPCMNREGISTRY Registry;
686 VP_STATUS rc;
687
688 rc = VBoxMPCmnRegInit(pExt, &Registry);
689 VBOXMP_WARN_VPS(rc);
690
691 if (iDisplay==0)
692 {
693 /*First name without a suffix*/
694 rc = VBoxMPCmnRegSetDword(Registry, L"CustomXRes", pMode->VisScreenWidth);
695 VBOXMP_WARN_VPS(rc);
696 rc = VBoxMPCmnRegSetDword(Registry, L"CustomYRes", pMode->VisScreenHeight);
697 VBOXMP_WARN_VPS(rc);
698 rc = VBoxMPCmnRegSetDword(Registry, L"CustomBPP", pMode->BitsPerPlane);
699 VBOXMP_WARN_VPS(rc);
700 }
701 else
702 {
703 wchar_t keyname[32];
704 swprintf(keyname, L"CustomXRes%d", iDisplay);
705 rc = VBoxMPCmnRegSetDword(Registry, keyname, pMode->VisScreenWidth);
706 VBOXMP_WARN_VPS(rc);
707 swprintf(keyname, L"CustomYRes%d", iDisplay);
708 rc = VBoxMPCmnRegSetDword(Registry, keyname, pMode->VisScreenHeight);
709 VBOXMP_WARN_VPS(rc);
710 swprintf(keyname, L"CustomBPP%d", iDisplay);
711 rc = VBoxMPCmnRegSetDword(Registry, keyname, pMode->BitsPerPlane);
712 VBOXMP_WARN_VPS(rc);
713 }
714
715 rc = VBoxMPCmnRegFini(Registry);
716 VBOXMP_WARN_VPS(rc);
717}
718
719#ifdef VBOX_XPDM_MINIPORT
720VIDEO_MODE_INFORMATION* VBoxMPXpdmCurrentVideoMode(PVBOXMP_DEVEXT pExt)
721{
722 return VBoxMPCmnGetVideoModeInfo(pExt->CurrentMode - 1);
723}
724
725ULONG VBoxMPXpdmGetVideoModesCount()
726{
727 return g_NumVideoModes;
728}
729
730/* Makes a table of video modes consisting of:
731 * Default modes
732 * Custom modes manually added to registry
733 * Custom modes for all displays (either from a display change hint or stored in registry)
734 * 2 special modes, for a pending display change for this adapter. See comments below.
735 */
736void VBoxMPXpdmBuildVideoModesTable(PVBOXMP_DEVEXT pExt)
737{
738 uint32_t cStandartModes, cCustomModes;
739 BOOLEAN bPending, bHaveSpecial;
740 VIDEO_MODE_INFORMATION specialMode;
741
742 /* Fill table with standart modes and ones manually added to registry.
743 * Up to VBOXMP_MAX_VIDEO_MODES elements can be used, the rest is reserved
744 * for custom mode alternating indexes.
745 */
746 cStandartModes = VBoxMPFillModesTable(pExt, pExt->iDevice, g_VideoModes, VBOXMP_MAX_VIDEO_MODES, NULL);
747
748 /* Add custom modes for all displays to the table */
749 cCustomModes = VBoxCommonFromDeviceExt(pExt)->cDisplays;
750 PVBOXMP_DEVEXT pDisplayExt = pExt->pPrimary; /* Corresponds to the display number in the loop. */
751 for (uint32_t i=0; i<cCustomModes; i++)
752 {
753 /* Make 2 entries in the video mode table. */
754 uint32_t iModeBase = cStandartModes + 2*i;
755 uint32_t iSpecialMode;
756
757 if (pDisplayExt)
758 {
759 /* Take the alternating index into account. */
760 BOOLEAN bAlternativeIndex = (pDisplayExt->iInvocationCounter % 2)? TRUE: FALSE;
761
762 iSpecialMode = iModeBase + (bAlternativeIndex? 1: 0);
763 uint32_t iStandardMode = iModeBase + (bAlternativeIndex? 0: 1);
764
765 /* Fill the special mode. */
766 memcpy(&g_VideoModes[iSpecialMode], &g_CustomVideoModes[i], sizeof(VIDEO_MODE_INFORMATION));
767 g_VideoModes[iSpecialMode].ModeIndex = iSpecialMode + 1;
768
769 /* Wipe the other entry so it is not selected. */
770 memcpy(&g_VideoModes[iStandardMode], &g_VideoModes[3], sizeof(VIDEO_MODE_INFORMATION));
771 g_VideoModes[iStandardMode].ModeIndex = iStandardMode + 1;
772
773 pDisplayExt = pDisplayExt->pNext;
774 }
775 else
776 {
777 /* Should not happen, but better to fallback than to crash. */
778 memcpy(&g_VideoModes[iModeBase], &g_CustomVideoModes[i], sizeof(VIDEO_MODE_INFORMATION));
779 g_VideoModes[iModeBase].ModeIndex = iModeBase + 1;
780
781 g_VideoModes[iModeBase + 1] = g_VideoModes[iModeBase];
782 g_VideoModes[iModeBase + 1].ModeIndex += 1;
783
784 iSpecialMode = iModeBase;
785 }
786
787 LOG(("added special mode[%d] %dx%d:%d for display %d\n",
788 iSpecialMode,
789 g_VideoModes[iSpecialMode].VisScreenWidth,
790 g_VideoModes[iSpecialMode].VisScreenHeight,
791 g_VideoModes[iSpecialMode].BitsPerPlane,
792 i));
793 }
794
795 /* Check if host wants us to switch video mode and it's for this adapter */
796 bPending = VBoxMPCheckPendingVideoMode(pExt, &specialMode);
797 bHaveSpecial = bPending && (pExt->iDevice == specialMode.ModeIndex);
798 LOG(("bPending %d, pExt->iDevice %d, specialMode.ModeIndex %d",
799 bPending, pExt->iDevice, specialMode.ModeIndex));
800
801 /* Check the startup case */
802 if (!bHaveSpecial && VBoxMPIsStartingUp(pExt, pExt->iDevice))
803 {
804 uint32_t xres=0, yres=0, bpp=0;
805 LOG(("Startup for screen %d", pExt->iDevice));
806 /* Check if we could make valid mode from values stored to registry */
807 if (VBoxMPValidateVideoModeParams(pExt, pExt->iDevice, xres, yres, bpp))
808 {
809 LOG(("Startup for screen %d validated %dx%d %d", pExt->iDevice, xres, yres, bpp));
810 VBoxFillVidModeInfo(&specialMode, xres, yres, bpp, 0, 0);
811 bHaveSpecial = TRUE;
812 }
813 }
814
815 /* Update number of modes. Each display has 2 entries for alternating custom mode index. */
816 g_NumVideoModes = cStandartModes + cCustomModes * 2;
817
818 if (bHaveSpecial)
819 {
820 /* We need to alternate mode index entry for a pending mode change,
821 * else windows will ignore actual mode change call.
822 * Only alternate index if one of mode parameters changed and
823 * regardless of conditions always add 2 entries to the table.
824 */
825 BOOLEAN bAlternativeIndex = FALSE;
826
827 BOOLEAN bChanged = (pExt->Prev_xres!=specialMode.VisScreenWidth
828 || pExt->Prev_yres!=specialMode.VisScreenHeight
829 || pExt->Prev_bpp!=specialMode.BitsPerPlane);
830
831 LOG(("prev %dx%dx%d, special %dx%dx%d",
832 pExt->Prev_xres, pExt->Prev_yres, pExt->Prev_bpp,
833 specialMode.VisScreenWidth, specialMode.VisScreenHeight, specialMode.BitsPerPlane));
834
835 if (bChanged)
836 {
837 pExt->Prev_xres = specialMode.VisScreenWidth;
838 pExt->Prev_yres = specialMode.VisScreenHeight;
839 pExt->Prev_bpp = specialMode.BitsPerPlane;
840 }
841
842 /* Check if we need to alternate the index */
843 if (!VBoxMPIsStartingUp(pExt, pExt->iDevice))
844 {
845 if (bChanged)
846 {
847 pExt->iInvocationCounter++;
848 }
849
850 if (pExt->iInvocationCounter % 2)
851 {
852 bAlternativeIndex = TRUE;
853 }
854 }
855
856 uint32_t iSpecialModeElement = cStandartModes + 2 * pExt->iDevice + (bAlternativeIndex? 1: 0);
857 uint32_t iSpecialModeElementOld = cStandartModes + 2 * pExt->iDevice + (bAlternativeIndex? 0: 1);
858
859 LOG(("add special mode[%d] %dx%d:%d for display %d (bChanged=%d, bAlternativeIndex=%d)",
860 iSpecialModeElement, specialMode.VisScreenWidth, specialMode.VisScreenHeight, specialMode.BitsPerPlane,
861 pExt->iDevice, bChanged, bAlternativeIndex));
862
863 /* Add special mode to the table
864 * Note: Y offset isn't used for a host-supplied modes
865 */
866 specialMode.ModeIndex = iSpecialModeElement + 1;
867 memcpy(&g_VideoModes[iSpecialModeElement], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
868
869 /* Save special mode in the custom modes table */
870 memcpy(&g_CustomVideoModes[pExt->iDevice], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
871
872 /* Wipe the old entry so the special mode will be found in the new positions. */
873 memcpy(&g_VideoModes[iSpecialModeElementOld], &g_VideoModes[3], sizeof(VIDEO_MODE_INFORMATION));
874 g_VideoModes[iSpecialModeElementOld].ModeIndex = iSpecialModeElementOld + 1;
875
876 /* Save special mode info to registry */
877 VBoxMPRegSaveModeInfo(pExt, pExt->iDevice, &specialMode);
878 }
879
880#if defined(LOG_ENABLED)
881 do
882 {
883 LOG(("Filled %d modes", g_NumVideoModes));
884
885 for (uint32_t i=0; i<g_NumVideoModes; ++i)
886 {
887 LOG(("Mode[%2d]: %4dx%4d:%2d (idx=%d)",
888 i, g_VideoModes[i].VisScreenWidth, g_VideoModes[i].VisScreenHeight,
889 g_VideoModes[i].BitsPerPlane, g_VideoModes[i].ModeIndex));
890 }
891 } while (0);
892#endif
893}
894#endif /*VBOX_XPDM_MINIPORT*/
895
896#ifdef VBOX_WDDM_MINIPORT
897static VBOXWDDM_VIDEOMODES_INFO g_aVBoxVideoModeInfos[VBOX_VIDEO_MAX_SCREENS] = {0};
898
899bool VBoxWddmFillMode(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, VIDEO_MODE_INFORMATION *pInfo, D3DDDIFORMAT enmFormat, ULONG w, ULONG h)
900{
901 switch (enmFormat)
902 {
903 case D3DDDIFMT_A8R8G8B8:
904 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 32))
905 {
906 WARN(("unsupported mode info for format(%d)", enmFormat));
907 return false;
908 }
909 VBoxFillVidModeInfo(pInfo, w, h, 32, 0, 0);
910 return true;
911 case D3DDDIFMT_R8G8B8:
912 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 24))
913 {
914 WARN(("unsupported mode info for format(%d)", enmFormat));
915 return false;
916 }
917 VBoxFillVidModeInfo(pInfo, w, h, 24, 0, 0);
918 return true;
919 case D3DDDIFMT_R5G6B5:
920 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 16))
921 {
922 WARN(("unsupported mode info for format(%d)", enmFormat));
923 return false;
924 }
925 VBoxFillVidModeInfo(pInfo, w, h, 16, 0, 0);
926 return true;
927 case D3DDDIFMT_P8:
928 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 8))
929 {
930 WARN(("unsupported mode info for format(%d)", enmFormat));
931 return false;
932 }
933 VBoxFillVidModeInfo(pInfo, w, h, 8, 0, 0);
934 return true;
935 default:
936 WARN(("unsupported enmFormat(%d)", enmFormat));
937 AssertBreakpoint();
938 break;
939 }
940
941 return false;
942}
943
944static void
945VBoxWddmBuildResolutionTable(PVIDEO_MODE_INFORMATION pModesTable, size_t tableSize, int iPreferredMode,
946 SIZE *pResolutions, uint32_t * pcResolutions, int *piPreferredResolution)
947{
948 uint32_t cResolutionsArray = *pcResolutions;
949 uint32_t cResolutions = 0;
950
951 *piPreferredResolution = -1;
952
953 for (uint32_t i=0; i<tableSize; ++i)
954 {
955 PVIDEO_MODE_INFORMATION pMode = &pModesTable[i];
956 int iResolution = -1;
957
958 for (uint32_t j=0; j<cResolutions; ++j)
959 {
960 if (pResolutions[j].cx == pMode->VisScreenWidth
961 && pResolutions[j].cy == pMode->VisScreenHeight)
962 {
963 iResolution = j;
964 break;
965 }
966 }
967
968 if (iResolution < 0)
969 {
970 if (cResolutions == cResolutionsArray)
971 {
972 WARN(("table overflow!"));
973 break;
974 }
975
976 iResolution = cResolutions;
977 pResolutions[cResolutions].cx = pMode->VisScreenWidth;
978 pResolutions[cResolutions].cy = pMode->VisScreenHeight;
979 ++cResolutions;
980 }
981
982 Assert(iResolution >= 0);
983 if (i == iPreferredMode)
984 {
985 Assert(*piPreferredResolution == -1);
986 *piPreferredResolution = iResolution;
987 }
988 }
989
990 *pcResolutions = cResolutions;
991 Assert(*piPreferredResolution >= 0);
992}
993
994static void VBoxWddmBuildResolutionTableForModes(PVBOXWDDM_VIDEOMODES_INFO pModes)
995{
996 pModes->cResolutions = RT_ELEMENTS(pModes->aResolutions);
997 VBoxWddmBuildResolutionTable(pModes->aModes, pModes->cModes, pModes->iPreferredMode,
998 (SIZE*)((void*)pModes->aResolutions), &pModes->cResolutions, &pModes->iPreferredResolution);
999 Assert(pModes->aResolutions[pModes->iPreferredResolution].cx == pModes->aModes[pModes->iPreferredMode].VisScreenWidth
1000 && pModes->aResolutions[pModes->iPreferredResolution].cy == pModes->aModes[pModes->iPreferredMode].VisScreenHeight);
1001}
1002
1003AssertCompile(sizeof (SIZE) == sizeof (D3DKMDT_2DREGION));
1004AssertCompile(RT_OFFSETOF(SIZE, cx) == RT_OFFSETOF(D3DKMDT_2DREGION, cx));
1005AssertCompile(RT_OFFSETOF(SIZE, cy) == RT_OFFSETOF(D3DKMDT_2DREGION, cy));
1006static void
1007VBoxWddmBuildVideoModesInfo(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId,
1008 PVBOXWDDM_VIDEOMODES_INFO pModes, VIDEO_MODE_INFORMATION *paAddlModes,
1009 UINT cAddlModes)
1010{
1011 pModes->cResolutions = RT_ELEMENTS(pModes->aResolutions);
1012
1013 /* Add default modes and ones read from registry. */
1014 pModes->cModes = VBoxMPFillModesTable(pExt, VidPnTargetId, pModes->aModes, RT_ELEMENTS(pModes->aModes), &pModes->iPreferredMode);
1015 Assert(pModes->cModes<=RT_ELEMENTS(pModes->aModes));
1016
1017 if (!VBoxMPIsStartingUp(pExt, VidPnTargetId))
1018 {
1019 /* make sure we keep the current mode to avoid mode flickering */
1020 PVBOXWDDM_ALLOC_DATA pAllocData = pExt->aSources[VidPnTargetId].pPrimaryAllocation ?
1021 &pExt->aSources[VidPnTargetId].pPrimaryAllocation->AllocData
1022 : &pExt->aSources[VidPnTargetId].AllocData;
1023 if (pModes->cModes < RT_ELEMENTS(pModes->aModes))
1024 {
1025 /* VBox WDDM driver does not allow 24 modes since OS could choose the 24bit mode as default in that case,
1026 * the pExt->aSources[iDisplay].AllocData.SurfDesc.bpp could be initially 24 though,
1027 * i.e. when driver occurs the current mode on driver load via DxgkCbAcquirePostDisplayOwnership
1028 * and until driver reports the supported modes
1029 * This is true for Win8 Display-Only driver currently since DxgkCbAcquirePostDisplayOwnership is only used by it
1030 *
1031 * This is why we check the bpp to be supported here and add the current mode to the list only in that case */
1032 if (VBoxMPIsSupportedBpp(pAllocData->SurfDesc.bpp))
1033 {
1034 int foundIdx;
1035 VBoxFillVidModeInfo(&pModes->aModes[pModes->cModes], pAllocData->SurfDesc.width, pAllocData->SurfDesc.height, pAllocData->SurfDesc.bpp, 1/*index*/, 0);
1036 if ((foundIdx=VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]))>=0)
1037 {
1038 pModes->iPreferredMode = foundIdx;
1039 }
1040 else
1041 {
1042 pModes->iPreferredMode = pModes->cModes;
1043 ++pModes->cModes;
1044 }
1045
1046#ifdef VBOX_WITH_8BPP_MODES
1047 int bytesPerPixel=1;
1048#else
1049 int bytesPerPixel=2;
1050#endif
1051 for (; bytesPerPixel<=4; bytesPerPixel++)
1052 {
1053 int bpp = 8*bytesPerPixel;
1054
1055 if (bpp == pAllocData->SurfDesc.bpp)
1056 continue;
1057
1058 if (!VBoxMPValidateVideoModeParamsGuest(pExt, VidPnTargetId,
1059 pAllocData->SurfDesc.width, pAllocData->SurfDesc.height,
1060 bpp))
1061 continue;
1062
1063 if (pModes->cModes >= RT_ELEMENTS(pModes->aModes))
1064 {
1065 WARN(("ran out of video modes 2"));
1066 break;
1067 }
1068
1069 VBoxFillVidModeInfo(&pModes->aModes[pModes->cModes],
1070 pAllocData->SurfDesc.width, pAllocData->SurfDesc.height,
1071 bpp, pModes->cModes, 0);
1072 if (VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]) < 0)
1073 {
1074 ++pModes->cModes;
1075 }
1076 }
1077 }
1078 }
1079 else
1080 {
1081 WARN(("ran out of video modes 1"));
1082 }
1083 }
1084
1085 /* Check if there's a pending display change request for this adapter */
1086 VIDEO_MODE_INFORMATION specialMode;
1087 if (VBoxMPCheckPendingVideoMode(pExt, &specialMode) && (specialMode.ModeIndex==VidPnTargetId))
1088 {
1089 /*Minor hack, ModeIndex!=0 Means this mode has been validated already and not just read from registry */
1090 specialMode.ModeIndex = 1;
1091 memcpy(&g_CustomVideoModes[VidPnTargetId], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
1092
1093 /* Save mode to registry */
1094 VBoxMPRegSaveModeInfo(pExt, VidPnTargetId, &specialMode);
1095 }
1096
1097 /* Validate the mode which has been read from registry */
1098 if (!g_CustomVideoModes[VidPnTargetId].ModeIndex)
1099 {
1100 uint32_t xres, yres, bpp;
1101
1102 xres = g_CustomVideoModes[VidPnTargetId].VisScreenWidth;
1103 yres = g_CustomVideoModes[VidPnTargetId].VisScreenHeight;
1104 bpp = g_CustomVideoModes[VidPnTargetId].BitsPerPlane;
1105
1106 if (VBoxMPValidateVideoModeParams(pExt, VidPnTargetId, xres, yres, bpp))
1107 {
1108 VBoxFillVidModeInfo(&g_CustomVideoModes[VidPnTargetId], xres, yres, bpp, 1/*index*/, 0);
1109 Assert(g_CustomVideoModes[VidPnTargetId].ModeIndex == 1);
1110 }
1111 }
1112
1113 /* Add custom mode to the table */
1114 if (g_CustomVideoModes[VidPnTargetId].ModeIndex)
1115 {
1116 if (RT_ELEMENTS(pModes->aModes) > pModes->cModes)
1117 {
1118 g_CustomVideoModes[VidPnTargetId].ModeIndex = pModes->cModes;
1119 pModes->aModes[pModes->cModes] = g_CustomVideoModes[VidPnTargetId];
1120
1121 /* Check if we already have this mode in the table */
1122 int foundIdx;
1123 if ((foundIdx=VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]))>=0)
1124 {
1125 pModes->iPreferredMode = foundIdx;
1126 }
1127 else
1128 {
1129 pModes->iPreferredMode = pModes->cModes;
1130 ++pModes->cModes;
1131 }
1132
1133 /* Add other bpp modes for this custom resolution */
1134#ifdef VBOX_WITH_8BPP_MODES
1135 UINT bpp=8;
1136#else
1137 UINT bpp=16;
1138#endif
1139 for (; bpp<=32; bpp+=8)
1140 {
1141 if (RT_ELEMENTS(pModes->aModes) == pModes->cModes)
1142 {
1143 WARN(("table full, can't add other bpp for specail mode!"));
1144#ifdef DEBUG_misha
1145 /* this is definitely something we do not expect */
1146 AssertFailed();
1147#endif
1148 break;
1149 }
1150
1151 AssertRelease(RT_ELEMENTS(pModes->aModes) > pModes->cModes); /* if not - the driver state is screwed up, @todo: better do KeBugCheckEx here */
1152
1153 if (pModes->aModes[pModes->iPreferredMode].BitsPerPlane == bpp)
1154 continue;
1155
1156 if (!VBoxMPValidateVideoModeParamsGuest(pExt, VidPnTargetId,
1157 pModes->aModes[pModes->iPreferredMode].VisScreenWidth,
1158 pModes->aModes[pModes->iPreferredMode].VisScreenHeight,
1159 bpp))
1160 continue;
1161
1162 VBoxFillVidModeInfo(&pModes->aModes[pModes->cModes],
1163 pModes->aModes[pModes->iPreferredMode].VisScreenWidth,
1164 pModes->aModes[pModes->iPreferredMode].VisScreenHeight,
1165 bpp, pModes->cModes, 0);
1166 if (VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]) < 0)
1167 {
1168 ++pModes->cModes;
1169 }
1170 }
1171 }
1172 else
1173 {
1174 AssertRelease(RT_ELEMENTS(pModes->aModes) == pModes->cModes); /* if not - the driver state is screwed up, @todo: better do KeBugCheckEx here */
1175 WARN(("table full, can't add video mode for a host request!"));
1176#ifdef DEBUG_misha
1177 /* this is definitely something we do not expect */
1178 AssertFailed();
1179#endif
1180 }
1181 }
1182
1183 /* Check and Add additional modes passed in paAddlModes */
1184 for (UINT i=0; i<cAddlModes; ++i)
1185 {
1186 if (RT_ELEMENTS(pModes->aModes) == pModes->cModes)
1187 {
1188 WARN(("table full, can't add addl modes!"));
1189#ifdef DEBUG_misha
1190 /* this is definitely something we do not expect */
1191 AssertFailed();
1192#endif
1193 break;
1194 }
1195
1196 AssertRelease(RT_ELEMENTS(pModes->aModes) > pModes->cModes); /* if not - the driver state is screwed up, @todo: better do KeBugCheckEx here */
1197
1198 if (!VBoxCommonFromDeviceExt(pExt)->fAnyX)
1199 {
1200 paAddlModes[i].VisScreenWidth &= 0xFFF8;
1201 }
1202
1203 if (VBoxLikesVideoMode(VidPnTargetId, paAddlModes[i].VisScreenWidth, paAddlModes[i].VisScreenHeight, paAddlModes[i].BitsPerPlane))
1204 {
1205 int foundIdx;
1206 if ((foundIdx=VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &paAddlModes[i]))>=0)
1207 {
1208 pModes->iPreferredMode = foundIdx;
1209 }
1210 else
1211 {
1212 memcpy(&pModes->aModes[pModes->cModes], &paAddlModes[i], sizeof(VIDEO_MODE_INFORMATION));
1213 pModes->aModes[pModes->cModes].ModeIndex = pModes->cModes;
1214 ++pModes->cModes;
1215 }
1216 }
1217 }
1218
1219 /* Build resolution table */
1220 VBoxWddmBuildResolutionTableForModes(pModes);
1221}
1222
1223void VBoxWddmInvalidateVideoModesInfo(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId)
1224{
1225 if (VidPnTargetId != D3DDDI_ID_ALL)
1226 {
1227 if (VidPnTargetId >= RT_ELEMENTS(g_aVBoxVideoModeInfos))
1228 {
1229 WARN(("VidPnTargetId (%d) must be less than (%d)", VidPnTargetId, RT_ELEMENTS(g_aVBoxVideoModeInfos)));
1230 return;
1231 }
1232 LOG(("invalidating videomede info for screen %d", VidPnTargetId));
1233 g_aVBoxVideoModeInfos[VidPnTargetId].cModes = 0;
1234 return;
1235 }
1236
1237 LOG(("invalidating ALL videomede infos"));
1238
1239 for (UINT i = 0; i < RT_ELEMENTS(g_aVBoxVideoModeInfos); ++i)
1240 {
1241 g_aVBoxVideoModeInfos[i].cModes = 0;
1242 }
1243}
1244
1245void VBoxWddmInvalidateAllVideoModesInfos(PVBOXMP_DEVEXT pExt)
1246{
1247 VBoxWddmInvalidateVideoModesInfo(pExt, D3DDDI_ID_ALL);
1248}
1249
1250PVBOXWDDM_VIDEOMODES_INFO VBoxWddmUpdateVideoModesInfoByMask(PVBOXMP_DEVEXT pExt, uint8_t *pScreenIdMask)
1251{
1252 for (int i = 0; i < VBoxCommonFromDeviceExt(pExt)->cDisplays; ++i)
1253 {
1254 if (ASMBitTest(pScreenIdMask, i))
1255 VBoxWddmInvalidateVideoModesInfo(pExt, i);
1256 }
1257
1258 /* ensure we have all the rest populated */
1259 VBoxWddmGetAllVideoModesInfos(pExt);
1260 return g_aVBoxVideoModeInfos;
1261}
1262
1263PVBOXWDDM_VIDEOMODES_INFO VBoxWddmUpdateAllVideoModesInfos(PVBOXMP_DEVEXT pExt)
1264{
1265 VBoxWddmInvalidateAllVideoModesInfos(pExt);
1266
1267 /* ensure we have all the rest populated */
1268 VBoxWddmGetAllVideoModesInfos(pExt);
1269 return g_aVBoxVideoModeInfos;
1270}
1271
1272void VBoxWddmAdjustMode(PVBOXMP_DEVEXT pExt, PVBOXWDDM_ADJUSTVIDEOMODE pMode)
1273{
1274 pMode->fFlags = 0;
1275
1276 if (pMode->Mode.Id >= (UINT)VBoxCommonFromDeviceExt(pExt)->cDisplays)
1277 {
1278 WARN(("invalid screen id (%d)", pMode->Mode.Id));
1279 pMode->fFlags = VBOXWDDM_ADJUSTVIDEOMODE_F_INVALISCREENID;
1280 return;
1281 }
1282
1283 PVBOXWDDM_TARGET pTarget = &pExt->aTargets[pMode->Mode.Id];
1284 /* @todo: this info should go from the target actually */
1285 PVBOXWDDM_SOURCE pSource = &pExt->aSources[pMode->Mode.Id];
1286
1287 UINT newWidth = pMode->Mode.Width;
1288 UINT newHeight = pMode->Mode.Height;
1289 UINT newBpp = pMode->Mode.BitsPerPixel;
1290
1291 if (!VBoxMPValidateVideoModeParams(pExt, pMode->Mode.Id, newWidth, newHeight, newBpp))
1292 {
1293 PVBOXWDDM_SOURCE pSource = &pExt->aSources[pMode->Mode.Id];
1294 pMode->fFlags |= VBOXWDDM_ADJUSTVIDEOMODE_F_UNSUPPORTED;
1295 }
1296
1297 if (pMode->Mode.Width != newWidth
1298 || pMode->Mode.Height != newHeight
1299 || pMode->Mode.BitsPerPixel != newBpp)
1300 {
1301 pMode->fFlags |= VBOXWDDM_ADJUSTVIDEOMODE_F_ADJUSTED;
1302 pMode->Mode.Width = newWidth;
1303 pMode->Mode.Height = newHeight;
1304 pMode->Mode.BitsPerPixel = newBpp;
1305 }
1306
1307 if (pTarget->HeightVisible /* <- active */
1308 && pSource->AllocData.SurfDesc.width == pMode->Mode.Width
1309 && pSource->AllocData.SurfDesc.height == pMode->Mode.Height
1310 && pSource->AllocData.SurfDesc.bpp == pMode->Mode.BitsPerPixel)
1311 {
1312 pMode->fFlags |= VBOXWDDM_ADJUSTVIDEOMODE_F_CURRENT;
1313 if (pMode->fFlags & VBOXWDDM_ADJUSTVIDEOMODE_F_UNSUPPORTED)
1314 {
1315 WARN(("current mode is reported as unsupported"));
1316 }
1317 }
1318}
1319
1320void VBoxWddmAdjustModes(PVBOXMP_DEVEXT pExt, uint32_t cModes, PVBOXWDDM_ADJUSTVIDEOMODE aModes)
1321{
1322 for (UINT i = 0; i < cModes; ++i)
1323 {
1324 PVBOXWDDM_ADJUSTVIDEOMODE pMode = &aModes[i];
1325 VBoxWddmAdjustMode(pExt, pMode);
1326 }
1327}
1328
1329NTSTATUS VBoxWddmGetModesForResolution(VIDEO_MODE_INFORMATION *pAllModes, uint32_t cAllModes, int iSearchPreferredMode,
1330 const D3DKMDT_2DREGION *pResolution, VIDEO_MODE_INFORMATION * pModes, uint32_t cModes, uint32_t *pcModes, int32_t *piPreferrableMode)
1331{
1332 NTSTATUS Status = STATUS_SUCCESS;
1333 uint32_t cFound = 0;
1334 int iFoundPreferrableMode = -1;
1335 for (uint32_t i = 0; i < cAllModes; ++i)
1336 {
1337 VIDEO_MODE_INFORMATION *pCur = &pAllModes[i];
1338 if (pResolution->cx == pCur->VisScreenWidth
1339 && pResolution->cy == pCur->VisScreenHeight)
1340 {
1341 if (pModes && cModes > cFound)
1342 memcpy(&pModes[cFound], pCur, sizeof(VIDEO_MODE_INFORMATION));
1343 else
1344 Status = STATUS_BUFFER_TOO_SMALL;
1345
1346 if (i == iSearchPreferredMode)
1347 iFoundPreferrableMode = cFound;
1348
1349 ++cFound;
1350 }
1351 }
1352
1353 Assert(iFoundPreferrableMode < 0 || cFound > (uint32_t)iFoundPreferrableMode);
1354
1355 *pcModes = cFound;
1356 if (piPreferrableMode)
1357 *piPreferrableMode = iFoundPreferrableMode;
1358
1359 return Status;
1360}
1361
1362static PVBOXWDDM_VIDEOMODES_INFO vboxWddmGetVideoModesInfoInternal(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId)
1363{
1364 Assert(VidPnTargetId < (D3DDDI_VIDEO_PRESENT_TARGET_ID)VBoxCommonFromDeviceExt(pExt)->cDisplays);
1365 if (VidPnTargetId >= (D3DDDI_VIDEO_PRESENT_TARGET_ID)VBoxCommonFromDeviceExt(pExt)->cDisplays)
1366 {
1367 WARN(("video mode info for invalid screen (%d) requested", VidPnTargetId));
1368 return NULL;
1369 }
1370
1371 PVBOXWDDM_VIDEOMODES_INFO pInfo = &g_aVBoxVideoModeInfos[VidPnTargetId];
1372
1373 if (!pInfo->cModes)
1374 {
1375 VBoxWddmBuildVideoModesInfo(pExt, VidPnTargetId, pInfo, NULL, 0);
1376 Assert(pInfo->cModes);
1377 }
1378
1379 return pInfo;
1380}
1381
1382static VOID vboxWddmAddVideoModes(PVBOXMP_DEVEXT pExt, PVBOXWDDM_VIDEOMODES_INFO pDstInfo, PVBOXWDDM_VIDEOMODES_INFO pSrcInfo)
1383{
1384 for (int i = 0; i < (int)pSrcInfo->cModes; ++i)
1385 {
1386 int foundIdx = VBoxMPFindVideoMode(pDstInfo->aModes, pDstInfo->cModes, &pSrcInfo->aModes[i]);
1387 if (foundIdx >= 0)
1388 continue;
1389
1390 Assert(0);
1391 pDstInfo->aModes[pDstInfo->cModes] = pSrcInfo->aModes[i];
1392 ++pDstInfo->cModes;
1393 }
1394
1395 VBoxWddmBuildResolutionTableForModes(pDstInfo);
1396}
1397
1398PVBOXWDDM_VIDEOMODES_INFO VBoxWddmGetAllVideoModesInfos(PVBOXMP_DEVEXT pExt)
1399{
1400 /* ensure all modes are initialized */
1401 for (int i = 0; i < VBoxCommonFromDeviceExt(pExt)->cDisplays; ++i)
1402 {
1403 vboxWddmGetVideoModesInfoInternal(pExt, (D3DDDI_VIDEO_PRESENT_TARGET_ID)i);
1404 }
1405
1406 return g_aVBoxVideoModeInfos;
1407}
1408
1409PVBOXWDDM_VIDEOMODES_INFO VBoxWddmGetVideoModesInfo(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId)
1410{
1411 return &VBoxWddmGetAllVideoModesInfos(pExt)[VidPnTargetId];
1412}
1413
1414#endif /*VBOX_WDDM_MINIPORT*/
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