VirtualBox

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

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

VPX: separate encoding thread; more cleanup

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

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