VirtualBox

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

Last change on this file since 83170 was 83142, checked in by vboxsync, 5 years ago

bugref:9637. Sending monitor positions (offsets) from GAs to svga device since vmwgfx fails to do so.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 129.4 KB
Line 
1/* $Id: DisplayImpl.cpp 83142 2020-02-24 19:24:26Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#define LOG_GROUP LOG_GROUP_MAIN_DISPLAY
19#include "LoggingNew.h"
20
21#include "DisplayImpl.h"
22#include "DisplayUtils.h"
23#include "ConsoleImpl.h"
24#include "ConsoleVRDPServer.h"
25#include "GuestImpl.h"
26#include "VMMDev.h"
27
28#include "AutoCaller.h"
29
30/* generated header */
31#include "VBoxEvents.h"
32
33#include <iprt/semaphore.h>
34#include <iprt/thread.h>
35#include <iprt/asm.h>
36#include <iprt/time.h>
37#include <iprt/cpp/utils.h>
38#include <iprt/alloca.h>
39
40#include <VBox/vmm/pdmdrv.h>
41
42#ifdef VBOX_WITH_VIDEOHWACCEL
43# include <VBoxVideo.h>
44#endif
45
46#include <VBox/com/array.h>
47
48#ifdef VBOX_WITH_RECORDING
49# include <iprt/path.h>
50# include "Recording.h"
51
52# ifdef VBOX_WITH_LIBVPX
53# ifdef _MSC_VER
54# pragma warning(push)
55# pragma warning(disable: 4668) /* vpx_codec.h(64) : warning C4668: '__GNUC__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif' */
56# include <vpx/vpx_encoder.h>
57# pragma warning(pop)
58# else
59# include <vpx/vpx_encoder.h>
60# endif
61# endif
62
63# include <VBox/vmm/pdmapi.h>
64# include <VBox/vmm/pdmaudioifs.h>
65#endif
66
67/**
68 * Display driver instance data.
69 *
70 * @implements PDMIDISPLAYCONNECTOR
71 */
72typedef struct DRVMAINDISPLAY
73{
74 /** Pointer to the display object. */
75 Display *pDisplay;
76 /** Pointer to the driver instance structure. */
77 PPDMDRVINS pDrvIns;
78 /** Pointer to the display port interface of the driver/device above us. */
79 PPDMIDISPLAYPORT pUpPort;
80 /** Our display connector interface. */
81 PDMIDISPLAYCONNECTOR IConnector;
82#if defined(VBOX_WITH_VIDEOHWACCEL)
83 /** VBVA callbacks */
84 PPDMIDISPLAYVBVACALLBACKS pVBVACallbacks;
85#endif
86} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
87
88/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
89#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) RT_FROM_MEMBER(pInterface, DRVMAINDISPLAY, IConnector)
90
91// constructor / destructor
92/////////////////////////////////////////////////////////////////////////////
93
94Display::Display()
95 : mParent(NULL)
96{
97}
98
99Display::~Display()
100{
101}
102
103
104HRESULT Display::FinalConstruct()
105{
106 int rc = videoAccelConstruct(&mVideoAccelLegacy);
107 AssertRC(rc);
108
109 mfVideoAccelVRDP = false;
110 mfu32SupportedOrders = 0;
111 mcVRDPRefs = 0;
112
113 mfSeamlessEnabled = false;
114 mpRectVisibleRegion = NULL;
115 mcRectVisibleRegion = 0;
116
117 mpDrv = NULL;
118
119 rc = RTCritSectInit(&mVideoAccelLock);
120 AssertRC(rc);
121
122#ifdef VBOX_WITH_HGSMI
123 mu32UpdateVBVAFlags = 0;
124 mfVMMDevSupportsGraphics = false;
125 mfGuestVBVACapabilities = 0;
126 mfHostCursorCapabilities = 0;
127#endif
128
129#ifdef VBOX_WITH_RECORDING
130 rc = RTCritSectInit(&mVideoRecLock);
131 AssertRC(rc);
132
133 for (unsigned i = 0; i < RT_ELEMENTS(maRecordingEnabled); i++)
134 maRecordingEnabled[i] = true;
135#endif
136
137 return BaseFinalConstruct();
138}
139
140void Display::FinalRelease()
141{
142 uninit();
143
144#ifdef VBOX_WITH_RECORDING
145 if (RTCritSectIsInitialized(&mVideoRecLock))
146 {
147 RTCritSectDelete(&mVideoRecLock);
148 RT_ZERO(mVideoRecLock);
149 }
150#endif
151
152 videoAccelDestroy(&mVideoAccelLegacy);
153 i_saveVisibleRegion(0, NULL);
154
155 if (RTCritSectIsInitialized(&mVideoAccelLock))
156 {
157 RTCritSectDelete(&mVideoAccelLock);
158 RT_ZERO(mVideoAccelLock);
159 }
160
161 BaseFinalRelease();
162}
163
164// public initializer/uninitializer for internal purposes only
165/////////////////////////////////////////////////////////////////////////////
166
167#define kMaxSizeThumbnail 64
168
169/**
170 * Save thumbnail and screenshot of the guest screen.
171 */
172static int displayMakeThumbnail(uint8_t *pbData, uint32_t cx, uint32_t cy,
173 uint8_t **ppu8Thumbnail, uint32_t *pcbThumbnail, uint32_t *pcxThumbnail, uint32_t *pcyThumbnail)
174{
175 int rc = VINF_SUCCESS;
176
177 uint8_t *pu8Thumbnail = NULL;
178 uint32_t cbThumbnail = 0;
179 uint32_t cxThumbnail = 0;
180 uint32_t cyThumbnail = 0;
181
182 if (cx > cy)
183 {
184 cxThumbnail = kMaxSizeThumbnail;
185 cyThumbnail = (kMaxSizeThumbnail * cy) / cx;
186 }
187 else
188 {
189 cyThumbnail = kMaxSizeThumbnail;
190 cxThumbnail = (kMaxSizeThumbnail * cx) / cy;
191 }
192
193 LogRelFlowFunc(("%dx%d -> %dx%d\n", cx, cy, cxThumbnail, cyThumbnail));
194
195 cbThumbnail = cxThumbnail * 4 * cyThumbnail;
196 pu8Thumbnail = (uint8_t *)RTMemAlloc(cbThumbnail);
197
198 if (pu8Thumbnail)
199 {
200 uint8_t *dst = pu8Thumbnail;
201 uint8_t *src = pbData;
202 int dstW = cxThumbnail;
203 int dstH = cyThumbnail;
204 int srcW = cx;
205 int srcH = cy;
206 int iDeltaLine = cx * 4;
207
208 BitmapScale32(dst,
209 dstW, dstH,
210 src,
211 iDeltaLine,
212 srcW, srcH);
213
214 *ppu8Thumbnail = pu8Thumbnail;
215 *pcbThumbnail = cbThumbnail;
216 *pcxThumbnail = cxThumbnail;
217 *pcyThumbnail = cyThumbnail;
218 }
219 else
220 {
221 rc = VERR_NO_MEMORY;
222 }
223
224 return rc;
225}
226
227DECLCALLBACK(void) Display::i_displaySSMSaveScreenshot(PSSMHANDLE pSSM, void *pvUser)
228{
229 Display *that = static_cast<Display*>(pvUser);
230
231 /* 32bpp small RGB image. */
232 uint8_t *pu8Thumbnail = NULL;
233 uint32_t cbThumbnail = 0;
234 uint32_t cxThumbnail = 0;
235 uint32_t cyThumbnail = 0;
236
237 /* PNG screenshot. */
238 uint8_t *pu8PNG = NULL;
239 uint32_t cbPNG = 0;
240 uint32_t cxPNG = 0;
241 uint32_t cyPNG = 0;
242
243 Console::SafeVMPtr ptrVM(that->mParent);
244 if (ptrVM.isOk())
245 {
246 /* Query RGB bitmap. */
247 /* SSM code is executed on EMT(0), therefore no need to use VMR3ReqCallWait. */
248 uint8_t *pbData = NULL;
249 size_t cbData = 0;
250 uint32_t cx = 0;
251 uint32_t cy = 0;
252 bool fFreeMem = false;
253 int rc = Display::i_displayTakeScreenshotEMT(that, VBOX_VIDEO_PRIMARY_SCREEN, &pbData, &cbData, &cx, &cy, &fFreeMem);
254
255 /*
256 * It is possible that success is returned but everything is 0 or NULL.
257 * (no display attached if a VM is running with VBoxHeadless on OSE for example)
258 */
259 if (RT_SUCCESS(rc) && pbData)
260 {
261 Assert(cx && cy);
262
263 /* Prepare a small thumbnail and a PNG screenshot. */
264 displayMakeThumbnail(pbData, cx, cy, &pu8Thumbnail, &cbThumbnail, &cxThumbnail, &cyThumbnail);
265 rc = DisplayMakePNG(pbData, cx, cy, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 1);
266 if (RT_FAILURE(rc))
267 {
268 if (pu8PNG)
269 {
270 RTMemFree(pu8PNG);
271 pu8PNG = NULL;
272 }
273 cbPNG = 0;
274 cxPNG = 0;
275 cyPNG = 0;
276 }
277
278 if (fFreeMem)
279 RTMemFree(pbData);
280 else
281 that->mpDrv->pUpPort->pfnFreeScreenshot(that->mpDrv->pUpPort, pbData);
282 }
283 }
284 else
285 {
286 LogFunc(("Failed to get VM pointer 0x%x\n", ptrVM.rc()));
287 }
288
289 /* Regardless of rc, save what is available:
290 * Data format:
291 * uint32_t cBlocks;
292 * [blocks]
293 *
294 * Each block is:
295 * uint32_t cbBlock; if 0 - no 'block data'.
296 * uint32_t typeOfBlock; 0 - 32bpp RGB bitmap, 1 - PNG, ignored if 'cbBlock' is 0.
297 * [block data]
298 *
299 * Block data for bitmap and PNG:
300 * uint32_t cx;
301 * uint32_t cy;
302 * [image data]
303 */
304 SSMR3PutU32(pSSM, 2); /* Write thumbnail and PNG screenshot. */
305
306 /* First block. */
307 SSMR3PutU32(pSSM, (uint32_t)(cbThumbnail + 2 * sizeof(uint32_t)));
308 SSMR3PutU32(pSSM, 0); /* Block type: thumbnail. */
309
310 if (cbThumbnail)
311 {
312 SSMR3PutU32(pSSM, cxThumbnail);
313 SSMR3PutU32(pSSM, cyThumbnail);
314 SSMR3PutMem(pSSM, pu8Thumbnail, cbThumbnail);
315 }
316
317 /* Second block. */
318 SSMR3PutU32(pSSM, (uint32_t)(cbPNG + 2 * sizeof(uint32_t)));
319 SSMR3PutU32(pSSM, 1); /* Block type: png. */
320
321 if (cbPNG)
322 {
323 SSMR3PutU32(pSSM, cxPNG);
324 SSMR3PutU32(pSSM, cyPNG);
325 SSMR3PutMem(pSSM, pu8PNG, cbPNG);
326 }
327
328 RTMemFree(pu8PNG);
329 RTMemFree(pu8Thumbnail);
330}
331
332DECLCALLBACK(int)
333Display::i_displaySSMLoadScreenshot(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
334{
335 RT_NOREF(pvUser);
336 if (uVersion != sSSMDisplayScreenshotVer)
337 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
338 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
339
340 /* Skip data. */
341 uint32_t cBlocks;
342 int rc = SSMR3GetU32(pSSM, &cBlocks);
343 AssertRCReturn(rc, rc);
344
345 for (uint32_t i = 0; i < cBlocks; i++)
346 {
347 uint32_t cbBlock;
348 rc = SSMR3GetU32(pSSM, &cbBlock);
349 AssertRCBreak(rc);
350
351 uint32_t typeOfBlock;
352 rc = SSMR3GetU32(pSSM, &typeOfBlock);
353 AssertRCBreak(rc);
354
355 LogRelFlowFunc(("[%d] type %d, size %d bytes\n", i, typeOfBlock, cbBlock));
356
357 /* Note: displaySSMSaveScreenshot writes size of a block = 8 and
358 * do not write any data if the image size was 0.
359 * @todo Fix and increase saved state version.
360 */
361 if (cbBlock > 2 * sizeof(uint32_t))
362 {
363 rc = SSMR3Skip(pSSM, cbBlock);
364 AssertRCBreak(rc);
365 }
366 }
367
368 return rc;
369}
370
371/**
372 * Save/Load some important guest state
373 */
374DECLCALLBACK(void)
375Display::i_displaySSMSave(PSSMHANDLE pSSM, void *pvUser)
376{
377 Display *that = static_cast<Display*>(pvUser);
378
379 SSMR3PutU32(pSSM, that->mcMonitors);
380 for (unsigned i = 0; i < that->mcMonitors; i++)
381 {
382 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32Offset);
383 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32MaxFramebufferSize);
384 SSMR3PutU32(pSSM, that->maFramebuffers[i].u32InformationSize);
385 SSMR3PutU32(pSSM, that->maFramebuffers[i].w);
386 SSMR3PutU32(pSSM, that->maFramebuffers[i].h);
387 SSMR3PutS32(pSSM, that->maFramebuffers[i].xOrigin);
388 SSMR3PutS32(pSSM, that->maFramebuffers[i].yOrigin);
389 SSMR3PutU32(pSSM, that->maFramebuffers[i].flags);
390 }
391 SSMR3PutS32(pSSM, that->xInputMappingOrigin);
392 SSMR3PutS32(pSSM, that->yInputMappingOrigin);
393 SSMR3PutU32(pSSM, that->cxInputMapping);
394 SSMR3PutU32(pSSM, that->cyInputMapping);
395 SSMR3PutU32(pSSM, that->mfGuestVBVACapabilities);
396 SSMR3PutU32(pSSM, that->mfHostCursorCapabilities);
397}
398
399DECLCALLBACK(int)
400Display::i_displaySSMLoad(PSSMHANDLE pSSM, void *pvUser, uint32_t uVersion, uint32_t uPass)
401{
402 Display *that = static_cast<Display*>(pvUser);
403
404 if ( uVersion != sSSMDisplayVer
405 && uVersion != sSSMDisplayVer2
406 && uVersion != sSSMDisplayVer3
407 && uVersion != sSSMDisplayVer4
408 && uVersion != sSSMDisplayVer5)
409 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
410 Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
411
412 uint32_t cMonitors;
413 int rc = SSMR3GetU32(pSSM, &cMonitors);
414 AssertRCReturn(rc, rc);
415 if (cMonitors != that->mcMonitors)
416 return SSMR3SetCfgError(pSSM, RT_SRC_POS, N_("Number of monitors changed (%d->%d)!"), cMonitors, that->mcMonitors);
417
418 for (uint32_t i = 0; i < cMonitors; i++)
419 {
420 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32Offset);
421 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32MaxFramebufferSize);
422 SSMR3GetU32(pSSM, &that->maFramebuffers[i].u32InformationSize);
423 if ( uVersion == sSSMDisplayVer2
424 || uVersion == sSSMDisplayVer3
425 || uVersion == sSSMDisplayVer4
426 || uVersion == sSSMDisplayVer5)
427 {
428 uint32_t w;
429 uint32_t h;
430 SSMR3GetU32(pSSM, &w);
431 SSMR3GetU32(pSSM, &h);
432 that->maFramebuffers[i].w = w;
433 that->maFramebuffers[i].h = h;
434 }
435 if ( uVersion == sSSMDisplayVer3
436 || uVersion == sSSMDisplayVer4
437 || uVersion == sSSMDisplayVer5)
438 {
439 int32_t xOrigin;
440 int32_t yOrigin;
441 uint32_t flags;
442 SSMR3GetS32(pSSM, &xOrigin);
443 SSMR3GetS32(pSSM, &yOrigin);
444 SSMR3GetU32(pSSM, &flags);
445 that->maFramebuffers[i].xOrigin = xOrigin;
446 that->maFramebuffers[i].yOrigin = yOrigin;
447 that->maFramebuffers[i].flags = (uint16_t)flags;
448 that->maFramebuffers[i].fDisabled = (that->maFramebuffers[i].flags & VBVA_SCREEN_F_DISABLED) != 0;
449 }
450 }
451 if ( uVersion == sSSMDisplayVer4
452 || uVersion == sSSMDisplayVer5)
453 {
454 SSMR3GetS32(pSSM, &that->xInputMappingOrigin);
455 SSMR3GetS32(pSSM, &that->yInputMappingOrigin);
456 SSMR3GetU32(pSSM, &that->cxInputMapping);
457 SSMR3GetU32(pSSM, &that->cyInputMapping);
458 }
459 if (uVersion == sSSMDisplayVer5)
460 {
461 SSMR3GetU32(pSSM, &that->mfGuestVBVACapabilities);
462 SSMR3GetU32(pSSM, &that->mfHostCursorCapabilities);
463 }
464
465 return VINF_SUCCESS;
466}
467
468/**
469 * Initializes the display object.
470 *
471 * @returns COM result indicator
472 * @param aParent handle of our parent object
473 */
474HRESULT Display::init(Console *aParent)
475{
476 ComAssertRet(aParent, E_INVALIDARG);
477 /* Enclose the state transition NotReady->InInit->Ready */
478 AutoInitSpan autoInitSpan(this);
479 AssertReturn(autoInitSpan.isOk(), E_FAIL);
480
481 unconst(mParent) = aParent;
482
483 mfSourceBitmapEnabled = true;
484 fVGAResizing = false;
485
486 ComPtr<IGraphicsAdapter> pGraphicsAdapter;
487 HRESULT hrc = mParent->i_machine()->COMGETTER(GraphicsAdapter)(pGraphicsAdapter.asOutParam());
488 AssertComRCReturnRC(hrc);
489 AssertReturn(!pGraphicsAdapter.isNull(), E_FAIL);
490
491 ULONG ul;
492 pGraphicsAdapter->COMGETTER(MonitorCount)(&ul);
493 mcMonitors = ul;
494 xInputMappingOrigin = 0;
495 yInputMappingOrigin = 0;
496 cxInputMapping = 0;
497 cyInputMapping = 0;
498
499 for (ul = 0; ul < mcMonitors; ul++)
500 {
501 maFramebuffers[ul].u32Offset = 0;
502 maFramebuffers[ul].u32MaxFramebufferSize = 0;
503 maFramebuffers[ul].u32InformationSize = 0;
504
505 maFramebuffers[ul].pFramebuffer = NULL;
506 /* All secondary monitors are disabled at startup. */
507 maFramebuffers[ul].fDisabled = ul > 0;
508
509 maFramebuffers[ul].u32Caps = 0;
510
511 maFramebuffers[ul].updateImage.pu8Address = NULL;
512 maFramebuffers[ul].updateImage.cbLine = 0;
513
514 maFramebuffers[ul].xOrigin = 0;
515 maFramebuffers[ul].yOrigin = 0;
516
517 maFramebuffers[ul].w = 0;
518 maFramebuffers[ul].h = 0;
519
520 maFramebuffers[ul].flags = maFramebuffers[ul].fDisabled? VBVA_SCREEN_F_DISABLED: 0;
521
522 maFramebuffers[ul].u16BitsPerPixel = 0;
523 maFramebuffers[ul].pu8FramebufferVRAM = NULL;
524 maFramebuffers[ul].u32LineSize = 0;
525
526 maFramebuffers[ul].pHostEvents = NULL;
527
528 maFramebuffers[ul].fDefaultFormat = false;
529
530#ifdef VBOX_WITH_HGSMI
531 maFramebuffers[ul].fVBVAEnabled = false;
532 maFramebuffers[ul].fVBVAForceResize = false;
533 maFramebuffers[ul].pVBVAHostFlags = NULL;
534#endif /* VBOX_WITH_HGSMI */
535 }
536
537 {
538 // register listener for state change events
539 ComPtr<IEventSource> es;
540 mParent->COMGETTER(EventSource)(es.asOutParam());
541 com::SafeArray<VBoxEventType_T> eventTypes;
542 eventTypes.push_back(VBoxEventType_OnStateChanged);
543 es->RegisterListener(this, ComSafeArrayAsInParam(eventTypes), true);
544 }
545
546 /* Confirm a successful initialization */
547 autoInitSpan.setSucceeded();
548
549 return S_OK;
550}
551
552/**
553 * Uninitializes the instance and sets the ready flag to FALSE.
554 * Called either from FinalRelease() or by the parent when it gets destroyed.
555 */
556void Display::uninit()
557{
558 LogRelFlowFunc(("this=%p\n", this));
559
560 /* Enclose the state transition Ready->InUninit->NotReady */
561 AutoUninitSpan autoUninitSpan(this);
562 if (autoUninitSpan.uninitDone())
563 return;
564
565 unsigned uScreenId;
566 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
567 {
568 maFramebuffers[uScreenId].pSourceBitmap.setNull();
569 maFramebuffers[uScreenId].updateImage.pSourceBitmap.setNull();
570 maFramebuffers[uScreenId].updateImage.pu8Address = NULL;
571 maFramebuffers[uScreenId].updateImage.cbLine = 0;
572 maFramebuffers[uScreenId].pFramebuffer.setNull();
573#ifdef VBOX_WITH_RECORDING
574 maFramebuffers[uScreenId].Recording.pSourceBitmap.setNull();
575#endif
576 }
577
578 if (mParent)
579 {
580 ComPtr<IEventSource> es;
581 mParent->COMGETTER(EventSource)(es.asOutParam());
582 es->UnregisterListener(this);
583 }
584
585 unconst(mParent) = NULL;
586
587 if (mpDrv)
588 mpDrv->pDisplay = NULL;
589
590 mpDrv = NULL;
591}
592
593/**
594 * Register the SSM methods. Called by the power up thread to be able to
595 * pass pVM
596 */
597int Display::i_registerSSM(PUVM pUVM)
598{
599 /* Version 2 adds width and height of the framebuffer; version 3 adds
600 * the framebuffer offset in the virtual desktop and the framebuffer flags;
601 * version 4 adds guest to host input event mapping and version 5 adds
602 * guest VBVA and host cursor capabilities.
603 */
604 int rc = SSMR3RegisterExternal(pUVM, "DisplayData", 0, sSSMDisplayVer5,
605 mcMonitors * sizeof(uint32_t) * 8 + sizeof(uint32_t),
606 NULL, NULL, NULL,
607 NULL, i_displaySSMSave, NULL,
608 NULL, i_displaySSMLoad, NULL, this);
609 AssertRCReturn(rc, rc);
610
611 /*
612 * Register loaders for old saved states where iInstance was
613 * 3 * sizeof(uint32_t *) due to a code mistake.
614 */
615 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 12 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
616 NULL, NULL, NULL,
617 NULL, NULL, NULL,
618 NULL, i_displaySSMLoad, NULL, this);
619 AssertRCReturn(rc, rc);
620
621 rc = SSMR3RegisterExternal(pUVM, "DisplayData", 24 /*uInstance*/, sSSMDisplayVer, 0 /*cbGuess*/,
622 NULL, NULL, NULL,
623 NULL, NULL, NULL,
624 NULL, i_displaySSMLoad, NULL, this);
625 AssertRCReturn(rc, rc);
626
627 /* uInstance is an arbitrary value greater than 1024. Such a value will ensure a quick seek in saved state file. */
628 rc = SSMR3RegisterExternal(pUVM, "DisplayScreenshot", 1100 /*uInstance*/, sSSMDisplayScreenshotVer, 0 /*cbGuess*/,
629 NULL, NULL, NULL,
630 NULL, i_displaySSMSaveScreenshot, NULL,
631 NULL, i_displaySSMLoadScreenshot, NULL, this);
632
633 AssertRCReturn(rc, rc);
634
635 return VINF_SUCCESS;
636}
637
638// public methods only for internal purposes
639/////////////////////////////////////////////////////////////////////////////
640
641/**
642 * Handles display resize event.
643 *
644 * @param uScreenId Screen ID
645 * @param bpp New bits per pixel.
646 * @param pvVRAM VRAM pointer.
647 * @param cbLine New bytes per line.
648 * @param w New display width.
649 * @param h New display height.
650 * @param flags Flags of the new video mode.
651 * @param xOrigin New display origin X.
652 * @param yOrigin New display origin Y.
653 * @param fVGAResize Whether the resize is originated from the VGA device (DevVGA).
654 */
655int Display::i_handleDisplayResize(unsigned uScreenId, uint32_t bpp, void *pvVRAM,
656 uint32_t cbLine, uint32_t w, uint32_t h, uint16_t flags,
657 int32_t xOrigin, int32_t yOrigin, bool fVGAResize)
658{
659 LogRel2(("Display::i_handleDisplayResize: uScreenId=%d pvVRAM=%p w=%d h=%d bpp=%d cbLine=0x%X flags=0x%X\n", uScreenId,
660 pvVRAM, w, h, bpp, cbLine, flags));
661
662 /* Caller must not hold the object lock. */
663 AssertReturn(!isWriteLockOnCurrentThread(), VERR_INVALID_STATE);
664
665 /* Note: the old code checked if the video mode was actially chnaged and
666 * did not invalidate the source bitmap if the mode did not change.
667 * The new code always invalidates the source bitmap, i.e. it will
668 * notify the frontend even if nothing actually changed.
669 *
670 * Implementing the filtering is possible but might lead to pfnSetRenderVRAM races
671 * between this method and QuerySourceBitmap. Such races can be avoided by implementing
672 * the @todo below.
673 */
674
675 /* Make sure that the VGA device does not access the source bitmap. */
676 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && mpDrv)
677 {
678 /// @todo It is probably more convenient to implement
679 // mpDrv->pUpPort->pfnSetOutputBitmap(pvVRAM, cbScanline, cBits, cx, cy, bool fSet);
680 // and remove IConnector.pbData, cbScanline, cBits, cx, cy.
681 // fSet = false disables rendering and VGA can check
682 // if it is already rendering to a different bitmap, avoiding
683 // enable/disable rendering races.
684 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, false);
685
686 mpDrv->IConnector.pbData = NULL;
687 mpDrv->IConnector.cbScanline = 0;
688 mpDrv->IConnector.cBits = 32; /* DevVGA does not work with cBits == 0. */
689 mpDrv->IConnector.cx = 0;
690 mpDrv->IConnector.cy = 0;
691 }
692
693 /* Update maFramebuffers[uScreenId] under lock. */
694 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
695
696 if (uScreenId >= mcMonitors)
697 {
698 LogRel(("Display::i_handleDisplayResize: mcMonitors=%u < uScreenId=%u (pvVRAM=%p w=%u h=%u bpp=%d cbLine=0x%X flags=0x%X)\n",
699 mcMonitors, uScreenId, pvVRAM, w, h, bpp, cbLine, flags));
700 return VINF_SUCCESS;
701 }
702
703 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
704
705 /* Whether the monitor position has changed.
706 * A resize initiated by the VGA device does not change the monitor position.
707 */
708 const bool fNewOrigin = !fVGAResize
709 && ( pFBInfo->xOrigin != xOrigin
710 || pFBInfo->yOrigin != yOrigin);
711
712 /* The event for disabled->enabled transition.
713 * VGA resizes also come when the guest uses VBVA mode. They do not affect pFBInfo->fDisabled.
714 * The primary screen is re-enabled when the guest leaves the VBVA mode in i_displayVBVADisable.
715 */
716 const bool fGuestMonitorChangedEvent = !fVGAResize
717 && (pFBInfo->fDisabled != RT_BOOL(flags & VBVA_SCREEN_F_DISABLED));
718
719 /* Reset the update mode. */
720 pFBInfo->updateImage.pSourceBitmap.setNull();
721 pFBInfo->updateImage.pu8Address = NULL;
722 pFBInfo->updateImage.cbLine = 0;
723
724 /* Release the current source bitmap. */
725 pFBInfo->pSourceBitmap.setNull();
726
727 /* VGA blanking is signaled as w=0, h=0, bpp=0 and cbLine=0, and it's
728 * best to keep the old resolution, as otherwise the window size would
729 * change before the new resolution is known. */
730 const bool fVGABlank = fVGAResize && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
731 && w == 0 && h == 0 && bpp == 0 && cbLine == 0;
732 if (fVGABlank)
733 {
734 w = pFBInfo->w;
735 h = pFBInfo->h;
736 }
737
738 /* Log changes. */
739 if ( pFBInfo->w != w
740 || pFBInfo->h != h
741 || pFBInfo->u32LineSize != cbLine
742 /*|| pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM - too noisy */
743 || ( !fVGAResize
744 && ( pFBInfo->xOrigin != xOrigin
745 || pFBInfo->yOrigin != yOrigin
746 || pFBInfo->flags != flags)))
747 LogRel(("Display::i_handleDisplayResize: uScreenId=%d pvVRAM=%p w=%d h=%d bpp=%d cbLine=0x%X flags=0x%X origin=%d,%d\n",
748 uScreenId, pvVRAM, w, h, bpp, cbLine, flags, xOrigin, yOrigin));
749
750 /* Update the video mode information. */
751 pFBInfo->w = w;
752 pFBInfo->h = h;
753 pFBInfo->u16BitsPerPixel = (uint16_t)bpp;
754 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM;
755 pFBInfo->u32LineSize = cbLine;
756 if (!fVGAResize)
757 {
758 /* Fields which are not used in not VBVA modes and not affected by a VGA resize. */
759 pFBInfo->flags = flags;
760 pFBInfo->xOrigin = xOrigin;
761 pFBInfo->yOrigin = yOrigin;
762 pFBInfo->fDisabled = RT_BOOL(flags & VBVA_SCREEN_F_DISABLED);
763 pFBInfo->fVBVAForceResize = false;
764 }
765 else
766 {
767 pFBInfo->flags = VBVA_SCREEN_F_ACTIVE;
768 if (fVGABlank)
769 pFBInfo->flags |= VBVA_SCREEN_F_BLANK;
770 pFBInfo->fDisabled = false;
771 }
772
773 /* Prepare local vars for the notification code below. */
774 ComPtr<IFramebuffer> pFramebuffer = pFBInfo->pFramebuffer;
775 const bool fDisabled = pFBInfo->fDisabled;
776
777 alock.release();
778
779 if (!pFramebuffer.isNull())
780 {
781 HRESULT hr = pFramebuffer->NotifyChange(uScreenId, 0, 0, w, h); /** @todo origin */
782 LogFunc(("NotifyChange hr %08X\n", hr));
783 NOREF(hr);
784 }
785
786 if (fGuestMonitorChangedEvent)
787 {
788 if (fDisabled)
789 fireGuestMonitorChangedEvent(mParent->i_getEventSource(),
790 GuestMonitorChangedEventType_Disabled,
791 uScreenId,
792 0, 0, 0, 0);
793 else
794 fireGuestMonitorChangedEvent(mParent->i_getEventSource(),
795 GuestMonitorChangedEventType_Enabled,
796 uScreenId,
797 xOrigin, yOrigin, w, h);
798 }
799
800 if (fNewOrigin)
801 fireGuestMonitorChangedEvent(mParent->i_getEventSource(),
802 GuestMonitorChangedEventType_NewOrigin,
803 uScreenId,
804 xOrigin, yOrigin, 0, 0);
805
806 /* Inform the VRDP server about the change of display parameters. */
807 LogRelFlowFunc(("Calling VRDP\n"));
808 mParent->i_consoleVRDPServer()->SendResize();
809
810 /* And re-send the seamless rectangles if necessary. */
811 if (mfSeamlessEnabled)
812 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
813
814#ifdef VBOX_WITH_RECORDING
815 i_recordingScreenChanged(uScreenId);
816#endif
817
818 LogRelFlowFunc(("[%d]: default format %d\n", uScreenId, pFBInfo->fDefaultFormat));
819
820 return VINF_SUCCESS;
821}
822
823static void i_checkCoordBounds(int *px, int *py, int *pw, int *ph, int cx, int cy)
824{
825 /* Correct negative x and y coordinates. */
826 if (*px < 0)
827 {
828 *px += *pw; /* Compute xRight which is also the new width. */
829
830 *pw = (*px < 0)? 0: *px;
831
832 *px = 0;
833 }
834
835 if (*py < 0)
836 {
837 *py += *ph; /* Compute xBottom, which is also the new height. */
838
839 *ph = (*py < 0)? 0: *py;
840
841 *py = 0;
842 }
843
844 /* Also check if coords are greater than the display resolution. */
845 if (*px + *pw > cx)
846 {
847 *pw = cx > *px? cx - *px: 0;
848 }
849
850 if (*py + *ph > cy)
851 {
852 *ph = cy > *py? cy - *py: 0;
853 }
854}
855
856void Display::i_handleDisplayUpdate(unsigned uScreenId, int x, int y, int w, int h)
857{
858 /*
859 * Always runs under either VBVA lock or, for HGSMI, DevVGA lock.
860 * Safe to use VBVA vars and take the framebuffer lock.
861 */
862
863#ifdef DEBUG_sunlover
864 LogFlowFunc(("[%d] %d,%d %dx%d\n",
865 uScreenId, x, y, w, h));
866#endif /* DEBUG_sunlover */
867
868 /* No updates for a disabled guest screen. */
869 if (maFramebuffers[uScreenId].fDisabled)
870 return;
871
872 /* No updates for a blank guest screen. */
873 /** @note Disabled for now, as the GUI does not update the picture when we
874 * first blank. */
875 /* if (maFramebuffers[uScreenId].flags & VBVA_SCREEN_F_BLANK)
876 return; */
877
878 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
879 AutoReadLock alockr(this COMMA_LOCKVAL_SRC_POS);
880
881 ComPtr<IFramebuffer> pFramebuffer = pFBInfo->pFramebuffer;
882 ComPtr<IDisplaySourceBitmap> pSourceBitmap = pFBInfo->updateImage.pSourceBitmap;
883
884 alockr.release();
885
886 if (RT_LIKELY(!pFramebuffer.isNull()))
887 {
888 if (RT_LIKELY(!RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_UpdateImage)))
889 {
890 i_checkCoordBounds(&x, &y, &w, &h, pFBInfo->w, pFBInfo->h);
891
892 if (w != 0 && h != 0)
893 {
894 pFramebuffer->NotifyUpdate(x, y, w, h);
895 }
896 }
897 else
898 {
899 if (RT_LIKELY(!pSourceBitmap.isNull()))
900 { /* likely */ }
901 else
902 {
903 /* Create a source bitmap if UpdateImage mode is used. */
904 HRESULT hr = QuerySourceBitmap(uScreenId, pSourceBitmap.asOutParam());
905 if (SUCCEEDED(hr))
906 {
907 BYTE *pAddress = NULL;
908 ULONG ulWidth = 0;
909 ULONG ulHeight = 0;
910 ULONG ulBitsPerPixel = 0;
911 ULONG ulBytesPerLine = 0;
912 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
913
914 hr = pSourceBitmap->QueryBitmapInfo(&pAddress,
915 &ulWidth,
916 &ulHeight,
917 &ulBitsPerPixel,
918 &ulBytesPerLine,
919 &bitmapFormat);
920 if (SUCCEEDED(hr))
921 {
922 AutoWriteLock alockw(this COMMA_LOCKVAL_SRC_POS);
923
924 if (pFBInfo->updateImage.pSourceBitmap.isNull())
925 {
926 pFBInfo->updateImage.pSourceBitmap = pSourceBitmap;
927 pFBInfo->updateImage.pu8Address = pAddress;
928 pFBInfo->updateImage.cbLine = ulBytesPerLine;
929 }
930
931 pSourceBitmap = pFBInfo->updateImage.pSourceBitmap;
932
933 alockw.release();
934 }
935 }
936 }
937
938 if (RT_LIKELY(!pSourceBitmap.isNull()))
939 {
940 BYTE *pbAddress = NULL;
941 ULONG ulWidth = 0;
942 ULONG ulHeight = 0;
943 ULONG ulBitsPerPixel = 0;
944 ULONG ulBytesPerLine = 0;
945 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
946
947 HRESULT hr = pSourceBitmap->QueryBitmapInfo(&pbAddress,
948 &ulWidth,
949 &ulHeight,
950 &ulBitsPerPixel,
951 &ulBytesPerLine,
952 &bitmapFormat);
953 if (SUCCEEDED(hr))
954 {
955 /* Make sure that the requested update is within the source bitmap dimensions. */
956 i_checkCoordBounds(&x, &y, &w, &h, ulWidth, ulHeight);
957
958 if (w != 0 && h != 0)
959 {
960 const size_t cbData = w * h * 4;
961 com::SafeArray<BYTE> image(cbData);
962
963 uint8_t *pu8Dst = image.raw();
964 const uint8_t *pu8Src = pbAddress + ulBytesPerLine * y + x * 4;
965
966 int i;
967 for (i = y; i < y + h; ++i)
968 {
969 memcpy(pu8Dst, pu8Src, w * 4);
970 pu8Dst += w * 4;
971 pu8Src += ulBytesPerLine;
972 }
973
974 pFramebuffer->NotifyUpdateImage(x, y, w, h, ComSafeArrayAsInParam(image));
975 }
976 }
977 }
978 }
979 }
980
981#ifndef VBOX_WITH_HGSMI
982 if (!mVideoAccelLegacy.fVideoAccelEnabled)
983 {
984#else
985 if (!mVideoAccelLegacy.fVideoAccelEnabled && !maFramebuffers[uScreenId].fVBVAEnabled)
986 {
987#endif /* VBOX_WITH_HGSMI */
988 /* When VBVA is enabled, the VRDP server is informed
989 * either in VideoAccelFlush or displayVBVAUpdateProcess.
990 * Inform the server here only if VBVA is disabled.
991 */
992 mParent->i_consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
993 }
994}
995
996void Display::i_updateGuestGraphicsFacility(void)
997{
998 Guest* pGuest = mParent->i_getGuest();
999 AssertPtrReturnVoid(pGuest);
1000 /* The following is from GuestImpl.cpp. */
1001 /** @todo A nit: The timestamp is wrong on saved state restore. Would be better
1002 * to move the graphics and seamless capability -> facility translation to
1003 * VMMDev so this could be saved. */
1004 RTTIMESPEC TimeSpecTS;
1005 RTTimeNow(&TimeSpecTS);
1006
1007 if ( mfVMMDevSupportsGraphics
1008 || (mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS) != 0)
1009 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1010 VBoxGuestFacilityStatus_Active,
1011 0 /*fFlags*/, &TimeSpecTS);
1012 else
1013 pGuest->i_setAdditionsStatus(VBoxGuestFacilityType_Graphics,
1014 VBoxGuestFacilityStatus_Inactive,
1015 0 /*fFlags*/, &TimeSpecTS);
1016}
1017
1018void Display::i_handleUpdateVMMDevSupportsGraphics(bool fSupportsGraphics)
1019{
1020 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1021 if (mfVMMDevSupportsGraphics == fSupportsGraphics)
1022 return;
1023 mfVMMDevSupportsGraphics = fSupportsGraphics;
1024 i_updateGuestGraphicsFacility();
1025 /* The VMMDev interface notifies the console. */
1026}
1027
1028void Display::i_handleUpdateGuestVBVACapabilities(uint32_t fNewCapabilities)
1029{
1030 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1031 bool fNotify = (fNewCapabilities & VBVACAPS_VIDEO_MODE_HINTS) != (mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS);
1032
1033 mfGuestVBVACapabilities = fNewCapabilities;
1034 if (!fNotify)
1035 return;
1036 i_updateGuestGraphicsFacility();
1037 /* Tell the console about it */
1038 mParent->i_onAdditionsStateChange();
1039}
1040
1041void Display::i_handleUpdateVBVAInputMapping(int32_t xOrigin, int32_t yOrigin, uint32_t cx, uint32_t cy)
1042{
1043 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1044
1045 xInputMappingOrigin = xOrigin;
1046 yInputMappingOrigin = yOrigin;
1047 cxInputMapping = cx;
1048 cyInputMapping = cy;
1049
1050 /* Re-send the seamless rectangles if necessary. */
1051 if (mfSeamlessEnabled)
1052 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1053}
1054
1055/**
1056 * Returns the upper left and lower right corners of the virtual framebuffer.
1057 * The lower right is "exclusive" (i.e. first pixel beyond the framebuffer),
1058 * and the origin is (0, 0), not (1, 1) like the GUI returns.
1059 */
1060void Display::i_getFramebufferDimensions(int32_t *px1, int32_t *py1,
1061 int32_t *px2, int32_t *py2)
1062{
1063 int32_t x1 = 0, y1 = 0, x2 = 0, y2 = 0;
1064 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1065
1066 AssertPtrReturnVoid(px1);
1067 AssertPtrReturnVoid(py1);
1068 AssertPtrReturnVoid(px2);
1069 AssertPtrReturnVoid(py2);
1070 LogRelFlowFunc(("\n"));
1071
1072 if (!mpDrv)
1073 return;
1074 /* If VBVA is not in use then this flag will not be set and this
1075 * will still work as it should. */
1076 if (!maFramebuffers[0].fDisabled)
1077 {
1078 x1 = (int32_t)maFramebuffers[0].xOrigin;
1079 y1 = (int32_t)maFramebuffers[0].yOrigin;
1080 x2 = (int32_t)maFramebuffers[0].w + (int32_t)maFramebuffers[0].xOrigin;
1081 y2 = (int32_t)maFramebuffers[0].h + (int32_t)maFramebuffers[0].yOrigin;
1082 }
1083 if (cxInputMapping && cyInputMapping)
1084 {
1085 x1 = xInputMappingOrigin;
1086 y1 = yInputMappingOrigin;
1087 x2 = xInputMappingOrigin + cxInputMapping;
1088 y2 = yInputMappingOrigin + cyInputMapping;
1089 }
1090 else
1091 for (unsigned i = 1; i < mcMonitors; ++i)
1092 {
1093 if (!maFramebuffers[i].fDisabled)
1094 {
1095 x1 = RT_MIN(x1, maFramebuffers[i].xOrigin);
1096 y1 = RT_MIN(y1, maFramebuffers[i].yOrigin);
1097 x2 = RT_MAX(x2, maFramebuffers[i].xOrigin + (int32_t)maFramebuffers[i].w);
1098 y2 = RT_MAX(y2, maFramebuffers[i].yOrigin + (int32_t)maFramebuffers[i].h);
1099 }
1100 }
1101 *px1 = x1;
1102 *py1 = y1;
1103 *px2 = x2;
1104 *py2 = y2;
1105}
1106
1107/** Updates the device's view of the host cursor handling capabilities.
1108 * Calls into mpDrv->pUpPort. */
1109void Display::i_UpdateDeviceCursorCapabilities(void)
1110{
1111 bool fRenderCursor = true;
1112 bool fMoveCursor = mcVRDPRefs == 0;
1113#ifdef VBOX_WITH_RECORDING
1114 RecordingContext *pCtx = mParent->i_recordingGetContext();
1115
1116 if ( pCtx
1117 && pCtx->IsStarted()
1118 && pCtx->IsFeatureEnabled(RecordingFeature_Video))
1119 fRenderCursor = fMoveCursor = false;
1120 else
1121#endif /* VBOX_WITH_RECORDING */
1122 {
1123 for (unsigned uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1124 {
1125 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1126 if (!(pFBInfo->u32Caps & FramebufferCapabilities_RenderCursor))
1127 fRenderCursor = false;
1128 if (!(pFBInfo->u32Caps & FramebufferCapabilities_MoveCursor))
1129 fMoveCursor = false;
1130 }
1131 }
1132
1133 if (mpDrv)
1134 mpDrv->pUpPort->pfnReportHostCursorCapabilities(mpDrv->pUpPort, fRenderCursor, fMoveCursor);
1135}
1136
1137HRESULT Display::i_reportHostCursorCapabilities(uint32_t fCapabilitiesAdded, uint32_t fCapabilitiesRemoved)
1138{
1139 /* Do we need this to access mParent? I presume that the safe VM pointer
1140 * ensures that mpDrv will remain valid. */
1141 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1142 uint32_t fHostCursorCapabilities = (mfHostCursorCapabilities | fCapabilitiesAdded)
1143 & ~fCapabilitiesRemoved;
1144
1145 Console::SafeVMPtr ptrVM(mParent);
1146 if (!ptrVM.isOk())
1147 return ptrVM.rc();
1148 if (mfHostCursorCapabilities == fHostCursorCapabilities)
1149 return S_OK;
1150 CHECK_CONSOLE_DRV(mpDrv);
1151 alock.release(); /* Release before calling up for lock order reasons. */
1152 mfHostCursorCapabilities = fHostCursorCapabilities;
1153 i_UpdateDeviceCursorCapabilities();
1154 return S_OK;
1155}
1156
1157HRESULT Display::i_reportHostCursorPosition(int32_t x, int32_t y, bool fOutOfRange)
1158{
1159 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1160 uint32_t xAdj = (uint32_t)RT_MAX(x - xInputMappingOrigin, 0);
1161 uint32_t yAdj = (uint32_t)RT_MAX(y - yInputMappingOrigin, 0);
1162 xAdj = RT_MIN(xAdj, cxInputMapping);
1163 yAdj = RT_MIN(yAdj, cyInputMapping);
1164
1165 Console::SafeVMPtr ptrVM(mParent);
1166 if (!ptrVM.isOk())
1167 return ptrVM.rc();
1168 CHECK_CONSOLE_DRV(mpDrv);
1169 alock.release(); /* Release before calling up for lock order reasons. */
1170 if (fOutOfRange)
1171 mpDrv->pUpPort->pfnReportHostCursorPosition(mpDrv->pUpPort, 0, 0, true);
1172 else
1173 mpDrv->pUpPort->pfnReportHostCursorPosition(mpDrv->pUpPort, xAdj, yAdj, false);
1174 return S_OK;
1175}
1176
1177static bool displayIntersectRect(RTRECT *prectResult,
1178 const RTRECT *prect1,
1179 const RTRECT *prect2)
1180{
1181 /* Initialize result to an empty record. */
1182 memset(prectResult, 0, sizeof(RTRECT));
1183
1184 int xLeftResult = RT_MAX(prect1->xLeft, prect2->xLeft);
1185 int xRightResult = RT_MIN(prect1->xRight, prect2->xRight);
1186
1187 if (xLeftResult < xRightResult)
1188 {
1189 /* There is intersection by X. */
1190
1191 int yTopResult = RT_MAX(prect1->yTop, prect2->yTop);
1192 int yBottomResult = RT_MIN(prect1->yBottom, prect2->yBottom);
1193
1194 if (yTopResult < yBottomResult)
1195 {
1196 /* There is intersection by Y. */
1197
1198 prectResult->xLeft = xLeftResult;
1199 prectResult->yTop = yTopResult;
1200 prectResult->xRight = xRightResult;
1201 prectResult->yBottom = yBottomResult;
1202
1203 return true;
1204 }
1205 }
1206
1207 return false;
1208}
1209
1210int Display::i_saveVisibleRegion(uint32_t cRect, PRTRECT pRect)
1211{
1212 RTRECT *pRectVisibleRegion = NULL;
1213
1214 if (pRect == mpRectVisibleRegion)
1215 return VINF_SUCCESS;
1216 if (cRect != 0)
1217 {
1218 pRectVisibleRegion = (RTRECT *)RTMemAlloc(cRect * sizeof(RTRECT));
1219 if (!pRectVisibleRegion)
1220 {
1221 return VERR_NO_MEMORY;
1222 }
1223 memcpy(pRectVisibleRegion, pRect, cRect * sizeof(RTRECT));
1224 }
1225 if (mpRectVisibleRegion)
1226 RTMemFree(mpRectVisibleRegion);
1227 mcRectVisibleRegion = cRect;
1228 mpRectVisibleRegion = pRectVisibleRegion;
1229 return VINF_SUCCESS;
1230}
1231
1232int Display::i_handleSetVisibleRegion(uint32_t cRect, PRTRECT pRect)
1233{
1234 RTRECT *pVisibleRegion = (RTRECT *)RTMemTmpAlloc( RT_MAX(cRect, 1)
1235 * sizeof(RTRECT));
1236 LogRel2(("%s: cRect=%u\n", __PRETTY_FUNCTION__, cRect));
1237 if (!pVisibleRegion)
1238 {
1239 return VERR_NO_TMP_MEMORY;
1240 }
1241 int rc = i_saveVisibleRegion(cRect, pRect);
1242 if (RT_FAILURE(rc))
1243 {
1244 RTMemTmpFree(pVisibleRegion);
1245 return rc;
1246 }
1247
1248 unsigned uScreenId;
1249 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1250 {
1251 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1252
1253 if ( !pFBInfo->pFramebuffer.isNull()
1254 && RT_BOOL(pFBInfo->u32Caps & FramebufferCapabilities_VisibleRegion))
1255 {
1256 /* Prepare a new array of rectangles which intersect with the framebuffer.
1257 */
1258 RTRECT rectFramebuffer;
1259 rectFramebuffer.xLeft = pFBInfo->xOrigin - xInputMappingOrigin;
1260 rectFramebuffer.yTop = pFBInfo->yOrigin - yInputMappingOrigin;
1261 rectFramebuffer.xRight = rectFramebuffer.xLeft + pFBInfo->w;
1262 rectFramebuffer.yBottom = rectFramebuffer.yTop + pFBInfo->h;
1263
1264 uint32_t cRectVisibleRegion = 0;
1265
1266 uint32_t i;
1267 for (i = 0; i < cRect; i++)
1268 {
1269 if (displayIntersectRect(&pVisibleRegion[cRectVisibleRegion], &pRect[i], &rectFramebuffer))
1270 {
1271 pVisibleRegion[cRectVisibleRegion].xLeft -= rectFramebuffer.xLeft;
1272 pVisibleRegion[cRectVisibleRegion].yTop -= rectFramebuffer.yTop;
1273 pVisibleRegion[cRectVisibleRegion].xRight -= rectFramebuffer.xLeft;
1274 pVisibleRegion[cRectVisibleRegion].yBottom -= rectFramebuffer.yTop;
1275
1276 cRectVisibleRegion++;
1277 }
1278 }
1279 pFBInfo->pFramebuffer->SetVisibleRegion((BYTE *)pVisibleRegion, cRectVisibleRegion);
1280 }
1281 }
1282
1283 RTMemTmpFree(pVisibleRegion);
1284
1285 return VINF_SUCCESS;
1286}
1287
1288int Display::i_handleUpdateMonitorPositions(uint32_t cPositions, PRTPOINT pPosition)
1289{
1290 AssertMsgReturn(pPosition, ("Empty monitor position array\n"), E_INVALIDARG);
1291 if (mpDrv && mpDrv->pUpPort->pfnReportMonitorPositions)
1292 mpDrv->pUpPort->pfnReportMonitorPositions(mpDrv->pUpPort, cPositions, pPosition);
1293 return VINF_SUCCESS;
1294}
1295
1296int Display::i_handleQueryVisibleRegion(uint32_t *pcRects, PRTRECT paRects)
1297{
1298 /// @todo Currently not used by the guest and is not implemented in
1299 /// framebuffers. Remove?
1300 RT_NOREF(pcRects, paRects);
1301 return VERR_NOT_SUPPORTED;
1302}
1303
1304#ifdef VBOX_WITH_HGSMI
1305static void vbvaSetMemoryFlagsHGSMI(unsigned uScreenId,
1306 uint32_t fu32SupportedOrders,
1307 bool fVideoAccelVRDP,
1308 DISPLAYFBINFO *pFBInfo)
1309{
1310 LogRelFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1311
1312 if (pFBInfo->pVBVAHostFlags)
1313 {
1314 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1315
1316 if (pFBInfo->fVBVAEnabled)
1317 {
1318 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1319
1320 if (fVideoAccelVRDP)
1321 {
1322 fu32HostEvents |= VBVA_F_MODE_VRDP;
1323 }
1324 }
1325
1326 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1327 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1328
1329 LogRelFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1330 }
1331}
1332
1333static void vbvaSetMemoryFlagsAllHGSMI(uint32_t fu32SupportedOrders,
1334 bool fVideoAccelVRDP,
1335 DISPLAYFBINFO *paFBInfos,
1336 unsigned cFBInfos)
1337{
1338 unsigned uScreenId;
1339
1340 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1341 {
1342 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1343 }
1344}
1345#endif /* VBOX_WITH_HGSMI */
1346
1347int Display::VideoAccelEnableVMMDev(bool fEnable, VBVAMEMORY *pVbvaMemory)
1348{
1349 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1350 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1351 if (RT_SUCCESS(rc))
1352 {
1353 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1354 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1355 }
1356 LogFlowFunc(("leave %Rrc\n", rc));
1357 return rc;
1358}
1359
1360int Display::VideoAccelEnableVGA(bool fEnable, VBVAMEMORY *pVbvaMemory)
1361{
1362 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1363 int rc = videoAccelEnterVGA(&mVideoAccelLegacy);
1364 if (RT_SUCCESS(rc))
1365 {
1366 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1367 videoAccelLeaveVGA(&mVideoAccelLegacy);
1368 }
1369 LogFlowFunc(("leave %Rrc\n", rc));
1370 return rc;
1371}
1372
1373void Display::VideoAccelFlushVMMDev(void)
1374{
1375 LogFlowFunc(("enter\n"));
1376 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1377 if (RT_SUCCESS(rc))
1378 {
1379 i_VideoAccelFlush(mpDrv->pUpPort);
1380 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1381 }
1382 LogFlowFunc(("leave\n"));
1383}
1384
1385/* Called always by one VRDP server thread. Can be thread-unsafe.
1386 */
1387void Display::i_VRDPConnectionEvent(bool fConnect)
1388{
1389 LogRelFlowFunc(("fConnect = %d\n", fConnect));
1390
1391 int c = fConnect?
1392 ASMAtomicIncS32(&mcVRDPRefs):
1393 ASMAtomicDecS32(&mcVRDPRefs);
1394
1395 i_VideoAccelVRDP(fConnect, c);
1396 i_UpdateDeviceCursorCapabilities();
1397}
1398
1399
1400void Display::i_VideoAccelVRDP(bool fEnable, int c)
1401{
1402 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
1403
1404 Assert (c >= 0);
1405 RT_NOREF(fEnable);
1406
1407 /* This can run concurrently with Display videoaccel state change. */
1408 RTCritSectEnter(&mVideoAccelLock);
1409
1410 if (c == 0)
1411 {
1412 /* The last client has disconnected, and the accel can be
1413 * disabled.
1414 */
1415 Assert(fEnable == false);
1416
1417 mfVideoAccelVRDP = false;
1418 mfu32SupportedOrders = 0;
1419
1420 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1421 maFramebuffers, mcMonitors);
1422#ifdef VBOX_WITH_HGSMI
1423 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1424 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1425#endif /* VBOX_WITH_HGSMI */
1426
1427 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1428 }
1429 else if ( c == 1
1430 && !mfVideoAccelVRDP)
1431 {
1432 /* The first client has connected. Enable the accel.
1433 */
1434 Assert(fEnable == true);
1435
1436 mfVideoAccelVRDP = true;
1437 /* Supporting all orders. */
1438 mfu32SupportedOrders = UINT32_MAX;
1439
1440 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1441 maFramebuffers, mcMonitors);
1442#ifdef VBOX_WITH_HGSMI
1443 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1444 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1445#endif /* VBOX_WITH_HGSMI */
1446
1447 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1448 }
1449 else
1450 {
1451 /* A client is connected or disconnected but there is no change in the
1452 * accel state. It remains enabled.
1453 */
1454 Assert(mfVideoAccelVRDP == true);
1455 }
1456
1457 RTCritSectLeave(&mVideoAccelLock);
1458}
1459
1460void Display::i_notifyPowerDown(void)
1461{
1462 LogRelFlowFunc(("\n"));
1463
1464 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1465
1466 /* Source bitmaps are not available anymore. */
1467 mfSourceBitmapEnabled = false;
1468
1469 alock.release();
1470
1471 /* Resize all displays to tell framebuffers to forget current source bitmap. */
1472 unsigned uScreenId = mcMonitors;
1473 while (uScreenId > 0)
1474 {
1475 --uScreenId;
1476
1477 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1478 if (!pFBInfo->fDisabled)
1479 {
1480 i_handleDisplayResize(uScreenId, 32,
1481 pFBInfo->pu8FramebufferVRAM,
1482 pFBInfo->u32LineSize,
1483 pFBInfo->w,
1484 pFBInfo->h,
1485 pFBInfo->flags,
1486 pFBInfo->xOrigin,
1487 pFBInfo->yOrigin,
1488 false);
1489 }
1490 }
1491}
1492
1493// Wrapped IDisplay methods
1494/////////////////////////////////////////////////////////////////////////////
1495HRESULT Display::getScreenResolution(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel,
1496 LONG *aXOrigin, LONG *aYOrigin, GuestMonitorStatus_T *aGuestMonitorStatus)
1497{
1498 LogRelFlowFunc(("aScreenId=%RU32\n", aScreenId));
1499
1500 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1501
1502 if (aScreenId >= mcMonitors)
1503 return E_INVALIDARG;
1504
1505 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1506
1507 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
1508
1509 if (pFBInfo->flags & VBVA_SCREEN_F_DISABLED)
1510 guestMonitorStatus = GuestMonitorStatus_Disabled;
1511 else if (pFBInfo->flags & (VBVA_SCREEN_F_BLANK | VBVA_SCREEN_F_BLANK2))
1512 guestMonitorStatus = GuestMonitorStatus_Blank;
1513
1514 if (aWidth)
1515 *aWidth = pFBInfo->w;
1516 if (aHeight)
1517 *aHeight = pFBInfo->h;
1518 if (aBitsPerPixel)
1519 *aBitsPerPixel = pFBInfo->u16BitsPerPixel;
1520 if (aXOrigin)
1521 *aXOrigin = pFBInfo->xOrigin;
1522 if (aYOrigin)
1523 *aYOrigin = pFBInfo->yOrigin;
1524 if (aGuestMonitorStatus)
1525 *aGuestMonitorStatus = guestMonitorStatus;
1526
1527 return S_OK;
1528}
1529
1530
1531HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer, com::Guid &aId)
1532{
1533 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1534
1535 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1536
1537 if (aScreenId >= mcMonitors)
1538 return setError(E_INVALIDARG, tr("AttachFramebuffer: Invalid screen %d (total %d)"),
1539 aScreenId, mcMonitors);
1540
1541 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1542 if (!pFBInfo->pFramebuffer.isNull())
1543 return setError(E_FAIL, tr("AttachFramebuffer: Framebuffer already attached to %d"),
1544 aScreenId);
1545
1546 pFBInfo->pFramebuffer = aFramebuffer;
1547 pFBInfo->framebufferId.create();
1548 aId = pFBInfo->framebufferId;
1549
1550 SafeArray<FramebufferCapabilities_T> caps;
1551 pFBInfo->pFramebuffer->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(caps));
1552 pFBInfo->u32Caps = 0;
1553 size_t i;
1554 for (i = 0; i < caps.size(); ++i)
1555 pFBInfo->u32Caps |= caps[i];
1556
1557 alock.release();
1558
1559 /* The driver might not have been constructed yet */
1560 if (mpDrv)
1561 {
1562 /* Inform the framebuffer about the actual screen size. */
1563 HRESULT hr = aFramebuffer->NotifyChange(aScreenId, 0, 0, pFBInfo->w, pFBInfo->h); /** @todo origin */
1564 LogFunc(("NotifyChange hr %08X\n", hr)); NOREF(hr);
1565
1566 /* Re-send the seamless rectangles if necessary. */
1567 if (mfSeamlessEnabled)
1568 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1569 }
1570
1571 Console::SafeVMPtrQuiet ptrVM(mParent);
1572 if (ptrVM.isOk())
1573 {
1574 VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
1575 3, this, aScreenId, false);
1576 }
1577
1578 LogRelFlowFunc(("Attached to %d %RTuuid\n", aScreenId, aId.raw()));
1579 return S_OK;
1580}
1581
1582HRESULT Display::detachFramebuffer(ULONG aScreenId, const com::Guid &aId)
1583{
1584 LogRelFlowFunc(("aScreenId = %d %RTuuid\n", aScreenId, aId.raw()));
1585
1586 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1587
1588 if (aScreenId >= mcMonitors)
1589 return setError(E_INVALIDARG, tr("DetachFramebuffer: Invalid screen %d (total %d)"),
1590 aScreenId, mcMonitors);
1591
1592 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1593
1594 if (pFBInfo->framebufferId != aId)
1595 {
1596 LogRelFlowFunc(("Invalid framebuffer aScreenId = %d, attached %p\n", aScreenId, pFBInfo->framebufferId.raw()));
1597 return setError(E_FAIL, tr("DetachFramebuffer: Invalid framebuffer object"));
1598 }
1599
1600 pFBInfo->pFramebuffer.setNull();
1601 pFBInfo->framebufferId.clear();
1602
1603 alock.release();
1604 return S_OK;
1605}
1606
1607HRESULT Display::queryFramebuffer(ULONG aScreenId, ComPtr<IFramebuffer> &aFramebuffer)
1608{
1609 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1610
1611 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1612
1613 if (aScreenId >= mcMonitors)
1614 return setError(E_INVALIDARG, tr("QueryFramebuffer: Invalid screen %d (total %d)"),
1615 aScreenId, mcMonitors);
1616
1617 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1618
1619 pFBInfo->pFramebuffer.queryInterfaceTo(aFramebuffer.asOutParam());
1620
1621 return S_OK;
1622}
1623
1624HRESULT Display::setVideoModeHint(ULONG aDisplay, BOOL aEnabled,
1625 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
1626 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel,
1627 BOOL aNotify)
1628{
1629 if (aWidth == 0 || aHeight == 0 || aBitsPerPixel == 0)
1630 {
1631 /* Some of parameters must not change. Query current mode. */
1632 ULONG ulWidth = 0;
1633 ULONG ulHeight = 0;
1634 ULONG ulBitsPerPixel = 0;
1635 HRESULT hr = getScreenResolution(aDisplay, &ulWidth, &ulHeight, &ulBitsPerPixel, NULL, NULL, NULL);
1636 if (FAILED(hr))
1637 return hr;
1638
1639 /* Assign current values to not changing parameters. */
1640 if (aWidth == 0)
1641 aWidth = ulWidth;
1642 if (aHeight == 0)
1643 aHeight = ulHeight;
1644 if (aBitsPerPixel == 0)
1645 aBitsPerPixel = ulBitsPerPixel;
1646 }
1647
1648 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1649
1650 if (aDisplay >= mcMonitors)
1651 return E_INVALIDARG;
1652
1653 VMMDevDisplayDef d;
1654 d.idDisplay = aDisplay;
1655 d.xOrigin = aOriginX;
1656 d.yOrigin = aOriginY;
1657 d.cx = aWidth;
1658 d.cy = aHeight;
1659 d.cBitsPerPixel = aBitsPerPixel;
1660 d.fDisplayFlags = VMMDEV_DISPLAY_CX | VMMDEV_DISPLAY_CY | VMMDEV_DISPLAY_BPP;
1661 if (!aEnabled)
1662 d.fDisplayFlags |= VMMDEV_DISPLAY_DISABLED;
1663 if (aChangeOrigin)
1664 d.fDisplayFlags |= VMMDEV_DISPLAY_ORIGIN;
1665 if (aDisplay == 0)
1666 d.fDisplayFlags |= VMMDEV_DISPLAY_PRIMARY;
1667
1668 /* Remember the monitor information. */
1669 maFramebuffers[aDisplay].monitorDesc = d;
1670
1671 CHECK_CONSOLE_DRV(mpDrv);
1672
1673 /*
1674 * It is up to the guest to decide whether the hint is
1675 * valid. Therefore don't do any VRAM sanity checks here.
1676 */
1677
1678 /* Have to release the lock because the pfnRequestDisplayChange
1679 * will call EMT. */
1680 alock.release();
1681
1682 /* We always send the hint to the graphics card in case the guest enables
1683 * support later. For now we notify exactly when support is enabled. */
1684 mpDrv->pUpPort->pfnSendModeHint(mpDrv->pUpPort, aWidth, aHeight,
1685 aBitsPerPixel, aDisplay,
1686 aChangeOrigin ? aOriginX : ~0,
1687 aChangeOrigin ? aOriginY : ~0,
1688 RT_BOOL(aEnabled),
1689 ( mfGuestVBVACapabilities
1690 & VBVACAPS_VIDEO_MODE_HINTS)
1691 && aNotify);
1692 if ( mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS
1693 && !(mfGuestVBVACapabilities & VBVACAPS_IRQ)
1694 && aNotify)
1695 {
1696 mParent->i_sendACPIMonitorHotPlugEvent();
1697 }
1698
1699 /* We currently never suppress the VMMDev hint if the guest has requested
1700 * it. Specifically the video graphics driver may not be responsible for
1701 * screen positioning in the guest virtual desktop, and the component
1702 * responsible may want to get the hint from VMMDev. */
1703 VMMDev *pVMMDev = mParent->i_getVMMDev();
1704 if (pVMMDev)
1705 {
1706 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1707 if (pVMMDevPort)
1708 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, 1, &d, false, RT_BOOL(aNotify));
1709 }
1710 /* Notify listeners. */
1711 fireGuestMonitorInfoChangedEvent(mParent->i_getEventSource(), aDisplay);
1712 return S_OK;
1713}
1714
1715HRESULT Display::getVideoModeHint(ULONG cDisplay, BOOL *pfEnabled,
1716 BOOL *pfChangeOrigin, LONG *pxOrigin, LONG *pyOrigin,
1717 ULONG *pcx, ULONG *pcy, ULONG *pcBitsPerPixel)
1718{
1719 if (cDisplay >= mcMonitors)
1720 return E_INVALIDARG;
1721
1722 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1723 if (pfEnabled)
1724 *pfEnabled = !( maFramebuffers[cDisplay].monitorDesc.fDisplayFlags
1725 & VMMDEV_DISPLAY_DISABLED);
1726 if (pfChangeOrigin)
1727 *pfChangeOrigin = RT_BOOL( maFramebuffers[cDisplay].monitorDesc.fDisplayFlags
1728 & VMMDEV_DISPLAY_ORIGIN);
1729 if (pxOrigin)
1730 *pxOrigin = maFramebuffers[cDisplay].monitorDesc.xOrigin;
1731 if (pyOrigin)
1732 *pyOrigin = maFramebuffers[cDisplay].monitorDesc.yOrigin;
1733 if (pcx)
1734 *pcx = maFramebuffers[cDisplay].monitorDesc.cx;
1735 if (pcy)
1736 *pcy = maFramebuffers[cDisplay].monitorDesc.cy;
1737 if (pcBitsPerPixel)
1738 *pcBitsPerPixel = maFramebuffers[cDisplay].monitorDesc.cBitsPerPixel;
1739 return S_OK;
1740}
1741
1742HRESULT Display::setSeamlessMode(BOOL enabled)
1743{
1744 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1745
1746 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
1747 alock.release();
1748
1749 VMMDev *pVMMDev = mParent->i_getVMMDev();
1750 if (pVMMDev)
1751 {
1752 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1753 if (pVMMDevPort)
1754 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
1755 }
1756 mfSeamlessEnabled = RT_BOOL(enabled);
1757 return S_OK;
1758}
1759
1760/* static */
1761int Display::i_displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppbData, size_t *pcbData,
1762 uint32_t *pcx, uint32_t *pcy, bool *pfMemFree)
1763{
1764 int rc;
1765 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
1766 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
1767 {
1768 if (pDisplay->mpDrv)
1769 {
1770 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
1771 *pfMemFree = false;
1772 }
1773 else
1774 {
1775 /* No image. */
1776 *ppbData = NULL;
1777 *pcbData = 0;
1778 *pcx = 0;
1779 *pcy = 0;
1780 *pfMemFree = true;
1781 rc = VINF_SUCCESS;
1782 }
1783 }
1784 else if (aScreenId < pDisplay->mcMonitors)
1785 {
1786 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
1787
1788 uint32_t width = pFBInfo->w;
1789 uint32_t height = pFBInfo->h;
1790
1791 /* Allocate 32 bit per pixel bitmap. */
1792 size_t cbRequired = width * 4 * height;
1793
1794 if (cbRequired)
1795 {
1796 uint8_t *pbDst = (uint8_t *)RTMemAlloc(cbRequired);
1797 if (pbDst != NULL)
1798 {
1799 if (pFBInfo->flags & VBVA_SCREEN_F_ACTIVE)
1800 {
1801 /* Copy guest VRAM to the allocated 32bpp buffer. */
1802 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
1803 int32_t xSrc = 0;
1804 int32_t ySrc = 0;
1805 uint32_t u32SrcWidth = width;
1806 uint32_t u32SrcHeight = height;
1807 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
1808 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
1809
1810 int32_t xDst = 0;
1811 int32_t yDst = 0;
1812 uint32_t u32DstWidth = u32SrcWidth;
1813 uint32_t u32DstHeight = u32SrcHeight;
1814 uint32_t u32DstLineSize = u32DstWidth * 4;
1815 uint32_t u32DstBitsPerPixel = 32;
1816
1817 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
1818 width, height,
1819 pu8Src,
1820 xSrc, ySrc,
1821 u32SrcWidth, u32SrcHeight,
1822 u32SrcLineSize, u32SrcBitsPerPixel,
1823 pbDst,
1824 xDst, yDst,
1825 u32DstWidth, u32DstHeight,
1826 u32DstLineSize, u32DstBitsPerPixel);
1827 }
1828 else
1829 {
1830 memset(pbDst, 0, cbRequired);
1831 rc = VINF_SUCCESS;
1832 }
1833 if (RT_SUCCESS(rc))
1834 {
1835 *ppbData = pbDst;
1836 *pcbData = cbRequired;
1837 *pcx = width;
1838 *pcy = height;
1839 *pfMemFree = true;
1840 }
1841 else
1842 {
1843 RTMemFree(pbDst);
1844
1845 /* CopyRect can fail if VBVA was paused in VGA device, retry using the generic method. */
1846 if ( rc == VERR_INVALID_STATE
1847 && aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1848 {
1849 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
1850 *pfMemFree = false;
1851 }
1852 }
1853 }
1854 else
1855 rc = VERR_NO_MEMORY;
1856 }
1857 else
1858 {
1859 /* No image. */
1860 *ppbData = NULL;
1861 *pcbData = 0;
1862 *pcx = 0;
1863 *pcy = 0;
1864 *pfMemFree = true;
1865 rc = VINF_SUCCESS;
1866 }
1867 }
1868 else
1869 rc = VERR_INVALID_PARAMETER;
1870 return rc;
1871}
1872
1873static int i_displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
1874 BYTE *address, ULONG width, ULONG height)
1875{
1876 uint8_t *pbData = NULL;
1877 size_t cbData = 0;
1878 uint32_t cx = 0;
1879 uint32_t cy = 0;
1880 bool fFreeMem = false;
1881 int vrc = VINF_SUCCESS;
1882
1883 int cRetries = 5;
1884 while (cRetries-- > 0)
1885 {
1886 /* Note! Not sure if the priority call is such a good idea here, but
1887 it would be nice to have an accurate screenshot for the bug
1888 report if the VM deadlocks. */
1889 vrc = VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)Display::i_displayTakeScreenshotEMT, 7,
1890 pDisplay, aScreenId, &pbData, &cbData, &cx, &cy, &fFreeMem);
1891 if (vrc != VERR_TRY_AGAIN)
1892 {
1893 break;
1894 }
1895
1896 RTThreadSleep(10);
1897 }
1898
1899 if (RT_SUCCESS(vrc) && pbData)
1900 {
1901 if (cx == width && cy == height)
1902 {
1903 /* No scaling required. */
1904 memcpy(address, pbData, cbData);
1905 }
1906 else
1907 {
1908 /* Scale. */
1909 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
1910
1911 uint8_t *dst = address;
1912 uint8_t *src = pbData;
1913 int dstW = width;
1914 int dstH = height;
1915 int srcW = cx;
1916 int srcH = cy;
1917 int iDeltaLine = cx * 4;
1918
1919 BitmapScale32(dst,
1920 dstW, dstH,
1921 src,
1922 iDeltaLine,
1923 srcW, srcH);
1924 }
1925
1926 if (fFreeMem)
1927 RTMemFree(pbData);
1928 else
1929 {
1930 /* This can be called from any thread. */
1931 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pbData);
1932 }
1933 }
1934
1935 return vrc;
1936}
1937
1938HRESULT Display::takeScreenShotWorker(ULONG aScreenId,
1939 BYTE *aAddress,
1940 ULONG aWidth,
1941 ULONG aHeight,
1942 BitmapFormat_T aBitmapFormat,
1943 ULONG *pcbOut)
1944{
1945 HRESULT rc = S_OK;
1946
1947 /* Do not allow too small and too large screenshots. This also filters out negative
1948 * values passed as either 'aWidth' or 'aHeight'.
1949 */
1950 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
1951 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
1952
1953 if ( aBitmapFormat != BitmapFormat_BGR0
1954 && aBitmapFormat != BitmapFormat_BGRA
1955 && aBitmapFormat != BitmapFormat_RGBA
1956 && aBitmapFormat != BitmapFormat_PNG)
1957 {
1958 return setError(E_NOTIMPL,
1959 tr("Unsupported screenshot format 0x%08X"), aBitmapFormat);
1960 }
1961
1962 Console::SafeVMPtr ptrVM(mParent);
1963 if (!ptrVM.isOk())
1964 return ptrVM.rc();
1965
1966 int vrc = i_displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, aAddress, aWidth, aHeight);
1967
1968 if (RT_SUCCESS(vrc))
1969 {
1970 const size_t cbData = aWidth * 4 * aHeight;
1971
1972 /* Most of uncompressed formats. */
1973 *pcbOut = (ULONG)cbData;
1974
1975 if (aBitmapFormat == BitmapFormat_BGR0)
1976 {
1977 /* Do nothing. */
1978 }
1979 else if (aBitmapFormat == BitmapFormat_BGRA)
1980 {
1981 uint32_t *pu32 = (uint32_t *)aAddress;
1982 size_t cPixels = aWidth * aHeight;
1983 while (cPixels--)
1984 {
1985 *pu32++ |= UINT32_C(0xFF000000);
1986 }
1987 }
1988 else if (aBitmapFormat == BitmapFormat_RGBA)
1989 {
1990 uint8_t *pu8 = aAddress;
1991 size_t cPixels = aWidth * aHeight;
1992 while (cPixels--)
1993 {
1994 uint8_t u8 = pu8[0];
1995 pu8[0] = pu8[2];
1996 pu8[2] = u8;
1997 pu8[3] = 0xFF;
1998
1999 pu8 += 4;
2000 }
2001 }
2002 else if (aBitmapFormat == BitmapFormat_PNG)
2003 {
2004 uint8_t *pu8PNG = NULL;
2005 uint32_t cbPNG = 0;
2006 uint32_t cxPNG = 0;
2007 uint32_t cyPNG = 0;
2008
2009 vrc = DisplayMakePNG(aAddress, aWidth, aHeight, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2010 if (RT_SUCCESS(vrc))
2011 {
2012 if (cbPNG <= cbData)
2013 {
2014 memcpy(aAddress, pu8PNG, cbPNG);
2015 *pcbOut = cbPNG;
2016 }
2017 else
2018 {
2019 rc = setError(E_FAIL,
2020 tr("PNG is larger than 32bpp bitmap"));
2021 }
2022 }
2023 else
2024 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2025 RTMemFree(pu8PNG);
2026 }
2027 }
2028 else if (vrc == VERR_TRY_AGAIN)
2029 rc = setErrorBoth(E_UNEXPECTED, vrc, tr("Screenshot is not available at this time"));
2030 else if (RT_FAILURE(vrc))
2031 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not take a screenshot (%Rrc)"), vrc);
2032
2033 return rc;
2034}
2035
2036HRESULT Display::takeScreenShot(ULONG aScreenId,
2037 BYTE *aAddress,
2038 ULONG aWidth,
2039 ULONG aHeight,
2040 BitmapFormat_T aBitmapFormat)
2041{
2042 HRESULT rc = S_OK;
2043
2044 LogRelFlowFunc(("[%d] address=%p, width=%d, height=%d, format 0x%08X\n",
2045 aScreenId, aAddress, aWidth, aHeight, aBitmapFormat));
2046
2047 ULONG cbOut = 0;
2048 rc = takeScreenShotWorker(aScreenId, aAddress, aWidth, aHeight, aBitmapFormat, &cbOut);
2049 NOREF(cbOut);
2050
2051 LogRelFlowFunc(("%Rhrc\n", rc));
2052 return rc;
2053}
2054
2055HRESULT Display::takeScreenShotToArray(ULONG aScreenId,
2056 ULONG aWidth,
2057 ULONG aHeight,
2058 BitmapFormat_T aBitmapFormat,
2059 std::vector<BYTE> &aScreenData)
2060{
2061 HRESULT rc = S_OK;
2062
2063 LogRelFlowFunc(("[%d] width=%d, height=%d, format 0x%08X\n",
2064 aScreenId, aWidth, aHeight, aBitmapFormat));
2065
2066 /* Do not allow too small and too large screenshots. This also filters out negative
2067 * values passed as either 'aWidth' or 'aHeight'.
2068 */
2069 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2070 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2071
2072 const size_t cbData = aWidth * 4 * aHeight;
2073 aScreenData.resize(cbData);
2074
2075 ULONG cbOut = 0;
2076 rc = takeScreenShotWorker(aScreenId, &aScreenData.front(), aWidth, aHeight, aBitmapFormat, &cbOut);
2077 if (FAILED(rc))
2078 cbOut = 0;
2079
2080 aScreenData.resize(cbOut);
2081
2082 LogRelFlowFunc(("%Rhrc\n", rc));
2083 return rc;
2084}
2085
2086#ifdef VBOX_WITH_RECORDING
2087/**
2088 * Invalidates the recording configuration.
2089 *
2090 * @returns IPRT status code.
2091 */
2092int Display::i_recordingInvalidate(void)
2093{
2094 RecordingContext *pCtx = mParent->i_recordingGetContext();
2095 if (!pCtx || !pCtx->IsStarted())
2096 return VINF_SUCCESS;
2097
2098 /*
2099 * Invalidate screens.
2100 */
2101 for (unsigned uScreen = 0; uScreen < mcMonitors; uScreen++)
2102 {
2103 RecordingStream *pRecordingStream = pCtx->GetStream(uScreen);
2104
2105 const bool fStreamEnabled = pRecordingStream->IsReady();
2106 bool fChanged = maRecordingEnabled[uScreen] != fStreamEnabled;
2107
2108 maRecordingEnabled[uScreen] = fStreamEnabled;
2109
2110 if (fChanged && uScreen < mcMonitors)
2111 i_recordingScreenChanged(uScreen);
2112 }
2113
2114 return VINF_SUCCESS;
2115}
2116
2117void Display::i_recordingScreenChanged(unsigned uScreenId)
2118{
2119 RecordingContext *pCtx = mParent->i_recordingGetContext();
2120
2121 i_UpdateDeviceCursorCapabilities();
2122 if ( RT_LIKELY(!maRecordingEnabled[uScreenId])
2123 || !pCtx || !pCtx->IsStarted())
2124 {
2125 /* Skip recording this screen. */
2126 return;
2127 }
2128
2129 /* Get a new source bitmap which will be used by video recording code. */
2130 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
2131 QuerySourceBitmap(uScreenId, pSourceBitmap.asOutParam());
2132
2133 int rc2 = RTCritSectEnter(&mVideoRecLock);
2134 if (RT_SUCCESS(rc2))
2135 {
2136 maFramebuffers[uScreenId].Recording.pSourceBitmap = pSourceBitmap;
2137
2138 rc2 = RTCritSectLeave(&mVideoRecLock);
2139 AssertRC(rc2);
2140 }
2141}
2142#endif /* VBOX_WITH_RECORDING */
2143
2144int Display::i_drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address,
2145 ULONG x, ULONG y, ULONG width, ULONG height)
2146{
2147 int rc = VINF_SUCCESS;
2148
2149 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2150
2151 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2152 {
2153 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2154 }
2155 else if (aScreenId < pDisplay->mcMonitors)
2156 {
2157 /* Copy the bitmap to the guest VRAM. */
2158 const uint8_t *pu8Src = address;
2159 int32_t xSrc = 0;
2160 int32_t ySrc = 0;
2161 uint32_t u32SrcWidth = width;
2162 uint32_t u32SrcHeight = height;
2163 uint32_t u32SrcLineSize = width * 4;
2164 uint32_t u32SrcBitsPerPixel = 32;
2165
2166 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2167 int32_t xDst = x;
2168 int32_t yDst = y;
2169 uint32_t u32DstWidth = pFBInfo->w;
2170 uint32_t u32DstHeight = pFBInfo->h;
2171 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2172 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2173
2174 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2175 width, height,
2176 pu8Src,
2177 xSrc, ySrc,
2178 u32SrcWidth, u32SrcHeight,
2179 u32SrcLineSize, u32SrcBitsPerPixel,
2180 pu8Dst,
2181 xDst, yDst,
2182 u32DstWidth, u32DstHeight,
2183 u32DstLineSize, u32DstBitsPerPixel);
2184 if (RT_SUCCESS(rc))
2185 {
2186 if (!pFBInfo->pSourceBitmap.isNull())
2187 {
2188 /* Update the changed screen area. When source bitmap uses VRAM directly, just notify
2189 * frontend to update. And for default format, render the guest VRAM to the source bitmap.
2190 */
2191 if ( pFBInfo->fDefaultFormat
2192 && !pFBInfo->fDisabled)
2193 {
2194 BYTE *pAddress = NULL;
2195 ULONG ulWidth = 0;
2196 ULONG ulHeight = 0;
2197 ULONG ulBitsPerPixel = 0;
2198 ULONG ulBytesPerLine = 0;
2199 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2200
2201 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2202 &ulWidth,
2203 &ulHeight,
2204 &ulBitsPerPixel,
2205 &ulBytesPerLine,
2206 &bitmapFormat);
2207 if (SUCCEEDED(hrc))
2208 {
2209 pu8Src = pFBInfo->pu8FramebufferVRAM;
2210 xSrc = x;
2211 ySrc = y;
2212 u32SrcWidth = pFBInfo->w;
2213 u32SrcHeight = pFBInfo->h;
2214 u32SrcLineSize = pFBInfo->u32LineSize;
2215 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2216
2217 /* Default format is 32 bpp. */
2218 pu8Dst = pAddress;
2219 xDst = xSrc;
2220 yDst = ySrc;
2221 u32DstWidth = u32SrcWidth;
2222 u32DstHeight = u32SrcHeight;
2223 u32DstLineSize = u32DstWidth * 4;
2224 u32DstBitsPerPixel = 32;
2225
2226 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2227 width, height,
2228 pu8Src,
2229 xSrc, ySrc,
2230 u32SrcWidth, u32SrcHeight,
2231 u32SrcLineSize, u32SrcBitsPerPixel,
2232 pu8Dst,
2233 xDst, yDst,
2234 u32DstWidth, u32DstHeight,
2235 u32DstLineSize, u32DstBitsPerPixel);
2236 }
2237 }
2238 }
2239
2240 pDisplay->i_handleDisplayUpdate(aScreenId, x, y, width, height);
2241 }
2242 }
2243 else
2244 {
2245 rc = VERR_INVALID_PARAMETER;
2246 }
2247
2248 if (RT_SUCCESS(rc))
2249 pDisplay->mParent->i_consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
2250
2251 return rc;
2252}
2253
2254HRESULT Display::drawToScreen(ULONG aScreenId, BYTE *aAddress, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2255{
2256 /// @todo (r=dmik) this function may take too long to complete if the VM
2257 // is doing something like saving state right now. Which, in case if it
2258 // is called on the GUI thread, will make it unresponsive. We should
2259 // check the machine state here (by enclosing the check and VMRequCall
2260 // within the Console lock to make it atomic).
2261
2262 LogRelFlowFunc(("aAddress=%p, x=%d, y=%d, width=%d, height=%d\n",
2263 (void *)aAddress, aX, aY, aWidth, aHeight));
2264
2265 CheckComArgExpr(aWidth, aWidth != 0);
2266 CheckComArgExpr(aHeight, aHeight != 0);
2267
2268 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2269
2270 CHECK_CONSOLE_DRV(mpDrv);
2271
2272 Console::SafeVMPtr ptrVM(mParent);
2273 if (!ptrVM.isOk())
2274 return ptrVM.rc();
2275
2276 /* Release lock because the call scheduled on EMT may also try to take it. */
2277 alock.release();
2278
2279 /*
2280 * Again we're lazy and make the graphics device do all the
2281 * dirty conversion work.
2282 */
2283 int vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_drawToScreenEMT, 7,
2284 this, aScreenId, aAddress, aX, aY, aWidth, aHeight);
2285
2286 /*
2287 * If the function returns not supported, we'll have to do all the
2288 * work ourselves using the framebuffer.
2289 */
2290 HRESULT rc = S_OK;
2291 if (vrc == VERR_NOT_SUPPORTED || vrc == VERR_NOT_IMPLEMENTED)
2292 {
2293 /** @todo implement generic fallback for screen blitting. */
2294 rc = E_NOTIMPL;
2295 }
2296 else if (RT_FAILURE(vrc))
2297 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not draw to the screen (%Rrc)"), vrc);
2298/// @todo
2299// else
2300// {
2301// /* All ok. Redraw the screen. */
2302// handleDisplayUpdate(x, y, width, height);
2303// }
2304
2305 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2306 return rc;
2307}
2308
2309int Display::i_InvalidateAndUpdateEMT(Display *pDisplay, unsigned uId, bool fUpdateAll)
2310{
2311 LogRelFlowFunc(("uId=%d, fUpdateAll %d\n", uId, fUpdateAll));
2312
2313 unsigned uScreenId;
2314 for (uScreenId = (fUpdateAll ? 0 : uId); uScreenId < pDisplay->mcMonitors; uScreenId++)
2315 {
2316 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2317
2318 if ( !pFBInfo->fVBVAEnabled
2319 && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2320 {
2321 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort, /* fFailOnResize = */ true);
2322 }
2323 else
2324 {
2325 if (!pFBInfo->fDisabled)
2326 {
2327 /* Render complete VRAM screen to the framebuffer.
2328 * When framebuffer uses VRAM directly, just notify it to update.
2329 */
2330 if (pFBInfo->fDefaultFormat && !pFBInfo->pSourceBitmap.isNull())
2331 {
2332 BYTE *pAddress = NULL;
2333 ULONG ulWidth = 0;
2334 ULONG ulHeight = 0;
2335 ULONG ulBitsPerPixel = 0;
2336 ULONG ulBytesPerLine = 0;
2337 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2338
2339 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2340 &ulWidth,
2341 &ulHeight,
2342 &ulBitsPerPixel,
2343 &ulBytesPerLine,
2344 &bitmapFormat);
2345 if (SUCCEEDED(hrc))
2346 {
2347 uint32_t width = pFBInfo->w;
2348 uint32_t height = pFBInfo->h;
2349
2350 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2351 int32_t xSrc = 0;
2352 int32_t ySrc = 0;
2353 uint32_t u32SrcWidth = pFBInfo->w;
2354 uint32_t u32SrcHeight = pFBInfo->h;
2355 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2356 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2357
2358 /* Default format is 32 bpp. */
2359 uint8_t *pu8Dst = pAddress;
2360 int32_t xDst = xSrc;
2361 int32_t yDst = ySrc;
2362 uint32_t u32DstWidth = u32SrcWidth;
2363 uint32_t u32DstHeight = u32SrcHeight;
2364 uint32_t u32DstLineSize = u32DstWidth * 4;
2365 uint32_t u32DstBitsPerPixel = 32;
2366
2367 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
2368 * implies resize of Framebuffer is in progress and
2369 * copyrect should not be called.
2370 */
2371 if (ulWidth == pFBInfo->w && ulHeight == pFBInfo->h)
2372 {
2373 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2374 width, height,
2375 pu8Src,
2376 xSrc, ySrc,
2377 u32SrcWidth, u32SrcHeight,
2378 u32SrcLineSize, u32SrcBitsPerPixel,
2379 pu8Dst,
2380 xDst, yDst,
2381 u32DstWidth, u32DstHeight,
2382 u32DstLineSize, u32DstBitsPerPixel);
2383 }
2384 }
2385 }
2386
2387 pDisplay->i_handleDisplayUpdate(uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2388 }
2389 }
2390 if (!fUpdateAll)
2391 break;
2392 }
2393 LogRelFlowFunc(("done\n"));
2394 return VINF_SUCCESS;
2395}
2396
2397/**
2398 * Does a full invalidation of the VM display and instructs the VM
2399 * to update it immediately.
2400 *
2401 * @returns COM status code
2402 */
2403
2404HRESULT Display::invalidateAndUpdate()
2405{
2406 LogRelFlowFunc(("\n"));
2407
2408 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2409
2410 CHECK_CONSOLE_DRV(mpDrv);
2411
2412 Console::SafeVMPtr ptrVM(mParent);
2413 if (!ptrVM.isOk())
2414 return ptrVM.rc();
2415
2416 HRESULT rc = S_OK;
2417
2418 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
2419
2420 /* Have to release the lock when calling EMT. */
2421 alock.release();
2422
2423 int vrc = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2424 3, this, 0, true);
2425 alock.acquire();
2426
2427 if (RT_FAILURE(vrc))
2428 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not invalidate and update the screen (%Rrc)"), vrc);
2429
2430 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2431 return rc;
2432}
2433
2434HRESULT Display::invalidateAndUpdateScreen(ULONG aScreenId)
2435{
2436 LogRelFlowFunc(("\n"));
2437
2438 HRESULT rc = S_OK;
2439
2440 Console::SafeVMPtr ptrVM(mParent);
2441 if (!ptrVM.isOk())
2442 return ptrVM.rc();
2443
2444 int vrc = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2445 3, this, aScreenId, false);
2446 if (RT_FAILURE(vrc))
2447 rc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not invalidate and update the screen %d (%Rrc)"), aScreenId, vrc);
2448
2449 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2450 return rc;
2451}
2452
2453HRESULT Display::completeVHWACommand(BYTE *aCommand)
2454{
2455#ifdef VBOX_WITH_VIDEOHWACCEL
2456 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsync(mpDrv->pVBVACallbacks, (VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *)aCommand);
2457 return S_OK;
2458#else
2459 RT_NOREF(aCommand);
2460 return E_NOTIMPL;
2461#endif
2462}
2463
2464HRESULT Display::viewportChanged(ULONG aScreenId, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2465{
2466 AssertMsgReturn(aScreenId < mcMonitors, ("aScreendId=%d mcMonitors=%d\n", aScreenId, mcMonitors), E_INVALIDARG);
2467
2468 /* The driver might not have been constructed yet */
2469 if (mpDrv && mpDrv->pUpPort->pfnSetViewport)
2470 mpDrv->pUpPort->pfnSetViewport(mpDrv->pUpPort, aScreenId, aX, aY, aWidth, aHeight);
2471
2472 return S_OK;
2473}
2474
2475HRESULT Display::querySourceBitmap(ULONG aScreenId,
2476 ComPtr<IDisplaySourceBitmap> &aDisplaySourceBitmap)
2477{
2478 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2479
2480 Console::SafeVMPtr ptrVM(mParent);
2481 if (!ptrVM.isOk())
2482 return ptrVM.rc();
2483
2484 CHECK_CONSOLE_DRV(mpDrv);
2485
2486 bool fSetRenderVRAM = false;
2487 bool fInvalidate = false;
2488
2489 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2490
2491 if (aScreenId >= mcMonitors)
2492 return setError(E_INVALIDARG, tr("QuerySourceBitmap: Invalid screen %d (total %d)"),
2493 aScreenId, mcMonitors);
2494
2495 if (!mfSourceBitmapEnabled)
2496 {
2497 aDisplaySourceBitmap = NULL;
2498 return E_FAIL;
2499 }
2500
2501 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2502
2503 /* No source bitmap for a blank guest screen. */
2504 if (pFBInfo->flags & VBVA_SCREEN_F_BLANK)
2505 {
2506 aDisplaySourceBitmap = NULL;
2507 return E_FAIL;
2508 }
2509
2510 HRESULT hr = S_OK;
2511
2512 if (pFBInfo->pSourceBitmap.isNull())
2513 {
2514 /* Create a new object. */
2515 ComObjPtr<DisplaySourceBitmap> obj;
2516 hr = obj.createObject();
2517 if (SUCCEEDED(hr))
2518 hr = obj->init(this, aScreenId, pFBInfo);
2519
2520 if (SUCCEEDED(hr))
2521 {
2522 pFBInfo->pSourceBitmap = obj;
2523 pFBInfo->fDefaultFormat = !obj->i_usesVRAM();
2524
2525 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2526 {
2527 /* Start buffer updates. */
2528 BYTE *pAddress = NULL;
2529 ULONG ulWidth = 0;
2530 ULONG ulHeight = 0;
2531 ULONG ulBitsPerPixel = 0;
2532 ULONG ulBytesPerLine = 0;
2533 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2534
2535 pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2536 &ulWidth,
2537 &ulHeight,
2538 &ulBitsPerPixel,
2539 &ulBytesPerLine,
2540 &bitmapFormat);
2541
2542 mpDrv->IConnector.pbData = pAddress;
2543 mpDrv->IConnector.cbScanline = ulBytesPerLine;
2544 mpDrv->IConnector.cBits = ulBitsPerPixel;
2545 mpDrv->IConnector.cx = ulWidth;
2546 mpDrv->IConnector.cy = ulHeight;
2547
2548 fSetRenderVRAM = pFBInfo->fDefaultFormat;
2549 }
2550
2551 /* Make sure that the bitmap contains the latest image. */
2552 fInvalidate = pFBInfo->fDefaultFormat;
2553 }
2554 }
2555
2556 if (SUCCEEDED(hr))
2557 {
2558 pFBInfo->pSourceBitmap.queryInterfaceTo(aDisplaySourceBitmap.asOutParam());
2559 }
2560
2561 /* Leave the IDisplay lock because the VGA device must not be called under it. */
2562 alock.release();
2563
2564 if (SUCCEEDED(hr))
2565 {
2566 if (fSetRenderVRAM)
2567 {
2568 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, true);
2569 }
2570
2571 if (fInvalidate)
2572 VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2573 3, this, aScreenId, false);
2574 }
2575
2576 LogRelFlowFunc(("%Rhrc\n", hr));
2577 return hr;
2578}
2579
2580HRESULT Display::getGuestScreenLayout(std::vector<ComPtr<IGuestScreenInfo> > &aGuestScreenLayout)
2581{
2582 NOREF(aGuestScreenLayout);
2583 return E_NOTIMPL;
2584}
2585
2586HRESULT Display::setScreenLayout(ScreenLayoutMode_T aScreenLayoutMode,
2587 const std::vector<ComPtr<IGuestScreenInfo> > &aGuestScreenInfo)
2588{
2589 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2590
2591 if (aGuestScreenInfo.size() != mcMonitors)
2592 return E_INVALIDARG;
2593
2594 CHECK_CONSOLE_DRV(mpDrv);
2595
2596 /*
2597 * It is up to the guest to decide whether the hint is
2598 * valid. Therefore don't do any VRAM sanity checks here.
2599 */
2600
2601 /* Have to release the lock because the pfnRequestDisplayChange
2602 * will call EMT. */
2603 alock.release();
2604
2605 VMMDev *pVMMDev = mParent->i_getVMMDev();
2606 if (pVMMDev)
2607 {
2608 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2609 if (pVMMDevPort)
2610 {
2611 uint32_t const cDisplays = (uint32_t)aGuestScreenInfo.size();
2612
2613 size_t const cbAlloc = cDisplays * sizeof(VMMDevDisplayDef);
2614 VMMDevDisplayDef *paDisplayDefs = (VMMDevDisplayDef *)RTMemAlloc(cbAlloc);
2615 if (paDisplayDefs)
2616 {
2617 for (uint32_t i = 0; i < cDisplays; ++i)
2618 {
2619 VMMDevDisplayDef *p = &paDisplayDefs[i];
2620 ComPtr<IGuestScreenInfo> pScreenInfo = aGuestScreenInfo[i];
2621
2622 ULONG screenId = 0;
2623 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
2624 BOOL origin = FALSE;
2625 BOOL primary = FALSE;
2626 LONG originX = 0;
2627 LONG originY = 0;
2628 ULONG width = 0;
2629 ULONG height = 0;
2630 ULONG bitsPerPixel = 0;
2631
2632 pScreenInfo->COMGETTER(ScreenId) (&screenId);
2633 pScreenInfo->COMGETTER(GuestMonitorStatus)(&guestMonitorStatus);
2634 pScreenInfo->COMGETTER(Primary) (&primary);
2635 pScreenInfo->COMGETTER(Origin) (&origin);
2636 pScreenInfo->COMGETTER(OriginX) (&originX);
2637 pScreenInfo->COMGETTER(OriginY) (&originY);
2638 pScreenInfo->COMGETTER(Width) (&width);
2639 pScreenInfo->COMGETTER(Height) (&height);
2640 pScreenInfo->COMGETTER(BitsPerPixel)(&bitsPerPixel);
2641
2642 LogFlowFunc(("%d %d,%d %dx%d\n", screenId, originX, originY, width, height));
2643
2644 p->idDisplay = screenId;
2645 p->xOrigin = originX;
2646 p->yOrigin = originY;
2647 p->cx = width;
2648 p->cy = height;
2649 p->cBitsPerPixel = bitsPerPixel;
2650 p->fDisplayFlags = VMMDEV_DISPLAY_CX | VMMDEV_DISPLAY_CY | VMMDEV_DISPLAY_BPP;
2651 if (guestMonitorStatus == GuestMonitorStatus_Disabled)
2652 p->fDisplayFlags |= VMMDEV_DISPLAY_DISABLED;
2653 if (origin)
2654 p->fDisplayFlags |= VMMDEV_DISPLAY_ORIGIN;
2655 if (primary)
2656 p->fDisplayFlags |= VMMDEV_DISPLAY_PRIMARY;
2657 }
2658
2659 bool const fForce = aScreenLayoutMode == ScreenLayoutMode_Reset
2660 || aScreenLayoutMode == ScreenLayoutMode_Apply;
2661 bool const fNotify = aScreenLayoutMode != ScreenLayoutMode_Silent;
2662 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, cDisplays, paDisplayDefs, fForce, fNotify);
2663
2664 RTMemFree(paDisplayDefs);
2665 }
2666 }
2667 }
2668 return S_OK;
2669}
2670
2671HRESULT Display::detachScreens(const std::vector<LONG> &aScreenIds)
2672{
2673 NOREF(aScreenIds);
2674 return E_NOTIMPL;
2675}
2676
2677HRESULT Display::createGuestScreenInfo(ULONG aDisplay,
2678 GuestMonitorStatus_T aStatus,
2679 BOOL aPrimary,
2680 BOOL aChangeOrigin,
2681 LONG aOriginX,
2682 LONG aOriginY,
2683 ULONG aWidth,
2684 ULONG aHeight,
2685 ULONG aBitsPerPixel,
2686 ComPtr<IGuestScreenInfo> &aGuestScreenInfo)
2687{
2688 /* Create a new object. */
2689 ComObjPtr<GuestScreenInfo> obj;
2690 HRESULT hr = obj.createObject();
2691 if (SUCCEEDED(hr))
2692 hr = obj->init(aDisplay, aStatus, aPrimary, aChangeOrigin, aOriginX, aOriginY,
2693 aWidth, aHeight, aBitsPerPixel);
2694 if (SUCCEEDED(hr))
2695 obj.queryInterfaceTo(aGuestScreenInfo.asOutParam());
2696
2697 return hr;
2698}
2699
2700
2701/*
2702 * GuestScreenInfo implementation.
2703 */
2704DEFINE_EMPTY_CTOR_DTOR(GuestScreenInfo)
2705
2706HRESULT GuestScreenInfo::FinalConstruct()
2707{
2708 return BaseFinalConstruct();
2709}
2710
2711void GuestScreenInfo::FinalRelease()
2712{
2713 uninit();
2714
2715 BaseFinalRelease();
2716}
2717
2718HRESULT GuestScreenInfo::init(ULONG aDisplay,
2719 GuestMonitorStatus_T aGuestMonitorStatus,
2720 BOOL aPrimary,
2721 BOOL aChangeOrigin,
2722 LONG aOriginX,
2723 LONG aOriginY,
2724 ULONG aWidth,
2725 ULONG aHeight,
2726 ULONG aBitsPerPixel)
2727{
2728 LogFlowThisFunc(("[%u]\n", aDisplay));
2729
2730 /* Enclose the state transition NotReady->InInit->Ready */
2731 AutoInitSpan autoInitSpan(this);
2732 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2733
2734 mScreenId = aDisplay;
2735 mGuestMonitorStatus = aGuestMonitorStatus;
2736 mPrimary = aPrimary;
2737 mOrigin = aChangeOrigin;
2738 mOriginX = aOriginX;
2739 mOriginY = aOriginY;
2740 mWidth = aWidth;
2741 mHeight = aHeight;
2742 mBitsPerPixel = aBitsPerPixel;
2743
2744 /* Confirm a successful initialization */
2745 autoInitSpan.setSucceeded();
2746
2747 return S_OK;
2748}
2749
2750void GuestScreenInfo::uninit()
2751{
2752 /* Enclose the state transition Ready->InUninit->NotReady */
2753 AutoUninitSpan autoUninitSpan(this);
2754 if (autoUninitSpan.uninitDone())
2755 return;
2756
2757 LogFlowThisFunc(("[%u]\n", mScreenId));
2758}
2759
2760HRESULT GuestScreenInfo::getScreenId(ULONG *aScreenId)
2761{
2762 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2763 *aScreenId = mScreenId;
2764 return S_OK;
2765}
2766
2767HRESULT GuestScreenInfo::getGuestMonitorStatus(GuestMonitorStatus_T *aGuestMonitorStatus)
2768{
2769 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2770 *aGuestMonitorStatus = mGuestMonitorStatus;
2771 return S_OK;
2772}
2773
2774HRESULT GuestScreenInfo::getPrimary(BOOL *aPrimary)
2775{
2776 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2777 *aPrimary = mPrimary;
2778 return S_OK;
2779}
2780
2781HRESULT GuestScreenInfo::getOrigin(BOOL *aOrigin)
2782{
2783 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2784 *aOrigin = mOrigin;
2785 return S_OK;
2786}
2787
2788HRESULT GuestScreenInfo::getOriginX(LONG *aOriginX)
2789{
2790 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2791 *aOriginX = mOriginX;
2792 return S_OK;
2793}
2794
2795HRESULT GuestScreenInfo::getOriginY(LONG *aOriginY)
2796{
2797 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2798 *aOriginY = mOriginY;
2799 return S_OK;
2800}
2801
2802HRESULT GuestScreenInfo::getWidth(ULONG *aWidth)
2803{
2804 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2805 *aWidth = mWidth;
2806 return S_OK;
2807}
2808
2809HRESULT GuestScreenInfo::getHeight(ULONG *aHeight)
2810{
2811 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2812 *aHeight = mHeight;
2813 return S_OK;
2814}
2815
2816HRESULT GuestScreenInfo::getBitsPerPixel(ULONG *aBitsPerPixel)
2817{
2818 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2819 *aBitsPerPixel = mBitsPerPixel;
2820 return S_OK;
2821}
2822
2823HRESULT GuestScreenInfo::getExtendedInfo(com::Utf8Str &aExtendedInfo)
2824{
2825 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2826 aExtendedInfo = com::Utf8Str();
2827 return S_OK;
2828}
2829
2830// wrapped IEventListener method
2831HRESULT Display::handleEvent(const ComPtr<IEvent> &aEvent)
2832{
2833 VBoxEventType_T aType = VBoxEventType_Invalid;
2834
2835 aEvent->COMGETTER(Type)(&aType);
2836 switch (aType)
2837 {
2838 case VBoxEventType_OnStateChanged:
2839 {
2840 ComPtr<IStateChangedEvent> scev = aEvent;
2841 Assert(scev);
2842 MachineState_T machineState;
2843 scev->COMGETTER(State)(&machineState);
2844 if ( machineState == MachineState_Running
2845 || machineState == MachineState_Teleporting
2846 || machineState == MachineState_LiveSnapshotting
2847 || machineState == MachineState_DeletingSnapshotOnline
2848 )
2849 {
2850 LogRelFlowFunc(("Machine is running.\n"));
2851
2852 }
2853 break;
2854 }
2855 default:
2856 AssertFailed();
2857 }
2858
2859 return S_OK;
2860}
2861
2862
2863// private methods
2864/////////////////////////////////////////////////////////////////////////////
2865
2866/**
2867 * Handle display resize event issued by the VGA device for the primary screen.
2868 *
2869 * @see PDMIDISPLAYCONNECTOR::pfnResize
2870 */
2871DECLCALLBACK(int) Display::i_displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
2872 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
2873{
2874 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2875 Display *pThis = pDrv->pDisplay;
2876
2877 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
2878 bpp, pvVRAM, cbLine, cx, cy));
2879
2880 bool f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, true, false);
2881 if (!f)
2882 {
2883 /* This is a result of recursive call when the source bitmap is being updated
2884 * during a VGA resize. Tell the VGA device to ignore the call.
2885 *
2886 * @todo It is a workaround, actually pfnUpdateDisplayAll must
2887 * fail on resize.
2888 */
2889 LogRel(("displayResizeCallback: already processing\n"));
2890 return VINF_VGA_RESIZE_IN_PROGRESS;
2891 }
2892
2893 int rc = pThis->i_handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, 0, 0, 0, true);
2894
2895 /* Restore the flag. */
2896 f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, false, true);
2897 AssertRelease(f);
2898
2899 return rc;
2900}
2901
2902/**
2903 * Handle display update.
2904 *
2905 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
2906 */
2907DECLCALLBACK(void) Display::i_displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
2908 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
2909{
2910 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2911
2912#ifdef DEBUG_sunlover
2913 LogFlowFunc(("fVideoAccelEnabled = %d, %d,%d %dx%d\n",
2914 pDrv->pDisplay->mVideoAccelLegacy.fVideoAccelEnabled, x, y, cx, cy));
2915#endif /* DEBUG_sunlover */
2916
2917 /* This call does update regardless of VBVA status.
2918 * But in VBVA mode this is called only as result of
2919 * pfnUpdateDisplayAll in the VGA device.
2920 */
2921
2922 pDrv->pDisplay->i_handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
2923}
2924
2925/**
2926 * Periodic display refresh callback.
2927 *
2928 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
2929 * @thread EMT
2930 */
2931/*static*/ DECLCALLBACK(void) Display::i_displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
2932{
2933 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2934
2935#ifdef DEBUG_sunlover_2
2936 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
2937 pDrv->pDisplay->mfVideoAccelEnabled));
2938#endif /* DEBUG_sunlover_2 */
2939
2940 Display *pDisplay = pDrv->pDisplay;
2941 unsigned uScreenId;
2942
2943 int rc = pDisplay->i_videoAccelRefreshProcess(pDrv->pUpPort);
2944 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
2945 {
2946 if (rc == VWRN_INVALID_STATE)
2947 {
2948 /* No VBVA do a display update. */
2949 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
2950 }
2951
2952 /* Inform the VRDP server that the current display update sequence is
2953 * completed. At this moment the framebuffer memory contains a definite
2954 * image, that is synchronized with the orders already sent to VRDP client.
2955 * The server can now process redraw requests from clients or initial
2956 * fullscreen updates for new clients.
2957 */
2958 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2959 {
2960 Assert(pDisplay->mParent && pDisplay->mParent->i_consoleVRDPServer());
2961 pDisplay->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, NULL, 0);
2962 }
2963 }
2964
2965#ifdef VBOX_WITH_RECORDING
2966 AssertPtr(pDisplay->mParent);
2967 RecordingContext *pCtx = pDisplay->mParent->i_recordingGetContext();
2968
2969 if ( pCtx
2970 && pCtx->IsStarted()
2971 && pCtx->IsFeatureEnabled(RecordingFeature_Video))
2972 {
2973 do
2974 {
2975 /* If the recording context has reached the configured recording
2976 * limit, disable recording. */
2977 if (pCtx->IsLimitReached())
2978 {
2979 pDisplay->mParent->i_onRecordingChange(FALSE /* Disable */);
2980 break;
2981 }
2982
2983 uint64_t tsNowMs = RTTimeProgramMilliTS();
2984 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2985 {
2986 if (!pDisplay->maRecordingEnabled[uScreenId])
2987 continue;
2988
2989 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2990 if (!pFBInfo->fDisabled)
2991 {
2992 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
2993 int rc2 = RTCritSectEnter(&pDisplay->mVideoRecLock);
2994 if (RT_SUCCESS(rc2))
2995 {
2996 pSourceBitmap = pFBInfo->Recording.pSourceBitmap;
2997 RTCritSectLeave(&pDisplay->mVideoRecLock);
2998 }
2999
3000 if (!pSourceBitmap.isNull())
3001 {
3002 BYTE *pbAddress = NULL;
3003 ULONG ulWidth = 0;
3004 ULONG ulHeight = 0;
3005 ULONG ulBitsPerPixel = 0;
3006 ULONG ulBytesPerLine = 0;
3007 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
3008 HRESULT hr = pSourceBitmap->QueryBitmapInfo(&pbAddress,
3009 &ulWidth,
3010 &ulHeight,
3011 &ulBitsPerPixel,
3012 &ulBytesPerLine,
3013 &bitmapFormat);
3014 if (SUCCEEDED(hr) && pbAddress)
3015 rc = pCtx->SendVideoFrame(uScreenId, 0, 0, BitmapFormat_BGR,
3016 ulBitsPerPixel, ulBytesPerLine, ulWidth, ulHeight,
3017 pbAddress, tsNowMs);
3018 else
3019 rc = VERR_NOT_SUPPORTED;
3020
3021 pSourceBitmap.setNull();
3022 }
3023 else
3024 rc = VERR_NOT_SUPPORTED;
3025
3026 if (rc == VINF_TRY_AGAIN)
3027 break;
3028 }
3029 }
3030 } while (0);
3031 }
3032#endif /* VBOX_WITH_RECORDING */
3033
3034#ifdef DEBUG_sunlover_2
3035 LogFlowFunc(("leave\n"));
3036#endif /* DEBUG_sunlover_2 */
3037}
3038
3039/**
3040 * Reset notification
3041 *
3042 * @see PDMIDISPLAYCONNECTOR::pfnReset
3043 */
3044DECLCALLBACK(void) Display::i_displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3045{
3046 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3047
3048 LogRelFlowFunc(("\n"));
3049
3050 /* Disable VBVA mode. */
3051 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3052}
3053
3054/**
3055 * LFBModeChange notification
3056 *
3057 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3058 */
3059DECLCALLBACK(void) Display::i_displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3060{
3061 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3062
3063 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
3064
3065 NOREF(fEnabled);
3066
3067 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3068 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3069}
3070
3071/**
3072 * Adapter information change notification.
3073 *
3074 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3075 */
3076DECLCALLBACK(void) Display::i_displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM,
3077 uint32_t u32VRAMSize)
3078{
3079 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3080 pDrv->pDisplay->processAdapterData(pvVRAM, u32VRAMSize);
3081}
3082
3083/**
3084 * Display information change notification.
3085 *
3086 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3087 */
3088DECLCALLBACK(void) Display::i_displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface,
3089 void *pvVRAM, unsigned uScreenId)
3090{
3091 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3092 pDrv->pDisplay->processDisplayData(pvVRAM, uScreenId);
3093}
3094
3095#ifdef VBOX_WITH_VIDEOHWACCEL
3096
3097int Display::i_handleVHWACommandProcess(int enmCmd, bool fGuestCmd, VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand)
3098{
3099 unsigned id = (unsigned)pCommand->iDisplay;
3100 if (id >= mcMonitors)
3101 return VERR_INVALID_PARAMETER;
3102
3103 ComPtr<IFramebuffer> pFramebuffer;
3104 AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
3105 pFramebuffer = maFramebuffers[id].pFramebuffer;
3106 bool fVHWASupported = RT_BOOL(maFramebuffers[id].u32Caps & FramebufferCapabilities_VHWA);
3107 arlock.release();
3108
3109 if (pFramebuffer == NULL || !fVHWASupported)
3110 return VERR_NOT_IMPLEMENTED; /* Implementation is not available. */
3111
3112 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE *)pCommand, enmCmd, fGuestCmd);
3113 if (hr == S_FALSE)
3114 return VINF_SUCCESS;
3115 if (SUCCEEDED(hr))
3116 return VINF_CALLBACK_RETURN;
3117 if (hr == E_ACCESSDENIED)
3118 return VERR_INVALID_STATE; /* notify we can not handle request atm */
3119 if (hr == E_NOTIMPL)
3120 return VERR_NOT_IMPLEMENTED;
3121 return VERR_GENERAL_FAILURE;
3122}
3123
3124DECLCALLBACK(int) Display::i_displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, int enmCmd, bool fGuestCmd,
3125 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand)
3126{
3127 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3128
3129 return pDrv->pDisplay->i_handleVHWACommandProcess(enmCmd, fGuestCmd, pCommand);
3130}
3131
3132#endif /* VBOX_WITH_VIDEOHWACCEL */
3133
3134HRESULT Display::notifyScaleFactorChange(ULONG aScreenId, ULONG aScaleFactorWMultiplied, ULONG aScaleFactorHMultiplied)
3135{
3136 RT_NOREF(aScreenId, aScaleFactorWMultiplied, aScaleFactorHMultiplied);
3137# if 0 /** @todo Thank you so very much from anyone using VMSVGA3d! */
3138 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while 3D acceleration is disabled in VM config. Ignored.\n"));
3139# else
3140 /* Need an interface like this here (and the #ifdefs needs adjusting):
3141 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3142 if (pUpPort && pUpPort->pfnSetScaleFactor)
3143 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3144# endif
3145 return S_OK;
3146}
3147
3148HRESULT Display::notifyHiDPIOutputPolicyChange(BOOL fUnscaledHiDPI)
3149{
3150 RT_NOREF(fUnscaledHiDPI);
3151
3152 /* Need an interface like this here (and the #ifdefs needs adjusting):
3153 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3154 if (pUpPort && pUpPort->pfnSetScaleFactor)
3155 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3156
3157 return S_OK;
3158}
3159
3160#ifdef VBOX_WITH_HGSMI
3161/**
3162 * @interface_method_impl{PDMIDISPLAYCONNECTOR,pfnVBVAEnable}
3163 */
3164DECLCALLBACK(int) Display::i_displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
3165 VBVAHOSTFLAGS RT_UNTRUSTED_VOLATILE_GUEST *pHostFlags)
3166{
3167 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3168
3169 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3170 Display *pThis = pDrv->pDisplay;
3171
3172 if (pThis->maFramebuffers[uScreenId].fVBVAEnabled)
3173 {
3174 LogRel(("Enabling different vbva mode\n"));
3175#ifdef DEBUG_misha
3176 AssertMsgFailed(("enabling different vbva mode\n"));
3177#endif
3178 return VERR_INVALID_STATE;
3179 }
3180
3181 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3182 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3183 pThis->maFramebuffers[uScreenId].fVBVAForceResize = true;
3184
3185 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3186
3187 return VINF_SUCCESS;
3188}
3189
3190/**
3191 * @interface_method_impl{PDMIDISPLAYCONNECTOR,pfnVBVADisable}
3192 */
3193DECLCALLBACK(void) Display::i_displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3194{
3195 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3196
3197 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3198 Display *pThis = pDrv->pDisplay;
3199
3200 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3201
3202 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3203 {
3204 /* Make sure that the primary screen is visible now.
3205 * The guest can't use VBVA anymore, so only only the VGA device output works.
3206 */
3207 pFBInfo->flags = 0;
3208 if (pFBInfo->fDisabled)
3209 {
3210 pFBInfo->fDisabled = false;
3211 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3212 GuestMonitorChangedEventType_Enabled,
3213 uScreenId,
3214 pFBInfo->xOrigin, pFBInfo->yOrigin,
3215 pFBInfo->w, pFBInfo->h);
3216 }
3217 }
3218
3219 pFBInfo->fVBVAEnabled = false;
3220 pFBInfo->fVBVAForceResize = false;
3221
3222 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3223
3224 pFBInfo->pVBVAHostFlags = NULL;
3225
3226 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3227 {
3228 /* Force full screen update, because VGA device must take control, do resize, etc. */
3229 pThis->mpDrv->pUpPort->pfnUpdateDisplayAll(pThis->mpDrv->pUpPort, /* fFailOnResize = */ false);
3230 }
3231}
3232
3233DECLCALLBACK(void) Display::i_displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3234{
3235 RT_NOREF(uScreenId);
3236 LogFlowFunc(("uScreenId %d\n", uScreenId));
3237
3238 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3239 Display *pThis = pDrv->pDisplay;
3240
3241 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3242 {
3243 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers,
3244 pThis->mcMonitors);
3245 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3246 }
3247}
3248
3249/**
3250 * @interface_method_impl{PDMIDISPLAYCONNECTOR,pfnVBVAUpdateProcess}
3251 */
3252DECLCALLBACK(void) Display::i_displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
3253 struct VBVACMDHDR const RT_UNTRUSTED_VOLATILE_GUEST *pCmd, size_t cbCmd)
3254{
3255 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3256 VBVACMDHDR hdrSaved;
3257 RT_COPY_VOLATILE(hdrSaved, *pCmd);
3258 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
3259
3260 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3261 Display *pThis = pDrv->pDisplay;
3262 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3263
3264 if (pFBInfo->fDefaultFormat)
3265 {
3266 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3267 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3268 && !pFBInfo->fDisabled)
3269 {
3270 pDrv->pUpPort->pfnUpdateDisplayRect(pDrv->pUpPort, hdrSaved.x, hdrSaved.y, hdrSaved.w, hdrSaved.h);
3271 }
3272 else if ( !pFBInfo->pSourceBitmap.isNull()
3273 && !pFBInfo->fDisabled)
3274 {
3275 /* Render VRAM content to the framebuffer. */
3276 BYTE *pAddress = NULL;
3277 ULONG ulWidth = 0;
3278 ULONG ulHeight = 0;
3279 ULONG ulBitsPerPixel = 0;
3280 ULONG ulBytesPerLine = 0;
3281 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
3282
3283 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
3284 &ulWidth,
3285 &ulHeight,
3286 &ulBitsPerPixel,
3287 &ulBytesPerLine,
3288 &bitmapFormat);
3289 if (SUCCEEDED(hrc))
3290 {
3291 uint32_t width = hdrSaved.w;
3292 uint32_t height = hdrSaved.h;
3293
3294 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3295 int32_t xSrc = hdrSaved.x - pFBInfo->xOrigin;
3296 int32_t ySrc = hdrSaved.y - pFBInfo->yOrigin;
3297 uint32_t u32SrcWidth = pFBInfo->w;
3298 uint32_t u32SrcHeight = pFBInfo->h;
3299 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3300 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3301
3302 uint8_t *pu8Dst = pAddress;
3303 int32_t xDst = xSrc;
3304 int32_t yDst = ySrc;
3305 uint32_t u32DstWidth = u32SrcWidth;
3306 uint32_t u32DstHeight = u32SrcHeight;
3307 uint32_t u32DstLineSize = u32DstWidth * 4;
3308 uint32_t u32DstBitsPerPixel = 32;
3309
3310 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3311 width, height,
3312 pu8Src,
3313 xSrc, ySrc,
3314 u32SrcWidth, u32SrcHeight,
3315 u32SrcLineSize, u32SrcBitsPerPixel,
3316 pu8Dst,
3317 xDst, yDst,
3318 u32DstWidth, u32DstHeight,
3319 u32DstLineSize, u32DstBitsPerPixel);
3320 }
3321 }
3322 }
3323
3324 /*
3325 * Here is your classic 'temporary' solution.
3326 */
3327 /** @todo New SendUpdate entry which can get a separate cmd header or coords. */
3328 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3329
3330 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3331 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3332
3333 pThis->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, pHdrUnconst, (uint32_t)cbCmd);
3334
3335 *pHdrUnconst = hdrSaved;
3336}
3337
3338DECLCALLBACK(void) Display::i_displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y,
3339 uint32_t cx, uint32_t cy)
3340{
3341 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3342
3343 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3344 Display *pThis = pDrv->pDisplay;
3345 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3346
3347 /** @todo handleFramebufferUpdate (uScreenId,
3348 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3349 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3350 * cx, cy);
3351 */
3352 pThis->i_handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3353}
3354
3355#ifdef DEBUG_sunlover
3356static void logVBVAResize(PCVBVAINFOVIEW pView, PCVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
3357{
3358 LogRel(("displayVBVAResize: [%d] %s\n"
3359 " pView->u32ViewIndex %d\n"
3360 " pView->u32ViewOffset 0x%08X\n"
3361 " pView->u32ViewSize 0x%08X\n"
3362 " pView->u32MaxScreenSize 0x%08X\n"
3363 " pScreen->i32OriginX %d\n"
3364 " pScreen->i32OriginY %d\n"
3365 " pScreen->u32StartOffset 0x%08X\n"
3366 " pScreen->u32LineSize 0x%08X\n"
3367 " pScreen->u32Width %d\n"
3368 " pScreen->u32Height %d\n"
3369 " pScreen->u16BitsPerPixel %d\n"
3370 " pScreen->u16Flags 0x%04X\n"
3371 " pFBInfo->u32Offset 0x%08X\n"
3372 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
3373 " pFBInfo->u32InformationSize 0x%08X\n"
3374 " pFBInfo->fDisabled %d\n"
3375 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
3376 " pFBInfo->u16BitsPerPixel %d\n"
3377 " pFBInfo->pu8FramebufferVRAM %p\n"
3378 " pFBInfo->u32LineSize 0x%08X\n"
3379 " pFBInfo->flags 0x%04X\n"
3380 " pFBInfo->pHostEvents %p\n"
3381 " pFBInfo->fDefaultFormat %d\n"
3382 " pFBInfo->fVBVAEnabled %d\n"
3383 " pFBInfo->fVBVAForceResize %d\n"
3384 " pFBInfo->pVBVAHostFlags %p\n"
3385 "",
3386 pScreen->u32ViewIndex,
3387 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
3388 pView->u32ViewIndex,
3389 pView->u32ViewOffset,
3390 pView->u32ViewSize,
3391 pView->u32MaxScreenSize,
3392 pScreen->i32OriginX,
3393 pScreen->i32OriginY,
3394 pScreen->u32StartOffset,
3395 pScreen->u32LineSize,
3396 pScreen->u32Width,
3397 pScreen->u32Height,
3398 pScreen->u16BitsPerPixel,
3399 pScreen->u16Flags,
3400 pFBInfo->u32Offset,
3401 pFBInfo->u32MaxFramebufferSize,
3402 pFBInfo->u32InformationSize,
3403 pFBInfo->fDisabled,
3404 pFBInfo->xOrigin,
3405 pFBInfo->yOrigin,
3406 pFBInfo->w,
3407 pFBInfo->h,
3408 pFBInfo->u16BitsPerPixel,
3409 pFBInfo->pu8FramebufferVRAM,
3410 pFBInfo->u32LineSize,
3411 pFBInfo->flags,
3412 pFBInfo->pHostEvents,
3413 pFBInfo->fDefaultFormat,
3414 pFBInfo->fVBVAEnabled,
3415 pFBInfo->fVBVAForceResize,
3416 pFBInfo->pVBVAHostFlags
3417 ));
3418}
3419#endif /* DEBUG_sunlover */
3420
3421DECLCALLBACK(int) Display::i_displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, PCVBVAINFOVIEW pView,
3422 PCVBVAINFOSCREEN pScreen, void *pvVRAM, bool fResetInputMapping)
3423{
3424 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3425
3426 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3427 Display *pThis = pDrv->pDisplay;
3428
3429 return pThis->processVBVAResize(pView, pScreen, pvVRAM, fResetInputMapping);
3430}
3431
3432int Display::processVBVAResize(PCVBVAINFOVIEW pView, PCVBVAINFOSCREEN pScreen, void *pvVRAM, bool fResetInputMapping)
3433{
3434 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3435
3436 RT_NOREF(pView);
3437
3438 DISPLAYFBINFO *pFBInfo = &maFramebuffers[pScreen->u32ViewIndex];
3439
3440#ifdef DEBUG_sunlover
3441 logVBVAResize(pView, pScreen, pFBInfo);
3442#endif
3443
3444 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
3445 {
3446 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
3447 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
3448 * the VM window will be black. */
3449 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
3450 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
3451 int32_t xOrigin = pFBInfo->xOrigin;
3452 int32_t yOrigin = pFBInfo->yOrigin;
3453
3454 alock.release();
3455
3456 i_handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
3457 u32Width, u32Height, pScreen->u16Flags, xOrigin, yOrigin, false);
3458
3459 return VINF_SUCCESS;
3460 }
3461
3462 VBVAINFOSCREEN screenInfo;
3463 RT_ZERO(screenInfo);
3464
3465 if (pScreen->u16Flags & VBVA_SCREEN_F_BLANK2)
3466 {
3467 /* Init a local VBVAINFOSCREEN structure, which will be used instead of
3468 * the original pScreen. Set VBVA_SCREEN_F_BLANK, which will force
3469 * the code below to choose the "blanking" branches.
3470 */
3471 screenInfo.u32ViewIndex = pScreen->u32ViewIndex;
3472 screenInfo.i32OriginX = pFBInfo->xOrigin;
3473 screenInfo.i32OriginY = pFBInfo->yOrigin;
3474 screenInfo.u32StartOffset = 0; /* Irrelevant */
3475 screenInfo.u32LineSize = pFBInfo->u32LineSize;
3476 screenInfo.u32Width = pFBInfo->w;
3477 screenInfo.u32Height = pFBInfo->h;
3478 screenInfo.u16BitsPerPixel = pFBInfo->u16BitsPerPixel;
3479 screenInfo.u16Flags = pScreen->u16Flags | VBVA_SCREEN_F_BLANK;
3480
3481 pScreen = &screenInfo;
3482 }
3483
3484 if (fResetInputMapping)
3485 {
3486 /// @todo Rename to m* and verify whether some kind of lock is required.
3487 xInputMappingOrigin = 0;
3488 yInputMappingOrigin = 0;
3489 cxInputMapping = 0;
3490 cyInputMapping = 0;
3491 }
3492
3493 alock.release();
3494
3495 return i_handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
3496 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
3497 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags,
3498 pScreen->i32OriginX, pScreen->i32OriginY, false);
3499}
3500
3501DECLCALLBACK(int) Display::i_displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
3502 uint32_t xHot, uint32_t yHot,
3503 uint32_t cx, uint32_t cy,
3504 const void *pvShape)
3505{
3506 LogFlowFunc(("\n"));
3507 LogRel2(("%s: fVisible=%RTbool\n", __PRETTY_FUNCTION__, fVisible));
3508
3509 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3510
3511 uint32_t cbShape = 0;
3512 if (pvShape)
3513 {
3514 cbShape = (cx + 7) / 8 * cy; /* size of the AND mask */
3515 cbShape = ((cbShape + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
3516 }
3517
3518 /* Tell the console about it */
3519 pDrv->pDisplay->mParent->i_onMousePointerShapeChange(fVisible, fAlpha,
3520 xHot, yHot, cx, cy, (uint8_t *)pvShape, cbShape);
3521
3522 return VINF_SUCCESS;
3523}
3524
3525DECLCALLBACK(void) Display::i_displayVBVAGuestCapabilityUpdate(PPDMIDISPLAYCONNECTOR pInterface, uint32_t fCapabilities)
3526{
3527 LogFlowFunc(("\n"));
3528
3529 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3530 Display *pThis = pDrv->pDisplay;
3531
3532 pThis->i_handleUpdateGuestVBVACapabilities(fCapabilities);
3533}
3534
3535DECLCALLBACK(void) Display::i_displayVBVAInputMappingUpdate(PPDMIDISPLAYCONNECTOR pInterface, int32_t xOrigin, int32_t yOrigin,
3536 uint32_t cx, uint32_t cy)
3537{
3538 LogFlowFunc(("\n"));
3539
3540 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3541 Display *pThis = pDrv->pDisplay;
3542
3543 pThis->i_handleUpdateVBVAInputMapping(xOrigin, yOrigin, cx, cy);
3544}
3545
3546DECLCALLBACK(void) Display::i_displayVBVAReportCursorPosition(PPDMIDISPLAYCONNECTOR pInterface, uint32_t fFlags, uint32_t aScreenId, uint32_t x, uint32_t y)
3547{
3548 LogFlowFunc(("\n"));
3549 LogRel2(("%s: fFlags=%RU32, aScreenId=%RU32, x=%RU32, y=%RU32\n",
3550 __PRETTY_FUNCTION__, fFlags, aScreenId, x, y));
3551
3552 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3553 Display *pThis = pDrv->pDisplay;
3554
3555 if (fFlags & VBVA_CURSOR_SCREEN_RELATIVE)
3556 {
3557 x += pThis->maFramebuffers[aScreenId].xOrigin;
3558 y += pThis->maFramebuffers[aScreenId].yOrigin;
3559 }
3560 fireCursorPositionChangedEvent(pThis->mParent->i_getEventSource(), RT_BOOL(fFlags & VBVA_CURSOR_VALID_DATA), x, y);
3561}
3562
3563#endif /* VBOX_WITH_HGSMI */
3564
3565/**
3566 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3567 */
3568DECLCALLBACK(void *) Display::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3569{
3570 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
3571 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3572 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
3573 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
3574 return NULL;
3575}
3576
3577
3578/**
3579 * @interface_method_impl{PDMDRVREG,pfnPowerOff,
3580 * Tries to ensure no client calls gets to HGCM or the VGA device from here on.}
3581 */
3582DECLCALLBACK(void) Display::i_drvPowerOff(PPDMDRVINS pDrvIns)
3583{
3584 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3585 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
3586
3587 /*
3588 * Do much of the work that i_drvDestruct does.
3589 */
3590 if (pThis->pUpPort)
3591 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
3592
3593 pThis->IConnector.pbData = NULL;
3594 pThis->IConnector.cbScanline = 0;
3595 pThis->IConnector.cBits = 32;
3596 pThis->IConnector.cx = 0;
3597 pThis->IConnector.cy = 0;
3598
3599 if (pThis->pDisplay)
3600 {
3601 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
3602#ifdef VBOX_WITH_RECORDING
3603 pThis->pDisplay->mParent->i_recordingStop();
3604#endif
3605#if defined(VBOX_WITH_VIDEOHWACCEL)
3606 pThis->pVBVACallbacks = NULL;
3607#endif
3608 }
3609}
3610
3611
3612/**
3613 * Destruct a display driver instance.
3614 *
3615 * @returns VBox status code.
3616 * @param pDrvIns The driver instance data.
3617 */
3618DECLCALLBACK(void) Display::i_drvDestruct(PPDMDRVINS pDrvIns)
3619{
3620 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
3621 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3622 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
3623
3624 /*
3625 * We repeat much of what i_drvPowerOff does in case it wasn't called.
3626 * In addition we sever the connection between us and the display.
3627 */
3628 if (pThis->pUpPort)
3629 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
3630
3631 pThis->IConnector.pbData = NULL;
3632 pThis->IConnector.cbScanline = 0;
3633 pThis->IConnector.cBits = 32;
3634 pThis->IConnector.cx = 0;
3635 pThis->IConnector.cy = 0;
3636
3637 if (pThis->pDisplay)
3638 {
3639 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
3640#ifdef VBOX_WITH_RECORDING
3641 pThis->pDisplay->mParent->i_recordingStop();
3642#endif
3643#if defined(VBOX_WITH_VIDEOHWACCEL)
3644 pThis->pVBVACallbacks = NULL;
3645#endif
3646
3647 pThis->pDisplay->mpDrv = NULL;
3648 pThis->pDisplay = NULL;
3649 }
3650#if defined(VBOX_WITH_VIDEOHWACCEL)
3651 pThis->pVBVACallbacks = NULL;
3652#endif
3653}
3654
3655
3656/**
3657 * Construct a display driver instance.
3658 *
3659 * @copydoc FNPDMDRVCONSTRUCT
3660 */
3661DECLCALLBACK(int) Display::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
3662{
3663 RT_NOREF(fFlags);
3664 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
3665 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3666 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
3667
3668 /*
3669 * Validate configuration.
3670 */
3671 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
3672 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
3673 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
3674 ("Configuration error: Not possible to attach anything to this driver!\n"),
3675 VERR_PDM_DRVINS_NO_ATTACH);
3676
3677 /*
3678 * Init Interfaces.
3679 */
3680 pDrvIns->IBase.pfnQueryInterface = Display::i_drvQueryInterface;
3681
3682 pThis->IConnector.pfnResize = Display::i_displayResizeCallback;
3683 pThis->IConnector.pfnUpdateRect = Display::i_displayUpdateCallback;
3684 pThis->IConnector.pfnRefresh = Display::i_displayRefreshCallback;
3685 pThis->IConnector.pfnReset = Display::i_displayResetCallback;
3686 pThis->IConnector.pfnLFBModeChange = Display::i_displayLFBModeChangeCallback;
3687 pThis->IConnector.pfnProcessAdapterData = Display::i_displayProcessAdapterDataCallback;
3688 pThis->IConnector.pfnProcessDisplayData = Display::i_displayProcessDisplayDataCallback;
3689#ifdef VBOX_WITH_VIDEOHWACCEL
3690 pThis->IConnector.pfnVHWACommandProcess = Display::i_displayVHWACommandProcess;
3691#endif
3692#ifdef VBOX_WITH_HGSMI
3693 pThis->IConnector.pfnVBVAEnable = Display::i_displayVBVAEnable;
3694 pThis->IConnector.pfnVBVADisable = Display::i_displayVBVADisable;
3695 pThis->IConnector.pfnVBVAUpdateBegin = Display::i_displayVBVAUpdateBegin;
3696 pThis->IConnector.pfnVBVAUpdateProcess = Display::i_displayVBVAUpdateProcess;
3697 pThis->IConnector.pfnVBVAUpdateEnd = Display::i_displayVBVAUpdateEnd;
3698 pThis->IConnector.pfnVBVAResize = Display::i_displayVBVAResize;
3699 pThis->IConnector.pfnVBVAMousePointerShape = Display::i_displayVBVAMousePointerShape;
3700 pThis->IConnector.pfnVBVAGuestCapabilityUpdate = Display::i_displayVBVAGuestCapabilityUpdate;
3701 pThis->IConnector.pfnVBVAInputMappingUpdate = Display::i_displayVBVAInputMappingUpdate;
3702 pThis->IConnector.pfnVBVAReportCursorPosition = Display::i_displayVBVAReportCursorPosition;
3703#endif
3704
3705 /*
3706 * Get the IDisplayPort interface of the above driver/device.
3707 */
3708 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
3709 if (!pThis->pUpPort)
3710 {
3711 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
3712 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3713 }
3714#if defined(VBOX_WITH_VIDEOHWACCEL)
3715 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
3716 if (!pThis->pVBVACallbacks)
3717 {
3718 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
3719 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3720 }
3721#endif
3722 /*
3723 * Get the Display object pointer and update the mpDrv member.
3724 */
3725 void *pv;
3726 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
3727 if (RT_FAILURE(rc))
3728 {
3729 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
3730 return rc;
3731 }
3732 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
3733 pThis->pDisplay = pDisplay;
3734 pThis->pDisplay->mpDrv = pThis;
3735
3736 /* Disable VRAM to a buffer copy initially. */
3737 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
3738 pThis->IConnector.cBits = 32; /* DevVGA does nothing otherwise. */
3739
3740 /*
3741 * Start periodic screen refreshes
3742 */
3743 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
3744
3745 return rc;
3746}
3747
3748
3749/**
3750 * Display driver registration record.
3751 */
3752const PDMDRVREG Display::DrvReg =
3753{
3754 /* u32Version */
3755 PDM_DRVREG_VERSION,
3756 /* szName */
3757 "MainDisplay",
3758 /* szRCMod */
3759 "",
3760 /* szR0Mod */
3761 "",
3762 /* pszDescription */
3763 "Main display driver (Main as in the API).",
3764 /* fFlags */
3765 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
3766 /* fClass. */
3767 PDM_DRVREG_CLASS_DISPLAY,
3768 /* cMaxInstances */
3769 ~0U,
3770 /* cbInstance */
3771 sizeof(DRVMAINDISPLAY),
3772 /* pfnConstruct */
3773 Display::i_drvConstruct,
3774 /* pfnDestruct */
3775 Display::i_drvDestruct,
3776 /* pfnRelocate */
3777 NULL,
3778 /* pfnIOCtl */
3779 NULL,
3780 /* pfnPowerOn */
3781 NULL,
3782 /* pfnReset */
3783 NULL,
3784 /* pfnSuspend */
3785 NULL,
3786 /* pfnResume */
3787 NULL,
3788 /* pfnAttach */
3789 NULL,
3790 /* pfnDetach */
3791 NULL,
3792 /* pfnPowerOff */
3793 Display::i_drvPowerOff,
3794 /* pfnSoftReset */
3795 NULL,
3796 /* u32EndVersion */
3797 PDM_DRVREG_VERSION
3798};
3799
3800/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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