VirtualBox

source: vbox/trunk/src/VBox/Main/DisplayImpl.cpp@ 24968

Last change on this file since 24968 was 24941, checked in by vboxsync, 15 years ago

DisplayImpl: VBVA lock (xTracker 4463).

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