VirtualBox

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

Last change on this file since 2597 was 2491, checked in by vboxsync, 18 years ago

HostKey+H; cosmetical changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.2 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
826
827static bool vbvaPartialRead (uint8_t **ppu8, uint32_t *pcb, uint32_t cbRecord, VBVAMEMORY *pVbvaMemory)
828{
829 uint8_t *pu8New;
830
831 LogFlow(("MAIN::DisplayImpl::vbvaPartialRead: p = %p, cb = %d, cbRecord 0x%08X\n",
832 *ppu8, *pcb, cbRecord));
833
834 if (*ppu8)
835 {
836 Assert (*pcb);
837 pu8New = (uint8_t *)RTMemRealloc (*ppu8, cbRecord);
838 }
839 else
840 {
841 Assert (!*pcb);
842 pu8New = (uint8_t *)RTMemAlloc (cbRecord);
843 }
844
845 if (!pu8New)
846 {
847 /* Memory allocation failed, fail the function. */
848 Log(("MAIN::vbvaPartialRead: failed to (re)alocate memory for partial record!!! cbRecord 0x%08X\n",
849 cbRecord));
850
851 if (*ppu8)
852 {
853 RTMemFree (*ppu8);
854 }
855
856 *ppu8 = NULL;
857 *pcb = 0;
858
859 return false;
860 }
861
862 /* Fetch data from the ring buffer. */
863 vbvaFetchBytes (pVbvaMemory, pu8New + *pcb, cbRecord - *pcb);
864
865 *ppu8 = pu8New;
866 *pcb = cbRecord;
867
868 return true;
869}
870
871/* For contiguous chunks just return the address in the buffer.
872 * For crossing boundary - allocate a buffer from heap.
873 */
874bool VMDisplay::vbvaFetchCmd (VBVACMDHDR **ppHdr, uint32_t *pcbCmd)
875{
876 uint32_t indexRecordFirst = mpVbvaMemory->indexRecordFirst;
877 uint32_t indexRecordFree = mpVbvaMemory->indexRecordFree;
878
879#ifdef DEBUG_sunlover
880 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd:first = %d, free = %d\n",
881 indexRecordFirst, indexRecordFree));
882#endif /* DEBUG_sunlover */
883
884 if (!vbvaVerifyRingBuffer (mpVbvaMemory))
885 {
886 return false;
887 }
888
889 if (indexRecordFirst == indexRecordFree)
890 {
891 /* No records to process. Return without assigning output variables. */
892 return true;
893 }
894
895 VBVARECORD *pRecord = &mpVbvaMemory->aRecords[indexRecordFirst];
896
897#ifdef DEBUG_sunlover
898 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: cbRecord = 0x%08X\n",
899 pRecord->cbRecord));
900#endif /* DEBUG_sunlover */
901
902 uint32_t cbRecord = pRecord->cbRecord & ~VBVA_F_RECORD_PARTIAL;
903
904 if (mcbVbvaPartial)
905 {
906 /* There is a partial read in process. Continue with it. */
907
908 Assert (mpu8VbvaPartial);
909
910 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: continue partial record mcbVbvaPartial = %d cbRecord 0x%08X, first = %d, free = %d\n",
911 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
912
913 if (cbRecord > mcbVbvaPartial)
914 {
915 /* New data has been added to the record. */
916 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
917 {
918 return false;
919 }
920 }
921
922 if (!(pRecord->cbRecord & VBVA_F_RECORD_PARTIAL))
923 {
924 /* The record is completed by guest. Return it to the caller. */
925 *ppHdr = (VBVACMDHDR *)mpu8VbvaPartial;
926 *pcbCmd = mcbVbvaPartial;
927
928 mpu8VbvaPartial = NULL;
929 mcbVbvaPartial = 0;
930
931 /* Advance the record index. */
932 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
933
934#ifdef DEBUG_sunlover
935 LogFlow(("MAIN::DisplayImpl::vbvaFetchBytes: partial done ok, data = %d, free = %d\n",
936 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
937#endif /* DEBUG_sunlover */
938 }
939
940 return true;
941 }
942
943 /* A new record need to be processed. */
944 if (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL)
945 {
946 /* Current record is being written by guest. '=' is important here. */
947 if (cbRecord >= VBVA_RING_BUFFER_SIZE - VBVA_RING_BUFFER_THRESHOLD)
948 {
949 /* Partial read must be started. */
950 if (!vbvaPartialRead (&mpu8VbvaPartial, &mcbVbvaPartial, cbRecord, mpVbvaMemory))
951 {
952 return false;
953 }
954
955 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: started partial record mcbVbvaPartial = 0x%08X cbRecord 0x%08X, first = %d, free = %d\n",
956 mcbVbvaPartial, pRecord->cbRecord, indexRecordFirst, indexRecordFree));
957 }
958
959 return true;
960 }
961
962 /* Current record is complete. */
963
964 /* The size of largest contiguos chunk in the ring biffer. */
965 uint32_t u32BytesTillBoundary = VBVA_RING_BUFFER_SIZE - mpVbvaMemory->off32Data;
966
967 /* The ring buffer pointer. */
968 uint8_t *au8RingBuffer = &mpVbvaMemory->au8RingBuffer[0];
969
970 /* The pointer to data in the ring buffer. */
971 uint8_t *src = &au8RingBuffer[mpVbvaMemory->off32Data];
972
973 /* Fetch or point the data. */
974 if (u32BytesTillBoundary >= cbRecord)
975 {
976 /* The command does not cross buffer boundary. Return address in the buffer. */
977 *ppHdr = (VBVACMDHDR *)src;
978
979 /* Advance data offset. */
980 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
981 }
982 else
983 {
984 /* The command crosses buffer boundary. Rare case, so not optimized. */
985 uint8_t *dst = (uint8_t *)RTMemAlloc (cbRecord);
986
987 if (!dst)
988 {
989 LogFlow(("MAIN::DisplayImpl::vbvaFetchCmd: could not allocate %d bytes from heap!!!\n", cbRecord));
990 mpVbvaMemory->off32Data = (mpVbvaMemory->off32Data + cbRecord) % VBVA_RING_BUFFER_SIZE;
991 return false;
992 }
993
994 vbvaFetchBytes (mpVbvaMemory, dst, cbRecord);
995
996 *ppHdr = (VBVACMDHDR *)dst;
997
998#ifdef DEBUG_sunlover
999 LogFlow(("MAIN::DisplayImpl::vbvaFetchBytes: Allocated from heap %p\n", dst));
1000#endif /* DEBUG_sunlover */
1001 }
1002
1003 *pcbCmd = cbRecord;
1004
1005 /* Advance the record index. */
1006 mpVbvaMemory->indexRecordFirst = (indexRecordFirst + 1) % VBVA_MAX_RECORDS;
1007
1008#ifdef DEBUG_sunlover
1009 LogFlow(("MAIN::DisplayImpl::vbvaFetchBytes: done ok, data = %d, free = %d\n",
1010 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1011#endif /* DEBUG_sunlover */
1012
1013 return true;
1014}
1015
1016void VMDisplay::vbvaReleaseCmd (VBVACMDHDR *pHdr, int32_t cbCmd)
1017{
1018 uint8_t *au8RingBuffer = mpVbvaMemory->au8RingBuffer;
1019
1020 if ( (uint8_t *)pHdr >= au8RingBuffer
1021 && (uint8_t *)pHdr < &au8RingBuffer[VBVA_RING_BUFFER_SIZE])
1022 {
1023 /* The pointer is inside ring buffer. Must be continuous chunk. */
1024 Assert (VBVA_RING_BUFFER_SIZE - ((uint8_t *)pHdr - au8RingBuffer) >= cbCmd);
1025
1026 /* Do nothing. */
1027
1028 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1029 }
1030 else
1031 {
1032 /* The pointer is outside. It is then an allocated copy. */
1033
1034#ifdef DEBUG_sunlover
1035 LogFlow(("MAIN::DisplayImpl::vbvaReleaseCmd: Free heap %p\n", pHdr));
1036#endif /* DEBUG_sunlover */
1037
1038 if ((uint8_t *)pHdr == mpu8VbvaPartial)
1039 {
1040 mpu8VbvaPartial = NULL;
1041 mcbVbvaPartial = 0;
1042 }
1043 else
1044 {
1045 Assert (!mpu8VbvaPartial && mcbVbvaPartial == 0);
1046 }
1047
1048 RTMemFree (pHdr);
1049 }
1050
1051 return;
1052}
1053
1054/**
1055 * Called regularly on the DisplayRefresh timer.
1056 * Also on behalf of guest, when the ring buffer is full.
1057 *
1058 * @thread EMT
1059 */
1060void VMDisplay::VideoAccelFlush (void)
1061{
1062#ifdef DEBUG_sunlover
1063 LogFlow(("Display::VideoAccelFlush: mfVideoAccelEnabled = %d\n", mfVideoAccelEnabled));
1064#endif /* DEBUG_sunlover */
1065
1066 if (!mfVideoAccelEnabled)
1067 {
1068 Log(("Display::VideoAccelFlush: called with disabled VBVA!!! Ignoring.\n"));
1069 return;
1070 }
1071
1072 /* Here VBVA is enabled and we have the accelerator memory pointer. */
1073 Assert(mpVbvaMemory);
1074
1075#ifdef DEBUG_sunlover
1076 LogFlow(("Display::VideoAccelFlush: indexRecordFirst = %d, indexRecordFree = %d, off32Data = %d, off32Free = %d\n",
1077 mpVbvaMemory->indexRecordFirst, mpVbvaMemory->indexRecordFree, mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1078#endif /* DEBUG_sunlover */
1079
1080 /* Quick check for "nothing to update" case. */
1081 if (mpVbvaMemory->indexRecordFirst == mpVbvaMemory->indexRecordFree)
1082 {
1083 return;
1084 }
1085
1086 /* Process the ring buffer */
1087
1088 bool fFramebufferIsNull = (mFramebuffer == NULL);
1089
1090 if (!fFramebufferIsNull)
1091 {
1092 mFramebuffer->Lock();
1093 }
1094
1095 /* Initialize dirty rectangles accumulator. */
1096 VBVADIRTYREGION rgn;
1097 vbvaRgnInit (&rgn, mFramebuffer, this, mpDrv->pUpPort);
1098
1099 for (;;)
1100 {
1101 VBVACMDHDR *phdr = NULL;
1102 uint32_t cbCmd = 0;
1103
1104 /* Fetch the command data. */
1105 if (!vbvaFetchCmd (&phdr, &cbCmd))
1106 {
1107 Log(("Display::VideoAccelFlush: unable to fetch command. off32Data = %d, off32Free = %d. Disabling VBVA!!!\n",
1108 mpVbvaMemory->off32Data, mpVbvaMemory->off32Free));
1109
1110 /* Disable VBVA on those processing errors. */
1111 VideoAccelEnable (false, NULL);
1112
1113 break;
1114 }
1115
1116 if (!cbCmd)
1117 {
1118 /* No more commands yet in the queue. */
1119 break;
1120 }
1121
1122 if (!fFramebufferIsNull)
1123 {
1124#ifdef DEBUG_sunlover
1125 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));
1126#endif /* DEBUG_sunlover */
1127
1128 /* Handle the command.
1129 *
1130 * Guest is responsible for updating the guest video memory.
1131 * The Windows guest does all drawing using Eng*.
1132 *
1133 * For local output, only dirty rectangle information is used
1134 * to update changed areas.
1135 *
1136 * Dirty rectangles are accumulated to exclude overlapping updates and
1137 * group small updates to a larger one.
1138 */
1139
1140 /* Accumulate the update. */
1141 vbvaRgnDirtyRect (&rgn, phdr);
1142
1143// /* Forward the command to VRDP server. */
1144// mParent->consoleVRDPServer()->SendUpdate (phdr, cbCmd);
1145 }
1146
1147 vbvaReleaseCmd (phdr, cbCmd);
1148 }
1149
1150 if (!fFramebufferIsNull)
1151 {
1152 mFramebuffer->Unlock ();
1153 }
1154
1155 /* Draw the framebuffer. */
1156 vbvaRgnUpdateFramebuffer (&rgn);
1157}
1158
1159/**
1160 * Queries an interface to the driver.
1161 *
1162 * @returns Pointer to interface.
1163 * @returns NULL if the interface was not supported by the driver.
1164 * @param pInterface Pointer to this interface structure.
1165 * @param enmInterface The requested interface identification.
1166 */
1167DECLCALLBACK(void *) VMDisplay::drvQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
1168{
1169 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1170 PDRVMAINDISPLAY pDrv = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
1171 switch (enmInterface)
1172 {
1173 case PDMINTERFACE_BASE:
1174 return &pDrvIns->IBase;
1175 case PDMINTERFACE_DISPLAY_CONNECTOR:
1176 return &pDrv->Connector;
1177 default:
1178 return NULL;
1179 }
1180}
1181
1182
1183/**
1184 * Construct a display driver instance.
1185 *
1186 * @returns VBox status.
1187 * @param pDrvIns The driver instance data.
1188 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
1189 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
1190 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
1191 * iInstance it's expected to be used a bit in this function.
1192 */
1193DECLCALLBACK(int) VMDisplay::drvConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
1194{
1195 PDRVMAINDISPLAY pData = PDMINS2DATA(pDrvIns, PDRVMAINDISPLAY);
1196 LogFlow(("VMDisplay::drvConstruct: iInstance=%d\n", pDrvIns->iInstance));
1197
1198
1199 /*
1200 * Validate configuration.
1201 */
1202 if (!CFGMR3AreValuesValid(pCfgHandle, "Object\0"))
1203 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
1204 PPDMIBASE pBaseIgnore;
1205 int rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pBaseIgnore);
1206 if (rc != VERR_PDM_NO_ATTACHED_DRIVER)
1207 {
1208 AssertMsgFailed(("Configuration error: Not possible to attach anything to this driver!\n"));
1209 return VERR_PDM_DRVINS_NO_ATTACH;
1210 }
1211
1212 /*
1213 * Init Interfaces.
1214 */
1215 pDrvIns->IBase.pfnQueryInterface = VMDisplay::drvQueryInterface;
1216
1217 pData->Connector.pfnResize = VMDisplay::displayResizeCallback;
1218 pData->Connector.pfnUpdateRect = VMDisplay::displayUpdateCallback;
1219 pData->Connector.pfnRefresh = VMDisplay::displayRefreshCallback;
1220 pData->Connector.pfnReset = VMDisplay::displayResetCallback;
1221 pData->Connector.pfnLFBModeChange = VMDisplay::displayLFBModeChangeCallback;
1222
1223 /*
1224 * Get the IDisplayPort interface of the above driver/device.
1225 */
1226 pData->pUpPort = (PPDMIDISPLAYPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_DISPLAY_PORT);
1227 if (!pData->pUpPort)
1228 {
1229 AssertMsgFailed(("Configuration error: No display port interface above!\n"));
1230 return VERR_PDM_MISSING_INTERFACE_ABOVE;
1231 }
1232
1233 /*
1234 * Get the VMDisplay object pointer and update the mpDrv member.
1235 */
1236 void *pv;
1237 rc = CFGMR3QueryPtr(pCfgHandle, "Object", &pv);
1238 if (VBOX_FAILURE(rc))
1239 {
1240 AssertMsgFailed(("Configuration error: No/bad \"Object\" value! rc=%Vrc\n", rc));
1241 return rc;
1242 }
1243 pData->pDisplay = (VMDisplay *)pv; /** @todo Check this cast! */
1244 pData->pDisplay->mpDrv = pData;
1245
1246 /*
1247 * If there is a Framebuffer, we have to update our display information
1248 */
1249 if (pData->pDisplay->mFramebuffer)
1250 {
1251 pData->pDisplay->updateDisplayData();
1252 }
1253
1254 /*
1255 * Start periodic screen refreshes
1256 */
1257 pData->pUpPort->pfnSetRefreshRate(pData->pUpPort, 50);
1258
1259 return VINF_SUCCESS;
1260}
1261
1262
1263/**
1264 * VMDisplay driver registration record.
1265 */
1266const PDMDRVREG VMDisplay::DrvReg =
1267{
1268 /* u32Version */
1269 PDM_DRVREG_VERSION,
1270 /* szDriverName */
1271 "MainDisplay",
1272 /* pszDescription */
1273 "Main display driver (Main as in the API).",
1274 /* fFlags */
1275 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1276 /* fClass. */
1277 PDM_DRVREG_CLASS_DISPLAY,
1278 /* cMaxInstances */
1279 ~0,
1280 /* cbInstance */
1281 sizeof(DRVMAINDISPLAY),
1282 /* pfnConstruct */
1283 VMDisplay::drvConstruct,
1284 /* pfnDestruct */
1285 NULL,
1286 /* pfnIOCtl */
1287 NULL,
1288 /* pfnPowerOn */
1289 NULL,
1290 /* pfnReset */
1291 NULL,
1292 /* pfnSuspend */
1293 NULL,
1294 /* pfnResume */
1295 NULL,
1296 /* pfnDetach */
1297 NULL
1298};
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