VirtualBox

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

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

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 47.6 KB
Line 
1/* $Id: VBoxMPVidModes.cpp 44529 2013-02-04 15:54:15Z 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 and 2 indexes are reserved for the last 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 || 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 (!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 (!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 cStandartModes = VBoxMPFillModesTable(pExt, pExt->iDevice, g_VideoModes, RT_ELEMENTS(g_VideoModes), NULL);
744
745 /* Add custom modes for all displays to the table */
746 cCustomModes = VBoxCommonFromDeviceExt(pExt)->cDisplays;
747 for (uint32_t i=0; i<cCustomModes; i++)
748 {
749 memcpy(&g_VideoModes[cStandartModes+i], &g_CustomVideoModes[i], sizeof(VIDEO_MODE_INFORMATION));
750 g_VideoModes[cStandartModes+i].ModeIndex = cStandartModes+i+1;
751 }
752
753 /* Check if host wants us to switch video mode and it's for this adapter */
754 bPending = VBoxMPCheckPendingVideoMode(pExt, &specialMode);
755 bHaveSpecial = bPending && (pExt->iDevice == specialMode.ModeIndex);
756
757 /* Check the startup case */
758 if (!bHaveSpecial && VBoxMPIsStartingUp(pExt, pExt->iDevice))
759 {
760 uint32_t xres=0, yres=0, bpp=0;
761 /* Check if we could make valid mode from values stored to registry */
762 if (VBoxMPValidateVideoModeParams(pExt, pExt->iDevice, xres, yres, bpp))
763 {
764 VBoxFillVidModeInfo(&specialMode, xres, yres, bpp, 0, 0);
765 bHaveSpecial = TRUE;
766 }
767 }
768
769 /* Update number of modes */
770 g_NumVideoModes = cStandartModes + cCustomModes;
771
772 if (!bHaveSpecial)
773 {
774 /* Just add 2 dummy modes to maintain table size. */
775 memcpy(&g_VideoModes[g_NumVideoModes], &g_VideoModes[2], sizeof(VIDEO_MODE_INFORMATION));
776 g_VideoModes[g_NumVideoModes].ModeIndex = g_NumVideoModes+1;
777 g_NumVideoModes++;
778 memcpy(&g_VideoModes[g_NumVideoModes], &g_VideoModes[2], sizeof(VIDEO_MODE_INFORMATION));
779 g_VideoModes[g_NumVideoModes].ModeIndex = g_NumVideoModes+1;
780 g_NumVideoModes++;
781 }
782 else
783 {
784 /* We need to alternate mode index entry for a pending mode change,
785 * else windows will ignore actual mode change call.
786 * Only alternate index if one of mode parameters changed and
787 * regardless of conditions always add 2 entries to the table.
788 */
789 static int s_InvocationCounter=0;
790 BOOLEAN bAlternativeIndex = FALSE;
791
792 static uint32_t s_Prev_xres=0;
793 static uint32_t s_Prev_yres=0;
794 static uint32_t s_Prev_bpp=0;
795 BOOLEAN bChanged = (s_Prev_xres!=specialMode.VisScreenWidth
796 || s_Prev_yres!=specialMode.VisScreenHeight
797 || s_Prev_bpp!=specialMode.BitsPerPlane);
798 if (bChanged)
799 {
800 s_Prev_xres = specialMode.VisScreenWidth;
801 s_Prev_yres = specialMode.VisScreenHeight;
802 s_Prev_bpp = specialMode.BitsPerPlane;
803 }
804
805 /* Make sure there's no other mode in the table with same parameters,
806 * because we need windows to pick up a new video mode index otherwise
807 * actual mode change wouldn't happen.
808 */
809 int iFoundIdx;
810 uint32_t uiStart=0;
811
812 while (0 <= (iFoundIdx = VBoxMPFindVideoMode(&g_VideoModes[uiStart], g_NumVideoModes-uiStart, &specialMode)))
813 {
814 memcpy(&g_VideoModes[uiStart+iFoundIdx], &g_VideoModes[2], sizeof(VIDEO_MODE_INFORMATION));
815 g_VideoModes[uiStart+iFoundIdx].ModeIndex = uiStart+iFoundIdx+1;
816 uiStart += iFoundIdx+1;
817 }
818
819 /* Check if we need to alternate the index */
820 if (!VBoxMPIsStartingUp(pExt, pExt->iDevice))
821 {
822 if (bChanged)
823 {
824 s_InvocationCounter++;
825 }
826
827 if (s_InvocationCounter % 2)
828 {
829 bAlternativeIndex = TRUE;
830 memcpy(&g_VideoModes[g_NumVideoModes], &g_VideoModes[2], sizeof(VIDEO_MODE_INFORMATION));
831 g_VideoModes[g_NumVideoModes].ModeIndex = g_NumVideoModes+1;
832 ++g_NumVideoModes;
833 }
834 }
835
836 LOG(("add special mode[%d] %dx%d:%d for display %d (bChanged=%d, bAlretnativeIndex=%d)",
837 g_NumVideoModes, specialMode.VisScreenWidth, specialMode.VisScreenHeight, specialMode.BitsPerPlane,
838 pExt->iDevice, bChanged, bAlternativeIndex));
839
840 /* Add special mode to the table
841 * Note: Y offset isn't used for a host-supplied modes
842 */
843 specialMode.ModeIndex = g_NumVideoModes+1;
844 memcpy(&g_VideoModes[g_NumVideoModes], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
845 ++g_NumVideoModes;
846
847 /* Save special mode in the custom modes table */
848 memcpy(&g_CustomVideoModes[pExt->iDevice], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
849
850
851 /* Make sure we've added 2nd mode if necessary to maintain table size */
852 if (VBoxMPIsStartingUp(pExt, pExt->iDevice))
853 {
854 memcpy(&g_VideoModes[g_NumVideoModes], &g_VideoModes[g_NumVideoModes-1], sizeof(VIDEO_MODE_INFORMATION));
855 g_VideoModes[g_NumVideoModes].ModeIndex = g_NumVideoModes+1;
856 ++g_NumVideoModes;
857 }
858 else if (!bAlternativeIndex)
859 {
860 memcpy(&g_VideoModes[g_NumVideoModes], &g_VideoModes[2], sizeof(VIDEO_MODE_INFORMATION));
861 g_VideoModes[g_NumVideoModes].ModeIndex = g_NumVideoModes+1;
862 ++g_NumVideoModes;
863 }
864
865 /* Save special mode info to registry */
866 VBoxMPRegSaveModeInfo(pExt, pExt->iDevice, &specialMode);
867 }
868
869#if defined(LOG_ENABLED)
870 do
871 {
872 LOG(("Filled %d modes", g_NumVideoModes));
873
874 for (uint32_t i=0; i<g_NumVideoModes; ++i)
875 {
876 LOG(("Mode[%2d]: %4dx%4d:%2d (idx=%d)",
877 i, g_VideoModes[i].VisScreenWidth, g_VideoModes[i].VisScreenHeight,
878 g_VideoModes[i].BitsPerPlane, g_VideoModes[i].ModeIndex));
879 }
880 } while (0);
881#endif
882}
883#endif /*VBOX_XPDM_MINIPORT*/
884
885#ifdef VBOX_WDDM_MINIPORT
886static VBOXWDDM_VIDEOMODES_INFO g_aVBoxVideoModeInfos[VBOX_VIDEO_MAX_SCREENS] = {0};
887
888bool VBoxWddmFillMode(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, VIDEO_MODE_INFORMATION *pInfo, D3DDDIFORMAT enmFormat, ULONG w, ULONG h)
889{
890 switch (enmFormat)
891 {
892 case D3DDDIFMT_A8R8G8B8:
893 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 32))
894 {
895 WARN(("unsupported mode info for format(%d)", enmFormat));
896 return false;
897 }
898 VBoxFillVidModeInfo(pInfo, w, h, 32, 0, 0);
899 return true;
900 case D3DDDIFMT_R8G8B8:
901 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 24))
902 {
903 WARN(("unsupported mode info for format(%d)", enmFormat));
904 return false;
905 }
906 VBoxFillVidModeInfo(pInfo, w, h, 24, 0, 0);
907 return true;
908 case D3DDDIFMT_R5G6B5:
909 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 16))
910 {
911 WARN(("unsupported mode info for format(%d)", enmFormat));
912 return false;
913 }
914 VBoxFillVidModeInfo(pInfo, w, h, 16, 0, 0);
915 return true;
916 case D3DDDIFMT_P8:
917 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, w, h, 8))
918 {
919 WARN(("unsupported mode info for format(%d)", enmFormat));
920 return false;
921 }
922 VBoxFillVidModeInfo(pInfo, w, h, 8, 0, 0);
923 return true;
924 default:
925 WARN(("unsupported enmFormat(%d)", enmFormat));
926 AssertBreakpoint();
927 break;
928 }
929
930 return false;
931}
932
933static void
934VBoxWddmBuildResolutionTable(PVIDEO_MODE_INFORMATION pModesTable, size_t tableSize, int iPreferredMode,
935 SIZE *pResolutions, uint32_t * pcResolutions, int *piPreferredResolution)
936{
937 uint32_t cResolutionsArray = *pcResolutions;
938 uint32_t cResolutions = 0;
939
940 *piPreferredResolution = -1;
941
942 for (uint32_t i=0; i<tableSize; ++i)
943 {
944 PVIDEO_MODE_INFORMATION pMode = &pModesTable[i];
945 int iResolution = -1;
946
947 for (uint32_t j=0; j<cResolutions; ++j)
948 {
949 if (pResolutions[j].cx == pMode->VisScreenWidth
950 && pResolutions[j].cy == pMode->VisScreenHeight)
951 {
952 iResolution = j;
953 break;
954 }
955 }
956
957 if (iResolution < 0)
958 {
959 if (cResolutions == cResolutionsArray)
960 {
961 WARN(("table overflow!"));
962 break;
963 }
964
965 iResolution = cResolutions;
966 pResolutions[cResolutions].cx = pMode->VisScreenWidth;
967 pResolutions[cResolutions].cy = pMode->VisScreenHeight;
968 ++cResolutions;
969 }
970
971 Assert(iResolution >= 0);
972 if (i == iPreferredMode)
973 {
974 Assert(*piPreferredResolution == -1);
975 *piPreferredResolution = iResolution;
976 }
977 }
978
979 *pcResolutions = cResolutions;
980 Assert(*piPreferredResolution >= 0);
981}
982
983static void VBoxWddmBuildResolutionTableForModes(PVBOXWDDM_VIDEOMODES_INFO pModes)
984{
985 pModes->cResolutions = RT_ELEMENTS(pModes->aResolutions);
986 VBoxWddmBuildResolutionTable(pModes->aModes, pModes->cModes, pModes->iPreferredMode,
987 (SIZE*)((void*)pModes->aResolutions), &pModes->cResolutions, &pModes->iPreferredResolution);
988 Assert(pModes->aResolutions[pModes->iPreferredResolution].cx == pModes->aModes[pModes->iPreferredMode].VisScreenWidth
989 && pModes->aResolutions[pModes->iPreferredResolution].cy == pModes->aModes[pModes->iPreferredMode].VisScreenHeight);
990}
991
992AssertCompile(sizeof (SIZE) == sizeof (D3DKMDT_2DREGION));
993AssertCompile(RT_OFFSETOF(SIZE, cx) == RT_OFFSETOF(D3DKMDT_2DREGION, cx));
994AssertCompile(RT_OFFSETOF(SIZE, cy) == RT_OFFSETOF(D3DKMDT_2DREGION, cy));
995static void
996VBoxWddmBuildVideoModesInfo(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId,
997 PVBOXWDDM_VIDEOMODES_INFO pModes, VIDEO_MODE_INFORMATION *paAddlModes,
998 UINT cAddlModes)
999{
1000 pModes->cResolutions = RT_ELEMENTS(pModes->aResolutions);
1001
1002 /* Add default modes and ones read from registry. */
1003 pModes->cModes = VBoxMPFillModesTable(pExt, VidPnTargetId, pModes->aModes, RT_ELEMENTS(pModes->aModes), &pModes->iPreferredMode);
1004 Assert(pModes->cModes<=RT_ELEMENTS(pModes->aModes));
1005
1006 if (!VBoxMPIsStartingUp(pExt, VidPnTargetId))
1007 {
1008 /* make sure we keep the current mode to avoid mode flickering */
1009 PVBOXWDDM_ALLOC_DATA pAllocData = pExt->aSources[VidPnTargetId].pPrimaryAllocation ?
1010 &pExt->aSources[VidPnTargetId].pPrimaryAllocation->AllocData
1011 : &pExt->aSources[VidPnTargetId].AllocData;
1012 if (pModes->cModes < RT_ELEMENTS(pModes->aModes))
1013 {
1014 /* VBox WDDM driver does not allow 24 modes since OS could choose the 24bit mode as default in that case,
1015 * the pExt->aSources[iDisplay].AllocData.SurfDesc.bpp could be initially 24 though,
1016 * i.e. when driver occurs the current mode on driver load via DxgkCbAcquirePostDisplayOwnership
1017 * and until driver reports the supported modes
1018 * This is true for Win8 Display-Only driver currently since DxgkCbAcquirePostDisplayOwnership is only used by it
1019 *
1020 * This is why we check the bpp to be supported here and add the current mode to the list only in that case */
1021 if (VBoxMPIsSupportedBpp(pAllocData->SurfDesc.bpp))
1022 {
1023 int foundIdx;
1024 VBoxFillVidModeInfo(&pModes->aModes[pModes->cModes], pAllocData->SurfDesc.width, pAllocData->SurfDesc.height, pAllocData->SurfDesc.bpp, 1/*index*/, 0);
1025 if ((foundIdx=VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]))>=0)
1026 {
1027 pModes->iPreferredMode = foundIdx;
1028 }
1029 else
1030 {
1031 pModes->iPreferredMode = pModes->cModes;
1032 ++pModes->cModes;
1033 }
1034
1035#ifdef VBOX_WITH_8BPP_MODES
1036 int bytesPerPixel=1;
1037#else
1038 int bytesPerPixel=2;
1039#endif
1040 for (; bytesPerPixel<=4; bytesPerPixel++)
1041 {
1042 int bpp = 8*bytesPerPixel;
1043
1044 if (bpp == pAllocData->SurfDesc.bpp)
1045 continue;
1046
1047 if (!VBoxMPValidateVideoModeParamsGuest(pExt, VidPnTargetId,
1048 pAllocData->SurfDesc.width, pAllocData->SurfDesc.height,
1049 bpp))
1050 continue;
1051
1052 if (pModes->cModes >= RT_ELEMENTS(pModes->aModes))
1053 {
1054 WARN(("ran out of video modes 2"));
1055 break;
1056 }
1057
1058 VBoxFillVidModeInfo(&pModes->aModes[pModes->cModes],
1059 pAllocData->SurfDesc.width, pAllocData->SurfDesc.height,
1060 bpp, pModes->cModes, 0);
1061 if (VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]) < 0)
1062 {
1063 ++pModes->cModes;
1064 }
1065 }
1066 }
1067 }
1068 else
1069 {
1070 WARN(("ran out of video modes 1"));
1071 }
1072 }
1073
1074 /* Check if there's a pending display change request for this adapter */
1075 VIDEO_MODE_INFORMATION specialMode;
1076 if (VBoxMPCheckPendingVideoMode(pExt, &specialMode) && (specialMode.ModeIndex==VidPnTargetId))
1077 {
1078 /*Minor hack, ModeIndex!=0 Means this mode has been validated already and not just read from registry */
1079 specialMode.ModeIndex = 1;
1080 memcpy(&g_CustomVideoModes[VidPnTargetId], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
1081
1082 /* Save mode to registry */
1083 VBoxMPRegSaveModeInfo(pExt, VidPnTargetId, &specialMode);
1084 }
1085
1086 /* Validate the mode which has been read from registry */
1087 if (!g_CustomVideoModes[VidPnTargetId].ModeIndex)
1088 {
1089 uint32_t xres, yres, bpp;
1090
1091 xres = g_CustomVideoModes[VidPnTargetId].VisScreenWidth;
1092 yres = g_CustomVideoModes[VidPnTargetId].VisScreenHeight;
1093 bpp = g_CustomVideoModes[VidPnTargetId].BitsPerPlane;
1094
1095 if (VBoxMPValidateVideoModeParams(pExt, VidPnTargetId, xres, yres, bpp))
1096 {
1097 VBoxFillVidModeInfo(&g_CustomVideoModes[VidPnTargetId], xres, yres, bpp, 1/*index*/, 0);
1098 Assert(g_CustomVideoModes[VidPnTargetId].ModeIndex == 1);
1099 }
1100 }
1101
1102 /* Add custom mode to the table */
1103 if (g_CustomVideoModes[VidPnTargetId].ModeIndex)
1104 {
1105 if (RT_ELEMENTS(pModes->aModes) > pModes->cModes)
1106 {
1107 g_CustomVideoModes[VidPnTargetId].ModeIndex = pModes->cModes;
1108 pModes->aModes[pModes->cModes] = g_CustomVideoModes[VidPnTargetId];
1109
1110 /* Check if we already have this mode in the table */
1111 int foundIdx;
1112 if ((foundIdx=VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]))>=0)
1113 {
1114 pModes->iPreferredMode = foundIdx;
1115 }
1116 else
1117 {
1118 pModes->iPreferredMode = pModes->cModes;
1119 ++pModes->cModes;
1120 }
1121
1122 /* Add other bpp modes for this custom resolution */
1123#ifdef VBOX_WITH_8BPP_MODES
1124 UINT bpp=8;
1125#else
1126 UINT bpp=16;
1127#endif
1128 for (; bpp<=32; bpp+=8)
1129 {
1130 if (RT_ELEMENTS(pModes->aModes) == pModes->cModes)
1131 {
1132 WARN(("table full, can't add other bpp for specail mode!"));
1133#ifdef DEBUG_misha
1134 /* this is definitely something we do not expect */
1135 AssertFailed();
1136#endif
1137 break;
1138 }
1139
1140 AssertRelease(RT_ELEMENTS(pModes->aModes) > pModes->cModes); /* if not - the driver state is screwed up, @todo: better do KeBugCheckEx here */
1141
1142 if (pModes->aModes[pModes->iPreferredMode].BitsPerPlane == bpp)
1143 continue;
1144
1145 if (!VBoxMPValidateVideoModeParamsGuest(pExt, VidPnTargetId,
1146 pModes->aModes[pModes->iPreferredMode].VisScreenWidth,
1147 pModes->aModes[pModes->iPreferredMode].VisScreenHeight,
1148 bpp))
1149 continue;
1150
1151 VBoxFillVidModeInfo(&pModes->aModes[pModes->cModes],
1152 pModes->aModes[pModes->iPreferredMode].VisScreenWidth,
1153 pModes->aModes[pModes->iPreferredMode].VisScreenHeight,
1154 bpp, pModes->cModes, 0);
1155 if (VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &pModes->aModes[pModes->cModes]) < 0)
1156 {
1157 ++pModes->cModes;
1158 }
1159 }
1160 }
1161 else
1162 {
1163 AssertRelease(RT_ELEMENTS(pModes->aModes) == pModes->cModes); /* if not - the driver state is screwed up, @todo: better do KeBugCheckEx here */
1164 WARN(("table full, can't add video mode for a host request!"));
1165#ifdef DEBUG_misha
1166 /* this is definitely something we do not expect */
1167 AssertFailed();
1168#endif
1169 }
1170 }
1171
1172 /* Check and Add additional modes passed in paAddlModes */
1173 for (UINT i=0; i<cAddlModes; ++i)
1174 {
1175 if (RT_ELEMENTS(pModes->aModes) == pModes->cModes)
1176 {
1177 WARN(("table full, can't add addl modes!"));
1178#ifdef DEBUG_misha
1179 /* this is definitely something we do not expect */
1180 AssertFailed();
1181#endif
1182 break;
1183 }
1184
1185 AssertRelease(RT_ELEMENTS(pModes->aModes) > pModes->cModes); /* if not - the driver state is screwed up, @todo: better do KeBugCheckEx here */
1186
1187 if (!pExt->fAnyX)
1188 {
1189 paAddlModes[i].VisScreenWidth &= 0xFFF8;
1190 }
1191
1192 if (VBoxLikesVideoMode(VidPnTargetId, paAddlModes[i].VisScreenWidth, paAddlModes[i].VisScreenHeight, paAddlModes[i].BitsPerPlane))
1193 {
1194 int foundIdx;
1195 if ((foundIdx=VBoxMPFindVideoMode(pModes->aModes, pModes->cModes, &paAddlModes[i]))>=0)
1196 {
1197 pModes->iPreferredMode = foundIdx;
1198 }
1199 else
1200 {
1201 memcpy(&pModes->aModes[pModes->cModes], &paAddlModes[i], sizeof(VIDEO_MODE_INFORMATION));
1202 pModes->aModes[pModes->cModes].ModeIndex = pModes->cModes;
1203 ++pModes->cModes;
1204 }
1205 }
1206 }
1207
1208 /* Build resolution table */
1209 VBoxWddmBuildResolutionTableForModes(pModes);
1210}
1211
1212void VBoxWddmInvalidateVideoModesInfo(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId)
1213{
1214 if (VidPnTargetId != D3DDDI_ID_ALL)
1215 {
1216 if (VidPnTargetId >= RT_ELEMENTS(g_aVBoxVideoModeInfos))
1217 {
1218 WARN(("VidPnTargetId (%d) must be less than (%d)", VidPnTargetId, RT_ELEMENTS(g_aVBoxVideoModeInfos)));
1219 return;
1220 }
1221 LOG(("invalidating videomede info for screen %d", VidPnTargetId));
1222 g_aVBoxVideoModeInfos[VidPnTargetId].cModes = 0;
1223 return;
1224 }
1225
1226 LOG(("invalidating ALL videomede infos"));
1227
1228 for (UINT i = 0; i < RT_ELEMENTS(g_aVBoxVideoModeInfos); ++i)
1229 {
1230 g_aVBoxVideoModeInfos[i].cModes = 0;
1231 }
1232}
1233
1234void VBoxWddmInvalidateAllVideoModesInfos(PVBOXMP_DEVEXT pExt)
1235{
1236 VBoxWddmInvalidateVideoModesInfo(pExt, D3DDDI_ID_ALL);
1237}
1238
1239PVBOXWDDM_VIDEOMODES_INFO VBoxWddmUpdateVideoModesInfoByMask(PVBOXMP_DEVEXT pExt, uint8_t *pScreenIdMask)
1240{
1241 for (int i = 0; i < VBoxCommonFromDeviceExt(pExt)->cDisplays; ++i)
1242 {
1243 if (ASMBitTest(pScreenIdMask, i))
1244 VBoxWddmInvalidateVideoModesInfo(pExt, i);
1245 }
1246
1247 /* ensure we have all the rest populated */
1248 VBoxWddmGetAllVideoModesInfos(pExt);
1249 return g_aVBoxVideoModeInfos;
1250}
1251
1252PVBOXWDDM_VIDEOMODES_INFO VBoxWddmUpdateAllVideoModesInfos(PVBOXMP_DEVEXT pExt)
1253{
1254 VBoxWddmInvalidateAllVideoModesInfos(pExt);
1255
1256 /* ensure we have all the rest populated */
1257 VBoxWddmGetAllVideoModesInfos(pExt);
1258 return g_aVBoxVideoModeInfos;
1259}
1260
1261void VBoxWddmAdjustMode(PVBOXMP_DEVEXT pExt, PVBOXWDDM_ADJUSTVIDEOMODE pMode)
1262{
1263 pMode->fFlags = 0;
1264
1265 if (pMode->Mode.Id >= (UINT)VBoxCommonFromDeviceExt(pExt)->cDisplays)
1266 {
1267 WARN(("invalid screen id (%d)", pMode->Mode.Id));
1268 pMode->fFlags = VBOXWDDM_ADJUSTVIDEOMODE_F_INVALISCREENID;
1269 return;
1270 }
1271
1272 PVBOXWDDM_TARGET pTarget = &pExt->aTargets[pMode->Mode.Id];
1273 /* @todo: this info should go from the target actually */
1274 PVBOXWDDM_SOURCE pSource = &pExt->aSources[pMode->Mode.Id];
1275
1276 UINT newWidth = pMode->Mode.Width;
1277 UINT newHeight = pMode->Mode.Height;
1278 UINT newBpp = pMode->Mode.BitsPerPixel;
1279
1280 if (!VBoxMPValidateVideoModeParams(pExt, pMode->Mode.Id, newWidth, newHeight, newBpp))
1281 {
1282 PVBOXWDDM_SOURCE pSource = &pExt->aSources[pMode->Mode.Id];
1283 pMode->fFlags |= VBOXWDDM_ADJUSTVIDEOMODE_F_UNSUPPORTED;
1284 }
1285
1286 if (pMode->Mode.Width != newWidth
1287 || pMode->Mode.Height != newHeight
1288 || pMode->Mode.BitsPerPixel != newBpp)
1289 {
1290 pMode->fFlags |= VBOXWDDM_ADJUSTVIDEOMODE_F_ADJUSTED;
1291 pMode->Mode.Width = newWidth;
1292 pMode->Mode.Height = newHeight;
1293 pMode->Mode.BitsPerPixel = newBpp;
1294 }
1295
1296 if (pTarget->HeightVisible /* <- active */
1297 && pSource->AllocData.SurfDesc.width == pMode->Mode.Width
1298 && pSource->AllocData.SurfDesc.height == pMode->Mode.Height
1299 && pSource->AllocData.SurfDesc.bpp == pMode->Mode.BitsPerPixel)
1300 {
1301 pMode->fFlags |= VBOXWDDM_ADJUSTVIDEOMODE_F_CURRENT;
1302 if (pMode->fFlags & VBOXWDDM_ADJUSTVIDEOMODE_F_UNSUPPORTED)
1303 {
1304 WARN(("current mode is reported as unsupported"));
1305 }
1306 }
1307}
1308
1309void VBoxWddmAdjustModes(PVBOXMP_DEVEXT pExt, uint32_t cModes, PVBOXWDDM_ADJUSTVIDEOMODE aModes)
1310{
1311 for (UINT i = 0; i < cModes; ++i)
1312 {
1313 PVBOXWDDM_ADJUSTVIDEOMODE pMode = &aModes[i];
1314 VBoxWddmAdjustMode(pExt, pMode);
1315 }
1316}
1317
1318NTSTATUS VBoxWddmGetModesForResolution(VIDEO_MODE_INFORMATION *pAllModes, uint32_t cAllModes, int iSearchPreferredMode,
1319 const D3DKMDT_2DREGION *pResolution, VIDEO_MODE_INFORMATION * pModes, uint32_t cModes, uint32_t *pcModes, int32_t *piPreferrableMode)
1320{
1321 NTSTATUS Status = STATUS_SUCCESS;
1322 uint32_t cFound = 0;
1323 int iFoundPreferrableMode = -1;
1324 for (uint32_t i = 0; i < cAllModes; ++i)
1325 {
1326 VIDEO_MODE_INFORMATION *pCur = &pAllModes[i];
1327 if (pResolution->cx == pCur->VisScreenWidth
1328 && pResolution->cy == pCur->VisScreenHeight)
1329 {
1330 if (pModes && cModes > cFound)
1331 memcpy(&pModes[cFound], pCur, sizeof(VIDEO_MODE_INFORMATION));
1332 else
1333 Status = STATUS_BUFFER_TOO_SMALL;
1334
1335 if (i == iSearchPreferredMode)
1336 iFoundPreferrableMode = cFound;
1337
1338 ++cFound;
1339 }
1340 }
1341
1342 Assert(iFoundPreferrableMode < 0 || cFound > (uint32_t)iFoundPreferrableMode);
1343
1344 *pcModes = cFound;
1345 if (piPreferrableMode)
1346 *piPreferrableMode = iFoundPreferrableMode;
1347
1348 return Status;
1349}
1350
1351static PVBOXWDDM_VIDEOMODES_INFO vboxWddmGetVideoModesInfoInternal(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId)
1352{
1353 Assert(VidPnTargetId < (D3DDDI_VIDEO_PRESENT_TARGET_ID)VBoxCommonFromDeviceExt(pExt)->cDisplays);
1354 if (VidPnTargetId >= (D3DDDI_VIDEO_PRESENT_TARGET_ID)VBoxCommonFromDeviceExt(pExt)->cDisplays)
1355 {
1356 WARN(("video mode info for invalid screen (%d) requested", VidPnTargetId));
1357 return NULL;
1358 }
1359
1360 PVBOXWDDM_VIDEOMODES_INFO pInfo = &g_aVBoxVideoModeInfos[VidPnTargetId];
1361
1362 if (!pInfo->cModes)
1363 {
1364 VBoxWddmBuildVideoModesInfo(pExt, VidPnTargetId, pInfo, NULL, 0);
1365 Assert(pInfo->cModes);
1366 }
1367
1368 return pInfo;
1369}
1370
1371static VOID vboxWddmAddVideoModes(PVBOXMP_DEVEXT pExt, PVBOXWDDM_VIDEOMODES_INFO pDstInfo, PVBOXWDDM_VIDEOMODES_INFO pSrcInfo)
1372{
1373 for (int i = 0; i < (int)pSrcInfo->cModes; ++i)
1374 {
1375 int foundIdx = VBoxMPFindVideoMode(pDstInfo->aModes, pDstInfo->cModes, &pSrcInfo->aModes[i]);
1376 if (foundIdx >= 0)
1377 continue;
1378
1379 Assert(0);
1380 pDstInfo->aModes[pDstInfo->cModes] = pSrcInfo->aModes[i];
1381 ++pDstInfo->cModes;
1382 }
1383
1384 VBoxWddmBuildResolutionTableForModes(pDstInfo);
1385}
1386
1387PVBOXWDDM_VIDEOMODES_INFO VBoxWddmGetAllVideoModesInfos(PVBOXMP_DEVEXT pExt)
1388{
1389 /* ensure all modes are initialized */
1390 for (int i = 0; i < VBoxCommonFromDeviceExt(pExt)->cDisplays; ++i)
1391 {
1392 vboxWddmGetVideoModesInfoInternal(pExt, (D3DDDI_VIDEO_PRESENT_TARGET_ID)i);
1393 }
1394
1395 return g_aVBoxVideoModeInfos;
1396}
1397
1398PVBOXWDDM_VIDEOMODES_INFO VBoxWddmGetVideoModesInfo(PVBOXMP_DEVEXT pExt, D3DDDI_VIDEO_PRESENT_TARGET_ID VidPnTargetId)
1399{
1400 return &VBoxWddmGetAllVideoModesInfos(pExt)[VidPnTargetId];
1401}
1402
1403#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