VirtualBox

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

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

crOpenGL: disable some async notifications for now

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

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