VirtualBox

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

Last change on this file since 35612 was 35612, checked in by vboxsync, 14 years ago

Main/Display: make sure that we do not try to update a disabled screen, and revert the 640x480 disabled resolution for now

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

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