VirtualBox

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

Last change on this file since 41528 was 41492, checked in by vboxsync, 13 years ago

DisplayImpl: secondary displays are disabled at startup.

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