VirtualBox

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

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

Main/DisplayImpl: free the screenshot buffer on error for secondary displays.

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