VirtualBox

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

Last change on this file since 49629 was 49603, checked in by vboxsync, 11 years ago

DevVGA,DisplayImpl: prevent access to invalid framebuffer.

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