VirtualBox

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

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

main: async call crogl when needed

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