VirtualBox

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

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

Build fix.

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