VirtualBox

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

Last change on this file since 56623 was 56468, checked in by vboxsync, 10 years ago

Main/Display: update seamless rectangles if window origin changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 149.1 KB
Line 
1/* $Id: DisplayImpl.cpp 56468 2015-06-17 07:12:16Z 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 *pu8Data, 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 = pu8Data;
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 /* Query RGB bitmap. */
306 uint8_t *pu8Data = NULL;
307 size_t cbData = 0;
308 uint32_t cx = 0;
309 uint32_t cy = 0;
310
311#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
312 BOOL f3DSnapshot = FALSE;
313 if ( that->mfIsCr3DEnabled
314 && that->mCrOglCallbacks.pfnHasData
315 && that->mCrOglCallbacks.pfnHasData())
316 {
317 VMMDev *pVMMDev = that->mParent->i_getVMMDev();
318 if (pVMMDev)
319 {
320 VBOX_DISPLAY_SAVESCREENSHOT_DATA *pScreenshot =
321 (VBOX_DISPLAY_SAVESCREENSHOT_DATA*)RTMemAllocZ(sizeof(*pScreenshot));
322 if (pScreenshot)
323 {
324 /* screen id or CRSCREEN_ALL to specify all enabled */
325 pScreenshot->Base.u32Screen = 0;
326 pScreenshot->Base.u32Width = 0;
327 pScreenshot->Base.u32Height = 0;
328 pScreenshot->Base.u32Pitch = 0;
329 pScreenshot->Base.pvBuffer = NULL;
330 pScreenshot->Base.pvContext = pScreenshot;
331 pScreenshot->Base.pfnScreenshotBegin = NULL;
332 pScreenshot->Base.pfnScreenshotPerform = displaySaveScreenshotReport;
333 pScreenshot->Base.pfnScreenshotEnd = NULL;
334
335 VBOXCRCMDCTL_HGCM data;
336 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
337 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
338
339 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
340 data.aParms[0].u.pointer.addr = &pScreenshot->Base;
341 data.aParms[0].u.pointer.size = sizeof(pScreenshot->Base);
342
343 int rc = that->i_crCtlSubmitSync(&data.Hdr, sizeof(data));
344 if (RT_SUCCESS(rc))
345 {
346 if (pScreenshot->pu8PNG)
347 {
348 pu8Thumbnail = pScreenshot->pu8Thumbnail;
349 cbThumbnail = pScreenshot->cbThumbnail;
350 cxThumbnail = pScreenshot->cxThumbnail;
351 cyThumbnail = pScreenshot->cyThumbnail;
352
353 /* PNG screenshot. */
354 pu8PNG = pScreenshot->pu8PNG;
355 cbPNG = pScreenshot->cbPNG;
356 cxPNG = pScreenshot->cxPNG;
357 cyPNG = pScreenshot->cyPNG;
358 f3DSnapshot = TRUE;
359 }
360 else
361 AssertMsgFailed(("no png\n"));
362 }
363 else
364 AssertMsgFailed(("SHCRGL_HOST_FN_TAKE_SCREENSHOT failed (rc=%Rrc)\n", rc));
365
366
367 RTMemFree(pScreenshot);
368 }
369 }
370 }
371
372 if (!f3DSnapshot)
373#endif
374 {
375 /* SSM code is executed on EMT(0), therefore no need to use VMR3ReqCallWait. */
376 int rc = Display::i_displayTakeScreenshotEMT(that, VBOX_VIDEO_PRIMARY_SCREEN, &pu8Data, &cbData, &cx, &cy);
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) && pu8Data)
383 {
384 Assert(cx && cy);
385
386 /* Prepare a small thumbnail and a PNG screenshot. */
387 displayMakeThumbnail(pu8Data, cx, cy, &pu8Thumbnail, &cbThumbnail, &cxThumbnail, &cyThumbnail);
388 rc = DisplayMakePNG(pu8Data, 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 /* This can be called from any thread. */
402 that->mpDrv->pUpPort->pfnFreeScreenshot(that->mpDrv->pUpPort, pu8Data);
403 }
404 }
405 }
406 else
407 {
408 LogFunc(("Failed to get VM pointer 0x%x\n", ptrVM.rc()));
409 }
410
411 /* Regardless of rc, save what is available:
412 * Data format:
413 * uint32_t cBlocks;
414 * [blocks]
415 *
416 * Each block is:
417 * uint32_t cbBlock; if 0 - no 'block data'.
418 * uint32_t typeOfBlock; 0 - 32bpp RGB bitmap, 1 - PNG, ignored if 'cbBlock' is 0.
419 * [block data]
420 *
421 * Block data for bitmap and PNG:
422 * uint32_t cx;
423 * uint32_t cy;
424 * [image data]
425 */
426 SSMR3PutU32(pSSM, 2); /* Write thumbnail and PNG screenshot. */
427
428 /* First block. */
429 SSMR3PutU32(pSSM, cbThumbnail + 2 * sizeof(uint32_t));
430 SSMR3PutU32(pSSM, 0); /* Block type: thumbnail. */
431
432 if (cbThumbnail)
433 {
434 SSMR3PutU32(pSSM, cxThumbnail);
435 SSMR3PutU32(pSSM, cyThumbnail);
436 SSMR3PutMem(pSSM, pu8Thumbnail, cbThumbnail);
437 }
438
439 /* Second block. */
440 SSMR3PutU32(pSSM, cbPNG + 2 * sizeof(uint32_t));
441 SSMR3PutU32(pSSM, 1); /* Block type: png. */
442
443 if (cbPNG)
444 {
445 SSMR3PutU32(pSSM, cxPNG);
446 SSMR3PutU32(pSSM, cyPNG);
447 SSMR3PutMem(pSSM, pu8PNG, cbPNG);
448 }
449
450 RTMemFree(pu8PNG);
451 RTMemFree(pu8Thumbnail);
452}
453
454DECLCALLBACK(int)
455Display::i_displaySSMLoadScreenshot(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
456{
457 Display *that = static_cast<Display*>(pvUser);
458
459 if (uVersion != sSSMDisplayScreenshotVer)
460 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
461 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
462
463 /* Skip data. */
464 uint32_t cBlocks;
465 int rc = SSMR3GetU32(pSSM, &cBlocks);
466 AssertRCReturn(rc, rc);
467
468 for (uint32_t i = 0; i < cBlocks; i++)
469 {
470 uint32_t cbBlock;
471 rc = SSMR3GetU32(pSSM, &cbBlock);
472 AssertRCBreak(rc);
473
474 uint32_t typeOfBlock;
475 rc = SSMR3GetU32(pSSM, &typeOfBlock);
476 AssertRCBreak(rc);
477
478 LogRelFlowFunc(("[%d] type %d, size %d bytes\n", i, typeOfBlock, cbBlock));
479
480 /* Note: displaySSMSaveScreenshot writes size of a block = 8 and
481 * do not write any data if the image size was 0.
482 * @todo Fix and increase saved state version.
483 */
484 if (cbBlock > 2 * sizeof(uint32_t))
485 {
486 rc = SSMR3Skip(pSSM, cbBlock);
487 AssertRCBreak(rc);
488 }
489 }
490
491 return rc;
492}
493
494/**
495 * Save/Load some important guest state
496 */
497DECLCALLBACK(void)
498Display::i_displaySSMSave(PSSMHANDLE pSSM, void *pvUser)
499{
500 Display *that = static_cast<Display*>(pvUser);
501
502 SSMR3PutU32(pSSM, that->mcMonitors);
503 for (unsigned i = 0; i < that->mcMonitors; i++)
504 {
505 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32Offset);
506 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32MaxFramebufferSize);
507 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32InformationSize);
508 SSMR3PutU32(pSSM, that->maFramebuffers[i].w);
509 SSMR3PutU32(pSSM, that->maFramebuffers[i].h);
510 SSMR3PutS32(pSSM, that->maFramebuffers[i].xOrigin);
511 SSMR3PutS32(pSSM, that->maFramebuffers[i].yOrigin);
512 SSMR3PutU32(pSSM, that->maFramebuffers[i].flags);
513 }
514 SSMR3PutS32(pSSM, that->xInputMappingOrigin);
515 SSMR3PutS32(pSSM, that->yInputMappingOrigin);
516 SSMR3PutU32(pSSM, that->cxInputMapping);
517 SSMR3PutU32(pSSM, that->cyInputMapping);
518 SSMR3PutU32(pSSM, that->mfGuestVBVACapabilities);
519 SSMR3PutU32(pSSM, that->mfHostCursorCapabilities);
520}
521
522DECLCALLBACK(int)
523Display::i_displaySSMLoad(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
524{
525 Display *that = static_cast<Display*>(pvUser);
526
527 if ( uVersion != sSSMDisplayVer
528 && uVersion != sSSMDisplayVer2
529 && uVersion != sSSMDisplayVer3
530 && uVersion != sSSMDisplayVer4
531 && uVersion != sSSMDisplayVer5)
532 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
533 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
534
535 uint32_t cMonitors;
536 int rc = SSMR3GetU32(pSSM, &cMonitors);
537 if (cMonitors != that->mcMonitors)
538 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Number of monitors changed (%d->%d)!"), cMonitors, that->mcMonitors);
539
540 for (uint32_t i = 0; i < cMonitors; i++)
541 {
542 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32Offset);
543 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32MaxFramebufferSize);
544 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32InformationSize);
545 if ( uVersion == sSSMDisplayVer2
546 || uVersion == sSSMDisplayVer3
547 || uVersion == sSSMDisplayVer4
548 || uVersion == sSSMDisplayVer5)
549 {
550 uint32_t w;
551 uint32_t h;
552 SSMR3GetU32(pSSM, &w);
553 SSMR3GetU32(pSSM, &h);
554 that->maFramebuffers[i].w = w;
555 that->maFramebuffers[i].h = h;
556 }
557 if ( uVersion == sSSMDisplayVer3
558 || uVersion == sSSMDisplayVer4
559 || uVersion == sSSMDisplayVer5)
560 {
561 int32_t xOrigin;
562 int32_t yOrigin;
563 uint32_t flags;
564 SSMR3GetS32(pSSM, &xOrigin);
565 SSMR3GetS32(pSSM, &yOrigin);
566 SSMR3GetU32(pSSM, &flags);
567 that->maFramebuffers[i].xOrigin = xOrigin;
568 that->maFramebuffers[i].yOrigin = yOrigin;
569 that->maFramebuffers[i].flags = (uint16_t)flags;
570 that->maFramebuffers[i].fDisabled = (that->maFramebuffers[i].flags & VBVA_SCREEN_F_DISABLED) != 0;
571 }
572 }
573 if ( uVersion == sSSMDisplayVer4
574 || uVersion == sSSMDisplayVer5)
575 {
576 SSMR3GetS32(pSSM, &that->xInputMappingOrigin);
577 SSMR3GetS32(pSSM, &that->yInputMappingOrigin);
578 SSMR3GetU32(pSSM, &that->cxInputMapping);
579 SSMR3GetU32(pSSM, &that->cyInputMapping);
580 }
581 if (uVersion == sSSMDisplayVer5)
582 {
583 SSMR3GetU32(pSSM, &that->mfGuestVBVACapabilities);
584 SSMR3GetU32(pSSM, &that->mfHostCursorCapabilities);
585 }
586
587 return VINF_SUCCESS;
588}
589
590/**
591 * Initializes the display object.
592 *
593 * @returns COM result indicator
594 * @param parent handle of our parent object
595 * @param qemuConsoleData address of common console data structure
596 */
597HRESULT Display::init(Console *aParent)
598{
599 ComAssertRet(aParent, E_INVALIDARG);
600 /* Enclose the state transition NotReady->InInit->Ready */
601 AutoInitSpan autoInitSpan(this);
602 AssertReturn(autoInitSpan.isOk(), E_FAIL);
603
604 unconst(mParent) = aParent;
605
606 mfSourceBitmapEnabled = true;
607 fVGAResizing = false;
608
609 ULONG ul;
610 mParent->i_machine()->COMGETTER(MonitorCount)(&ul);
611 mcMonitors = ul;
612 xInputMappingOrigin = 0;
613 yInputMappingOrigin = 0;
614 cxInputMapping = 0;
615 cyInputMapping = 0;
616
617 for (ul = 0; ul < mcMonitors; ul++)
618 {
619 maFramebuffers[ul].u32Offset = 0;
620 maFramebuffers[ul].u32MaxFramebufferSize = 0;
621 maFramebuffers[ul].u32InformationSize = 0;
622
623 maFramebuffers[ul].pFramebuffer = NULL;
624 /* All secondary monitors are disabled at startup. */
625 maFramebuffers[ul].fDisabled = ul > 0;
626
627 maFramebuffers[ul].u32Caps = 0;
628
629 maFramebuffers[ul].updateImage.pu8Address = NULL;
630 maFramebuffers[ul].updateImage.cbLine = 0;
631
632 maFramebuffers[ul].xOrigin = 0;
633 maFramebuffers[ul].yOrigin = 0;
634
635 maFramebuffers[ul].w = 0;
636 maFramebuffers[ul].h = 0;
637
638 maFramebuffers[ul].flags = maFramebuffers[ul].fDisabled? VBVA_SCREEN_F_DISABLED: 0;
639
640 maFramebuffers[ul].u16BitsPerPixel = 0;
641 maFramebuffers[ul].pu8FramebufferVRAM = NULL;
642 maFramebuffers[ul].u32LineSize = 0;
643
644 maFramebuffers[ul].pHostEvents = NULL;
645
646 maFramebuffers[ul].fDefaultFormat = false;
647
648#ifdef VBOX_WITH_HGSMI
649 maFramebuffers[ul].fVBVAEnabled = false;
650 maFramebuffers[ul].fVBVAForceResize = false;
651 maFramebuffers[ul].fRenderThreadMode = false;
652 maFramebuffers[ul].pVBVAHostFlags = NULL;
653#endif /* VBOX_WITH_HGSMI */
654#ifdef VBOX_WITH_CROGL
655 RT_ZERO(maFramebuffers[ul].pendingViewportInfo);
656#endif
657 }
658
659 {
660 // register listener for state change events
661 ComPtr<IEventSource> es;
662 mParent->COMGETTER(EventSource)(es.asOutParam());
663 com::SafeArray<VBoxEventType_T> eventTypes;
664 eventTypes.push_back(VBoxEventType_OnStateChanged);
665 es->RegisterListener(this, ComSafeArrayAsInParam(eventTypes), true);
666 }
667
668 /* Cache the 3D settings. */
669 BOOL fIs3DEnabled = FALSE;
670 mParent->i_machine()->COMGETTER(Accelerate3DEnabled)(&fIs3DEnabled);
671 GraphicsControllerType_T enmGpuType = (GraphicsControllerType_T)GraphicsControllerType_VBoxVGA;
672 mParent->i_machine()->COMGETTER(GraphicsControllerType)(&enmGpuType);
673 mfIsCr3DEnabled = fIs3DEnabled && enmGpuType == GraphicsControllerType_VBoxVGA;
674
675 /* Confirm a successful initialization */
676 autoInitSpan.setSucceeded();
677
678 return S_OK;
679}
680
681/**
682 * Uninitializes the instance and sets the ready flag to FALSE.
683 * Called either from FinalRelease() or by the parent when it gets destroyed.
684 */
685void Display::uninit()
686{
687 LogRelFlowFunc(("this=%p\n", this));
688
689 /* Enclose the state transition Ready->InUninit->NotReady */
690 AutoUninitSpan autoUninitSpan(this);
691 if (autoUninitSpan.uninitDone())
692 return;
693
694 unsigned uScreenId;
695 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
696 {
697 maFramebuffers[uScreenId].pSourceBitmap.setNull();
698 maFramebuffers[uScreenId].updateImage.pSourceBitmap.setNull();
699 maFramebuffers[uScreenId].updateImage.pu8Address = NULL;
700 maFramebuffers[uScreenId].updateImage.cbLine = 0;
701 maFramebuffers[uScreenId].pFramebuffer.setNull();
702 }
703
704 if (mParent)
705 {
706 ComPtr<IEventSource> es;
707 mParent->COMGETTER(EventSource)(es.asOutParam());
708 es->UnregisterListener(this);
709 }
710
711 unconst(mParent) = NULL;
712
713 if (mpDrv)
714 mpDrv->pDisplay = NULL;
715
716 mpDrv = NULL;
717 mpVMMDev = NULL;
718 mfVMMDevInited = true;
719}
720
721/**
722 * Register the SSM methods. Called by the power up thread to be able to
723 * pass pVM
724 */
725int Display::i_registerSSM(PUVM pUVM)
726{
727 /* Version 2 adds width and height of the framebuffer; version 3 adds
728 * the framebuffer offset in the virtual desktop and the framebuffer flags;
729 * version 4 adds guest to host input event mapping and version 5 adds
730 * guest VBVA and host cursor capabilities.
731 */
732 int rc = SSMR3RegisterExternal(pUVM, "DisplayData", 0, sSSMDisplayVer5,
733 mcMonitors * sizeof(uint32_t) * 8 + sizeof(uint32_t),
734 NULL, NULL, NULL,
735 NULL, i_displaySSMSave, NULL,
736 NULL, i_displaySSMLoad, NULL, this);
737 AssertRCReturn(rc, rc);
738
739 /*
740 * Register loaders for old saved states where iInstance was
741 * 3 * sizeof(uint32_t *) due to a code mistake.
742 */
743 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 12 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
744 NULL, NULL, NULL,
745 NULL, NULL, NULL,
746 NULL, i_displaySSMLoad, NULL, this);
747 AssertRCReturn(rc, rc);
748
749 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 24 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
750 NULL, NULL, NULL,
751 NULL, NULL, NULL,
752 NULL, i_displaySSMLoad, NULL, this);
753 AssertRCReturn(rc, rc);
754
755 /* uInstance is an arbitrary value greater than 1024. Such a value will ensure a quick seek in saved state file. */
756 rc = SSMR3RegisterExternal(pUVM, "DisplayScreenshot", 1100 /*uInstance*/, sSSMDisplayScreenshotVer, 0 /*cbGuess*/,
757 NULL, NULL, NULL,
758 NULL, i_displaySSMSaveScreenshot, NULL,
759 NULL, i_displaySSMLoadScreenshot, NULL, this);
760
761 AssertRCReturn(rc, rc);
762
763 return VINF_SUCCESS;
764}
765
766DECLCALLBACK(void) Display::i_displayCrCmdFree(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, int rc, void *pvCompletion)
767{
768 Assert(pvCompletion);
769 RTMemFree(pvCompletion);
770}
771
772#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
773int Display::i_crOglWindowsShow(bool fShow)
774{
775 if (!mfCrOglDataHidden == !!fShow)
776 return VINF_SUCCESS;
777
778 if (!mhCrOglSvc)
779 {
780 /* No 3D or the VMSVGA3d kind. */
781 Assert(!mfIsCr3DEnabled);
782 return VERR_INVALID_STATE;
783 }
784
785 VMMDev *pVMMDev = mParent->i_getVMMDev();
786 if (!pVMMDev)
787 {
788 AssertMsgFailed(("no vmmdev\n"));
789 return VERR_INVALID_STATE;
790 }
791
792 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(VBOXCRCMDCTL_HGCM));
793 if (!pData)
794 {
795 AssertMsgFailed(("RTMemAlloc failed\n"));
796 return VERR_NO_MEMORY;
797 }
798
799 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
800 pData->Hdr.u32Function = SHCRGL_HOST_FN_WINDOWS_SHOW;
801
802 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
803 pData->aParms[0].u.uint32 = (uint32_t)fShow;
804
805 int rc = i_crCtlSubmit(&pData->Hdr, sizeof(*pData), i_displayCrCmdFree, pData);
806 if (RT_SUCCESS(rc))
807 mfCrOglDataHidden = !fShow;
808 else
809 {
810 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
811 RTMemFree(pData);
812 }
813
814 return rc;
815}
816#endif
817
818
819// public methods only for internal purposes
820/////////////////////////////////////////////////////////////////////////////
821
822int Display::i_notifyCroglResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
823{
824#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
825 if (maFramebuffers[pScreen->u32ViewIndex].fRenderThreadMode)
826 return VINF_SUCCESS; /* nop it */
827
828 if (mfIsCr3DEnabled)
829 {
830 int rc = VERR_INVALID_STATE;
831 if (mhCrOglSvc)
832 {
833 VMMDev *pVMMDev = mParent->i_getVMMDev();
834 if (pVMMDev)
835 {
836 VBOXCRCMDCTL_HGCM *pCtl;
837 pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(CRVBOXHGCMDEVRESIZE) + sizeof(VBOXCRCMDCTL_HGCM));
838 if (pCtl)
839 {
840 CRVBOXHGCMDEVRESIZE *pData = (CRVBOXHGCMDEVRESIZE*)(pCtl+1);
841 pData->Screen = *pScreen;
842 pData->pvVRAM = pvVRAM;
843
844 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
845 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_DEV_RESIZE;
846 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
847 pCtl->aParms[0].u.pointer.addr = pData;
848 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
849
850 rc = i_crCtlSubmit(&pCtl->Hdr, sizeof(*pCtl), i_displayCrCmdFree, pCtl);
851 if (RT_FAILURE(rc))
852 {
853 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
854 RTMemFree(pCtl);
855 }
856 }
857 else
858 rc = VERR_NO_MEMORY;
859 }
860 }
861
862 return rc;
863 }
864#endif /* #if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
865 return VINF_SUCCESS;
866}
867
868/**
869 * Handles display resize event.
870 *
871 * @param w New display width
872 * @param h New display height
873 *
874 * @thread EMT
875 */
876int Display::i_handleDisplayResize(unsigned uScreenId, uint32_t bpp, void *pvVRAM,
877 uint32_t cbLine, uint32_t w, uint32_t h, uint16_t flags)
878{
879 LogRel(("Display::handleDisplayResize(): uScreenId = %d, pvVRAM=%p "
880 "w=%d h=%d bpp=%d cbLine=0x%X, flags=0x%X\n",
881 uScreenId, pvVRAM, w, h, bpp, cbLine, flags));
882
883 if (uScreenId >= mcMonitors)
884 {
885 return VINF_SUCCESS;
886 }
887
888 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
889
890 /* Reset the update mode. */
891 pFBInfo->updateImage.pSourceBitmap.setNull();
892 pFBInfo->updateImage.pu8Address = NULL;
893 pFBInfo->updateImage.cbLine = 0;
894
895 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
896 {
897 pFBInfo->w = w;
898 pFBInfo->h = h;
899
900 pFBInfo->u16BitsPerPixel = (uint16_t)bpp;
901 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM;
902 pFBInfo->u32LineSize = cbLine;
903 pFBInfo->flags = flags;
904 }
905
906 /* Guest screen image will be invalid during resize, make sure that it is not updated. */
907 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
908 {
909 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, false);
910
911 mpDrv->IConnector.pu8Data = NULL;
912 mpDrv->IConnector.cbScanline = 0;
913 mpDrv->IConnector.cBits = 32; /* DevVGA does not work with cBits == 0. */
914 mpDrv->IConnector.cx = 0;
915 mpDrv->IConnector.cy = 0;
916 }
917
918 maFramebuffers[uScreenId].pSourceBitmap.setNull();
919
920 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
921 {
922 HRESULT hr = maFramebuffers[uScreenId].pFramebuffer->NotifyChange(uScreenId, 0, 0, w, h); /* @todo origin */
923 LogFunc(("NotifyChange hr %08X\n", hr));
924 NOREF(hr);
925 }
926
927 bool fUpdateImage = RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_UpdateImage);
928 if (fUpdateImage && !pFBInfo->pFramebuffer.isNull())
929 {
930 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
931 HRESULT hr = QuerySourceBitmap(uScreenId, pSourceBitmap.asOutParam());
932 if (SUCCEEDED(hr))
933 {
934 BYTE *pAddress = NULL;
935 ULONG ulWidth = 0;
936 ULONG ulHeight = 0;
937 ULONG ulBitsPerPixel = 0;
938 ULONG ulBytesPerLine = 0;
939 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
940
941 hr = pSourceBitmap->QueryBitmapInfo(&pAddress,
942 &ulWidth,
943 &ulHeight,
944 &ulBitsPerPixel,
945 &ulBytesPerLine,
946 &bitmapFormat);
947 if (SUCCEEDED(hr))
948 {
949 pFBInfo->updateImage.pSourceBitmap = pSourceBitmap;
950 pFBInfo->updateImage.pu8Address = pAddress;
951 pFBInfo->updateImage.cbLine = ulBytesPerLine;
952 }
953 }
954 }
955
956 /* Inform the VRDP server about the change of display parameters. */
957 LogRelFlowFunc(("Calling VRDP\n"));
958 mParent->i_consoleVRDPServer()->SendResize();
959
960 /* And re-send the seamless rectangles if necessary. */
961 if (mfSeamlessEnabled)
962 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
963
964 LogRelFlowFunc(("[%d]: default format %d\n", uScreenId, pFBInfo->fDefaultFormat));
965
966 return VINF_SUCCESS;
967}
968
969static void i_checkCoordBounds(int *px, int *py, int *pw, int *ph, int cx, int cy)
970{
971 /* Correct negative x and y coordinates. */
972 if (*px < 0)
973 {
974 *px += *pw; /* Compute xRight which is also the new width. */
975
976 *pw = (*px < 0)? 0: *px;
977
978 *px = 0;
979 }
980
981 if (*py < 0)
982 {
983 *py += *ph; /* Compute xBottom, which is also the new height. */
984
985 *ph = (*py < 0)? 0: *py;
986
987 *py = 0;
988 }
989
990 /* Also check if coords are greater than the display resolution. */
991 if (*px + *pw > cx)
992 {
993 *pw = cx > *px? cx - *px: 0;
994 }
995
996 if (*py + *ph > cy)
997 {
998 *ph = cy > *py? cy - *py: 0;
999 }
1000}
1001
1002void Display::i_handleDisplayUpdate(unsigned uScreenId, int x, int y, int w, int h)
1003{
1004 /*
1005 * Always runs under either VBVA lock or, for HGSMI, DevVGA lock.
1006 * Safe to use VBVA vars and take the framebuffer lock.
1007 */
1008
1009#ifdef DEBUG_sunlover
1010 LogFlowFunc(("[%d] %d,%d %dx%d\n",
1011 uScreenId, x, y, w, h));
1012#endif /* DEBUG_sunlover */
1013
1014 /* No updates for a disabled guest screen. */
1015 if (maFramebuffers[uScreenId].fDisabled)
1016 return;
1017
1018 /* No updates for a blank guest screen. */
1019 /** @note Disabled for now, as the GUI does not update the picture when we
1020 * first blank. */
1021 /* if (maFramebuffers[uScreenId].flags & VBVA_SCREEN_F_BLANK)
1022 return; */
1023
1024 i_checkCoordBounds (&x, &y, &w, &h, maFramebuffers[uScreenId].w,
1025 maFramebuffers[uScreenId].h);
1026
1027 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
1028 if (pFramebuffer != NULL)
1029 {
1030 if (w != 0 && h != 0)
1031 {
1032 bool fUpdateImage = RT_BOOL(maFramebuffers[uScreenId].u32Caps & FramebufferCapabilities_UpdateImage);
1033 if (RT_LIKELY(!fUpdateImage))
1034 {
1035 pFramebuffer->NotifyUpdate(x, y, w, h);
1036 }
1037 else
1038 {
1039 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1040
1041 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1042
1043 if (!pFBInfo->updateImage.pSourceBitmap.isNull())
1044 {
1045 Assert(pFBInfo->updateImage.pu8Address);
1046
1047 size_t cbData = w * h * 4;
1048 com::SafeArray<BYTE> image(cbData);
1049
1050 uint8_t *pu8Dst = image.raw();
1051 const uint8_t *pu8Src = pFBInfo->updateImage.pu8Address + pFBInfo->updateImage.cbLine * y + x * 4;
1052
1053 int i;
1054 for (i = y; i < y + h; ++i)
1055 {
1056 memcpy(pu8Dst, pu8Src, w * 4);
1057 pu8Dst += w * 4;
1058 pu8Src += pFBInfo->updateImage.cbLine;
1059 }
1060
1061 pFramebuffer->NotifyUpdateImage(x, y, w, h, ComSafeArrayAsInParam(image));
1062 }
1063 }
1064 }
1065 }
1066
1067#ifndef VBOX_WITH_HGSMI
1068 if (!mVideoAccelLegacy.fVideoAccelEnabled)
1069 {
1070#else
1071 if (!mVideoAccelLegacy.fVideoAccelEnabled && !maFramebuffers[uScreenId].fVBVAEnabled)
1072 {
1073#endif /* VBOX_WITH_HGSMI */
1074 /* When VBVA is enabled, the VRDP server is informed
1075 * either in VideoAccelFlush or displayVBVAUpdateProcess.
1076 * Inform the server here only if VBVA is disabled.
1077 */
1078 mParent->i_consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
1079 }
1080}
1081
1082void Display::i_updateGuestGraphicsFacility(void)
1083{
1084 Guest* pGuest = mParent->i_getGuest();
1085 AssertPtrReturnVoid(pGuest);
1086 /* The following is from GuestImpl.cpp. */
1087 /** @todo A nit: The timestamp is wrong on saved state restore. Would be better
1088 * to move the graphics and seamless capability -> facility translation to
1089 * VMMDev so this could be saved. */
1090 RTTIMESPEC TimeSpecTS;
1091 RTTimeNow(&TimeSpecTS);
1092
1093 if ( mfVMMDevSupportsGraphics
1094 || (mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS) != 0)
1095 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1096 VBoxGuestFacilityStatus_Active,
1097 0 /*fFlags*/, &TimeSpecTS);
1098 else
1099 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1100 VBoxGuestFacilityStatus_Inactive,
1101 0 /*fFlags*/, &TimeSpecTS);
1102}
1103
1104void Display::i_handleUpdateVMMDevSupportsGraphics(bool fSupportsGraphics)
1105{
1106 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1107 if (mfVMMDevSupportsGraphics == fSupportsGraphics)
1108 return;
1109 mfVMMDevSupportsGraphics = fSupportsGraphics;
1110 i_updateGuestGraphicsFacility();
1111 /* The VMMDev interface notifies the console. */
1112}
1113
1114void Display::i_handleUpdateGuestVBVACapabilities(uint32_t fNewCapabilities)
1115{
1116 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1117 bool fNotify = (fNewCapabilities & VBVACAPS_VIDEO_MODE_HINTS) != 0;
1118
1119 mfGuestVBVACapabilities = fNewCapabilities;
1120 if (!fNotify)
1121 return;
1122 i_updateGuestGraphicsFacility();
1123 /* Tell the console about it */
1124 mParent->i_onAdditionsStateChange();
1125}
1126
1127void Display::i_handleUpdateVBVAInputMapping(int32_t xOrigin, int32_t yOrigin, uint32_t cx, uint32_t cy)
1128{
1129 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1130
1131 xInputMappingOrigin = xOrigin;
1132 yInputMappingOrigin = yOrigin;
1133 cxInputMapping = cx;
1134 cyInputMapping = cy;
1135
1136 /* Re-send the seamless rectangles if necessary. */
1137 if (mfSeamlessEnabled)
1138 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1139}
1140
1141/**
1142 * Returns the upper left and lower right corners of the virtual framebuffer.
1143 * The lower right is "exclusive" (i.e. first pixel beyond the framebuffer),
1144 * and the origin is (0, 0), not (1, 1) like the GUI returns.
1145 */
1146void Display::i_getFramebufferDimensions(int32_t *px1, int32_t *py1,
1147 int32_t *px2, int32_t *py2)
1148{
1149 int32_t x1 = 0, y1 = 0, x2 = 0, y2 = 0;
1150 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1151
1152 AssertPtrReturnVoid(px1);
1153 AssertPtrReturnVoid(py1);
1154 AssertPtrReturnVoid(px2);
1155 AssertPtrReturnVoid(py2);
1156 LogRelFlowFunc(("\n"));
1157
1158 if (!mpDrv)
1159 return;
1160 /* If VBVA is not in use then this flag will not be set and this
1161 * will still work as it should. */
1162 if (!maFramebuffers[0].fDisabled)
1163 {
1164 x1 = (int32_t)maFramebuffers[0].xOrigin;
1165 y1 = (int32_t)maFramebuffers[0].yOrigin;
1166 x2 = (int32_t)maFramebuffers[0].w + (int32_t)maFramebuffers[0].xOrigin;
1167 y2 = (int32_t)maFramebuffers[0].h + (int32_t)maFramebuffers[0].yOrigin;
1168 }
1169 if (cxInputMapping && cyInputMapping)
1170 {
1171 x1 = xInputMappingOrigin;
1172 y1 = yInputMappingOrigin;
1173 x2 = xInputMappingOrigin + cxInputMapping;
1174 y2 = yInputMappingOrigin + cyInputMapping;
1175 }
1176 else
1177 for (unsigned i = 1; i < mcMonitors; ++i)
1178 {
1179 if (!maFramebuffers[i].fDisabled)
1180 {
1181 x1 = RT_MIN(x1, maFramebuffers[i].xOrigin);
1182 y1 = RT_MIN(y1, maFramebuffers[i].yOrigin);
1183 x2 = RT_MAX(x2, maFramebuffers[i].xOrigin + (int32_t)maFramebuffers[i].w);
1184 y2 = RT_MAX(y2, maFramebuffers[i].yOrigin + (int32_t)maFramebuffers[i].h);
1185 }
1186 }
1187 *px1 = x1;
1188 *py1 = y1;
1189 *px2 = x2;
1190 *py2 = y2;
1191}
1192
1193HRESULT Display::i_reportHostCursorCapabilities(uint32_t fCapabilitiesAdded, uint32_t fCapabilitiesRemoved)
1194{
1195 /* Do we need this to access mParent? I presume that the safe VM pointer
1196 * ensures that mpDrv will remain valid. */
1197 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1198 uint32_t fHostCursorCapabilities = (mfHostCursorCapabilities | fCapabilitiesAdded)
1199 & ~fCapabilitiesRemoved;
1200
1201 Console::SafeVMPtr ptrVM(mParent);
1202 if (!ptrVM.isOk())
1203 return ptrVM.rc();
1204 if (mfHostCursorCapabilities == fHostCursorCapabilities)
1205 return S_OK;
1206 CHECK_CONSOLE_DRV(mpDrv);
1207 alock.release(); /* Release before calling up for lock order reasons. */
1208 mpDrv->pUpPort->pfnReportHostCursorCapabilities (mpDrv->pUpPort, fCapabilitiesAdded, fCapabilitiesRemoved);
1209 mfHostCursorCapabilities = fHostCursorCapabilities;
1210 return S_OK;
1211}
1212
1213HRESULT Display::i_reportHostCursorPosition(int32_t x, int32_t y)
1214{
1215 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1216 uint32_t xAdj = (uint32_t)RT_MAX(x - xInputMappingOrigin, 0);
1217 uint32_t yAdj = (uint32_t)RT_MAX(y - yInputMappingOrigin, 0);
1218 xAdj = RT_MIN(xAdj, cxInputMapping);
1219 yAdj = RT_MIN(yAdj, cyInputMapping);
1220
1221 Console::SafeVMPtr ptrVM(mParent);
1222 if (!ptrVM.isOk())
1223 return ptrVM.rc();
1224 CHECK_CONSOLE_DRV(mpDrv);
1225 alock.release(); /* Release before calling up for lock order reasons. */
1226 mpDrv->pUpPort->pfnReportHostCursorPosition(mpDrv->pUpPort, xAdj, yAdj);
1227 return S_OK;
1228}
1229
1230static bool displayIntersectRect(RTRECT *prectResult,
1231 const RTRECT *prect1,
1232 const RTRECT *prect2)
1233{
1234 /* Initialize result to an empty record. */
1235 memset(prectResult, 0, sizeof(RTRECT));
1236
1237 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
1238 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
1239
1240 if (xLeftResult < xRightResult)
1241 {
1242 /* There is intersection by X. */
1243
1244 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
1245 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
1246
1247 if (yTopResult < yBottomResult)
1248 {
1249 /* There is intersection by Y. */
1250
1251 prectResult->xLeft = xLeftResult;
1252 prectResult->yTop = yTopResult;
1253 prectResult->xRight = xRightResult;
1254 prectResult->yBottom = yBottomResult;
1255
1256 return true;
1257 }
1258 }
1259
1260 return false;
1261}
1262
1263int Display::i_saveVisibleRegion(uint32_t cRect, PRTRECT pRect)
1264{
1265 RTRECT *pRectVisibleRegion = NULL;
1266
1267 if (pRect == mpRectVisibleRegion)
1268 return VINF_SUCCESS;
1269 if (cRect != 0)
1270 {
1271 pRectVisibleRegion = (RTRECT *)RTMemAlloc(cRect * sizeof(RTRECT));
1272 if (!pRectVisibleRegion)
1273 {
1274 return VERR_NO_MEMORY;
1275 }
1276 memcpy(pRectVisibleRegion, pRect, cRect * sizeof(RTRECT));
1277 }
1278 if (mpRectVisibleRegion)
1279 RTMemFree(mpRectVisibleRegion);
1280 mcRectVisibleRegion = cRect;
1281 mpRectVisibleRegion = pRectVisibleRegion;
1282 return VINF_SUCCESS;
1283}
1284
1285int Display::i_handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1286{
1287 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc( RT_MAX(cRect, 1)
1288 * sizeof(RTRECT));
1289 if (!pVisibleRegion)
1290 {
1291 return VERR_NO_TMP_MEMORY;
1292 }
1293 int rc = i_saveVisibleRegion(cRect, pRect);
1294 if (RT_FAILURE(rc))
1295 {
1296 RTMemTmpFree(pVisibleRegion);
1297 return rc;
1298 }
1299
1300 unsigned uScreenId;
1301 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1302 {
1303 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1304
1305 if ( !pFBInfo->pFramebuffer.isNull()
1306 & RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_VisibleRegion))
1307 {
1308 /* Prepare a new array of rectangles which intersect with the framebuffer.
1309 */
1310 RTRECT rectFramebuffer;
1311 rectFramebuffer.xLeft = pFBInfo->xOrigin - xInputMappingOrigin;
1312 rectFramebuffer.yTop = pFBInfo->yOrigin - yInputMappingOrigin;
1313 rectFramebuffer.xRight = rectFramebuffer.xLeft + pFBInfo->w;
1314 rectFramebuffer.yBottom = rectFramebuffer.yTop + pFBInfo->h;
1315
1316 uint32_t cRectVisibleRegion = 0;
1317
1318 uint32_t i;
1319 for (i = 0; i < cRect; i++)
1320 {
1321 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1322 {
1323 pVisibleRegion[cRectVisibleRegion].xLeft -= rectFramebuffer.xLeft;
1324 pVisibleRegion[cRectVisibleRegion].yTop -= rectFramebuffer.yTop;
1325 pVisibleRegion[cRectVisibleRegion].xRight -= rectFramebuffer.xLeft;
1326 pVisibleRegion[cRectVisibleRegion].yBottom -= rectFramebuffer.yTop;
1327
1328 cRectVisibleRegion++;
1329 }
1330 }
1331 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1332 }
1333 }
1334
1335#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1336 VMMDev *vmmDev = mParent->i_getVMMDev();
1337 if (mfIsCr3DEnabled && vmmDev)
1338 {
1339 if (mhCrOglSvc)
1340 {
1341 VBOXCRCMDCTL_HGCM *pCtl;
1342 pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(RT_MAX(cRect, 1) * sizeof(RTRECT) + sizeof(VBOXCRCMDCTL_HGCM));
1343 if (pCtl)
1344 {
1345 RTRECT *pRectsCopy = (RTRECT*)(pCtl+1);
1346 memcpy(pRectsCopy, pRect, cRect * sizeof(RTRECT));
1347
1348 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1349 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1350
1351 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1352 pCtl->aParms[0].u.pointer.addr = pRectsCopy;
1353 pCtl->aParms[0].u.pointer.size = cRect * sizeof(RTRECT);
1354
1355 rc = i_crCtlSubmit(&pCtl->Hdr, sizeof(*pCtl), i_displayCrCmdFree, pCtl);
1356 if (!RT_SUCCESS(rc))
1357 {
1358 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1359 RTMemFree(pCtl);
1360 }
1361 }
1362 else
1363 AssertMsgFailed(("failed to allocate rects memory\n"));
1364 }
1365 else
1366 AssertMsgFailed(("mhCrOglSvc is NULL\n"));
1367 }
1368#endif
1369
1370 RTMemTmpFree(pVisibleRegion);
1371
1372 return VINF_SUCCESS;
1373}
1374
1375int Display::i_handleQueryVisibleRegion(uint32_t *pcRect, PRTRECT pRect)
1376{
1377 // @todo Currently not used by the guest and is not implemented in framebuffers. Remove?
1378 return VERR_NOT_SUPPORTED;
1379}
1380
1381#ifdef VBOX_WITH_HGSMI
1382static void vbvaSetMemoryFlagsHGSMI(unsigned uScreenId,
1383 uint32_t fu32SupportedOrders,
1384 bool fVideoAccelVRDP,
1385 DISPLAYFBINFO *pFBInfo)
1386{
1387 LogRelFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1388
1389 if (pFBInfo->pVBVAHostFlags)
1390 {
1391 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1392
1393 if (pFBInfo->fVBVAEnabled)
1394 {
1395 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1396
1397 if (fVideoAccelVRDP)
1398 {
1399 fu32HostEvents |= VBVA_F_MODE_VRDP;
1400 }
1401 }
1402
1403 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1404 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1405
1406 LogRelFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1407 }
1408}
1409
1410static void vbvaSetMemoryFlagsAllHGSMI(uint32_t fu32SupportedOrders,
1411 bool fVideoAccelVRDP,
1412 DISPLAYFBINFO *paFBInfos,
1413 unsigned cFBInfos)
1414{
1415 unsigned uScreenId;
1416
1417 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1418 {
1419 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1420 }
1421}
1422#endif /* VBOX_WITH_HGSMI */
1423
1424int Display::VideoAccelEnableVMMDev(bool fEnable, VBVAMEMORY *pVbvaMemory)
1425{
1426 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1427 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1428 if (RT_SUCCESS(rc))
1429 {
1430 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1431 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1432 }
1433 LogFlowFunc(("leave %Rrc\n", rc));
1434 return rc;
1435}
1436
1437int Display::VideoAccelEnableVGA(bool fEnable, VBVAMEMORY *pVbvaMemory)
1438{
1439 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1440 int rc = videoAccelEnterVGA(&mVideoAccelLegacy);
1441 if (RT_SUCCESS(rc))
1442 {
1443 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1444 videoAccelLeaveVGA(&mVideoAccelLegacy);
1445 }
1446 LogFlowFunc(("leave %Rrc\n", rc));
1447 return rc;
1448}
1449
1450void Display::VideoAccelFlushVMMDev(void)
1451{
1452 LogFlowFunc(("enter\n"));
1453 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1454 if (RT_SUCCESS(rc))
1455 {
1456 i_VideoAccelFlush(mpDrv->pUpPort);
1457 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1458 }
1459 LogFlowFunc(("leave\n"));
1460}
1461
1462/* Called always by one VRDP server thread. Can be thread-unsafe.
1463 */
1464void Display::i_VideoAccelVRDP(bool fEnable)
1465{
1466 LogRelFlowFunc(("fEnable = %d\n", fEnable));
1467
1468 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
1469
1470 int c = fEnable?
1471 ASMAtomicIncS32(&mcVideoAccelVRDPRefs):
1472 ASMAtomicDecS32(&mcVideoAccelVRDPRefs);
1473
1474 Assert (c >= 0);
1475
1476 /* This can run concurrently with Display videoaccel state change. */
1477 RTCritSectEnter(&mVideoAccelLock);
1478
1479 if (c == 0)
1480 {
1481 /* The last client has disconnected, and the accel can be
1482 * disabled.
1483 */
1484 Assert (fEnable == false);
1485
1486 mfVideoAccelVRDP = false;
1487 mfu32SupportedOrders = 0;
1488
1489 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1490 maFramebuffers, mcMonitors);
1491#ifdef VBOX_WITH_HGSMI
1492 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1493 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1494#endif /* VBOX_WITH_HGSMI */
1495
1496 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1497 }
1498 else if ( c == 1
1499 && !mfVideoAccelVRDP)
1500 {
1501 /* The first client has connected. Enable the accel.
1502 */
1503 Assert (fEnable == true);
1504
1505 mfVideoAccelVRDP = true;
1506 /* Supporting all orders. */
1507 mfu32SupportedOrders = ~0;
1508
1509 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1510 maFramebuffers, mcMonitors);
1511#ifdef VBOX_WITH_HGSMI
1512 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1513 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1514#endif /* VBOX_WITH_HGSMI */
1515
1516 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1517 }
1518 else
1519 {
1520 /* A client is connected or disconnected but there is no change in the
1521 * accel state. It remains enabled.
1522 */
1523 Assert(mfVideoAccelVRDP == true);
1524 }
1525
1526 RTCritSectLeave(&mVideoAccelLock);
1527}
1528
1529void Display::i_notifyPowerDown(void)
1530{
1531 LogRelFlowFunc(("\n"));
1532
1533 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1534
1535 /* Source bitmaps are not available anymore. */
1536 mfSourceBitmapEnabled = false;
1537
1538 alock.release();
1539
1540 /* Resize all displays to tell framebuffers to forget current source bitmap. */
1541 unsigned uScreenId = mcMonitors;
1542 while (uScreenId > 0)
1543 {
1544 --uScreenId;
1545
1546 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1547 if (!pFBInfo->fDisabled)
1548 {
1549 i_handleDisplayResize(uScreenId, 32,
1550 pFBInfo->pu8FramebufferVRAM,
1551 pFBInfo->u32LineSize,
1552 pFBInfo->w,
1553 pFBInfo->h,
1554 pFBInfo->flags);
1555 }
1556 }
1557}
1558
1559// Wrapped IDisplay methods
1560/////////////////////////////////////////////////////////////////////////////
1561HRESULT Display::getScreenResolution(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel,
1562 LONG *aXOrigin, LONG *aYOrigin, GuestMonitorStatus_T *aGuestMonitorStatus)
1563{
1564 LogRelFlowFunc(("aScreenId=%RU32\n", aScreenId));
1565
1566 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1567
1568 if (aScreenId >= mcMonitors)
1569 return E_INVALIDARG;
1570
1571 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1572
1573 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
1574 if (pFBInfo->flags & VBVA_SCREEN_F_DISABLED)
1575 guestMonitorStatus = GuestMonitorStatus_Disabled;
1576
1577 if (aWidth)
1578 *aWidth = pFBInfo->w;
1579 if (aHeight)
1580 *aHeight = pFBInfo->h;
1581 if (aBitsPerPixel)
1582 *aBitsPerPixel = pFBInfo->u16BitsPerPixel;
1583 if (aXOrigin)
1584 *aXOrigin = pFBInfo->xOrigin;
1585 if (aYOrigin)
1586 *aYOrigin = pFBInfo->yOrigin;
1587 if (aGuestMonitorStatus)
1588 *aGuestMonitorStatus = guestMonitorStatus;
1589
1590 return S_OK;
1591}
1592
1593
1594HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer, com::Guid &aId)
1595{
1596 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1597
1598 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1599
1600 if (aScreenId >= mcMonitors)
1601 return setError(E_INVALIDARG, tr("AttachFramebuffer: Invalid screen %d (total %d)"),
1602 aScreenId, mcMonitors);
1603
1604 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1605 if (!pFBInfo->pFramebuffer.isNull())
1606 return setError(E_FAIL, tr("AttachFramebuffer: Framebuffer already attached to %d"),
1607 aScreenId);
1608
1609 pFBInfo->pFramebuffer = aFramebuffer;
1610 pFBInfo->framebufferId.create();
1611 aId = pFBInfo->framebufferId;
1612
1613 SafeArray<FramebufferCapabilities_T> caps;
1614 pFBInfo->pFramebuffer->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(caps));
1615 pFBInfo->u32Caps = 0;
1616 size_t i;
1617 for (i = 0; i < caps.size(); ++i)
1618 pFBInfo->u32Caps |= caps[i];
1619
1620 alock.release();
1621
1622 /* The driver might not have been constructed yet */
1623 if (mpDrv)
1624 {
1625 /* Setup the new framebuffer. */
1626 i_handleDisplayResize(aScreenId, pFBInfo->u16BitsPerPixel,
1627 pFBInfo->pu8FramebufferVRAM,
1628 pFBInfo->u32LineSize,
1629 pFBInfo->w,
1630 pFBInfo->h,
1631 pFBInfo->flags);
1632 }
1633
1634 Console::SafeVMPtrQuiet ptrVM(mParent);
1635 if (ptrVM.isOk())
1636 {
1637#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1638 if (mfIsCr3DEnabled)
1639 {
1640 VBOXCRCMDCTL_HGCM data;
1641 RT_ZERO(data);
1642 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1643 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1644
1645 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1646 data.aParms[0].u.uint32 = aScreenId;
1647
1648 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1649 AssertRC(vrc);
1650 }
1651#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1652
1653 VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
1654 3, this, aScreenId, false);
1655 }
1656
1657 LogRelFlowFunc(("Attached to %d %RTuuid\n", aScreenId, aId.raw()));
1658 return S_OK;
1659}
1660
1661HRESULT Display::detachFramebuffer(ULONG aScreenId, const com::Guid &aId)
1662{
1663 LogRelFlowFunc(("aScreenId = %d %RTuuid\n", aScreenId, aId.raw()));
1664
1665 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1666
1667 if (aScreenId >= mcMonitors)
1668 return setError(E_INVALIDARG, tr("DetachFramebuffer: Invalid screen %d (total %d)"),
1669 aScreenId, mcMonitors);
1670
1671 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1672
1673 if (pFBInfo->framebufferId != aId)
1674 {
1675 LogRelFlowFunc(("Invalid framebuffer aScreenId = %d, attached %p\n", aScreenId, pFBInfo->framebufferId.raw()));
1676 return setError(E_FAIL, tr("DetachFramebuffer: Invalid framebuffer object"));
1677 }
1678
1679 pFBInfo->pFramebuffer.setNull();
1680 pFBInfo->framebufferId.clear();
1681
1682 alock.release();
1683
1684#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1685 Console::SafeVMPtrQuiet ptrVM(mParent);
1686 if (ptrVM.isOk())
1687 {
1688 if (mfIsCr3DEnabled)
1689 {
1690 VBOXCRCMDCTL_HGCM data;
1691 RT_ZERO(data);
1692 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1693 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1694
1695 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1696 data.aParms[0].u.uint32 = aScreenId;
1697
1698 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1699 AssertRC(vrc);
1700 }
1701 }
1702#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1703
1704 return S_OK;
1705}
1706
1707HRESULT Display::queryFramebuffer(ULONG aScreenId, ComPtr<IFramebuffer> &aFramebuffer)
1708{
1709 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1710
1711 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1712
1713 if (aScreenId >= mcMonitors)
1714 return setError(E_INVALIDARG, tr("QueryFramebuffer: Invalid screen %d (total %d)"),
1715 aScreenId, mcMonitors);
1716
1717 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1718
1719 pFBInfo->pFramebuffer.queryInterfaceTo(aFramebuffer.asOutParam());
1720
1721 return S_OK;
1722}
1723
1724HRESULT Display::setVideoModeHint(ULONG aDisplay, BOOL aEnabled,
1725 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
1726 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel)
1727{
1728 if (aWidth == 0 || aHeight == 0 || aBitsPerPixel == 0)
1729 {
1730 /* Some of parameters must not change. Query current mode. */
1731 ULONG ulWidth = 0;
1732 ULONG ulHeight = 0;
1733 ULONG ulBitsPerPixel = 0;
1734 HRESULT hr = getScreenResolution(aDisplay, &ulWidth, &ulHeight, &ulBitsPerPixel, NULL, NULL, NULL);
1735 if (FAILED(hr))
1736 return hr;
1737
1738 /* Assign current values to not changing parameters. */
1739 if (aWidth == 0)
1740 aWidth = ulWidth;
1741 if (aHeight == 0)
1742 aHeight = ulHeight;
1743 if (aBitsPerPixel == 0)
1744 aBitsPerPixel = ulBitsPerPixel;
1745 }
1746
1747 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1748
1749 if (aDisplay >= mcMonitors)
1750 return E_INVALIDARG;
1751
1752 CHECK_CONSOLE_DRV(mpDrv);
1753
1754 /*
1755 * It is up to the guest to decide whether the hint is
1756 * valid. Therefore don't do any VRAM sanity checks here.
1757 */
1758
1759 /* Have to release the lock because the pfnRequestDisplayChange
1760 * will call EMT. */
1761 alock.release();
1762
1763 /* We always send the hint to the graphics card in case the guest enables
1764 * support later. For now we notify exactly when support is enabled. */
1765 mpDrv->pUpPort->pfnSendModeHint(mpDrv->pUpPort, aWidth, aHeight,
1766 aBitsPerPixel, aDisplay,
1767 aChangeOrigin ? aOriginX : ~0,
1768 aChangeOrigin ? aOriginY : ~0,
1769 RT_BOOL(aEnabled),
1770 mfGuestVBVACapabilities
1771 & VBVACAPS_VIDEO_MODE_HINTS);
1772 if ( mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS
1773 && !(mfGuestVBVACapabilities & VBVACAPS_IRQ))
1774 {
1775 mParent->i_sendACPIMonitorHotPlugEvent();
1776 }
1777
1778 /* We currently never suppress the VMMDev hint if the guest has requested
1779 * it. Specifically the video graphics driver may not be responsible for
1780 * screen positioning in the guest virtual desktop, and the component
1781 * responsible may want to get the hint from VMMDev. */
1782 VMMDev *pVMMDev = mParent->i_getVMMDev();
1783 if (pVMMDev)
1784 {
1785 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1786 if (pVMMDevPort)
1787 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel,
1788 aDisplay, aOriginX, aOriginY,
1789 RT_BOOL(aEnabled), RT_BOOL(aChangeOrigin));
1790 }
1791 return S_OK;
1792}
1793
1794HRESULT Display::setSeamlessMode(BOOL enabled)
1795{
1796 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1797
1798 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
1799 alock.release();
1800
1801 VMMDev *pVMMDev = mParent->i_getVMMDev();
1802 if (pVMMDev)
1803 {
1804 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1805 if (pVMMDevPort)
1806 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
1807 }
1808 mfSeamlessEnabled = enabled;
1809
1810#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1811 if (!enabled)
1812 {
1813 VMMDev *vmmDev = mParent->i_getVMMDev();
1814 if (mfIsCr3DEnabled && vmmDev)
1815 {
1816 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(VBOXCRCMDCTL_HGCM));
1817 if (!pData)
1818 {
1819 AssertMsgFailed(("RTMemAlloc failed\n"));
1820 return VERR_NO_MEMORY;
1821 }
1822
1823 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1824 pData->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1825
1826 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1827 pData->aParms[0].u.pointer.addr = NULL;
1828 pData->aParms[0].u.pointer.size = 0; /* <- means null rects, NULL pRects address and 0 rects means "disable" */
1829
1830 int rc = i_crCtlSubmit(&pData->Hdr, sizeof(*pData), i_displayCrCmdFree, pData);
1831 if (!RT_SUCCESS(rc))
1832 {
1833 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1834 RTMemFree(pData);
1835 }
1836 }
1837 }
1838#endif
1839 return S_OK;
1840}
1841
1842#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1843BOOL Display::i_displayCheckTakeScreenshotCrOgl(Display *pDisplay, ULONG aScreenId, uint8_t *pu8Data,
1844 uint32_t u32Width, uint32_t u32Height)
1845{
1846 if ( pDisplay->mfIsCr3DEnabled
1847 && pDisplay->mCrOglCallbacks.pfnHasData
1848 && pDisplay->mCrOglCallbacks.pfnHasData())
1849 {
1850 VMMDev *pVMMDev = pDisplay->mParent->i_getVMMDev();
1851 if (pVMMDev)
1852 {
1853 CRVBOXHGCMTAKESCREENSHOT *pScreenshot = (CRVBOXHGCMTAKESCREENSHOT*)RTMemAlloc(sizeof(*pScreenshot));
1854 if (pScreenshot)
1855 {
1856 /* screen id or CRSCREEN_ALL to specify all enabled */
1857 pScreenshot->u32Screen = aScreenId;
1858 pScreenshot->u32Width = u32Width;
1859 pScreenshot->u32Height = u32Height;
1860 pScreenshot->u32Pitch = u32Width * 4;
1861 pScreenshot->pvBuffer = pu8Data;
1862 pScreenshot->pvContext = NULL;
1863 pScreenshot->pfnScreenshotBegin = NULL;
1864 pScreenshot->pfnScreenshotPerform = NULL;
1865 pScreenshot->pfnScreenshotEnd = NULL;
1866
1867 VBOXCRCMDCTL_HGCM data;
1868 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1869 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
1870
1871 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1872 data.aParms[0].u.pointer.addr = pScreenshot;
1873 data.aParms[0].u.pointer.size = sizeof(*pScreenshot);
1874
1875 int rc = pDisplay->i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1876
1877 RTMemFree(pScreenshot);
1878
1879 if (RT_SUCCESS(rc))
1880 return TRUE;
1881 AssertMsgFailed(("failed to get screenshot data from crOgl (rc=%Rrc)\n", rc));
1882 /* fall back to the non-3d mechanism */
1883 }
1884 }
1885 }
1886 return FALSE;
1887}
1888#endif
1889
1890int Display::i_displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppu8Data, size_t *pcbData,
1891 uint32_t *pu32Width, uint32_t *pu32Height)
1892{
1893 int rc;
1894
1895 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
1896 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
1897 {
1898 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppu8Data, pcbData, pu32Width, pu32Height);
1899 }
1900 else if (aScreenId < pDisplay->mcMonitors)
1901 {
1902 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
1903
1904 uint32_t width = pFBInfo->w;
1905 uint32_t height = pFBInfo->h;
1906
1907 /* Allocate 32 bit per pixel bitmap. */
1908 size_t cbRequired = width * 4 * height;
1909
1910 if (cbRequired)
1911 {
1912 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbRequired);
1913
1914 if (pu8Data == NULL)
1915 {
1916 rc = VERR_NO_MEMORY;
1917 }
1918 else
1919 {
1920 /* Copy guest VRAM to the allocated 32bpp buffer. */
1921 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
1922 int32_t xSrc = 0;
1923 int32_t ySrc = 0;
1924 uint32_t u32SrcWidth = width;
1925 uint32_t u32SrcHeight = height;
1926 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
1927 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
1928
1929 uint8_t *pu8Dst = pu8Data;
1930 int32_t xDst = 0;
1931 int32_t yDst = 0;
1932 uint32_t u32DstWidth = u32SrcWidth;
1933 uint32_t u32DstHeight = u32SrcHeight;
1934 uint32_t u32DstLineSize = u32DstWidth * 4;
1935 uint32_t u32DstBitsPerPixel = 32;
1936
1937 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
1938 width, height,
1939 pu8Src,
1940 xSrc, ySrc,
1941 u32SrcWidth, u32SrcHeight,
1942 u32SrcLineSize, u32SrcBitsPerPixel,
1943 pu8Dst,
1944 xDst, yDst,
1945 u32DstWidth, u32DstHeight,
1946 u32DstLineSize, u32DstBitsPerPixel);
1947 if (RT_SUCCESS(rc))
1948 {
1949 *ppu8Data = pu8Data;
1950 *pcbData = cbRequired;
1951 *pu32Width = width;
1952 *pu32Height = height;
1953 }
1954 else
1955 {
1956 RTMemFree(pu8Data);
1957
1958 /* CopyRect can fail if VBVA was paused in VGA device, retry using the generic method. */
1959 if ( rc == VERR_INVALID_STATE
1960 && aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1961 {
1962 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort,
1963 ppu8Data, pcbData, pu32Width, pu32Height);
1964 }
1965 }
1966 }
1967 }
1968 else
1969 {
1970 /* No image. */
1971 *ppu8Data = NULL;
1972 *pcbData = 0;
1973 *pu32Width = 0;
1974 *pu32Height = 0;
1975 rc = VINF_SUCCESS;
1976 }
1977 }
1978 else
1979 {
1980 rc = VERR_INVALID_PARAMETER;
1981 }
1982
1983 return rc;
1984}
1985
1986static int i_displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
1987 BYTE *address, ULONG width, ULONG height)
1988{
1989 uint8_t *pu8Data = NULL;
1990 size_t cbData = 0;
1991 uint32_t cx = 0;
1992 uint32_t cy = 0;
1993 int vrc = VINF_SUCCESS;
1994
1995# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1996 if (Display::i_displayCheckTakeScreenshotCrOgl(pDisplay, aScreenId, (uint8_t*)address, width, height))
1997 return VINF_SUCCESS;
1998#endif
1999
2000 int cRetries = 5;
2001
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, 6,
2008 pDisplay, aScreenId, &pu8Data, &cbData, &cx, &cy);
2009 if (vrc != VERR_TRY_AGAIN)
2010 {
2011 break;
2012 }
2013
2014 RTThreadSleep(10);
2015 }
2016
2017 if (RT_SUCCESS(vrc) && pu8Data)
2018 {
2019 if (cx == width && cy == height)
2020 {
2021 /* No scaling required. */
2022 memcpy(address, pu8Data, 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 = pu8Data;
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 (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2045 {
2046 /* This can be called from any thread. */
2047 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pu8Data);
2048 }
2049 else
2050 {
2051 RTMemFree(pu8Data);
2052 }
2053 }
2054
2055 return vrc;
2056}
2057
2058HRESULT Display::takeScreenShotWorker(ULONG aScreenId,
2059 BYTE *aAddress,
2060 ULONG aWidth,
2061 ULONG aHeight,
2062 BitmapFormat_T aBitmapFormat,
2063 ULONG *pcbOut)
2064{
2065 HRESULT rc = S_OK;
2066
2067 /* Do not allow too small and too large screenshots. This also filters out negative
2068 * values passed as either 'aWidth' or 'aHeight'.
2069 */
2070 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2071 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2072
2073 if ( aBitmapFormat != BitmapFormat_BGR0
2074 && aBitmapFormat != BitmapFormat_BGRA
2075 && aBitmapFormat != BitmapFormat_RGBA
2076 && aBitmapFormat != BitmapFormat_PNG)
2077 {
2078 return setError(E_NOTIMPL,
2079 tr("Unsupported screenshot format 0x%08X"), aBitmapFormat);
2080 }
2081
2082 Console::SafeVMPtr ptrVM(mParent);
2083 if (!ptrVM.isOk())
2084 return ptrVM.rc();
2085
2086 int vrc = i_displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, aAddress, aWidth, aHeight);
2087
2088 if (RT_SUCCESS(vrc))
2089 {
2090 const size_t cbData = aWidth * 4 * aHeight;
2091
2092 /* Most of uncompressed formats. */
2093 *pcbOut = (ULONG)cbData;
2094
2095 if (aBitmapFormat == BitmapFormat_BGR0)
2096 {
2097 /* Do nothing. */
2098 }
2099 else if (aBitmapFormat == BitmapFormat_BGRA)
2100 {
2101 uint32_t *pu32 = (uint32_t *)aAddress;
2102 size_t cPixels = aWidth * aHeight;
2103 while (cPixels--)
2104 {
2105 *pu32++ |= UINT32_C(0xFF000000);
2106 }
2107 }
2108 else if (aBitmapFormat == BitmapFormat_RGBA)
2109 {
2110 uint8_t *pu8 = aAddress;
2111 size_t cPixels = aWidth * aHeight;
2112 while (cPixels--)
2113 {
2114 uint8_t u8 = pu8[0];
2115 pu8[0] = pu8[2];
2116 pu8[2] = u8;
2117 pu8[3] = 0xFF;
2118
2119 pu8 += 4;
2120 }
2121 }
2122 else if (aBitmapFormat == BitmapFormat_PNG)
2123 {
2124 uint8_t *pu8PNG = NULL;
2125 uint32_t cbPNG = 0;
2126 uint32_t cxPNG = 0;
2127 uint32_t cyPNG = 0;
2128
2129 vrc = DisplayMakePNG(aAddress, aWidth, aHeight, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2130 if (RT_SUCCESS(vrc))
2131 {
2132 if (cbPNG <= cbData)
2133 {
2134 memcpy(aAddress, pu8PNG, cbPNG);
2135 *pcbOut = cbPNG;
2136 }
2137 else
2138 {
2139 rc = setError(E_FAIL,
2140 tr("PNG is larger than 32bpp bitmap"));
2141 }
2142 }
2143 else
2144 {
2145 rc = setError(VBOX_E_IPRT_ERROR,
2146 tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2147 }
2148 RTMemFree(pu8PNG);
2149 }
2150 }
2151 else if (vrc == VERR_TRY_AGAIN)
2152 rc = setError(E_UNEXPECTED,
2153 tr("Screenshot is not available at this time"));
2154 else if (RT_FAILURE(vrc))
2155 rc = setError(VBOX_E_IPRT_ERROR,
2156 tr("Could not take a screenshot (%Rrc)"), vrc);
2157
2158 return rc;
2159}
2160
2161HRESULT Display::takeScreenShot(ULONG aScreenId,
2162 BYTE *aAddress,
2163 ULONG aWidth,
2164 ULONG aHeight,
2165 BitmapFormat_T aBitmapFormat)
2166{
2167 HRESULT rc = S_OK;
2168
2169 LogRelFlowFunc(("[%d] address=%p, width=%d, height=%d, format 0x%08X\n",
2170 aScreenId, aAddress, aWidth, aHeight, aBitmapFormat));
2171
2172 ULONG cbOut = 0;
2173 rc = takeScreenShotWorker(aScreenId, aAddress, aWidth, aHeight, aBitmapFormat, &cbOut);
2174 NOREF(cbOut);
2175
2176 LogRelFlowFunc(("%Rhrc\n", rc));
2177 return rc;
2178}
2179
2180HRESULT Display::takeScreenShotToArray(ULONG aScreenId,
2181 ULONG aWidth,
2182 ULONG aHeight,
2183 BitmapFormat_T aBitmapFormat,
2184 std::vector<BYTE> &aScreenData)
2185{
2186 HRESULT rc = S_OK;
2187
2188 LogRelFlowFunc(("[%d] width=%d, height=%d, format 0x%08X\n",
2189 aScreenId, aWidth, aHeight, aBitmapFormat));
2190
2191 /* Do not allow too small and too large screenshots. This also filters out negative
2192 * values passed as either 'aWidth' or 'aHeight'.
2193 */
2194 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2195 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2196
2197 const size_t cbData = aWidth * 4 * aHeight;
2198 aScreenData.resize(cbData);
2199
2200 ULONG cbOut = 0;
2201 rc = takeScreenShotWorker(aScreenId, &aScreenData.front(), aWidth, aHeight, aBitmapFormat, &cbOut);
2202 if (FAILED(rc))
2203 cbOut = 0;
2204
2205 aScreenData.resize(cbOut);
2206
2207 LogRelFlowFunc(("%Rhrc\n", rc));
2208 return rc;
2209}
2210
2211
2212int Display::i_VideoCaptureEnableScreens(ComSafeArrayIn(BOOL, aScreens))
2213{
2214#ifdef VBOX_WITH_VPX
2215 com::SafeArray<BOOL> Screens(ComSafeArrayInArg(aScreens));
2216 for (unsigned i = 0; i < Screens.size(); i++)
2217 maVideoRecEnabled[i] = RT_BOOL(Screens[i]);
2218 return VINF_SUCCESS;
2219#else
2220 return VERR_NOT_IMPLEMENTED;
2221#endif
2222}
2223
2224/**
2225 * Start video capturing. Does nothing if capturing is already active.
2226 */
2227int Display::i_VideoCaptureStart()
2228{
2229#ifdef VBOX_WITH_VPX
2230 if (VideoRecIsEnabled(mpVideoRecCtx))
2231 return VINF_SUCCESS;
2232
2233 int rc = VideoRecContextCreate(&mpVideoRecCtx, mcMonitors);
2234 if (RT_FAILURE(rc))
2235 {
2236 LogFlow(("Failed to create video recording context (%Rrc)!\n", rc));
2237 return rc;
2238 }
2239 ComPtr<IMachine> pMachine = mParent->i_machine();
2240 com::SafeArray<BOOL> screens;
2241 HRESULT hrc = pMachine->COMGETTER(VideoCaptureScreens)(ComSafeArrayAsOutParam(screens));
2242 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2243 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
2244 maVideoRecEnabled[i] = i < screens.size() && screens[i];
2245 ULONG ulWidth;
2246 hrc = pMachine->COMGETTER(VideoCaptureWidth)(&ulWidth);
2247 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2248 ULONG ulHeight;
2249 hrc = pMachine->COMGETTER(VideoCaptureHeight)(&ulHeight);
2250 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2251 ULONG ulRate;
2252 hrc = pMachine->COMGETTER(VideoCaptureRate)(&ulRate);
2253 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2254 ULONG ulFPS;
2255 hrc = pMachine->COMGETTER(VideoCaptureFPS)(&ulFPS);
2256 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2257 BSTR strFile;
2258 hrc = pMachine->COMGETTER(VideoCaptureFile)(&strFile);
2259 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2260 ULONG ulMaxTime;
2261 hrc = pMachine->COMGETTER(VideoCaptureMaxTime)(&ulMaxTime);
2262 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2263 ULONG ulMaxSize;
2264 hrc = pMachine->COMGETTER(VideoCaptureMaxFileSize)(&ulMaxSize);
2265 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2266 BSTR strOptions;
2267 hrc = pMachine->COMGETTER(VideoCaptureOptions)(&strOptions);
2268 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2269
2270 RTTIMESPEC ts;
2271 RTTimeNow(&ts);
2272 RTTIME time;
2273 RTTimeExplode(&time, &ts);
2274 for (unsigned uScreen = 0; uScreen < mcMonitors; uScreen++)
2275 {
2276 char *pszAbsPath = RTPathAbsDup(com::Utf8Str(strFile).c_str());
2277 char *pszSuff = RTPathSuffix(pszAbsPath);
2278 if (pszSuff)
2279 pszSuff = RTStrDup(pszSuff);
2280 RTPathStripSuffix(pszAbsPath);
2281 if (!pszAbsPath)
2282 rc = VERR_INVALID_PARAMETER;
2283 if (!pszSuff)
2284 pszSuff = RTStrDup(".webm");
2285 char *pszName = NULL;
2286 if (RT_SUCCESS(rc))
2287 {
2288 if (mcMonitors > 1)
2289 rc = RTStrAPrintf(&pszName, "%s-%u%s", pszAbsPath, uScreen+1, pszSuff);
2290 else
2291 rc = RTStrAPrintf(&pszName, "%s%s", pszAbsPath, pszSuff);
2292 }
2293 if (RT_SUCCESS(rc))
2294 {
2295 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
2296 pszName, ulWidth, ulHeight,
2297 ulRate, ulFPS, ulMaxTime,
2298 ulMaxSize, com::Utf8Str(strOptions).c_str());
2299 if (rc == VERR_ALREADY_EXISTS)
2300 {
2301 RTStrFree(pszName);
2302 pszName = NULL;
2303
2304 if (mcMonitors > 1)
2305 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ-%u%s",
2306 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2307 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2308 uScreen+1, pszSuff);
2309 else
2310 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ%s",
2311 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2312 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2313 pszSuff);
2314 if (RT_SUCCESS(rc))
2315 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
2316 pszName, ulWidth, ulHeight, ulRate,
2317 ulFPS, ulMaxTime,
2318 ulMaxSize, com::Utf8Str(strOptions).c_str());
2319 }
2320 }
2321
2322 if (RT_SUCCESS(rc))
2323 LogRel(("WebM/VP8 video recording screen #%u with %ux%u @ %u kbps, %u fps to '%s' enabled.\n",
2324 uScreen, ulWidth, ulHeight, ulRate, ulFPS, pszName));
2325 else
2326 LogRel(("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(("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.pu8Data = 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.pu8Data)
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.pu8Data, 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.pu8Data = 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