VirtualBox

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

Last change on this file since 45958 was 45958, checked in by vboxsync, 12 years ago

Main/VPX: start at 1 not 0.

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

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