VirtualBox

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

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

DisplayImpl: force resize when VBVA is reenabled.

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