VirtualBox

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

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

VideoRec: Made failing to start video capturing non-critical on display construction.

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