VirtualBox

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

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

Main/Display, VBoxTray: removed obsolete code.

  • 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 35304 2010-12-22 15:43:32Z 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.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "DisplayImpl.h"
19#include "DisplayUtils.h"
20#include "ConsoleImpl.h"
21#include "ConsoleVRDPServer.h"
22#include "VMMDev.h"
23
24#include "AutoCaller.h"
25#include "Logging.h"
26
27/* generated header */
28#include "VBoxEvents.h"
29
30#include <iprt/semaphore.h>
31#include <iprt/thread.h>
32#include <iprt/asm.h>
33#include <iprt/cpp/utils.h>
34
35#include <VBox/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#if defined(VBOX_WITH_CROGL) || defined(VBOX_WITH_CRHGSMI)
45# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
46#endif
47
48#include <VBox/com/array.h>
49
50/**
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
979static bool displayIntersectRect(RTRECT *prectResult,
980 const RTRECT *prect1,
981 const RTRECT *prect2)
982{
983 /* Initialize result to an empty record. */
984 memset (prectResult, 0, sizeof (RTRECT));
985
986 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
987 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
988
989 if (xLeftResult < xRightResult)
990 {
991 /* There is intersection by X. */
992
993 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
994 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
995
996 if (yTopResult < yBottomResult)
997 {
998 /* There is intersection by Y. */
999
1000 prectResult->xLeft = xLeftResult;
1001 prectResult->yTop = yTopResult;
1002 prectResult->xRight = xRightResult;
1003 prectResult->yBottom = yBottomResult;
1004
1005 return true;
1006 }
1007 }
1008
1009 return false;
1010}
1011
1012int Display::handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1013{
1014 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc(cRect * sizeof (RTRECT));
1015 if (!pVisibleRegion)
1016 {
1017 return VERR_NO_TMP_MEMORY;
1018 }
1019
1020 unsigned uScreenId;
1021 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1022 {
1023 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1024
1025 if (!pFBInfo->pFramebuffer.isNull())
1026 {
1027 /* Prepare a new array of rectangles which intersect with the framebuffer.
1028 */
1029 RTRECT rectFramebuffer;
1030 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1031 {
1032 rectFramebuffer.xLeft = 0;
1033 rectFramebuffer.yTop = 0;
1034 if (mpDrv)
1035 {
1036 rectFramebuffer.xRight = mpDrv->IConnector.cx;
1037 rectFramebuffer.yBottom = mpDrv->IConnector.cy;
1038 }
1039 else
1040 {
1041 rectFramebuffer.xRight = 0;
1042 rectFramebuffer.yBottom = 0;
1043 }
1044 }
1045 else
1046 {
1047 rectFramebuffer.xLeft = pFBInfo->xOrigin;
1048 rectFramebuffer.yTop = pFBInfo->yOrigin;
1049 rectFramebuffer.xRight = pFBInfo->xOrigin + pFBInfo->w;
1050 rectFramebuffer.yBottom = pFBInfo->yOrigin + pFBInfo->h;
1051 }
1052
1053 uint32_t cRectVisibleRegion = 0;
1054
1055 uint32_t i;
1056 for (i = 0; i < cRect; i++)
1057 {
1058 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1059 {
1060 pVisibleRegion[cRectVisibleRegion].xLeft -= pFBInfo->xOrigin;
1061 pVisibleRegion[cRectVisibleRegion].yTop -= pFBInfo->yOrigin;
1062 pVisibleRegion[cRectVisibleRegion].xRight -= pFBInfo->xOrigin;
1063 pVisibleRegion[cRectVisibleRegion].yBottom -= pFBInfo->yOrigin;
1064
1065 cRectVisibleRegion++;
1066 }
1067 }
1068
1069 if (cRectVisibleRegion > 0)
1070 {
1071 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1072 }
1073 }
1074 }
1075
1076#if defined(RT_OS_DARWIN) && defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
1077 // @todo fix for multimonitor
1078 BOOL is3denabled = FALSE;
1079
1080 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
1081
1082 VMMDev *vmmDev = mParent->getVMMDev();
1083 if (is3denabled && vmmDev)
1084 {
1085 VBOXHGCMSVCPARM parms[2];
1086
1087 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
1088 parms[0].u.pointer.addr = pRect;
1089 parms[0].u.pointer.size = 0; /* We don't actually care. */
1090 parms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
1091 parms[1].u.uint32 = cRect;
1092
1093 vmmDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_VISIBLE_REGION, 2, &parms[0]);
1094 }
1095#endif
1096
1097 RTMemTmpFree(pVisibleRegion);
1098
1099 return VINF_SUCCESS;
1100}
1101
1102int Display::handleQueryVisibleRegion(uint32_t *pcRect, PRTRECT pRect)
1103{
1104 // @todo Currently not used by the guest and is not implemented in framebuffers. Remove?
1105 return VERR_NOT_SUPPORTED;
1106}
1107
1108typedef struct _VBVADIRTYREGION
1109{
1110 /* Copies of object's pointers used by vbvaRgn functions. */
1111 DISPLAYFBINFO *paFramebuffers;
1112 unsigned cMonitors;
1113 Display *pDisplay;
1114 PPDMIDISPLAYPORT pPort;
1115
1116} VBVADIRTYREGION;
1117
1118static void vbvaRgnInit (VBVADIRTYREGION *prgn, DISPLAYFBINFO *paFramebuffers, unsigned cMonitors, Display *pd, PPDMIDISPLAYPORT pp)
1119{
1120 prgn->paFramebuffers = paFramebuffers;
1121 prgn->cMonitors = cMonitors;
1122 prgn->pDisplay = pd;
1123 prgn->pPort = pp;
1124
1125 unsigned uScreenId;
1126 for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
1127 {
1128 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1129
1130 memset (&pFBInfo->dirtyRect, 0, sizeof (pFBInfo->dirtyRect));
1131 }
1132}
1133
1134static void vbvaRgnDirtyRect (VBVADIRTYREGION *prgn, unsigned uScreenId, VBVACMDHDR *phdr)
1135{
1136 LogSunlover (("x = %d, y = %d, w = %d, h = %d\n",
1137 phdr->x, phdr->y, phdr->w, phdr->h));
1138
1139 /*
1140 * Here update rectangles are accumulated to form an update area.
1141 * @todo
1142 * Now the simplest method is used which builds one rectangle that
1143 * includes all update areas. A bit more advanced method can be
1144 * employed here. The method should be fast however.
1145 */
1146 if (phdr->w == 0 || phdr->h == 0)
1147 {
1148 /* Empty rectangle. */
1149 return;
1150 }
1151
1152 int32_t xRight = phdr->x + phdr->w;
1153 int32_t yBottom = phdr->y + phdr->h;
1154
1155 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1156
1157 if (pFBInfo->dirtyRect.xRight == 0)
1158 {
1159 /* This is the first rectangle to be added. */
1160 pFBInfo->dirtyRect.xLeft = phdr->x;
1161 pFBInfo->dirtyRect.yTop = phdr->y;
1162 pFBInfo->dirtyRect.xRight = xRight;
1163 pFBInfo->dirtyRect.yBottom = yBottom;
1164 }
1165 else
1166 {
1167 /* Adjust region coordinates. */
1168 if (pFBInfo->dirtyRect.xLeft > phdr->x)
1169 {
1170 pFBInfo->dirtyRect.xLeft = phdr->x;
1171 }
1172
1173 if (pFBInfo->dirtyRect.yTop > phdr->y)
1174 {
1175 pFBInfo->dirtyRect.yTop = phdr->y;
1176 }
1177
1178 if (pFBInfo->dirtyRect.xRight < xRight)
1179 {
1180 pFBInfo->dirtyRect.xRight = xRight;
1181 }
1182
1183 if (pFBInfo->dirtyRect.yBottom < yBottom)
1184 {
1185 pFBInfo->dirtyRect.yBottom = yBottom;
1186 }
1187 }
1188
1189 if (pFBInfo->fDefaultFormat)
1190 {
1191 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
1192 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, phdr->x, phdr->y, phdr->w, phdr->h);
1193 prgn->pDisplay->handleDisplayUpdateLegacy (phdr->x + pFBInfo->xOrigin,
1194 phdr->y + pFBInfo->yOrigin, phdr->w, phdr->h);
1195 }
1196
1197 return;
1198}
1199
1200static void vbvaRgnUpdateFramebuffer (VBVADIRTYREGION *prgn, unsigned uScreenId)
1201{
1202 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1203
1204 uint32_t w = pFBInfo->dirtyRect.xRight - pFBInfo->dirtyRect.xLeft;
1205 uint32_t h = pFBInfo->dirtyRect.yBottom - pFBInfo->dirtyRect.yTop;
1206
1207 if (!pFBInfo->fDefaultFormat && pFBInfo->pFramebuffer && w != 0 && h != 0)
1208 {
1209 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
1210 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
1211 prgn->pDisplay->handleDisplayUpdateLegacy (pFBInfo->dirtyRect.xLeft + pFBInfo->xOrigin,
1212 pFBInfo->dirtyRect.yTop + pFBInfo->yOrigin, w, h);
1213 }
1214}
1215
1216static void vbvaSetMemoryFlags (VBVAMEMORY *pVbvaMemory,
1217 bool fVideoAccelEnabled,
1218 bool fVideoAccelVRDP,
1219 uint32_t fu32SupportedOrders,
1220 DISPLAYFBINFO *paFBInfos,
1221 unsigned cFBInfos)
1222{
1223 if (pVbvaMemory)
1224 {
1225 /* This called only on changes in mode. So reset VRDP always. */
1226 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
1227
1228 if (fVideoAccelEnabled)
1229 {
1230 fu32Flags |= VBVA_F_MODE_ENABLED;
1231
1232 if (fVideoAccelVRDP)
1233 {
1234 fu32Flags |= VBVA_F_MODE_VRDP | VBVA_F_MODE_VRDP_ORDER_MASK;
1235
1236 pVbvaMemory->fu32SupportedOrders = fu32SupportedOrders;
1237 }
1238 }
1239
1240 pVbvaMemory->fu32ModeFlags = fu32Flags;
1241 }
1242
1243 unsigned uScreenId;
1244 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1245 {
1246 if (paFBInfos[uScreenId].pHostEvents)
1247 {
1248 paFBInfos[uScreenId].pHostEvents->fu32Events |= VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1249 }
1250 }
1251}
1252
1253#ifdef VBOX_WITH_HGSMI
1254static void vbvaSetMemoryFlagsHGSMI (unsigned uScreenId,
1255 uint32_t fu32SupportedOrders,
1256 bool fVideoAccelVRDP,
1257 DISPLAYFBINFO *pFBInfo)
1258{
1259 LogFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1260
1261 if (pFBInfo->pVBVAHostFlags)
1262 {
1263 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1264
1265 if (pFBInfo->fVBVAEnabled)
1266 {
1267 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1268
1269 if (fVideoAccelVRDP)
1270 {
1271 fu32HostEvents |= VBVA_F_MODE_VRDP;
1272 }
1273 }
1274
1275 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1276 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1277
1278 LogFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1279 }
1280}
1281
1282static void vbvaSetMemoryFlagsAllHGSMI (uint32_t fu32SupportedOrders,
1283 bool fVideoAccelVRDP,
1284 DISPLAYFBINFO *paFBInfos,
1285 unsigned cFBInfos)
1286{
1287 unsigned uScreenId;
1288
1289 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1290 {
1291 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1292 }
1293}
1294#endif /* VBOX_WITH_HGSMI */
1295
1296bool Display::VideoAccelAllowed (void)
1297{
1298 return true;
1299}
1300
1301#ifdef VBOX_WITH_OLD_VBVA_LOCK
1302int Display::vbvaLock(void)
1303{
1304 return RTCritSectEnter(&mVBVALock);
1305}
1306
1307void Display::vbvaUnlock(void)
1308{
1309 RTCritSectLeave(&mVBVALock);
1310}
1311#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1312
1313/**
1314 * @thread EMT
1315 */
1316#ifdef VBOX_WITH_OLD_VBVA_LOCK
1317int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1318{
1319 int rc;
1320 vbvaLock();
1321 rc = videoAccelEnable (fEnable, pVbvaMemory);
1322 vbvaUnlock();
1323 return rc;
1324}
1325#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1326
1327#ifdef VBOX_WITH_OLD_VBVA_LOCK
1328int Display::videoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1329#else
1330int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1331#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1332{
1333 int rc = VINF_SUCCESS;
1334
1335 /* Called each time the guest wants to use acceleration,
1336 * or when the VGA device disables acceleration,
1337 * or when restoring the saved state with accel enabled.
1338 *
1339 * VGA device disables acceleration on each video mode change
1340 * and on reset.
1341 *
1342 * Guest enabled acceleration at will. And it has to enable
1343 * acceleration after a mode change.
1344 */
1345 LogFlowFunc (("mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
1346 mfVideoAccelEnabled, fEnable, pVbvaMemory));
1347
1348 /* Strictly check parameters. Callers must not pass anything in the case. */
1349 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
1350
1351 if (!VideoAccelAllowed ())
1352 return VERR_NOT_SUPPORTED;
1353
1354 /*
1355 * Verify that the VM is in running state. If it is not,
1356 * then this must be postponed until it goes to running.
1357 */
1358 if (!mfMachineRunning)
1359 {
1360 Assert (!mfVideoAccelEnabled);
1361
1362 LogFlowFunc (("Machine is not yet running.\n"));
1363
1364 if (fEnable)
1365 {
1366 mfPendingVideoAccelEnable = fEnable;
1367 mpPendingVbvaMemory = pVbvaMemory;
1368 }
1369
1370 return rc;
1371 }
1372
1373 /* Check that current status is not being changed */
1374 if (mfVideoAccelEnabled == fEnable)
1375 return rc;
1376
1377 if (mfVideoAccelEnabled)
1378 {
1379 /* Process any pending orders and empty the VBVA ring buffer. */
1380#ifdef VBOX_WITH_OLD_VBVA_LOCK
1381 videoAccelFlush ();
1382#else
1383 VideoAccelFlush ();
1384#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1385 }
1386
1387 if (!fEnable && mpVbvaMemory)
1388 mpVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
1389
1390 /* Safety precaution. There is no more VBVA until everything is setup! */
1391 mpVbvaMemory = NULL;
1392 mfVideoAccelEnabled = false;
1393
1394 /* Update entire display. */
1395 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].u32ResizeStatus == ResizeStatus_Void)
1396 mpDrv->pUpPort->pfnUpdateDisplayAll(mpDrv->pUpPort);
1397
1398 /* Everything OK. VBVA status can be changed. */
1399
1400 /* Notify the VMMDev, which saves VBVA status in the saved state,
1401 * and needs to know current status.
1402 */
1403 VMMDev *pVMMDev = mParent->getVMMDev();
1404 if (pVMMDev)
1405 {
1406 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1407 if (pVMMDevPort)
1408 pVMMDevPort->pfnVBVAChange(pVMMDevPort, fEnable);
1409 }
1410
1411 if (fEnable)
1412 {
1413 mpVbvaMemory = pVbvaMemory;
1414 mfVideoAccelEnabled = true;
1415
1416 /* Initialize the hardware memory. */
1417 vbvaSetMemoryFlags(mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
1418 mpVbvaMemory->off32Data = 0;
1419 mpVbvaMemory->off32Free = 0;
1420
1421 memset(mpVbvaMemory->aRecords, 0, sizeof (mpVbvaMemory->aRecords));
1422 mpVbvaMemory->indexRecordFirst = 0;
1423 mpVbvaMemory->indexRecordFree = 0;
1424
1425#ifdef VBOX_WITH_OLD_VBVA_LOCK
1426 mfu32PendingVideoAccelDisable = false;
1427#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1428
1429 LogRel(("VBVA: Enabled.\n"));
1430 }
1431 else
1432 {
1433 LogRel(("VBVA: Disabled.\n"));
1434 }
1435
1436 LogFlowFunc (("VideoAccelEnable: rc = %Rrc.\n", rc));
1437
1438 return rc;
1439}
1440
1441/* Called always by one VRDP server thread. Can be thread-unsafe.
1442 */
1443void Display::VideoAccelVRDP (bool fEnable)
1444{
1445 LogFlowFunc(("fEnable = %d\n", fEnable));
1446
1447#ifdef VBOX_WITH_OLD_VBVA_LOCK
1448 vbvaLock();
1449#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1450
1451 int c = fEnable?
1452 ASMAtomicIncS32 (&mcVideoAccelVRDPRefs):
1453 ASMAtomicDecS32 (&mcVideoAccelVRDPRefs);
1454
1455 Assert (c >= 0);
1456
1457 if (c == 0)
1458 {
1459 /* The last client has disconnected, and the accel can be
1460 * disabled.
1461 */
1462 Assert (fEnable == false);
1463
1464 mfVideoAccelVRDP = false;
1465 mfu32SupportedOrders = 0;
1466
1467 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
1468#ifdef VBOX_WITH_HGSMI
1469 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1470 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1471#endif /* VBOX_WITH_HGSMI */
1472
1473 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1474 }
1475 else if ( c == 1
1476 && !mfVideoAccelVRDP)
1477 {
1478 /* The first client has connected. Enable the accel.
1479 */
1480 Assert (fEnable == true);
1481
1482 mfVideoAccelVRDP = true;
1483 /* Supporting all orders. */
1484 mfu32SupportedOrders = ~0;
1485
1486 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
1487#ifdef VBOX_WITH_HGSMI
1488 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1489 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1490#endif /* VBOX_WITH_HGSMI */
1491
1492 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1493 }
1494 else
1495 {
1496 /* A client is connected or disconnected but there is no change in the
1497 * accel state. It remains enabled.
1498 */
1499 Assert (mfVideoAccelVRDP == true);
1500 }
1501#ifdef VBOX_WITH_OLD_VBVA_LOCK
1502 vbvaUnlock();
1503#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1504}
1505
1506static bool vbvaVerifyRingBuffer (VBVAMEMORY *pVbvaMemory)
1507{
1508 return true;
1509}
1510
1511static void vbvaFetchBytes (VBVAMEMORY *pVbvaMemory, uint8_t *pu8Dst, uint32_t cbDst)
1512{
1513 if (cbDst >= VBVA_RING_BUFFER_SIZE)
1514 {
1515 AssertMsgFailed (("cbDst = 0x%08X, ring buffer size 0x%08X", cbDst, VBVA_RING_BUFFER_SIZE));
1516 return;
1517 }
1518
1519 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
1520 uint8_t *src = &pVbvaMemory->au8RingBuffer[pVbvaMemory->off32Data];
1521 int32_t i32Diff = cbDst - u32BytesTillBoundary;
1522
1523 if (i32Diff <= 0)
1524 {
1525 /* Chunk will not cross buffer boundary. */
1526 memcpy (pu8Dst, src, cbDst);
1527 }
1528 else
1529 {
1530 /* Chunk crosses buffer boundary. */
1531 memcpy (pu8Dst, src, u32BytesTillBoundary);
1532 memcpy (pu8Dst + u32BytesTillBoundary, &pVbvaMemory->au8RingBuffer[0], i32Diff);
1533 }
1534
1535 /* Advance data offset. */
1536 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbDst) % VBVA_RING_BUFFER_SIZE;
1537
1538 return;
1539}
1540
1541
1542static bool vbvaPartialRead (uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
1543{
1544 uint8_t *pu8New;
1545
1546 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
1547 *ppu8, *pcb, cbRecord));
1548
1549 if (*ppu8)
1550 {
1551 Assert (*pcb);
1552 pu8New = (uint8_t *)RTMemRealloc (*ppu8, cbRecord);
1553 }
1554 else
1555 {
1556 Assert (!*pcb);
1557 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
1558 }
1559
1560 if (!pu8New)
1561 {
1562 /* Memory allocation failed, fail the function. */
1563 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
1564 cbRecord));
1565
1566 if (*ppu8)
1567 {
1568 RTMemFree (*ppu8);
1569 }
1570
1571 *ppu8 = NULL;
1572 *pcb = 0;
1573
1574 return false;
1575 }
1576
1577 /* Fetch data from the ring buffer. */
1578 vbvaFetchBytes (pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
1579
1580 *ppu8 = pu8New;
1581 *pcb = cbRecord;
1582
1583 return true;
1584}
1585
1586/* For contiguous chunks just return the address in the buffer.
1587 * For crossing boundary - allocate a buffer from heap.
1588 */
1589bool Display::vbvaFetchCmd (VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
1590{
1591 uint32_t indexRecordFirst = mpVbvaMemory->indexRecordFirst;
1592 uint32_t indexRecordFree = mpVbvaMemory->indexRecordFree;
1593
1594#ifdef DEBUG_sunlover
1595 LogFlowFunc (("first = %d, free = %d\n",
1596 indexRecordFirst, indexRecordFree));
1597#endif /* DEBUG_sunlover */
1598
1599 if (!vbvaVerifyRingBuffer (mpVbvaMemory))
1600 {
1601 return false;
1602 }
1603
1604 if (indexRecordFirst == indexRecordFree)
1605 {
1606 /* No records to process. Return without assigning output variables. */
1607 return true;
1608 }
1609
1610 VBVARECORD *pRecord = &mpVbvaMemory->aRecords[indexRecordFirst];
1611
1612#ifdef DEBUG_sunlover
1613 LogFlowFunc (("cbRecord = 0x%08X\n", pRecord->cbRecord));
1614#endif /* DEBUG_sunlover */
1615
1616 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
1617
1618 if (mcbVbvaPartial)
1619 {
1620 /* There is a partial read in process. Continue with it. */
1621
1622 Assert (mpu8VbvaPartial);
1623
1624 LogFlowFunc (("continue partial record mcbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
1625 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
1626
1627 if (cbRecord > mcbVbvaPartial)
1628 {
1629 /* New data has been added to the record. */
1630 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
1631 {
1632 return false;
1633 }
1634 }
1635
1636 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
1637 {
1638 /* The record is completed by guest. Return it to the caller. */
1639 *ppHdr = (VBVACMDHDR *)mpu8VbvaPartial;
1640 *pcbCmd = mcbVbvaPartial;
1641
1642 mpu8VbvaPartial = NULL;
1643 mcbVbvaPartial = 0;
1644
1645 /* Advance the record index. */
1646 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
1647
1648#ifdef DEBUG_sunlover
1649 LogFlowFunc (("partial done ok, data = %d, free = %d\n",
1650 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1651#endif /* DEBUG_sunlover */
1652 }
1653
1654 return true;
1655 }
1656
1657 /* A new record need to be processed. */
1658 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
1659 {
1660 /* Current record is being written by guest. '=' is important here. */
1661 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
1662 {
1663 /* Partial read must be started. */
1664 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
1665 {
1666 return false;
1667 }
1668
1669 LogFlowFunc (("started partial record mcbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
1670 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
1671 }
1672
1673 return true;
1674 }
1675
1676 /* Current record is complete. If it is not empty, process it. */
1677 if (cbRecord)
1678 {
1679 /* The size of largest contiguous chunk in the ring biffer. */
1680 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - mpVbvaMemory->off32Data;
1681
1682 /* The ring buffer pointer. */
1683 uint8_t *au8RingBuffer = &mpVbvaMemory->au8RingBuffer[0];
1684
1685 /* The pointer to data in the ring buffer. */
1686 uint8_t *src = &au8RingBuffer[mpVbvaMemory->off32Data];
1687
1688 /* Fetch or point the data. */
1689 if (u32BytesTillBoundary >= cbRecord)
1690 {
1691 /* The command does not cross buffer boundary. Return address in the buffer. */
1692 *ppHdr = (VBVACMDHDR *)src;
1693
1694 /* Advance data offset. */
1695 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1696 }
1697 else
1698 {
1699 /* The command crosses buffer boundary. Rare case, so not optimized. */
1700 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
1701
1702 if (!dst)
1703 {
1704 LogFlowFunc (("could not allocate %d bytes from heap!!!\n", cbRecord));
1705 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1706 return false;
1707 }
1708
1709 vbvaFetchBytes (mpVbvaMemory, dst, cbRecord);
1710
1711 *ppHdr = (VBVACMDHDR *)dst;
1712
1713#ifdef DEBUG_sunlover
1714 LogFlowFunc (("Allocated from heap %p\n", dst));
1715#endif /* DEBUG_sunlover */
1716 }
1717 }
1718
1719 *pcbCmd = cbRecord;
1720
1721 /* Advance the record index. */
1722 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
1723
1724#ifdef DEBUG_sunlover
1725 LogFlowFunc (("done ok, data = %d, free = %d\n",
1726 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1727#endif /* DEBUG_sunlover */
1728
1729 return true;
1730}
1731
1732void Display::vbvaReleaseCmd (VBVACMDHDR *pHdr, int32_t cbCmd)
1733{
1734 uint8_t *au8RingBuffer = mpVbvaMemory->au8RingBuffer;
1735
1736 if ( (uint8_t *)pHdr >= au8RingBuffer
1737 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
1738 {
1739 /* The pointer is inside ring buffer. Must be continuous chunk. */
1740 Assert (VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
1741
1742 /* Do nothing. */
1743
1744 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1745 }
1746 else
1747 {
1748 /* The pointer is outside. It is then an allocated copy. */
1749
1750#ifdef DEBUG_sunlover
1751 LogFlowFunc (("Free heap %p\n", pHdr));
1752#endif /* DEBUG_sunlover */
1753
1754 if ((uint8_t *)pHdr == mpu8VbvaPartial)
1755 {
1756 mpu8VbvaPartial = NULL;
1757 mcbVbvaPartial = 0;
1758 }
1759 else
1760 {
1761 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1762 }
1763
1764 RTMemFree (pHdr);
1765 }
1766
1767 return;
1768}
1769
1770
1771/**
1772 * Called regularly on the DisplayRefresh timer.
1773 * Also on behalf of guest, when the ring buffer is full.
1774 *
1775 * @thread EMT
1776 */
1777#ifdef VBOX_WITH_OLD_VBVA_LOCK
1778void Display::VideoAccelFlush (void)
1779{
1780 vbvaLock();
1781 videoAccelFlush();
1782 vbvaUnlock();
1783}
1784#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1785
1786#ifdef VBOX_WITH_OLD_VBVA_LOCK
1787/* Under VBVA lock. DevVGA is not taken. */
1788void Display::videoAccelFlush (void)
1789#else
1790void Display::VideoAccelFlush (void)
1791#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1792{
1793#ifdef DEBUG_sunlover_2
1794 LogFlowFunc (("mfVideoAccelEnabled = %d\n", mfVideoAccelEnabled));
1795#endif /* DEBUG_sunlover_2 */
1796
1797 if (!mfVideoAccelEnabled)
1798 {
1799 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
1800 return;
1801 }
1802
1803 /* Here VBVA is enabled and we have the accelerator memory pointer. */
1804 Assert(mpVbvaMemory);
1805
1806#ifdef DEBUG_sunlover_2
1807 LogFlowFunc (("indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
1808 mpVbvaMemory->indexRecordFirst, mpVbvaMemory->indexRecordFree, mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1809#endif /* DEBUG_sunlover_2 */
1810
1811 /* Quick check for "nothing to update" case. */
1812 if (mpVbvaMemory->indexRecordFirst == mpVbvaMemory->indexRecordFree)
1813 {
1814 return;
1815 }
1816
1817 /* Process the ring buffer */
1818 unsigned uScreenId;
1819#ifndef VBOX_WITH_OLD_VBVA_LOCK
1820 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1821 {
1822 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1823 {
1824 maFramebuffers[uScreenId].pFramebuffer->Lock ();
1825 }
1826 }
1827#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1828
1829 /* Initialize dirty rectangles accumulator. */
1830 VBVADIRTYREGION rgn;
1831 vbvaRgnInit (&rgn, maFramebuffers, mcMonitors, this, mpDrv->pUpPort);
1832
1833 for (;;)
1834 {
1835 VBVACMDHDR *phdr = NULL;
1836 uint32_t cbCmd = ~0;
1837
1838 /* Fetch the command data. */
1839 if (!vbvaFetchCmd (&phdr, &cbCmd))
1840 {
1841 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
1842 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1843
1844 /* Disable VBVA on those processing errors. */
1845#ifdef VBOX_WITH_OLD_VBVA_LOCK
1846 videoAccelEnable (false, NULL);
1847#else
1848 VideoAccelEnable (false, NULL);
1849#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1850
1851 break;
1852 }
1853
1854 if (cbCmd == uint32_t(~0))
1855 {
1856 /* No more commands yet in the queue. */
1857 break;
1858 }
1859
1860 if (cbCmd != 0)
1861 {
1862#ifdef DEBUG_sunlover
1863 LogFlowFunc (("hdr: cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
1864 cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
1865#endif /* DEBUG_sunlover */
1866
1867 VBVACMDHDR hdrSaved = *phdr;
1868
1869 int x = phdr->x;
1870 int y = phdr->y;
1871 int w = phdr->w;
1872 int h = phdr->h;
1873
1874 uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
1875
1876 phdr->x = (int16_t)x;
1877 phdr->y = (int16_t)y;
1878 phdr->w = (uint16_t)w;
1879 phdr->h = (uint16_t)h;
1880
1881 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1882
1883 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
1884 {
1885 /* Handle the command.
1886 *
1887 * Guest is responsible for updating the guest video memory.
1888 * The Windows guest does all drawing using Eng*.
1889 *
1890 * For local output, only dirty rectangle information is used
1891 * to update changed areas.
1892 *
1893 * Dirty rectangles are accumulated to exclude overlapping updates and
1894 * group small updates to a larger one.
1895 */
1896
1897 /* Accumulate the update. */
1898 vbvaRgnDirtyRect (&rgn, uScreenId, phdr);
1899
1900 /* Forward the command to VRDP server. */
1901 mParent->consoleVRDPServer()->SendUpdate (uScreenId, phdr, cbCmd);
1902
1903 *phdr = hdrSaved;
1904 }
1905 }
1906
1907 vbvaReleaseCmd (phdr, cbCmd);
1908 }
1909
1910 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1911 {
1912#ifndef VBOX_WITH_OLD_VBVA_LOCK
1913 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1914 {
1915 maFramebuffers[uScreenId].pFramebuffer->Unlock ();
1916 }
1917#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1918
1919 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
1920 {
1921 /* Draw the framebuffer. */
1922 vbvaRgnUpdateFramebuffer (&rgn, uScreenId);
1923 }
1924 }
1925}
1926
1927#ifdef VBOX_WITH_OLD_VBVA_LOCK
1928int Display::videoAccelRefreshProcess(void)
1929{
1930 int rc = VWRN_INVALID_STATE; /* Default is to do a display update in VGA device. */
1931
1932 vbvaLock();
1933
1934 if (ASMAtomicCmpXchgU32(&mfu32PendingVideoAccelDisable, false, true))
1935 {
1936 videoAccelEnable (false, NULL);
1937 }
1938 else if (mfPendingVideoAccelEnable)
1939 {
1940 /* Acceleration was enabled while machine was not yet running
1941 * due to restoring from saved state. Update entire display and
1942 * actually enable acceleration.
1943 */
1944 Assert(mpPendingVbvaMemory);
1945
1946 /* Acceleration can not be yet enabled.*/
1947 Assert(mpVbvaMemory == NULL);
1948 Assert(!mfVideoAccelEnabled);
1949
1950 if (mfMachineRunning)
1951 {
1952 videoAccelEnable (mfPendingVideoAccelEnable,
1953 mpPendingVbvaMemory);
1954
1955 /* Reset the pending state. */
1956 mfPendingVideoAccelEnable = false;
1957 mpPendingVbvaMemory = NULL;
1958 }
1959
1960 rc = VINF_TRY_AGAIN;
1961 }
1962 else
1963 {
1964 Assert(mpPendingVbvaMemory == NULL);
1965
1966 if (mfVideoAccelEnabled)
1967 {
1968 Assert(mpVbvaMemory);
1969 videoAccelFlush ();
1970
1971 rc = VINF_SUCCESS; /* VBVA processed, no need to a display update. */
1972 }
1973 }
1974
1975 vbvaUnlock();
1976
1977 return rc;
1978}
1979#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1980
1981
1982// IDisplay methods
1983/////////////////////////////////////////////////////////////////////////////
1984STDMETHODIMP Display::GetScreenResolution (ULONG aScreenId,
1985 ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel)
1986{
1987 LogFlowFunc (("aScreenId = %d\n", aScreenId));
1988
1989 AutoCaller autoCaller(this);
1990 if (FAILED(autoCaller.rc())) return autoCaller.rc();
1991
1992 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1993
1994 uint32_t u32Width = 0;
1995 uint32_t u32Height = 0;
1996 uint32_t u32BitsPerPixel = 0;
1997
1998 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1999 {
2000 CHECK_CONSOLE_DRV (mpDrv);
2001
2002 u32Width = mpDrv->IConnector.cx;
2003 u32Height = mpDrv->IConnector.cy;
2004 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &u32BitsPerPixel);
2005 AssertRC(rc);
2006 }
2007 else if (aScreenId < mcMonitors)
2008 {
2009 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2010 u32Width = pFBInfo->w;
2011 u32Height = pFBInfo->h;
2012 u32BitsPerPixel = pFBInfo->u16BitsPerPixel;
2013 }
2014 else
2015 {
2016 return E_INVALIDARG;
2017 }
2018
2019 if (aWidth)
2020 *aWidth = u32Width;
2021 if (aHeight)
2022 *aHeight = u32Height;
2023 if (aBitsPerPixel)
2024 *aBitsPerPixel = u32BitsPerPixel;
2025
2026 return S_OK;
2027}
2028
2029STDMETHODIMP Display::SetFramebuffer (ULONG aScreenId,
2030 IFramebuffer *aFramebuffer)
2031{
2032 LogFlowFunc (("\n"));
2033
2034 if (aFramebuffer != NULL)
2035 CheckComArgOutPointerValid(aFramebuffer);
2036
2037 AutoCaller autoCaller(this);
2038 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2039
2040 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2041
2042 Console::SafeVMPtrQuiet pVM (mParent);
2043 if (pVM.isOk())
2044 {
2045 /* Must leave the lock here because the changeFramebuffer will
2046 * also obtain it. */
2047 alock.leave ();
2048
2049 /* send request to the EMT thread */
2050 int vrc = VMR3ReqCallWait (pVM, VMCPUID_ANY,
2051 (PFNRT) changeFramebuffer, 3, this, aFramebuffer, aScreenId);
2052
2053 alock.enter ();
2054
2055 ComAssertRCRet (vrc, E_FAIL);
2056
2057#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2058 {
2059 BOOL is3denabled;
2060 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2061
2062 if (is3denabled)
2063 {
2064 VBOXHGCMSVCPARM parm;
2065
2066 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
2067 parm.u.uint32 = aScreenId;
2068
2069 VMMDev *pVMMDev = mParent->getVMMDev();
2070
2071 alock.leave ();
2072
2073 if (pVMMDev)
2074 vrc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED, SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
2075 /*ComAssertRCRet (vrc, E_FAIL);*/
2076
2077 alock.enter ();
2078 }
2079 }
2080#endif /* VBOX_WITH_CROGL */
2081 }
2082 else
2083 {
2084 /* No VM is created (VM is powered off), do a direct call */
2085 int vrc = changeFramebuffer (this, aFramebuffer, aScreenId);
2086 ComAssertRCRet (vrc, E_FAIL);
2087 }
2088
2089 return S_OK;
2090}
2091
2092STDMETHODIMP Display::GetFramebuffer (ULONG aScreenId,
2093 IFramebuffer **aFramebuffer, LONG *aXOrigin, LONG *aYOrigin)
2094{
2095 LogFlowFunc (("aScreenId = %d\n", aScreenId));
2096
2097 CheckComArgOutPointerValid(aFramebuffer);
2098
2099 AutoCaller autoCaller(this);
2100 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2101
2102 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2103
2104 if (aScreenId != 0 && aScreenId >= mcMonitors)
2105 return E_INVALIDARG;
2106
2107 /* @todo this should be actually done on EMT. */
2108 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2109
2110 *aFramebuffer = pFBInfo->pFramebuffer;
2111 if (*aFramebuffer)
2112 (*aFramebuffer)->AddRef ();
2113 if (aXOrigin)
2114 *aXOrigin = pFBInfo->xOrigin;
2115 if (aYOrigin)
2116 *aYOrigin = pFBInfo->yOrigin;
2117
2118 return S_OK;
2119}
2120
2121STDMETHODIMP Display::SetVideoModeHint(ULONG aWidth, ULONG aHeight,
2122 ULONG aBitsPerPixel, ULONG aDisplay)
2123{
2124 AutoCaller autoCaller(this);
2125 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2126
2127 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2128
2129 CHECK_CONSOLE_DRV (mpDrv);
2130
2131 /*
2132 * Do some rough checks for valid input
2133 */
2134 ULONG width = aWidth;
2135 if (!width)
2136 width = mpDrv->IConnector.cx;
2137 ULONG height = aHeight;
2138 if (!height)
2139 height = mpDrv->IConnector.cy;
2140 ULONG bpp = aBitsPerPixel;
2141 if (!bpp)
2142 {
2143 uint32_t cBits = 0;
2144 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
2145 AssertRC(rc);
2146 bpp = cBits;
2147 }
2148 ULONG cMonitors;
2149 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
2150 if (cMonitors == 0 && aDisplay > 0)
2151 return E_INVALIDARG;
2152 if (aDisplay >= cMonitors)
2153 return E_INVALIDARG;
2154
2155// sunlover 20070614: It is up to the guest to decide whether the hint is valid.
2156// ULONG vramSize;
2157// mParent->machine()->COMGETTER(VRAMSize)(&vramSize);
2158// /* enough VRAM? */
2159// if ((width * height * (bpp / 8)) > (vramSize * 1024 * 1024))
2160// return setError(E_FAIL, tr("Not enough VRAM for the selected video mode"));
2161
2162 /* Have to leave the lock because the pfnRequestDisplayChange
2163 * will call EMT. */
2164 alock.leave ();
2165
2166 VMMDev *pVMMDev = mParent->getVMMDev();
2167 if (pVMMDev)
2168 {
2169 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2170 if (pVMMDevPort)
2171 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel, aDisplay);
2172 }
2173 return S_OK;
2174}
2175
2176STDMETHODIMP Display::SetSeamlessMode (BOOL enabled)
2177{
2178 AutoCaller autoCaller(this);
2179 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2180
2181 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2182
2183 /* Have to leave the lock because the pfnRequestSeamlessChange will call EMT. */
2184 alock.leave ();
2185
2186 VMMDev *pVMMDev = mParent->getVMMDev();
2187 if (pVMMDev)
2188 {
2189 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2190 if (pVMMDevPort)
2191 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
2192 }
2193 return S_OK;
2194}
2195
2196#ifdef VBOX_WITH_OLD_VBVA_LOCK
2197int Display::displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppu8Data, size_t *pcbData, uint32_t *pu32Width, uint32_t *pu32Height)
2198{
2199 int rc;
2200 pDisplay->vbvaLock();
2201 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2202 {
2203 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppu8Data, pcbData, pu32Width, pu32Height);
2204 }
2205 else if (aScreenId < pDisplay->mcMonitors)
2206 {
2207 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2208
2209 uint32_t width = pFBInfo->w;
2210 uint32_t height = pFBInfo->h;
2211
2212 /* Allocate 32 bit per pixel bitmap. */
2213 size_t cbRequired = width * 4 * height;
2214
2215 if (cbRequired)
2216 {
2217 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbRequired);
2218
2219 if (pu8Data == NULL)
2220 {
2221 rc = VERR_NO_MEMORY;
2222 }
2223 else
2224 {
2225 /* Copy guest VRAM to the allocated 32bpp buffer. */
2226 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2227 int32_t xSrc = 0;
2228 int32_t ySrc = 0;
2229 uint32_t u32SrcWidth = width;
2230 uint32_t u32SrcHeight = height;
2231 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2232 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2233
2234 uint8_t *pu8Dst = pu8Data;
2235 int32_t xDst = 0;
2236 int32_t yDst = 0;
2237 uint32_t u32DstWidth = u32SrcWidth;
2238 uint32_t u32DstHeight = u32SrcHeight;
2239 uint32_t u32DstLineSize = u32DstWidth * 4;
2240 uint32_t u32DstBitsPerPixel = 32;
2241
2242 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2243 width, height,
2244 pu8Src,
2245 xSrc, ySrc,
2246 u32SrcWidth, u32SrcHeight,
2247 u32SrcLineSize, u32SrcBitsPerPixel,
2248 pu8Dst,
2249 xDst, yDst,
2250 u32DstWidth, u32DstHeight,
2251 u32DstLineSize, u32DstBitsPerPixel);
2252 if (RT_SUCCESS(rc))
2253 {
2254 *ppu8Data = pu8Data;
2255 *pcbData = cbRequired;
2256 *pu32Width = width;
2257 *pu32Height = height;
2258 }
2259 }
2260 }
2261 else
2262 {
2263 /* No image. */
2264 *ppu8Data = NULL;
2265 *pcbData = 0;
2266 *pu32Width = 0;
2267 *pu32Height = 0;
2268 rc = VINF_SUCCESS;
2269 }
2270 }
2271 else
2272 {
2273 rc = VERR_INVALID_PARAMETER;
2274 }
2275 pDisplay->vbvaUnlock();
2276 return rc;
2277}
2278#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2279
2280#ifdef VBOX_WITH_OLD_VBVA_LOCK
2281static int displayTakeScreenshot(PVM pVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId, BYTE *address, ULONG width, ULONG height)
2282#else
2283static int displayTakeScreenshot(PVM pVM, struct DRVMAINDISPLAY *pDrv, BYTE *address, ULONG width, ULONG height)
2284#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2285{
2286 uint8_t *pu8Data = NULL;
2287 size_t cbData = 0;
2288 uint32_t cx = 0;
2289 uint32_t cy = 0;
2290 int vrc = VINF_SUCCESS;
2291
2292#ifdef VBOX_WITH_OLD_VBVA_LOCK
2293 int cRetries = 5;
2294
2295 while (cRetries-- > 0)
2296 {
2297 vrc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)Display::displayTakeScreenshotEMT, 6,
2298 pDisplay, aScreenId, &pu8Data, &cbData, &cx, &cy);
2299 if (vrc != VERR_TRY_AGAIN)
2300 {
2301 break;
2302 }
2303
2304 RTThreadSleep(10);
2305 }
2306#else
2307 /* @todo pfnTakeScreenshot is probably callable from any thread, because it uses the VGA device lock. */
2308 vrc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pDrv->pUpPort->pfnTakeScreenshot, 5,
2309 pDrv->pUpPort, &pu8Data, &cbData, &cx, &cy);
2310#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2311
2312 if (RT_SUCCESS(vrc) && pu8Data)
2313 {
2314 if (cx == width && cy == height)
2315 {
2316 /* No scaling required. */
2317 memcpy(address, pu8Data, cbData);
2318 }
2319 else
2320 {
2321 /* Scale. */
2322 LogFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2323
2324 uint8_t *dst = address;
2325 uint8_t *src = pu8Data;
2326 int dstW = width;
2327 int dstH = height;
2328 int srcW = cx;
2329 int srcH = cy;
2330 int iDeltaLine = cx * 4;
2331
2332 BitmapScale32 (dst,
2333 dstW, dstH,
2334 src,
2335 iDeltaLine,
2336 srcW, srcH);
2337 }
2338
2339 /* This can be called from any thread. */
2340 pDrv->pUpPort->pfnFreeScreenshot (pDrv->pUpPort, pu8Data);
2341 }
2342
2343 return vrc;
2344}
2345
2346STDMETHODIMP Display::TakeScreenShot (ULONG aScreenId, BYTE *address, ULONG width, ULONG height)
2347{
2348 /// @todo (r=dmik) this function may take too long to complete if the VM
2349 // is doing something like saving state right now. Which, in case if it
2350 // is called on the GUI thread, will make it unresponsive. We should
2351 // check the machine state here (by enclosing the check and VMRequCall
2352 // within the Console lock to make it atomic).
2353
2354 LogFlowFuncEnter();
2355 LogFlowFunc (("address=%p, width=%d, height=%d\n",
2356 address, width, height));
2357
2358 CheckComArgNotNull(address);
2359 CheckComArgExpr(width, width != 0);
2360 CheckComArgExpr(height, height != 0);
2361
2362 AutoCaller autoCaller(this);
2363 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2364
2365 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2366
2367 CHECK_CONSOLE_DRV (mpDrv);
2368
2369 Console::SafeVMPtr pVM(mParent);
2370 if (FAILED(pVM.rc())) return pVM.rc();
2371
2372 HRESULT rc = S_OK;
2373
2374 LogFlowFunc (("Sending SCREENSHOT request\n"));
2375
2376 /* Leave lock because other thread (EMT) is called and it may initiate a resize
2377 * which also needs lock.
2378 *
2379 * This method does not need the lock anymore.
2380 */
2381 alock.leave();
2382
2383#ifdef VBOX_WITH_OLD_VBVA_LOCK
2384 int vrc = displayTakeScreenshot(pVM, this, mpDrv, aScreenId, address, width, height);
2385#else
2386 int vrc = displayTakeScreenshot(pVM, mpDrv, address, width, height);
2387#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2388
2389 if (vrc == VERR_NOT_IMPLEMENTED)
2390 rc = setError(E_NOTIMPL,
2391 tr("This feature is not implemented"));
2392 else if (vrc == VERR_TRY_AGAIN)
2393 rc = setError(E_UNEXPECTED,
2394 tr("This feature is not available at this time"));
2395 else if (RT_FAILURE(vrc))
2396 rc = setError(VBOX_E_IPRT_ERROR,
2397 tr("Could not take a screenshot (%Rrc)"), vrc);
2398
2399 LogFlowFunc (("rc=%08X\n", rc));
2400 LogFlowFuncLeave();
2401 return rc;
2402}
2403
2404STDMETHODIMP Display::TakeScreenShotToArray (ULONG aScreenId, ULONG width, ULONG height,
2405 ComSafeArrayOut(BYTE, aScreenData))
2406{
2407 LogFlowFuncEnter();
2408 LogFlowFunc (("width=%d, height=%d\n",
2409 width, height));
2410
2411 CheckComArgOutSafeArrayPointerValid(aScreenData);
2412 CheckComArgExpr(width, width != 0);
2413 CheckComArgExpr(height, height != 0);
2414
2415 AutoCaller autoCaller(this);
2416 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2417
2418 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2419
2420 CHECK_CONSOLE_DRV (mpDrv);
2421
2422 Console::SafeVMPtr pVM(mParent);
2423 if (FAILED(pVM.rc())) return pVM.rc();
2424
2425 HRESULT rc = S_OK;
2426
2427 LogFlowFunc (("Sending SCREENSHOT request\n"));
2428
2429 /* Leave lock because other thread (EMT) is called and it may initiate a resize
2430 * which also needs lock.
2431 *
2432 * This method does not need the lock anymore.
2433 */
2434 alock.leave();
2435
2436 size_t cbData = width * 4 * height;
2437 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
2438
2439 if (!pu8Data)
2440 return E_OUTOFMEMORY;
2441
2442#ifdef VBOX_WITH_OLD_VBVA_LOCK
2443 int vrc = displayTakeScreenshot(pVM, this, mpDrv, aScreenId, pu8Data, width, height);
2444#else
2445 int vrc = displayTakeScreenshot(pVM, mpDrv, pu8Data, width, height);
2446#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2447
2448 if (RT_SUCCESS(vrc))
2449 {
2450 /* Convert pixels to format expected by the API caller: [0] R, [1] G, [2] B, [3] A. */
2451 uint8_t *pu8 = pu8Data;
2452 unsigned cPixels = width * height;
2453 while (cPixels)
2454 {
2455 uint8_t u8 = pu8[0];
2456 pu8[0] = pu8[2];
2457 pu8[2] = u8;
2458 pu8[3] = 0xff;
2459 cPixels--;
2460 pu8 += 4;
2461 }
2462
2463 com::SafeArray<BYTE> screenData (cbData);
2464 screenData.initFrom(pu8Data, cbData);
2465 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
2466 }
2467 else if (vrc == VERR_NOT_IMPLEMENTED)
2468 rc = setError(E_NOTIMPL,
2469 tr("This feature is not implemented"));
2470 else
2471 rc = setError(VBOX_E_IPRT_ERROR,
2472 tr("Could not take a screenshot (%Rrc)"), vrc);
2473
2474 RTMemFree(pu8Data);
2475
2476 LogFlowFunc (("rc=%08X\n", rc));
2477 LogFlowFuncLeave();
2478 return rc;
2479}
2480
2481STDMETHODIMP Display::TakeScreenShotPNGToArray (ULONG aScreenId, ULONG width, ULONG height,
2482 ComSafeArrayOut(BYTE, aScreenData))
2483{
2484 LogFlowFuncEnter();
2485 LogFlowFunc (("width=%d, height=%d\n",
2486 width, height));
2487
2488 CheckComArgOutSafeArrayPointerValid(aScreenData);
2489 CheckComArgExpr(width, width != 0);
2490 CheckComArgExpr(height, height != 0);
2491
2492 AutoCaller autoCaller(this);
2493 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2494
2495 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2496
2497 CHECK_CONSOLE_DRV (mpDrv);
2498
2499 Console::SafeVMPtr pVM(mParent);
2500 if (FAILED(pVM.rc())) return pVM.rc();
2501
2502 HRESULT rc = S_OK;
2503
2504 LogFlowFunc (("Sending SCREENSHOT request\n"));
2505
2506 /* Leave lock because other thread (EMT) is called and it may initiate a resize
2507 * which also needs lock.
2508 *
2509 * This method does not need the lock anymore.
2510 */
2511 alock.leave();
2512
2513 size_t cbData = width * 4 * height;
2514 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
2515
2516 if (!pu8Data)
2517 return E_OUTOFMEMORY;
2518
2519#ifdef VBOX_WITH_OLD_VBVA_LOCK
2520 int vrc = displayTakeScreenshot(pVM, this, mpDrv, aScreenId, pu8Data, width, height);
2521#else
2522 int vrc = displayTakeScreenshot(pVM, mpDrv, pu8Data, width, height);
2523#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2524
2525 if (RT_SUCCESS(vrc))
2526 {
2527 uint8_t *pu8PNG = NULL;
2528 uint32_t cbPNG = 0;
2529 uint32_t cxPNG = 0;
2530 uint32_t cyPNG = 0;
2531
2532 DisplayMakePNG(pu8Data, width, height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2533
2534 com::SafeArray<BYTE> screenData (cbPNG);
2535 screenData.initFrom(pu8PNG, cbPNG);
2536 RTMemFree(pu8PNG);
2537
2538 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
2539 }
2540 else if (vrc == VERR_NOT_IMPLEMENTED)
2541 rc = setError(E_NOTIMPL,
2542 tr("This feature is not implemented"));
2543 else
2544 rc = setError(VBOX_E_IPRT_ERROR,
2545 tr("Could not take a screenshot (%Rrc)"), vrc);
2546
2547 RTMemFree(pu8Data);
2548
2549 LogFlowFunc (("rc=%08X\n", rc));
2550 LogFlowFuncLeave();
2551 return rc;
2552}
2553
2554
2555#ifdef VBOX_WITH_OLD_VBVA_LOCK
2556int Display::drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address, ULONG x, ULONG y, ULONG width, ULONG height)
2557{
2558 int rc;
2559 pDisplay->vbvaLock();
2560 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2561 {
2562 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2563 }
2564 else if (aScreenId < pDisplay->mcMonitors)
2565 {
2566 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2567
2568 /* Copy the bitmap to the guest VRAM. */
2569 const uint8_t *pu8Src = address;
2570 int32_t xSrc = 0;
2571 int32_t ySrc = 0;
2572 uint32_t u32SrcWidth = width;
2573 uint32_t u32SrcHeight = height;
2574 uint32_t u32SrcLineSize = width * 4;
2575 uint32_t u32SrcBitsPerPixel = 32;
2576
2577 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2578 int32_t xDst = x;
2579 int32_t yDst = y;
2580 uint32_t u32DstWidth = pFBInfo->w;
2581 uint32_t u32DstHeight = pFBInfo->h;
2582 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2583 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2584
2585 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2586 width, height,
2587 pu8Src,
2588 xSrc, ySrc,
2589 u32SrcWidth, u32SrcHeight,
2590 u32SrcLineSize, u32SrcBitsPerPixel,
2591 pu8Dst,
2592 xDst, yDst,
2593 u32DstWidth, u32DstHeight,
2594 u32DstLineSize, u32DstBitsPerPixel);
2595 if (RT_SUCCESS(rc))
2596 {
2597 if (!pFBInfo->pFramebuffer.isNull())
2598 {
2599 /* Update the changed screen area. When framebuffer uses VRAM directly, just notify
2600 * it to update. And for default format, render the guest VRAM to framebuffer.
2601 */
2602 if (pFBInfo->fDefaultFormat)
2603 {
2604 address = NULL;
2605 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2606 if (SUCCEEDED(hrc) && address != NULL)
2607 {
2608 pu8Src = pFBInfo->pu8FramebufferVRAM;
2609 xSrc = x;
2610 ySrc = y;
2611 u32SrcWidth = pFBInfo->w;
2612 u32SrcHeight = pFBInfo->h;
2613 u32SrcLineSize = pFBInfo->u32LineSize;
2614 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2615
2616 /* Default format is 32 bpp. */
2617 pu8Dst = address;
2618 xDst = xSrc;
2619 yDst = ySrc;
2620 u32DstWidth = u32SrcWidth;
2621 u32DstHeight = u32SrcHeight;
2622 u32DstLineSize = u32DstWidth * 4;
2623 u32DstBitsPerPixel = 32;
2624
2625 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2626 width, height,
2627 pu8Src,
2628 xSrc, ySrc,
2629 u32SrcWidth, u32SrcHeight,
2630 u32SrcLineSize, u32SrcBitsPerPixel,
2631 pu8Dst,
2632 xDst, yDst,
2633 u32DstWidth, u32DstHeight,
2634 u32DstLineSize, u32DstBitsPerPixel);
2635 }
2636 }
2637
2638 pDisplay->handleDisplayUpdate(aScreenId, x, y, width, height);
2639 }
2640 }
2641 }
2642 else
2643 {
2644 rc = VERR_INVALID_PARAMETER;
2645 }
2646 pDisplay->vbvaUnlock();
2647 return rc;
2648}
2649#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2650
2651STDMETHODIMP Display::DrawToScreen (ULONG aScreenId, BYTE *address, ULONG x, ULONG y,
2652 ULONG width, ULONG height)
2653{
2654 /// @todo (r=dmik) this function may take too long to complete if the VM
2655 // is doing something like saving state right now. Which, in case if it
2656 // is called on the GUI thread, will make it unresponsive. We should
2657 // check the machine state here (by enclosing the check and VMRequCall
2658 // within the Console lock to make it atomic).
2659
2660 LogFlowFuncEnter();
2661 LogFlowFunc (("address=%p, x=%d, y=%d, width=%d, height=%d\n",
2662 (void *)address, x, y, width, height));
2663
2664 CheckComArgNotNull(address);
2665 CheckComArgExpr(width, width != 0);
2666 CheckComArgExpr(height, height != 0);
2667
2668 AutoCaller autoCaller(this);
2669 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2670
2671 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2672
2673 CHECK_CONSOLE_DRV (mpDrv);
2674
2675 Console::SafeVMPtr pVM(mParent);
2676 if (FAILED(pVM.rc())) return pVM.rc();
2677
2678 /*
2679 * Again we're lazy and make the graphics device do all the
2680 * dirty conversion work.
2681 */
2682#ifdef VBOX_WITH_OLD_VBVA_LOCK
2683 int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)Display::drawToScreenEMT, 7,
2684 this, aScreenId, address, x, y, width, height);
2685#else
2686 int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)mpDrv->pUpPort->pfnDisplayBlt, 6,
2687 mpDrv->pUpPort, address, x, y, width, height);
2688#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2689
2690 /*
2691 * If the function returns not supported, we'll have to do all the
2692 * work ourselves using the framebuffer.
2693 */
2694 HRESULT rc = S_OK;
2695 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2696 {
2697 /** @todo implement generic fallback for screen blitting. */
2698 rc = E_NOTIMPL;
2699 }
2700 else if (RT_FAILURE(rcVBox))
2701 rc = setError(VBOX_E_IPRT_ERROR,
2702 tr("Could not draw to the screen (%Rrc)"), rcVBox);
2703//@todo
2704// else
2705// {
2706// /* All ok. Redraw the screen. */
2707// handleDisplayUpdate (x, y, width, height);
2708// }
2709
2710 LogFlowFunc (("rc=%08X\n", rc));
2711 LogFlowFuncLeave();
2712 return rc;
2713}
2714
2715#ifdef VBOX_WITH_OLD_VBVA_LOCK
2716void Display::InvalidateAndUpdateEMT(Display *pDisplay)
2717{
2718 pDisplay->vbvaLock();
2719 unsigned uScreenId;
2720 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2721 {
2722 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2723
2724 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
2725 {
2726 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort);
2727 }
2728 else
2729 {
2730 if (!pFBInfo->pFramebuffer.isNull())
2731 {
2732 /* Render complete VRAM screen to the framebuffer.
2733 * When framebuffer uses VRAM directly, just notify it to update.
2734 */
2735 if (pFBInfo->fDefaultFormat)
2736 {
2737 BYTE *address = NULL;
2738 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2739 if (SUCCEEDED(hrc) && address != NULL)
2740 {
2741 uint32_t width = pFBInfo->w;
2742 uint32_t height = pFBInfo->h;
2743
2744 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2745 int32_t xSrc = 0;
2746 int32_t ySrc = 0;
2747 uint32_t u32SrcWidth = pFBInfo->w;
2748 uint32_t u32SrcHeight = pFBInfo->h;
2749 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2750 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2751
2752 /* Default format is 32 bpp. */
2753 uint8_t *pu8Dst = address;
2754 int32_t xDst = xSrc;
2755 int32_t yDst = ySrc;
2756 uint32_t u32DstWidth = u32SrcWidth;
2757 uint32_t u32DstHeight = u32SrcHeight;
2758 uint32_t u32DstLineSize = u32DstWidth * 4;
2759 uint32_t u32DstBitsPerPixel = 32;
2760
2761 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2762 width, height,
2763 pu8Src,
2764 xSrc, ySrc,
2765 u32SrcWidth, u32SrcHeight,
2766 u32SrcLineSize, u32SrcBitsPerPixel,
2767 pu8Dst,
2768 xDst, yDst,
2769 u32DstWidth, u32DstHeight,
2770 u32DstLineSize, u32DstBitsPerPixel);
2771 }
2772 }
2773
2774 pDisplay->handleDisplayUpdate (uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2775 }
2776 }
2777 }
2778 pDisplay->vbvaUnlock();
2779}
2780#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2781
2782/**
2783 * Does a full invalidation of the VM display and instructs the VM
2784 * to update it immediately.
2785 *
2786 * @returns COM status code
2787 */
2788STDMETHODIMP Display::InvalidateAndUpdate()
2789{
2790 LogFlowFuncEnter();
2791
2792 AutoCaller autoCaller(this);
2793 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2794
2795 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2796
2797 CHECK_CONSOLE_DRV (mpDrv);
2798
2799 Console::SafeVMPtr pVM(mParent);
2800 if (FAILED(pVM.rc())) return pVM.rc();
2801
2802 HRESULT rc = S_OK;
2803
2804 LogFlowFunc (("Sending DPYUPDATE request\n"));
2805
2806 /* Have to leave the lock when calling EMT. */
2807 alock.leave ();
2808
2809 /* pdm.h says that this has to be called from the EMT thread */
2810#ifdef VBOX_WITH_OLD_VBVA_LOCK
2811 int rcVBox = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY, (PFNRT)Display::InvalidateAndUpdateEMT,
2812 1, this);
2813#else
2814 int rcVBox = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY,
2815 (PFNRT)mpDrv->pUpPort->pfnUpdateDisplayAll, 1, mpDrv->pUpPort);
2816#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2817 alock.enter ();
2818
2819 if (RT_FAILURE(rcVBox))
2820 rc = setError(VBOX_E_IPRT_ERROR,
2821 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2822
2823 LogFlowFunc (("rc=%08X\n", rc));
2824 LogFlowFuncLeave();
2825 return rc;
2826}
2827
2828/**
2829 * Notification that the framebuffer has completed the
2830 * asynchronous resize processing
2831 *
2832 * @returns COM status code
2833 */
2834STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
2835{
2836 LogFlowFunc (("\n"));
2837
2838 /// @todo (dmik) can we AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); here?
2839 // This will require general code review and may add some details.
2840 // In particular, we may want to check whether EMT is really waiting for
2841 // this notification, etc. It might be also good to obey the caller to make
2842 // sure this method is not called from more than one thread at a time
2843 // (and therefore don't use Display lock at all here to save some
2844 // milliseconds).
2845 AutoCaller autoCaller(this);
2846 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2847
2848 /* this is only valid for external framebuffers */
2849 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
2850 return setError(VBOX_E_NOT_SUPPORTED,
2851 tr("Resize completed notification is valid only for external framebuffers"));
2852
2853 /* Set the flag indicating that the resize has completed and display
2854 * data need to be updated. */
2855 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
2856 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
2857 AssertRelease(f);NOREF(f);
2858
2859 return S_OK;
2860}
2861
2862STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
2863{
2864#ifdef VBOX_WITH_VIDEOHWACCEL
2865 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsynch(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
2866 return S_OK;
2867#else
2868 return E_NOTIMPL;
2869#endif
2870}
2871
2872// private methods
2873/////////////////////////////////////////////////////////////////////////////
2874
2875/**
2876 * Helper to update the display information from the framebuffer.
2877 *
2878 * @thread EMT
2879 */
2880void Display::updateDisplayData(void)
2881{
2882 LogFlowFunc (("\n"));
2883
2884 /* the driver might not have been constructed yet */
2885 if (!mpDrv)
2886 return;
2887
2888#if DEBUG
2889 /*
2890 * Sanity check. Note that this method may be called on EMT after Console
2891 * has started the power down procedure (but before our #drvDestruct() is
2892 * called, in which case pVM will already be NULL but mpDrv will not). Since
2893 * we don't really need pVM to proceed, we avoid this check in the release
2894 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
2895 * time-critical method.
2896 */
2897 Console::SafeVMPtrQuiet pVM (mParent);
2898 if (pVM.isOk())
2899 VM_ASSERT_EMT (pVM.raw());
2900#endif
2901
2902 /* The method is only relevant to the primary framebuffer. */
2903 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
2904
2905 if (pFramebuffer)
2906 {
2907 HRESULT rc;
2908 BYTE *address = 0;
2909 rc = pFramebuffer->COMGETTER(Address) (&address);
2910 AssertComRC (rc);
2911 ULONG bytesPerLine = 0;
2912 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
2913 AssertComRC (rc);
2914 ULONG bitsPerPixel = 0;
2915 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
2916 AssertComRC (rc);
2917 ULONG width = 0;
2918 rc = pFramebuffer->COMGETTER(Width) (&width);
2919 AssertComRC (rc);
2920 ULONG height = 0;
2921 rc = pFramebuffer->COMGETTER(Height) (&height);
2922 AssertComRC (rc);
2923
2924 mpDrv->IConnector.pu8Data = (uint8_t *) address;
2925 mpDrv->IConnector.cbScanline = bytesPerLine;
2926 mpDrv->IConnector.cBits = bitsPerPixel;
2927 mpDrv->IConnector.cx = width;
2928 mpDrv->IConnector.cy = height;
2929 }
2930 else
2931 {
2932 /* black hole */
2933 mpDrv->IConnector.pu8Data = NULL;
2934 mpDrv->IConnector.cbScanline = 0;
2935 mpDrv->IConnector.cBits = 0;
2936 mpDrv->IConnector.cx = 0;
2937 mpDrv->IConnector.cy = 0;
2938 }
2939 LogFlowFunc (("leave\n"));
2940}
2941
2942#ifdef VBOX_WITH_CRHGSMI
2943void Display::setupCrHgsmiData(void)
2944{
2945 VMMDev *pVMMDev = mParent->getVMMDev();
2946 Assert(pVMMDev);
2947 int rc = VERR_GENERAL_FAILURE;
2948 if (pVMMDev)
2949 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
2950
2951 if (RT_SUCCESS(rc))
2952 {
2953 Assert(mhCrOglSvc);
2954 }
2955 else
2956 {
2957 mhCrOglSvc = NULL;
2958 }
2959}
2960
2961void Display::destructCrHgsmiData(void)
2962{
2963 mhCrOglSvc = NULL;
2964}
2965#endif
2966
2967/**
2968 * Changes the current frame buffer. Called on EMT to avoid both
2969 * race conditions and excessive locking.
2970 *
2971 * @note locks this object for writing
2972 * @thread EMT
2973 */
2974/* static */
2975DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
2976 unsigned uScreenId)
2977{
2978 LogFlowFunc (("uScreenId = %d\n", uScreenId));
2979
2980 AssertReturn(that, VERR_INVALID_PARAMETER);
2981 AssertReturn(uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
2982
2983 AutoCaller autoCaller(that);
2984 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2985
2986 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
2987
2988 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
2989 pDisplayFBInfo->pFramebuffer = aFB;
2990
2991 that->mParent->consoleVRDPServer()->SendResize ();
2992
2993 /* The driver might not have been constructed yet */
2994 if (that->mpDrv)
2995 {
2996 /* Setup the new framebuffer, the resize will lead to an updateDisplayData call. */
2997 DISPLAYFBINFO *pFBInfo = &that->maFramebuffers[uScreenId];
2998
2999 if (pFBInfo->fVBVAEnabled && pFBInfo->pu8FramebufferVRAM)
3000 {
3001 /* This display in VBVA mode. Resize it to the last guest resolution,
3002 * if it has been reported.
3003 */
3004 that->handleDisplayResize(uScreenId, pFBInfo->u16BitsPerPixel,
3005 pFBInfo->pu8FramebufferVRAM,
3006 pFBInfo->u32LineSize,
3007 pFBInfo->w,
3008 pFBInfo->h,
3009 pFBInfo->flags);
3010 }
3011 else if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3012 {
3013 /* VGA device mode, only for the primary screen. */
3014 that->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, that->mLastBitsPerPixel,
3015 that->mLastAddress,
3016 that->mLastBytesPerLine,
3017 that->mLastWidth,
3018 that->mLastHeight,
3019 that->mLastFlags);
3020 }
3021 }
3022
3023 LogFlowFunc (("leave\n"));
3024 return VINF_SUCCESS;
3025}
3026
3027/**
3028 * Handle display resize event issued by the VGA device for the primary screen.
3029 *
3030 * @see PDMIDISPLAYCONNECTOR::pfnResize
3031 */
3032DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
3033 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
3034{
3035 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3036
3037 LogFlowFunc (("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
3038 bpp, pvVRAM, cbLine, cx, cy));
3039
3040 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, VBVA_SCREEN_F_ACTIVE);
3041}
3042
3043/**
3044 * Handle display update.
3045 *
3046 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
3047 */
3048DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
3049 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
3050{
3051 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3052
3053#ifdef DEBUG_sunlover
3054 LogFlowFunc (("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
3055 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
3056#endif /* DEBUG_sunlover */
3057
3058 /* This call does update regardless of VBVA status.
3059 * But in VBVA mode this is called only as result of
3060 * pfnUpdateDisplayAll in the VGA device.
3061 */
3062
3063 pDrv->pDisplay->handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
3064}
3065
3066/**
3067 * Periodic display refresh callback.
3068 *
3069 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
3070 */
3071DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
3072{
3073 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3074
3075#ifdef DEBUG_sunlover
3076 STAM_PROFILE_START(&StatDisplayRefresh, a);
3077#endif /* DEBUG_sunlover */
3078
3079#ifdef DEBUG_sunlover_2
3080 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
3081 pDrv->pDisplay->mfVideoAccelEnabled));
3082#endif /* DEBUG_sunlover_2 */
3083
3084 Display *pDisplay = pDrv->pDisplay;
3085 bool fNoUpdate = false; /* Do not update the display if any of the framebuffers is being resized. */
3086 unsigned uScreenId;
3087
3088 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3089 {
3090 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3091
3092 /* Check the resize status. The status can be checked normally because
3093 * the status affects only the EMT.
3094 */
3095 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
3096
3097 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
3098 {
3099 LogFlowFunc (("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
3100 fNoUpdate = true; /* Always set it here, because pfnUpdateDisplayAll can cause a new resize. */
3101 /* The framebuffer was resized and display data need to be updated. */
3102 pDisplay->handleResizeCompletedEMT ();
3103 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
3104 {
3105 /* The resize status could be not Void here because a pending resize is issued. */
3106 continue;
3107 }
3108 /* Continue with normal processing because the status here is ResizeStatus_Void. */
3109 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3110 {
3111 /* Repaint the display because VM continued to run during the framebuffer resize. */
3112 if (!pFBInfo->pFramebuffer.isNull())
3113#ifdef VBOX_WITH_OLD_VBVA_LOCK
3114 {
3115 pDisplay->vbvaLock();
3116#endif /* VBOX_WITH_OLD_VBVA_LOCK */
3117 pDrv->pUpPort->pfnUpdateDisplayAll(pDrv->pUpPort);
3118#ifdef VBOX_WITH_OLD_VBVA_LOCK
3119 pDisplay->vbvaUnlock();
3120 }
3121#endif /* VBOX_WITH_OLD_VBVA_LOCK */
3122 }
3123 }
3124 else if (u32ResizeStatus == ResizeStatus_InProgress)
3125 {
3126 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
3127 LogFlowFunc (("ResizeStatus_InProcess\n"));
3128 fNoUpdate = true;
3129 continue;
3130 }
3131 }
3132
3133 if (!fNoUpdate)
3134 {
3135#ifdef VBOX_WITH_OLD_VBVA_LOCK
3136 int rc = pDisplay->videoAccelRefreshProcess();
3137
3138 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3139 {
3140 if (rc == VWRN_INVALID_STATE)
3141 {
3142 /* No VBVA do a display update. */
3143 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3144 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3145 {
3146 Assert(pDrv->IConnector.pu8Data);
3147 pDisplay->vbvaLock();
3148 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3149 pDisplay->vbvaUnlock();
3150 }
3151 }
3152
3153 /* Inform the VRDP server that the current display update sequence is
3154 * completed. At this moment the framebuffer memory contains a definite
3155 * image, that is synchronized with the orders already sent to VRDP client.
3156 * The server can now process redraw requests from clients or initial
3157 * fullscreen updates for new clients.
3158 */
3159 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3160 {
3161 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3162
3163 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3164 {
3165 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
3166 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
3167 }
3168 }
3169 }
3170#else
3171 if (pDisplay->mfPendingVideoAccelEnable)
3172 {
3173 /* Acceleration was enabled while machine was not yet running
3174 * due to restoring from saved state. Update entire display and
3175 * actually enable acceleration.
3176 */
3177 Assert(pDisplay->mpPendingVbvaMemory);
3178
3179 /* Acceleration can not be yet enabled.*/
3180 Assert(pDisplay->mpVbvaMemory == NULL);
3181 Assert(!pDisplay->mfVideoAccelEnabled);
3182
3183 if (pDisplay->mfMachineRunning)
3184 {
3185 pDisplay->VideoAccelEnable (pDisplay->mfPendingVideoAccelEnable,
3186 pDisplay->mpPendingVbvaMemory);
3187
3188 /* Reset the pending state. */
3189 pDisplay->mfPendingVideoAccelEnable = false;
3190 pDisplay->mpPendingVbvaMemory = NULL;
3191 }
3192 }
3193 else
3194 {
3195 Assert(pDisplay->mpPendingVbvaMemory == NULL);
3196
3197 if (pDisplay->mfVideoAccelEnabled)
3198 {
3199 Assert(pDisplay->mpVbvaMemory);
3200 pDisplay->VideoAccelFlush ();
3201 }
3202 else
3203 {
3204 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3205 if (!pFBInfo->pFramebuffer.isNull())
3206 {
3207 Assert(pDrv->IConnector.pu8Data);
3208 Assert(pFBInfo->u32ResizeStatus == ResizeStatus_Void);
3209 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3210 }
3211 }
3212
3213 /* Inform the VRDP server that the current display update sequence is
3214 * completed. At this moment the framebuffer memory contains a definite
3215 * image, that is synchronized with the orders already sent to VRDP client.
3216 * The server can now process redraw requests from clients or initial
3217 * fullscreen updates for new clients.
3218 */
3219 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3220 {
3221 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3222
3223 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3224 {
3225 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
3226 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
3227 }
3228 }
3229 }
3230#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
3231 }
3232
3233#ifdef DEBUG_sunlover
3234 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
3235#endif /* DEBUG_sunlover */
3236#ifdef DEBUG_sunlover_2
3237 LogFlowFunc (("leave\n"));
3238#endif /* DEBUG_sunlover_2 */
3239}
3240
3241/**
3242 * Reset notification
3243 *
3244 * @see PDMIDISPLAYCONNECTOR::pfnReset
3245 */
3246DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3247{
3248 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3249
3250 LogFlowFunc (("\n"));
3251
3252 /* Disable VBVA mode. */
3253 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3254}
3255
3256/**
3257 * LFBModeChange notification
3258 *
3259 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3260 */
3261DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3262{
3263 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3264
3265 LogFlowFunc (("fEnabled=%d\n", fEnabled));
3266
3267 NOREF(fEnabled);
3268
3269 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3270#ifdef VBOX_WITH_OLD_VBVA_LOCK
3271 /* This is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
3272 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
3273#else
3274 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3275#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
3276}
3277
3278/**
3279 * Adapter information change notification.
3280 *
3281 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3282 */
3283DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
3284{
3285 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3286
3287 if (pvVRAM == NULL)
3288 {
3289 unsigned i;
3290 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
3291 {
3292 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
3293
3294 pFBInfo->u32Offset = 0;
3295 pFBInfo->u32MaxFramebufferSize = 0;
3296 pFBInfo->u32InformationSize = 0;
3297 }
3298 }
3299#ifndef VBOX_WITH_HGSMI
3300 else
3301 {
3302 uint8_t *pu8 = (uint8_t *)pvVRAM;
3303 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3304
3305 // @todo
3306 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3307
3308 VBOXVIDEOINFOHDR *pHdr;
3309
3310 for (;;)
3311 {
3312 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3313 pu8 += sizeof (VBOXVIDEOINFOHDR);
3314
3315 if (pu8 >= pu8End)
3316 {
3317 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
3318 break;
3319 }
3320
3321 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
3322 {
3323 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
3324 {
3325 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
3326 break;
3327 }
3328
3329 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
3330
3331 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
3332 {
3333 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
3334 break;
3335 }
3336
3337 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
3338
3339 pFBInfo->u32Offset = pDisplay->u32Offset;
3340 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
3341 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
3342
3343 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));
3344 }
3345 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
3346 {
3347 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
3348 {
3349 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
3350 break;
3351 }
3352
3353 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
3354
3355 switch (pConf32->u32Index)
3356 {
3357 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
3358 {
3359 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
3360 } break;
3361
3362 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
3363 {
3364 /* @todo make configurable. */
3365 pConf32->u32Value = _1M;
3366 } break;
3367
3368 default:
3369 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
3370 }
3371 }
3372 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3373 {
3374 if (pHdr->u16Length != 0)
3375 {
3376 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3377 break;
3378 }
3379
3380 break;
3381 }
3382 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
3383 {
3384 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
3385 }
3386
3387 pu8 += pHdr->u16Length;
3388 }
3389 }
3390#endif /* !VBOX_WITH_HGSMI */
3391}
3392
3393/**
3394 * Display information change notification.
3395 *
3396 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3397 */
3398DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
3399{
3400 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3401
3402 if (uScreenId >= pDrv->pDisplay->mcMonitors)
3403 {
3404 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
3405 return;
3406 }
3407
3408 /* Get the display information structure. */
3409 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
3410
3411 uint8_t *pu8 = (uint8_t *)pvVRAM;
3412 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
3413
3414 // @todo
3415 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
3416
3417 VBOXVIDEOINFOHDR *pHdr;
3418
3419 for (;;)
3420 {
3421 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3422 pu8 += sizeof (VBOXVIDEOINFOHDR);
3423
3424 if (pu8 >= pu8End)
3425 {
3426 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
3427 break;
3428 }
3429
3430 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
3431 {
3432 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
3433 {
3434 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
3435 break;
3436 }
3437
3438 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
3439
3440 pFBInfo->xOrigin = pScreen->xOrigin;
3441 pFBInfo->yOrigin = pScreen->yOrigin;
3442
3443 pFBInfo->w = pScreen->u16Width;
3444 pFBInfo->h = pScreen->u16Height;
3445
3446 LogFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
3447 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
3448
3449 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
3450 {
3451 /* Primary screen resize is initiated by the VGA device. */
3452 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, VBVA_SCREEN_F_ACTIVE);
3453 }
3454 }
3455 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3456 {
3457 if (pHdr->u16Length != 0)
3458 {
3459 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3460 break;
3461 }
3462
3463 break;
3464 }
3465 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
3466 {
3467 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
3468 {
3469 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
3470 break;
3471 }
3472
3473 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
3474
3475 pFBInfo->pHostEvents = pHostEvents;
3476
3477 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
3478 pHostEvents));
3479 }
3480 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
3481 {
3482 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
3483 {
3484 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
3485 break;
3486 }
3487
3488 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
3489 pu8 += pLink->i32Offset;
3490 }
3491 else
3492 {
3493 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
3494 }
3495
3496 pu8 += pHdr->u16Length;
3497 }
3498}
3499
3500#ifdef VBOX_WITH_VIDEOHWACCEL
3501
3502void Display::handleVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3503{
3504 unsigned id = (unsigned)pCommand->iDisplay;
3505 int rc = VINF_SUCCESS;
3506 if (id < mcMonitors)
3507 {
3508 IFramebuffer *pFramebuffer = maFramebuffers[id].pFramebuffer;
3509#ifdef DEBUG_misha
3510 Assert (pFramebuffer);
3511#endif
3512
3513 if (pFramebuffer != NULL)
3514 {
3515 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3516 if (FAILED(hr))
3517 {
3518 rc = (hr == E_NOTIMPL) ? VERR_NOT_IMPLEMENTED : VERR_GENERAL_FAILURE;
3519 }
3520 }
3521 else
3522 {
3523 rc = VERR_NOT_IMPLEMENTED;
3524 }
3525 }
3526 else
3527 {
3528 rc = VERR_INVALID_PARAMETER;
3529 }
3530
3531 if (RT_FAILURE(rc))
3532 {
3533 /* tell the guest the command is complete */
3534 pCommand->Flags &= (~VBOXVHWACMD_FLAG_HG_ASYNCH);
3535 pCommand->rc = rc;
3536 }
3537}
3538
3539DECLCALLBACK(void) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3540{
3541 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3542
3543 pDrv->pDisplay->handleVHWACommandProcess(pInterface, pCommand);
3544}
3545#endif
3546
3547#ifdef VBOX_WITH_CRHGSMI
3548void Display::handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3549{
3550 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
3551}
3552
3553void Display::handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3554{
3555 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr, result);
3556}
3557
3558void Display::handleCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd)
3559{
3560 int rc = VERR_INVALID_FUNCTION;
3561 VBOXHGCMSVCPARM parm;
3562 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3563 parm.u.pointer.addr = pCmd;
3564 parm.u.pointer.size = 0;
3565
3566 if (mhCrOglSvc)
3567 {
3568 VMMDev *pVMMDev = mParent->getVMMDev();
3569 if (pVMMDev)
3570 {
3571 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, Display::displayCrHgsmiCommandCompletion, this);
3572 AssertRC(rc);
3573 if (RT_SUCCESS(rc))
3574 return;
3575 }
3576 else
3577 rc = VERR_INVALID_STATE;
3578 }
3579
3580 /* we are here because something went wrong with command processing, complete it */
3581 handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
3582}
3583
3584void Display::handleCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCtl)
3585{
3586 int rc = VERR_INVALID_FUNCTION;
3587 VBOXHGCMSVCPARM parm;
3588 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3589 parm.u.pointer.addr = pCtl;
3590 parm.u.pointer.size = 0;
3591
3592 if (mhCrOglSvc)
3593 {
3594 VMMDev *pVMMDev = mParent->getVMMDev();
3595 if (pVMMDev)
3596 {
3597 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm, Display::displayCrHgsmiControlCompletion, this);
3598 AssertRC(rc);
3599 if (RT_SUCCESS(rc))
3600 return;
3601 }
3602 else
3603 rc = VERR_INVALID_STATE;
3604 }
3605
3606 /* we are here because something went wrong with command processing, complete it */
3607 handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
3608}
3609
3610DECLCALLBACK(void) Display::displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd)
3611{
3612 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3613
3614 pDrv->pDisplay->handleCrHgsmiCommandProcess(pInterface, pCmd);
3615}
3616
3617DECLCALLBACK(void) Display::displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd)
3618{
3619 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3620
3621 pDrv->pDisplay->handleCrHgsmiControlProcess(pInterface, pCmd);
3622}
3623
3624DECLCALLBACK(void) Display::displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3625{
3626 Display *pDisplay = (Display *)pvContext;
3627 pDisplay->handleCrHgsmiCommandCompletion(result, u32Function, pParam);
3628}
3629
3630DECLCALLBACK(void) Display::displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3631{
3632 Display *pDisplay = (Display *)pvContext;
3633 pDisplay->handleCrHgsmiControlCompletion(result, u32Function, pParam);
3634}
3635#endif
3636
3637
3638#ifdef VBOX_WITH_HGSMI
3639DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags)
3640{
3641 LogFlowFunc(("uScreenId %d\n", uScreenId));
3642
3643 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3644 Display *pThis = pDrv->pDisplay;
3645
3646 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3647 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3648
3649 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3650
3651 return VINF_SUCCESS;
3652}
3653
3654DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3655{
3656 LogFlowFunc(("uScreenId %d\n", uScreenId));
3657
3658 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3659 Display *pThis = pDrv->pDisplay;
3660
3661 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3662
3663 pFBInfo->fVBVAEnabled = false;
3664
3665 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3666
3667 pFBInfo->pVBVAHostFlags = NULL;
3668
3669 pFBInfo->u32Offset = 0; /* Not used in HGSMI. */
3670 pFBInfo->u32MaxFramebufferSize = 0; /* Not used in HGSMI. */
3671 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3672
3673 pFBInfo->xOrigin = 0;
3674 pFBInfo->yOrigin = 0;
3675
3676 pFBInfo->w = 0;
3677 pFBInfo->h = 0;
3678
3679 pFBInfo->u16BitsPerPixel = 0;
3680 pFBInfo->pu8FramebufferVRAM = NULL;
3681 pFBInfo->u32LineSize = 0;
3682}
3683
3684DECLCALLBACK(void) Display::displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3685{
3686 LogFlowFunc(("uScreenId %d\n", uScreenId));
3687
3688 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3689 Display *pThis = pDrv->pDisplay;
3690 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3691
3692 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3693 {
3694 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers, pThis->mcMonitors);
3695 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3696 }
3697
3698 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
3699 {
3700 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
3701 {
3702 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
3703 * under display device lock, so thread safe.
3704 */
3705 pFBInfo->cVBVASkipUpdate = 0;
3706 pThis->handleDisplayUpdate(uScreenId, pFBInfo->vbvaSkippedRect.xLeft - pFBInfo->xOrigin,
3707 pFBInfo->vbvaSkippedRect.yTop - pFBInfo->yOrigin,
3708 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
3709 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
3710 }
3711 }
3712 else
3713 {
3714 /* The framebuffer is being resized. */
3715 pFBInfo->cVBVASkipUpdate++;
3716 }
3717}
3718
3719DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, const PVBVACMDHDR pCmd, size_t cbCmd)
3720{
3721 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3722
3723 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3724 Display *pThis = pDrv->pDisplay;
3725 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3726
3727 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3728 {
3729 if (pFBInfo->fDefaultFormat)
3730 {
3731 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3732 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
3733 {
3734 pDrv->pUpPort->pfnUpdateDisplayRect (pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
3735 }
3736 else if (!pFBInfo->pFramebuffer.isNull())
3737 {
3738 /* Render VRAM content to the framebuffer. */
3739 BYTE *address = NULL;
3740 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
3741 if (SUCCEEDED(hrc) && address != NULL)
3742 {
3743 uint32_t width = pCmd->w;
3744 uint32_t height = pCmd->h;
3745
3746 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3747 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
3748 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
3749 uint32_t u32SrcWidth = pFBInfo->w;
3750 uint32_t u32SrcHeight = pFBInfo->h;
3751 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3752 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3753
3754 uint8_t *pu8Dst = address;
3755 int32_t xDst = xSrc;
3756 int32_t yDst = ySrc;
3757 uint32_t u32DstWidth = u32SrcWidth;
3758 uint32_t u32DstHeight = u32SrcHeight;
3759 uint32_t u32DstLineSize = u32DstWidth * 4;
3760 uint32_t u32DstBitsPerPixel = 32;
3761
3762 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3763 width, height,
3764 pu8Src,
3765 xSrc, ySrc,
3766 u32SrcWidth, u32SrcHeight,
3767 u32SrcLineSize, u32SrcBitsPerPixel,
3768 pu8Dst,
3769 xDst, yDst,
3770 u32DstWidth, u32DstHeight,
3771 u32DstLineSize, u32DstBitsPerPixel);
3772 }
3773 }
3774 }
3775
3776 VBVACMDHDR hdrSaved = *pCmd;
3777
3778 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3779
3780 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3781 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3782
3783 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
3784 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, cbCmd);
3785
3786 *pHdrUnconst = hdrSaved;
3787 }
3788}
3789
3790DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y, uint32_t cx, uint32_t cy)
3791{
3792 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3793
3794 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3795 Display *pThis = pDrv->pDisplay;
3796 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3797
3798 /* @todo handleFramebufferUpdate (uScreenId,
3799 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3800 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3801 * cx, cy);
3802 */
3803 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3804 {
3805 pThis->handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3806 }
3807 else
3808 {
3809 /* Save the updated rectangle. */
3810 int32_t xRight = x + cx;
3811 int32_t yBottom = y + cy;
3812
3813 if (pFBInfo->cVBVASkipUpdate == 1)
3814 {
3815 pFBInfo->vbvaSkippedRect.xLeft = x;
3816 pFBInfo->vbvaSkippedRect.yTop = y;
3817 pFBInfo->vbvaSkippedRect.xRight = xRight;
3818 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3819 }
3820 else
3821 {
3822 if (pFBInfo->vbvaSkippedRect.xLeft > x)
3823 {
3824 pFBInfo->vbvaSkippedRect.xLeft = x;
3825 }
3826 if (pFBInfo->vbvaSkippedRect.yTop > y)
3827 {
3828 pFBInfo->vbvaSkippedRect.yTop = y;
3829 }
3830 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
3831 {
3832 pFBInfo->vbvaSkippedRect.xRight = xRight;
3833 }
3834 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
3835 {
3836 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3837 }
3838 }
3839 }
3840}
3841
3842DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
3843{
3844 LogFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3845
3846 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3847 Display *pThis = pDrv->pDisplay;
3848
3849 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
3850
3851 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
3852 {
3853 pFBInfo->fDisabled = true;
3854 pFBInfo->flags = pScreen->u16Flags;
3855
3856 /* Temporary: ask framebuffer to resize using a default format. The framebuffer will be black. */
3857 pThis->handleDisplayResize(pScreen->u32ViewIndex, 0,
3858 (uint8_t *)NULL,
3859 0, pFBInfo->w, pFBInfo->h, pScreen->u16Flags);
3860
3861 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
3862 GuestMonitorChangedEventType_Disabled,
3863 pScreen->u32ViewIndex,
3864 0, 0, 0, 0);
3865 return VINF_SUCCESS;
3866 }
3867
3868 if (pFBInfo->fDisabled)
3869 {
3870 pFBInfo->fDisabled = false;
3871 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
3872 GuestMonitorChangedEventType_Enabled,
3873 pScreen->u32ViewIndex,
3874 pScreen->i32OriginX, pScreen->i32OriginY,
3875 pScreen->u32Width, pScreen->u32Height);
3876 if (pFBInfo->pFramebuffer.isNull())
3877 {
3878 /* @todo If no framebuffer, remember the resize parameters to issue a requestResize later. */
3879 return VINF_SUCCESS;
3880 }
3881 /* If the framebuffer already set for the screen, do a regular resize. */
3882 }
3883
3884 /* Check if this is a real resize or a notification about the screen origin.
3885 * The guest uses this VBVAResize call for both.
3886 */
3887 bool fResize = pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
3888 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
3889 || pFBInfo->u32LineSize != pScreen->u32LineSize
3890 || pFBInfo->w != pScreen->u32Width
3891 || pFBInfo->h != pScreen->u32Height;
3892
3893 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
3894 || pFBInfo->yOrigin != pScreen->i32OriginY;
3895
3896 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
3897 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
3898 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3899
3900 pFBInfo->xOrigin = pScreen->i32OriginX;
3901 pFBInfo->yOrigin = pScreen->i32OriginY;
3902
3903 pFBInfo->w = pScreen->u32Width;
3904 pFBInfo->h = pScreen->u32Height;
3905
3906 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
3907 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
3908 pFBInfo->u32LineSize = pScreen->u32LineSize;
3909
3910 pFBInfo->flags = pScreen->u16Flags;
3911
3912 if (fNewOrigin)
3913 {
3914 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
3915 GuestMonitorChangedEventType_NewOrigin,
3916 pScreen->u32ViewIndex,
3917 pScreen->i32OriginX, pScreen->i32OriginY,
3918 0, 0);
3919 }
3920
3921#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3922 if (fNewOrigin && !fResize)
3923 {
3924 BOOL is3denabled;
3925 pThis->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3926
3927 if (is3denabled)
3928 {
3929 VBOXHGCMSVCPARM parm;
3930
3931 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
3932 parm.u.uint32 = pScreen->u32ViewIndex;
3933
3934 VMMDev *pVMMDev = pThis->mParent->getVMMDev();
3935
3936 if (pVMMDev)
3937 pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED, SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
3938 }
3939 }
3940#endif /* VBOX_WITH_CROGL */
3941
3942 if (!fResize)
3943 {
3944 /* No parameters of the framebuffer have actually changed. */
3945 if (fNewOrigin)
3946 {
3947 /* VRDP server still need this notification. */
3948 LogFlowFunc (("Calling VRDP\n"));
3949 pThis->mParent->consoleVRDPServer()->SendResize();
3950 }
3951 return VINF_SUCCESS;
3952 }
3953
3954 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
3955 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
3956 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
3957}
3958
3959DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
3960 uint32_t xHot, uint32_t yHot,
3961 uint32_t cx, uint32_t cy,
3962 const void *pvShape)
3963{
3964 LogFlowFunc(("\n"));
3965
3966 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3967 Display *pThis = pDrv->pDisplay;
3968
3969 size_t cbShapeSize = 0;
3970
3971 if (pvShape)
3972 {
3973 cbShapeSize = (cx + 7) / 8 * cy; /* size of the AND mask */
3974 cbShapeSize = ((cbShapeSize + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
3975 }
3976 com::SafeArray<BYTE> shapeData(cbShapeSize);
3977
3978 if (pvShape)
3979 ::memcpy(shapeData.raw(), pvShape, cbShapeSize);
3980
3981 /* Tell the console about it */
3982 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
3983 xHot, yHot, cx, cy, ComSafeArrayAsInParam(shapeData));
3984
3985 return VINF_SUCCESS;
3986}
3987#endif /* VBOX_WITH_HGSMI */
3988
3989/**
3990 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3991 */
3992DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3993{
3994 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
3995 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3996 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
3997 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
3998 return NULL;
3999}
4000
4001
4002/**
4003 * Destruct a display driver instance.
4004 *
4005 * @returns VBox status.
4006 * @param pDrvIns The driver instance data.
4007 */
4008DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
4009{
4010 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4011 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
4012 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4013
4014 if (pData->pDisplay)
4015 {
4016 AutoWriteLock displayLock(pData->pDisplay COMMA_LOCKVAL_SRC_POS);
4017#ifdef VBOX_WITH_CRHGSMI
4018 pData->pDisplay->destructCrHgsmiData();
4019#endif
4020 pData->pDisplay->mpDrv = NULL;
4021 pData->pDisplay->mpVMMDev = NULL;
4022 pData->pDisplay->mLastAddress = NULL;
4023 pData->pDisplay->mLastBytesPerLine = 0;
4024 pData->pDisplay->mLastBitsPerPixel = 0,
4025 pData->pDisplay->mLastWidth = 0;
4026 pData->pDisplay->mLastHeight = 0;
4027 }
4028}
4029
4030
4031/**
4032 * Construct a display driver instance.
4033 *
4034 * @copydoc FNPDMDRVCONSTRUCT
4035 */
4036DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
4037{
4038 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4039 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
4040 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
4041
4042 /*
4043 * Validate configuration.
4044 */
4045 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
4046 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
4047 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
4048 ("Configuration error: Not possible to attach anything to this driver!\n"),
4049 VERR_PDM_DRVINS_NO_ATTACH);
4050
4051 /*
4052 * Init Interfaces.
4053 */
4054 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
4055
4056 pData->IConnector.pfnResize = Display::displayResizeCallback;
4057 pData->IConnector.pfnUpdateRect = Display::displayUpdateCallback;
4058 pData->IConnector.pfnRefresh = Display::displayRefreshCallback;
4059 pData->IConnector.pfnReset = Display::displayResetCallback;
4060 pData->IConnector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
4061 pData->IConnector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
4062 pData->IConnector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
4063#ifdef VBOX_WITH_VIDEOHWACCEL
4064 pData->IConnector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
4065#endif
4066#ifdef VBOX_WITH_CRHGSMI
4067 pData->IConnector.pfnCrHgsmiCommandProcess = Display::displayCrHgsmiCommandProcess;
4068 pData->IConnector.pfnCrHgsmiControlProcess = Display::displayCrHgsmiControlProcess;
4069#endif
4070#ifdef VBOX_WITH_HGSMI
4071 pData->IConnector.pfnVBVAEnable = Display::displayVBVAEnable;
4072 pData->IConnector.pfnVBVADisable = Display::displayVBVADisable;
4073 pData->IConnector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
4074 pData->IConnector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
4075 pData->IConnector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
4076 pData->IConnector.pfnVBVAResize = Display::displayVBVAResize;
4077 pData->IConnector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
4078#endif
4079
4080
4081 /*
4082 * Get the IDisplayPort interface of the above driver/device.
4083 */
4084 pData->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
4085 if (!pData->pUpPort)
4086 {
4087 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
4088 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4089 }
4090#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
4091 pData->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
4092 if (!pData->pVBVACallbacks)
4093 {
4094 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
4095 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4096 }
4097#endif
4098 /*
4099 * Get the Display object pointer and update the mpDrv member.
4100 */
4101 void *pv;
4102 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
4103 if (RT_FAILURE(rc))
4104 {
4105 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
4106 return rc;
4107 }
4108 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
4109 pData->pDisplay->mpDrv = pData;
4110
4111 /*
4112 * Update our display information according to the framebuffer
4113 */
4114 pData->pDisplay->updateDisplayData();
4115
4116 /*
4117 * Start periodic screen refreshes
4118 */
4119 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
4120
4121#ifdef VBOX_WITH_CRHGSMI
4122 pData->pDisplay->setupCrHgsmiData();
4123#endif
4124
4125 return VINF_SUCCESS;
4126}
4127
4128
4129/**
4130 * Display driver registration record.
4131 */
4132const PDMDRVREG Display::DrvReg =
4133{
4134 /* u32Version */
4135 PDM_DRVREG_VERSION,
4136 /* szName */
4137 "MainDisplay",
4138 /* szRCMod */
4139 "",
4140 /* szR0Mod */
4141 "",
4142 /* pszDescription */
4143 "Main display driver (Main as in the API).",
4144 /* fFlags */
4145 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
4146 /* fClass. */
4147 PDM_DRVREG_CLASS_DISPLAY,
4148 /* cMaxInstances */
4149 ~0,
4150 /* cbInstance */
4151 sizeof(DRVMAINDISPLAY),
4152 /* pfnConstruct */
4153 Display::drvConstruct,
4154 /* pfnDestruct */
4155 Display::drvDestruct,
4156 /* pfnRelocate */
4157 NULL,
4158 /* pfnIOCtl */
4159 NULL,
4160 /* pfnPowerOn */
4161 NULL,
4162 /* pfnReset */
4163 NULL,
4164 /* pfnSuspend */
4165 NULL,
4166 /* pfnResume */
4167 NULL,
4168 /* pfnAttach */
4169 NULL,
4170 /* pfnDetach */
4171 NULL,
4172 /* pfnPowerOff */
4173 NULL,
4174 /* pfnSoftReset */
4175 NULL,
4176 /* u32EndVersion */
4177 PDM_DRVREG_VERSION
4178};
4179/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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