VirtualBox

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

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

ConsoleImpl.h/.cpp: changed SafeVMPtrBase to not mess around with pVM, only pUVM.

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