VirtualBox

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

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

Main/DisplayImpl: make it work with VBOX_WITH_CROGL disabled

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 145.4 KB
Line 
1/* $Id: DisplayImpl.cpp 54425 2015-02-24 09:02:48Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "DisplayImpl.h"
19#include "DisplayUtils.h"
20#include "ConsoleImpl.h"
21#include "ConsoleVRDPServer.h"
22#include "GuestImpl.h"
23#include "VMMDev.h"
24
25#include "AutoCaller.h"
26#include "Logging.h"
27
28/* generated header */
29#include "VBoxEvents.h"
30
31#include <iprt/semaphore.h>
32#include <iprt/thread.h>
33#include <iprt/asm.h>
34#include <iprt/time.h>
35#include <iprt/cpp/utils.h>
36#include <iprt/alloca.h>
37
38#include <VBox/vmm/pdmdrv.h>
39#if defined(DEBUG) || defined(VBOX_STRICT) /* for VM_ASSERT_EMT(). */
40# include <VBox/vmm/vm.h>
41#endif
42
43#ifdef VBOX_WITH_VIDEOHWACCEL
44# include <VBox/VBoxVideo.h>
45#endif
46
47#if defined(VBOX_WITH_CROGL) || defined(VBOX_WITH_CRHGSMI)
48# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
49#endif
50
51#include <VBox/com/array.h>
52
53#ifdef VBOX_WITH_VPX
54# include <iprt/path.h>
55# include "VideoRec.h"
56#endif
57
58#ifdef VBOX_WITH_CROGL
59typedef enum
60{
61 CRVREC_STATE_IDLE,
62 CRVREC_STATE_SUBMITTED
63} CRVREC_STATE;
64#endif
65
66/**
67 * Display driver instance data.
68 *
69 * @implements PDMIDISPLAYCONNECTOR
70 */
71typedef struct DRVMAINDISPLAY
72{
73 /** Pointer to the display object. */
74 Display *pDisplay;
75 /** Pointer to the driver instance structure. */
76 PPDMDRVINS pDrvIns;
77 /** Pointer to the keyboard port interface of the driver/device above us. */
78 PPDMIDISPLAYPORT pUpPort;
79 /** Our display connector interface. */
80 PDMIDISPLAYCONNECTOR IConnector;
81#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
82 /** VBVA callbacks */
83 PPDMIDISPLAYVBVACALLBACKS pVBVACallbacks;
84#endif
85} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
86
87/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
88#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) RT_FROM_MEMBER(pInterface, DRVMAINDISPLAY, IConnector)
89
90// constructor / destructor
91/////////////////////////////////////////////////////////////////////////////
92
93Display::Display()
94 : mParent(NULL), mfIsCr3DEnabled(false)
95{
96}
97
98Display::~Display()
99{
100}
101
102
103HRESULT Display::FinalConstruct()
104{
105 int rc = videoAccelConstruct(&mVideoAccelLegacy);
106 AssertRC(rc);
107
108 mfVideoAccelVRDP = false;
109 mfu32SupportedOrders = 0;
110 mcVideoAccelVRDPRefs = 0;
111
112#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 if (mfHostCursorCapabilities == fHostCursorCapabilities)
1186 return S_OK;
1187 CHECK_CONSOLE_DRV(mpDrv);
1188 alock.release(); /* Release before calling up for lock order reasons. */
1189 mpDrv->pUpPort->pfnReportHostCursorCapabilities (mpDrv->pUpPort, fCapabilitiesAdded, fCapabilitiesRemoved);
1190 mfHostCursorCapabilities = fHostCursorCapabilities;
1191 return S_OK;
1192}
1193
1194HRESULT Display::i_reportHostCursorPosition(int32_t x, int32_t y)
1195{
1196 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1197 uint32_t xAdj = (uint32_t)RT_MAX(x - xInputMappingOrigin, 0);
1198 uint32_t yAdj = (uint32_t)RT_MAX(y - yInputMappingOrigin, 0);
1199 xAdj = RT_MIN(xAdj, cxInputMapping);
1200 yAdj = RT_MIN(yAdj, cyInputMapping);
1201
1202 Console::SafeVMPtr ptrVM(mParent);
1203 if (!ptrVM.isOk())
1204 return ptrVM.rc();
1205 CHECK_CONSOLE_DRV(mpDrv);
1206 alock.release(); /* Release before calling up for lock order reasons. */
1207 mpDrv->pUpPort->pfnReportHostCursorPosition(mpDrv->pUpPort, xAdj, yAdj);
1208 return S_OK;
1209}
1210
1211static bool displayIntersectRect(RTRECT *prectResult,
1212 const RTRECT *prect1,
1213 const RTRECT *prect2)
1214{
1215 /* Initialize result to an empty record. */
1216 memset(prectResult, 0, sizeof(RTRECT));
1217
1218 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
1219 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
1220
1221 if (xLeftResult < xRightResult)
1222 {
1223 /* There is intersection by X. */
1224
1225 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
1226 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
1227
1228 if (yTopResult < yBottomResult)
1229 {
1230 /* There is intersection by Y. */
1231
1232 prectResult->xLeft = xLeftResult;
1233 prectResult->yTop = yTopResult;
1234 prectResult->xRight = xRightResult;
1235 prectResult->yBottom = yBottomResult;
1236
1237 return true;
1238 }
1239 }
1240
1241 return false;
1242}
1243
1244int Display::i_handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1245{
1246 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc( RT_MAX(cRect, 1)
1247 * sizeof(RTRECT));
1248 if (!pVisibleRegion)
1249 {
1250 return VERR_NO_TMP_MEMORY;
1251 }
1252
1253 unsigned uScreenId;
1254 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1255 {
1256 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1257
1258 if ( !pFBInfo->pFramebuffer.isNull()
1259 & RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_VisibleRegion))
1260 {
1261 /* Prepare a new array of rectangles which intersect with the framebuffer.
1262 */
1263 RTRECT rectFramebuffer;
1264 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1265 {
1266 rectFramebuffer.xLeft = 0;
1267 rectFramebuffer.yTop = 0;
1268 if (mpDrv)
1269 {
1270 rectFramebuffer.xRight = mpDrv->IConnector.cx;
1271 rectFramebuffer.yBottom = mpDrv->IConnector.cy;
1272 }
1273 else
1274 {
1275 rectFramebuffer.xRight = 0;
1276 rectFramebuffer.yBottom = 0;
1277 }
1278 }
1279 else
1280 {
1281 rectFramebuffer.xLeft = pFBInfo->xOrigin;
1282 rectFramebuffer.yTop = pFBInfo->yOrigin;
1283 rectFramebuffer.xRight = pFBInfo->xOrigin + pFBInfo->w;
1284 rectFramebuffer.yBottom = pFBInfo->yOrigin + pFBInfo->h;
1285 }
1286
1287 uint32_t cRectVisibleRegion = 0;
1288
1289 uint32_t i;
1290 for (i = 0; i < cRect; i++)
1291 {
1292 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1293 {
1294 pVisibleRegion[cRectVisibleRegion].xLeft -= pFBInfo->xOrigin;
1295 pVisibleRegion[cRectVisibleRegion].yTop -= pFBInfo->yOrigin;
1296 pVisibleRegion[cRectVisibleRegion].xRight -= pFBInfo->xOrigin;
1297 pVisibleRegion[cRectVisibleRegion].yBottom -= pFBInfo->yOrigin;
1298
1299 cRectVisibleRegion++;
1300 }
1301 }
1302 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1303 }
1304 }
1305
1306#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1307 VMMDev *vmmDev = mParent->i_getVMMDev();
1308 if (mfIsCr3DEnabled && vmmDev)
1309 {
1310 if (mhCrOglSvc)
1311 {
1312 VBOXCRCMDCTL_HGCM *pCtl;
1313 pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(RT_MAX(cRect, 1) * sizeof(RTRECT) + sizeof(VBOXCRCMDCTL_HGCM));
1314 if (pCtl)
1315 {
1316 RTRECT *pRectsCopy = (RTRECT*)(pCtl+1);
1317 memcpy(pRectsCopy, pRect, cRect * sizeof(RTRECT));
1318
1319 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1320 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1321
1322 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1323 pCtl->aParms[0].u.pointer.addr = pRectsCopy;
1324 pCtl->aParms[0].u.pointer.size = cRect * sizeof(RTRECT);
1325
1326 int rc = i_crCtlSubmit(&pCtl->Hdr, sizeof(*pCtl), i_displayCrCmdFree, pCtl);
1327 if (!RT_SUCCESS(rc))
1328 {
1329 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1330 RTMemFree(pCtl);
1331 }
1332 }
1333 else
1334 AssertMsgFailed(("failed to allocate rects memory\n"));
1335 }
1336 else
1337 AssertMsgFailed(("mhCrOglSvc is NULL\n"));
1338 }
1339#endif
1340
1341 RTMemTmpFree(pVisibleRegion);
1342
1343 return VINF_SUCCESS;
1344}
1345
1346int Display::i_handleQueryVisibleRegion(uint32_t *pcRect, PRTRECT pRect)
1347{
1348 // @todo Currently not used by the guest and is not implemented in framebuffers. Remove?
1349 return VERR_NOT_SUPPORTED;
1350}
1351
1352#ifdef VBOX_WITH_HGSMI
1353static void vbvaSetMemoryFlagsHGSMI(unsigned uScreenId,
1354 uint32_t fu32SupportedOrders,
1355 bool fVideoAccelVRDP,
1356 DISPLAYFBINFO *pFBInfo)
1357{
1358 LogRelFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1359
1360 if (pFBInfo->pVBVAHostFlags)
1361 {
1362 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1363
1364 if (pFBInfo->fVBVAEnabled)
1365 {
1366 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1367
1368 if (fVideoAccelVRDP)
1369 {
1370 fu32HostEvents |= VBVA_F_MODE_VRDP;
1371 }
1372 }
1373
1374 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1375 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1376
1377 LogRelFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1378 }
1379}
1380
1381static void vbvaSetMemoryFlagsAllHGSMI(uint32_t fu32SupportedOrders,
1382 bool fVideoAccelVRDP,
1383 DISPLAYFBINFO *paFBInfos,
1384 unsigned cFBInfos)
1385{
1386 unsigned uScreenId;
1387
1388 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1389 {
1390 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1391 }
1392}
1393#endif /* VBOX_WITH_HGSMI */
1394
1395int Display::VideoAccelEnableVMMDev(bool fEnable, VBVAMEMORY *pVbvaMemory)
1396{
1397 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1398 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1399 if (RT_SUCCESS(rc))
1400 {
1401 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1402 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1403 }
1404 LogFlowFunc(("leave %Rrc\n", rc));
1405 return rc;
1406}
1407
1408int Display::VideoAccelEnableVGA(bool fEnable, VBVAMEMORY *pVbvaMemory)
1409{
1410 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1411 int rc = videoAccelEnterVGA(&mVideoAccelLegacy);
1412 if (RT_SUCCESS(rc))
1413 {
1414 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1415 videoAccelLeaveVGA(&mVideoAccelLegacy);
1416 }
1417 LogFlowFunc(("leave %Rrc\n", rc));
1418 return rc;
1419}
1420
1421void Display::VideoAccelFlushVMMDev(void)
1422{
1423 LogFlowFunc(("enter\n"));
1424 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1425 if (RT_SUCCESS(rc))
1426 {
1427 i_VideoAccelFlush(mpDrv->pUpPort);
1428 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1429 }
1430 LogFlowFunc(("leave\n"));
1431}
1432
1433/* Called always by one VRDP server thread. Can be thread-unsafe.
1434 */
1435void Display::i_VideoAccelVRDP(bool fEnable)
1436{
1437 LogRelFlowFunc(("fEnable = %d\n", fEnable));
1438
1439 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
1440
1441 int c = fEnable?
1442 ASMAtomicIncS32(&mcVideoAccelVRDPRefs):
1443 ASMAtomicDecS32(&mcVideoAccelVRDPRefs);
1444
1445 Assert (c >= 0);
1446
1447 /* This can run concurrently with Display videoaccel state change. */
1448 RTCritSectEnter(&mVideoAccelLock);
1449
1450 if (c == 0)
1451 {
1452 /* The last client has disconnected, and the accel can be
1453 * disabled.
1454 */
1455 Assert (fEnable == false);
1456
1457 mfVideoAccelVRDP = false;
1458 mfu32SupportedOrders = 0;
1459
1460 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1461 maFramebuffers, mcMonitors);
1462#ifdef VBOX_WITH_HGSMI
1463 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1464 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1465#endif /* VBOX_WITH_HGSMI */
1466
1467 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1468 }
1469 else if ( c == 1
1470 && !mfVideoAccelVRDP)
1471 {
1472 /* The first client has connected. Enable the accel.
1473 */
1474 Assert (fEnable == true);
1475
1476 mfVideoAccelVRDP = true;
1477 /* Supporting all orders. */
1478 mfu32SupportedOrders = ~0;
1479
1480 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1481 maFramebuffers, mcMonitors);
1482#ifdef VBOX_WITH_HGSMI
1483 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1484 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1485#endif /* VBOX_WITH_HGSMI */
1486
1487 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1488 }
1489 else
1490 {
1491 /* A client is connected or disconnected but there is no change in the
1492 * accel state. It remains enabled.
1493 */
1494 Assert(mfVideoAccelVRDP == true);
1495 }
1496
1497 RTCritSectLeave(&mVideoAccelLock);
1498}
1499
1500void Display::i_notifyPowerDown(void)
1501{
1502 LogRelFlowFunc(("\n"));
1503
1504 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1505
1506 /* Source bitmaps are not available anymore. */
1507 mfSourceBitmapEnabled = false;
1508
1509 /* Resize all displays to tell framebuffers to forget current source bitmap. */
1510 unsigned uScreenId = mcMonitors;
1511 while (uScreenId > 0)
1512 {
1513 --uScreenId;
1514
1515 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1516 if (!pFBInfo->fDisabled)
1517 {
1518 i_handleDisplayResize(uScreenId, 32,
1519 pFBInfo->pu8FramebufferVRAM,
1520 pFBInfo->u32LineSize,
1521 pFBInfo->w,
1522 pFBInfo->h,
1523 pFBInfo->flags);
1524 }
1525 }
1526}
1527
1528// Wrapped IDisplay methods
1529/////////////////////////////////////////////////////////////////////////////
1530HRESULT Display::getScreenResolution(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel,
1531 LONG *aXOrigin, LONG *aYOrigin, GuestMonitorStatus_T *aGuestMonitorStatus)
1532{
1533 LogRelFlowFunc(("aScreenId=%RU32\n", aScreenId));
1534
1535 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1536
1537 uint32_t u32Width = 0;
1538 uint32_t u32Height = 0;
1539 uint32_t u32BitsPerPixel = 0;
1540 int32_t xOrigin = 0;
1541 int32_t yOrigin = 0;
1542 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
1543
1544 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1545 {
1546 if (mpDrv)
1547 {
1548 u32Width = mpDrv->IConnector.cx;
1549 u32Height = mpDrv->IConnector.cy;
1550
1551 alock.release();
1552
1553 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &u32BitsPerPixel);
1554 AssertRC(rc);
1555
1556 alock.acquire();
1557 }
1558 }
1559 else if (aScreenId < mcMonitors)
1560 {
1561 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1562 u32Width = pFBInfo->w;
1563 u32Height = pFBInfo->h;
1564 u32BitsPerPixel = pFBInfo->u16BitsPerPixel;
1565 xOrigin = pFBInfo->xOrigin;
1566 yOrigin = pFBInfo->yOrigin;
1567 if (pFBInfo->flags & VBVA_SCREEN_F_DISABLED)
1568 guestMonitorStatus = GuestMonitorStatus_Disabled;
1569 }
1570 else
1571 {
1572 return E_INVALIDARG;
1573 }
1574
1575 if (aWidth)
1576 *aWidth = u32Width;
1577 if (aHeight)
1578 *aHeight = u32Height;
1579 if (aBitsPerPixel)
1580 *aBitsPerPixel = u32BitsPerPixel;
1581 if (aXOrigin)
1582 *aXOrigin = xOrigin;
1583 if (aYOrigin)
1584 *aYOrigin = yOrigin;
1585 if (aGuestMonitorStatus)
1586 *aGuestMonitorStatus = guestMonitorStatus;
1587
1588 return S_OK;
1589}
1590
1591
1592HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer)
1593{
1594 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1595
1596 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1597
1598 if (aScreenId >= mcMonitors)
1599 return setError(E_INVALIDARG, tr("AttachFramebuffer: Invalid screen %d (total %d)"),
1600 aScreenId, mcMonitors);
1601
1602 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1603 if (!pFBInfo->pFramebuffer.isNull())
1604 return setError(E_FAIL, tr("AttachFramebuffer: Framebuffer already attached to %d"),
1605 aScreenId);
1606
1607 pFBInfo->pFramebuffer = aFramebuffer;
1608
1609 SafeArray<FramebufferCapabilities_T> caps;
1610 pFBInfo->pFramebuffer->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(caps));
1611 pFBInfo->u32Caps = 0;
1612 size_t i;
1613 for (i = 0; i < caps.size(); ++i)
1614 pFBInfo->u32Caps |= caps[i];
1615
1616 alock.release();
1617
1618 /* The driver might not have been constructed yet */
1619 if (mpDrv)
1620 {
1621 /* Setup the new framebuffer. */
1622 i_handleDisplayResize(aScreenId, pFBInfo->u16BitsPerPixel,
1623 pFBInfo->pu8FramebufferVRAM,
1624 pFBInfo->u32LineSize,
1625 pFBInfo->w,
1626 pFBInfo->h,
1627 pFBInfo->flags);
1628 }
1629
1630 Console::SafeVMPtrQuiet ptrVM(mParent);
1631 if (ptrVM.isOk())
1632 {
1633#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1634 if (mfIsCr3DEnabled)
1635 {
1636 VBOXCRCMDCTL_HGCM data;
1637 RT_ZERO(data);
1638 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1639 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1640
1641 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1642 data.aParms[0].u.uint32 = aScreenId;
1643
1644 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1645 AssertRC(vrc);
1646 }
1647#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1648
1649 VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
1650 3, this, aScreenId, false);
1651 }
1652
1653 LogRelFlowFunc(("Attached to %d\n", aScreenId));
1654 return S_OK;
1655}
1656
1657HRESULT Display::detachFramebuffer(ULONG aScreenId)
1658{
1659 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1660
1661 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1662
1663 if (aScreenId >= mcMonitors)
1664 return setError(E_INVALIDARG, tr("DetachFramebuffer: Invalid screen %d (total %d)"),
1665 aScreenId, mcMonitors);
1666
1667 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1668
1669 pFBInfo->pFramebuffer.setNull();
1670
1671 alock.release();
1672
1673#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1674 Console::SafeVMPtrQuiet ptrVM(mParent);
1675 if (ptrVM.isOk())
1676 {
1677 if (mfIsCr3DEnabled)
1678 {
1679 VBOXCRCMDCTL_HGCM data;
1680 RT_ZERO(data);
1681 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1682 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1683
1684 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1685 data.aParms[0].u.uint32 = aScreenId;
1686
1687 int vrc = i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1688 AssertRC(vrc);
1689 }
1690 }
1691#endif /* defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
1692
1693 return S_OK;
1694}
1695
1696HRESULT Display::queryFramebuffer(ULONG aScreenId, ComPtr<IFramebuffer> &aFramebuffer)
1697{
1698 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1699
1700 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1701
1702 if (aScreenId >= mcMonitors)
1703 return setError(E_INVALIDARG, tr("QueryFramebuffer: Invalid screen %d (total %d)"),
1704 aScreenId, mcMonitors);
1705
1706 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1707
1708 pFBInfo->pFramebuffer.queryInterfaceTo(aFramebuffer.asOutParam());
1709
1710 return S_OK;
1711}
1712
1713HRESULT Display::setVideoModeHint(ULONG aDisplay, BOOL aEnabled,
1714 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
1715 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel)
1716{
1717 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1718
1719 CHECK_CONSOLE_DRV(mpDrv);
1720
1721 /*
1722 * Do some rough checks for valid input.
1723 */
1724 ULONG width = aWidth;
1725 if (!width)
1726 width = mpDrv->IConnector.cx;
1727 ULONG height = aHeight;
1728 if (!height)
1729 height = mpDrv->IConnector.cy;
1730 ULONG bpp = aBitsPerPixel;
1731 if (!bpp)
1732 {
1733 alock.release();
1734
1735 uint32_t cBits = 0;
1736 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1737 AssertRC(rc);
1738 bpp = cBits;
1739
1740 alock.acquire();
1741 }
1742 ULONG cMonitors;
1743 mParent->i_machine()->COMGETTER(MonitorCount)(&cMonitors);
1744 if (cMonitors == 0 && aDisplay > 0)
1745 return E_INVALIDARG;
1746 if (aDisplay >= cMonitors)
1747 return E_INVALIDARG;
1748
1749 /*
1750 * sunlover 20070614: It is up to the guest to decide whether the hint is
1751 * valid. Therefore don't do any VRAM sanity checks here!
1752 */
1753
1754 /* Have to release the lock because the pfnRequestDisplayChange
1755 * will call EMT. */
1756 alock.release();
1757
1758 /* We always send the hint to the graphics card in case the guest enables
1759 * support later. For now we notify exactly when support is enabled. */
1760 mpDrv->pUpPort->pfnSendModeHint(mpDrv->pUpPort, aWidth, aHeight,
1761 aBitsPerPixel, aDisplay,
1762 aChangeOrigin ? aOriginX : ~0,
1763 aChangeOrigin ? aOriginY : ~0,
1764 RT_BOOL(aEnabled),
1765 mfGuestVBVACapabilities
1766 & VBVACAPS_VIDEO_MODE_HINTS);
1767 if ( mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS
1768 && !(mfGuestVBVACapabilities & VBVACAPS_IRQ))
1769 {
1770 HRESULT hrc = mParent->i_sendACPIMonitorHotPlugEvent();
1771 if (FAILED(hrc))
1772 return hrc;
1773 }
1774
1775 /* We currently never suppress the VMMDev hint if the guest has requested
1776 * it. Specifically the video graphics driver may not be responsible for
1777 * screen positioning in the guest virtual desktop, and the component
1778 * responsible may want to get the hint from VMMDev. */
1779 VMMDev *pVMMDev = mParent->i_getVMMDev();
1780 if (pVMMDev)
1781 {
1782 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1783 if (pVMMDevPort)
1784 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel,
1785 aDisplay, aOriginX, aOriginY,
1786 RT_BOOL(aEnabled), RT_BOOL(aChangeOrigin));
1787 }
1788 return S_OK;
1789}
1790
1791HRESULT Display::setSeamlessMode(BOOL enabled)
1792{
1793 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1794
1795 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
1796 alock.release();
1797
1798 VMMDev *pVMMDev = mParent->i_getVMMDev();
1799 if (pVMMDev)
1800 {
1801 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1802 if (pVMMDevPort)
1803 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
1804 }
1805
1806#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1807 if (!enabled)
1808 {
1809 VMMDev *vmmDev = mParent->i_getVMMDev();
1810 if (mfIsCr3DEnabled && vmmDev)
1811 {
1812 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof(VBOXCRCMDCTL_HGCM));
1813 if (!pData)
1814 {
1815 AssertMsgFailed(("RTMemAlloc failed\n"));
1816 return VERR_NO_MEMORY;
1817 }
1818
1819 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1820 pData->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1821
1822 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1823 pData->aParms[0].u.pointer.addr = NULL;
1824 pData->aParms[0].u.pointer.size = 0; /* <- means null rects, NULL pRects address and 0 rects means "disable" */
1825
1826 int rc = i_crCtlSubmit(&pData->Hdr, sizeof(*pData), i_displayCrCmdFree, pData);
1827 if (!RT_SUCCESS(rc))
1828 {
1829 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
1830 RTMemFree(pData);
1831 }
1832 }
1833 }
1834#endif
1835 return S_OK;
1836}
1837
1838#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1839BOOL Display::i_displayCheckTakeScreenshotCrOgl(Display *pDisplay, ULONG aScreenId, uint8_t *pu8Data,
1840 uint32_t u32Width, uint32_t u32Height)
1841{
1842 if ( pDisplay->mfIsCr3DEnabled
1843 && pDisplay->mCrOglCallbacks.pfnHasData
1844 && pDisplay->mCrOglCallbacks.pfnHasData())
1845 {
1846 VMMDev *pVMMDev = pDisplay->mParent->i_getVMMDev();
1847 if (pVMMDev)
1848 {
1849 CRVBOXHGCMTAKESCREENSHOT *pScreenshot = (CRVBOXHGCMTAKESCREENSHOT*)RTMemAlloc(sizeof(*pScreenshot));
1850 if (pScreenshot)
1851 {
1852 /* screen id or CRSCREEN_ALL to specify all enabled */
1853 pScreenshot->u32Screen = aScreenId;
1854 pScreenshot->u32Width = u32Width;
1855 pScreenshot->u32Height = u32Height;
1856 pScreenshot->u32Pitch = u32Width * 4;
1857 pScreenshot->pvBuffer = pu8Data;
1858 pScreenshot->pvContext = NULL;
1859 pScreenshot->pfnScreenshotBegin = NULL;
1860 pScreenshot->pfnScreenshotPerform = NULL;
1861 pScreenshot->pfnScreenshotEnd = NULL;
1862
1863 VBOXCRCMDCTL_HGCM data;
1864 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1865 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
1866
1867 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1868 data.aParms[0].u.pointer.addr = pScreenshot;
1869 data.aParms[0].u.pointer.size = sizeof(*pScreenshot);
1870
1871 int rc = pDisplay->i_crCtlSubmitSync(&data.Hdr, sizeof(data));
1872
1873 RTMemFree(pScreenshot);
1874
1875 if (RT_SUCCESS(rc))
1876 return TRUE;
1877 AssertMsgFailed(("failed to get screenshot data from crOgl (rc=%Rrc)\n", rc));
1878 /* fall back to the non-3d mechanism */
1879 }
1880 }
1881 }
1882 return FALSE;
1883}
1884#endif
1885
1886int Display::i_displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppu8Data, size_t *pcbData,
1887 uint32_t *pu32Width, uint32_t *pu32Height)
1888{
1889 int rc;
1890
1891 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
1892 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
1893 {
1894 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppu8Data, pcbData, pu32Width, pu32Height);
1895 }
1896 else if (aScreenId < pDisplay->mcMonitors)
1897 {
1898 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
1899
1900 uint32_t width = pFBInfo->w;
1901 uint32_t height = pFBInfo->h;
1902
1903 /* Allocate 32 bit per pixel bitmap. */
1904 size_t cbRequired = width * 4 * height;
1905
1906 if (cbRequired)
1907 {
1908 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbRequired);
1909
1910 if (pu8Data == NULL)
1911 {
1912 rc = VERR_NO_MEMORY;
1913 }
1914 else
1915 {
1916 /* Copy guest VRAM to the allocated 32bpp buffer. */
1917 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
1918 int32_t xSrc = 0;
1919 int32_t ySrc = 0;
1920 uint32_t u32SrcWidth = width;
1921 uint32_t u32SrcHeight = height;
1922 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
1923 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
1924
1925 uint8_t *pu8Dst = pu8Data;
1926 int32_t xDst = 0;
1927 int32_t yDst = 0;
1928 uint32_t u32DstWidth = u32SrcWidth;
1929 uint32_t u32DstHeight = u32SrcHeight;
1930 uint32_t u32DstLineSize = u32DstWidth * 4;
1931 uint32_t u32DstBitsPerPixel = 32;
1932
1933 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
1934 width, height,
1935 pu8Src,
1936 xSrc, ySrc,
1937 u32SrcWidth, u32SrcHeight,
1938 u32SrcLineSize, u32SrcBitsPerPixel,
1939 pu8Dst,
1940 xDst, yDst,
1941 u32DstWidth, u32DstHeight,
1942 u32DstLineSize, u32DstBitsPerPixel);
1943 if (RT_SUCCESS(rc))
1944 {
1945 *ppu8Data = pu8Data;
1946 *pcbData = cbRequired;
1947 *pu32Width = width;
1948 *pu32Height = height;
1949 }
1950 else
1951 {
1952 RTMemFree(pu8Data);
1953
1954 /* CopyRect can fail if VBVA was paused in VGA device, retry using the generic method. */
1955 if ( rc == VERR_INVALID_STATE
1956 && aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1957 {
1958 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort,
1959 ppu8Data, pcbData, pu32Width, pu32Height);
1960 }
1961 }
1962 }
1963 }
1964 else
1965 {
1966 /* No image. */
1967 *ppu8Data = NULL;
1968 *pcbData = 0;
1969 *pu32Width = 0;
1970 *pu32Height = 0;
1971 rc = VINF_SUCCESS;
1972 }
1973 }
1974 else
1975 {
1976 rc = VERR_INVALID_PARAMETER;
1977 }
1978
1979 return rc;
1980}
1981
1982static int i_displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
1983 BYTE *address, ULONG width, ULONG height)
1984{
1985 uint8_t *pu8Data = NULL;
1986 size_t cbData = 0;
1987 uint32_t cx = 0;
1988 uint32_t cy = 0;
1989 int vrc = VINF_SUCCESS;
1990
1991# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1992 if (Display::i_displayCheckTakeScreenshotCrOgl(pDisplay, aScreenId, (uint8_t*)address, width, height))
1993 return VINF_SUCCESS;
1994#endif
1995
1996 int cRetries = 5;
1997
1998 while (cRetries-- > 0)
1999 {
2000 /* Note! Not sure if the priority call is such a good idea here, but
2001 it would be nice to have an accurate screenshot for the bug
2002 report if the VM deadlocks. */
2003 vrc = VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)Display::i_displayTakeScreenshotEMT, 6,
2004 pDisplay, aScreenId, &pu8Data, &cbData, &cx, &cy);
2005 if (vrc != VERR_TRY_AGAIN)
2006 {
2007 break;
2008 }
2009
2010 RTThreadSleep(10);
2011 }
2012
2013 if (RT_SUCCESS(vrc) && pu8Data)
2014 {
2015 if (cx == width && cy == height)
2016 {
2017 /* No scaling required. */
2018 memcpy(address, pu8Data, cbData);
2019 }
2020 else
2021 {
2022 /* Scale. */
2023 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2024
2025 uint8_t *dst = address;
2026 uint8_t *src = pu8Data;
2027 int dstW = width;
2028 int dstH = height;
2029 int srcW = cx;
2030 int srcH = cy;
2031 int iDeltaLine = cx * 4;
2032
2033 BitmapScale32(dst,
2034 dstW, dstH,
2035 src,
2036 iDeltaLine,
2037 srcW, srcH);
2038 }
2039
2040 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2041 {
2042 /* This can be called from any thread. */
2043 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pu8Data);
2044 }
2045 else
2046 {
2047 RTMemFree(pu8Data);
2048 }
2049 }
2050
2051 return vrc;
2052}
2053
2054HRESULT Display::takeScreenShotWorker(ULONG aScreenId,
2055 BYTE *aAddress,
2056 ULONG aWidth,
2057 ULONG aHeight,
2058 BitmapFormat_T aBitmapFormat,
2059 ULONG *pcbOut)
2060{
2061 HRESULT rc = S_OK;
2062
2063 /* Do not allow too small and too large screenshots. This also filters out negative
2064 * values passed as either 'aWidth' or 'aHeight'.
2065 */
2066 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2067 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2068
2069 if ( aBitmapFormat != BitmapFormat_BGR0
2070 && aBitmapFormat != BitmapFormat_BGRA
2071 && aBitmapFormat != BitmapFormat_RGBA
2072 && aBitmapFormat != BitmapFormat_PNG)
2073 {
2074 return setError(E_NOTIMPL,
2075 tr("Unsupported screenshot format 0x%08X"), aBitmapFormat);
2076 }
2077
2078 Console::SafeVMPtr ptrVM(mParent);
2079 if (!ptrVM.isOk())
2080 return ptrVM.rc();
2081
2082 int vrc = i_displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, aAddress, aWidth, aHeight);
2083
2084 if (RT_SUCCESS(vrc))
2085 {
2086 const size_t cbData = aWidth * 4 * aHeight;
2087
2088 /* Most of uncompressed formats. */
2089 *pcbOut = (ULONG)cbData;
2090
2091 if (aBitmapFormat == BitmapFormat_BGR0)
2092 {
2093 /* Do nothing. */
2094 }
2095 else if (aBitmapFormat == BitmapFormat_BGRA)
2096 {
2097 uint32_t *pu32 = (uint32_t *)aAddress;
2098 size_t cPixels = aWidth * aHeight;
2099 while (cPixels--)
2100 {
2101 *pu32++ |= UINT32_C(0xFF000000);
2102 }
2103 }
2104 else if (aBitmapFormat == BitmapFormat_RGBA)
2105 {
2106 uint8_t *pu8 = aAddress;
2107 size_t cPixels = aWidth * aHeight;
2108 while (cPixels--)
2109 {
2110 uint8_t u8 = pu8[0];
2111 pu8[0] = pu8[2];
2112 pu8[2] = u8;
2113 pu8[3] = 0xFF;
2114
2115 pu8 += 4;
2116 }
2117 }
2118 else if (aBitmapFormat == BitmapFormat_PNG)
2119 {
2120 uint8_t *pu8PNG = NULL;
2121 uint32_t cbPNG = 0;
2122 uint32_t cxPNG = 0;
2123 uint32_t cyPNG = 0;
2124
2125 vrc = DisplayMakePNG(aAddress, aWidth, aHeight, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2126 if (RT_SUCCESS(vrc))
2127 {
2128 if (cbPNG <= cbData)
2129 {
2130 memcpy(aAddress, pu8PNG, cbPNG);
2131 *pcbOut = cbPNG;
2132 }
2133 else
2134 {
2135 rc = setError(E_FAIL,
2136 tr("PNG is larger than 32bpp bitmap"));
2137 }
2138 }
2139 else
2140 {
2141 rc = setError(VBOX_E_IPRT_ERROR,
2142 tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2143 }
2144 RTMemFree(pu8PNG);
2145 }
2146 }
2147 else if (vrc == VERR_TRY_AGAIN)
2148 rc = setError(E_UNEXPECTED,
2149 tr("Screenshot is not available at this time"));
2150 else if (RT_FAILURE(vrc))
2151 rc = setError(VBOX_E_IPRT_ERROR,
2152 tr("Could not take a screenshot (%Rrc)"), vrc);
2153
2154 return rc;
2155}
2156
2157HRESULT Display::takeScreenShot(ULONG aScreenId,
2158 BYTE *aAddress,
2159 ULONG aWidth,
2160 ULONG aHeight,
2161 BitmapFormat_T aBitmapFormat)
2162{
2163 HRESULT rc = S_OK;
2164
2165 LogRelFlowFunc(("[%d] address=%p, width=%d, height=%d, format 0x%08X\n",
2166 aScreenId, aAddress, aWidth, aHeight, aBitmapFormat));
2167
2168 ULONG cbOut = 0;
2169 rc = takeScreenShotWorker(aScreenId, aAddress, aWidth, aHeight, aBitmapFormat, &cbOut);
2170 NOREF(cbOut);
2171
2172 LogRelFlowFunc(("%Rhrc\n", rc));
2173 return rc;
2174}
2175
2176HRESULT Display::takeScreenShotToArray(ULONG aScreenId,
2177 ULONG aWidth,
2178 ULONG aHeight,
2179 BitmapFormat_T aBitmapFormat,
2180 std::vector<BYTE> &aScreenData)
2181{
2182 HRESULT rc = S_OK;
2183
2184 LogRelFlowFunc(("[%d] width=%d, height=%d, format 0x%08X\n",
2185 aScreenId, aWidth, aHeight, aBitmapFormat));
2186
2187 /* Do not allow too small and too large screenshots. This also filters out negative
2188 * values passed as either 'aWidth' or 'aHeight'.
2189 */
2190 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2191 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2192
2193 const size_t cbData = aWidth * 4 * aHeight;
2194 aScreenData.resize(cbData);
2195
2196 ULONG cbOut = 0;
2197 rc = takeScreenShotWorker(aScreenId, &aScreenData.front(), aWidth, aHeight, aBitmapFormat, &cbOut);
2198 if (FAILED(rc))
2199 cbOut = 0;
2200
2201 aScreenData.resize(cbOut);
2202
2203 LogRelFlowFunc(("%Rhrc\n", rc));
2204 return rc;
2205}
2206
2207
2208int Display::i_VideoCaptureEnableScreens(ComSafeArrayIn(BOOL, aScreens))
2209{
2210#ifdef VBOX_WITH_VPX
2211 com::SafeArray<BOOL> Screens(ComSafeArrayInArg(aScreens));
2212 for (unsigned i = 0; i < Screens.size(); i++)
2213 maVideoRecEnabled[i] = RT_BOOL(Screens[i]);
2214 return VINF_SUCCESS;
2215#else
2216 return VERR_NOT_IMPLEMENTED;
2217#endif
2218}
2219
2220/**
2221 * Start video capturing. Does nothing if capturing is already active.
2222 */
2223int Display::i_VideoCaptureStart()
2224{
2225#ifdef VBOX_WITH_VPX
2226 if (VideoRecIsEnabled(mpVideoRecCtx))
2227 return VINF_SUCCESS;
2228
2229 int rc = VideoRecContextCreate(&mpVideoRecCtx, mcMonitors);
2230 if (RT_FAILURE(rc))
2231 {
2232 LogFlow(("Failed to create video recording context (%Rrc)!\n", rc));
2233 return rc;
2234 }
2235 ComPtr<IMachine> pMachine = mParent->i_machine();
2236 com::SafeArray<BOOL> screens;
2237 HRESULT hrc = pMachine->COMGETTER(VideoCaptureScreens)(ComSafeArrayAsOutParam(screens));
2238 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2239 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
2240 maVideoRecEnabled[i] = i < screens.size() && screens[i];
2241 ULONG ulWidth;
2242 hrc = pMachine->COMGETTER(VideoCaptureWidth)(&ulWidth);
2243 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2244 ULONG ulHeight;
2245 hrc = pMachine->COMGETTER(VideoCaptureHeight)(&ulHeight);
2246 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2247 ULONG ulRate;
2248 hrc = pMachine->COMGETTER(VideoCaptureRate)(&ulRate);
2249 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2250 ULONG ulFPS;
2251 hrc = pMachine->COMGETTER(VideoCaptureFPS)(&ulFPS);
2252 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2253 BSTR strFile;
2254 hrc = pMachine->COMGETTER(VideoCaptureFile)(&strFile);
2255 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2256 ULONG ulMaxTime;
2257 hrc = pMachine->COMGETTER(VideoCaptureMaxTime)(&ulMaxTime);
2258 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2259 ULONG ulMaxSize;
2260 hrc = pMachine->COMGETTER(VideoCaptureMaxFileSize)(&ulMaxSize);
2261 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2262 BSTR strOptions;
2263 hrc = pMachine->COMGETTER(VideoCaptureOptions)(&strOptions);
2264 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
2265
2266 RTTIMESPEC ts;
2267 RTTimeNow(&ts);
2268 RTTIME time;
2269 RTTimeExplode(&time, &ts);
2270 for (unsigned uScreen = 0; uScreen < mcMonitors; uScreen++)
2271 {
2272 char *pszAbsPath = RTPathAbsDup(com::Utf8Str(strFile).c_str());
2273 char *pszSuff = RTPathSuffix(pszAbsPath);
2274 if (pszSuff)
2275 pszSuff = RTStrDup(pszSuff);
2276 RTPathStripSuffix(pszAbsPath);
2277 if (!pszAbsPath)
2278 rc = VERR_INVALID_PARAMETER;
2279 if (!pszSuff)
2280 pszSuff = RTStrDup(".webm");
2281 char *pszName = NULL;
2282 if (RT_SUCCESS(rc))
2283 {
2284 if (mcMonitors > 1)
2285 rc = RTStrAPrintf(&pszName, "%s-%u%s", pszAbsPath, uScreen+1, pszSuff);
2286 else
2287 rc = RTStrAPrintf(&pszName, "%s%s", pszAbsPath, pszSuff);
2288 }
2289 if (RT_SUCCESS(rc))
2290 {
2291 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
2292 pszName, ulWidth, ulHeight,
2293 ulRate, ulFPS, ulMaxTime,
2294 ulMaxSize, com::Utf8Str(strOptions).c_str());
2295 if (rc == VERR_ALREADY_EXISTS)
2296 {
2297 RTStrFree(pszName);
2298 pszName = NULL;
2299
2300 if (mcMonitors > 1)
2301 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ-%u%s",
2302 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2303 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2304 uScreen+1, pszSuff);
2305 else
2306 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ%s",
2307 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
2308 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
2309 pszSuff);
2310 if (RT_SUCCESS(rc))
2311 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
2312 pszName, ulWidth, ulHeight, ulRate,
2313 ulFPS, ulMaxTime,
2314 ulMaxSize, com::Utf8Str(strOptions).c_str());
2315 }
2316 }
2317
2318 if (RT_SUCCESS(rc))
2319 LogRel(("WebM/VP8 video recording screen #%u with %ux%u @ %u kbps, %u fps to '%s' enabled.\n",
2320 uScreen, ulWidth, ulHeight, ulRate, ulFPS, pszName));
2321 else
2322 LogRel(("Failed to initialize video recording context #%u (%Rrc)!\n", uScreen, rc));
2323 RTStrFree(pszName);
2324 RTStrFree(pszSuff);
2325 RTStrFree(pszAbsPath);
2326 }
2327 return rc;
2328#else
2329 return VERR_NOT_IMPLEMENTED;
2330#endif
2331}
2332
2333/**
2334 * Stop video capturing. Does nothing if video capturing is not active.
2335 */
2336void Display::i_VideoCaptureStop()
2337{
2338#ifdef VBOX_WITH_VPX
2339 if (VideoRecIsEnabled(mpVideoRecCtx))
2340 LogRel(("WebM/VP8 video recording stopped.\n"));
2341 VideoRecContextClose(mpVideoRecCtx);
2342 mpVideoRecCtx = NULL;
2343#endif
2344}
2345
2346int Display::i_drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address,
2347 ULONG x, ULONG y, ULONG width, ULONG height)
2348{
2349 int rc = VINF_SUCCESS;
2350
2351 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2352
2353 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2354 {
2355 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2356 }
2357 else if (aScreenId < pDisplay->mcMonitors)
2358 {
2359 /* Copy the bitmap to the guest VRAM. */
2360 const uint8_t *pu8Src = address;
2361 int32_t xSrc = 0;
2362 int32_t ySrc = 0;
2363 uint32_t u32SrcWidth = width;
2364 uint32_t u32SrcHeight = height;
2365 uint32_t u32SrcLineSize = width * 4;
2366 uint32_t u32SrcBitsPerPixel = 32;
2367
2368 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2369 int32_t xDst = x;
2370 int32_t yDst = y;
2371 uint32_t u32DstWidth = pFBInfo->w;
2372 uint32_t u32DstHeight = pFBInfo->h;
2373 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2374 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2375
2376 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2377 width, height,
2378 pu8Src,
2379 xSrc, ySrc,
2380 u32SrcWidth, u32SrcHeight,
2381 u32SrcLineSize, u32SrcBitsPerPixel,
2382 pu8Dst,
2383 xDst, yDst,
2384 u32DstWidth, u32DstHeight,
2385 u32DstLineSize, u32DstBitsPerPixel);
2386 if (RT_SUCCESS(rc))
2387 {
2388 if (!pFBInfo->pSourceBitmap.isNull())
2389 {
2390 /* Update the changed screen area. When source bitmap uses VRAM directly, just notify
2391 * frontend to update. And for default format, render the guest VRAM to the source bitmap.
2392 */
2393 if ( pFBInfo->fDefaultFormat
2394 && !pFBInfo->fDisabled)
2395 {
2396 BYTE *pAddress = NULL;
2397 ULONG ulWidth = 0;
2398 ULONG ulHeight = 0;
2399 ULONG ulBitsPerPixel = 0;
2400 ULONG ulBytesPerLine = 0;
2401 ULONG ulPixelFormat = 0;
2402
2403 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2404 &ulWidth,
2405 &ulHeight,
2406 &ulBitsPerPixel,
2407 &ulBytesPerLine,
2408 &ulPixelFormat);
2409 if (SUCCEEDED(hrc))
2410 {
2411 pu8Src = pFBInfo->pu8FramebufferVRAM;
2412 xSrc = x;
2413 ySrc = y;
2414 u32SrcWidth = pFBInfo->w;
2415 u32SrcHeight = pFBInfo->h;
2416 u32SrcLineSize = pFBInfo->u32LineSize;
2417 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2418
2419 /* Default format is 32 bpp. */
2420 pu8Dst = pAddress;
2421 xDst = xSrc;
2422 yDst = ySrc;
2423 u32DstWidth = u32SrcWidth;
2424 u32DstHeight = u32SrcHeight;
2425 u32DstLineSize = u32DstWidth * 4;
2426 u32DstBitsPerPixel = 32;
2427
2428 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2429 width, height,
2430 pu8Src,
2431 xSrc, ySrc,
2432 u32SrcWidth, u32SrcHeight,
2433 u32SrcLineSize, u32SrcBitsPerPixel,
2434 pu8Dst,
2435 xDst, yDst,
2436 u32DstWidth, u32DstHeight,
2437 u32DstLineSize, u32DstBitsPerPixel);
2438 }
2439 }
2440 }
2441
2442 pDisplay->i_handleDisplayUpdate(aScreenId, x, y, width, height);
2443 }
2444 }
2445 else
2446 {
2447 rc = VERR_INVALID_PARAMETER;
2448 }
2449
2450 if (RT_SUCCESS(rc))
2451 pDisplay->mParent->i_consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
2452
2453 return rc;
2454}
2455
2456HRESULT Display::drawToScreen(ULONG aScreenId, BYTE *aAddress, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2457{
2458 /// @todo (r=dmik) this function may take too long to complete if the VM
2459 // is doing something like saving state right now. Which, in case if it
2460 // is called on the GUI thread, will make it unresponsive. We should
2461 // check the machine state here (by enclosing the check and VMRequCall
2462 // within the Console lock to make it atomic).
2463
2464 LogRelFlowFunc(("aAddress=%p, x=%d, y=%d, width=%d, height=%d\n",
2465 (void *)aAddress, aX, aY, aWidth, aHeight));
2466
2467 CheckComArgExpr(aWidth, aWidth != 0);
2468 CheckComArgExpr(aHeight, aHeight != 0);
2469
2470 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2471
2472 CHECK_CONSOLE_DRV(mpDrv);
2473
2474 Console::SafeVMPtr ptrVM(mParent);
2475 if (!ptrVM.isOk())
2476 return ptrVM.rc();
2477
2478 /* Release lock because the call scheduled on EMT may also try to take it. */
2479 alock.release();
2480
2481 /*
2482 * Again we're lazy and make the graphics device do all the
2483 * dirty conversion work.
2484 */
2485 int rcVBox = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_drawToScreenEMT, 7,
2486 this, aScreenId, aAddress, aX, aY, aWidth, aHeight);
2487
2488 /*
2489 * If the function returns not supported, we'll have to do all the
2490 * work ourselves using the framebuffer.
2491 */
2492 HRESULT rc = S_OK;
2493 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2494 {
2495 /** @todo implement generic fallback for screen blitting. */
2496 rc = E_NOTIMPL;
2497 }
2498 else if (RT_FAILURE(rcVBox))
2499 rc = setError(VBOX_E_IPRT_ERROR,
2500 tr("Could not draw to the screen (%Rrc)"), rcVBox);
2501//@todo
2502// else
2503// {
2504// /* All ok. Redraw the screen. */
2505// handleDisplayUpdate (x, y, width, height);
2506// }
2507
2508 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2509 return rc;
2510}
2511
2512int Display::i_InvalidateAndUpdateEMT(Display *pDisplay, unsigned uId, bool fUpdateAll)
2513{
2514 LogRelFlowFunc(("uId=%d, fUpdateAll %d\n", uId, fUpdateAll));
2515
2516 unsigned uScreenId;
2517 for (uScreenId = (fUpdateAll ? 0 : uId); uScreenId < pDisplay->mcMonitors; uScreenId++)
2518 {
2519 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2520
2521 if ( !pFBInfo->fVBVAEnabled
2522 && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2523 {
2524 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort, /* fFailOnResize = */ true);
2525 }
2526 else
2527 {
2528 if (!pFBInfo->fDisabled)
2529 {
2530 /* Render complete VRAM screen to the framebuffer.
2531 * When framebuffer uses VRAM directly, just notify it to update.
2532 */
2533 if (pFBInfo->fDefaultFormat && !pFBInfo->pSourceBitmap.isNull())
2534 {
2535 BYTE *pAddress = NULL;
2536 ULONG ulWidth = 0;
2537 ULONG ulHeight = 0;
2538 ULONG ulBitsPerPixel = 0;
2539 ULONG ulBytesPerLine = 0;
2540 ULONG ulPixelFormat = 0;
2541
2542 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2543 &ulWidth,
2544 &ulHeight,
2545 &ulBitsPerPixel,
2546 &ulBytesPerLine,
2547 &ulPixelFormat);
2548 if (SUCCEEDED(hrc))
2549 {
2550 uint32_t width = pFBInfo->w;
2551 uint32_t height = pFBInfo->h;
2552
2553 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2554 int32_t xSrc = 0;
2555 int32_t ySrc = 0;
2556 uint32_t u32SrcWidth = pFBInfo->w;
2557 uint32_t u32SrcHeight = pFBInfo->h;
2558 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2559 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2560
2561 /* Default format is 32 bpp. */
2562 uint8_t *pu8Dst = pAddress;
2563 int32_t xDst = xSrc;
2564 int32_t yDst = ySrc;
2565 uint32_t u32DstWidth = u32SrcWidth;
2566 uint32_t u32DstHeight = u32SrcHeight;
2567 uint32_t u32DstLineSize = u32DstWidth * 4;
2568 uint32_t u32DstBitsPerPixel = 32;
2569
2570 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
2571 * implies resize of Framebuffer is in progress and
2572 * copyrect should not be called.
2573 */
2574 if (ulWidth == pFBInfo->w && ulHeight == pFBInfo->h)
2575 {
2576 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2577 width, height,
2578 pu8Src,
2579 xSrc, ySrc,
2580 u32SrcWidth, u32SrcHeight,
2581 u32SrcLineSize, u32SrcBitsPerPixel,
2582 pu8Dst,
2583 xDst, yDst,
2584 u32DstWidth, u32DstHeight,
2585 u32DstLineSize, u32DstBitsPerPixel);
2586 }
2587 }
2588 }
2589
2590 pDisplay->i_handleDisplayUpdate(uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2591 }
2592 }
2593 if (!fUpdateAll)
2594 break;
2595 }
2596 LogRelFlowFunc(("done\n"));
2597 return VINF_SUCCESS;
2598}
2599
2600/**
2601 * Does a full invalidation of the VM display and instructs the VM
2602 * to update it immediately.
2603 *
2604 * @returns COM status code
2605 */
2606
2607HRESULT Display::invalidateAndUpdate()
2608{
2609 LogRelFlowFunc(("\n"));
2610
2611 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2612
2613 CHECK_CONSOLE_DRV(mpDrv);
2614
2615 Console::SafeVMPtr ptrVM(mParent);
2616 if (!ptrVM.isOk())
2617 return ptrVM.rc();
2618
2619 HRESULT rc = S_OK;
2620
2621 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
2622
2623 /* Have to release the lock when calling EMT. */
2624 alock.release();
2625
2626 int rcVBox = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2627 3, this, 0, true);
2628 alock.acquire();
2629
2630 if (RT_FAILURE(rcVBox))
2631 rc = setError(VBOX_E_IPRT_ERROR,
2632 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2633
2634 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2635 return rc;
2636}
2637
2638HRESULT Display::invalidateAndUpdateScreen(ULONG aScreenId)
2639{
2640 LogRelFlowFunc(("\n"));
2641
2642 HRESULT rc = S_OK;
2643
2644 Console::SafeVMPtr ptrVM(mParent);
2645 if (!ptrVM.isOk())
2646 return ptrVM.rc();
2647
2648 int rcVBox = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2649 3, this, aScreenId, false);
2650 if (RT_FAILURE(rcVBox))
2651 rc = setError(VBOX_E_IPRT_ERROR,
2652 tr("Could not invalidate and update the screen %d (%Rrc)"), aScreenId, rcVBox);
2653
2654 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2655 return rc;
2656}
2657
2658HRESULT Display::completeVHWACommand(BYTE *aCommand)
2659{
2660#ifdef VBOX_WITH_VIDEOHWACCEL
2661 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)aCommand);
2662 return S_OK;
2663#else
2664 return E_NOTIMPL;
2665#endif
2666}
2667
2668HRESULT Display::viewportChanged(ULONG aScreenId, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2669{
2670 AssertMsgReturn(aScreenId < mcMonitors, ("aScreendId=%d mcMonitors=%d\n", aScreenId, mcMonitors), E_INVALIDARG);
2671
2672#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2673 if (mfIsCr3DEnabled)
2674 {
2675 int rc = i_crViewportNotify(aScreenId, aX, aY, aWidth, aHeight);
2676 if (RT_FAILURE(rc))
2677 {
2678 DISPLAYFBINFO *pFb = &maFramebuffers[aScreenId];
2679 pFb->pendingViewportInfo.fPending = true;
2680 pFb->pendingViewportInfo.x = aX;
2681 pFb->pendingViewportInfo.y = aY;
2682 pFb->pendingViewportInfo.width = aWidth;
2683 pFb->pendingViewportInfo.height = aHeight;
2684 }
2685 }
2686#endif /* VBOX_WITH_CROGL && VBOX_WITH_HGCM */
2687
2688#ifdef VBOX_WITH_VMSVGA
2689 /* The driver might not have been constructed yet */
2690 if (mpDrv)
2691 mpDrv->pUpPort->pfnSetViewPort(mpDrv->pUpPort, aScreenId, aX, aY, aWidth, aHeight);
2692#endif
2693
2694 return S_OK;
2695}
2696
2697HRESULT Display::querySourceBitmap(ULONG aScreenId,
2698 ComPtr<IDisplaySourceBitmap> &aDisplaySourceBitmap)
2699{
2700 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2701
2702 Console::SafeVMPtr ptrVM(mParent);
2703 if (!ptrVM.isOk())
2704 return ptrVM.rc();
2705
2706 bool fSetRenderVRAM = false;
2707 bool fInvalidate = false;
2708
2709 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2710
2711 if (aScreenId >= mcMonitors)
2712 return setError(E_INVALIDARG, tr("QuerySourceBitmap: Invalid screen %d (total %d)"),
2713 aScreenId, mcMonitors);
2714
2715 if (!mfSourceBitmapEnabled)
2716 {
2717 aDisplaySourceBitmap = NULL;
2718 return E_FAIL;
2719 }
2720
2721 HRESULT hr = S_OK;
2722
2723 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2724 if (pFBInfo->pSourceBitmap.isNull())
2725 {
2726 /* Create a new object. */
2727 ComObjPtr<DisplaySourceBitmap> obj;
2728 hr = obj.createObject();
2729 if (SUCCEEDED(hr))
2730 hr = obj->init(this, aScreenId, pFBInfo);
2731
2732 if (SUCCEEDED(hr))
2733 {
2734 bool fDefaultFormat = !obj->i_usesVRAM();
2735
2736 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2737 {
2738 /* Start buffer updates. */
2739 BYTE *pAddress = NULL;
2740 ULONG ulWidth = 0;
2741 ULONG ulHeight = 0;
2742 ULONG ulBitsPerPixel = 0;
2743 ULONG ulBytesPerLine = 0;
2744 ULONG ulPixelFormat = 0;
2745
2746 obj->QueryBitmapInfo(&pAddress,
2747 &ulWidth,
2748 &ulHeight,
2749 &ulBitsPerPixel,
2750 &ulBytesPerLine,
2751 &ulPixelFormat);
2752
2753 mpDrv->IConnector.pu8Data = pAddress;
2754 mpDrv->IConnector.cbScanline = ulBytesPerLine;
2755 mpDrv->IConnector.cBits = ulBitsPerPixel;
2756 mpDrv->IConnector.cx = ulWidth;
2757 mpDrv->IConnector.cy = ulHeight;
2758
2759 fSetRenderVRAM = fDefaultFormat;
2760 }
2761
2762 /* Make sure that the bitmap contains the latest image. */
2763 fInvalidate = fDefaultFormat;
2764
2765 pFBInfo->pSourceBitmap = obj;
2766 pFBInfo->fDefaultFormat = fDefaultFormat;
2767 }
2768 }
2769
2770 if (SUCCEEDED(hr))
2771 {
2772 pFBInfo->pSourceBitmap.queryInterfaceTo(aDisplaySourceBitmap.asOutParam());
2773 }
2774
2775 /* Leave the IDisplay lock because the VGA device must not be called under it. */
2776 alock.release();
2777
2778 if (SUCCEEDED(hr))
2779 {
2780 if (fSetRenderVRAM)
2781 {
2782 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, true);
2783 }
2784
2785 if (fInvalidate)
2786 VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2787 3, this, aScreenId, false);
2788 }
2789
2790 LogRelFlowFunc(("%Rhrc\n", hr));
2791 return hr;
2792}
2793
2794// wrapped IEventListener method
2795HRESULT Display::handleEvent(const ComPtr<IEvent> &aEvent)
2796{
2797 VBoxEventType_T aType = VBoxEventType_Invalid;
2798
2799 aEvent->COMGETTER(Type)(&aType);
2800 switch (aType)
2801 {
2802 case VBoxEventType_OnStateChanged:
2803 {
2804 ComPtr<IStateChangedEvent> scev = aEvent;
2805 Assert(scev);
2806 MachineState_T machineState;
2807 scev->COMGETTER(State)(&machineState);
2808 if ( machineState == MachineState_Running
2809 || machineState == MachineState_Teleporting
2810 || machineState == MachineState_LiveSnapshotting
2811 || machineState == MachineState_DeletingSnapshotOnline
2812 )
2813 {
2814 LogRelFlowFunc(("Machine is running.\n"));
2815
2816#ifdef VBOX_WITH_CROGL
2817 i_crOglWindowsShow(true);
2818#endif
2819 }
2820 else
2821 {
2822#ifdef VBOX_WITH_CROGL
2823 if (machineState == MachineState_Paused)
2824 i_crOglWindowsShow(false);
2825#endif
2826 }
2827 break;
2828 }
2829 default:
2830 AssertFailed();
2831 }
2832
2833 return S_OK;
2834}
2835
2836
2837// private methods
2838/////////////////////////////////////////////////////////////////////////////
2839
2840#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2841int Display::i_crViewportNotify(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
2842{
2843 VMMDev *pVMMDev = mParent->i_getVMMDev();
2844 if (!pVMMDev)
2845 return VERR_INVALID_STATE;
2846
2847 size_t cbData = RT_UOFFSETOF(VBOXCRCMDCTL_HGCM, aParms[5]);
2848 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)alloca(cbData);
2849
2850 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2851 pData->Hdr.u32Function = SHCRGL_HOST_FN_VIEWPORT_CHANGED;
2852
2853 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
2854 pData->aParms[0].u.uint32 = aScreenId;
2855
2856 pData->aParms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
2857 pData->aParms[1].u.uint32 = x;
2858
2859 pData->aParms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
2860 pData->aParms[2].u.uint32 = y;
2861
2862 pData->aParms[3].type = VBOX_HGCM_SVC_PARM_32BIT;
2863 pData->aParms[3].u.uint32 = width;
2864
2865 pData->aParms[4].type = VBOX_HGCM_SVC_PARM_32BIT;
2866 pData->aParms[4].u.uint32 = height;
2867
2868 return i_crCtlSubmitSyncIfHasDataForScreen(aScreenId, &pData->Hdr, (uint32_t)cbData);
2869}
2870#endif
2871
2872#ifdef VBOX_WITH_CRHGSMI
2873void Display::i_setupCrHgsmiData(void)
2874{
2875 VMMDev *pVMMDev = mParent->i_getVMMDev();
2876 Assert(pVMMDev);
2877 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
2878 AssertRC(rc);
2879
2880 if (pVMMDev)
2881 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
2882 else
2883 rc = VERR_GENERAL_FAILURE;
2884
2885 if (RT_SUCCESS(rc))
2886 {
2887 Assert(mhCrOglSvc);
2888 /* setup command completion callback */
2889 VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB Completion;
2890 Completion.Hdr.enmType = VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_MAINCB;
2891 Completion.Hdr.cbCmd = sizeof(Completion);
2892 Completion.hCompletion = mpDrv->pVBVACallbacks;
2893 Completion.pfnCompletion = mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync;
2894
2895 VBOXHGCMSVCPARM parm;
2896 parm.type = VBOX_HGCM_SVC_PARM_PTR;
2897 parm.u.pointer.addr = &Completion;
2898 parm.u.pointer.size = 0;
2899
2900 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_CRHGSMI_CTL, 1, &parm);
2901 if (RT_SUCCESS(rc))
2902 mCrOglCallbacks = Completion.MainInterface;
2903 else
2904 AssertMsgFailed(("VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION failed (rc=%Rrc)\n", rc));
2905 }
2906
2907 if (RT_FAILURE(rc))
2908 mhCrOglSvc = NULL;
2909
2910 RTCritSectRwLeaveExcl(&mCrOglLock);
2911}
2912
2913void Display::i_destructCrHgsmiData(void)
2914{
2915 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
2916 AssertRC(rc);
2917 mhCrOglSvc = NULL;
2918 RTCritSectRwLeaveExcl(&mCrOglLock);
2919}
2920#endif /* VBOX_WITH_CRHGSMI */
2921
2922/**
2923 * Handle display resize event issued by the VGA device for the primary screen.
2924 *
2925 * @see PDMIDISPLAYCONNECTOR::pfnResize
2926 */
2927DECLCALLBACK(int) Display::i_displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
2928 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
2929{
2930 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2931 Display *pThis = pDrv->pDisplay;
2932
2933 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
2934 bpp, pvVRAM, cbLine, cx, cy));
2935
2936 bool f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, true, false);
2937 if (!f)
2938 {
2939 /* This is a result of recursive call when the source bitmap is being updated
2940 * during a VGA resize. Tell the VGA device to ignore the call.
2941 *
2942 * @todo It is a workaround, actually pfnUpdateDisplayAll must
2943 * fail on resize.
2944 */
2945 LogRel(("displayResizeCallback: already processing\n"));
2946 return VINF_VGA_RESIZE_IN_PROGRESS;
2947 }
2948
2949 int rc = pThis->i_handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, VBVA_SCREEN_F_ACTIVE);
2950
2951 /* Restore the flag. */
2952 f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, false, true);
2953 AssertRelease(f);
2954
2955 return rc;
2956}
2957
2958/**
2959 * Handle display update.
2960 *
2961 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
2962 */
2963DECLCALLBACK(void) Display::i_displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
2964 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
2965{
2966 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2967
2968#ifdef DEBUG_sunlover
2969 LogFlowFunc(("fVideoAccelEnabled = %d, %d,%d %dx%d\n",
2970 pDrv->pDisplay->mVideoAccelLegacy.fVideoAccelEnabled, x, y, cx, cy));
2971#endif /* DEBUG_sunlover */
2972
2973 /* This call does update regardless of VBVA status.
2974 * But in VBVA mode this is called only as result of
2975 * pfnUpdateDisplayAll in the VGA device.
2976 */
2977
2978 pDrv->pDisplay->i_handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
2979}
2980
2981/**
2982 * Periodic display refresh callback.
2983 *
2984 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
2985 * @thread EMT
2986 */
2987/*static*/ DECLCALLBACK(void) Display::i_displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
2988{
2989 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2990
2991#ifdef DEBUG_sunlover_2
2992 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
2993 pDrv->pDisplay->mfVideoAccelEnabled));
2994#endif /* DEBUG_sunlover_2 */
2995
2996 Display *pDisplay = pDrv->pDisplay;
2997 unsigned uScreenId;
2998
2999 int rc = pDisplay->i_videoAccelRefreshProcess(pDrv->pUpPort);
3000 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3001 {
3002 if (rc == VWRN_INVALID_STATE)
3003 {
3004 /* No VBVA do a display update. */
3005 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3006 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3007 }
3008
3009 /* Inform the VRDP server that the current display update sequence is
3010 * completed. At this moment the framebuffer memory contains a definite
3011 * image, that is synchronized with the orders already sent to VRDP client.
3012 * The server can now process redraw requests from clients or initial
3013 * fullscreen updates for new clients.
3014 */
3015 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3016 {
3017 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3018
3019 Assert(pDisplay->mParent && pDisplay->mParent->i_consoleVRDPServer());
3020 pDisplay->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, NULL, 0);
3021 }
3022 }
3023
3024#ifdef VBOX_WITH_VPX
3025 if (VideoRecIsEnabled(pDisplay->mpVideoRecCtx))
3026 {
3027 do {
3028# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3029 if (pDisplay->mfIsCr3DEnabled)
3030 {
3031 if (ASMAtomicCmpXchgU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_SUBMITTED, CRVREC_STATE_IDLE))
3032 {
3033 if ( pDisplay->mCrOglCallbacks.pfnHasData
3034 && pDisplay->mCrOglCallbacks.pfnHasData())
3035 {
3036 /* submit */
3037 VBOXCRCMDCTL_HGCM *pData = &pDisplay->mCrOglScreenshotCtl;
3038
3039 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3040 pData->Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
3041
3042 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3043 pData->aParms[0].u.pointer.addr = &pDisplay->mCrOglScreenshotData;
3044 pData->aParms[0].u.pointer.size = sizeof(pDisplay->mCrOglScreenshotData);
3045 rc = pDisplay->i_crCtlSubmit(&pData->Hdr, sizeof(*pData), Display::i_displayVRecCompletion, pDisplay);
3046 if (RT_SUCCESS(rc))
3047 break;
3048 AssertMsgFailed(("crCtlSubmit failed (rc=%Rrc)\n", rc));
3049 }
3050
3051 /* no 3D data available, or error has occured,
3052 * go the straight way */
3053 ASMAtomicWriteU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_IDLE);
3054 }
3055 else
3056 {
3057 /* record request is still in progress, don't do anything */
3058 break;
3059 }
3060 }
3061# endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3062
3063 uint64_t u64Now = RTTimeProgramMilliTS();
3064 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3065 {
3066 if (!pDisplay->maVideoRecEnabled[uScreenId])
3067 continue;
3068
3069 if (VideoRecIsFull(pDisplay->mpVideoRecCtx, uScreenId, u64Now))
3070 {
3071 pDisplay->i_VideoCaptureStop();
3072 pDisplay->mParent->i_machine()->COMSETTER(VideoCaptureEnabled)(false);
3073 break;
3074 }
3075
3076 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3077
3078 if ( !pFBInfo->pFramebuffer.isNull()
3079 && !pFBInfo->fDisabled)
3080 {
3081 rc = VERR_NOT_SUPPORTED;
3082 if ( pFBInfo->fVBVAEnabled
3083 && pFBInfo->pu8FramebufferVRAM)
3084 {
3085 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
3086 BitmapFormat_BGR,
3087 pFBInfo->u16BitsPerPixel,
3088 pFBInfo->u32LineSize, pFBInfo->w, pFBInfo->h,
3089 pFBInfo->pu8FramebufferVRAM, u64Now);
3090 }
3091 else if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && pDrv->IConnector.pu8Data)
3092 {
3093 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
3094 BitmapFormat_BGR,
3095 pDrv->IConnector.cBits,
3096 pDrv->IConnector.cbScanline, pDrv->IConnector.cx,
3097 pDrv->IConnector.cy, pDrv->IConnector.pu8Data, u64Now);
3098 }
3099 if (rc == VINF_TRY_AGAIN)
3100 break;
3101 }
3102 }
3103 } while (0);
3104 }
3105#endif /* VBOX_WITH_VPX */
3106
3107#ifdef DEBUG_sunlover_2
3108 LogFlowFunc(("leave\n"));
3109#endif /* DEBUG_sunlover_2 */
3110}
3111
3112/**
3113 * Reset notification
3114 *
3115 * @see PDMIDISPLAYCONNECTOR::pfnReset
3116 */
3117DECLCALLBACK(void) Display::i_displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3118{
3119 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3120
3121 LogRelFlowFunc(("\n"));
3122
3123 /* Disable VBVA mode. */
3124 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3125}
3126
3127/**
3128 * LFBModeChange notification
3129 *
3130 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3131 */
3132DECLCALLBACK(void) Display::i_displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3133{
3134 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3135
3136 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
3137
3138 NOREF(fEnabled);
3139
3140 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3141 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3142}
3143
3144/**
3145 * Adapter information change notification.
3146 *
3147 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3148 */
3149DECLCALLBACK(void) Display::i_displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM,
3150 uint32_t u32VRAMSize)
3151{
3152 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3153 pDrv->pDisplay->processAdapterData(pvVRAM, u32VRAMSize);
3154}
3155
3156/**
3157 * Display information change notification.
3158 *
3159 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3160 */
3161DECLCALLBACK(void) Display::i_displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface,
3162 void *pvVRAM, unsigned uScreenId)
3163{
3164 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3165 pDrv->pDisplay->processDisplayData(pvVRAM, uScreenId);
3166}
3167
3168#ifdef VBOX_WITH_VIDEOHWACCEL
3169
3170#ifndef S_FALSE
3171# define S_FALSE ((HRESULT)1L)
3172#endif
3173
3174int Display::i_handleVHWACommandProcess(PVBOXVHWACMD pCommand)
3175{
3176 unsigned id = (unsigned)pCommand->iDisplay;
3177 int rc = VINF_SUCCESS;
3178 if (id >= mcMonitors)
3179 return VERR_INVALID_PARAMETER;
3180
3181 ComPtr<IFramebuffer> pFramebuffer;
3182 AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
3183 pFramebuffer = maFramebuffers[id].pFramebuffer;
3184 bool fVHWASupported = RT_BOOL(maFramebuffers[id].u32Caps & FramebufferCapabilities_VHWA);
3185 arlock.release();
3186
3187 if (pFramebuffer == NULL || !fVHWASupported)
3188 return VERR_NOT_IMPLEMENTED; /* Implementation is not available. */
3189
3190 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3191 if (hr == S_FALSE)
3192 return VINF_SUCCESS;
3193 else if (SUCCEEDED(hr))
3194 return VINF_CALLBACK_RETURN;
3195 else if (hr == E_ACCESSDENIED)
3196 return VERR_INVALID_STATE; /* notify we can not handle request atm */
3197 else if (hr == E_NOTIMPL)
3198 return VERR_NOT_IMPLEMENTED;
3199 return VERR_GENERAL_FAILURE;
3200}
3201
3202DECLCALLBACK(int) Display::i_displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3203{
3204 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3205
3206 return pDrv->pDisplay->i_handleVHWACommandProcess(pCommand);
3207}
3208#endif
3209
3210#ifdef VBOX_WITH_CRHGSMI
3211void Display::i_handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3212{
3213 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks,
3214 (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
3215}
3216
3217void Display::i_handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3218{
3219 PVBOXVDMACMD_CHROMIUM_CTL pCtl = (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr;
3220 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, pCtl, result);
3221}
3222
3223void Display::i_handleCrHgsmiCommandProcess(PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3224{
3225 int rc = VERR_NOT_SUPPORTED;
3226 VBOXHGCMSVCPARM parm;
3227 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3228 parm.u.pointer.addr = pCmd;
3229 parm.u.pointer.size = cbCmd;
3230
3231 if (mhCrOglSvc)
3232 {
3233 VMMDev *pVMMDev = mParent->i_getVMMDev();
3234 if (pVMMDev)
3235 {
3236 /* no completion callback is specified with this call,
3237 * the CrOgl code will complete the CrHgsmi command once it processes it */
3238 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
3239 AssertRC(rc);
3240 if (RT_SUCCESS(rc))
3241 return;
3242 }
3243 else
3244 rc = VERR_INVALID_STATE;
3245 }
3246
3247 /* we are here because something went wrong with command processing, complete it */
3248 i_handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
3249}
3250
3251void Display::i_handleCrHgsmiControlProcess(PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
3252{
3253 int rc = VERR_NOT_SUPPORTED;
3254 VBOXHGCMSVCPARM parm;
3255 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3256 parm.u.pointer.addr = pCtl;
3257 parm.u.pointer.size = cbCtl;
3258
3259 if (mhCrOglSvc)
3260 {
3261 VMMDev *pVMMDev = mParent->i_getVMMDev();
3262 if (pVMMDev)
3263 {
3264 bool fCheckPendingViewport = (pCtl->enmType == VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP);
3265 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm,
3266 Display::i_displayCrHgsmiControlCompletion, this);
3267 AssertRC(rc);
3268 if (RT_SUCCESS(rc))
3269 {
3270 if (fCheckPendingViewport)
3271 {
3272 ULONG ul;
3273 for (ul = 0; ul < mcMonitors; ul++)
3274 {
3275 DISPLAYFBINFO *pFb = &maFramebuffers[ul];
3276 if (!pFb->pendingViewportInfo.fPending)
3277 continue;
3278
3279 rc = i_crViewportNotify(ul, pFb->pendingViewportInfo.x, pFb->pendingViewportInfo.y,
3280 pFb->pendingViewportInfo.width, pFb->pendingViewportInfo.height);
3281 if (RT_SUCCESS(rc))
3282 pFb->pendingViewportInfo.fPending = false;
3283 else
3284 {
3285 AssertMsgFailed(("crViewportNotify failed (rc=%Rrc)\n", rc));
3286 rc = VINF_SUCCESS;
3287 }
3288 }
3289 }
3290 return;
3291 }
3292 }
3293 else
3294 rc = VERR_INVALID_STATE;
3295 }
3296
3297 /* we are here because something went wrong with command processing, complete it */
3298 i_handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
3299}
3300
3301DECLCALLBACK(void) Display::i_displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd,
3302 uint32_t cbCmd)
3303{
3304 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3305
3306 pDrv->pDisplay->i_handleCrHgsmiCommandProcess(pCmd, cbCmd);
3307}
3308
3309DECLCALLBACK(void) Display::i_displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd,
3310 uint32_t cbCmd)
3311{
3312 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3313
3314 pDrv->pDisplay->i_handleCrHgsmiControlProcess(pCmd, cbCmd);
3315}
3316
3317DECLCALLBACK(void) Display::i_displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3318 void *pvContext)
3319{
3320 AssertMsgFailed(("not expected!\n"));
3321 Display *pDisplay = (Display *)pvContext;
3322 pDisplay->i_handleCrHgsmiCommandCompletion(result, u32Function, pParam);
3323}
3324
3325DECLCALLBACK(void) Display::i_displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3326 void *pvContext)
3327{
3328 Display *pDisplay = (Display *)pvContext;
3329 pDisplay->i_handleCrHgsmiControlCompletion(result, u32Function, pParam);
3330
3331}
3332#endif
3333
3334#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3335DECLCALLBACK(void) Display::i_displayCrHgcmCtlSubmitCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
3336 void *pvContext)
3337{
3338 VBOXCRCMDCTL *pCmd = (VBOXCRCMDCTL*)pParam->u.pointer.addr;
3339 if (pCmd->u.pfnInternal)
3340 ((PFNCRCTLCOMPLETION)pCmd->u.pfnInternal)(pCmd, pParam->u.pointer.size, result, pvContext);
3341}
3342
3343int Display::i_handleCrHgcmCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
3344 PFNCRCTLCOMPLETION pfnCompletion,
3345 void *pvCompletion)
3346{
3347 VMMDev *pVMMDev = mParent ? mParent->i_getVMMDev() : NULL;
3348 if (!pVMMDev)
3349 {
3350 AssertMsgFailed(("no vmmdev\n"));
3351 return VERR_INVALID_STATE;
3352 }
3353
3354 Assert(mhCrOglSvc);
3355 VBOXHGCMSVCPARM parm;
3356 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3357 parm.u.pointer.addr = pCmd;
3358 parm.u.pointer.size = cbCmd;
3359
3360 pCmd->u.pfnInternal = (void(*)())pfnCompletion;
3361 int rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CTL, &parm, i_displayCrHgcmCtlSubmitCompletion,
3362 pvCompletion);
3363 if (!RT_SUCCESS(rc))
3364 AssertMsgFailed(("hgcmHostFastCallAsync failed (rc=%Rrc)\n", rc));
3365
3366 return rc;
3367}
3368
3369DECLCALLBACK(int) Display::i_displayCrHgcmCtlSubmit(PPDMIDISPLAYCONNECTOR pInterface,
3370 struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
3371 PFNCRCTLCOMPLETION pfnCompletion,
3372 void *pvCompletion)
3373{
3374 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3375 Display *pThis = pDrv->pDisplay;
3376 return pThis->i_handleCrHgcmCtlSubmit(pCmd, cbCmd, pfnCompletion, pvCompletion);
3377}
3378
3379int Display::i_crCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, PFNCRCTLCOMPLETION pfnCompletion, void *pvCompletion)
3380{
3381 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3382 if (RT_SUCCESS(rc))
3383 {
3384 if (mhCrOglSvc)
3385 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmit(mpDrv->pVBVACallbacks, pCmd, cbCmd, pfnCompletion, pvCompletion);
3386 else
3387 rc = VERR_NOT_SUPPORTED;
3388
3389 RTCritSectRwLeaveShared(&mCrOglLock);
3390 }
3391 return rc;
3392}
3393
3394int Display::i_crCtlSubmitSync(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3395{
3396 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3397 if (RT_SUCCESS(rc))
3398 {
3399 if (mhCrOglSvc)
3400 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmitSync(mpDrv->pVBVACallbacks, pCmd, cbCmd);
3401 else
3402 rc = VERR_NOT_SUPPORTED;
3403
3404 RTCritSectRwLeaveShared(&mCrOglLock);
3405 }
3406 return rc;
3407}
3408
3409int Display::i_crCtlSubmitAsyncCmdCopy(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3410{
3411 VBOXCRCMDCTL* pCmdCopy = (VBOXCRCMDCTL*)RTMemAlloc(cbCmd);
3412 if (!pCmdCopy)
3413 {
3414 LogRel(("RTMemAlloc failed\n"));
3415 return VERR_NO_MEMORY;
3416 }
3417
3418 memcpy(pCmdCopy, pCmd, cbCmd);
3419
3420 int rc = i_crCtlSubmit(pCmdCopy, cbCmd, i_displayCrCmdFree, pCmdCopy);
3421 if (RT_FAILURE(rc))
3422 {
3423 LogRel(("crCtlSubmit failed (rc=%Rrc)\n", rc));
3424 RTMemFree(pCmdCopy);
3425 return rc;
3426 }
3427
3428 return VINF_SUCCESS;
3429}
3430
3431int Display::i_crCtlSubmitSyncIfHasDataForScreen(uint32_t u32ScreenID, struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
3432{
3433 int rc = RTCritSectRwEnterShared(&mCrOglLock);
3434 AssertRCReturn(rc, rc);
3435
3436 if ( mCrOglCallbacks.pfnHasDataForScreen
3437 && mCrOglCallbacks.pfnHasDataForScreen(u32ScreenID))
3438 rc = i_crCtlSubmitSync(pCmd, cbCmd);
3439 else
3440 rc = i_crCtlSubmitAsyncCmdCopy(pCmd, cbCmd);
3441
3442 RTCritSectRwLeaveShared(&mCrOglLock);
3443
3444 return rc;
3445}
3446
3447bool Display::i_handleCrVRecScreenshotBegin(uint32_t uScreen, uint64_t u64TimeStamp)
3448{
3449# if VBOX_WITH_VPX
3450 return VideoRecIsReady(mpVideoRecCtx, uScreen, u64TimeStamp);
3451# else
3452 return false;
3453# endif
3454}
3455
3456void Display::i_handleCrVRecScreenshotEnd(uint32_t uScreen, uint64_t u64TimeStamp)
3457{
3458}
3459
3460void Display::i_handleCrVRecScreenshotPerform(uint32_t uScreen,
3461 uint32_t x, uint32_t y, uint32_t uPixelFormat,
3462 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
3463 uint32_t uGuestWidth, uint32_t uGuestHeight,
3464 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
3465{
3466 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
3467# if VBOX_WITH_VPX
3468 int rc = VideoRecCopyToIntBuf(mpVideoRecCtx, uScreen, x, y,
3469 uPixelFormat,
3470 uBitsPerPixel, uBytesPerLine,
3471 uGuestWidth, uGuestHeight,
3472 pu8BufferAddress, u64TimeStamp);
3473 Assert(rc == VINF_SUCCESS /* || rc == VERR_TRY_AGAIN || rc == VINF_TRY_AGAIN*/);
3474# endif
3475}
3476
3477void Display::i_handleVRecCompletion()
3478{
3479 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
3480 ASMAtomicWriteU32(&mfCrOglVideoRecState, CRVREC_STATE_IDLE);
3481}
3482
3483#endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3484
3485HRESULT Display::notifyScaleFactorChange(ULONG aScreenId, ULONG aScaleFactorWMultiplied, ULONG aScaleFactorHMultiplied)
3486{
3487#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3488 HRESULT hr = E_UNEXPECTED;
3489
3490 if (aScreenId >= mcMonitors)
3491 return E_INVALIDARG;
3492
3493 /* 3D acceleration enabled in VM config. */
3494 if (mfIsCr3DEnabled)
3495 {
3496 /* VBoxSharedCrOpenGL HGCM host service is running. */
3497 if (mhCrOglSvc)
3498 {
3499 VMMDev *pVMMDev = mParent->i_getVMMDev();
3500 if (pVMMDev)
3501 {
3502 VBOXCRCMDCTL_HGCM *pCtl;
3503 pCtl = (VBOXCRCMDCTL_HGCM *)RTMemAlloc(sizeof(CRVBOXHGCMSETSCALEFACTOR) + sizeof(VBOXCRCMDCTL_HGCM));
3504 if (pCtl)
3505 {
3506 CRVBOXHGCMSETSCALEFACTOR *pData = (CRVBOXHGCMSETSCALEFACTOR *)(pCtl + 1);
3507 int rc;
3508
3509 pData->u32Screen = aScreenId;
3510 pData->u32ScaleFactorWMultiplied = aScaleFactorWMultiplied;
3511 pData->u32ScaleFactorHMultiplied = aScaleFactorHMultiplied;
3512
3513 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3514 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_SCALE_FACTOR;
3515 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
3516 pCtl->aParms[0].u.pointer.addr = pData;
3517 pCtl->aParms[0].u.pointer.size = sizeof(*pData);
3518
3519 rc = i_crCtlSubmitSync(&pCtl->Hdr, sizeof(*pCtl));
3520 if (RT_FAILURE(rc))
3521 AssertMsgFailed(("crCtlSubmitSync failed (rc=%Rrc)\n", rc));
3522 else
3523 hr = S_OK;
3524
3525 RTMemFree(pCtl);
3526 }
3527 else
3528 {
3529 LogRel(("Running out of memory on attempt to set OpenGL content scale factor. Ignored.\n"));
3530 hr = E_OUTOFMEMORY;
3531 }
3532 }
3533 else
3534 LogRel(("Internal error occurred on attempt to set OpenGL content scale factor. Ignored.\n"));
3535 }
3536 else
3537 LogRel(("Attempt to specify OpenGL content scale factor while corresponding HGCM host service not yet runing. Ignored.\n"));
3538 }
3539 else
3540# if 0 /** @todo Thank you so very much from anyone using VMSVGA3d! */
3541 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while 3D acceleration is disabled in VM config. Ignored.\n"));
3542# else
3543 {
3544 hr = S_OK;
3545 /* Need an interface like this here (and the #ifdefs needs adjusting):
3546 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3547 if (pUpPort && pUpPort->pfnSetScaleFactor)
3548 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3549 }
3550# endif
3551
3552 return hr;
3553#else
3554 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while corresponding functionality is disabled."));
3555 return E_UNEXPECTED;
3556#endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
3557}
3558
3559#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3560DECLCALLBACK(void) Display::i_displayCrVRecScreenshotPerform(void *pvCtx, uint32_t uScreen,
3561 uint32_t x, uint32_t y,
3562 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
3563 uint32_t uGuestWidth, uint32_t uGuestHeight,
3564 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
3565{
3566 Display *pDisplay = (Display *)pvCtx;
3567 pDisplay->i_handleCrVRecScreenshotPerform(uScreen,
3568 x, y, BitmapFormat_BGR, uBitsPerPixel,
3569 uBytesPerLine, uGuestWidth, uGuestHeight,
3570 pu8BufferAddress, u64TimeStamp);
3571}
3572
3573DECLCALLBACK(bool) Display::i_displayCrVRecScreenshotBegin(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
3574{
3575 Display *pDisplay = (Display *)pvCtx;
3576 return pDisplay->i_handleCrVRecScreenshotBegin(uScreen, u64TimeStamp);
3577}
3578
3579DECLCALLBACK(void) Display::i_displayCrVRecScreenshotEnd(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
3580{
3581 Display *pDisplay = (Display *)pvCtx;
3582 pDisplay->i_handleCrVRecScreenshotEnd(uScreen, u64TimeStamp);
3583}
3584
3585DECLCALLBACK(void) Display::i_displayVRecCompletion(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, int rc, void *pvCompletion)
3586{
3587 Display *pDisplay = (Display *)pvCompletion;
3588 pDisplay->i_handleVRecCompletion();
3589}
3590
3591#endif
3592
3593
3594#ifdef VBOX_WITH_HGSMI
3595DECLCALLBACK(int) Display::i_displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags,
3596 bool fRenderThreadMode)
3597{
3598 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3599
3600 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3601 Display *pThis = pDrv->pDisplay;
3602
3603 if (pThis->maFramebuffers[uScreenId].fVBVAEnabled && pThis->maFramebuffers[uScreenId].fRenderThreadMode != fRenderThreadMode)
3604 {
3605 LogRel(("Enabling different vbva mode\n"));
3606#ifdef DEBUG_misha
3607 AssertMsgFailed(("enabling different vbva mode\n"));
3608#endif
3609 return VERR_INVALID_STATE;
3610 }
3611
3612 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3613 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3614 pThis->maFramebuffers[uScreenId].fRenderThreadMode = fRenderThreadMode;
3615 pThis->maFramebuffers[uScreenId].fVBVAForceResize = true;
3616
3617 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3618
3619 return VINF_SUCCESS;
3620}
3621
3622DECLCALLBACK(void) Display::i_displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3623{
3624 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3625
3626 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3627 Display *pThis = pDrv->pDisplay;
3628
3629 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3630
3631 bool fRenderThreadMode = pFBInfo->fRenderThreadMode;
3632
3633 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3634 {
3635 /* Make sure that the primary screen is visible now.
3636 * The guest can't use VBVA anymore, so only only the VGA device output works.
3637 */
3638 if (pFBInfo->fDisabled)
3639 {
3640 pFBInfo->fDisabled = false;
3641 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3642 GuestMonitorChangedEventType_Enabled,
3643 uScreenId,
3644 pFBInfo->xOrigin, pFBInfo->yOrigin,
3645 pFBInfo->w, pFBInfo->h);
3646 }
3647 }
3648
3649 pFBInfo->fVBVAEnabled = false;
3650 pFBInfo->fVBVAForceResize = false;
3651 pFBInfo->fRenderThreadMode = false;
3652
3653 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3654
3655 pFBInfo->pVBVAHostFlags = NULL;
3656
3657 if (!fRenderThreadMode && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3658 {
3659 /* Force full screen update, because VGA device must take control, do resize, etc. */
3660 pThis->mpDrv->pUpPort->pfnUpdateDisplayAll(pThis->mpDrv->pUpPort, /* fFailOnResize = */ false);
3661 }
3662}
3663
3664DECLCALLBACK(void) Display::i_displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3665{
3666 LogFlowFunc(("uScreenId %d\n", uScreenId));
3667
3668 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3669 Display *pThis = pDrv->pDisplay;
3670 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3671
3672 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3673 {
3674 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers,
3675 pThis->mcMonitors);
3676 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3677 }
3678}
3679
3680DECLCALLBACK(void) Display::i_displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
3681 const PVBVACMDHDR pCmd, size_t cbCmd)
3682{
3683 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3684
3685 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3686 Display *pThis = pDrv->pDisplay;
3687 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3688
3689 if (pFBInfo->fDefaultFormat)
3690 {
3691 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3692 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3693 && !pFBInfo->fDisabled)
3694 {
3695 pDrv->pUpPort->pfnUpdateDisplayRect(pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
3696 }
3697 else if ( !pFBInfo->pSourceBitmap.isNull()
3698 && !pFBInfo->fDisabled)
3699 {
3700 /* Render VRAM content to the framebuffer. */
3701 BYTE *pAddress = NULL;
3702 ULONG ulWidth = 0;
3703 ULONG ulHeight = 0;
3704 ULONG ulBitsPerPixel = 0;
3705 ULONG ulBytesPerLine = 0;
3706 ULONG ulPixelFormat = 0;
3707
3708 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
3709 &ulWidth,
3710 &ulHeight,
3711 &ulBitsPerPixel,
3712 &ulBytesPerLine,
3713 &ulPixelFormat);
3714 if (SUCCEEDED(hrc))
3715 {
3716 uint32_t width = pCmd->w;
3717 uint32_t height = pCmd->h;
3718
3719 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3720 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
3721 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
3722 uint32_t u32SrcWidth = pFBInfo->w;
3723 uint32_t u32SrcHeight = pFBInfo->h;
3724 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3725 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3726
3727 uint8_t *pu8Dst = pAddress;
3728 int32_t xDst = xSrc;
3729 int32_t yDst = ySrc;
3730 uint32_t u32DstWidth = u32SrcWidth;
3731 uint32_t u32DstHeight = u32SrcHeight;
3732 uint32_t u32DstLineSize = u32DstWidth * 4;
3733 uint32_t u32DstBitsPerPixel = 32;
3734
3735 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3736 width, height,
3737 pu8Src,
3738 xSrc, ySrc,
3739 u32SrcWidth, u32SrcHeight,
3740 u32SrcLineSize, u32SrcBitsPerPixel,
3741 pu8Dst,
3742 xDst, yDst,
3743 u32DstWidth, u32DstHeight,
3744 u32DstLineSize, u32DstBitsPerPixel);
3745 }
3746 }
3747 }
3748
3749 VBVACMDHDR hdrSaved = *pCmd;
3750
3751 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3752
3753 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3754 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3755
3756 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
3757 pThis->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, pCmd, (uint32_t)cbCmd);
3758
3759 *pHdrUnconst = hdrSaved;
3760}
3761
3762DECLCALLBACK(void) Display::i_displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y,
3763 uint32_t cx, uint32_t cy)
3764{
3765 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3766
3767 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3768 Display *pThis = pDrv->pDisplay;
3769 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3770
3771 /* @todo handleFramebufferUpdate (uScreenId,
3772 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3773 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3774 * cx, cy);
3775 */
3776 pThis->i_handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3777}
3778
3779#ifdef DEBUG_sunlover
3780static void logVBVAResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
3781{
3782 LogRel(("displayVBVAResize: [%d] %s\n"
3783 " pView->u32ViewIndex %d\n"
3784 " pView->u32ViewOffset 0x%08X\n"
3785 " pView->u32ViewSize 0x%08X\n"
3786 " pView->u32MaxScreenSize 0x%08X\n"
3787 " pScreen->i32OriginX %d\n"
3788 " pScreen->i32OriginY %d\n"
3789 " pScreen->u32StartOffset 0x%08X\n"
3790 " pScreen->u32LineSize 0x%08X\n"
3791 " pScreen->u32Width %d\n"
3792 " pScreen->u32Height %d\n"
3793 " pScreen->u16BitsPerPixel %d\n"
3794 " pScreen->u16Flags 0x%04X\n"
3795 " pFBInfo->u32Offset 0x%08X\n"
3796 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
3797 " pFBInfo->u32InformationSize 0x%08X\n"
3798 " pFBInfo->fDisabled %d\n"
3799 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
3800 " pFBInfo->u16BitsPerPixel %d\n"
3801 " pFBInfo->pu8FramebufferVRAM %p\n"
3802 " pFBInfo->u32LineSize 0x%08X\n"
3803 " pFBInfo->flags 0x%04X\n"
3804 " pFBInfo->pHostEvents %p\n"
3805 " pFBInfo->fDefaultFormat %d\n"
3806 " pFBInfo->fVBVAEnabled %d\n"
3807 " pFBInfo->fVBVAForceResize %d\n"
3808 " pFBInfo->pVBVAHostFlags %p\n"
3809 "",
3810 pScreen->u32ViewIndex,
3811 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
3812 pView->u32ViewIndex,
3813 pView->u32ViewOffset,
3814 pView->u32ViewSize,
3815 pView->u32MaxScreenSize,
3816 pScreen->i32OriginX,
3817 pScreen->i32OriginY,
3818 pScreen->u32StartOffset,
3819 pScreen->u32LineSize,
3820 pScreen->u32Width,
3821 pScreen->u32Height,
3822 pScreen->u16BitsPerPixel,
3823 pScreen->u16Flags,
3824 pFBInfo->u32Offset,
3825 pFBInfo->u32MaxFramebufferSize,
3826 pFBInfo->u32InformationSize,
3827 pFBInfo->fDisabled,
3828 pFBInfo->xOrigin,
3829 pFBInfo->yOrigin,
3830 pFBInfo->w,
3831 pFBInfo->h,
3832 pFBInfo->u16BitsPerPixel,
3833 pFBInfo->pu8FramebufferVRAM,
3834 pFBInfo->u32LineSize,
3835 pFBInfo->flags,
3836 pFBInfo->pHostEvents,
3837 pFBInfo->fDefaultFormat,
3838 pFBInfo->fVBVAEnabled,
3839 pFBInfo->fVBVAForceResize,
3840 pFBInfo->pVBVAHostFlags
3841 ));
3842}
3843#endif /* DEBUG_sunlover */
3844
3845DECLCALLBACK(int) Display::i_displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView,
3846 const PVBVAINFOSCREEN pScreen, void *pvVRAM)
3847{
3848 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3849
3850 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3851 Display *pThis = pDrv->pDisplay;
3852
3853 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
3854
3855 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
3856 {
3857 pThis->i_notifyCroglResize(pView, pScreen, pvVRAM);
3858
3859 pFBInfo->fDisabled = true;
3860 pFBInfo->flags = pScreen->u16Flags;
3861
3862 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
3863 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
3864 * the VM window will be black. */
3865 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
3866 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
3867 pThis->i_handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
3868 u32Width, u32Height, pScreen->u16Flags);
3869
3870 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3871 GuestMonitorChangedEventType_Disabled,
3872 pScreen->u32ViewIndex,
3873 0, 0, 0, 0);
3874 return VINF_SUCCESS;
3875 }
3876
3877 /* If display was disabled or there is no framebuffer, a resize will be required,
3878 * because the framebuffer was/will be changed.
3879 */
3880 bool fResize = pFBInfo->fDisabled || pFBInfo->pFramebuffer.isNull();
3881
3882 if (pFBInfo->fVBVAForceResize)
3883 {
3884 /* VBVA was just enabled. Do the resize. */
3885 fResize = true;
3886 pFBInfo->fVBVAForceResize = false;
3887 }
3888
3889 /* Check if this is a real resize or a notification about the screen origin.
3890 * The guest uses this VBVAResize call for both.
3891 */
3892 fResize = fResize
3893 || pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
3894 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
3895 || pFBInfo->u32LineSize != pScreen->u32LineSize
3896 || pFBInfo->w != pScreen->u32Width
3897 || pFBInfo->h != pScreen->u32Height;
3898
3899 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
3900 || pFBInfo->yOrigin != pScreen->i32OriginY;
3901
3902 if (fNewOrigin || fResize)
3903 pThis->i_notifyCroglResize(pView, pScreen, pvVRAM);
3904
3905 if (pFBInfo->fDisabled)
3906 {
3907 pFBInfo->fDisabled = false;
3908 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3909 GuestMonitorChangedEventType_Enabled,
3910 pScreen->u32ViewIndex,
3911 pScreen->i32OriginX, pScreen->i32OriginY,
3912 pScreen->u32Width, pScreen->u32Height);
3913 /* Continue to update pFBInfo. */
3914 }
3915
3916 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
3917 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
3918 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3919
3920 pFBInfo->xOrigin = pScreen->i32OriginX;
3921 pFBInfo->yOrigin = pScreen->i32OriginY;
3922
3923 pFBInfo->w = pScreen->u32Width;
3924 pFBInfo->h = pScreen->u32Height;
3925
3926 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
3927 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
3928 pFBInfo->u32LineSize = pScreen->u32LineSize;
3929
3930 pFBInfo->flags = pScreen->u16Flags;
3931
3932 pThis->xInputMappingOrigin = 0;
3933 pThis->yInputMappingOrigin = 0;
3934 pThis->cxInputMapping = 0;
3935 pThis->cyInputMapping = 0;
3936
3937 if (fNewOrigin)
3938 {
3939 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3940 GuestMonitorChangedEventType_NewOrigin,
3941 pScreen->u32ViewIndex,
3942 pScreen->i32OriginX, pScreen->i32OriginY,
3943 0, 0);
3944 }
3945
3946 if (!fResize)
3947 {
3948 /* No parameters of the framebuffer have actually changed. */
3949 if (fNewOrigin)
3950 {
3951 /* VRDP server still need this notification. */
3952 LogRelFlowFunc(("Calling VRDP\n"));
3953 pThis->mParent->i_consoleVRDPServer()->SendResize();
3954 }
3955 return VINF_SUCCESS;
3956 }
3957
3958 /* Do a regular resize. */
3959 return pThis->i_handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
3960 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
3961 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
3962}
3963
3964DECLCALLBACK(int) Display::i_displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
3965 uint32_t xHot, uint32_t yHot,
3966 uint32_t cx, uint32_t cy,
3967 const void *pvShape)
3968{
3969 LogFlowFunc(("\n"));
3970
3971 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3972 Display *pThis = pDrv->pDisplay;
3973
3974 uint32_t cbShape = 0;
3975 if (pvShape)
3976 {
3977 cbShape = (cx + 7) / 8 * cy; /* size of the AND mask */
3978 cbShape = ((cbShape + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
3979 }
3980
3981 /* Tell the console about it */
3982 pDrv->pDisplay->mParent->i_onMousePointerShapeChange(fVisible, fAlpha,
3983 xHot, yHot, cx, cy, (uint8_t *)pvShape, cbShape);
3984
3985 return VINF_SUCCESS;
3986}
3987
3988DECLCALLBACK(void) Display::i_displayVBVAGuestCapabilityUpdate(PPDMIDISPLAYCONNECTOR pInterface, uint32_t fCapabilities)
3989{
3990 LogFlowFunc(("\n"));
3991
3992 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3993 Display *pThis = pDrv->pDisplay;
3994
3995 pThis->i_handleUpdateGuestVBVACapabilities(fCapabilities);
3996}
3997
3998DECLCALLBACK(void) Display::i_displayVBVAInputMappingUpdate(PPDMIDISPLAYCONNECTOR pInterface, int32_t xOrigin, int32_t yOrigin,
3999 uint32_t cx, uint32_t cy)
4000{
4001 LogFlowFunc(("\n"));
4002
4003 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4004 Display *pThis = pDrv->pDisplay;
4005
4006 pThis->i_handleUpdateVBVAInputMapping(xOrigin, yOrigin, cx, cy);
4007}
4008
4009#endif /* VBOX_WITH_HGSMI */
4010
4011/**
4012 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4013 */
4014DECLCALLBACK(void *) Display::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
4015{
4016 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
4017 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4018 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
4019 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
4020 return NULL;
4021}
4022
4023
4024/**
4025 * Destruct a display driver instance.
4026 *
4027 * @returns VBox status.
4028 * @param pDrvIns The driver instance data.
4029 */
4030DECLCALLBACK(void) Display::i_drvDestruct(PPDMDRVINS pDrvIns)
4031{
4032 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4033 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4034 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4035
4036 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
4037
4038 pThis->IConnector.pu8Data = NULL;
4039 pThis->IConnector.cbScanline = 0;
4040 pThis->IConnector.cBits = 32;
4041 pThis->IConnector.cx = 0;
4042 pThis->IConnector.cy = 0;
4043
4044 if (pThis->pDisplay)
4045 {
4046 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
4047#ifdef VBOX_WITH_VPX
4048 pThis->pDisplay->i_VideoCaptureStop();
4049#endif
4050#ifdef VBOX_WITH_CRHGSMI
4051 pThis->pDisplay->i_destructCrHgsmiData();
4052#endif
4053 pThis->pDisplay->mpDrv = NULL;
4054 pThis->pDisplay->mpVMMDev = NULL;
4055 }
4056}
4057
4058
4059/**
4060 * Construct a display driver instance.
4061 *
4062 * @copydoc FNPDMDRVCONSTRUCT
4063 */
4064DECLCALLBACK(int) Display::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
4065{
4066 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
4067 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4068 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4069
4070 /*
4071 * Validate configuration.
4072 */
4073 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
4074 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
4075 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
4076 ("Configuration error: Not possible to attach anything to this driver!\n"),
4077 VERR_PDM_DRVINS_NO_ATTACH);
4078
4079 /*
4080 * Init Interfaces.
4081 */
4082 pDrvIns->IBase.pfnQueryInterface = Display::i_drvQueryInterface;
4083
4084 pThis->IConnector.pfnResize = Display::i_displayResizeCallback;
4085 pThis->IConnector.pfnUpdateRect = Display::i_displayUpdateCallback;
4086 pThis->IConnector.pfnRefresh = Display::i_displayRefreshCallback;
4087 pThis->IConnector.pfnReset = Display::i_displayResetCallback;
4088 pThis->IConnector.pfnLFBModeChange = Display::i_displayLFBModeChangeCallback;
4089 pThis->IConnector.pfnProcessAdapterData = Display::i_displayProcessAdapterDataCallback;
4090 pThis->IConnector.pfnProcessDisplayData = Display::i_displayProcessDisplayDataCallback;
4091#ifdef VBOX_WITH_VIDEOHWACCEL
4092 pThis->IConnector.pfnVHWACommandProcess = Display::i_displayVHWACommandProcess;
4093#endif
4094#ifdef VBOX_WITH_CRHGSMI
4095 pThis->IConnector.pfnCrHgsmiCommandProcess = Display::i_displayCrHgsmiCommandProcess;
4096 pThis->IConnector.pfnCrHgsmiControlProcess = Display::i_displayCrHgsmiControlProcess;
4097#endif
4098#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4099 pThis->IConnector.pfnCrHgcmCtlSubmit = Display::i_displayCrHgcmCtlSubmit;
4100#endif
4101#ifdef VBOX_WITH_HGSMI
4102 pThis->IConnector.pfnVBVAEnable = Display::i_displayVBVAEnable;
4103 pThis->IConnector.pfnVBVADisable = Display::i_displayVBVADisable;
4104 pThis->IConnector.pfnVBVAUpdateBegin = Display::i_displayVBVAUpdateBegin;
4105 pThis->IConnector.pfnVBVAUpdateProcess = Display::i_displayVBVAUpdateProcess;
4106 pThis->IConnector.pfnVBVAUpdateEnd = Display::i_displayVBVAUpdateEnd;
4107 pThis->IConnector.pfnVBVAResize = Display::i_displayVBVAResize;
4108 pThis->IConnector.pfnVBVAMousePointerShape = Display::i_displayVBVAMousePointerShape;
4109 pThis->IConnector.pfnVBVAGuestCapabilityUpdate = Display::i_displayVBVAGuestCapabilityUpdate;
4110 pThis->IConnector.pfnVBVAInputMappingUpdate = Display::i_displayVBVAInputMappingUpdate;
4111#endif
4112
4113 /*
4114 * Get the IDisplayPort interface of the above driver/device.
4115 */
4116 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
4117 if (!pThis->pUpPort)
4118 {
4119 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
4120 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4121 }
4122#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
4123 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
4124 if (!pThis->pVBVACallbacks)
4125 {
4126 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
4127 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4128 }
4129#endif
4130 /*
4131 * Get the Display object pointer and update the mpDrv member.
4132 */
4133 void *pv;
4134 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
4135 if (RT_FAILURE(rc))
4136 {
4137 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
4138 return rc;
4139 }
4140 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
4141 pThis->pDisplay = pDisplay;
4142 pThis->pDisplay->mpDrv = pThis;
4143
4144 /* Disable VRAM to a buffer copy initially. */
4145 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
4146 pThis->IConnector.cBits = 32; /* DevVGA does nothing otherwise. */
4147
4148 /*
4149 * Start periodic screen refreshes
4150 */
4151 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
4152
4153#ifdef VBOX_WITH_CRHGSMI
4154 pDisplay->i_setupCrHgsmiData();
4155#endif
4156
4157#ifdef VBOX_WITH_VPX
4158 ComPtr<IMachine> pMachine = pDisplay->mParent->i_machine();
4159 BOOL fEnabled = false;
4160 HRESULT hrc = pMachine->COMGETTER(VideoCaptureEnabled)(&fEnabled);
4161 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
4162 if (fEnabled)
4163 {
4164 rc = pDisplay->i_VideoCaptureStart();
4165 fireVideoCaptureChangedEvent(pDisplay->mParent->i_getEventSource());
4166 }
4167#endif
4168
4169 return rc;
4170}
4171
4172
4173/**
4174 * Display driver registration record.
4175 */
4176const PDMDRVREG Display::DrvReg =
4177{
4178 /* u32Version */
4179 PDM_DRVREG_VERSION,
4180 /* szName */
4181 "MainDisplay",
4182 /* szRCMod */
4183 "",
4184 /* szR0Mod */
4185 "",
4186 /* pszDescription */
4187 "Main display driver (Main as in the API).",
4188 /* fFlags */
4189 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
4190 /* fClass. */
4191 PDM_DRVREG_CLASS_DISPLAY,
4192 /* cMaxInstances */
4193 ~0U,
4194 /* cbInstance */
4195 sizeof(DRVMAINDISPLAY),
4196 /* pfnConstruct */
4197 Display::i_drvConstruct,
4198 /* pfnDestruct */
4199 Display::i_drvDestruct,
4200 /* pfnRelocate */
4201 NULL,
4202 /* pfnIOCtl */
4203 NULL,
4204 /* pfnPowerOn */
4205 NULL,
4206 /* pfnReset */
4207 NULL,
4208 /* pfnSuspend */
4209 NULL,
4210 /* pfnResume */
4211 NULL,
4212 /* pfnAttach */
4213 NULL,
4214 /* pfnDetach */
4215 NULL,
4216 /* pfnPowerOff */
4217 NULL,
4218 /* pfnSoftReset */
4219 NULL,
4220 /* u32EndVersion */
4221 PDM_DRVREG_VERSION
4222};
4223
4224/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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