VirtualBox

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

Last change on this file since 30832 was 30825, checked in by vboxsync, 14 years ago

Main: callbacks removal

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

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