VirtualBox

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

Last change on this file since 49935 was 49790, checked in by vboxsync, 11 years ago

Main: unsigned width/height

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