VirtualBox

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

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

Main/Display: missing initialisation and saved state bits for X.Org guest multi-monitor extensions

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

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