VirtualBox

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

Last change on this file since 51436 was 51436, checked in by vboxsync, 11 years ago

Main,Frontends: IDisplay provides the guest screen bitmap to frontends.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 185.2 KB
Line 
1/* $Id: DisplayImpl.cpp 51436 2014-05-28 09:12:15Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2013 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 "VMMDev.h"
23
24#include "AutoCaller.h"
25#include "Logging.h"
26
27/* generated header */
28#include "VBoxEvents.h"
29
30#include <iprt/semaphore.h>
31#include <iprt/thread.h>
32#include <iprt/asm.h>
33#include <iprt/time.h>
34#include <iprt/cpp/utils.h>
35#include <iprt/alloca.h>
36
37#include <VBox/vmm/pdmdrv.h>
38#if defined(DEBUG) || defined(VBOX_STRICT) /* for VM_ASSERT_EMT(). */
39# include <VBox/vmm/vm.h>
40#endif
41
42#ifdef VBOX_WITH_VIDEOHWACCEL
43# include <VBox/VBoxVideo.h>
44#endif
45
46#if defined(VBOX_WITH_CROGL) || defined(VBOX_WITH_CRHGSMI)
47# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
48#endif
49
50#include <VBox/com/array.h>
51
52#ifdef VBOX_WITH_VPX
53# include <iprt/path.h>
54# include "VideoRec.h"
55#endif
56
57#ifdef VBOX_WITH_CROGL
58typedef enum
59{
60 CRVREC_STATE_IDLE,
61 CRVREC_STATE_SUBMITTED
62} CRVREC_STATE;
63#endif
64
65/**
66 * Display driver instance data.
67 *
68 * @implements PDMIDISPLAYCONNECTOR
69 */
70typedef struct DRVMAINDISPLAY
71{
72 /** Pointer to the display object. */
73 Display *pDisplay;
74 /** Pointer to the driver instance structure. */
75 PPDMDRVINS pDrvIns;
76 /** Pointer to the keyboard port interface of the driver/device above us. */
77 PPDMIDISPLAYPORT pUpPort;
78 /** Our display connector interface. */
79 PDMIDISPLAYCONNECTOR IConnector;
80#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
81 /** VBVA callbacks */
82 PPDMIDISPLAYVBVACALLBACKS pVBVACallbacks;
83#endif
84} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
85
86/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
87#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) RT_FROM_MEMBER(pInterface, DRVMAINDISPLAY, IConnector)
88
89#ifdef DEBUG_sunlover
90static STAMPROFILE g_StatDisplayRefresh;
91static int g_stam = 0;
92#endif /* DEBUG_sunlover */
93
94// constructor / destructor
95/////////////////////////////////////////////////////////////////////////////
96
97Display::Display()
98 : mParent(NULL)
99{
100}
101
102Display::~Display()
103{
104}
105
106
107HRESULT Display::FinalConstruct()
108{
109 mpVbvaMemory = NULL;
110 mfVideoAccelEnabled = false;
111 mfVideoAccelVRDP = false;
112 mfu32SupportedOrders = 0;
113 mcVideoAccelVRDPRefs = 0;
114
115 mpPendingVbvaMemory = NULL;
116 mfPendingVideoAccelEnable = false;
117
118 mfMachineRunning = false;
119#ifdef VBOX_WITH_CROGL
120 mfCrOglDataHidden = false;
121#endif
122
123 mpu8VbvaPartial = NULL;
124 mcbVbvaPartial = 0;
125
126 mpDrv = NULL;
127 mpVMMDev = NULL;
128 mfVMMDevInited = false;
129
130 mLastAddress = NULL;
131 mLastBytesPerLine = 0;
132 mLastBitsPerPixel = 0,
133 mLastWidth = 0;
134 mLastHeight = 0;
135
136 int rc = RTCritSectInit(&mVBVALock);
137 AssertRC(rc);
138
139 rc = RTCritSectInit(&mSaveSeamlessRectLock);
140 AssertRC(rc);
141
142 mfu32PendingVideoAccelDisable = false;
143
144#ifdef VBOX_WITH_HGSMI
145 mu32UpdateVBVAFlags = 0;
146#endif
147#ifdef VBOX_WITH_VPX
148 mpVideoRecCtx = NULL;
149 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
150 maVideoRecEnabled[i] = true;
151#endif
152
153#ifdef VBOX_WITH_CRHGSMI
154 mhCrOglSvc = NULL;
155 rc = RTCritSectRwInit(&mCrOglLock);
156 AssertRC(rc);
157#endif
158#ifdef VBOX_WITH_CROGL
159 RT_ZERO(mCrOglCallbacks);
160 RT_ZERO(mCrOglScreenshotData);
161 mfCrOglVideoRecState = CRVREC_STATE_IDLE;
162 mCrOglScreenshotData.u32Screen = CRSCREEN_ALL;
163 mCrOglScreenshotData.pvContext = this;
164 mCrOglScreenshotData.pfnScreenshotBegin = displayCrVRecScreenshotBegin;
165 mCrOglScreenshotData.pfnScreenshotPerform = displayCrVRecScreenshotPerform;
166 mCrOglScreenshotData.pfnScreenshotEnd = displayCrVRecScreenshotEnd;
167#endif
168
169 return BaseFinalConstruct();
170}
171
172void Display::FinalRelease()
173{
174 uninit();
175
176 if (RTCritSectIsInitialized (&mVBVALock))
177 {
178 RTCritSectDelete (&mVBVALock);
179 RT_ZERO(mVBVALock);
180 }
181
182 if (RTCritSectIsInitialized(&mSaveSeamlessRectLock))
183 {
184 RTCritSectDelete(&mSaveSeamlessRectLock);
185 RT_ZERO(mSaveSeamlessRectLock);
186 }
187
188#ifdef VBOX_WITH_CRHGSMI
189 if (RTCritSectRwIsInitialized (&mCrOglLock))
190 {
191 RTCritSectRwDelete (&mCrOglLock);
192 RT_ZERO(mCrOglLock);
193 }
194#endif
195 BaseFinalRelease();
196}
197
198// public initializer/uninitializer for internal purposes only
199/////////////////////////////////////////////////////////////////////////////
200
201#define kMaxSizeThumbnail 64
202
203/**
204 * Save thumbnail and screenshot of the guest screen.
205 */
206static int displayMakeThumbnail(uint8_t *pu8Data, uint32_t cx, uint32_t cy,
207 uint8_t **ppu8Thumbnail, uint32_t *pcbThumbnail, uint32_t *pcxThumbnail, uint32_t *pcyThumbnail)
208{
209 int rc = VINF_SUCCESS;
210
211 uint8_t *pu8Thumbnail = NULL;
212 uint32_t cbThumbnail = 0;
213 uint32_t cxThumbnail = 0;
214 uint32_t cyThumbnail = 0;
215
216 if (cx > cy)
217 {
218 cxThumbnail = kMaxSizeThumbnail;
219 cyThumbnail = (kMaxSizeThumbnail * cy) / cx;
220 }
221 else
222 {
223 cyThumbnail = kMaxSizeThumbnail;
224 cxThumbnail = (kMaxSizeThumbnail * cx) / cy;
225 }
226
227 LogRelFlowFunc(("%dx%d -> %dx%d\n", cx, cy, cxThumbnail, cyThumbnail));
228
229 cbThumbnail = cxThumbnail * 4 * cyThumbnail;
230 pu8Thumbnail = (uint8_t *)RTMemAlloc(cbThumbnail);
231
232 if (pu8Thumbnail)
233 {
234 uint8_t *dst = pu8Thumbnail;
235 uint8_t *src = pu8Data;
236 int dstW = cxThumbnail;
237 int dstH = cyThumbnail;
238 int srcW = cx;
239 int srcH = cy;
240 int iDeltaLine = cx * 4;
241
242 BitmapScale32 (dst,
243 dstW, dstH,
244 src,
245 iDeltaLine,
246 srcW, srcH);
247
248 *ppu8Thumbnail = pu8Thumbnail;
249 *pcbThumbnail = cbThumbnail;
250 *pcxThumbnail = cxThumbnail;
251 *pcyThumbnail = cyThumbnail;
252 }
253 else
254 {
255 rc = VERR_NO_MEMORY;
256 }
257
258 return rc;
259}
260
261#ifdef VBOX_WITH_CROGL
262typedef struct
263{
264 CRVBOXHGCMTAKESCREENSHOT Base;
265
266 /* 32bpp small RGB image. */
267 uint8_t *pu8Thumbnail;
268 uint32_t cbThumbnail;
269 uint32_t cxThumbnail;
270 uint32_t cyThumbnail;
271
272 /* PNG screenshot. */
273 uint8_t *pu8PNG;
274 uint32_t cbPNG;
275 uint32_t cxPNG;
276 uint32_t cyPNG;
277} VBOX_DISPLAY_SAVESCREENSHOT_DATA;
278
279static DECLCALLBACK(void) displaySaveScreenshotReport(void *pvCtx, uint32_t uScreen,
280 uint32_t x, uint32_t y, uint32_t uBitsPerPixel,
281 uint32_t uBytesPerLine, uint32_t uGuestWidth, uint32_t uGuestHeight,
282 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
283{
284 VBOX_DISPLAY_SAVESCREENSHOT_DATA *pData = (VBOX_DISPLAY_SAVESCREENSHOT_DATA*)pvCtx;
285 displayMakeThumbnail(pu8BufferAddress, uGuestWidth, uGuestHeight, &pData->pu8Thumbnail,
286 &pData->cbThumbnail, &pData->cxThumbnail, &pData->cyThumbnail);
287 int rc = DisplayMakePNG(pu8BufferAddress, uGuestWidth, uGuestHeight, &pData->pu8PNG,
288 &pData->cbPNG, &pData->cxPNG, &pData->cyPNG, 1);
289 if (RT_FAILURE(rc))
290 {
291 AssertMsgFailed(("DisplayMakePNG failed %d\n", rc));
292 if (pData->pu8PNG)
293 {
294 RTMemFree(pData->pu8PNG);
295 pData->pu8PNG = NULL;
296 }
297 pData->cbPNG = 0;
298 pData->cxPNG = 0;
299 pData->cyPNG = 0;
300 }
301}
302#endif
303
304DECLCALLBACK(void)
305Display::displaySSMSaveScreenshot(PSSMHANDLE pSSM, void *pvUser)
306{
307 Display *that = static_cast<Display*>(pvUser);
308
309 /* 32bpp small RGB image. */
310 uint8_t *pu8Thumbnail = NULL;
311 uint32_t cbThumbnail = 0;
312 uint32_t cxThumbnail = 0;
313 uint32_t cyThumbnail = 0;
314
315 /* PNG screenshot. */
316 uint8_t *pu8PNG = NULL;
317 uint32_t cbPNG = 0;
318 uint32_t cxPNG = 0;
319 uint32_t cyPNG = 0;
320
321 Console::SafeVMPtr ptrVM(that->mParent);
322 if (ptrVM.isOk())
323 {
324 /* Query RGB bitmap. */
325 uint8_t *pu8Data = NULL;
326 size_t cbData = 0;
327 uint32_t cx = 0;
328 uint32_t cy = 0;
329
330#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
331 BOOL f3DSnapshot = FALSE;
332 BOOL is3denabled;
333 that->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
334 if (is3denabled && that->mCrOglCallbacks.pfnHasData())
335 {
336 VMMDev *pVMMDev = that->mParent->getVMMDev();
337 if (pVMMDev)
338 {
339 VBOX_DISPLAY_SAVESCREENSHOT_DATA *pScreenshot =
340 (VBOX_DISPLAY_SAVESCREENSHOT_DATA*)RTMemAllocZ(sizeof (*pScreenshot));
341 if (pScreenshot)
342 {
343 /* screen id or CRSCREEN_ALL to specify all enabled */
344 pScreenshot->Base.u32Screen = 0;
345 pScreenshot->Base.u32Width = 0;
346 pScreenshot->Base.u32Height = 0;
347 pScreenshot->Base.u32Pitch = 0;
348 pScreenshot->Base.pvBuffer = NULL;
349 pScreenshot->Base.pvContext = pScreenshot;
350 pScreenshot->Base.pfnScreenshotBegin = NULL;
351 pScreenshot->Base.pfnScreenshotPerform = displaySaveScreenshotReport;
352 pScreenshot->Base.pfnScreenshotEnd = NULL;
353
354 VBOXCRCMDCTL_HGCM data;
355 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
356 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
357
358 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
359 data.aParms[0].u.pointer.addr = &pScreenshot->Base;
360 data.aParms[0].u.pointer.size = sizeof (pScreenshot->Base);
361
362 int rc = that->crCtlSubmitSync(&data.Hdr, sizeof (data));
363 if (RT_SUCCESS(rc))
364 {
365 if (pScreenshot->pu8PNG)
366 {
367 pu8Thumbnail = pScreenshot->pu8Thumbnail;
368 cbThumbnail = pScreenshot->cbThumbnail;
369 cxThumbnail = pScreenshot->cxThumbnail;
370 cyThumbnail = pScreenshot->cyThumbnail;
371
372 /* PNG screenshot. */
373 pu8PNG = pScreenshot->pu8PNG;
374 cbPNG = pScreenshot->cbPNG;
375 cxPNG = pScreenshot->cxPNG;
376 cyPNG = pScreenshot->cyPNG;
377 f3DSnapshot = TRUE;
378 }
379 else
380 AssertMsgFailed(("no png\n"));
381 }
382 else
383 AssertMsgFailed(("SHCRGL_HOST_FN_TAKE_SCREENSHOT failed %d\n", rc));
384
385
386 RTMemFree(pScreenshot);
387 }
388 }
389 }
390
391 if (!f3DSnapshot)
392#endif
393 {
394 /* SSM code is executed on EMT(0), therefore no need to use VMR3ReqCallWait. */
395 int rc = Display::displayTakeScreenshotEMT(that, VBOX_VIDEO_PRIMARY_SCREEN, &pu8Data, &cbData, &cx, &cy);
396
397 /*
398 * It is possible that success is returned but everything is 0 or NULL.
399 * (no display attached if a VM is running with VBoxHeadless on OSE for example)
400 */
401 if (RT_SUCCESS(rc) && pu8Data)
402 {
403 Assert(cx && cy);
404
405 /* Prepare a small thumbnail and a PNG screenshot. */
406 displayMakeThumbnail(pu8Data, cx, cy, &pu8Thumbnail, &cbThumbnail, &cxThumbnail, &cyThumbnail);
407 rc = DisplayMakePNG(pu8Data, cx, cy, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 1);
408 if (RT_FAILURE(rc))
409 {
410 if (pu8PNG)
411 {
412 RTMemFree(pu8PNG);
413 pu8PNG = NULL;
414 }
415 cbPNG = 0;
416 cxPNG = 0;
417 cyPNG = 0;
418 }
419
420 /* This can be called from any thread. */
421 that->mpDrv->pUpPort->pfnFreeScreenshot(that->mpDrv->pUpPort, pu8Data);
422 }
423 }
424 }
425 else
426 {
427 LogFunc(("Failed to get VM pointer 0x%x\n", ptrVM.rc()));
428 }
429
430 /* Regardless of rc, save what is available:
431 * Data format:
432 * uint32_t cBlocks;
433 * [blocks]
434 *
435 * Each block is:
436 * uint32_t cbBlock; if 0 - no 'block data'.
437 * uint32_t typeOfBlock; 0 - 32bpp RGB bitmap, 1 - PNG, ignored if 'cbBlock' is 0.
438 * [block data]
439 *
440 * Block data for bitmap and PNG:
441 * uint32_t cx;
442 * uint32_t cy;
443 * [image data]
444 */
445 SSMR3PutU32(pSSM, 2); /* Write thumbnail and PNG screenshot. */
446
447 /* First block. */
448 SSMR3PutU32(pSSM, cbThumbnail + 2 * sizeof (uint32_t));
449 SSMR3PutU32(pSSM, 0); /* Block type: thumbnail. */
450
451 if (cbThumbnail)
452 {
453 SSMR3PutU32(pSSM, cxThumbnail);
454 SSMR3PutU32(pSSM, cyThumbnail);
455 SSMR3PutMem(pSSM, pu8Thumbnail, cbThumbnail);
456 }
457
458 /* Second block. */
459 SSMR3PutU32(pSSM, cbPNG + 2 * sizeof (uint32_t));
460 SSMR3PutU32(pSSM, 1); /* Block type: png. */
461
462 if (cbPNG)
463 {
464 SSMR3PutU32(pSSM, cxPNG);
465 SSMR3PutU32(pSSM, cyPNG);
466 SSMR3PutMem(pSSM, pu8PNG, cbPNG);
467 }
468
469 RTMemFree(pu8PNG);
470 RTMemFree(pu8Thumbnail);
471}
472
473DECLCALLBACK(int)
474Display::displaySSMLoadScreenshot(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
475{
476 Display *that = static_cast<Display*>(pvUser);
477
478 if (uVersion != sSSMDisplayScreenshotVer)
479 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
480 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
481
482 /* Skip data. */
483 uint32_t cBlocks;
484 int rc = SSMR3GetU32(pSSM, &cBlocks);
485 AssertRCReturn(rc, rc);
486
487 for (uint32_t i = 0; i < cBlocks; i++)
488 {
489 uint32_t cbBlock;
490 rc = SSMR3GetU32(pSSM, &cbBlock);
491 AssertRCBreak(rc);
492
493 uint32_t typeOfBlock;
494 rc = SSMR3GetU32(pSSM, &typeOfBlock);
495 AssertRCBreak(rc);
496
497 LogRelFlowFunc(("[%d] type %d, size %d bytes\n", i, typeOfBlock, cbBlock));
498
499 /* Note: displaySSMSaveScreenshot writes size of a block = 8 and
500 * do not write any data if the image size was 0.
501 * @todo Fix and increase saved state version.
502 */
503 if (cbBlock > 2 * sizeof (uint32_t))
504 {
505 rc = SSMR3Skip(pSSM, cbBlock);
506 AssertRCBreak(rc);
507 }
508 }
509
510 return rc;
511}
512
513/**
514 * Save/Load some important guest state
515 */
516DECLCALLBACK(void)
517Display::displaySSMSave(PSSMHANDLE pSSM, void *pvUser)
518{
519 Display *that = static_cast<Display*>(pvUser);
520
521 SSMR3PutU32(pSSM, that->mcMonitors);
522 for (unsigned i = 0; i < that->mcMonitors; i++)
523 {
524 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32Offset);
525 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32MaxFramebufferSize);
526 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32InformationSize);
527 SSMR3PutU32(pSSM, that->maFramebuffers[i].w);
528 SSMR3PutU32(pSSM, that->maFramebuffers[i].h);
529 SSMR3PutS32(pSSM, that->maFramebuffers[i].xOrigin);
530 SSMR3PutS32(pSSM, that->maFramebuffers[i].yOrigin);
531 SSMR3PutU32(pSSM, that->maFramebuffers[i].flags);
532 }
533}
534
535DECLCALLBACK(int)
536Display::displaySSMLoad(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
537{
538 Display *that = static_cast<Display*>(pvUser);
539
540 if (!( uVersion == sSSMDisplayVer
541 || uVersion == sSSMDisplayVer2
542 || uVersion == sSSMDisplayVer3))
543 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
544 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
545
546 uint32_t cMonitors;
547 int rc = SSMR3GetU32(pSSM, &cMonitors);
548 if (cMonitors != that->mcMonitors)
549 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Number of monitors changed (%d->%d)!"), cMonitors, that->mcMonitors);
550
551 for (uint32_t i = 0; i < cMonitors; i++)
552 {
553 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32Offset);
554 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32MaxFramebufferSize);
555 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32InformationSize);
556 if ( uVersion == sSSMDisplayVer2
557 || uVersion == sSSMDisplayVer3)
558 {
559 uint32_t w;
560 uint32_t h;
561 SSMR3GetU32(pSSM, &w);
562 SSMR3GetU32(pSSM, &h);
563 that->maFramebuffers[i].w = w;
564 that->maFramebuffers[i].h = h;
565 }
566 if (uVersion == sSSMDisplayVer3)
567 {
568 int32_t xOrigin;
569 int32_t yOrigin;
570 uint32_t flags;
571 SSMR3GetS32(pSSM, &xOrigin);
572 SSMR3GetS32(pSSM, &yOrigin);
573 SSMR3GetU32(pSSM, &flags);
574 that->maFramebuffers[i].xOrigin = xOrigin;
575 that->maFramebuffers[i].yOrigin = yOrigin;
576 that->maFramebuffers[i].flags = (uint16_t)flags;
577 that->maFramebuffers[i].fDisabled = (that->maFramebuffers[i].flags & VBVA_SCREEN_F_DISABLED) != 0;
578 }
579 }
580
581 return VINF_SUCCESS;
582}
583
584/**
585 * Initializes the display object.
586 *
587 * @returns COM result indicator
588 * @param parent handle of our parent object
589 * @param qemuConsoleData address of common console data structure
590 */
591HRESULT Display::init(Console *aParent)
592{
593 ComAssertRet(aParent, E_INVALIDARG);
594 /* Enclose the state transition NotReady->InInit->Ready */
595 AutoInitSpan autoInitSpan(this);
596 AssertReturn(autoInitSpan.isOk(), E_FAIL);
597
598 unconst(mParent) = aParent;
599
600 ULONG ul;
601 mParent->machine()->COMGETTER(MonitorCount)(&ul);
602 mcMonitors = ul;
603
604 for (ul = 0; ul < mcMonitors; ul++)
605 {
606 maFramebuffers[ul].u32Offset = 0;
607 maFramebuffers[ul].u32MaxFramebufferSize = 0;
608 maFramebuffers[ul].u32InformationSize = 0;
609
610 maFramebuffers[ul].pFramebuffer = NULL;
611 /* All secondary monitors are disabled at startup. */
612 maFramebuffers[ul].fDisabled = ul > 0;
613
614 maFramebuffers[ul].xOrigin = 0;
615 maFramebuffers[ul].yOrigin = 0;
616
617 maFramebuffers[ul].w = 0;
618 maFramebuffers[ul].h = 0;
619
620 maFramebuffers[ul].flags = maFramebuffers[ul].fDisabled? VBVA_SCREEN_F_DISABLED: 0;
621
622 maFramebuffers[ul].u16BitsPerPixel = 0;
623 maFramebuffers[ul].pu8FramebufferVRAM = NULL;
624 maFramebuffers[ul].u32LineSize = 0;
625
626 maFramebuffers[ul].pHostEvents = NULL;
627
628 maFramebuffers[ul].u32ResizeStatus = ResizeStatus_Void;
629
630 maFramebuffers[ul].fDefaultFormat = false;
631
632 maFramebuffers[ul].mcSavedVisibleRegion = 0;
633 maFramebuffers[ul].mpSavedVisibleRegion = NULL;
634
635 RT_ZERO(maFramebuffers[ul].dirtyRect);
636 RT_ZERO(maFramebuffers[ul].pendingResize);
637#ifdef VBOX_WITH_HGSMI
638 maFramebuffers[ul].fVBVAEnabled = false;
639 maFramebuffers[ul].fVBVAForceResize = false;
640 maFramebuffers[ul].fRenderThreadMode = false;
641 maFramebuffers[ul].cVBVASkipUpdate = 0;
642 RT_ZERO(maFramebuffers[ul].vbvaSkippedRect);
643 maFramebuffers[ul].pVBVAHostFlags = NULL;
644#endif /* VBOX_WITH_HGSMI */
645#ifdef VBOX_WITH_CROGL
646 RT_ZERO(maFramebuffers[ul].pendingViewportInfo);
647#endif
648 }
649
650 {
651 // register listener for state change events
652 ComPtr<IEventSource> es;
653 mParent->COMGETTER(EventSource)(es.asOutParam());
654 com::SafeArray <VBoxEventType_T> eventTypes;
655 eventTypes.push_back(VBoxEventType_OnStateChanged);
656 es->RegisterListener(this, ComSafeArrayAsInParam(eventTypes), true);
657 }
658
659 /* Confirm a successful initialization */
660 autoInitSpan.setSucceeded();
661
662 return S_OK;
663}
664
665/**
666 * Uninitializes the instance and sets the ready flag to FALSE.
667 * Called either from FinalRelease() or by the parent when it gets destroyed.
668 */
669void Display::uninit()
670{
671 LogRelFlowFunc(("this=%p\n", this));
672
673 /* Enclose the state transition Ready->InUninit->NotReady */
674 AutoUninitSpan autoUninitSpan(this);
675 if (autoUninitSpan.uninitDone())
676 return;
677
678 ULONG ul;
679 for (ul = 0; ul < mcMonitors; ul++)
680 maFramebuffers[ul].pFramebuffer = NULL;
681
682 if (mParent)
683 {
684 ComPtr<IEventSource> es;
685 mParent->COMGETTER(EventSource)(es.asOutParam());
686 es->UnregisterListener(this);
687 }
688
689 unconst(mParent) = NULL;
690
691 if (mpDrv)
692 mpDrv->pDisplay = NULL;
693
694 mpDrv = NULL;
695 mpVMMDev = NULL;
696 mfVMMDevInited = true;
697}
698
699/**
700 * Register the SSM methods. Called by the power up thread to be able to
701 * pass pVM
702 */
703int Display::registerSSM(PUVM pUVM)
704{
705 /* Version 2 adds width and height of the framebuffer; version 3 adds
706 * the framebuffer offset in the virtual desktop and the framebuffer flags.
707 */
708 int rc = SSMR3RegisterExternal(pUVM, "DisplayData", 0, sSSMDisplayVer3,
709 mcMonitors * sizeof(uint32_t) * 8 + sizeof(uint32_t),
710 NULL, NULL, NULL,
711 NULL, displaySSMSave, NULL,
712 NULL, displaySSMLoad, NULL, this);
713 AssertRCReturn(rc, rc);
714
715 /*
716 * Register loaders for old saved states where iInstance was
717 * 3 * sizeof(uint32_t *) due to a code mistake.
718 */
719 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 12 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
720 NULL, NULL, NULL,
721 NULL, NULL, NULL,
722 NULL, displaySSMLoad, NULL, this);
723 AssertRCReturn(rc, rc);
724
725 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 24 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
726 NULL, NULL, NULL,
727 NULL, NULL, NULL,
728 NULL, displaySSMLoad, NULL, this);
729 AssertRCReturn(rc, rc);
730
731 /* uInstance is an arbitrary value greater than 1024. Such a value will ensure a quick seek in saved state file. */
732 rc = SSMR3RegisterExternal(pUVM, "DisplayScreenshot", 1100 /*uInstance*/, sSSMDisplayScreenshotVer, 0 /*cbGuess*/,
733 NULL, NULL, NULL,
734 NULL, displaySSMSaveScreenshot, NULL,
735 NULL, displaySSMLoadScreenshot, NULL, this);
736
737 AssertRCReturn(rc, rc);
738
739 return VINF_SUCCESS;
740}
741
742DECLCALLBACK(void) Display::displayCrCmdFree(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, int rc, void *pvCompletion)
743{
744 Assert(pvCompletion);
745 RTMemFree(pvCompletion);
746}
747
748#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
749int Display::crOglWindowsShow(bool fShow)
750{
751 if (!mfCrOglDataHidden == !!fShow)
752 return VINF_SUCCESS;
753
754 if (!mhCrOglSvc)
755 {
756 /* no 3D */
757#ifdef DEBUG
758 BOOL is3denabled;
759 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
760 Assert(!is3denabled);
761#endif
762 return VERR_INVALID_STATE;
763 }
764
765 VMMDev *pVMMDev = mParent->getVMMDev();
766 if (!pVMMDev)
767 {
768 AssertMsgFailed(("no vmmdev\n"));
769 return VERR_INVALID_STATE;
770 }
771
772 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof (VBOXCRCMDCTL_HGCM));
773 if (!pData)
774 {
775 AssertMsgFailed(("RTMemAlloc failed\n"));
776 return VERR_NO_MEMORY;
777 }
778
779 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
780 pData->Hdr.u32Function = SHCRGL_HOST_FN_WINDOWS_SHOW;
781
782 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
783 pData->aParms[0].u.uint32 = (uint32_t)fShow;
784
785 int rc = crCtlSubmit(&pData->Hdr, sizeof (*pData), displayCrCmdFree, pData);
786 if (RT_SUCCESS(rc))
787 mfCrOglDataHidden = !fShow;
788 else
789 {
790 AssertMsgFailed(("crCtlSubmit failed rc %d\n", rc));
791 RTMemFree(pData);
792 }
793
794 return rc;
795}
796#endif
797
798
799// IEventListener method
800STDMETHODIMP Display::HandleEvent(IEvent * aEvent)
801{
802 VBoxEventType_T aType = VBoxEventType_Invalid;
803
804 aEvent->COMGETTER(Type)(&aType);
805 switch (aType)
806 {
807 case VBoxEventType_OnStateChanged:
808 {
809 ComPtr<IStateChangedEvent> scev = aEvent;
810 Assert(scev);
811 MachineState_T machineState;
812 scev->COMGETTER(State)(&machineState);
813 if ( machineState == MachineState_Running
814 || machineState == MachineState_Teleporting
815 || machineState == MachineState_LiveSnapshotting
816 )
817 {
818 LogRelFlowFunc(("Machine is running.\n"));
819
820 mfMachineRunning = true;
821
822#ifdef VBOX_WITH_CROGL
823 crOglWindowsShow(true);
824#endif
825 }
826 else
827 {
828 mfMachineRunning = false;
829
830#ifdef VBOX_WITH_CROGL
831 if (machineState == MachineState_Paused)
832 crOglWindowsShow(false);
833#endif
834 }
835 break;
836 }
837 default:
838 AssertFailed();
839 }
840
841 return S_OK;
842}
843
844// public methods only for internal purposes
845/////////////////////////////////////////////////////////////////////////////
846
847/**
848 * @thread EMT
849 */
850static int callFramebufferResize (IFramebuffer *pFramebuffer, unsigned uScreenId,
851 ULONG pixelFormat, void *pvVRAM,
852 uint32_t bpp, uint32_t cbLine,
853 uint32_t w, uint32_t h)
854{
855 Assert (pFramebuffer);
856
857 NOREF(pixelFormat);
858 NOREF(pvVRAM);
859 NOREF(bpp);
860 NOREF(cbLine);
861
862 /* Call the framebuffer to try and set required pixelFormat. */
863 HRESULT hr = pFramebuffer->NotifyChange(uScreenId, 0, 0, w, h); /* @todo origin */
864
865 Log(("pFramebuffer->NotifyChange hr %08x\n", hr));
866
867 return VINF_SUCCESS;
868}
869
870int Display::notifyCroglResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
871{
872#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
873 if (maFramebuffers[pScreen->u32ViewIndex].fRenderThreadMode)
874 return VINF_SUCCESS; /* nop it */
875
876 BOOL is3denabled;
877 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
878
879 if (is3denabled)
880 {
881 int rc = VERR_INVALID_STATE;
882 if (mhCrOglSvc)
883 {
884 VMMDev *pVMMDev = mParent->getVMMDev();
885 if (pVMMDev)
886 {
887 VBOXCRCMDCTL_HGCM *pCtl =
888 (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof (CRVBOXHGCMDEVRESIZE) + sizeof (VBOXCRCMDCTL_HGCM));
889 if (pCtl)
890 {
891 CRVBOXHGCMDEVRESIZE *pData = (CRVBOXHGCMDEVRESIZE*)(pCtl+1);
892 pData->Screen = *pScreen;
893 pData->pvVRAM = pvVRAM;
894
895 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
896 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_DEV_RESIZE;
897 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
898 pCtl->aParms[0].u.pointer.addr = pData;
899 pCtl->aParms[0].u.pointer.size = sizeof (*pData);
900
901 rc = crCtlSubmit(&pCtl->Hdr, sizeof (*pCtl), displayCrCmdFree, pCtl);
902 if (!RT_SUCCESS(rc))
903 {
904 AssertMsgFailed(("crCtlSubmit failed rc %d\n", rc));
905 RTMemFree(pCtl);
906 }
907 }
908 else
909 rc = VERR_NO_MEMORY;
910 }
911 }
912
913 return rc;
914 }
915#endif /* #if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
916 return VINF_SUCCESS;
917}
918
919/**
920 * Handles display resize event.
921 * Disables access to VGA device;
922 * calls the framebuffer RequestResize method;
923 * if framebuffer resizes synchronously,
924 * updates the display connector data and enables access to the VGA device.
925 *
926 * @param w New display width
927 * @param h New display height
928 *
929 * @thread EMT
930 */
931int Display::handleDisplayResize (unsigned uScreenId, uint32_t bpp, void *pvVRAM,
932 uint32_t cbLine, uint32_t w, uint32_t h, uint16_t flags)
933{
934 LogRel(("Display::handleDisplayResize(): uScreenId = %d, pvVRAM=%p "
935 "w=%d h=%d bpp=%d cbLine=0x%X, flags=0x%X\n",
936 uScreenId, pvVRAM, w, h, bpp, cbLine, flags));
937
938 /* If there is no framebuffer, this call is not interesting. */
939 if (uScreenId >= mcMonitors)
940 {
941 return VINF_SUCCESS;
942 }
943
944 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
945 {
946 mLastAddress = pvVRAM;
947 mLastBytesPerLine = cbLine;
948 mLastBitsPerPixel = bpp;
949 mLastWidth = w;
950 mLastHeight = h;
951 mLastFlags = flags;
952
953 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
954 pFBInfo->w = w;
955 pFBInfo->h = h;
956
957 pFBInfo->u16BitsPerPixel = (uint16_t)bpp;
958 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM;
959 pFBInfo->u32LineSize = cbLine;
960 }
961
962 if (maFramebuffers[uScreenId].pFramebuffer.isNull())
963 {
964 return VINF_SUCCESS;
965 }
966
967 ULONG pixelFormat;
968
969 switch (bpp)
970 {
971 case 32:
972 case 24:
973 case 16:
974 pixelFormat = FramebufferPixelFormat_FOURCC_RGB;
975 break;
976 default:
977 pixelFormat = FramebufferPixelFormat_Opaque;
978 bpp = cbLine = 0;
979 break;
980 }
981
982 /* Atomically set the resize status before calling the framebuffer. The new InProgress status will
983 * disable access to the VGA device by the EMT thread.
984 */
985 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
986 ResizeStatus_InProgress, ResizeStatus_Void);
987 if (!f)
988 {
989 /* This could be a result of the screenshot taking call Display::TakeScreenShot:
990 * if the framebuffer is processing the resize request and GUI calls the TakeScreenShot
991 * and the guest has reprogrammed the virtual VGA devices again so a new resize is required.
992 *
993 * Save the resize information and return the pending status code.
994 *
995 * Note: the resize information is only accessed on EMT so no serialization is required.
996 */
997 LogRel(("Display::handleDisplayResize(): Warning: resize postponed.\n"));
998
999 maFramebuffers[uScreenId].pendingResize.fPending = true;
1000 maFramebuffers[uScreenId].pendingResize.pixelFormat = pixelFormat;
1001 maFramebuffers[uScreenId].pendingResize.pvVRAM = pvVRAM;
1002 maFramebuffers[uScreenId].pendingResize.bpp = bpp;
1003 maFramebuffers[uScreenId].pendingResize.cbLine = cbLine;
1004 maFramebuffers[uScreenId].pendingResize.w = w;
1005 maFramebuffers[uScreenId].pendingResize.h = h;
1006 maFramebuffers[uScreenId].pendingResize.flags = flags;
1007
1008 return VINF_VGA_RESIZE_IN_PROGRESS;
1009 }
1010
1011 /* Framebuffer will be invalid during resize, make sure that it is not accessed. */
1012 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1013 {
1014 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, false);
1015
1016 mpDrv->IConnector.pu8Data = NULL;
1017 mpDrv->IConnector.cbScanline = 0;
1018 mpDrv->IConnector.cBits = 32; /* DevVGA does not work with cBits == 0. */
1019 mpDrv->IConnector.cx = 0;
1020 mpDrv->IConnector.cy = 0;
1021 }
1022
1023 maFramebuffers[uScreenId].pSourceBitmap.setNull();
1024
1025 int rc = callFramebufferResize (maFramebuffers[uScreenId].pFramebuffer, uScreenId,
1026 pixelFormat, pvVRAM, bpp, cbLine, w, h);
1027 if (rc == VINF_VGA_RESIZE_IN_PROGRESS)
1028 {
1029 /* Immediately return to the caller. ResizeCompleted will be called back by the
1030 * GUI thread. The ResizeCompleted callback will change the resize status from
1031 * InProgress to UpdateDisplayData. The latter status will be checked by the
1032 * display timer callback on EMT and all required adjustments will be done there.
1033 */
1034 return rc;
1035 }
1036
1037 /* Set the status so the 'handleResizeCompleted' would work. */
1038 f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
1039 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
1040 AssertRelease(f);NOREF(f);
1041
1042 AssertRelease(!maFramebuffers[uScreenId].pendingResize.fPending);
1043
1044 /* The method also unlocks the framebuffer. */
1045 handleResizeCompletedEMT(uScreenId);
1046
1047 return VINF_SUCCESS;
1048}
1049
1050/**
1051 * Framebuffer has been resized.
1052 * Read the new display data and unlock the framebuffer.
1053 *
1054 * @thread EMT
1055 */
1056void Display::handleResizeCompletedEMT(unsigned uScreenId)
1057{
1058 LogRelFlowFunc(("\n"));
1059
1060 do /* to use 'break' */
1061 {
1062 if (uScreenId >= mcMonitors)
1063 {
1064 break;
1065 }
1066
1067 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1068
1069 /* Try to into non resizing state. */
1070 bool f = ASMAtomicCmpXchgU32 (&pFBInfo->u32ResizeStatus, ResizeStatus_Void, ResizeStatus_UpdateDisplayData);
1071
1072 if (f == false)
1073 {
1074 /* This is not the display that has completed resizing. */
1075 AssertFailed();
1076 break;
1077 }
1078
1079 /* Check whether a resize is pending for this framebuffer. */
1080 if (pFBInfo->pendingResize.fPending)
1081 {
1082 /* Reset the condition, call the display resize with saved data and continue.
1083 *
1084 * Note: handleDisplayResize can call handleResizeCompletedEMT back,
1085 * but infinite recursion is not possible, because when the handleResizeCompletedEMT
1086 * is called, the pFBInfo->pendingResize.fPending is equal to false.
1087 */
1088 pFBInfo->pendingResize.fPending = false;
1089 handleDisplayResize (uScreenId, pFBInfo->pendingResize.bpp, pFBInfo->pendingResize.pvVRAM,
1090 pFBInfo->pendingResize.cbLine, pFBInfo->pendingResize.w, pFBInfo->pendingResize.h,
1091 pFBInfo->pendingResize.flags);
1092 break;
1093 }
1094
1095 /* Inform VRDP server about the change of display parameters.
1096 * Must be done before calling NotifyUpdate below.
1097 */
1098 LogRelFlowFunc(("Calling VRDP\n"));
1099 mParent->consoleVRDPServer()->SendResize();
1100
1101 /* @todo Merge these two 'if's within one 'if (!pFBInfo->pFramebuffer.isNull())' */
1102 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
1103 {
1104 /* If the screen resize was because of disabling, tell framebuffer to repaint.
1105 * The framebuffer if now in default format so it will not use guest VRAM
1106 * and will show usually black image which is there after framebuffer resize.
1107 */
1108 if (pFBInfo->fDisabled)
1109 pFBInfo->pFramebuffer->NotifyUpdate(0, 0, mpDrv->IConnector.cx, mpDrv->IConnector.cy);
1110 }
1111 else if (!pFBInfo->pFramebuffer.isNull())
1112 {
1113 /* If the screen resize was because of disabling, tell framebuffer to repaint.
1114 * The framebuffer if now in default format so it will not use guest VRAM
1115 * and will show usually black image which is there after framebuffer resize.
1116 */
1117 if (pFBInfo->fDisabled)
1118 pFBInfo->pFramebuffer->NotifyUpdate(0, 0, pFBInfo->w, pFBInfo->h);
1119 }
1120 LogRelFlow(("[%d]: default format %d\n", uScreenId, pFBInfo->fDefaultFormat));
1121
1122 /* Handle the case if there are some saved visible region that needs to be
1123 * applied after the resize of the framebuffer is completed
1124 */
1125 SaveSeamlessRectLock();
1126 PRTRECT pSavedVisibleRegion = pFBInfo->mpSavedVisibleRegion;
1127 uint32_t cSavedVisibleRegion = pFBInfo->mcSavedVisibleRegion;
1128 pFBInfo->mpSavedVisibleRegion = NULL;
1129 pFBInfo->mcSavedVisibleRegion = 0;
1130 SaveSeamlessRectUnLock();
1131
1132 if (pSavedVisibleRegion)
1133 {
1134 handleSetVisibleRegion(cSavedVisibleRegion, pSavedVisibleRegion);
1135 RTMemFree(pSavedVisibleRegion);
1136 }
1137
1138#ifdef DEBUG_sunlover
1139 if (!g_stam)
1140 {
1141 Console::SafeVMPtr ptrVM(mParent);
1142 AssertComRC(ptrVM.rc());
1143 STAMR3RegisterU(ptrVM.rawUVM(), &g_StatDisplayRefresh, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
1144 "/PROF/Display/Refresh", STAMUNIT_TICKS_PER_CALL, "Time spent in EMT for display updates.");
1145 g_stam = 1;
1146 }
1147#endif /* DEBUG_sunlover */
1148
1149#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1150 {
1151 BOOL is3denabled;
1152 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
1153
1154 if (is3denabled)
1155 {
1156 VBOXCRCMDCTL_HGCM data;
1157 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1158 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
1159
1160 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
1161 data.aParms[0].u.uint32 = uScreenId;
1162
1163 crCtlSubmitSync(&data.Hdr, sizeof (data));
1164 }
1165 }
1166#endif /* VBOX_WITH_CROGL */
1167 } while(0);
1168}
1169
1170static void checkCoordBounds (int *px, int *py, int *pw, int *ph, int cx, int cy)
1171{
1172 /* Correct negative x and y coordinates. */
1173 if (*px < 0)
1174 {
1175 *px += *pw; /* Compute xRight which is also the new width. */
1176
1177 *pw = (*px < 0)? 0: *px;
1178
1179 *px = 0;
1180 }
1181
1182 if (*py < 0)
1183 {
1184 *py += *ph; /* Compute xBottom, which is also the new height. */
1185
1186 *ph = (*py < 0)? 0: *py;
1187
1188 *py = 0;
1189 }
1190
1191 /* Also check if coords are greater than the display resolution. */
1192 if (*px + *pw > cx)
1193 {
1194 *pw = cx > *px? cx - *px: 0;
1195 }
1196
1197 if (*py + *ph > cy)
1198 {
1199 *ph = cy > *py? cy - *py: 0;
1200 }
1201}
1202
1203unsigned mapCoordsToScreen(DISPLAYFBINFO *pInfos, unsigned cInfos, int *px, int *py, int *pw, int *ph)
1204{
1205 DISPLAYFBINFO *pInfo = pInfos;
1206 unsigned uScreenId;
1207 LogSunlover(("mapCoordsToScreen: %d,%d %dx%d\n", *px, *py, *pw, *ph));
1208 for (uScreenId = 0; uScreenId < cInfos; uScreenId++, pInfo++)
1209 {
1210 LogSunlover((" [%d] %d,%d %dx%d\n", uScreenId, pInfo->xOrigin, pInfo->yOrigin, pInfo->w, pInfo->h));
1211 if ( (pInfo->xOrigin <= *px && *px < pInfo->xOrigin + (int)pInfo->w)
1212 && (pInfo->yOrigin <= *py && *py < pInfo->yOrigin + (int)pInfo->h))
1213 {
1214 /* The rectangle belongs to the screen. Correct coordinates. */
1215 *px -= pInfo->xOrigin;
1216 *py -= pInfo->yOrigin;
1217 LogSunlover((" -> %d,%d", *px, *py));
1218 break;
1219 }
1220 }
1221 if (uScreenId == cInfos)
1222 {
1223 /* Map to primary screen. */
1224 uScreenId = 0;
1225 }
1226 LogSunlover((" scr %d\n", uScreenId));
1227 return uScreenId;
1228}
1229
1230
1231/**
1232 * Handles display update event.
1233 *
1234 * @param x Update area x coordinate
1235 * @param y Update area y coordinate
1236 * @param w Update area width
1237 * @param h Update area height
1238 *
1239 * @thread EMT
1240 */
1241void Display::handleDisplayUpdateLegacy (int x, int y, int w, int h)
1242{
1243 unsigned uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
1244
1245#ifdef DEBUG_sunlover
1246 LogFlowFunc(("%d,%d %dx%d (checked)\n", x, y, w, h));
1247#endif /* DEBUG_sunlover */
1248
1249 handleDisplayUpdate (uScreenId, x, y, w, h);
1250}
1251
1252void Display::handleDisplayUpdate (unsigned uScreenId, int x, int y, int w, int h)
1253{
1254 /*
1255 * Always runs under either VBVA lock or, for HGSMI, DevVGA lock.
1256 * Safe to use VBVA vars and take the framebuffer lock.
1257 */
1258
1259#ifdef DEBUG_sunlover
1260 LogFlowFunc(("[%d] %d,%d %dx%d (%d,%d)\n",
1261 uScreenId, x, y, w, h, mpDrv->IConnector.cx, mpDrv->IConnector.cy));
1262#endif /* DEBUG_sunlover */
1263
1264 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
1265
1266 // if there is no framebuffer, this call is not interesting
1267 if ( pFramebuffer == NULL
1268 || maFramebuffers[uScreenId].fDisabled)
1269 return;
1270
1271 pFramebuffer->Lock();
1272
1273 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1274 checkCoordBounds (&x, &y, &w, &h, mpDrv->IConnector.cx, mpDrv->IConnector.cy);
1275 else
1276 checkCoordBounds (&x, &y, &w, &h, maFramebuffers[uScreenId].w,
1277 maFramebuffers[uScreenId].h);
1278
1279 if (w != 0 && h != 0)
1280 pFramebuffer->NotifyUpdate(x, y, w, h);
1281
1282 pFramebuffer->Unlock();
1283
1284#ifndef VBOX_WITH_HGSMI
1285 if (!mfVideoAccelEnabled)
1286 {
1287#else
1288 if (!mfVideoAccelEnabled && !maFramebuffers[uScreenId].fVBVAEnabled)
1289 {
1290#endif /* VBOX_WITH_HGSMI */
1291 /* When VBVA is enabled, the VRDP server is informed in the VideoAccelFlush.
1292 * Inform the server here only if VBVA is disabled.
1293 */
1294 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
1295 mParent->consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
1296 }
1297}
1298
1299/**
1300 * Returns the upper left and lower right corners of the virtual framebuffer.
1301 * The lower right is "exclusive" (i.e. first pixel beyond the framebuffer),
1302 * and the origin is (0, 0), not (1, 1) like the GUI returns.
1303 */
1304void Display::getFramebufferDimensions(int32_t *px1, int32_t *py1,
1305 int32_t *px2, int32_t *py2)
1306{
1307 int32_t x1 = 0, y1 = 0, x2 = 0, y2 = 0;
1308 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1309
1310 AssertPtrReturnVoid(px1);
1311 AssertPtrReturnVoid(py1);
1312 AssertPtrReturnVoid(px2);
1313 AssertPtrReturnVoid(py2);
1314 LogRelFlowFunc(("\n"));
1315
1316 if (!mpDrv)
1317 return;
1318 /* If VBVA is not in use then this flag will not be set and this
1319 * will still work as it should. */
1320 if (!maFramebuffers[0].fDisabled)
1321 {
1322 x1 = (int32_t)maFramebuffers[0].xOrigin;
1323 y1 = (int32_t)maFramebuffers[0].yOrigin;
1324 x2 = mpDrv->IConnector.cx + (int32_t)maFramebuffers[0].xOrigin;
1325 y2 = mpDrv->IConnector.cy + (int32_t)maFramebuffers[0].yOrigin;
1326 }
1327 for (unsigned i = 1; i < mcMonitors; ++i)
1328 {
1329 if (!maFramebuffers[i].fDisabled)
1330 {
1331 x1 = RT_MIN(x1, maFramebuffers[i].xOrigin);
1332 y1 = RT_MIN(y1, maFramebuffers[i].yOrigin);
1333 x2 = RT_MAX(x2, maFramebuffers[i].xOrigin
1334 + (int32_t)maFramebuffers[i].w);
1335 y2 = RT_MAX(y2, maFramebuffers[i].yOrigin
1336 + (int32_t)maFramebuffers[i].h);
1337 }
1338 }
1339 *px1 = x1;
1340 *py1 = y1;
1341 *px2 = x2;
1342 *py2 = y2;
1343}
1344
1345static bool displayIntersectRect(RTRECT *prectResult,
1346 const RTRECT *prect1,
1347 const RTRECT *prect2)
1348{
1349 /* Initialize result to an empty record. */
1350 memset (prectResult, 0, sizeof (RTRECT));
1351
1352 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
1353 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
1354
1355 if (xLeftResult < xRightResult)
1356 {
1357 /* There is intersection by X. */
1358
1359 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
1360 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
1361
1362 if (yTopResult < yBottomResult)
1363 {
1364 /* There is intersection by Y. */
1365
1366 prectResult->xLeft = xLeftResult;
1367 prectResult->yTop = yTopResult;
1368 prectResult->xRight = xRightResult;
1369 prectResult->yBottom = yBottomResult;
1370
1371 return true;
1372 }
1373 }
1374
1375 return false;
1376}
1377
1378int Display::handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1379{
1380 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc( RT_MAX(cRect, 1)
1381 * sizeof (RTRECT));
1382 if (!pVisibleRegion)
1383 {
1384 return VERR_NO_TMP_MEMORY;
1385 }
1386
1387 unsigned uScreenId;
1388 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1389 {
1390 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1391
1392 if (!pFBInfo->pFramebuffer.isNull())
1393 {
1394 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
1395 {
1396 /* handle the case where new rectangles are received from the GA
1397 * when framebuffer resizing is in progress.
1398 * Just save the rectangles to be applied for later time when FB resizing is complete
1399 * (from handleResizeCompletedEMT).
1400 * This is done to prevent a race condition where a new rectangles are received
1401 * from the GA after a resize event and framebuffer resizing is still in progress
1402 * As a result the coordinates of the framebuffer are still
1403 * not updated and hence there is no intersection with the new rectangles passed
1404 * for the new region (THis is checked in the above if condition ). With 0 intersection,
1405 * cRectVisibleRegions = 0 is returned to the GUI and if GUI has invalidated its
1406 * earlier region then it draws nothihing and seamless mode doesn't display the
1407 * guest desktop.
1408 */
1409 SaveSeamlessRectLock();
1410 RTMemFree(pFBInfo->mpSavedVisibleRegion);
1411
1412 pFBInfo->mpSavedVisibleRegion = (RTRECT *)RTMemAlloc( RT_MAX(cRect, 1)
1413 * sizeof (RTRECT));
1414 if (pFBInfo->mpSavedVisibleRegion)
1415 {
1416 memcpy(pFBInfo->mpSavedVisibleRegion, pRect, cRect * sizeof(RTRECT));
1417 pFBInfo->mcSavedVisibleRegion = cRect;
1418 }
1419 else
1420 {
1421 pFBInfo->mcSavedVisibleRegion = 0;
1422 }
1423 SaveSeamlessRectUnLock();
1424 continue;
1425 }
1426 /* Prepare a new array of rectangles which intersect with the framebuffer.
1427 */
1428 RTRECT rectFramebuffer;
1429 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1430 {
1431 rectFramebuffer.xLeft = 0;
1432 rectFramebuffer.yTop = 0;
1433 if (mpDrv)
1434 {
1435 rectFramebuffer.xRight = mpDrv->IConnector.cx;
1436 rectFramebuffer.yBottom = mpDrv->IConnector.cy;
1437 }
1438 else
1439 {
1440 rectFramebuffer.xRight = 0;
1441 rectFramebuffer.yBottom = 0;
1442 }
1443 }
1444 else
1445 {
1446 rectFramebuffer.xLeft = pFBInfo->xOrigin;
1447 rectFramebuffer.yTop = pFBInfo->yOrigin;
1448 rectFramebuffer.xRight = pFBInfo->xOrigin + pFBInfo->w;
1449 rectFramebuffer.yBottom = pFBInfo->yOrigin + pFBInfo->h;
1450 }
1451
1452 uint32_t cRectVisibleRegion = 0;
1453
1454 uint32_t i;
1455 for (i = 0; i < cRect; i++)
1456 {
1457 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1458 {
1459 pVisibleRegion[cRectVisibleRegion].xLeft -= pFBInfo->xOrigin;
1460 pVisibleRegion[cRectVisibleRegion].yTop -= pFBInfo->yOrigin;
1461 pVisibleRegion[cRectVisibleRegion].xRight -= pFBInfo->xOrigin;
1462 pVisibleRegion[cRectVisibleRegion].yBottom -= pFBInfo->yOrigin;
1463
1464 cRectVisibleRegion++;
1465 }
1466 }
1467 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1468 }
1469 }
1470
1471#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1472 BOOL is3denabled = FALSE;
1473
1474 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
1475
1476 VMMDev *vmmDev = mParent->getVMMDev();
1477 if (is3denabled && vmmDev)
1478 {
1479 if (mhCrOglSvc)
1480 {
1481 VBOXCRCMDCTL_HGCM *pCtl = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(RT_MAX(cRect, 1) * sizeof (RTRECT)
1482 + sizeof (VBOXCRCMDCTL_HGCM));
1483 if (pCtl)
1484 {
1485 RTRECT *pRectsCopy = (RTRECT*)(pCtl+1);
1486 memcpy(pRectsCopy, pRect, cRect * sizeof (RTRECT));
1487
1488 pCtl->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
1489 pCtl->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
1490
1491 pCtl->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1492 pCtl->aParms[0].u.pointer.addr = pRectsCopy;
1493 pCtl->aParms[0].u.pointer.size = cRect * sizeof (RTRECT);
1494
1495 int rc = crCtlSubmit(&pCtl->Hdr, sizeof (*pCtl), displayCrCmdFree, pCtl);
1496 if (!RT_SUCCESS(rc))
1497 {
1498 AssertMsgFailed(("crCtlSubmit failed rc %d\n", rc));
1499 RTMemFree(pCtl);
1500 }
1501 }
1502 else
1503 AssertMsgFailed(("failed to allocate rects memory\n"));
1504 }
1505 else
1506 AssertMsgFailed(("mhCrOglSvc is NULL\n"));
1507 }
1508#endif
1509
1510 RTMemTmpFree(pVisibleRegion);
1511
1512 return VINF_SUCCESS;
1513}
1514
1515int Display::handleQueryVisibleRegion(uint32_t *pcRect, PRTRECT pRect)
1516{
1517 // @todo Currently not used by the guest and is not implemented in framebuffers. Remove?
1518 return VERR_NOT_SUPPORTED;
1519}
1520
1521typedef struct _VBVADIRTYREGION
1522{
1523 /* Copies of object's pointers used by vbvaRgn functions. */
1524 DISPLAYFBINFO *paFramebuffers;
1525 unsigned cMonitors;
1526 Display *pDisplay;
1527 PPDMIDISPLAYPORT pPort;
1528
1529} VBVADIRTYREGION;
1530
1531static void vbvaRgnInit (VBVADIRTYREGION *prgn, DISPLAYFBINFO *paFramebuffers, unsigned cMonitors,
1532 Display *pd, PPDMIDISPLAYPORT pp)
1533{
1534 prgn->paFramebuffers = paFramebuffers;
1535 prgn->cMonitors = cMonitors;
1536 prgn->pDisplay = pd;
1537 prgn->pPort = pp;
1538
1539 unsigned uScreenId;
1540 for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
1541 {
1542 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1543
1544 RT_ZERO(pFBInfo->dirtyRect);
1545 }
1546}
1547
1548static void vbvaRgnDirtyRect (VBVADIRTYREGION *prgn, unsigned uScreenId, VBVACMDHDR *phdr)
1549{
1550 LogSunlover(("x = %d, y = %d, w = %d, h = %d\n",
1551 phdr->x, phdr->y, phdr->w, phdr->h));
1552
1553 /*
1554 * Here update rectangles are accumulated to form an update area.
1555 * @todo
1556 * Now the simplest method is used which builds one rectangle that
1557 * includes all update areas. A bit more advanced method can be
1558 * employed here. The method should be fast however.
1559 */
1560 if (phdr->w == 0 || phdr->h == 0)
1561 {
1562 /* Empty rectangle. */
1563 return;
1564 }
1565
1566 int32_t xRight = phdr->x + phdr->w;
1567 int32_t yBottom = phdr->y + phdr->h;
1568
1569 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1570
1571 if (pFBInfo->dirtyRect.xRight == 0)
1572 {
1573 /* This is the first rectangle to be added. */
1574 pFBInfo->dirtyRect.xLeft = phdr->x;
1575 pFBInfo->dirtyRect.yTop = phdr->y;
1576 pFBInfo->dirtyRect.xRight = xRight;
1577 pFBInfo->dirtyRect.yBottom = yBottom;
1578 }
1579 else
1580 {
1581 /* Adjust region coordinates. */
1582 if (pFBInfo->dirtyRect.xLeft > phdr->x)
1583 {
1584 pFBInfo->dirtyRect.xLeft = phdr->x;
1585 }
1586
1587 if (pFBInfo->dirtyRect.yTop > phdr->y)
1588 {
1589 pFBInfo->dirtyRect.yTop = phdr->y;
1590 }
1591
1592 if (pFBInfo->dirtyRect.xRight < xRight)
1593 {
1594 pFBInfo->dirtyRect.xRight = xRight;
1595 }
1596
1597 if (pFBInfo->dirtyRect.yBottom < yBottom)
1598 {
1599 pFBInfo->dirtyRect.yBottom = yBottom;
1600 }
1601 }
1602
1603 if (pFBInfo->fDefaultFormat)
1604 {
1605 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
1606 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, phdr->x, phdr->y, phdr->w, phdr->h);
1607 prgn->pDisplay->handleDisplayUpdateLegacy (phdr->x + pFBInfo->xOrigin,
1608 phdr->y + pFBInfo->yOrigin, phdr->w, phdr->h);
1609 }
1610
1611 return;
1612}
1613
1614static void vbvaRgnUpdateFramebuffer (VBVADIRTYREGION *prgn, unsigned uScreenId)
1615{
1616 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1617
1618 uint32_t w = pFBInfo->dirtyRect.xRight - pFBInfo->dirtyRect.xLeft;
1619 uint32_t h = pFBInfo->dirtyRect.yBottom - pFBInfo->dirtyRect.yTop;
1620
1621 if (!pFBInfo->fDefaultFormat && pFBInfo->pFramebuffer && w != 0 && h != 0)
1622 {
1623 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
1624 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
1625 prgn->pDisplay->handleDisplayUpdateLegacy (pFBInfo->dirtyRect.xLeft + pFBInfo->xOrigin,
1626 pFBInfo->dirtyRect.yTop + pFBInfo->yOrigin, w, h);
1627 }
1628}
1629
1630static void vbvaSetMemoryFlags (VBVAMEMORY *pVbvaMemory,
1631 bool fVideoAccelEnabled,
1632 bool fVideoAccelVRDP,
1633 uint32_t fu32SupportedOrders,
1634 DISPLAYFBINFO *paFBInfos,
1635 unsigned cFBInfos)
1636{
1637 if (pVbvaMemory)
1638 {
1639 /* This called only on changes in mode. So reset VRDP always. */
1640 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
1641
1642 if (fVideoAccelEnabled)
1643 {
1644 fu32Flags |= VBVA_F_MODE_ENABLED;
1645
1646 if (fVideoAccelVRDP)
1647 {
1648 fu32Flags |= VBVA_F_MODE_VRDP | VBVA_F_MODE_VRDP_ORDER_MASK;
1649
1650 pVbvaMemory->fu32SupportedOrders = fu32SupportedOrders;
1651 }
1652 }
1653
1654 pVbvaMemory->fu32ModeFlags = fu32Flags;
1655 }
1656
1657 unsigned uScreenId;
1658 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1659 {
1660 if (paFBInfos[uScreenId].pHostEvents)
1661 {
1662 paFBInfos[uScreenId].pHostEvents->fu32Events |= VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1663 }
1664 }
1665}
1666
1667#ifdef VBOX_WITH_HGSMI
1668static void vbvaSetMemoryFlagsHGSMI (unsigned uScreenId,
1669 uint32_t fu32SupportedOrders,
1670 bool fVideoAccelVRDP,
1671 DISPLAYFBINFO *pFBInfo)
1672{
1673 LogRelFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1674
1675 if (pFBInfo->pVBVAHostFlags)
1676 {
1677 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1678
1679 if (pFBInfo->fVBVAEnabled)
1680 {
1681 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1682
1683 if (fVideoAccelVRDP)
1684 {
1685 fu32HostEvents |= VBVA_F_MODE_VRDP;
1686 }
1687 }
1688
1689 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1690 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1691
1692 LogRelFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1693 }
1694}
1695
1696static void vbvaSetMemoryFlagsAllHGSMI (uint32_t fu32SupportedOrders,
1697 bool fVideoAccelVRDP,
1698 DISPLAYFBINFO *paFBInfos,
1699 unsigned cFBInfos)
1700{
1701 unsigned uScreenId;
1702
1703 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1704 {
1705 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1706 }
1707}
1708#endif /* VBOX_WITH_HGSMI */
1709
1710bool Display::VideoAccelAllowed (void)
1711{
1712 return true;
1713}
1714
1715int Display::vbvaLock(void)
1716{
1717 return RTCritSectEnter(&mVBVALock);
1718}
1719
1720void Display::vbvaUnlock(void)
1721{
1722 RTCritSectLeave(&mVBVALock);
1723}
1724
1725int Display::SaveSeamlessRectLock(void)
1726{
1727 return RTCritSectEnter(&mSaveSeamlessRectLock);
1728}
1729
1730void Display::SaveSeamlessRectUnLock(void)
1731{
1732 RTCritSectLeave(&mSaveSeamlessRectLock);
1733}
1734
1735
1736/**
1737 * @thread EMT
1738 */
1739int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1740{
1741 int rc;
1742 vbvaLock();
1743 rc = videoAccelEnable (fEnable, pVbvaMemory);
1744 vbvaUnlock();
1745 return rc;
1746}
1747
1748int Display::videoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1749{
1750 int rc = VINF_SUCCESS;
1751
1752 /* Called each time the guest wants to use acceleration,
1753 * or when the VGA device disables acceleration,
1754 * or when restoring the saved state with accel enabled.
1755 *
1756 * VGA device disables acceleration on each video mode change
1757 * and on reset.
1758 *
1759 * Guest enabled acceleration at will. And it has to enable
1760 * acceleration after a mode change.
1761 */
1762 LogRelFlowFunc(("mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
1763 mfVideoAccelEnabled, fEnable, pVbvaMemory));
1764
1765 /* Strictly check parameters. Callers must not pass anything in the case. */
1766 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
1767
1768 if (!VideoAccelAllowed ())
1769 return VERR_NOT_SUPPORTED;
1770
1771 /*
1772 * Verify that the VM is in running state. If it is not,
1773 * then this must be postponed until it goes to running.
1774 */
1775 if (!mfMachineRunning)
1776 {
1777 Assert (!mfVideoAccelEnabled);
1778
1779 LogRelFlowFunc(("Machine is not yet running.\n"));
1780
1781 if (fEnable)
1782 {
1783 mfPendingVideoAccelEnable = fEnable;
1784 mpPendingVbvaMemory = pVbvaMemory;
1785 }
1786
1787 return rc;
1788 }
1789
1790 /* Check that current status is not being changed */
1791 if (mfVideoAccelEnabled == fEnable)
1792 return rc;
1793
1794 if (mfVideoAccelEnabled)
1795 {
1796 /* Process any pending orders and empty the VBVA ring buffer. */
1797 videoAccelFlush ();
1798 }
1799
1800 if (!fEnable && mpVbvaMemory)
1801 mpVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
1802
1803 /* Safety precaution. There is no more VBVA until everything is setup! */
1804 mpVbvaMemory = NULL;
1805 mfVideoAccelEnabled = false;
1806
1807 /* Update entire display. */
1808 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].u32ResizeStatus == ResizeStatus_Void)
1809 mpDrv->pUpPort->pfnUpdateDisplayAll(mpDrv->pUpPort);
1810
1811 /* Everything OK. VBVA status can be changed. */
1812
1813 /* Notify the VMMDev, which saves VBVA status in the saved state,
1814 * and needs to know current status.
1815 */
1816 VMMDev *pVMMDev = mParent->getVMMDev();
1817 if (pVMMDev)
1818 {
1819 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1820 if (pVMMDevPort)
1821 pVMMDevPort->pfnVBVAChange(pVMMDevPort, fEnable);
1822 }
1823
1824 if (fEnable)
1825 {
1826 mpVbvaMemory = pVbvaMemory;
1827 mfVideoAccelEnabled = true;
1828
1829 /* Initialize the hardware memory. */
1830 vbvaSetMemoryFlags(mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
1831 mpVbvaMemory->off32Data = 0;
1832 mpVbvaMemory->off32Free = 0;
1833
1834 memset(mpVbvaMemory->aRecords, 0, sizeof (mpVbvaMemory->aRecords));
1835 mpVbvaMemory->indexRecordFirst = 0;
1836 mpVbvaMemory->indexRecordFree = 0;
1837
1838 mfu32PendingVideoAccelDisable = false;
1839
1840 LogRel(("VBVA: Enabled.\n"));
1841 }
1842 else
1843 {
1844 LogRel(("VBVA: Disabled.\n"));
1845 }
1846
1847 LogRelFlowFunc(("VideoAccelEnable: rc = %Rrc.\n", rc));
1848
1849 return rc;
1850}
1851
1852/* Called always by one VRDP server thread. Can be thread-unsafe.
1853 */
1854void Display::VideoAccelVRDP (bool fEnable)
1855{
1856 LogRelFlowFunc(("fEnable = %d\n", fEnable));
1857
1858 vbvaLock();
1859
1860 int c = fEnable?
1861 ASMAtomicIncS32 (&mcVideoAccelVRDPRefs):
1862 ASMAtomicDecS32 (&mcVideoAccelVRDPRefs);
1863
1864 Assert (c >= 0);
1865
1866 if (c == 0)
1867 {
1868 /* The last client has disconnected, and the accel can be
1869 * disabled.
1870 */
1871 Assert (fEnable == false);
1872
1873 mfVideoAccelVRDP = false;
1874 mfu32SupportedOrders = 0;
1875
1876 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1877 maFramebuffers, mcMonitors);
1878#ifdef VBOX_WITH_HGSMI
1879 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1880 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1881#endif /* VBOX_WITH_HGSMI */
1882
1883 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1884 }
1885 else if ( c == 1
1886 && !mfVideoAccelVRDP)
1887 {
1888 /* The first client has connected. Enable the accel.
1889 */
1890 Assert (fEnable == true);
1891
1892 mfVideoAccelVRDP = true;
1893 /* Supporting all orders. */
1894 mfu32SupportedOrders = ~0;
1895
1896 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1897 maFramebuffers, mcMonitors);
1898#ifdef VBOX_WITH_HGSMI
1899 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1900 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1901#endif /* VBOX_WITH_HGSMI */
1902
1903 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1904 }
1905 else
1906 {
1907 /* A client is connected or disconnected but there is no change in the
1908 * accel state. It remains enabled.
1909 */
1910 Assert (mfVideoAccelVRDP == true);
1911 }
1912 vbvaUnlock();
1913}
1914
1915static bool vbvaVerifyRingBuffer (VBVAMEMORY *pVbvaMemory)
1916{
1917 return true;
1918}
1919
1920static void vbvaFetchBytes (VBVAMEMORY *pVbvaMemory, uint8_t *pu8Dst, uint32_t cbDst)
1921{
1922 if (cbDst >= VBVA_RING_BUFFER_SIZE)
1923 {
1924 AssertMsgFailed (("cbDst = 0x%08X, ring buffer size 0x%08X\n", cbDst, VBVA_RING_BUFFER_SIZE));
1925 return;
1926 }
1927
1928 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
1929 uint8_t *src = &pVbvaMemory->au8RingBuffer[pVbvaMemory->off32Data];
1930 int32_t i32Diff = cbDst - u32BytesTillBoundary;
1931
1932 if (i32Diff <= 0)
1933 {
1934 /* Chunk will not cross buffer boundary. */
1935 memcpy (pu8Dst, src, cbDst);
1936 }
1937 else
1938 {
1939 /* Chunk crosses buffer boundary. */
1940 memcpy (pu8Dst, src, u32BytesTillBoundary);
1941 memcpy (pu8Dst + u32BytesTillBoundary, &pVbvaMemory->au8RingBuffer[0], i32Diff);
1942 }
1943
1944 /* Advance data offset. */
1945 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbDst) % VBVA_RING_BUFFER_SIZE;
1946
1947 return;
1948}
1949
1950
1951static bool vbvaPartialRead (uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
1952{
1953 uint8_t *pu8New;
1954
1955 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
1956 *ppu8, *pcb, cbRecord));
1957
1958 if (*ppu8)
1959 {
1960 Assert (*pcb);
1961 pu8New = (uint8_t *)RTMemRealloc (*ppu8, cbRecord);
1962 }
1963 else
1964 {
1965 Assert (!*pcb);
1966 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
1967 }
1968
1969 if (!pu8New)
1970 {
1971 /* Memory allocation failed, fail the function. */
1972 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
1973 cbRecord));
1974
1975 if (*ppu8)
1976 {
1977 RTMemFree (*ppu8);
1978 }
1979
1980 *ppu8 = NULL;
1981 *pcb = 0;
1982
1983 return false;
1984 }
1985
1986 /* Fetch data from the ring buffer. */
1987 vbvaFetchBytes (pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
1988
1989 *ppu8 = pu8New;
1990 *pcb = cbRecord;
1991
1992 return true;
1993}
1994
1995/* For contiguous chunks just return the address in the buffer.
1996 * For crossing boundary - allocate a buffer from heap.
1997 */
1998bool Display::vbvaFetchCmd (VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
1999{
2000 uint32_t indexRecordFirst = mpVbvaMemory->indexRecordFirst;
2001 uint32_t indexRecordFree = mpVbvaMemory->indexRecordFree;
2002
2003#ifdef DEBUG_sunlover
2004 LogFlowFunc(("first = %d, free = %d\n",
2005 indexRecordFirst, indexRecordFree));
2006#endif /* DEBUG_sunlover */
2007
2008 if (!vbvaVerifyRingBuffer (mpVbvaMemory))
2009 {
2010 return false;
2011 }
2012
2013 if (indexRecordFirst == indexRecordFree)
2014 {
2015 /* No records to process. Return without assigning output variables. */
2016 return true;
2017 }
2018
2019 VBVARECORD *pRecord = &mpVbvaMemory->aRecords[indexRecordFirst];
2020
2021#ifdef DEBUG_sunlover
2022 LogFlowFunc(("cbRecord = 0x%08X\n", pRecord->cbRecord));
2023#endif /* DEBUG_sunlover */
2024
2025 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
2026
2027 if (mcbVbvaPartial)
2028 {
2029 /* There is a partial read in process. Continue with it. */
2030
2031 Assert (mpu8VbvaPartial);
2032
2033 LogFlowFunc(("continue partial record mcbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
2034 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
2035
2036 if (cbRecord > mcbVbvaPartial)
2037 {
2038 /* New data has been added to the record. */
2039 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
2040 {
2041 return false;
2042 }
2043 }
2044
2045 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
2046 {
2047 /* The record is completed by guest. Return it to the caller. */
2048 *ppHdr = (VBVACMDHDR *)mpu8VbvaPartial;
2049 *pcbCmd = mcbVbvaPartial;
2050
2051 mpu8VbvaPartial = NULL;
2052 mcbVbvaPartial = 0;
2053
2054 /* Advance the record index. */
2055 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
2056
2057#ifdef DEBUG_sunlover
2058 LogFlowFunc(("partial done ok, data = %d, free = %d\n",
2059 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
2060#endif /* DEBUG_sunlover */
2061 }
2062
2063 return true;
2064 }
2065
2066 /* A new record need to be processed. */
2067 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
2068 {
2069 /* Current record is being written by guest. '=' is important here. */
2070 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
2071 {
2072 /* Partial read must be started. */
2073 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
2074 {
2075 return false;
2076 }
2077
2078 LogFlowFunc(("started partial record mcbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
2079 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
2080 }
2081
2082 return true;
2083 }
2084
2085 /* Current record is complete. If it is not empty, process it. */
2086 if (cbRecord)
2087 {
2088 /* The size of largest contiguous chunk in the ring biffer. */
2089 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - mpVbvaMemory->off32Data;
2090
2091 /* The ring buffer pointer. */
2092 uint8_t *au8RingBuffer = &mpVbvaMemory->au8RingBuffer[0];
2093
2094 /* The pointer to data in the ring buffer. */
2095 uint8_t *src = &au8RingBuffer[mpVbvaMemory->off32Data];
2096
2097 /* Fetch or point the data. */
2098 if (u32BytesTillBoundary >= cbRecord)
2099 {
2100 /* The command does not cross buffer boundary. Return address in the buffer. */
2101 *ppHdr = (VBVACMDHDR *)src;
2102
2103 /* Advance data offset. */
2104 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
2105 }
2106 else
2107 {
2108 /* The command crosses buffer boundary. Rare case, so not optimized. */
2109 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
2110
2111 if (!dst)
2112 {
2113 LogRelFlowFunc(("could not allocate %d bytes from heap!!!\n", cbRecord));
2114 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
2115 return false;
2116 }
2117
2118 vbvaFetchBytes (mpVbvaMemory, dst, cbRecord);
2119
2120 *ppHdr = (VBVACMDHDR *)dst;
2121
2122#ifdef DEBUG_sunlover
2123 LogFlowFunc(("Allocated from heap %p\n", dst));
2124#endif /* DEBUG_sunlover */
2125 }
2126 }
2127
2128 *pcbCmd = cbRecord;
2129
2130 /* Advance the record index. */
2131 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
2132
2133#ifdef DEBUG_sunlover
2134 LogFlowFunc(("done ok, data = %d, free = %d\n",
2135 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
2136#endif /* DEBUG_sunlover */
2137
2138 return true;
2139}
2140
2141void Display::vbvaReleaseCmd (VBVACMDHDR *pHdr, int32_t cbCmd)
2142{
2143 uint8_t *au8RingBuffer = mpVbvaMemory->au8RingBuffer;
2144
2145 if ( (uint8_t *)pHdr >= au8RingBuffer
2146 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
2147 {
2148 /* The pointer is inside ring buffer. Must be continuous chunk. */
2149 Assert (VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
2150
2151 /* Do nothing. */
2152
2153 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
2154 }
2155 else
2156 {
2157 /* The pointer is outside. It is then an allocated copy. */
2158
2159#ifdef DEBUG_sunlover
2160 LogFlowFunc(("Free heap %p\n", pHdr));
2161#endif /* DEBUG_sunlover */
2162
2163 if ((uint8_t *)pHdr == mpu8VbvaPartial)
2164 {
2165 mpu8VbvaPartial = NULL;
2166 mcbVbvaPartial = 0;
2167 }
2168 else
2169 {
2170 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
2171 }
2172
2173 RTMemFree (pHdr);
2174 }
2175
2176 return;
2177}
2178
2179
2180/**
2181 * Called regularly on the DisplayRefresh timer.
2182 * Also on behalf of guest, when the ring buffer is full.
2183 *
2184 * @thread EMT
2185 */
2186void Display::VideoAccelFlush (void)
2187{
2188 vbvaLock();
2189 videoAccelFlush();
2190 vbvaUnlock();
2191}
2192
2193/* Under VBVA lock. DevVGA is not taken. */
2194void Display::videoAccelFlush (void)
2195{
2196#ifdef DEBUG_sunlover_2
2197 LogFlowFunc(("mfVideoAccelEnabled = %d\n", mfVideoAccelEnabled));
2198#endif /* DEBUG_sunlover_2 */
2199
2200 if (!mfVideoAccelEnabled)
2201 {
2202 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
2203 return;
2204 }
2205
2206 /* Here VBVA is enabled and we have the accelerator memory pointer. */
2207 Assert(mpVbvaMemory);
2208
2209#ifdef DEBUG_sunlover_2
2210 LogFlowFunc(("indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
2211 mpVbvaMemory->indexRecordFirst, mpVbvaMemory->indexRecordFree,
2212 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
2213#endif /* DEBUG_sunlover_2 */
2214
2215 /* Quick check for "nothing to update" case. */
2216 if (mpVbvaMemory->indexRecordFirst == mpVbvaMemory->indexRecordFree)
2217 {
2218 return;
2219 }
2220
2221 /* Process the ring buffer */
2222 unsigned uScreenId;
2223
2224 /* Initialize dirty rectangles accumulator. */
2225 VBVADIRTYREGION rgn;
2226 vbvaRgnInit (&rgn, maFramebuffers, mcMonitors, this, mpDrv->pUpPort);
2227
2228 for (;;)
2229 {
2230 VBVACMDHDR *phdr = NULL;
2231 uint32_t cbCmd = ~0;
2232
2233 /* Fetch the command data. */
2234 if (!vbvaFetchCmd (&phdr, &cbCmd))
2235 {
2236 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
2237 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
2238
2239 /* Disable VBVA on those processing errors. */
2240 videoAccelEnable (false, NULL);
2241
2242 break;
2243 }
2244
2245 if (cbCmd == uint32_t(~0))
2246 {
2247 /* No more commands yet in the queue. */
2248 break;
2249 }
2250
2251 if (cbCmd != 0)
2252 {
2253#ifdef DEBUG_sunlover
2254 LogFlowFunc(("hdr: cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
2255 cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
2256#endif /* DEBUG_sunlover */
2257
2258 VBVACMDHDR hdrSaved = *phdr;
2259
2260 int x = phdr->x;
2261 int y = phdr->y;
2262 int w = phdr->w;
2263 int h = phdr->h;
2264
2265 uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
2266
2267 phdr->x = (int16_t)x;
2268 phdr->y = (int16_t)y;
2269 phdr->w = (uint16_t)w;
2270 phdr->h = (uint16_t)h;
2271
2272 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
2273
2274 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
2275 {
2276 /* Handle the command.
2277 *
2278 * Guest is responsible for updating the guest video memory.
2279 * The Windows guest does all drawing using Eng*.
2280 *
2281 * For local output, only dirty rectangle information is used
2282 * to update changed areas.
2283 *
2284 * Dirty rectangles are accumulated to exclude overlapping updates and
2285 * group small updates to a larger one.
2286 */
2287
2288 /* Accumulate the update. */
2289 vbvaRgnDirtyRect (&rgn, uScreenId, phdr);
2290
2291 /* Forward the command to VRDP server. */
2292 mParent->consoleVRDPServer()->SendUpdate (uScreenId, phdr, cbCmd);
2293
2294 *phdr = hdrSaved;
2295 }
2296 }
2297
2298 vbvaReleaseCmd (phdr, cbCmd);
2299 }
2300
2301 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
2302 {
2303 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
2304 {
2305 /* Draw the framebuffer. */
2306 vbvaRgnUpdateFramebuffer (&rgn, uScreenId);
2307 }
2308 }
2309}
2310
2311int Display::videoAccelRefreshProcess(void)
2312{
2313 int rc = VWRN_INVALID_STATE; /* Default is to do a display update in VGA device. */
2314
2315 vbvaLock();
2316
2317 if (ASMAtomicCmpXchgU32(&mfu32PendingVideoAccelDisable, false, true))
2318 {
2319 videoAccelEnable (false, NULL);
2320 }
2321 else if (mfPendingVideoAccelEnable)
2322 {
2323 /* Acceleration was enabled while machine was not yet running
2324 * due to restoring from saved state. Update entire display and
2325 * actually enable acceleration.
2326 */
2327 Assert(mpPendingVbvaMemory);
2328
2329 /* Acceleration can not be yet enabled.*/
2330 Assert(mpVbvaMemory == NULL);
2331 Assert(!mfVideoAccelEnabled);
2332
2333 if (mfMachineRunning)
2334 {
2335 videoAccelEnable (mfPendingVideoAccelEnable,
2336 mpPendingVbvaMemory);
2337
2338 /* Reset the pending state. */
2339 mfPendingVideoAccelEnable = false;
2340 mpPendingVbvaMemory = NULL;
2341 }
2342
2343 rc = VINF_TRY_AGAIN;
2344 }
2345 else
2346 {
2347 Assert(mpPendingVbvaMemory == NULL);
2348
2349 if (mfVideoAccelEnabled)
2350 {
2351 Assert(mpVbvaMemory);
2352 videoAccelFlush ();
2353
2354 rc = VINF_SUCCESS; /* VBVA processed, no need to a display update. */
2355 }
2356 }
2357
2358 vbvaUnlock();
2359
2360 return rc;
2361}
2362
2363
2364// IDisplay methods
2365/////////////////////////////////////////////////////////////////////////////
2366STDMETHODIMP Display::GetScreenResolution (ULONG aScreenId,
2367 ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel,
2368 LONG *aXOrigin, LONG *aYOrigin)
2369{
2370 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2371
2372 AutoCaller autoCaller(this);
2373 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2374
2375 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2376
2377 uint32_t u32Width = 0;
2378 uint32_t u32Height = 0;
2379 uint32_t u32BitsPerPixel = 0;
2380 int32_t xOrigin = 0;
2381 int32_t yOrigin = 0;
2382
2383 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2384 {
2385 if (mpDrv)
2386 {
2387 u32Width = mpDrv->IConnector.cx;
2388 u32Height = mpDrv->IConnector.cy;
2389 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &u32BitsPerPixel);
2390 AssertRC(rc);
2391 }
2392 }
2393 else if (aScreenId < mcMonitors)
2394 {
2395 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2396 u32Width = pFBInfo->w;
2397 u32Height = pFBInfo->h;
2398 u32BitsPerPixel = pFBInfo->u16BitsPerPixel;
2399 xOrigin = pFBInfo->xOrigin;
2400 yOrigin = pFBInfo->yOrigin;
2401 }
2402 else
2403 {
2404 return E_INVALIDARG;
2405 }
2406
2407 if (aWidth)
2408 *aWidth = u32Width;
2409 if (aHeight)
2410 *aHeight = u32Height;
2411 if (aBitsPerPixel)
2412 *aBitsPerPixel = u32BitsPerPixel;
2413 if (aXOrigin)
2414 *aXOrigin = xOrigin;
2415 if (aYOrigin)
2416 *aYOrigin = yOrigin;
2417
2418 return S_OK;
2419}
2420
2421STDMETHODIMP Display::SetFramebuffer(ULONG aScreenId, IFramebuffer *aFramebuffer)
2422{
2423 LogRelFlowFunc(("\n"));
2424
2425 if (aFramebuffer != NULL)
2426 CheckComArgOutPointerValid(aFramebuffer);
2427
2428 AutoCaller autoCaller(this);
2429 if (FAILED(autoCaller.rc()))
2430 return autoCaller.rc();
2431
2432 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2433
2434 Console::SafeVMPtrQuiet ptrVM(mParent);
2435 if (ptrVM.isOk())
2436 {
2437 /* Must release the lock here because the changeFramebuffer will
2438 * also obtain it. */
2439 alock.release();
2440
2441 /* send request to the EMT thread */
2442 int vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY,
2443 (PFNRT)changeFramebuffer, 3, this, aFramebuffer, aScreenId);
2444
2445 alock.acquire();
2446
2447 ComAssertRCRet (vrc, E_FAIL);
2448
2449#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2450 {
2451 BOOL is3denabled;
2452 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2453
2454 if (is3denabled)
2455 {
2456 VBOXCRCMDCTL_HGCM data;
2457 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2458 data.Hdr.u32Function = SHCRGL_HOST_FN_SCREEN_CHANGED;
2459
2460 data.aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
2461 data.aParms[0].u.uint32 = aScreenId;
2462
2463 alock.release();
2464
2465 crCtlSubmitSync(&data.Hdr, sizeof (data));
2466
2467 alock.acquire();
2468 }
2469 }
2470#endif /* #if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL) */
2471 }
2472 else
2473 {
2474 /* No VM is created (VM is powered off), do a direct call */
2475 int vrc = changeFramebuffer (this, aFramebuffer, aScreenId);
2476 ComAssertRCRet (vrc, E_FAIL);
2477 }
2478
2479 return S_OK;
2480}
2481
2482STDMETHODIMP Display::AttachFramebuffer(ULONG aScreenId,
2483 IFramebuffer *aFramebuffer)
2484{
2485 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2486
2487 CheckComArgPointerValid(aFramebuffer);
2488
2489 AutoCaller autoCaller(this);
2490 if (FAILED(autoCaller.rc()))
2491 return autoCaller.rc();
2492
2493 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2494
2495 if (aScreenId >= mcMonitors)
2496 return setError(E_INVALIDARG, tr("AttachFramebuffer: Invalid screen %d (total %d)"),
2497 aScreenId, mcMonitors);
2498
2499 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2500 if (!pFBInfo->pFramebuffer.isNull())
2501 return setError(E_FAIL, tr("AttachFramebuffer: Framebuffer already attached to %d"),
2502 aScreenId);
2503
2504 pFBInfo->pFramebuffer = aFramebuffer;
2505
2506 /* The driver might not have been constructed yet */
2507 if (mpDrv)
2508 {
2509 /* Setup the new framebuffer, the resize will lead to an updateDisplayData call. */
2510
2511#if defined(VBOX_WITH_CROGL)
2512 /* Release the lock, because SHCRGL_HOST_FN_SCREEN_CHANGED will read current framebuffer */
2513 /* @todo investigate */
2514 {
2515 BOOL is3denabled;
2516 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2517
2518 if (is3denabled)
2519 {
2520 alock.release();
2521 }
2522 }
2523#endif
2524
2525 /* @todo generic code for all monitors. */
2526 if (pFBInfo->fVBVAEnabled && pFBInfo->pu8FramebufferVRAM)
2527 {
2528 /* This display in VBVA mode. Resize it to the last guest resolution,
2529 * if it has been reported.
2530 */
2531 handleDisplayResize(aScreenId, pFBInfo->u16BitsPerPixel,
2532 pFBInfo->pu8FramebufferVRAM,
2533 pFBInfo->u32LineSize,
2534 pFBInfo->w,
2535 pFBInfo->h,
2536 pFBInfo->flags);
2537 }
2538 else if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2539 {
2540 /* VGA device mode, only for the primary screen. */
2541 handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, mLastBitsPerPixel,
2542 mLastAddress,
2543 mLastBytesPerLine,
2544 mLastWidth,
2545 mLastHeight,
2546 mLastFlags);
2547 }
2548 }
2549
2550 LogRelFlowFunc(("Attached to %d\n", aScreenId));
2551 return S_OK;
2552}
2553
2554STDMETHODIMP Display::DetachFramebuffer(ULONG aScreenId)
2555{
2556 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2557
2558 AutoCaller autoCaller(this);
2559 if (FAILED(autoCaller.rc()))
2560 return autoCaller.rc();
2561
2562 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2563
2564 if (aScreenId >= mcMonitors)
2565 return setError(E_INVALIDARG, tr("DetachFramebuffer: Invalid screen %d (total %d)"),
2566 aScreenId, mcMonitors);
2567
2568 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2569
2570 pFBInfo->pFramebuffer.setNull();
2571
2572 return S_OK;
2573}
2574
2575STDMETHODIMP Display::QueryFramebuffer(ULONG aScreenId,
2576 IFramebuffer **aFramebuffer)
2577{
2578 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2579
2580 CheckComArgOutPointerValid(aFramebuffer);
2581
2582 AutoCaller autoCaller(this);
2583 if (FAILED(autoCaller.rc()))
2584 return autoCaller.rc();
2585
2586 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2587
2588 if (aScreenId >= mcMonitors)
2589 return setError(E_INVALIDARG, tr("QueryFramebuffer: Invalid screen %d (total %d)"),
2590 aScreenId, mcMonitors);
2591
2592 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2593
2594 *aFramebuffer = pFBInfo->pFramebuffer;
2595 if (!pFBInfo->pFramebuffer.isNull())
2596 pFBInfo->pFramebuffer->AddRef();
2597
2598 return S_OK;
2599}
2600
2601STDMETHODIMP Display::GetFramebuffer(ULONG aScreenId,
2602 IFramebuffer **aFramebuffer, LONG *aXOrigin, LONG *aYOrigin)
2603{
2604 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2605
2606 CheckComArgOutPointerValid(aFramebuffer);
2607
2608 AutoCaller autoCaller(this);
2609 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2610
2611 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2612
2613 if (aScreenId != 0 && aScreenId >= mcMonitors)
2614 return E_INVALIDARG;
2615
2616 /* @todo this should be actually done on EMT. */
2617 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2618
2619 *aFramebuffer = pFBInfo->pFramebuffer;
2620 if (*aFramebuffer)
2621 (*aFramebuffer)->AddRef ();
2622 if (aXOrigin)
2623 *aXOrigin = pFBInfo->xOrigin;
2624 if (aYOrigin)
2625 *aYOrigin = pFBInfo->yOrigin;
2626
2627 return S_OK;
2628}
2629
2630STDMETHODIMP Display::SetVideoModeHint(ULONG aDisplay, BOOL aEnabled,
2631 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
2632 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel)
2633{
2634 AutoCaller autoCaller(this);
2635 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2636
2637 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2638
2639 CHECK_CONSOLE_DRV(mpDrv);
2640
2641 /*
2642 * Do some rough checks for valid input
2643 */
2644 ULONG width = aWidth;
2645 if (!width)
2646 width = mpDrv->IConnector.cx;
2647 ULONG height = aHeight;
2648 if (!height)
2649 height = mpDrv->IConnector.cy;
2650 ULONG bpp = aBitsPerPixel;
2651 if (!bpp)
2652 {
2653 uint32_t cBits = 0;
2654 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
2655 AssertRC(rc);
2656 bpp = cBits;
2657 }
2658 ULONG cMonitors;
2659 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
2660 if (cMonitors == 0 && aDisplay > 0)
2661 return E_INVALIDARG;
2662 if (aDisplay >= cMonitors)
2663 return E_INVALIDARG;
2664
2665 /*
2666 * sunlover 20070614: It is up to the guest to decide whether the hint is
2667 * valid. Therefore don't do any VRAM sanity checks here!
2668 */
2669
2670 /* Have to release the lock because the pfnRequestDisplayChange
2671 * will call EMT. */
2672 alock.release();
2673
2674 VMMDev *pVMMDev = mParent->getVMMDev();
2675 if (pVMMDev)
2676 {
2677 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2678 if (pVMMDevPort)
2679 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel,
2680 aDisplay, aOriginX, aOriginY,
2681 RT_BOOL(aEnabled), RT_BOOL(aChangeOrigin));
2682 }
2683 return S_OK;
2684}
2685
2686STDMETHODIMP Display::SetSeamlessMode (BOOL enabled)
2687{
2688 AutoCaller autoCaller(this);
2689 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2690
2691 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2692
2693 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
2694 alock.release();
2695
2696 VMMDev *pVMMDev = mParent->getVMMDev();
2697 if (pVMMDev)
2698 {
2699 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2700 if (pVMMDevPort)
2701 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
2702 }
2703
2704#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2705 if (!enabled)
2706 {
2707 BOOL is3denabled = FALSE;
2708
2709 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2710
2711 VMMDev *vmmDev = mParent->getVMMDev();
2712 if (is3denabled && vmmDev)
2713 {
2714 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)RTMemAlloc(sizeof (VBOXCRCMDCTL_HGCM));
2715 if (!pData)
2716 {
2717 AssertMsgFailed(("RTMemAlloc failed\n"));
2718 return VERR_NO_MEMORY;
2719 }
2720
2721 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2722 pData->Hdr.u32Function = SHCRGL_HOST_FN_SET_VISIBLE_REGION;
2723
2724 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2725 pData->aParms[0].u.pointer.addr = NULL;
2726 pData->aParms[0].u.pointer.size = 0; /* <- means null rects, NULL pRects address and 0 rects means "disable" */
2727
2728 int rc = crCtlSubmit(&pData->Hdr, sizeof (*pData), displayCrCmdFree, pData);
2729 if (!RT_SUCCESS(rc))
2730 {
2731 AssertMsgFailed(("crCtlSubmit failed rc %d\n", rc));
2732 RTMemFree(pData);
2733 }
2734 }
2735 }
2736#endif
2737 return S_OK;
2738}
2739
2740#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2741BOOL Display::displayCheckTakeScreenshotCrOgl(Display *pDisplay, ULONG aScreenId, uint8_t *pu8Data,
2742 uint32_t u32Width, uint32_t u32Height)
2743{
2744 BOOL is3denabled;
2745 pDisplay->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2746 if (is3denabled && pDisplay->mCrOglCallbacks.pfnHasData())
2747 {
2748 VMMDev *pVMMDev = pDisplay->mParent->getVMMDev();
2749 if (pVMMDev)
2750 {
2751 CRVBOXHGCMTAKESCREENSHOT *pScreenshot = (CRVBOXHGCMTAKESCREENSHOT*)RTMemAlloc(sizeof (*pScreenshot));
2752 if (pScreenshot)
2753 {
2754 /* screen id or CRSCREEN_ALL to specify all enabled */
2755 pScreenshot->u32Screen = aScreenId;
2756 pScreenshot->u32Width = u32Width;
2757 pScreenshot->u32Height = u32Height;
2758 pScreenshot->u32Pitch = u32Width * 4;
2759 pScreenshot->pvBuffer = pu8Data;
2760 pScreenshot->pvContext = NULL;
2761 pScreenshot->pfnScreenshotBegin = NULL;
2762 pScreenshot->pfnScreenshotPerform = NULL;
2763 pScreenshot->pfnScreenshotEnd = NULL;
2764
2765 VBOXCRCMDCTL_HGCM data;
2766 data.Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
2767 data.Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
2768
2769 data.aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2770 data.aParms[0].u.pointer.addr = pScreenshot;
2771 data.aParms[0].u.pointer.size = sizeof (*pScreenshot);
2772
2773 int rc = pDisplay->crCtlSubmitSync(&data.Hdr, sizeof (data));
2774
2775 RTMemFree(pScreenshot);
2776
2777 if (RT_SUCCESS(rc))
2778 return TRUE;
2779 else
2780 {
2781 AssertMsgFailed(("failed to get screenshot data from crOgl %d\n", rc));
2782 /* fall back to the non-3d mechanism */
2783 }
2784 }
2785 }
2786 }
2787 return FALSE;
2788}
2789#endif
2790
2791int Display::displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppu8Data, size_t *pcbData,
2792 uint32_t *pu32Width, uint32_t *pu32Height)
2793{
2794 int rc;
2795 pDisplay->vbvaLock();
2796 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
2797 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
2798 {
2799 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppu8Data, pcbData, pu32Width, pu32Height);
2800 }
2801 else if (aScreenId < pDisplay->mcMonitors)
2802 {
2803 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2804
2805 uint32_t width = pFBInfo->w;
2806 uint32_t height = pFBInfo->h;
2807
2808 /* Allocate 32 bit per pixel bitmap. */
2809 size_t cbRequired = width * 4 * height;
2810
2811 if (cbRequired)
2812 {
2813 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbRequired);
2814
2815 if (pu8Data == NULL)
2816 {
2817 rc = VERR_NO_MEMORY;
2818 }
2819 else
2820 {
2821 /* Copy guest VRAM to the allocated 32bpp buffer. */
2822 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2823 int32_t xSrc = 0;
2824 int32_t ySrc = 0;
2825 uint32_t u32SrcWidth = width;
2826 uint32_t u32SrcHeight = height;
2827 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2828 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2829
2830 uint8_t *pu8Dst = pu8Data;
2831 int32_t xDst = 0;
2832 int32_t yDst = 0;
2833 uint32_t u32DstWidth = u32SrcWidth;
2834 uint32_t u32DstHeight = u32SrcHeight;
2835 uint32_t u32DstLineSize = u32DstWidth * 4;
2836 uint32_t u32DstBitsPerPixel = 32;
2837
2838 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2839 width, height,
2840 pu8Src,
2841 xSrc, ySrc,
2842 u32SrcWidth, u32SrcHeight,
2843 u32SrcLineSize, u32SrcBitsPerPixel,
2844 pu8Dst,
2845 xDst, yDst,
2846 u32DstWidth, u32DstHeight,
2847 u32DstLineSize, u32DstBitsPerPixel);
2848 if (RT_SUCCESS(rc))
2849 {
2850 *ppu8Data = pu8Data;
2851 *pcbData = cbRequired;
2852 *pu32Width = width;
2853 *pu32Height = height;
2854 }
2855 else
2856 {
2857 RTMemFree(pu8Data);
2858
2859 /* CopyRect can fail if VBVA was paused in VGA device, retry using the generic method. */
2860 if ( rc == VERR_INVALID_STATE
2861 && aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2862 {
2863 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort,
2864 ppu8Data, pcbData, pu32Width, pu32Height);
2865 }
2866 }
2867 }
2868 }
2869 else
2870 {
2871 /* No image. */
2872 *ppu8Data = NULL;
2873 *pcbData = 0;
2874 *pu32Width = 0;
2875 *pu32Height = 0;
2876 rc = VINF_SUCCESS;
2877 }
2878 }
2879 else
2880 {
2881 rc = VERR_INVALID_PARAMETER;
2882 }
2883 pDisplay->vbvaUnlock();
2884 return rc;
2885}
2886
2887static int displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
2888 BYTE *address, ULONG width, ULONG height)
2889{
2890 uint8_t *pu8Data = NULL;
2891 size_t cbData = 0;
2892 uint32_t cx = 0;
2893 uint32_t cy = 0;
2894 int vrc = VINF_SUCCESS;
2895
2896# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2897 if (Display::displayCheckTakeScreenshotCrOgl(pDisplay, aScreenId, (uint8_t*)address, width, height))
2898 return VINF_SUCCESS;
2899#endif
2900
2901 int cRetries = 5;
2902
2903 while (cRetries-- > 0)
2904 {
2905 /* Note! Not sure if the priority call is such a good idea here, but
2906 it would be nice to have an accurate screenshot for the bug
2907 report if the VM deadlocks. */
2908 vrc = VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)Display::displayTakeScreenshotEMT, 6,
2909 pDisplay, aScreenId, &pu8Data, &cbData, &cx, &cy);
2910 if (vrc != VERR_TRY_AGAIN)
2911 {
2912 break;
2913 }
2914
2915 RTThreadSleep(10);
2916 }
2917
2918 if (RT_SUCCESS(vrc) && pu8Data)
2919 {
2920 if (cx == width && cy == height)
2921 {
2922 /* No scaling required. */
2923 memcpy(address, pu8Data, cbData);
2924 }
2925 else
2926 {
2927 /* Scale. */
2928 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2929
2930 uint8_t *dst = address;
2931 uint8_t *src = pu8Data;
2932 int dstW = width;
2933 int dstH = height;
2934 int srcW = cx;
2935 int srcH = cy;
2936 int iDeltaLine = cx * 4;
2937
2938 BitmapScale32(dst,
2939 dstW, dstH,
2940 src,
2941 iDeltaLine,
2942 srcW, srcH);
2943 }
2944
2945 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2946 {
2947 /* This can be called from any thread. */
2948 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pu8Data);
2949 }
2950 else
2951 {
2952 RTMemFree(pu8Data);
2953 }
2954 }
2955
2956 return vrc;
2957}
2958
2959STDMETHODIMP Display::TakeScreenShot(ULONG aScreenId, BYTE *address, ULONG width, ULONG height)
2960{
2961 /// @todo (r=dmik) this function may take too long to complete if the VM
2962 // is doing something like saving state right now. Which, in case if it
2963 // is called on the GUI thread, will make it unresponsive. We should
2964 // check the machine state here (by enclosing the check and VMRequCall
2965 // within the Console lock to make it atomic).
2966
2967 LogRelFlowFunc(("address=%p, width=%d, height=%d\n",
2968 address, width, height));
2969
2970 CheckComArgNotNull(address);
2971 CheckComArgExpr(width, width != 0);
2972 CheckComArgExpr(height, height != 0);
2973
2974 /* Do not allow too large screenshots. This also filters out negative
2975 * values passed as either 'width' or 'height'.
2976 */
2977 CheckComArgExpr(width, width <= 32767);
2978 CheckComArgExpr(height, height <= 32767);
2979
2980 AutoCaller autoCaller(this);
2981 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2982
2983 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2984
2985 if (!mpDrv)
2986 return E_FAIL;
2987
2988 Console::SafeVMPtr ptrVM(mParent);
2989 if (!ptrVM.isOk())
2990 return ptrVM.rc();
2991
2992 HRESULT rc = S_OK;
2993
2994 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
2995
2996 /* Release lock because other thread (EMT) is called and it may initiate a resize
2997 * which also needs lock.
2998 *
2999 * This method does not need the lock anymore.
3000 */
3001 alock.release();
3002
3003 int vrc = displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, address, width, height);
3004
3005 if (vrc == VERR_NOT_IMPLEMENTED)
3006 rc = setError(E_NOTIMPL,
3007 tr("This feature is not implemented"));
3008 else if (vrc == VERR_TRY_AGAIN)
3009 rc = setError(E_UNEXPECTED,
3010 tr("This feature is not available at this time"));
3011 else if (RT_FAILURE(vrc))
3012 rc = setError(VBOX_E_IPRT_ERROR,
3013 tr("Could not take a screenshot (%Rrc)"), vrc);
3014
3015 LogRelFlowFunc(("rc=%Rhrc\n", rc));
3016 return rc;
3017}
3018
3019STDMETHODIMP Display::TakeScreenShotToArray(ULONG aScreenId, ULONG width, ULONG height,
3020 ComSafeArrayOut(BYTE, aScreenData))
3021{
3022 LogRelFlowFunc(("width=%d, height=%d\n", width, height));
3023
3024 CheckComArgOutSafeArrayPointerValid(aScreenData);
3025 CheckComArgExpr(width, width != 0);
3026 CheckComArgExpr(height, height != 0);
3027
3028 /* Do not allow too large screenshots. This also filters out negative
3029 * values passed as either 'width' or 'height'.
3030 */
3031 CheckComArgExpr(width, width <= 32767);
3032 CheckComArgExpr(height, height <= 32767);
3033
3034 AutoCaller autoCaller(this);
3035 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3036
3037 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3038
3039 if (!mpDrv)
3040 return E_FAIL;
3041
3042 Console::SafeVMPtr ptrVM(mParent);
3043 if (!ptrVM.isOk())
3044 return ptrVM.rc();
3045
3046 HRESULT rc = S_OK;
3047
3048 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
3049
3050 /* Release lock because other thread (EMT) is called and it may initiate a resize
3051 * which also needs lock.
3052 *
3053 * This method does not need the lock anymore.
3054 */
3055 alock.release();
3056
3057 size_t cbData = width * 4 * height;
3058 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
3059
3060 if (!pu8Data)
3061 return E_OUTOFMEMORY;
3062
3063 int vrc = displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, pu8Data, width, height);
3064
3065 if (RT_SUCCESS(vrc))
3066 {
3067 /* Convert pixels to format expected by the API caller: [0] R, [1] G, [2] B, [3] A. */
3068 uint8_t *pu8 = pu8Data;
3069 unsigned cPixels = width * height;
3070 while (cPixels)
3071 {
3072 uint8_t u8 = pu8[0];
3073 pu8[0] = pu8[2];
3074 pu8[2] = u8;
3075 pu8[3] = 0xff;
3076 cPixels--;
3077 pu8 += 4;
3078 }
3079
3080 com::SafeArray<BYTE> screenData(cbData);
3081 screenData.initFrom(pu8Data, cbData);
3082 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
3083 }
3084 else if (vrc == VERR_NOT_IMPLEMENTED)
3085 rc = setError(E_NOTIMPL,
3086 tr("This feature is not implemented"));
3087 else
3088 rc = setError(VBOX_E_IPRT_ERROR,
3089 tr("Could not take a screenshot (%Rrc)"), vrc);
3090
3091 RTMemFree(pu8Data);
3092
3093 LogRelFlowFunc(("rc=%Rhrc\n", rc));
3094 return rc;
3095}
3096
3097STDMETHODIMP Display::TakeScreenShotPNGToArray(ULONG aScreenId, ULONG width, ULONG height,
3098 ComSafeArrayOut(BYTE, aScreenData))
3099{
3100 LogRelFlowFunc(("width=%d, height=%d\n", width, height));
3101
3102 CheckComArgOutSafeArrayPointerValid(aScreenData);
3103 CheckComArgExpr(width, width != 0);
3104 CheckComArgExpr(height, height != 0);
3105
3106 /* Do not allow too large screenshots. This also filters out negative
3107 * values passed as either 'width' or 'height'.
3108 */
3109 CheckComArgExpr(width, width <= 32767);
3110 CheckComArgExpr(height, height <= 32767);
3111
3112 AutoCaller autoCaller(this);
3113 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3114
3115 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3116
3117 CHECK_CONSOLE_DRV(mpDrv);
3118
3119 Console::SafeVMPtr ptrVM(mParent);
3120 if (!ptrVM.isOk())
3121 return ptrVM.rc();
3122
3123 HRESULT rc = S_OK;
3124
3125 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
3126
3127 /* Release lock because other thread (EMT) is called and it may initiate a resize
3128 * which also needs lock.
3129 *
3130 * This method does not need the lock anymore.
3131 */
3132 alock.release();
3133
3134 size_t cbData = width * 4 * height;
3135 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
3136
3137 if (!pu8Data)
3138 return E_OUTOFMEMORY;
3139
3140 int vrc = displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, pu8Data, width, height);
3141
3142 if (RT_SUCCESS(vrc))
3143 {
3144 uint8_t *pu8PNG = NULL;
3145 uint32_t cbPNG = 0;
3146 uint32_t cxPNG = 0;
3147 uint32_t cyPNG = 0;
3148
3149 vrc = DisplayMakePNG(pu8Data, width, height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
3150 if (RT_SUCCESS(vrc))
3151 {
3152 com::SafeArray<BYTE> screenData(cbPNG);
3153 screenData.initFrom(pu8PNG, cbPNG);
3154 if (pu8PNG)
3155 RTMemFree(pu8PNG);
3156
3157 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
3158 }
3159 else
3160 {
3161 if (pu8PNG)
3162 RTMemFree(pu8PNG);
3163 rc = setError(VBOX_E_IPRT_ERROR,
3164 tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
3165 }
3166 }
3167 else if (vrc == VERR_NOT_IMPLEMENTED)
3168 rc = setError(E_NOTIMPL,
3169 tr("This feature is not implemented"));
3170 else
3171 rc = setError(VBOX_E_IPRT_ERROR,
3172 tr("Could not take a screenshot (%Rrc)"), vrc);
3173
3174 RTMemFree(pu8Data);
3175
3176 LogRelFlowFunc(("rc=%Rhrc\n", rc));
3177 return rc;
3178}
3179
3180int Display::VideoCaptureEnableScreens(ComSafeArrayIn(BOOL, aScreens))
3181{
3182#ifdef VBOX_WITH_VPX
3183 com::SafeArray<BOOL> Screens(ComSafeArrayInArg(aScreens));
3184 for (unsigned i = 0; i < Screens.size(); i++)
3185 maVideoRecEnabled[i] = RT_BOOL(Screens[i]);
3186 return VINF_SUCCESS;
3187#else
3188 return VERR_NOT_IMPLEMENTED;
3189#endif
3190}
3191
3192/**
3193 * Start video capturing. Does nothing if capturing is already active.
3194 */
3195int Display::VideoCaptureStart()
3196{
3197#ifdef VBOX_WITH_VPX
3198 if (VideoRecIsEnabled(mpVideoRecCtx))
3199 return VINF_SUCCESS;
3200
3201 int rc = VideoRecContextCreate(&mpVideoRecCtx, mcMonitors);
3202 if (RT_FAILURE(rc))
3203 {
3204 LogFlow(("Failed to create video recording context (%Rrc)!\n", rc));
3205 return rc;
3206 }
3207 ComPtr<IMachine> pMachine = mParent->machine();
3208 com::SafeArray<BOOL> screens;
3209 HRESULT hrc = pMachine->COMGETTER(VideoCaptureScreens)(ComSafeArrayAsOutParam(screens));
3210 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
3211 for (unsigned i = 0; i < RT_ELEMENTS(maVideoRecEnabled); i++)
3212 maVideoRecEnabled[i] = i < screens.size() && screens[i];
3213 ULONG ulWidth;
3214 hrc = pMachine->COMGETTER(VideoCaptureWidth)(&ulWidth);
3215 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
3216 ULONG ulHeight;
3217 hrc = pMachine->COMGETTER(VideoCaptureHeight)(&ulHeight);
3218 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
3219 ULONG ulRate;
3220 hrc = pMachine->COMGETTER(VideoCaptureRate)(&ulRate);
3221 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
3222 ULONG ulFPS;
3223 hrc = pMachine->COMGETTER(VideoCaptureFPS)(&ulFPS);
3224 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
3225 BSTR strFile;
3226 hrc = pMachine->COMGETTER(VideoCaptureFile)(&strFile);
3227 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
3228 RTTIMESPEC ts;
3229 RTTimeNow(&ts);
3230 RTTIME time;
3231 RTTimeExplode(&time, &ts);
3232 for (unsigned uScreen = 0; uScreen < mcMonitors; uScreen++)
3233 {
3234 char *pszAbsPath = RTPathAbsDup(com::Utf8Str(strFile).c_str());
3235 char *pszSuff = RTPathSuffix(pszAbsPath);
3236 if (pszSuff)
3237 pszSuff = RTStrDup(pszSuff);
3238 RTPathStripSuffix(pszAbsPath);
3239 if (!pszAbsPath)
3240 rc = VERR_INVALID_PARAMETER;
3241 if (!pszSuff)
3242 pszSuff = RTStrDup(".webm");
3243 char *pszName = NULL;
3244 if (RT_SUCCESS(rc))
3245 {
3246 if (mcMonitors > 1)
3247 rc = RTStrAPrintf(&pszName, "%s-%u%s", pszAbsPath, uScreen+1, pszSuff);
3248 else
3249 rc = RTStrAPrintf(&pszName, "%s%s", pszAbsPath, pszSuff);
3250 }
3251 if (RT_SUCCESS(rc))
3252 {
3253 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
3254 pszName, ulWidth, ulHeight, ulRate, ulFPS);
3255 if (rc == VERR_ALREADY_EXISTS)
3256 {
3257 RTStrFree(pszName);
3258 pszName = NULL;
3259
3260 if (mcMonitors > 1)
3261 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ-%u%s",
3262 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
3263 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
3264 uScreen+1, pszSuff);
3265 else
3266 rc = RTStrAPrintf(&pszName, "%s-%04d-%02u-%02uT%02u-%02u-%02u-%09uZ%s",
3267 pszAbsPath, time.i32Year, time.u8Month, time.u8MonthDay,
3268 time.u8Hour, time.u8Minute, time.u8Second, time.u32Nanosecond,
3269 pszSuff);
3270 if (RT_SUCCESS(rc))
3271 rc = VideoRecStrmInit(mpVideoRecCtx, uScreen,
3272 pszName, ulWidth, ulHeight, ulRate, ulFPS);
3273 }
3274 }
3275
3276 if (RT_SUCCESS(rc))
3277 LogRel(("WebM/VP8 video recording screen #%u with %ux%u @ %u kbps, %u fps to '%s' enabled.\n",
3278 uScreen, ulWidth, ulHeight, ulRate, ulFPS, pszName));
3279 else
3280 LogRel(("Failed to initialize video recording context #%u (%Rrc)!\n", uScreen, rc));
3281 RTStrFree(pszName);
3282 RTStrFree(pszSuff);
3283 RTStrFree(pszAbsPath);
3284 }
3285 return rc;
3286#else
3287 return VERR_NOT_IMPLEMENTED;
3288#endif
3289}
3290
3291/**
3292 * Stop video capturing. Does nothing if video capturing is not active.
3293 */
3294void Display::VideoCaptureStop()
3295{
3296#ifdef VBOX_WITH_VPX
3297 if (VideoRecIsEnabled(mpVideoRecCtx))
3298 LogRel(("WebM/VP8 video recording stopped.\n"));
3299 VideoRecContextClose(mpVideoRecCtx);
3300 mpVideoRecCtx = NULL;
3301#endif
3302}
3303
3304int Display::drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address,
3305 ULONG x, ULONG y, ULONG width, ULONG height)
3306{
3307 int rc = VINF_SUCCESS;
3308 pDisplay->vbvaLock();
3309
3310 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
3311
3312 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3313 {
3314 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3315 {
3316 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
3317 }
3318 }
3319 else if (aScreenId < pDisplay->mcMonitors)
3320 {
3321 /* Copy the bitmap to the guest VRAM. */
3322 const uint8_t *pu8Src = address;
3323 int32_t xSrc = 0;
3324 int32_t ySrc = 0;
3325 uint32_t u32SrcWidth = width;
3326 uint32_t u32SrcHeight = height;
3327 uint32_t u32SrcLineSize = width * 4;
3328 uint32_t u32SrcBitsPerPixel = 32;
3329
3330 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
3331 int32_t xDst = x;
3332 int32_t yDst = y;
3333 uint32_t u32DstWidth = pFBInfo->w;
3334 uint32_t u32DstHeight = pFBInfo->h;
3335 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
3336 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
3337
3338 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
3339 width, height,
3340 pu8Src,
3341 xSrc, ySrc,
3342 u32SrcWidth, u32SrcHeight,
3343 u32SrcLineSize, u32SrcBitsPerPixel,
3344 pu8Dst,
3345 xDst, yDst,
3346 u32DstWidth, u32DstHeight,
3347 u32DstLineSize, u32DstBitsPerPixel);
3348 if (RT_SUCCESS(rc))
3349 {
3350 if (!pFBInfo->pFramebuffer.isNull())
3351 {
3352 /* Update the changed screen area. When framebuffer uses VRAM directly, just notify
3353 * it to update. And for default format, render the guest VRAM to framebuffer.
3354 */
3355 if ( pFBInfo->fDefaultFormat
3356 && !pFBInfo->fDisabled)
3357 {
3358 BYTE *pAddress = NULL;
3359 ULONG ulWidth = 0;
3360 ULONG ulHeight = 0;
3361 ULONG ulBitsPerPixel = 0;
3362 ULONG ulBytesPerLine = 0;
3363 ULONG ulPixelFormat = 0;
3364
3365 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
3366 &ulWidth,
3367 &ulHeight,
3368 &ulBitsPerPixel,
3369 &ulBytesPerLine,
3370 &ulPixelFormat);
3371 if (SUCCEEDED(hrc))
3372 {
3373 pu8Src = pFBInfo->pu8FramebufferVRAM;
3374 xSrc = x;
3375 ySrc = y;
3376 u32SrcWidth = pFBInfo->w;
3377 u32SrcHeight = pFBInfo->h;
3378 u32SrcLineSize = pFBInfo->u32LineSize;
3379 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3380
3381 /* Default format is 32 bpp. */
3382 pu8Dst = pAddress;
3383 xDst = xSrc;
3384 yDst = ySrc;
3385 u32DstWidth = u32SrcWidth;
3386 u32DstHeight = u32SrcHeight;
3387 u32DstLineSize = u32DstWidth * 4;
3388 u32DstBitsPerPixel = 32;
3389
3390 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
3391 width, height,
3392 pu8Src,
3393 xSrc, ySrc,
3394 u32SrcWidth, u32SrcHeight,
3395 u32SrcLineSize, u32SrcBitsPerPixel,
3396 pu8Dst,
3397 xDst, yDst,
3398 u32DstWidth, u32DstHeight,
3399 u32DstLineSize, u32DstBitsPerPixel);
3400 }
3401 }
3402
3403 pDisplay->handleDisplayUpdate(aScreenId, x, y, width, height);
3404 }
3405 }
3406 }
3407 else
3408 {
3409 rc = VERR_INVALID_PARAMETER;
3410 }
3411
3412 if ( RT_SUCCESS(rc)
3413 && pDisplay->maFramebuffers[aScreenId].u32ResizeStatus == ResizeStatus_Void)
3414 pDisplay->mParent->consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
3415
3416 pDisplay->vbvaUnlock();
3417 return rc;
3418}
3419
3420STDMETHODIMP Display::DrawToScreen(ULONG aScreenId, BYTE *address,
3421 ULONG x, ULONG y, ULONG width, ULONG height)
3422{
3423 /// @todo (r=dmik) this function may take too long to complete if the VM
3424 // is doing something like saving state right now. Which, in case if it
3425 // is called on the GUI thread, will make it unresponsive. We should
3426 // check the machine state here (by enclosing the check and VMRequCall
3427 // within the Console lock to make it atomic).
3428
3429 LogRelFlowFunc(("address=%p, x=%d, y=%d, width=%d, height=%d\n",
3430 (void *)address, x, y, width, height));
3431
3432 CheckComArgNotNull(address);
3433 CheckComArgExpr(width, width != 0);
3434 CheckComArgExpr(height, height != 0);
3435
3436 AutoCaller autoCaller(this);
3437 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3438
3439 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3440
3441 CHECK_CONSOLE_DRV(mpDrv);
3442
3443 Console::SafeVMPtr ptrVM(mParent);
3444 if (!ptrVM.isOk())
3445 return ptrVM.rc();
3446
3447 /* Release lock because the call scheduled on EMT may also try to take it. */
3448 alock.release();
3449
3450 /*
3451 * Again we're lazy and make the graphics device do all the
3452 * dirty conversion work.
3453 */
3454 int rcVBox = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::drawToScreenEMT, 7,
3455 this, aScreenId, address, x, y, width, height);
3456
3457 /*
3458 * If the function returns not supported, we'll have to do all the
3459 * work ourselves using the framebuffer.
3460 */
3461 HRESULT rc = S_OK;
3462 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
3463 {
3464 /** @todo implement generic fallback for screen blitting. */
3465 rc = E_NOTIMPL;
3466 }
3467 else if (RT_FAILURE(rcVBox))
3468 rc = setError(VBOX_E_IPRT_ERROR,
3469 tr("Could not draw to the screen (%Rrc)"), rcVBox);
3470//@todo
3471// else
3472// {
3473// /* All ok. Redraw the screen. */
3474// handleDisplayUpdate (x, y, width, height);
3475// }
3476
3477 LogRelFlowFunc(("rc=%Rhrc\n", rc));
3478 return rc;
3479}
3480
3481void Display::InvalidateAndUpdateEMT(Display *pDisplay, unsigned uId, bool fUpdateAll)
3482{
3483 pDisplay->vbvaLock();
3484 unsigned uScreenId;
3485 for (uScreenId = (fUpdateAll ? 0 : uId); uScreenId < pDisplay->mcMonitors; uScreenId++)
3486 {
3487 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3488
3489 if ( !pFBInfo->fVBVAEnabled
3490 && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3491 && !pFBInfo->pFramebuffer.isNull())
3492 {
3493 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort);
3494 }
3495 else
3496 {
3497 if ( !pFBInfo->pFramebuffer.isNull()
3498 && !pFBInfo->fDisabled
3499 && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3500 {
3501 /* Render complete VRAM screen to the framebuffer.
3502 * When framebuffer uses VRAM directly, just notify it to update.
3503 */
3504 if (pFBInfo->fDefaultFormat && !pFBInfo->pSourceBitmap.isNull())
3505 {
3506 BYTE *pAddress = NULL;
3507 ULONG ulWidth = 0;
3508 ULONG ulHeight = 0;
3509 ULONG ulBitsPerPixel = 0;
3510 ULONG ulBytesPerLine = 0;
3511 ULONG ulPixelFormat = 0;
3512
3513 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
3514 &ulWidth,
3515 &ulHeight,
3516 &ulBitsPerPixel,
3517 &ulBytesPerLine,
3518 &ulPixelFormat);
3519 if (SUCCEEDED(hrc))
3520 {
3521 uint32_t width = pFBInfo->w;
3522 uint32_t height = pFBInfo->h;
3523
3524 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3525 int32_t xSrc = 0;
3526 int32_t ySrc = 0;
3527 uint32_t u32SrcWidth = pFBInfo->w;
3528 uint32_t u32SrcHeight = pFBInfo->h;
3529 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3530 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3531
3532 /* Default format is 32 bpp. */
3533 uint8_t *pu8Dst = pAddress;
3534 int32_t xDst = xSrc;
3535 int32_t yDst = ySrc;
3536 uint32_t u32DstWidth = u32SrcWidth;
3537 uint32_t u32DstHeight = u32SrcHeight;
3538 uint32_t u32DstLineSize = u32DstWidth * 4;
3539 uint32_t u32DstBitsPerPixel = 32;
3540
3541 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
3542 * implies resize of Framebuffer is in progress and
3543 * copyrect should not be called.
3544 */
3545 if (ulWidth == pFBInfo->w && ulHeight == pFBInfo->h)
3546 {
3547
3548 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
3549 width, height,
3550 pu8Src,
3551 xSrc, ySrc,
3552 u32SrcWidth, u32SrcHeight,
3553 u32SrcLineSize, u32SrcBitsPerPixel,
3554 pu8Dst,
3555 xDst, yDst,
3556 u32DstWidth, u32DstHeight,
3557 u32DstLineSize, u32DstBitsPerPixel);
3558 }
3559 }
3560 }
3561
3562 pDisplay->handleDisplayUpdate (uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
3563 }
3564 }
3565 if (!fUpdateAll)
3566 break;
3567 }
3568 pDisplay->vbvaUnlock();
3569}
3570
3571/**
3572 * Does a full invalidation of the VM display and instructs the VM
3573 * to update it immediately.
3574 *
3575 * @returns COM status code
3576 */
3577STDMETHODIMP Display::InvalidateAndUpdate()
3578{
3579 LogRelFlowFunc(("\n"));
3580
3581 AutoCaller autoCaller(this);
3582 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3583
3584 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3585
3586 CHECK_CONSOLE_DRV(mpDrv);
3587
3588 Console::SafeVMPtr ptrVM(mParent);
3589 if (!ptrVM.isOk())
3590 return ptrVM.rc();
3591
3592 HRESULT rc = S_OK;
3593
3594 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
3595
3596 /* Have to release the lock when calling EMT. */
3597 alock.release();
3598
3599 /* pdm.h says that this has to be called from the EMT thread */
3600 int rcVBox = VMR3ReqCallVoidWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::InvalidateAndUpdateEMT,
3601 3, this, 0, true);
3602 alock.acquire();
3603
3604 if (RT_FAILURE(rcVBox))
3605 rc = setError(VBOX_E_IPRT_ERROR,
3606 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
3607
3608 LogRelFlowFunc(("rc=%Rhrc\n", rc));
3609 return rc;
3610}
3611
3612/**
3613 * Notification that the framebuffer has completed the
3614 * asynchronous resize processing
3615 *
3616 * @returns COM status code
3617 */
3618STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
3619{
3620 LogRelFlowFunc(("\n"));
3621
3622 /// @todo (dmik) can we AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); here?
3623 // This will require general code review and may add some details.
3624 // In particular, we may want to check whether EMT is really waiting for
3625 // this notification, etc. It might be also good to obey the caller to make
3626 // sure this method is not called from more than one thread at a time
3627 // (and therefore don't use Display lock at all here to save some
3628 // milliseconds).
3629 AutoCaller autoCaller(this);
3630 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3631
3632 /* this is only valid for external framebuffers */
3633 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
3634 return setError(VBOX_E_NOT_SUPPORTED,
3635 tr("Resize completed notification is valid only for external framebuffers"));
3636
3637 /* Set the flag indicating that the resize has completed and display
3638 * data need to be updated. */
3639 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
3640 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
3641 AssertRelease(f);NOREF(f);
3642
3643 return S_OK;
3644}
3645
3646STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
3647{
3648#ifdef VBOX_WITH_VIDEOHWACCEL
3649 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
3650 return S_OK;
3651#else
3652 return E_NOTIMPL;
3653#endif
3654}
3655
3656STDMETHODIMP Display::ViewportChanged(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
3657{
3658#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3659
3660 if (mcMonitors <= aScreenId)
3661 {
3662 AssertMsgFailed(("invalid screen id\n"));
3663 return E_INVALIDARG;
3664 }
3665
3666 BOOL is3denabled;
3667 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3668
3669 if (is3denabled)
3670 {
3671 int rc = crViewportNotify(aScreenId, x, y, width, height);
3672 if (RT_FAILURE(rc))
3673 {
3674 DISPLAYFBINFO *pFb = &maFramebuffers[aScreenId];
3675 pFb->pendingViewportInfo.fPending = true;
3676 pFb->pendingViewportInfo.x = x;
3677 pFb->pendingViewportInfo.y = y;
3678 pFb->pendingViewportInfo.width = width;
3679 pFb->pendingViewportInfo.height = height;
3680 }
3681 }
3682#endif /* VBOX_WITH_CROGL && VBOX_WITH_HGCM */
3683 return S_OK;
3684}
3685
3686STDMETHODIMP Display::QuerySourceBitmap(ULONG aScreenId,
3687 IDisplaySourceBitmap **aDisplaySourceBitmap)
3688{
3689 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
3690
3691 AutoCaller autoCaller(this);
3692 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3693
3694 Console::SafeVMPtr ptrVM(mParent);
3695 if (!ptrVM.isOk())
3696 return ptrVM.rc();
3697
3698 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
3699
3700 if (aScreenId >= mcMonitors)
3701 return setError(E_INVALIDARG, tr("QuerySourceBitmap: Invalid screen %d (total %d)"),
3702 aScreenId, mcMonitors);
3703
3704 HRESULT hr = querySourceBitmap(aScreenId, aDisplaySourceBitmap);
3705
3706 alock.release();
3707
3708 LogRelFlowFunc(("%Rhrc\n", hr));
3709 return hr;
3710}
3711
3712// private methods
3713/////////////////////////////////////////////////////////////////////////////
3714
3715HRESULT Display::querySourceBitmap(ULONG aScreenId,
3716 IDisplaySourceBitmap **ppDisplaySourceBitmap)
3717{
3718 HRESULT hr = S_OK;
3719
3720 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
3721 if (pFBInfo->pSourceBitmap.isNull())
3722 {
3723 /* Create a new object. */
3724 ComObjPtr<DisplaySourceBitmap> obj;
3725 hr = obj.createObject();
3726 if (SUCCEEDED(hr))
3727 {
3728 hr = obj->init(this, aScreenId, pFBInfo);
3729 }
3730
3731 if (SUCCEEDED(hr))
3732 {
3733 pFBInfo->pSourceBitmap = obj;
3734
3735 /* Whether VRAM must be copied to the internal buffer. */
3736 pFBInfo->fDefaultFormat = !obj->usesVRAM();
3737
3738 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3739 {
3740 /* Start buffer updates. */
3741 BYTE *pAddress = NULL;
3742 ULONG ulWidth = 0;
3743 ULONG ulHeight = 0;
3744 ULONG ulBitsPerPixel = 0;
3745 ULONG ulBytesPerLine = 0;
3746 ULONG ulPixelFormat = 0;
3747
3748 obj->QueryBitmapInfo(&pAddress,
3749 &ulWidth,
3750 &ulHeight,
3751 &ulBitsPerPixel,
3752 &ulBytesPerLine,
3753 &ulPixelFormat);
3754
3755 mpDrv->IConnector.pu8Data = pAddress;
3756 mpDrv->IConnector.cbScanline = ulBytesPerLine;
3757 mpDrv->IConnector.cBits = ulBitsPerPixel;
3758 mpDrv->IConnector.cx = ulWidth;
3759 mpDrv->IConnector.cy = ulHeight;
3760
3761 if (pFBInfo->fDefaultFormat)
3762 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, true);
3763 }
3764
3765 if (pFBInfo->fDefaultFormat)
3766 {
3767 /* @todo make sure that the bitmap contains the latest image? */
3768 }
3769 }
3770 }
3771
3772 if (SUCCEEDED(hr))
3773 {
3774 pFBInfo->pSourceBitmap->AddRef();
3775 *ppDisplaySourceBitmap = pFBInfo->pSourceBitmap;
3776 }
3777
3778 return hr;
3779}
3780
3781/**
3782 * Helper to update the display information from the framebuffer.
3783 *
3784 * @thread EMT
3785 */
3786int Display::updateDisplayData(void)
3787{
3788 LogRelFlowFunc(("\n"));
3789
3790 /* the driver might not have been constructed yet */
3791 if (!mpDrv)
3792 return VINF_SUCCESS;
3793
3794#ifdef VBOX_STRICT
3795 /*
3796 * Sanity check. Note that this method may be called on EMT after Console
3797 * has started the power down procedure (but before our #drvDestruct() is
3798 * called, in which case pVM will already be NULL but mpDrv will not). Since
3799 * we don't really need pVM to proceed, we avoid this check in the release
3800 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
3801 * time-critical method.
3802 */
3803 Console::SafeVMPtrQuiet ptrVM(mParent);
3804 if (ptrVM.isOk())
3805 {
3806 PVM pVM = VMR3GetVM(ptrVM.rawUVM());
3807 Assert(VM_IS_EMT(pVM));
3808 }
3809#endif
3810
3811 /* The method is only relevant to the primary framebuffer. */
3812 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
3813
3814 if (pFramebuffer)
3815 {
3816 HRESULT rc;
3817 BYTE *address = 0;
3818 rc = pFramebuffer->COMGETTER(Address) (&address);
3819 AssertComRC (rc);
3820 ULONG bytesPerLine = 0;
3821 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
3822 AssertComRC (rc);
3823 ULONG bitsPerPixel = 0;
3824 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
3825 AssertComRC (rc);
3826 ULONG width = 0;
3827 rc = pFramebuffer->COMGETTER(Width) (&width);
3828 AssertComRC (rc);
3829 ULONG height = 0;
3830 rc = pFramebuffer->COMGETTER(Height) (&height);
3831 AssertComRC (rc);
3832
3833 if ( (width != mLastWidth && mLastWidth != 0)
3834 || (height != mLastHeight && mLastHeight != 0))
3835 {
3836 LogRel(("updateDisplayData: size mismatch w %d(%d) h %d(%d)\n",
3837 width, mLastWidth, height, mLastHeight));
3838 return VERR_INVALID_STATE;
3839 }
3840
3841 mpDrv->IConnector.pu8Data = (uint8_t *) address;
3842 mpDrv->IConnector.cbScanline = bytesPerLine;
3843 mpDrv->IConnector.cBits = bitsPerPixel;
3844 mpDrv->IConnector.cx = width;
3845 mpDrv->IConnector.cy = height;
3846 }
3847 else
3848 {
3849 /* black hole */
3850 mpDrv->IConnector.pu8Data = NULL;
3851 mpDrv->IConnector.cbScanline = 0;
3852 mpDrv->IConnector.cBits = 0;
3853 mpDrv->IConnector.cx = 0;
3854 mpDrv->IConnector.cy = 0;
3855 }
3856 LogRelFlowFunc(("leave\n"));
3857 return VINF_SUCCESS;
3858}
3859
3860#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3861int Display::crViewportNotify(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
3862{
3863 VMMDev *pVMMDev = mParent->getVMMDev();
3864 if (!pVMMDev)
3865 return VERR_INVALID_STATE;
3866
3867 size_t cbData = RT_UOFFSETOF(VBOXCRCMDCTL_HGCM, aParms[5]);
3868 VBOXCRCMDCTL_HGCM *pData = (VBOXCRCMDCTL_HGCM*)alloca(cbData);
3869
3870 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
3871 pData->Hdr.u32Function = SHCRGL_HOST_FN_VIEWPORT_CHANGED;
3872
3873 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
3874 pData->aParms[0].u.uint32 = aScreenId;
3875
3876 pData->aParms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
3877 pData->aParms[1].u.uint32 = x;
3878
3879 pData->aParms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
3880 pData->aParms[2].u.uint32 = y;
3881
3882 pData->aParms[3].type = VBOX_HGCM_SVC_PARM_32BIT;
3883 pData->aParms[3].u.uint32 = width;
3884
3885 pData->aParms[4].type = VBOX_HGCM_SVC_PARM_32BIT;
3886 pData->aParms[4].u.uint32 = height;
3887
3888 return crCtlSubmitSyncIfHasDataForScreen(aScreenId, &pData->Hdr, cbData);
3889}
3890#endif
3891
3892#ifdef VBOX_WITH_CRHGSMI
3893void Display::setupCrHgsmiData(void)
3894{
3895 VMMDev *pVMMDev = mParent->getVMMDev();
3896 Assert(pVMMDev);
3897 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
3898 AssertRC(rc);
3899
3900 if (pVMMDev)
3901 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
3902 else
3903 rc = VERR_GENERAL_FAILURE;
3904
3905 if (RT_SUCCESS(rc))
3906 {
3907 Assert(mhCrOglSvc);
3908 /* setup command completion callback */
3909 VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_MAINCB Completion;
3910 Completion.Hdr.enmType = VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_MAINCB;
3911 Completion.Hdr.cbCmd = sizeof (Completion);
3912 Completion.hCompletion = mpDrv->pVBVACallbacks;
3913 Completion.pfnCompletion = mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync;
3914
3915 VBOXHGCMSVCPARM parm;
3916 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3917 parm.u.pointer.addr = &Completion;
3918 parm.u.pointer.size = 0;
3919
3920 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_CRHGSMI_CTL, 1, &parm);
3921 if (RT_SUCCESS(rc))
3922 mCrOglCallbacks = Completion.MainInterface;
3923 else
3924 AssertMsgFailed(("VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION failed rc %d", rc));
3925 }
3926
3927 if (RT_FAILURE(rc))
3928 mhCrOglSvc = NULL;
3929
3930 RTCritSectRwLeaveExcl(&mCrOglLock);
3931}
3932
3933void Display::destructCrHgsmiData(void)
3934{
3935 int rc = RTCritSectRwEnterExcl(&mCrOglLock);
3936 AssertRC(rc);
3937 mhCrOglSvc = NULL;
3938 RTCritSectRwLeaveExcl(&mCrOglLock);
3939}
3940#endif
3941
3942/**
3943 * Changes the current frame buffer. Called on EMT to avoid both
3944 * race conditions and excessive locking.
3945 *
3946 * @note locks this object for writing
3947 * @thread EMT
3948 */
3949/* static */
3950DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
3951 unsigned uScreenId)
3952{
3953 LogRelFlowFunc(("uScreenId = %d\n", uScreenId));
3954
3955 AssertReturn(that, VERR_INVALID_PARAMETER);
3956 AssertReturn(uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
3957
3958 AutoCaller autoCaller(that);
3959 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3960
3961 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
3962
3963 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
3964 pDisplayFBInfo->pFramebuffer = aFB;
3965
3966 that->mParent->consoleVRDPServer()->SendResize ();
3967
3968 /* The driver might not have been constructed yet */
3969 if (that->mpDrv)
3970 {
3971 /* Setup the new framebuffer, the resize will lead to an updateDisplayData call. */
3972 DISPLAYFBINFO *pFBInfo = &that->maFramebuffers[uScreenId];
3973
3974#if defined(VBOX_WITH_CROGL)
3975 /* Release the lock, because SHCRGL_HOST_FN_SCREEN_CHANGED will read current framebuffer */
3976 {
3977 BOOL is3denabled;
3978 that->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3979
3980 if (is3denabled)
3981 {
3982 alock.release();
3983 }
3984 }
3985#endif
3986
3987 if (pFBInfo->fVBVAEnabled && pFBInfo->pu8FramebufferVRAM)
3988 {
3989 /* This display in VBVA mode. Resize it to the last guest resolution,
3990 * if it has been reported.
3991 */
3992 that->handleDisplayResize(uScreenId, pFBInfo->u16BitsPerPixel,
3993 pFBInfo->pu8FramebufferVRAM,
3994 pFBInfo->u32LineSize,
3995 pFBInfo->w,
3996 pFBInfo->h,
3997 pFBInfo->flags);
3998 }
3999 else if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
4000 {
4001 /* VGA device mode, only for the primary screen. */
4002 that->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, that->mLastBitsPerPixel,
4003 that->mLastAddress,
4004 that->mLastBytesPerLine,
4005 that->mLastWidth,
4006 that->mLastHeight,
4007 that->mLastFlags);
4008 }
4009 }
4010
4011 LogRelFlowFunc(("leave\n"));
4012 return VINF_SUCCESS;
4013}
4014
4015/**
4016 * Handle display resize event issued by the VGA device for the primary screen.
4017 *
4018 * @see PDMIDISPLAYCONNECTOR::pfnResize
4019 */
4020DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
4021 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
4022{
4023 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4024
4025 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
4026 bpp, pvVRAM, cbLine, cx, cy));
4027
4028 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, VBVA_SCREEN_F_ACTIVE);
4029}
4030
4031/**
4032 * Handle display update.
4033 *
4034 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
4035 */
4036DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
4037 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
4038{
4039 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4040
4041#ifdef DEBUG_sunlover
4042 LogFlowFunc(("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
4043 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
4044#endif /* DEBUG_sunlover */
4045
4046 /* This call does update regardless of VBVA status.
4047 * But in VBVA mode this is called only as result of
4048 * pfnUpdateDisplayAll in the VGA device.
4049 */
4050
4051 pDrv->pDisplay->handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
4052}
4053
4054/**
4055 * Periodic display refresh callback.
4056 *
4057 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
4058 * @thread EMT
4059 */
4060DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
4061{
4062 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4063
4064#ifdef DEBUG_sunlover
4065 STAM_PROFILE_START(&g_StatDisplayRefresh, a);
4066#endif /* DEBUG_sunlover */
4067
4068#ifdef DEBUG_sunlover_2
4069 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
4070 pDrv->pDisplay->mfVideoAccelEnabled));
4071#endif /* DEBUG_sunlover_2 */
4072
4073 Display *pDisplay = pDrv->pDisplay;
4074 bool fNoUpdate = false; /* Do not update the display if any of the framebuffers is being resized. */
4075 unsigned uScreenId;
4076
4077 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
4078 {
4079 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
4080
4081 /* Check the resize status. The status can be checked normally because
4082 * the status affects only the EMT.
4083 */
4084 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
4085
4086 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
4087 {
4088 LogRelFlowFunc(("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
4089 fNoUpdate = true; /* Always set it here, because pfnUpdateDisplayAll can cause a new resize. */
4090 /* The framebuffer was resized and display data need to be updated. */
4091 pDisplay->handleResizeCompletedEMT(uScreenId);
4092 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
4093 {
4094 /* The resize status could be not Void here because a pending resize is issued. */
4095 continue;
4096 }
4097
4098 /* Repaint the display because VM continued to run during the framebuffer resize. */
4099 pDisplay->InvalidateAndUpdateEMT(pDisplay, uScreenId, false);
4100
4101 /* Continue with normal processing because the status here is ResizeStatus_Void. */
4102 }
4103 else if (u32ResizeStatus == ResizeStatus_InProgress)
4104 {
4105 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
4106 LogRelFlowFunc(("ResizeStatus_InProcess\n"));
4107 fNoUpdate = true;
4108 continue;
4109 }
4110 }
4111
4112 if (!fNoUpdate)
4113 {
4114 int rc = pDisplay->videoAccelRefreshProcess();
4115 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
4116 {
4117 if (rc == VWRN_INVALID_STATE)
4118 {
4119 /* No VBVA do a display update. */
4120 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
4121 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
4122 {
4123 pDisplay->vbvaLock();
4124 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
4125 pDisplay->vbvaUnlock();
4126 }
4127 }
4128
4129 /* Inform the VRDP server that the current display update sequence is
4130 * completed. At this moment the framebuffer memory contains a definite
4131 * image, that is synchronized with the orders already sent to VRDP client.
4132 * The server can now process redraw requests from clients or initial
4133 * fullscreen updates for new clients.
4134 */
4135 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
4136 {
4137 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
4138
4139 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
4140 {
4141 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
4142 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
4143 }
4144 }
4145 }
4146 }
4147
4148#ifdef VBOX_WITH_VPX
4149 if (VideoRecIsEnabled(pDisplay->mpVideoRecCtx))
4150 {
4151 do {
4152# if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4153 BOOL is3denabled;
4154 pDisplay->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
4155 if (is3denabled)
4156 {
4157 if (ASMAtomicCmpXchgU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_SUBMITTED, CRVREC_STATE_IDLE))
4158 {
4159 if (pDisplay->mCrOglCallbacks.pfnHasData())
4160 {
4161 /* submit */
4162 VBOXCRCMDCTL_HGCM *pData = &pDisplay->mCrOglScreenshotCtl;
4163
4164 pData->Hdr.enmType = VBOXCRCMDCTL_TYPE_HGCM;
4165 pData->Hdr.u32Function = SHCRGL_HOST_FN_TAKE_SCREENSHOT;
4166
4167 pData->aParms[0].type = VBOX_HGCM_SVC_PARM_PTR;
4168 pData->aParms[0].u.pointer.addr = &pDisplay->mCrOglScreenshotData;
4169 pData->aParms[0].u.pointer.size = sizeof (pDisplay->mCrOglScreenshotData);
4170 int rc = pDisplay->crCtlSubmit(&pData->Hdr, sizeof (*pData), displayCrCmdFree, pData);
4171 if (!RT_SUCCESS(rc))
4172 AssertMsgFailed(("crCtlSubmit failed rc %d\n", rc));
4173 }
4174
4175 /* no 3D data available, or error has occured,
4176 * go the straight way */
4177 ASMAtomicWriteU32(&pDisplay->mfCrOglVideoRecState, CRVREC_STATE_IDLE);
4178 }
4179 else
4180 {
4181 /* record request is still in progress, don't do anything */
4182 break;
4183 }
4184 }
4185# endif /* VBOX_WITH_HGCM && VBOX_WITH_CROGL */
4186
4187 uint64_t u64Now = RTTimeProgramMilliTS();
4188 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
4189 {
4190 if (!pDisplay->maVideoRecEnabled[uScreenId])
4191 continue;
4192
4193 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
4194
4195 if ( !pFBInfo->pFramebuffer.isNull()
4196 && !pFBInfo->fDisabled
4197 && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
4198 {
4199 int rc;
4200 if ( pFBInfo->fVBVAEnabled
4201 && pFBInfo->pu8FramebufferVRAM)
4202 {
4203 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
4204 FramebufferPixelFormat_FOURCC_RGB,
4205 pFBInfo->u16BitsPerPixel,
4206 pFBInfo->u32LineSize, pFBInfo->w, pFBInfo->h,
4207 pFBInfo->pu8FramebufferVRAM, u64Now);
4208 }
4209 else
4210 {
4211 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, uScreenId, 0, 0,
4212 FramebufferPixelFormat_FOURCC_RGB,
4213 pDrv->IConnector.cBits,
4214 pDrv->IConnector.cbScanline, pDrv->IConnector.cx,
4215 pDrv->IConnector.cy, pDrv->IConnector.pu8Data, u64Now);
4216 }
4217 if (rc == VINF_TRY_AGAIN)
4218 break;
4219 }
4220 }
4221 } while (0);
4222 }
4223#endif /* VBOX_WITH_VPX */
4224
4225#ifdef DEBUG_sunlover
4226 STAM_PROFILE_STOP(&g_StatDisplayRefresh, a);
4227#endif /* DEBUG_sunlover */
4228#ifdef DEBUG_sunlover_2
4229 LogFlowFunc(("leave\n"));
4230#endif /* DEBUG_sunlover_2 */
4231}
4232
4233/**
4234 * Reset notification
4235 *
4236 * @see PDMIDISPLAYCONNECTOR::pfnReset
4237 */
4238DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
4239{
4240 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4241
4242 LogRelFlowFunc(("\n"));
4243
4244 /* Disable VBVA mode. */
4245 pDrv->pDisplay->VideoAccelEnable (false, NULL);
4246}
4247
4248/**
4249 * LFBModeChange notification
4250 *
4251 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
4252 */
4253DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
4254{
4255 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4256
4257 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
4258
4259 NOREF(fEnabled);
4260
4261 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
4262 /* The LFBModeChange function is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
4263 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
4264}
4265
4266/**
4267 * Adapter information change notification.
4268 *
4269 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
4270 */
4271DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM,
4272 uint32_t u32VRAMSize)
4273{
4274 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4275
4276 if (pvVRAM == NULL)
4277 {
4278 unsigned i;
4279 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
4280 {
4281 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
4282
4283 pFBInfo->u32Offset = 0;
4284 pFBInfo->u32MaxFramebufferSize = 0;
4285 pFBInfo->u32InformationSize = 0;
4286 }
4287 }
4288#ifndef VBOX_WITH_HGSMI
4289 else
4290 {
4291 uint8_t *pu8 = (uint8_t *)pvVRAM;
4292 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
4293
4294 // @todo
4295 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
4296
4297 VBOXVIDEOINFOHDR *pHdr;
4298
4299 for (;;)
4300 {
4301 pHdr = (VBOXVIDEOINFOHDR *)pu8;
4302 pu8 += sizeof (VBOXVIDEOINFOHDR);
4303
4304 if (pu8 >= pu8End)
4305 {
4306 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
4307 break;
4308 }
4309
4310 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
4311 {
4312 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
4313 {
4314 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
4315 break;
4316 }
4317
4318 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
4319
4320 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
4321 {
4322 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
4323 break;
4324 }
4325
4326 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
4327
4328 pFBInfo->u32Offset = pDisplay->u32Offset;
4329 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
4330 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
4331
4332 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_DISPLAY: %d: at 0x%08X, size 0x%08X, info 0x%08X\n", pDisplay->u32Index,
4333 pDisplay->u32Offset, pDisplay->u32FramebufferSize, pDisplay->u32InformationSize));
4334 }
4335 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
4336 {
4337 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
4338 {
4339 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
4340 break;
4341 }
4342
4343 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
4344
4345 switch (pConf32->u32Index)
4346 {
4347 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
4348 {
4349 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
4350 } break;
4351
4352 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
4353 {
4354 /* @todo make configurable. */
4355 pConf32->u32Value = _1M;
4356 } break;
4357
4358 default:
4359 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
4360 }
4361 }
4362 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
4363 {
4364 if (pHdr->u16Length != 0)
4365 {
4366 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
4367 break;
4368 }
4369
4370 break;
4371 }
4372 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is
4373 Additions/WINNT/Graphics/Miniport/VBoxVideo. cpp
4374 pushing this to us? */
4375 {
4376 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
4377 }
4378
4379 pu8 += pHdr->u16Length;
4380 }
4381 }
4382#endif /* !VBOX_WITH_HGSMI */
4383}
4384
4385/**
4386 * Display information change notification.
4387 *
4388 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
4389 */
4390DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
4391{
4392 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4393
4394 if (uScreenId >= pDrv->pDisplay->mcMonitors)
4395 {
4396 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
4397 return;
4398 }
4399
4400 /* Get the display information structure. */
4401 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
4402
4403 uint8_t *pu8 = (uint8_t *)pvVRAM;
4404 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
4405
4406 // @todo
4407 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
4408
4409 VBOXVIDEOINFOHDR *pHdr;
4410
4411 for (;;)
4412 {
4413 pHdr = (VBOXVIDEOINFOHDR *)pu8;
4414 pu8 += sizeof (VBOXVIDEOINFOHDR);
4415
4416 if (pu8 >= pu8End)
4417 {
4418 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
4419 break;
4420 }
4421
4422 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
4423 {
4424 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
4425 {
4426 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
4427 break;
4428 }
4429
4430 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
4431
4432 pFBInfo->xOrigin = pScreen->xOrigin;
4433 pFBInfo->yOrigin = pScreen->yOrigin;
4434
4435 pFBInfo->w = pScreen->u16Width;
4436 pFBInfo->h = pScreen->u16Height;
4437
4438 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
4439 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width,
4440 pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
4441
4442 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
4443 {
4444 /* Primary screen resize is eeeeeeeee by the VGA device. */
4445 if (pFBInfo->fDisabled)
4446 {
4447 pFBInfo->fDisabled = false;
4448 fireGuestMonitorChangedEvent(pDrv->pDisplay->mParent->getEventSource(),
4449 GuestMonitorChangedEventType_Enabled,
4450 uScreenId,
4451 pFBInfo->xOrigin, pFBInfo->yOrigin,
4452 pFBInfo->w, pFBInfo->h);
4453 }
4454
4455 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel,
4456 (uint8_t *)pvVRAM + pFBInfo->u32Offset,
4457 pScreen->u32LineSize,
4458 pScreen->u16Width, pScreen->u16Height,
4459 VBVA_SCREEN_F_ACTIVE);
4460 }
4461 }
4462 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
4463 {
4464 if (pHdr->u16Length != 0)
4465 {
4466 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
4467 break;
4468 }
4469
4470 break;
4471 }
4472 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
4473 {
4474 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
4475 {
4476 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
4477 break;
4478 }
4479
4480 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
4481
4482 pFBInfo->pHostEvents = pHostEvents;
4483
4484 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
4485 pHostEvents));
4486 }
4487 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
4488 {
4489 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
4490 {
4491 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
4492 break;
4493 }
4494
4495 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
4496 pu8 += pLink->i32Offset;
4497 }
4498 else
4499 {
4500 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
4501 }
4502
4503 pu8 += pHdr->u16Length;
4504 }
4505}
4506
4507#ifdef VBOX_WITH_VIDEOHWACCEL
4508
4509#ifndef S_FALSE
4510# define S_FALSE ((HRESULT)1L)
4511#endif
4512
4513int Display::handleVHWACommandProcess(PVBOXVHWACMD pCommand)
4514{
4515 unsigned id = (unsigned)pCommand->iDisplay;
4516 int rc = VINF_SUCCESS;
4517 if (id >= mcMonitors)
4518 return VERR_INVALID_PARAMETER;
4519
4520 ComPtr<IFramebuffer> pFramebuffer;
4521 AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
4522 pFramebuffer = maFramebuffers[id].pFramebuffer;
4523 arlock.release();
4524
4525 if (pFramebuffer == NULL)
4526 return VERR_INVALID_STATE; /* notify we can not handle request atm */
4527
4528 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
4529 if (hr == S_FALSE)
4530 return VINF_SUCCESS;
4531 else if (SUCCEEDED(hr))
4532 return VINF_CALLBACK_RETURN;
4533 else if (hr == E_ACCESSDENIED)
4534 return VERR_INVALID_STATE; /* notify we can not handle request atm */
4535 else if (hr == E_NOTIMPL)
4536 return VERR_NOT_IMPLEMENTED;
4537 return VERR_GENERAL_FAILURE;
4538}
4539
4540DECLCALLBACK(int) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
4541{
4542 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4543
4544 return pDrv->pDisplay->handleVHWACommandProcess(pCommand);
4545}
4546#endif
4547
4548#ifdef VBOX_WITH_CRHGSMI
4549void Display::handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
4550{
4551 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks,
4552 (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
4553}
4554
4555void Display::handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
4556{
4557 PVBOXVDMACMD_CHROMIUM_CTL pCtl = (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr;
4558 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, pCtl, result);
4559}
4560
4561void Display::handleCrHgsmiCommandProcess(PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
4562{
4563 int rc = VERR_NOT_SUPPORTED;
4564 VBOXHGCMSVCPARM parm;
4565 parm.type = VBOX_HGCM_SVC_PARM_PTR;
4566 parm.u.pointer.addr = pCmd;
4567 parm.u.pointer.size = cbCmd;
4568
4569 if (mhCrOglSvc)
4570 {
4571 VMMDev *pVMMDev = mParent->getVMMDev();
4572 if (pVMMDev)
4573 {
4574 /* no completion callback is specified with this call,
4575 * the CrOgl code will complete the CrHgsmi command once it processes it */
4576 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
4577 AssertRC(rc);
4578 if (RT_SUCCESS(rc))
4579 return;
4580 }
4581 else
4582 rc = VERR_INVALID_STATE;
4583 }
4584
4585 /* we are here because something went wrong with command processing, complete it */
4586 handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
4587}
4588
4589void Display::handleCrHgsmiControlProcess(PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
4590{
4591 int rc = VERR_NOT_SUPPORTED;
4592 VBOXHGCMSVCPARM parm;
4593 parm.type = VBOX_HGCM_SVC_PARM_PTR;
4594 parm.u.pointer.addr = pCtl;
4595 parm.u.pointer.size = cbCtl;
4596
4597 if (mhCrOglSvc)
4598 {
4599 VMMDev *pVMMDev = mParent->getVMMDev();
4600 if (pVMMDev)
4601 {
4602 bool fCheckPendingViewport = (pCtl->enmType == VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP);
4603 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm,
4604 Display::displayCrHgsmiControlCompletion, this);
4605 AssertRC(rc);
4606 if (RT_SUCCESS(rc))
4607 {
4608 if (fCheckPendingViewport)
4609 {
4610 ULONG ul;
4611 for (ul = 0; ul < mcMonitors; ul++)
4612 {
4613 DISPLAYFBINFO *pFb = &maFramebuffers[ul];
4614 if (!pFb->pendingViewportInfo.fPending)
4615 continue;
4616
4617 rc = crViewportNotify(ul, pFb->pendingViewportInfo.x, pFb->pendingViewportInfo.y,
4618 pFb->pendingViewportInfo.width, pFb->pendingViewportInfo.height);
4619 if (RT_SUCCESS(rc))
4620 pFb->pendingViewportInfo.fPending = false;
4621 else
4622 {
4623 AssertMsgFailed(("crViewportNotify failed %d\n", rc));
4624 rc = VINF_SUCCESS;
4625 }
4626 }
4627 }
4628 return;
4629 }
4630 }
4631 else
4632 rc = VERR_INVALID_STATE;
4633 }
4634
4635 /* we are here because something went wrong with command processing, complete it */
4636 handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
4637}
4638
4639DECLCALLBACK(void) Display::displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd,
4640 uint32_t cbCmd)
4641{
4642 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4643
4644 pDrv->pDisplay->handleCrHgsmiCommandProcess(pCmd, cbCmd);
4645}
4646
4647DECLCALLBACK(void) Display::displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd,
4648 uint32_t cbCmd)
4649{
4650 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4651
4652 pDrv->pDisplay->handleCrHgsmiControlProcess(pCmd, cbCmd);
4653}
4654
4655DECLCALLBACK(void) Display::displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
4656 void *pvContext)
4657{
4658 AssertMsgFailed(("not expected!"));
4659 Display *pDisplay = (Display *)pvContext;
4660 pDisplay->handleCrHgsmiCommandCompletion(result, u32Function, pParam);
4661}
4662
4663DECLCALLBACK(void) Display::displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
4664 void *pvContext)
4665{
4666 Display *pDisplay = (Display *)pvContext;
4667 pDisplay->handleCrHgsmiControlCompletion(result, u32Function, pParam);
4668
4669}
4670#endif
4671
4672#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4673DECLCALLBACK(void) Display::displayCrHgcmCtlSubmitCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam,
4674 void *pvContext)
4675{
4676 VBOXCRCMDCTL *pCmd = (VBOXCRCMDCTL*)pParam->u.pointer.addr;
4677 if (pCmd->u.pfnInternal)
4678 ((PFNCRCTLCOMPLETION)pCmd->u.pfnInternal)(pCmd, pParam->u.pointer.size, result, pvContext);
4679}
4680
4681int Display::handleCrHgcmCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
4682 PFNCRCTLCOMPLETION pfnCompletion,
4683 void *pvCompletion)
4684{
4685 VMMDev *pVMMDev = mParent ? mParent->getVMMDev() : NULL;
4686 if (!pVMMDev)
4687 {
4688 AssertMsgFailed(("no vmmdev\n"));
4689 return VERR_INVALID_STATE;
4690 }
4691
4692 Assert(mhCrOglSvc);
4693 VBOXHGCMSVCPARM parm;
4694 parm.type = VBOX_HGCM_SVC_PARM_PTR;
4695 parm.u.pointer.addr = pCmd;
4696 parm.u.pointer.size = cbCmd;
4697
4698 pCmd->u.pfnInternal = (void(*)())pfnCompletion;
4699 int rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CTL, &parm, displayCrHgcmCtlSubmitCompletion,
4700 pvCompletion);
4701 if (!RT_SUCCESS(rc))
4702 AssertMsgFailed(("hgcmHostFastCallAsync failed rc %d\n", rc));
4703
4704 return rc;
4705}
4706
4707DECLCALLBACK(int) Display::displayCrHgcmCtlSubmit(PPDMIDISPLAYCONNECTOR pInterface,
4708 struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd,
4709 PFNCRCTLCOMPLETION pfnCompletion,
4710 void *pvCompletion)
4711{
4712 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4713 Display *pThis = pDrv->pDisplay;
4714 return pThis->handleCrHgcmCtlSubmit(pCmd, cbCmd, pfnCompletion, pvCompletion);
4715}
4716
4717int Display::crCtlSubmit(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd, PFNCRCTLCOMPLETION pfnCompletion, void *pvCompletion)
4718{
4719 int rc = RTCritSectRwEnterShared(&mCrOglLock);
4720 if (RT_SUCCESS(rc))
4721 {
4722 if (mhCrOglSvc)
4723 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmit(mpDrv->pVBVACallbacks, pCmd, cbCmd, pfnCompletion, pvCompletion);
4724 else
4725 rc = VERR_NOT_SUPPORTED;
4726
4727 RTCritSectRwLeaveShared(&mCrOglLock);
4728 }
4729 return rc;
4730}
4731
4732int Display::crCtlSubmitSync(struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
4733{
4734 int rc = RTCritSectRwEnterShared(&mCrOglLock);
4735 if (RT_SUCCESS(rc))
4736 {
4737 if (mhCrOglSvc)
4738 rc = mpDrv->pVBVACallbacks->pfnCrCtlSubmitSync(mpDrv->pVBVACallbacks, pCmd, cbCmd);
4739 else
4740 rc = VERR_NOT_SUPPORTED;
4741
4742 RTCritSectRwLeaveShared(&mCrOglLock);
4743 }
4744 return rc;
4745}
4746
4747int Display::crCtlSubmitSyncIfHasDataForScreen(uint32_t u32ScreenID, struct VBOXCRCMDCTL* pCmd, uint32_t cbCmd)
4748{
4749 if (mCrOglCallbacks.pfnHasDataForScreen(u32ScreenID))
4750 return crCtlSubmitSync(pCmd, cbCmd);
4751
4752 VBOXCRCMDCTL* pCmdCopy = (VBOXCRCMDCTL*)RTMemAlloc(cbCmd);
4753 if (!pCmdCopy)
4754 {
4755 LogRel(("RTMemAlloc failed\n"));
4756 return VERR_NO_MEMORY;
4757 }
4758
4759 memcpy(pCmdCopy, pCmd, cbCmd);
4760
4761 int rc = crCtlSubmit(pCmdCopy, cbCmd, displayCrCmdFree, pCmdCopy);
4762 if (RT_FAILURE(rc))
4763 {
4764 LogRel(("crCtlSubmit failed %d\n", rc));
4765 RTMemFree(pCmdCopy);
4766 return rc;
4767 }
4768
4769 return VINF_SUCCESS;
4770}
4771
4772bool Display::handleCrVRecScreenshotBegin(uint32_t uScreen, uint64_t u64TimeStamp)
4773{
4774# if VBOX_WITH_VPX
4775 return VideoRecIsReady(mpVideoRecCtx, uScreen, u64TimeStamp);
4776# else
4777 return false;
4778# endif
4779}
4780
4781void Display::handleCrVRecScreenshotEnd(uint32_t uScreen, uint64_t u64TimeStamp)
4782{
4783}
4784
4785void Display::handleCrVRecScreenshotPerform(uint32_t uScreen,
4786 uint32_t x, uint32_t y, uint32_t uPixelFormat,
4787 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
4788 uint32_t uGuestWidth, uint32_t uGuestHeight,
4789 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
4790{
4791 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
4792# if VBOX_WITH_VPX
4793 int rc = VideoRecCopyToIntBuf(mpVideoRecCtx, uScreen, x, y,
4794 uPixelFormat,
4795 uBitsPerPixel, uBytesPerLine,
4796 uGuestWidth, uGuestHeight,
4797 pu8BufferAddress, u64TimeStamp);
4798 Assert(rc == VINF_SUCCESS /* || rc == VERR_TRY_AGAIN || rc == VINF_TRY_AGAIN*/);
4799# endif
4800}
4801
4802void Display::handleVRecCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
4803{
4804 Assert(mfCrOglVideoRecState == CRVREC_STATE_SUBMITTED);
4805 ASMAtomicWriteU32(&mfCrOglVideoRecState, CRVREC_STATE_IDLE);
4806}
4807
4808DECLCALLBACK(void) Display::displayCrVRecScreenshotPerform(void *pvCtx, uint32_t uScreen,
4809 uint32_t x, uint32_t y,
4810 uint32_t uBitsPerPixel, uint32_t uBytesPerLine,
4811 uint32_t uGuestWidth, uint32_t uGuestHeight,
4812 uint8_t *pu8BufferAddress, uint64_t u64TimeStamp)
4813{
4814 Display *pDisplay = (Display *)pvCtx;
4815 pDisplay->handleCrVRecScreenshotPerform(uScreen,
4816 x, y, FramebufferPixelFormat_FOURCC_RGB, uBitsPerPixel,
4817 uBytesPerLine, uGuestWidth, uGuestHeight,
4818 pu8BufferAddress, u64TimeStamp);
4819}
4820
4821DECLCALLBACK(bool) Display::displayCrVRecScreenshotBegin(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
4822{
4823 Display *pDisplay = (Display *)pvCtx;
4824 return pDisplay->handleCrVRecScreenshotBegin(uScreen, u64TimeStamp);
4825}
4826
4827DECLCALLBACK(void) Display::displayCrVRecScreenshotEnd(void *pvCtx, uint32_t uScreen, uint64_t u64TimeStamp)
4828{
4829 Display *pDisplay = (Display *)pvCtx;
4830 pDisplay->handleCrVRecScreenshotEnd(uScreen, u64TimeStamp);
4831}
4832
4833DECLCALLBACK(void) Display::displayVRecCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
4834{
4835 Display *pDisplay = (Display *)pvContext;
4836 pDisplay->handleVRecCompletion(result, u32Function, pParam, pvContext);
4837}
4838
4839#endif
4840
4841
4842#ifdef VBOX_WITH_HGSMI
4843DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags, bool fRenderThreadMode)
4844{
4845 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
4846
4847 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4848 Display *pThis = pDrv->pDisplay;
4849
4850 if (pThis->maFramebuffers[uScreenId].fVBVAEnabled && pThis->maFramebuffers[uScreenId].fRenderThreadMode != fRenderThreadMode)
4851 {
4852 LogRel(("enabling different vbva mode"));
4853#ifdef DEBUG_misha
4854 AssertMsgFailed(("enabling different vbva mode"));
4855#endif
4856 return VERR_INVALID_STATE;
4857 }
4858
4859 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
4860 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
4861 pThis->maFramebuffers[uScreenId].fRenderThreadMode = fRenderThreadMode;
4862 pThis->maFramebuffers[uScreenId].fVBVAForceResize = true;
4863
4864 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
4865
4866 return VINF_SUCCESS;
4867}
4868
4869DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
4870{
4871 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
4872
4873 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4874 Display *pThis = pDrv->pDisplay;
4875
4876 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4877
4878 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
4879 {
4880 /* Make sure that the primary screen is visible now.
4881 * The guest can't use VBVA anymore, so only only the VGA device output works.
4882 */
4883 if (pFBInfo->fDisabled)
4884 {
4885 pFBInfo->fDisabled = false;
4886 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4887 GuestMonitorChangedEventType_Enabled,
4888 uScreenId,
4889 pFBInfo->xOrigin, pFBInfo->yOrigin,
4890 pFBInfo->w, pFBInfo->h);
4891 }
4892 }
4893
4894 pFBInfo->fVBVAEnabled = false;
4895 pFBInfo->fVBVAForceResize = false;
4896 pFBInfo->fRenderThreadMode = false;
4897
4898 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
4899
4900 pFBInfo->pVBVAHostFlags = NULL;
4901
4902 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
4903 {
4904 /* Force full screen update, because VGA device must take control, do resize, etc. */
4905 pThis->mpDrv->pUpPort->pfnUpdateDisplayAll(pThis->mpDrv->pUpPort);
4906 }
4907}
4908
4909DECLCALLBACK(void) Display::displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
4910{
4911 LogFlowFunc(("uScreenId %d\n", uScreenId));
4912
4913 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4914 Display *pThis = pDrv->pDisplay;
4915 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4916
4917 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
4918 {
4919 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers,
4920 pThis->mcMonitors);
4921 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
4922 }
4923
4924 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
4925 {
4926 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
4927 {
4928 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
4929 * under display device lock, so thread safe.
4930 */
4931 pFBInfo->cVBVASkipUpdate = 0;
4932 pThis->handleDisplayUpdate(uScreenId, pFBInfo->vbvaSkippedRect.xLeft - pFBInfo->xOrigin,
4933 pFBInfo->vbvaSkippedRect.yTop - pFBInfo->yOrigin,
4934 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
4935 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
4936 }
4937 }
4938 else
4939 {
4940 /* The framebuffer is being resized. */
4941 pFBInfo->cVBVASkipUpdate++;
4942 }
4943}
4944
4945DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
4946 const PVBVACMDHDR pCmd, size_t cbCmd)
4947{
4948 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
4949
4950 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4951 Display *pThis = pDrv->pDisplay;
4952 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
4953
4954 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
4955 {
4956 if (pFBInfo->fDefaultFormat)
4957 {
4958 /* Make sure that framebuffer contains the same image as the guest VRAM. */
4959 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
4960 && !pFBInfo->pFramebuffer.isNull()
4961 && !pFBInfo->fDisabled)
4962 {
4963 pDrv->pUpPort->pfnUpdateDisplayRect (pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
4964 }
4965 else if ( !pFBInfo->pFramebuffer.isNull()
4966 && !pFBInfo->fDisabled)
4967 {
4968 /* Render VRAM content to the framebuffer. */
4969 BYTE *pAddress = NULL;
4970 ULONG ulWidth = 0;
4971 ULONG ulHeight = 0;
4972 ULONG ulBitsPerPixel = 0;
4973 ULONG ulBytesPerLine = 0;
4974 ULONG ulPixelFormat = 0;
4975
4976 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
4977 &ulWidth,
4978 &ulHeight,
4979 &ulBitsPerPixel,
4980 &ulBytesPerLine,
4981 &ulPixelFormat);
4982 if (SUCCEEDED(hrc))
4983 {
4984 uint32_t width = pCmd->w;
4985 uint32_t height = pCmd->h;
4986
4987 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
4988 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
4989 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
4990 uint32_t u32SrcWidth = pFBInfo->w;
4991 uint32_t u32SrcHeight = pFBInfo->h;
4992 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
4993 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
4994
4995 uint8_t *pu8Dst = pAddress;
4996 int32_t xDst = xSrc;
4997 int32_t yDst = ySrc;
4998 uint32_t u32DstWidth = u32SrcWidth;
4999 uint32_t u32DstHeight = u32SrcHeight;
5000 uint32_t u32DstLineSize = u32DstWidth * 4;
5001 uint32_t u32DstBitsPerPixel = 32;
5002
5003 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
5004 width, height,
5005 pu8Src,
5006 xSrc, ySrc,
5007 u32SrcWidth, u32SrcHeight,
5008 u32SrcLineSize, u32SrcBitsPerPixel,
5009 pu8Dst,
5010 xDst, yDst,
5011 u32DstWidth, u32DstHeight,
5012 u32DstLineSize, u32DstBitsPerPixel);
5013 }
5014 }
5015 }
5016
5017 VBVACMDHDR hdrSaved = *pCmd;
5018
5019 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
5020
5021 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
5022 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
5023
5024 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
5025 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, (uint32_t)cbCmd);
5026
5027 *pHdrUnconst = hdrSaved;
5028 }
5029}
5030
5031DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y,
5032 uint32_t cx, uint32_t cy)
5033{
5034 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
5035
5036 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
5037 Display *pThis = pDrv->pDisplay;
5038 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
5039
5040 /* @todo handleFramebufferUpdate (uScreenId,
5041 * x - pThis->maFramebuffers[uScreenId].xOrigin,
5042 * y - pThis->maFramebuffers[uScreenId].yOrigin,
5043 * cx, cy);
5044 */
5045 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
5046 {
5047 pThis->handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
5048 }
5049 else
5050 {
5051 /* Save the updated rectangle. */
5052 int32_t xRight = x + cx;
5053 int32_t yBottom = y + cy;
5054
5055 if (pFBInfo->cVBVASkipUpdate == 1)
5056 {
5057 pFBInfo->vbvaSkippedRect.xLeft = x;
5058 pFBInfo->vbvaSkippedRect.yTop = y;
5059 pFBInfo->vbvaSkippedRect.xRight = xRight;
5060 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
5061 }
5062 else
5063 {
5064 if (pFBInfo->vbvaSkippedRect.xLeft > x)
5065 {
5066 pFBInfo->vbvaSkippedRect.xLeft = x;
5067 }
5068 if (pFBInfo->vbvaSkippedRect.yTop > y)
5069 {
5070 pFBInfo->vbvaSkippedRect.yTop = y;
5071 }
5072 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
5073 {
5074 pFBInfo->vbvaSkippedRect.xRight = xRight;
5075 }
5076 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
5077 {
5078 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
5079 }
5080 }
5081 }
5082}
5083
5084#ifdef DEBUG_sunlover
5085static void logVBVAResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
5086{
5087 LogRel(("displayVBVAResize: [%d] %s\n"
5088 " pView->u32ViewIndex %d\n"
5089 " pView->u32ViewOffset 0x%08X\n"
5090 " pView->u32ViewSize 0x%08X\n"
5091 " pView->u32MaxScreenSize 0x%08X\n"
5092 " pScreen->i32OriginX %d\n"
5093 " pScreen->i32OriginY %d\n"
5094 " pScreen->u32StartOffset 0x%08X\n"
5095 " pScreen->u32LineSize 0x%08X\n"
5096 " pScreen->u32Width %d\n"
5097 " pScreen->u32Height %d\n"
5098 " pScreen->u16BitsPerPixel %d\n"
5099 " pScreen->u16Flags 0x%04X\n"
5100 " pFBInfo->u32Offset 0x%08X\n"
5101 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
5102 " pFBInfo->u32InformationSize 0x%08X\n"
5103 " pFBInfo->fDisabled %d\n"
5104 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
5105 " pFBInfo->u16BitsPerPixel %d\n"
5106 " pFBInfo->pu8FramebufferVRAM %p\n"
5107 " pFBInfo->u32LineSize 0x%08X\n"
5108 " pFBInfo->flags 0x%04X\n"
5109 " pFBInfo->pHostEvents %p\n"
5110 " pFBInfo->u32ResizeStatus %d\n"
5111 " pFBInfo->fDefaultFormat %d\n"
5112 " dirtyRect %d-%d %d-%d\n"
5113 " pFBInfo->pendingResize.fPending %d\n"
5114 " pFBInfo->pendingResize.pixelFormat %d\n"
5115 " pFBInfo->pendingResize.pvVRAM %p\n"
5116 " pFBInfo->pendingResize.bpp %d\n"
5117 " pFBInfo->pendingResize.cbLine 0x%08X\n"
5118 " pFBInfo->pendingResize.w,h %dx%d\n"
5119 " pFBInfo->pendingResize.flags 0x%04X\n"
5120 " pFBInfo->fVBVAEnabled %d\n"
5121 " pFBInfo->fVBVAForceResize %d\n"
5122 " pFBInfo->cVBVASkipUpdate %d\n"
5123 " pFBInfo->vbvaSkippedRect %d-%d %d-%d\n"
5124 " pFBInfo->pVBVAHostFlags %p\n"
5125 "",
5126 pScreen->u32ViewIndex,
5127 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
5128 pView->u32ViewIndex,
5129 pView->u32ViewOffset,
5130 pView->u32ViewSize,
5131 pView->u32MaxScreenSize,
5132 pScreen->i32OriginX,
5133 pScreen->i32OriginY,
5134 pScreen->u32StartOffset,
5135 pScreen->u32LineSize,
5136 pScreen->u32Width,
5137 pScreen->u32Height,
5138 pScreen->u16BitsPerPixel,
5139 pScreen->u16Flags,
5140 pFBInfo->u32Offset,
5141 pFBInfo->u32MaxFramebufferSize,
5142 pFBInfo->u32InformationSize,
5143 pFBInfo->fDisabled,
5144 pFBInfo->xOrigin,
5145 pFBInfo->yOrigin,
5146 pFBInfo->w,
5147 pFBInfo->h,
5148 pFBInfo->u16BitsPerPixel,
5149 pFBInfo->pu8FramebufferVRAM,
5150 pFBInfo->u32LineSize,
5151 pFBInfo->flags,
5152 pFBInfo->pHostEvents,
5153 pFBInfo->u32ResizeStatus,
5154 pFBInfo->fDefaultFormat,
5155 pFBInfo->dirtyRect.xLeft,
5156 pFBInfo->dirtyRect.xRight,
5157 pFBInfo->dirtyRect.yTop,
5158 pFBInfo->dirtyRect.yBottom,
5159 pFBInfo->pendingResize.fPending,
5160 pFBInfo->pendingResize.pixelFormat,
5161 pFBInfo->pendingResize.pvVRAM,
5162 pFBInfo->pendingResize.bpp,
5163 pFBInfo->pendingResize.cbLine,
5164 pFBInfo->pendingResize.w,
5165 pFBInfo->pendingResize.h,
5166 pFBInfo->pendingResize.flags,
5167 pFBInfo->fVBVAEnabled,
5168 pFBInfo->fVBVAForceResize,
5169 pFBInfo->cVBVASkipUpdate,
5170 pFBInfo->vbvaSkippedRect.xLeft,
5171 pFBInfo->vbvaSkippedRect.yTop,
5172 pFBInfo->vbvaSkippedRect.xRight,
5173 pFBInfo->vbvaSkippedRect.yBottom,
5174 pFBInfo->pVBVAHostFlags
5175 ));
5176}
5177#endif /* DEBUG_sunlover */
5178
5179DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView,
5180 const PVBVAINFOSCREEN pScreen, void *pvVRAM)
5181{
5182 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
5183
5184 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
5185 Display *pThis = pDrv->pDisplay;
5186
5187 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
5188
5189 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
5190 {
5191 pThis->notifyCroglResize(pView, pScreen, pvVRAM);
5192
5193 pFBInfo->fDisabled = true;
5194 pFBInfo->flags = pScreen->u16Flags;
5195
5196 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
5197 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
5198 * the VM window will be black. */
5199 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
5200 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
5201 pThis->handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
5202 u32Width, u32Height, pScreen->u16Flags);
5203
5204 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
5205 GuestMonitorChangedEventType_Disabled,
5206 pScreen->u32ViewIndex,
5207 0, 0, 0, 0);
5208 return VINF_SUCCESS;
5209 }
5210
5211 /* If display was disabled or there is no framebuffer, a resize will be required,
5212 * because the framebuffer was/will be changed.
5213 */
5214 bool fResize = pFBInfo->fDisabled || pFBInfo->pFramebuffer.isNull();
5215
5216 if (pFBInfo->fVBVAForceResize)
5217 {
5218 /* VBVA was just enabled. Do the resize. */
5219 fResize = true;
5220 pFBInfo->fVBVAForceResize = false;
5221 }
5222
5223 /* Check if this is a real resize or a notification about the screen origin.
5224 * The guest uses this VBVAResize call for both.
5225 */
5226 fResize = fResize
5227 || pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
5228 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
5229 || pFBInfo->u32LineSize != pScreen->u32LineSize
5230 || pFBInfo->w != pScreen->u32Width
5231 || pFBInfo->h != pScreen->u32Height;
5232
5233 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
5234 || pFBInfo->yOrigin != pScreen->i32OriginY;
5235
5236 if (fNewOrigin || fResize)
5237 pThis->notifyCroglResize(pView, pScreen, pvVRAM);
5238
5239 if (pFBInfo->fDisabled)
5240 {
5241 pFBInfo->fDisabled = false;
5242 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
5243 GuestMonitorChangedEventType_Enabled,
5244 pScreen->u32ViewIndex,
5245 pScreen->i32OriginX, pScreen->i32OriginY,
5246 pScreen->u32Width, pScreen->u32Height);
5247 /* Continue to update pFBInfo. */
5248 }
5249
5250 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
5251 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
5252 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
5253
5254 pFBInfo->xOrigin = pScreen->i32OriginX;
5255 pFBInfo->yOrigin = pScreen->i32OriginY;
5256
5257 pFBInfo->w = pScreen->u32Width;
5258 pFBInfo->h = pScreen->u32Height;
5259
5260 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
5261 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
5262 pFBInfo->u32LineSize = pScreen->u32LineSize;
5263
5264 pFBInfo->flags = pScreen->u16Flags;
5265
5266 if (fNewOrigin)
5267 {
5268 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
5269 GuestMonitorChangedEventType_NewOrigin,
5270 pScreen->u32ViewIndex,
5271 pScreen->i32OriginX, pScreen->i32OriginY,
5272 0, 0);
5273 }
5274
5275 if (!fResize)
5276 {
5277 /* No parameters of the framebuffer have actually changed. */
5278 if (fNewOrigin)
5279 {
5280 /* VRDP server still need this notification. */
5281 LogRelFlowFunc(("Calling VRDP\n"));
5282 pThis->mParent->consoleVRDPServer()->SendResize();
5283 }
5284 return VINF_SUCCESS;
5285 }
5286
5287 if (pFBInfo->pFramebuffer.isNull())
5288 {
5289 /* If no framebuffer, the resize will be done later when a new framebuffer will be set in changeFramebuffer. */
5290 return VINF_SUCCESS;
5291 }
5292
5293 /* If the framebuffer already set for the screen, do a regular resize. */
5294 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
5295 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
5296 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
5297}
5298
5299DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
5300 uint32_t xHot, uint32_t yHot,
5301 uint32_t cx, uint32_t cy,
5302 const void *pvShape)
5303{
5304 LogFlowFunc(("\n"));
5305
5306 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
5307 Display *pThis = pDrv->pDisplay;
5308
5309 size_t cbShapeSize = 0;
5310
5311 if (pvShape)
5312 {
5313 cbShapeSize = (cx + 7) / 8 * cy; /* size of the AND mask */
5314 cbShapeSize = ((cbShapeSize + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
5315 }
5316 com::SafeArray<BYTE> shapeData(cbShapeSize);
5317
5318 if (pvShape)
5319 ::memcpy(shapeData.raw(), pvShape, cbShapeSize);
5320
5321 /* Tell the console about it */
5322 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
5323 xHot, yHot, cx, cy, ComSafeArrayAsInParam(shapeData));
5324
5325 return VINF_SUCCESS;
5326}
5327#endif /* VBOX_WITH_HGSMI */
5328
5329/**
5330 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
5331 */
5332DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
5333{
5334 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
5335 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
5336 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
5337 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
5338 return NULL;
5339}
5340
5341
5342/**
5343 * Destruct a display driver instance.
5344 *
5345 * @returns VBox status.
5346 * @param pDrvIns The driver instance data.
5347 */
5348DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
5349{
5350 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
5351 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
5352 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
5353
5354 if (pThis->pDisplay)
5355 {
5356 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
5357#ifdef VBOX_WITH_VPX
5358 pThis->pDisplay->VideoCaptureStop();
5359#endif
5360#ifdef VBOX_WITH_CRHGSMI
5361 pThis->pDisplay->destructCrHgsmiData();
5362#endif
5363 pThis->pDisplay->mpDrv = NULL;
5364 pThis->pDisplay->mpVMMDev = NULL;
5365 pThis->pDisplay->mLastAddress = NULL;
5366 pThis->pDisplay->mLastBytesPerLine = 0;
5367 pThis->pDisplay->mLastBitsPerPixel = 0,
5368 pThis->pDisplay->mLastWidth = 0;
5369 pThis->pDisplay->mLastHeight = 0;
5370 }
5371}
5372
5373
5374/**
5375 * Construct a display driver instance.
5376 *
5377 * @copydoc FNPDMDRVCONSTRUCT
5378 */
5379DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
5380{
5381 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
5382 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
5383 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
5384
5385 /*
5386 * Validate configuration.
5387 */
5388 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
5389 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
5390 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
5391 ("Configuration error: Not possible to attach anything to this driver!\n"),
5392 VERR_PDM_DRVINS_NO_ATTACH);
5393
5394 /*
5395 * Init Interfaces.
5396 */
5397 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
5398
5399 pThis->IConnector.pfnResize = Display::displayResizeCallback;
5400 pThis->IConnector.pfnUpdateRect = Display::displayUpdateCallback;
5401 pThis->IConnector.pfnRefresh = Display::displayRefreshCallback;
5402 pThis->IConnector.pfnReset = Display::displayResetCallback;
5403 pThis->IConnector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
5404 pThis->IConnector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
5405 pThis->IConnector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
5406#ifdef VBOX_WITH_VIDEOHWACCEL
5407 pThis->IConnector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
5408#endif
5409#ifdef VBOX_WITH_CRHGSMI
5410 pThis->IConnector.pfnCrHgsmiCommandProcess = Display::displayCrHgsmiCommandProcess;
5411 pThis->IConnector.pfnCrHgsmiControlProcess = Display::displayCrHgsmiControlProcess;
5412#endif
5413#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
5414 pThis->IConnector.pfnCrHgcmCtlSubmit = Display::displayCrHgcmCtlSubmit;
5415#endif
5416#ifdef VBOX_WITH_HGSMI
5417 pThis->IConnector.pfnVBVAEnable = Display::displayVBVAEnable;
5418 pThis->IConnector.pfnVBVADisable = Display::displayVBVADisable;
5419 pThis->IConnector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
5420 pThis->IConnector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
5421 pThis->IConnector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
5422 pThis->IConnector.pfnVBVAResize = Display::displayVBVAResize;
5423 pThis->IConnector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
5424#endif
5425
5426 /*
5427 * Get the IDisplayPort interface of the above driver/device.
5428 */
5429 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
5430 if (!pThis->pUpPort)
5431 {
5432 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
5433 return VERR_PDM_MISSING_INTERFACE_ABOVE;
5434 }
5435#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
5436 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
5437 if (!pThis->pVBVACallbacks)
5438 {
5439 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
5440 return VERR_PDM_MISSING_INTERFACE_ABOVE;
5441 }
5442#endif
5443 /*
5444 * Get the Display object pointer and update the mpDrv member.
5445 */
5446 void *pv;
5447 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
5448 if (RT_FAILURE(rc))
5449 {
5450 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
5451 return rc;
5452 }
5453 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
5454 pThis->pDisplay = pDisplay;
5455 pThis->pDisplay->mpDrv = pThis;
5456
5457 /* Disable VRAM to a buffer copy initially. */
5458 pThis->pUpPort->pfnSetRenderVRAM (pThis->pUpPort, false);
5459 pThis->IConnector.cBits = 32; /* DevVGA does nothing otherwise. */
5460
5461 /*
5462 * Start periodic screen refreshes
5463 */
5464 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
5465
5466#ifdef VBOX_WITH_CRHGSMI
5467 pDisplay->setupCrHgsmiData();
5468#endif
5469
5470#ifdef VBOX_WITH_VPX
5471 ComPtr<IMachine> pMachine = pDisplay->mParent->machine();
5472 BOOL fEnabled = false;
5473 HRESULT hrc = pMachine->COMGETTER(VideoCaptureEnabled)(&fEnabled);
5474 AssertComRCReturn(hrc, VERR_COM_UNEXPECTED);
5475 if (fEnabled)
5476 {
5477 rc = pDisplay->VideoCaptureStart();
5478 fireVideoCaptureChangedEvent(pDisplay->mParent->getEventSource());
5479 }
5480#endif
5481
5482 return rc;
5483}
5484
5485
5486/**
5487 * Display driver registration record.
5488 */
5489const PDMDRVREG Display::DrvReg =
5490{
5491 /* u32Version */
5492 PDM_DRVREG_VERSION,
5493 /* szName */
5494 "MainDisplay",
5495 /* szRCMod */
5496 "",
5497 /* szR0Mod */
5498 "",
5499 /* pszDescription */
5500 "Main display driver (Main as in the API).",
5501 /* fFlags */
5502 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
5503 /* fClass. */
5504 PDM_DRVREG_CLASS_DISPLAY,
5505 /* cMaxInstances */
5506 ~0U,
5507 /* cbInstance */
5508 sizeof(DRVMAINDISPLAY),
5509 /* pfnConstruct */
5510 Display::drvConstruct,
5511 /* pfnDestruct */
5512 Display::drvDestruct,
5513 /* pfnRelocate */
5514 NULL,
5515 /* pfnIOCtl */
5516 NULL,
5517 /* pfnPowerOn */
5518 NULL,
5519 /* pfnReset */
5520 NULL,
5521 /* pfnSuspend */
5522 NULL,
5523 /* pfnResume */
5524 NULL,
5525 /* pfnAttach */
5526 NULL,
5527 /* pfnDetach */
5528 NULL,
5529 /* pfnPowerOff */
5530 NULL,
5531 /* pfnSoftReset */
5532 NULL,
5533 /* u32EndVersion */
5534 PDM_DRVREG_VERSION
5535};
5536/* 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