VirtualBox

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

Last change on this file since 45941 was 45941, checked in by vboxsync, 12 years ago

Main/VPX: some code restructuring to be able to log errors / success during initialization

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 145.7 KB
Line 
1/* $Id: DisplayImpl.cpp 45941 2013-05-08 08:47:02Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "DisplayImpl.h"
19#include "DisplayUtils.h"
20#include "ConsoleImpl.h"
21#include "ConsoleVRDPServer.h"
22#include "VMMDev.h"
23
24#include "AutoCaller.h"
25#include "Logging.h"
26
27/* generated header */
28#include "VBoxEvents.h"
29
30#include <iprt/semaphore.h>
31#include <iprt/thread.h>
32#include <iprt/asm.h>
33#include <iprt/cpp/utils.h>
34
35#include <VBox/vmm/pdmdrv.h>
36#ifdef DEBUG /* for VM_ASSERT_EMT(). */
37# include <VBox/vmm/vm.h>
38#endif
39
40#ifdef VBOX_WITH_VIDEOHWACCEL
41# include <VBox/VBoxVideo.h>
42#endif
43
44#if defined(VBOX_WITH_CROGL) || defined(VBOX_WITH_CRHGSMI)
45# include <VBox/HostServices/VBoxCrOpenGLSvc.h>
46#endif
47
48#include <VBox/com/array.h>
49
50#ifdef VBOX_WITH_VPX
51# include "VideoRec.h"
52#endif
53
54/**
55 * Display driver instance data.
56 *
57 * @implements PDMIDISPLAYCONNECTOR
58 */
59typedef struct DRVMAINDISPLAY
60{
61 /** Pointer to the display object. */
62 Display *pDisplay;
63 /** Pointer to the driver instance structure. */
64 PPDMDRVINS pDrvIns;
65 /** Pointer to the keyboard port interface of the driver/device above us. */
66 PPDMIDISPLAYPORT pUpPort;
67 /** Our display connector interface. */
68 PDMIDISPLAYCONNECTOR IConnector;
69#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
70 /** VBVA callbacks */
71 PPDMIDISPLAYVBVACALLBACKS pVBVACallbacks;
72#endif
73} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
74
75/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
76#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) RT_FROM_MEMBER(pInterface, DRVMAINDISPLAY, IConnector)
77
78#ifdef DEBUG_sunlover
79static STAMPROFILE g_StatDisplayRefresh;
80static int g_stam = 0;
81#endif /* DEBUG_sunlover */
82
83// constructor / destructor
84/////////////////////////////////////////////////////////////////////////////
85
86Display::Display()
87 : mParent(NULL)
88{
89}
90
91Display::~Display()
92{
93}
94
95
96HRESULT Display::FinalConstruct()
97{
98 mpVbvaMemory = NULL;
99 mfVideoAccelEnabled = false;
100 mfVideoAccelVRDP = false;
101 mfu32SupportedOrders = 0;
102 mcVideoAccelVRDPRefs = 0;
103
104 mpPendingVbvaMemory = NULL;
105 mfPendingVideoAccelEnable = false;
106
107 mfMachineRunning = false;
108
109 mpu8VbvaPartial = NULL;
110 mcbVbvaPartial = 0;
111
112 mpDrv = NULL;
113 mpVMMDev = NULL;
114 mfVMMDevInited = false;
115
116 mLastAddress = NULL;
117 mLastBytesPerLine = 0;
118 mLastBitsPerPixel = 0,
119 mLastWidth = 0;
120 mLastHeight = 0;
121
122 int rc = RTCritSectInit(&mVBVALock);
123 AssertRC(rc);
124 mfu32PendingVideoAccelDisable = false;
125
126#ifdef VBOX_WITH_HGSMI
127 mu32UpdateVBVAFlags = 0;
128#endif
129
130 return BaseFinalConstruct();
131}
132
133void Display::FinalRelease()
134{
135 uninit();
136
137 if (RTCritSectIsInitialized (&mVBVALock))
138 {
139 RTCritSectDelete (&mVBVALock);
140 memset (&mVBVALock, 0, sizeof (mVBVALock));
141 }
142 BaseFinalRelease();
143}
144
145// public initializer/uninitializer for internal purposes only
146/////////////////////////////////////////////////////////////////////////////
147
148#define kMaxSizeThumbnail 64
149
150/**
151 * Save thumbnail and screenshot of the guest screen.
152 */
153static int displayMakeThumbnail(uint8_t *pu8Data, uint32_t cx, uint32_t cy,
154 uint8_t **ppu8Thumbnail, uint32_t *pcbThumbnail, uint32_t *pcxThumbnail, uint32_t *pcyThumbnail)
155{
156 int rc = VINF_SUCCESS;
157
158 uint8_t *pu8Thumbnail = NULL;
159 uint32_t cbThumbnail = 0;
160 uint32_t cxThumbnail = 0;
161 uint32_t cyThumbnail = 0;
162
163 if (cx > cy)
164 {
165 cxThumbnail = kMaxSizeThumbnail;
166 cyThumbnail = (kMaxSizeThumbnail * cy) / cx;
167 }
168 else
169 {
170 cyThumbnail = kMaxSizeThumbnail;
171 cxThumbnail = (kMaxSizeThumbnail * cx) / cy;
172 }
173
174 LogRelFlowFunc(("%dx%d -> %dx%d\n", cx, cy, cxThumbnail, cyThumbnail));
175
176 cbThumbnail = cxThumbnail * 4 * cyThumbnail;
177 pu8Thumbnail = (uint8_t *)RTMemAlloc(cbThumbnail);
178
179 if (pu8Thumbnail)
180 {
181 uint8_t *dst = pu8Thumbnail;
182 uint8_t *src = pu8Data;
183 int dstW = cxThumbnail;
184 int dstH = cyThumbnail;
185 int srcW = cx;
186 int srcH = cy;
187 int iDeltaLine = cx * 4;
188
189 BitmapScale32 (dst,
190 dstW, dstH,
191 src,
192 iDeltaLine,
193 srcW, srcH);
194
195 *ppu8Thumbnail = pu8Thumbnail;
196 *pcbThumbnail = cbThumbnail;
197 *pcxThumbnail = cxThumbnail;
198 *pcyThumbnail = cyThumbnail;
199 }
200 else
201 {
202 rc = VERR_NO_MEMORY;
203 }
204
205 return rc;
206}
207
208DECLCALLBACK(void)
209Display::displaySSMSaveScreenshot(PSSMHANDLE pSSM, void *pvUser)
210{
211 Display *that = static_cast<Display*>(pvUser);
212
213 /* 32bpp small RGB image. */
214 uint8_t *pu8Thumbnail = NULL;
215 uint32_t cbThumbnail = 0;
216 uint32_t cxThumbnail = 0;
217 uint32_t cyThumbnail = 0;
218
219 /* PNG screenshot. */
220 uint8_t *pu8PNG = NULL;
221 uint32_t cbPNG = 0;
222 uint32_t cxPNG = 0;
223 uint32_t cyPNG = 0;
224
225 Console::SafeVMPtr ptrVM(that->mParent);
226 if (ptrVM.isOk())
227 {
228 /* Query RGB bitmap. */
229 uint8_t *pu8Data = NULL;
230 size_t cbData = 0;
231 uint32_t cx = 0;
232 uint32_t cy = 0;
233
234 /* SSM code is executed on EMT(0), therefore no need to use VMR3ReqCallWait. */
235 int rc = Display::displayTakeScreenshotEMT(that, VBOX_VIDEO_PRIMARY_SCREEN, &pu8Data, &cbData, &cx, &cy);
236
237 /*
238 * It is possible that success is returned but everything is 0 or NULL.
239 * (no display attached if a VM is running with VBoxHeadless on OSE for example)
240 */
241 if (RT_SUCCESS(rc) && pu8Data)
242 {
243 Assert(cx && cy);
244
245 /* Prepare a small thumbnail and a PNG screenshot. */
246 displayMakeThumbnail(pu8Data, cx, cy, &pu8Thumbnail, &cbThumbnail, &cxThumbnail, &cyThumbnail);
247 rc = DisplayMakePNG(pu8Data, cx, cy, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 1);
248 if (RT_FAILURE(rc))
249 {
250 if (pu8PNG)
251 {
252 RTMemFree(pu8PNG);
253 pu8PNG = NULL;
254 }
255 cbPNG = 0;
256 cxPNG = 0;
257 cyPNG = 0;
258 }
259
260 /* This can be called from any thread. */
261 that->mpDrv->pUpPort->pfnFreeScreenshot(that->mpDrv->pUpPort, pu8Data);
262 }
263 }
264 else
265 {
266 LogFunc(("Failed to get VM pointer 0x%x\n", ptrVM.rc()));
267 }
268
269 /* Regardless of rc, save what is available:
270 * Data format:
271 * uint32_t cBlocks;
272 * [blocks]
273 *
274 * Each block is:
275 * uint32_t cbBlock; if 0 - no 'block data'.
276 * uint32_t typeOfBlock; 0 - 32bpp RGB bitmap, 1 - PNG, ignored if 'cbBlock' is 0.
277 * [block data]
278 *
279 * Block data for bitmap and PNG:
280 * uint32_t cx;
281 * uint32_t cy;
282 * [image data]
283 */
284 SSMR3PutU32(pSSM, 2); /* Write thumbnail and PNG screenshot. */
285
286 /* First block. */
287 SSMR3PutU32(pSSM, cbThumbnail + 2 * sizeof (uint32_t));
288 SSMR3PutU32(pSSM, 0); /* Block type: thumbnail. */
289
290 if (cbThumbnail)
291 {
292 SSMR3PutU32(pSSM, cxThumbnail);
293 SSMR3PutU32(pSSM, cyThumbnail);
294 SSMR3PutMem(pSSM, pu8Thumbnail, cbThumbnail);
295 }
296
297 /* Second block. */
298 SSMR3PutU32(pSSM, cbPNG + 2 * sizeof (uint32_t));
299 SSMR3PutU32(pSSM, 1); /* Block type: png. */
300
301 if (cbPNG)
302 {
303 SSMR3PutU32(pSSM, cxPNG);
304 SSMR3PutU32(pSSM, cyPNG);
305 SSMR3PutMem(pSSM, pu8PNG, cbPNG);
306 }
307
308 RTMemFree(pu8PNG);
309 RTMemFree(pu8Thumbnail);
310}
311
312DECLCALLBACK(int)
313Display::displaySSMLoadScreenshot(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
314{
315 Display *that = static_cast<Display*>(pvUser);
316
317 if (uVersion != sSSMDisplayScreenshotVer)
318 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
319 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
320
321 /* Skip data. */
322 uint32_t cBlocks;
323 int rc = SSMR3GetU32(pSSM, &cBlocks);
324 AssertRCReturn(rc, rc);
325
326 for (uint32_t i = 0; i < cBlocks; i++)
327 {
328 uint32_t cbBlock;
329 rc = SSMR3GetU32(pSSM, &cbBlock);
330 AssertRCBreak(rc);
331
332 uint32_t typeOfBlock;
333 rc = SSMR3GetU32(pSSM, &typeOfBlock);
334 AssertRCBreak(rc);
335
336 LogRelFlowFunc(("[%d] type %d, size %d bytes\n", i, typeOfBlock, cbBlock));
337
338 /* Note: displaySSMSaveScreenshot writes size of a block = 8 and
339 * do not write any data if the image size was 0.
340 * @todo Fix and increase saved state version.
341 */
342 if (cbBlock > 2 * sizeof (uint32_t))
343 {
344 rc = SSMR3Skip(pSSM, cbBlock);
345 AssertRCBreak(rc);
346 }
347 }
348
349 return rc;
350}
351
352/**
353 * Save/Load some important guest state
354 */
355DECLCALLBACK(void)
356Display::displaySSMSave(PSSMHANDLE pSSM, void *pvUser)
357{
358 Display *that = static_cast<Display*>(pvUser);
359
360 SSMR3PutU32(pSSM, that->mcMonitors);
361 for (unsigned i = 0; i < that->mcMonitors; i++)
362 {
363 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32Offset);
364 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32MaxFramebufferSize);
365 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32InformationSize);
366 SSMR3PutU32(pSSM, that->maFramebuffers[i].w);
367 SSMR3PutU32(pSSM, that->maFramebuffers[i].h);
368 SSMR3PutS32(pSSM, that->maFramebuffers[i].xOrigin);
369 SSMR3PutS32(pSSM, that->maFramebuffers[i].yOrigin);
370 SSMR3PutU32(pSSM, that->maFramebuffers[i].flags);
371 }
372}
373
374DECLCALLBACK(int)
375Display::displaySSMLoad(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
376{
377 Display *that = static_cast<Display*>(pvUser);
378
379 if (!( uVersion == sSSMDisplayVer
380 || uVersion == sSSMDisplayVer2
381 || uVersion == sSSMDisplayVer3))
382 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
383 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
384
385 uint32_t cMonitors;
386 int rc = SSMR3GetU32(pSSM, &cMonitors);
387 if (cMonitors != that->mcMonitors)
388 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Number of monitors changed (%d->%d)!"), cMonitors, that->mcMonitors);
389
390 for (uint32_t i = 0; i < cMonitors; i++)
391 {
392 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32Offset);
393 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32MaxFramebufferSize);
394 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32InformationSize);
395 if ( uVersion == sSSMDisplayVer2
396 || uVersion == sSSMDisplayVer3)
397 {
398 uint32_t w;
399 uint32_t h;
400 SSMR3GetU32(pSSM, &w);
401 SSMR3GetU32(pSSM, &h);
402 that->maFramebuffers[i].w = w;
403 that->maFramebuffers[i].h = h;
404 }
405 if (uVersion == sSSMDisplayVer3)
406 {
407 int32_t xOrigin;
408 int32_t yOrigin;
409 uint32_t flags;
410 SSMR3GetS32(pSSM, &xOrigin);
411 SSMR3GetS32(pSSM, &yOrigin);
412 SSMR3GetU32(pSSM, &flags);
413 that->maFramebuffers[i].xOrigin = xOrigin;
414 that->maFramebuffers[i].yOrigin = yOrigin;
415 that->maFramebuffers[i].flags = (uint16_t)flags;
416 that->maFramebuffers[i].fDisabled = (that->maFramebuffers[i].flags & VBVA_SCREEN_F_DISABLED) != 0;
417 }
418 }
419
420 return VINF_SUCCESS;
421}
422
423/**
424 * Initializes the display object.
425 *
426 * @returns COM result indicator
427 * @param parent handle of our parent object
428 * @param qemuConsoleData address of common console data structure
429 */
430HRESULT Display::init(Console *aParent)
431{
432 ComAssertRet(aParent, E_INVALIDARG);
433 /* Enclose the state transition NotReady->InInit->Ready */
434 AutoInitSpan autoInitSpan(this);
435 AssertReturn(autoInitSpan.isOk(), E_FAIL);
436
437 unconst(mParent) = aParent;
438
439 ULONG ul;
440 mParent->machine()->COMGETTER(MonitorCount)(&ul);
441 mcMonitors = ul;
442
443 for (ul = 0; ul < mcMonitors; ul++)
444 {
445 maFramebuffers[ul].u32Offset = 0;
446 maFramebuffers[ul].u32MaxFramebufferSize = 0;
447 maFramebuffers[ul].u32InformationSize = 0;
448
449 maFramebuffers[ul].pFramebuffer = NULL;
450 /* All secondary monitors are disabled at startup. */
451 maFramebuffers[ul].fDisabled = ul > 0;
452
453 maFramebuffers[ul].xOrigin = 0;
454 maFramebuffers[ul].yOrigin = 0;
455
456 maFramebuffers[ul].w = 0;
457 maFramebuffers[ul].h = 0;
458
459 maFramebuffers[ul].flags = maFramebuffers[ul].fDisabled? VBVA_SCREEN_F_DISABLED: 0;
460
461 maFramebuffers[ul].u16BitsPerPixel = 0;
462 maFramebuffers[ul].pu8FramebufferVRAM = NULL;
463 maFramebuffers[ul].u32LineSize = 0;
464
465 maFramebuffers[ul].pHostEvents = NULL;
466
467 maFramebuffers[ul].u32ResizeStatus = ResizeStatus_Void;
468
469 maFramebuffers[ul].fDefaultFormat = false;
470
471 memset (&maFramebuffers[ul].dirtyRect, 0 , sizeof (maFramebuffers[ul].dirtyRect));
472 memset (&maFramebuffers[ul].pendingResize, 0 , sizeof (maFramebuffers[ul].pendingResize));
473#ifdef VBOX_WITH_HGSMI
474 maFramebuffers[ul].fVBVAEnabled = false;
475 maFramebuffers[ul].cVBVASkipUpdate = 0;
476 memset (&maFramebuffers[ul].vbvaSkippedRect, 0, sizeof (maFramebuffers[ul].vbvaSkippedRect));
477 maFramebuffers[ul].pVBVAHostFlags = NULL;
478#endif /* VBOX_WITH_HGSMI */
479 }
480
481 {
482 // register listener for state change events
483 ComPtr<IEventSource> es;
484 mParent->COMGETTER(EventSource)(es.asOutParam());
485 com::SafeArray <VBoxEventType_T> eventTypes;
486 eventTypes.push_back(VBoxEventType_OnStateChanged);
487 es->RegisterListener(this, ComSafeArrayAsInParam(eventTypes), true);
488 }
489
490 /* Confirm a successful initialization */
491 autoInitSpan.setSucceeded();
492
493 return S_OK;
494}
495
496/**
497 * Uninitializes the instance and sets the ready flag to FALSE.
498 * Called either from FinalRelease() or by the parent when it gets destroyed.
499 */
500void Display::uninit()
501{
502 LogRelFlowFunc(("this=%p\n", this));
503
504 /* Enclose the state transition Ready->InUninit->NotReady */
505 AutoUninitSpan autoUninitSpan(this);
506 if (autoUninitSpan.uninitDone())
507 return;
508
509 ULONG ul;
510 for (ul = 0; ul < mcMonitors; ul++)
511 maFramebuffers[ul].pFramebuffer = NULL;
512
513 if (mParent)
514 {
515 ComPtr<IEventSource> es;
516 mParent->COMGETTER(EventSource)(es.asOutParam());
517 es->UnregisterListener(this);
518 }
519
520 unconst(mParent) = NULL;
521
522 if (mpDrv)
523 mpDrv->pDisplay = NULL;
524
525 mpDrv = NULL;
526 mpVMMDev = NULL;
527 mfVMMDevInited = true;
528
529#ifdef VBOX_WITH_VPX
530 if (mpVideoRecCtx)
531 VideoRecContextClose(mpVideoRecCtx);
532#endif
533}
534
535/**
536 * Register the SSM methods. Called by the power up thread to be able to
537 * pass pVM
538 */
539int Display::registerSSM(PUVM pUVM)
540{
541 /* Version 2 adds width and height of the framebuffer; version 3 adds
542 * the framebuffer offset in the virtual desktop and the framebuffer flags.
543 */
544 int rc = SSMR3RegisterExternal(pUVM, "DisplayData", 0, sSSMDisplayVer3,
545 mcMonitors * sizeof(uint32_t) * 8 + sizeof(uint32_t),
546 NULL, NULL, NULL,
547 NULL, displaySSMSave, NULL,
548 NULL, displaySSMLoad, NULL, this);
549 AssertRCReturn(rc, rc);
550
551 /*
552 * Register loaders for old saved states where iInstance was
553 * 3 * sizeof(uint32_t *) due to a code mistake.
554 */
555 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 12 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
556 NULL, NULL, NULL,
557 NULL, NULL, NULL,
558 NULL, displaySSMLoad, NULL, this);
559 AssertRCReturn(rc, rc);
560
561 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 24 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
562 NULL, NULL, NULL,
563 NULL, NULL, NULL,
564 NULL, displaySSMLoad, NULL, this);
565 AssertRCReturn(rc, rc);
566
567 /* uInstance is an arbitrary value greater than 1024. Such a value will ensure a quick seek in saved state file. */
568 rc = SSMR3RegisterExternal(pUVM, "DisplayScreenshot", 1100 /*uInstance*/, sSSMDisplayScreenshotVer, 0 /*cbGuess*/,
569 NULL, NULL, NULL,
570 NULL, displaySSMSaveScreenshot, NULL,
571 NULL, displaySSMLoadScreenshot, NULL, this);
572
573 AssertRCReturn(rc, rc);
574
575 return VINF_SUCCESS;
576}
577
578// IEventListener method
579STDMETHODIMP Display::HandleEvent(IEvent * aEvent)
580{
581 VBoxEventType_T aType = VBoxEventType_Invalid;
582
583 aEvent->COMGETTER(Type)(&aType);
584 switch (aType)
585 {
586 case VBoxEventType_OnStateChanged:
587 {
588 ComPtr<IStateChangedEvent> scev = aEvent;
589 Assert(scev);
590 MachineState_T machineState;
591 scev->COMGETTER(State)(&machineState);
592 if ( machineState == MachineState_Running
593 || machineState == MachineState_Teleporting
594 || machineState == MachineState_LiveSnapshotting
595 )
596 {
597 LogRelFlowFunc(("Machine is running.\n"));
598
599 mfMachineRunning = true;
600 }
601 else
602 mfMachineRunning = false;
603 break;
604 }
605 default:
606 AssertFailed();
607 }
608
609 return S_OK;
610}
611
612// public methods only for internal purposes
613/////////////////////////////////////////////////////////////////////////////
614
615/**
616 * @thread EMT
617 */
618static int callFramebufferResize (IFramebuffer *pFramebuffer, unsigned uScreenId,
619 ULONG pixelFormat, void *pvVRAM,
620 uint32_t bpp, uint32_t cbLine,
621 int w, int h)
622{
623 Assert (pFramebuffer);
624
625 /* Call the framebuffer to try and set required pixelFormat. */
626 BOOL finished = TRUE;
627
628 pFramebuffer->RequestResize (uScreenId, pixelFormat, (BYTE *) pvVRAM,
629 bpp, cbLine, w, h, &finished);
630
631 if (!finished)
632 {
633 LogRelFlowFunc(("External framebuffer wants us to wait!\n"));
634 return VINF_VGA_RESIZE_IN_PROGRESS;
635 }
636
637 return VINF_SUCCESS;
638}
639
640/**
641 * Handles display resize event.
642 * Disables access to VGA device;
643 * calls the framebuffer RequestResize method;
644 * if framebuffer resizes synchronously,
645 * updates the display connector data and enables access to the VGA device.
646 *
647 * @param w New display width
648 * @param h New display height
649 *
650 * @thread EMT
651 */
652int Display::handleDisplayResize (unsigned uScreenId, uint32_t bpp, void *pvVRAM,
653 uint32_t cbLine, int w, int h, uint16_t flags)
654{
655 LogRel(("Display::handleDisplayResize(): uScreenId = %d, pvVRAM=%p "
656 "w=%d h=%d bpp=%d cbLine=0x%X, flags=0x%X\n",
657 uScreenId, pvVRAM, w, h, bpp, cbLine, flags));
658
659 /* If there is no framebuffer, this call is not interesting. */
660 if ( uScreenId >= mcMonitors
661 || maFramebuffers[uScreenId].pFramebuffer.isNull())
662 {
663 return VINF_SUCCESS;
664 }
665
666 mLastAddress = pvVRAM;
667 mLastBytesPerLine = cbLine;
668 mLastBitsPerPixel = bpp,
669 mLastWidth = w;
670 mLastHeight = h;
671 mLastFlags = flags;
672
673 ULONG pixelFormat;
674
675 switch (bpp)
676 {
677 case 32:
678 case 24:
679 case 16:
680 pixelFormat = FramebufferPixelFormat_FOURCC_RGB;
681 break;
682 default:
683 pixelFormat = FramebufferPixelFormat_Opaque;
684 bpp = cbLine = 0;
685 break;
686 }
687
688 /* Atomically set the resize status before calling the framebuffer. The new InProgress status will
689 * disable access to the VGA device by the EMT thread.
690 */
691 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
692 ResizeStatus_InProgress, ResizeStatus_Void);
693 if (!f)
694 {
695 /* This could be a result of the screenshot taking call Display::TakeScreenShot:
696 * if the framebuffer is processing the resize request and GUI calls the TakeScreenShot
697 * and the guest has reprogrammed the virtual VGA devices again so a new resize is required.
698 *
699 * Save the resize information and return the pending status code.
700 *
701 * Note: the resize information is only accessed on EMT so no serialization is required.
702 */
703 LogRel(("Display::handleDisplayResize(): Warning: resize postponed.\n"));
704
705 maFramebuffers[uScreenId].pendingResize.fPending = true;
706 maFramebuffers[uScreenId].pendingResize.pixelFormat = pixelFormat;
707 maFramebuffers[uScreenId].pendingResize.pvVRAM = pvVRAM;
708 maFramebuffers[uScreenId].pendingResize.bpp = bpp;
709 maFramebuffers[uScreenId].pendingResize.cbLine = cbLine;
710 maFramebuffers[uScreenId].pendingResize.w = w;
711 maFramebuffers[uScreenId].pendingResize.h = h;
712 maFramebuffers[uScreenId].pendingResize.flags = flags;
713
714 return VINF_VGA_RESIZE_IN_PROGRESS;
715 }
716
717 int rc = callFramebufferResize (maFramebuffers[uScreenId].pFramebuffer, uScreenId,
718 pixelFormat, pvVRAM, bpp, cbLine, w, h);
719 if (rc == VINF_VGA_RESIZE_IN_PROGRESS)
720 {
721 /* Immediately return to the caller. ResizeCompleted will be called back by the
722 * GUI thread. The ResizeCompleted callback will change the resize status from
723 * InProgress to UpdateDisplayData. The latter status will be checked by the
724 * display timer callback on EMT and all required adjustments will be done there.
725 */
726 return rc;
727 }
728
729 /* Set the status so the 'handleResizeCompleted' would work. */
730 f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
731 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
732 AssertRelease(f);NOREF(f);
733
734 AssertRelease(!maFramebuffers[uScreenId].pendingResize.fPending);
735
736 /* The method also unlocks the framebuffer. */
737 handleResizeCompletedEMT();
738
739 return VINF_SUCCESS;
740}
741
742/**
743 * Framebuffer has been resized.
744 * Read the new display data and unlock the framebuffer.
745 *
746 * @thread EMT
747 */
748void Display::handleResizeCompletedEMT (void)
749{
750 LogRelFlowFunc(("\n"));
751
752 unsigned uScreenId;
753 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
754 {
755 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
756
757 /* Try to into non resizing state. */
758 bool f = ASMAtomicCmpXchgU32 (&pFBInfo->u32ResizeStatus, ResizeStatus_Void, ResizeStatus_UpdateDisplayData);
759
760 if (f == false)
761 {
762 /* This is not the display that has completed resizing. */
763 continue;
764 }
765
766 /* Check whether a resize is pending for this framebuffer. */
767 if (pFBInfo->pendingResize.fPending)
768 {
769 /* Reset the condition, call the display resize with saved data and continue.
770 *
771 * Note: handleDisplayResize can call handleResizeCompletedEMT back,
772 * but infinite recursion is not possible, because when the handleResizeCompletedEMT
773 * is called, the pFBInfo->pendingResize.fPending is equal to false.
774 */
775 pFBInfo->pendingResize.fPending = false;
776 handleDisplayResize (uScreenId, pFBInfo->pendingResize.bpp, pFBInfo->pendingResize.pvVRAM,
777 pFBInfo->pendingResize.cbLine, pFBInfo->pendingResize.w, pFBInfo->pendingResize.h, pFBInfo->pendingResize.flags);
778 continue;
779 }
780
781 /* @todo Merge these two 'if's within one 'if (!pFBInfo->pFramebuffer.isNull())' */
782 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
783 {
784 /* Primary framebuffer has completed the resize. Update the connector data for VGA device. */
785 updateDisplayData();
786
787 /* Check the framebuffer pixel format to setup the rendering in VGA device. */
788 BOOL usesGuestVRAM = FALSE;
789 pFBInfo->pFramebuffer->COMGETTER(UsesGuestVRAM) (&usesGuestVRAM);
790
791 pFBInfo->fDefaultFormat = (usesGuestVRAM == FALSE);
792
793 /* If the primary framebuffer is disabled, tell the VGA device to not to copy
794 * pixels from VRAM to the framebuffer.
795 */
796 if (pFBInfo->fDisabled)
797 mpDrv->pUpPort->pfnSetRenderVRAM (mpDrv->pUpPort, false);
798 else
799 mpDrv->pUpPort->pfnSetRenderVRAM (mpDrv->pUpPort,
800 pFBInfo->fDefaultFormat);
801
802 /* If the screen resize was because of disabling, tell framebuffer to repaint.
803 * The framebuffer if now in default format so it will not use guest VRAM
804 * and will show usually black image which is there after framebuffer resize.
805 */
806 if (pFBInfo->fDisabled)
807 pFBInfo->pFramebuffer->NotifyUpdate(0, 0, mpDrv->IConnector.cx, mpDrv->IConnector.cy);
808 }
809 else if (!pFBInfo->pFramebuffer.isNull())
810 {
811 BOOL usesGuestVRAM = FALSE;
812 pFBInfo->pFramebuffer->COMGETTER(UsesGuestVRAM) (&usesGuestVRAM);
813
814 pFBInfo->fDefaultFormat = (usesGuestVRAM == FALSE);
815
816 /* If the screen resize was because of disabling, tell framebuffer to repaint.
817 * The framebuffer if now in default format so it will not use guest VRAM
818 * and will show usually black image which is there after framebuffer resize.
819 */
820 if (pFBInfo->fDisabled)
821 pFBInfo->pFramebuffer->NotifyUpdate(0, 0, pFBInfo->w, pFBInfo->h);
822 }
823 LogRelFlow(("[%d]: default format %d\n", uScreenId, pFBInfo->fDefaultFormat));
824
825#ifdef DEBUG_sunlover
826 if (!g_stam)
827 {
828 Console::SafeVMPtr ptrVM(mParent);
829 AssertComRC(ptrVM.rc());
830 STAMR3RegisterU(ptrVM.rawUVM(), &g_StatDisplayRefresh, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS,
831 "/PROF/Display/Refresh", STAMUNIT_TICKS_PER_CALL, "Time spent in EMT for display updates.");
832 g_stam = 1;
833 }
834#endif /* DEBUG_sunlover */
835
836 /* Inform VRDP server about the change of display parameters. */
837 LogRelFlowFunc(("Calling VRDP\n"));
838 mParent->consoleVRDPServer()->SendResize();
839
840#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
841 {
842 BOOL is3denabled;
843 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
844
845 if (is3denabled)
846 {
847 VBOXHGCMSVCPARM parm;
848
849 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
850 parm.u.uint32 = uScreenId;
851
852 VMMDev *pVMMDev = mParent->getVMMDev();
853 if (pVMMDev)
854 pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED, SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
855 }
856 }
857#endif /* VBOX_WITH_CROGL */
858 }
859}
860
861static void checkCoordBounds (int *px, int *py, int *pw, int *ph, int cx, int cy)
862{
863 /* Correct negative x and y coordinates. */
864 if (*px < 0)
865 {
866 *px += *pw; /* Compute xRight which is also the new width. */
867
868 *pw = (*px < 0)? 0: *px;
869
870 *px = 0;
871 }
872
873 if (*py < 0)
874 {
875 *py += *ph; /* Compute xBottom, which is also the new height. */
876
877 *ph = (*py < 0)? 0: *py;
878
879 *py = 0;
880 }
881
882 /* Also check if coords are greater than the display resolution. */
883 if (*px + *pw > cx)
884 {
885 *pw = cx > *px? cx - *px: 0;
886 }
887
888 if (*py + *ph > cy)
889 {
890 *ph = cy > *py? cy - *py: 0;
891 }
892}
893
894unsigned mapCoordsToScreen(DISPLAYFBINFO *pInfos, unsigned cInfos, int *px, int *py, int *pw, int *ph)
895{
896 DISPLAYFBINFO *pInfo = pInfos;
897 unsigned uScreenId;
898 LogSunlover(("mapCoordsToScreen: %d,%d %dx%d\n", *px, *py, *pw, *ph));
899 for (uScreenId = 0; uScreenId < cInfos; uScreenId++, pInfo++)
900 {
901 LogSunlover((" [%d] %d,%d %dx%d\n", uScreenId, pInfo->xOrigin, pInfo->yOrigin, pInfo->w, pInfo->h));
902 if ( (pInfo->xOrigin <= *px && *px < pInfo->xOrigin + (int)pInfo->w)
903 && (pInfo->yOrigin <= *py && *py < pInfo->yOrigin + (int)pInfo->h))
904 {
905 /* The rectangle belongs to the screen. Correct coordinates. */
906 *px -= pInfo->xOrigin;
907 *py -= pInfo->yOrigin;
908 LogSunlover((" -> %d,%d", *px, *py));
909 break;
910 }
911 }
912 if (uScreenId == cInfos)
913 {
914 /* Map to primary screen. */
915 uScreenId = 0;
916 }
917 LogSunlover((" scr %d\n", uScreenId));
918 return uScreenId;
919}
920
921
922/**
923 * Handles display update event.
924 *
925 * @param x Update area x coordinate
926 * @param y Update area y coordinate
927 * @param w Update area width
928 * @param h Update area height
929 *
930 * @thread EMT
931 */
932void Display::handleDisplayUpdateLegacy (int x, int y, int w, int h)
933{
934 unsigned uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
935
936#ifdef DEBUG_sunlover
937 LogFlowFunc(("%d,%d %dx%d (checked)\n", x, y, w, h));
938#endif /* DEBUG_sunlover */
939
940 handleDisplayUpdate (uScreenId, x, y, w, h);
941}
942
943void Display::handleDisplayUpdate (unsigned uScreenId, int x, int y, int w, int h)
944{
945 /*
946 * Always runs under either VBVA lock or, for HGSMI, DevVGA lock.
947 * Safe to use VBVA vars and take the framebuffer lock.
948 */
949
950#ifdef DEBUG_sunlover
951 LogFlowFunc(("[%d] %d,%d %dx%d (%d,%d)\n",
952 uScreenId, x, y, w, h, mpDrv->IConnector.cx, mpDrv->IConnector.cy));
953#endif /* DEBUG_sunlover */
954
955 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
956
957 // if there is no framebuffer, this call is not interesting
958 if ( pFramebuffer == NULL
959 || maFramebuffers[uScreenId].fDisabled)
960 return;
961
962 pFramebuffer->Lock();
963
964 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
965 checkCoordBounds (&x, &y, &w, &h, mpDrv->IConnector.cx, mpDrv->IConnector.cy);
966 else
967 checkCoordBounds (&x, &y, &w, &h, maFramebuffers[uScreenId].w,
968 maFramebuffers[uScreenId].h);
969
970 if (w != 0 && h != 0)
971 pFramebuffer->NotifyUpdate(x, y, w, h);
972
973 pFramebuffer->Unlock();
974
975#ifndef VBOX_WITH_HGSMI
976 if (!mfVideoAccelEnabled)
977 {
978#else
979 if (!mfVideoAccelEnabled && !maFramebuffers[uScreenId].fVBVAEnabled)
980 {
981#endif /* VBOX_WITH_HGSMI */
982 /* When VBVA is enabled, the VRDP server is informed in the VideoAccelFlush.
983 * Inform the server here only if VBVA is disabled.
984 */
985 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
986 mParent->consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
987 }
988}
989
990/**
991 * Returns the upper left and lower right corners of the virtual framebuffer.
992 * The lower right is "exclusive" (i.e. first pixel beyond the framebuffer),
993 * and the origin is (0, 0), not (1, 1) like the GUI returns.
994 */
995void Display::getFramebufferDimensions(int32_t *px1, int32_t *py1,
996 int32_t *px2, int32_t *py2)
997{
998 int32_t x1 = 0, y1 = 0, x2 = 0, y2 = 0;
999 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1000
1001 AssertPtrReturnVoid(px1);
1002 AssertPtrReturnVoid(py1);
1003 AssertPtrReturnVoid(px2);
1004 AssertPtrReturnVoid(py2);
1005 LogRelFlowFunc(("\n"));
1006
1007 if (!mpDrv)
1008 return;
1009 /* If VBVA is not in use then this flag will not be set and this
1010 * will still work as it should. */
1011 if (!maFramebuffers[0].fDisabled)
1012 {
1013 x1 = (int32_t)maFramebuffers[0].xOrigin;
1014 y1 = (int32_t)maFramebuffers[0].yOrigin;
1015 x2 = mpDrv->IConnector.cx + (int32_t)maFramebuffers[0].xOrigin;
1016 y2 = mpDrv->IConnector.cy + (int32_t)maFramebuffers[0].yOrigin;
1017 }
1018 for (unsigned i = 1; i < mcMonitors; ++i)
1019 {
1020 if (!maFramebuffers[i].fDisabled)
1021 {
1022 x1 = RT_MIN(x1, maFramebuffers[i].xOrigin);
1023 y1 = RT_MIN(y1, maFramebuffers[i].yOrigin);
1024 x2 = RT_MAX(x2, maFramebuffers[i].xOrigin
1025 + (int32_t)maFramebuffers[i].w);
1026 y2 = RT_MAX(y2, maFramebuffers[i].yOrigin
1027 + (int32_t)maFramebuffers[i].h);
1028 }
1029 }
1030 *px1 = x1;
1031 *py1 = y1;
1032 *px2 = x2;
1033 *py2 = y2;
1034}
1035
1036static bool displayIntersectRect(RTRECT *prectResult,
1037 const RTRECT *prect1,
1038 const RTRECT *prect2)
1039{
1040 /* Initialize result to an empty record. */
1041 memset (prectResult, 0, sizeof (RTRECT));
1042
1043 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
1044 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
1045
1046 if (xLeftResult < xRightResult)
1047 {
1048 /* There is intersection by X. */
1049
1050 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
1051 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
1052
1053 if (yTopResult < yBottomResult)
1054 {
1055 /* There is intersection by Y. */
1056
1057 prectResult->xLeft = xLeftResult;
1058 prectResult->yTop = yTopResult;
1059 prectResult->xRight = xRightResult;
1060 prectResult->yBottom = yBottomResult;
1061
1062 return true;
1063 }
1064 }
1065
1066 return false;
1067}
1068
1069int Display::handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1070{
1071 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc( RT_MAX(cRect, 1)
1072 * sizeof (RTRECT));
1073 if (!pVisibleRegion)
1074 {
1075 return VERR_NO_TMP_MEMORY;
1076 }
1077
1078 unsigned uScreenId;
1079 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1080 {
1081 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1082
1083 if (!pFBInfo->pFramebuffer.isNull())
1084 {
1085 /* Prepare a new array of rectangles which intersect with the framebuffer.
1086 */
1087 RTRECT rectFramebuffer;
1088 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1089 {
1090 rectFramebuffer.xLeft = 0;
1091 rectFramebuffer.yTop = 0;
1092 if (mpDrv)
1093 {
1094 rectFramebuffer.xRight = mpDrv->IConnector.cx;
1095 rectFramebuffer.yBottom = mpDrv->IConnector.cy;
1096 }
1097 else
1098 {
1099 rectFramebuffer.xRight = 0;
1100 rectFramebuffer.yBottom = 0;
1101 }
1102 }
1103 else
1104 {
1105 rectFramebuffer.xLeft = pFBInfo->xOrigin;
1106 rectFramebuffer.yTop = pFBInfo->yOrigin;
1107 rectFramebuffer.xRight = pFBInfo->xOrigin + pFBInfo->w;
1108 rectFramebuffer.yBottom = pFBInfo->yOrigin + pFBInfo->h;
1109 }
1110
1111 uint32_t cRectVisibleRegion = 0;
1112
1113 uint32_t i;
1114 for (i = 0; i < cRect; i++)
1115 {
1116 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1117 {
1118 pVisibleRegion[cRectVisibleRegion].xLeft -= pFBInfo->xOrigin;
1119 pVisibleRegion[cRectVisibleRegion].yTop -= pFBInfo->yOrigin;
1120 pVisibleRegion[cRectVisibleRegion].xRight -= pFBInfo->xOrigin;
1121 pVisibleRegion[cRectVisibleRegion].yBottom -= pFBInfo->yOrigin;
1122
1123 cRectVisibleRegion++;
1124 }
1125 }
1126
1127 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1128 }
1129 }
1130
1131#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
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, IFramebuffer *aFramebuffer)
2038{
2039 LogRelFlowFunc(("\n"));
2040
2041 if (aFramebuffer != NULL)
2042 CheckComArgOutPointerValid(aFramebuffer);
2043
2044 AutoCaller autoCaller(this);
2045 if (FAILED(autoCaller.rc()))
2046 return autoCaller.rc();
2047
2048 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2049
2050 Console::SafeVMPtrQuiet ptrVM(mParent);
2051 if (ptrVM.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 = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY,
2059 (PFNRT)changeFramebuffer, 3, this, aFramebuffer, aScreenId);
2060
2061 alock.acquire();
2062
2063 ComAssertRCRet (vrc, E_FAIL);
2064
2065#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2066 {
2067 BOOL is3denabled;
2068 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2069
2070 if (is3denabled)
2071 {
2072 VBOXHGCMSVCPARM parm;
2073
2074 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
2075 parm.u.uint32 = aScreenId;
2076
2077 VMMDev *pVMMDev = mParent->getVMMDev();
2078
2079 alock.release();
2080
2081 if (pVMMDev)
2082 vrc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED, SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
2083 /*ComAssertRCRet (vrc, E_FAIL);*/
2084
2085 alock.acquire();
2086 }
2087 }
2088#endif /* VBOX_WITH_CROGL */
2089 }
2090 else
2091 {
2092 /* No VM is created (VM is powered off), do a direct call */
2093 int vrc = changeFramebuffer (this, aFramebuffer, aScreenId);
2094 ComAssertRCRet (vrc, E_FAIL);
2095 }
2096
2097 return S_OK;
2098}
2099
2100STDMETHODIMP Display::GetFramebuffer(ULONG aScreenId,
2101 IFramebuffer **aFramebuffer, LONG *aXOrigin, LONG *aYOrigin)
2102{
2103 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2104
2105 CheckComArgOutPointerValid(aFramebuffer);
2106
2107 AutoCaller autoCaller(this);
2108 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2109
2110 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2111
2112 if (aScreenId != 0 && aScreenId >= mcMonitors)
2113 return E_INVALIDARG;
2114
2115 /* @todo this should be actually done on EMT. */
2116 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2117
2118 *aFramebuffer = pFBInfo->pFramebuffer;
2119 if (*aFramebuffer)
2120 (*aFramebuffer)->AddRef ();
2121 if (aXOrigin)
2122 *aXOrigin = pFBInfo->xOrigin;
2123 if (aYOrigin)
2124 *aYOrigin = pFBInfo->yOrigin;
2125
2126 return S_OK;
2127}
2128
2129STDMETHODIMP Display::SetVideoModeHint(ULONG aDisplay, BOOL aEnabled,
2130 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
2131 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel)
2132{
2133 AutoCaller autoCaller(this);
2134 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2135
2136 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2137
2138 CHECK_CONSOLE_DRV(mpDrv);
2139
2140 /*
2141 * Do some rough checks for valid input
2142 */
2143 ULONG width = aWidth;
2144 if (!width)
2145 width = mpDrv->IConnector.cx;
2146 ULONG height = aHeight;
2147 if (!height)
2148 height = mpDrv->IConnector.cy;
2149 ULONG bpp = aBitsPerPixel;
2150 if (!bpp)
2151 {
2152 uint32_t cBits = 0;
2153 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
2154 AssertRC(rc);
2155 bpp = cBits;
2156 }
2157 ULONG cMonitors;
2158 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
2159 if (cMonitors == 0 && aDisplay > 0)
2160 return E_INVALIDARG;
2161 if (aDisplay >= cMonitors)
2162 return E_INVALIDARG;
2163
2164 /*
2165 * sunlover 20070614: It is up to the guest to decide whether the hint is
2166 * valid. Therefore don't do any VRAM sanity checks here!
2167 */
2168
2169 /* Have to release the lock because the pfnRequestDisplayChange
2170 * will call EMT. */
2171 alock.release();
2172
2173 VMMDev *pVMMDev = mParent->getVMMDev();
2174 if (pVMMDev)
2175 {
2176 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2177 if (pVMMDevPort)
2178 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aBitsPerPixel,
2179 aDisplay, aOriginX, aOriginY,
2180 RT_BOOL(aEnabled), RT_BOOL(aChangeOrigin));
2181 }
2182 return S_OK;
2183}
2184
2185STDMETHODIMP Display::SetSeamlessMode (BOOL enabled)
2186{
2187 AutoCaller autoCaller(this);
2188 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2189
2190 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2191
2192 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
2193 alock.release();
2194
2195 VMMDev *pVMMDev = mParent->getVMMDev();
2196 if (pVMMDev)
2197 {
2198 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2199 if (pVMMDevPort)
2200 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
2201 }
2202
2203#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2204 if (!enabled)
2205 {
2206 BOOL is3denabled = FALSE;
2207
2208 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2209
2210 VMMDev *vmmDev = mParent->getVMMDev();
2211 if (is3denabled && vmmDev)
2212 {
2213 VBOXHGCMSVCPARM parms[2];
2214
2215 parms[0].type = VBOX_HGCM_SVC_PARM_PTR;
2216 /* NULL means disable */
2217 parms[0].u.pointer.addr = NULL;
2218 parms[0].u.pointer.size = 0; /* We don't actually care. */
2219 parms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
2220 parms[1].u.uint32 = 0;
2221
2222 vmmDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SET_VISIBLE_REGION, 2, &parms[0]);
2223 }
2224 }
2225#endif
2226 return S_OK;
2227}
2228
2229int Display::displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppu8Data, size_t *pcbData, uint32_t *pu32Width, uint32_t *pu32Height)
2230{
2231 int rc;
2232 pDisplay->vbvaLock();
2233 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
2234 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
2235 {
2236 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppu8Data, pcbData, pu32Width, pu32Height);
2237 }
2238 else if (aScreenId < pDisplay->mcMonitors)
2239 {
2240 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2241
2242 uint32_t width = pFBInfo->w;
2243 uint32_t height = pFBInfo->h;
2244
2245 /* Allocate 32 bit per pixel bitmap. */
2246 size_t cbRequired = width * 4 * height;
2247
2248 if (cbRequired)
2249 {
2250 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbRequired);
2251
2252 if (pu8Data == NULL)
2253 {
2254 rc = VERR_NO_MEMORY;
2255 }
2256 else
2257 {
2258 /* Copy guest VRAM to the allocated 32bpp buffer. */
2259 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2260 int32_t xSrc = 0;
2261 int32_t ySrc = 0;
2262 uint32_t u32SrcWidth = width;
2263 uint32_t u32SrcHeight = height;
2264 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2265 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2266
2267 uint8_t *pu8Dst = pu8Data;
2268 int32_t xDst = 0;
2269 int32_t yDst = 0;
2270 uint32_t u32DstWidth = u32SrcWidth;
2271 uint32_t u32DstHeight = u32SrcHeight;
2272 uint32_t u32DstLineSize = u32DstWidth * 4;
2273 uint32_t u32DstBitsPerPixel = 32;
2274
2275 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2276 width, height,
2277 pu8Src,
2278 xSrc, ySrc,
2279 u32SrcWidth, u32SrcHeight,
2280 u32SrcLineSize, u32SrcBitsPerPixel,
2281 pu8Dst,
2282 xDst, yDst,
2283 u32DstWidth, u32DstHeight,
2284 u32DstLineSize, u32DstBitsPerPixel);
2285 if (RT_SUCCESS(rc))
2286 {
2287 *ppu8Data = pu8Data;
2288 *pcbData = cbRequired;
2289 *pu32Width = width;
2290 *pu32Height = height;
2291 }
2292 else
2293 {
2294 RTMemFree(pu8Data);
2295 }
2296 }
2297 }
2298 else
2299 {
2300 /* No image. */
2301 *ppu8Data = NULL;
2302 *pcbData = 0;
2303 *pu32Width = 0;
2304 *pu32Height = 0;
2305 rc = VINF_SUCCESS;
2306 }
2307 }
2308 else
2309 {
2310 rc = VERR_INVALID_PARAMETER;
2311 }
2312 pDisplay->vbvaUnlock();
2313 return rc;
2314}
2315
2316static int displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
2317 BYTE *address, ULONG width, ULONG height)
2318{
2319 uint8_t *pu8Data = NULL;
2320 size_t cbData = 0;
2321 uint32_t cx = 0;
2322 uint32_t cy = 0;
2323 int vrc = VINF_SUCCESS;
2324
2325 int cRetries = 5;
2326
2327 while (cRetries-- > 0)
2328 {
2329 /* Note! Not sure if the priority call is such a good idea here, but
2330 it would be nice to have an accurate screenshot for the bug
2331 report if the VM deadlocks. */
2332 vrc = VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)Display::displayTakeScreenshotEMT, 6,
2333 pDisplay, aScreenId, &pu8Data, &cbData, &cx, &cy);
2334 if (vrc != VERR_TRY_AGAIN)
2335 {
2336 break;
2337 }
2338
2339 RTThreadSleep(10);
2340 }
2341
2342 if (RT_SUCCESS(vrc) && pu8Data)
2343 {
2344 if (cx == width && cy == height)
2345 {
2346 /* No scaling required. */
2347 memcpy(address, pu8Data, cbData);
2348 }
2349 else
2350 {
2351 /* Scale. */
2352 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2353
2354 uint8_t *dst = address;
2355 uint8_t *src = pu8Data;
2356 int dstW = width;
2357 int dstH = height;
2358 int srcW = cx;
2359 int srcH = cy;
2360 int iDeltaLine = cx * 4;
2361
2362 BitmapScale32(dst,
2363 dstW, dstH,
2364 src,
2365 iDeltaLine,
2366 srcW, srcH);
2367 }
2368
2369 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2370 {
2371 /* This can be called from any thread. */
2372 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pu8Data);
2373 }
2374 else
2375 {
2376 RTMemFree(pu8Data);
2377 }
2378 }
2379
2380 return vrc;
2381}
2382
2383STDMETHODIMP Display::TakeScreenShot(ULONG aScreenId, BYTE *address, ULONG width, ULONG height)
2384{
2385 /// @todo (r=dmik) this function may take too long to complete if the VM
2386 // is doing something like saving state right now. Which, in case if it
2387 // is called on the GUI thread, will make it unresponsive. We should
2388 // check the machine state here (by enclosing the check and VMRequCall
2389 // within the Console lock to make it atomic).
2390
2391 LogRelFlowFunc(("address=%p, width=%d, height=%d\n",
2392 address, width, height));
2393
2394 CheckComArgNotNull(address);
2395 CheckComArgExpr(width, width != 0);
2396 CheckComArgExpr(height, height != 0);
2397
2398 /* Do not allow too large screenshots. This also filters out negative
2399 * values passed as either 'width' or 'height'.
2400 */
2401 CheckComArgExpr(width, width <= 32767);
2402 CheckComArgExpr(height, height <= 32767);
2403
2404 AutoCaller autoCaller(this);
2405 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2406
2407 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2408
2409 if (!mpDrv)
2410 return E_FAIL;
2411
2412 Console::SafeVMPtr ptrVM(mParent);
2413 if (!ptrVM.isOk())
2414 return ptrVM.rc();
2415
2416 HRESULT rc = S_OK;
2417
2418 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
2419
2420 /* Release lock because other thread (EMT) is called and it may initiate a resize
2421 * which also needs lock.
2422 *
2423 * This method does not need the lock anymore.
2424 */
2425 alock.release();
2426
2427 int vrc = displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, address, width, height);
2428
2429 if (vrc == VERR_NOT_IMPLEMENTED)
2430 rc = setError(E_NOTIMPL,
2431 tr("This feature is not implemented"));
2432 else if (vrc == VERR_TRY_AGAIN)
2433 rc = setError(E_UNEXPECTED,
2434 tr("This feature is not available at this time"));
2435 else if (RT_FAILURE(vrc))
2436 rc = setError(VBOX_E_IPRT_ERROR,
2437 tr("Could not take a screenshot (%Rrc)"), vrc);
2438
2439 LogRelFlowFunc(("rc=%08X\n", rc));
2440 return rc;
2441}
2442
2443STDMETHODIMP Display::TakeScreenShotToArray(ULONG aScreenId, ULONG width, ULONG height,
2444 ComSafeArrayOut(BYTE, aScreenData))
2445{
2446 LogRelFlowFunc(("width=%d, height=%d\n", width, height));
2447
2448 CheckComArgOutSafeArrayPointerValid(aScreenData);
2449 CheckComArgExpr(width, width != 0);
2450 CheckComArgExpr(height, height != 0);
2451
2452 /* Do not allow too large screenshots. This also filters out negative
2453 * values passed as either 'width' or 'height'.
2454 */
2455 CheckComArgExpr(width, width <= 32767);
2456 CheckComArgExpr(height, height <= 32767);
2457
2458 AutoCaller autoCaller(this);
2459 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2460
2461 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2462
2463 if (!mpDrv)
2464 return E_FAIL;
2465
2466 Console::SafeVMPtr ptrVM(mParent);
2467 if (!ptrVM.isOk())
2468 return ptrVM.rc();
2469
2470 HRESULT rc = S_OK;
2471
2472 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
2473
2474 /* Release lock because other thread (EMT) is called and it may initiate a resize
2475 * which also needs lock.
2476 *
2477 * This method does not need the lock anymore.
2478 */
2479 alock.release();
2480
2481 size_t cbData = width * 4 * height;
2482 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
2483
2484 if (!pu8Data)
2485 return E_OUTOFMEMORY;
2486
2487 int vrc = displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, pu8Data, width, height);
2488
2489 if (RT_SUCCESS(vrc))
2490 {
2491 /* Convert pixels to format expected by the API caller: [0] R, [1] G, [2] B, [3] A. */
2492 uint8_t *pu8 = pu8Data;
2493 unsigned cPixels = width * height;
2494 while (cPixels)
2495 {
2496 uint8_t u8 = pu8[0];
2497 pu8[0] = pu8[2];
2498 pu8[2] = u8;
2499 pu8[3] = 0xff;
2500 cPixels--;
2501 pu8 += 4;
2502 }
2503
2504 com::SafeArray<BYTE> screenData(cbData);
2505 screenData.initFrom(pu8Data, cbData);
2506 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
2507 }
2508 else if (vrc == VERR_NOT_IMPLEMENTED)
2509 rc = setError(E_NOTIMPL,
2510 tr("This feature is not implemented"));
2511 else
2512 rc = setError(VBOX_E_IPRT_ERROR,
2513 tr("Could not take a screenshot (%Rrc)"), vrc);
2514
2515 RTMemFree(pu8Data);
2516
2517 LogRelFlowFunc(("rc=%08X\n", rc));
2518 return rc;
2519}
2520
2521STDMETHODIMP Display::TakeScreenShotPNGToArray(ULONG aScreenId, ULONG width, ULONG height,
2522 ComSafeArrayOut(BYTE, aScreenData))
2523{
2524 LogRelFlowFunc(("width=%d, height=%d\n", width, height));
2525
2526 CheckComArgOutSafeArrayPointerValid(aScreenData);
2527 CheckComArgExpr(width, width != 0);
2528 CheckComArgExpr(height, height != 0);
2529
2530 /* Do not allow too large screenshots. This also filters out negative
2531 * values passed as either 'width' or 'height'.
2532 */
2533 CheckComArgExpr(width, width <= 32767);
2534 CheckComArgExpr(height, height <= 32767);
2535
2536 AutoCaller autoCaller(this);
2537 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2538
2539 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2540
2541 CHECK_CONSOLE_DRV(mpDrv);
2542
2543 Console::SafeVMPtr ptrVM(mParent);
2544 if (!ptrVM.isOk())
2545 return ptrVM.rc();
2546
2547 HRESULT rc = S_OK;
2548
2549 LogRelFlowFunc(("Sending SCREENSHOT request\n"));
2550
2551 /* Release lock because other thread (EMT) is called and it may initiate a resize
2552 * which also needs lock.
2553 *
2554 * This method does not need the lock anymore.
2555 */
2556 alock.release();
2557
2558 size_t cbData = width * 4 * height;
2559 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
2560
2561 if (!pu8Data)
2562 return E_OUTOFMEMORY;
2563
2564 int vrc = displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, pu8Data, width, height);
2565
2566 if (RT_SUCCESS(vrc))
2567 {
2568 uint8_t *pu8PNG = NULL;
2569 uint32_t cbPNG = 0;
2570 uint32_t cxPNG = 0;
2571 uint32_t cyPNG = 0;
2572
2573 vrc = DisplayMakePNG(pu8Data, width, height, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2574 if (RT_SUCCESS(vrc))
2575 {
2576 com::SafeArray<BYTE> screenData(cbPNG);
2577 screenData.initFrom(pu8PNG, cbPNG);
2578 if (pu8PNG)
2579 RTMemFree(pu8PNG);
2580
2581 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
2582 }
2583 else
2584 {
2585 if (pu8PNG)
2586 RTMemFree(pu8PNG);
2587 rc = setError(VBOX_E_IPRT_ERROR,
2588 tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2589 }
2590 }
2591 else if (vrc == VERR_NOT_IMPLEMENTED)
2592 rc = setError(E_NOTIMPL,
2593 tr("This feature is not implemented"));
2594 else
2595 rc = setError(VBOX_E_IPRT_ERROR,
2596 tr("Could not take a screenshot (%Rrc)"), vrc);
2597
2598 RTMemFree(pu8Data);
2599
2600 LogRelFlowFunc(("rc=%08X\n", rc));
2601 return rc;
2602}
2603
2604
2605int Display::drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address, ULONG x, ULONG y, ULONG width, ULONG height)
2606{
2607 int rc = VINF_SUCCESS;
2608 pDisplay->vbvaLock();
2609
2610 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2611
2612 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2613 {
2614 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
2615 {
2616 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2617 }
2618 }
2619 else if (aScreenId < pDisplay->mcMonitors)
2620 {
2621 /* Copy the bitmap to the guest VRAM. */
2622 const uint8_t *pu8Src = address;
2623 int32_t xSrc = 0;
2624 int32_t ySrc = 0;
2625 uint32_t u32SrcWidth = width;
2626 uint32_t u32SrcHeight = height;
2627 uint32_t u32SrcLineSize = width * 4;
2628 uint32_t u32SrcBitsPerPixel = 32;
2629
2630 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2631 int32_t xDst = x;
2632 int32_t yDst = y;
2633 uint32_t u32DstWidth = pFBInfo->w;
2634 uint32_t u32DstHeight = pFBInfo->h;
2635 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2636 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2637
2638 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2639 width, height,
2640 pu8Src,
2641 xSrc, ySrc,
2642 u32SrcWidth, u32SrcHeight,
2643 u32SrcLineSize, u32SrcBitsPerPixel,
2644 pu8Dst,
2645 xDst, yDst,
2646 u32DstWidth, u32DstHeight,
2647 u32DstLineSize, u32DstBitsPerPixel);
2648 if (RT_SUCCESS(rc))
2649 {
2650 if (!pFBInfo->pFramebuffer.isNull())
2651 {
2652 /* Update the changed screen area. When framebuffer uses VRAM directly, just notify
2653 * it to update. And for default format, render the guest VRAM to framebuffer.
2654 */
2655 if ( pFBInfo->fDefaultFormat
2656 && !pFBInfo->fDisabled)
2657 {
2658 address = NULL;
2659 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2660 if (SUCCEEDED(hrc) && address != NULL)
2661 {
2662 pu8Src = pFBInfo->pu8FramebufferVRAM;
2663 xSrc = x;
2664 ySrc = y;
2665 u32SrcWidth = pFBInfo->w;
2666 u32SrcHeight = pFBInfo->h;
2667 u32SrcLineSize = pFBInfo->u32LineSize;
2668 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2669
2670 /* Default format is 32 bpp. */
2671 pu8Dst = address;
2672 xDst = xSrc;
2673 yDst = ySrc;
2674 u32DstWidth = u32SrcWidth;
2675 u32DstHeight = u32SrcHeight;
2676 u32DstLineSize = u32DstWidth * 4;
2677 u32DstBitsPerPixel = 32;
2678
2679 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2680 width, height,
2681 pu8Src,
2682 xSrc, ySrc,
2683 u32SrcWidth, u32SrcHeight,
2684 u32SrcLineSize, u32SrcBitsPerPixel,
2685 pu8Dst,
2686 xDst, yDst,
2687 u32DstWidth, u32DstHeight,
2688 u32DstLineSize, u32DstBitsPerPixel);
2689 }
2690 }
2691
2692 pDisplay->handleDisplayUpdate(aScreenId, x, y, width, height);
2693 }
2694 }
2695 }
2696 else
2697 {
2698 rc = VERR_INVALID_PARAMETER;
2699 }
2700
2701 if ( RT_SUCCESS(rc)
2702 && pDisplay->maFramebuffers[aScreenId].u32ResizeStatus == ResizeStatus_Void)
2703 pDisplay->mParent->consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
2704
2705 pDisplay->vbvaUnlock();
2706 return rc;
2707}
2708
2709STDMETHODIMP Display::DrawToScreen (ULONG aScreenId, BYTE *address, ULONG x, ULONG y,
2710 ULONG width, ULONG height)
2711{
2712 /// @todo (r=dmik) this function may take too long to complete if the VM
2713 // is doing something like saving state right now. Which, in case if it
2714 // is called on the GUI thread, will make it unresponsive. We should
2715 // check the machine state here (by enclosing the check and VMRequCall
2716 // within the Console lock to make it atomic).
2717
2718 LogRelFlowFunc(("address=%p, x=%d, y=%d, width=%d, height=%d\n",
2719 (void *)address, x, y, width, height));
2720
2721 CheckComArgNotNull(address);
2722 CheckComArgExpr(width, width != 0);
2723 CheckComArgExpr(height, height != 0);
2724
2725 AutoCaller autoCaller(this);
2726 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2727
2728 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2729
2730 CHECK_CONSOLE_DRV(mpDrv);
2731
2732 Console::SafeVMPtr ptrVM(mParent);
2733 if (!ptrVM.isOk())
2734 return ptrVM.rc();
2735
2736 /* Release lock because the call scheduled on EMT may also try to take it. */
2737 alock.release();
2738
2739 /*
2740 * Again we're lazy and make the graphics device do all the
2741 * dirty conversion work.
2742 */
2743 int rcVBox = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::drawToScreenEMT, 7,
2744 this, aScreenId, address, x, y, width, height);
2745
2746 /*
2747 * If the function returns not supported, we'll have to do all the
2748 * work ourselves using the framebuffer.
2749 */
2750 HRESULT rc = S_OK;
2751 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2752 {
2753 /** @todo implement generic fallback for screen blitting. */
2754 rc = E_NOTIMPL;
2755 }
2756 else if (RT_FAILURE(rcVBox))
2757 rc = setError(VBOX_E_IPRT_ERROR,
2758 tr("Could not draw to the screen (%Rrc)"), rcVBox);
2759//@todo
2760// else
2761// {
2762// /* All ok. Redraw the screen. */
2763// handleDisplayUpdate (x, y, width, height);
2764// }
2765
2766 LogRelFlowFunc(("rc=%08X\n", rc));
2767 return rc;
2768}
2769
2770void Display::InvalidateAndUpdateEMT(Display *pDisplay, unsigned uId, bool fUpdateAll)
2771{
2772 pDisplay->vbvaLock();
2773 unsigned uScreenId;
2774 for (uScreenId = (fUpdateAll ? 0 : uId); uScreenId < pDisplay->mcMonitors; uScreenId++)
2775 {
2776 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2777
2778 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
2779 {
2780 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort);
2781 }
2782 else
2783 {
2784 if ( !pFBInfo->pFramebuffer.isNull()
2785 && !pFBInfo->fDisabled
2786 && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
2787 {
2788 /* Render complete VRAM screen to the framebuffer.
2789 * When framebuffer uses VRAM directly, just notify it to update.
2790 */
2791 if (pFBInfo->fDefaultFormat)
2792 {
2793 BYTE *address = NULL;
2794 ULONG uWidth = 0;
2795 ULONG uHeight = 0;
2796 pFBInfo->pFramebuffer->COMGETTER(Width) (&uWidth);
2797 pFBInfo->pFramebuffer->COMGETTER(Height) (&uHeight);
2798 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2799 if (SUCCEEDED(hrc) && address != NULL)
2800 {
2801 uint32_t width = pFBInfo->w;
2802 uint32_t height = pFBInfo->h;
2803
2804 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2805 int32_t xSrc = 0;
2806 int32_t ySrc = 0;
2807 uint32_t u32SrcWidth = pFBInfo->w;
2808 uint32_t u32SrcHeight = pFBInfo->h;
2809 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2810 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2811
2812 /* Default format is 32 bpp. */
2813 uint8_t *pu8Dst = address;
2814 int32_t xDst = xSrc;
2815 int32_t yDst = ySrc;
2816 uint32_t u32DstWidth = u32SrcWidth;
2817 uint32_t u32DstHeight = u32SrcHeight;
2818 uint32_t u32DstLineSize = u32DstWidth * 4;
2819 uint32_t u32DstBitsPerPixel = 32;
2820
2821 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
2822 * implies resize of Framebuffer is in progress and
2823 * copyrect should not be called.
2824 */
2825 if (uWidth == pFBInfo->w && uHeight == pFBInfo->h)
2826 {
2827
2828 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2829 width, height,
2830 pu8Src,
2831 xSrc, ySrc,
2832 u32SrcWidth, u32SrcHeight,
2833 u32SrcLineSize, u32SrcBitsPerPixel,
2834 pu8Dst,
2835 xDst, yDst,
2836 u32DstWidth, u32DstHeight,
2837 u32DstLineSize, u32DstBitsPerPixel);
2838 }
2839 }
2840 }
2841
2842 pDisplay->handleDisplayUpdate (uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2843 }
2844 }
2845 if (!fUpdateAll)
2846 break;
2847 }
2848 pDisplay->vbvaUnlock();
2849}
2850
2851/**
2852 * Does a full invalidation of the VM display and instructs the VM
2853 * to update it immediately.
2854 *
2855 * @returns COM status code
2856 */
2857STDMETHODIMP Display::InvalidateAndUpdate()
2858{
2859 LogRelFlowFunc(("\n"));
2860
2861 AutoCaller autoCaller(this);
2862 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2863
2864 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2865
2866 CHECK_CONSOLE_DRV(mpDrv);
2867
2868 Console::SafeVMPtr ptrVM(mParent);
2869 if (!ptrVM.isOk())
2870 return ptrVM.rc();
2871
2872 HRESULT rc = S_OK;
2873
2874 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
2875
2876 /* Have to release the lock when calling EMT. */
2877 alock.release();
2878
2879 /* pdm.h says that this has to be called from the EMT thread */
2880 int rcVBox = VMR3ReqCallVoidWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::InvalidateAndUpdateEMT,
2881 3, this, 0, true);
2882 alock.acquire();
2883
2884 if (RT_FAILURE(rcVBox))
2885 rc = setError(VBOX_E_IPRT_ERROR,
2886 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2887
2888 LogRelFlowFunc(("rc=%08X\n", rc));
2889 return rc;
2890}
2891
2892/**
2893 * Notification that the framebuffer has completed the
2894 * asynchronous resize processing
2895 *
2896 * @returns COM status code
2897 */
2898STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
2899{
2900 LogRelFlowFunc(("\n"));
2901
2902 /// @todo (dmik) can we AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); here?
2903 // This will require general code review and may add some details.
2904 // In particular, we may want to check whether EMT is really waiting for
2905 // this notification, etc. It might be also good to obey the caller to make
2906 // sure this method is not called from more than one thread at a time
2907 // (and therefore don't use Display lock at all here to save some
2908 // milliseconds).
2909 AutoCaller autoCaller(this);
2910 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2911
2912 /* this is only valid for external framebuffers */
2913 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
2914 return setError(VBOX_E_NOT_SUPPORTED,
2915 tr("Resize completed notification is valid only for external framebuffers"));
2916
2917 /* Set the flag indicating that the resize has completed and display
2918 * data need to be updated. */
2919 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
2920 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
2921 AssertRelease(f);NOREF(f);
2922
2923 return S_OK;
2924}
2925
2926STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
2927{
2928#ifdef VBOX_WITH_VIDEOHWACCEL
2929 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsynch(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
2930 return S_OK;
2931#else
2932 return E_NOTIMPL;
2933#endif
2934}
2935
2936STDMETHODIMP Display::ViewportChanged(ULONG aScreenId, ULONG x, ULONG y, ULONG width, ULONG height)
2937{
2938#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
2939 BOOL is3denabled;
2940 mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
2941
2942 if (is3denabled)
2943 {
2944 VBOXHGCMSVCPARM aParms[5];
2945
2946 aParms[0].type = VBOX_HGCM_SVC_PARM_32BIT;
2947 aParms[0].u.uint32 = aScreenId;
2948
2949 aParms[1].type = VBOX_HGCM_SVC_PARM_32BIT;
2950 aParms[1].u.uint32 = x;
2951
2952 aParms[2].type = VBOX_HGCM_SVC_PARM_32BIT;
2953 aParms[2].u.uint32 = y;
2954
2955
2956 aParms[3].type = VBOX_HGCM_SVC_PARM_32BIT;
2957 aParms[3].u.uint32 = width;
2958
2959 aParms[4].type = VBOX_HGCM_SVC_PARM_32BIT;
2960 aParms[4].u.uint32 = height;
2961
2962 VMMDev *pVMMDev = mParent->getVMMDev();
2963
2964 if (pVMMDev)
2965 pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_VIEWPORT_CHANGED, SHCRGL_CPARMS_VIEWPORT_CHANGED, aParms);
2966 }
2967#endif /* VBOX_WITH_CROGL && VBOX_WITH_HGCM */
2968 return S_OK;
2969}
2970
2971// private methods
2972/////////////////////////////////////////////////////////////////////////////
2973
2974/**
2975 * Helper to update the display information from the framebuffer.
2976 *
2977 * @thread EMT
2978 */
2979void Display::updateDisplayData(void)
2980{
2981 LogRelFlowFunc(("\n"));
2982
2983 /* the driver might not have been constructed yet */
2984 if (!mpDrv)
2985 return;
2986
2987#ifdef VBOX_STRICT
2988 /*
2989 * Sanity check. Note that this method may be called on EMT after Console
2990 * has started the power down procedure (but before our #drvDestruct() is
2991 * called, in which case pVM will already be NULL but mpDrv will not). Since
2992 * we don't really need pVM to proceed, we avoid this check in the release
2993 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
2994 * time-critical method.
2995 */
2996 Console::SafeVMPtrQuiet ptrVM(mParent);
2997 if (ptrVM.isOk())
2998 {
2999 PVM pVM = VMR3GetVM(ptrVM.rawUVM());
3000 Assert(VM_IS_EMT(pVM));
3001 }
3002#endif
3003
3004 /* The method is only relevant to the primary framebuffer. */
3005 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
3006
3007 if (pFramebuffer)
3008 {
3009 HRESULT rc;
3010 BYTE *address = 0;
3011 rc = pFramebuffer->COMGETTER(Address) (&address);
3012 AssertComRC (rc);
3013 ULONG bytesPerLine = 0;
3014 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
3015 AssertComRC (rc);
3016 ULONG bitsPerPixel = 0;
3017 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
3018 AssertComRC (rc);
3019 ULONG width = 0;
3020 rc = pFramebuffer->COMGETTER(Width) (&width);
3021 AssertComRC (rc);
3022 ULONG height = 0;
3023 rc = pFramebuffer->COMGETTER(Height) (&height);
3024 AssertComRC (rc);
3025
3026 mpDrv->IConnector.pu8Data = (uint8_t *) address;
3027 mpDrv->IConnector.cbScanline = bytesPerLine;
3028 mpDrv->IConnector.cBits = bitsPerPixel;
3029 mpDrv->IConnector.cx = width;
3030 mpDrv->IConnector.cy = height;
3031 }
3032 else
3033 {
3034 /* black hole */
3035 mpDrv->IConnector.pu8Data = NULL;
3036 mpDrv->IConnector.cbScanline = 0;
3037 mpDrv->IConnector.cBits = 0;
3038 mpDrv->IConnector.cx = 0;
3039 mpDrv->IConnector.cy = 0;
3040 }
3041 LogRelFlowFunc(("leave\n"));
3042}
3043
3044#ifdef VBOX_WITH_CRHGSMI
3045void Display::setupCrHgsmiData(void)
3046{
3047 VMMDev *pVMMDev = mParent->getVMMDev();
3048 Assert(pVMMDev);
3049 int rc = VERR_GENERAL_FAILURE;
3050 if (pVMMDev)
3051 rc = pVMMDev->hgcmHostSvcHandleCreate("VBoxSharedCrOpenGL", &mhCrOglSvc);
3052
3053 if (RT_SUCCESS(rc))
3054 {
3055 Assert(mhCrOglSvc);
3056 /* setup command completion callback */
3057 VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_COMPLETION Completion;
3058 Completion.Hdr.enmType = VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION;
3059 Completion.Hdr.cbCmd = sizeof (Completion);
3060 Completion.hCompletion = mpDrv->pVBVACallbacks;
3061 Completion.pfnCompletion = mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync;
3062
3063 VBOXHGCMSVCPARM parm;
3064 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3065 parm.u.pointer.addr = &Completion;
3066 parm.u.pointer.size = 0;
3067
3068 rc = pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_CRHGSMI_CTL, 1, &parm);
3069 if (RT_SUCCESS(rc))
3070 return;
3071
3072 AssertMsgFailed(("VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION failed rc %d", rc));
3073 }
3074
3075 mhCrOglSvc = NULL;
3076}
3077
3078void Display::destructCrHgsmiData(void)
3079{
3080 mhCrOglSvc = NULL;
3081}
3082#endif
3083
3084/**
3085 * Changes the current frame buffer. Called on EMT to avoid both
3086 * race conditions and excessive locking.
3087 *
3088 * @note locks this object for writing
3089 * @thread EMT
3090 */
3091/* static */
3092DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
3093 unsigned uScreenId)
3094{
3095 LogRelFlowFunc(("uScreenId = %d\n", uScreenId));
3096
3097 AssertReturn(that, VERR_INVALID_PARAMETER);
3098 AssertReturn(uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
3099
3100 AutoCaller autoCaller(that);
3101 if (FAILED(autoCaller.rc())) return autoCaller.rc();
3102
3103 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
3104
3105 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
3106 pDisplayFBInfo->pFramebuffer = aFB;
3107
3108 that->mParent->consoleVRDPServer()->SendResize ();
3109
3110 /* The driver might not have been constructed yet */
3111 if (that->mpDrv)
3112 {
3113 /* Setup the new framebuffer, the resize will lead to an updateDisplayData call. */
3114 DISPLAYFBINFO *pFBInfo = &that->maFramebuffers[uScreenId];
3115
3116#if defined(VBOX_WITH_CROGL)
3117 /* Release the lock, because SHCRGL_HOST_FN_SCREEN_CHANGED will read current framebuffer */
3118 {
3119 BOOL is3denabled;
3120 that->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3121
3122 if (is3denabled)
3123 {
3124 alock.release();
3125 }
3126 }
3127#endif
3128
3129 if (pFBInfo->fVBVAEnabled && pFBInfo->pu8FramebufferVRAM)
3130 {
3131 /* This display in VBVA mode. Resize it to the last guest resolution,
3132 * if it has been reported.
3133 */
3134 that->handleDisplayResize(uScreenId, pFBInfo->u16BitsPerPixel,
3135 pFBInfo->pu8FramebufferVRAM,
3136 pFBInfo->u32LineSize,
3137 pFBInfo->w,
3138 pFBInfo->h,
3139 pFBInfo->flags);
3140 }
3141 else if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3142 {
3143 /* VGA device mode, only for the primary screen. */
3144 that->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, that->mLastBitsPerPixel,
3145 that->mLastAddress,
3146 that->mLastBytesPerLine,
3147 that->mLastWidth,
3148 that->mLastHeight,
3149 that->mLastFlags);
3150 }
3151 }
3152
3153 LogRelFlowFunc(("leave\n"));
3154 return VINF_SUCCESS;
3155}
3156
3157/**
3158 * Handle display resize event issued by the VGA device for the primary screen.
3159 *
3160 * @see PDMIDISPLAYCONNECTOR::pfnResize
3161 */
3162DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
3163 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
3164{
3165 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3166
3167 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
3168 bpp, pvVRAM, cbLine, cx, cy));
3169
3170 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, VBVA_SCREEN_F_ACTIVE);
3171}
3172
3173/**
3174 * Handle display update.
3175 *
3176 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
3177 */
3178DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
3179 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
3180{
3181 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3182
3183#ifdef DEBUG_sunlover
3184 LogFlowFunc(("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
3185 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
3186#endif /* DEBUG_sunlover */
3187
3188 /* This call does update regardless of VBVA status.
3189 * But in VBVA mode this is called only as result of
3190 * pfnUpdateDisplayAll in the VGA device.
3191 */
3192
3193 pDrv->pDisplay->handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
3194}
3195
3196/**
3197 * Periodic display refresh callback.
3198 *
3199 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
3200 */
3201DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
3202{
3203 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3204
3205#ifdef DEBUG_sunlover
3206 STAM_PROFILE_START(&g_StatDisplayRefresh, a);
3207#endif /* DEBUG_sunlover */
3208
3209#ifdef DEBUG_sunlover_2
3210 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
3211 pDrv->pDisplay->mfVideoAccelEnabled));
3212#endif /* DEBUG_sunlover_2 */
3213
3214 Display *pDisplay = pDrv->pDisplay;
3215 bool fNoUpdate = false; /* Do not update the display if any of the framebuffers is being resized. */
3216 unsigned uScreenId;
3217
3218 Log2(("DisplayRefreshCallback\n"));
3219 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3220 {
3221 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3222
3223 /* Check the resize status. The status can be checked normally because
3224 * the status affects only the EMT.
3225 */
3226 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
3227
3228 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
3229 {
3230 LogRelFlowFunc(("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
3231 fNoUpdate = true; /* Always set it here, because pfnUpdateDisplayAll can cause a new resize. */
3232 /* The framebuffer was resized and display data need to be updated. */
3233 pDisplay->handleResizeCompletedEMT ();
3234 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
3235 {
3236 /* The resize status could be not Void here because a pending resize is issued. */
3237 continue;
3238 }
3239 /* Continue with normal processing because the status here is ResizeStatus_Void.
3240 * Repaint all displays because VM continued to run during the framebuffer resize.
3241 */
3242 pDisplay->InvalidateAndUpdateEMT(pDisplay, uScreenId, false);
3243 }
3244 else if (u32ResizeStatus == ResizeStatus_InProgress)
3245 {
3246 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
3247 LogRelFlowFunc(("ResizeStatus_InProcess\n"));
3248 fNoUpdate = true;
3249 continue;
3250 }
3251 }
3252
3253 if (!fNoUpdate)
3254 {
3255 int rc = pDisplay->videoAccelRefreshProcess();
3256 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3257 {
3258 if (rc == VWRN_INVALID_STATE)
3259 {
3260 /* No VBVA do a display update. */
3261 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3262 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3263 {
3264 Assert(pDrv->IConnector.pu8Data);
3265 pDisplay->vbvaLock();
3266 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3267 pDisplay->vbvaUnlock();
3268 }
3269 }
3270
3271 /* Inform the VRDP server that the current display update sequence is
3272 * completed. At this moment the framebuffer memory contains a definite
3273 * image, that is synchronized with the orders already sent to VRDP client.
3274 * The server can now process redraw requests from clients or initial
3275 * fullscreen updates for new clients.
3276 */
3277 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3278 {
3279 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3280
3281 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3282 {
3283 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
3284 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
3285 }
3286 }
3287 }
3288 }
3289
3290#ifdef VBOX_WITH_VPX
3291 if (VideoRecIsEnabled(pDisplay->mpVideoRecCtx))
3292 {
3293 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3294
3295 if ( !pFBInfo->pFramebuffer.isNull()
3296 && !pFBInfo->fDisabled
3297 && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3298 {
3299 uint64_t u64Now = RTTimeProgramMilliTS();
3300 int rc;
3301 if ( pFBInfo->fVBVAEnabled
3302 && pFBInfo->pu8FramebufferVRAM)
3303 {
3304 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, 0, 0,
3305 FramebufferPixelFormat_FOURCC_RGB,
3306 pFBInfo->u16BitsPerPixel,
3307 pFBInfo->u32LineSize, pFBInfo->w, pFBInfo->h,
3308 pFBInfo->pu8FramebufferVRAM, u64Now);
3309 }
3310 else
3311 {
3312 rc = VideoRecCopyToIntBuf(pDisplay->mpVideoRecCtx, 0, 0,
3313 FramebufferPixelFormat_FOURCC_RGB,
3314 pDrv->IConnector.cBits,
3315 pDrv->IConnector.cbScanline, pDrv->IConnector.cx,
3316 pDrv->IConnector.cy, pDrv->IConnector.pu8Data, u64Now);
3317 }
3318 }
3319 }
3320#endif
3321
3322#ifdef DEBUG_sunlover
3323 STAM_PROFILE_STOP(&g_StatDisplayRefresh, a);
3324#endif /* DEBUG_sunlover */
3325#ifdef DEBUG_sunlover_2
3326 LogFlowFunc(("leave\n"));
3327#endif /* DEBUG_sunlover_2 */
3328}
3329
3330/**
3331 * Reset notification
3332 *
3333 * @see PDMIDISPLAYCONNECTOR::pfnReset
3334 */
3335DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3336{
3337 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3338
3339 LogRelFlowFunc(("\n"));
3340
3341 /* Disable VBVA mode. */
3342 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3343}
3344
3345/**
3346 * LFBModeChange notification
3347 *
3348 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3349 */
3350DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3351{
3352 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3353
3354 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
3355
3356 NOREF(fEnabled);
3357
3358 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3359 /* The LFBModeChange function is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
3360 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
3361}
3362
3363/**
3364 * Adapter information change notification.
3365 *
3366 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3367 */
3368DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
3369{
3370 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3371
3372 if (pvVRAM == NULL)
3373 {
3374 unsigned i;
3375 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
3376 {
3377 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
3378
3379 pFBInfo->u32Offset = 0;
3380 pFBInfo->u32MaxFramebufferSize = 0;
3381 pFBInfo->u32InformationSize = 0;
3382 }
3383 }
3384#ifndef VBOX_WITH_HGSMI
3385 else
3386 {
3387 uint8_t *pu8 = (uint8_t *)pvVRAM;
3388 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3389
3390 // @todo
3391 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3392
3393 VBOXVIDEOINFOHDR *pHdr;
3394
3395 for (;;)
3396 {
3397 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3398 pu8 += sizeof (VBOXVIDEOINFOHDR);
3399
3400 if (pu8 >= pu8End)
3401 {
3402 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
3403 break;
3404 }
3405
3406 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
3407 {
3408 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
3409 {
3410 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
3411 break;
3412 }
3413
3414 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
3415
3416 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
3417 {
3418 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
3419 break;
3420 }
3421
3422 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
3423
3424 pFBInfo->u32Offset = pDisplay->u32Offset;
3425 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
3426 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
3427
3428 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));
3429 }
3430 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
3431 {
3432 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
3433 {
3434 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
3435 break;
3436 }
3437
3438 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
3439
3440 switch (pConf32->u32Index)
3441 {
3442 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
3443 {
3444 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
3445 } break;
3446
3447 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
3448 {
3449 /* @todo make configurable. */
3450 pConf32->u32Value = _1M;
3451 } break;
3452
3453 default:
3454 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
3455 }
3456 }
3457 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3458 {
3459 if (pHdr->u16Length != 0)
3460 {
3461 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3462 break;
3463 }
3464
3465 break;
3466 }
3467 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
3468 {
3469 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
3470 }
3471
3472 pu8 += pHdr->u16Length;
3473 }
3474 }
3475#endif /* !VBOX_WITH_HGSMI */
3476}
3477
3478/**
3479 * Display information change notification.
3480 *
3481 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3482 */
3483DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
3484{
3485 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3486
3487 if (uScreenId >= pDrv->pDisplay->mcMonitors)
3488 {
3489 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
3490 return;
3491 }
3492
3493 /* Get the display information structure. */
3494 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
3495
3496 uint8_t *pu8 = (uint8_t *)pvVRAM;
3497 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
3498
3499 // @todo
3500 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
3501
3502 VBOXVIDEOINFOHDR *pHdr;
3503
3504 for (;;)
3505 {
3506 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3507 pu8 += sizeof (VBOXVIDEOINFOHDR);
3508
3509 if (pu8 >= pu8End)
3510 {
3511 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
3512 break;
3513 }
3514
3515 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
3516 {
3517 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
3518 {
3519 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
3520 break;
3521 }
3522
3523 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
3524
3525 pFBInfo->xOrigin = pScreen->xOrigin;
3526 pFBInfo->yOrigin = pScreen->yOrigin;
3527
3528 pFBInfo->w = pScreen->u16Width;
3529 pFBInfo->h = pScreen->u16Height;
3530
3531 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
3532 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
3533
3534 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
3535 {
3536 /* Primary screen resize is eeeeeeeee by the VGA device. */
3537 if (pFBInfo->fDisabled)
3538 {
3539 pFBInfo->fDisabled = false;
3540 fireGuestMonitorChangedEvent(pDrv->pDisplay->mParent->getEventSource(),
3541 GuestMonitorChangedEventType_Enabled,
3542 uScreenId,
3543 pFBInfo->xOrigin, pFBInfo->yOrigin,
3544 pFBInfo->w, pFBInfo->h);
3545 }
3546
3547 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, VBVA_SCREEN_F_ACTIVE);
3548 }
3549 }
3550 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3551 {
3552 if (pHdr->u16Length != 0)
3553 {
3554 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3555 break;
3556 }
3557
3558 break;
3559 }
3560 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
3561 {
3562 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
3563 {
3564 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
3565 break;
3566 }
3567
3568 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
3569
3570 pFBInfo->pHostEvents = pHostEvents;
3571
3572 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
3573 pHostEvents));
3574 }
3575 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
3576 {
3577 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
3578 {
3579 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
3580 break;
3581 }
3582
3583 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
3584 pu8 += pLink->i32Offset;
3585 }
3586 else
3587 {
3588 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
3589 }
3590
3591 pu8 += pHdr->u16Length;
3592 }
3593}
3594
3595#ifdef VBOX_WITH_VIDEOHWACCEL
3596
3597void Display::handleVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3598{
3599 unsigned id = (unsigned)pCommand->iDisplay;
3600 int rc = VINF_SUCCESS;
3601 if (id < mcMonitors)
3602 {
3603 IFramebuffer *pFramebuffer = maFramebuffers[id].pFramebuffer;
3604#ifdef DEBUG_misha
3605 Assert (pFramebuffer);
3606#endif
3607
3608 if (pFramebuffer != NULL)
3609 {
3610 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3611 if (FAILED(hr))
3612 {
3613 rc = (hr == E_NOTIMPL) ? VERR_NOT_IMPLEMENTED : VERR_GENERAL_FAILURE;
3614 }
3615 }
3616 else
3617 {
3618 rc = VERR_NOT_IMPLEMENTED;
3619 }
3620 }
3621 else
3622 {
3623 rc = VERR_INVALID_PARAMETER;
3624 }
3625
3626 if (RT_FAILURE(rc))
3627 {
3628 /* tell the guest the command is complete */
3629 pCommand->Flags &= (~VBOXVHWACMD_FLAG_HG_ASYNCH);
3630 pCommand->rc = rc;
3631 }
3632}
3633
3634DECLCALLBACK(void) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3635{
3636 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3637
3638 pDrv->pDisplay->handleVHWACommandProcess(pInterface, pCommand);
3639}
3640#endif
3641
3642#ifdef VBOX_WITH_CRHGSMI
3643void Display::handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3644{
3645 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
3646}
3647
3648void Display::handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3649{
3650 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr, result);
3651}
3652
3653void Display::handleCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3654{
3655 int rc = VERR_INVALID_FUNCTION;
3656 VBOXHGCMSVCPARM parm;
3657 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3658 parm.u.pointer.addr = pCmd;
3659 parm.u.pointer.size = cbCmd;
3660
3661 if (mhCrOglSvc)
3662 {
3663 VMMDev *pVMMDev = mParent->getVMMDev();
3664 if (pVMMDev)
3665 {
3666 /* no completion callback is specified with this call,
3667 * the CrOgl code will complete the CrHgsmi command once it processes it */
3668 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
3669 AssertRC(rc);
3670 if (RT_SUCCESS(rc))
3671 return;
3672 }
3673 else
3674 rc = VERR_INVALID_STATE;
3675 }
3676
3677 /* we are here because something went wrong with command processing, complete it */
3678 handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
3679}
3680
3681void Display::handleCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
3682{
3683 int rc = VERR_INVALID_FUNCTION;
3684 VBOXHGCMSVCPARM parm;
3685 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3686 parm.u.pointer.addr = pCtl;
3687 parm.u.pointer.size = cbCtl;
3688
3689 if (mhCrOglSvc)
3690 {
3691 VMMDev *pVMMDev = mParent->getVMMDev();
3692 if (pVMMDev)
3693 {
3694 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm, Display::displayCrHgsmiControlCompletion, this);
3695 AssertRC(rc);
3696 if (RT_SUCCESS(rc))
3697 return;
3698 }
3699 else
3700 rc = VERR_INVALID_STATE;
3701 }
3702
3703 /* we are here because something went wrong with command processing, complete it */
3704 handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
3705}
3706
3707
3708DECLCALLBACK(void) Display::displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3709{
3710 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3711
3712 pDrv->pDisplay->handleCrHgsmiCommandProcess(pInterface, pCmd, cbCmd);
3713}
3714
3715DECLCALLBACK(void) Display::displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd, uint32_t cbCmd)
3716{
3717 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3718
3719 pDrv->pDisplay->handleCrHgsmiControlProcess(pInterface, pCmd, cbCmd);
3720}
3721
3722DECLCALLBACK(void) Display::displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3723{
3724 AssertMsgFailed(("not expected!"));
3725 Display *pDisplay = (Display *)pvContext;
3726 pDisplay->handleCrHgsmiCommandCompletion(result, u32Function, pParam);
3727}
3728
3729DECLCALLBACK(void) Display::displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3730{
3731 Display *pDisplay = (Display *)pvContext;
3732 pDisplay->handleCrHgsmiControlCompletion(result, u32Function, pParam);
3733}
3734#endif
3735
3736
3737#ifdef VBOX_WITH_HGSMI
3738DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags)
3739{
3740 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3741
3742 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3743 Display *pThis = pDrv->pDisplay;
3744
3745 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3746 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3747
3748 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3749
3750 return VINF_SUCCESS;
3751}
3752
3753DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3754{
3755 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3756
3757 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3758 Display *pThis = pDrv->pDisplay;
3759
3760 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3761
3762 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3763 {
3764 /* Make sure that the primary screen is visible now.
3765 * The guest can't use VBVA anymore, so only only the VGA device output works.
3766 */
3767 if (pFBInfo->fDisabled)
3768 {
3769 pFBInfo->fDisabled = false;
3770 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
3771 GuestMonitorChangedEventType_Enabled,
3772 uScreenId,
3773 pFBInfo->xOrigin, pFBInfo->yOrigin,
3774 pFBInfo->w, pFBInfo->h);
3775 }
3776 }
3777
3778 pFBInfo->fVBVAEnabled = false;
3779
3780 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3781
3782 pFBInfo->pVBVAHostFlags = NULL;
3783
3784 pFBInfo->u32Offset = 0; /* Not used in HGSMI. */
3785 pFBInfo->u32MaxFramebufferSize = 0; /* Not used in HGSMI. */
3786 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3787
3788 pFBInfo->xOrigin = 0;
3789 pFBInfo->yOrigin = 0;
3790
3791 pFBInfo->w = 0;
3792 pFBInfo->h = 0;
3793
3794 pFBInfo->u16BitsPerPixel = 0;
3795 pFBInfo->pu8FramebufferVRAM = NULL;
3796 pFBInfo->u32LineSize = 0;
3797}
3798
3799DECLCALLBACK(void) Display::displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3800{
3801 LogFlowFunc(("uScreenId %d\n", uScreenId));
3802
3803 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3804 Display *pThis = pDrv->pDisplay;
3805 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3806
3807 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3808 {
3809 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers, pThis->mcMonitors);
3810 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3811 }
3812
3813 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
3814 {
3815 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
3816 {
3817 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
3818 * under display device lock, so thread safe.
3819 */
3820 pFBInfo->cVBVASkipUpdate = 0;
3821 pThis->handleDisplayUpdate(uScreenId, pFBInfo->vbvaSkippedRect.xLeft - pFBInfo->xOrigin,
3822 pFBInfo->vbvaSkippedRect.yTop - pFBInfo->yOrigin,
3823 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
3824 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
3825 }
3826 }
3827 else
3828 {
3829 /* The framebuffer is being resized. */
3830 pFBInfo->cVBVASkipUpdate++;
3831 }
3832}
3833
3834DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, const PVBVACMDHDR pCmd, size_t cbCmd)
3835{
3836 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3837
3838 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3839 Display *pThis = pDrv->pDisplay;
3840 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3841
3842 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3843 {
3844 if (pFBInfo->fDefaultFormat)
3845 {
3846 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3847 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3848 && !pFBInfo->pFramebuffer.isNull()
3849 && !pFBInfo->fDisabled)
3850 {
3851 pDrv->pUpPort->pfnUpdateDisplayRect (pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
3852 }
3853 else if ( !pFBInfo->pFramebuffer.isNull()
3854 && !pFBInfo->fDisabled)
3855 {
3856 /* Render VRAM content to the framebuffer. */
3857 BYTE *address = NULL;
3858 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
3859 if (SUCCEEDED(hrc) && address != NULL)
3860 {
3861 uint32_t width = pCmd->w;
3862 uint32_t height = pCmd->h;
3863
3864 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3865 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
3866 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
3867 uint32_t u32SrcWidth = pFBInfo->w;
3868 uint32_t u32SrcHeight = pFBInfo->h;
3869 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3870 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3871
3872 uint8_t *pu8Dst = address;
3873 int32_t xDst = xSrc;
3874 int32_t yDst = ySrc;
3875 uint32_t u32DstWidth = u32SrcWidth;
3876 uint32_t u32DstHeight = u32SrcHeight;
3877 uint32_t u32DstLineSize = u32DstWidth * 4;
3878 uint32_t u32DstBitsPerPixel = 32;
3879
3880 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3881 width, height,
3882 pu8Src,
3883 xSrc, ySrc,
3884 u32SrcWidth, u32SrcHeight,
3885 u32SrcLineSize, u32SrcBitsPerPixel,
3886 pu8Dst,
3887 xDst, yDst,
3888 u32DstWidth, u32DstHeight,
3889 u32DstLineSize, u32DstBitsPerPixel);
3890 }
3891 }
3892 }
3893
3894 VBVACMDHDR hdrSaved = *pCmd;
3895
3896 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3897
3898 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3899 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3900
3901 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
3902 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, (uint32_t)cbCmd);
3903
3904 *pHdrUnconst = hdrSaved;
3905 }
3906}
3907
3908DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y, uint32_t cx, uint32_t cy)
3909{
3910 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3911
3912 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3913 Display *pThis = pDrv->pDisplay;
3914 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3915
3916 /* @todo handleFramebufferUpdate (uScreenId,
3917 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3918 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3919 * cx, cy);
3920 */
3921 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3922 {
3923 pThis->handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3924 }
3925 else
3926 {
3927 /* Save the updated rectangle. */
3928 int32_t xRight = x + cx;
3929 int32_t yBottom = y + cy;
3930
3931 if (pFBInfo->cVBVASkipUpdate == 1)
3932 {
3933 pFBInfo->vbvaSkippedRect.xLeft = x;
3934 pFBInfo->vbvaSkippedRect.yTop = y;
3935 pFBInfo->vbvaSkippedRect.xRight = xRight;
3936 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3937 }
3938 else
3939 {
3940 if (pFBInfo->vbvaSkippedRect.xLeft > x)
3941 {
3942 pFBInfo->vbvaSkippedRect.xLeft = x;
3943 }
3944 if (pFBInfo->vbvaSkippedRect.yTop > y)
3945 {
3946 pFBInfo->vbvaSkippedRect.yTop = y;
3947 }
3948 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
3949 {
3950 pFBInfo->vbvaSkippedRect.xRight = xRight;
3951 }
3952 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
3953 {
3954 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3955 }
3956 }
3957 }
3958}
3959
3960#ifdef DEBUG_sunlover
3961static void logVBVAResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
3962{
3963 LogRel(("displayVBVAResize: [%d] %s\n"
3964 " pView->u32ViewIndex %d\n"
3965 " pView->u32ViewOffset 0x%08X\n"
3966 " pView->u32ViewSize 0x%08X\n"
3967 " pView->u32MaxScreenSize 0x%08X\n"
3968 " pScreen->i32OriginX %d\n"
3969 " pScreen->i32OriginY %d\n"
3970 " pScreen->u32StartOffset 0x%08X\n"
3971 " pScreen->u32LineSize 0x%08X\n"
3972 " pScreen->u32Width %d\n"
3973 " pScreen->u32Height %d\n"
3974 " pScreen->u16BitsPerPixel %d\n"
3975 " pScreen->u16Flags 0x%04X\n"
3976 " pFBInfo->u32Offset 0x%08X\n"
3977 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
3978 " pFBInfo->u32InformationSize 0x%08X\n"
3979 " pFBInfo->fDisabled %d\n"
3980 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
3981 " pFBInfo->u16BitsPerPixel %d\n"
3982 " pFBInfo->pu8FramebufferVRAM %p\n"
3983 " pFBInfo->u32LineSize 0x%08X\n"
3984 " pFBInfo->flags 0x%04X\n"
3985 " pFBInfo->pHostEvents %p\n"
3986 " pFBInfo->u32ResizeStatus %d\n"
3987 " pFBInfo->fDefaultFormat %d\n"
3988 " dirtyRect %d-%d %d-%d\n"
3989 " pFBInfo->pendingResize.fPending %d\n"
3990 " pFBInfo->pendingResize.pixelFormat %d\n"
3991 " pFBInfo->pendingResize.pvVRAM %p\n"
3992 " pFBInfo->pendingResize.bpp %d\n"
3993 " pFBInfo->pendingResize.cbLine 0x%08X\n"
3994 " pFBInfo->pendingResize.w,h %dx%d\n"
3995 " pFBInfo->pendingResize.flags 0x%04X\n"
3996 " pFBInfo->fVBVAEnabled %d\n"
3997 " pFBInfo->cVBVASkipUpdate %d\n"
3998 " pFBInfo->vbvaSkippedRect %d-%d %d-%d\n"
3999 " pFBInfo->pVBVAHostFlags %p\n"
4000 "",
4001 pScreen->u32ViewIndex,
4002 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
4003 pView->u32ViewIndex,
4004 pView->u32ViewOffset,
4005 pView->u32ViewSize,
4006 pView->u32MaxScreenSize,
4007 pScreen->i32OriginX,
4008 pScreen->i32OriginY,
4009 pScreen->u32StartOffset,
4010 pScreen->u32LineSize,
4011 pScreen->u32Width,
4012 pScreen->u32Height,
4013 pScreen->u16BitsPerPixel,
4014 pScreen->u16Flags,
4015 pFBInfo->u32Offset,
4016 pFBInfo->u32MaxFramebufferSize,
4017 pFBInfo->u32InformationSize,
4018 pFBInfo->fDisabled,
4019 pFBInfo->xOrigin,
4020 pFBInfo->yOrigin,
4021 pFBInfo->w,
4022 pFBInfo->h,
4023 pFBInfo->u16BitsPerPixel,
4024 pFBInfo->pu8FramebufferVRAM,
4025 pFBInfo->u32LineSize,
4026 pFBInfo->flags,
4027 pFBInfo->pHostEvents,
4028 pFBInfo->u32ResizeStatus,
4029 pFBInfo->fDefaultFormat,
4030 pFBInfo->dirtyRect.xLeft,
4031 pFBInfo->dirtyRect.xRight,
4032 pFBInfo->dirtyRect.yTop,
4033 pFBInfo->dirtyRect.yBottom,
4034 pFBInfo->pendingResize.fPending,
4035 pFBInfo->pendingResize.pixelFormat,
4036 pFBInfo->pendingResize.pvVRAM,
4037 pFBInfo->pendingResize.bpp,
4038 pFBInfo->pendingResize.cbLine,
4039 pFBInfo->pendingResize.w,
4040 pFBInfo->pendingResize.h,
4041 pFBInfo->pendingResize.flags,
4042 pFBInfo->fVBVAEnabled,
4043 pFBInfo->cVBVASkipUpdate,
4044 pFBInfo->vbvaSkippedRect.xLeft,
4045 pFBInfo->vbvaSkippedRect.yTop,
4046 pFBInfo->vbvaSkippedRect.xRight,
4047 pFBInfo->vbvaSkippedRect.yBottom,
4048 pFBInfo->pVBVAHostFlags
4049 ));
4050}
4051#endif /* DEBUG_sunlover */
4052
4053DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
4054{
4055 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
4056
4057 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4058 Display *pThis = pDrv->pDisplay;
4059
4060 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
4061
4062 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
4063 {
4064 pFBInfo->fDisabled = true;
4065 pFBInfo->flags = pScreen->u16Flags;
4066
4067 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
4068 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
4069 * the VM window will be black. */
4070 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
4071 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
4072 pThis->handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
4073 u32Width, u32Height, pScreen->u16Flags);
4074
4075 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4076 GuestMonitorChangedEventType_Disabled,
4077 pScreen->u32ViewIndex,
4078 0, 0, 0, 0);
4079 return VINF_SUCCESS;
4080 }
4081
4082 /* If display was disabled or there is no framebuffer, a resize will be required,
4083 * because the framebuffer was/will be changed.
4084 */
4085 bool fResize = pFBInfo->fDisabled || pFBInfo->pFramebuffer.isNull();
4086
4087 if (pFBInfo->fDisabled)
4088 {
4089 pFBInfo->fDisabled = false;
4090 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4091 GuestMonitorChangedEventType_Enabled,
4092 pScreen->u32ViewIndex,
4093 pScreen->i32OriginX, pScreen->i32OriginY,
4094 pScreen->u32Width, pScreen->u32Height);
4095 /* Continue to update pFBInfo. */
4096 }
4097
4098 /* Check if this is a real resize or a notification about the screen origin.
4099 * The guest uses this VBVAResize call for both.
4100 */
4101 fResize = fResize
4102 || pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
4103 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
4104 || pFBInfo->u32LineSize != pScreen->u32LineSize
4105 || pFBInfo->w != pScreen->u32Width
4106 || pFBInfo->h != pScreen->u32Height;
4107
4108 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
4109 || pFBInfo->yOrigin != pScreen->i32OriginY;
4110
4111 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
4112 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
4113 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
4114
4115 pFBInfo->xOrigin = pScreen->i32OriginX;
4116 pFBInfo->yOrigin = pScreen->i32OriginY;
4117
4118 pFBInfo->w = pScreen->u32Width;
4119 pFBInfo->h = pScreen->u32Height;
4120
4121 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
4122 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
4123 pFBInfo->u32LineSize = pScreen->u32LineSize;
4124
4125 pFBInfo->flags = pScreen->u16Flags;
4126
4127 if (fNewOrigin)
4128 {
4129 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4130 GuestMonitorChangedEventType_NewOrigin,
4131 pScreen->u32ViewIndex,
4132 pScreen->i32OriginX, pScreen->i32OriginY,
4133 0, 0);
4134 }
4135
4136#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4137 if (fNewOrigin && !fResize)
4138 {
4139 BOOL is3denabled;
4140 pThis->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
4141
4142 if (is3denabled)
4143 {
4144 VBOXHGCMSVCPARM parm;
4145
4146 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
4147 parm.u.uint32 = pScreen->u32ViewIndex;
4148
4149 VMMDev *pVMMDev = pThis->mParent->getVMMDev();
4150
4151 if (pVMMDev)
4152 pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED, SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
4153 }
4154 }
4155#endif /* VBOX_WITH_CROGL */
4156
4157 if (!fResize)
4158 {
4159 /* No parameters of the framebuffer have actually changed. */
4160 if (fNewOrigin)
4161 {
4162 /* VRDP server still need this notification. */
4163 LogRelFlowFunc(("Calling VRDP\n"));
4164 pThis->mParent->consoleVRDPServer()->SendResize();
4165 }
4166 return VINF_SUCCESS;
4167 }
4168
4169 if (pFBInfo->pFramebuffer.isNull())
4170 {
4171 /* If no framebuffer, the resize will be done later when a new framebuffer will be set in changeFramebuffer. */
4172 return VINF_SUCCESS;
4173 }
4174
4175 /* If the framebuffer already set for the screen, do a regular resize. */
4176 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
4177 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
4178 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
4179}
4180
4181DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
4182 uint32_t xHot, uint32_t yHot,
4183 uint32_t cx, uint32_t cy,
4184 const void *pvShape)
4185{
4186 LogFlowFunc(("\n"));
4187
4188 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4189 Display *pThis = pDrv->pDisplay;
4190
4191 size_t cbShapeSize = 0;
4192
4193 if (pvShape)
4194 {
4195 cbShapeSize = (cx + 7) / 8 * cy; /* size of the AND mask */
4196 cbShapeSize = ((cbShapeSize + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
4197 }
4198 com::SafeArray<BYTE> shapeData(cbShapeSize);
4199
4200 if (pvShape)
4201 ::memcpy(shapeData.raw(), pvShape, cbShapeSize);
4202
4203 /* Tell the console about it */
4204 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
4205 xHot, yHot, cx, cy, ComSafeArrayAsInParam(shapeData));
4206
4207 return VINF_SUCCESS;
4208}
4209#endif /* VBOX_WITH_HGSMI */
4210
4211/**
4212 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4213 */
4214DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
4215{
4216 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
4217 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4218 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
4219 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
4220 return NULL;
4221}
4222
4223
4224/**
4225 * Destruct a display driver instance.
4226 *
4227 * @returns VBox status.
4228 * @param pDrvIns The driver instance data.
4229 */
4230DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
4231{
4232 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4233 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4234 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4235
4236 if (pThis->pDisplay)
4237 {
4238 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
4239#ifdef VBOX_WITH_CRHGSMI
4240 pThis->pDisplay->destructCrHgsmiData();
4241#endif
4242 pThis->pDisplay->mpDrv = NULL;
4243 pThis->pDisplay->mpVMMDev = NULL;
4244 pThis->pDisplay->mLastAddress = NULL;
4245 pThis->pDisplay->mLastBytesPerLine = 0;
4246 pThis->pDisplay->mLastBitsPerPixel = 0,
4247 pThis->pDisplay->mLastWidth = 0;
4248 pThis->pDisplay->mLastHeight = 0;
4249 }
4250}
4251
4252
4253/**
4254 * Construct a display driver instance.
4255 *
4256 * @copydoc FNPDMDRVCONSTRUCT
4257 */
4258DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
4259{
4260 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
4261 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4262 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
4263
4264 /*
4265 * Validate configuration.
4266 */
4267 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
4268 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
4269 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
4270 ("Configuration error: Not possible to attach anything to this driver!\n"),
4271 VERR_PDM_DRVINS_NO_ATTACH);
4272
4273 /*
4274 * Init Interfaces.
4275 */
4276 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
4277
4278 pThis->IConnector.pfnResize = Display::displayResizeCallback;
4279 pThis->IConnector.pfnUpdateRect = Display::displayUpdateCallback;
4280 pThis->IConnector.pfnRefresh = Display::displayRefreshCallback;
4281 pThis->IConnector.pfnReset = Display::displayResetCallback;
4282 pThis->IConnector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
4283 pThis->IConnector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
4284 pThis->IConnector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
4285#ifdef VBOX_WITH_VIDEOHWACCEL
4286 pThis->IConnector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
4287#endif
4288#ifdef VBOX_WITH_CRHGSMI
4289 pThis->IConnector.pfnCrHgsmiCommandProcess = Display::displayCrHgsmiCommandProcess;
4290 pThis->IConnector.pfnCrHgsmiControlProcess = Display::displayCrHgsmiControlProcess;
4291#endif
4292#ifdef VBOX_WITH_HGSMI
4293 pThis->IConnector.pfnVBVAEnable = Display::displayVBVAEnable;
4294 pThis->IConnector.pfnVBVADisable = Display::displayVBVADisable;
4295 pThis->IConnector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
4296 pThis->IConnector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
4297 pThis->IConnector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
4298 pThis->IConnector.pfnVBVAResize = Display::displayVBVAResize;
4299 pThis->IConnector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
4300#endif
4301
4302 /*
4303 * Get the IDisplayPort interface of the above driver/device.
4304 */
4305 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
4306 if (!pThis->pUpPort)
4307 {
4308 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
4309 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4310 }
4311#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
4312 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
4313 if (!pThis->pVBVACallbacks)
4314 {
4315 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
4316 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4317 }
4318#endif
4319 /*
4320 * Get the Display object pointer and update the mpDrv member.
4321 */
4322 void *pv;
4323 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
4324 if (RT_FAILURE(rc))
4325 {
4326 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
4327 return rc;
4328 }
4329 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
4330 pThis->pDisplay = pDisplay;
4331 pThis->pDisplay->mpDrv = pThis;
4332 /*
4333 * Update our display information according to the framebuffer
4334 */
4335 pDisplay->updateDisplayData();
4336
4337 /*
4338 * Start periodic screen refreshes
4339 */
4340 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
4341
4342#ifdef VBOX_WITH_CRHGSMI
4343 pDisplay->setupCrHgsmiData();
4344#endif
4345
4346#ifdef VBOX_WITH_VPX
4347 rc = VideoRecContextCreate(&pDisplay->mpVideoRecCtx);
4348 if (RT_FAILURE(rc))
4349 {
4350 LogFlow(("Failed to create video recording context (%Rrc)!\n", rc));
4351 return E_FAIL;
4352 }
4353
4354 ComPtr<IMachine> pMachine = pDisplay->mParent->machine();
4355 BOOL fEnabled = false;
4356 pMachine->COMGETTER(VideoCaptureEnabled)(&fEnabled);
4357 if (fEnabled)
4358 {
4359 ULONG ulWidth;
4360 int hrc = pMachine->COMGETTER(VideoCaptureWidth)(&ulWidth);
4361 AssertComRCReturnRC(hrc);
4362 ULONG ulHeight;
4363 hrc = pMachine->COMGETTER(VideoCaptureHeight)(&ulHeight);
4364 AssertComRCReturnRC(hrc);
4365 ULONG ulRate;
4366 hrc = pMachine->COMGETTER(VideoCaptureRate)(&ulRate);
4367 AssertComRCReturnRC(hrc);
4368 ULONG ulFps;
4369 hrc = pMachine->COMGETTER(VideoCaptureFps)(&ulFps);
4370 AssertComRCReturnRC(hrc);
4371 BSTR strFile;
4372 hrc = pMachine->COMGETTER(VideoCaptureFile)(&strFile);
4373 AssertComRCReturnRC(hrc);
4374 rc = VideoRecContextInit(pDisplay->mpVideoRecCtx, com::Utf8Str(strFile).c_str(),
4375 ulWidth, ulHeight, ulRate, ulFps);
4376 if (RT_SUCCESS(rc))
4377 LogRel(("WebM/VP8 video recording with %ux%u @ %u kbps, %u fps to '%s' enabled!\n",
4378 ulWidth, ulHeight, ulRate, ulFps, com::Utf8Str(strFile).c_str()));
4379 else
4380 LogRel(("Failed to initialize video recording context (%Rrc)!\n", rc));
4381 }
4382#endif
4383
4384 return VINF_SUCCESS;
4385}
4386
4387
4388/**
4389 * Display driver registration record.
4390 */
4391const PDMDRVREG Display::DrvReg =
4392{
4393 /* u32Version */
4394 PDM_DRVREG_VERSION,
4395 /* szName */
4396 "MainDisplay",
4397 /* szRCMod */
4398 "",
4399 /* szR0Mod */
4400 "",
4401 /* pszDescription */
4402 "Main display driver (Main as in the API).",
4403 /* fFlags */
4404 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
4405 /* fClass. */
4406 PDM_DRVREG_CLASS_DISPLAY,
4407 /* cMaxInstances */
4408 ~0U,
4409 /* cbInstance */
4410 sizeof(DRVMAINDISPLAY),
4411 /* pfnConstruct */
4412 Display::drvConstruct,
4413 /* pfnDestruct */
4414 Display::drvDestruct,
4415 /* pfnRelocate */
4416 NULL,
4417 /* pfnIOCtl */
4418 NULL,
4419 /* pfnPowerOn */
4420 NULL,
4421 /* pfnReset */
4422 NULL,
4423 /* pfnSuspend */
4424 NULL,
4425 /* pfnResume */
4426 NULL,
4427 /* pfnAttach */
4428 NULL,
4429 /* pfnDetach */
4430 NULL,
4431 /* pfnPowerOff */
4432 NULL,
4433 /* pfnSoftReset */
4434 NULL,
4435 /* u32EndVersion */
4436 PDM_DRVREG_VERSION
4437};
4438/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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