VirtualBox

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

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

DisplayImpl: make sure that the primary screen is enabled, when the guest does not use VBVA (for example after a VM reset).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 140.9 KB
Line 
1/* $Id: DisplayImpl.cpp 41597 2012-06-06 13:19:24Z 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 aWidth, ULONG aHeight,
2130 ULONG aBitsPerPixel, ULONG aDisplay)
2131{
2132 AutoCaller autoCaller(this);
2133 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2134
2135 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2136
2137 CHECK_CONSOLE_DRV (mpDrv);
2138
2139 /*
2140 * Do some rough checks for valid input
2141 */
2142 ULONG width = aWidth;
2143 if (!width)
2144 width = mpDrv->IConnector.cx;
2145 ULONG height = aHeight;
2146 if (!height)
2147 height = mpDrv->IConnector.cy;
2148 ULONG bpp = aBitsPerPixel;
2149 if (!bpp)
2150 {
2151 uint32_t cBits = 0;
2152 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
2153 AssertRC(rc);
2154 bpp = cBits;
2155 }
2156 ULONG cMonitors;
2157 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
2158 if (cMonitors == 0 && aDisplay > 0)
2159 return E_INVALIDARG;
2160 if (aDisplay >= cMonitors)
2161 return E_INVALIDARG;
2162
2163// sunlover 20070614: It is up to the guest to decide whether the hint is valid.
2164// ULONG vramSize;
2165// mParent->machine()->COMGETTER(VRAMSize)(&vramSize);
2166// /* enough VRAM? */
2167// if ((width * height * (bpp / 8)) > (vramSize * 1024 * 1024))
2168// return setError(E_FAIL, tr("Not enough VRAM for the selected video mode"));
2169
2170 /* Have to release the lock because the pfnRequestDisplayChange
2171 * will call EMT. */
2172 alock.release();
2173
2174 VMMDev *pVMMDev = mParent->getVMMDev();
2175 if (pVMMDev)
2176 {
2177 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2178 if (pVMMDevPort)
2179 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel, aDisplay);
2180 }
2181 return S_OK;
2182}
2183
2184STDMETHODIMP Display::SetSeamlessMode (BOOL enabled)
2185{
2186 AutoCaller autoCaller(this);
2187 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2188
2189 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2190
2191 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
2192 alock.release();
2193
2194 VMMDev *pVMMDev = mParent->getVMMDev();
2195 if (pVMMDev)
2196 {
2197 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2198 if (pVMMDevPort)
2199 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
2200 }
2201 return S_OK;
2202}
2203
2204int Display::displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppu8Data, size_t *pcbData, uint32_t *pu32Width, uint32_t *pu32Height)
2205{
2206 int rc;
2207 pDisplay->vbvaLock();
2208 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
2209 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
2210 {
2211 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppu8Data, pcbData, pu32Width, pu32Height);
2212 }
2213 else if (aScreenId < pDisplay->mcMonitors)
2214 {
2215 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2216
2217 uint32_t width = pFBInfo->w;
2218 uint32_t height = pFBInfo->h;
2219
2220 /* Allocate 32 bit per pixel bitmap. */
2221 size_t cbRequired = width * 4 * height;
2222
2223 if (cbRequired)
2224 {
2225 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbRequired);
2226
2227 if (pu8Data == NULL)
2228 {
2229 rc = VERR_NO_MEMORY;
2230 }
2231 else
2232 {
2233 /* Copy guest VRAM to the allocated 32bpp buffer. */
2234 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2235 int32_t xSrc = 0;
2236 int32_t ySrc = 0;
2237 uint32_t u32SrcWidth = width;
2238 uint32_t u32SrcHeight = height;
2239 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2240 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2241
2242 uint8_t *pu8Dst = pu8Data;
2243 int32_t xDst = 0;
2244 int32_t yDst = 0;
2245 uint32_t u32DstWidth = u32SrcWidth;
2246 uint32_t u32DstHeight = u32SrcHeight;
2247 uint32_t u32DstLineSize = u32DstWidth * 4;
2248 uint32_t u32DstBitsPerPixel = 32;
2249
2250 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2251 width, height,
2252 pu8Src,
2253 xSrc, ySrc,
2254 u32SrcWidth, u32SrcHeight,
2255 u32SrcLineSize, u32SrcBitsPerPixel,
2256 pu8Dst,
2257 xDst, yDst,
2258 u32DstWidth, u32DstHeight,
2259 u32DstLineSize, u32DstBitsPerPixel);
2260 if (RT_SUCCESS(rc))
2261 {
2262 *ppu8Data = pu8Data;
2263 *pcbData = cbRequired;
2264 *pu32Width = width;
2265 *pu32Height = height;
2266 }
2267 else
2268 {
2269 RTMemFree(pu8Data);
2270 }
2271 }
2272 }
2273 else
2274 {
2275 /* No image. */
2276 *ppu8Data = NULL;
2277 *pcbData = 0;
2278 *pu32Width = 0;
2279 *pu32Height = 0;
2280 rc = VINF_SUCCESS;
2281 }
2282 }
2283 else
2284 {
2285 rc = VERR_INVALID_PARAMETER;
2286 }
2287 pDisplay->vbvaUnlock();
2288 return rc;
2289}
2290
2291static int displayTakeScreenshot(PVM pVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId, BYTE *address, ULONG width, ULONG height)
2292{
2293 uint8_t *pu8Data = NULL;
2294 size_t cbData = 0;
2295 uint32_t cx = 0;
2296 uint32_t cy = 0;
2297 int vrc = VINF_SUCCESS;
2298
2299 int cRetries = 5;
2300
2301 while (cRetries-- > 0)
2302 {
2303 /* Note! Not sure if the priority call is such a good idea here, but
2304 it would be nice to have an accurate screenshot for the bug
2305 report if the VM deadlocks. */
2306 vrc = VMR3ReqPriorityCallWait(pVM, VMCPUID_ANY, (PFNRT)Display::displayTakeScreenshotEMT, 6,
2307 pDisplay, aScreenId, &pu8Data, &cbData, &cx, &cy);
2308 if (vrc != VERR_TRY_AGAIN)
2309 {
2310 break;
2311 }
2312
2313 RTThreadSleep(10);
2314 }
2315
2316 if (RT_SUCCESS(vrc) && pu8Data)
2317 {
2318 if (cx == width && cy == height)
2319 {
2320 /* No scaling required. */
2321 memcpy(address, pu8Data, cbData);
2322 }
2323 else
2324 {
2325 /* Scale. */
2326 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2327
2328 uint8_t *dst = address;
2329 uint8_t *src = pu8Data;
2330 int dstW = width;
2331 int dstH = height;
2332 int srcW = cx;
2333 int srcH = cy;
2334 int iDeltaLine = cx * 4;
2335
2336 BitmapScale32(dst,
2337 dstW, dstH,
2338 src,
2339 iDeltaLine,
2340 srcW, srcH);
2341 }
2342
2343 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2344 {
2345 /* This can be called from any thread. */
2346 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pu8Data);
2347 }
2348 else
2349 {
2350 RTMemFree(pu8Data);
2351 }
2352 }
2353
2354 return vrc;
2355}
2356
2357STDMETHODIMP Display::TakeScreenShot(ULONG aScreenId, BYTE *address, ULONG width, ULONG height)
2358{
2359 /// @todo (r=dmik) this function may take too long to complete if the VM
2360 // is doing something like saving state right now. Which, in case if it
2361 // is called on the GUI thread, will make it unresponsive. We should
2362 // check the machine state here (by enclosing the check and VMRequCall
2363 // within the Console lock to make it atomic).
2364
2365 LogRelFlowFunc(("address=%p, width=%d, height=%d\n",
2366 address, width, height));
2367
2368 CheckComArgNotNull(address);
2369 CheckComArgExpr(width, width != 0);
2370 CheckComArgExpr(height, height != 0);
2371
2372 /* Do not allow too large screenshots. This also filters out negative
2373 * values passed as either 'width' or 'height'.
2374 */
2375 CheckComArgExpr(width, width <= 32767);
2376 CheckComArgExpr(height, height <= 32767);
2377
2378 AutoCaller autoCaller(this);
2379 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2380
2381 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2382
2383 CHECK_CONSOLE_DRV(mpDrv);
2384
2385 Console::SafeVMPtr pVM(mParent);
2386 if (FAILED(pVM.rc())) return pVM.rc();
2387
2388 HRESULT rc = S_OK;
2389
2390 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
2391
2392 /* Release lock because other thread (EMT) is called and it may initiate a resize
2393 * which also needs lock.
2394 *
2395 * This method does not need the lock anymore.
2396 */
2397 alock.release();
2398
2399 int vrc = displayTakeScreenshot(pVM, this, mpDrv, aScreenId, address, width, height);
2400
2401 if (vrc == VERR_NOT_IMPLEMENTED)
2402 rc = setError(E_NOTIMPL,
2403 tr("This feature is not implemented"));
2404 else if (vrc == VERR_TRY_AGAIN)
2405 rc = setError(E_UNEXPECTED,
2406 tr("This feature is not available at this time"));
2407 else if (RT_FAILURE(vrc))
2408 rc = setError(VBOX_E_IPRT_ERROR,
2409 tr("Could not take a screenshot (%Rrc)"), vrc);
2410
2411 LogRelFlowFunc(("rc=%08X\n", rc));
2412 return rc;
2413}
2414
2415STDMETHODIMP Display::TakeScreenShotToArray(ULONG aScreenId, ULONG width, ULONG height,
2416 ComSafeArrayOut(BYTE, aScreenData))
2417{
2418 LogRelFlowFunc(("width=%d, height=%d\n", width, height));
2419
2420 CheckComArgOutSafeArrayPointerValid(aScreenData);
2421 CheckComArgExpr(width, width != 0);
2422 CheckComArgExpr(height, height != 0);
2423
2424 /* Do not allow too large screenshots. This also filters out negative
2425 * values passed as either 'width' or 'height'.
2426 */
2427 CheckComArgExpr(width, width <= 32767);
2428 CheckComArgExpr(height, height <= 32767);
2429
2430 AutoCaller autoCaller(this);
2431 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2432
2433 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2434
2435 CHECK_CONSOLE_DRV(mpDrv);
2436
2437 Console::SafeVMPtr pVM(mParent);
2438 if (FAILED(pVM.rc())) return pVM.rc();
2439
2440 HRESULT rc = S_OK;
2441
2442 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
2443
2444 /* Release lock because other thread (EMT) is called and it may initiate a resize
2445 * which also needs lock.
2446 *
2447 * This method does not need the lock anymore.
2448 */
2449 alock.release();
2450
2451 size_t cbData = width * 4 * height;
2452 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
2453
2454 if (!pu8Data)
2455 return E_OUTOFMEMORY;
2456
2457 int vrc = displayTakeScreenshot(pVM, this, mpDrv, aScreenId, pu8Data, width, height);
2458
2459 if (RT_SUCCESS(vrc))
2460 {
2461 /* Convert pixels to format expected by the API caller: [0] R, [1] G, [2] B, [3] A. */
2462 uint8_t *pu8 = pu8Data;
2463 unsigned cPixels = width * height;
2464 while (cPixels)
2465 {
2466 uint8_t u8 = pu8[0];
2467 pu8[0] = pu8[2];
2468 pu8[2] = u8;
2469 pu8[3] = 0xff;
2470 cPixels--;
2471 pu8 += 4;
2472 }
2473
2474 com::SafeArray<BYTE> screenData(cbData);
2475 screenData.initFrom(pu8Data, cbData);
2476 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
2477 }
2478 else if (vrc == VERR_NOT_IMPLEMENTED)
2479 rc = setError(E_NOTIMPL,
2480 tr("This feature is not implemented"));
2481 else
2482 rc = setError(VBOX_E_IPRT_ERROR,
2483 tr("Could not take a screenshot (%Rrc)"), vrc);
2484
2485 RTMemFree(pu8Data);
2486
2487 LogRelFlowFunc(("rc=%08X\n", rc));
2488 return rc;
2489}
2490
2491STDMETHODIMP Display::TakeScreenShotPNGToArray(ULONG aScreenId, ULONG width, ULONG height,
2492 ComSafeArrayOut(BYTE, aScreenData))
2493{
2494 LogRelFlowFunc(("width=%d, height=%d\n", width, height));
2495
2496 CheckComArgOutSafeArrayPointerValid(aScreenData);
2497 CheckComArgExpr(width, width != 0);
2498 CheckComArgExpr(height, height != 0);
2499
2500 /* Do not allow too large screenshots. This also filters out negative
2501 * values passed as either 'width' or 'height'.
2502 */
2503 CheckComArgExpr(width, width <= 32767);
2504 CheckComArgExpr(height, height <= 32767);
2505
2506 AutoCaller autoCaller(this);
2507 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2508
2509 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2510
2511 CHECK_CONSOLE_DRV(mpDrv);
2512
2513 Console::SafeVMPtr pVM(mParent);
2514 if (FAILED(pVM.rc())) return pVM.rc();
2515
2516 HRESULT rc = S_OK;
2517
2518 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
2519
2520 /* Release lock because other thread (EMT) is called and it may initiate a resize
2521 * which also needs lock.
2522 *
2523 * This method does not need the lock anymore.
2524 */
2525 alock.release();
2526
2527 size_t cbData = width * 4 * height;
2528 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
2529
2530 if (!pu8Data)
2531 return E_OUTOFMEMORY;
2532
2533 int vrc = displayTakeScreenshot(pVM, this, mpDrv, aScreenId, pu8Data, width, height);
2534
2535 if (RT_SUCCESS(vrc))
2536 {
2537 uint8_t *pu8PNG = NULL;
2538 uint32_t cbPNG = 0;
2539 uint32_t cxPNG = 0;
2540 uint32_t cyPNG = 0;
2541
2542 vrc = DisplayMakePNG(pu8Data, width, height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2543 if (RT_SUCCESS(vrc))
2544 {
2545 com::SafeArray<BYTE> screenData(cbPNG);
2546 screenData.initFrom(pu8PNG, cbPNG);
2547 if (pu8PNG)
2548 RTMemFree(pu8PNG);
2549
2550 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
2551 }
2552 else
2553 {
2554 if (pu8PNG)
2555 RTMemFree(pu8PNG);
2556 rc = setError(VBOX_E_IPRT_ERROR,
2557 tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2558 }
2559 }
2560 else if (vrc == VERR_NOT_IMPLEMENTED)
2561 rc = setError(E_NOTIMPL,
2562 tr("This feature is not implemented"));
2563 else
2564 rc = setError(VBOX_E_IPRT_ERROR,
2565 tr("Could not take a screenshot (%Rrc)"), vrc);
2566
2567 RTMemFree(pu8Data);
2568
2569 LogRelFlowFunc(("rc=%08X\n", rc));
2570 return rc;
2571}
2572
2573
2574int Display::drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address, ULONG x, ULONG y, ULONG width, ULONG height)
2575{
2576 int rc = VINF_SUCCESS;
2577 pDisplay->vbvaLock();
2578
2579 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2580
2581 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2582 {
2583 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
2584 {
2585 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2586 }
2587 }
2588 else if (aScreenId < pDisplay->mcMonitors)
2589 {
2590 /* Copy the bitmap to the guest VRAM. */
2591 const uint8_t *pu8Src = address;
2592 int32_t xSrc = 0;
2593 int32_t ySrc = 0;
2594 uint32_t u32SrcWidth = width;
2595 uint32_t u32SrcHeight = height;
2596 uint32_t u32SrcLineSize = width * 4;
2597 uint32_t u32SrcBitsPerPixel = 32;
2598
2599 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2600 int32_t xDst = x;
2601 int32_t yDst = y;
2602 uint32_t u32DstWidth = pFBInfo->w;
2603 uint32_t u32DstHeight = pFBInfo->h;
2604 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2605 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2606
2607 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2608 width, height,
2609 pu8Src,
2610 xSrc, ySrc,
2611 u32SrcWidth, u32SrcHeight,
2612 u32SrcLineSize, u32SrcBitsPerPixel,
2613 pu8Dst,
2614 xDst, yDst,
2615 u32DstWidth, u32DstHeight,
2616 u32DstLineSize, u32DstBitsPerPixel);
2617 if (RT_SUCCESS(rc))
2618 {
2619 if (!pFBInfo->pFramebuffer.isNull())
2620 {
2621 /* Update the changed screen area. When framebuffer uses VRAM directly, just notify
2622 * it to update. And for default format, render the guest VRAM to framebuffer.
2623 */
2624 if ( pFBInfo->fDefaultFormat
2625 && !(pFBInfo->fDisabled))
2626 {
2627 address = NULL;
2628 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2629 if (SUCCEEDED(hrc) && address != NULL)
2630 {
2631 pu8Src = pFBInfo->pu8FramebufferVRAM;
2632 xSrc = x;
2633 ySrc = y;
2634 u32SrcWidth = pFBInfo->w;
2635 u32SrcHeight = pFBInfo->h;
2636 u32SrcLineSize = pFBInfo->u32LineSize;
2637 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2638
2639 /* Default format is 32 bpp. */
2640 pu8Dst = address;
2641 xDst = xSrc;
2642 yDst = ySrc;
2643 u32DstWidth = u32SrcWidth;
2644 u32DstHeight = u32SrcHeight;
2645 u32DstLineSize = u32DstWidth * 4;
2646 u32DstBitsPerPixel = 32;
2647
2648 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2649 width, height,
2650 pu8Src,
2651 xSrc, ySrc,
2652 u32SrcWidth, u32SrcHeight,
2653 u32SrcLineSize, u32SrcBitsPerPixel,
2654 pu8Dst,
2655 xDst, yDst,
2656 u32DstWidth, u32DstHeight,
2657 u32DstLineSize, u32DstBitsPerPixel);
2658 }
2659 }
2660
2661 pDisplay->handleDisplayUpdate(aScreenId, x, y, width, height);
2662 }
2663 }
2664 }
2665 else
2666 {
2667 rc = VERR_INVALID_PARAMETER;
2668 }
2669
2670 if (RT_SUCCESS(rc) && pDisplay->maFramebuffers[aScreenId].u32ResizeStatus == ResizeStatus_Void)
2671 pDisplay->mParent->consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
2672
2673 pDisplay->vbvaUnlock();
2674 return rc;
2675}
2676
2677STDMETHODIMP Display::DrawToScreen (ULONG aScreenId, BYTE *address, ULONG x, ULONG y,
2678 ULONG width, ULONG height)
2679{
2680 /// @todo (r=dmik) this function may take too long to complete if the VM
2681 // is doing something like saving state right now. Which, in case if it
2682 // is called on the GUI thread, will make it unresponsive. We should
2683 // check the machine state here (by enclosing the check and VMRequCall
2684 // within the Console lock to make it atomic).
2685
2686 LogRelFlowFunc (("address=%p, x=%d, y=%d, width=%d, height=%d\n",
2687 (void *)address, x, y, width, height));
2688
2689 CheckComArgNotNull(address);
2690 CheckComArgExpr(width, width != 0);
2691 CheckComArgExpr(height, height != 0);
2692
2693 AutoCaller autoCaller(this);
2694 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2695
2696 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2697
2698 CHECK_CONSOLE_DRV (mpDrv);
2699
2700 Console::SafeVMPtr pVM(mParent);
2701 if (FAILED(pVM.rc())) return pVM.rc();
2702
2703 /* Release lock because the call scheduled on EMT may also try to take it. */
2704 alock.release();
2705
2706 /*
2707 * Again we're lazy and make the graphics device do all the
2708 * dirty conversion work.
2709 */
2710 int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)Display::drawToScreenEMT, 7,
2711 this, aScreenId, address, x, y, width, height);
2712
2713 /*
2714 * If the function returns not supported, we'll have to do all the
2715 * work ourselves using the framebuffer.
2716 */
2717 HRESULT rc = S_OK;
2718 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2719 {
2720 /** @todo implement generic fallback for screen blitting. */
2721 rc = E_NOTIMPL;
2722 }
2723 else if (RT_FAILURE(rcVBox))
2724 rc = setError(VBOX_E_IPRT_ERROR,
2725 tr("Could not draw to the screen (%Rrc)"), rcVBox);
2726//@todo
2727// else
2728// {
2729// /* All ok. Redraw the screen. */
2730// handleDisplayUpdate (x, y, width, height);
2731// }
2732
2733 LogRelFlowFunc (("rc=%08X\n", rc));
2734 return rc;
2735}
2736
2737void Display::InvalidateAndUpdateEMT(Display *pDisplay)
2738{
2739 pDisplay->vbvaLock();
2740 unsigned uScreenId;
2741 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2742 {
2743 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2744
2745 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
2746 {
2747 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort);
2748 }
2749 else
2750 {
2751 if ( !pFBInfo->pFramebuffer.isNull()
2752 && !(pFBInfo->fDisabled))
2753 {
2754 /* Render complete VRAM screen to the framebuffer.
2755 * When framebuffer uses VRAM directly, just notify it to update.
2756 */
2757 if (pFBInfo->fDefaultFormat)
2758 {
2759 BYTE *address = NULL;
2760 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2761 if (SUCCEEDED(hrc) && address != NULL)
2762 {
2763 uint32_t width = pFBInfo->w;
2764 uint32_t height = pFBInfo->h;
2765
2766 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2767 int32_t xSrc = 0;
2768 int32_t ySrc = 0;
2769 uint32_t u32SrcWidth = pFBInfo->w;
2770 uint32_t u32SrcHeight = pFBInfo->h;
2771 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2772 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2773
2774 /* Default format is 32 bpp. */
2775 uint8_t *pu8Dst = address;
2776 int32_t xDst = xSrc;
2777 int32_t yDst = ySrc;
2778 uint32_t u32DstWidth = u32SrcWidth;
2779 uint32_t u32DstHeight = u32SrcHeight;
2780 uint32_t u32DstLineSize = u32DstWidth * 4;
2781 uint32_t u32DstBitsPerPixel = 32;
2782
2783 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2784 width, height,
2785 pu8Src,
2786 xSrc, ySrc,
2787 u32SrcWidth, u32SrcHeight,
2788 u32SrcLineSize, u32SrcBitsPerPixel,
2789 pu8Dst,
2790 xDst, yDst,
2791 u32DstWidth, u32DstHeight,
2792 u32DstLineSize, u32DstBitsPerPixel);
2793 }
2794 }
2795
2796 pDisplay->handleDisplayUpdate (uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2797 }
2798 }
2799 }
2800 pDisplay->vbvaUnlock();
2801}
2802
2803/**
2804 * Does a full invalidation of the VM display and instructs the VM
2805 * to update it immediately.
2806 *
2807 * @returns COM status code
2808 */
2809STDMETHODIMP Display::InvalidateAndUpdate()
2810{
2811 LogRelFlowFunc(("\n"));
2812
2813 AutoCaller autoCaller(this);
2814 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2815
2816 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2817
2818 CHECK_CONSOLE_DRV (mpDrv);
2819
2820 Console::SafeVMPtr pVM(mParent);
2821 if (FAILED(pVM.rc())) return pVM.rc();
2822
2823 HRESULT rc = S_OK;
2824
2825 LogRelFlowFunc (("Sending DPYUPDATE request\n"));
2826
2827 /* Have to release the lock when calling EMT. */
2828 alock.release();
2829
2830 /* pdm.h says that this has to be called from the EMT thread */
2831 int rcVBox = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY, (PFNRT)Display::InvalidateAndUpdateEMT,
2832 1, this);
2833 alock.acquire();
2834
2835 if (RT_FAILURE(rcVBox))
2836 rc = setError(VBOX_E_IPRT_ERROR,
2837 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2838
2839 LogRelFlowFunc (("rc=%08X\n", rc));
2840 return rc;
2841}
2842
2843/**
2844 * Notification that the framebuffer has completed the
2845 * asynchronous resize processing
2846 *
2847 * @returns COM status code
2848 */
2849STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
2850{
2851 LogRelFlowFunc (("\n"));
2852
2853 /// @todo (dmik) can we AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); here?
2854 // This will require general code review and may add some details.
2855 // In particular, we may want to check whether EMT is really waiting for
2856 // this notification, etc. It might be also good to obey the caller to make
2857 // sure this method is not called from more than one thread at a time
2858 // (and therefore don't use Display lock at all here to save some
2859 // milliseconds).
2860 AutoCaller autoCaller(this);
2861 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2862
2863 /* this is only valid for external framebuffers */
2864 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
2865 return setError(VBOX_E_NOT_SUPPORTED,
2866 tr("Resize completed notification is valid only for external framebuffers"));
2867
2868 /* Set the flag indicating that the resize has completed and display
2869 * data need to be updated. */
2870 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
2871 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
2872 AssertRelease(f);NOREF(f);
2873
2874 return S_OK;
2875}
2876
2877STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
2878{
2879#ifdef VBOX_WITH_VIDEOHWACCEL
2880 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsynch(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
2881 return S_OK;
2882#else
2883 return E_NOTIMPL;
2884#endif
2885}
2886
2887STDMETHODIMP Display::ViewportChanged(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
2888{
2889#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2890 BOOL is3denabled;
2891 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2892
2893 if (is3denabled)
2894 {
2895 VBOXHGCMSVCPARM aParms[5];
2896
2897 aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
2898 aParms[0].u.uint32 = aScreenId;
2899
2900 aParms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
2901 aParms[1].u.uint32 = x;
2902
2903 aParms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
2904 aParms[2].u.uint32 = y;
2905
2906
2907 aParms[3].type = VBOX_HGCM_SVC_PARM_32BIT;
2908 aParms[3].u.uint32 = width;
2909
2910 aParms[4].type = VBOX_HGCM_SVC_PARM_32BIT;
2911 aParms[4].u.uint32 = height;
2912
2913 VMMDev *pVMMDev = mParent->getVMMDev();
2914
2915 if (pVMMDev)
2916 pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_VIEWPORT_CHANGED, SHCRGL_CPARMS_VIEWPORT_CHANGED, aParms);
2917 }
2918#endif /* VBOX_WITH_CROGL && VBOX_WITH_HGCM */
2919 return S_OK;
2920}
2921
2922// private methods
2923/////////////////////////////////////////////////////////////////////////////
2924
2925/**
2926 * Helper to update the display information from the framebuffer.
2927 *
2928 * @thread EMT
2929 */
2930void Display::updateDisplayData(void)
2931{
2932 LogRelFlowFunc (("\n"));
2933
2934 /* the driver might not have been constructed yet */
2935 if (!mpDrv)
2936 return;
2937
2938#if DEBUG
2939 /*
2940 * Sanity check. Note that this method may be called on EMT after Console
2941 * has started the power down procedure (but before our #drvDestruct() is
2942 * called, in which case pVM will already be NULL but mpDrv will not). Since
2943 * we don't really need pVM to proceed, we avoid this check in the release
2944 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
2945 * time-critical method.
2946 */
2947 Console::SafeVMPtrQuiet pVM (mParent);
2948 if (pVM.isOk())
2949 VM_ASSERT_EMT (pVM.raw());
2950#endif
2951
2952 /* The method is only relevant to the primary framebuffer. */
2953 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
2954
2955 if (pFramebuffer)
2956 {
2957 HRESULT rc;
2958 BYTE *address = 0;
2959 rc = pFramebuffer->COMGETTER(Address) (&address);
2960 AssertComRC (rc);
2961 ULONG bytesPerLine = 0;
2962 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
2963 AssertComRC (rc);
2964 ULONG bitsPerPixel = 0;
2965 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
2966 AssertComRC (rc);
2967 ULONG width = 0;
2968 rc = pFramebuffer->COMGETTER(Width) (&width);
2969 AssertComRC (rc);
2970 ULONG height = 0;
2971 rc = pFramebuffer->COMGETTER(Height) (&height);
2972 AssertComRC (rc);
2973
2974 mpDrv->IConnector.pu8Data = (uint8_t *) address;
2975 mpDrv->IConnector.cbScanline = bytesPerLine;
2976 mpDrv->IConnector.cBits = bitsPerPixel;
2977 mpDrv->IConnector.cx = width;
2978 mpDrv->IConnector.cy = height;
2979 }
2980 else
2981 {
2982 /* black hole */
2983 mpDrv->IConnector.pu8Data = NULL;
2984 mpDrv->IConnector.cbScanline = 0;
2985 mpDrv->IConnector.cBits = 0;
2986 mpDrv->IConnector.cx = 0;
2987 mpDrv->IConnector.cy = 0;
2988 }
2989 LogRelFlowFunc (("leave\n"));
2990}
2991
2992#ifdef VBOX_WITH_CRHGSMI
2993void Display::setupCrHgsmiData(void)
2994{
2995 VMMDev *pVMMDev = mParent->getVMMDev();
2996 Assert(pVMMDev);
2997 int rc = VERR_GENERAL_FAILURE;
2998 if (pVMMDev)
2999 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
3000
3001 if (RT_SUCCESS(rc))
3002 {
3003 Assert(mhCrOglSvc);
3004 /* setup command completion callback */
3005 VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_COMPLETION Completion;
3006 Completion.Hdr.enmType = VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION;
3007 Completion.Hdr.cbCmd = sizeof (Completion);
3008 Completion.hCompletion = mpDrv->pVBVACallbacks;
3009 Completion.pfnCompletion = mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync;
3010
3011 VBOXHGCMSVCPARM parm;
3012 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3013 parm.u.pointer.addr = &Completion;
3014 parm.u.pointer.size = 0;
3015
3016 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_CRHGSMI_CTL, 1, &parm);
3017 if (RT_SUCCESS(rc))
3018 return;
3019
3020 AssertMsgFailed(("VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION failed rc %d", rc));
3021 }
3022
3023 mhCrOglSvc = NULL;
3024}
3025
3026void Display::destructCrHgsmiData(void)
3027{
3028 mhCrOglSvc = NULL;
3029}
3030#endif
3031
3032/**
3033 * Changes the current frame buffer. Called on EMT to avoid both
3034 * race conditions and excessive locking.
3035 *
3036 * @note locks this object for writing
3037 * @thread EMT
3038 */
3039/* static */
3040DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
3041 unsigned uScreenId)
3042{
3043 LogRelFlowFunc (("uScreenId = %d\n", uScreenId));
3044
3045 AssertReturn(that, VERR_INVALID_PARAMETER);
3046 AssertReturn(uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
3047
3048 AutoCaller autoCaller(that);
3049 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3050
3051 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
3052
3053 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
3054 pDisplayFBInfo->pFramebuffer = aFB;
3055
3056 that->mParent->consoleVRDPServer()->SendResize ();
3057
3058 /* The driver might not have been constructed yet */
3059 if (that->mpDrv)
3060 {
3061 /* Setup the new framebuffer, the resize will lead to an updateDisplayData call. */
3062 DISPLAYFBINFO *pFBInfo = &that->maFramebuffers[uScreenId];
3063
3064#if defined(VBOX_WITH_CROGL)
3065 /* Release the lock, because SHCRGL_HOST_FN_SCREEN_CHANGED will read current framebuffer */
3066 {
3067 BOOL is3denabled;
3068 that->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3069
3070 if (is3denabled)
3071 {
3072 alock.release();
3073 }
3074 }
3075#endif
3076
3077 if (pFBInfo->fVBVAEnabled && pFBInfo->pu8FramebufferVRAM)
3078 {
3079 /* This display in VBVA mode. Resize it to the last guest resolution,
3080 * if it has been reported.
3081 */
3082 that->handleDisplayResize(uScreenId, pFBInfo->u16BitsPerPixel,
3083 pFBInfo->pu8FramebufferVRAM,
3084 pFBInfo->u32LineSize,
3085 pFBInfo->w,
3086 pFBInfo->h,
3087 pFBInfo->flags);
3088 }
3089 else if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3090 {
3091 /* VGA device mode, only for the primary screen. */
3092 that->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, that->mLastBitsPerPixel,
3093 that->mLastAddress,
3094 that->mLastBytesPerLine,
3095 that->mLastWidth,
3096 that->mLastHeight,
3097 that->mLastFlags);
3098 }
3099 }
3100
3101 LogRelFlowFunc (("leave\n"));
3102 return VINF_SUCCESS;
3103}
3104
3105/**
3106 * Handle display resize event issued by the VGA device for the primary screen.
3107 *
3108 * @see PDMIDISPLAYCONNECTOR::pfnResize
3109 */
3110DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
3111 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
3112{
3113 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3114
3115 LogRelFlowFunc (("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
3116 bpp, pvVRAM, cbLine, cx, cy));
3117
3118 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, VBVA_SCREEN_F_ACTIVE);
3119}
3120
3121/**
3122 * Handle display update.
3123 *
3124 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
3125 */
3126DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
3127 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
3128{
3129 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3130
3131#ifdef DEBUG_sunlover
3132 LogFlowFunc (("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
3133 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
3134#endif /* DEBUG_sunlover */
3135
3136 /* This call does update regardless of VBVA status.
3137 * But in VBVA mode this is called only as result of
3138 * pfnUpdateDisplayAll in the VGA device.
3139 */
3140
3141 pDrv->pDisplay->handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
3142}
3143
3144/**
3145 * Periodic display refresh callback.
3146 *
3147 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
3148 */
3149DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
3150{
3151 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3152
3153#ifdef DEBUG_sunlover
3154 STAM_PROFILE_START(&StatDisplayRefresh, a);
3155#endif /* DEBUG_sunlover */
3156
3157#ifdef DEBUG_sunlover_2
3158 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
3159 pDrv->pDisplay->mfVideoAccelEnabled));
3160#endif /* DEBUG_sunlover_2 */
3161
3162 Display *pDisplay = pDrv->pDisplay;
3163 bool fNoUpdate = false; /* Do not update the display if any of the framebuffers is being resized. */
3164 unsigned uScreenId;
3165
3166 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3167 {
3168 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3169
3170 /* Check the resize status. The status can be checked normally because
3171 * the status affects only the EMT.
3172 */
3173 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
3174
3175 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
3176 {
3177 LogRelFlowFunc (("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
3178 fNoUpdate = true; /* Always set it here, because pfnUpdateDisplayAll can cause a new resize. */
3179 /* The framebuffer was resized and display data need to be updated. */
3180 pDisplay->handleResizeCompletedEMT ();
3181 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
3182 {
3183 /* The resize status could be not Void here because a pending resize is issued. */
3184 continue;
3185 }
3186 /* Continue with normal processing because the status here is ResizeStatus_Void.
3187 * Repaint all displays because VM continued to run during the framebuffer resize.
3188 */
3189 pDisplay->InvalidateAndUpdateEMT(pDisplay);
3190 }
3191 else if (u32ResizeStatus == ResizeStatus_InProgress)
3192 {
3193 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
3194 LogRelFlowFunc (("ResizeStatus_InProcess\n"));
3195 fNoUpdate = true;
3196 continue;
3197 }
3198 }
3199
3200 if (!fNoUpdate)
3201 {
3202 int rc = pDisplay->videoAccelRefreshProcess();
3203
3204 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3205 {
3206 if (rc == VWRN_INVALID_STATE)
3207 {
3208 /* No VBVA do a display update. */
3209 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3210 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3211 {
3212 Assert(pDrv->IConnector.pu8Data);
3213 pDisplay->vbvaLock();
3214 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3215 pDisplay->vbvaUnlock();
3216 }
3217 }
3218
3219 /* Inform the VRDP server that the current display update sequence is
3220 * completed. At this moment the framebuffer memory contains a definite
3221 * image, that is synchronized with the orders already sent to VRDP client.
3222 * The server can now process redraw requests from clients or initial
3223 * fullscreen updates for new clients.
3224 */
3225 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3226 {
3227 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3228
3229 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3230 {
3231 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
3232 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
3233 }
3234 }
3235 }
3236 }
3237
3238#ifdef DEBUG_sunlover
3239 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
3240#endif /* DEBUG_sunlover */
3241#ifdef DEBUG_sunlover_2
3242 LogFlowFunc (("leave\n"));
3243#endif /* DEBUG_sunlover_2 */
3244}
3245
3246/**
3247 * Reset notification
3248 *
3249 * @see PDMIDISPLAYCONNECTOR::pfnReset
3250 */
3251DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3252{
3253 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3254
3255 LogRelFlowFunc (("\n"));
3256
3257 /* Disable VBVA mode. */
3258 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3259}
3260
3261/**
3262 * LFBModeChange notification
3263 *
3264 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3265 */
3266DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3267{
3268 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3269
3270 LogRelFlowFunc (("fEnabled=%d\n", fEnabled));
3271
3272 NOREF(fEnabled);
3273
3274 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3275 /* The LFBModeChange function is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
3276 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
3277}
3278
3279/**
3280 * Adapter information change notification.
3281 *
3282 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3283 */
3284DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
3285{
3286 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3287
3288 if (pvVRAM == NULL)
3289 {
3290 unsigned i;
3291 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
3292 {
3293 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
3294
3295 pFBInfo->u32Offset = 0;
3296 pFBInfo->u32MaxFramebufferSize = 0;
3297 pFBInfo->u32InformationSize = 0;
3298 }
3299 }
3300#ifndef VBOX_WITH_HGSMI
3301 else
3302 {
3303 uint8_t *pu8 = (uint8_t *)pvVRAM;
3304 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3305
3306 // @todo
3307 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3308
3309 VBOXVIDEOINFOHDR *pHdr;
3310
3311 for (;;)
3312 {
3313 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3314 pu8 += sizeof (VBOXVIDEOINFOHDR);
3315
3316 if (pu8 >= pu8End)
3317 {
3318 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
3319 break;
3320 }
3321
3322 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
3323 {
3324 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
3325 {
3326 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
3327 break;
3328 }
3329
3330 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
3331
3332 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
3333 {
3334 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
3335 break;
3336 }
3337
3338 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
3339
3340 pFBInfo->u32Offset = pDisplay->u32Offset;
3341 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
3342 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
3343
3344 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));
3345 }
3346 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
3347 {
3348 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
3349 {
3350 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
3351 break;
3352 }
3353
3354 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
3355
3356 switch (pConf32->u32Index)
3357 {
3358 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
3359 {
3360 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
3361 } break;
3362
3363 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
3364 {
3365 /* @todo make configurable. */
3366 pConf32->u32Value = _1M;
3367 } break;
3368
3369 default:
3370 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
3371 }
3372 }
3373 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3374 {
3375 if (pHdr->u16Length != 0)
3376 {
3377 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3378 break;
3379 }
3380
3381 break;
3382 }
3383 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
3384 {
3385 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
3386 }
3387
3388 pu8 += pHdr->u16Length;
3389 }
3390 }
3391#endif /* !VBOX_WITH_HGSMI */
3392}
3393
3394/**
3395 * Display information change notification.
3396 *
3397 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3398 */
3399DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
3400{
3401 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3402
3403 if (uScreenId >= pDrv->pDisplay->mcMonitors)
3404 {
3405 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
3406 return;
3407 }
3408
3409 /* Get the display information structure. */
3410 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
3411
3412 uint8_t *pu8 = (uint8_t *)pvVRAM;
3413 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
3414
3415 // @todo
3416 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
3417
3418 VBOXVIDEOINFOHDR *pHdr;
3419
3420 for (;;)
3421 {
3422 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3423 pu8 += sizeof (VBOXVIDEOINFOHDR);
3424
3425 if (pu8 >= pu8End)
3426 {
3427 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
3428 break;
3429 }
3430
3431 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
3432 {
3433 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
3434 {
3435 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
3436 break;
3437 }
3438
3439 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
3440
3441 pFBInfo->xOrigin = pScreen->xOrigin;
3442 pFBInfo->yOrigin = pScreen->yOrigin;
3443
3444 pFBInfo->w = pScreen->u16Width;
3445 pFBInfo->h = pScreen->u16Height;
3446
3447 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
3448 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
3449
3450 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
3451 {
3452 /* Primary screen resize is initiated by the VGA device. */
3453 if (pFBInfo->fDisabled)
3454 {
3455 pFBInfo->fDisabled = false;
3456 fireGuestMonitorChangedEvent(pDrv->pDisplay->mParent->getEventSource(),
3457 GuestMonitorChangedEventType_Enabled,
3458 uScreenId,
3459 pFBInfo->xOrigin, pFBInfo->yOrigin,
3460 pFBInfo->w, pFBInfo->h);
3461 }
3462
3463 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, VBVA_SCREEN_F_ACTIVE);
3464 }
3465 }
3466 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3467 {
3468 if (pHdr->u16Length != 0)
3469 {
3470 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3471 break;
3472 }
3473
3474 break;
3475 }
3476 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
3477 {
3478 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
3479 {
3480 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
3481 break;
3482 }
3483
3484 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
3485
3486 pFBInfo->pHostEvents = pHostEvents;
3487
3488 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
3489 pHostEvents));
3490 }
3491 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
3492 {
3493 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
3494 {
3495 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
3496 break;
3497 }
3498
3499 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
3500 pu8 += pLink->i32Offset;
3501 }
3502 else
3503 {
3504 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
3505 }
3506
3507 pu8 += pHdr->u16Length;
3508 }
3509}
3510
3511#ifdef VBOX_WITH_VIDEOHWACCEL
3512
3513void Display::handleVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3514{
3515 unsigned id = (unsigned)pCommand->iDisplay;
3516 int rc = VINF_SUCCESS;
3517 if (id < mcMonitors)
3518 {
3519 IFramebuffer *pFramebuffer = maFramebuffers[id].pFramebuffer;
3520#ifdef DEBUG_misha
3521 Assert (pFramebuffer);
3522#endif
3523
3524 if (pFramebuffer != NULL)
3525 {
3526 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3527 if (FAILED(hr))
3528 {
3529 rc = (hr == E_NOTIMPL) ? VERR_NOT_IMPLEMENTED : VERR_GENERAL_FAILURE;
3530 }
3531 }
3532 else
3533 {
3534 rc = VERR_NOT_IMPLEMENTED;
3535 }
3536 }
3537 else
3538 {
3539 rc = VERR_INVALID_PARAMETER;
3540 }
3541
3542 if (RT_FAILURE(rc))
3543 {
3544 /* tell the guest the command is complete */
3545 pCommand->Flags &= (~VBOXVHWACMD_FLAG_HG_ASYNCH);
3546 pCommand->rc = rc;
3547 }
3548}
3549
3550DECLCALLBACK(void) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3551{
3552 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3553
3554 pDrv->pDisplay->handleVHWACommandProcess(pInterface, pCommand);
3555}
3556#endif
3557
3558#ifdef VBOX_WITH_CRHGSMI
3559void Display::handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3560{
3561 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
3562}
3563
3564void Display::handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3565{
3566 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr, result);
3567}
3568
3569void Display::handleCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3570{
3571 int rc = VERR_INVALID_FUNCTION;
3572 VBOXHGCMSVCPARM parm;
3573 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3574 parm.u.pointer.addr = pCmd;
3575 parm.u.pointer.size = cbCmd;
3576
3577 if (mhCrOglSvc)
3578 {
3579 VMMDev *pVMMDev = mParent->getVMMDev();
3580 if (pVMMDev)
3581 {
3582 /* no completion callback is specified with this call,
3583 * the CrOgl code will complete the CrHgsmi command once it processes it */
3584 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
3585 AssertRC(rc);
3586 if (RT_SUCCESS(rc))
3587 return;
3588 }
3589 else
3590 rc = VERR_INVALID_STATE;
3591 }
3592
3593 /* we are here because something went wrong with command processing, complete it */
3594 handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
3595}
3596
3597void Display::handleCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
3598{
3599 int rc = VERR_INVALID_FUNCTION;
3600 VBOXHGCMSVCPARM parm;
3601 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3602 parm.u.pointer.addr = pCtl;
3603 parm.u.pointer.size = cbCtl;
3604
3605 if (mhCrOglSvc)
3606 {
3607 VMMDev *pVMMDev = mParent->getVMMDev();
3608 if (pVMMDev)
3609 {
3610 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm, Display::displayCrHgsmiControlCompletion, this);
3611 AssertRC(rc);
3612 if (RT_SUCCESS(rc))
3613 return;
3614 }
3615 else
3616 rc = VERR_INVALID_STATE;
3617 }
3618
3619 /* we are here because something went wrong with command processing, complete it */
3620 handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
3621}
3622
3623
3624DECLCALLBACK(void) Display::displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3625{
3626 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3627
3628 pDrv->pDisplay->handleCrHgsmiCommandProcess(pInterface, pCmd, cbCmd);
3629}
3630
3631DECLCALLBACK(void) Display::displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd, uint32_t cbCmd)
3632{
3633 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3634
3635 pDrv->pDisplay->handleCrHgsmiControlProcess(pInterface, pCmd, cbCmd);
3636}
3637
3638DECLCALLBACK(void) Display::displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3639{
3640 AssertMsgFailed(("not expected!"));
3641 Display *pDisplay = (Display *)pvContext;
3642 pDisplay->handleCrHgsmiCommandCompletion(result, u32Function, pParam);
3643}
3644
3645DECLCALLBACK(void) Display::displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3646{
3647 Display *pDisplay = (Display *)pvContext;
3648 pDisplay->handleCrHgsmiControlCompletion(result, u32Function, pParam);
3649}
3650#endif
3651
3652
3653#ifdef VBOX_WITH_HGSMI
3654DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags)
3655{
3656 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3657
3658 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3659 Display *pThis = pDrv->pDisplay;
3660
3661 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3662 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3663
3664 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3665
3666 return VINF_SUCCESS;
3667}
3668
3669DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3670{
3671 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3672
3673 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3674 Display *pThis = pDrv->pDisplay;
3675
3676 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3677
3678 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3679 {
3680 /* Make sure that the primary screen is visible now.
3681 * The guest can't use VBVA anymore, so only only the VGA device output works.
3682 */
3683 if (pFBInfo->fDisabled)
3684 {
3685 pFBInfo->fDisabled = false;
3686 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
3687 GuestMonitorChangedEventType_Enabled,
3688 uScreenId,
3689 pFBInfo->xOrigin, pFBInfo->yOrigin,
3690 pFBInfo->w, pFBInfo->h);
3691 }
3692 }
3693
3694 pFBInfo->fVBVAEnabled = false;
3695
3696 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3697
3698 pFBInfo->pVBVAHostFlags = NULL;
3699
3700 pFBInfo->u32Offset = 0; /* Not used in HGSMI. */
3701 pFBInfo->u32MaxFramebufferSize = 0; /* Not used in HGSMI. */
3702 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3703
3704 pFBInfo->xOrigin = 0;
3705 pFBInfo->yOrigin = 0;
3706
3707 pFBInfo->w = 0;
3708 pFBInfo->h = 0;
3709
3710 pFBInfo->u16BitsPerPixel = 0;
3711 pFBInfo->pu8FramebufferVRAM = NULL;
3712 pFBInfo->u32LineSize = 0;
3713}
3714
3715DECLCALLBACK(void) Display::displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3716{
3717 LogFlowFunc(("uScreenId %d\n", uScreenId));
3718
3719 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3720 Display *pThis = pDrv->pDisplay;
3721 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3722
3723 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3724 {
3725 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers, pThis->mcMonitors);
3726 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3727 }
3728
3729 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
3730 {
3731 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
3732 {
3733 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
3734 * under display device lock, so thread safe.
3735 */
3736 pFBInfo->cVBVASkipUpdate = 0;
3737 pThis->handleDisplayUpdate(uScreenId, pFBInfo->vbvaSkippedRect.xLeft - pFBInfo->xOrigin,
3738 pFBInfo->vbvaSkippedRect.yTop - pFBInfo->yOrigin,
3739 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
3740 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
3741 }
3742 }
3743 else
3744 {
3745 /* The framebuffer is being resized. */
3746 pFBInfo->cVBVASkipUpdate++;
3747 }
3748}
3749
3750DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, const PVBVACMDHDR pCmd, size_t cbCmd)
3751{
3752 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3753
3754 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3755 Display *pThis = pDrv->pDisplay;
3756 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3757
3758 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3759 {
3760 if (pFBInfo->fDefaultFormat)
3761 {
3762 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3763 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3764 && !pFBInfo->pFramebuffer.isNull()
3765 && !pFBInfo->fDisabled)
3766 {
3767 pDrv->pUpPort->pfnUpdateDisplayRect (pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
3768 }
3769 else if ( !pFBInfo->pFramebuffer.isNull()
3770 && !(pFBInfo->fDisabled))
3771 {
3772 /* Render VRAM content to the framebuffer. */
3773 BYTE *address = NULL;
3774 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
3775 if (SUCCEEDED(hrc) && address != NULL)
3776 {
3777 uint32_t width = pCmd->w;
3778 uint32_t height = pCmd->h;
3779
3780 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3781 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
3782 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
3783 uint32_t u32SrcWidth = pFBInfo->w;
3784 uint32_t u32SrcHeight = pFBInfo->h;
3785 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3786 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3787
3788 uint8_t *pu8Dst = address;
3789 int32_t xDst = xSrc;
3790 int32_t yDst = ySrc;
3791 uint32_t u32DstWidth = u32SrcWidth;
3792 uint32_t u32DstHeight = u32SrcHeight;
3793 uint32_t u32DstLineSize = u32DstWidth * 4;
3794 uint32_t u32DstBitsPerPixel = 32;
3795
3796 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3797 width, height,
3798 pu8Src,
3799 xSrc, ySrc,
3800 u32SrcWidth, u32SrcHeight,
3801 u32SrcLineSize, u32SrcBitsPerPixel,
3802 pu8Dst,
3803 xDst, yDst,
3804 u32DstWidth, u32DstHeight,
3805 u32DstLineSize, u32DstBitsPerPixel);
3806 }
3807 }
3808 }
3809
3810 VBVACMDHDR hdrSaved = *pCmd;
3811
3812 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3813
3814 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3815 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3816
3817 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
3818 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, cbCmd);
3819
3820 *pHdrUnconst = hdrSaved;
3821 }
3822}
3823
3824DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y, uint32_t cx, uint32_t cy)
3825{
3826 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3827
3828 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3829 Display *pThis = pDrv->pDisplay;
3830 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3831
3832 /* @todo handleFramebufferUpdate (uScreenId,
3833 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3834 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3835 * cx, cy);
3836 */
3837 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3838 {
3839 pThis->handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3840 }
3841 else
3842 {
3843 /* Save the updated rectangle. */
3844 int32_t xRight = x + cx;
3845 int32_t yBottom = y + cy;
3846
3847 if (pFBInfo->cVBVASkipUpdate == 1)
3848 {
3849 pFBInfo->vbvaSkippedRect.xLeft = x;
3850 pFBInfo->vbvaSkippedRect.yTop = y;
3851 pFBInfo->vbvaSkippedRect.xRight = xRight;
3852 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3853 }
3854 else
3855 {
3856 if (pFBInfo->vbvaSkippedRect.xLeft > x)
3857 {
3858 pFBInfo->vbvaSkippedRect.xLeft = x;
3859 }
3860 if (pFBInfo->vbvaSkippedRect.yTop > y)
3861 {
3862 pFBInfo->vbvaSkippedRect.yTop = y;
3863 }
3864 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
3865 {
3866 pFBInfo->vbvaSkippedRect.xRight = xRight;
3867 }
3868 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
3869 {
3870 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3871 }
3872 }
3873 }
3874}
3875
3876#ifdef DEBUG_sunlover
3877static void logVBVAResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
3878{
3879 LogRel(("displayVBVAResize: [%d] %s\n"
3880 " pView->u32ViewIndex %d\n"
3881 " pView->u32ViewOffset 0x%08X\n"
3882 " pView->u32ViewSize 0x%08X\n"
3883 " pView->u32MaxScreenSize 0x%08X\n"
3884 " pScreen->i32OriginX %d\n"
3885 " pScreen->i32OriginY %d\n"
3886 " pScreen->u32StartOffset 0x%08X\n"
3887 " pScreen->u32LineSize 0x%08X\n"
3888 " pScreen->u32Width %d\n"
3889 " pScreen->u32Height %d\n"
3890 " pScreen->u16BitsPerPixel %d\n"
3891 " pScreen->u16Flags 0x%04X\n"
3892 " pFBInfo->u32Offset 0x%08X\n"
3893 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
3894 " pFBInfo->u32InformationSize 0x%08X\n"
3895 " pFBInfo->fDisabled %d\n"
3896 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
3897 " pFBInfo->u16BitsPerPixel %d\n"
3898 " pFBInfo->pu8FramebufferVRAM %p\n"
3899 " pFBInfo->u32LineSize 0x%08X\n"
3900 " pFBInfo->flags 0x%04X\n"
3901 " pFBInfo->pHostEvents %p\n"
3902 " pFBInfo->u32ResizeStatus %d\n"
3903 " pFBInfo->fDefaultFormat %d\n"
3904 " dirtyRect %d-%d %d-%d\n"
3905 " pFBInfo->pendingResize.fPending %d\n"
3906 " pFBInfo->pendingResize.pixelFormat %d\n"
3907 " pFBInfo->pendingResize.pvVRAM %p\n"
3908 " pFBInfo->pendingResize.bpp %d\n"
3909 " pFBInfo->pendingResize.cbLine 0x%08X\n"
3910 " pFBInfo->pendingResize.w,h %dx%d\n"
3911 " pFBInfo->pendingResize.flags 0x%04X\n"
3912 " pFBInfo->fVBVAEnabled %d\n"
3913 " pFBInfo->cVBVASkipUpdate %d\n"
3914 " pFBInfo->vbvaSkippedRect %d-%d %d-%d\n"
3915 " pFBInfo->pVBVAHostFlags %p\n"
3916 "",
3917 pScreen->u32ViewIndex,
3918 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
3919 pView->u32ViewIndex,
3920 pView->u32ViewOffset,
3921 pView->u32ViewSize,
3922 pView->u32MaxScreenSize,
3923 pScreen->i32OriginX,
3924 pScreen->i32OriginY,
3925 pScreen->u32StartOffset,
3926 pScreen->u32LineSize,
3927 pScreen->u32Width,
3928 pScreen->u32Height,
3929 pScreen->u16BitsPerPixel,
3930 pScreen->u16Flags,
3931 pFBInfo->u32Offset,
3932 pFBInfo->u32MaxFramebufferSize,
3933 pFBInfo->u32InformationSize,
3934 pFBInfo->fDisabled,
3935 pFBInfo->xOrigin,
3936 pFBInfo->yOrigin,
3937 pFBInfo->w,
3938 pFBInfo->h,
3939 pFBInfo->u16BitsPerPixel,
3940 pFBInfo->pu8FramebufferVRAM,
3941 pFBInfo->u32LineSize,
3942 pFBInfo->flags,
3943 pFBInfo->pHostEvents,
3944 pFBInfo->u32ResizeStatus,
3945 pFBInfo->fDefaultFormat,
3946 pFBInfo->dirtyRect.xLeft,
3947 pFBInfo->dirtyRect.xRight,
3948 pFBInfo->dirtyRect.yTop,
3949 pFBInfo->dirtyRect.yBottom,
3950 pFBInfo->pendingResize.fPending,
3951 pFBInfo->pendingResize.pixelFormat,
3952 pFBInfo->pendingResize.pvVRAM,
3953 pFBInfo->pendingResize.bpp,
3954 pFBInfo->pendingResize.cbLine,
3955 pFBInfo->pendingResize.w,
3956 pFBInfo->pendingResize.h,
3957 pFBInfo->pendingResize.flags,
3958 pFBInfo->fVBVAEnabled,
3959 pFBInfo->cVBVASkipUpdate,
3960 pFBInfo->vbvaSkippedRect.xLeft,
3961 pFBInfo->vbvaSkippedRect.yTop,
3962 pFBInfo->vbvaSkippedRect.xRight,
3963 pFBInfo->vbvaSkippedRect.yBottom,
3964 pFBInfo->pVBVAHostFlags
3965 ));
3966}
3967#endif /* DEBUG_sunlover */
3968
3969DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
3970{
3971 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3972
3973 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3974 Display *pThis = pDrv->pDisplay;
3975
3976 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
3977
3978 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
3979 {
3980 pFBInfo->fDisabled = true;
3981 pFBInfo->flags = pScreen->u16Flags;
3982
3983 /* Temporary: ask framebuffer to resize using a default format. The framebuffer will be black. */
3984 pThis->handleDisplayResize(pScreen->u32ViewIndex, 0,
3985 (uint8_t *)NULL,
3986 pScreen->u32LineSize, pScreen->u32Width,
3987 pScreen->u32Height, pScreen->u16Flags);
3988
3989 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
3990 GuestMonitorChangedEventType_Disabled,
3991 pScreen->u32ViewIndex,
3992 0, 0, 0, 0);
3993 return VINF_SUCCESS;
3994 }
3995
3996 /* If display was disabled or there is no framebuffer, a resize will be required,
3997 * because the framebuffer was/will be changed.
3998 */
3999 bool fResize = pFBInfo->fDisabled || pFBInfo->pFramebuffer.isNull();
4000
4001 if (pFBInfo->fDisabled)
4002 {
4003 pFBInfo->fDisabled = false;
4004 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4005 GuestMonitorChangedEventType_Enabled,
4006 pScreen->u32ViewIndex,
4007 pScreen->i32OriginX, pScreen->i32OriginY,
4008 pScreen->u32Width, pScreen->u32Height);
4009 /* Continue to update pFBInfo. */
4010 }
4011
4012 /* Check if this is a real resize or a notification about the screen origin.
4013 * The guest uses this VBVAResize call for both.
4014 */
4015 fResize = fResize
4016 || pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
4017 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
4018 || pFBInfo->u32LineSize != pScreen->u32LineSize
4019 || pFBInfo->w != pScreen->u32Width
4020 || pFBInfo->h != pScreen->u32Height;
4021
4022 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
4023 || pFBInfo->yOrigin != pScreen->i32OriginY;
4024
4025 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
4026 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
4027 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
4028
4029 pFBInfo->xOrigin = pScreen->i32OriginX;
4030 pFBInfo->yOrigin = pScreen->i32OriginY;
4031
4032 pFBInfo->w = pScreen->u32Width;
4033 pFBInfo->h = pScreen->u32Height;
4034
4035 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
4036 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
4037 pFBInfo->u32LineSize = pScreen->u32LineSize;
4038
4039 pFBInfo->flags = pScreen->u16Flags;
4040
4041 if (fNewOrigin)
4042 {
4043 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4044 GuestMonitorChangedEventType_NewOrigin,
4045 pScreen->u32ViewIndex,
4046 pScreen->i32OriginX, pScreen->i32OriginY,
4047 0, 0);
4048 }
4049
4050#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4051 if (fNewOrigin && !fResize)
4052 {
4053 BOOL is3denabled;
4054 pThis->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
4055
4056 if (is3denabled)
4057 {
4058 VBOXHGCMSVCPARM parm;
4059
4060 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
4061 parm.u.uint32 = pScreen->u32ViewIndex;
4062
4063 VMMDev *pVMMDev = pThis->mParent->getVMMDev();
4064
4065 if (pVMMDev)
4066 pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED, SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
4067 }
4068 }
4069#endif /* VBOX_WITH_CROGL */
4070
4071 if (!fResize)
4072 {
4073 /* No parameters of the framebuffer have actually changed. */
4074 if (fNewOrigin)
4075 {
4076 /* VRDP server still need this notification. */
4077 LogRelFlowFunc (("Calling VRDP\n"));
4078 pThis->mParent->consoleVRDPServer()->SendResize();
4079 }
4080 return VINF_SUCCESS;
4081 }
4082
4083 if (pFBInfo->pFramebuffer.isNull())
4084 {
4085 /* If no framebuffer, the resize will be done later when a new framebuffer will be set in changeFramebuffer. */
4086 return VINF_SUCCESS;
4087 }
4088
4089 /* If the framebuffer already set for the screen, do a regular resize. */
4090 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
4091 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
4092 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
4093}
4094
4095DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
4096 uint32_t xHot, uint32_t yHot,
4097 uint32_t cx, uint32_t cy,
4098 const void *pvShape)
4099{
4100 LogFlowFunc(("\n"));
4101
4102 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4103 Display *pThis = pDrv->pDisplay;
4104
4105 size_t cbShapeSize = 0;
4106
4107 if (pvShape)
4108 {
4109 cbShapeSize = (cx + 7) / 8 * cy; /* size of the AND mask */
4110 cbShapeSize = ((cbShapeSize + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
4111 }
4112 com::SafeArray<BYTE> shapeData(cbShapeSize);
4113
4114 if (pvShape)
4115 ::memcpy(shapeData.raw(), pvShape, cbShapeSize);
4116
4117 /* Tell the console about it */
4118 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
4119 xHot, yHot, cx, cy, ComSafeArrayAsInParam(shapeData));
4120
4121 return VINF_SUCCESS;
4122}
4123#endif /* VBOX_WITH_HGSMI */
4124
4125/**
4126 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4127 */
4128DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
4129{
4130 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
4131 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4132 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
4133 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
4134 return NULL;
4135}
4136
4137
4138/**
4139 * Destruct a display driver instance.
4140 *
4141 * @returns VBox status.
4142 * @param pDrvIns The driver instance data.
4143 */
4144DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
4145{
4146 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4147 LogRelFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
4148 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4149
4150 if (pData->pDisplay)
4151 {
4152 AutoWriteLock displayLock(pData->pDisplay COMMA_LOCKVAL_SRC_POS);
4153#ifdef VBOX_WITH_CRHGSMI
4154 pData->pDisplay->destructCrHgsmiData();
4155#endif
4156 pData->pDisplay->mpDrv = NULL;
4157 pData->pDisplay->mpVMMDev = NULL;
4158 pData->pDisplay->mLastAddress = NULL;
4159 pData->pDisplay->mLastBytesPerLine = 0;
4160 pData->pDisplay->mLastBitsPerPixel = 0,
4161 pData->pDisplay->mLastWidth = 0;
4162 pData->pDisplay->mLastHeight = 0;
4163 }
4164}
4165
4166
4167/**
4168 * Construct a display driver instance.
4169 *
4170 * @copydoc FNPDMDRVCONSTRUCT
4171 */
4172DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
4173{
4174 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4175 LogRelFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
4176 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
4177
4178 /*
4179 * Validate configuration.
4180 */
4181 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
4182 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
4183 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
4184 ("Configuration error: Not possible to attach anything to this driver!\n"),
4185 VERR_PDM_DRVINS_NO_ATTACH);
4186
4187 /*
4188 * Init Interfaces.
4189 */
4190 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
4191
4192 pData->IConnector.pfnResize = Display::displayResizeCallback;
4193 pData->IConnector.pfnUpdateRect = Display::displayUpdateCallback;
4194 pData->IConnector.pfnRefresh = Display::displayRefreshCallback;
4195 pData->IConnector.pfnReset = Display::displayResetCallback;
4196 pData->IConnector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
4197 pData->IConnector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
4198 pData->IConnector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
4199#ifdef VBOX_WITH_VIDEOHWACCEL
4200 pData->IConnector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
4201#endif
4202#ifdef VBOX_WITH_CRHGSMI
4203 pData->IConnector.pfnCrHgsmiCommandProcess = Display::displayCrHgsmiCommandProcess;
4204 pData->IConnector.pfnCrHgsmiControlProcess = Display::displayCrHgsmiControlProcess;
4205#endif
4206#ifdef VBOX_WITH_HGSMI
4207 pData->IConnector.pfnVBVAEnable = Display::displayVBVAEnable;
4208 pData->IConnector.pfnVBVADisable = Display::displayVBVADisable;
4209 pData->IConnector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
4210 pData->IConnector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
4211 pData->IConnector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
4212 pData->IConnector.pfnVBVAResize = Display::displayVBVAResize;
4213 pData->IConnector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
4214#endif
4215
4216
4217 /*
4218 * Get the IDisplayPort interface of the above driver/device.
4219 */
4220 pData->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
4221 if (!pData->pUpPort)
4222 {
4223 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
4224 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4225 }
4226#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
4227 pData->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
4228 if (!pData->pVBVACallbacks)
4229 {
4230 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
4231 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4232 }
4233#endif
4234 /*
4235 * Get the Display object pointer and update the mpDrv member.
4236 */
4237 void *pv;
4238 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
4239 if (RT_FAILURE(rc))
4240 {
4241 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
4242 return rc;
4243 }
4244 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
4245 pData->pDisplay->mpDrv = pData;
4246
4247 /*
4248 * Update our display information according to the framebuffer
4249 */
4250 pData->pDisplay->updateDisplayData();
4251
4252 /*
4253 * Start periodic screen refreshes
4254 */
4255 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
4256
4257#ifdef VBOX_WITH_CRHGSMI
4258 pData->pDisplay->setupCrHgsmiData();
4259#endif
4260
4261 return VINF_SUCCESS;
4262}
4263
4264
4265/**
4266 * Display driver registration record.
4267 */
4268const PDMDRVREG Display::DrvReg =
4269{
4270 /* u32Version */
4271 PDM_DRVREG_VERSION,
4272 /* szName */
4273 "MainDisplay",
4274 /* szRCMod */
4275 "",
4276 /* szR0Mod */
4277 "",
4278 /* pszDescription */
4279 "Main display driver (Main as in the API).",
4280 /* fFlags */
4281 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
4282 /* fClass. */
4283 PDM_DRVREG_CLASS_DISPLAY,
4284 /* cMaxInstances */
4285 ~0U,
4286 /* cbInstance */
4287 sizeof(DRVMAINDISPLAY),
4288 /* pfnConstruct */
4289 Display::drvConstruct,
4290 /* pfnDestruct */
4291 Display::drvDestruct,
4292 /* pfnRelocate */
4293 NULL,
4294 /* pfnIOCtl */
4295 NULL,
4296 /* pfnPowerOn */
4297 NULL,
4298 /* pfnReset */
4299 NULL,
4300 /* pfnSuspend */
4301 NULL,
4302 /* pfnResume */
4303 NULL,
4304 /* pfnAttach */
4305 NULL,
4306 /* pfnDetach */
4307 NULL,
4308 /* pfnPowerOff */
4309 NULL,
4310 /* pfnSoftReset */
4311 NULL,
4312 /* u32EndVersion */
4313 PDM_DRVREG_VERSION
4314};
4315/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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