VirtualBox

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

Last change on this file since 42864 was 42861, checked in by vboxsync, 13 years ago

main/VideoCaptureAndEncoding: Error checking, formatting. Still under testing and development

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

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