VirtualBox

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

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

Main/Display: multimonitor enable/disable screen fixes.

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

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