VirtualBox

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

Last change on this file since 46075 was 46075, checked in by vboxsync, 12 years ago

Main/DisplayImpl: 6711: Code fix for SMP and code improvement.

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