VirtualBox

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

Last change on this file since 19844 was 19844, checked in by vboxsync, 16 years ago

HGSMI: post host VBVA commands to display; Video HW Accel: mechanism for passing/processing commands to framebuffer

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.7 KB
Line 
1/* $Id: DisplayImpl.cpp 19844 2009-05-19 23:12:55Z vboxsync $ */
2
3/** @file
4 *
5 * VirtualBox COM class implementation
6 */
7
8/*
9 * Copyright (C) 2006-2008 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24#include "DisplayImpl.h"
25#include "ConsoleImpl.h"
26#include "ConsoleVRDPServer.h"
27#include "VMMDev.h"
28
29#include "Logging.h"
30
31#include <iprt/semaphore.h>
32#include <iprt/thread.h>
33#include <iprt/asm.h>
34
35#include <VBox/pdmdrv.h>
36#ifdef DEBUG /* for VM_ASSERT_EMT(). */
37# include <VBox/vm.h>
38#endif
39
40#ifdef VBOX_WITH_VIDEOHWACCEL
41# include <VBox/VBoxVideo.h>
42#endif
43/**
44 * Display driver instance data.
45 */
46typedef struct DRVMAINDISPLAY
47{
48 /** Pointer to the display object. */
49 Display *pDisplay;
50 /** Pointer to the driver instance structure. */
51 PPDMDRVINS pDrvIns;
52 /** Pointer to the keyboard port interface of the driver/device above us. */
53 PPDMIDISPLAYPORT pUpPort;
54 /** Our display connector interface. */
55 PDMIDISPLAYCONNECTOR Connector;
56#if defined(VBOX_WITH_VIDEOHWACCEL)
57 /** VBVA callbacks */
58 PPDMDDISPLAYVBVACALLBACKS pVBVACallbacks;
59#endif
60} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
61
62/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
63#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) ( (PDRVMAINDISPLAY) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINDISPLAY, Connector)) )
64
65#ifdef DEBUG_sunlover
66static STAMPROFILE StatDisplayRefresh;
67static int stam = 0;
68#endif /* DEBUG_sunlover */
69
70// constructor / destructor
71/////////////////////////////////////////////////////////////////////////////
72
73DEFINE_EMPTY_CTOR_DTOR (Display)
74
75HRESULT Display::FinalConstruct()
76{
77 mpVbvaMemory = NULL;
78 mfVideoAccelEnabled = false;
79 mfVideoAccelVRDP = false;
80 mfu32SupportedOrders = 0;
81 mcVideoAccelVRDPRefs = 0;
82
83 mpPendingVbvaMemory = NULL;
84 mfPendingVideoAccelEnable = false;
85
86 mfMachineRunning = false;
87
88 mpu8VbvaPartial = NULL;
89 mcbVbvaPartial = 0;
90
91 mpDrv = NULL;
92 mpVMMDev = NULL;
93 mfVMMDevInited = false;
94
95 mLastAddress = NULL;
96 mLastBytesPerLine = 0;
97 mLastBitsPerPixel = 0,
98 mLastWidth = 0;
99 mLastHeight = 0;
100
101 return S_OK;
102}
103
104void Display::FinalRelease()
105{
106 uninit();
107}
108
109// public initializer/uninitializer for internal purposes only
110/////////////////////////////////////////////////////////////////////////////
111
112/**
113 * Initializes the display object.
114 *
115 * @returns COM result indicator
116 * @param parent handle of our parent object
117 * @param qemuConsoleData address of common console data structure
118 */
119HRESULT Display::init (Console *aParent)
120{
121 LogFlowThisFunc (("aParent=%p\n", aParent));
122
123 ComAssertRet (aParent, E_INVALIDARG);
124
125 /* Enclose the state transition NotReady->InInit->Ready */
126 AutoInitSpan autoInitSpan (this);
127 AssertReturn (autoInitSpan.isOk(), E_FAIL);
128
129 unconst (mParent) = aParent;
130
131 // by default, we have an internal framebuffer which is
132 // NULL, i.e. a black hole for no display output
133 mFramebufferOpened = false;
134
135 ULONG ul;
136 mParent->machine()->COMGETTER(MonitorCount)(&ul);
137 mcMonitors = ul;
138
139 for (ul = 0; ul < mcMonitors; ul++)
140 {
141 maFramebuffers[ul].u32Offset = 0;
142 maFramebuffers[ul].u32MaxFramebufferSize = 0;
143 maFramebuffers[ul].u32InformationSize = 0;
144
145 maFramebuffers[ul].pFramebuffer = NULL;
146
147 maFramebuffers[ul].xOrigin = 0;
148 maFramebuffers[ul].yOrigin = 0;
149
150 maFramebuffers[ul].w = 0;
151 maFramebuffers[ul].h = 0;
152
153 maFramebuffers[ul].pHostEvents = NULL;
154
155 maFramebuffers[ul].u32ResizeStatus = ResizeStatus_Void;
156
157 maFramebuffers[ul].fDefaultFormat = false;
158
159 memset (&maFramebuffers[ul].dirtyRect, 0 , sizeof (maFramebuffers[ul].dirtyRect));
160 memset (&maFramebuffers[ul].pendingResize, 0 , sizeof (maFramebuffers[ul].pendingResize));
161 }
162
163 mParent->RegisterCallback (this);
164
165 /* Confirm a successful initialization */
166 autoInitSpan.setSucceeded();
167
168 return S_OK;
169}
170
171/**
172 * Uninitializes the instance and sets the ready flag to FALSE.
173 * Called either from FinalRelease() or by the parent when it gets destroyed.
174 */
175void Display::uninit()
176{
177 LogFlowThisFunc (("\n"));
178
179 /* Enclose the state transition Ready->InUninit->NotReady */
180 AutoUninitSpan autoUninitSpan (this);
181 if (autoUninitSpan.uninitDone())
182 return;
183
184 ULONG ul;
185 for (ul = 0; ul < mcMonitors; ul++)
186 maFramebuffers[ul].pFramebuffer = NULL;
187
188 if (mParent)
189 mParent->UnregisterCallback (this);
190
191 unconst (mParent).setNull();
192
193 if (mpDrv)
194 mpDrv->pDisplay = NULL;
195
196 mpDrv = NULL;
197 mpVMMDev = NULL;
198 mfVMMDevInited = true;
199}
200
201// IConsoleCallback method
202STDMETHODIMP Display::OnStateChange(MachineState_T machineState)
203{
204 if (machineState == MachineState_Running)
205 {
206 LogFlowFunc (("Machine is running.\n"));
207
208 mfMachineRunning = true;
209 }
210 else
211 mfMachineRunning = false;
212
213 return S_OK;
214}
215
216// public methods only for internal purposes
217/////////////////////////////////////////////////////////////////////////////
218
219/**
220 * @thread EMT
221 */
222static int callFramebufferResize (IFramebuffer *pFramebuffer, unsigned uScreenId,
223 ULONG pixelFormat, void *pvVRAM,
224 uint32_t bpp, uint32_t cbLine,
225 int w, int h)
226{
227 Assert (pFramebuffer);
228
229 /* Call the framebuffer to try and set required pixelFormat. */
230 BOOL finished = TRUE;
231
232 pFramebuffer->RequestResize (uScreenId, pixelFormat, (BYTE *) pvVRAM,
233 bpp, cbLine, w, h, &finished);
234
235 if (!finished)
236 {
237 LogFlowFunc (("External framebuffer wants us to wait!\n"));
238 return VINF_VGA_RESIZE_IN_PROGRESS;
239 }
240
241 return VINF_SUCCESS;
242}
243
244/**
245 * Handles display resize event.
246 * Disables access to VGA device;
247 * calls the framebuffer RequestResize method;
248 * if framebuffer resizes synchronously,
249 * updates the display connector data and enables access to the VGA device.
250 *
251 * @param w New display width
252 * @param h New display height
253 *
254 * @thread EMT
255 */
256int Display::handleDisplayResize (unsigned uScreenId, uint32_t bpp, void *pvVRAM,
257 uint32_t cbLine, int w, int h)
258{
259 LogRel (("Display::handleDisplayResize(): uScreenId = %d, pvVRAM=%p "
260 "w=%d h=%d bpp=%d cbLine=0x%X\n",
261 uScreenId, pvVRAM, w, h, bpp, cbLine));
262
263 /* If there is no framebuffer, this call is not interesting. */
264 if ( uScreenId >= mcMonitors
265 || maFramebuffers[uScreenId].pFramebuffer.isNull())
266 {
267 return VINF_SUCCESS;
268 }
269
270 mLastAddress = pvVRAM;
271 mLastBytesPerLine = cbLine;
272 mLastBitsPerPixel = bpp,
273 mLastWidth = w;
274 mLastHeight = h;
275
276 ULONG pixelFormat;
277
278 switch (bpp)
279 {
280 case 32:
281 case 24:
282 case 16:
283 pixelFormat = FramebufferPixelFormat_FOURCC_RGB;
284 break;
285 default:
286 pixelFormat = FramebufferPixelFormat_Opaque;
287 bpp = cbLine = 0;
288 break;
289 }
290
291 /* Atomically set the resize status before calling the framebuffer. The new InProgress status will
292 * disable access to the VGA device by the EMT thread.
293 */
294 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
295 ResizeStatus_InProgress, ResizeStatus_Void);
296 if (!f)
297 {
298 /* This could be a result of the screenshot taking call Display::TakeScreenShot:
299 * if the framebuffer is processing the resize request and GUI calls the TakeScreenShot
300 * and the guest has reprogrammed the virtual VGA devices again so a new resize is required.
301 *
302 * Save the resize information and return the pending status code.
303 *
304 * Note: the resize information is only accessed on EMT so no serialization is required.
305 */
306 LogRel (("Display::handleDisplayResize(): Warning: resize postponed.\n"));
307
308 maFramebuffers[uScreenId].pendingResize.fPending = true;
309 maFramebuffers[uScreenId].pendingResize.pixelFormat = pixelFormat;
310 maFramebuffers[uScreenId].pendingResize.pvVRAM = pvVRAM;
311 maFramebuffers[uScreenId].pendingResize.bpp = bpp;
312 maFramebuffers[uScreenId].pendingResize.cbLine = cbLine;
313 maFramebuffers[uScreenId].pendingResize.w = w;
314 maFramebuffers[uScreenId].pendingResize.h = h;
315
316 return VINF_VGA_RESIZE_IN_PROGRESS;
317 }
318
319 int rc = callFramebufferResize (maFramebuffers[uScreenId].pFramebuffer, uScreenId,
320 pixelFormat, pvVRAM, bpp, cbLine, w, h);
321 if (rc == VINF_VGA_RESIZE_IN_PROGRESS)
322 {
323 /* Immediately return to the caller. ResizeCompleted will be called back by the
324 * GUI thread. The ResizeCompleted callback will change the resize status from
325 * InProgress to UpdateDisplayData. The latter status will be checked by the
326 * display timer callback on EMT and all required adjustments will be done there.
327 */
328 return rc;
329 }
330
331 /* Set the status so the 'handleResizeCompleted' would work. */
332 f = ASMAtomicCmpXchgU32 (&maFramebuffers[uScreenId].u32ResizeStatus,
333 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
334 AssertRelease(f);NOREF(f);
335
336 AssertRelease(!maFramebuffers[uScreenId].pendingResize.fPending);
337
338 /* The method also unlocks the framebuffer. */
339 handleResizeCompletedEMT();
340
341 return VINF_SUCCESS;
342}
343
344/**
345 * Framebuffer has been resized.
346 * Read the new display data and unlock the framebuffer.
347 *
348 * @thread EMT
349 */
350void Display::handleResizeCompletedEMT (void)
351{
352 LogFlowFunc(("\n"));
353
354 unsigned uScreenId;
355 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
356 {
357 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
358
359 /* Try to into non resizing state. */
360 bool f = ASMAtomicCmpXchgU32 (&pFBInfo->u32ResizeStatus, ResizeStatus_Void, ResizeStatus_UpdateDisplayData);
361
362 if (f == false)
363 {
364 /* This is not the display that has completed resizing. */
365 continue;
366 }
367
368 /* Check whether a resize is pending for this framebuffer. */
369 if (pFBInfo->pendingResize.fPending)
370 {
371 /* Reset the condition, call the display resize with saved data and continue.
372 *
373 * Note: handleDisplayResize can call handleResizeCompletedEMT back,
374 * but infinite recursion is not possible, because when the handleResizeCompletedEMT
375 * is called, the pFBInfo->pendingResize.fPending is equal to false.
376 */
377 pFBInfo->pendingResize.fPending = false;
378 handleDisplayResize (uScreenId, pFBInfo->pendingResize.bpp, pFBInfo->pendingResize.pvVRAM,
379 pFBInfo->pendingResize.cbLine, pFBInfo->pendingResize.w, pFBInfo->pendingResize.h);
380 continue;
381 }
382
383 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN && !pFBInfo->pFramebuffer.isNull())
384 {
385 /* Primary framebuffer has completed the resize. Update the connector data for VGA device. */
386 updateDisplayData();
387
388 /* Check the framebuffer pixel format to setup the rendering in VGA device. */
389 BOOL usesGuestVRAM = FALSE;
390 pFBInfo->pFramebuffer->COMGETTER(UsesGuestVRAM) (&usesGuestVRAM);
391
392 pFBInfo->fDefaultFormat = (usesGuestVRAM == FALSE);
393
394 mpDrv->pUpPort->pfnSetRenderVRAM (mpDrv->pUpPort, pFBInfo->fDefaultFormat);
395 }
396
397#ifdef DEBUG_sunlover
398 if (!stam)
399 {
400 /* protect mpVM */
401 Console::SafeVMPtr pVM (mParent);
402 AssertComRC (pVM.rc());
403
404 STAM_REG(pVM, &StatDisplayRefresh, STAMTYPE_PROFILE, "/PROF/Display/Refresh", STAMUNIT_TICKS_PER_CALL, "Time spent in EMT for display updates.");
405 stam = 1;
406 }
407#endif /* DEBUG_sunlover */
408
409 /* Inform VRDP server about the change of display parameters. */
410 LogFlowFunc (("Calling VRDP\n"));
411 mParent->consoleVRDPServer()->SendResize();
412 }
413}
414
415static void checkCoordBounds (int *px, int *py, int *pw, int *ph, int cx, int cy)
416{
417 /* Correct negative x and y coordinates. */
418 if (*px < 0)
419 {
420 *px += *pw; /* Compute xRight which is also the new width. */
421
422 *pw = (*px < 0)? 0: *px;
423
424 *px = 0;
425 }
426
427 if (*py < 0)
428 {
429 *py += *ph; /* Compute xBottom, which is also the new height. */
430
431 *ph = (*py < 0)? 0: *py;
432
433 *py = 0;
434 }
435
436 /* Also check if coords are greater than the display resolution. */
437 if (*px + *pw > cx)
438 {
439 *pw = cx > *px? cx - *px: 0;
440 }
441
442 if (*py + *ph > cy)
443 {
444 *ph = cy > *py? cy - *py: 0;
445 }
446}
447
448unsigned mapCoordsToScreen(DISPLAYFBINFO *pInfos, unsigned cInfos, int *px, int *py, int *pw, int *ph)
449{
450 DISPLAYFBINFO *pInfo = pInfos;
451 unsigned uScreenId;
452 LogSunlover (("mapCoordsToScreen: %d,%d %dx%d\n", *px, *py, *pw, *ph));
453 for (uScreenId = 0; uScreenId < cInfos; uScreenId++, pInfo++)
454 {
455 LogSunlover ((" [%d] %d,%d %dx%d\n", uScreenId, pInfo->xOrigin, pInfo->yOrigin, pInfo->w, pInfo->h));
456 if ( (pInfo->xOrigin <= *px && *px < pInfo->xOrigin + (int)pInfo->w)
457 && (pInfo->yOrigin <= *py && *py < pInfo->yOrigin + (int)pInfo->h))
458 {
459 /* The rectangle belongs to the screen. Correct coordinates. */
460 *px -= pInfo->xOrigin;
461 *py -= pInfo->yOrigin;
462 LogSunlover ((" -> %d,%d", *px, *py));
463 break;
464 }
465 }
466 if (uScreenId == cInfos)
467 {
468 /* Map to primary screen. */
469 uScreenId = 0;
470 }
471 LogSunlover ((" scr %d\n", uScreenId));
472 return uScreenId;
473}
474
475
476/**
477 * Handles display update event.
478 *
479 * @param x Update area x coordinate
480 * @param y Update area y coordinate
481 * @param w Update area width
482 * @param h Update area height
483 *
484 * @thread EMT
485 */
486void Display::handleDisplayUpdate (int x, int y, int w, int h)
487{
488#ifdef DEBUG_sunlover
489 LogFlowFunc (("%d,%d %dx%d (%d,%d)\n",
490 x, y, w, h, mpDrv->Connector.cx, mpDrv->Connector.cy));
491#endif /* DEBUG_sunlover */
492
493 unsigned uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
494
495#ifdef DEBUG_sunlover
496 LogFlowFunc (("%d,%d %dx%d (checked)\n", x, y, w, h));
497#endif /* DEBUG_sunlover */
498
499 IFramebuffer *pFramebuffer = maFramebuffers[uScreenId].pFramebuffer;
500
501 // if there is no framebuffer, this call is not interesting
502 if (pFramebuffer == NULL)
503 return;
504
505 pFramebuffer->Lock();
506
507 checkCoordBounds (&x, &y, &w, &h, mpDrv->Connector.cx, mpDrv->Connector.cy);
508
509 if (w != 0 && h != 0)
510 pFramebuffer->NotifyUpdate(x, y, w, h);
511
512 pFramebuffer->Unlock();
513
514 if (!mfVideoAccelEnabled)
515 {
516 /* When VBVA is enabled, the VRDP server is informed in the VideoAccelFlush.
517 * Inform the server here only if VBVA is disabled.
518 */
519 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
520 mParent->consoleVRDPServer()->SendUpdateBitmap(uScreenId, x, y, w, h);
521 }
522}
523
524typedef struct _VBVADIRTYREGION
525{
526 /* Copies of object's pointers used by vbvaRgn functions. */
527 DISPLAYFBINFO *paFramebuffers;
528 unsigned cMonitors;
529 Display *pDisplay;
530 PPDMIDISPLAYPORT pPort;
531
532} VBVADIRTYREGION;
533
534static void vbvaRgnInit (VBVADIRTYREGION *prgn, DISPLAYFBINFO *paFramebuffers, unsigned cMonitors, Display *pd, PPDMIDISPLAYPORT pp)
535{
536 prgn->paFramebuffers = paFramebuffers;
537 prgn->cMonitors = cMonitors;
538 prgn->pDisplay = pd;
539 prgn->pPort = pp;
540
541 unsigned uScreenId;
542 for (uScreenId = 0; uScreenId < cMonitors; uScreenId++)
543 {
544 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
545
546 memset (&pFBInfo->dirtyRect, 0, sizeof (pFBInfo->dirtyRect));
547 }
548}
549
550static void vbvaRgnDirtyRect (VBVADIRTYREGION *prgn, unsigned uScreenId, VBVACMDHDR *phdr)
551{
552 LogSunlover (("x = %d, y = %d, w = %d, h = %d\n",
553 phdr->x, phdr->y, phdr->w, phdr->h));
554
555 /*
556 * Here update rectangles are accumulated to form an update area.
557 * @todo
558 * Now the simpliest method is used which builds one rectangle that
559 * includes all update areas. A bit more advanced method can be
560 * employed here. The method should be fast however.
561 */
562 if (phdr->w == 0 || phdr->h == 0)
563 {
564 /* Empty rectangle. */
565 return;
566 }
567
568 int32_t xRight = phdr->x + phdr->w;
569 int32_t yBottom = phdr->y + phdr->h;
570
571 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
572
573 if (pFBInfo->dirtyRect.xRight == 0)
574 {
575 /* This is the first rectangle to be added. */
576 pFBInfo->dirtyRect.xLeft = phdr->x;
577 pFBInfo->dirtyRect.yTop = phdr->y;
578 pFBInfo->dirtyRect.xRight = xRight;
579 pFBInfo->dirtyRect.yBottom = yBottom;
580 }
581 else
582 {
583 /* Adjust region coordinates. */
584 if (pFBInfo->dirtyRect.xLeft > phdr->x)
585 {
586 pFBInfo->dirtyRect.xLeft = phdr->x;
587 }
588
589 if (pFBInfo->dirtyRect.yTop > phdr->y)
590 {
591 pFBInfo->dirtyRect.yTop = phdr->y;
592 }
593
594 if (pFBInfo->dirtyRect.xRight < xRight)
595 {
596 pFBInfo->dirtyRect.xRight = xRight;
597 }
598
599 if (pFBInfo->dirtyRect.yBottom < yBottom)
600 {
601 pFBInfo->dirtyRect.yBottom = yBottom;
602 }
603 }
604
605 if (pFBInfo->fDefaultFormat)
606 {
607 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
608 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, phdr->x, phdr->y, phdr->w, phdr->h);
609 prgn->pDisplay->handleDisplayUpdate (phdr->x, phdr->y, phdr->w, phdr->h);
610 }
611
612 return;
613}
614
615static void vbvaRgnUpdateFramebuffer (VBVADIRTYREGION *prgn, unsigned uScreenId)
616{
617 DISPLAYFBINFO *pFBInfo = &prgn->paFramebuffers[uScreenId];
618
619 uint32_t w = pFBInfo->dirtyRect.xRight - pFBInfo->dirtyRect.xLeft;
620 uint32_t h = pFBInfo->dirtyRect.yBottom - pFBInfo->dirtyRect.yTop;
621
622 if (!pFBInfo->fDefaultFormat && pFBInfo->pFramebuffer && w != 0 && h != 0)
623 {
624 //@todo pfnUpdateDisplayRect must take the vram offset parameter for the framebuffer
625 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
626 prgn->pDisplay->handleDisplayUpdate (pFBInfo->dirtyRect.xLeft, pFBInfo->dirtyRect.yTop, w, h);
627 }
628}
629
630static void vbvaSetMemoryFlags (VBVAMEMORY *pVbvaMemory,
631 bool fVideoAccelEnabled,
632 bool fVideoAccelVRDP,
633 uint32_t fu32SupportedOrders,
634 DISPLAYFBINFO *paFBInfos,
635 unsigned cFBInfos)
636{
637 if (pVbvaMemory)
638 {
639 /* This called only on changes in mode. So reset VRDP always. */
640 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
641
642 if (fVideoAccelEnabled)
643 {
644 fu32Flags |= VBVA_F_MODE_ENABLED;
645
646 if (fVideoAccelVRDP)
647 {
648 fu32Flags |= VBVA_F_MODE_VRDP | VBVA_F_MODE_VRDP_ORDER_MASK;
649
650 pVbvaMemory->fu32SupportedOrders = fu32SupportedOrders;
651 }
652 }
653
654 pVbvaMemory->fu32ModeFlags = fu32Flags;
655 }
656
657 unsigned uScreenId;
658 for (uScreenId = 0; uScreenId < cFBInfos; uScreenId++)
659 {
660 if (paFBInfos[uScreenId].pHostEvents)
661 {
662 paFBInfos[uScreenId].pHostEvents->fu32Events |= VBOX_VIDEO_INFO_HOST_EVENTS_F_VRDP_RESET;
663 }
664 }
665}
666
667bool Display::VideoAccelAllowed (void)
668{
669 return true;
670}
671
672/**
673 * @thread EMT
674 */
675int Display::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
676{
677 int rc = VINF_SUCCESS;
678
679 /* Called each time the guest wants to use acceleration,
680 * or when the VGA device disables acceleration,
681 * or when restoring the saved state with accel enabled.
682 *
683 * VGA device disables acceleration on each video mode change
684 * and on reset.
685 *
686 * Guest enabled acceleration at will. And it has to enable
687 * acceleration after a mode change.
688 */
689 LogFlowFunc (("mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
690 mfVideoAccelEnabled, fEnable, pVbvaMemory));
691
692 /* Strictly check parameters. Callers must not pass anything in the case. */
693 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
694
695 if (!VideoAccelAllowed ())
696 {
697 return VERR_NOT_SUPPORTED;
698 }
699
700 /*
701 * Verify that the VM is in running state. If it is not,
702 * then this must be postponed until it goes to running.
703 */
704 if (!mfMachineRunning)
705 {
706 Assert (!mfVideoAccelEnabled);
707
708 LogFlowFunc (("Machine is not yet running.\n"));
709
710 if (fEnable)
711 {
712 mfPendingVideoAccelEnable = fEnable;
713 mpPendingVbvaMemory = pVbvaMemory;
714 }
715
716 return rc;
717 }
718
719 /* Check that current status is not being changed */
720 if (mfVideoAccelEnabled == fEnable)
721 {
722 return rc;
723 }
724
725 if (mfVideoAccelEnabled)
726 {
727 /* Process any pending orders and empty the VBVA ring buffer. */
728 VideoAccelFlush ();
729 }
730
731 if (!fEnable && mpVbvaMemory)
732 {
733 mpVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
734 }
735
736 /* Safety precaution. There is no more VBVA until everything is setup! */
737 mpVbvaMemory = NULL;
738 mfVideoAccelEnabled = false;
739
740 /* Update entire display. */
741 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].u32ResizeStatus == ResizeStatus_Void)
742 {
743 mpDrv->pUpPort->pfnUpdateDisplayAll(mpDrv->pUpPort);
744 }
745
746 /* Everything OK. VBVA status can be changed. */
747
748 /* Notify the VMMDev, which saves VBVA status in the saved state,
749 * and needs to know current status.
750 */
751 PPDMIVMMDEVPORT pVMMDevPort = mParent->getVMMDev()->getVMMDevPort ();
752
753 if (pVMMDevPort)
754 {
755 pVMMDevPort->pfnVBVAChange (pVMMDevPort, fEnable);
756 }
757
758 if (fEnable)
759 {
760 mpVbvaMemory = pVbvaMemory;
761 mfVideoAccelEnabled = true;
762
763 /* Initialize the hardware memory. */
764 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
765 mpVbvaMemory->off32Data = 0;
766 mpVbvaMemory->off32Free = 0;
767
768 memset (mpVbvaMemory->aRecords, 0, sizeof (mpVbvaMemory->aRecords));
769 mpVbvaMemory->indexRecordFirst = 0;
770 mpVbvaMemory->indexRecordFree = 0;
771
772 LogRel(("VBVA: Enabled.\n"));
773 }
774 else
775 {
776 LogRel(("VBVA: Disabled.\n"));
777 }
778
779 LogFlowFunc (("VideoAccelEnable: rc = %Rrc.\n", rc));
780
781 return rc;
782}
783
784#ifdef VBOX_WITH_VRDP
785/* Called always by one VRDP server thread. Can be thread-unsafe.
786 */
787void Display::VideoAccelVRDP (bool fEnable)
788{
789 int c = fEnable?
790 ASMAtomicIncS32 (&mcVideoAccelVRDPRefs):
791 ASMAtomicDecS32 (&mcVideoAccelVRDPRefs);
792
793 Assert (c >= 0);
794
795 if (c == 0)
796 {
797 /* The last client has disconnected, and the accel can be
798 * disabled.
799 */
800 Assert (fEnable == false);
801
802 mfVideoAccelVRDP = false;
803 mfu32SupportedOrders = 0;
804
805 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
806
807 LogRel(("VBVA: VRDP acceleration has been disabled.\n"));
808 }
809 else if ( c == 1
810 && !mfVideoAccelVRDP)
811 {
812 /* The first client has connected. Enable the accel.
813 */
814 Assert (fEnable == true);
815
816 mfVideoAccelVRDP = true;
817 /* Supporting all orders. */
818 mfu32SupportedOrders = ~0;
819
820 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, mfVideoAccelVRDP, mfu32SupportedOrders, maFramebuffers, mcMonitors);
821
822 LogRel(("VBVA: VRDP acceleration has been requested.\n"));
823 }
824 else
825 {
826 /* A client is connected or disconnected but there is no change in the
827 * accel state. It remains enabled.
828 */
829 Assert (mfVideoAccelVRDP == true);
830 }
831}
832#endif /* VBOX_WITH_VRDP */
833
834static bool vbvaVerifyRingBuffer (VBVAMEMORY *pVbvaMemory)
835{
836 return true;
837}
838
839static void vbvaFetchBytes (VBVAMEMORY *pVbvaMemory, uint8_t *pu8Dst, uint32_t cbDst)
840{
841 if (cbDst >= VBVA_RING_BUFFER_SIZE)
842 {
843 AssertMsgFailed (("cbDst = 0x%08X, ring buffer size 0x%08X", cbDst, VBVA_RING_BUFFER_SIZE));
844 return;
845 }
846
847 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
848 uint8_t *src = &pVbvaMemory->au8RingBuffer[pVbvaMemory->off32Data];
849 int32_t i32Diff = cbDst - u32BytesTillBoundary;
850
851 if (i32Diff <= 0)
852 {
853 /* Chunk will not cross buffer boundary. */
854 memcpy (pu8Dst, src, cbDst);
855 }
856 else
857 {
858 /* Chunk crosses buffer boundary. */
859 memcpy (pu8Dst, src, u32BytesTillBoundary);
860 memcpy (pu8Dst + u32BytesTillBoundary, &pVbvaMemory->au8RingBuffer[0], i32Diff);
861 }
862
863 /* Advance data offset. */
864 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbDst) % VBVA_RING_BUFFER_SIZE;
865
866 return;
867}
868
869
870static bool vbvaPartialRead (uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
871{
872 uint8_t *pu8New;
873
874 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
875 *ppu8, *pcb, cbRecord));
876
877 if (*ppu8)
878 {
879 Assert (*pcb);
880 pu8New = (uint8_t *)RTMemRealloc (*ppu8, cbRecord);
881 }
882 else
883 {
884 Assert (!*pcb);
885 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
886 }
887
888 if (!pu8New)
889 {
890 /* Memory allocation failed, fail the function. */
891 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
892 cbRecord));
893
894 if (*ppu8)
895 {
896 RTMemFree (*ppu8);
897 }
898
899 *ppu8 = NULL;
900 *pcb = 0;
901
902 return false;
903 }
904
905 /* Fetch data from the ring buffer. */
906 vbvaFetchBytes (pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
907
908 *ppu8 = pu8New;
909 *pcb = cbRecord;
910
911 return true;
912}
913
914/* For contiguous chunks just return the address in the buffer.
915 * For crossing boundary - allocate a buffer from heap.
916 */
917bool Display::vbvaFetchCmd (VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
918{
919 uint32_t indexRecordFirst = mpVbvaMemory->indexRecordFirst;
920 uint32_t indexRecordFree = mpVbvaMemory->indexRecordFree;
921
922#ifdef DEBUG_sunlover
923 LogFlowFunc (("first = %d, free = %d\n",
924 indexRecordFirst, indexRecordFree));
925#endif /* DEBUG_sunlover */
926
927 if (!vbvaVerifyRingBuffer (mpVbvaMemory))
928 {
929 return false;
930 }
931
932 if (indexRecordFirst == indexRecordFree)
933 {
934 /* No records to process. Return without assigning output variables. */
935 return true;
936 }
937
938 VBVARECORD *pRecord = &mpVbvaMemory->aRecords[indexRecordFirst];
939
940#ifdef DEBUG_sunlover
941 LogFlowFunc (("cbRecord = 0x%08X\n", pRecord->cbRecord));
942#endif /* DEBUG_sunlover */
943
944 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
945
946 if (mcbVbvaPartial)
947 {
948 /* There is a partial read in process. Continue with it. */
949
950 Assert (mpu8VbvaPartial);
951
952 LogFlowFunc (("continue partial record mcbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
953 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
954
955 if (cbRecord > mcbVbvaPartial)
956 {
957 /* New data has been added to the record. */
958 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
959 {
960 return false;
961 }
962 }
963
964 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
965 {
966 /* The record is completed by guest. Return it to the caller. */
967 *ppHdr = (VBVACMDHDR *)mpu8VbvaPartial;
968 *pcbCmd = mcbVbvaPartial;
969
970 mpu8VbvaPartial = NULL;
971 mcbVbvaPartial = 0;
972
973 /* Advance the record index. */
974 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
975
976#ifdef DEBUG_sunlover
977 LogFlowFunc (("partial done ok, data = %d, free = %d\n",
978 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
979#endif /* DEBUG_sunlover */
980 }
981
982 return true;
983 }
984
985 /* A new record need to be processed. */
986 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
987 {
988 /* Current record is being written by guest. '=' is important here. */
989 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
990 {
991 /* Partial read must be started. */
992 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
993 {
994 return false;
995 }
996
997 LogFlowFunc (("started partial record mcbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
998 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
999 }
1000
1001 return true;
1002 }
1003
1004 /* Current record is complete. If it is not empty, process it. */
1005 if (cbRecord)
1006 {
1007 /* The size of largest contiguos chunk in the ring biffer. */
1008 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - mpVbvaMemory->off32Data;
1009
1010 /* The ring buffer pointer. */
1011 uint8_t *au8RingBuffer = &mpVbvaMemory->au8RingBuffer[0];
1012
1013 /* The pointer to data in the ring buffer. */
1014 uint8_t *src = &au8RingBuffer[mpVbvaMemory->off32Data];
1015
1016 /* Fetch or point the data. */
1017 if (u32BytesTillBoundary >= cbRecord)
1018 {
1019 /* The command does not cross buffer boundary. Return address in the buffer. */
1020 *ppHdr = (VBVACMDHDR *)src;
1021
1022 /* Advance data offset. */
1023 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1024 }
1025 else
1026 {
1027 /* The command crosses buffer boundary. Rare case, so not optimized. */
1028 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
1029
1030 if (!dst)
1031 {
1032 LogFlowFunc (("could not allocate %d bytes from heap!!!\n", cbRecord));
1033 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
1034 return false;
1035 }
1036
1037 vbvaFetchBytes (mpVbvaMemory, dst, cbRecord);
1038
1039 *ppHdr = (VBVACMDHDR *)dst;
1040
1041#ifdef DEBUG_sunlover
1042 LogFlowFunc (("Allocated from heap %p\n", dst));
1043#endif /* DEBUG_sunlover */
1044 }
1045 }
1046
1047 *pcbCmd = cbRecord;
1048
1049 /* Advance the record index. */
1050 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
1051
1052#ifdef DEBUG_sunlover
1053 LogFlowFunc (("done ok, data = %d, free = %d\n",
1054 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1055#endif /* DEBUG_sunlover */
1056
1057 return true;
1058}
1059
1060void Display::vbvaReleaseCmd (VBVACMDHDR *pHdr, int32_t cbCmd)
1061{
1062 uint8_t *au8RingBuffer = mpVbvaMemory->au8RingBuffer;
1063
1064 if ( (uint8_t *)pHdr >= au8RingBuffer
1065 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
1066 {
1067 /* The pointer is inside ring buffer. Must be continuous chunk. */
1068 Assert (VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
1069
1070 /* Do nothing. */
1071
1072 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1073 }
1074 else
1075 {
1076 /* The pointer is outside. It is then an allocated copy. */
1077
1078#ifdef DEBUG_sunlover
1079 LogFlowFunc (("Free heap %p\n", pHdr));
1080#endif /* DEBUG_sunlover */
1081
1082 if ((uint8_t *)pHdr == mpu8VbvaPartial)
1083 {
1084 mpu8VbvaPartial = NULL;
1085 mcbVbvaPartial = 0;
1086 }
1087 else
1088 {
1089 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1090 }
1091
1092 RTMemFree (pHdr);
1093 }
1094
1095 return;
1096}
1097
1098
1099/**
1100 * Called regularly on the DisplayRefresh timer.
1101 * Also on behalf of guest, when the ring buffer is full.
1102 *
1103 * @thread EMT
1104 */
1105void Display::VideoAccelFlush (void)
1106{
1107#ifdef DEBUG_sunlover_2
1108 LogFlowFunc (("mfVideoAccelEnabled = %d\n", mfVideoAccelEnabled));
1109#endif /* DEBUG_sunlover_2 */
1110
1111 if (!mfVideoAccelEnabled)
1112 {
1113 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
1114 return;
1115 }
1116
1117 /* Here VBVA is enabled and we have the accelerator memory pointer. */
1118 Assert(mpVbvaMemory);
1119
1120#ifdef DEBUG_sunlover_2
1121 LogFlowFunc (("indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
1122 mpVbvaMemory->indexRecordFirst, mpVbvaMemory->indexRecordFree, mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1123#endif /* DEBUG_sunlover_2 */
1124
1125 /* Quick check for "nothing to update" case. */
1126 if (mpVbvaMemory->indexRecordFirst == mpVbvaMemory->indexRecordFree)
1127 {
1128 return;
1129 }
1130
1131 /* Process the ring buffer */
1132 unsigned uScreenId;
1133 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1134 {
1135 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1136 {
1137 maFramebuffers[uScreenId].pFramebuffer->Lock ();
1138 }
1139 }
1140
1141 /* Initialize dirty rectangles accumulator. */
1142 VBVADIRTYREGION rgn;
1143 vbvaRgnInit (&rgn, maFramebuffers, mcMonitors, this, mpDrv->pUpPort);
1144
1145 for (;;)
1146 {
1147 VBVACMDHDR *phdr = NULL;
1148 uint32_t cbCmd = ~0;
1149
1150 /* Fetch the command data. */
1151 if (!vbvaFetchCmd (&phdr, &cbCmd))
1152 {
1153 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
1154 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1155
1156 /* Disable VBVA on those processing errors. */
1157 VideoAccelEnable (false, NULL);
1158
1159 break;
1160 }
1161
1162 if (cbCmd == uint32_t(~0))
1163 {
1164 /* No more commands yet in the queue. */
1165 break;
1166 }
1167
1168 if (cbCmd != 0)
1169 {
1170#ifdef DEBUG_sunlover
1171 LogFlowFunc (("hdr: cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n",
1172 cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
1173#endif /* DEBUG_sunlover */
1174
1175 VBVACMDHDR hdrSaved = *phdr;
1176
1177 int x = phdr->x;
1178 int y = phdr->y;
1179 int w = phdr->w;
1180 int h = phdr->h;
1181
1182 uScreenId = mapCoordsToScreen(maFramebuffers, mcMonitors, &x, &y, &w, &h);
1183
1184 phdr->x = (int16_t)x;
1185 phdr->y = (int16_t)y;
1186 phdr->w = (uint16_t)w;
1187 phdr->h = (uint16_t)h;
1188
1189 DISPLAYFBINFO *pFBInfo = &maFramebuffers[uScreenId];
1190
1191 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
1192 {
1193 /* Handle the command.
1194 *
1195 * Guest is responsible for updating the guest video memory.
1196 * The Windows guest does all drawing using Eng*.
1197 *
1198 * For local output, only dirty rectangle information is used
1199 * to update changed areas.
1200 *
1201 * Dirty rectangles are accumulated to exclude overlapping updates and
1202 * group small updates to a larger one.
1203 */
1204
1205 /* Accumulate the update. */
1206 vbvaRgnDirtyRect (&rgn, uScreenId, phdr);
1207
1208 /* Forward the command to VRDP server. */
1209 mParent->consoleVRDPServer()->SendUpdate (uScreenId, phdr, cbCmd);
1210
1211 *phdr = hdrSaved;
1212 }
1213 }
1214
1215 vbvaReleaseCmd (phdr, cbCmd);
1216 }
1217
1218 for (uScreenId = 0; uScreenId < mcMonitors; uScreenId++)
1219 {
1220 if (!maFramebuffers[uScreenId].pFramebuffer.isNull())
1221 {
1222 maFramebuffers[uScreenId].pFramebuffer->Unlock ();
1223 }
1224
1225 if (maFramebuffers[uScreenId].u32ResizeStatus == ResizeStatus_Void)
1226 {
1227 /* Draw the framebuffer. */
1228 vbvaRgnUpdateFramebuffer (&rgn, uScreenId);
1229 }
1230 }
1231}
1232
1233
1234// IDisplay properties
1235/////////////////////////////////////////////////////////////////////////////
1236
1237/**
1238 * Returns the current display width in pixel
1239 *
1240 * @returns COM status code
1241 * @param width Address of result variable.
1242 */
1243STDMETHODIMP Display::COMGETTER(Width) (ULONG *width)
1244{
1245 CheckComArgNotNull(width);
1246
1247 AutoCaller autoCaller (this);
1248 CheckComRCReturnRC (autoCaller.rc());
1249
1250 AutoWriteLock alock (this);
1251
1252 CHECK_CONSOLE_DRV (mpDrv);
1253
1254 *width = mpDrv->Connector.cx;
1255
1256 return S_OK;
1257}
1258
1259/**
1260 * Returns the current display height in pixel
1261 *
1262 * @returns COM status code
1263 * @param height Address of result variable.
1264 */
1265STDMETHODIMP Display::COMGETTER(Height) (ULONG *height)
1266{
1267 CheckComArgNotNull(height);
1268
1269 AutoCaller autoCaller (this);
1270 CheckComRCReturnRC (autoCaller.rc());
1271
1272 AutoWriteLock alock (this);
1273
1274 CHECK_CONSOLE_DRV (mpDrv);
1275
1276 *height = mpDrv->Connector.cy;
1277
1278 return S_OK;
1279}
1280
1281/**
1282 * Returns the current display color depth in bits
1283 *
1284 * @returns COM status code
1285 * @param bitsPerPixel Address of result variable.
1286 */
1287STDMETHODIMP Display::COMGETTER(BitsPerPixel) (ULONG *bitsPerPixel)
1288{
1289 if (!bitsPerPixel)
1290 return E_INVALIDARG;
1291
1292 AutoCaller autoCaller (this);
1293 CheckComRCReturnRC (autoCaller.rc());
1294
1295 AutoWriteLock alock (this);
1296
1297 CHECK_CONSOLE_DRV (mpDrv);
1298
1299 uint32_t cBits = 0;
1300 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1301 AssertRC(rc);
1302 *bitsPerPixel = cBits;
1303
1304 return S_OK;
1305}
1306
1307
1308// IDisplay methods
1309/////////////////////////////////////////////////////////////////////////////
1310
1311STDMETHODIMP Display::SetFramebuffer (ULONG aScreenId,
1312 IFramebuffer *aFramebuffer)
1313{
1314 LogFlowFunc (("\n"));
1315
1316 if (aFramebuffer != NULL)
1317 CheckComArgOutPointerValid(aFramebuffer);
1318
1319 AutoCaller autoCaller (this);
1320 CheckComRCReturnRC (autoCaller.rc());
1321
1322 AutoWriteLock alock (this);
1323
1324 Console::SafeVMPtrQuiet pVM (mParent);
1325 if (pVM.isOk())
1326 {
1327 /* Must leave the lock here because the changeFramebuffer will
1328 * also obtain it. */
1329 alock.leave ();
1330
1331 /* send request to the EMT thread */
1332 PVMREQ pReq = NULL;
1333 int vrc = VMR3ReqCall (pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT,
1334 (PFNRT) changeFramebuffer, 3, this, aFramebuffer, aScreenId);
1335 if (RT_SUCCESS (vrc))
1336 vrc = pReq->iStatus;
1337 VMR3ReqFree (pReq);
1338
1339 alock.enter ();
1340
1341 ComAssertRCRet (vrc, E_FAIL);
1342 }
1343 else
1344 {
1345 /* No VM is created (VM is powered off), do a direct call */
1346 int vrc = changeFramebuffer (this, aFramebuffer, aScreenId);
1347 ComAssertRCRet (vrc, E_FAIL);
1348 }
1349
1350 return S_OK;
1351}
1352
1353STDMETHODIMP Display::GetFramebuffer (ULONG aScreenId,
1354 IFramebuffer **aFramebuffer, LONG *aXOrigin, LONG *aYOrigin)
1355{
1356 LogFlowFunc (("aScreenId = %d\n", aScreenId));
1357
1358 CheckComArgOutPointerValid(aFramebuffer);
1359
1360 AutoCaller autoCaller (this);
1361 CheckComRCReturnRC (autoCaller.rc());
1362
1363 AutoWriteLock alock (this);
1364
1365 /* @todo this should be actually done on EMT. */
1366 DISPLAYFBINFO *pFBInfo = &maFramebuffers[aScreenId];
1367
1368 *aFramebuffer = pFBInfo->pFramebuffer;
1369 if (*aFramebuffer)
1370 (*aFramebuffer)->AddRef ();
1371 if (aXOrigin)
1372 *aXOrigin = pFBInfo->xOrigin;
1373 if (aYOrigin)
1374 *aYOrigin = pFBInfo->yOrigin;
1375
1376 return S_OK;
1377}
1378
1379STDMETHODIMP Display::SetVideoModeHint(ULONG aWidth, ULONG aHeight,
1380 ULONG aBitsPerPixel, ULONG aDisplay)
1381{
1382 AutoCaller autoCaller (this);
1383 CheckComRCReturnRC (autoCaller.rc());
1384
1385 AutoWriteLock alock (this);
1386
1387 CHECK_CONSOLE_DRV (mpDrv);
1388
1389 /*
1390 * Do some rough checks for valid input
1391 */
1392 ULONG width = aWidth;
1393 if (!width)
1394 width = mpDrv->Connector.cx;
1395 ULONG height = aHeight;
1396 if (!height)
1397 height = mpDrv->Connector.cy;
1398 ULONG bpp = aBitsPerPixel;
1399 if (!bpp)
1400 {
1401 uint32_t cBits = 0;
1402 int rc = mpDrv->pUpPort->pfnQueryColorDepth(mpDrv->pUpPort, &cBits);
1403 AssertRC(rc);
1404 bpp = cBits;
1405 }
1406 ULONG cMonitors;
1407 mParent->machine()->COMGETTER(MonitorCount)(&cMonitors);
1408 if (cMonitors == 0 && aDisplay > 0)
1409 return E_INVALIDARG;
1410 if (aDisplay >= cMonitors)
1411 return E_INVALIDARG;
1412
1413// sunlover 20070614: It is up to the guest to decide whether the hint is valid.
1414// ULONG vramSize;
1415// mParent->machine()->COMGETTER(VRAMSize)(&vramSize);
1416// /* enough VRAM? */
1417// if ((width * height * (bpp / 8)) > (vramSize * 1024 * 1024))
1418// return setError(E_FAIL, tr("Not enough VRAM for the selected video mode"));
1419
1420 /* Have to leave the lock because the pfnRequestDisplayChange
1421 * will call EMT. */
1422 alock.leave ();
1423 if (mParent->getVMMDev())
1424 mParent->getVMMDev()->getVMMDevPort()->
1425 pfnRequestDisplayChange (mParent->getVMMDev()->getVMMDevPort(),
1426 aWidth, aHeight, aBitsPerPixel, aDisplay);
1427 return S_OK;
1428}
1429
1430STDMETHODIMP Display::SetSeamlessMode (BOOL enabled)
1431{
1432 AutoCaller autoCaller (this);
1433 CheckComRCReturnRC (autoCaller.rc());
1434
1435 AutoWriteLock alock (this);
1436
1437 /* Have to leave the lock because the pfnRequestSeamlessChange will call EMT. */
1438 alock.leave ();
1439 if (mParent->getVMMDev())
1440 mParent->getVMMDev()->getVMMDevPort()->
1441 pfnRequestSeamlessChange (mParent->getVMMDev()->getVMMDevPort(),
1442 !!enabled);
1443 return S_OK;
1444}
1445
1446STDMETHODIMP Display::TakeScreenShot (BYTE *address, ULONG width, ULONG height)
1447{
1448 /// @todo (r=dmik) this function may take too long to complete if the VM
1449 // is doing something like saving state right now. Which, in case if it
1450 // is called on the GUI thread, will make it unresponsive. We should
1451 // check the machine state here (by enclosing the check and VMRequCall
1452 // within the Console lock to make it atomic).
1453
1454 LogFlowFuncEnter();
1455 LogFlowFunc (("address=%p, width=%d, height=%d\n",
1456 address, width, height));
1457
1458 CheckComArgNotNull(address);
1459 CheckComArgExpr(width, width != 0);
1460 CheckComArgExpr(height, height != 0);
1461
1462 AutoCaller autoCaller (this);
1463 CheckComRCReturnRC (autoCaller.rc());
1464
1465 AutoWriteLock alock (this);
1466
1467 CHECK_CONSOLE_DRV (mpDrv);
1468
1469 Console::SafeVMPtr pVM (mParent);
1470 CheckComRCReturnRC (pVM.rc());
1471
1472 HRESULT rc = S_OK;
1473
1474 LogFlowFunc (("Sending SCREENSHOT request\n"));
1475
1476 /*
1477 * First try use the graphics device features for making a snapshot.
1478 * This does not support stretching, is an optional feature (returns
1479 * not supported).
1480 *
1481 * Note: It may cause a display resize. Watch out for deadlocks.
1482 */
1483 int rcVBox = VERR_NOT_SUPPORTED;
1484 if ( mpDrv->Connector.cx == width
1485 && mpDrv->Connector.cy == height)
1486 {
1487 PVMREQ pReq;
1488 size_t cbData = RT_ALIGN_Z(width, 4) * 4 * height;
1489 rcVBox = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT,
1490 (PFNRT)mpDrv->pUpPort->pfnSnapshot, 6, mpDrv->pUpPort,
1491 address, cbData, (uintptr_t)NULL, (uintptr_t)NULL, (uintptr_t)NULL);
1492 if (RT_SUCCESS(rcVBox))
1493 {
1494 rcVBox = pReq->iStatus;
1495 VMR3ReqFree(pReq);
1496 }
1497 }
1498
1499 /*
1500 * If the function returns not supported, or if stretching is requested,
1501 * we'll have to do all the work ourselves using the framebuffer data.
1502 */
1503 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
1504 {
1505 /** @todo implement snapshot stretching & generic snapshot fallback. */
1506 rc = setError (E_NOTIMPL, tr ("This feature is not implemented"));
1507 }
1508 else if (RT_FAILURE(rcVBox))
1509 rc = setError (VBOX_E_IPRT_ERROR,
1510 tr ("Could not take a screenshot (%Rrc)"), rcVBox);
1511
1512 LogFlowFunc (("rc=%08X\n", rc));
1513 LogFlowFuncLeave();
1514 return rc;
1515}
1516
1517STDMETHODIMP Display::DrawToScreen (BYTE *address, ULONG x, ULONG y,
1518 ULONG width, ULONG height)
1519{
1520 /// @todo (r=dmik) this function may take too long to complete if the VM
1521 // is doing something like saving state right now. Which, in case if it
1522 // is called on the GUI thread, will make it unresponsive. We should
1523 // check the machine state here (by enclosing the check and VMRequCall
1524 // within the Console lock to make it atomic).
1525
1526 LogFlowFuncEnter();
1527 LogFlowFunc (("address=%p, x=%d, y=%d, width=%d, height=%d\n",
1528 (void *)address, x, y, width, height));
1529
1530 CheckComArgNotNull(address);
1531 CheckComArgExpr(width, width != 0);
1532 CheckComArgExpr(height, height != 0);
1533
1534 AutoCaller autoCaller (this);
1535 CheckComRCReturnRC (autoCaller.rc());
1536
1537 AutoWriteLock alock (this);
1538
1539 CHECK_CONSOLE_DRV (mpDrv);
1540
1541 Console::SafeVMPtr pVM (mParent);
1542 CheckComRCReturnRC (pVM.rc());
1543
1544 /*
1545 * Again we're lazy and make the graphics device do all the
1546 * dirty conversion work.
1547 */
1548 PVMREQ pReq;
1549 int rcVBox = VMR3ReqCall(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT,
1550 (PFNRT)mpDrv->pUpPort->pfnDisplayBlt, 6, mpDrv->pUpPort,
1551 address, x, y, width, height);
1552 if (RT_SUCCESS(rcVBox))
1553 {
1554 rcVBox = pReq->iStatus;
1555 VMR3ReqFree(pReq);
1556 }
1557
1558 /*
1559 * If the function returns not supported, we'll have to do all the
1560 * work ourselves using the framebuffer.
1561 */
1562 HRESULT rc = S_OK;
1563 if (rcVBox == VERR_NOT_SUPPORTED || rcVBox == VERR_NOT_IMPLEMENTED)
1564 {
1565 /** @todo implement generic fallback for screen blitting. */
1566 rc = E_NOTIMPL;
1567 }
1568 else if (RT_FAILURE(rcVBox))
1569 rc = setError (VBOX_E_IPRT_ERROR,
1570 tr ("Could not draw to the screen (%Rrc)"), rcVBox);
1571//@todo
1572// else
1573// {
1574// /* All ok. Redraw the screen. */
1575// handleDisplayUpdate (x, y, width, height);
1576// }
1577
1578 LogFlowFunc (("rc=%08X\n", rc));
1579 LogFlowFuncLeave();
1580 return rc;
1581}
1582
1583/**
1584 * Does a full invalidation of the VM display and instructs the VM
1585 * to update it immediately.
1586 *
1587 * @returns COM status code
1588 */
1589STDMETHODIMP Display::InvalidateAndUpdate()
1590{
1591 LogFlowFuncEnter();
1592
1593 AutoCaller autoCaller (this);
1594 CheckComRCReturnRC (autoCaller.rc());
1595
1596 AutoWriteLock alock (this);
1597
1598 CHECK_CONSOLE_DRV (mpDrv);
1599
1600 Console::SafeVMPtr pVM (mParent);
1601 CheckComRCReturnRC (pVM.rc());
1602
1603 HRESULT rc = S_OK;
1604
1605 LogFlowFunc (("Sending DPYUPDATE request\n"));
1606
1607 /* Have to leave the lock when calling EMT. */
1608 alock.leave ();
1609
1610 /* pdm.h says that this has to be called from the EMT thread */
1611 PVMREQ pReq;
1612 int rcVBox = VMR3ReqCallVoid(pVM, VMCPUID_ANY, &pReq, RT_INDEFINITE_WAIT,
1613 (PFNRT)mpDrv->pUpPort->pfnUpdateDisplayAll, 1, mpDrv->pUpPort);
1614 if (RT_SUCCESS(rcVBox))
1615 VMR3ReqFree(pReq);
1616
1617 alock.enter ();
1618
1619 if (RT_FAILURE(rcVBox))
1620 rc = setError (VBOX_E_IPRT_ERROR,
1621 tr ("Could not invalidate and update the screen (%Rrc)"), rcVBox);
1622
1623 LogFlowFunc (("rc=%08X\n", rc));
1624 LogFlowFuncLeave();
1625 return rc;
1626}
1627
1628/**
1629 * Notification that the framebuffer has completed the
1630 * asynchronous resize processing
1631 *
1632 * @returns COM status code
1633 */
1634STDMETHODIMP Display::ResizeCompleted(ULONG aScreenId)
1635{
1636 LogFlowFunc (("\n"));
1637
1638 /// @todo (dmik) can we AutoWriteLock alock (this); here?
1639 // do it when we switch this class to VirtualBoxBase_NEXT.
1640 // This will require general code review and may add some details.
1641 // In particular, we may want to check whether EMT is really waiting for
1642 // this notification, etc. It might be also good to obey the caller to make
1643 // sure this method is not called from more than one thread at a time
1644 // (and therefore don't use Display lock at all here to save some
1645 // milliseconds).
1646 AutoCaller autoCaller (this);
1647 CheckComRCReturnRC (autoCaller.rc());
1648
1649 /* this is only valid for external framebuffers */
1650 if (maFramebuffers[aScreenId].pFramebuffer == NULL)
1651 return setError (VBOX_E_NOT_SUPPORTED,
1652 tr ("Resize completed notification is valid only "
1653 "for external framebuffers"));
1654
1655 /* Set the flag indicating that the resize has completed and display
1656 * data need to be updated. */
1657 bool f = ASMAtomicCmpXchgU32 (&maFramebuffers[aScreenId].u32ResizeStatus,
1658 ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
1659 AssertRelease(f);NOREF(f);
1660
1661 return S_OK;
1662}
1663
1664/**
1665 * Notification that the framebuffer has completed the
1666 * asynchronous update processing
1667 *
1668 * @returns COM status code
1669 */
1670STDMETHODIMP Display::UpdateCompleted()
1671{
1672 LogFlowFunc (("\n"));
1673
1674 /// @todo (dmik) can we AutoWriteLock alock (this); here?
1675 // do it when we switch this class to VirtualBoxBase_NEXT.
1676 // Tthis will require general code review and may add some details.
1677 // In particular, we may want to check whether EMT is really waiting for
1678 // this notification, etc. It might be also good to obey the caller to make
1679 // sure this method is not called from more than one thread at a time
1680 // (and therefore don't use Display lock at all here to save some
1681 // milliseconds).
1682 AutoCaller autoCaller (this);
1683 CheckComRCReturnRC (autoCaller.rc());
1684
1685 /* this is only valid for external framebuffers */
1686 if (maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer == NULL)
1687 return setError (VBOX_E_NOT_SUPPORTED,
1688 tr ("Resize completed notification is valid only "
1689 "for external framebuffers"));
1690
1691 return S_OK;
1692}
1693
1694STDMETHODIMP Display::CompleteVHWACommand(BYTE *pCommand)
1695{
1696#ifdef VBOX_WITH_VIDEOHWACCEL
1697 mpDrv->pVBVACallbacks->pfnVHWACommandCompleteAsynch(mpDrv->pVBVACallbacks, (PVBOXVHWACMD)pCommand);
1698 return S_OK;
1699#else
1700 return E_NOTIMPL;
1701#endif
1702}
1703
1704// private methods
1705/////////////////////////////////////////////////////////////////////////////
1706
1707/**
1708 * Helper to update the display information from the framebuffer.
1709 *
1710 * @param aCheckParams true to compare the parameters of the current framebuffer
1711 * and the new one and issue handleDisplayResize()
1712 * if they differ.
1713 * @thread EMT
1714 */
1715void Display::updateDisplayData (bool aCheckParams /* = false */)
1716{
1717 /* the driver might not have been constructed yet */
1718 if (!mpDrv)
1719 return;
1720
1721#if DEBUG
1722 /*
1723 * Sanity check. Note that this method may be called on EMT after Console
1724 * has started the power down procedure (but before our #drvDestruct() is
1725 * called, in which case pVM will aleady be NULL but mpDrv will not). Since
1726 * we don't really need pVM to proceed, we avoid this check in the release
1727 * build to save some ms (necessary to construct SafeVMPtrQuiet) in this
1728 * time-critical method.
1729 */
1730 Console::SafeVMPtrQuiet pVM (mParent);
1731 if (pVM.isOk())
1732 VM_ASSERT_EMT (pVM.raw());
1733#endif
1734
1735 /* The method is only relevant to the primary framebuffer. */
1736 IFramebuffer *pFramebuffer = maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN].pFramebuffer;
1737
1738 if (pFramebuffer)
1739 {
1740 HRESULT rc;
1741 BYTE *address = 0;
1742 rc = pFramebuffer->COMGETTER(Address) (&address);
1743 AssertComRC (rc);
1744 ULONG bytesPerLine = 0;
1745 rc = pFramebuffer->COMGETTER(BytesPerLine) (&bytesPerLine);
1746 AssertComRC (rc);
1747 ULONG bitsPerPixel = 0;
1748 rc = pFramebuffer->COMGETTER(BitsPerPixel) (&bitsPerPixel);
1749 AssertComRC (rc);
1750 ULONG width = 0;
1751 rc = pFramebuffer->COMGETTER(Width) (&width);
1752 AssertComRC (rc);
1753 ULONG height = 0;
1754 rc = pFramebuffer->COMGETTER(Height) (&height);
1755 AssertComRC (rc);
1756
1757 /*
1758 * Check current parameters with new ones and issue handleDisplayResize()
1759 * to let the new frame buffer adjust itself properly. Note that it will
1760 * result into a recursive updateDisplayData() call but with
1761 * aCheckOld = false.
1762 */
1763 if (aCheckParams &&
1764 (mLastAddress != address ||
1765 mLastBytesPerLine != bytesPerLine ||
1766 mLastBitsPerPixel != bitsPerPixel ||
1767 mLastWidth != (int) width ||
1768 mLastHeight != (int) height))
1769 {
1770 handleDisplayResize (VBOX_VIDEO_PRIMARY_SCREEN, mLastBitsPerPixel,
1771 mLastAddress,
1772 mLastBytesPerLine,
1773 mLastWidth,
1774 mLastHeight);
1775 return;
1776 }
1777
1778 mpDrv->Connector.pu8Data = (uint8_t *) address;
1779 mpDrv->Connector.cbScanline = bytesPerLine;
1780 mpDrv->Connector.cBits = bitsPerPixel;
1781 mpDrv->Connector.cx = width;
1782 mpDrv->Connector.cy = height;
1783 }
1784 else
1785 {
1786 /* black hole */
1787 mpDrv->Connector.pu8Data = NULL;
1788 mpDrv->Connector.cbScanline = 0;
1789 mpDrv->Connector.cBits = 0;
1790 mpDrv->Connector.cx = 0;
1791 mpDrv->Connector.cy = 0;
1792 }
1793}
1794
1795/**
1796 * Changes the current frame buffer. Called on EMT to avoid both
1797 * race conditions and excessive locking.
1798 *
1799 * @note locks this object for writing
1800 * @thread EMT
1801 */
1802/* static */
1803DECLCALLBACK(int) Display::changeFramebuffer (Display *that, IFramebuffer *aFB,
1804 unsigned uScreenId)
1805{
1806 LogFlowFunc (("uScreenId = %d\n", uScreenId));
1807
1808 AssertReturn (that, VERR_INVALID_PARAMETER);
1809 AssertReturn (uScreenId < that->mcMonitors, VERR_INVALID_PARAMETER);
1810
1811 AutoCaller autoCaller (that);
1812 CheckComRCReturnRC (autoCaller.rc());
1813
1814 AutoWriteLock alock (that);
1815
1816 DISPLAYFBINFO *pDisplayFBInfo = &that->maFramebuffers[uScreenId];
1817 pDisplayFBInfo->pFramebuffer = aFB;
1818
1819 that->mParent->consoleVRDPServer()->SendResize ();
1820
1821 that->updateDisplayData (true /* aCheckParams */);
1822
1823 return VINF_SUCCESS;
1824}
1825
1826/**
1827 * Handle display resize event issued by the VGA device for the primary screen.
1828 *
1829 * @see PDMIDISPLAYCONNECTOR::pfnResize
1830 */
1831DECLCALLBACK(int) Display::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface,
1832 uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
1833{
1834 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1835
1836 LogFlowFunc (("bpp %d, pvVRAM %p, cbLine %d, cx %d, cy %d\n",
1837 bpp, pvVRAM, cbLine, cx, cy));
1838
1839 return pDrv->pDisplay->handleDisplayResize(VBOX_VIDEO_PRIMARY_SCREEN, bpp, pvVRAM, cbLine, cx, cy);
1840}
1841
1842/**
1843 * Handle display update.
1844 *
1845 * @see PDMIDISPLAYCONNECTOR::pfnUpdateRect
1846 */
1847DECLCALLBACK(void) Display::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
1848 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
1849{
1850 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1851
1852#ifdef DEBUG_sunlover
1853 LogFlowFunc (("mfVideoAccelEnabled = %d, %d,%d %dx%d\n",
1854 pDrv->pDisplay->mfVideoAccelEnabled, x, y, cx, cy));
1855#endif /* DEBUG_sunlover */
1856
1857 /* This call does update regardless of VBVA status.
1858 * But in VBVA mode this is called only as result of
1859 * pfnUpdateDisplayAll in the VGA device.
1860 */
1861
1862 pDrv->pDisplay->handleDisplayUpdate(x, y, cx, cy);
1863}
1864
1865/**
1866 * Periodic display refresh callback.
1867 *
1868 * @see PDMIDISPLAYCONNECTOR::pfnRefresh
1869 */
1870DECLCALLBACK(void) Display::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
1871{
1872 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1873
1874#ifdef DEBUG_sunlover
1875 STAM_PROFILE_START(&StatDisplayRefresh, a);
1876#endif /* DEBUG_sunlover */
1877
1878#ifdef DEBUG_sunlover_2
1879 LogFlowFunc (("pDrv->pDisplay->mfVideoAccelEnabled = %d\n",
1880 pDrv->pDisplay->mfVideoAccelEnabled));
1881#endif /* DEBUG_sunlover_2 */
1882
1883 Display *pDisplay = pDrv->pDisplay;
1884
1885 unsigned uScreenId;
1886 for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
1887 {
1888 DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[uScreenId];
1889
1890 /* Check the resize status. The status can be checked normally because
1891 * the status affects only the EMT.
1892 */
1893 uint32_t u32ResizeStatus = pFBInfo->u32ResizeStatus;
1894
1895 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
1896 {
1897 LogFlowFunc (("ResizeStatus_UpdateDisplayData %d\n", uScreenId));
1898 /* The framebuffer was resized and display data need to be updated. */
1899 pDisplay->handleResizeCompletedEMT ();
1900 if (pFBInfo->u32ResizeStatus != ResizeStatus_Void)
1901 {
1902 /* The resize status could be not Void here because a pending resize is issued. */
1903 continue;
1904 }
1905 /* Continue with normal processing because the status here is ResizeStatus_Void. */
1906 if (uScreenId == VBOX_VIDEO_PRIMARY_SCREEN)
1907 {
1908 /* Repaint the display because VM continued to run during the framebuffer resize. */
1909 if (!pFBInfo->pFramebuffer.isNull())
1910 pDrv->pUpPort->pfnUpdateDisplayAll(pDrv->pUpPort);
1911 }
1912 /* Ignore the refresh for the screen to replay the logic. */
1913 continue;
1914 }
1915 else if (u32ResizeStatus == ResizeStatus_InProgress)
1916 {
1917 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
1918 LogFlowFunc (("ResizeStatus_InProcess\n"));
1919 continue;
1920 }
1921
1922 if (pFBInfo->pFramebuffer.isNull())
1923 {
1924 /*
1925 * Do nothing in the "black hole" mode to avoid copying guest
1926 * video memory to the frame buffer
1927 */
1928 }
1929 else
1930 {
1931 if (pDisplay->mfPendingVideoAccelEnable)
1932 {
1933 /* Acceleration was enabled while machine was not yet running
1934 * due to restoring from saved state. Update entire display and
1935 * actually enable acceleration.
1936 */
1937 Assert(pDisplay->mpPendingVbvaMemory);
1938
1939 /* Acceleration can not be yet enabled.*/
1940 Assert(pDisplay->mpVbvaMemory == NULL);
1941 Assert(!pDisplay->mfVideoAccelEnabled);
1942
1943 if (pDisplay->mfMachineRunning)
1944 {
1945 pDisplay->VideoAccelEnable (pDisplay->mfPendingVideoAccelEnable,
1946 pDisplay->mpPendingVbvaMemory);
1947
1948 /* Reset the pending state. */
1949 pDisplay->mfPendingVideoAccelEnable = false;
1950 pDisplay->mpPendingVbvaMemory = NULL;
1951 }
1952 }
1953 else
1954 {
1955 Assert(pDisplay->mpPendingVbvaMemory == NULL);
1956
1957 if (pDisplay->mfVideoAccelEnabled)
1958 {
1959 Assert(pDisplay->mpVbvaMemory);
1960 pDisplay->VideoAccelFlush ();
1961 }
1962 else
1963 {
1964 Assert(pDrv->Connector.pu8Data);
1965 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
1966 }
1967 }
1968 /* Inform the VRDP server that the current display update sequence is
1969 * completed. At this moment the framebuffer memory contains a definite
1970 * image, that is synchronized with the orders already sent to VRDP client.
1971 * The server can now process redraw requests from clients or initial
1972 * fullscreen updates for new clients.
1973 */
1974 if (pFBInfo->u32ResizeStatus == ResizeStatus_Void)
1975 {
1976 Assert (pDisplay->mParent && pDisplay->mParent->consoleVRDPServer());
1977 pDisplay->mParent->consoleVRDPServer()->SendUpdate (uScreenId, NULL, 0);
1978 }
1979 }
1980 }
1981
1982#ifdef DEBUG_sunlover
1983 STAM_PROFILE_STOP(&StatDisplayRefresh, a);
1984#endif /* DEBUG_sunlover */
1985#ifdef DEBUG_sunlover_2
1986 LogFlowFunc (("leave\n"));
1987#endif /* DEBUG_sunlover_2 */
1988}
1989
1990/**
1991 * Reset notification
1992 *
1993 * @see PDMIDISPLAYCONNECTOR::pfnReset
1994 */
1995DECLCALLBACK(void) Display::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
1996{
1997 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
1998
1999 LogFlowFunc (("\n"));
2000
2001 /* Disable VBVA mode. */
2002 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2003}
2004
2005/**
2006 * LFBModeChange notification
2007 *
2008 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
2009 */
2010DECLCALLBACK(void) Display::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
2011{
2012 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2013
2014 LogFlowFunc (("fEnabled=%d\n", fEnabled));
2015
2016 NOREF(fEnabled);
2017
2018 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
2019 pDrv->pDisplay->VideoAccelEnable (false, NULL);
2020}
2021
2022/**
2023 * Adapter information change notification.
2024 *
2025 * @see PDMIDISPLAYCONNECTOR::pfnProcessAdapterData
2026 */
2027DECLCALLBACK(void) Display::displayProcessAdapterDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, uint32_t u32VRAMSize)
2028{
2029 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2030
2031 if (pvVRAM == NULL)
2032 {
2033 unsigned i;
2034 for (i = 0; i < pDrv->pDisplay->mcMonitors; i++)
2035 {
2036 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[i];
2037
2038 pFBInfo->u32Offset = 0;
2039 pFBInfo->u32MaxFramebufferSize = 0;
2040 pFBInfo->u32InformationSize = 0;
2041 }
2042 }
2043#ifndef VBOX_WITH_HGSMI
2044 else
2045 {
2046 uint8_t *pu8 = (uint8_t *)pvVRAM;
2047 pu8 += u32VRAMSize - VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2048
2049 // @todo
2050 uint8_t *pu8End = pu8 + VBOX_VIDEO_ADAPTER_INFORMATION_SIZE;
2051
2052 VBOXVIDEOINFOHDR *pHdr;
2053
2054 for (;;)
2055 {
2056 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2057 pu8 += sizeof (VBOXVIDEOINFOHDR);
2058
2059 if (pu8 >= pu8End)
2060 {
2061 LogRel(("VBoxVideo: Guest adapter information overflow!!!\n"));
2062 break;
2063 }
2064
2065 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_DISPLAY)
2066 {
2067 if (pHdr->u16Length != sizeof (VBOXVIDEOINFODISPLAY))
2068 {
2069 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "DISPLAY", pHdr->u16Length));
2070 break;
2071 }
2072
2073 VBOXVIDEOINFODISPLAY *pDisplay = (VBOXVIDEOINFODISPLAY *)pu8;
2074
2075 if (pDisplay->u32Index >= pDrv->pDisplay->mcMonitors)
2076 {
2077 LogRel(("VBoxVideo: Guest adapter information invalid display index %d!!!\n", pDisplay->u32Index));
2078 break;
2079 }
2080
2081 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[pDisplay->u32Index];
2082
2083 pFBInfo->u32Offset = pDisplay->u32Offset;
2084 pFBInfo->u32MaxFramebufferSize = pDisplay->u32FramebufferSize;
2085 pFBInfo->u32InformationSize = pDisplay->u32InformationSize;
2086
2087 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));
2088 }
2089 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_QUERY_CONF32)
2090 {
2091 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOQUERYCONF32))
2092 {
2093 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "CONF32", pHdr->u16Length));
2094 break;
2095 }
2096
2097 VBOXVIDEOINFOQUERYCONF32 *pConf32 = (VBOXVIDEOINFOQUERYCONF32 *)pu8;
2098
2099 switch (pConf32->u32Index)
2100 {
2101 case VBOX_VIDEO_QCI32_MONITOR_COUNT:
2102 {
2103 pConf32->u32Value = pDrv->pDisplay->mcMonitors;
2104 } break;
2105
2106 case VBOX_VIDEO_QCI32_OFFSCREEN_HEAP_SIZE:
2107 {
2108 /* @todo make configurable. */
2109 pConf32->u32Value = _1M;
2110 } break;
2111
2112 default:
2113 LogRel(("VBoxVideo: CONF32 %d not supported!!! Skipping.\n", pConf32->u32Index));
2114 }
2115 }
2116 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2117 {
2118 if (pHdr->u16Length != 0)
2119 {
2120 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2121 break;
2122 }
2123
2124 break;
2125 }
2126 else if (pHdr->u8Type != VBOX_VIDEO_INFO_TYPE_NV_HEAP) /** @todo why is Additions/WINNT/Graphics/Miniport/VBoxVideo.cpp pushing this to us? */
2127 {
2128 LogRel(("Guest adapter information contains unsupported type %d. The block has been skipped.\n", pHdr->u8Type));
2129 }
2130
2131 pu8 += pHdr->u16Length;
2132 }
2133 }
2134#endif /* !VBOX_WITH_HGSMI */
2135}
2136
2137/**
2138 * Display information change notification.
2139 *
2140 * @see PDMIDISPLAYCONNECTOR::pfnProcessDisplayData
2141 */
2142DECLCALLBACK(void) Display::displayProcessDisplayDataCallback(PPDMIDISPLAYCONNECTOR pInterface, void *pvVRAM, unsigned uScreenId)
2143{
2144 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2145
2146 if (uScreenId >= pDrv->pDisplay->mcMonitors)
2147 {
2148 LogRel(("VBoxVideo: Guest display information invalid display index %d!!!\n", uScreenId));
2149 return;
2150 }
2151
2152 /* Get the display information structure. */
2153 DISPLAYFBINFO *pFBInfo = &pDrv->pDisplay->maFramebuffers[uScreenId];
2154
2155 uint8_t *pu8 = (uint8_t *)pvVRAM;
2156 pu8 += pFBInfo->u32Offset + pFBInfo->u32MaxFramebufferSize;
2157
2158 // @todo
2159 uint8_t *pu8End = pu8 + pFBInfo->u32InformationSize;
2160
2161 VBOXVIDEOINFOHDR *pHdr;
2162
2163 for (;;)
2164 {
2165 pHdr = (VBOXVIDEOINFOHDR *)pu8;
2166 pu8 += sizeof (VBOXVIDEOINFOHDR);
2167
2168 if (pu8 >= pu8End)
2169 {
2170 LogRel(("VBoxVideo: Guest display information overflow!!!\n"));
2171 break;
2172 }
2173
2174 if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_SCREEN)
2175 {
2176 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOSCREEN))
2177 {
2178 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "SCREEN", pHdr->u16Length));
2179 break;
2180 }
2181
2182 VBOXVIDEOINFOSCREEN *pScreen = (VBOXVIDEOINFOSCREEN *)pu8;
2183
2184 pFBInfo->xOrigin = pScreen->xOrigin;
2185 pFBInfo->yOrigin = pScreen->yOrigin;
2186
2187 pFBInfo->w = pScreen->u16Width;
2188 pFBInfo->h = pScreen->u16Height;
2189
2190 LogFlow(("VBOX_VIDEO_INFO_TYPE_SCREEN: (%p) %d: at %d,%d, linesize 0x%X, size %dx%d, bpp %d, flags 0x%02X\n",
2191 pHdr, uScreenId, pScreen->xOrigin, pScreen->yOrigin, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height, pScreen->bitsPerPixel, pScreen->u8Flags));
2192
2193 if (uScreenId != VBOX_VIDEO_PRIMARY_SCREEN)
2194 {
2195 /* Primary screen resize is initiated by the VGA device. */
2196 pDrv->pDisplay->handleDisplayResize(uScreenId, pScreen->bitsPerPixel, (uint8_t *)pvVRAM + pFBInfo->u32Offset, pScreen->u32LineSize, pScreen->u16Width, pScreen->u16Height);
2197 }
2198 }
2199 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_END)
2200 {
2201 if (pHdr->u16Length != 0)
2202 {
2203 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "END", pHdr->u16Length));
2204 break;
2205 }
2206
2207 break;
2208 }
2209 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_HOST_EVENTS)
2210 {
2211 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOHOSTEVENTS))
2212 {
2213 LogRel(("VBoxVideo: Guest display information %s invalid length %d!!!\n", "HOST_EVENTS", pHdr->u16Length));
2214 break;
2215 }
2216
2217 VBOXVIDEOINFOHOSTEVENTS *pHostEvents = (VBOXVIDEOINFOHOSTEVENTS *)pu8;
2218
2219 pFBInfo->pHostEvents = pHostEvents;
2220
2221 LogFlow(("VBOX_VIDEO_INFO_TYPE_HOSTEVENTS: (%p)\n",
2222 pHostEvents));
2223 }
2224 else if (pHdr->u8Type == VBOX_VIDEO_INFO_TYPE_LINK)
2225 {
2226 if (pHdr->u16Length != sizeof (VBOXVIDEOINFOLINK))
2227 {
2228 LogRel(("VBoxVideo: Guest adapter information %s invalid length %d!!!\n", "LINK", pHdr->u16Length));
2229 break;
2230 }
2231
2232 VBOXVIDEOINFOLINK *pLink = (VBOXVIDEOINFOLINK *)pu8;
2233 pu8 += pLink->i32Offset;
2234 }
2235 else
2236 {
2237 LogRel(("Guest display information contains unsupported type %d\n", pHdr->u8Type));
2238 }
2239
2240 pu8 += pHdr->u16Length;
2241 }
2242}
2243
2244#ifdef VBOX_WITH_VIDEOHWACCEL
2245
2246void Display::handleVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
2247{
2248 unsigned id = (unsigned)pCommand->iDisplay;
2249 int rc = VINF_SUCCESS;
2250 if(id < mcMonitors)
2251 {
2252 IFramebuffer *pFramebuffer = maFramebuffers[id].pFramebuffer;
2253
2254 // if there is no framebuffer, this call is not interesting
2255 if (pFramebuffer == NULL)
2256 return;
2257
2258 pFramebuffer->Lock();
2259
2260 HRESULT hr = pFramebuffer->ProcessVHWACommand((BYTE*)pCommand);
2261 if(FAILED(hr))
2262 {
2263 rc = VERR_GENERAL_FAILURE;
2264 }
2265
2266 pFramebuffer->Unlock();
2267
2268 }
2269 else
2270 {
2271 rc = VERR_INVALID_PARAMETER;
2272 }
2273
2274 if(RT_FAILURE(rc))
2275 {
2276 /* tell the guest the command is complete */
2277 pCommand->rc = rc;
2278 }
2279}
2280
2281DECLCALLBACK(void) Display::displayVHWACommandProcess(PPDMIDISPLAYCONNECTOR pInterface, PVBOXVHWACMD pCommand)
2282{
2283 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
2284
2285 pDrv->pDisplay->handleVHWACommandProcess(pInterface, pCommand);
2286}
2287#endif
2288
2289/**
2290 * Queries an interface to the driver.
2291 *
2292 * @returns Pointer to interface.
2293 * @returns NULL if the interface was not supported by the driver.
2294 * @param pInterface Pointer to this interface structure.
2295 * @param enmInterface The requested interface identification.
2296 */
2297DECLCALLBACK(void *) Display::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
2298{
2299 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2300 PDRVMAINDISPLAY pDrv = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
2301 switch (enmInterface)
2302 {
2303 case PDMINTERFACE_BASE:
2304 return &pDrvIns->IBase;
2305 case PDMINTERFACE_DISPLAY_CONNECTOR:
2306 return &pDrv->Connector;
2307 default:
2308 return NULL;
2309 }
2310}
2311
2312
2313/**
2314 * Destruct a display driver instance.
2315 *
2316 * @returns VBox status.
2317 * @param pDrvIns The driver instance data.
2318 */
2319DECLCALLBACK(void) Display::drvDestruct(PPDMDRVINS pDrvIns)
2320{
2321 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
2322 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2323 if (pData->pDisplay)
2324 {
2325 AutoWriteLock displayLock (pData->pDisplay);
2326 pData->pDisplay->mpDrv = NULL;
2327 pData->pDisplay->mpVMMDev = NULL;
2328 pData->pDisplay->mLastAddress = NULL;
2329 pData->pDisplay->mLastBytesPerLine = 0;
2330 pData->pDisplay->mLastBitsPerPixel = 0,
2331 pData->pDisplay->mLastWidth = 0;
2332 pData->pDisplay->mLastHeight = 0;
2333 }
2334}
2335
2336
2337/**
2338 * Construct a display driver instance.
2339 *
2340 * @returns VBox status.
2341 * @param pDrvIns The driver instance data.
2342 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
2343 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
2344 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
2345 * iInstance it's expected to be used a bit in this function.
2346 */
2347DECLCALLBACK(int) Display::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
2348{
2349 PDRVMAINDISPLAY pData = PDMINS_2_DATA(pDrvIns, PDRVMAINDISPLAY);
2350 LogFlowFunc (("iInstance=%d\n", pDrvIns->iInstance));
2351
2352 /*
2353 * Validate configuration.
2354 */
2355 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
2356 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
2357 PPDMIBASE pBaseIgnore;
2358 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
2359 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
2360 {
2361 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
2362 return VERR_PDM_DRVINS_NO_ATTACH;
2363 }
2364
2365 /*
2366 * Init Interfaces.
2367 */
2368 pDrvIns->IBase.pfnQueryInterface = Display::drvQueryInterface;
2369
2370 pData->Connector.pfnResize = Display::displayResizeCallback;
2371 pData->Connector.pfnUpdateRect = Display::displayUpdateCallback;
2372 pData->Connector.pfnRefresh = Display::displayRefreshCallback;
2373 pData->Connector.pfnReset = Display::displayResetCallback;
2374 pData->Connector.pfnLFBModeChange = Display::displayLFBModeChangeCallback;
2375 pData->Connector.pfnProcessAdapterData = Display::displayProcessAdapterDataCallback;
2376 pData->Connector.pfnProcessDisplayData = Display::displayProcessDisplayDataCallback;
2377#ifdef VBOX_WITH_VIDEOHWACCEL
2378 pData->Connector.pfnVHWACommandProcess = Display::displayVHWACommandProcess;
2379#endif
2380
2381 /*
2382 * Get the IDisplayPort interface of the above driver/device.
2383 */
2384 pData->pUpPort = (PPDMIDISPLAYPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_PORT);
2385 if (!pData->pUpPort)
2386 {
2387 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
2388 return VERR_PDM_MISSING_INTERFACE_ABOVE;
2389 }
2390#if defined(VBOX_WITH_VIDEOHWACCEL)
2391 pData->pVBVACallbacks = (PPDMDDISPLAYVBVACALLBACKS)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_VBVA_CALLBACKS);
2392 if (!pData->pVBVACallbacks)
2393 {
2394 AssertMsgFailed(("Configuration error: No VBVA callback interface above!\n"));
2395 return VERR_PDM_MISSING_INTERFACE_ABOVE;
2396 }
2397#endif
2398 /*
2399 * Get the Display object pointer and update the mpDrv member.
2400 */
2401 void *pv;
2402 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
2403 if (RT_FAILURE(rc))
2404 {
2405 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Rrc\n", rc));
2406 return rc;
2407 }
2408 pData->pDisplay = (Display *)pv; /** @todo Check this cast! */
2409 pData->pDisplay->mpDrv = pData;
2410
2411 /*
2412 * Update our display information according to the framebuffer
2413 */
2414 pData->pDisplay->updateDisplayData();
2415
2416 /*
2417 * Start periodic screen refreshes
2418 */
2419 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 20);
2420
2421 return VINF_SUCCESS;
2422}
2423
2424
2425/**
2426 * Display driver registration record.
2427 */
2428const PDMDRVREG Display::DrvReg =
2429{
2430 /* u32Version */
2431 PDM_DRVREG_VERSION,
2432 /* szDriverName */
2433 "MainDisplay",
2434 /* pszDescription */
2435 "Main display driver (Main as in the API).",
2436 /* fFlags */
2437 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2438 /* fClass. */
2439 PDM_DRVREG_CLASS_DISPLAY,
2440 /* cMaxInstances */
2441 ~0,
2442 /* cbInstance */
2443 sizeof(DRVMAINDISPLAY),
2444 /* pfnConstruct */
2445 Display::drvConstruct,
2446 /* pfnDestruct */
2447 Display::drvDestruct,
2448 /* pfnIOCtl */
2449 NULL,
2450 /* pfnPowerOn */
2451 NULL,
2452 /* pfnReset */
2453 NULL,
2454 /* pfnSuspend */
2455 NULL,
2456 /* pfnResume */
2457 NULL,
2458 /* pfnDetach */
2459 NULL
2460};
2461/* 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