VirtualBox

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

Last change on this file since 50963 was 50963, checked in by vboxsync, 11 years ago

Display: update display when VBVA is disabled

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

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