VirtualBox

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

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

VPX: nits

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