VirtualBox

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

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

Main/Display: fixed memory leak in Display::TakeScreenShotToArray()

  • 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 28447 2010-04-19 08:45:18Z 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 RTMemFree(pu8Data);
2566
2567 LogFlowFunc (("rc=%08X\n", rc));
2568 LogFlowFuncLeave();
2569 return rc;
2570}
2571
2572#ifdef VBOX_WITH_OLD_VBVA_LOCK
2573int Display::drawToScreenEMT(Display *pDisplay, ULONG aScreenId, BYTE *address, ULONG x, ULONG y, ULONG width, ULONG height)
2574{
2575 int rc;
2576 pDisplay->vbvaLock();
2577 if (aScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2578 {
2579 rc = pDisplay->mpDrv->pUpPort->pfnDisplayBlt(pDisplay->mpDrv->pUpPort, address, x, y, width, height);
2580 }
2581 else if (aScreenId < pDisplay->mcMonitors)
2582 {
2583 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[aScreenId];
2584
2585 /* Copy the bitmap to the guest VRAM. */
2586 const uint8_t *pu8Src = address;
2587 int32_t xSrc = 0;
2588 int32_t ySrc = 0;
2589 uint32_t u32SrcWidth = width;
2590 uint32_t u32SrcHeight = height;
2591 uint32_t u32SrcLineSize = width * 4;
2592 uint32_t u32SrcBitsPerPixel = 32;
2593
2594 uint8_t *pu8Dst = pFBInfo->pu8FramebufferVRAM;
2595 int32_t xDst = x;
2596 int32_t yDst = y;
2597 uint32_t u32DstWidth = pFBInfo->w;
2598 uint32_t u32DstHeight = pFBInfo->h;
2599 uint32_t u32DstLineSize = pFBInfo->u32LineSize;
2600 uint32_t u32DstBitsPerPixel = pFBInfo->u16BitsPerPixel;
2601
2602 rc = pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2603 width, height,
2604 pu8Src,
2605 xSrc, ySrc,
2606 u32SrcWidth, u32SrcHeight,
2607 u32SrcLineSize, u32SrcBitsPerPixel,
2608 pu8Dst,
2609 xDst, yDst,
2610 u32DstWidth, u32DstHeight,
2611 u32DstLineSize, u32DstBitsPerPixel);
2612 if (RT_SUCCESS(rc))
2613 {
2614 if (!pFBInfo->pFramebuffer.isNull())
2615 {
2616 /* Update the changed screen area. When framebuffer uses VRAM directly, just notify
2617 * it to update. And for default format, render the guest VRAM to framebuffer.
2618 */
2619 if (pFBInfo->fDefaultFormat)
2620 {
2621 address = NULL;
2622 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2623 if (SUCCEEDED(hrc) && address != NULL)
2624 {
2625 pu8Src = pFBInfo->pu8FramebufferVRAM;
2626 xSrc = x;
2627 ySrc = y;
2628 u32SrcWidth = pFBInfo->w;
2629 u32SrcHeight = pFBInfo->h;
2630 u32SrcLineSize = pFBInfo->u32LineSize;
2631 u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2632
2633 /* Default format is 32 bpp. */
2634 pu8Dst = address;
2635 xDst = xSrc;
2636 yDst = ySrc;
2637 u32DstWidth = u32SrcWidth;
2638 u32DstHeight = u32SrcHeight;
2639 u32DstLineSize = u32DstWidth * 4;
2640 u32DstBitsPerPixel = 32;
2641
2642 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2643 width, height,
2644 pu8Src,
2645 xSrc, ySrc,
2646 u32SrcWidth, u32SrcHeight,
2647 u32SrcLineSize, u32SrcBitsPerPixel,
2648 pu8Dst,
2649 xDst, yDst,
2650 u32DstWidth, u32DstHeight,
2651 u32DstLineSize, u32DstBitsPerPixel);
2652 }
2653 }
2654
2655 pDisplay->handleDisplayUpdate(x + pFBInfo->xOrigin, y + pFBInfo->yOrigin, width, height);
2656 }
2657 }
2658 }
2659 else
2660 {
2661 rc = VERR_INVALID_PARAMETER;
2662 }
2663 pDisplay->vbvaUnlock();
2664 return rc;
2665}
2666#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2667
2668STDMETHODIMP Display::DrawToScreen (ULONG aScreenId, BYTE *address, ULONG x, ULONG y,
2669 ULONG width, ULONG height)
2670{
2671 /// @todo (r=dmik) this function may take too long to complete if the VM
2672 // is doing something like saving state right now. Which, in case if it
2673 // is called on the GUI thread, will make it unresponsive. We should
2674 // check the machine state here (by enclosing the check and VMRequCall
2675 // within the Console lock to make it atomic).
2676
2677 LogFlowFuncEnter();
2678 LogFlowFunc (("address=%p, x=%d, y=%d, width=%d, height=%d\n",
2679 (void *)address, x, y, width, height));
2680
2681 CheckComArgNotNull(address);
2682 CheckComArgExpr(width, width != 0);
2683 CheckComArgExpr(height, height != 0);
2684
2685 AutoCaller autoCaller(this);
2686 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2687
2688 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2689
2690 CHECK_CONSOLE_DRV (mpDrv);
2691
2692 Console::SafeVMPtr pVM(mParent);
2693 if (FAILED(pVM.rc())) return pVM.rc();
2694
2695 /*
2696 * Again we're lazy and make the graphics device do all the
2697 * dirty conversion work.
2698 */
2699#ifdef VBOX_WITH_OLD_VBVA_LOCK
2700 int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)Display::drawToScreenEMT, 7,
2701 this, aScreenId, address, x, y, width, height);
2702#else
2703 int rcVBox = VMR3ReqCallWait(pVM, VMCPUID_ANY, (PFNRT)mpDrv->pUpPort->pfnDisplayBlt, 6,
2704 mpDrv->pUpPort, address, x, y, width, height);
2705#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2706
2707 /*
2708 * If the function returns not supported, we'll have to do all the
2709 * work ourselves using the framebuffer.
2710 */
2711 HRESULT rc = S_OK;
2712 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
2713 {
2714 /** @todo implement generic fallback for screen blitting. */
2715 rc = E_NOTIMPL;
2716 }
2717 else if (RT_FAILURE(rcVBox))
2718 rc = setError(VBOX_E_IPRT_ERROR,
2719 tr("Could not draw to the screen (%Rrc)"), rcVBox);
2720//@todo
2721// else
2722// {
2723// /* All ok. Redraw the screen. */
2724// handleDisplayUpdate (x, y, width, height);
2725// }
2726
2727 LogFlowFunc (("rc=%08X\n", rc));
2728 LogFlowFuncLeave();
2729 return rc;
2730}
2731
2732#ifdef VBOX_WITH_OLD_VBVA_LOCK
2733void Display::InvalidateAndUpdateEMT(Display *pDisplay)
2734{
2735 pDisplay->vbvaLock();
2736 unsigned uScreenId;
2737 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
2738 {
2739 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
2740 {
2741 pDisplay->mpDrv->pUpPort->pfnUpdateDisplayAll(pDisplay->mpDrv->pUpPort);
2742 }
2743 else
2744 {
2745 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
2746
2747 if (!pFBInfo->pFramebuffer.isNull())
2748 {
2749 /* Render complete VRAM screen to the framebuffer.
2750 * When framebuffer uses VRAM directly, just notify it to update.
2751 */
2752 if (pFBInfo->fDefaultFormat)
2753 {
2754 BYTE *address = NULL;
2755 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
2756 if (SUCCEEDED(hrc) && address != NULL)
2757 {
2758 uint32_t width = pFBInfo->w;
2759 uint32_t height = pFBInfo->h;
2760
2761 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
2762 int32_t xSrc = 0;
2763 int32_t ySrc = 0;
2764 uint32_t u32SrcWidth = pFBInfo->w;
2765 uint32_t u32SrcHeight = pFBInfo->h;
2766 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
2767 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
2768
2769 /* Default format is 32 bpp. */
2770 uint8_t *pu8Dst = address;
2771 int32_t xDst = xSrc;
2772 int32_t yDst = ySrc;
2773 uint32_t u32DstWidth = u32SrcWidth;
2774 uint32_t u32DstHeight = u32SrcHeight;
2775 uint32_t u32DstLineSize = u32DstWidth * 4;
2776 uint32_t u32DstBitsPerPixel = 32;
2777
2778 pDisplay->mpDrv->pUpPort->pfnCopyRect(pDisplay->mpDrv->pUpPort,
2779 width, height,
2780 pu8Src,
2781 xSrc, ySrc,
2782 u32SrcWidth, u32SrcHeight,
2783 u32SrcLineSize, u32SrcBitsPerPixel,
2784 pu8Dst,
2785 xDst, yDst,
2786 u32DstWidth, u32DstHeight,
2787 u32DstLineSize, u32DstBitsPerPixel);
2788 }
2789 }
2790
2791 pDisplay->handleDisplayUpdate (pFBInfo->xOrigin, pFBInfo->yOrigin, pFBInfo->w, pFBInfo->h);
2792 }
2793 }
2794 }
2795 pDisplay->vbvaUnlock();
2796}
2797#endif /* VBOX_WITH_OLD_VBVA_LOCK */
2798
2799/**
2800 * Does a full invalidation of the VM display and instructs the VM
2801 * to update it immediately.
2802 *
2803 * @returns COM status code
2804 */
2805STDMETHODIMP Display::InvalidateAndUpdate()
2806{
2807 LogFlowFuncEnter();
2808
2809 AutoCaller autoCaller(this);
2810 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2811
2812 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
2813
2814 CHECK_CONSOLE_DRV (mpDrv);
2815
2816 Console::SafeVMPtr pVM(mParent);
2817 if (FAILED(pVM.rc())) return pVM.rc();
2818
2819 HRESULT rc = S_OK;
2820
2821 LogFlowFunc (("Sending DPYUPDATE request\n"));
2822
2823 /* Have to leave the lock when calling EMT. */
2824 alock.leave ();
2825
2826 /* pdm.h says that this has to be called from the EMT thread */
2827#ifdef VBOX_WITH_OLD_VBVA_LOCK
2828 int rcVBox = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY, (PFNRT)Display::InvalidateAndUpdateEMT,
2829 1, this);
2830#else
2831 int rcVBox = VMR3ReqCallVoidWait(pVM, VMCPUID_ANY,
2832 (PFNRT)mpDrv->pUpPort->pfnUpdateDisplayAll, 1, mpDrv->pUpPort);
2833#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
2834 alock.enter ();
2835
2836 if (RT_FAILURE(rcVBox))
2837 rc = setError(VBOX_E_IPRT_ERROR,
2838 tr("Could not invalidate and update the screen (%Rrc)"), rcVBox);
2839
2840 LogFlowFunc (("rc=%08X\n", rc));
2841 LogFlowFuncLeave();
2842 return rc;
2843}
2844
2845/**
2846 * Notification that the framebuffer has completed the
2847 * asynchronous resize processing
2848 *
2849 * @returns COM status code
2850 */
2851STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
2852{
2853 LogFlowFunc (("\n"));
2854
2855 /// @todo (dmik) can we AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS); here?
2856 // do it when we switch this class to VirtualBoxBase_NEXT.
2857 // This will require general code review and may add some details.
2858 // In particular, we may want to check whether EMT is really waiting for
2859 // this notification, etc. It might be also good to obey the caller to make
2860 // sure this method is not called from more than one thread at a time
2861 // (and therefore don't use Display lock at all here to save some
2862 // milliseconds).
2863 AutoCaller autoCaller(this);
2864 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2865
2866 /* this is only valid for external framebuffers */
2867 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
2868 return setError(VBOX_E_NOT_SUPPORTED,
2869 tr("Resize completed notification is valid only for external framebuffers"));
2870
2871 /* Set the flag indicating that the resize has completed and display
2872 * data need to be updated. */
2873 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
2874 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
2875 AssertRelease(f);NOREF(f);
2876
2877 return S_OK;
2878}
2879
2880STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
2881{
2882#ifdef VBOX_WITH_VIDEOHWACCEL
2883 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsynch(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
2884 return S_OK;
2885#else
2886 return E_NOTIMPL;
2887#endif
2888}
2889
2890// private methods
2891/////////////////////////////////////////////////////////////////////////////
2892
2893/**
2894 * Helper to update the display information from the framebuffer.
2895 *
2896 * @param aCheckParams true to compare the parameters of the current framebuffer
2897 * and the new one and issue handleDisplayResize()
2898 * if they differ.
2899 * @thread EMT
2900 */
2901void Display::updateDisplayData (bool aCheckParams /* = false */)
2902{
2903 /* the driver might not have been constructed yet */
2904 if (!mpDrv)
2905 return;
2906
2907#if DEBUG
2908 /*
2909 * Sanity check. Note that this method may be called on EMT after Console
2910 * has started the power down procedure (but before our #drvDestruct() is
2911 * called, in which case pVM will aleady be NULL but mpDrv will not). Since
2912 * we don't really need pVM to proceed, we avoid this check in the release
2913 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
2914 * time-critical method.
2915 */
2916 Console::SafeVMPtrQuiet pVM (mParent);
2917 if (pVM.isOk())
2918 VM_ASSERT_EMT (pVM.raw());
2919#endif
2920
2921 /* The method is only relevant to the primary framebuffer. */
2922 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
2923
2924 if (pFramebuffer)
2925 {
2926 HRESULT rc;
2927 BYTE *address = 0;
2928 rc = pFramebuffer->COMGETTER(Address) (&address);
2929 AssertComRC (rc);
2930 ULONG bytesPerLine = 0;
2931 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
2932 AssertComRC (rc);
2933 ULONG bitsPerPixel = 0;
2934 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
2935 AssertComRC (rc);
2936 ULONG width = 0;
2937 rc = pFramebuffer->COMGETTER(Width) (&width);
2938 AssertComRC (rc);
2939 ULONG height = 0;
2940 rc = pFramebuffer->COMGETTER(Height) (&height);
2941 AssertComRC (rc);
2942
2943 /*
2944 * Check current parameters with new ones and issue handleDisplayResize()
2945 * to let the new frame buffer adjust itself properly. Note that it will
2946 * result into a recursive updateDisplayData() call but with
2947 * aCheckOld = false.
2948 */
2949 if (aCheckParams &&
2950 (mLastAddress != address ||
2951 mLastBytesPerLine != bytesPerLine ||
2952 mLastBitsPerPixel != bitsPerPixel ||
2953 mLastWidth != (int) width ||
2954 mLastHeight != (int) height))
2955 {
2956 handleDisplayResize (VBOX_VIDEO_PRIMARY_SCREEN, mLastBitsPerPixel,
2957 mLastAddress,
2958 mLastBytesPerLine,
2959 mLastWidth,
2960 mLastHeight);
2961 return;
2962 }
2963
2964 mpDrv->IConnector.pu8Data = (uint8_t *) address;
2965 mpDrv->IConnector.cbScanline = bytesPerLine;
2966 mpDrv->IConnector.cBits = bitsPerPixel;
2967 mpDrv->IConnector.cx = width;
2968 mpDrv->IConnector.cy = height;
2969 }
2970 else
2971 {
2972 /* black hole */
2973 mpDrv->IConnector.pu8Data = NULL;
2974 mpDrv->IConnector.cbScanline = 0;
2975 mpDrv->IConnector.cBits = 0;
2976 mpDrv->IConnector.cx = 0;
2977 mpDrv->IConnector.cy = 0;
2978 }
2979}
2980
2981/**
2982 * Changes the current frame buffer. Called on EMT to avoid both
2983 * race conditions and excessive locking.
2984 *
2985 * @note locks this object for writing
2986 * @thread EMT
2987 */
2988/* static */
2989DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
2990 unsigned uScreenId)
2991{
2992 LogFlowFunc (("uScreenId = %d\n", uScreenId));
2993
2994 AssertReturn(that, VERR_INVALID_PARAMETER);
2995 AssertReturn(uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
2996
2997 AutoCaller autoCaller(that);
2998 if (FAILED(autoCaller.rc())) return autoCaller.rc();
2999
3000 AutoWriteLock alock(that COMMA_LOCKVAL_SRC_POS);
3001
3002 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
3003 pDisplayFBInfo->pFramebuffer = aFB;
3004
3005 that->mParent->consoleVRDPServer()->SendResize ();
3006
3007 that->updateDisplayData (true /* aCheckParams */);
3008
3009 return VINF_SUCCESS;
3010}
3011
3012/**
3013 * Handle display resize event issued by the VGA device for the primary screen.
3014 *
3015 * @see PDMIDISPLAYCONNECTOR::pfnResize
3016 */
3017DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
3018 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
3019{
3020 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3021
3022 LogFlowFunc (("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
3023 bpp, pvVRAM, cbLine, cx, cy));
3024
3025 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy);
3026}
3027
3028/**
3029 * Handle display update.
3030 *
3031 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
3032 */
3033DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
3034 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
3035{
3036 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3037
3038#ifdef DEBUG_sunlover
3039 LogFlowFunc (("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
3040 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
3041#endif /* DEBUG_sunlover */
3042
3043 /* This call does update regardless of VBVA status.
3044 * But in VBVA mode this is called only as result of
3045 * pfnUpdateDisplayAll in the VGA device.
3046 */
3047
3048 pDrv->pDisplay->handleDisplayUpdate(x, y, cx, cy);
3049}
3050
3051/**
3052 * Periodic display refresh callback.
3053 *
3054 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
3055 */
3056DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
3057{
3058 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3059
3060#ifdef DEBUG_sunlover
3061 STAM_PROFILE_START(&StatDisplayRefresh, a);
3062#endif /* DEBUG_sunlover */
3063
3064#ifdef DEBUG_sunlover_2
3065 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
3066 pDrv->pDisplay->mfVideoAccelEnabled));
3067#endif /* DEBUG_sunlover_2 */
3068
3069 Display *pDisplay = pDrv->pDisplay;
3070 bool fNoUpdate = false; /* Do not update the display if any of the framebuffers is being resized. */
3071 unsigned uScreenId;
3072
3073 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3074 {
3075 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3076
3077 /* Check the resize status. The status can be checked normally because
3078 * the status affects only the EMT.
3079 */
3080 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
3081
3082 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
3083 {
3084 LogFlowFunc (("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
3085 fNoUpdate = true; /* Always set it here, because pfnUpdateDisplayAll can cause a new resize. */
3086 /* The framebuffer was resized and display data need to be updated. */
3087 pDisplay->handleResizeCompletedEMT ();
3088 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
3089 {
3090 /* The resize status could be not Void here because a pending resize is issued. */
3091 continue;
3092 }
3093 /* Continue with normal processing because the status here is ResizeStatus_Void. */
3094 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3095 {
3096 /* Repaint the display because VM continued to run during the framebuffer resize. */
3097 if (!pFBInfo->pFramebuffer.isNull())
3098#ifdef VBOX_WITH_OLD_VBVA_LOCK
3099 {
3100 pDisplay->vbvaLock();
3101#endif /* VBOX_WITH_OLD_VBVA_LOCK */
3102 pDrv->pUpPort->pfnUpdateDisplayAll(pDrv->pUpPort);
3103#ifdef VBOX_WITH_OLD_VBVA_LOCK
3104 pDisplay->vbvaUnlock();
3105 }
3106#endif /* VBOX_WITH_OLD_VBVA_LOCK */
3107 }
3108 }
3109 else if (u32ResizeStatus == ResizeStatus_InProgress)
3110 {
3111 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
3112 LogFlowFunc (("ResizeStatus_InProcess\n"));
3113 fNoUpdate = true;
3114 continue;
3115 }
3116 }
3117
3118 if (!fNoUpdate)
3119 {
3120#ifdef VBOX_WITH_OLD_VBVA_LOCK
3121 int rc = pDisplay->videoAccelRefreshProcess();
3122
3123 if (rc != VINF_TRY_AGAIN) /* Means 'do nothing' here. */
3124 {
3125 if (rc == VWRN_INVALID_STATE)
3126 {
3127 /* No VBVA do a display update. */
3128 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3129 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3130 {
3131 Assert(pDrv->IConnector.pu8Data);
3132 pDisplay->vbvaLock();
3133 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3134 pDisplay->vbvaUnlock();
3135 }
3136 }
3137
3138 /* Inform the VRDP server that the current display update sequence is
3139 * completed. At this moment the framebuffer memory contains a definite
3140 * image, that is synchronized with the orders already sent to VRDP client.
3141 * The server can now process redraw requests from clients or initial
3142 * fullscreen updates for new clients.
3143 */
3144 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3145 {
3146 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3147
3148 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3149 {
3150 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
3151 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
3152 }
3153 }
3154 }
3155#else
3156 if (pDisplay->mfPendingVideoAccelEnable)
3157 {
3158 /* Acceleration was enabled while machine was not yet running
3159 * due to restoring from saved state. Update entire display and
3160 * actually enable acceleration.
3161 */
3162 Assert(pDisplay->mpPendingVbvaMemory);
3163
3164 /* Acceleration can not be yet enabled.*/
3165 Assert(pDisplay->mpVbvaMemory == NULL);
3166 Assert(!pDisplay->mfVideoAccelEnabled);
3167
3168 if (pDisplay->mfMachineRunning)
3169 {
3170 pDisplay->VideoAccelEnable (pDisplay->mfPendingVideoAccelEnable,
3171 pDisplay->mpPendingVbvaMemory);
3172
3173 /* Reset the pending state. */
3174 pDisplay->mfPendingVideoAccelEnable = false;
3175 pDisplay->mpPendingVbvaMemory = NULL;
3176 }
3177 }
3178 else
3179 {
3180 Assert(pDisplay->mpPendingVbvaMemory == NULL);
3181
3182 if (pDisplay->mfVideoAccelEnabled)
3183 {
3184 Assert(pDisplay->mpVbvaMemory);
3185 pDisplay->VideoAccelFlush ();
3186 }
3187 else
3188 {
3189 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
3190 if (!pFBInfo->pFramebuffer.isNull())
3191 {
3192 Assert(pDrv->IConnector.pu8Data);
3193 Assert(pFBInfo->u32ResizeStatus == ResizeStatus_Void);
3194 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
3195 }
3196 }
3197
3198 /* Inform the VRDP server that the current display update sequence is
3199 * completed. At this moment the framebuffer memory contains a definite
3200 * image, that is synchronized with the orders already sent to VRDP client.
3201 * The server can now process redraw requests from clients or initial
3202 * fullscreen updates for new clients.
3203 */
3204 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
3205 {
3206 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
3207
3208 if (!pFBInfo->pFramebuffer.isNull() && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
3209 {
3210 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
3211 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
3212 }
3213 }
3214 }
3215#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
3216 }
3217
3218#ifdef DEBUG_sunlover
3219 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
3220#endif /* DEBUG_sunlover */
3221#ifdef DEBUG_sunlover_2
3222 LogFlowFunc (("leave\n"));
3223#endif /* DEBUG_sunlover_2 */
3224}
3225
3226/**
3227 * Reset notification
3228 *
3229 * @see PDMIDISPLAYCONNECTOR::pfnReset
3230 */
3231DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
3232{
3233 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3234
3235 LogFlowFunc (("\n"));
3236
3237 /* Disable VBVA mode. */
3238 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3239}
3240
3241/**
3242 * LFBModeChange notification
3243 *
3244 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
3245 */
3246DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
3247{
3248 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3249
3250 LogFlowFunc (("fEnabled=%d\n", fEnabled));
3251
3252 NOREF(fEnabled);
3253
3254 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
3255#ifdef VBOX_WITH_OLD_VBVA_LOCK
3256 /* This is called under DevVGA lock. Postpone disabling VBVA, do it in the refresh timer. */
3257 ASMAtomicWriteU32(&pDrv->pDisplay->mfu32PendingVideoAccelDisable, true);
3258#else
3259 pDrv->pDisplay->VideoAccelEnable (false, NULL);
3260#endif /* !VBOX_WITH_OLD_VBVA_LOCK */
3261}
3262
3263/**
3264 * Adapter information change notification.
3265 *
3266 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
3267 */
3268DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
3269{
3270 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3271
3272 if (pvVRAM == NULL)
3273 {
3274 unsigned i;
3275 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
3276 {
3277 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
3278
3279 pFBInfo->u32Offset = 0;
3280 pFBInfo->u32MaxFramebufferSize = 0;
3281 pFBInfo->u32InformationSize = 0;
3282 }
3283 }
3284#ifndef VBOX_WITH_HGSMI
3285 else
3286 {
3287 uint8_t *pu8 = (uint8_t *)pvVRAM;
3288 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3289
3290 // @todo
3291 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
3292
3293 VBOXVIDEOINFOHDR *pHdr;
3294
3295 for (;;)
3296 {
3297 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3298 pu8 += sizeof (VBOXVIDEOINFOHDR);
3299
3300 if (pu8 >= pu8End)
3301 {
3302 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
3303 break;
3304 }
3305
3306 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
3307 {
3308 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
3309 {
3310 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
3311 break;
3312 }
3313
3314 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
3315
3316 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
3317 {
3318 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
3319 break;
3320 }
3321
3322 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
3323
3324 pFBInfo->u32Offset = pDisplay->u32Offset;
3325 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
3326 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
3327
3328 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));
3329 }
3330 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
3331 {
3332 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
3333 {
3334 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
3335 break;
3336 }
3337
3338 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
3339
3340 switch (pConf32->u32Index)
3341 {
3342 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
3343 {
3344 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
3345 } break;
3346
3347 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
3348 {
3349 /* @todo make configurable. */
3350 pConf32->u32Value = _1M;
3351 } break;
3352
3353 default:
3354 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
3355 }
3356 }
3357 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3358 {
3359 if (pHdr->u16Length != 0)
3360 {
3361 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3362 break;
3363 }
3364
3365 break;
3366 }
3367 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
3368 {
3369 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
3370 }
3371
3372 pu8 += pHdr->u16Length;
3373 }
3374 }
3375#endif /* !VBOX_WITH_HGSMI */
3376}
3377
3378/**
3379 * Display information change notification.
3380 *
3381 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
3382 */
3383DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
3384{
3385 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3386
3387 if (uScreenId >= pDrv->pDisplay->mcMonitors)
3388 {
3389 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
3390 return;
3391 }
3392
3393 /* Get the display information structure. */
3394 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
3395
3396 uint8_t *pu8 = (uint8_t *)pvVRAM;
3397 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
3398
3399 // @todo
3400 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
3401
3402 VBOXVIDEOINFOHDR *pHdr;
3403
3404 for (;;)
3405 {
3406 pHdr = (VBOXVIDEOINFOHDR *)pu8;
3407 pu8 += sizeof (VBOXVIDEOINFOHDR);
3408
3409 if (pu8 >= pu8End)
3410 {
3411 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
3412 break;
3413 }
3414
3415 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
3416 {
3417 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
3418 {
3419 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
3420 break;
3421 }
3422
3423 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
3424
3425 pFBInfo->xOrigin = pScreen->xOrigin;
3426 pFBInfo->yOrigin = pScreen->yOrigin;
3427
3428 pFBInfo->w = pScreen->u16Width;
3429 pFBInfo->h = pScreen->u16Height;
3430
3431 LogFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
3432 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
3433
3434 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
3435 {
3436 /* Primary screen resize is initiated by the VGA device. */
3437 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height);
3438 }
3439 }
3440 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
3441 {
3442 if (pHdr->u16Length != 0)
3443 {
3444 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
3445 break;
3446 }
3447
3448 break;
3449 }
3450 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
3451 {
3452 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
3453 {
3454 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
3455 break;
3456 }
3457
3458 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
3459
3460 pFBInfo->pHostEvents = pHostEvents;
3461
3462 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
3463 pHostEvents));
3464 }
3465 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
3466 {
3467 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
3468 {
3469 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
3470 break;
3471 }
3472
3473 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
3474 pu8 += pLink->i32Offset;
3475 }
3476 else
3477 {
3478 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
3479 }
3480
3481 pu8 += pHdr->u16Length;
3482 }
3483}
3484
3485#ifdef VBOX_WITH_VIDEOHWACCEL
3486
3487void Display::handleVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3488{
3489 unsigned id = (unsigned)pCommand->iDisplay;
3490 int rc = VINF_SUCCESS;
3491 if (id < mcMonitors)
3492 {
3493 IFramebuffer *pFramebuffer = maFramebuffers[id].pFramebuffer;
3494#ifdef DEBUG_misha
3495 Assert (pFramebuffer);
3496#endif
3497
3498 if (pFramebuffer != NULL)
3499 {
3500 pFramebuffer->Lock();
3501
3502 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
3503 if (FAILED(hr))
3504 {
3505 rc = (hr == E_NOTIMPL) ? VERR_NOT_IMPLEMENTED : VERR_GENERAL_FAILURE;
3506 }
3507
3508 pFramebuffer->Unlock();
3509 }
3510 else
3511 {
3512 rc = VERR_NOT_IMPLEMENTED;
3513 }
3514 }
3515 else
3516 {
3517 rc = VERR_INVALID_PARAMETER;
3518 }
3519
3520 if (RT_FAILURE(rc))
3521 {
3522 /* tell the guest the command is complete */
3523 pCommand->Flags &= (~VBOXVHWACMD_FLAG_HG_ASYNCH);
3524 pCommand->rc = rc;
3525 }
3526}
3527
3528DECLCALLBACK(void) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
3529{
3530 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3531
3532 pDrv->pDisplay->handleVHWACommandProcess(pInterface, pCommand);
3533}
3534#endif
3535
3536#ifdef VBOX_WITH_HGSMI
3537DECLCALLBACK(int) Display::displayVBVAEnable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, PVBVAHOSTFLAGS pHostFlags)
3538{
3539 LogFlowFunc(("uScreenId %d\n", uScreenId));
3540
3541 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3542 Display *pThis = pDrv->pDisplay;
3543
3544 pThis->maFramebuffers[uScreenId].fVBVAEnabled = true;
3545 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = pHostFlags;
3546
3547 vbvaSetMemoryFlagsHGSMI(uScreenId, pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, &pThis->maFramebuffers[uScreenId]);
3548
3549 return VINF_SUCCESS;
3550}
3551
3552DECLCALLBACK(void) Display::displayVBVADisable(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3553{
3554 LogFlowFunc(("uScreenId %d\n", uScreenId));
3555
3556 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3557 Display *pThis = pDrv->pDisplay;
3558
3559 pThis->maFramebuffers[uScreenId].fVBVAEnabled = false;
3560
3561 vbvaSetMemoryFlagsHGSMI(uScreenId, 0, false, &pThis->maFramebuffers[uScreenId]);
3562
3563 pThis->maFramebuffers[uScreenId].pVBVAHostFlags = NULL;
3564}
3565
3566DECLCALLBACK(void) Display::displayVBVAUpdateBegin(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId)
3567{
3568 LogFlowFunc(("uScreenId %d\n", uScreenId));
3569
3570 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3571 Display *pThis = pDrv->pDisplay;
3572 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3573
3574 if (ASMAtomicReadU32(&pThis->mu32UpdateVBVAFlags) > 0)
3575 {
3576 vbvaSetMemoryFlagsAllHGSMI(pThis->mfu32SupportedOrders, pThis->mfVideoAccelVRDP, pThis->maFramebuffers, pThis->mcMonitors);
3577 ASMAtomicDecU32(&pThis->mu32UpdateVBVAFlags);
3578 }
3579
3580 if (RT_LIKELY(pFBInfo->u32ResizeStatus == ResizeStatus_Void))
3581 {
3582 if (RT_UNLIKELY(pFBInfo->cVBVASkipUpdate != 0))
3583 {
3584 /* Some updates were skipped. Note: displayVBVAUpdate* callbacks are called
3585 * under display device lock, so thread safe.
3586 */
3587 pFBInfo->cVBVASkipUpdate = 0;
3588 pThis->handleDisplayUpdate(pFBInfo->vbvaSkippedRect.xLeft,
3589 pFBInfo->vbvaSkippedRect.yTop,
3590 pFBInfo->vbvaSkippedRect.xRight - pFBInfo->vbvaSkippedRect.xLeft,
3591 pFBInfo->vbvaSkippedRect.yBottom - pFBInfo->vbvaSkippedRect.yTop);
3592 }
3593 }
3594 else
3595 {
3596 /* The framebuffer is being resized. */
3597 pFBInfo->cVBVASkipUpdate++;
3598 }
3599}
3600
3601DECLCALLBACK(void) Display::displayVBVAUpdateProcess(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, const PVBVACMDHDR pCmd, size_t cbCmd)
3602{
3603 LogFlowFunc(("uScreenId %d pCmd %p cbCmd %d\n", uScreenId, pCmd, cbCmd));
3604
3605 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3606 Display *pThis = pDrv->pDisplay;
3607 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3608
3609 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3610 {
3611 if (pFBInfo->fDefaultFormat)
3612 {
3613 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
3614 {
3615 pDrv->pUpPort->pfnUpdateDisplayRect (pDrv->pUpPort, pCmd->x, pCmd->y, pCmd->w, pCmd->h);
3616 }
3617 else if (!pFBInfo->pFramebuffer.isNull())
3618 {
3619 /* Render VRAM content to the framebuffer. */
3620 BYTE *address = NULL;
3621 HRESULT hrc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
3622 if (SUCCEEDED(hrc) && address != NULL)
3623 {
3624 uint32_t width = pCmd->w;
3625 uint32_t height = pCmd->h;
3626
3627 const uint8_t *pu8Src = pFBInfo->pu8FramebufferVRAM;
3628 int32_t xSrc = pCmd->x - pFBInfo->xOrigin;
3629 int32_t ySrc = pCmd->y - pFBInfo->yOrigin;
3630 uint32_t u32SrcWidth = pFBInfo->w;
3631 uint32_t u32SrcHeight = pFBInfo->h;
3632 uint32_t u32SrcLineSize = pFBInfo->u32LineSize;
3633 uint32_t u32SrcBitsPerPixel = pFBInfo->u16BitsPerPixel;
3634
3635 uint8_t *pu8Dst = address;
3636 int32_t xDst = xSrc;
3637 int32_t yDst = ySrc;
3638 uint32_t u32DstWidth = u32SrcWidth;
3639 uint32_t u32DstHeight = u32SrcHeight;
3640 uint32_t u32DstLineSize = u32DstWidth * 4;
3641 uint32_t u32DstBitsPerPixel = 32;
3642
3643 pDrv->pUpPort->pfnCopyRect(pDrv->pUpPort,
3644 width, height,
3645 pu8Src,
3646 xSrc, ySrc,
3647 u32SrcWidth, u32SrcHeight,
3648 u32SrcLineSize, u32SrcBitsPerPixel,
3649 pu8Dst,
3650 xDst, yDst,
3651 u32DstWidth, u32DstHeight,
3652 u32DstLineSize, u32DstBitsPerPixel);
3653 }
3654 }
3655 pThis->handleDisplayUpdate (pCmd->x + pFBInfo->xOrigin,
3656 pCmd->y + pFBInfo->yOrigin, pCmd->w, pCmd->h);
3657 }
3658
3659 pThis->mParent->consoleVRDPServer()->SendUpdate (uScreenId, pCmd, cbCmd);
3660 }
3661}
3662
3663DECLCALLBACK(void) Display::displayVBVAUpdateEnd(PPDMIDISPLAYCONNECTOR pInterface, unsigned uScreenId, int32_t x, int32_t y, uint32_t cx, uint32_t cy)
3664{
3665 LogFlowFunc(("uScreenId %d %d,%d %dx%d\n", uScreenId, x, y, cx, cy));
3666
3667 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3668 Display *pThis = pDrv->pDisplay;
3669 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[uScreenId];
3670
3671 /* @todo handleFramebufferUpdate (uScreenId,
3672 * x - pThis->maFramebuffers[uScreenId].xOrigin,
3673 * y - pThis->maFramebuffers[uScreenId].yOrigin,
3674 * cx, cy);
3675 */
3676 if (RT_LIKELY(pFBInfo->cVBVASkipUpdate == 0))
3677 {
3678 pThis->handleDisplayUpdate(x, y, cx, cy);
3679 }
3680 else
3681 {
3682 /* Save the updated rectangle. */
3683 int32_t xRight = x + cx;
3684 int32_t yBottom = y + cy;
3685
3686 if (pFBInfo->cVBVASkipUpdate == 1)
3687 {
3688 pFBInfo->vbvaSkippedRect.xLeft = x;
3689 pFBInfo->vbvaSkippedRect.yTop = y;
3690 pFBInfo->vbvaSkippedRect.xRight = xRight;
3691 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3692 }
3693 else
3694 {
3695 if (pFBInfo->vbvaSkippedRect.xLeft > x)
3696 {
3697 pFBInfo->vbvaSkippedRect.xLeft = x;
3698 }
3699 if (pFBInfo->vbvaSkippedRect.yTop > y)
3700 {
3701 pFBInfo->vbvaSkippedRect.yTop = y;
3702 }
3703 if (pFBInfo->vbvaSkippedRect.xRight < xRight)
3704 {
3705 pFBInfo->vbvaSkippedRect.xRight = xRight;
3706 }
3707 if (pFBInfo->vbvaSkippedRect.yBottom < yBottom)
3708 {
3709 pFBInfo->vbvaSkippedRect.yBottom = yBottom;
3710 }
3711 }
3712 }
3713}
3714
3715DECLCALLBACK(int) Display::displayVBVAResize(PPDMIDISPLAYCONNECTOR pInterface, const PVBVAINFOVIEW pView, const PVBVAINFOSCREEN pScreen, void *pvVRAM)
3716{
3717 LogFlowFunc(("pScreen %p, pvVRAM %p\n", pScreen, pvVRAM));
3718
3719 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3720 Display *pThis = pDrv->pDisplay;
3721
3722 DISPLAYFBINFO *pFBInfo = &pThis->maFramebuffers[pScreen->u32ViewIndex];
3723
3724 /* Check if this is a real resize or a notification about the screen origin.
3725 * The guest uses this VBVAResize call for both.
3726 */
3727 bool fResize = pFBInfo->u16BitsPerPixel != pScreen->u16BitsPerPixel
3728 || pFBInfo->pu8FramebufferVRAM != (uint8_t *)pvVRAM + pScreen->u32StartOffset
3729 || pFBInfo->u32LineSize != pScreen->u32LineSize
3730 || pFBInfo->w != pScreen->u32Width
3731 || pFBInfo->h != pScreen->u32Height;
3732
3733 bool fNewOrigin = pFBInfo->xOrigin != pScreen->i32OriginX
3734 || pFBInfo->yOrigin != pScreen->i32OriginY;
3735
3736 pFBInfo->u32Offset = pView->u32ViewOffset; /* Not used in HGSMI. */
3737 pFBInfo->u32MaxFramebufferSize = pView->u32MaxScreenSize; /* Not used in HGSMI. */
3738 pFBInfo->u32InformationSize = 0; /* Not used in HGSMI. */
3739
3740 pFBInfo->xOrigin = pScreen->i32OriginX;
3741 pFBInfo->yOrigin = pScreen->i32OriginY;
3742
3743 pFBInfo->w = pScreen->u32Width;
3744 pFBInfo->h = pScreen->u32Height;
3745
3746 pFBInfo->u16BitsPerPixel = pScreen->u16BitsPerPixel;
3747 pFBInfo->pu8FramebufferVRAM = (uint8_t *)pvVRAM + pScreen->u32StartOffset;
3748 pFBInfo->u32LineSize = pScreen->u32LineSize;
3749
3750 if (fNewOrigin)
3751 {
3752 /* @todo May be framebuffer/display should be notified in this case. */
3753 }
3754
3755#if defined(VBOX_WITH_HGCM) && defined(VBOX_WITH_CROGL)
3756 if (fNewOrigin && !fResize)
3757 {
3758 BOOL is3denabled;
3759 pThis->mParent->machine()->COMGETTER(Accelerate3DEnabled)(&is3denabled);
3760
3761 if (is3denabled)
3762 {
3763 VBOXHGCMSVCPARM parm;
3764
3765 parm.type = VBOX_HGCM_SVC_PARM_32BIT;
3766 parm.u.uint32 = pScreen->u32ViewIndex;
3767
3768 pThis->mParent->getVMMDev()->hgcmHostCall("VBoxSharedCrOpenGL", SHCRGL_HOST_FN_SCREEN_CHANGED,
3769 SHCRGL_CPARMS_SCREEN_CHANGED, &parm);
3770 }
3771 }
3772#endif /* VBOX_WITH_CROGL */
3773
3774 if (!fResize)
3775 {
3776 /* No paramaters of the framebuffer have actually changed. */
3777 return VINF_SUCCESS;
3778 }
3779
3780 return pThis->handleDisplayResize(pScreen->u32ViewIndex, pScreen->u16BitsPerPixel,
3781 (uint8_t *)pvVRAM + pScreen->u32StartOffset,
3782 pScreen->u32LineSize, pScreen->u32Width, pScreen->u32Height);
3783}
3784
3785DECLCALLBACK(int) Display::displayVBVAMousePointerShape(PPDMIDISPLAYCONNECTOR pInterface, bool fVisible, bool fAlpha,
3786 uint32_t xHot, uint32_t yHot,
3787 uint32_t cx, uint32_t cy,
3788 const void *pvShape)
3789{
3790 LogFlowFunc(("\n"));
3791
3792 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
3793 Display *pThis = pDrv->pDisplay;
3794
3795 /* Tell the console about it */
3796 pDrv->pDisplay->mParent->onMousePointerShapeChange(fVisible, fAlpha,
3797 xHot, yHot, cx, cy, (void *)pvShape);
3798
3799 return VINF_SUCCESS;
3800}
3801#endif /* VBOX_WITH_HGSMI */
3802
3803/**
3804 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
3805 */
3806DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, const char *pszIID)
3807{
3808 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
3809 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3810 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
3811 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIDISPLAYCONNECTOR, &pDrv->IConnector);
3812 return NULL;
3813}
3814
3815
3816/**
3817 * Destruct a display driver instance.
3818 *
3819 * @returns VBox status.
3820 * @param pDrvIns The driver instance data.
3821 */
3822DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
3823{
3824 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3825 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
3826 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
3827
3828 if (pData->pDisplay)
3829 {
3830 AutoWriteLock displayLock(pData->pDisplay COMMA_LOCKVAL_SRC_POS);
3831 pData->pDisplay->mpDrv = NULL;
3832 pData->pDisplay->mpVMMDev = NULL;
3833 pData->pDisplay->mLastAddress = NULL;
3834 pData->pDisplay->mLastBytesPerLine = 0;
3835 pData->pDisplay->mLastBitsPerPixel = 0,
3836 pData->pDisplay->mLastWidth = 0;
3837 pData->pDisplay->mLastHeight = 0;
3838 }
3839}
3840
3841
3842/**
3843 * Construct a display driver instance.
3844 *
3845 * @copydoc FNPDMDRVCONSTRUCT
3846 */
3847DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
3848{
3849 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
3850 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
3851 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
3852
3853 /*
3854 * Validate configuration.
3855 */
3856 if (!CFGMR3AreValuesValid(pCfg, "Object\0"))
3857 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
3858 AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
3859 ("Configuration error: Not possible to attach anything to this driver!\n"),
3860 VERR_PDM_DRVINS_NO_ATTACH);
3861
3862 /*
3863 * Init Interfaces.
3864 */
3865 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
3866
3867 pData->IConnector.pfnResize = Display::displayResizeCallback;
3868 pData->IConnector.pfnUpdateRect = Display::displayUpdateCallback;
3869 pData->IConnector.pfnRefresh = Display::displayRefreshCallback;
3870 pData->IConnector.pfnReset = Display::displayResetCallback;
3871 pData->IConnector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
3872 pData->IConnector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
3873 pData->IConnector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
3874#ifdef VBOX_WITH_VIDEOHWACCEL
3875 pData->IConnector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
3876#endif
3877#ifdef VBOX_WITH_HGSMI
3878 pData->IConnector.pfnVBVAEnable = Display::displayVBVAEnable;
3879 pData->IConnector.pfnVBVADisable = Display::displayVBVADisable;
3880 pData->IConnector.pfnVBVAUpdateBegin = Display::displayVBVAUpdateBegin;
3881 pData->IConnector.pfnVBVAUpdateProcess = Display::displayVBVAUpdateProcess;
3882 pData->IConnector.pfnVBVAUpdateEnd = Display::displayVBVAUpdateEnd;
3883 pData->IConnector.pfnVBVAResize = Display::displayVBVAResize;
3884 pData->IConnector.pfnVBVAMousePointerShape = Display::displayVBVAMousePointerShape;
3885#endif
3886
3887
3888 /*
3889 * Get the IDisplayPort interface of the above driver/device.
3890 */
3891 pData->pUpPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYPORT);
3892 if (!pData->pUpPort)
3893 {
3894 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
3895 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3896 }
3897#if defined(VBOX_WITH_VIDEOHWACCEL)
3898 pData->pVBVACallbacks = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIDISPLAYVBVACALLBACKS);
3899 if (!pData->pVBVACallbacks)
3900 {
3901 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
3902 return VERR_PDM_MISSING_INTERFACE_ABOVE;
3903 }
3904#endif
3905 /*
3906 * Get the Display object pointer and update the mpDrv member.
3907 */
3908 void *pv;
3909 int rc = CFGMR3QueryPtr(pCfg, "Object", &pv);
3910 if (RT_FAILURE(rc))
3911 {
3912 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
3913 return rc;
3914 }
3915 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
3916 pData->pDisplay->mpDrv = pData;
3917
3918 /*
3919 * Update our display information according to the framebuffer
3920 */
3921 pData->pDisplay->updateDisplayData();
3922
3923 /*
3924 * Start periodic screen refreshes
3925 */
3926 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
3927
3928 return VINF_SUCCESS;
3929}
3930
3931
3932/**
3933 * Display driver registration record.
3934 */
3935const PDMDRVREG Display::DrvReg =
3936{
3937 /* u32Version */
3938 PDM_DRVREG_VERSION,
3939 /* szName */
3940 "MainDisplay",
3941 /* szRCMod */
3942 "",
3943 /* szR0Mod */
3944 "",
3945 /* pszDescription */
3946 "Main display driver (Main as in the API).",
3947 /* fFlags */
3948 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
3949 /* fClass. */
3950 PDM_DRVREG_CLASS_DISPLAY,
3951 /* cMaxInstances */
3952 ~0,
3953 /* cbInstance */
3954 sizeof(DRVMAINDISPLAY),
3955 /* pfnConstruct */
3956 Display::drvConstruct,
3957 /* pfnDestruct */
3958 Display::drvDestruct,
3959 /* pfnRelocate */
3960 NULL,
3961 /* pfnIOCtl */
3962 NULL,
3963 /* pfnPowerOn */
3964 NULL,
3965 /* pfnReset */
3966 NULL,
3967 /* pfnSuspend */
3968 NULL,
3969 /* pfnResume */
3970 NULL,
3971 /* pfnAttach */
3972 NULL,
3973 /* pfnDetach */
3974 NULL,
3975 /* pfnPowerOff */
3976 NULL,
3977 /* pfnSoftReset */
3978 NULL,
3979 /* u32EndVersion */
3980 PDM_DRVREG_VERSION
3981};
3982/* 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