VirtualBox

source: vbox/trunk/src/VBox/Main/DisplayImpl.cpp@ 32398

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

Main: Save guest dimension in save state; add method for queering this info to IMachine.

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