VirtualBox

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

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

Main: warnings

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