VirtualBox

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

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

Main/GraphicsAdapter: Split off a few attributes from Machine interface, which affects quite a few other interfaces.
Frontends/VirtualBox+VBoxManage+VBoxSDL+VBoxShell: Adapt accordingly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 129.1 KB
Line 
1/* $Id: DisplayImpl.cpp 81964 2019-11-18 20:42:02Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#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 keyboard 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_handleQueryVisibleRegion(uint32_t *pcRects, PRTRECT paRects)
1289{
1290 /// @todo Currently not used by the guest and is not implemented in
1291 /// framebuffers. Remove?
1292 RT_NOREF(pcRects, paRects);
1293 return VERR_NOT_SUPPORTED;
1294}
1295
1296#ifdef VBOX_WITH_HGSMI
1297static void vbvaSetMemoryFlagsHGSMI(unsigned uScreenId,
1298 uint32_t fu32SupportedOrders,
1299 bool fVideoAccelVRDP,
1300 DISPLAYFBINFO *pFBInfo)
1301{
1302 LogRelFlowFunc(("HGSMI[%d]: %p\n", uScreenId, pFBInfo->pVBVAHostFlags));
1303
1304 if (pFBInfo->pVBVAHostFlags)
1305 {
1306 uint32_t fu32HostEvents = VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1307
1308 if (pFBInfo->fVBVAEnabled)
1309 {
1310 fu32HostEvents |= VBVA_F_MODE_ENABLED;
1311
1312 if (fVideoAccelVRDP)
1313 {
1314 fu32HostEvents |= VBVA_F_MODE_VRDP;
1315 }
1316 }
1317
1318 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32HostEvents, fu32HostEvents);
1319 ASMAtomicWriteU32(&pFBInfo->pVBVAHostFlags->u32SupportedOrders, fu32SupportedOrders);
1320
1321 LogRelFlowFunc((" fu32HostEvents = 0x%08X, fu32SupportedOrders = 0x%08X\n", fu32HostEvents, fu32SupportedOrders));
1322 }
1323}
1324
1325static void vbvaSetMemoryFlagsAllHGSMI(uint32_t fu32SupportedOrders,
1326 bool fVideoAccelVRDP,
1327 DISPLAYFBINFO *paFBInfos,
1328 unsigned cFBInfos)
1329{
1330 unsigned uScreenId;
1331
1332 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1333 {
1334 vbvaSetMemoryFlagsHGSMI(uScreenId, fu32SupportedOrders, fVideoAccelVRDP, &paFBInfos[uScreenId]);
1335 }
1336}
1337#endif /* VBOX_WITH_HGSMI */
1338
1339int Display::VideoAccelEnableVMMDev(bool fEnable, VBVAMEMORY *pVbvaMemory)
1340{
1341 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1342 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1343 if (RT_SUCCESS(rc))
1344 {
1345 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1346 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1347 }
1348 LogFlowFunc(("leave %Rrc\n", rc));
1349 return rc;
1350}
1351
1352int Display::VideoAccelEnableVGA(bool fEnable, VBVAMEMORY *pVbvaMemory)
1353{
1354 LogFlowFunc(("%d %p\n", fEnable, pVbvaMemory));
1355 int rc = videoAccelEnterVGA(&mVideoAccelLegacy);
1356 if (RT_SUCCESS(rc))
1357 {
1358 rc = i_VideoAccelEnable(fEnable, pVbvaMemory, mpDrv->pUpPort);
1359 videoAccelLeaveVGA(&mVideoAccelLegacy);
1360 }
1361 LogFlowFunc(("leave %Rrc\n", rc));
1362 return rc;
1363}
1364
1365void Display::VideoAccelFlushVMMDev(void)
1366{
1367 LogFlowFunc(("enter\n"));
1368 int rc = videoAccelEnterVMMDev(&mVideoAccelLegacy);
1369 if (RT_SUCCESS(rc))
1370 {
1371 i_VideoAccelFlush(mpDrv->pUpPort);
1372 videoAccelLeaveVMMDev(&mVideoAccelLegacy);
1373 }
1374 LogFlowFunc(("leave\n"));
1375}
1376
1377/* Called always by one VRDP server thread. Can be thread-unsafe.
1378 */
1379void Display::i_VRDPConnectionEvent(bool fConnect)
1380{
1381 LogRelFlowFunc(("fConnect = %d\n", fConnect));
1382
1383 int c = fConnect?
1384 ASMAtomicIncS32(&mcVRDPRefs):
1385 ASMAtomicDecS32(&mcVRDPRefs);
1386
1387 i_VideoAccelVRDP(fConnect, c);
1388 i_UpdateDeviceCursorCapabilities();
1389}
1390
1391
1392void Display::i_VideoAccelVRDP(bool fEnable, int c)
1393{
1394 VIDEOACCEL *pVideoAccel = &mVideoAccelLegacy;
1395
1396 Assert (c >= 0);
1397 RT_NOREF(fEnable);
1398
1399 /* This can run concurrently with Display videoaccel state change. */
1400 RTCritSectEnter(&mVideoAccelLock);
1401
1402 if (c == 0)
1403 {
1404 /* The last client has disconnected, and the accel can be
1405 * disabled.
1406 */
1407 Assert(fEnable == false);
1408
1409 mfVideoAccelVRDP = false;
1410 mfu32SupportedOrders = 0;
1411
1412 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1413 maFramebuffers, mcMonitors);
1414#ifdef VBOX_WITH_HGSMI
1415 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1416 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1417#endif /* VBOX_WITH_HGSMI */
1418
1419 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
1420 }
1421 else if ( c == 1
1422 && !mfVideoAccelVRDP)
1423 {
1424 /* The first client has connected. Enable the accel.
1425 */
1426 Assert(fEnable == true);
1427
1428 mfVideoAccelVRDP = true;
1429 /* Supporting all orders. */
1430 mfu32SupportedOrders = UINT32_MAX;
1431
1432 i_vbvaSetMemoryFlags(pVideoAccel->pVbvaMemory, pVideoAccel->fVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders,
1433 maFramebuffers, mcMonitors);
1434#ifdef VBOX_WITH_HGSMI
1435 /* Here is VRDP-IN thread. Process the request in vbvaUpdateBegin under DevVGA lock on an EMT. */
1436 ASMAtomicIncU32(&mu32UpdateVBVAFlags);
1437#endif /* VBOX_WITH_HGSMI */
1438
1439 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
1440 }
1441 else
1442 {
1443 /* A client is connected or disconnected but there is no change in the
1444 * accel state. It remains enabled.
1445 */
1446 Assert(mfVideoAccelVRDP == true);
1447 }
1448
1449 RTCritSectLeave(&mVideoAccelLock);
1450}
1451
1452void Display::i_notifyPowerDown(void)
1453{
1454 LogRelFlowFunc(("\n"));
1455
1456 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1457
1458 /* Source bitmaps are not available anymore. */
1459 mfSourceBitmapEnabled = false;
1460
1461 alock.release();
1462
1463 /* Resize all displays to tell framebuffers to forget current source bitmap. */
1464 unsigned uScreenId = mcMonitors;
1465 while (uScreenId > 0)
1466 {
1467 --uScreenId;
1468
1469 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1470 if (!pFBInfo->fDisabled)
1471 {
1472 i_handleDisplayResize(uScreenId, 32,
1473 pFBInfo->pu8FramebufferVRAM,
1474 pFBInfo->u32LineSize,
1475 pFBInfo->w,
1476 pFBInfo->h,
1477 pFBInfo->flags,
1478 pFBInfo->xOrigin,
1479 pFBInfo->yOrigin,
1480 false);
1481 }
1482 }
1483}
1484
1485// Wrapped IDisplay methods
1486/////////////////////////////////////////////////////////////////////////////
1487HRESULT Display::getScreenResolution(ULONG aScreenId, ULONG *aWidth, ULONG *aHeight, ULONG *aBitsPerPixel,
1488 LONG *aXOrigin, LONG *aYOrigin, GuestMonitorStatus_T *aGuestMonitorStatus)
1489{
1490 LogRelFlowFunc(("aScreenId=%RU32\n", aScreenId));
1491
1492 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1493
1494 if (aScreenId >= mcMonitors)
1495 return E_INVALIDARG;
1496
1497 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1498
1499 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
1500
1501 if (pFBInfo->flags & VBVA_SCREEN_F_DISABLED)
1502 guestMonitorStatus = GuestMonitorStatus_Disabled;
1503 else if (pFBInfo->flags & (VBVA_SCREEN_F_BLANK | VBVA_SCREEN_F_BLANK2))
1504 guestMonitorStatus = GuestMonitorStatus_Blank;
1505
1506 if (aWidth)
1507 *aWidth = pFBInfo->w;
1508 if (aHeight)
1509 *aHeight = pFBInfo->h;
1510 if (aBitsPerPixel)
1511 *aBitsPerPixel = pFBInfo->u16BitsPerPixel;
1512 if (aXOrigin)
1513 *aXOrigin = pFBInfo->xOrigin;
1514 if (aYOrigin)
1515 *aYOrigin = pFBInfo->yOrigin;
1516 if (aGuestMonitorStatus)
1517 *aGuestMonitorStatus = guestMonitorStatus;
1518
1519 return S_OK;
1520}
1521
1522
1523HRESULT Display::attachFramebuffer(ULONG aScreenId, const ComPtr<IFramebuffer> &aFramebuffer, com::Guid &aId)
1524{
1525 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1526
1527 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1528
1529 if (aScreenId >= mcMonitors)
1530 return setError(E_INVALIDARG, tr("AttachFramebuffer: Invalid screen %d (total %d)"),
1531 aScreenId, mcMonitors);
1532
1533 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1534 if (!pFBInfo->pFramebuffer.isNull())
1535 return setError(E_FAIL, tr("AttachFramebuffer: Framebuffer already attached to %d"),
1536 aScreenId);
1537
1538 pFBInfo->pFramebuffer = aFramebuffer;
1539 pFBInfo->framebufferId.create();
1540 aId = pFBInfo->framebufferId;
1541
1542 SafeArray<FramebufferCapabilities_T> caps;
1543 pFBInfo->pFramebuffer->COMGETTER(Capabilities)(ComSafeArrayAsOutParam(caps));
1544 pFBInfo->u32Caps = 0;
1545 size_t i;
1546 for (i = 0; i < caps.size(); ++i)
1547 pFBInfo->u32Caps |= caps[i];
1548
1549 alock.release();
1550
1551 /* The driver might not have been constructed yet */
1552 if (mpDrv)
1553 {
1554 /* Inform the framebuffer about the actual screen size. */
1555 HRESULT hr = aFramebuffer->NotifyChange(aScreenId, 0, 0, pFBInfo->w, pFBInfo->h); /** @todo origin */
1556 LogFunc(("NotifyChange hr %08X\n", hr)); NOREF(hr);
1557
1558 /* Re-send the seamless rectangles if necessary. */
1559 if (mfSeamlessEnabled)
1560 i_handleSetVisibleRegion(mcRectVisibleRegion, mpRectVisibleRegion);
1561 }
1562
1563 Console::SafeVMPtrQuiet ptrVM(mParent);
1564 if (ptrVM.isOk())
1565 {
1566 VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
1567 3, this, aScreenId, false);
1568 }
1569
1570 LogRelFlowFunc(("Attached to %d %RTuuid\n", aScreenId, aId.raw()));
1571 return S_OK;
1572}
1573
1574HRESULT Display::detachFramebuffer(ULONG aScreenId, const com::Guid &aId)
1575{
1576 LogRelFlowFunc(("aScreenId = %d %RTuuid\n", aScreenId, aId.raw()));
1577
1578 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1579
1580 if (aScreenId >= mcMonitors)
1581 return setError(E_INVALIDARG, tr("DetachFramebuffer: Invalid screen %d (total %d)"),
1582 aScreenId, mcMonitors);
1583
1584 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1585
1586 if (pFBInfo->framebufferId != aId)
1587 {
1588 LogRelFlowFunc(("Invalid framebuffer aScreenId = %d, attached %p\n", aScreenId, pFBInfo->framebufferId.raw()));
1589 return setError(E_FAIL, tr("DetachFramebuffer: Invalid framebuffer object"));
1590 }
1591
1592 pFBInfo->pFramebuffer.setNull();
1593 pFBInfo->framebufferId.clear();
1594
1595 alock.release();
1596 return S_OK;
1597}
1598
1599HRESULT Display::queryFramebuffer(ULONG aScreenId, ComPtr<IFramebuffer> &aFramebuffer)
1600{
1601 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
1602
1603 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1604
1605 if (aScreenId >= mcMonitors)
1606 return setError(E_INVALIDARG, tr("QueryFramebuffer: Invalid screen %d (total %d)"),
1607 aScreenId, mcMonitors);
1608
1609 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1610
1611 pFBInfo->pFramebuffer.queryInterfaceTo(aFramebuffer.asOutParam());
1612
1613 return S_OK;
1614}
1615
1616HRESULT Display::setVideoModeHint(ULONG aDisplay, BOOL aEnabled,
1617 BOOL aChangeOrigin, LONG aOriginX, LONG aOriginY,
1618 ULONG aWidth, ULONG aHeight, ULONG aBitsPerPixel,
1619 BOOL aNotify)
1620{
1621 if (aWidth == 0 || aHeight == 0 || aBitsPerPixel == 0)
1622 {
1623 /* Some of parameters must not change. Query current mode. */
1624 ULONG ulWidth = 0;
1625 ULONG ulHeight = 0;
1626 ULONG ulBitsPerPixel = 0;
1627 HRESULT hr = getScreenResolution(aDisplay, &ulWidth, &ulHeight, &ulBitsPerPixel, NULL, NULL, NULL);
1628 if (FAILED(hr))
1629 return hr;
1630
1631 /* Assign current values to not changing parameters. */
1632 if (aWidth == 0)
1633 aWidth = ulWidth;
1634 if (aHeight == 0)
1635 aHeight = ulHeight;
1636 if (aBitsPerPixel == 0)
1637 aBitsPerPixel = ulBitsPerPixel;
1638 }
1639
1640 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1641
1642 if (aDisplay >= mcMonitors)
1643 return E_INVALIDARG;
1644
1645 VMMDevDisplayDef d;
1646 d.idDisplay = aDisplay;
1647 d.xOrigin = aOriginX;
1648 d.yOrigin = aOriginY;
1649 d.cx = aWidth;
1650 d.cy = aHeight;
1651 d.cBitsPerPixel = aBitsPerPixel;
1652 d.fDisplayFlags = VMMDEV_DISPLAY_CX | VMMDEV_DISPLAY_CY | VMMDEV_DISPLAY_BPP;
1653 if (!aEnabled)
1654 d.fDisplayFlags |= VMMDEV_DISPLAY_DISABLED;
1655 if (aChangeOrigin)
1656 d.fDisplayFlags |= VMMDEV_DISPLAY_ORIGIN;
1657 if (aDisplay == 0)
1658 d.fDisplayFlags |= VMMDEV_DISPLAY_PRIMARY;
1659
1660 /* Remember the monitor information. */
1661 maFramebuffers[aDisplay].monitorDesc = d;
1662
1663 CHECK_CONSOLE_DRV(mpDrv);
1664
1665 /*
1666 * It is up to the guest to decide whether the hint is
1667 * valid. Therefore don't do any VRAM sanity checks here.
1668 */
1669
1670 /* Have to release the lock because the pfnRequestDisplayChange
1671 * will call EMT. */
1672 alock.release();
1673
1674 /* We always send the hint to the graphics card in case the guest enables
1675 * support later. For now we notify exactly when support is enabled. */
1676 mpDrv->pUpPort->pfnSendModeHint(mpDrv->pUpPort, aWidth, aHeight,
1677 aBitsPerPixel, aDisplay,
1678 aChangeOrigin ? aOriginX : ~0,
1679 aChangeOrigin ? aOriginY : ~0,
1680 RT_BOOL(aEnabled),
1681 ( mfGuestVBVACapabilities
1682 & VBVACAPS_VIDEO_MODE_HINTS)
1683 && aNotify);
1684 if ( mfGuestVBVACapabilities & VBVACAPS_VIDEO_MODE_HINTS
1685 && !(mfGuestVBVACapabilities & VBVACAPS_IRQ)
1686 && aNotify)
1687 {
1688 mParent->i_sendACPIMonitorHotPlugEvent();
1689 }
1690
1691 /* We currently never suppress the VMMDev hint if the guest has requested
1692 * it. Specifically the video graphics driver may not be responsible for
1693 * screen positioning in the guest virtual desktop, and the component
1694 * responsible may want to get the hint from VMMDev. */
1695 VMMDev *pVMMDev = mParent->i_getVMMDev();
1696 if (pVMMDev)
1697 {
1698 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1699 if (pVMMDevPort)
1700 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, 1, &d, false, RT_BOOL(aNotify));
1701 }
1702 /* Notify listeners. */
1703 fireGuestMonitorInfoChangedEvent(mParent->i_getEventSource(), aDisplay);
1704 return S_OK;
1705}
1706
1707HRESULT Display::getVideoModeHint(ULONG cDisplay, BOOL *pfEnabled,
1708 BOOL *pfChangeOrigin, LONG *pxOrigin, LONG *pyOrigin,
1709 ULONG *pcx, ULONG *pcy, ULONG *pcBitsPerPixel)
1710{
1711 if (cDisplay >= mcMonitors)
1712 return E_INVALIDARG;
1713
1714 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
1715 if (pfEnabled)
1716 *pfEnabled = !( maFramebuffers[cDisplay].monitorDesc.fDisplayFlags
1717 & VMMDEV_DISPLAY_DISABLED);
1718 if (pfChangeOrigin)
1719 *pfChangeOrigin = RT_BOOL( maFramebuffers[cDisplay].monitorDesc.fDisplayFlags
1720 & VMMDEV_DISPLAY_ORIGIN);
1721 if (pxOrigin)
1722 *pxOrigin = maFramebuffers[cDisplay].monitorDesc.xOrigin;
1723 if (pyOrigin)
1724 *pyOrigin = maFramebuffers[cDisplay].monitorDesc.yOrigin;
1725 if (pcx)
1726 *pcx = maFramebuffers[cDisplay].monitorDesc.cx;
1727 if (pcy)
1728 *pcy = maFramebuffers[cDisplay].monitorDesc.cy;
1729 if (pcBitsPerPixel)
1730 *pcBitsPerPixel = maFramebuffers[cDisplay].monitorDesc.cBitsPerPixel;
1731 return S_OK;
1732}
1733
1734HRESULT Display::setSeamlessMode(BOOL enabled)
1735{
1736 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
1737
1738 /* Have to release the lock because the pfnRequestSeamlessChange will call EMT. */
1739 alock.release();
1740
1741 VMMDev *pVMMDev = mParent->i_getVMMDev();
1742 if (pVMMDev)
1743 {
1744 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
1745 if (pVMMDevPort)
1746 pVMMDevPort->pfnRequestSeamlessChange(pVMMDevPort, !!enabled);
1747 }
1748 mfSeamlessEnabled = RT_BOOL(enabled);
1749 return S_OK;
1750}
1751
1752/* static */
1753int Display::i_displayTakeScreenshotEMT(Display *pDisplay, ULONG aScreenId, uint8_t **ppbData, size_t *pcbData,
1754 uint32_t *pcx, uint32_t *pcy, bool *pfMemFree)
1755{
1756 int rc;
1757 if ( aScreenId == VBOX_VIDEO_PRIMARY_SCREEN
1758 && pDisplay->maFramebuffers[aScreenId].fVBVAEnabled == false) /* A non-VBVA mode. */
1759 {
1760 if (pDisplay->mpDrv)
1761 {
1762 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
1763 *pfMemFree = false;
1764 }
1765 else
1766 {
1767 /* No image. */
1768 *ppbData = NULL;
1769 *pcbData = 0;
1770 *pcx = 0;
1771 *pcy = 0;
1772 *pfMemFree = true;
1773 rc = VINF_SUCCESS;
1774 }
1775 }
1776 else if (aScreenId < pDisplay->mcMonitors)
1777 {
1778 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
1779
1780 uint32_t width = pFBInfo->w;
1781 uint32_t height = pFBInfo->h;
1782
1783 /* Allocate 32 bit per pixel bitmap. */
1784 size_t cbRequired = width * 4 * height;
1785
1786 if (cbRequired)
1787 {
1788 uint8_t *pbDst = (uint8_t *)RTMemAlloc(cbRequired);
1789 if (pbDst != NULL)
1790 {
1791 if (pFBInfo->flags & VBVA_SCREEN_F_ACTIVE)
1792 {
1793 /* Copy guest VRAM to the allocated 32bpp buffer. */
1794 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
1795 int32_t xSrc = 0;
1796 int32_t ySrc = 0;
1797 uint32_t u32SrcWidth = width;
1798 uint32_t u32SrcHeight = height;
1799 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
1800 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
1801
1802 int32_t xDst = 0;
1803 int32_t yDst = 0;
1804 uint32_t u32DstWidth = u32SrcWidth;
1805 uint32_t u32DstHeight = u32SrcHeight;
1806 uint32_t u32DstLineSize = u32DstWidth * 4;
1807 uint32_t u32DstBitsPerPixel = 32;
1808
1809 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
1810 width, height,
1811 pu8Src,
1812 xSrc, ySrc,
1813 u32SrcWidth, u32SrcHeight,
1814 u32SrcLineSize, u32SrcBitsPerPixel,
1815 pbDst,
1816 xDst, yDst,
1817 u32DstWidth, u32DstHeight,
1818 u32DstLineSize, u32DstBitsPerPixel);
1819 }
1820 else
1821 {
1822 memset(pbDst, 0, cbRequired);
1823 rc = VINF_SUCCESS;
1824 }
1825 if (RT_SUCCESS(rc))
1826 {
1827 *ppbData = pbDst;
1828 *pcbData = cbRequired;
1829 *pcx = width;
1830 *pcy = height;
1831 *pfMemFree = true;
1832 }
1833 else
1834 {
1835 RTMemFree(pbDst);
1836
1837 /* CopyRect can fail if VBVA was paused in VGA device, retry using the generic method. */
1838 if ( rc == VERR_INVALID_STATE
1839 && aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1840 {
1841 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppbData, pcbData, pcx, pcy);
1842 *pfMemFree = false;
1843 }
1844 }
1845 }
1846 else
1847 rc = VERR_NO_MEMORY;
1848 }
1849 else
1850 {
1851 /* No image. */
1852 *ppbData = NULL;
1853 *pcbData = 0;
1854 *pcx = 0;
1855 *pcy = 0;
1856 *pfMemFree = true;
1857 rc = VINF_SUCCESS;
1858 }
1859 }
1860 else
1861 rc = VERR_INVALID_PARAMETER;
1862 return rc;
1863}
1864
1865static int i_displayTakeScreenshot(PUVM pUVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, ULONG aScreenId,
1866 BYTE *address, ULONG width, ULONG height)
1867{
1868 uint8_t *pbData = NULL;
1869 size_t cbData = 0;
1870 uint32_t cx = 0;
1871 uint32_t cy = 0;
1872 bool fFreeMem = false;
1873 int vrc = VINF_SUCCESS;
1874
1875 int cRetries = 5;
1876 while (cRetries-- > 0)
1877 {
1878 /* Note! Not sure if the priority call is such a good idea here, but
1879 it would be nice to have an accurate screenshot for the bug
1880 report if the VM deadlocks. */
1881 vrc = VMR3ReqPriorityCallWaitU(pUVM, VMCPUID_ANY, (PFNRT)Display::i_displayTakeScreenshotEMT, 7,
1882 pDisplay, aScreenId, &pbData, &cbData, &cx, &cy, &fFreeMem);
1883 if (vrc != VERR_TRY_AGAIN)
1884 {
1885 break;
1886 }
1887
1888 RTThreadSleep(10);
1889 }
1890
1891 if (RT_SUCCESS(vrc) && pbData)
1892 {
1893 if (cx == width && cy == height)
1894 {
1895 /* No scaling required. */
1896 memcpy(address, pbData, cbData);
1897 }
1898 else
1899 {
1900 /* Scale. */
1901 LogRelFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
1902
1903 uint8_t *dst = address;
1904 uint8_t *src = pbData;
1905 int dstW = width;
1906 int dstH = height;
1907 int srcW = cx;
1908 int srcH = cy;
1909 int iDeltaLine = cx * 4;
1910
1911 BitmapScale32(dst,
1912 dstW, dstH,
1913 src,
1914 iDeltaLine,
1915 srcW, srcH);
1916 }
1917
1918 if (fFreeMem)
1919 RTMemFree(pbData);
1920 else
1921 {
1922 /* This can be called from any thread. */
1923 pDrv->pUpPort->pfnFreeScreenshot(pDrv->pUpPort, pbData);
1924 }
1925 }
1926
1927 return vrc;
1928}
1929
1930HRESULT Display::takeScreenShotWorker(ULONG aScreenId,
1931 BYTE *aAddress,
1932 ULONG aWidth,
1933 ULONG aHeight,
1934 BitmapFormat_T aBitmapFormat,
1935 ULONG *pcbOut)
1936{
1937 HRESULT rc = S_OK;
1938
1939 /* Do not allow too small and too large screenshots. This also filters out negative
1940 * values passed as either 'aWidth' or 'aHeight'.
1941 */
1942 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
1943 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
1944
1945 if ( aBitmapFormat != BitmapFormat_BGR0
1946 && aBitmapFormat != BitmapFormat_BGRA
1947 && aBitmapFormat != BitmapFormat_RGBA
1948 && aBitmapFormat != BitmapFormat_PNG)
1949 {
1950 return setError(E_NOTIMPL,
1951 tr("Unsupported screenshot format 0x%08X"), aBitmapFormat);
1952 }
1953
1954 Console::SafeVMPtr ptrVM(mParent);
1955 if (!ptrVM.isOk())
1956 return ptrVM.rc();
1957
1958 int vrc = i_displayTakeScreenshot(ptrVM.rawUVM(), this, mpDrv, aScreenId, aAddress, aWidth, aHeight);
1959
1960 if (RT_SUCCESS(vrc))
1961 {
1962 const size_t cbData = aWidth * 4 * aHeight;
1963
1964 /* Most of uncompressed formats. */
1965 *pcbOut = (ULONG)cbData;
1966
1967 if (aBitmapFormat == BitmapFormat_BGR0)
1968 {
1969 /* Do nothing. */
1970 }
1971 else if (aBitmapFormat == BitmapFormat_BGRA)
1972 {
1973 uint32_t *pu32 = (uint32_t *)aAddress;
1974 size_t cPixels = aWidth * aHeight;
1975 while (cPixels--)
1976 {
1977 *pu32++ |= UINT32_C(0xFF000000);
1978 }
1979 }
1980 else if (aBitmapFormat == BitmapFormat_RGBA)
1981 {
1982 uint8_t *pu8 = aAddress;
1983 size_t cPixels = aWidth * aHeight;
1984 while (cPixels--)
1985 {
1986 uint8_t u8 = pu8[0];
1987 pu8[0] = pu8[2];
1988 pu8[2] = u8;
1989 pu8[3] = 0xFF;
1990
1991 pu8 += 4;
1992 }
1993 }
1994 else if (aBitmapFormat == BitmapFormat_PNG)
1995 {
1996 uint8_t *pu8PNG = NULL;
1997 uint32_t cbPNG = 0;
1998 uint32_t cxPNG = 0;
1999 uint32_t cyPNG = 0;
2000
2001 vrc = DisplayMakePNG(aAddress, aWidth, aHeight, &pu8PNG, &cbPNG, &cxPNG, &cyPNG, 0);
2002 if (RT_SUCCESS(vrc))
2003 {
2004 if (cbPNG <= cbData)
2005 {
2006 memcpy(aAddress, pu8PNG, cbPNG);
2007 *pcbOut = cbPNG;
2008 }
2009 else
2010 {
2011 rc = setError(E_FAIL,
2012 tr("PNG is larger than 32bpp bitmap"));
2013 }
2014 }
2015 else
2016 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not convert screenshot to PNG (%Rrc)"), vrc);
2017 RTMemFree(pu8PNG);
2018 }
2019 }
2020 else if (vrc == VERR_TRY_AGAIN)
2021 rc = setErrorBoth(E_UNEXPECTED, vrc, tr("Screenshot is not available at this time"));
2022 else if (RT_FAILURE(vrc))
2023 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not take a screenshot (%Rrc)"), vrc);
2024
2025 return rc;
2026}
2027
2028HRESULT Display::takeScreenShot(ULONG aScreenId,
2029 BYTE *aAddress,
2030 ULONG aWidth,
2031 ULONG aHeight,
2032 BitmapFormat_T aBitmapFormat)
2033{
2034 HRESULT rc = S_OK;
2035
2036 LogRelFlowFunc(("[%d] address=%p, width=%d, height=%d, format 0x%08X\n",
2037 aScreenId, aAddress, aWidth, aHeight, aBitmapFormat));
2038
2039 ULONG cbOut = 0;
2040 rc = takeScreenShotWorker(aScreenId, aAddress, aWidth, aHeight, aBitmapFormat, &cbOut);
2041 NOREF(cbOut);
2042
2043 LogRelFlowFunc(("%Rhrc\n", rc));
2044 return rc;
2045}
2046
2047HRESULT Display::takeScreenShotToArray(ULONG aScreenId,
2048 ULONG aWidth,
2049 ULONG aHeight,
2050 BitmapFormat_T aBitmapFormat,
2051 std::vector<BYTE> &aScreenData)
2052{
2053 HRESULT rc = S_OK;
2054
2055 LogRelFlowFunc(("[%d] width=%d, height=%d, format 0x%08X\n",
2056 aScreenId, aWidth, aHeight, aBitmapFormat));
2057
2058 /* Do not allow too small and too large screenshots. This also filters out negative
2059 * values passed as either 'aWidth' or 'aHeight'.
2060 */
2061 CheckComArgExpr(aWidth, aWidth != 0 && aWidth <= 32767);
2062 CheckComArgExpr(aHeight, aHeight != 0 && aHeight <= 32767);
2063
2064 const size_t cbData = aWidth * 4 * aHeight;
2065 aScreenData.resize(cbData);
2066
2067 ULONG cbOut = 0;
2068 rc = takeScreenShotWorker(aScreenId, &aScreenData.front(), aWidth, aHeight, aBitmapFormat, &cbOut);
2069 if (FAILED(rc))
2070 cbOut = 0;
2071
2072 aScreenData.resize(cbOut);
2073
2074 LogRelFlowFunc(("%Rhrc\n", rc));
2075 return rc;
2076}
2077
2078#ifdef VBOX_WITH_RECORDING
2079/**
2080 * Invalidates the recording configuration.
2081 *
2082 * @returns IPRT status code.
2083 */
2084int Display::i_recordingInvalidate(void)
2085{
2086 RecordingContext *pCtx = mParent->i_recordingGetContext();
2087 if (!pCtx || !pCtx->IsStarted())
2088 return VINF_SUCCESS;
2089
2090 /*
2091 * Invalidate screens.
2092 */
2093 for (unsigned uScreen = 0; uScreen < mcMonitors; uScreen++)
2094 {
2095 RecordingStream *pRecordingStream = pCtx->GetStream(uScreen);
2096
2097 const bool fStreamEnabled = pRecordingStream->IsReady();
2098 bool fChanged = maRecordingEnabled[uScreen] != fStreamEnabled;
2099
2100 maRecordingEnabled[uScreen] = fStreamEnabled;
2101
2102 if (fChanged && uScreen < mcMonitors)
2103 i_recordingScreenChanged(uScreen);
2104 }
2105
2106 return VINF_SUCCESS;
2107}
2108
2109void Display::i_recordingScreenChanged(unsigned uScreenId)
2110{
2111 RecordingContext *pCtx = mParent->i_recordingGetContext();
2112
2113 i_UpdateDeviceCursorCapabilities();
2114 if ( RT_LIKELY(!maRecordingEnabled[uScreenId])
2115 || !pCtx || !pCtx->IsStarted())
2116 {
2117 /* Skip recording this screen. */
2118 return;
2119 }
2120
2121 /* Get a new source bitmap which will be used by video recording code. */
2122 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
2123 QuerySourceBitmap(uScreenId, pSourceBitmap.asOutParam());
2124
2125 int rc2 = RTCritSectEnter(&mVideoRecLock);
2126 if (RT_SUCCESS(rc2))
2127 {
2128 maFramebuffers[uScreenId].Recording.pSourceBitmap = pSourceBitmap;
2129
2130 rc2 = RTCritSectLeave(&mVideoRecLock);
2131 AssertRC(rc2);
2132 }
2133}
2134#endif /* VBOX_WITH_RECORDING */
2135
2136int Display::i_drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address,
2137 ULONG x, ULONG y, ULONG width, ULONG height)
2138{
2139 int rc = VINF_SUCCESS;
2140
2141 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2142
2143 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2144 {
2145 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2146 }
2147 else if (aScreenId < pDisplay->mcMonitors)
2148 {
2149 /* Copy the bitmap to the guest VRAM. */
2150 const uint8_t *pu8Src = address;
2151 int32_t xSrc = 0;
2152 int32_t ySrc = 0;
2153 uint32_t u32SrcWidth = width;
2154 uint32_t u32SrcHeight = height;
2155 uint32_t u32SrcLineSize = width * 4;
2156 uint32_t u32SrcBitsPerPixel = 32;
2157
2158 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2159 int32_t xDst = x;
2160 int32_t yDst = y;
2161 uint32_t u32DstWidth = pFBInfo->w;
2162 uint32_t u32DstHeight = pFBInfo->h;
2163 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2164 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2165
2166 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2167 width, height,
2168 pu8Src,
2169 xSrc, ySrc,
2170 u32SrcWidth, u32SrcHeight,
2171 u32SrcLineSize, u32SrcBitsPerPixel,
2172 pu8Dst,
2173 xDst, yDst,
2174 u32DstWidth, u32DstHeight,
2175 u32DstLineSize, u32DstBitsPerPixel);
2176 if (RT_SUCCESS(rc))
2177 {
2178 if (!pFBInfo->pSourceBitmap.isNull())
2179 {
2180 /* Update the changed screen area. When source bitmap uses VRAM directly, just notify
2181 * frontend to update. And for default format, render the guest VRAM to the source bitmap.
2182 */
2183 if ( pFBInfo->fDefaultFormat
2184 && !pFBInfo->fDisabled)
2185 {
2186 BYTE *pAddress = NULL;
2187 ULONG ulWidth = 0;
2188 ULONG ulHeight = 0;
2189 ULONG ulBitsPerPixel = 0;
2190 ULONG ulBytesPerLine = 0;
2191 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2192
2193 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2194 &ulWidth,
2195 &ulHeight,
2196 &ulBitsPerPixel,
2197 &ulBytesPerLine,
2198 &bitmapFormat);
2199 if (SUCCEEDED(hrc))
2200 {
2201 pu8Src = pFBInfo->pu8FramebufferVRAM;
2202 xSrc = x;
2203 ySrc = y;
2204 u32SrcWidth = pFBInfo->w;
2205 u32SrcHeight = pFBInfo->h;
2206 u32SrcLineSize = pFBInfo->u32LineSize;
2207 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2208
2209 /* Default format is 32 bpp. */
2210 pu8Dst = pAddress;
2211 xDst = xSrc;
2212 yDst = ySrc;
2213 u32DstWidth = u32SrcWidth;
2214 u32DstHeight = u32SrcHeight;
2215 u32DstLineSize = u32DstWidth * 4;
2216 u32DstBitsPerPixel = 32;
2217
2218 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2219 width, height,
2220 pu8Src,
2221 xSrc, ySrc,
2222 u32SrcWidth, u32SrcHeight,
2223 u32SrcLineSize, u32SrcBitsPerPixel,
2224 pu8Dst,
2225 xDst, yDst,
2226 u32DstWidth, u32DstHeight,
2227 u32DstLineSize, u32DstBitsPerPixel);
2228 }
2229 }
2230 }
2231
2232 pDisplay->i_handleDisplayUpdate(aScreenId, x, y, width, height);
2233 }
2234 }
2235 else
2236 {
2237 rc = VERR_INVALID_PARAMETER;
2238 }
2239
2240 if (RT_SUCCESS(rc))
2241 pDisplay->mParent->i_consoleVRDPServer()->SendUpdateBitmap(aScreenId, x, y, width, height);
2242
2243 return rc;
2244}
2245
2246HRESULT Display::drawToScreen(ULONG aScreenId, BYTE *aAddress, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2247{
2248 /// @todo (r=dmik) this function may take too long to complete if the VM
2249 // is doing something like saving state right now. Which, in case if it
2250 // is called on the GUI thread, will make it unresponsive. We should
2251 // check the machine state here (by enclosing the check and VMRequCall
2252 // within the Console lock to make it atomic).
2253
2254 LogRelFlowFunc(("aAddress=%p, x=%d, y=%d, width=%d, height=%d\n",
2255 (void *)aAddress, aX, aY, aWidth, aHeight));
2256
2257 CheckComArgExpr(aWidth, aWidth != 0);
2258 CheckComArgExpr(aHeight, aHeight != 0);
2259
2260 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2261
2262 CHECK_CONSOLE_DRV(mpDrv);
2263
2264 Console::SafeVMPtr ptrVM(mParent);
2265 if (!ptrVM.isOk())
2266 return ptrVM.rc();
2267
2268 /* Release lock because the call scheduled on EMT may also try to take it. */
2269 alock.release();
2270
2271 /*
2272 * Again we're lazy and make the graphics device do all the
2273 * dirty conversion work.
2274 */
2275 int vrc = VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_drawToScreenEMT, 7,
2276 this, aScreenId, aAddress, aX, aY, aWidth, aHeight);
2277
2278 /*
2279 * If the function returns not supported, we'll have to do all the
2280 * work ourselves using the framebuffer.
2281 */
2282 HRESULT rc = S_OK;
2283 if (vrc == VERR_NOT_SUPPORTED || vrc == VERR_NOT_IMPLEMENTED)
2284 {
2285 /** @todo implement generic fallback for screen blitting. */
2286 rc = E_NOTIMPL;
2287 }
2288 else if (RT_FAILURE(vrc))
2289 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not draw to the screen (%Rrc)"), vrc);
2290/// @todo
2291// else
2292// {
2293// /* All ok. Redraw the screen. */
2294// handleDisplayUpdate(x, y, width, height);
2295// }
2296
2297 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2298 return rc;
2299}
2300
2301int Display::i_InvalidateAndUpdateEMT(Display *pDisplay, unsigned uId, bool fUpdateAll)
2302{
2303 LogRelFlowFunc(("uId=%d, fUpdateAll %d\n", uId, fUpdateAll));
2304
2305 unsigned uScreenId;
2306 for (uScreenId = (fUpdateAll ? 0 : uId); uScreenId < pDisplay->mcMonitors; uScreenId++)
2307 {
2308 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2309
2310 if ( !pFBInfo->fVBVAEnabled
2311 && uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2312 {
2313 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort, /* fFailOnResize = */ true);
2314 }
2315 else
2316 {
2317 if (!pFBInfo->fDisabled)
2318 {
2319 /* Render complete VRAM screen to the framebuffer.
2320 * When framebuffer uses VRAM directly, just notify it to update.
2321 */
2322 if (pFBInfo->fDefaultFormat && !pFBInfo->pSourceBitmap.isNull())
2323 {
2324 BYTE *pAddress = NULL;
2325 ULONG ulWidth = 0;
2326 ULONG ulHeight = 0;
2327 ULONG ulBitsPerPixel = 0;
2328 ULONG ulBytesPerLine = 0;
2329 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2330
2331 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2332 &ulWidth,
2333 &ulHeight,
2334 &ulBitsPerPixel,
2335 &ulBytesPerLine,
2336 &bitmapFormat);
2337 if (SUCCEEDED(hrc))
2338 {
2339 uint32_t width = pFBInfo->w;
2340 uint32_t height = pFBInfo->h;
2341
2342 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2343 int32_t xSrc = 0;
2344 int32_t ySrc = 0;
2345 uint32_t u32SrcWidth = pFBInfo->w;
2346 uint32_t u32SrcHeight = pFBInfo->h;
2347 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2348 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2349
2350 /* Default format is 32 bpp. */
2351 uint8_t *pu8Dst = pAddress;
2352 int32_t xDst = xSrc;
2353 int32_t yDst = ySrc;
2354 uint32_t u32DstWidth = u32SrcWidth;
2355 uint32_t u32DstHeight = u32SrcHeight;
2356 uint32_t u32DstLineSize = u32DstWidth * 4;
2357 uint32_t u32DstBitsPerPixel = 32;
2358
2359 /* if uWidth != pFBInfo->w and uHeight != pFBInfo->h
2360 * implies resize of Framebuffer is in progress and
2361 * copyrect should not be called.
2362 */
2363 if (ulWidth == pFBInfo->w && ulHeight == pFBInfo->h)
2364 {
2365 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2366 width, height,
2367 pu8Src,
2368 xSrc, ySrc,
2369 u32SrcWidth, u32SrcHeight,
2370 u32SrcLineSize, u32SrcBitsPerPixel,
2371 pu8Dst,
2372 xDst, yDst,
2373 u32DstWidth, u32DstHeight,
2374 u32DstLineSize, u32DstBitsPerPixel);
2375 }
2376 }
2377 }
2378
2379 pDisplay->i_handleDisplayUpdate(uScreenId, 0, 0, pFBInfo->w, pFBInfo->h);
2380 }
2381 }
2382 if (!fUpdateAll)
2383 break;
2384 }
2385 LogRelFlowFunc(("done\n"));
2386 return VINF_SUCCESS;
2387}
2388
2389/**
2390 * Does a full invalidation of the VM display and instructs the VM
2391 * to update it immediately.
2392 *
2393 * @returns COM status code
2394 */
2395
2396HRESULT Display::invalidateAndUpdate()
2397{
2398 LogRelFlowFunc(("\n"));
2399
2400 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2401
2402 CHECK_CONSOLE_DRV(mpDrv);
2403
2404 Console::SafeVMPtr ptrVM(mParent);
2405 if (!ptrVM.isOk())
2406 return ptrVM.rc();
2407
2408 HRESULT rc = S_OK;
2409
2410 LogRelFlowFunc(("Sending DPYUPDATE request\n"));
2411
2412 /* Have to release the lock when calling EMT. */
2413 alock.release();
2414
2415 int vrc = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2416 3, this, 0, true);
2417 alock.acquire();
2418
2419 if (RT_FAILURE(vrc))
2420 rc = setErrorBoth(VBOX_E_VM_ERROR, vrc, tr("Could not invalidate and update the screen (%Rrc)"), vrc);
2421
2422 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2423 return rc;
2424}
2425
2426HRESULT Display::invalidateAndUpdateScreen(ULONG aScreenId)
2427{
2428 LogRelFlowFunc(("\n"));
2429
2430 HRESULT rc = S_OK;
2431
2432 Console::SafeVMPtr ptrVM(mParent);
2433 if (!ptrVM.isOk())
2434 return ptrVM.rc();
2435
2436 int vrc = VMR3ReqCallNoWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2437 3, this, aScreenId, false);
2438 if (RT_FAILURE(vrc))
2439 rc = setErrorBoth(VBOX_E_IPRT_ERROR, vrc, tr("Could not invalidate and update the screen %d (%Rrc)"), aScreenId, vrc);
2440
2441 LogRelFlowFunc(("rc=%Rhrc\n", rc));
2442 return rc;
2443}
2444
2445HRESULT Display::completeVHWACommand(BYTE *aCommand)
2446{
2447#ifdef VBOX_WITH_VIDEOHWACCEL
2448 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsync(mpDrv->pVBVACallbacks, (VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *)aCommand);
2449 return S_OK;
2450#else
2451 RT_NOREF(aCommand);
2452 return E_NOTIMPL;
2453#endif
2454}
2455
2456HRESULT Display::viewportChanged(ULONG aScreenId, ULONG aX, ULONG aY, ULONG aWidth, ULONG aHeight)
2457{
2458 AssertMsgReturn(aScreenId < mcMonitors, ("aScreendId=%d mcMonitors=%d\n", aScreenId, mcMonitors), E_INVALIDARG);
2459
2460 /* The driver might not have been constructed yet */
2461 if (mpDrv && mpDrv->pUpPort->pfnSetViewport)
2462 mpDrv->pUpPort->pfnSetViewport(mpDrv->pUpPort, aScreenId, aX, aY, aWidth, aHeight);
2463
2464 return S_OK;
2465}
2466
2467HRESULT Display::querySourceBitmap(ULONG aScreenId,
2468 ComPtr<IDisplaySourceBitmap> &aDisplaySourceBitmap)
2469{
2470 LogRelFlowFunc(("aScreenId = %d\n", aScreenId));
2471
2472 Console::SafeVMPtr ptrVM(mParent);
2473 if (!ptrVM.isOk())
2474 return ptrVM.rc();
2475
2476 CHECK_CONSOLE_DRV(mpDrv);
2477
2478 bool fSetRenderVRAM = false;
2479 bool fInvalidate = false;
2480
2481 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2482
2483 if (aScreenId >= mcMonitors)
2484 return setError(E_INVALIDARG, tr("QuerySourceBitmap: Invalid screen %d (total %d)"),
2485 aScreenId, mcMonitors);
2486
2487 if (!mfSourceBitmapEnabled)
2488 {
2489 aDisplaySourceBitmap = NULL;
2490 return E_FAIL;
2491 }
2492
2493 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2494
2495 /* No source bitmap for a blank guest screen. */
2496 if (pFBInfo->flags & VBVA_SCREEN_F_BLANK)
2497 {
2498 aDisplaySourceBitmap = NULL;
2499 return E_FAIL;
2500 }
2501
2502 HRESULT hr = S_OK;
2503
2504 if (pFBInfo->pSourceBitmap.isNull())
2505 {
2506 /* Create a new object. */
2507 ComObjPtr<DisplaySourceBitmap> obj;
2508 hr = obj.createObject();
2509 if (SUCCEEDED(hr))
2510 hr = obj->init(this, aScreenId, pFBInfo);
2511
2512 if (SUCCEEDED(hr))
2513 {
2514 pFBInfo->pSourceBitmap = obj;
2515 pFBInfo->fDefaultFormat = !obj->i_usesVRAM();
2516
2517 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2518 {
2519 /* Start buffer updates. */
2520 BYTE *pAddress = NULL;
2521 ULONG ulWidth = 0;
2522 ULONG ulHeight = 0;
2523 ULONG ulBitsPerPixel = 0;
2524 ULONG ulBytesPerLine = 0;
2525 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
2526
2527 pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
2528 &ulWidth,
2529 &ulHeight,
2530 &ulBitsPerPixel,
2531 &ulBytesPerLine,
2532 &bitmapFormat);
2533
2534 mpDrv->IConnector.pbData = pAddress;
2535 mpDrv->IConnector.cbScanline = ulBytesPerLine;
2536 mpDrv->IConnector.cBits = ulBitsPerPixel;
2537 mpDrv->IConnector.cx = ulWidth;
2538 mpDrv->IConnector.cy = ulHeight;
2539
2540 fSetRenderVRAM = pFBInfo->fDefaultFormat;
2541 }
2542
2543 /* Make sure that the bitmap contains the latest image. */
2544 fInvalidate = pFBInfo->fDefaultFormat;
2545 }
2546 }
2547
2548 if (SUCCEEDED(hr))
2549 {
2550 pFBInfo->pSourceBitmap.queryInterfaceTo(aDisplaySourceBitmap.asOutParam());
2551 }
2552
2553 /* Leave the IDisplay lock because the VGA device must not be called under it. */
2554 alock.release();
2555
2556 if (SUCCEEDED(hr))
2557 {
2558 if (fSetRenderVRAM)
2559 {
2560 mpDrv->pUpPort->pfnSetRenderVRAM(mpDrv->pUpPort, true);
2561 }
2562
2563 if (fInvalidate)
2564 VMR3ReqCallWaitU(ptrVM.rawUVM(), VMCPUID_ANY, (PFNRT)Display::i_InvalidateAndUpdateEMT,
2565 3, this, aScreenId, false);
2566 }
2567
2568 LogRelFlowFunc(("%Rhrc\n", hr));
2569 return hr;
2570}
2571
2572HRESULT Display::getGuestScreenLayout(std::vector<ComPtr<IGuestScreenInfo> > &aGuestScreenLayout)
2573{
2574 NOREF(aGuestScreenLayout);
2575 return E_NOTIMPL;
2576}
2577
2578HRESULT Display::setScreenLayout(ScreenLayoutMode_T aScreenLayoutMode,
2579 const std::vector<ComPtr<IGuestScreenInfo> > &aGuestScreenInfo)
2580{
2581 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2582
2583 if (aGuestScreenInfo.size() != mcMonitors)
2584 return E_INVALIDARG;
2585
2586 CHECK_CONSOLE_DRV(mpDrv);
2587
2588 /*
2589 * It is up to the guest to decide whether the hint is
2590 * valid. Therefore don't do any VRAM sanity checks here.
2591 */
2592
2593 /* Have to release the lock because the pfnRequestDisplayChange
2594 * will call EMT. */
2595 alock.release();
2596
2597 VMMDev *pVMMDev = mParent->i_getVMMDev();
2598 if (pVMMDev)
2599 {
2600 PPDMIVMMDEVPORT pVMMDevPort = pVMMDev->getVMMDevPort();
2601 if (pVMMDevPort)
2602 {
2603 uint32_t const cDisplays = (uint32_t)aGuestScreenInfo.size();
2604
2605 size_t const cbAlloc = cDisplays * sizeof(VMMDevDisplayDef);
2606 VMMDevDisplayDef *paDisplayDefs = (VMMDevDisplayDef *)RTMemAlloc(cbAlloc);
2607 if (paDisplayDefs)
2608 {
2609 for (uint32_t i = 0; i < cDisplays; ++i)
2610 {
2611 VMMDevDisplayDef *p = &paDisplayDefs[i];
2612 ComPtr<IGuestScreenInfo> pScreenInfo = aGuestScreenInfo[i];
2613
2614 ULONG screenId = 0;
2615 GuestMonitorStatus_T guestMonitorStatus = GuestMonitorStatus_Enabled;
2616 BOOL origin = FALSE;
2617 BOOL primary = FALSE;
2618 LONG originX = 0;
2619 LONG originY = 0;
2620 ULONG width = 0;
2621 ULONG height = 0;
2622 ULONG bitsPerPixel = 0;
2623
2624 pScreenInfo->COMGETTER(ScreenId) (&screenId);
2625 pScreenInfo->COMGETTER(GuestMonitorStatus)(&guestMonitorStatus);
2626 pScreenInfo->COMGETTER(Primary) (&primary);
2627 pScreenInfo->COMGETTER(Origin) (&origin);
2628 pScreenInfo->COMGETTER(OriginX) (&originX);
2629 pScreenInfo->COMGETTER(OriginY) (&originY);
2630 pScreenInfo->COMGETTER(Width) (&width);
2631 pScreenInfo->COMGETTER(Height) (&height);
2632 pScreenInfo->COMGETTER(BitsPerPixel)(&bitsPerPixel);
2633
2634 LogFlowFunc(("%d %d,%d %dx%d\n", screenId, originX, originY, width, height));
2635
2636 p->idDisplay = screenId;
2637 p->xOrigin = originX;
2638 p->yOrigin = originY;
2639 p->cx = width;
2640 p->cy = height;
2641 p->cBitsPerPixel = bitsPerPixel;
2642 p->fDisplayFlags = VMMDEV_DISPLAY_CX | VMMDEV_DISPLAY_CY | VMMDEV_DISPLAY_BPP;
2643 if (guestMonitorStatus == GuestMonitorStatus_Disabled)
2644 p->fDisplayFlags |= VMMDEV_DISPLAY_DISABLED;
2645 if (origin)
2646 p->fDisplayFlags |= VMMDEV_DISPLAY_ORIGIN;
2647 if (primary)
2648 p->fDisplayFlags |= VMMDEV_DISPLAY_PRIMARY;
2649 }
2650
2651 bool const fForce = aScreenLayoutMode == ScreenLayoutMode_Reset
2652 || aScreenLayoutMode == ScreenLayoutMode_Apply;
2653 bool const fNotify = aScreenLayoutMode != ScreenLayoutMode_Silent;
2654 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, cDisplays, paDisplayDefs, fForce, fNotify);
2655
2656 RTMemFree(paDisplayDefs);
2657 }
2658 }
2659 }
2660 return S_OK;
2661}
2662
2663HRESULT Display::detachScreens(const std::vector<LONG> &aScreenIds)
2664{
2665 NOREF(aScreenIds);
2666 return E_NOTIMPL;
2667}
2668
2669HRESULT Display::createGuestScreenInfo(ULONG aDisplay,
2670 GuestMonitorStatus_T aStatus,
2671 BOOL aPrimary,
2672 BOOL aChangeOrigin,
2673 LONG aOriginX,
2674 LONG aOriginY,
2675 ULONG aWidth,
2676 ULONG aHeight,
2677 ULONG aBitsPerPixel,
2678 ComPtr<IGuestScreenInfo> &aGuestScreenInfo)
2679{
2680 /* Create a new object. */
2681 ComObjPtr<GuestScreenInfo> obj;
2682 HRESULT hr = obj.createObject();
2683 if (SUCCEEDED(hr))
2684 hr = obj->init(aDisplay, aStatus, aPrimary, aChangeOrigin, aOriginX, aOriginY,
2685 aWidth, aHeight, aBitsPerPixel);
2686 if (SUCCEEDED(hr))
2687 obj.queryInterfaceTo(aGuestScreenInfo.asOutParam());
2688
2689 return hr;
2690}
2691
2692
2693/*
2694 * GuestScreenInfo implementation.
2695 */
2696DEFINE_EMPTY_CTOR_DTOR(GuestScreenInfo)
2697
2698HRESULT GuestScreenInfo::FinalConstruct()
2699{
2700 return BaseFinalConstruct();
2701}
2702
2703void GuestScreenInfo::FinalRelease()
2704{
2705 uninit();
2706
2707 BaseFinalRelease();
2708}
2709
2710HRESULT GuestScreenInfo::init(ULONG aDisplay,
2711 GuestMonitorStatus_T aGuestMonitorStatus,
2712 BOOL aPrimary,
2713 BOOL aChangeOrigin,
2714 LONG aOriginX,
2715 LONG aOriginY,
2716 ULONG aWidth,
2717 ULONG aHeight,
2718 ULONG aBitsPerPixel)
2719{
2720 LogFlowThisFunc(("[%u]\n", aDisplay));
2721
2722 /* Enclose the state transition NotReady->InInit->Ready */
2723 AutoInitSpan autoInitSpan(this);
2724 AssertReturn(autoInitSpan.isOk(), E_FAIL);
2725
2726 mScreenId = aDisplay;
2727 mGuestMonitorStatus = aGuestMonitorStatus;
2728 mPrimary = aPrimary;
2729 mOrigin = aChangeOrigin;
2730 mOriginX = aOriginX;
2731 mOriginY = aOriginY;
2732 mWidth = aWidth;
2733 mHeight = aHeight;
2734 mBitsPerPixel = aBitsPerPixel;
2735
2736 /* Confirm a successful initialization */
2737 autoInitSpan.setSucceeded();
2738
2739 return S_OK;
2740}
2741
2742void GuestScreenInfo::uninit()
2743{
2744 /* Enclose the state transition Ready->InUninit->NotReady */
2745 AutoUninitSpan autoUninitSpan(this);
2746 if (autoUninitSpan.uninitDone())
2747 return;
2748
2749 LogFlowThisFunc(("[%u]\n", mScreenId));
2750}
2751
2752HRESULT GuestScreenInfo::getScreenId(ULONG *aScreenId)
2753{
2754 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2755 *aScreenId = mScreenId;
2756 return S_OK;
2757}
2758
2759HRESULT GuestScreenInfo::getGuestMonitorStatus(GuestMonitorStatus_T *aGuestMonitorStatus)
2760{
2761 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2762 *aGuestMonitorStatus = mGuestMonitorStatus;
2763 return S_OK;
2764}
2765
2766HRESULT GuestScreenInfo::getPrimary(BOOL *aPrimary)
2767{
2768 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2769 *aPrimary = mPrimary;
2770 return S_OK;
2771}
2772
2773HRESULT GuestScreenInfo::getOrigin(BOOL *aOrigin)
2774{
2775 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2776 *aOrigin = mOrigin;
2777 return S_OK;
2778}
2779
2780HRESULT GuestScreenInfo::getOriginX(LONG *aOriginX)
2781{
2782 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2783 *aOriginX = mOriginX;
2784 return S_OK;
2785}
2786
2787HRESULT GuestScreenInfo::getOriginY(LONG *aOriginY)
2788{
2789 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2790 *aOriginY = mOriginY;
2791 return S_OK;
2792}
2793
2794HRESULT GuestScreenInfo::getWidth(ULONG *aWidth)
2795{
2796 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2797 *aWidth = mWidth;
2798 return S_OK;
2799}
2800
2801HRESULT GuestScreenInfo::getHeight(ULONG *aHeight)
2802{
2803 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2804 *aHeight = mHeight;
2805 return S_OK;
2806}
2807
2808HRESULT GuestScreenInfo::getBitsPerPixel(ULONG *aBitsPerPixel)
2809{
2810 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2811 *aBitsPerPixel = mBitsPerPixel;
2812 return S_OK;
2813}
2814
2815HRESULT GuestScreenInfo::getExtendedInfo(com::Utf8Str &aExtendedInfo)
2816{
2817 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
2818 aExtendedInfo = com::Utf8Str();
2819 return S_OK;
2820}
2821
2822// wrapped IEventListener method
2823HRESULT Display::handleEvent(const ComPtr<IEvent> &aEvent)
2824{
2825 VBoxEventType_T aType = VBoxEventType_Invalid;
2826
2827 aEvent->COMGETTER(Type)(&aType);
2828 switch (aType)
2829 {
2830 case VBoxEventType_OnStateChanged:
2831 {
2832 ComPtr<IStateChangedEvent> scev = aEvent;
2833 Assert(scev);
2834 MachineState_T machineState;
2835 scev->COMGETTER(State)(&machineState);
2836 if ( machineState == MachineState_Running
2837 || machineState == MachineState_Teleporting
2838 || machineState == MachineState_LiveSnapshotting
2839 || machineState == MachineState_DeletingSnapshotOnline
2840 )
2841 {
2842 LogRelFlowFunc(("Machine is running.\n"));
2843
2844 }
2845 break;
2846 }
2847 default:
2848 AssertFailed();
2849 }
2850
2851 return S_OK;
2852}
2853
2854
2855// private methods
2856/////////////////////////////////////////////////////////////////////////////
2857
2858/**
2859 * Handle display resize event issued by the VGA device for the primary screen.
2860 *
2861 * @see PDMIDISPLAYCONNECTOR::pfnResize
2862 */
2863DECLCALLBACK(int) Display::i_displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
2864 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
2865{
2866 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2867 Display *pThis = pDrv->pDisplay;
2868
2869 LogRelFlowFunc(("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
2870 bpp, pvVRAM, cbLine, cx, cy));
2871
2872 bool f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, true, false);
2873 if (!f)
2874 {
2875 /* This is a result of recursive call when the source bitmap is being updated
2876 * during a VGA resize. Tell the VGA device to ignore the call.
2877 *
2878 * @todo It is a workaround, actually pfnUpdateDisplayAll must
2879 * fail on resize.
2880 */
2881 LogRel(("displayResizeCallback: already processing\n"));
2882 return VINF_VGA_RESIZE_IN_PROGRESS;
2883 }
2884
2885 int rc = pThis->i_handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy, 0, 0, 0, true);
2886
2887 /* Restore the flag. */
2888 f = ASMAtomicCmpXchgBool(&pThis->fVGAResizing, false, true);
2889 AssertRelease(f);
2890
2891 return rc;
2892}
2893
2894/**
2895 * Handle display update.
2896 *
2897 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
2898 */
2899DECLCALLBACK(void) Display::i_displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
2900 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
2901{
2902 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2903
2904#ifdef DEBUG_sunlover
2905 LogFlowFunc(("fVideoAccelEnabled = %d, %d,%d %dx%d\n",
2906 pDrv->pDisplay->mVideoAccelLegacy.fVideoAccelEnabled, x, y, cx, cy));
2907#endif /* DEBUG_sunlover */
2908
2909 /* This call does update regardless of VBVA status.
2910 * But in VBVA mode this is called only as result of
2911 * pfnUpdateDisplayAll in the VGA device.
2912 */
2913
2914 pDrv->pDisplay->i_handleDisplayUpdate(VBOX_VIDEO_PRIMARY_SCREEN, x, y, cx, cy);
2915}
2916
2917/**
2918 * Periodic display refresh callback.
2919 *
2920 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
2921 * @thread EMT
2922 */
2923/*static*/ DECLCALLBACK(void) Display::i_displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
2924{
2925 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2926
2927#ifdef DEBUG_sunlover_2
2928 LogFlowFunc(("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
2929 pDrv->pDisplay->mfVideoAccelEnabled));
2930#endif /* DEBUG_sunlover_2 */
2931
2932 Display *pDisplay = pDrv->pDisplay;
2933 unsigned uScreenId;
2934
2935 int rc = pDisplay->i_videoAccelRefreshProcess(pDrv->pUpPort);
2936 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
2937 {
2938 if (rc == VWRN_INVALID_STATE)
2939 {
2940 /* No VBVA do a display update. */
2941 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
2942 }
2943
2944 /* Inform the VRDP server that the current display update sequence is
2945 * completed. At this moment the framebuffer memory contains a definite
2946 * image, that is synchronized with the orders already sent to VRDP client.
2947 * The server can now process redraw requests from clients or initial
2948 * fullscreen updates for new clients.
2949 */
2950 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2951 {
2952 Assert(pDisplay->mParent && pDisplay->mParent->i_consoleVRDPServer());
2953 pDisplay->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, NULL, 0);
2954 }
2955 }
2956
2957#ifdef VBOX_WITH_RECORDING
2958 AssertPtr(pDisplay->mParent);
2959 RecordingContext *pCtx = pDisplay->mParent->i_recordingGetContext();
2960
2961 if ( pCtx
2962 && pCtx->IsStarted()
2963 && pCtx->IsFeatureEnabled(RecordingFeature_Video))
2964 {
2965 do
2966 {
2967 /* If the recording context has reached the configured recording
2968 * limit, disable recording. */
2969 if (pCtx->IsLimitReached())
2970 {
2971 pDisplay->mParent->i_onRecordingChange(FALSE /* Disable */);
2972 break;
2973 }
2974
2975 uint64_t tsNowMs = RTTimeProgramMilliTS();
2976 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2977 {
2978 if (!pDisplay->maRecordingEnabled[uScreenId])
2979 continue;
2980
2981 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2982 if (!pFBInfo->fDisabled)
2983 {
2984 ComPtr<IDisplaySourceBitmap> pSourceBitmap;
2985 int rc2 = RTCritSectEnter(&pDisplay->mVideoRecLock);
2986 if (RT_SUCCESS(rc2))
2987 {
2988 pSourceBitmap = pFBInfo->Recording.pSourceBitmap;
2989 RTCritSectLeave(&pDisplay->mVideoRecLock);
2990 }
2991
2992 if (!pSourceBitmap.isNull())
2993 {
2994 BYTE *pbAddress = NULL;
2995 ULONG ulWidth = 0;
2996 ULONG ulHeight = 0;
2997 ULONG ulBitsPerPixel = 0;
2998 ULONG ulBytesPerLine = 0;
2999 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
3000 HRESULT hr = pSourceBitmap->QueryBitmapInfo(&pbAddress,
3001 &ulWidth,
3002 &ulHeight,
3003 &ulBitsPerPixel,
3004 &ulBytesPerLine,
3005 &bitmapFormat);
3006 if (SUCCEEDED(hr) && pbAddress)
3007 rc = pCtx->SendVideoFrame(uScreenId, 0, 0, BitmapFormat_BGR,
3008 ulBitsPerPixel, ulBytesPerLine, ulWidth, ulHeight,
3009 pbAddress, tsNowMs);
3010 else
3011 rc = VERR_NOT_SUPPORTED;
3012
3013 pSourceBitmap.setNull();
3014 }
3015 else
3016 rc = VERR_NOT_SUPPORTED;
3017
3018 if (rc == VINF_TRY_AGAIN)
3019 break;
3020 }
3021 }
3022 } while (0);
3023 }
3024#endif /* VBOX_WITH_RECORDING */
3025
3026#ifdef DEBUG_sunlover_2
3027 LogFlowFunc(("leave\n"));
3028#endif /* DEBUG_sunlover_2 */
3029}
3030
3031/**
3032 * Reset notification
3033 *
3034 * @see PDMIDISPLAYCONNECTOR::pfnReset
3035 */
3036DECLCALLBACK(void) Display::i_displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3037{
3038 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3039
3040 LogRelFlowFunc(("\n"));
3041
3042 /* Disable VBVA mode. */
3043 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3044}
3045
3046/**
3047 * LFBModeChange notification
3048 *
3049 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3050 */
3051DECLCALLBACK(void) Display::i_displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3052{
3053 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3054
3055 LogRelFlowFunc(("fEnabled=%d\n", fEnabled));
3056
3057 NOREF(fEnabled);
3058
3059 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3060 pDrv->pDisplay->VideoAccelEnableVGA(false, NULL);
3061}
3062
3063/**
3064 * Adapter information change notification.
3065 *
3066 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3067 */
3068DECLCALLBACK(void) Display::i_displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM,
3069 uint32_t u32VRAMSize)
3070{
3071 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3072 pDrv->pDisplay->processAdapterData(pvVRAM, u32VRAMSize);
3073}
3074
3075/**
3076 * Display information change notification.
3077 *
3078 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3079 */
3080DECLCALLBACK(void) Display::i_displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface,
3081 void *pvVRAM, unsigned uScreenId)
3082{
3083 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3084 pDrv->pDisplay->processDisplayData(pvVRAM, uScreenId);
3085}
3086
3087#ifdef VBOX_WITH_VIDEOHWACCEL
3088
3089int Display::i_handleVHWACommandProcess(int enmCmd, bool fGuestCmd, VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand)
3090{
3091 unsigned id = (unsigned)pCommand->iDisplay;
3092 if (id >= mcMonitors)
3093 return VERR_INVALID_PARAMETER;
3094
3095 ComPtr<IFramebuffer> pFramebuffer;
3096 AutoReadLock arlock(this COMMA_LOCKVAL_SRC_POS);
3097 pFramebuffer = maFramebuffers[id].pFramebuffer;
3098 bool fVHWASupported = RT_BOOL(maFramebuffers[id].u32Caps & FramebufferCapabilities_VHWA);
3099 arlock.release();
3100
3101 if (pFramebuffer == NULL || !fVHWASupported)
3102 return VERR_NOT_IMPLEMENTED; /* Implementation is not available. */
3103
3104 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE *)pCommand, enmCmd, fGuestCmd);
3105 if (hr == S_FALSE)
3106 return VINF_SUCCESS;
3107 if (SUCCEEDED(hr))
3108 return VINF_CALLBACK_RETURN;
3109 if (hr == E_ACCESSDENIED)
3110 return VERR_INVALID_STATE; /* notify we can not handle request atm */
3111 if (hr == E_NOTIMPL)
3112 return VERR_NOT_IMPLEMENTED;
3113 return VERR_GENERAL_FAILURE;
3114}
3115
3116DECLCALLBACK(int) Display::i_displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, int enmCmd, bool fGuestCmd,
3117 VBOXVHWACMD RT_UNTRUSTED_VOLATILE_GUEST *pCommand)
3118{
3119 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3120
3121 return pDrv->pDisplay->i_handleVHWACommandProcess(enmCmd, fGuestCmd, pCommand);
3122}
3123
3124#endif /* VBOX_WITH_VIDEOHWACCEL */
3125
3126HRESULT Display::notifyScaleFactorChange(ULONG aScreenId, ULONG aScaleFactorWMultiplied, ULONG aScaleFactorHMultiplied)
3127{
3128 RT_NOREF(aScreenId, aScaleFactorWMultiplied, aScaleFactorHMultiplied);
3129# if 0 /** @todo Thank you so very much from anyone using VMSVGA3d! */
3130 AssertMsgFailed(("Attempt to specify OpenGL content scale factor while 3D acceleration is disabled in VM config. Ignored.\n"));
3131# else
3132 /* Need an interface like this here (and the #ifdefs needs adjusting):
3133 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3134 if (pUpPort && pUpPort->pfnSetScaleFactor)
3135 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3136# endif
3137 return S_OK;
3138}
3139
3140HRESULT Display::notifyHiDPIOutputPolicyChange(BOOL fUnscaledHiDPI)
3141{
3142 RT_NOREF(fUnscaledHiDPI);
3143
3144 /* Need an interface like this here (and the #ifdefs needs adjusting):
3145 PPDMIDISPLAYPORT pUpPort = mpDrv ? mpDrv->pUpPort : NULL;
3146 if (pUpPort && pUpPort->pfnSetScaleFactor)
3147 pUpPort->pfnSetScaleFactor(pUpPort, aScreeId, aScaleFactorWMultiplied, aScaleFactorHMultiplied); */
3148
3149 return S_OK;
3150}
3151
3152#ifdef VBOX_WITH_HGSMI
3153/**
3154 * @interface_method_impl{PDMIDISPLAYCONNECTOR,pfnVBVAEnable}
3155 */
3156DECLCALLBACK(int) Display::i_displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
3157 VBVAHOSTFLAGS RT_UNTRUSTED_VOLATILE_GUEST *pHostFlags)
3158{
3159 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3160
3161 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3162 Display *pThis = pDrv->pDisplay;
3163
3164 if (pThis->maFramebuffers[uScreenId].fVBVAEnabled)
3165 {
3166 LogRel(("Enabling different vbva mode\n"));
3167#ifdef DEBUG_misha
3168 AssertMsgFailed(("enabling different vbva mode\n"));
3169#endif
3170 return VERR_INVALID_STATE;
3171 }
3172
3173 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3174 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3175 pThis->maFramebuffers[uScreenId].fVBVAForceResize = true;
3176
3177 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3178
3179 return VINF_SUCCESS;
3180}
3181
3182/**
3183 * @interface_method_impl{PDMIDISPLAYCONNECTOR,pfnVBVADisable}
3184 */
3185DECLCALLBACK(void) Display::i_displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3186{
3187 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3188
3189 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3190 Display *pThis = pDrv->pDisplay;
3191
3192 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3193
3194 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3195 {
3196 /* Make sure that the primary screen is visible now.
3197 * The guest can't use VBVA anymore, so only only the VGA device output works.
3198 */
3199 pFBInfo->flags = 0;
3200 if (pFBInfo->fDisabled)
3201 {
3202 pFBInfo->fDisabled = false;
3203 fireGuestMonitorChangedEvent(pThis->mParent->i_getEventSource(),
3204 GuestMonitorChangedEventType_Enabled,
3205 uScreenId,
3206 pFBInfo->xOrigin, pFBInfo->yOrigin,
3207 pFBInfo->w, pFBInfo->h);
3208 }
3209 }
3210
3211 pFBInfo->fVBVAEnabled = false;
3212 pFBInfo->fVBVAForceResize = false;
3213
3214 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3215
3216 pFBInfo->pVBVAHostFlags = NULL;
3217
3218 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3219 {
3220 /* Force full screen update, because VGA device must take control, do resize, etc. */
3221 pThis->mpDrv->pUpPort->pfnUpdateDisplayAll(pThis->mpDrv->pUpPort, /* fFailOnResize = */ false);
3222 }
3223}
3224
3225DECLCALLBACK(void) Display::i_displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3226{
3227 RT_NOREF(uScreenId);
3228 LogFlowFunc(("uScreenId %d\n", uScreenId));
3229
3230 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3231 Display *pThis = pDrv->pDisplay;
3232
3233 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3234 {
3235 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers,
3236 pThis->mcMonitors);
3237 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3238 }
3239}
3240
3241/**
3242 * @interface_method_impl{PDMIDISPLAYCONNECTOR,pfnVBVAUpdateProcess}
3243 */
3244DECLCALLBACK(void) Display::i_displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId,
3245 struct VBVACMDHDR const RT_UNTRUSTED_VOLATILE_GUEST *pCmd, size_t cbCmd)
3246{
3247 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3248 VBVACMDHDR hdrSaved;
3249 RT_COPY_VOLATILE(hdrSaved, *pCmd);
3250 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE();
3251
3252 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3253 Display *pThis = pDrv->pDisplay;
3254 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3255
3256 if (pFBInfo->fDefaultFormat)
3257 {
3258 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3259 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3260 && !pFBInfo->fDisabled)
3261 {
3262 pDrv->pUpPort->pfnUpdateDisplayRect(pDrv->pUpPort, hdrSaved.x, hdrSaved.y, hdrSaved.w, hdrSaved.h);
3263 }
3264 else if ( !pFBInfo->pSourceBitmap.isNull()
3265 && !pFBInfo->fDisabled)
3266 {
3267 /* Render VRAM content to the framebuffer. */
3268 BYTE *pAddress = NULL;
3269 ULONG ulWidth = 0;
3270 ULONG ulHeight = 0;
3271 ULONG ulBitsPerPixel = 0;
3272 ULONG ulBytesPerLine = 0;
3273 BitmapFormat_T bitmapFormat = BitmapFormat_Opaque;
3274
3275 HRESULT hrc = pFBInfo->pSourceBitmap->QueryBitmapInfo(&pAddress,
3276 &ulWidth,
3277 &ulHeight,
3278 &ulBitsPerPixel,
3279 &ulBytesPerLine,
3280 &bitmapFormat);
3281 if (SUCCEEDED(hrc))
3282 {
3283 uint32_t width = hdrSaved.w;
3284 uint32_t height = hdrSaved.h;
3285
3286 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3287 int32_t xSrc = hdrSaved.x - pFBInfo->xOrigin;
3288 int32_t ySrc = hdrSaved.y - pFBInfo->yOrigin;
3289 uint32_t u32SrcWidth = pFBInfo->w;
3290 uint32_t u32SrcHeight = pFBInfo->h;
3291 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3292 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3293
3294 uint8_t *pu8Dst = pAddress;
3295 int32_t xDst = xSrc;
3296 int32_t yDst = ySrc;
3297 uint32_t u32DstWidth = u32SrcWidth;
3298 uint32_t u32DstHeight = u32SrcHeight;
3299 uint32_t u32DstLineSize = u32DstWidth * 4;
3300 uint32_t u32DstBitsPerPixel = 32;
3301
3302 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3303 width, height,
3304 pu8Src,
3305 xSrc, ySrc,
3306 u32SrcWidth, u32SrcHeight,
3307 u32SrcLineSize, u32SrcBitsPerPixel,
3308 pu8Dst,
3309 xDst, yDst,
3310 u32DstWidth, u32DstHeight,
3311 u32DstLineSize, u32DstBitsPerPixel);
3312 }
3313 }
3314 }
3315
3316 /*
3317 * Here is your classic 'temporary' solution.
3318 */
3319 /** @todo New SendUpdate entry which can get a separate cmd header or coords. */
3320 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3321
3322 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3323 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3324
3325 pThis->mParent->i_consoleVRDPServer()->SendUpdate(uScreenId, pHdrUnconst, (uint32_t)cbCmd);
3326
3327 *pHdrUnconst = hdrSaved;
3328}
3329
3330DECLCALLBACK(void) Display::i_displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y,
3331 uint32_t cx, uint32_t cy)
3332{
3333 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3334
3335 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3336 Display *pThis = pDrv->pDisplay;
3337 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3338
3339 /** @todo handleFramebufferUpdate (uScreenId,
3340 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3341 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3342 * cx, cy);
3343 */
3344 pThis->i_handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3345}
3346
3347#ifdef DEBUG_sunlover
3348static void logVBVAResize(PCVBVAINFOVIEW pView, PCVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
3349{
3350 LogRel(("displayVBVAResize: [%d] %s\n"
3351 " pView->u32ViewIndex %d\n"
3352 " pView->u32ViewOffset 0x%08X\n"
3353 " pView->u32ViewSize 0x%08X\n"
3354 " pView->u32MaxScreenSize 0x%08X\n"
3355 " pScreen->i32OriginX %d\n"
3356 " pScreen->i32OriginY %d\n"
3357 " pScreen->u32StartOffset 0x%08X\n"
3358 " pScreen->u32LineSize 0x%08X\n"
3359 " pScreen->u32Width %d\n"
3360 " pScreen->u32Height %d\n"
3361 " pScreen->u16BitsPerPixel %d\n"
3362 " pScreen->u16Flags 0x%04X\n"
3363 " pFBInfo->u32Offset 0x%08X\n"
3364 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
3365 " pFBInfo->u32InformationSize 0x%08X\n"
3366 " pFBInfo->fDisabled %d\n"
3367 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
3368 " pFBInfo->u16BitsPerPixel %d\n"
3369 " pFBInfo->pu8FramebufferVRAM %p\n"
3370 " pFBInfo->u32LineSize 0x%08X\n"
3371 " pFBInfo->flags 0x%04X\n"
3372 " pFBInfo->pHostEvents %p\n"
3373 " pFBInfo->fDefaultFormat %d\n"
3374 " pFBInfo->fVBVAEnabled %d\n"
3375 " pFBInfo->fVBVAForceResize %d\n"
3376 " pFBInfo->pVBVAHostFlags %p\n"
3377 "",
3378 pScreen->u32ViewIndex,
3379 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
3380 pView->u32ViewIndex,
3381 pView->u32ViewOffset,
3382 pView->u32ViewSize,
3383 pView->u32MaxScreenSize,
3384 pScreen->i32OriginX,
3385 pScreen->i32OriginY,
3386 pScreen->u32StartOffset,
3387 pScreen->u32LineSize,
3388 pScreen->u32Width,
3389 pScreen->u32Height,
3390 pScreen->u16BitsPerPixel,
3391 pScreen->u16Flags,
3392 pFBInfo->u32Offset,
3393 pFBInfo->u32MaxFramebufferSize,
3394 pFBInfo->u32InformationSize,
3395 pFBInfo->fDisabled,
3396 pFBInfo->xOrigin,
3397 pFBInfo->yOrigin,
3398 pFBInfo->w,
3399 pFBInfo->h,
3400 pFBInfo->u16BitsPerPixel,
3401 pFBInfo->pu8FramebufferVRAM,
3402 pFBInfo->u32LineSize,
3403 pFBInfo->flags,
3404 pFBInfo->pHostEvents,
3405 pFBInfo->fDefaultFormat,
3406 pFBInfo->fVBVAEnabled,
3407 pFBInfo->fVBVAForceResize,
3408 pFBInfo->pVBVAHostFlags
3409 ));
3410}
3411#endif /* DEBUG_sunlover */
3412
3413DECLCALLBACK(int) Display::i_displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, PCVBVAINFOVIEW pView,
3414 PCVBVAINFOSCREEN pScreen, void *pvVRAM, bool fResetInputMapping)
3415{
3416 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3417
3418 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3419 Display *pThis = pDrv->pDisplay;
3420
3421 return pThis->processVBVAResize(pView, pScreen, pvVRAM, fResetInputMapping);
3422}
3423
3424int Display::processVBVAResize(PCVBVAINFOVIEW pView, PCVBVAINFOSCREEN pScreen, void *pvVRAM, bool fResetInputMapping)
3425{
3426 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
3427
3428 RT_NOREF(pView);
3429
3430 DISPLAYFBINFO *pFBInfo = &maFramebuffers[pScreen->u32ViewIndex];
3431
3432#ifdef DEBUG_sunlover
3433 logVBVAResize(pView, pScreen, pFBInfo);
3434#endif
3435
3436 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
3437 {
3438 /* Ask the framebuffer to resize using a default format. The framebuffer will be black.
3439 * So if the frontend does not support GuestMonitorChangedEventType_Disabled event,
3440 * the VM window will be black. */
3441 uint32_t u32Width = pFBInfo->w ? pFBInfo->w : 640;
3442 uint32_t u32Height = pFBInfo->h ? pFBInfo->h : 480;
3443 int32_t xOrigin = pFBInfo->xOrigin;
3444 int32_t yOrigin = pFBInfo->yOrigin;
3445
3446 alock.release();
3447
3448 i_handleDisplayResize(pScreen->u32ViewIndex, 0, (uint8_t *)NULL, 0,
3449 u32Width, u32Height, pScreen->u16Flags, xOrigin, yOrigin, false);
3450
3451 return VINF_SUCCESS;
3452 }
3453
3454 VBVAINFOSCREEN screenInfo;
3455 RT_ZERO(screenInfo);
3456
3457 if (pScreen->u16Flags & VBVA_SCREEN_F_BLANK2)
3458 {
3459 /* Init a local VBVAINFOSCREEN structure, which will be used instead of
3460 * the original pScreen. Set VBVA_SCREEN_F_BLANK, which will force
3461 * the code below to choose the "blanking" branches.
3462 */
3463 screenInfo.u32ViewIndex = pScreen->u32ViewIndex;
3464 screenInfo.i32OriginX = pFBInfo->xOrigin;
3465 screenInfo.i32OriginY = pFBInfo->yOrigin;
3466 screenInfo.u32StartOffset = 0; /* Irrelevant */
3467 screenInfo.u32LineSize = pFBInfo->u32LineSize;
3468 screenInfo.u32Width = pFBInfo->w;
3469 screenInfo.u32Height = pFBInfo->h;
3470 screenInfo.u16BitsPerPixel = pFBInfo->u16BitsPerPixel;
3471 screenInfo.u16Flags = pScreen->u16Flags | VBVA_SCREEN_F_BLANK;
3472
3473 pScreen = &screenInfo;
3474 }
3475
3476 if (fResetInputMapping)
3477 {
3478 /// @todo Rename to m* and verify whether some kind of lock is required.
3479 xInputMappingOrigin = 0;
3480 yInputMappingOrigin = 0;
3481 cxInputMapping = 0;
3482 cyInputMapping = 0;
3483 }
3484
3485 alock.release();
3486
3487 return i_handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
3488 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
3489 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags,
3490 pScreen->i32OriginX, pScreen->i32OriginY, false);
3491}
3492
3493DECLCALLBACK(int) Display::i_displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
3494 uint32_t xHot, uint32_t yHot,
3495 uint32_t cx, uint32_t cy,
3496 const void *pvShape)
3497{
3498 LogFlowFunc(("\n"));
3499 LogRel2(("%s: fVisible=%RTbool\n", __PRETTY_FUNCTION__, fVisible));
3500
3501 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3502
3503 uint32_t cbShape = 0;
3504 if (pvShape)
3505 {
3506 cbShape = (cx + 7) / 8 * cy; /* size of the AND mask */
3507 cbShape = ((cbShape + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
3508 }
3509
3510 /* Tell the console about it */
3511 pDrv->pDisplay->mParent->i_onMousePointerShapeChange(fVisible, fAlpha,
3512 xHot, yHot, cx, cy, (uint8_t *)pvShape, cbShape);
3513
3514 return VINF_SUCCESS;
3515}
3516
3517DECLCALLBACK(void) Display::i_displayVBVAGuestCapabilityUpdate(PPDMIDISPLAYCONNECTOR pInterface, uint32_t fCapabilities)
3518{
3519 LogFlowFunc(("\n"));
3520
3521 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3522 Display *pThis = pDrv->pDisplay;
3523
3524 pThis->i_handleUpdateGuestVBVACapabilities(fCapabilities);
3525}
3526
3527DECLCALLBACK(void) Display::i_displayVBVAInputMappingUpdate(PPDMIDISPLAYCONNECTOR pInterface, int32_t xOrigin, int32_t yOrigin,
3528 uint32_t cx, uint32_t cy)
3529{
3530 LogFlowFunc(("\n"));
3531
3532 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3533 Display *pThis = pDrv->pDisplay;
3534
3535 pThis->i_handleUpdateVBVAInputMapping(xOrigin, yOrigin, cx, cy);
3536}
3537
3538DECLCALLBACK(void) Display::i_displayVBVAReportCursorPosition(PPDMIDISPLAYCONNECTOR pInterface, uint32_t fFlags, uint32_t aScreenId, uint32_t x, uint32_t y)
3539{
3540 LogFlowFunc(("\n"));
3541 LogRel2(("%s: fFlags=%RU32, aScreenId=%RU32, x=%RU32, y=%RU32\n",
3542 __PRETTY_FUNCTION__, fFlags, aScreenId, x, y));
3543
3544 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3545 Display *pThis = pDrv->pDisplay;
3546
3547 if (fFlags & VBVA_CURSOR_SCREEN_RELATIVE)
3548 {
3549 x += pThis->maFramebuffers[aScreenId].xOrigin;
3550 y += pThis->maFramebuffers[aScreenId].yOrigin;
3551 }
3552 fireCursorPositionChangedEvent(pThis->mParent->i_getEventSource(), RT_BOOL(fFlags & VBVA_CURSOR_VALID_DATA), x, y);
3553}
3554
3555#endif /* VBOX_WITH_HGSMI */
3556
3557/**
3558 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3559 */
3560DECLCALLBACK(void *) Display::i_drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3561{
3562 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
3563 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3564 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
3565 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
3566 return NULL;
3567}
3568
3569
3570/**
3571 * @interface_method_impl{PDMDRVREG,pfnPowerOff,
3572 * Tries to ensure no client calls gets to HGCM or the VGA device from here on.}
3573 */
3574DECLCALLBACK(void) Display::i_drvPowerOff(PPDMDRVINS pDrvIns)
3575{
3576 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3577 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
3578
3579 /*
3580 * Do much of the work that i_drvDestruct does.
3581 */
3582 if (pThis->pUpPort)
3583 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
3584
3585 pThis->IConnector.pbData = NULL;
3586 pThis->IConnector.cbScanline = 0;
3587 pThis->IConnector.cBits = 32;
3588 pThis->IConnector.cx = 0;
3589 pThis->IConnector.cy = 0;
3590
3591 if (pThis->pDisplay)
3592 {
3593 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
3594#ifdef VBOX_WITH_RECORDING
3595 pThis->pDisplay->mParent->i_recordingStop();
3596#endif
3597#if defined(VBOX_WITH_VIDEOHWACCEL)
3598 pThis->pVBVACallbacks = NULL;
3599#endif
3600 }
3601}
3602
3603
3604/**
3605 * Destruct a display driver instance.
3606 *
3607 * @returns VBox status code.
3608 * @param pDrvIns The driver instance data.
3609 */
3610DECLCALLBACK(void) Display::i_drvDestruct(PPDMDRVINS pDrvIns)
3611{
3612 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
3613 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3614 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
3615
3616 /*
3617 * We repeat much of what i_drvPowerOff does in case it wasn't called.
3618 * In addition we sever the connection between us and the display.
3619 */
3620 if (pThis->pUpPort)
3621 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
3622
3623 pThis->IConnector.pbData = NULL;
3624 pThis->IConnector.cbScanline = 0;
3625 pThis->IConnector.cBits = 32;
3626 pThis->IConnector.cx = 0;
3627 pThis->IConnector.cy = 0;
3628
3629 if (pThis->pDisplay)
3630 {
3631 AutoWriteLock displayLock(pThis->pDisplay COMMA_LOCKVAL_SRC_POS);
3632#ifdef VBOX_WITH_RECORDING
3633 pThis->pDisplay->mParent->i_recordingStop();
3634#endif
3635#if defined(VBOX_WITH_VIDEOHWACCEL)
3636 pThis->pVBVACallbacks = NULL;
3637#endif
3638
3639 pThis->pDisplay->mpDrv = NULL;
3640 pThis->pDisplay = NULL;
3641 }
3642#if defined(VBOX_WITH_VIDEOHWACCEL)
3643 pThis->pVBVACallbacks = NULL;
3644#endif
3645}
3646
3647
3648/**
3649 * Construct a display driver instance.
3650 *
3651 * @copydoc FNPDMDRVCONSTRUCT
3652 */
3653DECLCALLBACK(int) Display::i_drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
3654{
3655 RT_NOREF(fFlags);
3656 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
3657 PDRVMAINDISPLAY pThis = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3658 LogRelFlowFunc(("iInstance=%d\n", pDrvIns->iInstance));
3659
3660 /*
3661 * Validate configuration.
3662 */
3663 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
3664 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
3665 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
3666 ("Configuration error: Not possible to attach anything to this driver!\n"),
3667 VERR_PDM_DRVINS_NO_ATTACH);
3668
3669 /*
3670 * Init Interfaces.
3671 */
3672 pDrvIns->IBase.pfnQueryInterface = Display::i_drvQueryInterface;
3673
3674 pThis->IConnector.pfnResize = Display::i_displayResizeCallback;
3675 pThis->IConnector.pfnUpdateRect = Display::i_displayUpdateCallback;
3676 pThis->IConnector.pfnRefresh = Display::i_displayRefreshCallback;
3677 pThis->IConnector.pfnReset = Display::i_displayResetCallback;
3678 pThis->IConnector.pfnLFBModeChange = Display::i_displayLFBModeChangeCallback;
3679 pThis->IConnector.pfnProcessAdapterData = Display::i_displayProcessAdapterDataCallback;
3680 pThis->IConnector.pfnProcessDisplayData = Display::i_displayProcessDisplayDataCallback;
3681#ifdef VBOX_WITH_VIDEOHWACCEL
3682 pThis->IConnector.pfnVHWACommandProcess = Display::i_displayVHWACommandProcess;
3683#endif
3684#ifdef VBOX_WITH_HGSMI
3685 pThis->IConnector.pfnVBVAEnable = Display::i_displayVBVAEnable;
3686 pThis->IConnector.pfnVBVADisable = Display::i_displayVBVADisable;
3687 pThis->IConnector.pfnVBVAUpdateBegin = Display::i_displayVBVAUpdateBegin;
3688 pThis->IConnector.pfnVBVAUpdateProcess = Display::i_displayVBVAUpdateProcess;
3689 pThis->IConnector.pfnVBVAUpdateEnd = Display::i_displayVBVAUpdateEnd;
3690 pThis->IConnector.pfnVBVAResize = Display::i_displayVBVAResize;
3691 pThis->IConnector.pfnVBVAMousePointerShape = Display::i_displayVBVAMousePointerShape;
3692 pThis->IConnector.pfnVBVAGuestCapabilityUpdate = Display::i_displayVBVAGuestCapabilityUpdate;
3693 pThis->IConnector.pfnVBVAInputMappingUpdate = Display::i_displayVBVAInputMappingUpdate;
3694 pThis->IConnector.pfnVBVAReportCursorPosition = Display::i_displayVBVAReportCursorPosition;
3695#endif
3696
3697 /*
3698 * Get the IDisplayPort interface of the above driver/device.
3699 */
3700 pThis->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
3701 if (!pThis->pUpPort)
3702 {
3703 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
3704 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3705 }
3706#if defined(VBOX_WITH_VIDEOHWACCEL)
3707 pThis->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
3708 if (!pThis->pVBVACallbacks)
3709 {
3710 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
3711 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3712 }
3713#endif
3714 /*
3715 * Get the Display object pointer and update the mpDrv member.
3716 */
3717 void *pv;
3718 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
3719 if (RT_FAILURE(rc))
3720 {
3721 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
3722 return rc;
3723 }
3724 Display *pDisplay = (Display *)pv; /** @todo Check this cast! */
3725 pThis->pDisplay = pDisplay;
3726 pThis->pDisplay->mpDrv = pThis;
3727
3728 /* Disable VRAM to a buffer copy initially. */
3729 pThis->pUpPort->pfnSetRenderVRAM(pThis->pUpPort, false);
3730 pThis->IConnector.cBits = 32; /* DevVGA does nothing otherwise. */
3731
3732 /*
3733 * Start periodic screen refreshes
3734 */
3735 pThis->pUpPort->pfnSetRefreshRate(pThis->pUpPort, 20);
3736
3737 return rc;
3738}
3739
3740
3741/**
3742 * Display driver registration record.
3743 */
3744const PDMDRVREG Display::DrvReg =
3745{
3746 /* u32Version */
3747 PDM_DRVREG_VERSION,
3748 /* szName */
3749 "MainDisplay",
3750 /* szRCMod */
3751 "",
3752 /* szR0Mod */
3753 "",
3754 /* pszDescription */
3755 "Main display driver (Main as in the API).",
3756 /* fFlags */
3757 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
3758 /* fClass. */
3759 PDM_DRVREG_CLASS_DISPLAY,
3760 /* cMaxInstances */
3761 ~0U,
3762 /* cbInstance */
3763 sizeof(DRVMAINDISPLAY),
3764 /* pfnConstruct */
3765 Display::i_drvConstruct,
3766 /* pfnDestruct */
3767 Display::i_drvDestruct,
3768 /* pfnRelocate */
3769 NULL,
3770 /* pfnIOCtl */
3771 NULL,
3772 /* pfnPowerOn */
3773 NULL,
3774 /* pfnReset */
3775 NULL,
3776 /* pfnSuspend */
3777 NULL,
3778 /* pfnResume */
3779 NULL,
3780 /* pfnAttach */
3781 NULL,
3782 /* pfnDetach */
3783 NULL,
3784 /* pfnPowerOff */
3785 Display::i_drvPowerOff,
3786 /* pfnSoftReset */
3787 NULL,
3788 /* u32EndVersion */
3789 PDM_DRVREG_VERSION
3790};
3791
3792/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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