VirtualBox

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

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

crOpenGL: crcmd enhancements & fixes; osx deadlock fix; temporary disabled crcmd for testing

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

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