VirtualBox

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

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

Main/Display: VBVA should call Framebuffer::RequestResize only if framebuffer parameters change (xTracker 4655).

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

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