VirtualBox

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

Last change on this file since 50183 was 50178, checked in by vboxsync, 11 years ago

crOpenGL: seamless and resize bugfixes

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

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