VirtualBox

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

Last change on this file since 45029 was 45029, checked in by vboxsync, 12 years ago

Main: drvDestruct/drvConstruct cleanups.

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

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