VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/DisplayImpl.cpp@ 67605

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

Main/Display: eliminate the old resize code, the new code is well tested and doing the right thing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 157.7 KB
Line 
1/* $Id: DisplayImpl.cpp 67531 2017-06-21 09:17:51Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 "DisplayImpl.h"
19#include "DisplayUtils.h"
20#include "ConsoleImpl.h"
21#include "ConsoleVRDPServer.h"
22#include "GuestImpl.h"
23#include "VMMDev.h"
24
25#include "AutoCaller.h"
26#include "Logging.h"
27
28/* generated header */
29#include "VBoxEvents.h"
30
31#include <iprt/semaphore.h>
32#include <iprt/thread.h>
33#include <iprt/asm.h>
34#include <iprt/time.h>
35#include <iprt/cpp/utils.h>
36#include <iprt/alloca.h>
37
38#include <VBox/vmm/pdmdrv.h>
39#if defined(DEBUG) || defined(VBOX_STRICT) /* for VM_ASSERT_EMT(). */
40# include <VBox/vmm/vm.h>
41#endif
42
43#ifdef VBOX_WITH_VIDEOHWACCEL
44# include <VBoxVideo.h>
45#endif
46
47#if defined(VBOX_WITH_CROGL) || defined(VBOX_WITH_CRHGSMI)
48# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
49#endif
50
51#include <VBox/com/array.h>
52
53#ifdef VBOX_WITH_VIDEOREC
54# include <iprt/path.h>
55# include "VideoRec.h"
56#endif
57
58#ifdef VBOX_WITH_CROGL
59typedef enum
60{
61 CRVREC_STATE_IDLE,
62 CRVREC_STATE_SUBMITTED
63} CRVREC_STATE;
64#endif
65
66/**
67 * Display driver instance data.
68 *
69 * @implements PDMIDISPLAYCONNECTOR
70 */
71typedef struct DRVMAINDISPLAY
72{
73 /** Pointer to the display object. */
74 Display *pDisplay;
75 /** Pointer to the driver instance structure. */
76 PPDMDRVINS pDrvIns;
77 /** Pointer to the keyboard port interface of the driver/device above us. */
78 PPDMIDISPLAYPORT pUpPort;
79 /** Our display connector interface. */
80 PDMIDISPLAYCONNECTOR IConnector;
81#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
82 /** VBVA callbacks */
83 PPDMIDISPLAYVBVACALLBACKS pVBVACallbacks;
84#endif
85} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
86
87/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
88#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) RT_FROM_MEMBER(pInterface, DRVMAINDISPLAY, IConnector)
89
90// constructor / destructor
91/////////////////////////////////////////////////////////////////////////////
92
93Display::Display()
94 : mParent(NULL), mfIsCr3DEnabled(false)
95{
96}
97
98Display::~Display()
99{
100}
101
102
103HRESULT Display::FinalConstruct()
104{
105 int rc = videoAccelConstruct(&mVideoAccelLegacy);
106 AssertRC(rc);
107
108 mfVideoAccelVRDP = false;
109 mfu32SupportedOrders = 0;
110 mcVideoAccelVRDPRefs = 0;
111
112 mfSeamlessEnabled = false;
113 mpRectVisibleRegion = NULL;
114 mcRectVisibleRegion = 0;
115
116#ifdef VBOX_WITH_CROGL
117 mfCrOglDataHidden = false;
118#endif
119
120 mpDrv = NULL;
121 mpVMMDev = NULL;
122 mfVMMDevInited = false;
123
124 rc = RTCritSectInit(&mVideoAccelLock);
125 AssertRC(rc);
126
127#ifdef VBOX_WITH_HGSMI
128 mu32UpdateVBVAFlags = 0;
129 mfVMMDevSupportsGraphics = false;
130 mfGuestVBVACapabilities = 0;
131 mfHostCursorCapabilities = 0;
132#endif
133
134#ifdef VBOX_WITH_VIDEOREC
135 rc = RTCritSectInit(&mVideoCaptureLock);
136 AssertRC(rc);
137
138 mpVideoRecCtx = NULL;
139 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
140 maVideoRecEnabled[i] = true;
141#endif
142
143#ifdef VBOX_WITH_CRHGSMI
144 mhCrOglSvc = NULL;
145 rc = RTCritSectRwInit(&mCrOglLock);
146 AssertRC(rc);
147#endif
148
149#ifdef VBOX_WITH_CROGL
150 RT_ZERO(mCrOglCallbacks);
151 RT_ZERO(mCrOglScreenshotData);
152 mfCrOglVideoRecState = CRVREC_STATE_IDLE;
153 mCrOglScreenshotData.u32Screen = CRSCREEN_ALL;
154 mCrOglScreenshotData.pvContext = this;
155 mCrOglScreenshotData.pfnScreenshotBegin = i_displayCrVRecScreenshotBegin;
156 mCrOglScreenshotData.pfnScreenshotPerform = i_displayCrVRecScreenshotPerform;
157 mCrOglScreenshotData.pfnScreenshotEnd = i_displayCrVRecScreenshotEnd;
158#endif
159
160 return BaseFinalConstruct();
161}
162
163void Display::FinalRelease()
164{
165 uninit();
166
167#ifdef VBOX_WITH_VIDEOREC
168 if (RTCritSectIsInitialized(&mVideoCaptureLock))
169 {
170 RTCritSectDelete(&mVideoCaptureLock);
171 RT_ZERO(mVideoCaptureLock);
172 }
173#endif
174
175 videoAccelDestroy(&mVideoAccelLegacy);
176 i_saveVisibleRegion(0, NULL);
177
178 if (RTCritSectIsInitialized(&mVideoAccelLock))
179 {
180 RTCritSectDelete(&mVideoAccelLock);
181 RT_ZERO(mVideoAccelLock);
182 }
183
184#ifdef VBOX_WITH_CRHGSMI
185 if (RTCritSectRwIsInitialized(&mCrOglLock))
186 {
187 RTCritSectRwDelete(&mCrOglLock);
188 RT_ZERO(mCrOglLock);
189 }
190#endif
191 BaseFinalRelease();
192}
193
194// public initializer/uninitializer for internal purposes only
195/////////////////////////////////////////////////////////////////////////////
196
197#define kMaxSizeThumbnail 64
198
199/**
200 * Save thumbnail and screenshot of the guest screen.
201 */
202static int displayMakeThumbnail(uint8_t *pbData, uint32_t cx, uint32_t cy,
203 uint8_t **ppu8Thumbnail, uint32_t *pcbThumbnail, uint32_t *pcxThumbnail, uint32_t *pcyThumbnail)
204{
205 int rc = VINF_SUCCESS;
206
207 uint8_t *pu8Thumbnail = NULL;
208 uint32_t cbThumbnail = 0;
209 uint32_t cxThumbnail = 0;
210 uint32_t cyThumbnail = 0;
211
212 if (cx > cy)
213 {
214 cxThumbnail = kMaxSizeThumbnail;
215 cyThumbnail = (kMaxSizeThumbnail * cy) / cx;
216 }
217 else
218 {
219 cyThumbnail = kMaxSizeThumbnail;
220 cxThumbnail = (kMaxSizeThumbnail * cx) / cy;
221 }
222
223 LogRelFlowFunc(("%dx%d -> %dx%d\n", cx, cy, cxThumbnail, cyThumbnail));
224
225 cbThumbnail = cxThumbnail * 4 * cyThumbnail;
226 pu8Thumbnail = (uint8_t *)RTMemAlloc(cbThumbnail);
227
228 if (pu8Thumbnail)
229 {
230 uint8_t *dst = pu8Thumbnail;
231 uint8_t *src = pbData;
232 int dstW = cxThumbnail;
233 int dstH = cyThumbnail;
234 int srcW = cx;
235 int srcH = cy;
236 int iDeltaLine = cx * 4;
237
238 BitmapScale32(dst,
239 dstW, dstH,
240 src,
241 iDeltaLine,
242 srcW, srcH);
243
244 *ppu8Thumbnail = pu8Thumbnail;
245 *pcbThumbnail = cbThumbnail;
246 *pcxThumbnail = cxThumbnail;
247 *pcyThumbnail = cyThumbnail;
248 }
249 else
250 {
251 rc = VERR_NO_MEMORY;
252 }
253
254 return rc;
255}
256
257#ifdef VBOX_WITH_CROGL
258typedef struct
259{
260 CRVBOXHGCMTAKESCREENSHOT Base;
261
262 /* 32bpp small RGB image. */
263 uint8_t *pu8Thumbnail;
264 uint32_t cbThumbnail;
265 uint32_t cxThumbnail;
266 uint32_t cyThumbnail;
267
268 /* PNG screenshot. */
269 uint8_t *pu8PNG;
270 uint32_t cbPNG;
271 uint32_t cxPNG;
272 uint32_t cyPNG;
273} VBOX_DISPLAY_SAVESCREENSHOT_DATA;
274
275static DECLCALLBACK(void) displaySaveScreenshotReport(void *pvCtx, uint32_t uScreen,
276 uint32_t x, uint32_t y, uint32_t uBitsPerPixel,
277 uint32_t uBytesPerLine, uint32_t uGuestWidth, uint32_t uGuestHeight,
278 uint8_t *pu8BufferAddress, uint64_t u64Timestamp)
279{
280 RT_NOREF(uScreen, x, y, uBitsPerPixel,uBytesPerLine, u64Timestamp);
281 VBOX_DISPLAY_SAVESCREENSHOT_DATA *pData = (VBOX_DISPLAY_SAVESCREENSHOT_DATA*)pvCtx;
282 displayMakeThumbnail(pu8BufferAddress, uGuestWidth, uGuestHeight, &pData->pu8Thumbnail,
283 &pData->cbThumbnail, &pData->cxThumbnail, &pData->cyThumbnail);
284 int rc = DisplayMakePNG(pu8BufferAddress, uGuestWidth, uGuestHeight, &pData->pu8PNG,
285 &pData->cbPNG, &pData->cxPNG, &pData->cyPNG, 1);
286 if (RT_FAILURE(rc))
287 {
288 AssertMsgFailed(("DisplayMakePNG failed (rc=%Rrc)\n", rc));
289 if (pData->pu8PNG)
290 {
291 RTMemFree(pData->pu8PNG);
292 pData->pu8PNG = NULL;
293 }
294 pData->cbPNG = 0;
295 pData->cxPNG = 0;
296 pData->cyPNG = 0;
297 }
298}
299#endif
300
301DECLCALLBACK(void) Display::i_displaySSMSaveScreenshot(PSSMHANDLE pSSM, void *pvUser)
302{
303 Display *that = static_cast<Display*>(pvUser);
304
305 /* 32bpp small RGB image. */
306 uint8_t *pu8Thumbnail = NULL;
307 uint32_t cbThumbnail = 0;
308 uint32_t cxThumbnail = 0;
309 uint32_t cyThumbnail = 0;
310
311 /* PNG screenshot. */
312 uint8_t *pu8PNG = NULL;
313 uint32_t cbPNG = 0;
314 uint32_t cxPNG = 0;
315 uint32_t cyPNG = 0;
316
317 Console::SafeVMPtr ptrVM(that->mParent);
318 if (ptrVM.isOk())
319 {
320#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
321 BOOL f3DSnapshot = FALSE;
322 if ( that->mfIsCr3DEnabled
323 && that->mCrOglCallbacks.pfnHasData
324 && that->mCrOglCallbacks.pfnHasData())
325 {
326 VMMDev *pVMMDev = that->mParent->i_getVMMDev();
327 if (pVMMDev)
328 {
329 VBOX_DISPLAY_SAVESCREENSHOT_DATA *pScreenshot;
330 pScreenshot = (VBOX_DISPLAY_SAVESCREENSHOT_DATA*)RTMemAllocZ(sizeof(*pScreenshot));
331 if (pScreenshot)
332 {
333 /* screen id or CRSCREEN_ALL to specify all enabled */
334 pScreenshot->Base.u32Screen = 0;
335 pScreenshot->Base.u32Width = 0;
336 pScreenshot->Base.u32Height = 0;
337 pScreenshot->Base.u32Pitch = 0;
338 pScreenshot->Base.pvBuffer = NULL;
339 pScreenshot->Base.pvContext = pScreenshot;
340 pScreenshot->Base.pfnScreenshotBegin = NULL;
341 pScreenshot->Base.pfnScreenshotPerform = displaySaveScreenshotReport;
342 pScreenshot->Base.pfnScreenshotEnd = NULL;
343
344 VBOXCRCMDCTL_HGCM data;
345 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
346 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
347
348 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
349 data.aParms[0].u.pointer.addr = &pScreenshot->Base;
350 data.aParms[0].u.pointer.size = sizeof(pScreenshot->Base);
351
352 int rc = that->i_crCtlSubmitSync(&data.Hdr, sizeof(data));
353 if (RT_SUCCESS(rc))
354 {
355 if (pScreenshot->pu8PNG)
356 {
357 pu8Thumbnail = pScreenshot->pu8Thumbnail;
358 cbThumbnail = pScreenshot->cbThumbnail;
359 cxThumbnail = pScreenshot->cxThumbnail;
360 cyThumbnail = pScreenshot->cyThumbnail;
361
362 /* PNG screenshot. */
363 pu8PNG = pScreenshot->pu8PNG;
364 cbPNG = pScreenshot->cbPNG;
365 cxPNG = pScreenshot->cxPNG;
366 cyPNG = pScreenshot->cyPNG;
367 f3DSnapshot = TRUE;
368 }
369 else
370 AssertMsgFailed(("no png\n"));
371 }
372 else
373 AssertMsgFailed(("SHCRGL_HOST_FN_TAKE_SCREENSHOT failed (rc=%Rrc)\n", rc));
374
375
376 RTMemFree(pScreenshot);
377 }
378 }
379 }
380
381 if (!f3DSnapshot)
382#endif
383 {
384 /* Query RGB bitmap. */
385 /* SSM code is executed on EMT(0), therefore no need to use VMR3ReqCallWait. */
386 uint8_t *pbData = NULL;
387 size_t cbData = 0;
388 uint32_t cx = 0;
389 uint32_t cy = 0;
390 bool fFreeMem = false;
391 int rc = Display::i_displayTakeScreenshotEMT(that, VBOX_VIDEO_PRIMARY_SCREEN, &pbData, &cbData, &cx, &cy, &fFreeMem);
392
393 /*
394 * It is possible that success is returned but everything is 0 or NULL.
395 * (no display attached if a VM is running with VBoxHeadless on OSE for example)
396 */
397 if (RT_SUCCESS(rc) && pbData)
398 {
399 Assert(cx && cy);
400
401 /* Prepare a small thumbnail and a PNG screenshot. */
402 displayMakeThumbnail(pbData, cx, cy, &pu8Thumbnail, &cbThumbnail, &cxThumbnail, &cyThumbnail);
403 rc = DisplayMakePNG(pbData, cx, cy, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 1);
404 if (RT_FAILURE(rc))
405 {
406 if (pu8PNG)
407 {
408 RTMemFree(pu8PNG);
409 pu8PNG = NULL;
410 }
411 cbPNG = 0;
412 cxPNG = 0;
413 cyPNG = 0;
414 }
415
416 if (fFreeMem)
417 RTMemFree(pbData);
418 else
419 that->mpDrv->pUpPort->pfnFreeScreenshot(that->mpDrv->pUpPort, pbData);
420 }
421 }
422 }
423 else
424 {
425 LogFunc(("Failed to get VM pointer 0x%x\n", ptrVM.rc()));
426 }
427
428 /* Regardless of rc, save what is available:
429 * Data format:
430 * uint32_t cBlocks;
431 * [blocks]
432 *
433 * Each block is:
434 * uint32_t cbBlock; if 0 - no 'block data'.
435 * uint32_t typeOfBlock; 0 - 32bpp RGB bitmap, 1 - PNG, ignored if 'cbBlock' is 0.
436 * [block data]
437 *
438 * Block data for bitmap and PNG:
439 * uint32_t cx;
440 * uint32_t cy;
441 * [image data]
442 */
443 SSMR3PutU32(pSSM, 2); /* Write thumbnail and PNG screenshot. */
444
445 /* First block. */
446 SSMR3PutU32(pSSM, (uint32_t)(cbThumbnail + 2 * sizeof(uint32_t)));
447 SSMR3PutU32(pSSM, 0); /* Block type: thumbnail. */
448
449 if (cbThumbnail)
450 {
451 SSMR3PutU32(pSSM, cxThumbnail);
452 SSMR3PutU32(pSSM, cyThumbnail);
453 SSMR3PutMem(pSSM, pu8Thumbnail, cbThumbnail);
454 }
455
456 /* Second block. */
457 SSMR3PutU32(pSSM, (uint32_t)(cbPNG + 2 * sizeof(uint32_t)));
458 SSMR3PutU32(pSSM, 1); /* Block type: png. */
459
460 if (cbPNG)
461 {
462 SSMR3PutU32(pSSM, cxPNG);
463 SSMR3PutU32(pSSM, cyPNG);
464 SSMR3PutMem(pSSM, pu8PNG, cbPNG);
465 }
466
467 RTMemFree(pu8PNG);
468 RTMemFree(pu8Thumbnail);
469}
470
471DECLCALLBACK(int)
472Display::i_displaySSMLoadScreenshot(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
473{
474 RT_NOREF(pvUser);
475 if (uVersion != sSSMDisplayScreenshotVer)
476 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
477 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
478
479 /* Skip data. */
480 uint32_t cBlocks;
481 int rc = SSMR3GetU32(pSSM, &cBlocks);
482 AssertRCReturn(rc, rc);
483
484 for (uint32_t i = 0; i < cBlocks; i++)
485 {
486 uint32_t cbBlock;
487 rc = SSMR3GetU32(pSSM, &cbBlock);
488 AssertRCBreak(rc);
489
490 uint32_t typeOfBlock;
491 rc = SSMR3GetU32(pSSM, &typeOfBlock);
492 AssertRCBreak(rc);
493
494 LogRelFlowFunc(("[%d] type %d, size %d bytes\n", i, typeOfBlock, cbBlock));
495
496 /* Note: displaySSMSaveScreenshot writes size of a block = 8 and
497 * do not write any data if the image size was 0.
498 * @todo Fix and increase saved state version.
499 */
500 if (cbBlock > 2 * sizeof(uint32_t))
501 {
502 rc = SSMR3Skip(pSSM, cbBlock);
503 AssertRCBreak(rc);
504 }
505 }
506
507 return rc;
508}
509
510/**
511 * Save/Load some important guest state
512 */
513DECLCALLBACK(void)
514Display::i_displaySSMSave(PSSMHANDLE pSSM, void *pvUser)
515{
516 Display *that = static_cast<Display*>(pvUser);
517
518 SSMR3PutU32(pSSM, that->mcMonitors);
519 for (unsigned i = 0; i < that->mcMonitors; i++)
520 {
521 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32Offset);
522 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32MaxFramebufferSize);
523 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32InformationSize);
524 SSMR3PutU32(pSSM, that->maFramebuffers[i].w);
525 SSMR3PutU32(pSSM, that->maFramebuffers[i].h);
526 SSMR3PutS32(pSSM, that->maFramebuffers[i].xOrigin);
527 SSMR3PutS32(pSSM, that->maFramebuffers[i].yOrigin);
528 SSMR3PutU32(pSSM, that->maFramebuffers[i].flags);
529 }
530 SSMR3PutS32(pSSM, that->xInputMappingOrigin);
531 SSMR3PutS32(pSSM, that->yInputMappingOrigin);
532 SSMR3PutU32(pSSM, that->cxInputMapping);
533 SSMR3PutU32(pSSM, that->cyInputMapping);
534 SSMR3PutU32(pSSM, that->mfGuestVBVACapabilities);
535 SSMR3PutU32(pSSM, that->mfHostCursorCapabilities);
536}
537
538DECLCALLBACK(int)
539Display::i_displaySSMLoad(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
540{
541 Display *that = static_cast<Display*>(pvUser);
542
543 if ( uVersion != sSSMDisplayVer
544 && uVersion != sSSMDisplayVer2
545 && uVersion != sSSMDisplayVer3
546 && uVersion != sSSMDisplayVer4
547 && uVersion != sSSMDisplayVer5)
548 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
549 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
550
551 uint32_t cMonitors;
552 int rc = SSMR3GetU32(pSSM, &cMonitors);
553 AssertRCReturn(rc, rc);
554 if (cMonitors != that->mcMonitors)
555 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Number of monitors changed (%d->%d)!"), cMonitors, that->mcMonitors);
556
557 for (uint32_t i = 0; i < cMonitors; i++)
558 {
559 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32Offset);
560 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32MaxFramebufferSize);
561 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32InformationSize);
562 if ( uVersion == sSSMDisplayVer2
563 || uVersion == sSSMDisplayVer3
564 || uVersion == sSSMDisplayVer4
565 || uVersion == sSSMDisplayVer5)
566 {
567 uint32_t w;
568 uint32_t h;
569 SSMR3GetU32(pSSM, &w);
570 SSMR3GetU32(pSSM, &h);
571 that->maFramebuffers[i].w = w;
572 that->maFramebuffers[i].h = h;
573 }
574 if ( uVersion == sSSMDisplayVer3
575 || uVersion == sSSMDisplayVer4
576 || uVersion == sSSMDisplayVer5)
577 {
578 int32_t xOrigin;
579 int32_t yOrigin;
580 uint32_t flags;
581 SSMR3GetS32(pSSM, &xOrigin);
582 SSMR3GetS32(pSSM, &yOrigin);
583 SSMR3GetU32(pSSM, &flags);
584 that->maFramebuffers[i].xOrigin = xOrigin;
585 that->maFramebuffers[i].yOrigin = yOrigin;
586 that->maFramebuffers[i].flags = (uint16_t)flags;
587 that->maFramebuffers[i].fDisabled = (that->maFramebuffers[i].flags & VBVA_SCREEN_F_DISABLED) != 0;
588 }
589 }
590 if ( uVersion == sSSMDisplayVer4
591 || uVersion == sSSMDisplayVer5)
592 {
593 SSMR3GetS32(pSSM, &that->xInputMappingOrigin);
594 SSMR3GetS32(pSSM, &that->yInputMappingOrigin);
595 SSMR3GetU32(pSSM, &that->cxInputMapping);
596 SSMR3GetU32(pSSM, &that->cyInputMapping);
597 }
598 if (uVersion == sSSMDisplayVer5)
599 {
600 SSMR3GetU32(pSSM, &that->mfGuestVBVACapabilities);
601 SSMR3GetU32(pSSM, &that->mfHostCursorCapabilities);
602 }
603
604 return VINF_SUCCESS;
605}
606
607/**
608 * Initializes the display object.
609 *
610 * @returns COM result indicator
611 * @param aParent handle of our parent object
612 */
613HRESULT Display::init(Console *aParent)
614{
615 ComAssertRet(aParent, E_INVALIDARG);
616 /* Enclose the state transition NotReady->InInit->Ready */
617 AutoInitSpan autoInitSpan(this);
618 AssertReturn(autoInitSpan.isOk(), E_FAIL);
619
620 unconst(mParent) = aParent;
621
622 mfSourceBitmapEnabled = true;
623 fVGAResizing = false;
624
625 ULONG ul;
626 mParent->i_machine()->COMGETTER(MonitorCount)(&ul);
627 mcMonitors = ul;
628 xInputMappingOrigin = 0;
629 yInputMappingOrigin = 0;
630 cxInputMapping = 0;
631 cyInputMapping = 0;
632
633 for (ul = 0; ul < mcMonitors; ul++)
634 {
635 maFramebuffers[ul].u32Offset = 0;
636 maFramebuffers[ul].u32MaxFramebufferSize = 0;
637 maFramebuffers[ul].u32InformationSize = 0;
638
639 maFramebuffers[ul].pFramebuffer = NULL;
640 /* All secondary monitors are disabled at startup. */
641 maFramebuffers[ul].fDisabled = ul > 0;
642
643 maFramebuffers[ul].u32Caps = 0;
644
645 maFramebuffers[ul].updateImage.pu8Address = NULL;
646 maFramebuffers[ul].updateImage.cbLine = 0;
647
648 maFramebuffers[ul].xOrigin = 0;
649 maFramebuffers[ul].yOrigin = 0;
650
651 maFramebuffers[ul].w = 0;
652 maFramebuffers[ul].h = 0;
653
654 maFramebuffers[ul].flags = maFramebuffers[ul].fDisabled? VBVA_SCREEN_F_DISABLED: 0;
655
656 maFramebuffers[ul].u16BitsPerPixel = 0;
657 maFramebuffers[ul].pu8FramebufferVRAM = NULL;
658 maFramebuffers[ul].u32LineSize = 0;
659
660 maFramebuffers[ul].pHostEvents = NULL;
661
662 maFramebuffers[ul].fDefaultFormat = false;
663
664#ifdef VBOX_WITH_HGSMI
665 maFramebuffers[ul].fVBVAEnabled = false;
666 maFramebuffers[ul].fVBVAForceResize = false;
667 maFramebuffers[ul].fRenderThreadMode = false;
668 maFramebuffers[ul].pVBVAHostFlags = NULL;
669#endif /* VBOX_WITH_HGSMI */
670#ifdef VBOX_WITH_CROGL
671 RT_ZERO(maFramebuffers[ul].pendingViewportInfo);
672#endif
673 }
674
675 {
676 // register listener for state change events
677 ComPtr<IEventSource> es;
678 mParent->COMGETTER(EventSource)(es.asOutParam());
679 com::SafeArray<VBoxEventType_T> eventTypes;
680 eventTypes.push_back(VBoxEventType_OnStateChanged);
681 es->RegisterListener(this, ComSafeArrayAsInParam(eventTypes), true);
682 }
683
684 /* Cache the 3D settings. */
685 BOOL fIs3DEnabled = FALSE;
686 mParent->i_machine()->COMGETTER(Accelerate3DEnabled)(&fIs3DEnabled);
687 GraphicsControllerType_T enmGpuType = (GraphicsControllerType_T)GraphicsControllerType_VBoxVGA;
688 mParent->i_machine()->COMGETTER(GraphicsControllerType)(&enmGpuType);
689 mfIsCr3DEnabled = fIs3DEnabled && enmGpuType == GraphicsControllerType_VBoxVGA;
690
691 /* Confirm a successful initialization */
692 autoInitSpan.setSucceeded();
693
694 return S_OK;
695}
696
697/**
698 * Uninitializes the instance and sets the ready flag to FALSE.
699 * Called either from FinalRelease() or by the parent when it gets destroyed.
700 */
701void Display::uninit()
702{
703 LogRelFlowFunc(("this=%p\n", this));
704
705 /* Enclose the state transition Ready->InUninit->NotReady */
706 AutoUninitSpan autoUninitSpan(this);
707 if (autoUninitSpan.uninitDone())
708 return;
709
710 unsigned uScreenId;
711 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
712 {
713 maFramebuffers[uScreenId].pSourceBitmap.setNull();
714 maFramebuffers[uScreenId].updateImage.pSourceBitmap.setNull();
715 maFramebuffers[uScreenId].updateImage.pu8Address = NULL;
716 maFramebuffers[uScreenId].updateImage.cbLine = 0;
717 maFramebuffers[uScreenId].pFramebuffer.setNull();
718#ifdef VBOX_WITH_VIDEOREC
719 maFramebuffers[uScreenId].videoCapture.pSourceBitmap.setNull();
720#endif
721 }
722
723 if (mParent)
724 {
725 ComPtr<IEventSource> es;
726 mParent->COMGETTER(EventSource)(es.asOutParam());
727 es->UnregisterListener(this);
728 }
729
730 unconst(mParent) = NULL;
731
732 if (mpDrv)
733 mpDrv->pDisplay = NULL;
734
735 mpDrv = NULL;
736 mpVMMDev = NULL;
737 mfVMMDevInited = true;
738}
739
740/**
741 * Register the SSM methods. Called by the power up thread to be able to
742 * pass pVM
743 */
744int Display::i_registerSSM(PUVM pUVM)
745{
746 /* Version 2 adds width and height of the framebuffer; version 3 adds
747 * the framebuffer offset in the virtual desktop and the framebuffer flags;
748 * version 4 adds guest to host input event mapping and version 5 adds
749 * guest VBVA and host cursor capabilities.
750 */
751 int rc = SSMR3RegisterExternal(pUVM, "DisplayData", 0, sSSMDisplayVer5,
752 mcMonitors * sizeof(uint32_t) * 8 + sizeof(uint32_t),
753 NULL, NULL, NULL,
754 NULL, i_displaySSMSave, NULL,
755 NULL, i_displaySSMLoad, NULL, this);
756 AssertRCReturn(rc, rc);
757
758 /*
759 * Register loaders for old saved states where iInstance was
760 * 3 * sizeof(uint32_t *) due to a code mistake.
761 */
762 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 12 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
763 NULL, NULL, NULL,
764 NULL, NULL, NULL,
765 NULL, i_displaySSMLoad, NULL, this);
766 AssertRCReturn(rc, rc);
767
768 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 24 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
769 NULL, NULL, NULL,
770 NULL, NULL, NULL,
771 NULL, i_displaySSMLoad, NULL, this);
772 AssertRCReturn(rc, rc);
773
774 /* uInstance is an arbitrary value greater than 1024. Such a value will ensure a quick seek in saved state file. */
775 rc = SSMR3RegisterExternal(pUVM, "DisplayScreenshot", 1100 /*uInstance*/, sSSMDisplayScreenshotVer, 0 /*cbGuess*/,
776 NULL, NULL, NULL,
777 NULL, i_displaySSMSaveScreenshot, NULL,
778 NULL, i_displaySSMLoadScreenshot, NULL, this);
779
780 AssertRCReturn(rc, rc);
781
782 return VINF_SUCCESS;
783}
784
785DECLCALLBACK(void) Display::i_displayCrCmdFree(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, int rc, void *pvCompletion)
786{
787 RT_NOREF(pCmd, cbCmd, rc);
788 Assert(pvCompletion);
789 RTMemFree(pvCompletion);
790}
791
792#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
793int Display::i_crOglWindowsShow(bool fShow)
794{
795 if (!mfCrOglDataHidden == !!fShow)
796 return VINF_SUCCESS;
797
798 if (!mhCrOglSvc)
799 {
800 /* No 3D or the VMSVGA3d kind. */
801 Assert(!mfIsCr3DEnabled);
802 return VERR_INVALID_STATE;
803 }
804
805 VMMDev *pVMMDev = mParent->i_getVMMDev();
806 if (!pVMMDev)
807 {
808 AssertMsgFailed(("no vmmdev\n"));
809 return VERR_INVALID_STATE;
810 }
811
812 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(VBOXCRCMDCTL_HGCM));
813 if (!pData)
814 {
815 AssertMsgFailed(("RTMemAlloc failed\n"));
816 return VERR_NO_MEMORY;
817 }
818
819 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
820 pData->Hdr.u32Function = SHCRGL_HOST_FN_WINDOWS_SHOW;
821
822 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
823 pData->aParms[0].u.uint32 = (uint32_t)fShow;
824
825 int rc = i_crCtlSubmit(&pData->Hdr, sizeof(*pData), i_displayCrCmdFree, pData);
826 if (RT_SUCCESS(rc))
827 mfCrOglDataHidden = !fShow;
828 else
829 {
830 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
831 RTMemFree(pData);
832 }
833
834 return rc;
835}
836#endif
837
838
839// public methods only for internal purposes
840/////////////////////////////////////////////////////////////////////////////
841
842int Display::i_notifyCroglResize(PCVBVAINFOVIEW pView, PCVBVAINFOSCREEN pScreen, void *pvVRAM)
843{
844 RT_NOREF(pView);
845#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
846 if (maFramebuffers[pScreen->u32ViewIndex].fRenderThreadMode)
847 return VINF_SUCCESS; /* nop it */
848
849 if (mfIsCr3DEnabled)
850 {
851 int rc = VERR_INVALID_STATE;
852 if (mhCrOglSvc)
853 {
854 VMMDev *pVMMDev = mParent->i_getVMMDev();
855 if (pVMMDev)
856 {
857 VBOXCRCMDCTL_HGCM *pCtl;
858 pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(CRVBOXHGCMDEVRESIZE) + sizeof(VBOXCRCMDCTL_HGCM));
859 if (pCtl)
860 {
861 CRVBOXHGCMDEVRESIZE *pData = (CRVBOXHGCMDEVRESIZE*)(pCtl+1);
862 pData->Screen = *pScreen;
863 pData->pvVRAM = pvVRAM;
864
865 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
866 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_DEV_RESIZE;
867 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
868 pCtl->aParms[0].u.pointer.addr = pData;
869 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
870
871 rc = i_crCtlSubmit(&pCtl->Hdr, sizeof(*pCtl), i_displayCrCmdFree, pCtl);
872 if (RT_FAILURE(rc))
873 {
874 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
875 RTMemFree(pCtl);
876 }
877 }
878 else
879 rc = VERR_NO_MEMORY;
880 }
881 }
882
883 return rc;
884 }
885#else /* !VBOX_WITH_HGCM || !VBOX_WITH_CROGL */
886 RT_NOREF(pScreen, pvVRAM);
887#endif
888 return VINF_SUCCESS;
889}
890
891/**
892 * Handles display resize event.
893 *
894 * @param uScreenId Screen ID
895 * @param bpp New bits per pixel.
896 * @param pvVRAM VRAM pointer.
897 * @param cbLine New bytes per line.
898 * @param w New display width.
899 * @param h New display height.
900 * @param flags Flags of the new video mode.
901 * @param xOrigin New display origin X.
902 * @param yOrigin New display origin Y.
903 * @param fVGAResize Whether the resize is originated from the VGA device (DevVGA).
904 */
905int Display::i_handleDisplayResize(unsigned uScreenId, uint32_t bpp, void *pvVRAM,
906 uint32_t cbLine, uint32_t w, uint32_t h, uint16_t flags,
907 int32_t xOrigin, int32_t yOrigin, bool fVGAResize)
908{
909 LogRel(("Display::handleDisplayResize: uScreenId=%d pvVRAM=%p w=%d h=%d bpp=%d cbLine=0x%X flags=0x%X\n", uScreenId,
910 pvVRAM, w, h, bpp, cbLine, flags));
911
912 /* Caller must not hold the object lock. */
913 AssertReturn(!isWriteLockOnCurrentThread(), VERR_INVALID_STATE);
914
915 /* Note: the old code checked if the video mode was actially chnaged and
916 * did not invalidate the source bitmap if the mode did not change.
917 * The new code always invalidates the source bitmap, i.e. it will
918 * notify the frontend even if nothing actually changed.
919 *
920 * Implementing the filtering is possible but might lead to pfnSetRenderVRAM races
921 * between this method and QuerySourceBitmap. Such races can be avoided by implementing
922 * the @todo below.
923 */
924
925 /* Make sure that the VGA device does not access the source bitmap. */
926 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && mpDrv)
927 {
928 /// @todo It is probably more convenient to implement
929 // mpDrv->pUpPort->pfnSetOutputBitmap(pvVRAM, cbScanline, cBits, cx, cy, bool fSet);
930 // and remove IConnector.pbData, cbScanline, cBits, cx, cy.
931 // fSet = false disables rendering and VGA can check
932 // if it is already rendering to a different bitmap, avoiding
933 // enable/disable rendering races.
934 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, false);
935
936 mpDrv->IConnector.pbData = NULL;
937 mpDrv->IConnector.cbScanline = 0;
938 mpDrv->IConnector.cBits = 32; /* DevVGA does not work with cBits == 0. */
939 mpDrv->IConnector.cx = 0;
940 mpDrv->IConnector.cy = 0;
941 }
942
943 /* Update maFramebuffers[uScreenId] under lock. */
944 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
945
946 if (uScreenId >= mcMonitors)
947 return VINF_SUCCESS;
948
949 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
950
951 /* Whether the monitor position has changed.
952 * A resize initiated by the VGA device does not change the monitor position.
953 */
954 const bool fNewOrigin = !fVGAResize
955 && ( pFBInfo->xOrigin != xOrigin
956 || pFBInfo->yOrigin != yOrigin);
957
958 /* The event for disabled->enabled transition.
959 * VGA resizes also come when the guest uses VBVA mode. They do not affect pFBInfo->fDisabled.
960 * The primary screen is re-enabled when the guest leaves the VBVA mode in i_displayVBVADisable.
961 */
962 const bool fGuestMonitorChangedEvent = !fVGAResize
963 && (pFBInfo->fDisabled != RT_BOOL(flags & VBVA_SCREEN_F_DISABLED));
964
965 /* Reset the update mode. */
966 pFBInfo->updateImage.pSourceBitmap.setNull();
967 pFBInfo->updateImage.pu8Address = NULL;
968 pFBInfo->updateImage.cbLine = 0;
969
970 /* Release the current source bitmap. */
971 pFBInfo->pSourceBitmap.setNull();
972
973 /* VGA blanking is signaled as w=0, h=0, bpp=0 and cbLine=0, and it's
974 * best to keep the old resolution, as otherwise the window size would
975 * change before the new resolution is known. */
976 const bool fVGABlank = fVGAResize && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
977 && w == 0 && h == 0 && bpp == 0 && cbLine == 0;
978 if (fVGABlank)
979 {
980 w = pFBInfo->w;
981 h = pFBInfo->h;
982 }
983
984 /* Update the video mode information. */
985 pFBInfo->w = w;
986 pFBInfo->h = h;
987 pFBInfo->u16BitsPerPixel = (uint16_t)bpp;
988 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM;
989 pFBInfo->u32LineSize = cbLine;
990 if (!fVGAResize)
991 {
992 /* Fields which are not used in not VBVA modes and not affected by a VGA resize. */
993 pFBInfo->flags = flags;
994 pFBInfo->xOrigin = xOrigin;
995 pFBInfo->yOrigin = yOrigin;
996 pFBInfo->fDisabled = RT_BOOL(flags & VBVA_SCREEN_F_DISABLED);
997 pFBInfo->fVBVAForceResize = false;
998 }
999 else
1000 {
1001 pFBInfo->flags = 0;
1002 if (fVGABlank)
1003 pFBInfo->flags |= VBVA_SCREEN_F_BLANK;
1004 pFBInfo->fDisabled = false;
1005 }
1006
1007 /* Prepare local vars for the notification code below. */
1008 ComPtr<IFramebuffer> pFramebuffer = pFBInfo->pFramebuffer;
1009 const bool fDisabled = pFBInfo->fDisabled;
1010
1011 alock.release();
1012
1013 if (!pFramebuffer.isNull())
1014 {
1015 HRESULT hr = pFramebuffer->NotifyChange(uScreenId, 0, 0, w, h); /** @todo origin */
1016 LogFunc(("NotifyChange hr %08X\n", hr));
1017 NOREF(hr);
1018 }
1019
1020 if (fGuestMonitorChangedEvent)
1021 {
1022 if (fDisabled)
1023 fireGuestMonitorChangedEvent(mParent->i_getEventSource(),
1024 GuestMonitorChangedEventType_Disabled,
1025 uScreenId,
1026 0, 0, 0, 0);
1027 else
1028 fireGuestMonitorChangedEvent(mParent->i_getEventSource(),
1029 GuestMonitorChangedEventType_Enabled,
1030 uScreenId,
1031 xOrigin, yOrigin, w, h);
1032 }
1033
1034 if (fNewOrigin)
1035 fireGuestMonitorChangedEvent(mParent->i_getEventSource(),
1036 GuestMonitorChangedEventType_NewOrigin,
1037 uScreenId,
1038 xOrigin, yOrigin, 0, 0);
1039
1040 /* Inform the VRDP server about the change of display parameters. */
1041 LogRelFlowFunc(("Calling VRDP\n"));
1042 mParent->i_consoleVRDPServer()->SendResize();
1043
1044 /* And re-send the seamless rectangles if necessary. */
1045 if (mfSeamlessEnabled)
1046 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1047
1048#ifdef VBOX_WITH_VIDEOREC
1049 videoCaptureScreenChanged(uScreenId);
1050#endif
1051
1052 LogRelFlowFunc(("[%d]: default format %d\n", uScreenId, pFBInfo->fDefaultFormat));
1053
1054 return VINF_SUCCESS;
1055}
1056
1057static void i_checkCoordBounds(int *px, int *py, int *pw, int *ph, int cx, int cy)
1058{
1059 /* Correct negative x and y coordinates. */
1060 if (*px < 0)
1061 {
1062 *px += *pw; /* Compute xRight which is also the new width. */
1063
1064 *pw = (*px < 0)? 0: *px;
1065
1066 *px = 0;
1067 }
1068
1069 if (*py < 0)
1070 {
1071 *py += *ph; /* Compute xBottom, which is also the new height. */
1072
1073 *ph = (*py < 0)? 0: *py;
1074
1075 *py = 0;
1076 }
1077
1078 /* Also check if coords are greater than the display resolution. */
1079 if (*px + *pw > cx)
1080 {
1081 *pw = cx > *px? cx - *px: 0;
1082 }
1083
1084 if (*py + *ph > cy)
1085 {
1086 *ph = cy > *py? cy - *py: 0;
1087 }
1088}
1089
1090void Display::i_handleDisplayUpdate(unsigned uScreenId, int x, int y, int w, int h)
1091{
1092 /*
1093 * Always runs under either VBVA lock or, for HGSMI, DevVGA lock.
1094 * Safe to use VBVA vars and take the framebuffer lock.
1095 */
1096
1097#ifdef DEBUG_sunlover
1098 LogFlowFunc(("[%d] %d,%d %dx%d\n",
1099 uScreenId, x, y, w, h));
1100#endif /* DEBUG_sunlover */
1101
1102 /* No updates for a disabled guest screen. */
1103 if (maFramebuffers[uScreenId].fDisabled)
1104 return;
1105
1106 /* No updates for a blank guest screen. */
1107 /** @note Disabled for now, as the GUI does not update the picture when we
1108 * first blank. */
1109 /* if (maFramebuffers[uScreenId].flags & VBVA_SCREEN_F_BLANK)
1110 return; */
1111
1112 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1113 AutoReadLock alockr(this COMMA_LOCKVAL_SRC_POS);
1114
1115 ComPtr<IFramebuffer> pFramebuffer = pFBInfo->pFramebuffer;
1116 ComPtr<IDisplaySourceBitmap> pSourceBitmap = pFBInfo->updateImage.pSourceBitmap;
1117
1118 alockr.release();
1119
1120 if (RT_LIKELY(!pFramebuffer.isNull()))
1121 {
1122 if (RT_LIKELY(!RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_UpdateImage)))
1123 {
1124 i_checkCoordBounds(&x, &y, &w, &h, pFBInfo->w, pFBInfo->h);
1125
1126 if (w != 0 && h != 0)
1127 {
1128 pFramebuffer->NotifyUpdate(x, y, w, h);
1129 }
1130 }
1131 else
1132 {
1133 if (RT_LIKELY(!pSourceBitmap.isNull()))
1134 { /* likely */ }
1135 else
1136 {
1137 /* Create a source bitmap if UpdateImage mode is used. */
1138 HRESULT hr = QuerySourceBitmap(uScreenId, pSourceBitmap.asOutParam());
1139 if (SUCCEEDED(hr))
1140 {
1141 BYTE *pAddress = NULL;
1142 ULONG ulWidth = 0;
1143 ULONG ulHeight = 0;
1144 ULONG ulBitsPerPixel = 0;
1145 ULONG ulBytesPerLine = 0;
1146 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
1147
1148 hr = pSourceBitmap->QueryBitmapInfo(&pAddress,
1149 &ulWidth,
1150 &ulHeight,
1151 &ulBitsPerPixel,
1152 &ulBytesPerLine,
1153 &bitmapFormat);
1154 if (SUCCEEDED(hr))
1155 {
1156 AutoWriteLock alockw(this COMMA_LOCKVAL_SRC_POS);
1157
1158 if (pFBInfo->updateImage.pSourceBitmap.isNull())
1159 {
1160 pFBInfo->updateImage.pSourceBitmap = pSourceBitmap;
1161 pFBInfo->updateImage.pu8Address = pAddress;
1162 pFBInfo->updateImage.cbLine = ulBytesPerLine;
1163 }
1164
1165 pSourceBitmap = pFBInfo->updateImage.pSourceBitmap;
1166
1167 alockw.release();
1168 }
1169 }
1170 }
1171
1172 if (RT_LIKELY(!pSourceBitmap.isNull()))
1173 {
1174 BYTE *pbAddress = NULL;
1175 ULONG ulWidth = 0;
1176 ULONG ulHeight = 0;
1177 ULONG ulBitsPerPixel = 0;
1178 ULONG ulBytesPerLine = 0;
1179 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
1180
1181 HRESULT hr = pSourceBitmap->QueryBitmapInfo(&pbAddress,
1182 &ulWidth,
1183 &ulHeight,
1184 &ulBitsPerPixel,
1185 &ulBytesPerLine,
1186 &bitmapFormat);
1187 if (SUCCEEDED(hr))
1188 {
1189 /* Make sure that the requested update is within the source bitmap dimensions. */
1190 i_checkCoordBounds(&x, &y, &w, &h, ulWidth, ulHeight);
1191
1192 if (w != 0 && h != 0)
1193 {
1194 const size_t cbData = w * h * 4;
1195 com::SafeArray<BYTE> image(cbData);
1196
1197 uint8_t *pu8Dst = image.raw();
1198 const uint8_t *pu8Src = pbAddress + ulBytesPerLine * y + x * 4;
1199
1200 int i;
1201 for (i = y; i < y + h; ++i)
1202 {
1203 memcpy(pu8Dst, pu8Src, w * 4);
1204 pu8Dst += w * 4;
1205 pu8Src += ulBytesPerLine;
1206 }
1207
1208 pFramebuffer->NotifyUpdateImage(x, y, w, h, ComSafeArrayAsInParam(image));
1209 }
1210 }
1211 }
1212 }
1213 }
1214
1215#ifndef VBOX_WITH_HGSMI
1216 if (!mVideoAccelLegacy.fVideoAccelEnabled)
1217 {
1218#else
1219 if (!mVideoAccelLegacy.fVideoAccelEnabled && !maFramebuffers[uScreenId].fVBVAEnabled)
1220 {
1221#endif /* VBOX_WITH_HGSMI */
1222 /* When VBVA is enabled, the VRDP server is informed
1223 * either in VideoAccelFlush or displayVBVAUpdateProcess.
1224 * Inform the server here only if VBVA is disabled.
1225 */
1226 mParent->i_consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
1227 }
1228}
1229
1230void Display::i_updateGuestGraphicsFacility(void)
1231{
1232 Guest* pGuest = mParent->i_getGuest();
1233 AssertPtrReturnVoid(pGuest);
1234 /* The following is from GuestImpl.cpp. */
1235 /** @todo A nit: The timestamp is wrong on saved state restore. Would be better
1236 * to move the graphics and seamless capability -> facility translation to
1237 * VMMDev so this could be saved. */
1238 RTTIMESPEC TimeSpecTS;
1239 RTTimeNow(&TimeSpecTS);
1240
1241 if ( mfVMMDevSupportsGraphics
1242 || (mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS) != 0)
1243 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1244 VBoxGuestFacilityStatus_Active,
1245 0 /*fFlags*/, &TimeSpecTS);
1246 else
1247 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1248 VBoxGuestFacilityStatus_Inactive,
1249 0 /*fFlags*/, &TimeSpecTS);
1250}
1251
1252void Display::i_handleUpdateVMMDevSupportsGraphics(bool fSupportsGraphics)
1253{
1254 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1255 if (mfVMMDevSupportsGraphics == fSupportsGraphics)
1256 return;
1257 mfVMMDevSupportsGraphics = fSupportsGraphics;
1258 i_updateGuestGraphicsFacility();
1259 /* The VMMDev interface notifies the console. */
1260}
1261
1262void Display::i_handleUpdateGuestVBVACapabilities(uint32_t fNewCapabilities)
1263{
1264 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1265 bool fNotify = (fNewCapabilities & VBVACAPS_VIDEO_MODE_HINTS) != (mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS);
1266
1267 mfGuestVBVACapabilities = fNewCapabilities;
1268 if (!fNotify)
1269 return;
1270 i_updateGuestGraphicsFacility();
1271 /* Tell the console about it */
1272 mParent->i_onAdditionsStateChange();
1273}
1274
1275void Display::i_handleUpdateVBVAInputMapping(int32_t xOrigin, int32_t yOrigin, uint32_t cx, uint32_t cy)
1276{
1277 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1278
1279 xInputMappingOrigin = xOrigin;
1280 yInputMappingOrigin = yOrigin;
1281 cxInputMapping = cx;
1282 cyInputMapping = cy;
1283
1284 /* Re-send the seamless rectangles if necessary. */
1285 if (mfSeamlessEnabled)
1286 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1287}
1288
1289/**
1290 * Returns the upper left and lower right corners of the virtual framebuffer.
1291 * The lower right is "exclusive" (i.e. first pixel beyond the framebuffer),
1292 * and the origin is (0, 0), not (1, 1) like the GUI returns.
1293 */
1294void Display::i_getFramebufferDimensions(int32_t *px1, int32_t *py1,
1295 int32_t *px2, int32_t *py2)
1296{
1297 int32_t x1 = 0, y1 = 0, x2 = 0, y2 = 0;
1298 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1299
1300 AssertPtrReturnVoid(px1);
1301 AssertPtrReturnVoid(py1);
1302 AssertPtrReturnVoid(px2);
1303 AssertPtrReturnVoid(py2);
1304 LogRelFlowFunc(("\n"));
1305
1306 if (!mpDrv)
1307 return;
1308 /* If VBVA is not in use then this flag will not be set and this
1309 * will still work as it should. */
1310 if (!maFramebuffers[0].fDisabled)
1311 {
1312 x1 = (int32_t)maFramebuffers[0].xOrigin;
1313 y1 = (int32_t)maFramebuffers[0].yOrigin;
1314 x2 = (int32_t)maFramebuffers[0].w + (int32_t)maFramebuffers[0].xOrigin;
1315 y2 = (int32_t)maFramebuffers[0].h + (int32_t)maFramebuffers[0].yOrigin;
1316 }
1317 if (cxInputMapping && cyInputMapping)
1318 {
1319 x1 = xInputMappingOrigin;
1320 y1 = yInputMappingOrigin;
1321 x2 = xInputMappingOrigin + cxInputMapping;
1322 y2 = yInputMappingOrigin + cyInputMapping;
1323 }
1324 else
1325 for (unsigned i = 1; i < mcMonitors; ++i)
1326 {
1327 if (!maFramebuffers[i].fDisabled)
1328 {
1329 x1 = RT_MIN(x1, maFramebuffers[i].xOrigin);
1330 y1 = RT_MIN(y1, maFramebuffers[i].yOrigin);
1331 x2 = RT_MAX(x2, maFramebuffers[i].xOrigin + (int32_t)maFramebuffers[i].w);
1332 y2 = RT_MAX(y2, maFramebuffers[i].yOrigin + (int32_t)maFramebuffers[i].h);
1333 }
1334 }
1335 *px1 = x1;
1336 *py1 = y1;
1337 *px2 = x2;
1338 *py2 = y2;
1339}
1340
1341HRESULT Display::i_reportHostCursorCapabilities(uint32_t fCapabilitiesAdded, uint32_t fCapabilitiesRemoved)
1342{
1343 /* Do we need this to access mParent? I presume that the safe VM pointer
1344 * ensures that mpDrv will remain valid. */
1345 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1346 uint32_t fHostCursorCapabilities = (mfHostCursorCapabilities | fCapabilitiesAdded)
1347 & ~fCapabilitiesRemoved;
1348
1349 Console::SafeVMPtr ptrVM(mParent);
1350 if (!ptrVM.isOk())
1351 return ptrVM.rc();
1352 if (mfHostCursorCapabilities == fHostCursorCapabilities)
1353 return S_OK;
1354 CHECK_CONSOLE_DRV(mpDrv);
1355 alock.release(); /* Release before calling up for lock order reasons. */
1356 mpDrv->pUpPort->pfnReportHostCursorCapabilities(mpDrv->pUpPort, fCapabilitiesAdded, fCapabilitiesRemoved);
1357 mfHostCursorCapabilities = fHostCursorCapabilities;
1358 return S_OK;
1359}
1360
1361HRESULT Display::i_reportHostCursorPosition(int32_t x, int32_t y)
1362{
1363 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1364 uint32_t xAdj = (uint32_t)RT_MAX(x - xInputMappingOrigin, 0);
1365 uint32_t yAdj = (uint32_t)RT_MAX(y - yInputMappingOrigin, 0);
1366 xAdj = RT_MIN(xAdj, cxInputMapping);
1367 yAdj = RT_MIN(yAdj, cyInputMapping);
1368
1369 Console::SafeVMPtr ptrVM(mParent);
1370 if (!ptrVM.isOk())
1371 return ptrVM.rc();
1372 CHECK_CONSOLE_DRV(mpDrv);
1373 alock.release(); /* Release before calling up for lock order reasons. */
1374 mpDrv->pUpPort->pfnReportHostCursorPosition(mpDrv->pUpPort, xAdj, yAdj);
1375 return S_OK;
1376}
1377
1378static bool displayIntersectRect(RTRECT *prectResult,
1379 const RTRECT *prect1,
1380 const RTRECT *prect2)
1381{
1382 /* Initialize result to an empty record. */
1383 memset(prectResult, 0, sizeof(RTRECT));
1384
1385 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
1386 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
1387
1388 if (xLeftResult < xRightResult)
1389 {
1390 /* There is intersection by X. */
1391
1392 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
1393 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
1394
1395 if (yTopResult < yBottomResult)
1396 {
1397 /* There is intersection by Y. */
1398
1399 prectResult->xLeft = xLeftResult;
1400 prectResult->yTop = yTopResult;
1401 prectResult->xRight = xRightResult;
1402 prectResult->yBottom = yBottomResult;
1403
1404 return true;
1405 }
1406 }
1407
1408 return false;
1409}
1410
1411int Display::i_saveVisibleRegion(uint32_t cRect, PRTRECT pRect)
1412{
1413 RTRECT *pRectVisibleRegion = NULL;
1414
1415 if (pRect == mpRectVisibleRegion)
1416 return VINF_SUCCESS;
1417 if (cRect != 0)
1418 {
1419 pRectVisibleRegion = (RTRECT *)RTMemAlloc(cRect * sizeof(RTRECT));
1420 if (!pRectVisibleRegion)
1421 {
1422 return VERR_NO_MEMORY;
1423 }
1424 memcpy(pRectVisibleRegion, pRect, cRect * sizeof(RTRECT));
1425 }
1426 if (mpRectVisibleRegion)
1427 RTMemFree(mpRectVisibleRegion);
1428 mcRectVisibleRegion = cRect;
1429 mpRectVisibleRegion = pRectVisibleRegion;
1430 return VINF_SUCCESS;
1431}
1432
1433int Display::i_handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1434{
1435 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc( RT_MAX(cRect, 1)
1436 * sizeof(RTRECT));
1437 LogRel2(("%s: cRect=%u\n", __PRETTY_FUNCTION__, cRect));
1438 if (!pVisibleRegion)
1439 {
1440 return VERR_NO_TMP_MEMORY;
1441 }
1442 int rc = i_saveVisibleRegion(cRect, pRect);
1443 if (RT_FAILURE(rc))
1444 {
1445 RTMemTmpFree(pVisibleRegion);
1446 return rc;
1447 }
1448
1449 unsigned uScreenId;
1450 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1451 {
1452 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1453
1454 if ( !pFBInfo->pFramebuffer.isNull()
1455 && RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_VisibleRegion))
1456 {
1457 /* Prepare a new array of rectangles which intersect with the framebuffer.
1458 */
1459 RTRECT rectFramebuffer;
1460 rectFramebuffer.xLeft = pFBInfo->xOrigin - xInputMappingOrigin;
1461 rectFramebuffer.yTop = pFBInfo->yOrigin - yInputMappingOrigin;
1462 rectFramebuffer.xRight = rectFramebuffer.xLeft + pFBInfo->w;
1463 rectFramebuffer.yBottom = rectFramebuffer.yTop + pFBInfo->h;
1464
1465 uint32_t cRectVisibleRegion = 0;
1466
1467 uint32_t i;
1468 for (i = 0; i < cRect; i++)
1469 {
1470 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1471 {
1472 pVisibleRegion[cRectVisibleRegion].xLeft -= rectFramebuffer.xLeft;
1473 pVisibleRegion[cRectVisibleRegion].yTop -= rectFramebuffer.yTop;
1474 pVisibleRegion[cRectVisibleRegion].xRight -= rectFramebuffer.xLeft;
1475 pVisibleRegion[cRectVisibleRegion].yBottom -= rectFramebuffer.yTop;
1476
1477 cRectVisibleRegion++;
1478 }
1479 }
1480 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1481 }
1482 }
1483
1484#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1485 VMMDev *vmmDev = mParent->i_getVMMDev();
1486 if (mfIsCr3DEnabled && vmmDev)
1487 {
1488 if (mhCrOglSvc)
1489 {
1490 VBOXCRCMDCTL_HGCM *pCtl;
1491 pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(RT_MAX(cRect, 1) * sizeof(RTRECT) + sizeof(VBOXCRCMDCTL_HGCM));
1492 if (pCtl)
1493 {
1494 RTRECT *pRectsCopy = (RTRECT*)(pCtl+1);
1495 memcpy(pRectsCopy, pRect, cRect * sizeof(RTRECT));
1496
1497 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1498 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1499
1500 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1501 pCtl->aParms[0].u.pointer.addr = pRectsCopy;
1502 pCtl->aParms[0].u.pointer.size = (uint32_t)(cRect * sizeof(RTRECT));
1503
1504 rc = i_crCtlSubmit(&pCtl->Hdr, sizeof(*pCtl), i_displayCrCmdFree, pCtl);
1505 if (!RT_SUCCESS(rc))
1506 {
1507 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1508 RTMemFree(pCtl);
1509 }
1510 }
1511 else
1512 AssertMsgFailed(("failed to allocate rects memory\n"));
1513 }
1514 else
1515 AssertMsgFailed(("mhCrOglSvc is NULL\n"));
1516 }
1517#endif
1518
1519 RTMemTmpFree(pVisibleRegion);
1520
1521 return VINF_SUCCESS;
1522}
1523
1524int Display::i_handleQueryVisibleRegion(uint32_t *pcRects, PRTRECT paRects)
1525{
1526 /// @todo Currently not used by the guest and is not implemented in
1527 /// framebuffers. Remove?
1528 RT_NOREF(pcRects, paRects);
1529 return VERR_NOT_SUPPORTED;
1530}
1531
1532#ifdef VBOX_WITH_HGSMI
1533static void vbvaSetMemoryFlagsHGSMI(unsigned uScreenId,
1534 uint32_t fu32SupportedOrders,
1535 bool fVideoAccelVRDP,
1536 DISPLAYFBINFO *pFBInfo)
1537{
1538 LogRelFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1539
1540 if (pFBInfo->pVBVAHostFlags)
1541 {
1542 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1543
1544 if (pFBInfo->fVBVAEnabled)
1545 {
1546 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1547
1548 if (fVideoAccelVRDP)
1549 {
1550 fu32HostEvents |= VBVA_F_MODE_VRDP;
1551 }
1552 }
1553
1554 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1555 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1556
1557 LogRelFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1558 }
1559}
1560
1561static void vbvaSetMemoryFlagsAllHGSMI(uint32_t fu32SupportedOrders,
1562 bool fVideoAccelVRDP,
1563 DISPLAYFBINFO *paFBInfos,
1564 unsigned cFBInfos)
1565{
1566 unsigned uScreenId;
1567
1568 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1569 {
1570 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1571 }
1572}
1573#endif /* VBOX_WITH_HGSMI */
1574
1575int Display::VideoAccelEnableVMMDev(bool fEnable, VBVAMEMORY *pVbvaMemory)
1576{
1577 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1578 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1579 if (RT_SUCCESS(rc))
1580 {
1581 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1582 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1583 }
1584 LogFlowFunc(("leave %Rrc\n", rc));
1585 return rc;
1586}
1587
1588int Display::VideoAccelEnableVGA(bool fEnable, VBVAMEMORY *pVbvaMemory)
1589{
1590 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1591 int rc = videoAccelEnterVGA(&mVideoAccelLegacy);
1592 if (RT_SUCCESS(rc))
1593 {
1594 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1595 videoAccelLeaveVGA(&mVideoAccelLegacy);
1596 }
1597 LogFlowFunc(("leave %Rrc\n", rc));
1598 return rc;
1599}
1600
1601void Display::VideoAccelFlushVMMDev(void)
1602{
1603 LogFlowFunc(("enter\n"));
1604 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1605 if (RT_SUCCESS(rc))
1606 {
1607 i_VideoAccelFlush(mpDrv->pUpPort);
1608 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1609 }
1610 LogFlowFunc(("leave\n"));
1611}
1612
1613/* Called always by one VRDP server thread. Can be thread-unsafe.
1614 */
1615void Display::i_VideoAccelVRDP(bool fEnable)
1616{
1617 LogRelFlowFunc(("fEnable = %d\n", fEnable));
1618
1619 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
1620
1621 int c = fEnable?
1622 ASMAtomicIncS32(&mcVideoAccelVRDPRefs):
1623 ASMAtomicDecS32(&mcVideoAccelVRDPRefs);
1624
1625 Assert (c >= 0);
1626
1627 /* This can run concurrently with Display videoaccel state change. */
1628 RTCritSectEnter(&mVideoAccelLock);
1629
1630 if (c == 0)
1631 {
1632 /* The last client has disconnected, and the accel can be
1633 * disabled.
1634 */
1635 Assert(fEnable == false);
1636
1637 mfVideoAccelVRDP = false;
1638 mfu32SupportedOrders = 0;
1639
1640 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1641 maFramebuffers, mcMonitors);
1642#ifdef VBOX_WITH_HGSMI
1643 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1644 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1645#endif /* VBOX_WITH_HGSMI */
1646
1647 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1648 }
1649 else if ( c == 1
1650 && !mfVideoAccelVRDP)
1651 {
1652 /* The first client has connected. Enable the accel.
1653 */
1654 Assert(fEnable == true);
1655
1656 mfVideoAccelVRDP = true;
1657 /* Supporting all orders. */
1658 mfu32SupportedOrders = UINT32_MAX;
1659
1660 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1661 maFramebuffers, mcMonitors);
1662#ifdef VBOX_WITH_HGSMI
1663 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1664 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1665#endif /* VBOX_WITH_HGSMI */
1666
1667 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1668 }
1669 else
1670 {
1671 /* A client is connected or disconnected but there is no change in the
1672 * accel state. It remains enabled.
1673 */
1674 Assert(mfVideoAccelVRDP == true);
1675 }
1676
1677 RTCritSectLeave(&mVideoAccelLock);
1678}
1679
1680void Display::i_notifyPowerDown(void)
1681{
1682 LogRelFlowFunc(("\n"));
1683
1684 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1685
1686 /* Source bitmaps are not available anymore. */
1687 mfSourceBitmapEnabled = false;
1688
1689 alock.release();
1690
1691 /* Resize all displays to tell framebuffers to forget current source bitmap. */
1692 unsigned uScreenId = mcMonitors;
1693 while (uScreenId > 0)
1694 {
1695 --uScreenId;
1696
1697 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1698 if (!pFBInfo->fDisabled)
1699 {
1700 i_handleDisplayResize(uScreenId, 32,
1701 pFBInfo->pu8FramebufferVRAM,
1702 pFBInfo->u32LineSize,
1703 pFBInfo->w,
1704 pFBInfo->h,
1705 pFBInfo->flags,
1706 pFBInfo->xOrigin,
1707 pFBInfo->yOrigin,
1708 false);
1709 }
1710 }
1711}
1712
1713// Wrapped IDisplay methods
1714/////////////////////////////////////////////////////////////////////////////
1715HRESULT Display::getScreenResolution(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel,
1716 LONG *aXOrigin, LONG *aYOrigin, GuestMonitorStatus_T *aGuestMonitorStatus)
1717{
1718 LogRelFlowFunc(("aScreenId=%RU32\n", aScreenId));
1719
1720 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1721
1722 if (aScreenId >= mcMonitors)
1723 return E_INVALIDARG;
1724
1725 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1726
1727 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
1728
1729 if (pFBInfo->flags & VBVA_SCREEN_F_DISABLED)
1730 guestMonitorStatus = GuestMonitorStatus_Disabled;
1731 else if (pFBInfo->flags & (VBVA_SCREEN_F_BLANK | VBVA_SCREEN_F_BLANK2))
1732 guestMonitorStatus = GuestMonitorStatus_Blank;
1733
1734 if (aWidth)
1735 *aWidth = pFBInfo->w;
1736 if (aHeight)
1737 *aHeight = pFBInfo->h;
1738 if (aBitsPerPixel)
1739 *aBitsPerPixel = pFBInfo->u16BitsPerPixel;
1740 if (aXOrigin)
1741 *aXOrigin = pFBInfo->xOrigin;
1742 if (aYOrigin)
1743 *aYOrigin = pFBInfo->yOrigin;
1744 if (aGuestMonitorStatus)
1745 *aGuestMonitorStatus = guestMonitorStatus;
1746
1747 return S_OK;
1748}
1749
1750
1751HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer, com::Guid &aId)
1752{
1753 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1754
1755 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1756
1757 if (aScreenId >= mcMonitors)
1758 return setError(E_INVALIDARG, tr("AttachFramebuffer: Invalid screen %d (total %d)"),
1759 aScreenId, mcMonitors);
1760
1761 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1762 if (!pFBInfo->pFramebuffer.isNull())
1763 return setError(E_FAIL, tr("AttachFramebuffer: Framebuffer already attached to %d"),
1764 aScreenId);
1765
1766 pFBInfo->pFramebuffer = aFramebuffer;
1767 pFBInfo->framebufferId.create();
1768 aId = pFBInfo->framebufferId;
1769
1770 SafeArray<FramebufferCapabilities_T> caps;
1771 pFBInfo->pFramebuffer->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(caps));
1772 pFBInfo->u32Caps = 0;
1773 size_t i;
1774 for (i = 0; i < caps.size(); ++i)
1775 pFBInfo->u32Caps |= caps[i];
1776
1777 alock.release();
1778
1779 /* The driver might not have been constructed yet */
1780 if (mpDrv)
1781 {
1782 /* Inform the framebuffer about the actual screen size. */
1783 HRESULT hr = aFramebuffer->NotifyChange(aScreenId, 0, 0, pFBInfo->w, pFBInfo->h); /** @todo origin */
1784 LogFunc(("NotifyChange hr %08X\n", hr)); NOREF(hr);
1785
1786 /* Re-send the seamless rectangles if necessary. */
1787 if (mfSeamlessEnabled)
1788 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1789 }
1790
1791 Console::SafeVMPtrQuiet ptrVM(mParent);
1792 if (ptrVM.isOk())
1793 {
1794#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1795 if (mfIsCr3DEnabled)
1796 {
1797 VBOXCRCMDCTL_HGCM data;
1798 RT_ZERO(data);
1799 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1800 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1801
1802 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1803 data.aParms[0].u.uint32 = aScreenId;
1804
1805 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1806 AssertRC(vrc);
1807 }
1808#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1809
1810 VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
1811 3, this, aScreenId, false);
1812 }
1813
1814 LogRelFlowFunc(("Attached to %d %RTuuid\n", aScreenId, aId.raw()));
1815 return S_OK;
1816}
1817
1818HRESULT Display::detachFramebuffer(ULONG aScreenId, const com::Guid &aId)
1819{
1820 LogRelFlowFunc(("aScreenId = %d %RTuuid\n", aScreenId, aId.raw()));
1821
1822 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1823
1824 if (aScreenId >= mcMonitors)
1825 return setError(E_INVALIDARG, tr("DetachFramebuffer: Invalid screen %d (total %d)"),
1826 aScreenId, mcMonitors);
1827
1828 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1829
1830 if (pFBInfo->framebufferId != aId)
1831 {
1832 LogRelFlowFunc(("Invalid framebuffer aScreenId = %d, attached %p\n", aScreenId, pFBInfo->framebufferId.raw()));
1833 return setError(E_FAIL, tr("DetachFramebuffer: Invalid framebuffer object"));
1834 }
1835
1836 pFBInfo->pFramebuffer.setNull();
1837 pFBInfo->framebufferId.clear();
1838
1839 alock.release();
1840
1841#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1842 Console::SafeVMPtrQuiet ptrVM(mParent);
1843 if (ptrVM.isOk())
1844 {
1845 if (mfIsCr3DEnabled)
1846 {
1847 VBOXCRCMDCTL_HGCM data;
1848 RT_ZERO(data);
1849 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1850 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1851
1852 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1853 data.aParms[0].u.uint32 = aScreenId;
1854
1855 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1856 AssertRC(vrc);
1857 }
1858 }
1859#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1860
1861 return S_OK;
1862}
1863
1864HRESULT Display::queryFramebuffer(ULONG aScreenId, ComPtr<IFramebuffer> &aFramebuffer)
1865{
1866 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1867
1868 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1869
1870 if (aScreenId >= mcMonitors)
1871 return setError(E_INVALIDARG, tr("QueryFramebuffer: Invalid screen %d (total %d)"),
1872 aScreenId, mcMonitors);
1873
1874 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1875
1876 pFBInfo->pFramebuffer.queryInterfaceTo(aFramebuffer.asOutParam());
1877
1878 return S_OK;
1879}
1880
1881HRESULT Display::setVideoModeHint(ULONG aDisplay, BOOL aEnabled,
1882 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
1883 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel)
1884{
1885 if (aWidth == 0 || aHeight == 0 || aBitsPerPixel == 0)
1886 {
1887 /* Some of parameters must not change. Query current mode. */
1888 ULONG ulWidth = 0;
1889 ULONG ulHeight = 0;
1890 ULONG ulBitsPerPixel = 0;
1891 HRESULT hr = getScreenResolution(aDisplay, &ulWidth, &ulHeight, &ulBitsPerPixel, NULL, NULL, NULL);
1892 if (FAILED(hr))
1893 return hr;
1894
1895 /* Assign current values to not changing parameters. */
1896 if (aWidth == 0)
1897 aWidth = ulWidth;
1898 if (aHeight == 0)
1899 aHeight = ulHeight;
1900 if (aBitsPerPixel == 0)
1901 aBitsPerPixel = ulBitsPerPixel;
1902 }
1903
1904 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1905
1906 if (aDisplay >= mcMonitors)
1907 return E_INVALIDARG;
1908
1909 CHECK_CONSOLE_DRV(mpDrv);
1910
1911 /*
1912 * It is up to the guest to decide whether the hint is
1913 * valid. Therefore don't do any VRAM sanity checks here.
1914 */
1915
1916 /* Have to release the lock because the pfnRequestDisplayChange
1917 * will call EMT. */
1918 alock.release();
1919
1920 /* We always send the hint to the graphics card in case the guest enables
1921 * support later. For now we notify exactly when support is enabled. */
1922 mpDrv->pUpPort->pfnSendModeHint(mpDrv->pUpPort, aWidth, aHeight,
1923 aBitsPerPixel, aDisplay,
1924 aChangeOrigin ? aOriginX : ~0,
1925 aChangeOrigin ? aOriginY : ~0,
1926 RT_BOOL(aEnabled),
1927 mfGuestVBVACapabilities
1928 & VBVACAPS_VIDEO_MODE_HINTS);
1929 if ( mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS
1930 && !(mfGuestVBVACapabilities & VBVACAPS_IRQ))
1931 {
1932 mParent->i_sendACPIMonitorHotPlugEvent();
1933 }
1934
1935 /* We currently never suppress the VMMDev hint if the guest has requested
1936 * it. Specifically the video graphics driver may not be responsible for
1937 * screen positioning in the guest virtual desktop, and the component
1938 * responsible may want to get the hint from VMMDev. */
1939 VMMDev *pVMMDev = mParent->i_getVMMDev();
1940 if (pVMMDev)
1941 {
1942 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1943 if (pVMMDevPort)
1944 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel,
1945 aDisplay, aOriginX, aOriginY,
1946 RT_BOOL(aEnabled), RT_BOOL(aChangeOrigin));
1947 }
1948 return S_OK;
1949}
1950
1951HRESULT Display::setSeamlessMode(BOOL enabled)
1952{
1953 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1954
1955 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
1956 alock.release();
1957
1958 VMMDev *pVMMDev = mParent->i_getVMMDev();
1959 if (pVMMDev)
1960 {
1961 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1962 if (pVMMDevPort)
1963 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
1964 }
1965 mfSeamlessEnabled = RT_BOOL(enabled);
1966
1967#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1968 if (!enabled)
1969 {
1970 VMMDev *vmmDev = mParent->i_getVMMDev();
1971 if (mfIsCr3DEnabled && vmmDev)
1972 {
1973 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(VBOXCRCMDCTL_HGCM));
1974 if (!pData)
1975 {
1976 AssertMsgFailed(("RTMemAlloc failed\n"));
1977 return VERR_NO_MEMORY;
1978 }
1979
1980 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1981 pData->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1982
1983 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1984 pData->aParms[0].u.pointer.addr = NULL;
1985 pData->aParms[0].u.pointer.size = 0; /* <- means null rects, NULL pRects address and 0 rects means "disable" */
1986
1987 int rc = i_crCtlSubmit(&pData->Hdr, sizeof(*pData), i_displayCrCmdFree, pData);
1988 if (!RT_SUCCESS(rc))
1989 {
1990 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1991 RTMemFree(pData);
1992 }
1993 }
1994 }
1995#endif
1996 return S_OK;
1997}
1998
1999#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2000BOOL Display::i_displayCheckTakeScreenshotCrOgl(Display *pDisplay, ULONG aScreenId, uint8_t *pbData,
2001 uint32_t u32Width, uint32_t u32Height)
2002{
2003 if ( pDisplay->mfIsCr3DEnabled
2004 && pDisplay->mCrOglCallbacks.pfnHasData
2005 && pDisplay->mCrOglCallbacks.pfnHasData())
2006 {
2007 VMMDev *pVMMDev = pDisplay->mParent->i_getVMMDev();
2008 if (pVMMDev)
2009 {
2010 CRVBOXHGCMTAKESCREENSHOT *pScreenshot = (CRVBOXHGCMTAKESCREENSHOT *)RTMemAlloc(sizeof(*pScreenshot));
2011 if (pScreenshot)
2012 {
2013 /* screen id or CRSCREEN_ALL to specify all enabled */
2014 pScreenshot->u32Screen = aScreenId;
2015 pScreenshot->u32Width = u32Width;
2016 pScreenshot->u32Height = u32Height;
2017 pScreenshot->u32Pitch = u32Width * 4;
2018 pScreenshot->pvBuffer = pbData;
2019 pScreenshot->pvContext = NULL;
2020 pScreenshot->pfnScreenshotBegin = NULL;
2021 pScreenshot->pfnScreenshotPerform = NULL;
2022 pScreenshot->pfnScreenshotEnd = NULL;
2023
2024 VBOXCRCMDCTL_HGCM data;
2025 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2026 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
2027
2028 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2029 data.aParms[0].u.pointer.addr = pScreenshot;
2030 data.aParms[0].u.pointer.size = sizeof(*pScreenshot);
2031
2032 int rc = pDisplay->i_crCtlSubmitSync(&data.Hdr, sizeof(data));
2033
2034 RTMemFree(pScreenshot);
2035
2036 if (RT_SUCCESS(rc))
2037 return TRUE;
2038 AssertMsgFailed(("failed to get screenshot data from crOgl (rc=%Rrc)\n", rc));
2039 /* fall back to the non-3d mechanism */
2040 }
2041 }
2042 }
2043 return FALSE;
2044}
2045#endif
2046
2047/* static */
2048int Display::i_displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppbData, size_t *pcbData,
2049 uint32_t *pcx, uint32_t *pcy, bool *pfMemFree)
2050{
2051 int rc;
2052 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
2053 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
2054 {
2055 if (pDisplay->mpDrv)
2056 {
2057 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
2058 *pfMemFree = false;
2059 }
2060 else
2061 {
2062 /* No image. */
2063 *ppbData = NULL;
2064 *pcbData = 0;
2065 *pcx = 0;
2066 *pcy = 0;
2067 *pfMemFree = true;
2068 rc = VINF_SUCCESS;
2069 }
2070 }
2071 else if (aScreenId < pDisplay->mcMonitors)
2072 {
2073 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2074
2075 uint32_t width = pFBInfo->w;
2076 uint32_t height = pFBInfo->h;
2077
2078 /* Allocate 32 bit per pixel bitmap. */
2079 size_t cbRequired = width * 4 * height;
2080
2081 if (cbRequired)
2082 {
2083 uint8_t *pbDst = (uint8_t *)RTMemAlloc(cbRequired);
2084 if (pbDst != NULL)
2085 {
2086 if (pFBInfo->flags & VBVA_SCREEN_F_ACTIVE)
2087 {
2088 /* Copy guest VRAM to the allocated 32bpp buffer. */
2089 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2090 int32_t xSrc = 0;
2091 int32_t ySrc = 0;
2092 uint32_t u32SrcWidth = width;
2093 uint32_t u32SrcHeight = height;
2094 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2095 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2096
2097 int32_t xDst = 0;
2098 int32_t yDst = 0;
2099 uint32_t u32DstWidth = u32SrcWidth;
2100 uint32_t u32DstHeight = u32SrcHeight;
2101 uint32_t u32DstLineSize = u32DstWidth * 4;
2102 uint32_t u32DstBitsPerPixel = 32;
2103
2104 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2105 width, height,
2106 pu8Src,
2107 xSrc, ySrc,
2108 u32SrcWidth, u32SrcHeight,
2109 u32SrcLineSize, u32SrcBitsPerPixel,
2110 pbDst,
2111 xDst, yDst,
2112 u32DstWidth, u32DstHeight,
2113 u32DstLineSize, u32DstBitsPerPixel);
2114 }
2115 else
2116 {
2117 memset(pbDst, 0, cbRequired);
2118 rc = VINF_SUCCESS;
2119 }
2120 if (RT_SUCCESS(rc))
2121 {
2122 *ppbData = pbDst;
2123 *pcbData = cbRequired;
2124 *pcx = width;
2125 *pcy = height;
2126 *pfMemFree = true;
2127 }
2128 else
2129 {
2130 RTMemFree(pbDst);
2131
2132 /* CopyRect can fail if VBVA was paused in VGA device, retry using the generic method. */
2133 if ( rc == VERR_INVALID_STATE
2134 && aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2135 {
2136 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
2137 *pfMemFree = false;
2138 }
2139 }
2140 }
2141 else
2142 rc = VERR_NO_MEMORY;
2143 }
2144 else
2145 {
2146 /* No image. */
2147 *ppbData = NULL;
2148 *pcbData = 0;
2149 *pcx = 0;
2150 *pcy = 0;
2151 *pfMemFree = true;
2152 rc = VINF_SUCCESS;
2153 }
2154 }
2155 else
2156 rc = VERR_INVALID_PARAMETER;
2157 return rc;
2158}
2159
2160static int i_displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
2161 BYTE *address, ULONG width, ULONG height)
2162{
2163#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2164 /*
2165 * CrOgl screenshot hook/hack.
2166 */
2167 if (Display::i_displayCheckTakeScreenshotCrOgl(pDisplay, aScreenId, (uint8_t *)address, width, height))
2168 return VINF_SUCCESS;
2169#endif
2170
2171 uint8_t *pbData = NULL;
2172 size_t cbData = 0;
2173 uint32_t cx = 0;
2174 uint32_t cy = 0;
2175 bool fFreeMem = false;
2176 int vrc = VINF_SUCCESS;
2177
2178 int cRetries = 5;
2179 while (cRetries-- > 0)
2180 {
2181 /* Note! Not sure if the priority call is such a good idea here, but
2182 it would be nice to have an accurate screenshot for the bug
2183 report if the VM deadlocks. */
2184 vrc = VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)Display::i_displayTakeScreenshotEMT, 7,
2185 pDisplay, aScreenId, &pbData, &cbData, &cx, &cy, &fFreeMem);
2186 if (vrc != VERR_TRY_AGAIN)
2187 {
2188 break;
2189 }
2190
2191 RTThreadSleep(10);
2192 }
2193
2194 if (RT_SUCCESS(vrc) && pbData)
2195 {
2196 if (cx == width && cy == height)
2197 {
2198 /* No scaling required. */
2199 memcpy(address, pbData, cbData);
2200 }
2201 else
2202 {
2203 /* Scale. */
2204 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2205
2206 uint8_t *dst = address;
2207 uint8_t *src = pbData;
2208 int dstW = width;
2209 int dstH = height;
2210 int srcW = cx;
2211 int srcH = cy;
2212 int iDeltaLine = cx * 4;
2213
2214 BitmapScale32(dst,
2215 dstW, dstH,
2216 src,
2217 iDeltaLine,
2218 srcW, srcH);
2219 }
2220
2221 if (fFreeMem)
2222 RTMemFree(pbData);
2223 else
2224 {
2225 /* This can be called from any thread. */
2226 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pbData);
2227 }
2228 }
2229
2230 return vrc;
2231}
2232
2233HRESULT Display::takeScreenShotWorker(ULONG aScreenId,
2234 BYTE *aAddress,
2235 ULONG aWidth,
2236 ULONG aHeight,
2237 BitmapFormat_T aBitmapFormat,
2238 ULONG *pcbOut)
2239{
2240 HRESULT rc = S_OK;
2241
2242 /* Do not allow too small and too large screenshots. This also filters out negative
2243 * values passed as either 'aWidth' or 'aHeight'.
2244 */
2245 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2246 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2247
2248 if ( aBitmapFormat != BitmapFormat_BGR0
2249 && aBitmapFormat != BitmapFormat_BGRA
2250 && aBitmapFormat != BitmapFormat_RGBA
2251 && aBitmapFormat != BitmapFormat_PNG)
2252 {
2253 return setError(E_NOTIMPL,
2254 tr("Unsupported screenshot format 0x%08X"), aBitmapFormat);
2255 }
2256
2257 Console::SafeVMPtr ptrVM(mParent);
2258 if (!ptrVM.isOk())
2259 return ptrVM.rc();
2260
2261 int vrc = i_displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, aAddress, aWidth, aHeight);
2262
2263 if (RT_SUCCESS(vrc))
2264 {
2265 const size_t cbData = aWidth * 4 * aHeight;
2266
2267 /* Most of uncompressed formats. */
2268 *pcbOut = (ULONG)cbData;
2269
2270 if (aBitmapFormat == BitmapFormat_BGR0)
2271 {
2272 /* Do nothing. */
2273 }
2274 else if (aBitmapFormat == BitmapFormat_BGRA)
2275 {
2276 uint32_t *pu32 = (uint32_t *)aAddress;
2277 size_t cPixels = aWidth * aHeight;
2278 while (cPixels--)
2279 {
2280 *pu32++ |= UINT32_C(0xFF000000);
2281 }
2282 }
2283 else if (aBitmapFormat == BitmapFormat_RGBA)
2284 {
2285 uint8_t *pu8 = aAddress;
2286 size_t cPixels = aWidth * aHeight;
2287 while (cPixels--)
2288 {
2289 uint8_t u8 = pu8[0];
2290 pu8[0] = pu8[2];
2291 pu8[2] = u8;
2292 pu8[3] = 0xFF;
2293
2294 pu8 += 4;
2295 }
2296 }
2297 else if (aBitmapFormat == BitmapFormat_PNG)
2298 {
2299 uint8_t *pu8PNG = NULL;
2300 uint32_t cbPNG = 0;
2301 uint32_t cxPNG = 0;
2302 uint32_t cyPNG = 0;
2303
2304 vrc = DisplayMakePNG(aAddress, aWidth, aHeight, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2305 if (RT_SUCCESS(vrc))
2306 {
2307 if (cbPNG <= cbData)
2308 {
2309 memcpy(aAddress, pu8PNG, cbPNG);
2310 *pcbOut = cbPNG;
2311 }
2312 else
2313 {
2314 rc = setError(E_FAIL,
2315 tr("PNG is larger than 32bpp bitmap"));
2316 }
2317 }
2318 else
2319 {
2320 rc = setError(VBOX_E_IPRT_ERROR,
2321 tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2322 }
2323 RTMemFree(pu8PNG);
2324 }
2325 }
2326 else if (vrc == VERR_TRY_AGAIN)
2327 rc = setError(E_UNEXPECTED,
2328 tr("Screenshot is not available at this time"));
2329 else if (RT_FAILURE(vrc))
2330 rc = setError(VBOX_E_IPRT_ERROR,
2331 tr("Could not take a screenshot (%Rrc)"), vrc);
2332
2333 return rc;
2334}
2335
2336HRESULT Display::takeScreenShot(ULONG aScreenId,
2337 BYTE *aAddress,
2338 ULONG aWidth,
2339 ULONG aHeight,
2340 BitmapFormat_T aBitmapFormat)
2341{
2342 HRESULT rc = S_OK;
2343
2344 LogRelFlowFunc(("[%d] address=%p, width=%d, height=%d, format 0x%08X\n",
2345 aScreenId, aAddress, aWidth, aHeight, aBitmapFormat));
2346
2347 ULONG cbOut = 0;
2348 rc = takeScreenShotWorker(aScreenId, aAddress, aWidth, aHeight, aBitmapFormat, &cbOut);
2349 NOREF(cbOut);
2350
2351 LogRelFlowFunc(("%Rhrc\n", rc));
2352 return rc;
2353}
2354
2355HRESULT Display::takeScreenShotToArray(ULONG aScreenId,
2356 ULONG aWidth,
2357 ULONG aHeight,
2358 BitmapFormat_T aBitmapFormat,
2359 std::vector<BYTE> &aScreenData)
2360{
2361 HRESULT rc = S_OK;
2362
2363 LogRelFlowFunc(("[%d] width=%d, height=%d, format 0x%08X\n",
2364 aScreenId, aWidth, aHeight, aBitmapFormat));
2365
2366 /* Do not allow too small and too large screenshots. This also filters out negative
2367 * values passed as either 'aWidth' or 'aHeight'.
2368 */
2369 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2370 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2371
2372 const size_t cbData = aWidth * 4 * aHeight;
2373 aScreenData.resize(cbData);
2374
2375 ULONG cbOut = 0;
2376 rc = takeScreenShotWorker(aScreenId, &aScreenData.front(), aWidth, aHeight, aBitmapFormat, &cbOut);
2377 if (FAILED(rc))
2378 cbOut = 0;
2379
2380 aScreenData.resize(cbOut);
2381
2382 LogRelFlowFunc(("%Rhrc\n", rc));
2383 return rc;
2384}
2385
2386int Display::i_videoCaptureEnableScreens(ComSafeArrayIn(BOOL, aScreens))
2387{
2388#ifdef VBOX_WITH_VIDEOREC
2389 com::SafeArray<BOOL> Screens(ComSafeArrayInArg(aScreens));
2390 for (unsigned i = 0; i < Screens.size(); i++)
2391 {
2392 bool fChanged = maVideoRecEnabled[i] != RT_BOOL(Screens[i]);
2393
2394 maVideoRecEnabled[i] = RT_BOOL(Screens[i]);
2395
2396 if (fChanged && i < mcMonitors)
2397 videoCaptureScreenChanged(i);
2398
2399 }
2400 return VINF_SUCCESS;
2401#else
2402 ComSafeArrayNoRef(aScreens);
2403 return VERR_NOT_SUPPORTED;
2404#endif
2405}
2406
2407/**
2408 * Sends belonging audio samples to the video capturing code.
2409 * Does nothing if capturing is disabled or if audio support for video capturing is disabled.
2410 *
2411 * @returns IPRT status code.
2412 * @param pvData Audio data.
2413 * @param cbData Size (in bytes) of audio data.
2414 * @param uTimestampMs Timestamp (in ms) of the audio data.
2415 */
2416int Display::i_videoCaptureSendAudio(const void *pvData, size_t cbData, uint64_t uTimestampMs)
2417{
2418#ifdef VBOX_WITH_AUDIO_VIDEOREC
2419 if (!VideoRecIsEnabled(mpVideoRecCtx))
2420 return VINF_SUCCESS;
2421
2422 return VideoRecSendAudioFrame(mpVideoRecCtx, pvData, cbData, uTimestampMs);
2423#else
2424 RT_NOREF(pvData, cbData, uTimestampMs);
2425 return VERR_NOT_SUPPORTED;
2426#endif
2427}
2428
2429/**
2430 * Start video capturing. Does nothing if capturing is already active.
2431 *
2432 * @returns IPRT status code.
2433 */
2434int Display::i_videoCaptureStart(void)
2435{
2436#ifdef VBOX_WITH_VIDEOREC
2437 if (VideoRecIsEnabled(mpVideoRecCtx))
2438 return VINF_SUCCESS;
2439
2440 int rc = VideoRecContextCreate(mcMonitors, &mpVideoRecCtx);
2441 if (RT_FAILURE(rc))
2442 {
2443 LogFlow(("Failed to create video recording context (%Rrc)!\n", rc));
2444 return rc;
2445 }
2446 ComPtr<IMachine> pMachine = mParent->i_machine();
2447 com::SafeArray<BOOL> screens;
2448 HRESULT hrc = pMachine->COMGETTER(VideoCaptureScreens)(ComSafeArrayAsOutParam(screens));
2449 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2450 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
2451 maVideoRecEnabled[i] = i < screens.size() && screens[i];
2452 ULONG ulWidth;
2453 hrc = pMachine->COMGETTER(VideoCaptureWidth)(&ulWidth);
2454 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2455 ULONG ulHeight;
2456 hrc = pMachine->COMGETTER(VideoCaptureHeight)(&ulHeight);
2457 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2458 ULONG ulRate;
2459 hrc = pMachine->COMGETTER(VideoCaptureRate)(&ulRate);
2460 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2461 ULONG ulFPS;
2462 hrc = pMachine->COMGETTER(VideoCaptureFPS)(&ulFPS);
2463 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2464 BSTR strFile;
2465 hrc = pMachine->COMGETTER(VideoCaptureFile)(&strFile);
2466 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2467 ULONG ulMaxTime;
2468 hrc = pMachine->COMGETTER(VideoCaptureMaxTime)(&ulMaxTime);
2469 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2470 ULONG ulMaxSize;
2471 hrc = pMachine->COMGETTER(VideoCaptureMaxFileSize)(&ulMaxSize);
2472 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2473 BSTR strOptions;
2474 hrc = pMachine->COMGETTER(VideoCaptureOptions)(&strOptions);
2475 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2476
2477 RTTIMESPEC ts;
2478 RTTimeNow(&ts);
2479 RTTIME time;
2480 RTTimeExplode(&time, &ts);
2481 for (unsigned uScreen = 0; uScreen < mcMonitors; uScreen++)
2482 {
2483 char *pszAbsPath = RTPathAbsDup(com::Utf8Str(strFile).c_str());
2484 char *pszSuff = RTPathSuffix(pszAbsPath);
2485 if (pszSuff)
2486 pszSuff = RTStrDup(pszSuff);
2487 RTPathStripSuffix(pszAbsPath);
2488 if (!pszAbsPath)
2489 rc = VERR_INVALID_PARAMETER;
2490 if (!pszSuff)
2491 pszSuff = RTStrDup(".webm");
2492 char *pszName = NULL;
2493 if (RT_SUCCESS(rc))
2494 {
2495 if (mcMonitors > 1)
2496 rc = RTStrAPrintf(&pszName, "%s-%u%s", pszAbsPath, uScreen+1, pszSuff);
2497 else
2498 rc = RTStrAPrintf(&pszName, "%s%s", pszAbsPath, pszSuff);
2499 }
2500 if (RT_SUCCESS(rc))
2501 {
2502 rc = VideoRecStreamInit(mpVideoRecCtx, uScreen,
2503 pszName, ulWidth, ulHeight,
2504 ulRate, ulFPS, ulMaxTime,
2505 ulMaxSize, com::Utf8Str(strOptions).c_str());
2506 if (rc == VERR_ALREADY_EXISTS)
2507 {
2508 RTStrFree(pszName);
2509 pszName = NULL;
2510
2511 if (mcMonitors > 1)
2512 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ-%u%s",
2513 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2514 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2515 uScreen+1, pszSuff);
2516 else
2517 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ%s",
2518 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2519 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2520 pszSuff);
2521 if (RT_SUCCESS(rc))
2522 rc = VideoRecStreamInit(mpVideoRecCtx, uScreen,
2523 pszName, ulWidth, ulHeight, ulRate,
2524 ulFPS, ulMaxTime,
2525 ulMaxSize, com::Utf8Str(strOptions).c_str());
2526 }
2527 }
2528
2529 if (RT_SUCCESS(rc))
2530 {
2531 videoCaptureScreenChanged(uScreen);
2532 }
2533 else
2534 LogRel(("Display::VideoCaptureStart: Failed to initialize video recording context #%u, (%Rrc)\n", uScreen, rc));
2535
2536 RTStrFree(pszName);
2537 RTStrFree(pszSuff);
2538 RTStrFree(pszAbsPath);
2539 }
2540 return rc;
2541#else /* VBOX_WITH_VIDEOREC */
2542 return VERR_NOT_SUPPORTED;
2543#endif
2544}
2545
2546/**
2547 * Stop video capturing. Does nothing if video capturing is not active.
2548 */
2549void Display::i_videoCaptureStop()
2550{
2551#ifdef VBOX_WITH_VIDEOREC
2552 if (!VideoRecIsEnabled(mpVideoRecCtx))
2553 return;
2554
2555 VideoRecContextDestroy(mpVideoRecCtx);
2556 mpVideoRecCtx = NULL;
2557
2558 unsigned uScreenId;
2559 for (uScreenId = 0; uScreenId < mcMonitors; ++uScreenId)
2560 videoCaptureScreenChanged(uScreenId);
2561#endif
2562}
2563
2564#ifdef VBOX_WITH_VIDEOREC
2565void Display::videoCaptureScreenChanged(unsigned uScreenId)
2566{
2567 if ( !VideoRecIsEnabled(mpVideoRecCtx)
2568 || !maVideoRecEnabled[uScreenId])
2569 {
2570 /* Skip recording this screen. */
2571 return;
2572 }
2573
2574 /* Get a new source bitmap which will be used by video capture code. */
2575 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
2576 QuerySourceBitmap(uScreenId, pSourceBitmap.asOutParam());
2577
2578 int rc2 = RTCritSectEnter(&mVideoCaptureLock);
2579 if (RT_SUCCESS(rc2))
2580 {
2581 maFramebuffers[uScreenId].videoCapture.pSourceBitmap = pSourceBitmap;
2582
2583 rc2 = RTCritSectLeave(&mVideoCaptureLock);
2584 AssertRC(rc2);
2585 }
2586}
2587#endif /* VBOX_WITH_VIDEOREC */
2588
2589int Display::i_drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address,
2590 ULONG x, ULONG y, ULONG width, ULONG height)
2591{
2592 int rc = VINF_SUCCESS;
2593
2594 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2595
2596 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2597 {
2598 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2599 }
2600 else if (aScreenId < pDisplay->mcMonitors)
2601 {
2602 /* Copy the bitmap to the guest VRAM. */
2603 const uint8_t *pu8Src = address;
2604 int32_t xSrc = 0;
2605 int32_t ySrc = 0;
2606 uint32_t u32SrcWidth = width;
2607 uint32_t u32SrcHeight = height;
2608 uint32_t u32SrcLineSize = width * 4;
2609 uint32_t u32SrcBitsPerPixel = 32;
2610
2611 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2612 int32_t xDst = x;
2613 int32_t yDst = y;
2614 uint32_t u32DstWidth = pFBInfo->w;
2615 uint32_t u32DstHeight = pFBInfo->h;
2616 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2617 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2618
2619 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2620 width, height,
2621 pu8Src,
2622 xSrc, ySrc,
2623 u32SrcWidth, u32SrcHeight,
2624 u32SrcLineSize, u32SrcBitsPerPixel,
2625 pu8Dst,
2626 xDst, yDst,
2627 u32DstWidth, u32DstHeight,
2628 u32DstLineSize, u32DstBitsPerPixel);
2629 if (RT_SUCCESS(rc))
2630 {
2631 if (!pFBInfo->pSourceBitmap.isNull())
2632 {
2633 /* Update the changed screen area. When source bitmap uses VRAM directly, just notify
2634 * frontend to update. And for default format, render the guest VRAM to the source bitmap.
2635 */
2636 if ( pFBInfo->fDefaultFormat
2637 && !pFBInfo->fDisabled)
2638 {
2639 BYTE *pAddress = NULL;
2640 ULONG ulWidth = 0;
2641 ULONG ulHeight = 0;
2642 ULONG ulBitsPerPixel = 0;
2643 ULONG ulBytesPerLine = 0;
2644 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2645
2646 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2647 &ulWidth,
2648 &ulHeight,
2649 &ulBitsPerPixel,
2650 &ulBytesPerLine,
2651 &bitmapFormat);
2652 if (SUCCEEDED(hrc))
2653 {
2654 pu8Src = pFBInfo->pu8FramebufferVRAM;
2655 xSrc = x;
2656 ySrc = y;
2657 u32SrcWidth = pFBInfo->w;
2658 u32SrcHeight = pFBInfo->h;
2659 u32SrcLineSize = pFBInfo->u32LineSize;
2660 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2661
2662 /* Default format is 32 bpp. */
2663 pu8Dst = pAddress;
2664 xDst = xSrc;
2665 yDst = ySrc;
2666 u32DstWidth = u32SrcWidth;
2667 u32DstHeight = u32SrcHeight;
2668 u32DstLineSize = u32DstWidth * 4;
2669 u32DstBitsPerPixel = 32;
2670
2671 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2672 width, height,
2673 pu8Src,
2674 xSrc, ySrc,
2675 u32SrcWidth, u32SrcHeight,
2676 u32SrcLineSize, u32SrcBitsPerPixel,
2677 pu8Dst,
2678 xDst, yDst,
2679 u32DstWidth, u32DstHeight,
2680 u32DstLineSize, u32DstBitsPerPixel);
2681 }
2682 }
2683 }
2684
2685 pDisplay->i_handleDisplayUpdate(aScreenId, x, y, width, height);
2686 }
2687 }
2688 else
2689 {
2690 rc = VERR_INVALID_PARAMETER;
2691 }
2692
2693 if (RT_SUCCESS(rc))
2694 pDisplay->mParent->i_consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
2695
2696 return rc;
2697}
2698
2699HRESULT Display::drawToScreen(ULONG aScreenId, BYTE *aAddress, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2700{
2701 /// @todo (r=dmik) this function may take too long to complete if the VM
2702 // is doing something like saving state right now. Which, in case if it
2703 // is called on the GUI thread, will make it unresponsive. We should
2704 // check the machine state here (by enclosing the check and VMRequCall
2705 // within the Console lock to make it atomic).
2706
2707 LogRelFlowFunc(("aAddress=%p, x=%d, y=%d, width=%d, height=%d\n",
2708 (void *)aAddress, aX, aY, aWidth, aHeight));
2709
2710 CheckComArgExpr(aWidth, aWidth != 0);
2711 CheckComArgExpr(aHeight, aHeight != 0);
2712
2713 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2714
2715 CHECK_CONSOLE_DRV(mpDrv);
2716
2717 Console::SafeVMPtr ptrVM(mParent);
2718 if (!ptrVM.isOk())
2719 return ptrVM.rc();
2720
2721 /* Release lock because the call scheduled on EMT may also try to take it. */
2722 alock.release();
2723
2724 /*
2725 * Again we're lazy and make the graphics device do all the
2726 * dirty conversion work.
2727 */
2728 int rcVBox = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_drawToScreenEMT, 7,
2729 this, aScreenId, aAddress, aX, aY, aWidth, aHeight);
2730
2731 /*
2732 * If the function returns not supported, we'll have to do all the
2733 * work ourselves using the framebuffer.
2734 */
2735 HRESULT rc = S_OK;
2736 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2737 {
2738 /** @todo implement generic fallback for screen blitting. */
2739 rc = E_NOTIMPL;
2740 }
2741 else if (RT_FAILURE(rcVBox))
2742 rc = setError(VBOX_E_IPRT_ERROR,
2743 tr("Could not draw to the screen (%Rrc)"), rcVBox);
2744/// @todo
2745// else
2746// {
2747// /* All ok. Redraw the screen. */
2748// handleDisplayUpdate(x, y, width, height);
2749// }
2750
2751 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2752 return rc;
2753}
2754
2755int Display::i_InvalidateAndUpdateEMT(Display *pDisplay, unsigned uId, bool fUpdateAll)
2756{
2757 LogRelFlowFunc(("uId=%d, fUpdateAll %d\n", uId, fUpdateAll));
2758
2759 unsigned uScreenId;
2760 for (uScreenId = (fUpdateAll ? 0 : uId); uScreenId < pDisplay->mcMonitors; uScreenId++)
2761 {
2762 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2763
2764 if ( !pFBInfo->fVBVAEnabled
2765 && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2766 {
2767 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort, /* fFailOnResize = */ true);
2768 }
2769 else
2770 {
2771 if (!pFBInfo->fDisabled)
2772 {
2773 /* Render complete VRAM screen to the framebuffer.
2774 * When framebuffer uses VRAM directly, just notify it to update.
2775 */
2776 if (pFBInfo->fDefaultFormat && !pFBInfo->pSourceBitmap.isNull())
2777 {
2778 BYTE *pAddress = NULL;
2779 ULONG ulWidth = 0;
2780 ULONG ulHeight = 0;
2781 ULONG ulBitsPerPixel = 0;
2782 ULONG ulBytesPerLine = 0;
2783 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2784
2785 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2786 &ulWidth,
2787 &ulHeight,
2788 &ulBitsPerPixel,
2789 &ulBytesPerLine,
2790 &bitmapFormat);
2791 if (SUCCEEDED(hrc))
2792 {
2793 uint32_t width = pFBInfo->w;
2794 uint32_t height = pFBInfo->h;
2795
2796 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2797 int32_t xSrc = 0;
2798 int32_t ySrc = 0;
2799 uint32_t u32SrcWidth = pFBInfo->w;
2800 uint32_t u32SrcHeight = pFBInfo->h;
2801 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2802 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2803
2804 /* Default format is 32 bpp. */
2805 uint8_t *pu8Dst = pAddress;
2806 int32_t xDst = xSrc;
2807 int32_t yDst = ySrc;
2808 uint32_t u32DstWidth = u32SrcWidth;
2809 uint32_t u32DstHeight = u32SrcHeight;
2810 uint32_t u32DstLineSize = u32DstWidth * 4;
2811 uint32_t u32DstBitsPerPixel = 32;
2812
2813 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
2814 * implies resize of Framebuffer is in progress and
2815 * copyrect should not be called.
2816 */
2817 if (ulWidth == pFBInfo->w && ulHeight == pFBInfo->h)
2818 {
2819 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2820 width, height,
2821 pu8Src,
2822 xSrc, ySrc,
2823 u32SrcWidth, u32SrcHeight,
2824 u32SrcLineSize, u32SrcBitsPerPixel,
2825 pu8Dst,
2826 xDst, yDst,
2827 u32DstWidth, u32DstHeight,
2828 u32DstLineSize, u32DstBitsPerPixel);
2829 }
2830 }
2831 }
2832
2833 pDisplay->i_handleDisplayUpdate(uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2834 }
2835 }
2836 if (!fUpdateAll)
2837 break;
2838 }
2839 LogRelFlowFunc(("done\n"));
2840 return VINF_SUCCESS;
2841}
2842
2843/**
2844 * Does a full invalidation of the VM display and instructs the VM
2845 * to update it immediately.
2846 *
2847 * @returns COM status code
2848 */
2849
2850HRESULT Display::invalidateAndUpdate()
2851{
2852 LogRelFlowFunc(("\n"));
2853
2854 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2855
2856 CHECK_CONSOLE_DRV(mpDrv);
2857
2858 Console::SafeVMPtr ptrVM(mParent);
2859 if (!ptrVM.isOk())
2860 return ptrVM.rc();
2861
2862 HRESULT rc = S_OK;
2863
2864 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
2865
2866 /* Have to release the lock when calling EMT. */
2867 alock.release();
2868
2869 int rcVBox = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2870 3, this, 0, true);
2871 alock.acquire();
2872
2873 if (RT_FAILURE(rcVBox))
2874 rc = setError(VBOX_E_IPRT_ERROR,
2875 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2876
2877 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2878 return rc;
2879}
2880
2881HRESULT Display::invalidateAndUpdateScreen(ULONG aScreenId)
2882{
2883 LogRelFlowFunc(("\n"));
2884
2885 HRESULT rc = S_OK;
2886
2887 Console::SafeVMPtr ptrVM(mParent);
2888 if (!ptrVM.isOk())
2889 return ptrVM.rc();
2890
2891 int rcVBox = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2892 3, this, aScreenId, false);
2893 if (RT_FAILURE(rcVBox))
2894 rc = setError(VBOX_E_IPRT_ERROR,
2895 tr("Could not invalidate and update the screen %d (%Rrc)"), aScreenId, rcVBox);
2896
2897 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2898 return rc;
2899}
2900
2901HRESULT Display::completeVHWACommand(BYTE *aCommand)
2902{
2903#ifdef VBOX_WITH_VIDEOHWACCEL
2904 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)aCommand);
2905 return S_OK;
2906#else
2907 return E_NOTIMPL;
2908#endif
2909}
2910
2911HRESULT Display::viewportChanged(ULONG aScreenId, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2912{
2913 AssertMsgReturn(aScreenId < mcMonitors, ("aScreendId=%d mcMonitors=%d\n", aScreenId, mcMonitors), E_INVALIDARG);
2914
2915#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2916 if (mfIsCr3DEnabled)
2917 {
2918 int rc = i_crViewportNotify(aScreenId, aX, aY, aWidth, aHeight);
2919 if (RT_FAILURE(rc))
2920 {
2921 DISPLAYFBINFO *pFb = &maFramebuffers[aScreenId];
2922 pFb->pendingViewportInfo.fPending = true;
2923 pFb->pendingViewportInfo.x = aX;
2924 pFb->pendingViewportInfo.y = aY;
2925 pFb->pendingViewportInfo.width = aWidth;
2926 pFb->pendingViewportInfo.height = aHeight;
2927 }
2928 }
2929#endif /* VBOX_WITH_CROGL && VBOX_WITH_HGCM */
2930
2931 /* The driver might not have been constructed yet */
2932 if (mpDrv && mpDrv->pUpPort->pfnSetViewport)
2933 mpDrv->pUpPort->pfnSetViewport(mpDrv->pUpPort, aScreenId, aX, aY, aWidth, aHeight);
2934
2935 return S_OK;
2936}
2937
2938HRESULT Display::querySourceBitmap(ULONG aScreenId,
2939 ComPtr<IDisplaySourceBitmap> &aDisplaySourceBitmap)
2940{
2941 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2942
2943 Console::SafeVMPtr ptrVM(mParent);
2944 if (!ptrVM.isOk())
2945 return ptrVM.rc();
2946
2947 bool fSetRenderVRAM = false;
2948 bool fInvalidate = false;
2949
2950 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2951
2952 if (aScreenId >= mcMonitors)
2953 return setError(E_INVALIDARG, tr("QuerySourceBitmap: Invalid screen %d (total %d)"),
2954 aScreenId, mcMonitors);
2955
2956 if (!mfSourceBitmapEnabled)
2957 {
2958 aDisplaySourceBitmap = NULL;
2959 return E_FAIL;
2960 }
2961
2962 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2963
2964 /* No source bitmap for a blank guest screen. */
2965 if (pFBInfo->flags & VBVA_SCREEN_F_BLANK)
2966 {
2967 aDisplaySourceBitmap = NULL;
2968 return E_FAIL;
2969 }
2970
2971 HRESULT hr = S_OK;
2972
2973 if (pFBInfo->pSourceBitmap.isNull())
2974 {
2975 /* Create a new object. */
2976 ComObjPtr<DisplaySourceBitmap> obj;
2977 hr = obj.createObject();
2978 if (SUCCEEDED(hr))
2979 hr = obj->init(this, aScreenId, pFBInfo);
2980
2981 if (SUCCEEDED(hr))
2982 {
2983 pFBInfo->pSourceBitmap = obj;
2984 pFBInfo->fDefaultFormat = !obj->i_usesVRAM();
2985
2986 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2987 {
2988 /* Start buffer updates. */
2989 BYTE *pAddress = NULL;
2990 ULONG ulWidth = 0;
2991 ULONG ulHeight = 0;
2992 ULONG ulBitsPerPixel = 0;
2993 ULONG ulBytesPerLine = 0;
2994 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2995
2996 pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2997 &ulWidth,
2998 &ulHeight,
2999 &ulBitsPerPixel,
3000 &ulBytesPerLine,
3001 &bitmapFormat);
3002
3003 mpDrv->IConnector.pbData = pAddress;
3004 mpDrv->IConnector.cbScanline = ulBytesPerLine;
3005 mpDrv->IConnector.cBits = ulBitsPerPixel;
3006 mpDrv->IConnector.cx = ulWidth;
3007 mpDrv->IConnector.cy = ulHeight;
3008
3009 fSetRenderVRAM = pFBInfo->fDefaultFormat;
3010 }
3011
3012 /* Make sure that the bitmap contains the latest image. */
3013 fInvalidate = pFBInfo->fDefaultFormat;
3014 }
3015 }
3016
3017 if (SUCCEEDED(hr))
3018 {
3019 pFBInfo->pSourceBitmap.queryInterfaceTo(aDisplaySourceBitmap.asOutParam());
3020 }
3021
3022 /* Leave the IDisplay lock because the VGA device must not be called under it. */
3023 alock.release();
3024
3025 if (SUCCEEDED(hr))
3026 {
3027 if (fSetRenderVRAM)
3028 {
3029 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, true);
3030 }
3031
3032 if (fInvalidate)
3033 VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
3034 3, this, aScreenId, false);
3035 }
3036
3037 LogRelFlowFunc(("%Rhrc\n", hr));
3038 return hr;
3039}
3040
3041HRESULT Display::getGuestScreenLayout(std::vector<ComPtr<IGuestScreenInfo> > &aGuestScreenLayout)
3042{
3043 NOREF(aGuestScreenLayout);
3044 return E_NOTIMPL;
3045}
3046
3047HRESULT Display::setScreenLayout(ScreenLayoutMode_T aScreenLayoutMode,
3048 const std::vector<ComPtr<IGuestScreenInfo> > &aGuestScreenInfo)
3049{
3050 NOREF(aScreenLayoutMode);
3051 NOREF(aGuestScreenInfo);
3052 return E_NOTIMPL;
3053}
3054
3055// wrapped IEventListener method
3056HRESULT Display::handleEvent(const ComPtr<IEvent> &aEvent)
3057{
3058 VBoxEventType_T aType = VBoxEventType_Invalid;
3059
3060 aEvent->COMGETTER(Type)(&aType);
3061 switch (aType)
3062 {
3063 case VBoxEventType_OnStateChanged:
3064 {
3065 ComPtr<IStateChangedEvent> scev = aEvent;
3066 Assert(scev);
3067 MachineState_T machineState;
3068 scev->COMGETTER(State)(&machineState);
3069 if ( machineState == MachineState_Running
3070 || machineState == MachineState_Teleporting
3071 || machineState == MachineState_LiveSnapshotting
3072 || machineState == MachineState_DeletingSnapshotOnline
3073 )
3074 {
3075 LogRelFlowFunc(("Machine is running.\n"));
3076
3077#ifdef VBOX_WITH_CROGL
3078 i_crOglWindowsShow(true);
3079#endif
3080 }
3081 else
3082 {
3083#ifdef VBOX_WITH_CROGL
3084 if (machineState == MachineState_Paused)
3085 i_crOglWindowsShow(false);
3086#endif
3087 }
3088 break;
3089 }
3090 default:
3091 AssertFailed();
3092 }
3093
3094 return S_OK;
3095}
3096
3097
3098// private methods
3099/////////////////////////////////////////////////////////////////////////////
3100
3101#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3102int Display::i_crViewportNotify(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
3103{
3104 VMMDev *pVMMDev = mParent->i_getVMMDev();
3105 if (!pVMMDev)
3106 return VERR_INVALID_STATE;
3107
3108 size_t cbData = RT_UOFFSETOF(VBOXCRCMDCTL_HGCM, aParms[5]);
3109 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM *)alloca(cbData);
3110
3111 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3112 pData->Hdr.u32Function = SHCRGL_HOST_FN_VIEWPORT_CHANGED;
3113
3114 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
3115 pData->aParms[0].u.uint32 = aScreenId;
3116
3117 pData->aParms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
3118 pData->aParms[1].u.uint32 = x;
3119
3120 pData->aParms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
3121 pData->aParms[2].u.uint32 = y;
3122
3123 pData->aParms[3].type = VBOX_HGCM_SVC_PARM_32BIT;
3124 pData->aParms[3].u.uint32 = width;
3125
3126 pData->aParms[4].type = VBOX_HGCM_SVC_PARM_32BIT;
3127 pData->aParms[4].u.uint32 = height;
3128
3129 return i_crCtlSubmitSyncIfHasDataForScreen(aScreenId, &pData->Hdr, (uint32_t)cbData);
3130}
3131#endif
3132
3133#ifdef VBOX_WITH_CRHGSMI
3134void Display::i_setupCrHgsmiData(void)
3135{
3136 VMMDev *pVMMDev = mParent->i_getVMMDev();
3137 Assert(pVMMDev);
3138 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
3139 AssertRC(rc);
3140
3141 if (pVMMDev)
3142 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
3143 else
3144 rc = VERR_GENERAL_FAILURE;
3145
3146 if (RT_SUCCESS(rc))
3147 {
3148 Assert(mhCrOglSvc);
3149 /* setup command completion callback */
3150 VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB Completion;
3151 Completion.Hdr.enmType = VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_MAINCB;
3152 Completion.Hdr.cbCmd = sizeof(Completion);
3153 Completion.hCompletion = mpDrv->pVBVACallbacks;
3154 Completion.pfnCompletion = mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync;
3155
3156 VBOXHGCMSVCPARM parm;
3157 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3158 parm.u.pointer.addr = &Completion;
3159 parm.u.pointer.size = 0;
3160
3161 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_CRHGSMI_CTL, 1, &parm);
3162 if (RT_SUCCESS(rc))
3163 mCrOglCallbacks = Completion.MainInterface;
3164 else
3165 AssertMsgFailed(("VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION failed (rc=%Rrc)\n", rc));
3166 }
3167
3168 if (RT_FAILURE(rc))
3169 mhCrOglSvc = NULL;
3170
3171 RTCritSectRwLeaveExcl(&mCrOglLock);
3172}
3173
3174void Display::i_destructCrHgsmiData(void)
3175{
3176 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
3177 AssertRC(rc);
3178 mhCrOglSvc = NULL;
3179 RTCritSectRwLeaveExcl(&mCrOglLock);
3180}
3181#endif /* VBOX_WITH_CRHGSMI */
3182
3183/**
3184 * Handle display resize event issued by the VGA device for the primary screen.
3185 *
3186 * @see PDMIDISPLAYCONNECTOR::pfnResize
3187 */
3188DECLCALLBACK(int) Display::i_displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
3189 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
3190{
3191 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3192 Display *pThis = pDrv->pDisplay;
3193
3194 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
3195 bpp, pvVRAM, cbLine, cx, cy));
3196
3197 bool f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, true, false);
3198 if (!f)
3199 {
3200 /* This is a result of recursive call when the source bitmap is being updated
3201 * during a VGA resize. Tell the VGA device to ignore the call.
3202 *
3203 * @todo It is a workaround, actually pfnUpdateDisplayAll must
3204 * fail on resize.
3205 */
3206 LogRel(("displayResizeCallback: already processing\n"));
3207 return VINF_VGA_RESIZE_IN_PROGRESS;
3208 }
3209
3210 int rc = pThis->i_handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, 0, 0, 0, true);
3211
3212 /* Restore the flag. */
3213 f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, false, true);
3214 AssertRelease(f);
3215
3216 return rc;
3217}
3218
3219/**
3220 * Handle display update.
3221 *
3222 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
3223 */
3224DECLCALLBACK(void) Display::i_displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
3225 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
3226{
3227 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3228
3229#ifdef DEBUG_sunlover
3230 LogFlowFunc(("fVideoAccelEnabled = %d, %d,%d %dx%d\n",
3231 pDrv->pDisplay->mVideoAccelLegacy.fVideoAccelEnabled, x, y, cx, cy));
3232#endif /* DEBUG_sunlover */
3233
3234 /* This call does update regardless of VBVA status.
3235 * But in VBVA mode this is called only as result of
3236 * pfnUpdateDisplayAll in the VGA device.
3237 */
3238
3239 pDrv->pDisplay->i_handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
3240}
3241
3242/**
3243 * Periodic display refresh callback.
3244 *
3245 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
3246 * @thread EMT
3247 */
3248/*static*/ DECLCALLBACK(void) Display::i_displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
3249{
3250 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3251
3252#ifdef DEBUG_sunlover_2
3253 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
3254 pDrv->pDisplay->mfVideoAccelEnabled));
3255#endif /* DEBUG_sunlover_2 */
3256
3257 Display *pDisplay = pDrv->pDisplay;
3258 unsigned uScreenId;
3259
3260 int rc = pDisplay->i_videoAccelRefreshProcess(pDrv->pUpPort);
3261 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3262 {
3263 if (rc == VWRN_INVALID_STATE)
3264 {
3265 /* No VBVA do a display update. */
3266 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3267 }
3268
3269 /* Inform the VRDP server that the current display update sequence is
3270 * completed. At this moment the framebuffer memory contains a definite
3271 * image, that is synchronized with the orders already sent to VRDP client.
3272 * The server can now process redraw requests from clients or initial
3273 * fullscreen updates for new clients.
3274 */
3275 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3276 {
3277 Assert(pDisplay->mParent && pDisplay->mParent->i_consoleVRDPServer());
3278 pDisplay->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, NULL, 0);
3279 }
3280 }
3281
3282#ifdef VBOX_WITH_VIDEOREC
3283 if (VideoRecIsEnabled(pDisplay->mpVideoRecCtx))
3284 {
3285 do {
3286# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3287 if (pDisplay->mfIsCr3DEnabled)
3288 {
3289 if (ASMAtomicCmpXchgU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_SUBMITTED, CRVREC_STATE_IDLE))
3290 {
3291 if ( pDisplay->mCrOglCallbacks.pfnHasData
3292 && pDisplay->mCrOglCallbacks.pfnHasData())
3293 {
3294 /* submit */
3295 VBOXCRCMDCTL_HGCM *pData = &pDisplay->mCrOglScreenshotCtl;
3296
3297 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3298 pData->Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
3299
3300 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3301 pData->aParms[0].u.pointer.addr = &pDisplay->mCrOglScreenshotData;
3302 pData->aParms[0].u.pointer.size = sizeof(pDisplay->mCrOglScreenshotData);
3303 rc = pDisplay->i_crCtlSubmit(&pData->Hdr, sizeof(*pData), Display::i_displayVRecCompletion, pDisplay);
3304 if (RT_SUCCESS(rc))
3305 break;
3306 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
3307 }
3308
3309 /* no 3D data available, or error has occured,
3310 * go the straight way */
3311 ASMAtomicWriteU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_IDLE);
3312 }
3313 else
3314 {
3315 /* record request is still in progress, don't do anything */
3316 break;
3317 }
3318 }
3319# endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3320
3321 uint64_t u64Now = RTTimeProgramMilliTS();
3322 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3323 {
3324 if (!pDisplay->maVideoRecEnabled[uScreenId])
3325 continue;
3326
3327 if (VideoRecIsLimitReached(pDisplay->mpVideoRecCtx, uScreenId, u64Now))
3328 {
3329 pDisplay->i_videoCaptureStop();
3330 pDisplay->mParent->i_machine()->COMSETTER(VideoCaptureEnabled)(false);
3331 break;
3332 }
3333
3334 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3335 if (!pFBInfo->fDisabled)
3336 {
3337 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
3338 int rc2 = RTCritSectEnter(&pDisplay->mVideoCaptureLock);
3339 if (RT_SUCCESS(rc2))
3340 {
3341 pSourceBitmap = pFBInfo->videoCapture.pSourceBitmap;
3342 RTCritSectLeave(&pDisplay->mVideoCaptureLock);
3343 }
3344
3345 if (!pSourceBitmap.isNull())
3346 {
3347 BYTE *pbAddress = NULL;
3348 ULONG ulWidth = 0;
3349 ULONG ulHeight = 0;
3350 ULONG ulBitsPerPixel = 0;
3351 ULONG ulBytesPerLine = 0;
3352 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
3353 HRESULT hr = pSourceBitmap->QueryBitmapInfo(&pbAddress,
3354 &ulWidth,
3355 &ulHeight,
3356 &ulBitsPerPixel,
3357 &ulBytesPerLine,
3358 &bitmapFormat);
3359 if (SUCCEEDED(hr) && pbAddress)
3360 rc = VideoRecSendVideoFrame(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
3361 BitmapFormat_BGR,
3362 ulBitsPerPixel, ulBytesPerLine, ulWidth, ulHeight,
3363 pbAddress, u64Now);
3364 else
3365 rc = VERR_NOT_SUPPORTED;
3366
3367 pSourceBitmap.setNull();
3368 }
3369 else
3370 rc = VERR_NOT_SUPPORTED;
3371
3372 if (rc == VINF_TRY_AGAIN)
3373 break;
3374 }
3375 }
3376 } while (0);
3377 }
3378#endif /* VBOX_WITH_VIDEOREC */
3379
3380#ifdef DEBUG_sunlover_2
3381 LogFlowFunc(("leave\n"));
3382#endif /* DEBUG_sunlover_2 */
3383}
3384
3385/**
3386 * Reset notification
3387 *
3388 * @see PDMIDISPLAYCONNECTOR::pfnReset
3389 */
3390DECLCALLBACK(void) Display::i_displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3391{
3392 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3393
3394 LogRelFlowFunc(("\n"));
3395
3396 /* Disable VBVA mode. */
3397 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3398}
3399
3400/**
3401 * LFBModeChange notification
3402 *
3403 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3404 */
3405DECLCALLBACK(void) Display::i_displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3406{
3407 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3408
3409 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
3410
3411 NOREF(fEnabled);
3412
3413 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3414 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3415}
3416
3417/**
3418 * Adapter information change notification.
3419 *
3420 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3421 */
3422DECLCALLBACK(void) Display::i_displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM,
3423 uint32_t u32VRAMSize)
3424{
3425 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3426 pDrv->pDisplay->processAdapterData(pvVRAM, u32VRAMSize);
3427}
3428
3429/**
3430 * Display information change notification.
3431 *
3432 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3433 */
3434DECLCALLBACK(void) Display::i_displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface,
3435 void *pvVRAM, unsigned uScreenId)
3436{
3437 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3438 pDrv->pDisplay->processDisplayData(pvVRAM, uScreenId);
3439}
3440
3441#ifdef VBOX_WITH_VIDEOHWACCEL
3442
3443#ifndef S_FALSE
3444# define S_FALSE ((HRESULT)1L)
3445#endif
3446
3447int Display::i_handleVHWACommandProcess(PVBOXVHWACMD pCommand)
3448{
3449 unsigned id = (unsigned)pCommand->iDisplay;
3450 if (id >= mcMonitors)
3451 return VERR_INVALID_PARAMETER;
3452
3453 ComPtr<IFramebuffer> pFramebuffer;
3454 AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
3455 pFramebuffer = maFramebuffers[id].pFramebuffer;
3456 bool fVHWASupported = RT_BOOL(maFramebuffers[id].u32Caps & FramebufferCapabilities_VHWA);
3457 arlock.release();
3458
3459 if (pFramebuffer == NULL || !fVHWASupported)
3460 return VERR_NOT_IMPLEMENTED; /* Implementation is not available. */
3461
3462 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3463 if (hr == S_FALSE)
3464 return VINF_SUCCESS;
3465 if (SUCCEEDED(hr))
3466 return VINF_CALLBACK_RETURN;
3467 if (hr == E_ACCESSDENIED)
3468 return VERR_INVALID_STATE; /* notify we can not handle request atm */
3469 if (hr == E_NOTIMPL)
3470 return VERR_NOT_IMPLEMENTED;
3471 return VERR_GENERAL_FAILURE;
3472}
3473
3474DECLCALLBACK(int) Display::i_displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3475{
3476 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3477
3478 return pDrv->pDisplay->i_handleVHWACommandProcess(pCommand);
3479}
3480#endif
3481
3482#ifdef VBOX_WITH_CRHGSMI
3483void Display::i_handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3484{
3485 RT_NOREF(u32Function);
3486 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks,
3487 (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
3488}
3489
3490void Display::i_handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3491{
3492 RT_NOREF(u32Function);
3493 PVBOXVDMACMD_CHROMIUM_CTL pCtl = (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr;
3494 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, pCtl, result);
3495}
3496
3497void Display::i_handleCrHgsmiCommandProcess(PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3498{
3499 int rc = VERR_NOT_SUPPORTED;
3500 VBOXHGCMSVCPARM parm;
3501 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3502 parm.u.pointer.addr = pCmd;
3503 parm.u.pointer.size = cbCmd;
3504
3505 if (mhCrOglSvc)
3506 {
3507 VMMDev *pVMMDev = mParent->i_getVMMDev();
3508 if (pVMMDev)
3509 {
3510 /* no completion callback is specified with this call,
3511 * the CrOgl code will complete the CrHgsmi command once it processes it */
3512 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
3513 AssertRC(rc);
3514 if (RT_SUCCESS(rc))
3515 return;
3516 }
3517 else
3518 rc = VERR_INVALID_STATE;
3519 }
3520
3521 /* we are here because something went wrong with command processing, complete it */
3522 i_handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
3523}
3524
3525void Display::i_handleCrHgsmiControlProcess(PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
3526{
3527 int rc = VERR_NOT_SUPPORTED;
3528 VBOXHGCMSVCPARM parm;
3529 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3530 parm.u.pointer.addr = pCtl;
3531 parm.u.pointer.size = cbCtl;
3532
3533 if (mhCrOglSvc)
3534 {
3535 VMMDev *pVMMDev = mParent->i_getVMMDev();
3536 if (pVMMDev)
3537 {
3538 bool fCheckPendingViewport = (pCtl->enmType == VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP);
3539 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm,
3540 Display::i_displayCrHgsmiControlCompletion, this);
3541 AssertRC(rc);
3542 if (RT_SUCCESS(rc))
3543 {
3544 if (fCheckPendingViewport)
3545 {
3546 ULONG ul;
3547 for (ul = 0; ul < mcMonitors; ul++)
3548 {
3549 DISPLAYFBINFO *pFb = &maFramebuffers[ul];
3550 if (!pFb->pendingViewportInfo.fPending)
3551 continue;
3552
3553 rc = i_crViewportNotify(ul, pFb->pendingViewportInfo.x, pFb->pendingViewportInfo.y,
3554 pFb->pendingViewportInfo.width, pFb->pendingViewportInfo.height);
3555 if (RT_SUCCESS(rc))
3556 pFb->pendingViewportInfo.fPending = false;
3557 else
3558 {
3559 AssertMsgFailed(("crViewportNotify failed (rc=%Rrc)\n", rc));
3560 rc = VINF_SUCCESS;
3561 }
3562 }
3563 }
3564 return;
3565 }
3566 }
3567 else
3568 rc = VERR_INVALID_STATE;
3569 }
3570
3571 /* we are here because something went wrong with command processing, complete it */
3572 i_handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
3573}
3574
3575DECLCALLBACK(void) Display::i_displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd,
3576 uint32_t cbCmd)
3577{
3578 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3579
3580 pDrv->pDisplay->i_handleCrHgsmiCommandProcess(pCmd, cbCmd);
3581}
3582
3583DECLCALLBACK(void) Display::i_displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd,
3584 uint32_t cbCmd)
3585{
3586 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3587
3588 pDrv->pDisplay->i_handleCrHgsmiControlProcess(pCmd, cbCmd);
3589}
3590
3591DECLCALLBACK(void) Display::i_displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3592 void *pvContext)
3593{
3594 AssertMsgFailed(("not expected!\n"));
3595 Display *pDisplay = (Display *)pvContext;
3596 pDisplay->i_handleCrHgsmiCommandCompletion(result, u32Function, pParam);
3597}
3598
3599DECLCALLBACK(void) Display::i_displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3600 void *pvContext)
3601{
3602 Display *pDisplay = (Display *)pvContext;
3603 pDisplay->i_handleCrHgsmiControlCompletion(result, u32Function, pParam);
3604
3605}
3606#endif
3607
3608#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3609DECLCALLBACK(void) Display::i_displayCrHgcmCtlSubmitCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3610 void *pvContext)
3611{
3612 RT_NOREF(u32Function);
3613 VBOXCRCMDCTL *pCmd = (VBOXCRCMDCTL*)pParam->u.pointer.addr;
3614 if (pCmd->u.pfnInternal)
3615 ((PFNCRCTLCOMPLETION)pCmd->u.pfnInternal)(pCmd, pParam->u.pointer.size, result, pvContext);
3616}
3617
3618int Display::i_handleCrHgcmCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
3619 PFNCRCTLCOMPLETION pfnCompletion,
3620 void *pvCompletion)
3621{
3622 VMMDev *pVMMDev = mParent ? mParent->i_getVMMDev() : NULL;
3623 if (!pVMMDev)
3624 {
3625 AssertMsgFailed(("no vmmdev\n"));
3626 return VERR_INVALID_STATE;
3627 }
3628
3629 Assert(mhCrOglSvc);
3630 VBOXHGCMSVCPARM parm;
3631 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3632 parm.u.pointer.addr = pCmd;
3633 parm.u.pointer.size = cbCmd;
3634
3635 pCmd->u.pfnInternal = (void(*)())pfnCompletion;
3636 int rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CTL, &parm, i_displayCrHgcmCtlSubmitCompletion,
3637 pvCompletion);
3638 if (!RT_SUCCESS(rc))
3639 AssertMsgFailed(("hgcmHostFastCallAsync failed (rc=%Rrc)\n", rc));
3640
3641 return rc;
3642}
3643
3644DECLCALLBACK(int) Display::i_displayCrHgcmCtlSubmit(PPDMIDISPLAYCONNECTOR pInterface,
3645 struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
3646 PFNCRCTLCOMPLETION pfnCompletion,
3647 void *pvCompletion)
3648{
3649 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3650 Display *pThis = pDrv->pDisplay;
3651 return pThis->i_handleCrHgcmCtlSubmit(pCmd, cbCmd, pfnCompletion, pvCompletion);
3652}
3653
3654int Display::i_crCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, PFNCRCTLCOMPLETION pfnCompletion, void *pvCompletion)
3655{
3656 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3657 if (RT_SUCCESS(rc))
3658 {
3659 if (mhCrOglSvc)
3660 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmit(mpDrv->pVBVACallbacks, pCmd, cbCmd, pfnCompletion, pvCompletion);
3661 else
3662 rc = VERR_NOT_SUPPORTED;
3663
3664 RTCritSectRwLeaveShared(&mCrOglLock);
3665 }
3666 return rc;
3667}
3668
3669int Display::i_crCtlSubmitSync(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3670{
3671 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3672 if (RT_SUCCESS(rc))
3673 {
3674 if (mhCrOglSvc)
3675 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmitSync(mpDrv->pVBVACallbacks, pCmd, cbCmd);
3676 else
3677 rc = VERR_NOT_SUPPORTED;
3678
3679 RTCritSectRwLeaveShared(&mCrOglLock);
3680 }
3681 return rc;
3682}
3683
3684int Display::i_crCtlSubmitAsyncCmdCopy(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3685{
3686 VBOXCRCMDCTL* pCmdCopy = (VBOXCRCMDCTL*)RTMemAlloc(cbCmd);
3687 if (!pCmdCopy)
3688 {
3689 LogRel(("RTMemAlloc failed\n"));
3690 return VERR_NO_MEMORY;
3691 }
3692
3693 memcpy(pCmdCopy, pCmd, cbCmd);
3694
3695 int rc = i_crCtlSubmit(pCmdCopy, cbCmd, i_displayCrCmdFree, pCmdCopy);
3696 if (RT_FAILURE(rc))
3697 {
3698 LogRel(("crCtlSubmit failed (rc=%Rrc)\n", rc));
3699 RTMemFree(pCmdCopy);
3700 return rc;
3701 }
3702
3703 return VINF_SUCCESS;
3704}
3705
3706int Display::i_crCtlSubmitSyncIfHasDataForScreen(uint32_t u32ScreenID, struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3707{
3708 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3709 AssertRCReturn(rc, rc);
3710
3711 if ( mCrOglCallbacks.pfnHasDataForScreen
3712 && mCrOglCallbacks.pfnHasDataForScreen(u32ScreenID))
3713 rc = i_crCtlSubmitSync(pCmd, cbCmd);
3714 else
3715 rc = i_crCtlSubmitAsyncCmdCopy(pCmd, cbCmd);
3716
3717 RTCritSectRwLeaveShared(&mCrOglLock);
3718
3719 return rc;
3720}
3721
3722bool Display::i_handleCrVRecScreenshotBegin(uint32_t uScreen, uint64_t u64Timestamp)
3723{
3724 /** @todo r=bird: u64Timestamp - using the 'u64' prefix add nothing.
3725 * However, using one of the prefixes indicating the timestamp unit
3726 * would be very valuable! */
3727# ifdef VBOX_WITH_VIDEOREC
3728 return VideoRecIsReady(mpVideoRecCtx, uScreen, u64Timestamp);
3729# else
3730 RT_NOREF(uScreen, u64Timestamp);
3731 return false;
3732# endif
3733}
3734
3735void Display::i_handleCrVRecScreenshotEnd(uint32_t uScreen, uint64_t u64Timestamp)
3736{
3737 RT_NOREF(uScreen, u64Timestamp);
3738}
3739
3740void Display::i_handleCrVRecScreenshotPerform(uint32_t uScreen,
3741 uint32_t x, uint32_t y, uint32_t uPixelFormat,
3742 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
3743 uint32_t uGuestWidth, uint32_t uGuestHeight,
3744 uint8_t *pu8BufferAddress, uint64_t u64Timestamp)
3745{
3746 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
3747# ifdef VBOX_WITH_VIDEOREC
3748 int rc = VideoRecSendVideoFrame(mpVideoRecCtx, uScreen, x, y,
3749 uPixelFormat,
3750 uBitsPerPixel, uBytesPerLine,
3751 uGuestWidth, uGuestHeight,
3752 pu8BufferAddress, u64Timestamp);
3753 NOREF(rc);
3754 Assert(rc == VINF_SUCCESS /* || rc == VERR_TRY_AGAIN || rc == VINF_TRY_AGAIN*/);
3755# else
3756 RT_NOREF(uScreen, x, y, uPixelFormat, \
3757 uBitsPerPixel, uBytesPerLine, uGuestWidth, uGuestHeight, pu8BufferAddress, u64Timestamp);
3758# endif /* VBOX_WITH_VIDEOREC */
3759}
3760
3761void Display::i_handleVRecCompletion()
3762{
3763 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
3764 ASMAtomicWriteU32(&mfCrOglVideoRecState, CRVREC_STATE_IDLE);
3765}
3766
3767#endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3768
3769HRESULT Display::notifyScaleFactorChange(ULONG aScreenId, ULONG aScaleFactorWMultiplied, ULONG aScaleFactorHMultiplied)
3770{
3771#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3772 HRESULT hr = E_UNEXPECTED;
3773
3774 if (aScreenId >= mcMonitors)
3775 return E_INVALIDARG;
3776
3777 /* 3D acceleration enabled in VM config. */
3778 if (mfIsCr3DEnabled)
3779 {
3780 /* VBoxSharedCrOpenGL HGCM host service is running. */
3781 if (mhCrOglSvc)
3782 {
3783 VMMDev *pVMMDev = mParent->i_getVMMDev();
3784 if (pVMMDev)
3785 {
3786 VBOXCRCMDCTL_HGCM *pCtl;
3787 pCtl = (VBOXCRCMDCTL_HGCM *)RTMemAlloc(sizeof(CRVBOXHGCMSETSCALEFACTOR) + sizeof(VBOXCRCMDCTL_HGCM));
3788 if (pCtl)
3789 {
3790 CRVBOXHGCMSETSCALEFACTOR *pData = (CRVBOXHGCMSETSCALEFACTOR *)(pCtl + 1);
3791 int rc;
3792
3793 pData->u32Screen = aScreenId;
3794 pData->u32ScaleFactorWMultiplied = aScaleFactorWMultiplied;
3795 pData->u32ScaleFactorHMultiplied = aScaleFactorHMultiplied;
3796
3797 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3798 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_SCALE_FACTOR;
3799 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3800 pCtl->aParms[0].u.pointer.addr = pData;
3801 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
3802
3803 rc = i_crCtlSubmitSync(&pCtl->Hdr, sizeof(*pCtl));
3804 if (RT_FAILURE(rc))
3805 AssertMsgFailed(("crCtlSubmitSync failed (rc=%Rrc)\n", rc));
3806 else
3807 hr = S_OK;
3808
3809 RTMemFree(pCtl);
3810 }
3811 else
3812 {
3813 LogRel(("Running out of memory on attempt to set OpenGL content scale factor. Ignored.\n"));
3814 hr = E_OUTOFMEMORY;
3815 }
3816 }
3817 else
3818 LogRel(("Internal error occurred on attempt to set OpenGL content scale factor. Ignored.\n"));
3819 }
3820 else
3821 LogRel(("Attempt to specify OpenGL content scale factor while corresponding HGCM host service not yet runing. Ignored.\n"));
3822 }
3823 else
3824# if 0 /** @todo Thank you so very much from anyone using VMSVGA3d! */
3825 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while 3D acceleration is disabled in VM config. Ignored.\n"));
3826# else
3827 {
3828 hr = S_OK;
3829 /* Need an interface like this here (and the #ifdefs needs adjusting):
3830 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3831 if (pUpPort && pUpPort->pfnSetScaleFactor)
3832 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3833 }
3834# endif
3835
3836 return hr;
3837
3838#else /* !VBOX_WITH_HGCM || !VBOX_WITH_CROGL */
3839 RT_NOREF(aScreenId, aScaleFactorWMultiplied, aScaleFactorHMultiplied);
3840 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while corresponding functionality is disabled."));
3841 return E_UNEXPECTED;
3842#endif /* !VBOX_WITH_HGCM || !VBOX_WITH_CROGL */
3843}
3844
3845HRESULT Display::notifyHiDPIOutputPolicyChange(BOOL fUnscaledHiDPI)
3846{
3847#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3848 HRESULT hr = E_UNEXPECTED;
3849
3850 /* 3D acceleration enabled in VM config. */
3851 if (mfIsCr3DEnabled)
3852 {
3853 /* VBoxSharedCrOpenGL HGCM host service is running. */
3854 if (mhCrOglSvc)
3855 {
3856 VMMDev *pVMMDev = mParent->i_getVMMDev();
3857 if (pVMMDev)
3858 {
3859 VBOXCRCMDCTL_HGCM *pCtl;
3860 pCtl = (VBOXCRCMDCTL_HGCM *)RTMemAlloc(sizeof(CRVBOXHGCMSETUNSCALEDHIDPIOUTPUT) + sizeof(VBOXCRCMDCTL_HGCM));
3861 if (pCtl)
3862 {
3863 CRVBOXHGCMSETUNSCALEDHIDPIOUTPUT *pData = (CRVBOXHGCMSETUNSCALEDHIDPIOUTPUT *)(pCtl + 1);
3864 int rc;
3865
3866 pData->fUnscaledHiDPI = RT_BOOL(fUnscaledHiDPI);
3867
3868 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3869 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_UNSCALED_HIDPI;
3870 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3871 pCtl->aParms[0].u.pointer.addr = pData;
3872 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
3873
3874 rc = i_crCtlSubmitSync(&pCtl->Hdr, sizeof(*pCtl));
3875 if (RT_FAILURE(rc))
3876 AssertMsgFailed(("crCtlSubmitSync failed (rc=%Rrc)\n", rc));
3877 else
3878 hr = S_OK;
3879
3880 RTMemFree(pCtl);
3881 }
3882 else
3883 {
3884 LogRel(("Running out of memory on attempt to notify OpenGL about HiDPI output scaling policy change. Ignored.\n"));
3885 hr = E_OUTOFMEMORY;
3886 }
3887 }
3888 else
3889 LogRel(("Internal error occurred on attempt to notify OpenGL about HiDPI output scaling policy change. Ignored.\n"));
3890 }
3891 else
3892 LogRel(("Attempt to notify OpenGL about HiDPI output scaling policy change while corresponding HGCM host service not yet runing. Ignored.\n"));
3893 }
3894 else
3895 {
3896 hr = S_OK;
3897 /* Need an interface like this here (and the #ifdefs needs adjusting):
3898 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3899 if (pUpPort && pUpPort->pfnSetScaleFactor)
3900 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3901 }
3902
3903 return hr;
3904
3905#else /* !VBOX_WITH_HGCM || !VBOX_WITH_CROGL */
3906 RT_NOREF(fUnscaledHiDPI);
3907 AssertMsgFailed(("Attempt to notify OpenGL about HiDPI output scaling policy change while corresponding functionality is disabled."));
3908 return E_UNEXPECTED;
3909#endif /* !VBOX_WITH_HGCM || !VBOX_WITH_CROGL */
3910}
3911
3912#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3913DECLCALLBACK(void) Display::i_displayCrVRecScreenshotPerform(void *pvCtx, uint32_t uScreen,
3914 uint32_t x, uint32_t y,
3915 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
3916 uint32_t uGuestWidth, uint32_t uGuestHeight,
3917 uint8_t *pu8BufferAddress, uint64_t u64Timestamp)
3918{
3919 Display *pDisplay = (Display *)pvCtx;
3920 pDisplay->i_handleCrVRecScreenshotPerform(uScreen,
3921 x, y, BitmapFormat_BGR, uBitsPerPixel,
3922 uBytesPerLine, uGuestWidth, uGuestHeight,
3923 pu8BufferAddress, u64Timestamp);
3924}
3925
3926DECLCALLBACK(bool) Display::i_displayCrVRecScreenshotBegin(void *pvCtx, uint32_t uScreen, uint64_t u64Timestamp)
3927{
3928 Display *pDisplay = (Display *)pvCtx;
3929 return pDisplay->i_handleCrVRecScreenshotBegin(uScreen, u64Timestamp);
3930}
3931
3932DECLCALLBACK(void) Display::i_displayCrVRecScreenshotEnd(void *pvCtx, uint32_t uScreen, uint64_t u64Timestamp)
3933{
3934 Display *pDisplay = (Display *)pvCtx;
3935 pDisplay->i_handleCrVRecScreenshotEnd(uScreen, u64Timestamp);
3936}
3937
3938DECLCALLBACK(void) Display::i_displayVRecCompletion(struct VBOXCRCMDCTL *pCmd, uint32_t cbCmd, int rc, void *pvCompletion)
3939{
3940 RT_NOREF(pCmd, cbCmd, rc);
3941 Display *pDisplay = (Display *)pvCompletion;
3942 pDisplay->i_handleVRecCompletion();
3943}
3944
3945#endif
3946
3947
3948#ifdef VBOX_WITH_HGSMI
3949DECLCALLBACK(int) Display::i_displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags,
3950 bool fRenderThreadMode)
3951{
3952 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3953
3954 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3955 Display *pThis = pDrv->pDisplay;
3956
3957 if (pThis->maFramebuffers[uScreenId].fVBVAEnabled && pThis->maFramebuffers[uScreenId].fRenderThreadMode != fRenderThreadMode)
3958 {
3959 LogRel(("Enabling different vbva mode\n"));
3960#ifdef DEBUG_misha
3961 AssertMsgFailed(("enabling different vbva mode\n"));
3962#endif
3963 return VERR_INVALID_STATE;
3964 }
3965
3966 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3967 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3968 pThis->maFramebuffers[uScreenId].fRenderThreadMode = fRenderThreadMode;
3969 pThis->maFramebuffers[uScreenId].fVBVAForceResize = true;
3970
3971 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3972
3973 return VINF_SUCCESS;
3974}
3975
3976DECLCALLBACK(void) Display::i_displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3977{
3978 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3979
3980 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3981 Display *pThis = pDrv->pDisplay;
3982
3983 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3984
3985 bool fRenderThreadMode = pFBInfo->fRenderThreadMode;
3986
3987 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3988 {
3989 /* Make sure that the primary screen is visible now.
3990 * The guest can't use VBVA anymore, so only only the VGA device output works.
3991 */
3992 pFBInfo->flags = 0;
3993 if (pFBInfo->fDisabled)
3994 {
3995 pFBInfo->fDisabled = false;
3996 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3997 GuestMonitorChangedEventType_Enabled,
3998 uScreenId,
3999 pFBInfo->xOrigin, pFBInfo->yOrigin,
4000 pFBInfo->w, pFBInfo->h);
4001 }
4002 }
4003
4004 pFBInfo->fVBVAEnabled = false;
4005 pFBInfo->fVBVAForceResize = false;
4006 pFBInfo->fRenderThreadMode = false;
4007
4008 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
4009
4010 pFBInfo->pVBVAHostFlags = NULL;
4011
4012 if (!fRenderThreadMode && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
4013 {
4014 /* Force full screen update, because VGA device must take control, do resize, etc. */
4015 pThis->mpDrv->pUpPort->pfnUpdateDisplayAll(pThis->mpDrv->pUpPort, /* fFailOnResize = */ false);
4016 }
4017}
4018
4019DECLCALLBACK(void) Display::i_displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
4020{
4021 RT_NOREF(uScreenId);
4022 LogFlowFunc(("uScreenId %d\n", uScreenId));
4023
4024 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4025 Display *pThis = pDrv->pDisplay;
4026
4027 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
4028 {
4029 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers,
4030 pThis->mcMonitors);
4031 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
4032 }
4033}
4034
4035DECLCALLBACK(void) Display::i_displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
4036 PCVBVACMDHDR pCmd, size_t cbCmd)
4037{
4038 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
4039
4040 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4041 Display *pThis = pDrv->pDisplay;
4042 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4043
4044 if (pFBInfo->fDefaultFormat)
4045 {
4046 /* Make sure that framebuffer contains the same image as the guest VRAM. */
4047 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
4048 && !pFBInfo->fDisabled)
4049 {
4050 pDrv->pUpPort->pfnUpdateDisplayRect(pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
4051 }
4052 else if ( !pFBInfo->pSourceBitmap.isNull()
4053 && !pFBInfo->fDisabled)
4054 {
4055 /* Render VRAM content to the framebuffer. */
4056 BYTE *pAddress = NULL;
4057 ULONG ulWidth = 0;
4058 ULONG ulHeight = 0;
4059 ULONG ulBitsPerPixel = 0;
4060 ULONG ulBytesPerLine = 0;
4061 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
4062
4063 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
4064 &ulWidth,
4065 &ulHeight,
4066 &ulBitsPerPixel,
4067 &ulBytesPerLine,
4068 &bitmapFormat);
4069 if (SUCCEEDED(hrc))
4070 {
4071 uint32_t width = pCmd->w;
4072 uint32_t height = pCmd->h;
4073
4074 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
4075 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
4076 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
4077 uint32_t u32SrcWidth = pFBInfo->w;
4078 uint32_t u32SrcHeight = pFBInfo->h;
4079 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
4080 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
4081
4082 uint8_t *pu8Dst = pAddress;
4083 int32_t xDst = xSrc;
4084 int32_t yDst = ySrc;
4085 uint32_t u32DstWidth = u32SrcWidth;
4086 uint32_t u32DstHeight = u32SrcHeight;
4087 uint32_t u32DstLineSize = u32DstWidth * 4;
4088 uint32_t u32DstBitsPerPixel = 32;
4089
4090 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
4091 width, height,
4092 pu8Src,
4093 xSrc, ySrc,
4094 u32SrcWidth, u32SrcHeight,
4095 u32SrcLineSize, u32SrcBitsPerPixel,
4096 pu8Dst,
4097 xDst, yDst,
4098 u32DstWidth, u32DstHeight,
4099 u32DstLineSize, u32DstBitsPerPixel);
4100 }
4101 }
4102 }
4103
4104 VBVACMDHDR hdrSaved = *pCmd;
4105
4106 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
4107
4108 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
4109 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
4110
4111 /** @todo new SendUpdate entry which can get a separate cmd header or coords. */
4112 pThis->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, pHdrUnconst, (uint32_t)cbCmd);
4113
4114 *pHdrUnconst = hdrSaved;
4115}
4116
4117DECLCALLBACK(void) Display::i_displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y,
4118 uint32_t cx, uint32_t cy)
4119{
4120 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
4121
4122 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4123 Display *pThis = pDrv->pDisplay;
4124 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4125
4126 /** @todo handleFramebufferUpdate (uScreenId,
4127 * x - pThis->maFramebuffers[uScreenId].xOrigin,
4128 * y - pThis->maFramebuffers[uScreenId].yOrigin,
4129 * cx, cy);
4130 */
4131 pThis->i_handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
4132}
4133
4134#ifdef DEBUG_sunlover
4135static void logVBVAResize(PCVBVAINFOVIEW pView, PCVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
4136{
4137 LogRel(("displayVBVAResize: [%d] %s\n"
4138 " pView->u32ViewIndex %d\n"
4139 " pView->u32ViewOffset 0x%08X\n"
4140 " pView->u32ViewSize 0x%08X\n"
4141 " pView->u32MaxScreenSize 0x%08X\n"
4142 " pScreen->i32OriginX %d\n"
4143 " pScreen->i32OriginY %d\n"
4144 " pScreen->u32StartOffset 0x%08X\n"
4145 " pScreen->u32LineSize 0x%08X\n"
4146 " pScreen->u32Width %d\n"
4147 " pScreen->u32Height %d\n"
4148 " pScreen->u16BitsPerPixel %d\n"
4149 " pScreen->u16Flags 0x%04X\n"
4150 " pFBInfo->u32Offset 0x%08X\n"
4151 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
4152 " pFBInfo->u32InformationSize 0x%08X\n"
4153 " pFBInfo->fDisabled %d\n"
4154 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
4155 " pFBInfo->u16BitsPerPixel %d\n"
4156 " pFBInfo->pu8FramebufferVRAM %p\n"
4157 " pFBInfo->u32LineSize 0x%08X\n"
4158 " pFBInfo->flags 0x%04X\n"
4159 " pFBInfo->pHostEvents %p\n"
4160 " pFBInfo->fDefaultFormat %d\n"
4161 " pFBInfo->fVBVAEnabled %d\n"
4162 " pFBInfo->fVBVAForceResize %d\n"
4163 " pFBInfo->pVBVAHostFlags %p\n"
4164 "",
4165 pScreen->u32ViewIndex,
4166 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
4167 pView->u32ViewIndex,
4168 pView->u32ViewOffset,
4169 pView->u32ViewSize,
4170 pView->u32MaxScreenSize,
4171 pScreen->i32OriginX,
4172 pScreen->i32OriginY,
4173 pScreen->u32StartOffset,
4174 pScreen->u32LineSize,
4175 pScreen->u32Width,
4176 pScreen->u32Height,
4177 pScreen->u16BitsPerPixel,
4178 pScreen->u16Flags,
4179 pFBInfo->u32Offset,
4180 pFBInfo->u32MaxFramebufferSize,
4181 pFBInfo->u32InformationSize,
4182 pFBInfo->fDisabled,
4183 pFBInfo->xOrigin,
4184 pFBInfo->yOrigin,
4185 pFBInfo->w,
4186 pFBInfo->h,
4187 pFBInfo->u16BitsPerPixel,
4188 pFBInfo->pu8FramebufferVRAM,
4189 pFBInfo->u32LineSize,
4190 pFBInfo->flags,
4191 pFBInfo->pHostEvents,
4192 pFBInfo->fDefaultFormat,
4193 pFBInfo->fVBVAEnabled,
4194 pFBInfo->fVBVAForceResize,
4195 pFBInfo->pVBVAHostFlags
4196 ));
4197}
4198#endif /* DEBUG_sunlover */
4199
4200DECLCALLBACK(int) Display::i_displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, PCVBVAINFOVIEW pView,
4201 PCVBVAINFOSCREEN pScreen, void *pvVRAM, bool fResetInputMapping)
4202{
4203 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
4204
4205 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4206 Display *pThis = pDrv->pDisplay;
4207
4208 return pThis->processVBVAResize(pView, pScreen, pvVRAM, fResetInputMapping);
4209}
4210
4211int Display::processVBVAResize(PCVBVAINFOVIEW pView, PCVBVAINFOSCREEN pScreen, void *pvVRAM, bool fResetInputMapping)
4212{
4213 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
4214
4215 DISPLAYFBINFO *pFBInfo = &maFramebuffers[pScreen->u32ViewIndex];
4216
4217 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
4218 {
4219 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
4220 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
4221 * the VM window will be black. */
4222 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
4223 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
4224 int32_t xOrigin = pFBInfo->xOrigin;
4225 int32_t yOrigin = pFBInfo->yOrigin;
4226
4227 alock.release();
4228
4229 i_notifyCroglResize(pView, pScreen, pvVRAM);
4230
4231 i_handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
4232 u32Width, u32Height, pScreen->u16Flags, xOrigin, yOrigin, false);
4233
4234 return VINF_SUCCESS;
4235 }
4236
4237 VBVAINFOSCREEN screenInfo;
4238 RT_ZERO(screenInfo);
4239
4240 if (pScreen->u16Flags & VBVA_SCREEN_F_BLANK2)
4241 {
4242 /* Init a local VBVAINFOSCREEN structure, which will be used instead of
4243 * the original pScreen. Set VBVA_SCREEN_F_BLANK, which will force
4244 * the code below to choose the "blanking" branches.
4245 */
4246 screenInfo.u32ViewIndex = pScreen->u32ViewIndex;
4247 screenInfo.i32OriginX = pFBInfo->xOrigin;
4248 screenInfo.i32OriginY = pFBInfo->yOrigin;
4249 screenInfo.u32StartOffset = 0; /* Irrelevant */
4250 screenInfo.u32LineSize = pFBInfo->u32LineSize;
4251 screenInfo.u32Width = pFBInfo->w;
4252 screenInfo.u32Height = pFBInfo->h;
4253 screenInfo.u16BitsPerPixel = pFBInfo->u16BitsPerPixel;
4254 screenInfo.u16Flags = pScreen->u16Flags | VBVA_SCREEN_F_BLANK;
4255
4256 pScreen = &screenInfo;
4257 }
4258
4259 if (fResetInputMapping)
4260 {
4261 /// @todo Rename to m* and verify whether some kind of lock is required.
4262 xInputMappingOrigin = 0;
4263 yInputMappingOrigin = 0;
4264 cxInputMapping = 0;
4265 cyInputMapping = 0;
4266 }
4267
4268 alock.release();
4269
4270 i_notifyCroglResize(pView, pScreen, pvVRAM);
4271
4272 return i_handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
4273 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
4274 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags,
4275 pScreen->i32OriginX, pScreen->i32OriginY, false);
4276}
4277
4278DECLCALLBACK(int) Display::i_displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
4279 uint32_t xHot, uint32_t yHot,
4280 uint32_t cx, uint32_t cy,
4281 const void *pvShape)
4282{
4283 LogFlowFunc(("\n"));
4284
4285 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4286
4287 uint32_t cbShape = 0;
4288 if (pvShape)
4289 {
4290 cbShape = (cx + 7) / 8 * cy; /* size of the AND mask */
4291 cbShape = ((cbShape + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
4292 }
4293
4294 /* Tell the console about it */
4295 pDrv->pDisplay->mParent->i_onMousePointerShapeChange(fVisible, fAlpha,
4296 xHot, yHot, cx, cy, (uint8_t *)pvShape, cbShape);
4297
4298 return VINF_SUCCESS;
4299}
4300
4301DECLCALLBACK(void) Display::i_displayVBVAGuestCapabilityUpdate(PPDMIDISPLAYCONNECTOR pInterface, uint32_t fCapabilities)
4302{
4303 LogFlowFunc(("\n"));
4304
4305 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4306 Display *pThis = pDrv->pDisplay;
4307
4308 pThis->i_handleUpdateGuestVBVACapabilities(fCapabilities);
4309}
4310
4311DECLCALLBACK(void) Display::i_displayVBVAInputMappingUpdate(PPDMIDISPLAYCONNECTOR pInterface, int32_t xOrigin, int32_t yOrigin,
4312 uint32_t cx, uint32_t cy)
4313{
4314 LogFlowFunc(("\n"));
4315
4316 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4317 Display *pThis = pDrv->pDisplay;
4318
4319 pThis->i_handleUpdateVBVAInputMapping(xOrigin, yOrigin, cx, cy);
4320}
4321
4322#endif /* VBOX_WITH_HGSMI */
4323
4324/**
4325 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4326 */
4327DECLCALLBACK(void *) Display::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
4328{
4329 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
4330 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4331 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
4332 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
4333 return NULL;
4334}
4335
4336
4337/**
4338 * Destruct a display driver instance.
4339 *
4340 * @returns VBox status code.
4341 * @param pDrvIns The driver instance data.
4342 */
4343DECLCALLBACK(void) Display::i_drvDestruct(PPDMDRVINS pDrvIns)
4344{
4345 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4346 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4347 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4348
4349 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
4350
4351 pThis->IConnector.pbData = NULL;
4352 pThis->IConnector.cbScanline = 0;
4353 pThis->IConnector.cBits = 32;
4354 pThis->IConnector.cx = 0;
4355 pThis->IConnector.cy = 0;
4356
4357 if (pThis->pDisplay)
4358 {
4359 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
4360#ifdef VBOX_WITH_VIDEOREC
4361 pThis->pDisplay->i_videoCaptureStop();
4362#endif
4363#ifdef VBOX_WITH_CRHGSMI
4364 pThis->pDisplay->i_destructCrHgsmiData();
4365#endif
4366 pThis->pDisplay->mpDrv = NULL;
4367 pThis->pDisplay->mpVMMDev = NULL;
4368 }
4369}
4370
4371
4372/**
4373 * Construct a display driver instance.
4374 *
4375 * @copydoc FNPDMDRVCONSTRUCT
4376 */
4377DECLCALLBACK(int) Display::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
4378{
4379 RT_NOREF(fFlags);
4380 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
4381 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4382 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4383
4384 /*
4385 * Validate configuration.
4386 */
4387 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
4388 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
4389 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
4390 ("Configuration error: Not possible to attach anything to this driver!\n"),
4391 VERR_PDM_DRVINS_NO_ATTACH);
4392
4393 /*
4394 * Init Interfaces.
4395 */
4396 pDrvIns->IBase.pfnQueryInterface = Display::i_drvQueryInterface;
4397
4398 pThis->IConnector.pfnResize = Display::i_displayResizeCallback;
4399 pThis->IConnector.pfnUpdateRect = Display::i_displayUpdateCallback;
4400 pThis->IConnector.pfnRefresh = Display::i_displayRefreshCallback;
4401 pThis->IConnector.pfnReset = Display::i_displayResetCallback;
4402 pThis->IConnector.pfnLFBModeChange = Display::i_displayLFBModeChangeCallback;
4403 pThis->IConnector.pfnProcessAdapterData = Display::i_displayProcessAdapterDataCallback;
4404 pThis->IConnector.pfnProcessDisplayData = Display::i_displayProcessDisplayDataCallback;
4405#ifdef VBOX_WITH_VIDEOHWACCEL
4406 pThis->IConnector.pfnVHWACommandProcess = Display::i_displayVHWACommandProcess;
4407#endif
4408#ifdef VBOX_WITH_CRHGSMI
4409 pThis->IConnector.pfnCrHgsmiCommandProcess = Display::i_displayCrHgsmiCommandProcess;
4410 pThis->IConnector.pfnCrHgsmiControlProcess = Display::i_displayCrHgsmiControlProcess;
4411#endif
4412#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4413 pThis->IConnector.pfnCrHgcmCtlSubmit = Display::i_displayCrHgcmCtlSubmit;
4414#endif
4415#ifdef VBOX_WITH_HGSMI
4416 pThis->IConnector.pfnVBVAEnable = Display::i_displayVBVAEnable;
4417 pThis->IConnector.pfnVBVADisable = Display::i_displayVBVADisable;
4418 pThis->IConnector.pfnVBVAUpdateBegin = Display::i_displayVBVAUpdateBegin;
4419 pThis->IConnector.pfnVBVAUpdateProcess = Display::i_displayVBVAUpdateProcess;
4420 pThis->IConnector.pfnVBVAUpdateEnd = Display::i_displayVBVAUpdateEnd;
4421 pThis->IConnector.pfnVBVAResize = Display::i_displayVBVAResize;
4422 pThis->IConnector.pfnVBVAMousePointerShape = Display::i_displayVBVAMousePointerShape;
4423 pThis->IConnector.pfnVBVAGuestCapabilityUpdate = Display::i_displayVBVAGuestCapabilityUpdate;
4424 pThis->IConnector.pfnVBVAInputMappingUpdate = Display::i_displayVBVAInputMappingUpdate;
4425#endif
4426
4427 /*
4428 * Get the IDisplayPort interface of the above driver/device.
4429 */
4430 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
4431 if (!pThis->pUpPort)
4432 {
4433 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
4434 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4435 }
4436#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
4437 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
4438 if (!pThis->pVBVACallbacks)
4439 {
4440 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
4441 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4442 }
4443#endif
4444 /*
4445 * Get the Display object pointer and update the mpDrv member.
4446 */
4447 void *pv;
4448 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
4449 if (RT_FAILURE(rc))
4450 {
4451 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
4452 return rc;
4453 }
4454 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
4455 pThis->pDisplay = pDisplay;
4456 pThis->pDisplay->mpDrv = pThis;
4457
4458 /* Disable VRAM to a buffer copy initially. */
4459 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
4460 pThis->IConnector.cBits = 32; /* DevVGA does nothing otherwise. */
4461
4462 /*
4463 * Start periodic screen refreshes
4464 */
4465 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
4466
4467#ifdef VBOX_WITH_CRHGSMI
4468 pDisplay->i_setupCrHgsmiData();
4469#endif
4470
4471#ifdef VBOX_WITH_VIDEOREC
4472 ComPtr<IMachine> pMachine = pDisplay->mParent->i_machine();
4473 BOOL fEnabled = false;
4474 HRESULT hrc = pMachine->COMGETTER(VideoCaptureEnabled)(&fEnabled);
4475 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
4476
4477 if (fEnabled)
4478 {
4479 rc = pDisplay->i_videoCaptureStart();
4480 fireVideoCaptureChangedEvent(pDisplay->mParent->i_getEventSource());
4481 }
4482#endif
4483
4484 return rc;
4485}
4486
4487
4488/**
4489 * Display driver registration record.
4490 */
4491const PDMDRVREG Display::DrvReg =
4492{
4493 /* u32Version */
4494 PDM_DRVREG_VERSION,
4495 /* szName */
4496 "MainDisplay",
4497 /* szRCMod */
4498 "",
4499 /* szR0Mod */
4500 "",
4501 /* pszDescription */
4502 "Main display driver (Main as in the API).",
4503 /* fFlags */
4504 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
4505 /* fClass. */
4506 PDM_DRVREG_CLASS_DISPLAY,
4507 /* cMaxInstances */
4508 ~0U,
4509 /* cbInstance */
4510 sizeof(DRVMAINDISPLAY),
4511 /* pfnConstruct */
4512 Display::i_drvConstruct,
4513 /* pfnDestruct */
4514 Display::i_drvDestruct,
4515 /* pfnRelocate */
4516 NULL,
4517 /* pfnIOCtl */
4518 NULL,
4519 /* pfnPowerOn */
4520 NULL,
4521 /* pfnReset */
4522 NULL,
4523 /* pfnSuspend */
4524 NULL,
4525 /* pfnResume */
4526 NULL,
4527 /* pfnAttach */
4528 NULL,
4529 /* pfnDetach */
4530 NULL,
4531 /* pfnPowerOff */
4532 NULL,
4533 /* pfnSoftReset */
4534 NULL,
4535 /* u32EndVersion */
4536 PDM_DRVREG_VERSION
4537};
4538
4539/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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