VirtualBox

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

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

HGSMI/VBVA: enable VRDP commands only if there are connected RDP clients.

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

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