VirtualBox

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

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

Main: updated the guest resizing code (disabled).

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

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