VirtualBox

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

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

Main/Display: seamless for multimonitor. (xTracker 4655)

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