VirtualBox

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

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

DisplayImpl: should work without attached framebuffer.

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