VirtualBox

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

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

Main/Display: clean up the interface to the video recording code

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