VirtualBox

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

Last change on this file since 57119 was 57088, checked in by vboxsync, 10 years ago

DisplayImpl.cpp: Free the screen shot data in the correct way. pu8Data -> pbData and other cleanups.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 149.3 KB
Line 
1/* $Id: DisplayImpl.cpp 57088 2015-07-26 23:36:29Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "DisplayImpl.h"
19#include "DisplayUtils.h"
20#include "ConsoleImpl.h"
21#include "ConsoleVRDPServer.h"
22#include "GuestImpl.h"
23#include "VMMDev.h"
24
25#include "AutoCaller.h"
26#include "Logging.h"
27
28/* generated header */
29#include "VBoxEvents.h"
30
31#include <iprt/semaphore.h>
32#include <iprt/thread.h>
33#include <iprt/asm.h>
34#include <iprt/time.h>
35#include <iprt/cpp/utils.h>
36#include <iprt/alloca.h>
37
38#include <VBox/vmm/pdmdrv.h>
39#if defined(DEBUG) || defined(VBOX_STRICT) /* for VM_ASSERT_EMT(). */
40# include <VBox/vmm/vm.h>
41#endif
42
43#ifdef VBOX_WITH_VIDEOHWACCEL
44# include <VBox/VBoxVideo.h>
45#endif
46
47#if defined(VBOX_WITH_CROGL) || defined(VBOX_WITH_CRHGSMI)
48# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
49#endif
50
51#include <VBox/com/array.h>
52
53#ifdef VBOX_WITH_VPX
54# include <iprt/path.h>
55# include "VideoRec.h"
56#endif
57
58#ifdef VBOX_WITH_CROGL
59typedef enum
60{
61 CRVREC_STATE_IDLE,
62 CRVREC_STATE_SUBMITTED
63} CRVREC_STATE;
64#endif
65
66/**
67 * Display driver instance data.
68 *
69 * @implements PDMIDISPLAYCONNECTOR
70 */
71typedef struct DRVMAINDISPLAY
72{
73 /** Pointer to the display object. */
74 Display *pDisplay;
75 /** Pointer to the driver instance structure. */
76 PPDMDRVINS pDrvIns;
77 /** Pointer to the keyboard port interface of the driver/device above us. */
78 PPDMIDISPLAYPORT pUpPort;
79 /** Our display connector interface. */
80 PDMIDISPLAYCONNECTOR IConnector;
81#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
82 /** VBVA callbacks */
83 PPDMIDISPLAYVBVACALLBACKS pVBVACallbacks;
84#endif
85} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
86
87/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
88#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) RT_FROM_MEMBER(pInterface, DRVMAINDISPLAY, IConnector)
89
90// constructor / destructor
91/////////////////////////////////////////////////////////////////////////////
92
93Display::Display()
94 : mParent(NULL), mfIsCr3DEnabled(false)
95{
96}
97
98Display::~Display()
99{
100}
101
102
103HRESULT Display::FinalConstruct()
104{
105 int rc = videoAccelConstruct(&mVideoAccelLegacy);
106 AssertRC(rc);
107
108 mfVideoAccelVRDP = false;
109 mfu32SupportedOrders = 0;
110 mcVideoAccelVRDPRefs = 0;
111
112 mfSeamlessEnabled = false;
113 mpRectVisibleRegion = NULL;
114
115#ifdef VBOX_WITH_CROGL
116 mfCrOglDataHidden = false;
117#endif
118
119 mpDrv = NULL;
120 mpVMMDev = NULL;
121 mfVMMDevInited = false;
122
123 rc = RTCritSectInit(&mVideoAccelLock);
124 AssertRC(rc);
125
126#ifdef VBOX_WITH_HGSMI
127 mu32UpdateVBVAFlags = 0;
128 mfVMMDevSupportsGraphics = false;
129 mfGuestVBVACapabilities = 0;
130 mfHostCursorCapabilities = 0;
131#endif
132#ifdef VBOX_WITH_VPX
133 mpVideoRecCtx = NULL;
134 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
135 maVideoRecEnabled[i] = true;
136#endif
137
138#ifdef VBOX_WITH_CRHGSMI
139 mhCrOglSvc = NULL;
140 rc = RTCritSectRwInit(&mCrOglLock);
141 AssertRC(rc);
142#endif
143#ifdef VBOX_WITH_CROGL
144 RT_ZERO(mCrOglCallbacks);
145 RT_ZERO(mCrOglScreenshotData);
146 mfCrOglVideoRecState = CRVREC_STATE_IDLE;
147 mCrOglScreenshotData.u32Screen = CRSCREEN_ALL;
148 mCrOglScreenshotData.pvContext = this;
149 mCrOglScreenshotData.pfnScreenshotBegin = i_displayCrVRecScreenshotBegin;
150 mCrOglScreenshotData.pfnScreenshotPerform = i_displayCrVRecScreenshotPerform;
151 mCrOglScreenshotData.pfnScreenshotEnd = i_displayCrVRecScreenshotEnd;
152#endif
153
154 return BaseFinalConstruct();
155}
156
157void Display::FinalRelease()
158{
159 uninit();
160
161 videoAccelDestroy(&mVideoAccelLegacy);
162 i_saveVisibleRegion(0, NULL);
163
164 if (RTCritSectIsInitialized(&mVideoAccelLock))
165 {
166 RTCritSectDelete(&mVideoAccelLock);
167 RT_ZERO(mVideoAccelLock);
168 }
169
170#ifdef VBOX_WITH_CRHGSMI
171 if (RTCritSectRwIsInitialized (&mCrOglLock))
172 {
173 RTCritSectRwDelete (&mCrOglLock);
174 RT_ZERO(mCrOglLock);
175 }
176#endif
177 BaseFinalRelease();
178}
179
180// public initializer/uninitializer for internal purposes only
181/////////////////////////////////////////////////////////////////////////////
182
183#define kMaxSizeThumbnail 64
184
185/**
186 * Save thumbnail and screenshot of the guest screen.
187 */
188static int displayMakeThumbnail(uint8_t *pbData, uint32_t cx, uint32_t cy,
189 uint8_t **ppu8Thumbnail, uint32_t *pcbThumbnail, uint32_t *pcxThumbnail, uint32_t *pcyThumbnail)
190{
191 int rc = VINF_SUCCESS;
192
193 uint8_t *pu8Thumbnail = NULL;
194 uint32_t cbThumbnail = 0;
195 uint32_t cxThumbnail = 0;
196 uint32_t cyThumbnail = 0;
197
198 if (cx > cy)
199 {
200 cxThumbnail = kMaxSizeThumbnail;
201 cyThumbnail = (kMaxSizeThumbnail * cy) / cx;
202 }
203 else
204 {
205 cyThumbnail = kMaxSizeThumbnail;
206 cxThumbnail = (kMaxSizeThumbnail * cx) / cy;
207 }
208
209 LogRelFlowFunc(("%dx%d -> %dx%d\n", cx, cy, cxThumbnail, cyThumbnail));
210
211 cbThumbnail = cxThumbnail * 4 * cyThumbnail;
212 pu8Thumbnail = (uint8_t *)RTMemAlloc(cbThumbnail);
213
214 if (pu8Thumbnail)
215 {
216 uint8_t *dst = pu8Thumbnail;
217 uint8_t *src = pbData;
218 int dstW = cxThumbnail;
219 int dstH = cyThumbnail;
220 int srcW = cx;
221 int srcH = cy;
222 int iDeltaLine = cx * 4;
223
224 BitmapScale32(dst,
225 dstW, dstH,
226 src,
227 iDeltaLine,
228 srcW, srcH);
229
230 *ppu8Thumbnail = pu8Thumbnail;
231 *pcbThumbnail = cbThumbnail;
232 *pcxThumbnail = cxThumbnail;
233 *pcyThumbnail = cyThumbnail;
234 }
235 else
236 {
237 rc = VERR_NO_MEMORY;
238 }
239
240 return rc;
241}
242
243#ifdef VBOX_WITH_CROGL
244typedef struct
245{
246 CRVBOXHGCMTAKESCREENSHOT Base;
247
248 /* 32bpp small RGB image. */
249 uint8_t *pu8Thumbnail;
250 uint32_t cbThumbnail;
251 uint32_t cxThumbnail;
252 uint32_t cyThumbnail;
253
254 /* PNG screenshot. */
255 uint8_t *pu8PNG;
256 uint32_t cbPNG;
257 uint32_t cxPNG;
258 uint32_t cyPNG;
259} VBOX_DISPLAY_SAVESCREENSHOT_DATA;
260
261static DECLCALLBACK(void) displaySaveScreenshotReport(void *pvCtx, uint32_t uScreen,
262 uint32_t x, uint32_t y, uint32_t uBitsPerPixel,
263 uint32_t uBytesPerLine, uint32_t uGuestWidth, uint32_t uGuestHeight,
264 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
265{
266 VBOX_DISPLAY_SAVESCREENSHOT_DATA *pData = (VBOX_DISPLAY_SAVESCREENSHOT_DATA*)pvCtx;
267 displayMakeThumbnail(pu8BufferAddress, uGuestWidth, uGuestHeight, &pData->pu8Thumbnail,
268 &pData->cbThumbnail, &pData->cxThumbnail, &pData->cyThumbnail);
269 int rc = DisplayMakePNG(pu8BufferAddress, uGuestWidth, uGuestHeight, &pData->pu8PNG,
270 &pData->cbPNG, &pData->cxPNG, &pData->cyPNG, 1);
271 if (RT_FAILURE(rc))
272 {
273 AssertMsgFailed(("DisplayMakePNG failed (rc=%Rrc)\n", rc));
274 if (pData->pu8PNG)
275 {
276 RTMemFree(pData->pu8PNG);
277 pData->pu8PNG = NULL;
278 }
279 pData->cbPNG = 0;
280 pData->cxPNG = 0;
281 pData->cyPNG = 0;
282 }
283}
284#endif
285
286DECLCALLBACK(void) Display::i_displaySSMSaveScreenshot(PSSMHANDLE pSSM, void *pvUser)
287{
288 Display *that = static_cast<Display*>(pvUser);
289
290 /* 32bpp small RGB image. */
291 uint8_t *pu8Thumbnail = NULL;
292 uint32_t cbThumbnail = 0;
293 uint32_t cxThumbnail = 0;
294 uint32_t cyThumbnail = 0;
295
296 /* PNG screenshot. */
297 uint8_t *pu8PNG = NULL;
298 uint32_t cbPNG = 0;
299 uint32_t cxPNG = 0;
300 uint32_t cyPNG = 0;
301
302 Console::SafeVMPtr ptrVM(that->mParent);
303 if (ptrVM.isOk())
304 {
305#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
306 BOOL f3DSnapshot = FALSE;
307 if ( that->mfIsCr3DEnabled
308 && that->mCrOglCallbacks.pfnHasData
309 && that->mCrOglCallbacks.pfnHasData())
310 {
311 VMMDev *pVMMDev = that->mParent->i_getVMMDev();
312 if (pVMMDev)
313 {
314 VBOX_DISPLAY_SAVESCREENSHOT_DATA *pScreenshot;
315 pScreenshot = (VBOX_DISPLAY_SAVESCREENSHOT_DATA*)RTMemAllocZ(sizeof(*pScreenshot));
316 if (pScreenshot)
317 {
318 /* screen id or CRSCREEN_ALL to specify all enabled */
319 pScreenshot->Base.u32Screen = 0;
320 pScreenshot->Base.u32Width = 0;
321 pScreenshot->Base.u32Height = 0;
322 pScreenshot->Base.u32Pitch = 0;
323 pScreenshot->Base.pvBuffer = NULL;
324 pScreenshot->Base.pvContext = pScreenshot;
325 pScreenshot->Base.pfnScreenshotBegin = NULL;
326 pScreenshot->Base.pfnScreenshotPerform = displaySaveScreenshotReport;
327 pScreenshot->Base.pfnScreenshotEnd = NULL;
328
329 VBOXCRCMDCTL_HGCM data;
330 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
331 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
332
333 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
334 data.aParms[0].u.pointer.addr = &pScreenshot->Base;
335 data.aParms[0].u.pointer.size = sizeof(pScreenshot->Base);
336
337 int rc = that->i_crCtlSubmitSync(&data.Hdr, sizeof(data));
338 if (RT_SUCCESS(rc))
339 {
340 if (pScreenshot->pu8PNG)
341 {
342 pu8Thumbnail = pScreenshot->pu8Thumbnail;
343 cbThumbnail = pScreenshot->cbThumbnail;
344 cxThumbnail = pScreenshot->cxThumbnail;
345 cyThumbnail = pScreenshot->cyThumbnail;
346
347 /* PNG screenshot. */
348 pu8PNG = pScreenshot->pu8PNG;
349 cbPNG = pScreenshot->cbPNG;
350 cxPNG = pScreenshot->cxPNG;
351 cyPNG = pScreenshot->cyPNG;
352 f3DSnapshot = TRUE;
353 }
354 else
355 AssertMsgFailed(("no png\n"));
356 }
357 else
358 AssertMsgFailed(("SHCRGL_HOST_FN_TAKE_SCREENSHOT failed (rc=%Rrc)\n", rc));
359
360
361 RTMemFree(pScreenshot);
362 }
363 }
364 }
365
366 if (!f3DSnapshot)
367#endif
368 {
369 /* Query RGB bitmap. */
370 /* SSM code is executed on EMT(0), therefore no need to use VMR3ReqCallWait. */
371 uint8_t *pbData = NULL;
372 size_t cbData = 0;
373 uint32_t cx = 0;
374 uint32_t cy = 0;
375 bool fFreeMem = false;
376 int rc = Display::i_displayTakeScreenshotEMT(that, VBOX_VIDEO_PRIMARY_SCREEN, &pbData, &cbData, &cx, &cy, &fFreeMem);
377
378 /*
379 * It is possible that success is returned but everything is 0 or NULL.
380 * (no display attached if a VM is running with VBoxHeadless on OSE for example)
381 */
382 if (RT_SUCCESS(rc) && pbData)
383 {
384 Assert(cx && cy);
385
386 /* Prepare a small thumbnail and a PNG screenshot. */
387 displayMakeThumbnail(pbData, cx, cy, &pu8Thumbnail, &cbThumbnail, &cxThumbnail, &cyThumbnail);
388 rc = DisplayMakePNG(pbData, cx, cy, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 1);
389 if (RT_FAILURE(rc))
390 {
391 if (pu8PNG)
392 {
393 RTMemFree(pu8PNG);
394 pu8PNG = NULL;
395 }
396 cbPNG = 0;
397 cxPNG = 0;
398 cyPNG = 0;
399 }
400
401 if (fFreeMem)
402 RTMemFree(pbData);
403 else
404 that->mpDrv->pUpPort->pfnFreeScreenshot(that->mpDrv->pUpPort, pbData);
405 }
406 }
407 }
408 else
409 {
410 LogFunc(("Failed to get VM pointer 0x%x\n", ptrVM.rc()));
411 }
412
413 /* Regardless of rc, save what is available:
414 * Data format:
415 * uint32_t cBlocks;
416 * [blocks]
417 *
418 * Each block is:
419 * uint32_t cbBlock; if 0 - no 'block data'.
420 * uint32_t typeOfBlock; 0 - 32bpp RGB bitmap, 1 - PNG, ignored if 'cbBlock' is 0.
421 * [block data]
422 *
423 * Block data for bitmap and PNG:
424 * uint32_t cx;
425 * uint32_t cy;
426 * [image data]
427 */
428 SSMR3PutU32(pSSM, 2); /* Write thumbnail and PNG screenshot. */
429
430 /* First block. */
431 SSMR3PutU32(pSSM, cbThumbnail + 2 * sizeof(uint32_t));
432 SSMR3PutU32(pSSM, 0); /* Block type: thumbnail. */
433
434 if (cbThumbnail)
435 {
436 SSMR3PutU32(pSSM, cxThumbnail);
437 SSMR3PutU32(pSSM, cyThumbnail);
438 SSMR3PutMem(pSSM, pu8Thumbnail, cbThumbnail);
439 }
440
441 /* Second block. */
442 SSMR3PutU32(pSSM, cbPNG + 2 * sizeof(uint32_t));
443 SSMR3PutU32(pSSM, 1); /* Block type: png. */
444
445 if (cbPNG)
446 {
447 SSMR3PutU32(pSSM, cxPNG);
448 SSMR3PutU32(pSSM, cyPNG);
449 SSMR3PutMem(pSSM, pu8PNG, cbPNG);
450 }
451
452 RTMemFree(pu8PNG);
453 RTMemFree(pu8Thumbnail);
454}
455
456DECLCALLBACK(int)
457Display::i_displaySSMLoadScreenshot(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
458{
459 Display *that = static_cast<Display*>(pvUser);
460
461 if (uVersion != sSSMDisplayScreenshotVer)
462 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
463 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
464
465 /* Skip data. */
466 uint32_t cBlocks;
467 int rc = SSMR3GetU32(pSSM, &cBlocks);
468 AssertRCReturn(rc, rc);
469
470 for (uint32_t i = 0; i < cBlocks; i++)
471 {
472 uint32_t cbBlock;
473 rc = SSMR3GetU32(pSSM, &cbBlock);
474 AssertRCBreak(rc);
475
476 uint32_t typeOfBlock;
477 rc = SSMR3GetU32(pSSM, &typeOfBlock);
478 AssertRCBreak(rc);
479
480 LogRelFlowFunc(("[%d] type %d, size %d bytes\n", i, typeOfBlock, cbBlock));
481
482 /* Note: displaySSMSaveScreenshot writes size of a block = 8 and
483 * do not write any data if the image size was 0.
484 * @todo Fix and increase saved state version.
485 */
486 if (cbBlock > 2 * sizeof(uint32_t))
487 {
488 rc = SSMR3Skip(pSSM, cbBlock);
489 AssertRCBreak(rc);
490 }
491 }
492
493 return rc;
494}
495
496/**
497 * Save/Load some important guest state
498 */
499DECLCALLBACK(void)
500Display::i_displaySSMSave(PSSMHANDLE pSSM, void *pvUser)
501{
502 Display *that = static_cast<Display*>(pvUser);
503
504 SSMR3PutU32(pSSM, that->mcMonitors);
505 for (unsigned i = 0; i < that->mcMonitors; i++)
506 {
507 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32Offset);
508 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32MaxFramebufferSize);
509 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32InformationSize);
510 SSMR3PutU32(pSSM, that->maFramebuffers[i].w);
511 SSMR3PutU32(pSSM, that->maFramebuffers[i].h);
512 SSMR3PutS32(pSSM, that->maFramebuffers[i].xOrigin);
513 SSMR3PutS32(pSSM, that->maFramebuffers[i].yOrigin);
514 SSMR3PutU32(pSSM, that->maFramebuffers[i].flags);
515 }
516 SSMR3PutS32(pSSM, that->xInputMappingOrigin);
517 SSMR3PutS32(pSSM, that->yInputMappingOrigin);
518 SSMR3PutU32(pSSM, that->cxInputMapping);
519 SSMR3PutU32(pSSM, that->cyInputMapping);
520 SSMR3PutU32(pSSM, that->mfGuestVBVACapabilities);
521 SSMR3PutU32(pSSM, that->mfHostCursorCapabilities);
522}
523
524DECLCALLBACK(int)
525Display::i_displaySSMLoad(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
526{
527 Display *that = static_cast<Display*>(pvUser);
528
529 if ( uVersion != sSSMDisplayVer
530 && uVersion != sSSMDisplayVer2
531 && uVersion != sSSMDisplayVer3
532 && uVersion != sSSMDisplayVer4
533 && uVersion != sSSMDisplayVer5)
534 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
535 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
536
537 uint32_t cMonitors;
538 int rc = SSMR3GetU32(pSSM, &cMonitors);
539 if (cMonitors != that->mcMonitors)
540 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Number of monitors changed (%d->%d)!"), cMonitors, that->mcMonitors);
541
542 for (uint32_t i = 0; i < cMonitors; i++)
543 {
544 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32Offset);
545 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32MaxFramebufferSize);
546 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32InformationSize);
547 if ( uVersion == sSSMDisplayVer2
548 || uVersion == sSSMDisplayVer3
549 || uVersion == sSSMDisplayVer4
550 || uVersion == sSSMDisplayVer5)
551 {
552 uint32_t w;
553 uint32_t h;
554 SSMR3GetU32(pSSM, &w);
555 SSMR3GetU32(pSSM, &h);
556 that->maFramebuffers[i].w = w;
557 that->maFramebuffers[i].h = h;
558 }
559 if ( uVersion == sSSMDisplayVer3
560 || uVersion == sSSMDisplayVer4
561 || uVersion == sSSMDisplayVer5)
562 {
563 int32_t xOrigin;
564 int32_t yOrigin;
565 uint32_t flags;
566 SSMR3GetS32(pSSM, &xOrigin);
567 SSMR3GetS32(pSSM, &yOrigin);
568 SSMR3GetU32(pSSM, &flags);
569 that->maFramebuffers[i].xOrigin = xOrigin;
570 that->maFramebuffers[i].yOrigin = yOrigin;
571 that->maFramebuffers[i].flags = (uint16_t)flags;
572 that->maFramebuffers[i].fDisabled = (that->maFramebuffers[i].flags & VBVA_SCREEN_F_DISABLED) != 0;
573 }
574 }
575 if ( uVersion == sSSMDisplayVer4
576 || uVersion == sSSMDisplayVer5)
577 {
578 SSMR3GetS32(pSSM, &that->xInputMappingOrigin);
579 SSMR3GetS32(pSSM, &that->yInputMappingOrigin);
580 SSMR3GetU32(pSSM, &that->cxInputMapping);
581 SSMR3GetU32(pSSM, &that->cyInputMapping);
582 }
583 if (uVersion == sSSMDisplayVer5)
584 {
585 SSMR3GetU32(pSSM, &that->mfGuestVBVACapabilities);
586 SSMR3GetU32(pSSM, &that->mfHostCursorCapabilities);
587 }
588
589 return VINF_SUCCESS;
590}
591
592/**
593 * Initializes the display object.
594 *
595 * @returns COM result indicator
596 * @param parent handle of our parent object
597 * @param qemuConsoleData address of common console data structure
598 */
599HRESULT Display::init(Console *aParent)
600{
601 ComAssertRet(aParent, E_INVALIDARG);
602 /* Enclose the state transition NotReady->InInit->Ready */
603 AutoInitSpan autoInitSpan(this);
604 AssertReturn(autoInitSpan.isOk(), E_FAIL);
605
606 unconst(mParent) = aParent;
607
608 mfSourceBitmapEnabled = true;
609 fVGAResizing = false;
610
611 ULONG ul;
612 mParent->i_machine()->COMGETTER(MonitorCount)(&ul);
613 mcMonitors = ul;
614 xInputMappingOrigin = 0;
615 yInputMappingOrigin = 0;
616 cxInputMapping = 0;
617 cyInputMapping = 0;
618
619 for (ul = 0; ul < mcMonitors; ul++)
620 {
621 maFramebuffers[ul].u32Offset = 0;
622 maFramebuffers[ul].u32MaxFramebufferSize = 0;
623 maFramebuffers[ul].u32InformationSize = 0;
624
625 maFramebuffers[ul].pFramebuffer = NULL;
626 /* All secondary monitors are disabled at startup. */
627 maFramebuffers[ul].fDisabled = ul > 0;
628
629 maFramebuffers[ul].u32Caps = 0;
630
631 maFramebuffers[ul].updateImage.pu8Address = NULL;
632 maFramebuffers[ul].updateImage.cbLine = 0;
633
634 maFramebuffers[ul].xOrigin = 0;
635 maFramebuffers[ul].yOrigin = 0;
636
637 maFramebuffers[ul].w = 0;
638 maFramebuffers[ul].h = 0;
639
640 maFramebuffers[ul].flags = maFramebuffers[ul].fDisabled? VBVA_SCREEN_F_DISABLED: 0;
641
642 maFramebuffers[ul].u16BitsPerPixel = 0;
643 maFramebuffers[ul].pu8FramebufferVRAM = NULL;
644 maFramebuffers[ul].u32LineSize = 0;
645
646 maFramebuffers[ul].pHostEvents = NULL;
647
648 maFramebuffers[ul].fDefaultFormat = false;
649
650#ifdef VBOX_WITH_HGSMI
651 maFramebuffers[ul].fVBVAEnabled = false;
652 maFramebuffers[ul].fVBVAForceResize = false;
653 maFramebuffers[ul].fRenderThreadMode = false;
654 maFramebuffers[ul].pVBVAHostFlags = NULL;
655#endif /* VBOX_WITH_HGSMI */
656#ifdef VBOX_WITH_CROGL
657 RT_ZERO(maFramebuffers[ul].pendingViewportInfo);
658#endif
659 }
660
661 {
662 // register listener for state change events
663 ComPtr<IEventSource> es;
664 mParent->COMGETTER(EventSource)(es.asOutParam());
665 com::SafeArray<VBoxEventType_T> eventTypes;
666 eventTypes.push_back(VBoxEventType_OnStateChanged);
667 es->RegisterListener(this, ComSafeArrayAsInParam(eventTypes), true);
668 }
669
670 /* Cache the 3D settings. */
671 BOOL fIs3DEnabled = FALSE;
672 mParent->i_machine()->COMGETTER(Accelerate3DEnabled)(&fIs3DEnabled);
673 GraphicsControllerType_T enmGpuType = (GraphicsControllerType_T)GraphicsControllerType_VBoxVGA;
674 mParent->i_machine()->COMGETTER(GraphicsControllerType)(&enmGpuType);
675 mfIsCr3DEnabled = fIs3DEnabled && enmGpuType == GraphicsControllerType_VBoxVGA;
676
677 /* Confirm a successful initialization */
678 autoInitSpan.setSucceeded();
679
680 return S_OK;
681}
682
683/**
684 * Uninitializes the instance and sets the ready flag to FALSE.
685 * Called either from FinalRelease() or by the parent when it gets destroyed.
686 */
687void Display::uninit()
688{
689 LogRelFlowFunc(("this=%p\n", this));
690
691 /* Enclose the state transition Ready->InUninit->NotReady */
692 AutoUninitSpan autoUninitSpan(this);
693 if (autoUninitSpan.uninitDone())
694 return;
695
696 unsigned uScreenId;
697 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
698 {
699 maFramebuffers[uScreenId].pSourceBitmap.setNull();
700 maFramebuffers[uScreenId].updateImage.pSourceBitmap.setNull();
701 maFramebuffers[uScreenId].updateImage.pu8Address = NULL;
702 maFramebuffers[uScreenId].updateImage.cbLine = 0;
703 maFramebuffers[uScreenId].pFramebuffer.setNull();
704 }
705
706 if (mParent)
707 {
708 ComPtr<IEventSource> es;
709 mParent->COMGETTER(EventSource)(es.asOutParam());
710 es->UnregisterListener(this);
711 }
712
713 unconst(mParent) = NULL;
714
715 if (mpDrv)
716 mpDrv->pDisplay = NULL;
717
718 mpDrv = NULL;
719 mpVMMDev = NULL;
720 mfVMMDevInited = true;
721}
722
723/**
724 * Register the SSM methods. Called by the power up thread to be able to
725 * pass pVM
726 */
727int Display::i_registerSSM(PUVM pUVM)
728{
729 /* Version 2 adds width and height of the framebuffer; version 3 adds
730 * the framebuffer offset in the virtual desktop and the framebuffer flags;
731 * version 4 adds guest to host input event mapping and version 5 adds
732 * guest VBVA and host cursor capabilities.
733 */
734 int rc = SSMR3RegisterExternal(pUVM, "DisplayData", 0, sSSMDisplayVer5,
735 mcMonitors * sizeof(uint32_t) * 8 + sizeof(uint32_t),
736 NULL, NULL, NULL,
737 NULL, i_displaySSMSave, NULL,
738 NULL, i_displaySSMLoad, NULL, this);
739 AssertRCReturn(rc, rc);
740
741 /*
742 * Register loaders for old saved states where iInstance was
743 * 3 * sizeof(uint32_t *) due to a code mistake.
744 */
745 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 12 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
746 NULL, NULL, NULL,
747 NULL, NULL, NULL,
748 NULL, i_displaySSMLoad, NULL, this);
749 AssertRCReturn(rc, rc);
750
751 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 24 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
752 NULL, NULL, NULL,
753 NULL, NULL, NULL,
754 NULL, i_displaySSMLoad, NULL, this);
755 AssertRCReturn(rc, rc);
756
757 /* uInstance is an arbitrary value greater than 1024. Such a value will ensure a quick seek in saved state file. */
758 rc = SSMR3RegisterExternal(pUVM, "DisplayScreenshot", 1100 /*uInstance*/, sSSMDisplayScreenshotVer, 0 /*cbGuess*/,
759 NULL, NULL, NULL,
760 NULL, i_displaySSMSaveScreenshot, NULL,
761 NULL, i_displaySSMLoadScreenshot, NULL, this);
762
763 AssertRCReturn(rc, rc);
764
765 return VINF_SUCCESS;
766}
767
768DECLCALLBACK(void) Display::i_displayCrCmdFree(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, int rc, void *pvCompletion)
769{
770 Assert(pvCompletion);
771 RTMemFree(pvCompletion);
772}
773
774#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
775int Display::i_crOglWindowsShow(bool fShow)
776{
777 if (!mfCrOglDataHidden == !!fShow)
778 return VINF_SUCCESS;
779
780 if (!mhCrOglSvc)
781 {
782 /* No 3D or the VMSVGA3d kind. */
783 Assert(!mfIsCr3DEnabled);
784 return VERR_INVALID_STATE;
785 }
786
787 VMMDev *pVMMDev = mParent->i_getVMMDev();
788 if (!pVMMDev)
789 {
790 AssertMsgFailed(("no vmmdev\n"));
791 return VERR_INVALID_STATE;
792 }
793
794 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(VBOXCRCMDCTL_HGCM));
795 if (!pData)
796 {
797 AssertMsgFailed(("RTMemAlloc failed\n"));
798 return VERR_NO_MEMORY;
799 }
800
801 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
802 pData->Hdr.u32Function = SHCRGL_HOST_FN_WINDOWS_SHOW;
803
804 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
805 pData->aParms[0].u.uint32 = (uint32_t)fShow;
806
807 int rc = i_crCtlSubmit(&pData->Hdr, sizeof(*pData), i_displayCrCmdFree, pData);
808 if (RT_SUCCESS(rc))
809 mfCrOglDataHidden = !fShow;
810 else
811 {
812 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
813 RTMemFree(pData);
814 }
815
816 return rc;
817}
818#endif
819
820
821// public methods only for internal purposes
822/////////////////////////////////////////////////////////////////////////////
823
824int Display::i_notifyCroglResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
825{
826#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
827 if (maFramebuffers[pScreen->u32ViewIndex].fRenderThreadMode)
828 return VINF_SUCCESS; /* nop it */
829
830 if (mfIsCr3DEnabled)
831 {
832 int rc = VERR_INVALID_STATE;
833 if (mhCrOglSvc)
834 {
835 VMMDev *pVMMDev = mParent->i_getVMMDev();
836 if (pVMMDev)
837 {
838 VBOXCRCMDCTL_HGCM *pCtl;
839 pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(CRVBOXHGCMDEVRESIZE) + sizeof(VBOXCRCMDCTL_HGCM));
840 if (pCtl)
841 {
842 CRVBOXHGCMDEVRESIZE *pData = (CRVBOXHGCMDEVRESIZE*)(pCtl+1);
843 pData->Screen = *pScreen;
844 pData->pvVRAM = pvVRAM;
845
846 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
847 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_DEV_RESIZE;
848 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
849 pCtl->aParms[0].u.pointer.addr = pData;
850 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
851
852 rc = i_crCtlSubmit(&pCtl->Hdr, sizeof(*pCtl), i_displayCrCmdFree, pCtl);
853 if (RT_FAILURE(rc))
854 {
855 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
856 RTMemFree(pCtl);
857 }
858 }
859 else
860 rc = VERR_NO_MEMORY;
861 }
862 }
863
864 return rc;
865 }
866#endif /* #if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
867 return VINF_SUCCESS;
868}
869
870/**
871 * Handles display resize event.
872 *
873 * @param w New display width
874 * @param h New display height
875 *
876 * @thread EMT
877 */
878int Display::i_handleDisplayResize(unsigned uScreenId, uint32_t bpp, void *pvVRAM,
879 uint32_t cbLine, uint32_t w, uint32_t h, uint16_t flags)
880{
881 LogRel(("Display::handleDisplayResize: uScreenId=%d pvVRAM=%p w=%d h=%d bpp=%d cbLine=0x%X flags=0x%X\n", uScreenId,
882 pvVRAM, w, h, bpp, cbLine, flags));
883
884 if (uScreenId >= mcMonitors)
885 {
886 return VINF_SUCCESS;
887 }
888
889 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
890
891 /* Reset the update mode. */
892 pFBInfo->updateImage.pSourceBitmap.setNull();
893 pFBInfo->updateImage.pu8Address = NULL;
894 pFBInfo->updateImage.cbLine = 0;
895
896 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
897 {
898 pFBInfo->w = w;
899 pFBInfo->h = h;
900
901 pFBInfo->u16BitsPerPixel = (uint16_t)bpp;
902 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM;
903 pFBInfo->u32LineSize = cbLine;
904 pFBInfo->flags = flags;
905 }
906
907 /* Guest screen image will be invalid during resize, make sure that it is not updated. */
908 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
909 {
910 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, false);
911
912 mpDrv->IConnector.pbData = NULL;
913 mpDrv->IConnector.cbScanline = 0;
914 mpDrv->IConnector.cBits = 32; /* DevVGA does not work with cBits == 0. */
915 mpDrv->IConnector.cx = 0;
916 mpDrv->IConnector.cy = 0;
917 }
918
919 maFramebuffers[uScreenId].pSourceBitmap.setNull();
920
921 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
922 {
923 HRESULT hr = maFramebuffers[uScreenId].pFramebuffer->NotifyChange(uScreenId, 0, 0, w, h); /* @todo origin */
924 LogFunc(("NotifyChange hr %08X\n", hr));
925 NOREF(hr);
926 }
927
928 bool fUpdateImage = RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_UpdateImage);
929 if (fUpdateImage && !pFBInfo->pFramebuffer.isNull())
930 {
931 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
932 HRESULT hr = QuerySourceBitmap(uScreenId, pSourceBitmap.asOutParam());
933 if (SUCCEEDED(hr))
934 {
935 BYTE *pAddress = NULL;
936 ULONG ulWidth = 0;
937 ULONG ulHeight = 0;
938 ULONG ulBitsPerPixel = 0;
939 ULONG ulBytesPerLine = 0;
940 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
941
942 hr = pSourceBitmap->QueryBitmapInfo(&pAddress,
943 &ulWidth,
944 &ulHeight,
945 &ulBitsPerPixel,
946 &ulBytesPerLine,
947 &bitmapFormat);
948 if (SUCCEEDED(hr))
949 {
950 pFBInfo->updateImage.pSourceBitmap = pSourceBitmap;
951 pFBInfo->updateImage.pu8Address = pAddress;
952 pFBInfo->updateImage.cbLine = ulBytesPerLine;
953 }
954 }
955 }
956
957 /* Inform the VRDP server about the change of display parameters. */
958 LogRelFlowFunc(("Calling VRDP\n"));
959 mParent->i_consoleVRDPServer()->SendResize();
960
961 /* And re-send the seamless rectangles if necessary. */
962 if (mfSeamlessEnabled)
963 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
964
965 LogRelFlowFunc(("[%d]: default format %d\n", uScreenId, pFBInfo->fDefaultFormat));
966
967 return VINF_SUCCESS;
968}
969
970static void i_checkCoordBounds(int *px, int *py, int *pw, int *ph, int cx, int cy)
971{
972 /* Correct negative x and y coordinates. */
973 if (*px < 0)
974 {
975 *px += *pw; /* Compute xRight which is also the new width. */
976
977 *pw = (*px < 0)? 0: *px;
978
979 *px = 0;
980 }
981
982 if (*py < 0)
983 {
984 *py += *ph; /* Compute xBottom, which is also the new height. */
985
986 *ph = (*py < 0)? 0: *py;
987
988 *py = 0;
989 }
990
991 /* Also check if coords are greater than the display resolution. */
992 if (*px + *pw > cx)
993 {
994 *pw = cx > *px? cx - *px: 0;
995 }
996
997 if (*py + *ph > cy)
998 {
999 *ph = cy > *py? cy - *py: 0;
1000 }
1001}
1002
1003void Display::i_handleDisplayUpdate(unsigned uScreenId, int x, int y, int w, int h)
1004{
1005 /*
1006 * Always runs under either VBVA lock or, for HGSMI, DevVGA lock.
1007 * Safe to use VBVA vars and take the framebuffer lock.
1008 */
1009
1010#ifdef DEBUG_sunlover
1011 LogFlowFunc(("[%d] %d,%d %dx%d\n",
1012 uScreenId, x, y, w, h));
1013#endif /* DEBUG_sunlover */
1014
1015 /* No updates for a disabled guest screen. */
1016 if (maFramebuffers[uScreenId].fDisabled)
1017 return;
1018
1019 /* No updates for a blank guest screen. */
1020 /** @note Disabled for now, as the GUI does not update the picture when we
1021 * first blank. */
1022 /* if (maFramebuffers[uScreenId].flags & VBVA_SCREEN_F_BLANK)
1023 return; */
1024
1025 i_checkCoordBounds (&x, &y, &w, &h, maFramebuffers[uScreenId].w,
1026 maFramebuffers[uScreenId].h);
1027
1028 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
1029 if (pFramebuffer != NULL)
1030 {
1031 if (w != 0 && h != 0)
1032 {
1033 bool fUpdateImage = RT_BOOL(maFramebuffers[uScreenId].u32Caps & FramebufferCapabilities_UpdateImage);
1034 if (RT_LIKELY(!fUpdateImage))
1035 {
1036 pFramebuffer->NotifyUpdate(x, y, w, h);
1037 }
1038 else
1039 {
1040 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1041
1042 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1043
1044 if (!pFBInfo->updateImage.pSourceBitmap.isNull())
1045 {
1046 Assert(pFBInfo->updateImage.pu8Address);
1047
1048 size_t cbData = w * h * 4;
1049 com::SafeArray<BYTE> image(cbData);
1050
1051 uint8_t *pu8Dst = image.raw();
1052 const uint8_t *pu8Src = pFBInfo->updateImage.pu8Address + pFBInfo->updateImage.cbLine * y + x * 4;
1053
1054 int i;
1055 for (i = y; i < y + h; ++i)
1056 {
1057 memcpy(pu8Dst, pu8Src, w * 4);
1058 pu8Dst += w * 4;
1059 pu8Src += pFBInfo->updateImage.cbLine;
1060 }
1061
1062 pFramebuffer->NotifyUpdateImage(x, y, w, h, ComSafeArrayAsInParam(image));
1063 }
1064 }
1065 }
1066 }
1067
1068#ifndef VBOX_WITH_HGSMI
1069 if (!mVideoAccelLegacy.fVideoAccelEnabled)
1070 {
1071#else
1072 if (!mVideoAccelLegacy.fVideoAccelEnabled && !maFramebuffers[uScreenId].fVBVAEnabled)
1073 {
1074#endif /* VBOX_WITH_HGSMI */
1075 /* When VBVA is enabled, the VRDP server is informed
1076 * either in VideoAccelFlush or displayVBVAUpdateProcess.
1077 * Inform the server here only if VBVA is disabled.
1078 */
1079 mParent->i_consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
1080 }
1081}
1082
1083void Display::i_updateGuestGraphicsFacility(void)
1084{
1085 Guest* pGuest = mParent->i_getGuest();
1086 AssertPtrReturnVoid(pGuest);
1087 /* The following is from GuestImpl.cpp. */
1088 /** @todo A nit: The timestamp is wrong on saved state restore. Would be better
1089 * to move the graphics and seamless capability -> facility translation to
1090 * VMMDev so this could be saved. */
1091 RTTIMESPEC TimeSpecTS;
1092 RTTimeNow(&TimeSpecTS);
1093
1094 if ( mfVMMDevSupportsGraphics
1095 || (mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS) != 0)
1096 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1097 VBoxGuestFacilityStatus_Active,
1098 0 /*fFlags*/, &TimeSpecTS);
1099 else
1100 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1101 VBoxGuestFacilityStatus_Inactive,
1102 0 /*fFlags*/, &TimeSpecTS);
1103}
1104
1105void Display::i_handleUpdateVMMDevSupportsGraphics(bool fSupportsGraphics)
1106{
1107 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1108 if (mfVMMDevSupportsGraphics == fSupportsGraphics)
1109 return;
1110 mfVMMDevSupportsGraphics = fSupportsGraphics;
1111 i_updateGuestGraphicsFacility();
1112 /* The VMMDev interface notifies the console. */
1113}
1114
1115void Display::i_handleUpdateGuestVBVACapabilities(uint32_t fNewCapabilities)
1116{
1117 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1118 bool fNotify = (fNewCapabilities & VBVACAPS_VIDEO_MODE_HINTS) != (mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS);
1119
1120 mfGuestVBVACapabilities = fNewCapabilities;
1121 if (!fNotify)
1122 return;
1123 i_updateGuestGraphicsFacility();
1124 /* Tell the console about it */
1125 mParent->i_onAdditionsStateChange();
1126}
1127
1128void Display::i_handleUpdateVBVAInputMapping(int32_t xOrigin, int32_t yOrigin, uint32_t cx, uint32_t cy)
1129{
1130 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1131
1132 xInputMappingOrigin = xOrigin;
1133 yInputMappingOrigin = yOrigin;
1134 cxInputMapping = cx;
1135 cyInputMapping = cy;
1136
1137 /* Re-send the seamless rectangles if necessary. */
1138 if (mfSeamlessEnabled)
1139 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1140}
1141
1142/**
1143 * Returns the upper left and lower right corners of the virtual framebuffer.
1144 * The lower right is "exclusive" (i.e. first pixel beyond the framebuffer),
1145 * and the origin is (0, 0), not (1, 1) like the GUI returns.
1146 */
1147void Display::i_getFramebufferDimensions(int32_t *px1, int32_t *py1,
1148 int32_t *px2, int32_t *py2)
1149{
1150 int32_t x1 = 0, y1 = 0, x2 = 0, y2 = 0;
1151 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1152
1153 AssertPtrReturnVoid(px1);
1154 AssertPtrReturnVoid(py1);
1155 AssertPtrReturnVoid(px2);
1156 AssertPtrReturnVoid(py2);
1157 LogRelFlowFunc(("\n"));
1158
1159 if (!mpDrv)
1160 return;
1161 /* If VBVA is not in use then this flag will not be set and this
1162 * will still work as it should. */
1163 if (!maFramebuffers[0].fDisabled)
1164 {
1165 x1 = (int32_t)maFramebuffers[0].xOrigin;
1166 y1 = (int32_t)maFramebuffers[0].yOrigin;
1167 x2 = (int32_t)maFramebuffers[0].w + (int32_t)maFramebuffers[0].xOrigin;
1168 y2 = (int32_t)maFramebuffers[0].h + (int32_t)maFramebuffers[0].yOrigin;
1169 }
1170 if (cxInputMapping && cyInputMapping)
1171 {
1172 x1 = xInputMappingOrigin;
1173 y1 = yInputMappingOrigin;
1174 x2 = xInputMappingOrigin + cxInputMapping;
1175 y2 = yInputMappingOrigin + cyInputMapping;
1176 }
1177 else
1178 for (unsigned i = 1; i < mcMonitors; ++i)
1179 {
1180 if (!maFramebuffers[i].fDisabled)
1181 {
1182 x1 = RT_MIN(x1, maFramebuffers[i].xOrigin);
1183 y1 = RT_MIN(y1, maFramebuffers[i].yOrigin);
1184 x2 = RT_MAX(x2, maFramebuffers[i].xOrigin + (int32_t)maFramebuffers[i].w);
1185 y2 = RT_MAX(y2, maFramebuffers[i].yOrigin + (int32_t)maFramebuffers[i].h);
1186 }
1187 }
1188 *px1 = x1;
1189 *py1 = y1;
1190 *px2 = x2;
1191 *py2 = y2;
1192}
1193
1194HRESULT Display::i_reportHostCursorCapabilities(uint32_t fCapabilitiesAdded, uint32_t fCapabilitiesRemoved)
1195{
1196 /* Do we need this to access mParent? I presume that the safe VM pointer
1197 * ensures that mpDrv will remain valid. */
1198 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1199 uint32_t fHostCursorCapabilities = (mfHostCursorCapabilities | fCapabilitiesAdded)
1200 & ~fCapabilitiesRemoved;
1201
1202 Console::SafeVMPtr ptrVM(mParent);
1203 if (!ptrVM.isOk())
1204 return ptrVM.rc();
1205 if (mfHostCursorCapabilities == fHostCursorCapabilities)
1206 return S_OK;
1207 CHECK_CONSOLE_DRV(mpDrv);
1208 alock.release(); /* Release before calling up for lock order reasons. */
1209 mpDrv->pUpPort->pfnReportHostCursorCapabilities (mpDrv->pUpPort, fCapabilitiesAdded, fCapabilitiesRemoved);
1210 mfHostCursorCapabilities = fHostCursorCapabilities;
1211 return S_OK;
1212}
1213
1214HRESULT Display::i_reportHostCursorPosition(int32_t x, int32_t y)
1215{
1216 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1217 uint32_t xAdj = (uint32_t)RT_MAX(x - xInputMappingOrigin, 0);
1218 uint32_t yAdj = (uint32_t)RT_MAX(y - yInputMappingOrigin, 0);
1219 xAdj = RT_MIN(xAdj, cxInputMapping);
1220 yAdj = RT_MIN(yAdj, cyInputMapping);
1221
1222 Console::SafeVMPtr ptrVM(mParent);
1223 if (!ptrVM.isOk())
1224 return ptrVM.rc();
1225 CHECK_CONSOLE_DRV(mpDrv);
1226 alock.release(); /* Release before calling up for lock order reasons. */
1227 mpDrv->pUpPort->pfnReportHostCursorPosition(mpDrv->pUpPort, xAdj, yAdj);
1228 return S_OK;
1229}
1230
1231static bool displayIntersectRect(RTRECT *prectResult,
1232 const RTRECT *prect1,
1233 const RTRECT *prect2)
1234{
1235 /* Initialize result to an empty record. */
1236 memset(prectResult, 0, sizeof(RTRECT));
1237
1238 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
1239 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
1240
1241 if (xLeftResult < xRightResult)
1242 {
1243 /* There is intersection by X. */
1244
1245 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
1246 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
1247
1248 if (yTopResult < yBottomResult)
1249 {
1250 /* There is intersection by Y. */
1251
1252 prectResult->xLeft = xLeftResult;
1253 prectResult->yTop = yTopResult;
1254 prectResult->xRight = xRightResult;
1255 prectResult->yBottom = yBottomResult;
1256
1257 return true;
1258 }
1259 }
1260
1261 return false;
1262}
1263
1264int Display::i_saveVisibleRegion(uint32_t cRect, PRTRECT pRect)
1265{
1266 RTRECT *pRectVisibleRegion = NULL;
1267
1268 if (pRect == mpRectVisibleRegion)
1269 return VINF_SUCCESS;
1270 if (cRect != 0)
1271 {
1272 pRectVisibleRegion = (RTRECT *)RTMemAlloc(cRect * sizeof(RTRECT));
1273 if (!pRectVisibleRegion)
1274 {
1275 return VERR_NO_MEMORY;
1276 }
1277 memcpy(pRectVisibleRegion, pRect, cRect * sizeof(RTRECT));
1278 }
1279 if (mpRectVisibleRegion)
1280 RTMemFree(mpRectVisibleRegion);
1281 mcRectVisibleRegion = cRect;
1282 mpRectVisibleRegion = pRectVisibleRegion;
1283 return VINF_SUCCESS;
1284}
1285
1286int Display::i_handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1287{
1288 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc( RT_MAX(cRect, 1)
1289 * sizeof(RTRECT));
1290 if (!pVisibleRegion)
1291 {
1292 return VERR_NO_TMP_MEMORY;
1293 }
1294 int rc = i_saveVisibleRegion(cRect, pRect);
1295 if (RT_FAILURE(rc))
1296 {
1297 RTMemTmpFree(pVisibleRegion);
1298 return rc;
1299 }
1300
1301 unsigned uScreenId;
1302 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1303 {
1304 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1305
1306 if ( !pFBInfo->pFramebuffer.isNull()
1307 & RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_VisibleRegion))
1308 {
1309 /* Prepare a new array of rectangles which intersect with the framebuffer.
1310 */
1311 RTRECT rectFramebuffer;
1312 rectFramebuffer.xLeft = pFBInfo->xOrigin - xInputMappingOrigin;
1313 rectFramebuffer.yTop = pFBInfo->yOrigin - yInputMappingOrigin;
1314 rectFramebuffer.xRight = rectFramebuffer.xLeft + pFBInfo->w;
1315 rectFramebuffer.yBottom = rectFramebuffer.yTop + pFBInfo->h;
1316
1317 uint32_t cRectVisibleRegion = 0;
1318
1319 uint32_t i;
1320 for (i = 0; i < cRect; i++)
1321 {
1322 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1323 {
1324 pVisibleRegion[cRectVisibleRegion].xLeft -= rectFramebuffer.xLeft;
1325 pVisibleRegion[cRectVisibleRegion].yTop -= rectFramebuffer.yTop;
1326 pVisibleRegion[cRectVisibleRegion].xRight -= rectFramebuffer.xLeft;
1327 pVisibleRegion[cRectVisibleRegion].yBottom -= rectFramebuffer.yTop;
1328
1329 cRectVisibleRegion++;
1330 }
1331 }
1332 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1333 }
1334 }
1335
1336#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1337 VMMDev *vmmDev = mParent->i_getVMMDev();
1338 if (mfIsCr3DEnabled && vmmDev)
1339 {
1340 if (mhCrOglSvc)
1341 {
1342 VBOXCRCMDCTL_HGCM *pCtl;
1343 pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(RT_MAX(cRect, 1) * sizeof(RTRECT) + sizeof(VBOXCRCMDCTL_HGCM));
1344 if (pCtl)
1345 {
1346 RTRECT *pRectsCopy = (RTRECT*)(pCtl+1);
1347 memcpy(pRectsCopy, pRect, cRect * sizeof(RTRECT));
1348
1349 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1350 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1351
1352 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1353 pCtl->aParms[0].u.pointer.addr = pRectsCopy;
1354 pCtl->aParms[0].u.pointer.size = cRect * sizeof(RTRECT);
1355
1356 rc = i_crCtlSubmit(&pCtl->Hdr, sizeof(*pCtl), i_displayCrCmdFree, pCtl);
1357 if (!RT_SUCCESS(rc))
1358 {
1359 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1360 RTMemFree(pCtl);
1361 }
1362 }
1363 else
1364 AssertMsgFailed(("failed to allocate rects memory\n"));
1365 }
1366 else
1367 AssertMsgFailed(("mhCrOglSvc is NULL\n"));
1368 }
1369#endif
1370
1371 RTMemTmpFree(pVisibleRegion);
1372
1373 return VINF_SUCCESS;
1374}
1375
1376int Display::i_handleQueryVisibleRegion(uint32_t *pcRect, PRTRECT pRect)
1377{
1378 // @todo Currently not used by the guest and is not implemented in framebuffers. Remove?
1379 return VERR_NOT_SUPPORTED;
1380}
1381
1382#ifdef VBOX_WITH_HGSMI
1383static void vbvaSetMemoryFlagsHGSMI(unsigned uScreenId,
1384 uint32_t fu32SupportedOrders,
1385 bool fVideoAccelVRDP,
1386 DISPLAYFBINFO *pFBInfo)
1387{
1388 LogRelFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1389
1390 if (pFBInfo->pVBVAHostFlags)
1391 {
1392 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1393
1394 if (pFBInfo->fVBVAEnabled)
1395 {
1396 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1397
1398 if (fVideoAccelVRDP)
1399 {
1400 fu32HostEvents |= VBVA_F_MODE_VRDP;
1401 }
1402 }
1403
1404 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1405 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1406
1407 LogRelFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1408 }
1409}
1410
1411static void vbvaSetMemoryFlagsAllHGSMI(uint32_t fu32SupportedOrders,
1412 bool fVideoAccelVRDP,
1413 DISPLAYFBINFO *paFBInfos,
1414 unsigned cFBInfos)
1415{
1416 unsigned uScreenId;
1417
1418 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1419 {
1420 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1421 }
1422}
1423#endif /* VBOX_WITH_HGSMI */
1424
1425int Display::VideoAccelEnableVMMDev(bool fEnable, VBVAMEMORY *pVbvaMemory)
1426{
1427 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1428 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1429 if (RT_SUCCESS(rc))
1430 {
1431 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1432 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1433 }
1434 LogFlowFunc(("leave %Rrc\n", rc));
1435 return rc;
1436}
1437
1438int Display::VideoAccelEnableVGA(bool fEnable, VBVAMEMORY *pVbvaMemory)
1439{
1440 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1441 int rc = videoAccelEnterVGA(&mVideoAccelLegacy);
1442 if (RT_SUCCESS(rc))
1443 {
1444 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1445 videoAccelLeaveVGA(&mVideoAccelLegacy);
1446 }
1447 LogFlowFunc(("leave %Rrc\n", rc));
1448 return rc;
1449}
1450
1451void Display::VideoAccelFlushVMMDev(void)
1452{
1453 LogFlowFunc(("enter\n"));
1454 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1455 if (RT_SUCCESS(rc))
1456 {
1457 i_VideoAccelFlush(mpDrv->pUpPort);
1458 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1459 }
1460 LogFlowFunc(("leave\n"));
1461}
1462
1463/* Called always by one VRDP server thread. Can be thread-unsafe.
1464 */
1465void Display::i_VideoAccelVRDP(bool fEnable)
1466{
1467 LogRelFlowFunc(("fEnable = %d\n", fEnable));
1468
1469 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
1470
1471 int c = fEnable?
1472 ASMAtomicIncS32(&mcVideoAccelVRDPRefs):
1473 ASMAtomicDecS32(&mcVideoAccelVRDPRefs);
1474
1475 Assert (c >= 0);
1476
1477 /* This can run concurrently with Display videoaccel state change. */
1478 RTCritSectEnter(&mVideoAccelLock);
1479
1480 if (c == 0)
1481 {
1482 /* The last client has disconnected, and the accel can be
1483 * disabled.
1484 */
1485 Assert (fEnable == false);
1486
1487 mfVideoAccelVRDP = false;
1488 mfu32SupportedOrders = 0;
1489
1490 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1491 maFramebuffers, mcMonitors);
1492#ifdef VBOX_WITH_HGSMI
1493 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1494 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1495#endif /* VBOX_WITH_HGSMI */
1496
1497 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1498 }
1499 else if ( c == 1
1500 && !mfVideoAccelVRDP)
1501 {
1502 /* The first client has connected. Enable the accel.
1503 */
1504 Assert (fEnable == true);
1505
1506 mfVideoAccelVRDP = true;
1507 /* Supporting all orders. */
1508 mfu32SupportedOrders = ~0;
1509
1510 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1511 maFramebuffers, mcMonitors);
1512#ifdef VBOX_WITH_HGSMI
1513 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1514 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1515#endif /* VBOX_WITH_HGSMI */
1516
1517 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1518 }
1519 else
1520 {
1521 /* A client is connected or disconnected but there is no change in the
1522 * accel state. It remains enabled.
1523 */
1524 Assert(mfVideoAccelVRDP == true);
1525 }
1526
1527 RTCritSectLeave(&mVideoAccelLock);
1528}
1529
1530void Display::i_notifyPowerDown(void)
1531{
1532 LogRelFlowFunc(("\n"));
1533
1534 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1535
1536 /* Source bitmaps are not available anymore. */
1537 mfSourceBitmapEnabled = false;
1538
1539 alock.release();
1540
1541 /* Resize all displays to tell framebuffers to forget current source bitmap. */
1542 unsigned uScreenId = mcMonitors;
1543 while (uScreenId > 0)
1544 {
1545 --uScreenId;
1546
1547 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1548 if (!pFBInfo->fDisabled)
1549 {
1550 i_handleDisplayResize(uScreenId, 32,
1551 pFBInfo->pu8FramebufferVRAM,
1552 pFBInfo->u32LineSize,
1553 pFBInfo->w,
1554 pFBInfo->h,
1555 pFBInfo->flags);
1556 }
1557 }
1558}
1559
1560// Wrapped IDisplay methods
1561/////////////////////////////////////////////////////////////////////////////
1562HRESULT Display::getScreenResolution(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel,
1563 LONG *aXOrigin, LONG *aYOrigin, GuestMonitorStatus_T *aGuestMonitorStatus)
1564{
1565 LogRelFlowFunc(("aScreenId=%RU32\n", aScreenId));
1566
1567 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1568
1569 if (aScreenId >= mcMonitors)
1570 return E_INVALIDARG;
1571
1572 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1573
1574 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
1575 if (pFBInfo->flags & VBVA_SCREEN_F_DISABLED)
1576 guestMonitorStatus = GuestMonitorStatus_Disabled;
1577
1578 if (aWidth)
1579 *aWidth = pFBInfo->w;
1580 if (aHeight)
1581 *aHeight = pFBInfo->h;
1582 if (aBitsPerPixel)
1583 *aBitsPerPixel = pFBInfo->u16BitsPerPixel;
1584 if (aXOrigin)
1585 *aXOrigin = pFBInfo->xOrigin;
1586 if (aYOrigin)
1587 *aYOrigin = pFBInfo->yOrigin;
1588 if (aGuestMonitorStatus)
1589 *aGuestMonitorStatus = guestMonitorStatus;
1590
1591 return S_OK;
1592}
1593
1594
1595HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer, com::Guid &aId)
1596{
1597 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1598
1599 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1600
1601 if (aScreenId >= mcMonitors)
1602 return setError(E_INVALIDARG, tr("AttachFramebuffer: Invalid screen %d (total %d)"),
1603 aScreenId, mcMonitors);
1604
1605 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1606 if (!pFBInfo->pFramebuffer.isNull())
1607 return setError(E_FAIL, tr("AttachFramebuffer: Framebuffer already attached to %d"),
1608 aScreenId);
1609
1610 pFBInfo->pFramebuffer = aFramebuffer;
1611 pFBInfo->framebufferId.create();
1612 aId = pFBInfo->framebufferId;
1613
1614 SafeArray<FramebufferCapabilities_T> caps;
1615 pFBInfo->pFramebuffer->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(caps));
1616 pFBInfo->u32Caps = 0;
1617 size_t i;
1618 for (i = 0; i < caps.size(); ++i)
1619 pFBInfo->u32Caps |= caps[i];
1620
1621 alock.release();
1622
1623 /* The driver might not have been constructed yet */
1624 if (mpDrv)
1625 {
1626 /* Setup the new framebuffer. */
1627 i_handleDisplayResize(aScreenId, pFBInfo->u16BitsPerPixel,
1628 pFBInfo->pu8FramebufferVRAM,
1629 pFBInfo->u32LineSize,
1630 pFBInfo->w,
1631 pFBInfo->h,
1632 pFBInfo->flags);
1633 }
1634
1635 Console::SafeVMPtrQuiet ptrVM(mParent);
1636 if (ptrVM.isOk())
1637 {
1638#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1639 if (mfIsCr3DEnabled)
1640 {
1641 VBOXCRCMDCTL_HGCM data;
1642 RT_ZERO(data);
1643 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1644 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1645
1646 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1647 data.aParms[0].u.uint32 = aScreenId;
1648
1649 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1650 AssertRC(vrc);
1651 }
1652#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1653
1654 VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
1655 3, this, aScreenId, false);
1656 }
1657
1658 LogRelFlowFunc(("Attached to %d %RTuuid\n", aScreenId, aId.raw()));
1659 return S_OK;
1660}
1661
1662HRESULT Display::detachFramebuffer(ULONG aScreenId, const com::Guid &aId)
1663{
1664 LogRelFlowFunc(("aScreenId = %d %RTuuid\n", aScreenId, aId.raw()));
1665
1666 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1667
1668 if (aScreenId >= mcMonitors)
1669 return setError(E_INVALIDARG, tr("DetachFramebuffer: Invalid screen %d (total %d)"),
1670 aScreenId, mcMonitors);
1671
1672 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1673
1674 if (pFBInfo->framebufferId != aId)
1675 {
1676 LogRelFlowFunc(("Invalid framebuffer aScreenId = %d, attached %p\n", aScreenId, pFBInfo->framebufferId.raw()));
1677 return setError(E_FAIL, tr("DetachFramebuffer: Invalid framebuffer object"));
1678 }
1679
1680 pFBInfo->pFramebuffer.setNull();
1681 pFBInfo->framebufferId.clear();
1682
1683 alock.release();
1684
1685#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1686 Console::SafeVMPtrQuiet ptrVM(mParent);
1687 if (ptrVM.isOk())
1688 {
1689 if (mfIsCr3DEnabled)
1690 {
1691 VBOXCRCMDCTL_HGCM data;
1692 RT_ZERO(data);
1693 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1694 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1695
1696 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1697 data.aParms[0].u.uint32 = aScreenId;
1698
1699 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1700 AssertRC(vrc);
1701 }
1702 }
1703#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1704
1705 return S_OK;
1706}
1707
1708HRESULT Display::queryFramebuffer(ULONG aScreenId, ComPtr<IFramebuffer> &aFramebuffer)
1709{
1710 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1711
1712 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1713
1714 if (aScreenId >= mcMonitors)
1715 return setError(E_INVALIDARG, tr("QueryFramebuffer: Invalid screen %d (total %d)"),
1716 aScreenId, mcMonitors);
1717
1718 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1719
1720 pFBInfo->pFramebuffer.queryInterfaceTo(aFramebuffer.asOutParam());
1721
1722 return S_OK;
1723}
1724
1725HRESULT Display::setVideoModeHint(ULONG aDisplay, BOOL aEnabled,
1726 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
1727 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel)
1728{
1729 if (aWidth == 0 || aHeight == 0 || aBitsPerPixel == 0)
1730 {
1731 /* Some of parameters must not change. Query current mode. */
1732 ULONG ulWidth = 0;
1733 ULONG ulHeight = 0;
1734 ULONG ulBitsPerPixel = 0;
1735 HRESULT hr = getScreenResolution(aDisplay, &ulWidth, &ulHeight, &ulBitsPerPixel, NULL, NULL, NULL);
1736 if (FAILED(hr))
1737 return hr;
1738
1739 /* Assign current values to not changing parameters. */
1740 if (aWidth == 0)
1741 aWidth = ulWidth;
1742 if (aHeight == 0)
1743 aHeight = ulHeight;
1744 if (aBitsPerPixel == 0)
1745 aBitsPerPixel = ulBitsPerPixel;
1746 }
1747
1748 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1749
1750 if (aDisplay >= mcMonitors)
1751 return E_INVALIDARG;
1752
1753 CHECK_CONSOLE_DRV(mpDrv);
1754
1755 /*
1756 * It is up to the guest to decide whether the hint is
1757 * valid. Therefore don't do any VRAM sanity checks here.
1758 */
1759
1760 /* Have to release the lock because the pfnRequestDisplayChange
1761 * will call EMT. */
1762 alock.release();
1763
1764 /* We always send the hint to the graphics card in case the guest enables
1765 * support later. For now we notify exactly when support is enabled. */
1766 mpDrv->pUpPort->pfnSendModeHint(mpDrv->pUpPort, aWidth, aHeight,
1767 aBitsPerPixel, aDisplay,
1768 aChangeOrigin ? aOriginX : ~0,
1769 aChangeOrigin ? aOriginY : ~0,
1770 RT_BOOL(aEnabled),
1771 mfGuestVBVACapabilities
1772 & VBVACAPS_VIDEO_MODE_HINTS);
1773 if ( mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS
1774 && !(mfGuestVBVACapabilities & VBVACAPS_IRQ))
1775 {
1776 mParent->i_sendACPIMonitorHotPlugEvent();
1777 }
1778
1779 /* We currently never suppress the VMMDev hint if the guest has requested
1780 * it. Specifically the video graphics driver may not be responsible for
1781 * screen positioning in the guest virtual desktop, and the component
1782 * responsible may want to get the hint from VMMDev. */
1783 VMMDev *pVMMDev = mParent->i_getVMMDev();
1784 if (pVMMDev)
1785 {
1786 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1787 if (pVMMDevPort)
1788 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel,
1789 aDisplay, aOriginX, aOriginY,
1790 RT_BOOL(aEnabled), RT_BOOL(aChangeOrigin));
1791 }
1792 return S_OK;
1793}
1794
1795HRESULT Display::setSeamlessMode(BOOL enabled)
1796{
1797 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1798
1799 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
1800 alock.release();
1801
1802 VMMDev *pVMMDev = mParent->i_getVMMDev();
1803 if (pVMMDev)
1804 {
1805 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1806 if (pVMMDevPort)
1807 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
1808 }
1809 mfSeamlessEnabled = enabled;
1810
1811#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1812 if (!enabled)
1813 {
1814 VMMDev *vmmDev = mParent->i_getVMMDev();
1815 if (mfIsCr3DEnabled && vmmDev)
1816 {
1817 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(VBOXCRCMDCTL_HGCM));
1818 if (!pData)
1819 {
1820 AssertMsgFailed(("RTMemAlloc failed\n"));
1821 return VERR_NO_MEMORY;
1822 }
1823
1824 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1825 pData->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1826
1827 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1828 pData->aParms[0].u.pointer.addr = NULL;
1829 pData->aParms[0].u.pointer.size = 0; /* <- means null rects, NULL pRects address and 0 rects means "disable" */
1830
1831 int rc = i_crCtlSubmit(&pData->Hdr, sizeof(*pData), i_displayCrCmdFree, pData);
1832 if (!RT_SUCCESS(rc))
1833 {
1834 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1835 RTMemFree(pData);
1836 }
1837 }
1838 }
1839#endif
1840 return S_OK;
1841}
1842
1843#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1844BOOL Display::i_displayCheckTakeScreenshotCrOgl(Display *pDisplay, ULONG aScreenId, uint8_t *pbData,
1845 uint32_t u32Width, uint32_t u32Height)
1846{
1847 if ( pDisplay->mfIsCr3DEnabled
1848 && pDisplay->mCrOglCallbacks.pfnHasData
1849 && pDisplay->mCrOglCallbacks.pfnHasData())
1850 {
1851 VMMDev *pVMMDev = pDisplay->mParent->i_getVMMDev();
1852 if (pVMMDev)
1853 {
1854 CRVBOXHGCMTAKESCREENSHOT *pScreenshot = (CRVBOXHGCMTAKESCREENSHOT *)RTMemAlloc(sizeof(*pScreenshot));
1855 if (pScreenshot)
1856 {
1857 /* screen id or CRSCREEN_ALL to specify all enabled */
1858 pScreenshot->u32Screen = aScreenId;
1859 pScreenshot->u32Width = u32Width;
1860 pScreenshot->u32Height = u32Height;
1861 pScreenshot->u32Pitch = u32Width * 4;
1862 pScreenshot->pvBuffer = pbData;
1863 pScreenshot->pvContext = NULL;
1864 pScreenshot->pfnScreenshotBegin = NULL;
1865 pScreenshot->pfnScreenshotPerform = NULL;
1866 pScreenshot->pfnScreenshotEnd = NULL;
1867
1868 VBOXCRCMDCTL_HGCM data;
1869 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1870 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
1871
1872 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1873 data.aParms[0].u.pointer.addr = pScreenshot;
1874 data.aParms[0].u.pointer.size = sizeof(*pScreenshot);
1875
1876 int rc = pDisplay->i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1877
1878 RTMemFree(pScreenshot);
1879
1880 if (RT_SUCCESS(rc))
1881 return TRUE;
1882 AssertMsgFailed(("failed to get screenshot data from crOgl (rc=%Rrc)\n", rc));
1883 /* fall back to the non-3d mechanism */
1884 }
1885 }
1886 }
1887 return FALSE;
1888}
1889#endif
1890
1891/* static */
1892int Display::i_displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppbData, size_t *pcbData,
1893 uint32_t *pcx, uint32_t *pcy, bool *pfMemFree)
1894{
1895 int rc;
1896 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
1897 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
1898 {
1899 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
1900 *pfMemFree = false;
1901 }
1902 else if (aScreenId < pDisplay->mcMonitors)
1903 {
1904 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
1905
1906 uint32_t width = pFBInfo->w;
1907 uint32_t height = pFBInfo->h;
1908
1909 /* Allocate 32 bit per pixel bitmap. */
1910 size_t cbRequired = width * 4 * height;
1911
1912 if (cbRequired)
1913 {
1914 uint8_t *pbDst = (uint8_t *)RTMemAlloc(cbRequired);
1915 if (pbDst != NULL)
1916 {
1917 /* Copy guest VRAM to the allocated 32bpp buffer. */
1918 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
1919 int32_t xSrc = 0;
1920 int32_t ySrc = 0;
1921 uint32_t u32SrcWidth = width;
1922 uint32_t u32SrcHeight = height;
1923 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
1924 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
1925
1926 int32_t xDst = 0;
1927 int32_t yDst = 0;
1928 uint32_t u32DstWidth = u32SrcWidth;
1929 uint32_t u32DstHeight = u32SrcHeight;
1930 uint32_t u32DstLineSize = u32DstWidth * 4;
1931 uint32_t u32DstBitsPerPixel = 32;
1932
1933 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
1934 width, height,
1935 pu8Src,
1936 xSrc, ySrc,
1937 u32SrcWidth, u32SrcHeight,
1938 u32SrcLineSize, u32SrcBitsPerPixel,
1939 pbDst,
1940 xDst, yDst,
1941 u32DstWidth, u32DstHeight,
1942 u32DstLineSize, u32DstBitsPerPixel);
1943 if (RT_SUCCESS(rc))
1944 {
1945 *ppbData = pbDst;
1946 *pcbData = cbRequired;
1947 *pcx = width;
1948 *pcy = height;
1949 *pfMemFree = true;
1950 }
1951 else
1952 {
1953 RTMemFree(pbDst);
1954
1955 /* CopyRect can fail if VBVA was paused in VGA device, retry using the generic method. */
1956 if ( rc == VERR_INVALID_STATE
1957 && aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1958 {
1959 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
1960 *pfMemFree = false;
1961 }
1962 }
1963 }
1964 else
1965 rc = VERR_NO_MEMORY;
1966 }
1967 else
1968 {
1969 /* No image. */
1970 *ppbData = NULL;
1971 *pcbData = 0;
1972 *pcx = 0;
1973 *pcy = 0;
1974 *pfMemFree = true;
1975 rc = VINF_SUCCESS;
1976 }
1977 }
1978 else
1979 rc = VERR_INVALID_PARAMETER;
1980 return rc;
1981}
1982
1983static int i_displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
1984 BYTE *address, ULONG width, ULONG height)
1985{
1986#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1987 /*
1988 * CrOgl screenshot hook/hack.
1989 */
1990 if (Display::i_displayCheckTakeScreenshotCrOgl(pDisplay, aScreenId, (uint8_t *)address, width, height))
1991 return VINF_SUCCESS;
1992#endif
1993
1994 uint8_t *pbData = NULL;
1995 size_t cbData = 0;
1996 uint32_t cx = 0;
1997 uint32_t cy = 0;
1998 bool fFreeMem = false;
1999 int vrc = VINF_SUCCESS;
2000
2001 int cRetries = 5;
2002 while (cRetries-- > 0)
2003 {
2004 /* Note! Not sure if the priority call is such a good idea here, but
2005 it would be nice to have an accurate screenshot for the bug
2006 report if the VM deadlocks. */
2007 vrc = VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)Display::i_displayTakeScreenshotEMT, 7,
2008 pDisplay, aScreenId, &pbData, &cbData, &cx, &cy, &fFreeMem);
2009 if (vrc != VERR_TRY_AGAIN)
2010 {
2011 break;
2012 }
2013
2014 RTThreadSleep(10);
2015 }
2016
2017 if (RT_SUCCESS(vrc) && pbData)
2018 {
2019 if (cx == width && cy == height)
2020 {
2021 /* No scaling required. */
2022 memcpy(address, pbData, cbData);
2023 }
2024 else
2025 {
2026 /* Scale. */
2027 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2028
2029 uint8_t *dst = address;
2030 uint8_t *src = pbData;
2031 int dstW = width;
2032 int dstH = height;
2033 int srcW = cx;
2034 int srcH = cy;
2035 int iDeltaLine = cx * 4;
2036
2037 BitmapScale32(dst,
2038 dstW, dstH,
2039 src,
2040 iDeltaLine,
2041 srcW, srcH);
2042 }
2043
2044 if (fFreeMem)
2045 RTMemFree(pbData);
2046 else
2047 {
2048 /* This can be called from any thread. */
2049 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pbData);
2050 }
2051 }
2052
2053 return vrc;
2054}
2055
2056HRESULT Display::takeScreenShotWorker(ULONG aScreenId,
2057 BYTE *aAddress,
2058 ULONG aWidth,
2059 ULONG aHeight,
2060 BitmapFormat_T aBitmapFormat,
2061 ULONG *pcbOut)
2062{
2063 HRESULT rc = S_OK;
2064
2065 /* Do not allow too small and too large screenshots. This also filters out negative
2066 * values passed as either 'aWidth' or 'aHeight'.
2067 */
2068 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2069 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2070
2071 if ( aBitmapFormat != BitmapFormat_BGR0
2072 && aBitmapFormat != BitmapFormat_BGRA
2073 && aBitmapFormat != BitmapFormat_RGBA
2074 && aBitmapFormat != BitmapFormat_PNG)
2075 {
2076 return setError(E_NOTIMPL,
2077 tr("Unsupported screenshot format 0x%08X"), aBitmapFormat);
2078 }
2079
2080 Console::SafeVMPtr ptrVM(mParent);
2081 if (!ptrVM.isOk())
2082 return ptrVM.rc();
2083
2084 int vrc = i_displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, aAddress, aWidth, aHeight);
2085
2086 if (RT_SUCCESS(vrc))
2087 {
2088 const size_t cbData = aWidth * 4 * aHeight;
2089
2090 /* Most of uncompressed formats. */
2091 *pcbOut = (ULONG)cbData;
2092
2093 if (aBitmapFormat == BitmapFormat_BGR0)
2094 {
2095 /* Do nothing. */
2096 }
2097 else if (aBitmapFormat == BitmapFormat_BGRA)
2098 {
2099 uint32_t *pu32 = (uint32_t *)aAddress;
2100 size_t cPixels = aWidth * aHeight;
2101 while (cPixels--)
2102 {
2103 *pu32++ |= UINT32_C(0xFF000000);
2104 }
2105 }
2106 else if (aBitmapFormat == BitmapFormat_RGBA)
2107 {
2108 uint8_t *pu8 = aAddress;
2109 size_t cPixels = aWidth * aHeight;
2110 while (cPixels--)
2111 {
2112 uint8_t u8 = pu8[0];
2113 pu8[0] = pu8[2];
2114 pu8[2] = u8;
2115 pu8[3] = 0xFF;
2116
2117 pu8 += 4;
2118 }
2119 }
2120 else if (aBitmapFormat == BitmapFormat_PNG)
2121 {
2122 uint8_t *pu8PNG = NULL;
2123 uint32_t cbPNG = 0;
2124 uint32_t cxPNG = 0;
2125 uint32_t cyPNG = 0;
2126
2127 vrc = DisplayMakePNG(aAddress, aWidth, aHeight, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2128 if (RT_SUCCESS(vrc))
2129 {
2130 if (cbPNG <= cbData)
2131 {
2132 memcpy(aAddress, pu8PNG, cbPNG);
2133 *pcbOut = cbPNG;
2134 }
2135 else
2136 {
2137 rc = setError(E_FAIL,
2138 tr("PNG is larger than 32bpp bitmap"));
2139 }
2140 }
2141 else
2142 {
2143 rc = setError(VBOX_E_IPRT_ERROR,
2144 tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2145 }
2146 RTMemFree(pu8PNG);
2147 }
2148 }
2149 else if (vrc == VERR_TRY_AGAIN)
2150 rc = setError(E_UNEXPECTED,
2151 tr("Screenshot is not available at this time"));
2152 else if (RT_FAILURE(vrc))
2153 rc = setError(VBOX_E_IPRT_ERROR,
2154 tr("Could not take a screenshot (%Rrc)"), vrc);
2155
2156 return rc;
2157}
2158
2159HRESULT Display::takeScreenShot(ULONG aScreenId,
2160 BYTE *aAddress,
2161 ULONG aWidth,
2162 ULONG aHeight,
2163 BitmapFormat_T aBitmapFormat)
2164{
2165 HRESULT rc = S_OK;
2166
2167 LogRelFlowFunc(("[%d] address=%p, width=%d, height=%d, format 0x%08X\n",
2168 aScreenId, aAddress, aWidth, aHeight, aBitmapFormat));
2169
2170 ULONG cbOut = 0;
2171 rc = takeScreenShotWorker(aScreenId, aAddress, aWidth, aHeight, aBitmapFormat, &cbOut);
2172 NOREF(cbOut);
2173
2174 LogRelFlowFunc(("%Rhrc\n", rc));
2175 return rc;
2176}
2177
2178HRESULT Display::takeScreenShotToArray(ULONG aScreenId,
2179 ULONG aWidth,
2180 ULONG aHeight,
2181 BitmapFormat_T aBitmapFormat,
2182 std::vector<BYTE> &aScreenData)
2183{
2184 HRESULT rc = S_OK;
2185
2186 LogRelFlowFunc(("[%d] width=%d, height=%d, format 0x%08X\n",
2187 aScreenId, aWidth, aHeight, aBitmapFormat));
2188
2189 /* Do not allow too small and too large screenshots. This also filters out negative
2190 * values passed as either 'aWidth' or 'aHeight'.
2191 */
2192 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2193 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2194
2195 const size_t cbData = aWidth * 4 * aHeight;
2196 aScreenData.resize(cbData);
2197
2198 ULONG cbOut = 0;
2199 rc = takeScreenShotWorker(aScreenId, &aScreenData.front(), aWidth, aHeight, aBitmapFormat, &cbOut);
2200 if (FAILED(rc))
2201 cbOut = 0;
2202
2203 aScreenData.resize(cbOut);
2204
2205 LogRelFlowFunc(("%Rhrc\n", rc));
2206 return rc;
2207}
2208
2209
2210int Display::i_VideoCaptureEnableScreens(ComSafeArrayIn(BOOL, aScreens))
2211{
2212#ifdef VBOX_WITH_VPX
2213 com::SafeArray<BOOL> Screens(ComSafeArrayInArg(aScreens));
2214 for (unsigned i = 0; i < Screens.size(); i++)
2215 maVideoRecEnabled[i] = RT_BOOL(Screens[i]);
2216 return VINF_SUCCESS;
2217#else
2218 return VERR_NOT_IMPLEMENTED;
2219#endif
2220}
2221
2222/**
2223 * Start video capturing. Does nothing if capturing is already active.
2224 */
2225int Display::i_VideoCaptureStart()
2226{
2227#ifdef VBOX_WITH_VPX
2228 if (VideoRecIsEnabled(mpVideoRecCtx))
2229 return VINF_SUCCESS;
2230
2231 int rc = VideoRecContextCreate(&mpVideoRecCtx, mcMonitors);
2232 if (RT_FAILURE(rc))
2233 {
2234 LogFlow(("Failed to create video recording context (%Rrc)!\n", rc));
2235 return rc;
2236 }
2237 ComPtr<IMachine> pMachine = mParent->i_machine();
2238 com::SafeArray<BOOL> screens;
2239 HRESULT hrc = pMachine->COMGETTER(VideoCaptureScreens)(ComSafeArrayAsOutParam(screens));
2240 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2241 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
2242 maVideoRecEnabled[i] = i < screens.size() && screens[i];
2243 ULONG ulWidth;
2244 hrc = pMachine->COMGETTER(VideoCaptureWidth)(&ulWidth);
2245 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2246 ULONG ulHeight;
2247 hrc = pMachine->COMGETTER(VideoCaptureHeight)(&ulHeight);
2248 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2249 ULONG ulRate;
2250 hrc = pMachine->COMGETTER(VideoCaptureRate)(&ulRate);
2251 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2252 ULONG ulFPS;
2253 hrc = pMachine->COMGETTER(VideoCaptureFPS)(&ulFPS);
2254 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2255 BSTR strFile;
2256 hrc = pMachine->COMGETTER(VideoCaptureFile)(&strFile);
2257 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2258 ULONG ulMaxTime;
2259 hrc = pMachine->COMGETTER(VideoCaptureMaxTime)(&ulMaxTime);
2260 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2261 ULONG ulMaxSize;
2262 hrc = pMachine->COMGETTER(VideoCaptureMaxFileSize)(&ulMaxSize);
2263 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2264 BSTR strOptions;
2265 hrc = pMachine->COMGETTER(VideoCaptureOptions)(&strOptions);
2266 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2267
2268 RTTIMESPEC ts;
2269 RTTimeNow(&ts);
2270 RTTIME time;
2271 RTTimeExplode(&time, &ts);
2272 for (unsigned uScreen = 0; uScreen < mcMonitors; uScreen++)
2273 {
2274 char *pszAbsPath = RTPathAbsDup(com::Utf8Str(strFile).c_str());
2275 char *pszSuff = RTPathSuffix(pszAbsPath);
2276 if (pszSuff)
2277 pszSuff = RTStrDup(pszSuff);
2278 RTPathStripSuffix(pszAbsPath);
2279 if (!pszAbsPath)
2280 rc = VERR_INVALID_PARAMETER;
2281 if (!pszSuff)
2282 pszSuff = RTStrDup(".webm");
2283 char *pszName = NULL;
2284 if (RT_SUCCESS(rc))
2285 {
2286 if (mcMonitors > 1)
2287 rc = RTStrAPrintf(&pszName, "%s-%u%s", pszAbsPath, uScreen+1, pszSuff);
2288 else
2289 rc = RTStrAPrintf(&pszName, "%s%s", pszAbsPath, pszSuff);
2290 }
2291 if (RT_SUCCESS(rc))
2292 {
2293 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
2294 pszName, ulWidth, ulHeight,
2295 ulRate, ulFPS, ulMaxTime,
2296 ulMaxSize, com::Utf8Str(strOptions).c_str());
2297 if (rc == VERR_ALREADY_EXISTS)
2298 {
2299 RTStrFree(pszName);
2300 pszName = NULL;
2301
2302 if (mcMonitors > 1)
2303 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ-%u%s",
2304 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2305 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2306 uScreen+1, pszSuff);
2307 else
2308 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ%s",
2309 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2310 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2311 pszSuff);
2312 if (RT_SUCCESS(rc))
2313 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
2314 pszName, ulWidth, ulHeight, ulRate,
2315 ulFPS, ulMaxTime,
2316 ulMaxSize, com::Utf8Str(strOptions).c_str());
2317 }
2318 }
2319
2320 if (RT_SUCCESS(rc))
2321 {
2322 LogRel(("Display::VideoCaptureStart: WebM/VP8 video recording screen #%u with %ux%u @ %u kbps, %u fps to '%s' "
2323 "enabled\n", uScreen, ulWidth, ulHeight, ulRate, ulFPS, pszName));
2324 }
2325 else
2326 LogRel(("Display::VideoCaptureStart: Failed to initialize video recording context #%u (%Rrc)!\n", uScreen, rc));
2327 RTStrFree(pszName);
2328 RTStrFree(pszSuff);
2329 RTStrFree(pszAbsPath);
2330 }
2331 return rc;
2332#else
2333 return VERR_NOT_IMPLEMENTED;
2334#endif
2335}
2336
2337/**
2338 * Stop video capturing. Does nothing if video capturing is not active.
2339 */
2340void Display::i_VideoCaptureStop()
2341{
2342#ifdef VBOX_WITH_VPX
2343 if (VideoRecIsEnabled(mpVideoRecCtx))
2344 LogRel(("Display::VideoCaptureStop: WebM/VP8 video recording stopped\n"));
2345 VideoRecContextClose(mpVideoRecCtx);
2346 mpVideoRecCtx = NULL;
2347#endif
2348}
2349
2350int Display::i_drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address,
2351 ULONG x, ULONG y, ULONG width, ULONG height)
2352{
2353 int rc = VINF_SUCCESS;
2354
2355 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2356
2357 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2358 {
2359 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2360 }
2361 else if (aScreenId < pDisplay->mcMonitors)
2362 {
2363 /* Copy the bitmap to the guest VRAM. */
2364 const uint8_t *pu8Src = address;
2365 int32_t xSrc = 0;
2366 int32_t ySrc = 0;
2367 uint32_t u32SrcWidth = width;
2368 uint32_t u32SrcHeight = height;
2369 uint32_t u32SrcLineSize = width * 4;
2370 uint32_t u32SrcBitsPerPixel = 32;
2371
2372 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2373 int32_t xDst = x;
2374 int32_t yDst = y;
2375 uint32_t u32DstWidth = pFBInfo->w;
2376 uint32_t u32DstHeight = pFBInfo->h;
2377 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2378 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2379
2380 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2381 width, height,
2382 pu8Src,
2383 xSrc, ySrc,
2384 u32SrcWidth, u32SrcHeight,
2385 u32SrcLineSize, u32SrcBitsPerPixel,
2386 pu8Dst,
2387 xDst, yDst,
2388 u32DstWidth, u32DstHeight,
2389 u32DstLineSize, u32DstBitsPerPixel);
2390 if (RT_SUCCESS(rc))
2391 {
2392 if (!pFBInfo->pSourceBitmap.isNull())
2393 {
2394 /* Update the changed screen area. When source bitmap uses VRAM directly, just notify
2395 * frontend to update. And for default format, render the guest VRAM to the source bitmap.
2396 */
2397 if ( pFBInfo->fDefaultFormat
2398 && !pFBInfo->fDisabled)
2399 {
2400 BYTE *pAddress = NULL;
2401 ULONG ulWidth = 0;
2402 ULONG ulHeight = 0;
2403 ULONG ulBitsPerPixel = 0;
2404 ULONG ulBytesPerLine = 0;
2405 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2406
2407 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2408 &ulWidth,
2409 &ulHeight,
2410 &ulBitsPerPixel,
2411 &ulBytesPerLine,
2412 &bitmapFormat);
2413 if (SUCCEEDED(hrc))
2414 {
2415 pu8Src = pFBInfo->pu8FramebufferVRAM;
2416 xSrc = x;
2417 ySrc = y;
2418 u32SrcWidth = pFBInfo->w;
2419 u32SrcHeight = pFBInfo->h;
2420 u32SrcLineSize = pFBInfo->u32LineSize;
2421 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2422
2423 /* Default format is 32 bpp. */
2424 pu8Dst = pAddress;
2425 xDst = xSrc;
2426 yDst = ySrc;
2427 u32DstWidth = u32SrcWidth;
2428 u32DstHeight = u32SrcHeight;
2429 u32DstLineSize = u32DstWidth * 4;
2430 u32DstBitsPerPixel = 32;
2431
2432 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2433 width, height,
2434 pu8Src,
2435 xSrc, ySrc,
2436 u32SrcWidth, u32SrcHeight,
2437 u32SrcLineSize, u32SrcBitsPerPixel,
2438 pu8Dst,
2439 xDst, yDst,
2440 u32DstWidth, u32DstHeight,
2441 u32DstLineSize, u32DstBitsPerPixel);
2442 }
2443 }
2444 }
2445
2446 pDisplay->i_handleDisplayUpdate(aScreenId, x, y, width, height);
2447 }
2448 }
2449 else
2450 {
2451 rc = VERR_INVALID_PARAMETER;
2452 }
2453
2454 if (RT_SUCCESS(rc))
2455 pDisplay->mParent->i_consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
2456
2457 return rc;
2458}
2459
2460HRESULT Display::drawToScreen(ULONG aScreenId, BYTE *aAddress, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2461{
2462 /// @todo (r=dmik) this function may take too long to complete if the VM
2463 // is doing something like saving state right now. Which, in case if it
2464 // is called on the GUI thread, will make it unresponsive. We should
2465 // check the machine state here (by enclosing the check and VMRequCall
2466 // within the Console lock to make it atomic).
2467
2468 LogRelFlowFunc(("aAddress=%p, x=%d, y=%d, width=%d, height=%d\n",
2469 (void *)aAddress, aX, aY, aWidth, aHeight));
2470
2471 CheckComArgExpr(aWidth, aWidth != 0);
2472 CheckComArgExpr(aHeight, aHeight != 0);
2473
2474 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2475
2476 CHECK_CONSOLE_DRV(mpDrv);
2477
2478 Console::SafeVMPtr ptrVM(mParent);
2479 if (!ptrVM.isOk())
2480 return ptrVM.rc();
2481
2482 /* Release lock because the call scheduled on EMT may also try to take it. */
2483 alock.release();
2484
2485 /*
2486 * Again we're lazy and make the graphics device do all the
2487 * dirty conversion work.
2488 */
2489 int rcVBox = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_drawToScreenEMT, 7,
2490 this, aScreenId, aAddress, aX, aY, aWidth, aHeight);
2491
2492 /*
2493 * If the function returns not supported, we'll have to do all the
2494 * work ourselves using the framebuffer.
2495 */
2496 HRESULT rc = S_OK;
2497 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2498 {
2499 /** @todo implement generic fallback for screen blitting. */
2500 rc = E_NOTIMPL;
2501 }
2502 else if (RT_FAILURE(rcVBox))
2503 rc = setError(VBOX_E_IPRT_ERROR,
2504 tr("Could not draw to the screen (%Rrc)"), rcVBox);
2505//@todo
2506// else
2507// {
2508// /* All ok. Redraw the screen. */
2509// handleDisplayUpdate (x, y, width, height);
2510// }
2511
2512 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2513 return rc;
2514}
2515
2516int Display::i_InvalidateAndUpdateEMT(Display *pDisplay, unsigned uId, bool fUpdateAll)
2517{
2518 LogRelFlowFunc(("uId=%d, fUpdateAll %d\n", uId, fUpdateAll));
2519
2520 unsigned uScreenId;
2521 for (uScreenId = (fUpdateAll ? 0 : uId); uScreenId < pDisplay->mcMonitors; uScreenId++)
2522 {
2523 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2524
2525 if ( !pFBInfo->fVBVAEnabled
2526 && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2527 {
2528 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort, /* fFailOnResize = */ true);
2529 }
2530 else
2531 {
2532 if (!pFBInfo->fDisabled)
2533 {
2534 /* Render complete VRAM screen to the framebuffer.
2535 * When framebuffer uses VRAM directly, just notify it to update.
2536 */
2537 if (pFBInfo->fDefaultFormat && !pFBInfo->pSourceBitmap.isNull())
2538 {
2539 BYTE *pAddress = NULL;
2540 ULONG ulWidth = 0;
2541 ULONG ulHeight = 0;
2542 ULONG ulBitsPerPixel = 0;
2543 ULONG ulBytesPerLine = 0;
2544 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2545
2546 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2547 &ulWidth,
2548 &ulHeight,
2549 &ulBitsPerPixel,
2550 &ulBytesPerLine,
2551 &bitmapFormat);
2552 if (SUCCEEDED(hrc))
2553 {
2554 uint32_t width = pFBInfo->w;
2555 uint32_t height = pFBInfo->h;
2556
2557 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2558 int32_t xSrc = 0;
2559 int32_t ySrc = 0;
2560 uint32_t u32SrcWidth = pFBInfo->w;
2561 uint32_t u32SrcHeight = pFBInfo->h;
2562 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2563 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2564
2565 /* Default format is 32 bpp. */
2566 uint8_t *pu8Dst = pAddress;
2567 int32_t xDst = xSrc;
2568 int32_t yDst = ySrc;
2569 uint32_t u32DstWidth = u32SrcWidth;
2570 uint32_t u32DstHeight = u32SrcHeight;
2571 uint32_t u32DstLineSize = u32DstWidth * 4;
2572 uint32_t u32DstBitsPerPixel = 32;
2573
2574 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
2575 * implies resize of Framebuffer is in progress and
2576 * copyrect should not be called.
2577 */
2578 if (ulWidth == pFBInfo->w && ulHeight == pFBInfo->h)
2579 {
2580 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2581 width, height,
2582 pu8Src,
2583 xSrc, ySrc,
2584 u32SrcWidth, u32SrcHeight,
2585 u32SrcLineSize, u32SrcBitsPerPixel,
2586 pu8Dst,
2587 xDst, yDst,
2588 u32DstWidth, u32DstHeight,
2589 u32DstLineSize, u32DstBitsPerPixel);
2590 }
2591 }
2592 }
2593
2594 pDisplay->i_handleDisplayUpdate(uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2595 }
2596 }
2597 if (!fUpdateAll)
2598 break;
2599 }
2600 LogRelFlowFunc(("done\n"));
2601 return VINF_SUCCESS;
2602}
2603
2604/**
2605 * Does a full invalidation of the VM display and instructs the VM
2606 * to update it immediately.
2607 *
2608 * @returns COM status code
2609 */
2610
2611HRESULT Display::invalidateAndUpdate()
2612{
2613 LogRelFlowFunc(("\n"));
2614
2615 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2616
2617 CHECK_CONSOLE_DRV(mpDrv);
2618
2619 Console::SafeVMPtr ptrVM(mParent);
2620 if (!ptrVM.isOk())
2621 return ptrVM.rc();
2622
2623 HRESULT rc = S_OK;
2624
2625 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
2626
2627 /* Have to release the lock when calling EMT. */
2628 alock.release();
2629
2630 int rcVBox = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2631 3, this, 0, true);
2632 alock.acquire();
2633
2634 if (RT_FAILURE(rcVBox))
2635 rc = setError(VBOX_E_IPRT_ERROR,
2636 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2637
2638 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2639 return rc;
2640}
2641
2642HRESULT Display::invalidateAndUpdateScreen(ULONG aScreenId)
2643{
2644 LogRelFlowFunc(("\n"));
2645
2646 HRESULT rc = S_OK;
2647
2648 Console::SafeVMPtr ptrVM(mParent);
2649 if (!ptrVM.isOk())
2650 return ptrVM.rc();
2651
2652 int rcVBox = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2653 3, this, aScreenId, false);
2654 if (RT_FAILURE(rcVBox))
2655 rc = setError(VBOX_E_IPRT_ERROR,
2656 tr("Could not invalidate and update the screen %d (%Rrc)"), aScreenId, rcVBox);
2657
2658 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2659 return rc;
2660}
2661
2662HRESULT Display::completeVHWACommand(BYTE *aCommand)
2663{
2664#ifdef VBOX_WITH_VIDEOHWACCEL
2665 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)aCommand);
2666 return S_OK;
2667#else
2668 return E_NOTIMPL;
2669#endif
2670}
2671
2672HRESULT Display::viewportChanged(ULONG aScreenId, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2673{
2674 AssertMsgReturn(aScreenId < mcMonitors, ("aScreendId=%d mcMonitors=%d\n", aScreenId, mcMonitors), E_INVALIDARG);
2675
2676#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2677 if (mfIsCr3DEnabled)
2678 {
2679 int rc = i_crViewportNotify(aScreenId, aX, aY, aWidth, aHeight);
2680 if (RT_FAILURE(rc))
2681 {
2682 DISPLAYFBINFO *pFb = &maFramebuffers[aScreenId];
2683 pFb->pendingViewportInfo.fPending = true;
2684 pFb->pendingViewportInfo.x = aX;
2685 pFb->pendingViewportInfo.y = aY;
2686 pFb->pendingViewportInfo.width = aWidth;
2687 pFb->pendingViewportInfo.height = aHeight;
2688 }
2689 }
2690#endif /* VBOX_WITH_CROGL && VBOX_WITH_HGCM */
2691
2692#ifdef VBOX_WITH_VMSVGA
2693 /* The driver might not have been constructed yet */
2694 if (mpDrv)
2695 mpDrv->pUpPort->pfnSetViewPort(mpDrv->pUpPort, aScreenId, aX, aY, aWidth, aHeight);
2696#endif
2697
2698 return S_OK;
2699}
2700
2701HRESULT Display::querySourceBitmap(ULONG aScreenId,
2702 ComPtr<IDisplaySourceBitmap> &aDisplaySourceBitmap)
2703{
2704 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2705
2706 Console::SafeVMPtr ptrVM(mParent);
2707 if (!ptrVM.isOk())
2708 return ptrVM.rc();
2709
2710 bool fSetRenderVRAM = false;
2711 bool fInvalidate = false;
2712
2713 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2714
2715 if (aScreenId >= mcMonitors)
2716 return setError(E_INVALIDARG, tr("QuerySourceBitmap: Invalid screen %d (total %d)"),
2717 aScreenId, mcMonitors);
2718
2719 if (!mfSourceBitmapEnabled)
2720 {
2721 aDisplaySourceBitmap = NULL;
2722 return E_FAIL;
2723 }
2724
2725 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2726
2727 /* No source bitmap for a blank guest screen. */
2728 if (pFBInfo->flags & VBVA_SCREEN_F_BLANK)
2729 {
2730 aDisplaySourceBitmap = NULL;
2731 return E_FAIL;
2732 }
2733
2734 HRESULT hr = S_OK;
2735
2736 if (pFBInfo->pSourceBitmap.isNull())
2737 {
2738 /* Create a new object. */
2739 ComObjPtr<DisplaySourceBitmap> obj;
2740 hr = obj.createObject();
2741 if (SUCCEEDED(hr))
2742 hr = obj->init(this, aScreenId, pFBInfo);
2743
2744 if (SUCCEEDED(hr))
2745 {
2746 bool fDefaultFormat = !obj->i_usesVRAM();
2747
2748 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2749 {
2750 /* Start buffer updates. */
2751 BYTE *pAddress = NULL;
2752 ULONG ulWidth = 0;
2753 ULONG ulHeight = 0;
2754 ULONG ulBitsPerPixel = 0;
2755 ULONG ulBytesPerLine = 0;
2756 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2757
2758 obj->QueryBitmapInfo(&pAddress,
2759 &ulWidth,
2760 &ulHeight,
2761 &ulBitsPerPixel,
2762 &ulBytesPerLine,
2763 &bitmapFormat);
2764
2765 mpDrv->IConnector.pbData = pAddress;
2766 mpDrv->IConnector.cbScanline = ulBytesPerLine;
2767 mpDrv->IConnector.cBits = ulBitsPerPixel;
2768 mpDrv->IConnector.cx = ulWidth;
2769 mpDrv->IConnector.cy = ulHeight;
2770
2771 fSetRenderVRAM = fDefaultFormat;
2772 }
2773
2774 /* Make sure that the bitmap contains the latest image. */
2775 fInvalidate = fDefaultFormat;
2776
2777 pFBInfo->pSourceBitmap = obj;
2778 pFBInfo->fDefaultFormat = fDefaultFormat;
2779 }
2780 }
2781
2782 if (SUCCEEDED(hr))
2783 {
2784 pFBInfo->pSourceBitmap.queryInterfaceTo(aDisplaySourceBitmap.asOutParam());
2785 }
2786
2787 /* Leave the IDisplay lock because the VGA device must not be called under it. */
2788 alock.release();
2789
2790 if (SUCCEEDED(hr))
2791 {
2792 if (fSetRenderVRAM)
2793 {
2794 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, true);
2795 }
2796
2797 if (fInvalidate)
2798 VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2799 3, this, aScreenId, false);
2800 }
2801
2802 LogRelFlowFunc(("%Rhrc\n", hr));
2803 return hr;
2804}
2805
2806// wrapped IEventListener method
2807HRESULT Display::handleEvent(const ComPtr<IEvent> &aEvent)
2808{
2809 VBoxEventType_T aType = VBoxEventType_Invalid;
2810
2811 aEvent->COMGETTER(Type)(&aType);
2812 switch (aType)
2813 {
2814 case VBoxEventType_OnStateChanged:
2815 {
2816 ComPtr<IStateChangedEvent> scev = aEvent;
2817 Assert(scev);
2818 MachineState_T machineState;
2819 scev->COMGETTER(State)(&machineState);
2820 if ( machineState == MachineState_Running
2821 || machineState == MachineState_Teleporting
2822 || machineState == MachineState_LiveSnapshotting
2823 || machineState == MachineState_DeletingSnapshotOnline
2824 )
2825 {
2826 LogRelFlowFunc(("Machine is running.\n"));
2827
2828#ifdef VBOX_WITH_CROGL
2829 i_crOglWindowsShow(true);
2830#endif
2831 }
2832 else
2833 {
2834#ifdef VBOX_WITH_CROGL
2835 if (machineState == MachineState_Paused)
2836 i_crOglWindowsShow(false);
2837#endif
2838 }
2839 break;
2840 }
2841 default:
2842 AssertFailed();
2843 }
2844
2845 return S_OK;
2846}
2847
2848
2849// private methods
2850/////////////////////////////////////////////////////////////////////////////
2851
2852#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2853int Display::i_crViewportNotify(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
2854{
2855 VMMDev *pVMMDev = mParent->i_getVMMDev();
2856 if (!pVMMDev)
2857 return VERR_INVALID_STATE;
2858
2859 size_t cbData = RT_UOFFSETOF(VBOXCRCMDCTL_HGCM, aParms[5]);
2860 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM *)alloca(cbData);
2861
2862 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2863 pData->Hdr.u32Function = SHCRGL_HOST_FN_VIEWPORT_CHANGED;
2864
2865 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
2866 pData->aParms[0].u.uint32 = aScreenId;
2867
2868 pData->aParms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
2869 pData->aParms[1].u.uint32 = x;
2870
2871 pData->aParms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
2872 pData->aParms[2].u.uint32 = y;
2873
2874 pData->aParms[3].type = VBOX_HGCM_SVC_PARM_32BIT;
2875 pData->aParms[3].u.uint32 = width;
2876
2877 pData->aParms[4].type = VBOX_HGCM_SVC_PARM_32BIT;
2878 pData->aParms[4].u.uint32 = height;
2879
2880 return i_crCtlSubmitSyncIfHasDataForScreen(aScreenId, &pData->Hdr, (uint32_t)cbData);
2881}
2882#endif
2883
2884#ifdef VBOX_WITH_CRHGSMI
2885void Display::i_setupCrHgsmiData(void)
2886{
2887 VMMDev *pVMMDev = mParent->i_getVMMDev();
2888 Assert(pVMMDev);
2889 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
2890 AssertRC(rc);
2891
2892 if (pVMMDev)
2893 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
2894 else
2895 rc = VERR_GENERAL_FAILURE;
2896
2897 if (RT_SUCCESS(rc))
2898 {
2899 Assert(mhCrOglSvc);
2900 /* setup command completion callback */
2901 VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB Completion;
2902 Completion.Hdr.enmType = VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_MAINCB;
2903 Completion.Hdr.cbCmd = sizeof(Completion);
2904 Completion.hCompletion = mpDrv->pVBVACallbacks;
2905 Completion.pfnCompletion = mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync;
2906
2907 VBOXHGCMSVCPARM parm;
2908 parm.type = VBOX_HGCM_SVC_PARM_PTR;
2909 parm.u.pointer.addr = &Completion;
2910 parm.u.pointer.size = 0;
2911
2912 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_CRHGSMI_CTL, 1, &parm);
2913 if (RT_SUCCESS(rc))
2914 mCrOglCallbacks = Completion.MainInterface;
2915 else
2916 AssertMsgFailed(("VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION failed (rc=%Rrc)\n", rc));
2917 }
2918
2919 if (RT_FAILURE(rc))
2920 mhCrOglSvc = NULL;
2921
2922 RTCritSectRwLeaveExcl(&mCrOglLock);
2923}
2924
2925void Display::i_destructCrHgsmiData(void)
2926{
2927 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
2928 AssertRC(rc);
2929 mhCrOglSvc = NULL;
2930 RTCritSectRwLeaveExcl(&mCrOglLock);
2931}
2932#endif /* VBOX_WITH_CRHGSMI */
2933
2934/**
2935 * Handle display resize event issued by the VGA device for the primary screen.
2936 *
2937 * @see PDMIDISPLAYCONNECTOR::pfnResize
2938 */
2939DECLCALLBACK(int) Display::i_displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
2940 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
2941{
2942 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2943 Display *pThis = pDrv->pDisplay;
2944
2945 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
2946 bpp, pvVRAM, cbLine, cx, cy));
2947
2948 bool f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, true, false);
2949 if (!f)
2950 {
2951 /* This is a result of recursive call when the source bitmap is being updated
2952 * during a VGA resize. Tell the VGA device to ignore the call.
2953 *
2954 * @todo It is a workaround, actually pfnUpdateDisplayAll must
2955 * fail on resize.
2956 */
2957 LogRel(("displayResizeCallback: already processing\n"));
2958 return VINF_VGA_RESIZE_IN_PROGRESS;
2959 }
2960
2961 int rc = pThis->i_handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, VBVA_SCREEN_F_ACTIVE);
2962
2963 /* Restore the flag. */
2964 f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, false, true);
2965 AssertRelease(f);
2966
2967 return rc;
2968}
2969
2970/**
2971 * Handle display update.
2972 *
2973 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
2974 */
2975DECLCALLBACK(void) Display::i_displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
2976 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
2977{
2978 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2979
2980#ifdef DEBUG_sunlover
2981 LogFlowFunc(("fVideoAccelEnabled = %d, %d,%d %dx%d\n",
2982 pDrv->pDisplay->mVideoAccelLegacy.fVideoAccelEnabled, x, y, cx, cy));
2983#endif /* DEBUG_sunlover */
2984
2985 /* This call does update regardless of VBVA status.
2986 * But in VBVA mode this is called only as result of
2987 * pfnUpdateDisplayAll in the VGA device.
2988 */
2989
2990 pDrv->pDisplay->i_handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
2991}
2992
2993/**
2994 * Periodic display refresh callback.
2995 *
2996 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
2997 * @thread EMT
2998 */
2999/*static*/ DECLCALLBACK(void) Display::i_displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
3000{
3001 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3002
3003#ifdef DEBUG_sunlover_2
3004 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
3005 pDrv->pDisplay->mfVideoAccelEnabled));
3006#endif /* DEBUG_sunlover_2 */
3007
3008 Display *pDisplay = pDrv->pDisplay;
3009 unsigned uScreenId;
3010
3011 int rc = pDisplay->i_videoAccelRefreshProcess(pDrv->pUpPort);
3012 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3013 {
3014 if (rc == VWRN_INVALID_STATE)
3015 {
3016 /* No VBVA do a display update. */
3017 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3018 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3019 }
3020
3021 /* Inform the VRDP server that the current display update sequence is
3022 * completed. At this moment the framebuffer memory contains a definite
3023 * image, that is synchronized with the orders already sent to VRDP client.
3024 * The server can now process redraw requests from clients or initial
3025 * fullscreen updates for new clients.
3026 */
3027 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3028 {
3029 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3030
3031 Assert(pDisplay->mParent && pDisplay->mParent->i_consoleVRDPServer());
3032 pDisplay->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, NULL, 0);
3033 }
3034 }
3035
3036#ifdef VBOX_WITH_VPX
3037 if (VideoRecIsEnabled(pDisplay->mpVideoRecCtx))
3038 {
3039 do {
3040# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3041 if (pDisplay->mfIsCr3DEnabled)
3042 {
3043 if (ASMAtomicCmpXchgU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_SUBMITTED, CRVREC_STATE_IDLE))
3044 {
3045 if ( pDisplay->mCrOglCallbacks.pfnHasData
3046 && pDisplay->mCrOglCallbacks.pfnHasData())
3047 {
3048 /* submit */
3049 VBOXCRCMDCTL_HGCM *pData = &pDisplay->mCrOglScreenshotCtl;
3050
3051 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3052 pData->Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
3053
3054 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3055 pData->aParms[0].u.pointer.addr = &pDisplay->mCrOglScreenshotData;
3056 pData->aParms[0].u.pointer.size = sizeof(pDisplay->mCrOglScreenshotData);
3057 rc = pDisplay->i_crCtlSubmit(&pData->Hdr, sizeof(*pData), Display::i_displayVRecCompletion, pDisplay);
3058 if (RT_SUCCESS(rc))
3059 break;
3060 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
3061 }
3062
3063 /* no 3D data available, or error has occured,
3064 * go the straight way */
3065 ASMAtomicWriteU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_IDLE);
3066 }
3067 else
3068 {
3069 /* record request is still in progress, don't do anything */
3070 break;
3071 }
3072 }
3073# endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3074
3075 uint64_t u64Now = RTTimeProgramMilliTS();
3076 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3077 {
3078 if (!pDisplay->maVideoRecEnabled[uScreenId])
3079 continue;
3080
3081 if (VideoRecIsFull(pDisplay->mpVideoRecCtx, uScreenId, u64Now))
3082 {
3083 pDisplay->i_VideoCaptureStop();
3084 pDisplay->mParent->i_machine()->COMSETTER(VideoCaptureEnabled)(false);
3085 break;
3086 }
3087
3088 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3089
3090 if ( !pFBInfo->pFramebuffer.isNull()
3091 && !pFBInfo->fDisabled)
3092 {
3093 rc = VERR_NOT_SUPPORTED;
3094 if ( pFBInfo->fVBVAEnabled
3095 && pFBInfo->pu8FramebufferVRAM)
3096 {
3097 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
3098 BitmapFormat_BGR,
3099 pFBInfo->u16BitsPerPixel,
3100 pFBInfo->u32LineSize, pFBInfo->w, pFBInfo->h,
3101 pFBInfo->pu8FramebufferVRAM, u64Now);
3102 }
3103 else if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && pDrv->IConnector.pbData)
3104 {
3105 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
3106 BitmapFormat_BGR,
3107 pDrv->IConnector.cBits,
3108 pDrv->IConnector.cbScanline, pDrv->IConnector.cx,
3109 pDrv->IConnector.cy, pDrv->IConnector.pbData, u64Now);
3110 }
3111 if (rc == VINF_TRY_AGAIN)
3112 break;
3113 }
3114 }
3115 } while (0);
3116 }
3117#endif /* VBOX_WITH_VPX */
3118
3119#ifdef DEBUG_sunlover_2
3120 LogFlowFunc(("leave\n"));
3121#endif /* DEBUG_sunlover_2 */
3122}
3123
3124/**
3125 * Reset notification
3126 *
3127 * @see PDMIDISPLAYCONNECTOR::pfnReset
3128 */
3129DECLCALLBACK(void) Display::i_displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3130{
3131 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3132
3133 LogRelFlowFunc(("\n"));
3134
3135 /* Disable VBVA mode. */
3136 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3137}
3138
3139/**
3140 * LFBModeChange notification
3141 *
3142 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3143 */
3144DECLCALLBACK(void) Display::i_displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3145{
3146 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3147
3148 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
3149
3150 NOREF(fEnabled);
3151
3152 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3153 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3154}
3155
3156/**
3157 * Adapter information change notification.
3158 *
3159 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3160 */
3161DECLCALLBACK(void) Display::i_displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM,
3162 uint32_t u32VRAMSize)
3163{
3164 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3165 pDrv->pDisplay->processAdapterData(pvVRAM, u32VRAMSize);
3166}
3167
3168/**
3169 * Display information change notification.
3170 *
3171 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3172 */
3173DECLCALLBACK(void) Display::i_displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface,
3174 void *pvVRAM, unsigned uScreenId)
3175{
3176 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3177 pDrv->pDisplay->processDisplayData(pvVRAM, uScreenId);
3178}
3179
3180#ifdef VBOX_WITH_VIDEOHWACCEL
3181
3182#ifndef S_FALSE
3183# define S_FALSE ((HRESULT)1L)
3184#endif
3185
3186int Display::i_handleVHWACommandProcess(PVBOXVHWACMD pCommand)
3187{
3188 unsigned id = (unsigned)pCommand->iDisplay;
3189 int rc = VINF_SUCCESS;
3190 if (id >= mcMonitors)
3191 return VERR_INVALID_PARAMETER;
3192
3193 ComPtr<IFramebuffer> pFramebuffer;
3194 AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
3195 pFramebuffer = maFramebuffers[id].pFramebuffer;
3196 bool fVHWASupported = RT_BOOL(maFramebuffers[id].u32Caps & FramebufferCapabilities_VHWA);
3197 arlock.release();
3198
3199 if (pFramebuffer == NULL || !fVHWASupported)
3200 return VERR_NOT_IMPLEMENTED; /* Implementation is not available. */
3201
3202 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3203 if (hr == S_FALSE)
3204 return VINF_SUCCESS;
3205 else if (SUCCEEDED(hr))
3206 return VINF_CALLBACK_RETURN;
3207 else if (hr == E_ACCESSDENIED)
3208 return VERR_INVALID_STATE; /* notify we can not handle request atm */
3209 else if (hr == E_NOTIMPL)
3210 return VERR_NOT_IMPLEMENTED;
3211 return VERR_GENERAL_FAILURE;
3212}
3213
3214DECLCALLBACK(int) Display::i_displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3215{
3216 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3217
3218 return pDrv->pDisplay->i_handleVHWACommandProcess(pCommand);
3219}
3220#endif
3221
3222#ifdef VBOX_WITH_CRHGSMI
3223void Display::i_handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3224{
3225 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks,
3226 (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
3227}
3228
3229void Display::i_handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3230{
3231 PVBOXVDMACMD_CHROMIUM_CTL pCtl = (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr;
3232 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, pCtl, result);
3233}
3234
3235void Display::i_handleCrHgsmiCommandProcess(PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3236{
3237 int rc = VERR_NOT_SUPPORTED;
3238 VBOXHGCMSVCPARM parm;
3239 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3240 parm.u.pointer.addr = pCmd;
3241 parm.u.pointer.size = cbCmd;
3242
3243 if (mhCrOglSvc)
3244 {
3245 VMMDev *pVMMDev = mParent->i_getVMMDev();
3246 if (pVMMDev)
3247 {
3248 /* no completion callback is specified with this call,
3249 * the CrOgl code will complete the CrHgsmi command once it processes it */
3250 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
3251 AssertRC(rc);
3252 if (RT_SUCCESS(rc))
3253 return;
3254 }
3255 else
3256 rc = VERR_INVALID_STATE;
3257 }
3258
3259 /* we are here because something went wrong with command processing, complete it */
3260 i_handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
3261}
3262
3263void Display::i_handleCrHgsmiControlProcess(PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
3264{
3265 int rc = VERR_NOT_SUPPORTED;
3266 VBOXHGCMSVCPARM parm;
3267 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3268 parm.u.pointer.addr = pCtl;
3269 parm.u.pointer.size = cbCtl;
3270
3271 if (mhCrOglSvc)
3272 {
3273 VMMDev *pVMMDev = mParent->i_getVMMDev();
3274 if (pVMMDev)
3275 {
3276 bool fCheckPendingViewport = (pCtl->enmType == VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP);
3277 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm,
3278 Display::i_displayCrHgsmiControlCompletion, this);
3279 AssertRC(rc);
3280 if (RT_SUCCESS(rc))
3281 {
3282 if (fCheckPendingViewport)
3283 {
3284 ULONG ul;
3285 for (ul = 0; ul < mcMonitors; ul++)
3286 {
3287 DISPLAYFBINFO *pFb = &maFramebuffers[ul];
3288 if (!pFb->pendingViewportInfo.fPending)
3289 continue;
3290
3291 rc = i_crViewportNotify(ul, pFb->pendingViewportInfo.x, pFb->pendingViewportInfo.y,
3292 pFb->pendingViewportInfo.width, pFb->pendingViewportInfo.height);
3293 if (RT_SUCCESS(rc))
3294 pFb->pendingViewportInfo.fPending = false;
3295 else
3296 {
3297 AssertMsgFailed(("crViewportNotify failed (rc=%Rrc)\n", rc));
3298 rc = VINF_SUCCESS;
3299 }
3300 }
3301 }
3302 return;
3303 }
3304 }
3305 else
3306 rc = VERR_INVALID_STATE;
3307 }
3308
3309 /* we are here because something went wrong with command processing, complete it */
3310 i_handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
3311}
3312
3313DECLCALLBACK(void) Display::i_displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd,
3314 uint32_t cbCmd)
3315{
3316 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3317
3318 pDrv->pDisplay->i_handleCrHgsmiCommandProcess(pCmd, cbCmd);
3319}
3320
3321DECLCALLBACK(void) Display::i_displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd,
3322 uint32_t cbCmd)
3323{
3324 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3325
3326 pDrv->pDisplay->i_handleCrHgsmiControlProcess(pCmd, cbCmd);
3327}
3328
3329DECLCALLBACK(void) Display::i_displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3330 void *pvContext)
3331{
3332 AssertMsgFailed(("not expected!\n"));
3333 Display *pDisplay = (Display *)pvContext;
3334 pDisplay->i_handleCrHgsmiCommandCompletion(result, u32Function, pParam);
3335}
3336
3337DECLCALLBACK(void) Display::i_displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3338 void *pvContext)
3339{
3340 Display *pDisplay = (Display *)pvContext;
3341 pDisplay->i_handleCrHgsmiControlCompletion(result, u32Function, pParam);
3342
3343}
3344#endif
3345
3346#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3347DECLCALLBACK(void) Display::i_displayCrHgcmCtlSubmitCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3348 void *pvContext)
3349{
3350 VBOXCRCMDCTL *pCmd = (VBOXCRCMDCTL*)pParam->u.pointer.addr;
3351 if (pCmd->u.pfnInternal)
3352 ((PFNCRCTLCOMPLETION)pCmd->u.pfnInternal)(pCmd, pParam->u.pointer.size, result, pvContext);
3353}
3354
3355int Display::i_handleCrHgcmCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
3356 PFNCRCTLCOMPLETION pfnCompletion,
3357 void *pvCompletion)
3358{
3359 VMMDev *pVMMDev = mParent ? mParent->i_getVMMDev() : NULL;
3360 if (!pVMMDev)
3361 {
3362 AssertMsgFailed(("no vmmdev\n"));
3363 return VERR_INVALID_STATE;
3364 }
3365
3366 Assert(mhCrOglSvc);
3367 VBOXHGCMSVCPARM parm;
3368 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3369 parm.u.pointer.addr = pCmd;
3370 parm.u.pointer.size = cbCmd;
3371
3372 pCmd->u.pfnInternal = (void(*)())pfnCompletion;
3373 int rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CTL, &parm, i_displayCrHgcmCtlSubmitCompletion,
3374 pvCompletion);
3375 if (!RT_SUCCESS(rc))
3376 AssertMsgFailed(("hgcmHostFastCallAsync failed (rc=%Rrc)\n", rc));
3377
3378 return rc;
3379}
3380
3381DECLCALLBACK(int) Display::i_displayCrHgcmCtlSubmit(PPDMIDISPLAYCONNECTOR pInterface,
3382 struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
3383 PFNCRCTLCOMPLETION pfnCompletion,
3384 void *pvCompletion)
3385{
3386 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3387 Display *pThis = pDrv->pDisplay;
3388 return pThis->i_handleCrHgcmCtlSubmit(pCmd, cbCmd, pfnCompletion, pvCompletion);
3389}
3390
3391int Display::i_crCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, PFNCRCTLCOMPLETION pfnCompletion, void *pvCompletion)
3392{
3393 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3394 if (RT_SUCCESS(rc))
3395 {
3396 if (mhCrOglSvc)
3397 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmit(mpDrv->pVBVACallbacks, pCmd, cbCmd, pfnCompletion, pvCompletion);
3398 else
3399 rc = VERR_NOT_SUPPORTED;
3400
3401 RTCritSectRwLeaveShared(&mCrOglLock);
3402 }
3403 return rc;
3404}
3405
3406int Display::i_crCtlSubmitSync(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3407{
3408 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3409 if (RT_SUCCESS(rc))
3410 {
3411 if (mhCrOglSvc)
3412 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmitSync(mpDrv->pVBVACallbacks, pCmd, cbCmd);
3413 else
3414 rc = VERR_NOT_SUPPORTED;
3415
3416 RTCritSectRwLeaveShared(&mCrOglLock);
3417 }
3418 return rc;
3419}
3420
3421int Display::i_crCtlSubmitAsyncCmdCopy(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3422{
3423 VBOXCRCMDCTL* pCmdCopy = (VBOXCRCMDCTL*)RTMemAlloc(cbCmd);
3424 if (!pCmdCopy)
3425 {
3426 LogRel(("RTMemAlloc failed\n"));
3427 return VERR_NO_MEMORY;
3428 }
3429
3430 memcpy(pCmdCopy, pCmd, cbCmd);
3431
3432 int rc = i_crCtlSubmit(pCmdCopy, cbCmd, i_displayCrCmdFree, pCmdCopy);
3433 if (RT_FAILURE(rc))
3434 {
3435 LogRel(("crCtlSubmit failed (rc=%Rrc)\n", rc));
3436 RTMemFree(pCmdCopy);
3437 return rc;
3438 }
3439
3440 return VINF_SUCCESS;
3441}
3442
3443int Display::i_crCtlSubmitSyncIfHasDataForScreen(uint32_t u32ScreenID, struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3444{
3445 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3446 AssertRCReturn(rc, rc);
3447
3448 if ( mCrOglCallbacks.pfnHasDataForScreen
3449 && mCrOglCallbacks.pfnHasDataForScreen(u32ScreenID))
3450 rc = i_crCtlSubmitSync(pCmd, cbCmd);
3451 else
3452 rc = i_crCtlSubmitAsyncCmdCopy(pCmd, cbCmd);
3453
3454 RTCritSectRwLeaveShared(&mCrOglLock);
3455
3456 return rc;
3457}
3458
3459bool Display::i_handleCrVRecScreenshotBegin(uint32_t uScreen, uint64_t u64TimeStamp)
3460{
3461# if VBOX_WITH_VPX
3462 return VideoRecIsReady(mpVideoRecCtx, uScreen, u64TimeStamp);
3463# else
3464 return false;
3465# endif
3466}
3467
3468void Display::i_handleCrVRecScreenshotEnd(uint32_t uScreen, uint64_t u64TimeStamp)
3469{
3470}
3471
3472void Display::i_handleCrVRecScreenshotPerform(uint32_t uScreen,
3473 uint32_t x, uint32_t y, uint32_t uPixelFormat,
3474 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
3475 uint32_t uGuestWidth, uint32_t uGuestHeight,
3476 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
3477{
3478 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
3479# if VBOX_WITH_VPX
3480 int rc = VideoRecCopyToIntBuf(mpVideoRecCtx, uScreen, x, y,
3481 uPixelFormat,
3482 uBitsPerPixel, uBytesPerLine,
3483 uGuestWidth, uGuestHeight,
3484 pu8BufferAddress, u64TimeStamp);
3485 Assert(rc == VINF_SUCCESS /* || rc == VERR_TRY_AGAIN || rc == VINF_TRY_AGAIN*/);
3486# endif
3487}
3488
3489void Display::i_handleVRecCompletion()
3490{
3491 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
3492 ASMAtomicWriteU32(&mfCrOglVideoRecState, CRVREC_STATE_IDLE);
3493}
3494
3495#endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3496
3497HRESULT Display::notifyScaleFactorChange(ULONG aScreenId, ULONG aScaleFactorWMultiplied, ULONG aScaleFactorHMultiplied)
3498{
3499#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3500 HRESULT hr = E_UNEXPECTED;
3501
3502 if (aScreenId >= mcMonitors)
3503 return E_INVALIDARG;
3504
3505 /* 3D acceleration enabled in VM config. */
3506 if (mfIsCr3DEnabled)
3507 {
3508 /* VBoxSharedCrOpenGL HGCM host service is running. */
3509 if (mhCrOglSvc)
3510 {
3511 VMMDev *pVMMDev = mParent->i_getVMMDev();
3512 if (pVMMDev)
3513 {
3514 VBOXCRCMDCTL_HGCM *pCtl;
3515 pCtl = (VBOXCRCMDCTL_HGCM *)RTMemAlloc(sizeof(CRVBOXHGCMSETSCALEFACTOR) + sizeof(VBOXCRCMDCTL_HGCM));
3516 if (pCtl)
3517 {
3518 CRVBOXHGCMSETSCALEFACTOR *pData = (CRVBOXHGCMSETSCALEFACTOR *)(pCtl + 1);
3519 int rc;
3520
3521 pData->u32Screen = aScreenId;
3522 pData->u32ScaleFactorWMultiplied = aScaleFactorWMultiplied;
3523 pData->u32ScaleFactorHMultiplied = aScaleFactorHMultiplied;
3524
3525 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3526 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_SCALE_FACTOR;
3527 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3528 pCtl->aParms[0].u.pointer.addr = pData;
3529 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
3530
3531 rc = i_crCtlSubmitSync(&pCtl->Hdr, sizeof(*pCtl));
3532 if (RT_FAILURE(rc))
3533 AssertMsgFailed(("crCtlSubmitSync failed (rc=%Rrc)\n", rc));
3534 else
3535 hr = S_OK;
3536
3537 RTMemFree(pCtl);
3538 }
3539 else
3540 {
3541 LogRel(("Running out of memory on attempt to set OpenGL content scale factor. Ignored.\n"));
3542 hr = E_OUTOFMEMORY;
3543 }
3544 }
3545 else
3546 LogRel(("Internal error occurred on attempt to set OpenGL content scale factor. Ignored.\n"));
3547 }
3548 else
3549 LogRel(("Attempt to specify OpenGL content scale factor while corresponding HGCM host service not yet runing. Ignored.\n"));
3550 }
3551 else
3552# if 0 /** @todo Thank you so very much from anyone using VMSVGA3d! */
3553 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while 3D acceleration is disabled in VM config. Ignored.\n"));
3554# else
3555 {
3556 hr = S_OK;
3557 /* Need an interface like this here (and the #ifdefs needs adjusting):
3558 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3559 if (pUpPort && pUpPort->pfnSetScaleFactor)
3560 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3561 }
3562# endif
3563
3564 return hr;
3565#else
3566 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while corresponding functionality is disabled."));
3567 return E_UNEXPECTED;
3568#endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3569}
3570
3571HRESULT Display::notifyHiDPIOutputPolicyChange(BOOL fUnscaledHiDPI)
3572{
3573#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3574 HRESULT hr = E_UNEXPECTED;
3575
3576 /* 3D acceleration enabled in VM config. */
3577 if (mfIsCr3DEnabled)
3578 {
3579 /* VBoxSharedCrOpenGL HGCM host service is running. */
3580 if (mhCrOglSvc)
3581 {
3582 VMMDev *pVMMDev = mParent->i_getVMMDev();
3583 if (pVMMDev)
3584 {
3585 VBOXCRCMDCTL_HGCM *pCtl;
3586 pCtl = (VBOXCRCMDCTL_HGCM *)RTMemAlloc(sizeof(CRVBOXHGCMSETUNSCALEDHIDPIOUTPUT) + sizeof(VBOXCRCMDCTL_HGCM));
3587 if (pCtl)
3588 {
3589 CRVBOXHGCMSETUNSCALEDHIDPIOUTPUT *pData = (CRVBOXHGCMSETUNSCALEDHIDPIOUTPUT *)(pCtl + 1);
3590 int rc;
3591
3592 pData->fUnscaledHiDPI = RT_BOOL(fUnscaledHiDPI);
3593
3594 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3595 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_UNSCALED_HIDPI;
3596 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3597 pCtl->aParms[0].u.pointer.addr = pData;
3598 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
3599
3600 rc = i_crCtlSubmitSync(&pCtl->Hdr, sizeof(*pCtl));
3601 if (RT_FAILURE(rc))
3602 AssertMsgFailed(("crCtlSubmitSync failed (rc=%Rrc)\n", rc));
3603 else
3604 hr = S_OK;
3605
3606 RTMemFree(pCtl);
3607 }
3608 else
3609 {
3610 LogRel(("Running out of memory on attempt to notify OpenGL about HiDPI output scaling policy change. Ignored.\n"));
3611 hr = E_OUTOFMEMORY;
3612 }
3613 }
3614 else
3615 LogRel(("Internal error occurred on attempt to notify OpenGL about HiDPI output scaling policy change. Ignored.\n"));
3616 }
3617 else
3618 LogRel(("Attempt to notify OpenGL about HiDPI output scaling policy change while corresponding HGCM host service not yet runing. Ignored.\n"));
3619 }
3620 else
3621 {
3622 hr = S_OK;
3623 /* Need an interface like this here (and the #ifdefs needs adjusting):
3624 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3625 if (pUpPort && pUpPort->pfnSetScaleFactor)
3626 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3627 }
3628
3629 return hr;
3630#else
3631 AssertMsgFailed(("Attempt to notify OpenGL about HiDPI output scaling policy change while corresponding functionality is disabled."));
3632 return E_UNEXPECTED;
3633#endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3634}
3635
3636#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3637DECLCALLBACK(void) Display::i_displayCrVRecScreenshotPerform(void *pvCtx, uint32_t uScreen,
3638 uint32_t x, uint32_t y,
3639 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
3640 uint32_t uGuestWidth, uint32_t uGuestHeight,
3641 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
3642{
3643 Display *pDisplay = (Display *)pvCtx;
3644 pDisplay->i_handleCrVRecScreenshotPerform(uScreen,
3645 x, y, BitmapFormat_BGR, uBitsPerPixel,
3646 uBytesPerLine, uGuestWidth, uGuestHeight,
3647 pu8BufferAddress, u64TimeStamp);
3648}
3649
3650DECLCALLBACK(bool) Display::i_displayCrVRecScreenshotBegin(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
3651{
3652 Display *pDisplay = (Display *)pvCtx;
3653 return pDisplay->i_handleCrVRecScreenshotBegin(uScreen, u64TimeStamp);
3654}
3655
3656DECLCALLBACK(void) Display::i_displayCrVRecScreenshotEnd(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
3657{
3658 Display *pDisplay = (Display *)pvCtx;
3659 pDisplay->i_handleCrVRecScreenshotEnd(uScreen, u64TimeStamp);
3660}
3661
3662DECLCALLBACK(void) Display::i_displayVRecCompletion(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, int rc, void *pvCompletion)
3663{
3664 Display *pDisplay = (Display *)pvCompletion;
3665 pDisplay->i_handleVRecCompletion();
3666}
3667
3668#endif
3669
3670
3671#ifdef VBOX_WITH_HGSMI
3672DECLCALLBACK(int) Display::i_displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags,
3673 bool fRenderThreadMode)
3674{
3675 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3676
3677 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3678 Display *pThis = pDrv->pDisplay;
3679
3680 if (pThis->maFramebuffers[uScreenId].fVBVAEnabled && pThis->maFramebuffers[uScreenId].fRenderThreadMode != fRenderThreadMode)
3681 {
3682 LogRel(("Enabling different vbva mode\n"));
3683#ifdef DEBUG_misha
3684 AssertMsgFailed(("enabling different vbva mode\n"));
3685#endif
3686 return VERR_INVALID_STATE;
3687 }
3688
3689 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3690 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3691 pThis->maFramebuffers[uScreenId].fRenderThreadMode = fRenderThreadMode;
3692 pThis->maFramebuffers[uScreenId].fVBVAForceResize = true;
3693
3694 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3695
3696 return VINF_SUCCESS;
3697}
3698
3699DECLCALLBACK(void) Display::i_displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3700{
3701 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3702
3703 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3704 Display *pThis = pDrv->pDisplay;
3705
3706 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3707
3708 bool fRenderThreadMode = pFBInfo->fRenderThreadMode;
3709
3710 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3711 {
3712 /* Make sure that the primary screen is visible now.
3713 * The guest can't use VBVA anymore, so only only the VGA device output works.
3714 */
3715 if (pFBInfo->fDisabled)
3716 {
3717 pFBInfo->fDisabled = false;
3718 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3719 GuestMonitorChangedEventType_Enabled,
3720 uScreenId,
3721 pFBInfo->xOrigin, pFBInfo->yOrigin,
3722 pFBInfo->w, pFBInfo->h);
3723 }
3724 }
3725
3726 pFBInfo->fVBVAEnabled = false;
3727 pFBInfo->fVBVAForceResize = false;
3728 pFBInfo->fRenderThreadMode = false;
3729
3730 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3731
3732 pFBInfo->pVBVAHostFlags = NULL;
3733
3734 if (!fRenderThreadMode && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3735 {
3736 /* Force full screen update, because VGA device must take control, do resize, etc. */
3737 pThis->mpDrv->pUpPort->pfnUpdateDisplayAll(pThis->mpDrv->pUpPort, /* fFailOnResize = */ false);
3738 }
3739}
3740
3741DECLCALLBACK(void) Display::i_displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3742{
3743 LogFlowFunc(("uScreenId %d\n", uScreenId));
3744
3745 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3746 Display *pThis = pDrv->pDisplay;
3747 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3748
3749 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3750 {
3751 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers,
3752 pThis->mcMonitors);
3753 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3754 }
3755}
3756
3757DECLCALLBACK(void) Display::i_displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
3758 const PVBVACMDHDR pCmd, size_t cbCmd)
3759{
3760 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3761
3762 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3763 Display *pThis = pDrv->pDisplay;
3764 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3765
3766 if (pFBInfo->fDefaultFormat)
3767 {
3768 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3769 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3770 && !pFBInfo->fDisabled)
3771 {
3772 pDrv->pUpPort->pfnUpdateDisplayRect(pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
3773 }
3774 else if ( !pFBInfo->pSourceBitmap.isNull()
3775 && !pFBInfo->fDisabled)
3776 {
3777 /* Render VRAM content to the framebuffer. */
3778 BYTE *pAddress = NULL;
3779 ULONG ulWidth = 0;
3780 ULONG ulHeight = 0;
3781 ULONG ulBitsPerPixel = 0;
3782 ULONG ulBytesPerLine = 0;
3783 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
3784
3785 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
3786 &ulWidth,
3787 &ulHeight,
3788 &ulBitsPerPixel,
3789 &ulBytesPerLine,
3790 &bitmapFormat);
3791 if (SUCCEEDED(hrc))
3792 {
3793 uint32_t width = pCmd->w;
3794 uint32_t height = pCmd->h;
3795
3796 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3797 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
3798 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
3799 uint32_t u32SrcWidth = pFBInfo->w;
3800 uint32_t u32SrcHeight = pFBInfo->h;
3801 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3802 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3803
3804 uint8_t *pu8Dst = pAddress;
3805 int32_t xDst = xSrc;
3806 int32_t yDst = ySrc;
3807 uint32_t u32DstWidth = u32SrcWidth;
3808 uint32_t u32DstHeight = u32SrcHeight;
3809 uint32_t u32DstLineSize = u32DstWidth * 4;
3810 uint32_t u32DstBitsPerPixel = 32;
3811
3812 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3813 width, height,
3814 pu8Src,
3815 xSrc, ySrc,
3816 u32SrcWidth, u32SrcHeight,
3817 u32SrcLineSize, u32SrcBitsPerPixel,
3818 pu8Dst,
3819 xDst, yDst,
3820 u32DstWidth, u32DstHeight,
3821 u32DstLineSize, u32DstBitsPerPixel);
3822 }
3823 }
3824 }
3825
3826 VBVACMDHDR hdrSaved = *pCmd;
3827
3828 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3829
3830 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3831 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3832
3833 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
3834 pThis->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, pCmd, (uint32_t)cbCmd);
3835
3836 *pHdrUnconst = hdrSaved;
3837}
3838
3839DECLCALLBACK(void) Display::i_displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y,
3840 uint32_t cx, uint32_t cy)
3841{
3842 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3843
3844 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3845 Display *pThis = pDrv->pDisplay;
3846 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3847
3848 /* @todo handleFramebufferUpdate (uScreenId,
3849 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3850 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3851 * cx, cy);
3852 */
3853 pThis->i_handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3854}
3855
3856#ifdef DEBUG_sunlover
3857static void logVBVAResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
3858{
3859 LogRel(("displayVBVAResize: [%d] %s\n"
3860 " pView->u32ViewIndex %d\n"
3861 " pView->u32ViewOffset 0x%08X\n"
3862 " pView->u32ViewSize 0x%08X\n"
3863 " pView->u32MaxScreenSize 0x%08X\n"
3864 " pScreen->i32OriginX %d\n"
3865 " pScreen->i32OriginY %d\n"
3866 " pScreen->u32StartOffset 0x%08X\n"
3867 " pScreen->u32LineSize 0x%08X\n"
3868 " pScreen->u32Width %d\n"
3869 " pScreen->u32Height %d\n"
3870 " pScreen->u16BitsPerPixel %d\n"
3871 " pScreen->u16Flags 0x%04X\n"
3872 " pFBInfo->u32Offset 0x%08X\n"
3873 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
3874 " pFBInfo->u32InformationSize 0x%08X\n"
3875 " pFBInfo->fDisabled %d\n"
3876 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
3877 " pFBInfo->u16BitsPerPixel %d\n"
3878 " pFBInfo->pu8FramebufferVRAM %p\n"
3879 " pFBInfo->u32LineSize 0x%08X\n"
3880 " pFBInfo->flags 0x%04X\n"
3881 " pFBInfo->pHostEvents %p\n"
3882 " pFBInfo->fDefaultFormat %d\n"
3883 " pFBInfo->fVBVAEnabled %d\n"
3884 " pFBInfo->fVBVAForceResize %d\n"
3885 " pFBInfo->pVBVAHostFlags %p\n"
3886 "",
3887 pScreen->u32ViewIndex,
3888 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
3889 pView->u32ViewIndex,
3890 pView->u32ViewOffset,
3891 pView->u32ViewSize,
3892 pView->u32MaxScreenSize,
3893 pScreen->i32OriginX,
3894 pScreen->i32OriginY,
3895 pScreen->u32StartOffset,
3896 pScreen->u32LineSize,
3897 pScreen->u32Width,
3898 pScreen->u32Height,
3899 pScreen->u16BitsPerPixel,
3900 pScreen->u16Flags,
3901 pFBInfo->u32Offset,
3902 pFBInfo->u32MaxFramebufferSize,
3903 pFBInfo->u32InformationSize,
3904 pFBInfo->fDisabled,
3905 pFBInfo->xOrigin,
3906 pFBInfo->yOrigin,
3907 pFBInfo->w,
3908 pFBInfo->h,
3909 pFBInfo->u16BitsPerPixel,
3910 pFBInfo->pu8FramebufferVRAM,
3911 pFBInfo->u32LineSize,
3912 pFBInfo->flags,
3913 pFBInfo->pHostEvents,
3914 pFBInfo->fDefaultFormat,
3915 pFBInfo->fVBVAEnabled,
3916 pFBInfo->fVBVAForceResize,
3917 pFBInfo->pVBVAHostFlags
3918 ));
3919}
3920#endif /* DEBUG_sunlover */
3921
3922DECLCALLBACK(int) Display::i_displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView,
3923 const PVBVAINFOSCREEN pScreen, void *pvVRAM)
3924{
3925 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3926
3927 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3928 Display *pThis = pDrv->pDisplay;
3929
3930 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
3931
3932 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
3933 {
3934 pThis->i_notifyCroglResize(pView, pScreen, pvVRAM);
3935
3936 pFBInfo->fDisabled = true;
3937 pFBInfo->flags = pScreen->u16Flags;
3938
3939 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
3940 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
3941 * the VM window will be black. */
3942 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
3943 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
3944 pThis->i_handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
3945 u32Width, u32Height, pScreen->u16Flags);
3946
3947 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3948 GuestMonitorChangedEventType_Disabled,
3949 pScreen->u32ViewIndex,
3950 0, 0, 0, 0);
3951 return VINF_SUCCESS;
3952 }
3953
3954 /* If display was disabled or there is no framebuffer, a resize will be required,
3955 * because the framebuffer was/will be changed.
3956 */
3957 bool fResize = pFBInfo->fDisabled || pFBInfo->pFramebuffer.isNull();
3958
3959 if (pFBInfo->fVBVAForceResize)
3960 {
3961 /* VBVA was just enabled. Do the resize. */
3962 fResize = true;
3963 pFBInfo->fVBVAForceResize = false;
3964 }
3965
3966 /* If the screen if blanked, then do a resize request to make sure that the framebuffer
3967 * switches to the default format.
3968 */
3969 fResize = fResize || RT_BOOL((pScreen->u16Flags ^ pFBInfo->flags) & VBVA_SCREEN_F_BLANK);
3970
3971 /* Check if this is a real resize or a notification about the screen origin.
3972 * The guest uses this VBVAResize call for both.
3973 */
3974 fResize = fResize
3975 || pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
3976 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
3977 || pFBInfo->u32LineSize != pScreen->u32LineSize
3978 || pFBInfo->w != pScreen->u32Width
3979 || pFBInfo->h != pScreen->u32Height;
3980
3981 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
3982 || pFBInfo->yOrigin != pScreen->i32OriginY;
3983
3984 if (fNewOrigin || fResize)
3985 pThis->i_notifyCroglResize(pView, pScreen, pvVRAM);
3986
3987 if (pFBInfo->fDisabled)
3988 {
3989 pFBInfo->fDisabled = false;
3990 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3991 GuestMonitorChangedEventType_Enabled,
3992 pScreen->u32ViewIndex,
3993 pScreen->i32OriginX, pScreen->i32OriginY,
3994 pScreen->u32Width, pScreen->u32Height);
3995 /* Continue to update pFBInfo. */
3996 }
3997
3998 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
3999 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
4000 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
4001
4002 pFBInfo->xOrigin = pScreen->i32OriginX;
4003 pFBInfo->yOrigin = pScreen->i32OriginY;
4004
4005 pFBInfo->w = pScreen->u32Width;
4006 pFBInfo->h = pScreen->u32Height;
4007
4008 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
4009 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
4010 pFBInfo->u32LineSize = pScreen->u32LineSize;
4011
4012 pFBInfo->flags = pScreen->u16Flags;
4013
4014 pThis->xInputMappingOrigin = 0;
4015 pThis->yInputMappingOrigin = 0;
4016 pThis->cxInputMapping = 0;
4017 pThis->cyInputMapping = 0;
4018
4019 if (fNewOrigin)
4020 {
4021 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
4022 GuestMonitorChangedEventType_NewOrigin,
4023 pScreen->u32ViewIndex,
4024 pScreen->i32OriginX, pScreen->i32OriginY,
4025 0, 0);
4026 }
4027
4028 if (!fResize)
4029 {
4030 /* No parameters of the framebuffer have actually changed. */
4031 if (fNewOrigin)
4032 {
4033 /* VRDP server still need this notification. */
4034 LogRelFlowFunc(("Calling VRDP\n"));
4035 pThis->mParent->i_consoleVRDPServer()->SendResize();
4036 }
4037 return VINF_SUCCESS;
4038 }
4039
4040 /* Do a regular resize. */
4041 return pThis->i_handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
4042 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
4043 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
4044}
4045
4046DECLCALLBACK(int) Display::i_displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
4047 uint32_t xHot, uint32_t yHot,
4048 uint32_t cx, uint32_t cy,
4049 const void *pvShape)
4050{
4051 LogFlowFunc(("\n"));
4052
4053 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4054 Display *pThis = pDrv->pDisplay;
4055
4056 uint32_t cbShape = 0;
4057 if (pvShape)
4058 {
4059 cbShape = (cx + 7) / 8 * cy; /* size of the AND mask */
4060 cbShape = ((cbShape + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
4061 }
4062
4063 /* Tell the console about it */
4064 pDrv->pDisplay->mParent->i_onMousePointerShapeChange(fVisible, fAlpha,
4065 xHot, yHot, cx, cy, (uint8_t *)pvShape, cbShape);
4066
4067 return VINF_SUCCESS;
4068}
4069
4070DECLCALLBACK(void) Display::i_displayVBVAGuestCapabilityUpdate(PPDMIDISPLAYCONNECTOR pInterface, uint32_t fCapabilities)
4071{
4072 LogFlowFunc(("\n"));
4073
4074 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4075 Display *pThis = pDrv->pDisplay;
4076
4077 pThis->i_handleUpdateGuestVBVACapabilities(fCapabilities);
4078}
4079
4080DECLCALLBACK(void) Display::i_displayVBVAInputMappingUpdate(PPDMIDISPLAYCONNECTOR pInterface, int32_t xOrigin, int32_t yOrigin,
4081 uint32_t cx, uint32_t cy)
4082{
4083 LogFlowFunc(("\n"));
4084
4085 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4086 Display *pThis = pDrv->pDisplay;
4087
4088 pThis->i_handleUpdateVBVAInputMapping(xOrigin, yOrigin, cx, cy);
4089}
4090
4091#endif /* VBOX_WITH_HGSMI */
4092
4093/**
4094 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4095 */
4096DECLCALLBACK(void *) Display::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
4097{
4098 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
4099 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4100 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
4101 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
4102 return NULL;
4103}
4104
4105
4106/**
4107 * Destruct a display driver instance.
4108 *
4109 * @returns VBox status.
4110 * @param pDrvIns The driver instance data.
4111 */
4112DECLCALLBACK(void) Display::i_drvDestruct(PPDMDRVINS pDrvIns)
4113{
4114 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4115 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4116 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4117
4118 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
4119
4120 pThis->IConnector.pbData = NULL;
4121 pThis->IConnector.cbScanline = 0;
4122 pThis->IConnector.cBits = 32;
4123 pThis->IConnector.cx = 0;
4124 pThis->IConnector.cy = 0;
4125
4126 if (pThis->pDisplay)
4127 {
4128 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
4129#ifdef VBOX_WITH_VPX
4130 pThis->pDisplay->i_VideoCaptureStop();
4131#endif
4132#ifdef VBOX_WITH_CRHGSMI
4133 pThis->pDisplay->i_destructCrHgsmiData();
4134#endif
4135 pThis->pDisplay->mpDrv = NULL;
4136 pThis->pDisplay->mpVMMDev = NULL;
4137 }
4138}
4139
4140
4141/**
4142 * Construct a display driver instance.
4143 *
4144 * @copydoc FNPDMDRVCONSTRUCT
4145 */
4146DECLCALLBACK(int) Display::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
4147{
4148 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
4149 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4150 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4151
4152 /*
4153 * Validate configuration.
4154 */
4155 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
4156 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
4157 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
4158 ("Configuration error: Not possible to attach anything to this driver!\n"),
4159 VERR_PDM_DRVINS_NO_ATTACH);
4160
4161 /*
4162 * Init Interfaces.
4163 */
4164 pDrvIns->IBase.pfnQueryInterface = Display::i_drvQueryInterface;
4165
4166 pThis->IConnector.pfnResize = Display::i_displayResizeCallback;
4167 pThis->IConnector.pfnUpdateRect = Display::i_displayUpdateCallback;
4168 pThis->IConnector.pfnRefresh = Display::i_displayRefreshCallback;
4169 pThis->IConnector.pfnReset = Display::i_displayResetCallback;
4170 pThis->IConnector.pfnLFBModeChange = Display::i_displayLFBModeChangeCallback;
4171 pThis->IConnector.pfnProcessAdapterData = Display::i_displayProcessAdapterDataCallback;
4172 pThis->IConnector.pfnProcessDisplayData = Display::i_displayProcessDisplayDataCallback;
4173#ifdef VBOX_WITH_VIDEOHWACCEL
4174 pThis->IConnector.pfnVHWACommandProcess = Display::i_displayVHWACommandProcess;
4175#endif
4176#ifdef VBOX_WITH_CRHGSMI
4177 pThis->IConnector.pfnCrHgsmiCommandProcess = Display::i_displayCrHgsmiCommandProcess;
4178 pThis->IConnector.pfnCrHgsmiControlProcess = Display::i_displayCrHgsmiControlProcess;
4179#endif
4180#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4181 pThis->IConnector.pfnCrHgcmCtlSubmit = Display::i_displayCrHgcmCtlSubmit;
4182#endif
4183#ifdef VBOX_WITH_HGSMI
4184 pThis->IConnector.pfnVBVAEnable = Display::i_displayVBVAEnable;
4185 pThis->IConnector.pfnVBVADisable = Display::i_displayVBVADisable;
4186 pThis->IConnector.pfnVBVAUpdateBegin = Display::i_displayVBVAUpdateBegin;
4187 pThis->IConnector.pfnVBVAUpdateProcess = Display::i_displayVBVAUpdateProcess;
4188 pThis->IConnector.pfnVBVAUpdateEnd = Display::i_displayVBVAUpdateEnd;
4189 pThis->IConnector.pfnVBVAResize = Display::i_displayVBVAResize;
4190 pThis->IConnector.pfnVBVAMousePointerShape = Display::i_displayVBVAMousePointerShape;
4191 pThis->IConnector.pfnVBVAGuestCapabilityUpdate = Display::i_displayVBVAGuestCapabilityUpdate;
4192 pThis->IConnector.pfnVBVAInputMappingUpdate = Display::i_displayVBVAInputMappingUpdate;
4193#endif
4194
4195 /*
4196 * Get the IDisplayPort interface of the above driver/device.
4197 */
4198 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
4199 if (!pThis->pUpPort)
4200 {
4201 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
4202 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4203 }
4204#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
4205 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
4206 if (!pThis->pVBVACallbacks)
4207 {
4208 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
4209 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4210 }
4211#endif
4212 /*
4213 * Get the Display object pointer and update the mpDrv member.
4214 */
4215 void *pv;
4216 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
4217 if (RT_FAILURE(rc))
4218 {
4219 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
4220 return rc;
4221 }
4222 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
4223 pThis->pDisplay = pDisplay;
4224 pThis->pDisplay->mpDrv = pThis;
4225
4226 /* Disable VRAM to a buffer copy initially. */
4227 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
4228 pThis->IConnector.cBits = 32; /* DevVGA does nothing otherwise. */
4229
4230 /*
4231 * Start periodic screen refreshes
4232 */
4233 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
4234
4235#ifdef VBOX_WITH_CRHGSMI
4236 pDisplay->i_setupCrHgsmiData();
4237#endif
4238
4239#ifdef VBOX_WITH_VPX
4240 ComPtr<IMachine> pMachine = pDisplay->mParent->i_machine();
4241 BOOL fEnabled = false;
4242 HRESULT hrc = pMachine->COMGETTER(VideoCaptureEnabled)(&fEnabled);
4243 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
4244 if (fEnabled)
4245 {
4246 rc = pDisplay->i_VideoCaptureStart();
4247 fireVideoCaptureChangedEvent(pDisplay->mParent->i_getEventSource());
4248 }
4249#endif
4250
4251 return rc;
4252}
4253
4254
4255/**
4256 * Display driver registration record.
4257 */
4258const PDMDRVREG Display::DrvReg =
4259{
4260 /* u32Version */
4261 PDM_DRVREG_VERSION,
4262 /* szName */
4263 "MainDisplay",
4264 /* szRCMod */
4265 "",
4266 /* szR0Mod */
4267 "",
4268 /* pszDescription */
4269 "Main display driver (Main as in the API).",
4270 /* fFlags */
4271 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
4272 /* fClass. */
4273 PDM_DRVREG_CLASS_DISPLAY,
4274 /* cMaxInstances */
4275 ~0U,
4276 /* cbInstance */
4277 sizeof(DRVMAINDISPLAY),
4278 /* pfnConstruct */
4279 Display::i_drvConstruct,
4280 /* pfnDestruct */
4281 Display::i_drvDestruct,
4282 /* pfnRelocate */
4283 NULL,
4284 /* pfnIOCtl */
4285 NULL,
4286 /* pfnPowerOn */
4287 NULL,
4288 /* pfnReset */
4289 NULL,
4290 /* pfnSuspend */
4291 NULL,
4292 /* pfnResume */
4293 NULL,
4294 /* pfnAttach */
4295 NULL,
4296 /* pfnDetach */
4297 NULL,
4298 /* pfnPowerOff */
4299 NULL,
4300 /* pfnSoftReset */
4301 NULL,
4302 /* u32EndVersion */
4303 PDM_DRVREG_VERSION
4304};
4305
4306/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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