VirtualBox

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

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

crOpenGL: screenshot support; a bit of performance for video recording

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