VirtualBox

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

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

DisplayImpl: cosmetcial fix

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