VirtualBox

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

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

GA/Display: Support for dynamic configuration (position and enable/disable) of the virtual screen for Linux guest.

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

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