VirtualBox

source: vbox/trunk/src/VBox/Main/DisplayImpl.cpp@ 35135

Last change on this file since 35135 was 35047, checked in by vboxsync, 14 years ago

Main: do not assume that the origin of the first monitor is zero

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

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