VirtualBox

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

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

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

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

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