VirtualBox

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

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

Main/src-client/DisplayImpl: Code improvement for video recording of SDL session.

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

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