VirtualBox

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

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

Main: back out r63429

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

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