VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxBFE/DisplayImpl.cpp@ 2791

Last change on this file since 2791 was 2791, checked in by vboxsync, 17 years ago

L4: Set an initial video mode hint to resize the guest to the resolution of the L4 console

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.5 KB
Line 
1/** @file
2 *
3 * VBox frontends: Basic Frontend (BFE):
4 * Implementation of VMDisplay class
5 */
6
7/*
8 * Copyright (C) 2006 InnoTek Systemberatung GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * If you received this file as part of a commercial VirtualBox
19 * distribution, then only the terms of your commercial VirtualBox
20 * license agreement apply instead of the previous paragraph.
21 */
22
23#define LOG_GROUP LOG_GROUP_MAIN
24
25#ifdef VBOXBFE_WITHOUT_COM
26# include "COMDefs.h"
27# include <iprt/string.h>
28#else
29# include <VBox/com/defs.h>
30#endif
31
32#include <iprt/alloc.h>
33#include <iprt/semaphore.h>
34#include <iprt/thread.h>
35#include <VBox/pdm.h>
36#include <VBox/VBoxGuest.h>
37#include <VBox/cfgm.h>
38#include <VBox/err.h>
39#include <iprt/assert.h>
40#include <VBox/log.h>
41#include <iprt/asm.h>
42
43#ifdef __L4__
44#include <stdio.h>
45#include <l4/util/util.h>
46#include <l4/log/l4log.h>
47#endif
48
49#include "DisplayImpl.h"
50#include "Framebuffer.h"
51#include "VMMDevInterface.h"
52
53
54/*******************************************************************************
55* Structures and Typedefs *
56*******************************************************************************/
57
58/**
59 * VMDisplay driver instance data.
60 */
61typedef struct DRVMAINDISPLAY
62{
63 /** Pointer to the display object. */
64 VMDisplay *pDisplay;
65 /** Pointer to the driver instance structure. */
66 PPDMDRVINS pDrvIns;
67 /** Pointer to the keyboard port interface of the driver/device above us. */
68 PPDMIDISPLAYPORT pUpPort;
69 /** Our display connector interface. */
70 PDMIDISPLAYCONNECTOR Connector;
71} DRVMAINDISPLAY, *PDRVMAINDISPLAY;
72
73/** Converts PDMIDISPLAYCONNECTOR pointer to a DRVMAINDISPLAY pointer. */
74#define PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface) ( (PDRVMAINDISPLAY) ((uintptr_t)pInterface - RT_OFFSETOF(DRVMAINDISPLAY, Connector)) )
75
76
77// constructor / destructor
78/////////////////////////////////////////////////////////////////////////////
79
80VMDisplay::VMDisplay()
81{
82 mpDrv = NULL;
83
84 mpVbvaMemory = NULL;
85 mfVideoAccelEnabled = false;
86
87 mpPendingVbvaMemory = NULL;
88 mfPendingVideoAccelEnable = false;
89
90 mfMachineRunning = false;
91
92 mpu8VbvaPartial = NULL;
93 mcbVbvaPartial = 0;
94
95 RTSemEventMultiCreate(&mUpdateSem);
96
97 // reset the event sems
98 RTSemEventMultiReset(mUpdateSem);
99
100 // by default, we have an internal Framebuffer which is
101 // NULL, i.e. a black hole for no display output
102 mFramebuffer = 0;
103 mInternalFramebuffer = true;
104 mFramebufferOpened = false;
105
106 mu32ResizeStatus = ResizeStatus_Void;
107}
108
109VMDisplay::~VMDisplay()
110{
111 mFramebuffer = 0;
112 RTSemEventMultiDestroy(mUpdateSem);
113}
114
115// public methods only for internal purposes
116/////////////////////////////////////////////////////////////////////////////
117
118/**
119 * Handle display resize event.
120 *
121 * @returns COM status code
122 * @param w New display width
123 * @param h New display height
124 */
125int VMDisplay::handleDisplayResize (int w, int h)
126{
127 LogFlow(("VMDisplay::handleDisplayResize(): w=%d, h=%d\n", w, h));
128
129 // if there is no Framebuffer, this call is not interesting
130 if (mFramebuffer == NULL)
131 return VINF_SUCCESS;
132
133 /* Atomically set the resize status before calling the framebuffer. The new InProgress status will
134 * disable access to the VGA device by the EMT thread.
135 */
136 bool f = ASMAtomicCmpXchgU32 (&mu32ResizeStatus, ResizeStatus_InProgress, ResizeStatus_Void);
137 AssertRelease(f);NOREF(f);
138
139 // callback into the Framebuffer to notify it
140 BOOL finished;
141
142 mFramebuffer->Lock();
143
144 mFramebuffer->RequestResize(w, h, &finished);
145
146 if (!finished)
147 {
148 LogFlow(("VMDisplay::handleDisplayResize: external framebuffer wants us to wait!\n"));
149
150 /* Note: The previously obtained framebuffer lock must be preserved.
151 * The EMT keeps the framebuffer lock until the resize process completes.
152 */
153
154 return VINF_VGA_RESIZE_IN_PROGRESS;
155 }
156
157 /* Set the status so the 'handleResizeCompleted' would work. */
158 f = ASMAtomicCmpXchgU32 (&mu32ResizeStatus, ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
159 AssertRelease(f);NOREF(f);
160
161 /* The method also unlocks the framebuffer. */
162 handleResizeCompletedEMT();
163
164 return VINF_SUCCESS;
165}
166
167/**
168 * Framebuffer has been resized.
169 * Read the new display data and unlock the framebuffer.
170 *
171 * @thread EMT
172 */
173void VMDisplay::handleResizeCompletedEMT (void)
174{
175 LogFlowFunc(("\n"));
176 if (mFramebuffer)
177 {
178 /* Framebuffer has completed the resize. Update the connector data. */
179 updateDisplayData();
180
181 mpDrv->pUpPort->pfnSetRenderVRAM (mpDrv->pUpPort, true);
182
183 /* Unlock framebuffer. */
184 mFramebuffer->Unlock();
185 }
186
187 /* Go into non resizing state. */
188 bool f = ASMAtomicCmpXchgU32 (&mu32ResizeStatus, ResizeStatus_Void, ResizeStatus_UpdateDisplayData);
189 AssertRelease(f);NOREF(f);
190}
191
192/**
193 * Notification that the framebuffer has completed the
194 * asynchronous resize processing
195 *
196 * @returns COM status code
197 */
198STDMETHODIMP VMDisplay::ResizeCompleted()
199{
200 LogFlow(("VMDisplay::ResizeCompleted\n"));
201
202 // this is only valid for external framebuffers
203 if (mInternalFramebuffer)
204 return E_FAIL;
205
206 /* Set the flag indicating that the resize has completed and display data need to be updated. */
207 bool f = ASMAtomicCmpXchgU32 (&mu32ResizeStatus, ResizeStatus_UpdateDisplayData, ResizeStatus_InProgress);
208 AssertRelease(f);NOREF(f);
209
210 return S_OK;
211}
212
213static void checkCoordBounds (int *px, int *py, int *pw, int *ph, int cx, int cy)
214{
215 /* Correct negative x and y coordinates. */
216 if (*px < 0)
217 {
218 *px += *pw; /* Compute xRight which is also the new width. */
219 *pw = (*px < 0) ? 0: *px;
220 *px = 0;
221 }
222
223 if (*py < 0)
224 {
225 *py += *ph; /* Compute xBottom, which is also the new height. */
226 *ph = (*py < 0) ? 0: *py;
227 *py = 0;
228 }
229
230 /* Also check if coords are greater than the display resolution. */
231 if (*px + *pw > cx)
232 *pw = cx > *px ? cx - *px: 0;
233
234 if (*py + *ph > cy)
235 *ph = cy > *py ? cy - *py: 0;
236}
237
238/**
239 * Handle display update
240 *
241 * @returns COM status code
242 * @param w New display width
243 * @param h New display height
244 */
245void VMDisplay::handleDisplayUpdate (int x, int y, int w, int h)
246{
247 // if there is no Framebuffer, this call is not interesting
248 if (mFramebuffer == NULL)
249 return;
250
251 mFramebuffer->Lock();
252
253 checkCoordBounds (&x, &y, &w, &h, mpDrv->Connector.cx, mpDrv->Connector.cy);
254
255 // special processing for the internal Framebuffer
256 if (mInternalFramebuffer)
257 {
258 mFramebuffer->Unlock();
259 }
260 else
261 {
262 // callback into the Framebuffer to notify it
263 BOOL finished;
264
265 RTSemEventMultiReset(mUpdateSem);
266
267 mFramebuffer->NotifyUpdate(x, y, w, h, &finished);
268 mFramebuffer->Unlock();
269
270 if (!finished)
271 {
272 // the Framebuffer needs more time to process
273 // the event so we have to halt the VM until it's done
274 RTSemEventMultiWait(mUpdateSem, RT_INDEFINITE_WAIT);
275 }
276 }
277}
278
279// IDisplay properties
280/////////////////////////////////////////////////////////////////////////////
281
282/**
283 * Returns the current display width in pixel
284 *
285 * @returns COM status code
286 * @param width Address of result variable.
287 */
288uint32_t VMDisplay::getWidth()
289{
290 Assert(mpDrv);
291 return mpDrv->Connector.cx;
292}
293
294/**
295 * Returns the current display height in pixel
296 *
297 * @returns COM status code
298 * @param height Address of result variable.
299 */
300uint32_t VMDisplay::getHeight()
301{
302 Assert(mpDrv);
303 return mpDrv->Connector.cy;
304}
305
306/**
307 * Returns the current display color depth in bits
308 *
309 * @returns COM status code
310 * @param colorDepth Address of result variable.
311 */
312uint32_t VMDisplay::getColorDepth()
313{
314 Assert(mpDrv);
315 return mpDrv->Connector.cBits;
316}
317
318void VMDisplay::updatePointerShape(bool fVisible, bool fAlpha, uint32_t xHot, uint32_t yHot, uint32_t width, uint32_t height, void *pShape)
319{
320}
321
322
323// IDisplay methods
324/////////////////////////////////////////////////////////////////////////////
325
326/**
327 * Registers an external Framebuffer
328 *
329 * @returns COM status code
330 * @param Framebuffer external Framebuffer object
331 */
332STDMETHODIMP VMDisplay::RegisterExternalFramebuffer(Framebuffer *Framebuffer)
333{
334 if (!Framebuffer)
335 return E_POINTER;
336
337 // free current Framebuffer (if there is any)
338 mFramebuffer = 0;
339 mInternalFramebuffer = false;
340 mFramebuffer = Framebuffer;
341 updateDisplayData();
342 return S_OK;
343}
344
345/* InvalidateAndUpdate schedules a request that eventually calls */
346/* mpDrv->pUpPort->pfnUpdateDisplayAll which in turns accesses the */
347/* framebuffer. In order to synchronize with other framebuffer */
348/* related activities this call needs to be framed by Lock/Unlock. */
349void
350VMDisplay::doInvalidateAndUpdate(struct DRVMAINDISPLAY *mpDrv)
351{
352 mpDrv->pDisplay->mFramebuffer->Lock();
353 mpDrv->pUpPort->pfnUpdateDisplayAll( mpDrv->pUpPort);
354 mpDrv->pDisplay->mFramebuffer->Unlock();
355}
356
357/**
358 * Does a full invalidation of the VM display and instructs the VM
359 * to update it immediately.
360 *
361 * @returns COM status code
362 */
363STDMETHODIMP VMDisplay::InvalidateAndUpdate()
364{
365 LogFlow (("VMDisplay::InvalidateAndUpdate(): BEGIN\n"));
366
367 HRESULT rc = S_OK;
368
369 LogFlow (("VMDisplay::InvalidateAndUpdate(): sending DPYUPDATE request\n"));
370
371 Assert(pVM);
372 /* pdm.h says that this has to be called from the EMT thread */
373 PVMREQ pReq;
374 int rcVBox = VMR3ReqCallVoid(pVM, &pReq, RT_INDEFINITE_WAIT,
375 (PFNRT)VMDisplay::doInvalidateAndUpdate, 1, mpDrv);
376 if (VBOX_SUCCESS(rcVBox))
377 VMR3ReqFree(pReq);
378
379 if (VBOX_FAILURE(rcVBox))
380 rc = E_FAIL;
381
382 LogFlow (("VMDisplay::InvalidateAndUpdate(): END: rc=%08X\n", rc));
383 return rc;
384}
385
386// private methods
387/////////////////////////////////////////////////////////////////////////////
388
389/**
390 * Helper to update the display information from the Framebuffer
391 *
392 */
393void VMDisplay::updateDisplayData()
394{
395
396 while(!mFramebuffer)
397 {
398#if __L4__
399 asm volatile ("nop":::"memory");
400 l4_sleep(5);
401#else
402 RTThreadYield();
403#endif
404 }
405 Assert(mFramebuffer);
406 // the driver might not have been constructed yet
407 if (mpDrv)
408 {
409 mFramebuffer->getAddress ((uintptr_t *)&mpDrv->Connector.pu8Data);
410 mFramebuffer->getLineSize ((ULONG*)&mpDrv->Connector.cbScanline);
411 mFramebuffer->getColorDepth ((ULONG*)&mpDrv->Connector.cBits);
412 mFramebuffer->getWidth ((ULONG*)&mpDrv->Connector.cx);
413 mFramebuffer->getHeight ((ULONG*)&mpDrv->Connector.cy);
414 }
415}
416
417void VMDisplay::resetFramebuffer()
418{
419 if (!mFramebuffer)
420 return;
421
422 // the driver might not have been constructed yet
423 if (mpDrv)
424 {
425 mFramebuffer->getAddress ((uintptr_t *)&mpDrv->Connector.pu8Data);
426 mFramebuffer->getColorDepth ((ULONG*)&mpDrv->Connector.cBits);
427 }
428}
429
430/**
431 * Handle display resize event
432 *
433 * @param pInterface VMDisplay connector.
434 * @param cx New width in pixels.
435 * @param cy New height in pixels.
436 */
437DECLCALLBACK(int) VMDisplay::displayResizeCallback(PPDMIDISPLAYCONNECTOR pInterface, uint32_t bpp, void *pvVRAM, uint32_t cbLine, uint32_t cx, uint32_t cy)
438{
439 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
440
441 // forward call to instance handler
442 return pDrv->pDisplay->handleDisplayResize(cx, cy);
443}
444
445/**
446 * Handle display update
447 *
448 * @param pInterface VMDisplay connector.
449 * @param x Left upper boundary x.
450 * @param y Left upper boundary y.
451 * @param cx Update rect width.
452 * @param cy Update rect height.
453 */
454DECLCALLBACK(void) VMDisplay::displayUpdateCallback(PPDMIDISPLAYCONNECTOR pInterface,
455 uint32_t x, uint32_t y, uint32_t cx, uint32_t cy)
456{
457 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
458
459 // forward call to instance handler
460 pDrv->pDisplay->handleDisplayUpdate(x, y, cx, cy);
461}
462
463/**
464 * Periodic display refresh callback.
465 *
466 * @param pInterface VMDisplay connector.
467 */
468DECLCALLBACK(void) VMDisplay::displayRefreshCallback(PPDMIDISPLAYCONNECTOR pInterface)
469{
470 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
471
472
473 /* Contrary to displayUpdateCallback and displayResizeCallback
474 * the framebuffer lock must be taken since the the function
475 * pointed to by pDrv->pUpPort->pfnUpdateDisplay is anaware
476 * of any locking issues. */
477
478 VMDisplay *pDisplay = pDrv->pDisplay;
479
480 uint32_t u32ResizeStatus = pDisplay->mu32ResizeStatus;
481
482 if (u32ResizeStatus == ResizeStatus_UpdateDisplayData)
483 {
484#ifdef DEBUG_sunlover
485 LogFlowFunc (("ResizeStatus_UpdateDisplayData\n"));
486#endif /* DEBUG_sunlover */
487 /* The framebuffer was resized and display data need to be updated. */
488 pDisplay->handleResizeCompletedEMT ();
489 /* Continue with normal processing because the status here is ResizeStatus_Void. */
490 Assert (pDisplay->mu32ResizeStatus == ResizeStatus_Void);
491 }
492 else if (u32ResizeStatus == ResizeStatus_InProgress)
493 {
494#ifdef DEBUG_sunlover
495 LogFlowFunc (("ResizeStatus_InProcess\n"));
496#endif /* DEBUG_sunlover */
497 /* The framebuffer is being resized. Do not call the VGA device back. Immediately return. */
498 return;
499 }
500
501 if (pDisplay->mfPendingVideoAccelEnable)
502 {
503 /* Acceleration was enabled while machine was not yet running
504 * due to restoring from saved state. Update entire display and
505 * actually enable acceleration.
506 */
507 Assert(pDisplay->mpPendingVbvaMemory);
508
509 /* Acceleration can not be yet enabled.*/
510 Assert(pDisplay->mpVbvaMemory == NULL);
511 Assert(!pDisplay->mfVideoAccelEnabled);
512
513 if (pDisplay->mfMachineRunning)
514 {
515 pDisplay->VideoAccelEnable (pDisplay->mfPendingVideoAccelEnable, pDisplay->mpPendingVbvaMemory);
516
517 /* Reset the pending state. */
518 pDisplay->mfPendingVideoAccelEnable = false;
519 pDisplay->mpPendingVbvaMemory = NULL;
520 }
521 }
522 else
523 {
524 Assert(pDisplay->mpPendingVbvaMemory == NULL);
525
526 if (pDisplay->mfVideoAccelEnabled)
527 {
528 Assert(pDisplay->mpVbvaMemory);
529 pDisplay->VideoAccelFlush ();
530 }
531 else
532 {
533 Assert(pDrv->Connector.pu8Data);
534 pDisplay->mFramebuffer->Lock();
535 pDrv->pUpPort->pfnUpdateDisplay(pDrv->pUpPort);
536 pDisplay->mFramebuffer->Unlock();
537 }
538 }
539}
540
541/**
542 * Reset notification
543 *
544 * @param pInterface Display connector.
545 */
546DECLCALLBACK(void) VMDisplay::displayResetCallback(PPDMIDISPLAYCONNECTOR pInterface)
547{
548 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
549
550 LogFlow(("Display::displayResetCallback\n"));
551
552 /* Disable VBVA mode. */
553 pDrv->pDisplay->VideoAccelEnable (false, NULL);
554}
555
556/**
557 * LFBModeChange notification
558 *
559 * @see PDMIDISPLAYCONNECTOR::pfnLFBModeChange
560 */
561DECLCALLBACK(void) VMDisplay::displayLFBModeChangeCallback(PPDMIDISPLAYCONNECTOR pInterface, bool fEnabled)
562{
563 PDRVMAINDISPLAY pDrv = PDMIDISPLAYCONNECTOR_2_MAINDISPLAY(pInterface);
564
565 LogFlow(("Display::displayLFBModeChangeCallback: %d\n", fEnabled));
566
567 NOREF(fEnabled);
568
569 /**
570 * @todo: If we got the callback then VM if definitely running.
571 * But a better method should be implemented.
572 */
573 pDrv->pDisplay->mfMachineRunning = true;
574
575 /* Disable VBVA mode in any case. The guest driver reenables VBVA mode if necessary. */
576 pDrv->pDisplay->VideoAccelEnable (false, NULL);
577}
578
579typedef struct _VBVADIRTYREGION
580{
581 /* Copies of object's pointers used by vbvaRgn functions. */
582 Framebuffer *pFramebuffer;
583 VMDisplay *pDisplay;
584 PPDMIDISPLAYPORT pPort;
585
586 /* Merged rectangles. */
587 int32_t xLeft;
588 int32_t xRight;
589 int32_t yTop;
590 int32_t yBottom;
591
592} VBVADIRTYREGION;
593
594void vbvaRgnInit (VBVADIRTYREGION *prgn, Framebuffer *pfb, VMDisplay *pd, PPDMIDISPLAYPORT pp)
595{
596 memset (prgn, 0, sizeof (VBVADIRTYREGION));
597
598 prgn->pFramebuffer = pfb;
599 prgn->pDisplay = pd;
600 prgn->pPort = pp;
601
602 return;
603}
604
605void vbvaRgnDirtyRect (VBVADIRTYREGION *prgn, VBVACMDHDR *phdr)
606{
607 LogFlow(("vbvaRgnDirtyRect: x = %d, y = %d, w = %d, h = %d\n", phdr->x, phdr->y, phdr->w, phdr->h));
608
609 /*
610 * Here update rectangles are accumulated to form an update area.
611 * @todo
612 * Now the simplies method is used which builds one rectangle that
613 * includes all update areas. A bit more advanced method can be
614 * employed here. The method should be fast however.
615 */
616 if (phdr->w == 0 || phdr->h == 0)
617 {
618 /* Empty rectangle. */
619 return;
620 }
621
622 int32_t xRight = phdr->x + phdr->w;
623 int32_t yBottom = phdr->y + phdr->h;
624
625 if (prgn->xRight == 0)
626 {
627 /* This is the first rectangle to be added. */
628 prgn->xLeft = phdr->x;
629 prgn->yTop = phdr->y;
630 prgn->xRight = xRight;
631 prgn->yBottom = yBottom;
632 }
633 else
634 {
635 /* Adjust region coordinates. */
636 if (prgn->xLeft > phdr->x)
637 prgn->xLeft = phdr->x;
638
639 if (prgn->yTop > phdr->y)
640 prgn->yTop = phdr->y;
641
642 if (prgn->xRight < xRight)
643 prgn->xRight = xRight;
644
645 if (prgn->yBottom < yBottom)
646 prgn->yBottom = yBottom;
647 }
648}
649
650void vbvaRgnUpdateFramebuffer (VBVADIRTYREGION *prgn)
651{
652 uint32_t w = prgn->xRight - prgn->xLeft;
653 uint32_t h = prgn->yBottom - prgn->yTop;
654
655 if (prgn->pFramebuffer && w != 0 && h != 0)
656 {
657 prgn->pPort->pfnUpdateDisplayRect (prgn->pPort, prgn->xLeft, prgn->yTop, w, h);
658 prgn->pDisplay->handleDisplayUpdate (prgn->xLeft, prgn->yTop, w, h);
659 }
660}
661
662static void vbvaSetMemoryFlags (VBVAMEMORY *pVbvaMemory, bool fVideoAccelEnabled, bool fVideoAccelVRDP)
663{
664 if (pVbvaMemory)
665 {
666 /* This called only on changes in mode. So reset VRDP always. */
667 uint32_t fu32Flags = VBVA_F_MODE_VRDP_RESET;
668
669 if (fVideoAccelEnabled)
670 {
671 fu32Flags |= VBVA_F_MODE_ENABLED;
672
673 if (fVideoAccelVRDP)
674 {
675 fu32Flags |= VBVA_F_MODE_VRDP;
676 }
677 }
678
679 pVbvaMemory->fu32ModeFlags = fu32Flags;
680 }
681}
682
683bool VMDisplay::VideoAccelAllowed (void)
684{
685 return true;
686}
687
688/**
689 * @thread EMT
690 */
691int VMDisplay::VideoAccelEnable (bool fEnable, VBVAMEMORY *pVbvaMemory)
692{
693 int rc = VINF_SUCCESS;
694
695 /* Called each time the guest wants to use acceleration,
696 * or when the VGA device disables acceleration,
697 * or when restoring the saved state with accel enabled.
698 *
699 * VGA device disables acceleration on each video mode change
700 * and on reset.
701 *
702 * Guest enabled acceleration at will. And it needs to enable
703 * acceleration after a mode change.
704 */
705 LogFlow(("Display::VideoAccelEnable: mfVideoAccelEnabled = %d, fEnable = %d, pVbvaMemory = %p\n",
706 mfVideoAccelEnabled, fEnable, pVbvaMemory));
707
708 /* Strictly check parameters. Callers must not pass anything in the case. */
709 Assert((fEnable && pVbvaMemory) || (!fEnable && pVbvaMemory == NULL));
710
711 if (!VideoAccelAllowed ())
712 return VERR_NOT_SUPPORTED;
713
714 /*
715 * Verify that the VM is in running state. If it is not,
716 * then this must be postponed until it goes to running.
717 */
718 if (!mfMachineRunning)
719 {
720 Assert (!mfVideoAccelEnabled);
721
722 LogFlow(("Display::VideoAccelEnable: Machine is not yet running.\n"));
723
724 if (fEnable)
725 {
726 mfPendingVideoAccelEnable = fEnable;
727 mpPendingVbvaMemory = pVbvaMemory;
728 }
729
730 return rc;
731 }
732
733 /* Check that current status is not being changed */
734 if (mfVideoAccelEnabled == fEnable)
735 return rc;
736
737 if (mfVideoAccelEnabled)
738 {
739 /* Process any pending orders and empty the VBVA ring buffer. */
740 VideoAccelFlush ();
741 }
742
743 if (!fEnable && mpVbvaMemory)
744 {
745 mpVbvaMemory->fu32ModeFlags &= ~VBVA_F_MODE_ENABLED;
746 }
747
748 /* Safety precaution. There is no more VBVA until everything is setup! */
749 mpVbvaMemory = NULL;
750 mfVideoAccelEnabled = false;
751
752 /* Update entire display. */
753 mpDrv->pUpPort->pfnUpdateDisplayAll(mpDrv->pUpPort);
754
755 /* Everything OK. VBVA status can be changed. */
756
757 /* Notify the VMMDev, which saves VBVA status in the saved state,
758 * and needs to know current status.
759 */
760 PPDMIVMMDEVPORT pVMMDevPort = gVMMDev->getVMMDevPort ();
761
762 if (pVMMDevPort)
763 pVMMDevPort->pfnVBVAChange (pVMMDevPort, fEnable);
764
765 if (fEnable)
766 {
767 mpVbvaMemory = pVbvaMemory;
768 mfVideoAccelEnabled = true;
769
770 /* Initialize the hardware memory. */
771 vbvaSetMemoryFlags (mpVbvaMemory, mfVideoAccelEnabled, false);
772 mpVbvaMemory->off32Data = 0;
773 mpVbvaMemory->off32Free = 0;
774
775 memset (mpVbvaMemory->aRecords, 0, sizeof (mpVbvaMemory->aRecords));
776 mpVbvaMemory->indexRecordFirst = 0;
777 mpVbvaMemory->indexRecordFree = 0;
778
779 LogRel(("VBVA: Enabled.\n"));
780 }
781 else
782 {
783 LogRel(("VBVA: Disabled.\n"));
784 }
785
786 LogFlow(("Display::VideoAccelEnable: rc = %Vrc.\n", rc));
787
788 return rc;
789}
790
791static bool vbvaVerifyRingBuffer (VBVAMEMORY *pVbvaMemory)
792{
793 return true;
794}
795
796static void vbvaFetchBytes (VBVAMEMORY *pVbvaMemory, uint8_t *pu8Dst, uint32_t cbDst)
797{
798 if (cbDst >= VBVA_RING_BUFFER_SIZE)
799 {
800 AssertFailed ();
801 return;
802 }
803
804 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - pVbvaMemory->off32Data;
805 uint8_t *src = &pVbvaMemory->au8RingBuffer[pVbvaMemory->off32Data];
806 int32_t i32Diff = cbDst - u32BytesTillBoundary;
807
808 if (i32Diff <= 0)
809 {
810 /* Chunk will not cross buffer boundary. */
811 memcpy (pu8Dst, src, cbDst);
812 }
813 else
814 {
815 /* Chunk crosses buffer boundary. */
816 memcpy (pu8Dst, src, u32BytesTillBoundary);
817 memcpy (pu8Dst + u32BytesTillBoundary, &pVbvaMemory->au8RingBuffer[0], i32Diff);
818 }
819
820 /* Advance data offset. */
821 pVbvaMemory->off32Data = (pVbvaMemory->off32Data + cbDst) % VBVA_RING_BUFFER_SIZE;
822
823 return;
824}
825
826void VMDisplay::SetVideoModeHint(ULONG aWidth, ULONG aHeight, ULONG aColorDepth)
827{
828 PPDMIVMMDEVPORT pVMMDevPort = gVMMDev->getVMMDevPort ();
829
830 if (pVMMDevPort)
831 pVMMDevPort->pfnRequestDisplayChange(pVMMDevPort, aWidth, aHeight, aColorDepth);
832}
833
834static bool vbvaPartialRead (uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
835{
836 uint8_t *pu8New;
837
838 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
839 *ppu8, *pcb, cbRecord));
840
841 if (*ppu8)
842 {
843 Assert (*pcb);
844 pu8New = (uint8_t *)RTMemRealloc (*ppu8, cbRecord);
845 }
846 else
847 {
848 Assert (!*pcb);
849 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
850 }
851
852 if (!pu8New)
853 {
854 /* Memory allocation failed, fail the function. */
855 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
856 cbRecord));
857
858 if (*ppu8)
859 {
860 RTMemFree (*ppu8);
861 }
862
863 *ppu8 = NULL;
864 *pcb = 0;
865
866 return false;
867 }
868
869 /* Fetch data from the ring buffer. */
870 vbvaFetchBytes (pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
871
872 *ppu8 = pu8New;
873 *pcb = cbRecord;
874
875 return true;
876}
877
878/* For contiguous chunks just return the address in the buffer.
879 * For crossing boundary - allocate a buffer from heap.
880 */
881bool VMDisplay::vbvaFetchCmd (VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
882{
883 uint32_t indexRecordFirst = mpVbvaMemory->indexRecordFirst;
884 uint32_t indexRecordFree = mpVbvaMemory->indexRecordFree;
885
886#ifdef DEBUG_sunlover
887 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd:first = %d, free = %d\n",
888 indexRecordFirst, indexRecordFree));
889#endif /* DEBUG_sunlover */
890
891 if (!vbvaVerifyRingBuffer (mpVbvaMemory))
892 {
893 return false;
894 }
895
896 if (indexRecordFirst == indexRecordFree)
897 {
898 /* No records to process. Return without assigning output variables. */
899 return true;
900 }
901
902 VBVARECORD *pRecord = &mpVbvaMemory->aRecords[indexRecordFirst];
903
904#ifdef DEBUG_sunlover
905 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: cbRecord = 0x%08X\n",
906 pRecord->cbRecord));
907#endif /* DEBUG_sunlover */
908
909 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
910
911 if (mcbVbvaPartial)
912 {
913 /* There is a partial read in process. Continue with it. */
914
915 Assert (mpu8VbvaPartial);
916
917 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: continue partial record mcbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
918 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
919
920 if (cbRecord > mcbVbvaPartial)
921 {
922 /* New data has been added to the record. */
923 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
924 {
925 return false;
926 }
927 }
928
929 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
930 {
931 /* The record is completed by guest. Return it to the caller. */
932 *ppHdr = (VBVACMDHDR *)mpu8VbvaPartial;
933 *pcbCmd = mcbVbvaPartial;
934
935 mpu8VbvaPartial = NULL;
936 mcbVbvaPartial = 0;
937
938 /* Advance the record index. */
939 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
940
941#ifdef DEBUG_sunlover
942 LogFlow(("MAIN::DisplayImpl::vbvaFetchBytes: partial done ok, data = %d, free = %d\n",
943 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
944#endif /* DEBUG_sunlover */
945 }
946
947 return true;
948 }
949
950 /* A new record need to be processed. */
951 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
952 {
953 /* Current record is being written by guest. '=' is important here. */
954 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
955 {
956 /* Partial read must be started. */
957 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
958 {
959 return false;
960 }
961
962 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: started partial record mcbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
963 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
964 }
965
966 return true;
967 }
968
969 /* Current record is complete. */
970
971 /* The size of largest contiguos chunk in the ring biffer. */
972 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - mpVbvaMemory->off32Data;
973
974 /* The ring buffer pointer. */
975 uint8_t *au8RingBuffer = &mpVbvaMemory->au8RingBuffer[0];
976
977 /* The pointer to data in the ring buffer. */
978 uint8_t *src = &au8RingBuffer[mpVbvaMemory->off32Data];
979
980 /* Fetch or point the data. */
981 if (u32BytesTillBoundary >= cbRecord)
982 {
983 /* The command does not cross buffer boundary. Return address in the buffer. */
984 *ppHdr = (VBVACMDHDR *)src;
985
986 /* Advance data offset. */
987 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
988 }
989 else
990 {
991 /* The command crosses buffer boundary. Rare case, so not optimized. */
992 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
993
994 if (!dst)
995 {
996 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: could not allocate %d bytes from heap!!!\n", cbRecord));
997 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
998 return false;
999 }
1000
1001 vbvaFetchBytes (mpVbvaMemory, dst, cbRecord);
1002
1003 *ppHdr = (VBVACMDHDR *)dst;
1004
1005#ifdef DEBUG_sunlover
1006 LogFlow(("MAIN::DisplayImpl::vbvaFetchBytes: Allocated from heap %p\n", dst));
1007#endif /* DEBUG_sunlover */
1008 }
1009
1010 *pcbCmd = cbRecord;
1011
1012 /* Advance the record index. */
1013 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
1014
1015#ifdef DEBUG_sunlover
1016 LogFlow(("MAIN::DisplayImpl::vbvaFetchBytes: done ok, data = %d, free = %d\n",
1017 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1018#endif /* DEBUG_sunlover */
1019
1020 return true;
1021}
1022
1023void VMDisplay::vbvaReleaseCmd (VBVACMDHDR *pHdr, int32_t cbCmd)
1024{
1025 uint8_t *au8RingBuffer = mpVbvaMemory->au8RingBuffer;
1026
1027 if ( (uint8_t *)pHdr >= au8RingBuffer
1028 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
1029 {
1030 /* The pointer is inside ring buffer. Must be continuous chunk. */
1031 Assert (VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
1032
1033 /* Do nothing. */
1034
1035 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1036 }
1037 else
1038 {
1039 /* The pointer is outside. It is then an allocated copy. */
1040
1041#ifdef DEBUG_sunlover
1042 LogFlow(("MAIN::DisplayImpl::vbvaReleaseCmd: Free heap %p\n", pHdr));
1043#endif /* DEBUG_sunlover */
1044
1045 if ((uint8_t *)pHdr == mpu8VbvaPartial)
1046 {
1047 mpu8VbvaPartial = NULL;
1048 mcbVbvaPartial = 0;
1049 }
1050 else
1051 {
1052 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1053 }
1054
1055 RTMemFree (pHdr);
1056 }
1057
1058 return;
1059}
1060
1061/**
1062 * Called regularly on the DisplayRefresh timer.
1063 * Also on behalf of guest, when the ring buffer is full.
1064 *
1065 * @thread EMT
1066 */
1067void VMDisplay::VideoAccelFlush (void)
1068{
1069#ifdef DEBUG_sunlover
1070 LogFlow(("Display::VideoAccelFlush: mfVideoAccelEnabled = %d\n", mfVideoAccelEnabled));
1071#endif /* DEBUG_sunlover */
1072
1073 if (!mfVideoAccelEnabled)
1074 {
1075 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
1076 return;
1077 }
1078
1079 /* Here VBVA is enabled and we have the accelerator memory pointer. */
1080 Assert(mpVbvaMemory);
1081
1082#ifdef DEBUG_sunlover
1083 LogFlow(("Display::VideoAccelFlush: indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
1084 mpVbvaMemory->indexRecordFirst, mpVbvaMemory->indexRecordFree, mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1085#endif /* DEBUG_sunlover */
1086
1087 /* Quick check for "nothing to update" case. */
1088 if (mpVbvaMemory->indexRecordFirst == mpVbvaMemory->indexRecordFree)
1089 {
1090 return;
1091 }
1092
1093 /* Process the ring buffer */
1094
1095 bool fFramebufferIsNull = (mFramebuffer == NULL);
1096
1097 if (!fFramebufferIsNull)
1098 {
1099 mFramebuffer->Lock();
1100 }
1101
1102 /* Initialize dirty rectangles accumulator. */
1103 VBVADIRTYREGION rgn;
1104 vbvaRgnInit (&rgn, mFramebuffer, this, mpDrv->pUpPort);
1105
1106 for (;;)
1107 {
1108 VBVACMDHDR *phdr = NULL;
1109 uint32_t cbCmd = 0;
1110
1111 /* Fetch the command data. */
1112 if (!vbvaFetchCmd (&phdr, &cbCmd))
1113 {
1114 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
1115 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1116
1117 /* Disable VBVA on those processing errors. */
1118 VideoAccelEnable (false, NULL);
1119
1120 break;
1121 }
1122
1123 if (!cbCmd)
1124 {
1125 /* No more commands yet in the queue. */
1126 break;
1127 }
1128
1129 if (!fFramebufferIsNull)
1130 {
1131#ifdef DEBUG_sunlover
1132 LogFlow(("MAIN::DisplayImpl::VideoAccelFlush: hdr: cbCmd = %d, x=%d, y=%d, w=%d, h=%d\n", cbCmd, phdr->x, phdr->y, phdr->w, phdr->h));
1133#endif /* DEBUG_sunlover */
1134
1135 /* Handle the command.
1136 *
1137 * Guest is responsible for updating the guest video memory.
1138 * The Windows guest does all drawing using Eng*.
1139 *
1140 * For local output, only dirty rectangle information is used
1141 * to update changed areas.
1142 *
1143 * Dirty rectangles are accumulated to exclude overlapping updates and
1144 * group small updates to a larger one.
1145 */
1146
1147 /* Accumulate the update. */
1148 vbvaRgnDirtyRect (&rgn, phdr);
1149
1150// /* Forward the command to VRDP server. */
1151// mParent->consoleVRDPServer()->SendUpdate (phdr, cbCmd);
1152 }
1153
1154 vbvaReleaseCmd (phdr, cbCmd);
1155 }
1156
1157 if (!fFramebufferIsNull)
1158 {
1159 mFramebuffer->Unlock ();
1160 }
1161
1162 /* Draw the framebuffer. */
1163 vbvaRgnUpdateFramebuffer (&rgn);
1164}
1165
1166/**
1167 * Queries an interface to the driver.
1168 *
1169 * @returns Pointer to interface.
1170 * @returns NULL if the interface was not supported by the driver.
1171 * @param pInterface Pointer to this interface structure.
1172 * @param enmInterface The requested interface identification.
1173 */
1174DECLCALLBACK(void *) VMDisplay::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
1175{
1176 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1177 PDRVMAINDISPLAY pDrv = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
1178 switch (enmInterface)
1179 {
1180 case PDMINTERFACE_BASE:
1181 return &pDrvIns->IBase;
1182 case PDMINTERFACE_DISPLAY_CONNECTOR:
1183 return &pDrv->Connector;
1184 default:
1185 return NULL;
1186 }
1187}
1188
1189
1190/**
1191 * Construct a display driver instance.
1192 *
1193 * @returns VBox status.
1194 * @param pDrvIns The driver instance data.
1195 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
1196 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
1197 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
1198 * iInstance it's expected to be used a bit in this function.
1199 */
1200DECLCALLBACK(int) VMDisplay::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
1201{
1202 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
1203 LogFlow(("VMDisplay::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
1204
1205
1206 /*
1207 * Validate configuration.
1208 */
1209 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
1210 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1211 PPDMIBASE pBaseIgnore;
1212 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
1213 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
1214 {
1215 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
1216 return VERR_PDM_DRVINS_NO_ATTACH;
1217 }
1218
1219 /*
1220 * Init Interfaces.
1221 */
1222 pDrvIns->IBase.pfnQueryInterface = VMDisplay::drvQueryInterface;
1223
1224 pData->Connector.pfnResize = VMDisplay::displayResizeCallback;
1225 pData->Connector.pfnUpdateRect = VMDisplay::displayUpdateCallback;
1226 pData->Connector.pfnRefresh = VMDisplay::displayRefreshCallback;
1227 pData->Connector.pfnReset = VMDisplay::displayResetCallback;
1228 pData->Connector.pfnLFBModeChange = VMDisplay::displayLFBModeChangeCallback;
1229
1230 /*
1231 * Get the IDisplayPort interface of the above driver/device.
1232 */
1233 pData->pUpPort = (PPDMIDISPLAYPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_PORT);
1234 if (!pData->pUpPort)
1235 {
1236 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
1237 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1238 }
1239
1240 /*
1241 * Get the VMDisplay object pointer and update the mpDrv member.
1242 */
1243 void *pv;
1244 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
1245 if (VBOX_FAILURE(rc))
1246 {
1247 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
1248 return rc;
1249 }
1250 pData->pDisplay = (VMDisplay *)pv; /** @todo Check this cast! */
1251 pData->pDisplay->mpDrv = pData;
1252
1253 /*
1254 * If there is a Framebuffer, we have to update our display information
1255 */
1256 if (pData->pDisplay->mFramebuffer)
1257 {
1258 pData->pDisplay->updateDisplayData();
1259 }
1260
1261 /*
1262 * Start periodic screen refreshes
1263 */
1264 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 50);
1265
1266 return VINF_SUCCESS;
1267}
1268
1269
1270/**
1271 * VMDisplay driver registration record.
1272 */
1273const PDMDRVREG VMDisplay::DrvReg =
1274{
1275 /* u32Version */
1276 PDM_DRVREG_VERSION,
1277 /* szDriverName */
1278 "MainDisplay",
1279 /* pszDescription */
1280 "Main display driver (Main as in the API).",
1281 /* fFlags */
1282 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1283 /* fClass. */
1284 PDM_DRVREG_CLASS_DISPLAY,
1285 /* cMaxInstances */
1286 ~0,
1287 /* cbInstance */
1288 sizeof(DRVMAINDISPLAY),
1289 /* pfnConstruct */
1290 VMDisplay::drvConstruct,
1291 /* pfnDestruct */
1292 NULL,
1293 /* pfnIOCtl */
1294 NULL,
1295 /* pfnPowerOn */
1296 NULL,
1297 /* pfnReset */
1298 NULL,
1299 /* pfnSuspend */
1300 NULL,
1301 /* pfnResume */
1302 NULL,
1303 /* pfnDetach */
1304 NULL
1305};
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