VirtualBox

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

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

Main/VideoRecAndEncode module: Replaced COM error codes with general error code and other code improvements.

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

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