VirtualBox

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

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

FE/Qt, Main: 7678: Do not log the improper call to 3D-overlay rescaling when 3D is not enabled/allowed for the running VM; Assert instead.

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

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