VirtualBox

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

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

Main: thumbnail PNG API, refactoring

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