VirtualBox

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

Last change on this file since 35242 was 35211, checked in by vboxsync, 14 years ago

Main/Display: proper pointer position when a guest screen is disabled

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