VirtualBox

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

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

crOpenGL: VM window scroll handling

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