VirtualBox

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

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

Main/DisplayImpl: Fix for access violation in case of multimonitor.

Display requests a resize of 4 framebuffers simultaneously. When one of framebuffers is resized, Display calls InvalidateAndUpdateEMT which is updating _all_ framebuffers but some of them are not yet finished with the resize. As a result, InvalidateAndUpdateEMT is assuming that all the framebuffers have right size, which is not true. Thus, crash while copying from source to destination recatangle .

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