VirtualBox

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

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

DisplayImpl: VBVA lock (incomplete, xTracker 4463).

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