VirtualBox

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

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

DisplayImpl: VBVA lock (xTracker 4463).

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