VirtualBox

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

Last change on this file since 50152 was 50149, checked in by vboxsync, 11 years ago

crOpenGL: bugfixes

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

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