VirtualBox

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

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

DisplayImpl: update attached framebuffer.

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