VirtualBox

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

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

Devices/Graphics and Main: avoid unnecessary graphics notifications to the guest.

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

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