VirtualBox

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

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

Main/DisplayImpl.cpp: Working on supporting video recording for VBoxSDL session.. Current coding under #ifdef VBOX_WITH_VPX_MAIN. Please don't use this define as the code is not in usable form right now..

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 141.7 KB
Line 
1/* $Id: DisplayImpl.cpp 42479 2012-07-31 13:32:00Z 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 VideoRecCopyToIntBuffer(pVideoRecContext, pDisplay->xOrigin, pDisplay->yOrigin,
3249 pDisplay->w, pDisplay->h, mPixelFormat,
3250 pDisplay->u16BitsPerPixel, mBytesPerLine, pDisplay->w,
3251 pDisplay->h, pDisplay->h, pDisplay->w,
3252 pu8Framebuffer, mTempRGBBuffer);
3253#endif
3254
3255
3256
3257#ifdef DEBUG_sunlover
3258 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
3259#endif /* DEBUG_sunlover */
3260#ifdef DEBUG_sunlover_2
3261 LogFlowFunc (("leave\n"));
3262#endif /* DEBUG_sunlover_2 */
3263}
3264
3265/**
3266 * Reset notification
3267 *
3268 * @see PDMIDISPLAYCONNECTOR::pfnReset
3269 */
3270DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3271{
3272 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3273
3274 LogRelFlowFunc (("\n"));
3275
3276 /* Disable VBVA mode. */
3277 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3278}
3279
3280/**
3281 * LFBModeChange notification
3282 *
3283 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3284 */
3285DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3286{
3287 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3288
3289 LogRelFlowFunc (("fEnabled=%d\n", fEnabled));
3290
3291 NOREF(fEnabled);
3292
3293 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3294 /* The LFBModeChange function is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
3295 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
3296}
3297
3298/**
3299 * Adapter information change notification.
3300 *
3301 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3302 */
3303DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
3304{
3305 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3306
3307 if (pvVRAM == NULL)
3308 {
3309 unsigned i;
3310 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
3311 {
3312 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
3313
3314 pFBInfo->u32Offset = 0;
3315 pFBInfo->u32MaxFramebufferSize = 0;
3316 pFBInfo->u32InformationSize = 0;
3317 }
3318 }
3319#ifndef VBOX_WITH_HGSMI
3320 else
3321 {
3322 uint8_t *pu8 = (uint8_t *)pvVRAM;
3323 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3324
3325 // @todo
3326 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3327
3328 VBOXVIDEOINFOHDR *pHdr;
3329
3330 for (;;)
3331 {
3332 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3333 pu8 += sizeof (VBOXVIDEOINFOHDR);
3334
3335 if (pu8 >= pu8End)
3336 {
3337 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
3338 break;
3339 }
3340
3341 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
3342 {
3343 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
3344 {
3345 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
3346 break;
3347 }
3348
3349 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
3350
3351 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
3352 {
3353 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
3354 break;
3355 }
3356
3357 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
3358
3359 pFBInfo->u32Offset = pDisplay->u32Offset;
3360 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
3361 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
3362
3363 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));
3364 }
3365 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
3366 {
3367 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
3368 {
3369 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
3370 break;
3371 }
3372
3373 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
3374
3375 switch (pConf32->u32Index)
3376 {
3377 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
3378 {
3379 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
3380 } break;
3381
3382 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
3383 {
3384 /* @todo make configurable. */
3385 pConf32->u32Value = _1M;
3386 } break;
3387
3388 default:
3389 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
3390 }
3391 }
3392 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3393 {
3394 if (pHdr->u16Length != 0)
3395 {
3396 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3397 break;
3398 }
3399
3400 break;
3401 }
3402 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
3403 {
3404 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
3405 }
3406
3407 pu8 += pHdr->u16Length;
3408 }
3409 }
3410#endif /* !VBOX_WITH_HGSMI */
3411}
3412
3413/**
3414 * Display information change notification.
3415 *
3416 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3417 */
3418DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
3419{
3420 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3421
3422 if (uScreenId >= pDrv->pDisplay->mcMonitors)
3423 {
3424 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
3425 return;
3426 }
3427
3428 /* Get the display information structure. */
3429 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
3430
3431 uint8_t *pu8 = (uint8_t *)pvVRAM;
3432 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
3433
3434 // @todo
3435 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
3436
3437 VBOXVIDEOINFOHDR *pHdr;
3438
3439 for (;;)
3440 {
3441 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3442 pu8 += sizeof (VBOXVIDEOINFOHDR);
3443
3444 if (pu8 >= pu8End)
3445 {
3446 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
3447 break;
3448 }
3449
3450 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
3451 {
3452 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
3453 {
3454 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
3455 break;
3456 }
3457
3458 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
3459
3460 pFBInfo->xOrigin = pScreen->xOrigin;
3461 pFBInfo->yOrigin = pScreen->yOrigin;
3462
3463 pFBInfo->w = pScreen->u16Width;
3464 pFBInfo->h = pScreen->u16Height;
3465
3466 LogRelFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
3467 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
3468
3469 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
3470 {
3471 /* Primary screen resize is initiated by the VGA device. */
3472 if (pFBInfo->fDisabled)
3473 {
3474 pFBInfo->fDisabled = false;
3475 fireGuestMonitorChangedEvent(pDrv->pDisplay->mParent->getEventSource(),
3476 GuestMonitorChangedEventType_Enabled,
3477 uScreenId,
3478 pFBInfo->xOrigin, pFBInfo->yOrigin,
3479 pFBInfo->w, pFBInfo->h);
3480 }
3481
3482 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, VBVA_SCREEN_F_ACTIVE);
3483 }
3484 }
3485 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3486 {
3487 if (pHdr->u16Length != 0)
3488 {
3489 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3490 break;
3491 }
3492
3493 break;
3494 }
3495 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
3496 {
3497 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
3498 {
3499 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
3500 break;
3501 }
3502
3503 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
3504
3505 pFBInfo->pHostEvents = pHostEvents;
3506
3507 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
3508 pHostEvents));
3509 }
3510 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
3511 {
3512 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
3513 {
3514 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
3515 break;
3516 }
3517
3518 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
3519 pu8 += pLink->i32Offset;
3520 }
3521 else
3522 {
3523 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
3524 }
3525
3526 pu8 += pHdr->u16Length;
3527 }
3528}
3529
3530#ifdef VBOX_WITH_VIDEOHWACCEL
3531
3532void Display::handleVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3533{
3534 unsigned id = (unsigned)pCommand->iDisplay;
3535 int rc = VINF_SUCCESS;
3536 if (id < mcMonitors)
3537 {
3538 IFramebuffer *pFramebuffer = maFramebuffers[id].pFramebuffer;
3539#ifdef DEBUG_misha
3540 Assert (pFramebuffer);
3541#endif
3542
3543 if (pFramebuffer != NULL)
3544 {
3545 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3546 if (FAILED(hr))
3547 {
3548 rc = (hr == E_NOTIMPL) ? VERR_NOT_IMPLEMENTED : VERR_GENERAL_FAILURE;
3549 }
3550 }
3551 else
3552 {
3553 rc = VERR_NOT_IMPLEMENTED;
3554 }
3555 }
3556 else
3557 {
3558 rc = VERR_INVALID_PARAMETER;
3559 }
3560
3561 if (RT_FAILURE(rc))
3562 {
3563 /* tell the guest the command is complete */
3564 pCommand->Flags &= (~VBOXVHWACMD_FLAG_HG_ASYNCH);
3565 pCommand->rc = rc;
3566 }
3567}
3568
3569DECLCALLBACK(void) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3570{
3571 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3572
3573 pDrv->pDisplay->handleVHWACommandProcess(pInterface, pCommand);
3574}
3575#endif
3576
3577#ifdef VBOX_WITH_CRHGSMI
3578void Display::handleCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3579{
3580 mpDrv->pVBVACallbacks->pfnCrHgsmiCommandCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CMD)pParam->u.pointer.addr, result);
3581}
3582
3583void Display::handleCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam)
3584{
3585 mpDrv->pVBVACallbacks->pfnCrHgsmiControlCompleteAsync(mpDrv->pVBVACallbacks, (PVBOXVDMACMD_CHROMIUM_CTL)pParam->u.pointer.addr, result);
3586}
3587
3588void Display::handleCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3589{
3590 int rc = VERR_INVALID_FUNCTION;
3591 VBOXHGCMSVCPARM parm;
3592 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3593 parm.u.pointer.addr = pCmd;
3594 parm.u.pointer.size = cbCmd;
3595
3596 if (mhCrOglSvc)
3597 {
3598 VMMDev *pVMMDev = mParent->getVMMDev();
3599 if (pVMMDev)
3600 {
3601 /* no completion callback is specified with this call,
3602 * the CrOgl code will complete the CrHgsmi command once it processes it */
3603 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm, NULL, NULL);
3604 AssertRC(rc);
3605 if (RT_SUCCESS(rc))
3606 return;
3607 }
3608 else
3609 rc = VERR_INVALID_STATE;
3610 }
3611
3612 /* we are here because something went wrong with command processing, complete it */
3613 handleCrHgsmiCommandCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CMD, &parm);
3614}
3615
3616void Display::handleCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCtl)
3617{
3618 int rc = VERR_INVALID_FUNCTION;
3619 VBOXHGCMSVCPARM parm;
3620 parm.type = VBOX_HGCM_SVC_PARM_PTR;
3621 parm.u.pointer.addr = pCtl;
3622 parm.u.pointer.size = cbCtl;
3623
3624 if (mhCrOglSvc)
3625 {
3626 VMMDev *pVMMDev = mParent->getVMMDev();
3627 if (pVMMDev)
3628 {
3629 rc = pVMMDev->hgcmHostFastCallAsync(mhCrOglSvc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm, Display::displayCrHgsmiControlCompletion, this);
3630 AssertRC(rc);
3631 if (RT_SUCCESS(rc))
3632 return;
3633 }
3634 else
3635 rc = VERR_INVALID_STATE;
3636 }
3637
3638 /* we are here because something went wrong with command processing, complete it */
3639 handleCrHgsmiControlCompletion(rc, SHCRGL_HOST_FN_CRHGSMI_CTL, &parm);
3640}
3641
3642
3643DECLCALLBACK(void) Display::displayCrHgsmiCommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd)
3644{
3645 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3646
3647 pDrv->pDisplay->handleCrHgsmiCommandProcess(pInterface, pCmd, cbCmd);
3648}
3649
3650DECLCALLBACK(void) Display::displayCrHgsmiControlProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVDMACMD_CHROMIUM_CTL pCmd, uint32_t cbCmd)
3651{
3652 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3653
3654 pDrv->pDisplay->handleCrHgsmiControlProcess(pInterface, pCmd, cbCmd);
3655}
3656
3657DECLCALLBACK(void) Display::displayCrHgsmiCommandCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3658{
3659 AssertMsgFailed(("not expected!"));
3660 Display *pDisplay = (Display *)pvContext;
3661 pDisplay->handleCrHgsmiCommandCompletion(result, u32Function, pParam);
3662}
3663
3664DECLCALLBACK(void) Display::displayCrHgsmiControlCompletion(int32_t result, uint32_t u32Function, PVBOXHGCMSVCPARM pParam, void *pvContext)
3665{
3666 Display *pDisplay = (Display *)pvContext;
3667 pDisplay->handleCrHgsmiControlCompletion(result, u32Function, pParam);
3668}
3669#endif
3670
3671
3672#ifdef VBOX_WITH_HGSMI
3673DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags)
3674{
3675 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3676
3677 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3678 Display *pThis = pDrv->pDisplay;
3679
3680 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3681 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3682
3683 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3684
3685 return VINF_SUCCESS;
3686}
3687
3688DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3689{
3690 LogRelFlowFunc(("uScreenId %d\n", uScreenId));
3691
3692 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3693 Display *pThis = pDrv->pDisplay;
3694
3695 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3696
3697 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3698 {
3699 /* Make sure that the primary screen is visible now.
3700 * The guest can't use VBVA anymore, so only only the VGA device output works.
3701 */
3702 if (pFBInfo->fDisabled)
3703 {
3704 pFBInfo->fDisabled = false;
3705 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
3706 GuestMonitorChangedEventType_Enabled,
3707 uScreenId,
3708 pFBInfo->xOrigin, pFBInfo->yOrigin,
3709 pFBInfo->w, pFBInfo->h);
3710 }
3711 }
3712
3713 pFBInfo->fVBVAEnabled = false;
3714
3715 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, pFBInfo);
3716
3717 pFBInfo->pVBVAHostFlags = NULL;
3718
3719 pFBInfo->u32Offset = 0; /* Not used in HGSMI. */
3720 pFBInfo->u32MaxFramebufferSize = 0; /* Not used in HGSMI. */
3721 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3722
3723 pFBInfo->xOrigin = 0;
3724 pFBInfo->yOrigin = 0;
3725
3726 pFBInfo->w = 0;
3727 pFBInfo->h = 0;
3728
3729 pFBInfo->u16BitsPerPixel = 0;
3730 pFBInfo->pu8FramebufferVRAM = NULL;
3731 pFBInfo->u32LineSize = 0;
3732}
3733
3734DECLCALLBACK(void) Display::displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3735{
3736 LogFlowFunc(("uScreenId %d\n", uScreenId));
3737
3738 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3739 Display *pThis = pDrv->pDisplay;
3740 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3741
3742 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3743 {
3744 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers, pThis->mcMonitors);
3745 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3746 }
3747
3748 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
3749 {
3750 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
3751 {
3752 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
3753 * under display device lock, so thread safe.
3754 */
3755 pFBInfo->cVBVASkipUpdate = 0;
3756 pThis->handleDisplayUpdate(uScreenId, pFBInfo->vbvaSkippedRect.xLeft - pFBInfo->xOrigin,
3757 pFBInfo->vbvaSkippedRect.yTop - pFBInfo->yOrigin,
3758 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
3759 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
3760 }
3761 }
3762 else
3763 {
3764 /* The framebuffer is being resized. */
3765 pFBInfo->cVBVASkipUpdate++;
3766 }
3767}
3768
3769DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, const PVBVACMDHDR pCmd, size_t cbCmd)
3770{
3771 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d, @%d,%d %dx%d\n", uScreenId, pCmd, cbCmd, pCmd->x, pCmd->y, pCmd->w, pCmd->h));
3772
3773 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3774 Display *pThis = pDrv->pDisplay;
3775 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3776
3777 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3778 {
3779 if (pFBInfo->fDefaultFormat)
3780 {
3781 /* Make sure that framebuffer contains the same image as the guest VRAM. */
3782 if ( uScreenId == VBOX_VIDEO_PRIMARY_SCREEN
3783 && !pFBInfo->pFramebuffer.isNull()
3784 && !pFBInfo->fDisabled)
3785 {
3786 pDrv->pUpPort->pfnUpdateDisplayRect (pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
3787 }
3788 else if ( !pFBInfo->pFramebuffer.isNull()
3789 && !(pFBInfo->fDisabled))
3790 {
3791 /* Render VRAM content to the framebuffer. */
3792 BYTE *address = NULL;
3793 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
3794 if (SUCCEEDED(hrc) && address != NULL)
3795 {
3796 uint32_t width = pCmd->w;
3797 uint32_t height = pCmd->h;
3798
3799 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3800 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
3801 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
3802 uint32_t u32SrcWidth = pFBInfo->w;
3803 uint32_t u32SrcHeight = pFBInfo->h;
3804 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3805 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3806
3807 uint8_t *pu8Dst = address;
3808 int32_t xDst = xSrc;
3809 int32_t yDst = ySrc;
3810 uint32_t u32DstWidth = u32SrcWidth;
3811 uint32_t u32DstHeight = u32SrcHeight;
3812 uint32_t u32DstLineSize = u32DstWidth * 4;
3813 uint32_t u32DstBitsPerPixel = 32;
3814
3815 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3816 width, height,
3817 pu8Src,
3818 xSrc, ySrc,
3819 u32SrcWidth, u32SrcHeight,
3820 u32SrcLineSize, u32SrcBitsPerPixel,
3821 pu8Dst,
3822 xDst, yDst,
3823 u32DstWidth, u32DstHeight,
3824 u32DstLineSize, u32DstBitsPerPixel);
3825 }
3826 }
3827 }
3828
3829 VBVACMDHDR hdrSaved = *pCmd;
3830
3831 VBVACMDHDR *pHdrUnconst = (VBVACMDHDR *)pCmd;
3832
3833 pHdrUnconst->x -= (int16_t)pFBInfo->xOrigin;
3834 pHdrUnconst->y -= (int16_t)pFBInfo->yOrigin;
3835
3836 /* @todo new SendUpdate entry which can get a separate cmd header or coords. */
3837 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, cbCmd);
3838
3839 *pHdrUnconst = hdrSaved;
3840 }
3841}
3842
3843DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y, uint32_t cx, uint32_t cy)
3844{
3845 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3846
3847 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3848 Display *pThis = pDrv->pDisplay;
3849 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3850
3851 /* @todo handleFramebufferUpdate (uScreenId,
3852 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3853 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3854 * cx, cy);
3855 */
3856 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3857 {
3858 pThis->handleDisplayUpdate(uScreenId, x - pFBInfo->xOrigin, y - pFBInfo->yOrigin, cx, cy);
3859 }
3860 else
3861 {
3862 /* Save the updated rectangle. */
3863 int32_t xRight = x + cx;
3864 int32_t yBottom = y + cy;
3865
3866 if (pFBInfo->cVBVASkipUpdate == 1)
3867 {
3868 pFBInfo->vbvaSkippedRect.xLeft = x;
3869 pFBInfo->vbvaSkippedRect.yTop = y;
3870 pFBInfo->vbvaSkippedRect.xRight = xRight;
3871 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3872 }
3873 else
3874 {
3875 if (pFBInfo->vbvaSkippedRect.xLeft > x)
3876 {
3877 pFBInfo->vbvaSkippedRect.xLeft = x;
3878 }
3879 if (pFBInfo->vbvaSkippedRect.yTop > y)
3880 {
3881 pFBInfo->vbvaSkippedRect.yTop = y;
3882 }
3883 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
3884 {
3885 pFBInfo->vbvaSkippedRect.xRight = xRight;
3886 }
3887 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
3888 {
3889 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3890 }
3891 }
3892 }
3893}
3894
3895#ifdef DEBUG_sunlover
3896static void logVBVAResize(const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, const DISPLAYFBINFO *pFBInfo)
3897{
3898 LogRel(("displayVBVAResize: [%d] %s\n"
3899 " pView->u32ViewIndex %d\n"
3900 " pView->u32ViewOffset 0x%08X\n"
3901 " pView->u32ViewSize 0x%08X\n"
3902 " pView->u32MaxScreenSize 0x%08X\n"
3903 " pScreen->i32OriginX %d\n"
3904 " pScreen->i32OriginY %d\n"
3905 " pScreen->u32StartOffset 0x%08X\n"
3906 " pScreen->u32LineSize 0x%08X\n"
3907 " pScreen->u32Width %d\n"
3908 " pScreen->u32Height %d\n"
3909 " pScreen->u16BitsPerPixel %d\n"
3910 " pScreen->u16Flags 0x%04X\n"
3911 " pFBInfo->u32Offset 0x%08X\n"
3912 " pFBInfo->u32MaxFramebufferSize 0x%08X\n"
3913 " pFBInfo->u32InformationSize 0x%08X\n"
3914 " pFBInfo->fDisabled %d\n"
3915 " xOrigin, yOrigin, w, h: %d,%d %dx%d\n"
3916 " pFBInfo->u16BitsPerPixel %d\n"
3917 " pFBInfo->pu8FramebufferVRAM %p\n"
3918 " pFBInfo->u32LineSize 0x%08X\n"
3919 " pFBInfo->flags 0x%04X\n"
3920 " pFBInfo->pHostEvents %p\n"
3921 " pFBInfo->u32ResizeStatus %d\n"
3922 " pFBInfo->fDefaultFormat %d\n"
3923 " dirtyRect %d-%d %d-%d\n"
3924 " pFBInfo->pendingResize.fPending %d\n"
3925 " pFBInfo->pendingResize.pixelFormat %d\n"
3926 " pFBInfo->pendingResize.pvVRAM %p\n"
3927 " pFBInfo->pendingResize.bpp %d\n"
3928 " pFBInfo->pendingResize.cbLine 0x%08X\n"
3929 " pFBInfo->pendingResize.w,h %dx%d\n"
3930 " pFBInfo->pendingResize.flags 0x%04X\n"
3931 " pFBInfo->fVBVAEnabled %d\n"
3932 " pFBInfo->cVBVASkipUpdate %d\n"
3933 " pFBInfo->vbvaSkippedRect %d-%d %d-%d\n"
3934 " pFBInfo->pVBVAHostFlags %p\n"
3935 "",
3936 pScreen->u32ViewIndex,
3937 (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)? "DISABLED": "ENABLED",
3938 pView->u32ViewIndex,
3939 pView->u32ViewOffset,
3940 pView->u32ViewSize,
3941 pView->u32MaxScreenSize,
3942 pScreen->i32OriginX,
3943 pScreen->i32OriginY,
3944 pScreen->u32StartOffset,
3945 pScreen->u32LineSize,
3946 pScreen->u32Width,
3947 pScreen->u32Height,
3948 pScreen->u16BitsPerPixel,
3949 pScreen->u16Flags,
3950 pFBInfo->u32Offset,
3951 pFBInfo->u32MaxFramebufferSize,
3952 pFBInfo->u32InformationSize,
3953 pFBInfo->fDisabled,
3954 pFBInfo->xOrigin,
3955 pFBInfo->yOrigin,
3956 pFBInfo->w,
3957 pFBInfo->h,
3958 pFBInfo->u16BitsPerPixel,
3959 pFBInfo->pu8FramebufferVRAM,
3960 pFBInfo->u32LineSize,
3961 pFBInfo->flags,
3962 pFBInfo->pHostEvents,
3963 pFBInfo->u32ResizeStatus,
3964 pFBInfo->fDefaultFormat,
3965 pFBInfo->dirtyRect.xLeft,
3966 pFBInfo->dirtyRect.xRight,
3967 pFBInfo->dirtyRect.yTop,
3968 pFBInfo->dirtyRect.yBottom,
3969 pFBInfo->pendingResize.fPending,
3970 pFBInfo->pendingResize.pixelFormat,
3971 pFBInfo->pendingResize.pvVRAM,
3972 pFBInfo->pendingResize.bpp,
3973 pFBInfo->pendingResize.cbLine,
3974 pFBInfo->pendingResize.w,
3975 pFBInfo->pendingResize.h,
3976 pFBInfo->pendingResize.flags,
3977 pFBInfo->fVBVAEnabled,
3978 pFBInfo->cVBVASkipUpdate,
3979 pFBInfo->vbvaSkippedRect.xLeft,
3980 pFBInfo->vbvaSkippedRect.yTop,
3981 pFBInfo->vbvaSkippedRect.xRight,
3982 pFBInfo->vbvaSkippedRect.yBottom,
3983 pFBInfo->pVBVAHostFlags
3984 ));
3985}
3986#endif /* DEBUG_sunlover */
3987
3988DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
3989{
3990 LogRelFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3991
3992 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3993 Display *pThis = pDrv->pDisplay;
3994
3995 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
3996
3997 if (pScreen->u16Flags & VBVA_SCREEN_F_DISABLED)
3998 {
3999 pFBInfo->fDisabled = true;
4000 pFBInfo->flags = pScreen->u16Flags;
4001
4002 /* Temporary: ask framebuffer to resize using a default format. The framebuffer will be black. */
4003 pThis->handleDisplayResize(pScreen->u32ViewIndex, 0,
4004 (uint8_t *)NULL,
4005 pScreen->u32LineSize, pScreen->u32Width,
4006 pScreen->u32Height, pScreen->u16Flags);
4007
4008 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4009 GuestMonitorChangedEventType_Disabled,
4010 pScreen->u32ViewIndex,
4011 0, 0, 0, 0);
4012 return VINF_SUCCESS;
4013 }
4014
4015 /* If display was disabled or there is no framebuffer, a resize will be required,
4016 * because the framebuffer was/will be changed.
4017 */
4018 bool fResize = pFBInfo->fDisabled || pFBInfo->pFramebuffer.isNull();
4019
4020 if (pFBInfo->fDisabled)
4021 {
4022 pFBInfo->fDisabled = false;
4023 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4024 GuestMonitorChangedEventType_Enabled,
4025 pScreen->u32ViewIndex,
4026 pScreen->i32OriginX, pScreen->i32OriginY,
4027 pScreen->u32Width, pScreen->u32Height);
4028 /* Continue to update pFBInfo. */
4029 }
4030
4031 /* Check if this is a real resize or a notification about the screen origin.
4032 * The guest uses this VBVAResize call for both.
4033 */
4034 fResize = fResize
4035 || pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
4036 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
4037 || pFBInfo->u32LineSize != pScreen->u32LineSize
4038 || pFBInfo->w != pScreen->u32Width
4039 || pFBInfo->h != pScreen->u32Height;
4040
4041 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
4042 || pFBInfo->yOrigin != pScreen->i32OriginY;
4043
4044 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
4045 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
4046 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
4047
4048 pFBInfo->xOrigin = pScreen->i32OriginX;
4049 pFBInfo->yOrigin = pScreen->i32OriginY;
4050
4051 pFBInfo->w = pScreen->u32Width;
4052 pFBInfo->h = pScreen->u32Height;
4053
4054 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
4055 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
4056 pFBInfo->u32LineSize = pScreen->u32LineSize;
4057
4058 pFBInfo->flags = pScreen->u16Flags;
4059
4060 if (fNewOrigin)
4061 {
4062 fireGuestMonitorChangedEvent(pThis->mParent->getEventSource(),
4063 GuestMonitorChangedEventType_NewOrigin,
4064 pScreen->u32ViewIndex,
4065 pScreen->i32OriginX, pScreen->i32OriginY,
4066 0, 0);
4067 }
4068
4069#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
4070 if (fNewOrigin && !fResize)
4071 {
4072 BOOL is3denabled;
4073 pThis->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
4074
4075 if (is3denabled)
4076 {
4077 VBOXHGCMSVCPARM parm;
4078
4079 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
4080 parm.u.uint32 = pScreen->u32ViewIndex;
4081
4082 VMMDev *pVMMDev = pThis->mParent->getVMMDev();
4083
4084 if (pVMMDev)
4085 pVMMDev->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED, SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
4086 }
4087 }
4088#endif /* VBOX_WITH_CROGL */
4089
4090 if (!fResize)
4091 {
4092 /* No parameters of the framebuffer have actually changed. */
4093 if (fNewOrigin)
4094 {
4095 /* VRDP server still need this notification. */
4096 LogRelFlowFunc (("Calling VRDP\n"));
4097 pThis->mParent->consoleVRDPServer()->SendResize();
4098 }
4099 return VINF_SUCCESS;
4100 }
4101
4102 if (pFBInfo->pFramebuffer.isNull())
4103 {
4104 /* If no framebuffer, the resize will be done later when a new framebuffer will be set in changeFramebuffer. */
4105 return VINF_SUCCESS;
4106 }
4107
4108 /* If the framebuffer already set for the screen, do a regular resize. */
4109 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
4110 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
4111 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height, pScreen->u16Flags);
4112}
4113
4114DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
4115 uint32_t xHot, uint32_t yHot,
4116 uint32_t cx, uint32_t cy,
4117 const void *pvShape)
4118{
4119 LogFlowFunc(("\n"));
4120
4121 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
4122 Display *pThis = pDrv->pDisplay;
4123
4124 size_t cbShapeSize = 0;
4125
4126 if (pvShape)
4127 {
4128 cbShapeSize = (cx + 7) / 8 * cy; /* size of the AND mask */
4129 cbShapeSize = ((cbShapeSize + 3) & ~3) + cx * 4 * cy; /* + gap + size of the XOR mask */
4130 }
4131 com::SafeArray<BYTE> shapeData(cbShapeSize);
4132
4133 if (pvShape)
4134 ::memcpy(shapeData.raw(), pvShape, cbShapeSize);
4135
4136 /* Tell the console about it */
4137 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
4138 xHot, yHot, cx, cy, ComSafeArrayAsInParam(shapeData));
4139
4140 return VINF_SUCCESS;
4141}
4142#endif /* VBOX_WITH_HGSMI */
4143
4144/**
4145 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4146 */
4147DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
4148{
4149 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
4150 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4151 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
4152 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
4153 return NULL;
4154}
4155
4156
4157/**
4158 * Destruct a display driver instance.
4159 *
4160 * @returns VBox status.
4161 * @param pDrvIns The driver instance data.
4162 */
4163DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
4164{
4165 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4166 LogRelFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
4167 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
4168
4169 if (pData->pDisplay)
4170 {
4171 AutoWriteLock displayLock(pData->pDisplay COMMA_LOCKVAL_SRC_POS);
4172#ifdef VBOX_WITH_CRHGSMI
4173 pData->pDisplay->destructCrHgsmiData();
4174#endif
4175 pData->pDisplay->mpDrv = NULL;
4176 pData->pDisplay->mpVMMDev = NULL;
4177 pData->pDisplay->mLastAddress = NULL;
4178 pData->pDisplay->mLastBytesPerLine = 0;
4179 pData->pDisplay->mLastBitsPerPixel = 0,
4180 pData->pDisplay->mLastWidth = 0;
4181 pData->pDisplay->mLastHeight = 0;
4182 }
4183}
4184
4185
4186/**
4187 * Construct a display driver instance.
4188 *
4189 * @copydoc FNPDMDRVCONSTRUCT
4190 */
4191DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
4192{
4193 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
4194 LogRelFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
4195 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
4196
4197 /*
4198 * Validate configuration.
4199 */
4200 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
4201 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
4202 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
4203 ("Configuration error: Not possible to attach anything to this driver!\n"),
4204 VERR_PDM_DRVINS_NO_ATTACH);
4205
4206 /*
4207 * Init Interfaces.
4208 */
4209 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
4210
4211 pData->IConnector.pfnResize = Display::displayResizeCallback;
4212 pData->IConnector.pfnUpdateRect = Display::displayUpdateCallback;
4213 pData->IConnector.pfnRefresh = Display::displayRefreshCallback;
4214 pData->IConnector.pfnReset = Display::displayResetCallback;
4215 pData->IConnector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
4216 pData->IConnector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
4217 pData->IConnector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
4218#ifdef VBOX_WITH_VIDEOHWACCEL
4219 pData->IConnector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
4220#endif
4221#ifdef VBOX_WITH_CRHGSMI
4222 pData->IConnector.pfnCrHgsmiCommandProcess = Display::displayCrHgsmiCommandProcess;
4223 pData->IConnector.pfnCrHgsmiControlProcess = Display::displayCrHgsmiControlProcess;
4224#endif
4225#ifdef VBOX_WITH_HGSMI
4226 pData->IConnector.pfnVBVAEnable = Display::displayVBVAEnable;
4227 pData->IConnector.pfnVBVADisable = Display::displayVBVADisable;
4228 pData->IConnector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
4229 pData->IConnector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
4230 pData->IConnector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
4231 pData->IConnector.pfnVBVAResize = Display::displayVBVAResize;
4232 pData->IConnector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
4233#endif
4234#ifdef VBOX_WITH_VPX_MAIN
4235 rc = VideoRecContextCreate(&pVideoRecContext);
4236 rc = RTCritSectInit(&mCritSect);
4237 AssertReturn(rc == VINF_SUCCESS, E_UNEXPECTED);
4238
4239 if(rc == VINF_SUCCESS)
4240 rc = VideoRecContextInit(pVideoRecContext, "test.webm", 800, 720);
4241
4242#endif
4243
4244
4245 /*
4246 * Get the IDisplayPort interface of the above driver/device.
4247 */
4248 pData->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
4249 if (!pData->pUpPort)
4250 {
4251 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
4252 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4253 }
4254#if defined(VBOX_WITH_VIDEOHWACCEL) || defined(VBOX_WITH_CRHGSMI)
4255 pData->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
4256 if (!pData->pVBVACallbacks)
4257 {
4258 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
4259 return VERR_PDM_MISSING_INTERFACE_ABOVE;
4260 }
4261#endif
4262 /*
4263 * Get the Display object pointer and update the mpDrv member.
4264 */
4265 void *pv;
4266 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
4267 if (RT_FAILURE(rc))
4268 {
4269 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
4270 return rc;
4271 }
4272 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
4273 pData->pDisplay->mpDrv = pData;
4274
4275 /*
4276 * Update our display information according to the framebuffer
4277 */
4278 pData->pDisplay->updateDisplayData();
4279
4280 /*
4281 * Start periodic screen refreshes
4282 */
4283 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
4284
4285#ifdef VBOX_WITH_CRHGSMI
4286 pData->pDisplay->setupCrHgsmiData();
4287#endif
4288
4289 return VINF_SUCCESS;
4290}
4291
4292
4293/**
4294 * Display driver registration record.
4295 */
4296const PDMDRVREG Display::DrvReg =
4297{
4298 /* u32Version */
4299 PDM_DRVREG_VERSION,
4300 /* szName */
4301 "MainDisplay",
4302 /* szRCMod */
4303 "",
4304 /* szR0Mod */
4305 "",
4306 /* pszDescription */
4307 "Main display driver (Main as in the API).",
4308 /* fFlags */
4309 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
4310 /* fClass. */
4311 PDM_DRVREG_CLASS_DISPLAY,
4312 /* cMaxInstances */
4313 ~0U,
4314 /* cbInstance */
4315 sizeof(DRVMAINDISPLAY),
4316 /* pfnConstruct */
4317 Display::drvConstruct,
4318 /* pfnDestruct */
4319 Display::drvDestruct,
4320 /* pfnRelocate */
4321 NULL,
4322 /* pfnIOCtl */
4323 NULL,
4324 /* pfnPowerOn */
4325 NULL,
4326 /* pfnReset */
4327 NULL,
4328 /* pfnSuspend */
4329 NULL,
4330 /* pfnResume */
4331 NULL,
4332 /* pfnAttach */
4333 NULL,
4334 /* pfnDetach */
4335 NULL,
4336 /* pfnPowerOff */
4337 NULL,
4338 /* pfnSoftReset */
4339 NULL,
4340 /* u32EndVersion */
4341 PDM_DRVREG_VERSION
4342};
4343/* 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