VirtualBox

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

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

IMachine::VideoCaptureFps

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