VirtualBox

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

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

Main/crOpenGL/DevVGA: synchronization, bugfixes, cleanup

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

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