VirtualBox

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

Last change on this file since 65193 was 65193, checked in by vboxsync, 8 years ago

bugref:8282: Additions/linux: submit DRM driver to the Linux kernel: revert r112542 again, this does not seem to be the right direction for reorganisation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.8 KB
Line 
1/* $Id: VBoxMPVidModes.cpp 65193 2017-01-08 14:01:47Z vboxsync $ */
2/** @file
3 * VBox Miniport video modes related functions
4 */
5
6/*
7 * Copyright (C) 2011-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "VBoxMPCommon.h"
19
20#if _MSC_VER >= 1400 /* bird: MS fixed swprintf to be standard-conforming... */
21#define _INC_SWPRINTF_INL_
22extern "C" int __cdecl swprintf(wchar_t *, const wchar_t *, ...);
23#endif
24#include <wchar.h>
25#include <VBox/Hardware/VBoxVideoVBE.h>
26
27#ifdef VBOX_WITH_WDDM
28# define VBOX_WITHOUT_24BPP_MODES
29#endif
30
31/* Custom video modes which are being read from registry at driver startup. */
32static VIDEO_MODE_INFORMATION g_CustomVideoModes[VBOX_VIDEO_MAX_SCREENS] = { 0 };
33
34static BOOLEAN
35VBoxMPValidateVideoModeParamsGuest(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, uint32_t xres, uint32_t yres, uint32_t bpp)
36{
37 RT_NOREF(iDisplay, xres, yres);
38
39 switch (bpp)
40 {
41 case 32:
42 break;
43 case 24:
44#ifdef VBOX_WITHOUT_24BPP_MODES
45 return FALSE;
46#else
47 break;
48#endif
49 case 16:
50 break;
51 case 8:
52#ifndef VBOX_WITH_8BPP_MODES
53 return FALSE;
54#else
55#ifdef VBOX_XPDM_MINIPORT
56 if (pExt->iDevice != 0) /* Secondary monitors do not support 8 bit */
57 return FALSE;
58#endif
59 break;
60#endif
61 default:
62 WARN(("Unexpected bpp (%d)", bpp));
63 return FALSE;
64 }
65 return TRUE;
66}
67
68/* Fills given video mode BPP related fields */
69static void
70VBoxFillVidModeBPP(VIDEO_MODE_INFORMATION *pMode, ULONG bitsR, ULONG bitsG, ULONG bitsB,
71 ULONG maskR, ULONG maskG, ULONG maskB)
72{
73 pMode->NumberRedBits = bitsR;
74 pMode->NumberGreenBits = bitsG;
75 pMode->NumberBlueBits = bitsB;
76 pMode->RedMask = maskR;
77 pMode->GreenMask = maskG;
78 pMode->BlueMask = maskB;
79}
80
81/* Fills given video mode structure */
82static void
83VBoxFillVidModeInfo(VIDEO_MODE_INFORMATION *pMode, ULONG xres, ULONG yres, ULONG bpp, ULONG index, ULONG yoffset)
84{
85 LOGF(("%dx%d:%d (idx=%d, yoffset=%d)", xres, yres, bpp, index, yoffset));
86
87 memset(pMode, 0, sizeof(VIDEO_MODE_INFORMATION));
88
89 /*Common entries*/
90 pMode->Length = sizeof(VIDEO_MODE_INFORMATION);
91 pMode->ModeIndex = index;
92 pMode->VisScreenWidth = xres;
93 pMode->VisScreenHeight = yres - yoffset;
94 pMode->ScreenStride = xres * ((bpp + 7) / 8);
95 pMode->NumberOfPlanes = 1;
96 pMode->BitsPerPlane = bpp;
97 pMode->Frequency = 60;
98 pMode->XMillimeter = 320;
99 pMode->YMillimeter = 240;
100 pMode->VideoMemoryBitmapWidth = xres;
101 pMode->VideoMemoryBitmapHeight = yres - yoffset;
102 pMode->DriverSpecificAttributeFlags = 0;
103 pMode->AttributeFlags = VIDEO_MODE_GRAPHICS | VIDEO_MODE_COLOR | VIDEO_MODE_NO_OFF_SCREEN;
104
105 /*BPP related entries*/
106 switch (bpp)
107 {
108#ifdef VBOX_WITH_8BPP_MODES
109 case 8:
110 VBoxFillVidModeBPP(pMode, 6, 6, 6, 0, 0, 0);
111
112 pMode->AttributeFlags |= VIDEO_MODE_PALETTE_DRIVEN | VIDEO_MODE_MANAGED_PALETTE;
113 break;
114#endif
115 case 16:
116 VBoxFillVidModeBPP(pMode, 5, 6, 5, 0xF800, 0x7E0, 0x1F);
117 break;
118 case 24:
119 case 32:
120 VBoxFillVidModeBPP(pMode, 8, 8, 8, 0xFF0000, 0xFF00, 0xFF);
121 break;
122 default:
123 Assert(0);
124 break;
125 }
126}
127
128void VBoxMPCmnInitCustomVideoModes(PVBOXMP_DEVEXT pExt)
129{
130 VBOXMPCMNREGISTRY Registry;
131 VP_STATUS rc;
132 int iMode;
133
134 LOGF_ENTER();
135
136 rc = VBoxMPCmnRegInit(pExt, &Registry);
137 VBOXMP_WARN_VPS(rc);
138
139 /* Initialize all custom modes to the 800x600x32 */
140 VBoxFillVidModeInfo(&g_CustomVideoModes[0], 800, 600, 32, 0, 0);
141 for (iMode=1; iMode<RT_ELEMENTS(g_CustomVideoModes); ++iMode)
142 {
143 g_CustomVideoModes[iMode] = g_CustomVideoModes[0];
144 }
145
146 /* Read stored custom resolution info from registry */
147 for (iMode=0; iMode<VBoxCommonFromDeviceExt(pExt)->cDisplays; ++iMode)
148 {
149 uint32_t CustomXRes = 0, CustomYRes = 0, CustomBPP = 0;
150
151 if (iMode==0)
152 {
153 /*First name without a suffix*/
154 rc = VBoxMPCmnRegQueryDword(Registry, L"CustomXRes", &CustomXRes);
155 VBOXMP_WARN_VPS_NOBP(rc);
156 rc = VBoxMPCmnRegQueryDword(Registry, L"CustomYRes", &CustomYRes);
157 VBOXMP_WARN_VPS_NOBP(rc);
158 rc = VBoxMPCmnRegQueryDword(Registry, L"CustomBPP", &CustomBPP);
159 VBOXMP_WARN_VPS_NOBP(rc);
160 }
161 else
162 {
163 wchar_t keyname[32];
164 swprintf(keyname, L"CustomXRes%d", iMode);
165 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomXRes);
166 VBOXMP_WARN_VPS_NOBP(rc);
167 swprintf(keyname, L"CustomYRes%d", iMode);
168 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomYRes);
169 VBOXMP_WARN_VPS_NOBP(rc);
170 swprintf(keyname, L"CustomBPP%d", iMode);
171 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomBPP);
172 VBOXMP_WARN_VPS_NOBP(rc);
173 }
174
175 LOG(("got stored custom resolution[%d] %dx%dx%d", iMode, CustomXRes, CustomYRes, CustomBPP));
176
177 if (CustomXRes || CustomYRes || CustomBPP)
178 {
179 if (CustomXRes == 0)
180 {
181 CustomXRes = g_CustomVideoModes[iMode].VisScreenWidth;
182 }
183 if (CustomYRes == 0)
184 {
185 CustomYRes = g_CustomVideoModes[iMode].VisScreenHeight;
186 }
187 if (CustomBPP == 0)
188 {
189 CustomBPP = g_CustomVideoModes[iMode].BitsPerPlane;
190 }
191
192 if (VBoxMPValidateVideoModeParamsGuest(pExt, iMode, CustomXRes, CustomYRes, CustomBPP))
193 {
194 VBoxFillVidModeInfo(&g_CustomVideoModes[iMode], CustomXRes, CustomYRes, CustomBPP, 0, 0);
195 }
196 }
197 }
198
199 rc = VBoxMPCmnRegFini(Registry);
200 VBOXMP_WARN_VPS(rc);
201 LOGF_LEAVE();
202}
203
204VIDEO_MODE_INFORMATION *VBoxMPCmnGetCustomVideoModeInfo(ULONG ulIndex)
205{
206 return (ulIndex<RT_ELEMENTS(g_CustomVideoModes)) ? &g_CustomVideoModes[ulIndex] : NULL;
207}
208
209#ifdef VBOX_XPDM_MINIPORT
210VIDEO_MODE_INFORMATION* VBoxMPCmnGetVideoModeInfo(PVBOXMP_DEVEXT pExt, ULONG ulIndex)
211{
212 return (ulIndex<RT_ELEMENTS(pExt->aVideoModes)) ? &pExt->aVideoModes[ulIndex] : NULL;
213}
214#endif
215
216static bool VBoxMPVideoModesMatch(const PVIDEO_MODE_INFORMATION pMode1, const PVIDEO_MODE_INFORMATION pMode2)
217{
218 return pMode1->VisScreenHeight == pMode2->VisScreenHeight
219 && pMode1->VisScreenWidth == pMode2->VisScreenWidth
220 && pMode1->BitsPerPlane == pMode2->BitsPerPlane;
221}
222
223static int
224VBoxMPFindVideoMode(const PVIDEO_MODE_INFORMATION pModesTable, int cModes, const PVIDEO_MODE_INFORMATION pMode)
225{
226 for (int i = 0; i < cModes; ++i)
227 {
228 if (VBoxMPVideoModesMatch(pMode, &pModesTable[i]))
229 {
230 return i;
231 }
232 }
233 return -1;
234}
235
236/* Helper function to dynamically build our table of standard video
237 * modes. We take the amount of VRAM and create modes with standard
238 * geometries until we've either reached the maximum number of modes
239 * or the available VRAM does not allow for additional modes.
240 * We also check registry for manually added video modes.
241 * Returns number of modes added to the table.
242 */
243static uint32_t
244VBoxMPFillModesTable(PVBOXMP_DEVEXT pExt, int iDisplay, PVIDEO_MODE_INFORMATION pModesTable, size_t tableSize,
245 int32_t *pPrefModeIdx)
246{
247 /* the resolution matrix */
248 struct
249 {
250 uint16_t xRes;
251 uint16_t yRes;
252 } resolutionMatrix[] =
253 {
254 /* standard modes */
255 { 640, 480 },
256 { 800, 600 },
257 { 1024, 768 },
258 { 1152, 864 },
259 { 1280, 960 },
260 { 1280, 1024 },
261 { 1400, 1050 },
262 { 1600, 1200 },
263 { 1920, 1440 },
264#ifndef VBOX_WITH_WDDM
265 /* multi screen modes with 1280x1024 */
266 { 2560, 1024 },
267 { 3840, 1024 },
268 { 5120, 1024 },
269 /* multi screen modes with 1600x1200 */
270 { 3200, 1200 },
271 { 4800, 1200 },
272 { 6400, 1200 },
273#endif
274 };
275
276#ifdef VBOX_XPDM_MINIPORT
277 ULONG vramSize = pExt->pPrimary->u.primary.ulMaxFrameBufferSize;
278#else
279 ULONG vramSize = vboxWddmVramCpuVisibleSegmentSize(pExt);
280 vramSize /= pExt->u.primary.commonInfo.cDisplays;
281# ifdef VBOX_WDDM_WIN8
282 if (!g_VBoxDisplayOnly)
283# endif
284 {
285 /* at least two surfaces will be needed: primary & shadow */
286 vramSize /= 2;
287 }
288 vramSize &= ~PAGE_OFFSET_MASK;
289#endif
290
291 uint32_t iMode=0, iPrefIdx=0;
292 /* there are 4 color depths: 8, 16, 24 and 32bpp and we reserve 50% of the modes for other sources */
293 size_t maxModesPerColorDepth = VBOXMP_MAX_VIDEO_MODES / 2 / 4;
294
295 /* Always add 800x600 video modes. Windows XP+ needs at least 800x600 resolution
296 * and fallbacks to 800x600x4bpp VGA mode if the driver did not report suitable modes.
297 * This resolution could be rejected by a low resolution host (netbooks, etc).
298 */
299#ifdef VBOX_WITH_8BPP_MODES
300 int bytesPerPixel=1;
301#else
302 int bytesPerPixel=2;
303#endif
304 for (; bytesPerPixel<=4; bytesPerPixel++)
305 {
306 int bitsPerPixel = 8*bytesPerPixel;
307
308 if (800*600*bytesPerPixel > (LONG)vramSize)
309 {
310 /* we don't have enough VRAM for this mode */
311 continue;
312 }
313
314 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iMode, 800, 600, bitsPerPixel))
315 continue;
316
317 VBoxFillVidModeInfo(&pModesTable[iMode], 800, 600, bitsPerPixel, iMode+1, 0);
318
319 if (32==bitsPerPixel)
320 {
321 iPrefIdx = iMode;
322 }
323 ++iMode;
324 }
325
326 /* Query yoffset from the host */
327 ULONG yOffset = VBoxGetHeightReduction();
328
329 /* Iterate through our static resolution table and add supported video modes for different bpp's */
330#ifdef VBOX_WITH_8BPP_MODES
331 bytesPerPixel=1;
332#else
333 bytesPerPixel=2;
334#endif
335 for (; bytesPerPixel<=4; bytesPerPixel++)
336 {
337 int bitsPerPixel = 8*bytesPerPixel;
338 size_t cAdded, resIndex;
339
340 for (cAdded=0, resIndex=0; resIndex<RT_ELEMENTS(resolutionMatrix) && cAdded<maxModesPerColorDepth; resIndex++)
341 {
342 if (resolutionMatrix[resIndex].xRes * resolutionMatrix[resIndex].yRes * bytesPerPixel > (LONG)vramSize)
343 {
344 /* we don't have enough VRAM for this mode */
345 continue;
346 }
347
348 if (yOffset == 0 && resolutionMatrix[resIndex].xRes == 800 && resolutionMatrix[resIndex].yRes == 600)
349 {
350 /* this mode was already added */
351 continue;
352 }
353
354 if (
355#ifdef VBOX_WDDM_MINIPORT
356 /* 1024x768 resolution is a minimal resolutions for win8 to make most metro apps run.
357 * For small host display resolutions, host will dislike the mode 1024x768 and above
358 * if the framebuffer window requires scrolling to fit the guest resolution.
359 * So add 1024x768 resolution for win8 guest to allow user switch to it */
360 ( (VBoxQueryWinVersion() != WIN8 && VBoxQueryWinVersion() != WIN81)
361 || resolutionMatrix[resIndex].xRes != 1024
362 || resolutionMatrix[resIndex].yRes != 768)
363 &&
364#endif
365 !VBoxLikesVideoMode(iDisplay, resolutionMatrix[resIndex].xRes,
366 resolutionMatrix[resIndex].yRes - yOffset, bitsPerPixel))
367 {
368 /* host doesn't like this mode */
369 continue;
370 }
371
372 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, resolutionMatrix[resIndex].xRes, resolutionMatrix[resIndex].yRes, bitsPerPixel))
373 {
374 /* guest does not like this mode */
375 continue;
376 }
377
378 /* Sanity check, we shouldn't ever get here */
379 if (iMode >= tableSize)
380 {
381 WARN(("video modes table overflow!"));
382 break;
383 }
384
385 VBoxFillVidModeInfo(&pModesTable[iMode], resolutionMatrix[resIndex].xRes, resolutionMatrix[resIndex].yRes, bitsPerPixel, iMode+1, yOffset);
386 ++iMode;
387 ++cAdded;
388 }
389 }
390
391 /* Check registry for manually added modes, up to 128 entries is supported
392 * Give up on the first error encountered.
393 */
394 VBOXMPCMNREGISTRY Registry;
395 int fPrefSet=0;
396 VP_STATUS rc;
397
398 rc = VBoxMPCmnRegInit(pExt, &Registry);
399 VBOXMP_WARN_VPS(rc);
400
401 for (int curKey=0; curKey<128; curKey++)
402 {
403 if (iMode>=tableSize)
404 {
405 WARN(("ignoring possible custom mode(s), table is full!"));
406 break;
407 }
408
409 wchar_t keyname[24];
410 uint32_t xres, yres, bpp = 0;
411
412 swprintf(keyname, L"CustomMode%dWidth", curKey);
413 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &xres);
414 VBOXMP_CHECK_VPS_BREAK(rc);
415
416 swprintf(keyname, L"CustomMode%dHeight", curKey);
417 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &yres);
418 VBOXMP_CHECK_VPS_BREAK(rc);
419
420 swprintf(keyname, L"CustomMode%dBPP", curKey);
421 rc = VBoxMPCmnRegQueryDword(Registry, keyname, &bpp);
422 VBOXMP_CHECK_VPS_BREAK(rc);
423
424 LOG(("got custom mode[%u]=%ux%u:%u", curKey, xres, yres, bpp));
425
426 /* round down width to be a multiple of 8 if necessary */
427 if (!VBoxCommonFromDeviceExt(pExt)->fAnyX)
428 {
429 xres &= 0xFFF8;
430 }
431
432 if ( (xres > (1 << 16))
433 || (yres > (1 << 16))
434 || ( (bpp != 16)
435 && (bpp != 24)
436 && (bpp != 32)))
437 {
438 /* incorrect values */
439 break;
440 }
441
442 /* does it fit within our VRAM? */
443 if (xres * yres * (bpp / 8) > vramSize)
444 {
445 /* we don't have enough VRAM for this mode */
446 break;
447 }
448
449 if (!VBoxLikesVideoMode(iDisplay, xres, yres, bpp))
450 {
451 /* host doesn't like this mode */
452 break;
453 }
454
455 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, xres, yres, bpp))
456 {
457 /* guest does not like this mode */
458 continue;
459 }
460
461 LOG(("adding video mode from registry."));
462
463 VBoxFillVidModeInfo(&pModesTable[iMode], xres, yres, bpp, iMode+1, yOffset);
464
465 if (!fPrefSet)
466 {
467 fPrefSet = 1;
468 iPrefIdx = iMode;
469 }
470#ifdef VBOX_WDDM_MINIPORT
471 /*check if the same mode has been added to the table already*/
472 int foundIdx = VBoxMPFindVideoMode(pModesTable, iMode, &pModesTable[iMode]);
473
474 if (foundIdx>=0)
475 {
476 if (iPrefIdx==iMode)
477 {
478 iPrefIdx=foundIdx;
479 }
480 }
481 else
482#endif
483 {
484 ++iMode;
485 }
486 }
487
488 rc = VBoxMPCmnRegFini(Registry);
489 VBOXMP_WARN_VPS(rc);
490
491 if (pPrefModeIdx)
492 {
493 *pPrefModeIdx = iPrefIdx;
494 }
495
496 return iMode;
497}
498
499/* Returns if we're in the first mode change, ie doesn't have valid video mode set yet */
500static BOOLEAN VBoxMPIsStartingUp(PVBOXMP_DEVEXT pExt, uint32_t iDisplay)
501{
502#ifdef VBOX_XPDM_MINIPORT
503 RT_NOREF(iDisplay);
504 return (pExt->CurrentMode == 0);
505#else
506 VBOXWDDM_SOURCE *pSource = &pExt->aSources[iDisplay];
507 return !pSource->AllocData.SurfDesc.width || !pSource->AllocData.SurfDesc.height;
508#endif
509}
510
511#ifdef VBOX_WDDM_MINIPORT
512static const uint32_t g_aVBoxVidModesSupportedBpps[] = {
513 32
514#ifndef VBOX_WITHOUT_24BPP_MODES
515 , 24
516#endif
517 , 16
518#ifdef VBOX_WITH_8BPP_MODES
519 , 8
520#endif
521};
522DECLINLINE(BOOLEAN) VBoxMPIsSupportedBpp(uint32_t bpp)
523{
524 for (int i = 0; i < RT_ELEMENTS(g_aVBoxVidModesSupportedBpps); ++i)
525 {
526 if (bpp == g_aVBoxVidModesSupportedBpps[i])
527 return TRUE;
528 }
529 return FALSE;
530}
531
532DECLINLINE(uint32_t) VBoxMPAdjustBpp(uint32_t bpp)
533{
534 if (VBoxMPIsSupportedBpp(bpp))
535 return bpp;
536 Assert(g_aVBoxVidModesSupportedBpps[0] == 32);
537 return g_aVBoxVidModesSupportedBpps[0];
538}
539#endif
540/* Updates missing video mode params with current values,
541 * Checks if resulting mode is liked by the host and fits into VRAM.
542 * Returns TRUE if resulting mode could be used.
543 */
544static BOOLEAN
545VBoxMPValidateVideoModeParams(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, uint32_t &xres, uint32_t &yres, uint32_t &bpp)
546{
547 /* Make sure all important video mode values are set */
548 if (VBoxMPIsStartingUp(pExt, iDisplay))
549 {
550 /* Use stored custom values only if nothing was read from host. */
551 xres = xres ? xres:g_CustomVideoModes[iDisplay].VisScreenWidth;
552 yres = yres ? yres:g_CustomVideoModes[iDisplay].VisScreenHeight;
553 bpp = bpp ? bpp :g_CustomVideoModes[iDisplay].BitsPerPlane;
554 }
555 else
556 {
557 /* Use current values for field which weren't read from host. */
558#ifdef VBOX_XPDM_MINIPORT
559 xres = xres ? xres:pExt->CurrentModeWidth;
560 yres = yres ? yres:pExt->CurrentModeHeight;
561 bpp = bpp ? bpp :pExt->CurrentModeBPP;
562#else
563 PVBOXWDDM_ALLOC_DATA pAllocData = pExt->aSources[iDisplay].pPrimaryAllocation ?
564 &pExt->aSources[iDisplay].pPrimaryAllocation->AllocData
565 : &pExt->aSources[iDisplay].AllocData;
566 xres = xres ? xres:pAllocData->SurfDesc.width;
567 yres = yres ? yres:pAllocData->SurfDesc.height;
568 /* VBox WDDM driver does not allow 24 modes since OS could choose the 24bit mode as default in that case,
569 * the pExt->aSources[iDisplay].AllocData.SurfDesc.bpp could be initially 24 though,
570 * i.e. when driver occurs the current mode on driver load via DxgkCbAcquirePostDisplayOwnership
571 * and until driver reports the supported modes
572 * This is true for Win8 Display-Only driver currently since DxgkCbAcquirePostDisplayOwnership is only used by it
573 *
574 * This is why we need to adjust the current mode bpp to the value we actually report as supported */
575 bpp = bpp ? bpp : VBoxMPAdjustBpp(pAllocData->SurfDesc.bpp);
576#endif
577 }
578
579 /* Round down width to be a multiple of 8 if necessary */
580 if (!VBoxCommonFromDeviceExt(pExt)->fAnyX)
581 {
582 xres &= 0xFFF8;
583 }
584
585 /* We always need bpp to be set */
586 if (!bpp)
587 {
588 bpp=32;
589 }
590
591 if (!VBoxMPValidateVideoModeParamsGuest(pExt, iDisplay, xres, yres, bpp))
592 {
593 WARN(("GUEST does not like special mode %dx%d:%d for display %d", xres, yres, bpp, iDisplay));
594 return FALSE;
595 }
596
597 /* Check if host likes this mode */
598 if (!VBoxLikesVideoMode(iDisplay, xres, yres, bpp))
599 {
600 WARN_NOBP(("HOST does not like special mode %dx%d:%d for display %d", xres, yres, bpp, iDisplay));
601 return FALSE;
602 }
603
604#ifdef VBOX_XPDM_MINIPORT
605 ULONG vramSize = pExt->pPrimary->u.primary.ulMaxFrameBufferSize;
606#else
607 ULONG vramSize = vboxWddmVramCpuVisibleSegmentSize(pExt);
608 vramSize /= pExt->u.primary.commonInfo.cDisplays;
609# ifdef VBOX_WDDM_WIN8
610 if (!g_VBoxDisplayOnly)
611# endif
612 {
613 /* at least two surfaces will be needed: primary & shadow */
614 vramSize /= 2;
615 }
616 vramSize &= ~PAGE_OFFSET_MASK;
617#endif
618
619 /* Check that values are valid and mode fits into VRAM */
620 if (!xres || !yres
621 || !((bpp == 16)
622#ifdef VBOX_WITH_8BPP_MODES
623 || (bpp == 8)
624#endif
625 || (bpp == 24)
626 || (bpp == 32)))
627 {
628 LOG(("invalid params for special mode %dx%d:%d", xres, yres, bpp));
629 return FALSE;
630 }
631
632
633
634 if ((xres * yres * (bpp / 8) >= vramSize))
635 {
636 /* Store values of last reported release log message to avoid log flooding. */
637 static uint32_t s_xresNoVRAM=0, s_yresNoVRAM=0, s_bppNoVRAM=0;
638
639 LOG(("not enough VRAM for video mode %dx%dx%dbpp. Available: %d bytes. Required: more than %d bytes.",
640 xres, yres, bpp, vramSize, xres * yres * (bpp / 8)));
641
642 s_xresNoVRAM = xres;
643 s_yresNoVRAM = yres;
644 s_bppNoVRAM = bpp;
645
646 return FALSE;
647 }
648
649 return TRUE;
650}
651
652/* Checks if there's a pending video mode change hint,
653 * and fills pPendingMode with associated info.
654 * returns TRUE if there's a pending change. Otherwise returns FALSE.
655 */
656static BOOLEAN
657VBoxMPCheckPendingVideoMode(PVBOXMP_DEVEXT pExt, PVIDEO_MODE_INFORMATION pPendingMode)
658{
659 uint32_t xres=0, yres=0, bpp=0, display=0;
660
661 /* Check if there's a pending display change request for this display */
662 if (VBoxQueryDisplayRequest(&xres, &yres, &bpp, &display) && (xres || yres || bpp))
663 {
664 if (display >= RT_ELEMENTS(g_CustomVideoModes))
665 {
666 /*display = RT_ELEMENTS(g_CustomVideoModes) - 1;*/
667 WARN(("VBoxQueryDisplayRequest returned invalid display number %d", display));
668 return FALSE;
669 }
670 }
671 else
672 {
673 LOG(("no pending request"));
674 return FALSE;
675 }
676
677 /* Correct video mode params and check if host likes it */
678 if (VBoxMPValidateVideoModeParams(pExt, display, xres, yres, bpp))
679 {
680 VBoxFillVidModeInfo(pPendingMode, xres, yres, bpp, display, 0);
681 return TRUE;
682 }
683
684 return FALSE;
685}
686
687/* Save custom mode info to registry */
688static void VBoxMPRegSaveModeInfo(PVBOXMP_DEVEXT pExt, uint32_t iDisplay, PVIDEO_MODE_INFORMATION pMode)
689{
690 VBOXMPCMNREGISTRY Registry;
691 VP_STATUS rc;
692
693 rc = VBoxMPCmnRegInit(pExt, &Registry);
694 VBOXMP_WARN_VPS(rc);
695
696 if (iDisplay==0)
697 {
698 /*First name without a suffix*/
699 rc = VBoxMPCmnRegSetDword(Registry, L"CustomXRes", pMode->VisScreenWidth);
700 VBOXMP_WARN_VPS(rc);
701 rc = VBoxMPCmnRegSetDword(Registry, L"CustomYRes", pMode->VisScreenHeight);
702 VBOXMP_WARN_VPS(rc);
703 rc = VBoxMPCmnRegSetDword(Registry, L"CustomBPP", pMode->BitsPerPlane);
704 VBOXMP_WARN_VPS(rc);
705 }
706 else
707 {
708 wchar_t keyname[32];
709 swprintf(keyname, L"CustomXRes%d", iDisplay);
710 rc = VBoxMPCmnRegSetDword(Registry, keyname, pMode->VisScreenWidth);
711 VBOXMP_WARN_VPS(rc);
712 swprintf(keyname, L"CustomYRes%d", iDisplay);
713 rc = VBoxMPCmnRegSetDword(Registry, keyname, pMode->VisScreenHeight);
714 VBOXMP_WARN_VPS(rc);
715 swprintf(keyname, L"CustomBPP%d", iDisplay);
716 rc = VBoxMPCmnRegSetDword(Registry, keyname, pMode->BitsPerPlane);
717 VBOXMP_WARN_VPS(rc);
718 }
719
720 rc = VBoxMPCmnRegFini(Registry);
721 VBOXMP_WARN_VPS(rc);
722}
723
724#ifdef VBOX_XPDM_MINIPORT
725VIDEO_MODE_INFORMATION* VBoxMPXpdmCurrentVideoMode(PVBOXMP_DEVEXT pExt)
726{
727 return VBoxMPCmnGetVideoModeInfo(pExt, pExt->CurrentMode - 1);
728}
729
730ULONG VBoxMPXpdmGetVideoModesCount(PVBOXMP_DEVEXT pExt)
731{
732 return pExt->cVideoModes;
733}
734
735/* Makes a table of video modes consisting of:
736 * Default modes
737 * Custom modes manually added to registry
738 * Custom modes for all displays (either from a display change hint or stored in registry)
739 * 2 special modes, for a pending display change for this adapter. See comments below.
740 */
741void VBoxMPXpdmBuildVideoModesTable(PVBOXMP_DEVEXT pExt)
742{
743 uint32_t cStandartModes;
744 BOOLEAN bPending, bHaveSpecial;
745 VIDEO_MODE_INFORMATION specialMode;
746
747 /* Fill table with standart modes and ones manually added to registry.
748 * Up to VBOXMP_MAX_VIDEO_MODES elements can be used, the rest is reserved
749 * for custom mode alternating indexes.
750 */
751 cStandartModes = VBoxMPFillModesTable(pExt, pExt->iDevice, pExt->aVideoModes, VBOXMP_MAX_VIDEO_MODES, NULL);
752
753 /* Add custom mode for this display to the table */
754 /* Make 2 entries in the video mode table. */
755 uint32_t iModeBase = cStandartModes;
756
757 /* Take the alternating index into account. */
758 BOOLEAN bAlternativeIndex = (pExt->iInvocationCounter % 2)? TRUE: FALSE;
759
760 uint32_t iSpecialMode = iModeBase + (bAlternativeIndex? 1: 0);
761 uint32_t iStandardMode = iModeBase + (bAlternativeIndex? 0: 1);
762
763 /* Fill the special mode. */
764 memcpy(&pExt->aVideoModes[iSpecialMode], &g_CustomVideoModes[pExt->iDevice], sizeof(VIDEO_MODE_INFORMATION));
765 pExt->aVideoModes[iSpecialMode].ModeIndex = iSpecialMode + 1;
766
767 /* Wipe the other entry so it is not selected. */
768 memcpy(&pExt->aVideoModes[iStandardMode], &pExt->aVideoModes[3], sizeof(VIDEO_MODE_INFORMATION));
769 pExt->aVideoModes[iStandardMode].ModeIndex = iStandardMode + 1;
770
771 LOG(("added special mode[%d] %dx%d:%d for display %d\n",
772 iSpecialMode,
773 pExt->aVideoModes[iSpecialMode].VisScreenWidth,
774 pExt->aVideoModes[iSpecialMode].VisScreenHeight,
775 pExt->aVideoModes[iSpecialMode].BitsPerPlane,
776 pExt->iDevice));
777
778 /* Check if host wants us to switch video mode and it's for this adapter */
779 bPending = VBoxMPCheckPendingVideoMode(pExt, &specialMode);
780 bHaveSpecial = bPending && (pExt->iDevice == specialMode.ModeIndex);
781 LOG(("bPending %d, pExt->iDevice %d, specialMode.ModeIndex %d",
782 bPending, pExt->iDevice, specialMode.ModeIndex));
783
784 /* Check the startup case */
785 if (!bHaveSpecial && VBoxMPIsStartingUp(pExt, pExt->iDevice))
786 {
787 uint32_t xres=0, yres=0, bpp=0;
788 LOG(("Startup for screen %d", pExt->iDevice));
789 /* Check if we could make valid mode from values stored to registry */
790 if (VBoxMPValidateVideoModeParams(pExt, pExt->iDevice, xres, yres, bpp))
791 {
792 LOG(("Startup for screen %d validated %dx%d %d", pExt->iDevice, xres, yres, bpp));
793 VBoxFillVidModeInfo(&specialMode, xres, yres, bpp, 0, 0);
794 bHaveSpecial = TRUE;
795 }
796 }
797
798 /* Update number of modes. Each display has 2 entries for alternating custom mode index. */
799 pExt->cVideoModes = cStandartModes + 2;
800
801 if (bHaveSpecial)
802 {
803 /* We need to alternate mode index entry for a pending mode change,
804 * else windows will ignore actual mode change call.
805 * Only alternate index if one of mode parameters changed and
806 * regardless of conditions always add 2 entries to the table.
807 */
808 BOOLEAN bAlternativeIndex = FALSE;
809
810 BOOLEAN bChanged = (pExt->Prev_xres!=specialMode.VisScreenWidth
811 || pExt->Prev_yres!=specialMode.VisScreenHeight
812 || pExt->Prev_bpp!=specialMode.BitsPerPlane);
813
814 LOG(("prev %dx%dx%d, special %dx%dx%d",
815 pExt->Prev_xres, pExt->Prev_yres, pExt->Prev_bpp,
816 specialMode.VisScreenWidth, specialMode.VisScreenHeight, specialMode.BitsPerPlane));
817
818 if (bChanged)
819 {
820 pExt->Prev_xres = specialMode.VisScreenWidth;
821 pExt->Prev_yres = specialMode.VisScreenHeight;
822 pExt->Prev_bpp = specialMode.BitsPerPlane;
823 }
824
825 /* Check if we need to alternate the index */
826 if (!VBoxMPIsStartingUp(pExt, pExt->iDevice))
827 {
828 if (bChanged)
829 {
830 pExt->iInvocationCounter++;
831 }
832
833 if (pExt->iInvocationCounter % 2)
834 {
835 bAlternativeIndex = TRUE;
836 }
837 }
838
839 uint32_t iSpecialModeElement = cStandartModes + (bAlternativeIndex? 1: 0);
840 uint32_t iSpecialModeElementOld = cStandartModes + (bAlternativeIndex? 0: 1);
841
842 LOG(("add special mode[%d] %dx%d:%d for display %d (bChanged=%d, bAlternativeIndex=%d)",
843 iSpecialModeElement, specialMode.VisScreenWidth, specialMode.VisScreenHeight, specialMode.BitsPerPlane,
844 pExt->iDevice, bChanged, bAlternativeIndex));
845
846 /* Add special mode to the table
847 * Note: Y offset isn't used for a host-supplied modes
848 */
849 specialMode.ModeIndex = iSpecialModeElement + 1;
850 memcpy(&pExt->aVideoModes[iSpecialModeElement], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
851
852 /* Save special mode in the custom modes table */
853 memcpy(&g_CustomVideoModes[pExt->iDevice], &specialMode, sizeof(VIDEO_MODE_INFORMATION));
854
855 /* Wipe the old entry so the special mode will be found in the new positions. */
856 memcpy(&pExt->aVideoModes[iSpecialModeElementOld], &pExt->aVideoModes[3], sizeof(VIDEO_MODE_INFORMATION));
857 pExt->aVideoModes[iSpecialModeElementOld].ModeIndex = iSpecialModeElementOld + 1;
858
859 /* Save special mode info to registry */
860 VBoxMPRegSaveModeInfo(pExt, pExt->iDevice, &specialMode);
861 }
862
863#if defined(LOG_ENABLED)
864 do
865 {
866 LOG(("Filled %d modes for display %d", pExt->cVideoModes, pExt->iDevice));
867
868 for (uint32_t i=0; i < pExt->cVideoModes; ++i)
869 {
870 LOG(("Mode[%2d]: %4dx%4d:%2d (idx=%d)",
871 i, pExt->aVideoModes[i].VisScreenWidth, pExt->aVideoModes[i].VisScreenHeight,
872 pExt->aVideoModes[i].BitsPerPlane, pExt->aVideoModes[i].ModeIndex));
873 }
874 } while (0);
875#endif
876}
877#endif /*VBOX_XPDM_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