VirtualBox

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

Last change on this file since 24930 was 24924, checked in by vboxsync, 15 years ago

More VBVA lock code (disabled, xTracker 4463)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 105.8 KB
Line 
1/* $Id: DisplayImpl.cpp 24924 2009-11-24 20:11:58Z 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 VBVA or DevVGA lock.
990 * Safe to use VBVA vars and take the framebuffer lock.
991 */
992#ifdef DEBUG_sunlover
993 /* This assert is here for testing only. */
994 Assert(RTCritSectIsOwner(&mVBVALock));
995#endif /* DEBUG_sunlover */
996#endif /* VBOX_WITH_OLD_VBVA_LOCK */
997
998#ifdef DEBUG_sunlover
999 LogFlowFunc (("%d,%d %dx%d (%d,%d)\n",
1000 x, y, w, h, mpDrv->Connector.cx, mpDrv->Connector.cy));
1001#endif /* DEBUG_sunlover */
1002
1003 unsigned uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
1004
1005#ifdef DEBUG_sunlover
1006 LogFlowFunc (("%d,%d %dx%d (checked)\n", x, y, w, h));
1007#endif /* DEBUG_sunlover */
1008
1009 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
1010
1011 // if there is no framebuffer, this call is not interesting
1012 if (pFramebuffer == NULL)
1013 return;
1014
1015 pFramebuffer->Lock();
1016
1017 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1018 checkCoordBounds (&x, &y, &w, &h, mpDrv->Connector.cx, mpDrv->Connector.cy);
1019 else
1020 checkCoordBounds (&x, &y, &w, &h, maFramebuffers[uScreenId].w,
1021 maFramebuffers[uScreenId].h);
1022
1023 if (w != 0 && h != 0)
1024 pFramebuffer->NotifyUpdate(x, y, w, h);
1025
1026 pFramebuffer->Unlock();
1027
1028#ifndef VBOX_WITH_HGSMI
1029 if (!mfVideoAccelEnabled)
1030 {
1031#else
1032 if (!mfVideoAccelEnabled && !maFramebuffers[uScreenId].fVBVAEnabled)
1033 {
1034#endif /* VBOX_WITH_HGSMI */
1035 /* When VBVA is enabled, the VRDP server is informed in the VideoAccelFlush.
1036 * Inform the server here only if VBVA is disabled.
1037 */
1038 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
1039 mParent->consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
1040 }
1041}
1042
1043typedef struct _VBVADIRTYREGION
1044{
1045 /* Copies of object's pointers used by vbvaRgn functions. */
1046 DISPLAYFBINFO *paFramebuffers;
1047 unsigned cMonitors;
1048 Display *pDisplay;
1049 PPDMIDISPLAYPORT pPort;
1050
1051} VBVADIRTYREGION;
1052
1053static void vbvaRgnInit (VBVADIRTYREGION *prgn, DISPLAYFBINFO *paFramebuffers, unsigned cMonitors, Display *pd, PPDMIDISPLAYPORT pp)
1054{
1055 prgn->paFramebuffers = paFramebuffers;
1056 prgn->cMonitors = cMonitors;
1057 prgn->pDisplay = pd;
1058 prgn->pPort = pp;
1059
1060 unsigned uScreenId;
1061 for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
1062 {
1063 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1064
1065 memset (&pFBInfo->dirtyRect, 0, sizeof (pFBInfo->dirtyRect));
1066 }
1067}
1068
1069static void vbvaRgnDirtyRect (VBVADIRTYREGION *prgn, unsigned uScreenId, VBVACMDHDR *phdr)
1070{
1071 LogSunlover (("x = %d, y = %d, w = %d, h = %d\n",
1072 phdr->x, phdr->y, phdr->w, phdr->h));
1073
1074 /*
1075 * Here update rectangles are accumulated to form an update area.
1076 * @todo
1077 * Now the simpliest method is used which builds one rectangle that
1078 * includes all update areas. A bit more advanced method can be
1079 * employed here. The method should be fast however.
1080 */
1081 if (phdr->w == 0 || phdr->h == 0)
1082 {
1083 /* Empty rectangle. */
1084 return;
1085 }
1086
1087 int32_t xRight = phdr->x + phdr->w;
1088 int32_t yBottom = phdr->y + phdr->h;
1089
1090 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1091
1092 if (pFBInfo->dirtyRect.xRight == 0)
1093 {
1094 /* This is the first rectangle to be added. */
1095 pFBInfo->dirtyRect.xLeft = phdr->x;
1096 pFBInfo->dirtyRect.yTop = phdr->y;
1097 pFBInfo->dirtyRect.xRight = xRight;
1098 pFBInfo->dirtyRect.yBottom = yBottom;
1099 }
1100 else
1101 {
1102 /* Adjust region coordinates. */
1103 if (pFBInfo->dirtyRect.xLeft > phdr->x)
1104 {
1105 pFBInfo->dirtyRect.xLeft = phdr->x;
1106 }
1107
1108 if (pFBInfo->dirtyRect.yTop > phdr->y)
1109 {
1110 pFBInfo->dirtyRect.yTop = phdr->y;
1111 }
1112
1113 if (pFBInfo->dirtyRect.xRight < xRight)
1114 {
1115 pFBInfo->dirtyRect.xRight = xRight;
1116 }
1117
1118 if (pFBInfo->dirtyRect.yBottom < yBottom)
1119 {
1120 pFBInfo->dirtyRect.yBottom = yBottom;
1121 }
1122 }
1123
1124 if (pFBInfo->fDefaultFormat)
1125 {
1126 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
1127 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, phdr->x, phdr->y, phdr->w, phdr->h);
1128 prgn->pDisplay->handleDisplayUpdate (phdr->x + pFBInfo->xOrigin,
1129 phdr->y + pFBInfo->yOrigin, phdr->w, phdr->h);
1130 }
1131
1132 return;
1133}
1134
1135static void vbvaRgnUpdateFramebuffer (VBVADIRTYREGION *prgn, unsigned uScreenId)
1136{
1137 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
1138
1139 uint32_t w = pFBInfo->dirtyRect.xRight - pFBInfo->dirtyRect.xLeft;
1140 uint32_t h = pFBInfo->dirtyRect.yBottom - pFBInfo->dirtyRect.yTop;
1141
1142 if (!pFBInfo->fDefaultFormat && pFBInfo->pFramebuffer && w != 0 && h != 0)
1143 {
1144 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
1145 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
1146 prgn->pDisplay->handleDisplayUpdate (pFBInfo->dirtyRect.xLeft + pFBInfo->xOrigin,
1147 pFBInfo->dirtyRect.yTop + pFBInfo->yOrigin, w, h);
1148 }
1149}
1150
1151static void vbvaSetMemoryFlags (VBVAMEMORY *pVbvaMemory,
1152 bool fVideoAccelEnabled,
1153 bool fVideoAccelVRDP,
1154 uint32_t fu32SupportedOrders,
1155 DISPLAYFBINFO *paFBInfos,
1156 unsigned cFBInfos)
1157{
1158 if (pVbvaMemory)
1159 {
1160 /* This called only on changes in mode. So reset VRDP always. */
1161 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
1162
1163 if (fVideoAccelEnabled)
1164 {
1165 fu32Flags |= VBVA_F_MODE_ENABLED;
1166
1167 if (fVideoAccelVRDP)
1168 {
1169 fu32Flags |= VBVA_F_MODE_VRDP | VBVA_F_MODE_VRDP_ORDER_MASK;
1170
1171 pVbvaMemory->fu32SupportedOrders = fu32SupportedOrders;
1172 }
1173 }
1174
1175 pVbvaMemory->fu32ModeFlags = fu32Flags;
1176 }
1177
1178 unsigned uScreenId;
1179 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
1180 {
1181 if (paFBInfos[uScreenId].pHostEvents)
1182 {
1183 paFBInfos[uScreenId].pHostEvents->fu32Events |= VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
1184 }
1185 }
1186}
1187
1188bool Display::VideoAccelAllowed (void)
1189{
1190 return true;
1191}
1192
1193#ifdef VBOX_WITH_OLD_VBVA_LOCK
1194int Display::vbvaLock(void)
1195{
1196 return RTCritSectEnter(&mVBVALock);
1197}
1198
1199void Display::vbvaUnlock(void)
1200{
1201 RTCritSectLeave(&mVBVALock);
1202}
1203#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1204
1205/**
1206 * @thread EMT
1207 */
1208#ifdef VBOX_WITH_OLD_VBVA_LOCK
1209int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1210{
1211 int rc;
1212 vbvaLock();
1213 rc = videoAccelEnable (fEnable, pVbvaMemory);
1214 vbvaUnlock();
1215 return rc;
1216}
1217#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1218
1219#ifdef VBOX_WITH_OLD_VBVA_LOCK
1220int Display::videoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1221#else
1222int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
1223#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1224{
1225 int rc = VINF_SUCCESS;
1226
1227 /* Called each time the guest wants to use acceleration,
1228 * or when the VGA device disables acceleration,
1229 * or when restoring the saved state with accel enabled.
1230 *
1231 * VGA device disables acceleration on each video mode change
1232 * and on reset.
1233 *
1234 * Guest enabled acceleration at will. And it has to enable
1235 * acceleration after a mode change.
1236 */
1237 LogFlowFunc (("mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
1238 mfVideoAccelEnabled, fEnable, pVbvaMemory));
1239
1240 /* Strictly check parameters. Callers must not pass anything in the case. */
1241 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
1242
1243 if (!VideoAccelAllowed ())
1244 {
1245 return VERR_NOT_SUPPORTED;
1246 }
1247
1248 /*
1249 * Verify that the VM is in running state. If it is not,
1250 * then this must be postponed until it goes to running.
1251 */
1252 if (!mfMachineRunning)
1253 {
1254 Assert (!mfVideoAccelEnabled);
1255
1256 LogFlowFunc (("Machine is not yet running.\n"));
1257
1258 if (fEnable)
1259 {
1260 mfPendingVideoAccelEnable = fEnable;
1261 mpPendingVbvaMemory = pVbvaMemory;
1262 }
1263
1264 return rc;
1265 }
1266
1267 /* Check that current status is not being changed */
1268 if (mfVideoAccelEnabled == fEnable)
1269 {
1270 return rc;
1271 }
1272
1273 if (mfVideoAccelEnabled)
1274 {
1275 /* Process any pending orders and empty the VBVA ring buffer. */
1276#ifdef VBOX_WITH_OLD_VBVA_LOCK
1277 videoAccelFlush ();
1278#else
1279 VideoAccelFlush ();
1280#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
1281 }
1282
1283 if (!fEnable && mpVbvaMemory)
1284 {
1285 mpVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
1286 }
1287
1288 /* Safety precaution. There is no more VBVA until everything is setup! */
1289 mpVbvaMemory = NULL;
1290 mfVideoAccelEnabled = false;
1291
1292 /* Update entire display. */
1293 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].u32ResizeStatus == ResizeStatus_Void)
1294 {
1295 mpDrv->pUpPort->pfnUpdateDisplayAll(mpDrv->pUpPort);
1296 }
1297
1298 /* Everything OK. VBVA status can be changed. */
1299
1300 /* Notify the VMMDev, which saves VBVA status in the saved state,
1301 * and needs to know current status.
1302 */
1303 PPDMIVMMDEVPORT pVMMDevPort = mParent->getVMMDev()->getVMMDevPort ();
1304
1305 if (pVMMDevPort)
1306 {
1307 pVMMDevPort->pfnVBVAChange (pVMMDevPort, fEnable);
1308 }
1309
1310 if (fEnable)
1311 {
1312 mpVbvaMemory = pVbvaMemory;
1313 mfVideoAccelEnabled = true;
1314
1315 /* Initialize the hardware memory. */
1316 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
1317 mpVbvaMemory->off32Data = 0;
1318 mpVbvaMemory->off32Free = 0;
1319
1320 memset (mpVbvaMemory->aRecords, 0, sizeof (mpVbvaMemory->aRecords));
1321 mpVbvaMemory->indexRecordFirst = 0;
1322 mpVbvaMemory->indexRecordFree = 0;
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#ifdef DEBUG_sunlover
1820 LogFlowFunc(("\n"));
1821#endif
1822
1823 vbvaLock();
1824
1825 if (ASMAtomicCmpXchgU32(&mfu32PendingVideoAccelDisable, false, true))
1826 {
1827 videoAccelEnable (false, NULL);
1828 }
1829 else if (mfPendingVideoAccelEnable)
1830 {
1831 /* Acceleration was enabled while machine was not yet running
1832 * due to restoring from saved state. Update entire display and
1833 * actually enable acceleration.
1834 */
1835 Assert(mpPendingVbvaMemory);
1836
1837 /* Acceleration can not be yet enabled.*/
1838 Assert(mpVbvaMemory == NULL);
1839 Assert(!mfVideoAccelEnabled);
1840
1841 if (mfMachineRunning)
1842 {
1843 videoAccelEnable (mfPendingVideoAccelEnable,
1844 mpPendingVbvaMemory);
1845
1846 /* Reset the pending state. */
1847 mfPendingVideoAccelEnable = false;
1848 mpPendingVbvaMemory = NULL;
1849 }
1850
1851 rc = VINF_TRY_AGAIN;
1852 }
1853 else
1854 {
1855 Assert(mpPendingVbvaMemory == NULL);
1856
1857 if (mfVideoAccelEnabled)
1858 {
1859 Assert(mpVbvaMemory);
1860 videoAccelFlush ();
1861
1862 rc = VINF_SUCCESS; /* VBVA processed, no need to a display update. */
1863 }
1864 }
1865
1866 vbvaUnlock();
1867
1868#ifdef DEBUG_sunlover
1869 LogFlowFunc(("rc = %d\n"));
1870#endif
1871
1872 return rc;
1873}
1874#endif /* VBOX_WITH_OLD_VBVA_LOCK */
1875
1876
1877// IDisplay properties
1878/////////////////////////////////////////////////////////////////////////////
1879
1880/**
1881 * Returns the current display width in pixel
1882 *
1883 * @returns COM status code
1884 * @param width Address of result variable.
1885 */
1886STDMETHODIMP Display::COMGETTER(Width) (ULONG *width)
1887{
1888 CheckComArgNotNull(width);
1889
1890 AutoCaller autoCaller(this);
1891 CheckComRCReturnRC(autoCaller.rc());
1892
1893 AutoWriteLock alock(this);
1894
1895 CHECK_CONSOLE_DRV (mpDrv);
1896
1897 *width = mpDrv->Connector.cx;
1898
1899 return S_OK;
1900}
1901
1902/**
1903 * Returns the current display height in pixel
1904 *
1905 * @returns COM status code
1906 * @param height Address of result variable.
1907 */
1908STDMETHODIMP Display::COMGETTER(Height) (ULONG *height)
1909{
1910 CheckComArgNotNull(height);
1911
1912 AutoCaller autoCaller(this);
1913 CheckComRCReturnRC(autoCaller.rc());
1914
1915 AutoWriteLock alock(this);
1916
1917 CHECK_CONSOLE_DRV (mpDrv);
1918
1919 *height = mpDrv->Connector.cy;
1920
1921 return S_OK;
1922}
1923
1924/**
1925 * Returns the current display color depth in bits
1926 *
1927 * @returns COM status code
1928 * @param bitsPerPixel Address of result variable.
1929 */
1930STDMETHODIMP Display::COMGETTER(BitsPerPixel) (ULONG *bitsPerPixel)
1931{
1932 if (!bitsPerPixel)
1933 return E_INVALIDARG;
1934
1935 AutoCaller autoCaller(this);
1936 CheckComRCReturnRC(autoCaller.rc());
1937
1938 AutoWriteLock alock(this);
1939
1940 CHECK_CONSOLE_DRV (mpDrv);
1941
1942 uint32_t cBits = 0;
1943 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1944 AssertRC(rc);
1945 *bitsPerPixel = cBits;
1946
1947 return S_OK;
1948}
1949
1950
1951// IDisplay methods
1952/////////////////////////////////////////////////////////////////////////////
1953
1954STDMETHODIMP Display::SetFramebuffer (ULONG aScreenId,
1955 IFramebuffer *aFramebuffer)
1956{
1957 LogFlowFunc (("\n"));
1958
1959 if (aFramebuffer != NULL)
1960 CheckComArgOutPointerValid(aFramebuffer);
1961
1962 AutoCaller autoCaller(this);
1963 CheckComRCReturnRC(autoCaller.rc());
1964
1965 AutoWriteLock alock(this);
1966
1967 Console::SafeVMPtrQuiet pVM (mParent);
1968 if (pVM.isOk())
1969 {
1970 /* Must leave the lock here because the changeFramebuffer will
1971 * also obtain it. */
1972 alock.leave ();
1973
1974 /* send request to the EMT thread */
1975 int vrc = VMR3ReqCallWait (pVM, VMCPUID_ANY,
1976 (PFNRT) changeFramebuffer, 3, this, aFramebuffer, aScreenId);
1977
1978 alock.enter ();
1979
1980 ComAssertRCRet (vrc, E_FAIL);
1981 }
1982 else
1983 {
1984 /* No VM is created (VM is powered off), do a direct call */
1985 int vrc = changeFramebuffer (this, aFramebuffer, aScreenId);
1986 ComAssertRCRet (vrc, E_FAIL);
1987 }
1988
1989 return S_OK;
1990}
1991
1992STDMETHODIMP Display::GetFramebuffer (ULONG aScreenId,
1993 IFramebuffer **aFramebuffer, LONG *aXOrigin, LONG *aYOrigin)
1994{
1995 LogFlowFunc (("aScreenId = %d\n", aScreenId));
1996
1997 CheckComArgOutPointerValid(aFramebuffer);
1998
1999 AutoCaller autoCaller(this);
2000 CheckComRCReturnRC(autoCaller.rc());
2001
2002 AutoWriteLock alock(this);
2003
2004 /* @todo this should be actually done on EMT. */
2005 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
2006
2007 *aFramebuffer = pFBInfo->pFramebuffer;
2008 if (*aFramebuffer)
2009 (*aFramebuffer)->AddRef ();
2010 if (aXOrigin)
2011 *aXOrigin = pFBInfo->xOrigin;
2012 if (aYOrigin)
2013 *aYOrigin = pFBInfo->yOrigin;
2014
2015 return S_OK;
2016}
2017
2018STDMETHODIMP Display::SetVideoModeHint(ULONG aWidth, ULONG aHeight,
2019 ULONG aBitsPerPixel, ULONG aDisplay)
2020{
2021 AutoCaller autoCaller(this);
2022 CheckComRCReturnRC(autoCaller.rc());
2023
2024 AutoWriteLock alock(this);
2025
2026 CHECK_CONSOLE_DRV (mpDrv);
2027
2028 /*
2029 * Do some rough checks for valid input
2030 */
2031 ULONG width = aWidth;
2032 if (!width)
2033 width = mpDrv->Connector.cx;
2034 ULONG height = aHeight;
2035 if (!height)
2036 height = mpDrv->Connector.cy;
2037 ULONG bpp = aBitsPerPixel;
2038 if (!bpp)
2039 {
2040 uint32_t cBits = 0;
2041 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
2042 AssertRC(rc);
2043 bpp = cBits;
2044 }
2045 ULONG cMonitors;
2046 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
2047 if (cMonitors == 0 && aDisplay > 0)
2048 return E_INVALIDARG;
2049 if (aDisplay >= cMonitors)
2050 return E_INVALIDARG;
2051
2052// sunlover 20070614: It is up to the guest to decide whether the hint is valid.
2053// ULONG vramSize;
2054// mParent->machine()->COMGETTER(VRAMSize)(&vramSize);
2055// /* enough VRAM? */
2056// if ((width * height * (bpp / 8)) > (vramSize * 1024 * 1024))
2057// return setError(E_FAIL, tr("Not enough VRAM for the selected video mode"));
2058
2059 /* Have to leave the lock because the pfnRequestDisplayChange
2060 * will call EMT. */
2061 alock.leave ();
2062 if (mParent->getVMMDev())
2063 mParent->getVMMDev()->getVMMDevPort()->
2064 pfnRequestDisplayChange (mParent->getVMMDev()->getVMMDevPort(),
2065 aWidth, aHeight, aBitsPerPixel, aDisplay);
2066 return S_OK;
2067}
2068
2069STDMETHODIMP Display::SetSeamlessMode (BOOL enabled)
2070{
2071 AutoCaller autoCaller(this);
2072 CheckComRCReturnRC(autoCaller.rc());
2073
2074 AutoWriteLock alock(this);
2075
2076 /* Have to leave the lock because the pfnRequestSeamlessChange will call EMT. */
2077 alock.leave ();
2078 if (mParent->getVMMDev())
2079 mParent->getVMMDev()->getVMMDevPort()->
2080 pfnRequestSeamlessChange (mParent->getVMMDev()->getVMMDevPort(),
2081 !!enabled);
2082 return S_OK;
2083}
2084
2085#ifdef VBOX_WITH_OLD_VBVA_LOCK
2086int Display::displayTakeScreenshotEMT(Display *pDisplay, uint8_t **ppu8Data, size_t *pcbData, uint32_t *pu32Width, uint32_t *pu32Height)
2087{
2088 int rc;
2089 pDisplay->vbvaLock();
2090 rc = pDisplay->mpDrv->pUpPort->pfnTakeScreenshot(pDisplay->mpDrv->pUpPort, ppu8Data, pcbData, pu32Width, pu32Height);
2091 pDisplay->vbvaUnlock();
2092 return rc;
2093}
2094#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2095
2096#ifdef VBOX_WITH_OLD_VBVA_LOCK
2097static int displayTakeScreenshot(PVM pVM, Display *pDisplay, struct DRVMAINDISPLAY *pDrv, BYTE *address, ULONG width, ULONG height)
2098#else
2099static int displayTakeScreenshot(PVM pVM, struct DRVMAINDISPLAY *pDrv, BYTE *address, ULONG width, ULONG height)
2100#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2101{
2102 uint8_t *pu8Data = NULL;
2103 size_t cbData = 0;
2104 uint32_t cx = 0;
2105 uint32_t cy = 0;
2106
2107#ifdef VBOX_WITH_OLD_VBVA_LOCK
2108 int vrc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)Display::displayTakeScreenshotEMT, 5,
2109 pDisplay, &pu8Data, &cbData, &cx, &cy);
2110#else
2111 /* @todo pfnTakeScreenshot is probably callable from any thread, because it uses the VGA device lock. */
2112 int vrc = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)pDrv->pUpPort->pfnTakeScreenshot, 5,
2113 pDrv->pUpPort, &pu8Data, &cbData, &cx, &cy);
2114#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2115
2116 if (RT_SUCCESS(vrc))
2117 {
2118 if (cx == width && cy == height)
2119 {
2120 /* No scaling required. */
2121 memcpy(address, pu8Data, cbData);
2122 }
2123 else
2124 {
2125 /* Scale. */
2126 LogFlowFunc(("SCALE: %dx%d -> %dx%d\n", cx, cy, width, height));
2127
2128 uint8_t *dst = address;
2129 uint8_t *src = pu8Data;
2130 int dstX = 0;
2131 int dstY = 0;
2132 int srcX = 0;
2133 int srcY = 0;
2134 int dstW = width;
2135 int dstH = height;
2136 int srcW = cx;
2137 int srcH = cy;
2138 gdImageCopyResampled (dst,
2139 src,
2140 dstX, dstY,
2141 srcX, srcY,
2142 dstW, dstH, srcW, srcH);
2143 }
2144
2145 /* This can be called from any thread. */
2146 pDrv->pUpPort->pfnFreeScreenshot (pDrv->pUpPort, pu8Data);
2147 }
2148
2149 return vrc;
2150}
2151
2152STDMETHODIMP Display::TakeScreenShot (BYTE *address, ULONG width, ULONG height)
2153{
2154 /// @todo (r=dmik) this function may take too long to complete if the VM
2155 // is doing something like saving state right now. Which, in case if it
2156 // is called on the GUI thread, will make it unresponsive. We should
2157 // check the machine state here (by enclosing the check and VMRequCall
2158 // within the Console lock to make it atomic).
2159
2160 LogFlowFuncEnter();
2161 LogFlowFunc (("address=%p, width=%d, height=%d\n",
2162 address, width, height));
2163
2164 CheckComArgNotNull(address);
2165 CheckComArgExpr(width, width != 0);
2166 CheckComArgExpr(height, height != 0);
2167
2168 AutoCaller autoCaller(this);
2169 CheckComRCReturnRC(autoCaller.rc());
2170
2171 AutoWriteLock alock(this);
2172
2173 CHECK_CONSOLE_DRV (mpDrv);
2174
2175 Console::SafeVMPtr pVM (mParent);
2176 CheckComRCReturnRC(pVM.rc());
2177
2178 HRESULT rc = S_OK;
2179
2180 LogFlowFunc (("Sending SCREENSHOT request\n"));
2181
2182 /* Leave lock because other thread (EMT) is called and it may initiate a resize
2183 * which also needs lock.
2184 *
2185 * This method does not need the lock anymore.
2186 */
2187 alock.leave();
2188
2189#ifdef VBOX_WITH_OLD_VBVA_LOCK
2190 int vrc = displayTakeScreenshot(pVM, this, mpDrv, address, width, height);
2191#else
2192 int vrc = displayTakeScreenshot(pVM, mpDrv, address, width, height);
2193#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2194
2195 if (vrc == VERR_NOT_IMPLEMENTED)
2196 rc = setError (E_NOTIMPL,
2197 tr ("This feature is not implemented"));
2198 else if (RT_FAILURE(vrc))
2199 rc = setError (VBOX_E_IPRT_ERROR,
2200 tr ("Could not take a screenshot (%Rrc)"), vrc);
2201
2202 LogFlowFunc (("rc=%08X\n", rc));
2203 LogFlowFuncLeave();
2204 return rc;
2205}
2206
2207STDMETHODIMP Display::TakeScreenShotSlow (ULONG width, ULONG height,
2208 ComSafeArrayOut(BYTE, aScreenData))
2209{
2210 LogFlowFuncEnter();
2211 LogFlowFunc (("width=%d, height=%d\n",
2212 width, height));
2213
2214 CheckComArgSafeArrayNotNull(aScreenData);
2215 CheckComArgExpr(width, width != 0);
2216 CheckComArgExpr(height, height != 0);
2217
2218 AutoCaller autoCaller(this);
2219 CheckComRCReturnRC(autoCaller.rc());
2220
2221 AutoWriteLock alock(this);
2222
2223 CHECK_CONSOLE_DRV (mpDrv);
2224
2225 Console::SafeVMPtr pVM (mParent);
2226 CheckComRCReturnRC(pVM.rc());
2227
2228 HRESULT rc = S_OK;
2229
2230 LogFlowFunc (("Sending SCREENSHOT request\n"));
2231
2232 /* Leave lock because other thread (EMT) is called and it may initiate a resize
2233 * which also needs lock.
2234 *
2235 * This method does not need the lock anymore.
2236 */
2237 alock.leave();
2238
2239 size_t cbData = width * 4 * height;
2240 uint8_t *pu8Data = (uint8_t *)RTMemAlloc(cbData);
2241
2242 if (!pu8Data)
2243 return E_OUTOFMEMORY;
2244
2245#ifdef VBOX_WITH_OLD_VBVA_LOCK
2246 int vrc = displayTakeScreenshot(pVM, this, mpDrv, pu8Data, width, height);
2247#else
2248 int vrc = displayTakeScreenshot(pVM, mpDrv, pu8Data, width, height);
2249#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2250
2251 if (RT_SUCCESS(vrc))
2252 {
2253 /* Convert pixels to format expected by the API caller: [0] R, [1] G, [2] B, [3] A. */
2254 uint8_t *pu8 = pu8Data;
2255 unsigned cPixels = width * height;
2256 while (cPixels)
2257 {
2258 uint8_t u8 = pu8[0];
2259 pu8[0] = pu8[2];
2260 pu8[2] = u8;
2261 pu8[3] = 0xff;
2262 cPixels--;
2263 pu8 += 4;
2264 }
2265
2266 com::SafeArray<BYTE> screenData (cbData);
2267 for (unsigned i = 0; i < cbData; i++)
2268 screenData[i] = pu8Data[i];
2269 screenData.detachTo(ComSafeArrayOutArg(aScreenData));
2270 }
2271 else if (vrc == VERR_NOT_IMPLEMENTED)
2272 rc = setError (E_NOTIMPL,
2273 tr ("This feature is not implemented"));
2274 else
2275 rc = setError (VBOX_E_IPRT_ERROR,
2276 tr ("Could not take a screenshot (%Rrc)"), vrc);
2277
2278 LogFlowFunc (("rc=%08X\n", rc));
2279 LogFlowFuncLeave();
2280 return rc;
2281}
2282
2283
2284STDMETHODIMP Display::DrawToScreen (BYTE *address, ULONG x, ULONG y,
2285 ULONG width, ULONG height)
2286{
2287 /// @todo (r=dmik) this function may take too long to complete if the VM
2288 // is doing something like saving state right now. Which, in case if it
2289 // is called on the GUI thread, will make it unresponsive. We should
2290 // check the machine state here (by enclosing the check and VMRequCall
2291 // within the Console lock to make it atomic).
2292
2293 LogFlowFuncEnter();
2294 LogFlowFunc (("address=%p, x=%d, y=%d, width=%d, height=%d\n",
2295 (void *)address, x, y, width, height));
2296
2297 CheckComArgNotNull(address);
2298 CheckComArgExpr(width, width != 0);
2299 CheckComArgExpr(height, height != 0);
2300
2301 AutoCaller autoCaller(this);
2302 CheckComRCReturnRC(autoCaller.rc());
2303
2304 AutoWriteLock alock(this);
2305
2306 CHECK_CONSOLE_DRV (mpDrv);
2307
2308 Console::SafeVMPtr pVM (mParent);
2309 CheckComRCReturnRC(pVM.rc());
2310
2311 /*
2312 * Again we're lazy and make the graphics device do all the
2313 * dirty conversion work.
2314 */
2315#ifdef VBOX_WITH_OLD_VBVA_LOCK
2316 int rcVBox = 0;
2317// @todo vbva int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)Display::DrawToScreenEMT, 6,
2318// this, address, x, y, width, height);
2319#else
2320 int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)mpDrv->pUpPort->pfnDisplayBlt, 6,
2321 mpDrv->pUpPort, address, x, y, width, height);
2322#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2323
2324 /*
2325 * If the function returns not supported, we'll have to do all the
2326 * work ourselves using the framebuffer.
2327 */
2328 HRESULT rc = S_OK;
2329 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2330 {
2331 /** @todo implement generic fallback for screen blitting. */
2332 rc = E_NOTIMPL;
2333 }
2334 else if (RT_FAILURE(rcVBox))
2335 rc = setError (VBOX_E_IPRT_ERROR,
2336 tr ("Could not draw to the screen (%Rrc)"), rcVBox);
2337//@todo
2338// else
2339// {
2340// /* All ok. Redraw the screen. */
2341// handleDisplayUpdate (x, y, width, height);
2342// }
2343
2344 LogFlowFunc (("rc=%08X\n", rc));
2345 LogFlowFuncLeave();
2346 return rc;
2347}
2348
2349/**
2350 * Does a full invalidation of the VM display and instructs the VM
2351 * to update it immediately.
2352 *
2353 * @returns COM status code
2354 */
2355STDMETHODIMP Display::InvalidateAndUpdate()
2356{
2357 LogFlowFuncEnter();
2358
2359 AutoCaller autoCaller(this);
2360 CheckComRCReturnRC(autoCaller.rc());
2361
2362 AutoWriteLock alock(this);
2363
2364 CHECK_CONSOLE_DRV (mpDrv);
2365
2366 Console::SafeVMPtr pVM (mParent);
2367 CheckComRCReturnRC(pVM.rc());
2368
2369 HRESULT rc = S_OK;
2370
2371 LogFlowFunc (("Sending DPYUPDATE request\n"));
2372
2373 /* Have to leave the lock when calling EMT. */
2374 alock.leave ();
2375
2376 /* pdm.h says that this has to be called from the EMT thread */
2377#ifdef VBOX_WITH_OLD_VBVA_LOCK
2378 int rcVBox = 0;
2379// #todo vbva int rcVBox = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY, (PFNRT)Display::InvalidateAndUpdateEMT,
2380// 1, this);
2381#else
2382 int rcVBox = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY,
2383 (PFNRT)mpDrv->pUpPort->pfnUpdateDisplayAll, 1, mpDrv->pUpPort);
2384#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2385 alock.enter ();
2386
2387 if (RT_FAILURE(rcVBox))
2388 rc = setError (VBOX_E_IPRT_ERROR,
2389 tr ("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2390
2391 LogFlowFunc (("rc=%08X\n", rc));
2392 LogFlowFuncLeave();
2393 return rc;
2394}
2395
2396/**
2397 * Notification that the framebuffer has completed the
2398 * asynchronous resize processing
2399 *
2400 * @returns COM status code
2401 */
2402STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
2403{
2404 LogFlowFunc (("\n"));
2405
2406 /// @todo (dmik) can we AutoWriteLock alock(this); here?
2407 // do it when we switch this class to VirtualBoxBase_NEXT.
2408 // This will require general code review and may add some details.
2409 // In particular, we may want to check whether EMT is really waiting for
2410 // this notification, etc. It might be also good to obey the caller to make
2411 // sure this method is not called from more than one thread at a time
2412 // (and therefore don't use Display lock at all here to save some
2413 // milliseconds).
2414 AutoCaller autoCaller(this);
2415 CheckComRCReturnRC(autoCaller.rc());
2416
2417 /* this is only valid for external framebuffers */
2418 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
2419 return setError (VBOX_E_NOT_SUPPORTED,
2420 tr ("Resize completed notification is valid only "
2421 "for external framebuffers"));
2422
2423 /* Set the flag indicating that the resize has completed and display
2424 * data need to be updated. */
2425 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
2426 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
2427 AssertRelease(f);NOREF(f);
2428
2429 return S_OK;
2430}
2431
2432/**
2433 * Notification that the framebuffer has completed the
2434 * asynchronous update processing
2435 *
2436 * @returns COM status code
2437 */
2438STDMETHODIMP Display::UpdateCompleted()
2439{
2440 LogFlowFunc (("\n"));
2441
2442 /// @todo (dmik) can we AutoWriteLock alock(this); here?
2443 // do it when we switch this class to VirtualBoxBase_NEXT.
2444 // Tthis will require general code review and may add some details.
2445 // In particular, we may want to check whether EMT is really waiting for
2446 // this notification, etc. It might be also good to obey the caller to make
2447 // sure this method is not called from more than one thread at a time
2448 // (and therefore don't use Display lock at all here to save some
2449 // milliseconds).
2450 AutoCaller autoCaller(this);
2451 CheckComRCReturnRC(autoCaller.rc());
2452
2453 /* this is only valid for external framebuffers */
2454 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer == NULL)
2455 return setError (VBOX_E_NOT_SUPPORTED,
2456 tr ("Resize completed notification is valid only "
2457 "for external framebuffers"));
2458
2459 return S_OK;
2460}
2461
2462STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
2463{
2464#ifdef VBOX_WITH_VIDEOHWACCEL
2465 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsynch(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
2466 return S_OK;
2467#else
2468 return E_NOTIMPL;
2469#endif
2470}
2471
2472// private methods
2473/////////////////////////////////////////////////////////////////////////////
2474
2475/**
2476 * Helper to update the display information from the framebuffer.
2477 *
2478 * @param aCheckParams true to compare the parameters of the current framebuffer
2479 * and the new one and issue handleDisplayResize()
2480 * if they differ.
2481 * @thread EMT
2482 */
2483void Display::updateDisplayData (bool aCheckParams /* = false */)
2484{
2485 /* the driver might not have been constructed yet */
2486 if (!mpDrv)
2487 return;
2488
2489#if DEBUG
2490 /*
2491 * Sanity check. Note that this method may be called on EMT after Console
2492 * has started the power down procedure (but before our #drvDestruct() is
2493 * called, in which case pVM will aleady be NULL but mpDrv will not). Since
2494 * we don't really need pVM to proceed, we avoid this check in the release
2495 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
2496 * time-critical method.
2497 */
2498 Console::SafeVMPtrQuiet pVM (mParent);
2499 if (pVM.isOk())
2500 VM_ASSERT_EMT (pVM.raw());
2501#endif
2502
2503 /* The method is only relevant to the primary framebuffer. */
2504 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
2505
2506 if (pFramebuffer)
2507 {
2508 HRESULT rc;
2509 BYTE *address = 0;
2510 rc = pFramebuffer->COMGETTER(Address) (&address);
2511 AssertComRC (rc);
2512 ULONG bytesPerLine = 0;
2513 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
2514 AssertComRC (rc);
2515 ULONG bitsPerPixel = 0;
2516 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
2517 AssertComRC (rc);
2518 ULONG width = 0;
2519 rc = pFramebuffer->COMGETTER(Width) (&width);
2520 AssertComRC (rc);
2521 ULONG height = 0;
2522 rc = pFramebuffer->COMGETTER(Height) (&height);
2523 AssertComRC (rc);
2524
2525 /*
2526 * Check current parameters with new ones and issue handleDisplayResize()
2527 * to let the new frame buffer adjust itself properly. Note that it will
2528 * result into a recursive updateDisplayData() call but with
2529 * aCheckOld = false.
2530 */
2531 if (aCheckParams &&
2532 (mLastAddress != address ||
2533 mLastBytesPerLine != bytesPerLine ||
2534 mLastBitsPerPixel != bitsPerPixel ||
2535 mLastWidth != (int) width ||
2536 mLastHeight != (int) height))
2537 {
2538 handleDisplayResize (VBOX_VIDEO_PRIMARY_SCREEN, mLastBitsPerPixel,
2539 mLastAddress,
2540 mLastBytesPerLine,
2541 mLastWidth,
2542 mLastHeight);
2543 return;
2544 }
2545
2546 mpDrv->Connector.pu8Data = (uint8_t *) address;
2547 mpDrv->Connector.cbScanline = bytesPerLine;
2548 mpDrv->Connector.cBits = bitsPerPixel;
2549 mpDrv->Connector.cx = width;
2550 mpDrv->Connector.cy = height;
2551 }
2552 else
2553 {
2554 /* black hole */
2555 mpDrv->Connector.pu8Data = NULL;
2556 mpDrv->Connector.cbScanline = 0;
2557 mpDrv->Connector.cBits = 0;
2558 mpDrv->Connector.cx = 0;
2559 mpDrv->Connector.cy = 0;
2560 }
2561}
2562
2563/**
2564 * Changes the current frame buffer. Called on EMT to avoid both
2565 * race conditions and excessive locking.
2566 *
2567 * @note locks this object for writing
2568 * @thread EMT
2569 */
2570/* static */
2571DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
2572 unsigned uScreenId)
2573{
2574 LogFlowFunc (("uScreenId = %d\n", uScreenId));
2575
2576 AssertReturn(that, VERR_INVALID_PARAMETER);
2577 AssertReturn(uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
2578
2579 AutoCaller autoCaller(that);
2580 CheckComRCReturnRC(autoCaller.rc());
2581
2582 AutoWriteLock alock(that);
2583
2584 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
2585 pDisplayFBInfo->pFramebuffer = aFB;
2586
2587 that->mParent->consoleVRDPServer()->SendResize ();
2588
2589 that->updateDisplayData (true /* aCheckParams */);
2590
2591 return VINF_SUCCESS;
2592}
2593
2594/**
2595 * Handle display resize event issued by the VGA device for the primary screen.
2596 *
2597 * @see PDMIDISPLAYCONNECTOR::pfnResize
2598 */
2599DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
2600 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
2601{
2602 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2603
2604 LogFlowFunc (("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
2605 bpp, pvVRAM, cbLine, cx, cy));
2606
2607 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy);
2608}
2609
2610/**
2611 * Handle display update.
2612 *
2613 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
2614 */
2615DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
2616 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
2617{
2618 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2619
2620#ifdef DEBUG_sunlover
2621 LogFlowFunc (("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
2622 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
2623#endif /* DEBUG_sunlover */
2624
2625 /* This call does update regardless of VBVA status.
2626 * But in VBVA mode this is called only as result of
2627 * pfnUpdateDisplayAll in the VGA device.
2628 */
2629
2630 pDrv->pDisplay->handleDisplayUpdate(x, y, cx, cy);
2631}
2632
2633/**
2634 * Periodic display refresh callback.
2635 *
2636 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
2637 */
2638DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
2639{
2640 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2641
2642#ifdef DEBUG_sunlover
2643 STAM_PROFILE_START(&StatDisplayRefresh, a);
2644#endif /* DEBUG_sunlover */
2645
2646#ifdef DEBUG_sunlover_2
2647 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
2648 pDrv->pDisplay->mfVideoAccelEnabled));
2649#endif /* DEBUG_sunlover_2 */
2650
2651 Display *pDisplay = pDrv->pDisplay;
2652 bool fNoUpdate = false; /* Do not update the display if any of the framebuffers is being resized. */
2653 unsigned uScreenId;
2654
2655 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2656 {
2657 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2658
2659 /* Check the resize status. The status can be checked normally because
2660 * the status affects only the EMT.
2661 */
2662 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
2663
2664 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
2665 {
2666 LogFlowFunc (("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
2667 fNoUpdate = true; /* Always set it here, because pfnUpdateDisplayAll can cause a new resize. */
2668 /* The framebuffer was resized and display data need to be updated. */
2669 pDisplay->handleResizeCompletedEMT ();
2670 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
2671 {
2672 /* The resize status could be not Void here because a pending resize is issued. */
2673 continue;
2674 }
2675 /* Continue with normal processing because the status here is ResizeStatus_Void. */
2676 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2677 {
2678 /* Repaint the display because VM continued to run during the framebuffer resize. */
2679 if (!pFBInfo->pFramebuffer.isNull())
2680#ifdef VBOX_WITH_OLD_VBVA_LOCK
2681 {
2682 pDisplay->vbvaLock();
2683#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2684 pDrv->pUpPort->pfnUpdateDisplayAll(pDrv->pUpPort);
2685#ifdef VBOX_WITH_OLD_VBVA_LOCK
2686 pDisplay->vbvaUnlock();
2687 }
2688#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2689 }
2690 }
2691 else if (u32ResizeStatus == ResizeStatus_InProgress)
2692 {
2693 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
2694 LogFlowFunc (("ResizeStatus_InProcess\n"));
2695 fNoUpdate = true;
2696 continue;
2697 }
2698 }
2699
2700 if (!fNoUpdate)
2701 {
2702#ifdef VBOX_WITH_OLD_VBVA_LOCK
2703 int rc = pDisplay->videoAccelRefreshProcess();
2704
2705 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
2706 {
2707 if (rc == VWRN_INVALID_STATE)
2708 {
2709 /* No VBVA do a display update. */
2710 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
2711 if (!pFBInfo->pFramebuffer.isNull())
2712 {
2713 Assert(pDrv->Connector.pu8Data);
2714 Assert(pFBInfo->u32ResizeStatus == ResizeStatus_Void);
2715 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
2716 }
2717 }
2718
2719 /* Inform the VRDP server that the current display update sequence is
2720 * completed. At this moment the framebuffer memory contains a definite
2721 * image, that is synchronized with the orders already sent to VRDP client.
2722 * The server can now process redraw requests from clients or initial
2723 * fullscreen updates for new clients.
2724 */
2725 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2726 {
2727 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2728
2729 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
2730 {
2731 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
2732 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
2733 }
2734 }
2735 }
2736#else
2737 if (pDisplay->mfPendingVideoAccelEnable)
2738 {
2739 /* Acceleration was enabled while machine was not yet running
2740 * due to restoring from saved state. Update entire display and
2741 * actually enable acceleration.
2742 */
2743 Assert(pDisplay->mpPendingVbvaMemory);
2744
2745 /* Acceleration can not be yet enabled.*/
2746 Assert(pDisplay->mpVbvaMemory == NULL);
2747 Assert(!pDisplay->mfVideoAccelEnabled);
2748
2749 if (pDisplay->mfMachineRunning)
2750 {
2751 pDisplay->VideoAccelEnable (pDisplay->mfPendingVideoAccelEnable,
2752 pDisplay->mpPendingVbvaMemory);
2753
2754 /* Reset the pending state. */
2755 pDisplay->mfPendingVideoAccelEnable = false;
2756 pDisplay->mpPendingVbvaMemory = NULL;
2757 }
2758 }
2759 else
2760 {
2761 Assert(pDisplay->mpPendingVbvaMemory == NULL);
2762
2763 if (pDisplay->mfVideoAccelEnabled)
2764 {
2765 Assert(pDisplay->mpVbvaMemory);
2766 pDisplay->VideoAccelFlush ();
2767 }
2768 else
2769 {
2770 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
2771 if (!pFBInfo->pFramebuffer.isNull())
2772 {
2773 Assert(pDrv->Connector.pu8Data);
2774 Assert(pFBInfo->u32ResizeStatus == ResizeStatus_Void);
2775 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
2776 }
2777 }
2778
2779 /* Inform the VRDP server that the current display update sequence is
2780 * completed. At this moment the framebuffer memory contains a definite
2781 * image, that is synchronized with the orders already sent to VRDP client.
2782 * The server can now process redraw requests from clients or initial
2783 * fullscreen updates for new clients.
2784 */
2785 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2786 {
2787 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2788
2789 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
2790 {
2791 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
2792 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
2793 }
2794 }
2795 }
2796#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2797 }
2798
2799#ifdef DEBUG_sunlover
2800 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
2801#endif /* DEBUG_sunlover */
2802#ifdef DEBUG_sunlover_2
2803 LogFlowFunc (("leave\n"));
2804#endif /* DEBUG_sunlover_2 */
2805}
2806
2807/**
2808 * Reset notification
2809 *
2810 * @see PDMIDISPLAYCONNECTOR::pfnReset
2811 */
2812DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
2813{
2814 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2815
2816 LogFlowFunc (("\n"));
2817
2818 /* Disable VBVA mode. */
2819 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2820}
2821
2822/**
2823 * LFBModeChange notification
2824 *
2825 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
2826 */
2827DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
2828{
2829 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2830
2831 LogFlowFunc (("fEnabled=%d\n", fEnabled));
2832
2833 NOREF(fEnabled);
2834
2835 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
2836#ifdef VBOX_WITH_OLD_VBVA_LOCK
2837 /* This is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
2838 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
2839#else
2840 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2841#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2842}
2843
2844/**
2845 * Adapter information change notification.
2846 *
2847 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
2848 */
2849DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
2850{
2851 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2852
2853 if (pvVRAM == NULL)
2854 {
2855 unsigned i;
2856 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
2857 {
2858 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
2859
2860 pFBInfo->u32Offset = 0;
2861 pFBInfo->u32MaxFramebufferSize = 0;
2862 pFBInfo->u32InformationSize = 0;
2863 }
2864 }
2865#ifndef VBOX_WITH_HGSMI
2866 else
2867 {
2868 uint8_t *pu8 = (uint8_t *)pvVRAM;
2869 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2870
2871 // @todo
2872 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2873
2874 VBOXVIDEOINFOHDR *pHdr;
2875
2876 for (;;)
2877 {
2878 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2879 pu8 += sizeof (VBOXVIDEOINFOHDR);
2880
2881 if (pu8 >= pu8End)
2882 {
2883 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
2884 break;
2885 }
2886
2887 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
2888 {
2889 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
2890 {
2891 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
2892 break;
2893 }
2894
2895 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
2896
2897 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
2898 {
2899 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
2900 break;
2901 }
2902
2903 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
2904
2905 pFBInfo->u32Offset = pDisplay->u32Offset;
2906 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
2907 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
2908
2909 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));
2910 }
2911 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
2912 {
2913 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
2914 {
2915 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
2916 break;
2917 }
2918
2919 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
2920
2921 switch (pConf32->u32Index)
2922 {
2923 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
2924 {
2925 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
2926 } break;
2927
2928 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
2929 {
2930 /* @todo make configurable. */
2931 pConf32->u32Value = _1M;
2932 } break;
2933
2934 default:
2935 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
2936 }
2937 }
2938 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2939 {
2940 if (pHdr->u16Length != 0)
2941 {
2942 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2943 break;
2944 }
2945
2946 break;
2947 }
2948 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
2949 {
2950 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
2951 }
2952
2953 pu8 += pHdr->u16Length;
2954 }
2955 }
2956#endif /* !VBOX_WITH_HGSMI */
2957}
2958
2959/**
2960 * Display information change notification.
2961 *
2962 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
2963 */
2964DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
2965{
2966 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2967
2968 if (uScreenId >= pDrv->pDisplay->mcMonitors)
2969 {
2970 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
2971 return;
2972 }
2973
2974 /* Get the display information structure. */
2975 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
2976
2977 uint8_t *pu8 = (uint8_t *)pvVRAM;
2978 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
2979
2980 // @todo
2981 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
2982
2983 VBOXVIDEOINFOHDR *pHdr;
2984
2985 for (;;)
2986 {
2987 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2988 pu8 += sizeof (VBOXVIDEOINFOHDR);
2989
2990 if (pu8 >= pu8End)
2991 {
2992 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
2993 break;
2994 }
2995
2996 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
2997 {
2998 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
2999 {
3000 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
3001 break;
3002 }
3003
3004 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
3005
3006 pFBInfo->xOrigin = pScreen->xOrigin;
3007 pFBInfo->yOrigin = pScreen->yOrigin;
3008
3009 pFBInfo->w = pScreen->u16Width;
3010 pFBInfo->h = pScreen->u16Height;
3011
3012 LogFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
3013 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
3014
3015 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
3016 {
3017 /* Primary screen resize is initiated by the VGA device. */
3018 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height);
3019 }
3020 }
3021 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3022 {
3023 if (pHdr->u16Length != 0)
3024 {
3025 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3026 break;
3027 }
3028
3029 break;
3030 }
3031 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
3032 {
3033 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
3034 {
3035 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
3036 break;
3037 }
3038
3039 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
3040
3041 pFBInfo->pHostEvents = pHostEvents;
3042
3043 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
3044 pHostEvents));
3045 }
3046 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
3047 {
3048 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
3049 {
3050 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
3051 break;
3052 }
3053
3054 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
3055 pu8 += pLink->i32Offset;
3056 }
3057 else
3058 {
3059 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
3060 }
3061
3062 pu8 += pHdr->u16Length;
3063 }
3064}
3065
3066#ifdef VBOX_WITH_VIDEOHWACCEL
3067
3068void Display::handleVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3069{
3070 unsigned id = (unsigned)pCommand->iDisplay;
3071 int rc = VINF_SUCCESS;
3072 if(id < mcMonitors)
3073 {
3074 IFramebuffer *pFramebuffer = maFramebuffers[id].pFramebuffer;
3075
3076 if (pFramebuffer != NULL)
3077 {
3078 pFramebuffer->Lock();
3079
3080 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3081 if(FAILED(hr))
3082 {
3083 rc = (hr == E_NOTIMPL) ? VERR_NOT_IMPLEMENTED : VERR_GENERAL_FAILURE;
3084 }
3085
3086 pFramebuffer->Unlock();
3087 }
3088 else
3089 {
3090 rc = VERR_NOT_IMPLEMENTED;
3091 }
3092 }
3093 else
3094 {
3095 rc = VERR_INVALID_PARAMETER;
3096 }
3097
3098 if(RT_FAILURE(rc))
3099 {
3100 /* tell the guest the command is complete */
3101 pCommand->Flags &= (~VBOXVHWACMD_FLAG_HG_ASYNCH);
3102 pCommand->rc = rc;
3103 }
3104}
3105
3106DECLCALLBACK(void) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3107{
3108 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3109
3110 pDrv->pDisplay->handleVHWACommandProcess(pInterface, pCommand);
3111}
3112#endif
3113
3114#ifdef VBOX_WITH_HGSMI
3115DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3116{
3117 LogFlowFunc(("uScreenId %d\n", uScreenId));
3118
3119 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3120 Display *pThis = pDrv->pDisplay;
3121
3122 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3123
3124 return VINF_SUCCESS;
3125}
3126
3127DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3128{
3129 LogFlowFunc(("uScreenId %d\n", uScreenId));
3130
3131 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3132 Display *pThis = pDrv->pDisplay;
3133
3134 pThis->maFramebuffers[uScreenId].fVBVAEnabled = false;
3135}
3136
3137DECLCALLBACK(void) Display::displayVBVAUpdateBegin(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 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3144
3145 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
3146 {
3147 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
3148 {
3149 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
3150 * under display device lock, so thread safe.
3151 */
3152 pFBInfo->cVBVASkipUpdate = 0;
3153 pThis->handleDisplayUpdate(pFBInfo->vbvaSkippedRect.xLeft,
3154 pFBInfo->vbvaSkippedRect.yTop,
3155 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
3156 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
3157 }
3158 }
3159 else
3160 {
3161 /* The framebuffer is being resized. */
3162 pFBInfo->cVBVASkipUpdate++;
3163 }
3164}
3165
3166DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, const PVBVACMDHDR pCmd, size_t cbCmd)
3167{
3168 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d\n", uScreenId, pCmd, cbCmd));
3169
3170 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3171 Display *pThis = pDrv->pDisplay;
3172 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3173
3174 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3175 {
3176 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, cbCmd);
3177 }
3178}
3179
3180DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y, uint32_t cx, uint32_t cy)
3181{
3182 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3183
3184 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3185 Display *pThis = pDrv->pDisplay;
3186 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3187
3188 /* @todo handleFramebufferUpdate (uScreenId,
3189 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3190 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3191 * cx, cy);
3192 */
3193 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3194 {
3195 pThis->handleDisplayUpdate(x, y, cx, cy);
3196 }
3197 else
3198 {
3199 /* Save the updated rectangle. */
3200 int32_t xRight = x + cx;
3201 int32_t yBottom = y + cy;
3202
3203 if (pFBInfo->cVBVASkipUpdate == 1)
3204 {
3205 pFBInfo->vbvaSkippedRect.xLeft = x;
3206 pFBInfo->vbvaSkippedRect.yTop = y;
3207 pFBInfo->vbvaSkippedRect.xRight = xRight;
3208 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3209 }
3210 else
3211 {
3212 if (pFBInfo->vbvaSkippedRect.xLeft > x)
3213 {
3214 pFBInfo->vbvaSkippedRect.xLeft = x;
3215 }
3216 if (pFBInfo->vbvaSkippedRect.yTop > y)
3217 {
3218 pFBInfo->vbvaSkippedRect.yTop = y;
3219 }
3220 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
3221 {
3222 pFBInfo->vbvaSkippedRect.xRight = xRight;
3223 }
3224 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
3225 {
3226 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3227 }
3228 }
3229 }
3230}
3231
3232DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
3233{
3234 LogFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3235
3236 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3237 Display *pThis = pDrv->pDisplay;
3238
3239 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
3240
3241 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
3242 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
3243 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3244
3245 pFBInfo->xOrigin = pScreen->i32OriginX;
3246 pFBInfo->yOrigin = pScreen->i32OriginY;
3247
3248 pFBInfo->w = pScreen->u32Width;
3249 pFBInfo->h = pScreen->u32Height;
3250
3251 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
3252 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
3253 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height);
3254}
3255
3256DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
3257 uint32_t xHot, uint32_t yHot,
3258 uint32_t cx, uint32_t cy,
3259 const void *pvShape)
3260{
3261 LogFlowFunc(("\n"));
3262
3263 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3264 Display *pThis = pDrv->pDisplay;
3265
3266 /* Tell the console about it */
3267 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
3268 xHot, yHot, cx, cy, (void *)pvShape);
3269
3270 return VINF_SUCCESS;
3271}
3272#endif /* VBOX_WITH_HGSMI */
3273
3274/**
3275 * Queries an interface to the driver.
3276 *
3277 * @returns Pointer to interface.
3278 * @returns NULL if the interface was not supported by the driver.
3279 * @param pInterface Pointer to this interface structure.
3280 * @param enmInterface The requested interface identification.
3281 */
3282DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
3283{
3284 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
3285 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3286 switch (enmInterface)
3287 {
3288 case PDMINTERFACE_BASE:
3289 return &pDrvIns->IBase;
3290 case PDMINTERFACE_DISPLAY_CONNECTOR:
3291 return &pDrv->Connector;
3292 default:
3293 return NULL;
3294 }
3295}
3296
3297
3298/**
3299 * Destruct a display driver instance.
3300 *
3301 * @returns VBox status.
3302 * @param pDrvIns The driver instance data.
3303 */
3304DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
3305{
3306 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3307 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
3308 if (pData->pDisplay)
3309 {
3310 AutoWriteLock displayLock (pData->pDisplay);
3311 pData->pDisplay->mpDrv = NULL;
3312 pData->pDisplay->mpVMMDev = NULL;
3313 pData->pDisplay->mLastAddress = NULL;
3314 pData->pDisplay->mLastBytesPerLine = 0;
3315 pData->pDisplay->mLastBitsPerPixel = 0,
3316 pData->pDisplay->mLastWidth = 0;
3317 pData->pDisplay->mLastHeight = 0;
3318 }
3319}
3320
3321
3322/**
3323 * Construct a display driver instance.
3324 *
3325 * @copydoc FNPDMDRVCONSTRUCT
3326 */
3327DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle, uint32_t fFlags)
3328{
3329 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3330 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
3331
3332 /*
3333 * Validate configuration.
3334 */
3335 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
3336 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
3337 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
3338 ("Configuration error: Not possible to attach anything to this driver!\n"),
3339 VERR_PDM_DRVINS_NO_ATTACH);
3340
3341 /*
3342 * Init Interfaces.
3343 */
3344 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
3345
3346 pData->Connector.pfnResize = Display::displayResizeCallback;
3347 pData->Connector.pfnUpdateRect = Display::displayUpdateCallback;
3348 pData->Connector.pfnRefresh = Display::displayRefreshCallback;
3349 pData->Connector.pfnReset = Display::displayResetCallback;
3350 pData->Connector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
3351 pData->Connector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
3352 pData->Connector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
3353#ifdef VBOX_WITH_VIDEOHWACCEL
3354 pData->Connector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
3355#endif
3356#ifdef VBOX_WITH_HGSMI
3357 pData->Connector.pfnVBVAEnable = Display::displayVBVAEnable;
3358 pData->Connector.pfnVBVADisable = Display::displayVBVADisable;
3359 pData->Connector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
3360 pData->Connector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
3361 pData->Connector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
3362 pData->Connector.pfnVBVAResize = Display::displayVBVAResize;
3363 pData->Connector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
3364#endif
3365
3366
3367 /*
3368 * Get the IDisplayPort interface of the above driver/device.
3369 */
3370 pData->pUpPort = (PPDMIDISPLAYPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_PORT);
3371 if (!pData->pUpPort)
3372 {
3373 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
3374 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3375 }
3376#if defined(VBOX_WITH_VIDEOHWACCEL)
3377 pData->pVBVACallbacks = (PPDMDDISPLAYVBVACALLBACKS)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_VBVA_CALLBACKS);
3378 if (!pData->pVBVACallbacks)
3379 {
3380 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
3381 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3382 }
3383#endif
3384 /*
3385 * Get the Display object pointer and update the mpDrv member.
3386 */
3387 void *pv;
3388 int rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
3389 if (RT_FAILURE(rc))
3390 {
3391 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
3392 return rc;
3393 }
3394 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
3395 pData->pDisplay->mpDrv = pData;
3396
3397 /*
3398 * Update our display information according to the framebuffer
3399 */
3400 pData->pDisplay->updateDisplayData();
3401
3402 /*
3403 * Start periodic screen refreshes
3404 */
3405 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
3406
3407 return VINF_SUCCESS;
3408}
3409
3410
3411/**
3412 * Display driver registration record.
3413 */
3414const PDMDRVREG Display::DrvReg =
3415{
3416 /* u32Version */
3417 PDM_DRVREG_VERSION,
3418 /* szDriverName */
3419 "MainDisplay",
3420 /* pszDescription */
3421 "Main display driver (Main as in the API).",
3422 /* fFlags */
3423 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
3424 /* fClass. */
3425 PDM_DRVREG_CLASS_DISPLAY,
3426 /* cMaxInstances */
3427 ~0,
3428 /* cbInstance */
3429 sizeof(DRVMAINDISPLAY),
3430 /* pfnConstruct */
3431 Display::drvConstruct,
3432 /* pfnDestruct */
3433 Display::drvDestruct,
3434 /* pfnIOCtl */
3435 NULL,
3436 /* pfnPowerOn */
3437 NULL,
3438 /* pfnReset */
3439 NULL,
3440 /* pfnSuspend */
3441 NULL,
3442 /* pfnResume */
3443 NULL,
3444 /* pfnAttach */
3445 NULL,
3446 /* pfnDetach */
3447 NULL,
3448 /* pfnPowerOff */
3449 NULL,
3450 /* pfnSoftReset */
3451 NULL,
3452 /* u32EndVersion */
3453 PDM_DRVREG_VERSION
3454};
3455/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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