VirtualBox

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

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

PDMIDISPLAYCONNECTOR: const ptr vs const data mismatch.

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

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