VirtualBox

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

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

Main/Display: seamless for multimonitor, fix for not Windows guests. (xTracker 4655)

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