1 | /* $Id: vbva.c 69064 2017-10-12 18:18:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox X11 Additions graphics driver 2D acceleration functions
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
10 | * copy of this software and associated documentation files (the "Software"),
|
---|
11 | * to deal in the Software without restriction, including without limitation
|
---|
12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
13 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
14 | * Software is furnished to do so, subject to the following conditions:
|
---|
15 | *
|
---|
16 | * The above copyright notice and this permission notice shall be included in
|
---|
17 | * all copies or substantial portions of the Software.
|
---|
18 | *
|
---|
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
---|
22 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
---|
23 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
---|
24 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
---|
25 | * USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include <VBox/VBoxGuestLib.h>
|
---|
29 |
|
---|
30 | #include <iprt/string.h>
|
---|
31 | #include "compiler.h"
|
---|
32 |
|
---|
33 | #include "vboxvideo.h"
|
---|
34 |
|
---|
35 | #ifdef XORG_7X
|
---|
36 | # include <stdlib.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | /**************************************************************************
|
---|
40 | * Main functions *
|
---|
41 | **************************************************************************/
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Callback function called by the X server to tell us about dirty
|
---|
45 | * rectangles in the video buffer.
|
---|
46 | *
|
---|
47 | * @param pScrn pointer to the information structure for the current
|
---|
48 | * screen
|
---|
49 | * @param iRects Number of dirty rectangles to update
|
---|
50 | * @param aRects Array of structures containing the coordinates of the
|
---|
51 | * rectangles
|
---|
52 | */
|
---|
53 | void vbvxHandleDirtyRect(ScrnInfoPtr pScrn, int iRects, BoxPtr aRects)
|
---|
54 | {
|
---|
55 | VBVACMDHDR cmdHdr;
|
---|
56 | VBOXPtr pVBox;
|
---|
57 | int i;
|
---|
58 | unsigned j;
|
---|
59 |
|
---|
60 | pVBox = pScrn->driverPrivate;
|
---|
61 | if (!pScrn->vtSema)
|
---|
62 | return;
|
---|
63 |
|
---|
64 | for (j = 0; j < pVBox->cScreens; ++j)
|
---|
65 | {
|
---|
66 | /* Just continue quietly if VBVA is not currently active. */
|
---|
67 | struct VBVABUFFER *pVBVA = pVBox->pScreens[j].aVbvaCtx.pVBVA;
|
---|
68 | if ( !pVBVA
|
---|
69 | || !(pVBVA->hostFlags.u32HostEvents & VBVA_F_MODE_ENABLED))
|
---|
70 | continue;
|
---|
71 | for (i = 0; i < iRects; ++i)
|
---|
72 | {
|
---|
73 | if ( aRects[i].x1 > pVBox->pScreens[j].aScreenLocation.x
|
---|
74 | + pVBox->pScreens[j].aScreenLocation.cx
|
---|
75 | || aRects[i].y1 > pVBox->pScreens[j].aScreenLocation.y
|
---|
76 | + pVBox->pScreens[j].aScreenLocation.cy
|
---|
77 | || aRects[i].x2 < pVBox->pScreens[j].aScreenLocation.x
|
---|
78 | || aRects[i].y2 < pVBox->pScreens[j].aScreenLocation.y)
|
---|
79 | continue;
|
---|
80 | cmdHdr.x = (int16_t)aRects[i].x1 - pVBox->pScreens[0].aScreenLocation.x;
|
---|
81 | cmdHdr.y = (int16_t)aRects[i].y1 - pVBox->pScreens[0].aScreenLocation.y;
|
---|
82 | cmdHdr.w = (uint16_t)(aRects[i].x2 - aRects[i].x1);
|
---|
83 | cmdHdr.h = (uint16_t)(aRects[i].y2 - aRects[i].y1);
|
---|
84 |
|
---|
85 | #if 0
|
---|
86 | TRACE_LOG("display=%u, x=%d, y=%d, w=%d, h=%d\n",
|
---|
87 | j, cmdHdr.x, cmdHdr.y, cmdHdr.w, cmdHdr.h);
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | if (VBoxVBVABufferBeginUpdate(&pVBox->pScreens[j].aVbvaCtx,
|
---|
91 | &pVBox->guestCtx))
|
---|
92 | {
|
---|
93 | VBoxVBVAWrite(&pVBox->pScreens[j].aVbvaCtx, &pVBox->guestCtx, &cmdHdr,
|
---|
94 | sizeof(cmdHdr));
|
---|
95 | VBoxVBVABufferEndUpdate(&pVBox->pScreens[j].aVbvaCtx);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | static DECLCALLBACK(void *) hgsmiEnvAlloc(void *pvEnv, HGSMISIZE cb)
|
---|
102 | {
|
---|
103 | NOREF(pvEnv);
|
---|
104 | return calloc(1, cb);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static DECLCALLBACK(void) hgsmiEnvFree(void *pvEnv, void *pv)
|
---|
108 | {
|
---|
109 | NOREF(pvEnv);
|
---|
110 | free(pv);
|
---|
111 | }
|
---|
112 |
|
---|
113 | static HGSMIENV g_hgsmiEnv =
|
---|
114 | {
|
---|
115 | NULL,
|
---|
116 | hgsmiEnvAlloc,
|
---|
117 | hgsmiEnvFree
|
---|
118 | };
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Calculate the location in video RAM of and initialise the heap for guest to
|
---|
122 | * host messages. In the VirtualBox 4.3 and earlier Guest Additions this
|
---|
123 | * function creates the heap structures directly in guest video RAM, so it
|
---|
124 | * needs to be called whenever video RAM is (re-)set-up.
|
---|
125 | */
|
---|
126 | void vbvxSetUpHGSMIHeapInGuest(VBOXPtr pVBox, uint32_t cbVRAM)
|
---|
127 | {
|
---|
128 | int rc;
|
---|
129 | uint32_t offVRAMBaseMapping, offGuestHeapMemory, cbGuestHeapMemory;
|
---|
130 | void *pvGuestHeapMemory;
|
---|
131 |
|
---|
132 | VBoxHGSMIGetBaseMappingInfo(cbVRAM, &offVRAMBaseMapping, NULL, &offGuestHeapMemory, &cbGuestHeapMemory, NULL);
|
---|
133 | pvGuestHeapMemory = ((uint8_t *)pVBox->base) + offVRAMBaseMapping + offGuestHeapMemory;
|
---|
134 | rc = VBoxHGSMISetupGuestContext(&pVBox->guestCtx, pvGuestHeapMemory, cbGuestHeapMemory,
|
---|
135 | offVRAMBaseMapping + offGuestHeapMemory, &g_hgsmiEnv);
|
---|
136 | VBVXASSERT(RT_SUCCESS(rc), ("Failed to set up the guest-to-host message buffer heap, rc=%d\n", rc));
|
---|
137 | pVBox->cbView = offVRAMBaseMapping;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** Callback to fill in the view structures */
|
---|
141 | static DECLCALLBACK(int) vboxFillViewInfo(void *pvVBox, struct VBVAINFOVIEW *pViews, uint32_t cViews)
|
---|
142 | {
|
---|
143 | VBOXPtr pVBox = (VBOXPtr)pvVBox;
|
---|
144 | unsigned i;
|
---|
145 | for (i = 0; i < cViews; ++i)
|
---|
146 | {
|
---|
147 | pViews[i].u32ViewIndex = i;
|
---|
148 | pViews[i].u32ViewOffset = 0;
|
---|
149 | pViews[i].u32ViewSize = pVBox->cbView;
|
---|
150 | pViews[i].u32MaxScreenSize = pVBox->cbFBMax;
|
---|
151 | }
|
---|
152 | return VINF_SUCCESS;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Initialise VirtualBox's accelerated video extensions.
|
---|
157 | *
|
---|
158 | * @returns TRUE on success, FALSE on failure
|
---|
159 | */
|
---|
160 | static Bool vboxSetupVRAMVbva(VBOXPtr pVBox)
|
---|
161 | {
|
---|
162 | int rc = VINF_SUCCESS;
|
---|
163 | unsigned i;
|
---|
164 |
|
---|
165 | pVBox->cbFBMax = pVBox->cbView;
|
---|
166 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
167 | {
|
---|
168 | pVBox->cbFBMax -= VBVA_MIN_BUFFER_SIZE;
|
---|
169 | pVBox->pScreens[i].aoffVBVABuffer = pVBox->cbFBMax;
|
---|
170 | TRACE_LOG("VBVA buffer offset for screen %u: 0x%lx\n", i,
|
---|
171 | (unsigned long) pVBox->cbFBMax);
|
---|
172 | VBoxVBVASetupBufferContext(&pVBox->pScreens[i].aVbvaCtx,
|
---|
173 | pVBox->pScreens[i].aoffVBVABuffer,
|
---|
174 | VBVA_MIN_BUFFER_SIZE);
|
---|
175 | }
|
---|
176 | TRACE_LOG("Maximum framebuffer size: %lu (0x%lx)\n",
|
---|
177 | (unsigned long) pVBox->cbFBMax,
|
---|
178 | (unsigned long) pVBox->cbFBMax);
|
---|
179 | rc = VBoxHGSMISendViewInfo(&pVBox->guestCtx, pVBox->cScreens,
|
---|
180 | vboxFillViewInfo, (void *)pVBox);
|
---|
181 | VBVXASSERT(RT_SUCCESS(rc), ("Failed to send the view information to the host, rc=%d\n", rc));
|
---|
182 | return TRUE;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static Bool haveHGSMIModeHintAndCursorReportingInterface(VBOXPtr pVBox)
|
---|
186 | {
|
---|
187 | uint32_t fModeHintReporting, fCursorReporting;
|
---|
188 |
|
---|
189 | return RT_SUCCESS(VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_MODE_HINT_REPORTING, &fModeHintReporting))
|
---|
190 | && RT_SUCCESS(VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING, &fCursorReporting))
|
---|
191 | && fModeHintReporting == VINF_SUCCESS
|
---|
192 | && fCursorReporting == VINF_SUCCESS;
|
---|
193 | }
|
---|
194 |
|
---|
195 | static Bool hostHasScreenBlankingFlag(VBOXPtr pVBox)
|
---|
196 | {
|
---|
197 | uint32_t fScreenFlags;
|
---|
198 |
|
---|
199 | return RT_SUCCESS(VBoxQueryConfHGSMI(&pVBox->guestCtx, VBOX_VBVA_CONF32_SCREEN_FLAGS, &fScreenFlags))
|
---|
200 | && fScreenFlags & VBVA_SCREEN_F_BLANK;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Inform VBox that we will supply it with dirty rectangle information
|
---|
205 | * and install the dirty rectangle handler.
|
---|
206 | *
|
---|
207 | * @returns TRUE for success, FALSE for failure
|
---|
208 | * @param pScrn Pointer to a structure describing the X screen in use
|
---|
209 | */
|
---|
210 | Bool
|
---|
211 | vboxEnableVbva(ScrnInfoPtr pScrn)
|
---|
212 | {
|
---|
213 | Bool rc = TRUE;
|
---|
214 | unsigned i;
|
---|
215 | VBOXPtr pVBox = pScrn->driverPrivate;
|
---|
216 |
|
---|
217 | TRACE_ENTRY();
|
---|
218 | if (!vboxSetupVRAMVbva(pVBox))
|
---|
219 | return FALSE;
|
---|
220 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
221 | {
|
---|
222 | struct VBVABUFFER *pVBVA;
|
---|
223 |
|
---|
224 | pVBVA = (struct VBVABUFFER *) ( ((uint8_t *)pVBox->base)
|
---|
225 | + pVBox->pScreens[i].aoffVBVABuffer);
|
---|
226 | if (!VBoxVBVAEnable(&pVBox->pScreens[i].aVbvaCtx, &pVBox->guestCtx,
|
---|
227 | pVBVA, i))
|
---|
228 | rc = FALSE;
|
---|
229 | }
|
---|
230 | VBVXASSERT(rc, ("Failed to enable screen update reporting for at least one virtual monitor.\n"));
|
---|
231 | pVBox->fHaveHGSMIModeHints = haveHGSMIModeHintAndCursorReportingInterface(pVBox);
|
---|
232 | pVBox->fHostHasScreenBlankingFlag = hostHasScreenBlankingFlag(pVBox);
|
---|
233 | return rc;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Inform VBox that we will stop supplying it with dirty rectangle
|
---|
238 | * information. This function is intended to be called when an X
|
---|
239 | * virtual terminal is disabled, or the X server is terminated.
|
---|
240 | *
|
---|
241 | * @returns TRUE for success, FALSE for failure
|
---|
242 | * @param pScrn Pointer to a structure describing the X screen in use
|
---|
243 | */
|
---|
244 | void
|
---|
245 | vboxDisableVbva(ScrnInfoPtr pScrn)
|
---|
246 | {
|
---|
247 | unsigned i;
|
---|
248 | VBOXPtr pVBox = pScrn->driverPrivate;
|
---|
249 |
|
---|
250 | TRACE_ENTRY();
|
---|
251 | for (i = 0; i < pVBox->cScreens; ++i)
|
---|
252 | VBoxVBVADisable(&pVBox->pScreens[i].aVbvaCtx, &pVBox->guestCtx, i);
|
---|
253 | }
|
---|