VirtualBox

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

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

crOpenGL: new command submission continued

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 173.2 KB
Line 
1/* $Id: DisplayImpl.cpp 50848 2014-03-24 10:15: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#ifdef 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#ifdef 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 %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 %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 %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 /* 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 %n", rc));
2591 RTMemFree(pData);
2592 }
2593 }
2594 }
2595#endif
2596 return S_OK;
2597}
2598
2599#ifdef 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 (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
3336 {
3337 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort);
3338 }
3339 else
3340 {
3341 if ( !pFBInfo->pFramebuffer.isNull()
3342 && !pFBInfo->fDisabled
3343 && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3344 {
3345 /* Render complete VRAM screen to the framebuffer.
3346 * When framebuffer uses VRAM directly, just notify it to update.
3347 */
3348 if (pFBInfo->fDefaultFormat)
3349 {
3350 BYTE *address = NULL;
3351 ULONG uWidth = 0;
3352 ULONG uHeight = 0;
3353 pFBInfo->pFramebuffer->COMGETTER(Width) (&uWidth);
3354 pFBInfo->pFramebuffer->COMGETTER(Height) (&uHeight);
3355 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
3356 if (SUCCEEDED(hrc) && address != NULL)
3357 {
3358 uint32_t width = pFBInfo->w;
3359 uint32_t height = pFBInfo->h;
3360
3361 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3362 int32_t xSrc = 0;
3363 int32_t ySrc = 0;
3364 uint32_t u32SrcWidth = pFBInfo->w;
3365 uint32_t u32SrcHeight = pFBInfo->h;
3366 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3367 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3368
3369 /* Default format is 32 bpp. */
3370 uint8_t *pu8Dst = address;
3371 int32_t xDst = xSrc;
3372 int32_t yDst = ySrc;
3373 uint32_t u32DstWidth = u32SrcWidth;
3374 uint32_t u32DstHeight = u32SrcHeight;
3375 uint32_t u32DstLineSize = u32DstWidth * 4;
3376 uint32_t u32DstBitsPerPixel = 32;
3377
3378 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
3379 * implies resize of Framebuffer is in progress and
3380 * copyrect should not be called.
3381 */
3382 if (uWidth == pFBInfo->w && uHeight == pFBInfo->h)
3383 {
3384
3385 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
3386 width, height,
3387 pu8Src,
3388 xSrc, ySrc,
3389 u32SrcWidth, u32SrcHeight,
3390 u32SrcLineSize, u32SrcBitsPerPixel,
3391 pu8Dst,
3392 xDst, yDst,
3393 u32DstWidth, u32DstHeight,
3394 u32DstLineSize, u32DstBitsPerPixel);
3395 }
3396 }
3397 }
3398
3399 pDisplay->handleDisplayUpdate (uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
3400 }
3401 }
3402 if (!fUpdateAll)
3403 break;
3404 }
3405 pDisplay->vbvaUnlock();
3406}
3407
3408/**
3409 * Does a full invalidation of the VM display and instructs the VM
3410 * to update it immediately.
3411 *
3412 * @returns COM status code
3413 */
3414STDMETHODIMP Display::InvalidateAndUpdate()
3415{
3416 LogRelFlowFunc(("\n"));
3417
3418 AutoCaller autoCaller(this);
3419 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3420
3421 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3422
3423 CHECK_CONSOLE_DRV(mpDrv);
3424
3425 Console::SafeVMPtr ptrVM(mParent);
3426 if (!ptrVM.isOk())
3427 return ptrVM.rc();
3428
3429 HRESULT rc = S_OK;
3430
3431 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
3432
3433 /* Have to release the lock when calling EMT. */
3434 alock.release();
3435
3436 /* pdm.h says that this has to be called from the EMT thread */
3437 int rcVBox = VMR3ReqCallVoidWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::InvalidateAndUpdateEMT,
3438 3, this, 0, true);
3439 alock.acquire();
3440
3441 if (RT_FAILURE(rcVBox))
3442 rc = setError(VBOX_E_IPRT_ERROR,
3443 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
3444
3445 LogRelFlowFunc(("rc=%Rhrc\n", rc));
3446 return rc;
3447}
3448
3449/**
3450 * Notification that the framebuffer has completed the
3451 * asynchronous resize processing
3452 *
3453 * @returns COM status code
3454 */
3455STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
3456{
3457 LogRelFlowFunc(("\n"));
3458
3459 /// @todo (dmik) can we AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); here?
3460 // This will require general code review and may add some details.
3461 // In particular, we may want to check whether EMT is really waiting for
3462 // this notification, etc. It might be also good to obey the caller to make
3463 // sure this method is not called from more than one thread at a time
3464 // (and therefore don't use Display lock at all here to save some
3465 // milliseconds).
3466 AutoCaller autoCaller(this);
3467 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3468
3469 /* this is only valid for external framebuffers */
3470 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
3471 return setError(VBOX_E_NOT_SUPPORTED,
3472 tr("Resize completed notification is valid only for external framebuffers"));
3473
3474 /* Set the flag indicating that the resize has completed and display
3475 * data need to be updated. */
3476 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
3477 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
3478 AssertRelease(f);NOREF(f);
3479
3480 return S_OK;
3481}
3482
3483STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
3484{
3485#ifdef VBOX_WITH_VIDEOHWACCEL
3486 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsynch(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
3487 return S_OK;
3488#else
3489 return E_NOTIMPL;
3490#endif
3491}
3492
3493STDMETHODIMP Display::ViewportChanged(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
3494{
3495#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3496
3497 if (mcMonitors <= aScreenId)
3498 {
3499 AssertMsgFailed(("invalid screen id\n"));
3500 return E_INVALIDARG;
3501 }
3502
3503 BOOL is3denabled;
3504 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3505
3506 if (is3denabled)
3507 {
3508 VMMDev *pVMMDev = mParent->getVMMDev();
3509
3510 if (pVMMDev)
3511 {
3512 crViewportNotify(pVMMDev, aScreenId, x, y, width, height);
3513 }
3514 else
3515 {
3516 DISPLAYFBINFO *pFb = &maFramebuffers[aScreenId];
3517 pFb->pendingViewportInfo.fPending = true;
3518 pFb->pendingViewportInfo.x = x;
3519 pFb->pendingViewportInfo.y = y;
3520 pFb->pendingViewportInfo.width = width;
3521 pFb->pendingViewportInfo.height = height;
3522 }
3523 }
3524#endif /* VBOX_WITH_CROGL && VBOX_WITH_HGCM */
3525 return S_OK;
3526}
3527
3528// private methods
3529/////////////////////////////////////////////////////////////////////////////
3530
3531/**
3532 * Helper to update the display information from the framebuffer.
3533 *
3534 * @thread EMT
3535 */
3536int Display::updateDisplayData(void)
3537{
3538 LogRelFlowFunc(("\n"));
3539
3540 /* the driver might not have been constructed yet */
3541 if (!mpDrv)
3542 return VINF_SUCCESS;
3543
3544#ifdef VBOX_STRICT
3545 /*
3546 * Sanity check. Note that this method may be called on EMT after Console
3547 * has started the power down procedure (but before our #drvDestruct() is
3548 * called, in which case pVM will already be NULL but mpDrv will not). Since
3549 * we don't really need pVM to proceed, we avoid this check in the release
3550 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
3551 * time-critical method.
3552 */
3553 Console::SafeVMPtrQuiet ptrVM(mParent);
3554 if (ptrVM.isOk())
3555 {
3556 PVM pVM = VMR3GetVM(ptrVM.rawUVM());
3557 Assert(VM_IS_EMT(pVM));
3558 }
3559#endif
3560
3561 /* The method is only relevant to the primary framebuffer. */
3562 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
3563
3564 if (pFramebuffer)
3565 {
3566 HRESULT rc;
3567 BYTE *address = 0;
3568 rc = pFramebuffer->COMGETTER(Address) (&address);
3569 AssertComRC (rc);
3570 ULONG bytesPerLine = 0;
3571 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
3572 AssertComRC (rc);
3573 ULONG bitsPerPixel = 0;
3574 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
3575 AssertComRC (rc);
3576 ULONG width = 0;
3577 rc = pFramebuffer->COMGETTER(Width) (&width);
3578 AssertComRC (rc);
3579 ULONG height = 0;
3580 rc = pFramebuffer->COMGETTER(Height) (&height);
3581 AssertComRC (rc);
3582
3583 if ( (width != mLastWidth && mLastWidth != 0)
3584 || (height != mLastHeight && mLastHeight != 0))
3585 {
3586 LogRel(("updateDisplayData: size mismatch w %d(%d) h %d(%d)\n",
3587 width, mLastWidth, height, mLastHeight));
3588 return VERR_INVALID_STATE;
3589 }
3590
3591 mpDrv->IConnector.pu8Data = (uint8_t *) address;
3592 mpDrv->IConnector.cbScanline = bytesPerLine;
3593 mpDrv->IConnector.cBits = bitsPerPixel;
3594 mpDrv->IConnector.cx = width;
3595 mpDrv->IConnector.cy = height;
3596 }
3597 else
3598 {
3599 /* black hole */
3600 mpDrv->IConnector.pu8Data = NULL;
3601 mpDrv->IConnector.cbScanline = 0;
3602 mpDrv->IConnector.cBits = 0;
3603 mpDrv->IConnector.cx = 0;
3604 mpDrv->IConnector.cy = 0;
3605 }
3606 LogRelFlowFunc(("leave\n"));
3607 return VINF_SUCCESS;
3608}
3609
3610#ifdef VBOX_WITH_CROGL
3611void Display::crViewportNotify(VMMDev *pVMMDev, ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
3612{
3613 struct {
3614 VBOXCRCMDCTL_HGCM data;
3615 VBOXHGCMSVCPARM aParms[4];
3616 } s;
3617
3618 s.data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3619 s.data.Hdr.u32Function = SHCRGL_HOST_FN_VIEWPORT_CHANGED;
3620
3621 s.data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
3622 s.data.aParms[0].u.uint32 = aScreenId;
3623
3624 s.data.aParms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
3625 s.data.aParms[1].u.uint32 = x;
3626
3627 s.data.aParms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
3628 s.data.aParms[2].u.uint32 = y;
3629
3630 s.data.aParms[3].type = VBOX_HGCM_SVC_PARM_32BIT;
3631 s.data.aParms[3].u.uint32 = width;
3632
3633 s.data.aParms[4].type = VBOX_HGCM_SVC_PARM_32BIT;
3634 s.data.aParms[4].u.uint32 = height;
3635
3636 crCtlSubmitSync(&s.data.Hdr, RT_OFFSETOF(VBOXCRCMDCTL_HGCM, aParms[5]));
3637}
3638#endif
3639
3640#ifdef VBOX_WITH_CRHGSMI
3641void Display::setupCrHgsmiData(void)
3642{
3643 VMMDev *pVMMDev = mParent->getVMMDev();
3644 Assert(pVMMDev);
3645 int rc = VERR_GENERAL_FAILURE;
3646 if (pVMMDev)
3647 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
3648
3649 if (RT_SUCCESS(rc))
3650 {
3651 Assert(mhCrOglSvc);
3652 /* setup command completion callback */
3653 VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB Completion;
3654 Completion.Hdr.enmType = VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_MAINCB;
3655 Completion.Hdr.cbCmd = sizeof (Completion);
3656 Completion.hCompletion = mpDrv->pVBVACallbacks;
3657 Completion.pfnCompletion = mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync;
3658
3659 VBOXHGCMSVCPARM parm;
3660 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3661 parm.u.pointer.addr = &Completion;
3662 parm.u.pointer.size = 0;
3663
3664 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_CRHGSMI_CTL, 1, &parm);
3665 if (RT_SUCCESS(rc))
3666 {
3667 mCrOglCallbacks = Completion.MainInterface;
3668
3669 return;
3670 }
3671
3672 AssertMsgFailed(("VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION failed rc %d", rc));
3673 }
3674
3675 mhCrOglSvc = NULL;
3676}
3677
3678void Display::destructCrHgsmiData(void)
3679{
3680 mhCrOglSvc = NULL;
3681}
3682#endif
3683
3684/**
3685 * Changes the current frame buffer. Called on EMT to avoid both
3686 * race conditions and excessive locking.
3687 *
3688 * @note locks this object for writing
3689 * @thread EMT
3690 */
3691/* static */
3692DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
3693 unsigned uScreenId)
3694{
3695 LogRelFlowFunc(("uScreenId = %d\n", uScreenId));
3696
3697 AssertReturn(that, VERR_INVALID_PARAMETER);
3698 AssertReturn(uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
3699
3700 AutoCaller autoCaller(that);
3701 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3702
3703 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
3704
3705 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
3706 pDisplayFBInfo->pFramebuffer = aFB;
3707
3708 that->mParent->consoleVRDPServer()->SendResize ();
3709
3710 /* The driver might not have been constructed yet */
3711 if (that->mpDrv)
3712 {
3713 /* Setup the new framebuffer, the resize will lead to an updateDisplayData call. */
3714 DISPLAYFBINFO *pFBInfo = &that->maFramebuffers[uScreenId];
3715
3716#if defined(VBOX_WITH_CROGL)
3717 /* Release the lock, because SHCRGL_HOST_FN_SCREEN_CHANGED will read current framebuffer */
3718 {
3719 BOOL is3denabled;
3720 that->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3721
3722 if (is3denabled)
3723 {
3724 alock.release();
3725 }
3726 }
3727#endif
3728
3729 if (pFBInfo->fVBVAEnabled && pFBInfo->pu8FramebufferVRAM)
3730 {
3731 /* This display in VBVA mode. Resize it to the last guest resolution,
3732 * if it has been reported.
3733 */
3734 that->handleDisplayResize(uScreenId, pFBInfo->u16BitsPerPixel,
3735 pFBInfo->pu8FramebufferVRAM,
3736 pFBInfo->u32LineSize,
3737 pFBInfo->w,
3738 pFBInfo->h,
3739 pFBInfo->flags);
3740 }
3741 else if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3742 {
3743 /* VGA device mode, only for the primary screen. */
3744 that->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, that->mLastBitsPerPixel,
3745 that->mLastAddress,
3746 that->mLastBytesPerLine,
3747 that->mLastWidth,
3748 that->mLastHeight,
3749 that->mLastFlags);
3750 }
3751 }
3752
3753 LogRelFlowFunc(("leave\n"));
3754 return VINF_SUCCESS;
3755}
3756
3757/**
3758 * Handle display resize event issued by the VGA device for the primary screen.
3759 *
3760 * @see PDMIDISPLAYCONNECTOR::pfnResize
3761 */
3762DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
3763 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
3764{
3765 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3766
3767 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
3768 bpp, pvVRAM, cbLine, cx, cy));
3769
3770 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, VBVA_SCREEN_F_ACTIVE);
3771}
3772
3773/**
3774 * Handle display update.
3775 *
3776 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
3777 */
3778DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
3779 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
3780{
3781 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3782
3783#ifdef DEBUG_sunlover
3784 LogFlowFunc(("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
3785 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
3786#endif /* DEBUG_sunlover */
3787
3788 /* This call does update regardless of VBVA status.
3789 * But in VBVA mode this is called only as result of
3790 * pfnUpdateDisplayAll in the VGA device.
3791 */
3792
3793 pDrv->pDisplay->handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
3794}
3795
3796/**
3797 * Periodic display refresh callback.
3798 *
3799 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
3800 * @thread EMT
3801 */
3802DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
3803{
3804 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3805
3806#ifdef DEBUG_sunlover
3807 STAM_PROFILE_START(&g_StatDisplayRefresh, a);
3808#endif /* DEBUG_sunlover */
3809
3810#ifdef DEBUG_sunlover_2
3811 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
3812 pDrv->pDisplay->mfVideoAccelEnabled));
3813#endif /* DEBUG_sunlover_2 */
3814
3815 Display *pDisplay = pDrv->pDisplay;
3816 bool fNoUpdate = false; /* Do not update the display if any of the framebuffers is being resized. */
3817 unsigned uScreenId;
3818
3819 Log2(("DisplayRefreshCallback\n"));
3820 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3821 {
3822 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3823
3824 /* Check the resize status. The status can be checked normally because
3825 * the status affects only the EMT.
3826 */
3827 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
3828
3829 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
3830 {
3831 LogRelFlowFunc(("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
3832 fNoUpdate = true; /* Always set it here, because pfnUpdateDisplayAll can cause a new resize. */
3833 /* The framebuffer was resized and display data need to be updated. */
3834 pDisplay->handleResizeCompletedEMT(uScreenId);
3835 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
3836 {
3837 /* The resize status could be not Void here because a pending resize is issued. */
3838 continue;
3839 }
3840
3841 /* Repaint the display because VM continued to run during the framebuffer resize. */
3842 pDisplay->InvalidateAndUpdateEMT(pDisplay, uScreenId, false);
3843
3844 /* Continue with normal processing because the status here is ResizeStatus_Void. */
3845 }
3846 else if (u32ResizeStatus == ResizeStatus_InProgress)
3847 {
3848 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
3849 LogRelFlowFunc(("ResizeStatus_InProcess\n"));
3850 fNoUpdate = true;
3851 continue;
3852 }
3853 }
3854
3855 if (!fNoUpdate)
3856 {
3857 int rc = pDisplay->videoAccelRefreshProcess();
3858 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3859 {
3860 if (rc == VWRN_INVALID_STATE)
3861 {
3862 /* No VBVA do a display update. */
3863 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3864 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3865 {
3866 Assert(pDrv->IConnector.pu8Data);
3867 pDisplay->vbvaLock();
3868 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3869 pDisplay->vbvaUnlock();
3870 }
3871 }
3872
3873 /* Inform the VRDP server that the current display update sequence is
3874 * completed. At this moment the framebuffer memory contains a definite
3875 * image, that is synchronized with the orders already sent to VRDP client.
3876 * The server can now process redraw requests from clients or initial
3877 * fullscreen updates for new clients.
3878 */
3879 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3880 {
3881 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3882
3883 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3884 {
3885 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
3886 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
3887 }
3888 }
3889 }
3890 }
3891
3892#ifdef VBOX_WITH_VPX
3893 if (VideoRecIsEnabled(pDisplay->mpVideoRecCtx))
3894 {
3895 do {
3896# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3897 BOOL is3denabled;
3898 pDisplay->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3899 if (is3denabled)
3900 {
3901 if (ASMAtomicCmpXchgU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_SUBMITTED, CRVREC_STATE_IDLE))
3902 {
3903 if (pDisplay->mCrOglCallbacks.pfnHasData())
3904 {
3905 /* submit */
3906 VBOXCRCMDCTL_HGCM *pData = &pDisplay->mCrOglScreenshotCtl;
3907
3908 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3909 pData->Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
3910
3911 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3912 pData->aParms[0].u.pointer.addr = &pDisplay->mCrOglScreenshotData;
3913 pData->aParms[0].u.pointer.size = sizeof (pDisplay->mCrOglScreenshotData);
3914 int rc = pDisplay->crCtlSubmit(&pData->Hdr, sizeof (*pData), displayCrCmdFree, pData);
3915 if (!RT_SUCCESS(rc))
3916 AssertMsgFailed(("crCtlSubmit failed rc %n", rc));
3917 }
3918
3919 /* no 3D data available, or error has occured,
3920 * go the straight way */
3921 ASMAtomicWriteU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_IDLE);
3922 }
3923 else
3924 {
3925 /* record request is still in progress, don't do anything */
3926 break;
3927 }
3928 }
3929# endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3930
3931 uint64_t u64Now = RTTimeProgramMilliTS();
3932 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3933 {
3934 if (!pDisplay->maVideoRecEnabled[uScreenId])
3935 continue;
3936
3937 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3938
3939 if ( !pFBInfo->pFramebuffer.isNull()
3940 && !pFBInfo->fDisabled
3941 && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3942 {
3943 int rc;
3944 if ( pFBInfo->fVBVAEnabled
3945 && pFBInfo->pu8FramebufferVRAM)
3946 {
3947 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
3948 FramebufferPixelFormat_FOURCC_RGB,
3949 pFBInfo->u16BitsPerPixel,
3950 pFBInfo->u32LineSize, pFBInfo->w, pFBInfo->h,
3951 pFBInfo->pu8FramebufferVRAM, u64Now);
3952 }
3953 else
3954 {
3955 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
3956 FramebufferPixelFormat_FOURCC_RGB,
3957 pDrv->IConnector.cBits,
3958 pDrv->IConnector.cbScanline, pDrv->IConnector.cx,
3959 pDrv->IConnector.cy, pDrv->IConnector.pu8Data, u64Now);
3960 }
3961 if (rc == VINF_TRY_AGAIN)
3962 break;
3963 }
3964 }
3965 } while (0);
3966 }
3967#endif /* VBOX_WITH_VPX */
3968
3969#ifdef DEBUG_sunlover
3970 STAM_PROFILE_STOP(&g_StatDisplayRefresh, a);
3971#endif /* DEBUG_sunlover */
3972#ifdef DEBUG_sunlover_2
3973 LogFlowFunc(("leave\n"));
3974#endif /* DEBUG_sunlover_2 */
3975}
3976
3977/**
3978 * Reset notification
3979 *
3980 * @see PDMIDISPLAYCONNECTOR::pfnReset
3981 */
3982DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3983{
3984 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3985
3986 LogRelFlowFunc(("\n"));
3987
3988 /* Disable VBVA mode. */
3989 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3990}
3991
3992/**
3993 * LFBModeChange notification
3994 *
3995 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3996 */
3997DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3998{
3999 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4000
4001 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
4002
4003 NOREF(fEnabled);
4004
4005 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
4006 /* The LFBModeChange function is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
4007 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
4008}
4009
4010/**
4011 * Adapter information change notification.
4012 *
4013 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
4014 */
4015DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
4016{
4017 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4018
4019 if (pvVRAM == NULL)
4020 {
4021 unsigned i;
4022 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
4023 {
4024 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
4025
4026 pFBInfo->u32Offset = 0;
4027 pFBInfo->u32MaxFramebufferSize = 0;
4028 pFBInfo->u32InformationSize = 0;
4029 }
4030 }
4031#ifndef VBOX_WITH_HGSMI
4032 else
4033 {
4034 uint8_t *pu8 = (uint8_t *)pvVRAM;
4035 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
4036
4037 // @todo
4038 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
4039
4040 VBOXVIDEOINFOHDR *pHdr;
4041
4042 for (;;)
4043 {
4044 pHdr = (VBOXVIDEOINFOHDR *)pu8;
4045 pu8 += sizeof (VBOXVIDEOINFOHDR);
4046
4047 if (pu8 >= pu8End)
4048 {
4049 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
4050 break;
4051 }
4052
4053 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
4054 {
4055 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
4056 {
4057 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
4058 break;
4059 }
4060
4061 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
4062
4063 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
4064 {
4065 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
4066 break;
4067 }
4068
4069 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
4070
4071 pFBInfo->u32Offset = pDisplay->u32Offset;
4072 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
4073 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
4074
4075 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));
4076 }
4077 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
4078 {
4079 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
4080 {
4081 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
4082 break;
4083 }
4084
4085 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
4086
4087 switch (pConf32->u32Index)
4088 {
4089 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
4090 {
4091 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
4092 } break;
4093
4094 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
4095 {
4096 /* @todo make configurable. */
4097 pConf32->u32Value = _1M;
4098 } break;
4099
4100 default:
4101 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
4102 }
4103 }
4104 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
4105 {
4106 if (pHdr->u16Length != 0)
4107 {
4108 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
4109 break;
4110 }
4111
4112 break;
4113 }
4114 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
4115 {
4116 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
4117 }
4118
4119 pu8 += pHdr->u16Length;
4120 }
4121 }
4122#endif /* !VBOX_WITH_HGSMI */
4123}
4124
4125/**
4126 * Display information change notification.
4127 *
4128 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
4129 */
4130DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
4131{
4132 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4133
4134 if (uScreenId >= pDrv->pDisplay->mcMonitors)
4135 {
4136 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
4137 return;
4138 }
4139
4140 /* Get the display information structure. */
4141 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
4142
4143 uint8_t *pu8 = (uint8_t *)pvVRAM;
4144 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
4145
4146 // @todo
4147 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
4148
4149 VBOXVIDEOINFOHDR *pHdr;
4150
4151 for (;;)
4152 {
4153 pHdr = (VBOXVIDEOINFOHDR *)pu8;
4154 pu8 += sizeof (VBOXVIDEOINFOHDR);
4155
4156 if (pu8 >= pu8End)
4157 {
4158 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
4159 break;
4160 }
4161
4162 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
4163 {
4164 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
4165 {
4166 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
4167 break;
4168 }
4169
4170 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
4171
4172 pFBInfo->xOrigin = pScreen->xOrigin;
4173 pFBInfo->yOrigin = pScreen->yOrigin;
4174
4175 pFBInfo->w = pScreen->u16Width;
4176 pFBInfo->h = pScreen->u16Height;
4177
4178 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
4179 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
4180
4181 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
4182 {
4183 /* Primary screen resize is eeeeeeeee by the VGA device. */
4184 if (pFBInfo->fDisabled)
4185 {
4186 pFBInfo->fDisabled = false;
4187 fireGuestMonitorChangedEvent(pDrv->pDisplay->mParent->getEventSource(),
4188 GuestMonitorChangedEventType_Enabled,
4189 uScreenId,
4190 pFBInfo->xOrigin, pFBInfo->yOrigin,
4191 pFBInfo->w, pFBInfo->h);
4192 }
4193
4194 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, VBVA_SCREEN_F_ACTIVE);
4195 }
4196 }
4197 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
4198 {
4199 if (pHdr->u16Length != 0)
4200 {
4201 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
4202 break;
4203 }
4204
4205 break;
4206 }
4207 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
4208 {
4209 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
4210 {
4211 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
4212 break;
4213 }
4214
4215 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
4216
4217 pFBInfo->pHostEvents = pHostEvents;
4218
4219 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
4220 pHostEvents));
4221 }
4222 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
4223 {
4224 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
4225 {
4226 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
4227 break;
4228 }
4229
4230 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
4231 pu8 += pLink->i32Offset;
4232 }
4233 else
4234 {
4235 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
4236 }
4237
4238 pu8 += pHdr->u16Length;
4239 }
4240}
4241
4242#ifdef VBOX_WITH_VIDEOHWACCEL
4243
4244#ifndef S_FALSE
4245# define S_FALSE ((HRESULT)1L)
4246#endif
4247
4248int Display::handleVHWACommandProcess(PVBOXVHWACMD pCommand)
4249{
4250 unsigned id = (unsigned)pCommand->iDisplay;
4251 int rc = VINF_SUCCESS;
4252 if (id >= mcMonitors)
4253 return VERR_INVALID_PARAMETER;
4254
4255 ComPtr<IFramebuffer> pFramebuffer;
4256 AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
4257 pFramebuffer = maFramebuffers[id].pFramebuffer;
4258 arlock.release();
4259
4260 if (pFramebuffer == NULL)
4261 return VERR_INVALID_STATE; /* notify we can not handle request atm */
4262
4263 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
4264 if (hr == S_FALSE)
4265 return VINF_SUCCESS;
4266 else if (SUCCEEDED(hr))
4267 return VINF_CALLBACK_RETURN;
4268 else if (hr == E_ACCESSDENIED)
4269 return VERR_INVALID_STATE; /* notify we can not handle request atm */
4270 else if (hr == E_NOTIMPL)
4271 return VERR_NOT_IMPLEMENTED;
4272 return VERR_GENERAL_FAILURE;
4273}
4274
4275DECLCALLBACK(int) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
4276{
4277 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4278
4279 return pDrv->pDisplay->handleVHWACommandProcess(pCommand);
4280}
4281#endif
4282
4283#ifdef VBOX_WITH_CRHGSMI
4284void Display::handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
4285{
4286 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
4287}
4288
4289void Display::handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
4290{
4291 PVBOXVDMACMD_CHROMIUM_CTL pCtl = (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr;
4292 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, pCtl, result);
4293}
4294
4295void Display::handleCrHgsmiCommandProcess(PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
4296{
4297 int rc = VERR_NOT_SUPPORTED;
4298 VBOXHGCMSVCPARM parm;
4299 parm.type = VBOX_HGCM_SVC_PARM_PTR;
4300 parm.u.pointer.addr = pCmd;
4301 parm.u.pointer.size = cbCmd;
4302
4303 if (mhCrOglSvc)
4304 {
4305 VMMDev *pVMMDev = mParent->getVMMDev();
4306 if (pVMMDev)
4307 {
4308 /* no completion callback is specified with this call,
4309 * the CrOgl code will complete the CrHgsmi command once it processes it */
4310 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
4311 AssertRC(rc);
4312 if (RT_SUCCESS(rc))
4313 return;
4314 }
4315 else
4316 rc = VERR_INVALID_STATE;
4317 }
4318
4319 /* we are here because something went wrong with command processing, complete it */
4320 handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
4321}
4322
4323void Display::handleCrHgsmiControlProcess(PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
4324{
4325 int rc = VERR_NOT_SUPPORTED;
4326 VBOXHGCMSVCPARM parm;
4327 parm.type = VBOX_HGCM_SVC_PARM_PTR;
4328 parm.u.pointer.addr = pCtl;
4329 parm.u.pointer.size = cbCtl;
4330
4331 if (mhCrOglSvc)
4332 {
4333 VMMDev *pVMMDev = mParent->getVMMDev();
4334 if (pVMMDev)
4335 {
4336 bool fCheckPendingViewport = (pCtl->enmType == VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP);
4337 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm, Display::displayCrHgsmiControlCompletion, this);
4338 AssertRC(rc);
4339 if (RT_SUCCESS(rc))
4340 {
4341 if (fCheckPendingViewport)
4342 {
4343 ULONG ul;
4344 for (ul = 0; ul < mcMonitors; ul++)
4345 {
4346 DISPLAYFBINFO *pFb = &maFramebuffers[ul];
4347 if (!pFb->pendingViewportInfo.fPending)
4348 continue;
4349
4350 crViewportNotify(pVMMDev, ul, pFb->pendingViewportInfo.x, pFb->pendingViewportInfo.y, pFb->pendingViewportInfo.width, pFb->pendingViewportInfo.height);
4351 pFb->pendingViewportInfo.fPending = false;
4352 }
4353 }
4354 return;
4355 }
4356 }
4357 else
4358 rc = VERR_INVALID_STATE;
4359 }
4360
4361 /* we are here because something went wrong with command processing, complete it */
4362 handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
4363}
4364
4365DECLCALLBACK(void) Display::displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
4366{
4367 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4368
4369 pDrv->pDisplay->handleCrHgsmiCommandProcess(pCmd, cbCmd);
4370}
4371
4372DECLCALLBACK(void) Display::displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd, uint32_t cbCmd)
4373{
4374 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4375
4376 pDrv->pDisplay->handleCrHgsmiControlProcess(pCmd, cbCmd);
4377}
4378
4379DECLCALLBACK(void) Display::displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
4380{
4381 AssertMsgFailed(("not expected!"));
4382 Display *pDisplay = (Display *)pvContext;
4383 pDisplay->handleCrHgsmiCommandCompletion(result, u32Function, pParam);
4384}
4385
4386DECLCALLBACK(void) Display::displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
4387{
4388 Display *pDisplay = (Display *)pvContext;
4389 pDisplay->handleCrHgsmiControlCompletion(result, u32Function, pParam);
4390
4391}
4392#endif
4393
4394DECLCALLBACK(void) Display::displayCrHgcmCtlSubmitCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
4395{
4396 VBOXCRCMDCTL *pCmd = (VBOXCRCMDCTL*)pParam->u.pointer.addr;
4397 if (pCmd->u.pfnInternal)
4398 ((PFNCRCTLCOMPLETION)pCmd->u.pfnInternal)(pCmd, pParam->u.pointer.size, result, pvContext);
4399}
4400
4401int Display::handleCrHgcmCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
4402 PFNCRCTLCOMPLETION pfnCompletion,
4403 void *pvCompletion)
4404{
4405 VMMDev *pVMMDev = mParent->getVMMDev();
4406 if (!pVMMDev)
4407 {
4408 AssertMsgFailed(("no vmmdev\n"));
4409 return VERR_INVALID_STATE;
4410 }
4411
4412 Assert(mhCrOglSvc);
4413 VBOXHGCMSVCPARM parm;
4414 parm.type = VBOX_HGCM_SVC_PARM_PTR;
4415 parm.u.pointer.addr = pCmd;
4416 parm.u.pointer.size = cbCmd;
4417
4418 pCmd->u.pfnInternal = (void(*)())pfnCompletion;
4419 int rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CTL, &parm, displayCrHgcmCtlSubmitCompletion, pvCompletion);
4420 if (!RT_SUCCESS(rc))
4421 AssertMsgFailed(("hgcmHostFastCallAsync failed rc %n", rc));
4422
4423 return rc;
4424}
4425
4426DECLCALLBACK(int) Display::displayCrHgcmCtlSubmit(PPDMIDISPLAYCONNECTOR pInterface,
4427 struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
4428 PFNCRCTLCOMPLETION pfnCompletion,
4429 void *pvCompletion)
4430{
4431 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4432 Display *pThis = pDrv->pDisplay;
4433 return pThis->handleCrHgcmCtlSubmit(pCmd, cbCmd, pfnCompletion, pvCompletion);
4434}
4435
4436int Display::crCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, PFNCRCTLCOMPLETION pfnCompletion, void *pvCompletion)
4437{
4438 return mpDrv->pVBVACallbacks->pfnCrCtlSubmit(mpDrv->pVBVACallbacks, pCmd, cbCmd, pfnCompletion, pvCompletion);
4439}
4440
4441int Display::crCtlSubmitSync(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
4442{
4443 return mpDrv->pVBVACallbacks->pfnCrCtlSubmitSync(mpDrv->pVBVACallbacks, pCmd, cbCmd);
4444}
4445
4446#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4447bool Display::handleCrVRecScreenshotBegin(uint32_t uScreen, uint64_t u64TimeStamp)
4448{
4449# if VBOX_WITH_VPX
4450 return VideoRecIsReady(mpVideoRecCtx, uScreen, u64TimeStamp);
4451# else
4452 return false;
4453# endif
4454}
4455
4456void Display::handleCrVRecScreenshotEnd(uint32_t uScreen, uint64_t u64TimeStamp)
4457{
4458}
4459
4460void Display::handleCrVRecScreenshotPerform(uint32_t uScreen,
4461 uint32_t x, uint32_t y, uint32_t uPixelFormat,
4462 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
4463 uint32_t uGuestWidth, uint32_t uGuestHeight,
4464 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
4465{
4466 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
4467# if VBOX_WITH_VPX
4468 int rc = VideoRecCopyToIntBuf(mpVideoRecCtx, uScreen, x, y,
4469 uPixelFormat,
4470 uBitsPerPixel, uBytesPerLine,
4471 uGuestWidth, uGuestHeight,
4472 pu8BufferAddress, u64TimeStamp);
4473 Assert(rc == VINF_SUCCESS /* || rc == VERR_TRY_AGAIN || rc == VINF_TRY_AGAIN*/);
4474# endif
4475}
4476
4477void Display::handleVRecCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
4478{
4479 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
4480 ASMAtomicWriteU32(&mfCrOglVideoRecState, CRVREC_STATE_IDLE);
4481}
4482
4483DECLCALLBACK(void) Display::displayCrVRecScreenshotPerform(void *pvCtx, uint32_t uScreen,
4484 uint32_t x, uint32_t y,
4485 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
4486 uint32_t uGuestWidth, uint32_t uGuestHeight,
4487 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
4488{
4489 Display *pDisplay = (Display *)pvCtx;
4490 pDisplay->handleCrVRecScreenshotPerform(uScreen,
4491 x, y, FramebufferPixelFormat_FOURCC_RGB, uBitsPerPixel,
4492 uBytesPerLine, uGuestWidth, uGuestHeight,
4493 pu8BufferAddress, u64TimeStamp);
4494}
4495
4496DECLCALLBACK(bool) Display::displayCrVRecScreenshotBegin(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
4497{
4498 Display *pDisplay = (Display *)pvCtx;
4499 return pDisplay->handleCrVRecScreenshotBegin(uScreen, u64TimeStamp);
4500}
4501
4502DECLCALLBACK(void) Display::displayCrVRecScreenshotEnd(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
4503{
4504 Display *pDisplay = (Display *)pvCtx;
4505 pDisplay->handleCrVRecScreenshotEnd(uScreen, u64TimeStamp);
4506}
4507
4508DECLCALLBACK(void) Display::displayVRecCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
4509{
4510 Display *pDisplay = (Display *)pvContext;
4511 pDisplay->handleVRecCompletion(result, u32Function, pParam, pvContext);
4512}
4513
4514#endif
4515
4516
4517#ifdef VBOX_WITH_HGSMI
4518DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags)
4519{
4520 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
4521
4522 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4523 Display *pThis = pDrv->pDisplay;
4524
4525 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
4526 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
4527 pThis->maFramebuffers[uScreenId].fVBVAForceResize = true;
4528
4529 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
4530
4531 return VINF_SUCCESS;
4532}
4533
4534DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
4535{
4536 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
4537
4538 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4539 Display *pThis = pDrv->pDisplay;
4540
4541 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4542
4543 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
4544 {
4545 /* Make sure that the primary screen is visible now.
4546 * The guest can't use VBVA anymore, so only only the VGA device output works.
4547 */
4548 if (pFBInfo->fDisabled)
4549 {
4550 pFBInfo->fDisabled = false;
4551 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4552 GuestMonitorChangedEventType_Enabled,
4553 uScreenId,
4554 pFBInfo->xOrigin, pFBInfo->yOrigin,
4555 pFBInfo->w, pFBInfo->h);
4556 }
4557 }
4558
4559 pFBInfo->fVBVAEnabled = false;
4560 pFBInfo->fVBVAForceResize = false;
4561
4562 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
4563
4564 pFBInfo->pVBVAHostFlags = NULL;
4565}
4566
4567DECLCALLBACK(void) Display::displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
4568{
4569 LogFlowFunc(("uScreenId %d\n", uScreenId));
4570
4571 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4572 Display *pThis = pDrv->pDisplay;
4573 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4574
4575 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
4576 {
4577 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers, pThis->mcMonitors);
4578 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
4579 }
4580
4581 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
4582 {
4583 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
4584 {
4585 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
4586 * under display device lock, so thread safe.
4587 */
4588 pFBInfo->cVBVASkipUpdate = 0;
4589 pThis->handleDisplayUpdate(uScreenId, pFBInfo->vbvaSkippedRect.xLeft - pFBInfo->xOrigin,
4590 pFBInfo->vbvaSkippedRect.yTop - pFBInfo->yOrigin,
4591 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
4592 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
4593 }
4594 }
4595 else
4596 {
4597 /* The framebuffer is being resized. */
4598 pFBInfo->cVBVASkipUpdate++;
4599 }
4600}
4601
4602DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, const PVBVACMDHDR pCmd, size_t cbCmd)
4603{
4604 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
4605
4606 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4607 Display *pThis = pDrv->pDisplay;
4608 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4609
4610 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
4611 {
4612 if (pFBInfo->fDefaultFormat)
4613 {
4614 /* Make sure that framebuffer contains the same image as the guest VRAM. */
4615 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
4616 && !pFBInfo->pFramebuffer.isNull()
4617 && !pFBInfo->fDisabled)
4618 {
4619 pDrv->pUpPort->pfnUpdateDisplayRect (pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
4620 }
4621 else if ( !pFBInfo->pFramebuffer.isNull()
4622 && !pFBInfo->fDisabled)
4623 {
4624 /* Render VRAM content to the framebuffer. */
4625 BYTE *address = NULL;
4626 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
4627 if (SUCCEEDED(hrc) && address != NULL)
4628 {
4629 uint32_t width = pCmd->w;
4630 uint32_t height = pCmd->h;
4631
4632 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
4633 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
4634 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
4635 uint32_t u32SrcWidth = pFBInfo->w;
4636 uint32_t u32SrcHeight = pFBInfo->h;
4637 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
4638 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
4639
4640 uint8_t *pu8Dst = address;
4641 int32_t xDst = xSrc;
4642 int32_t yDst = ySrc;
4643 uint32_t u32DstWidth = u32SrcWidth;
4644 uint32_t u32DstHeight = u32SrcHeight;
4645 uint32_t u32DstLineSize = u32DstWidth * 4;
4646 uint32_t u32DstBitsPerPixel = 32;
4647
4648 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
4649 width, height,
4650 pu8Src,
4651 xSrc, ySrc,
4652 u32SrcWidth, u32SrcHeight,
4653 u32SrcLineSize, u32SrcBitsPerPixel,
4654 pu8Dst,
4655 xDst, yDst,
4656 u32DstWidth, u32DstHeight,
4657 u32DstLineSize, u32DstBitsPerPixel);
4658 }
4659 }
4660 }
4661
4662 VBVACMDHDR hdrSaved = *pCmd;
4663
4664 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
4665
4666 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
4667 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
4668
4669 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
4670 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, (uint32_t)cbCmd);
4671
4672 *pHdrUnconst = hdrSaved;
4673 }
4674}
4675
4676DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y, uint32_t cx, uint32_t cy)
4677{
4678 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
4679
4680 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4681 Display *pThis = pDrv->pDisplay;
4682 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4683
4684 /* @todo handleFramebufferUpdate (uScreenId,
4685 * x - pThis->maFramebuffers[uScreenId].xOrigin,
4686 * y - pThis->maFramebuffers[uScreenId].yOrigin,
4687 * cx, cy);
4688 */
4689 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
4690 {
4691 pThis->handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
4692 }
4693 else
4694 {
4695 /* Save the updated rectangle. */
4696 int32_t xRight = x + cx;
4697 int32_t yBottom = y + cy;
4698
4699 if (pFBInfo->cVBVASkipUpdate == 1)
4700 {
4701 pFBInfo->vbvaSkippedRect.xLeft = x;
4702 pFBInfo->vbvaSkippedRect.yTop = y;
4703 pFBInfo->vbvaSkippedRect.xRight = xRight;
4704 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
4705 }
4706 else
4707 {
4708 if (pFBInfo->vbvaSkippedRect.xLeft > x)
4709 {
4710 pFBInfo->vbvaSkippedRect.xLeft = x;
4711 }
4712 if (pFBInfo->vbvaSkippedRect.yTop > y)
4713 {
4714 pFBInfo->vbvaSkippedRect.yTop = y;
4715 }
4716 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
4717 {
4718 pFBInfo->vbvaSkippedRect.xRight = xRight;
4719 }
4720 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
4721 {
4722 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
4723 }
4724 }
4725 }
4726}
4727
4728#ifdef DEBUG_sunlover
4729static void logVBVAResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
4730{
4731 LogRel(("displayVBVAResize: [%d] %s\n"
4732 " pView->u32ViewIndex %d\n"
4733 " pView->u32ViewOffset 0x%08X\n"
4734 " pView->u32ViewSize 0x%08X\n"
4735 " pView->u32MaxScreenSize 0x%08X\n"
4736 " pScreen->i32OriginX %d\n"
4737 " pScreen->i32OriginY %d\n"
4738 " pScreen->u32StartOffset 0x%08X\n"
4739 " pScreen->u32LineSize 0x%08X\n"
4740 " pScreen->u32Width %d\n"
4741 " pScreen->u32Height %d\n"
4742 " pScreen->u16BitsPerPixel %d\n"
4743 " pScreen->u16Flags 0x%04X\n"
4744 " pFBInfo->u32Offset 0x%08X\n"
4745 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
4746 " pFBInfo->u32InformationSize 0x%08X\n"
4747 " pFBInfo->fDisabled %d\n"
4748 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
4749 " pFBInfo->u16BitsPerPixel %d\n"
4750 " pFBInfo->pu8FramebufferVRAM %p\n"
4751 " pFBInfo->u32LineSize 0x%08X\n"
4752 " pFBInfo->flags 0x%04X\n"
4753 " pFBInfo->pHostEvents %p\n"
4754 " pFBInfo->u32ResizeStatus %d\n"
4755 " pFBInfo->fDefaultFormat %d\n"
4756 " dirtyRect %d-%d %d-%d\n"
4757 " pFBInfo->pendingResize.fPending %d\n"
4758 " pFBInfo->pendingResize.pixelFormat %d\n"
4759 " pFBInfo->pendingResize.pvVRAM %p\n"
4760 " pFBInfo->pendingResize.bpp %d\n"
4761 " pFBInfo->pendingResize.cbLine 0x%08X\n"
4762 " pFBInfo->pendingResize.w,h %dx%d\n"
4763 " pFBInfo->pendingResize.flags 0x%04X\n"
4764 " pFBInfo->fVBVAEnabled %d\n"
4765 " pFBInfo->fVBVAForceResize %d\n"
4766 " pFBInfo->cVBVASkipUpdate %d\n"
4767 " pFBInfo->vbvaSkippedRect %d-%d %d-%d\n"
4768 " pFBInfo->pVBVAHostFlags %p\n"
4769 "",
4770 pScreen->u32ViewIndex,
4771 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
4772 pView->u32ViewIndex,
4773 pView->u32ViewOffset,
4774 pView->u32ViewSize,
4775 pView->u32MaxScreenSize,
4776 pScreen->i32OriginX,
4777 pScreen->i32OriginY,
4778 pScreen->u32StartOffset,
4779 pScreen->u32LineSize,
4780 pScreen->u32Width,
4781 pScreen->u32Height,
4782 pScreen->u16BitsPerPixel,
4783 pScreen->u16Flags,
4784 pFBInfo->u32Offset,
4785 pFBInfo->u32MaxFramebufferSize,
4786 pFBInfo->u32InformationSize,
4787 pFBInfo->fDisabled,
4788 pFBInfo->xOrigin,
4789 pFBInfo->yOrigin,
4790 pFBInfo->w,
4791 pFBInfo->h,
4792 pFBInfo->u16BitsPerPixel,
4793 pFBInfo->pu8FramebufferVRAM,
4794 pFBInfo->u32LineSize,
4795 pFBInfo->flags,
4796 pFBInfo->pHostEvents,
4797 pFBInfo->u32ResizeStatus,
4798 pFBInfo->fDefaultFormat,
4799 pFBInfo->dirtyRect.xLeft,
4800 pFBInfo->dirtyRect.xRight,
4801 pFBInfo->dirtyRect.yTop,
4802 pFBInfo->dirtyRect.yBottom,
4803 pFBInfo->pendingResize.fPending,
4804 pFBInfo->pendingResize.pixelFormat,
4805 pFBInfo->pendingResize.pvVRAM,
4806 pFBInfo->pendingResize.bpp,
4807 pFBInfo->pendingResize.cbLine,
4808 pFBInfo->pendingResize.w,
4809 pFBInfo->pendingResize.h,
4810 pFBInfo->pendingResize.flags,
4811 pFBInfo->fVBVAEnabled,
4812 pFBInfo->fVBVAForceResize,
4813 pFBInfo->cVBVASkipUpdate,
4814 pFBInfo->vbvaSkippedRect.xLeft,
4815 pFBInfo->vbvaSkippedRect.yTop,
4816 pFBInfo->vbvaSkippedRect.xRight,
4817 pFBInfo->vbvaSkippedRect.yBottom,
4818 pFBInfo->pVBVAHostFlags
4819 ));
4820}
4821#endif /* DEBUG_sunlover */
4822
4823DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
4824{
4825 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
4826
4827 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4828 Display *pThis = pDrv->pDisplay;
4829
4830 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
4831
4832 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
4833 {
4834 pThis->notifyCroglResize(pView, pScreen, pvVRAM);
4835
4836 pFBInfo->fDisabled = true;
4837 pFBInfo->flags = pScreen->u16Flags;
4838
4839 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
4840 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
4841 * the VM window will be black. */
4842 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
4843 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
4844 pThis->handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
4845 u32Width, u32Height, pScreen->u16Flags);
4846
4847 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4848 GuestMonitorChangedEventType_Disabled,
4849 pScreen->u32ViewIndex,
4850 0, 0, 0, 0);
4851 return VINF_SUCCESS;
4852 }
4853
4854 /* If display was disabled or there is no framebuffer, a resize will be required,
4855 * because the framebuffer was/will be changed.
4856 */
4857 bool fResize = pFBInfo->fDisabled || pFBInfo->pFramebuffer.isNull();
4858
4859 if (pFBInfo->fVBVAForceResize)
4860 {
4861 /* VBVA was just enabled. Do the resize. */
4862 fResize = true;
4863 pFBInfo->fVBVAForceResize = false;
4864 }
4865
4866 /* Check if this is a real resize or a notification about the screen origin.
4867 * The guest uses this VBVAResize call for both.
4868 */
4869 fResize = fResize
4870 || pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
4871 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
4872 || pFBInfo->u32LineSize != pScreen->u32LineSize
4873 || pFBInfo->w != pScreen->u32Width
4874 || pFBInfo->h != pScreen->u32Height;
4875
4876 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
4877 || pFBInfo->yOrigin != pScreen->i32OriginY;
4878
4879 if (fNewOrigin || fResize)
4880 pThis->notifyCroglResize(pView, pScreen, pvVRAM);
4881
4882 if (pFBInfo->fDisabled)
4883 {
4884 pFBInfo->fDisabled = false;
4885 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4886 GuestMonitorChangedEventType_Enabled,
4887 pScreen->u32ViewIndex,
4888 pScreen->i32OriginX, pScreen->i32OriginY,
4889 pScreen->u32Width, pScreen->u32Height);
4890 /* Continue to update pFBInfo. */
4891 }
4892
4893 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
4894 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
4895 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
4896
4897 pFBInfo->xOrigin = pScreen->i32OriginX;
4898 pFBInfo->yOrigin = pScreen->i32OriginY;
4899
4900 pFBInfo->w = pScreen->u32Width;
4901 pFBInfo->h = pScreen->u32Height;
4902
4903 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
4904 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
4905 pFBInfo->u32LineSize = pScreen->u32LineSize;
4906
4907 pFBInfo->flags = pScreen->u16Flags;
4908
4909 if (fNewOrigin)
4910 {
4911 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4912 GuestMonitorChangedEventType_NewOrigin,
4913 pScreen->u32ViewIndex,
4914 pScreen->i32OriginX, pScreen->i32OriginY,
4915 0, 0);
4916 }
4917
4918 if (!fResize)
4919 {
4920 /* No parameters of the framebuffer have actually changed. */
4921 if (fNewOrigin)
4922 {
4923 /* VRDP server still need this notification. */
4924 LogRelFlowFunc(("Calling VRDP\n"));
4925 pThis->mParent->consoleVRDPServer()->SendResize();
4926 }
4927 return VINF_SUCCESS;
4928 }
4929
4930 if (pFBInfo->pFramebuffer.isNull())
4931 {
4932 /* If no framebuffer, the resize will be done later when a new framebuffer will be set in changeFramebuffer. */
4933 return VINF_SUCCESS;
4934 }
4935
4936 /* If the framebuffer already set for the screen, do a regular resize. */
4937 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
4938 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
4939 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
4940}
4941
4942DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
4943 uint32_t xHot, uint32_t yHot,
4944 uint32_t cx, uint32_t cy,
4945 const void *pvShape)
4946{
4947 LogFlowFunc(("\n"));
4948
4949 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4950 Display *pThis = pDrv->pDisplay;
4951
4952 size_t cbShapeSize = 0;
4953
4954 if (pvShape)
4955 {
4956 cbShapeSize = (cx + 7) / 8 * cy; /* size of the AND mask */
4957 cbShapeSize = ((cbShapeSize + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
4958 }
4959 com::SafeArray<BYTE> shapeData(cbShapeSize);
4960
4961 if (pvShape)
4962 ::memcpy(shapeData.raw(), pvShape, cbShapeSize);
4963
4964 /* Tell the console about it */
4965 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
4966 xHot, yHot, cx, cy, ComSafeArrayAsInParam(shapeData));
4967
4968 return VINF_SUCCESS;
4969}
4970#endif /* VBOX_WITH_HGSMI */
4971
4972/**
4973 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4974 */
4975DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
4976{
4977 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
4978 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4979 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
4980 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
4981 return NULL;
4982}
4983
4984
4985/**
4986 * Destruct a display driver instance.
4987 *
4988 * @returns VBox status.
4989 * @param pDrvIns The driver instance data.
4990 */
4991DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
4992{
4993 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4994 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4995 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4996
4997 if (pThis->pDisplay)
4998 {
4999 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
5000#ifdef VBOX_WITH_VPX
5001 pThis->pDisplay->VideoCaptureStop();
5002#endif
5003#ifdef VBOX_WITH_CRHGSMI
5004 pThis->pDisplay->destructCrHgsmiData();
5005#endif
5006 pThis->pDisplay->mpDrv = NULL;
5007 pThis->pDisplay->mpVMMDev = NULL;
5008 pThis->pDisplay->mLastAddress = NULL;
5009 pThis->pDisplay->mLastBytesPerLine = 0;
5010 pThis->pDisplay->mLastBitsPerPixel = 0,
5011 pThis->pDisplay->mLastWidth = 0;
5012 pThis->pDisplay->mLastHeight = 0;
5013 }
5014}
5015
5016
5017/**
5018 * Construct a display driver instance.
5019 *
5020 * @copydoc FNPDMDRVCONSTRUCT
5021 */
5022DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
5023{
5024 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
5025 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
5026 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
5027
5028 /*
5029 * Validate configuration.
5030 */
5031 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
5032 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
5033 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
5034 ("Configuration error: Not possible to attach anything to this driver!\n"),
5035 VERR_PDM_DRVINS_NO_ATTACH);
5036
5037 /*
5038 * Init Interfaces.
5039 */
5040 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
5041
5042 pThis->IConnector.pfnResize = Display::displayResizeCallback;
5043 pThis->IConnector.pfnUpdateRect = Display::displayUpdateCallback;
5044 pThis->IConnector.pfnRefresh = Display::displayRefreshCallback;
5045 pThis->IConnector.pfnReset = Display::displayResetCallback;
5046 pThis->IConnector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
5047 pThis->IConnector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
5048 pThis->IConnector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
5049#ifdef VBOX_WITH_VIDEOHWACCEL
5050 pThis->IConnector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
5051#endif
5052#ifdef VBOX_WITH_CRHGSMI
5053 pThis->IConnector.pfnCrHgsmiCommandProcess = Display::displayCrHgsmiCommandProcess;
5054 pThis->IConnector.pfnCrHgsmiControlProcess = Display::displayCrHgsmiControlProcess;
5055#endif
5056 pThis->IConnector.pfnCrHgcmCtlSubmit = Display::displayCrHgcmCtlSubmit;
5057#ifdef VBOX_WITH_HGSMI
5058 pThis->IConnector.pfnVBVAEnable = Display::displayVBVAEnable;
5059 pThis->IConnector.pfnVBVADisable = Display::displayVBVADisable;
5060 pThis->IConnector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
5061 pThis->IConnector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
5062 pThis->IConnector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
5063 pThis->IConnector.pfnVBVAResize = Display::displayVBVAResize;
5064 pThis->IConnector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
5065#endif
5066
5067 /*
5068 * Get the IDisplayPort interface of the above driver/device.
5069 */
5070 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
5071 if (!pThis->pUpPort)
5072 {
5073 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
5074 return VERR_PDM_MISSING_INTERFACE_ABOVE;
5075 }
5076#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
5077 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
5078 if (!pThis->pVBVACallbacks)
5079 {
5080 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
5081 return VERR_PDM_MISSING_INTERFACE_ABOVE;
5082 }
5083#endif
5084 /*
5085 * Get the Display object pointer and update the mpDrv member.
5086 */
5087 void *pv;
5088 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
5089 if (RT_FAILURE(rc))
5090 {
5091 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
5092 return rc;
5093 }
5094 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
5095 pThis->pDisplay = pDisplay;
5096 pThis->pDisplay->mpDrv = pThis;
5097 /*
5098 * Update our display information according to the framebuffer
5099 */
5100 pDisplay->updateDisplayData();
5101
5102 /*
5103 * Start periodic screen refreshes
5104 */
5105 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
5106
5107#ifdef VBOX_WITH_CRHGSMI
5108 pDisplay->setupCrHgsmiData();
5109#endif
5110
5111#ifdef VBOX_WITH_VPX
5112 ComPtr<IMachine> pMachine = pDisplay->mParent->machine();
5113 BOOL fEnabled = false;
5114 HRESULT hrc = pMachine->COMGETTER(VideoCaptureEnabled)(&fEnabled);
5115 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
5116 if (fEnabled)
5117 {
5118 rc = pDisplay->VideoCaptureStart();
5119 fireVideoCaptureChangedEvent(pDisplay->mParent->getEventSource());
5120 }
5121#endif
5122
5123 return rc;
5124}
5125
5126
5127/**
5128 * Display driver registration record.
5129 */
5130const PDMDRVREG Display::DrvReg =
5131{
5132 /* u32Version */
5133 PDM_DRVREG_VERSION,
5134 /* szName */
5135 "MainDisplay",
5136 /* szRCMod */
5137 "",
5138 /* szR0Mod */
5139 "",
5140 /* pszDescription */
5141 "Main display driver (Main as in the API).",
5142 /* fFlags */
5143 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
5144 /* fClass. */
5145 PDM_DRVREG_CLASS_DISPLAY,
5146 /* cMaxInstances */
5147 ~0U,
5148 /* cbInstance */
5149 sizeof(DRVMAINDISPLAY),
5150 /* pfnConstruct */
5151 Display::drvConstruct,
5152 /* pfnDestruct */
5153 Display::drvDestruct,
5154 /* pfnRelocate */
5155 NULL,
5156 /* pfnIOCtl */
5157 NULL,
5158 /* pfnPowerOn */
5159 NULL,
5160 /* pfnReset */
5161 NULL,
5162 /* pfnSuspend */
5163 NULL,
5164 /* pfnResume */
5165 NULL,
5166 /* pfnAttach */
5167 NULL,
5168 /* pfnDetach */
5169 NULL,
5170 /* pfnPowerOff */
5171 NULL,
5172 /* pfnSoftReset */
5173 NULL,
5174 /* u32EndVersion */
5175 PDM_DRVREG_VERSION
5176};
5177/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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