VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoVbva.cpp@ 33711

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

Additions/WINNT/Graphics: more refactoring

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1/** @file
2 *
3 * VirtualBox Windows Wddm guest video driver
4 *
5 * VBVA dirty rectangles calculations.
6 *
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "../VBoxVideo.h"
19#include "../Helper.h"
20
21static int vboxVBVAInformHost (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO * pVbva, BOOL bEnable)
22{
23 int rc = VERR_NO_MEMORY;
24 void *p = vboxHGSMIBufferAlloc (commonFromDeviceExt(pDevExt),
25 sizeof (VBVAENABLE_EX),
26 HGSMI_CH_VBVA,
27 VBVA_ENABLE);
28 Assert(p);
29 if (!p)
30 {
31 drprintf((__FUNCTION__":vboxVBVAInformHost: HGSMIHeapAlloc failed\n"));
32 rc = VERR_NO_MEMORY;
33 }
34 else
35 {
36 VBVAENABLE_EX *pEnableEx = (VBVAENABLE_EX *)p;
37 pEnableEx->u32ScreenId = pVbva->srcId;
38
39 VBVAENABLE *pEnable = &pEnableEx->Base;
40 pEnable->u32Flags = bEnable? VBVA_F_ENABLE: VBVA_F_DISABLE;
41 pEnable->u32Flags |= VBVA_F_EXTENDED | VBVA_F_ABSOFFSET;
42 pEnable->u32Offset = (uint32_t)pVbva->offVBVA;
43 pEnable->i32Result = VERR_NOT_SUPPORTED;
44
45 vboxHGSMIBufferSubmit (commonFromDeviceExt(pDevExt), p);
46
47 if (bEnable)
48 {
49 rc = pEnable->i32Result;
50 AssertRC(rc);
51 }
52 else
53 rc = VINF_SUCCESS;
54
55 vboxHGSMIBufferFree (commonFromDeviceExt(pDevExt), p);
56 }
57 return rc;
58}
59
60/*
61 * Public hardware buffer methods.
62 */
63int vboxVbvaEnable (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva)
64{
65 VBVABUFFER *pVBVA = pVbva->pVBVA;
66
67// DISPDBG((1, "VBoxDisp::vboxVbvaEnable screen %p vbva off 0x%x\n", ppdev->pjScreen, ppdev->layout.offVBVABuffer));
68
69 pVBVA->hostFlags.u32HostEvents = 0;
70 pVBVA->hostFlags.u32SupportedOrders = 0;
71 pVBVA->off32Data = 0;
72 pVBVA->off32Free = 0;
73 RtlZeroMemory (pVBVA->aRecords, sizeof (pVBVA->aRecords));
74 pVBVA->indexRecordFirst = 0;
75 pVBVA->indexRecordFree = 0;
76 pVBVA->cbPartialWriteThreshold = 256;
77 pVBVA->cbData = pVbva->cbVBVA - sizeof (VBVABUFFER) + sizeof (pVBVA->au8Data);
78
79 pVbva->fHwBufferOverflow = FALSE;
80 pVbva->pRecord = NULL;
81
82 int rc = vboxVBVAInformHost (pDevExt, pVbva, TRUE);
83 AssertRC(rc);
84
85 if (!RT_SUCCESS(rc))
86 vboxVbvaDisable (pDevExt, pVbva);
87
88 return rc;
89}
90
91int vboxVbvaDisable (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva)
92{
93// DISPDBG((1, "VBoxDisp::vbvaDisable called.\n"));
94
95 pVbva->fHwBufferOverflow = FALSE;
96 pVbva->pRecord = NULL;
97// ppdev->pVBVA = NULL;
98
99 return vboxVBVAInformHost (pDevExt, pVbva, FALSE);
100}
101
102int vboxVbvaCreate(PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva, ULONG offBuffer, ULONG cbBuffer, D3DDDI_VIDEO_PRESENT_SOURCE_ID srcId)
103{
104 memset(pVbva, 0, sizeof(VBOXVBVAINFO));
105
106 KeInitializeSpinLock(&pVbva->Lock);
107
108 int rc = VBoxMapAdapterMemory (commonFromDeviceExt(pDevExt),
109 (void**)&pVbva->pVBVA,
110 offBuffer,
111 cbBuffer);
112 AssertRC(rc);
113 if (RT_SUCCESS(rc))
114 {
115 Assert(pVbva->pVBVA);
116 pVbva->offVBVA = offBuffer;
117 pVbva->cbVBVA = cbBuffer;
118 pVbva->srcId = srcId;
119 }
120
121 return rc;
122}
123
124int vboxVbvaDestroy(PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva)
125{
126 int rc = VINF_SUCCESS;
127 VBoxUnmapAdapterMemory(commonFromDeviceExt(pDevExt), (void**)&pVbva->pVBVA);
128 memset(pVbva, 0, sizeof(VBOXVBVAINFO));
129 return rc;
130}
131
132/*
133 * Private operations.
134 */
135static uint32_t vboxHwBufferAvail (const VBVABUFFER *pVBVA)
136{
137 int32_t i32Diff = pVBVA->off32Data - pVBVA->off32Free;
138
139 return i32Diff > 0? i32Diff: pVBVA->cbData + i32Diff;
140}
141
142static void vboxHwBufferFlush (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva)
143{
144 /* Issue the flush command. */
145 void *p = vboxHGSMIBufferAlloc (commonFromDeviceExt(pDevExt),
146 sizeof (VBVAFLUSH),
147 HGSMI_CH_VBVA,
148 VBVA_FLUSH);
149 Assert(p);
150 if (!p)
151 {
152 drprintf((__FUNCTION__":vboxHwBufferFlush: HGSMIHeapAlloc failed\n"));
153 }
154 else
155 {
156 VBVAFLUSH *pFlush = (VBVAFLUSH *)p;
157
158 pFlush->u32Reserved = 0;
159
160 vboxHGSMIBufferSubmit (commonFromDeviceExt(pDevExt), p);
161
162 vboxHGSMIBufferFree (commonFromDeviceExt(pDevExt), p);
163 }
164
165 return;
166}
167
168static void vboxHwBufferPlaceDataAt (VBVABUFFER *pVBVA, const void *p, uint32_t cb, uint32_t offset)
169{
170 uint32_t u32BytesTillBoundary = pVBVA->cbData - offset;
171 uint8_t *dst = &pVBVA->au8Data[offset];
172 int32_t i32Diff = cb - u32BytesTillBoundary;
173
174 if (i32Diff <= 0)
175 {
176 /* Chunk will not cross buffer boundary. */
177 memcpy (dst, p, cb);
178 }
179 else
180 {
181 /* Chunk crosses buffer boundary. */
182 memcpy (dst, p, u32BytesTillBoundary);
183 memcpy (&pVBVA->au8Data[0], (uint8_t *)p + u32BytesTillBoundary, i32Diff);
184 }
185
186 return;
187}
188
189BOOL vboxVbvaBufferBeginUpdate (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva)
190{
191 BOOL bRc = FALSE;
192
193 // DISPDBG((1, "VBoxDisp::vboxHwBufferBeginUpdate called flags = 0x%08X\n",
194 // ppdev->pVBVA? ppdev->pVBVA->u32HostEvents: -1));
195
196 if ( pVbva->pVBVA
197 && (pVbva->pVBVA->hostFlags.u32HostEvents & VBVA_F_MODE_ENABLED))
198 {
199 uint32_t indexRecordNext;
200
201 Assert (!pVbva->fHwBufferOverflow);
202 Assert (pVbva->pRecord == NULL);
203
204 indexRecordNext = (pVbva->pVBVA->indexRecordFree + 1) % VBVA_MAX_RECORDS;
205
206 if (indexRecordNext == pVbva->pVBVA->indexRecordFirst)
207 {
208 /* All slots in the records queue are used. */
209 vboxHwBufferFlush (pDevExt, pVbva);
210 }
211
212 if (indexRecordNext == pVbva->pVBVA->indexRecordFirst)
213 {
214// /* Even after flush there is no place. Fail the request. */
215// DISPDBG((1, "VBoxDisp::vboxHwBufferBeginUpdate no space in the queue of records!!! first %d, last %d\n",
216// ppdev->pVBVA->indexRecordFirst, ppdev->pVBVA->indexRecordFree));
217 }
218 else
219 {
220 /* Initialize the record. */
221 VBVARECORD *pRecord = &pVbva->pVBVA->aRecords[pVbva->pVBVA->indexRecordFree];
222
223 pRecord->cbRecord = VBVA_F_RECORD_PARTIAL;
224
225 pVbva->pVBVA->indexRecordFree = indexRecordNext;
226
227 // DISPDBG((1, "VBoxDisp::vboxHwBufferBeginUpdate indexRecordNext = %d\n", indexRecordNext));
228
229 /* Remember which record we are using. */
230 pVbva->pRecord = pRecord;
231
232 bRc = TRUE;
233 }
234 }
235
236 return bRc;
237}
238
239void vboxVbvaBufferEndUpdate (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva)
240{
241 VBVARECORD *pRecord;
242
243 // DISPDBG((1, "VBoxDisp::vboxHwBufferEndUpdate called\n"));
244
245 Assert(pVbva->pVBVA);
246
247 pRecord = pVbva->pRecord;
248 Assert (pRecord && (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL));
249
250 /* Mark the record completed. */
251 pRecord->cbRecord &= ~VBVA_F_RECORD_PARTIAL;
252
253 pVbva->fHwBufferOverflow = FALSE;
254 pVbva->pRecord = NULL;
255
256 return;
257}
258
259static int vboxHwBufferWrite (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva, const void *p, uint32_t cb)
260{
261 VBVARECORD *pRecord;
262 uint32_t cbHwBufferAvail;
263
264 uint32_t cbWritten = 0;
265
266 VBVABUFFER *pVBVA = pVbva->pVBVA;
267 Assert(pVBVA);
268
269 if (!pVBVA || pVbva->fHwBufferOverflow)
270 {
271 return VERR_INVALID_STATE;
272 }
273
274 Assert (pVBVA->indexRecordFirst != pVBVA->indexRecordFree);
275
276 pRecord = pVbva->pRecord;
277 Assert (pRecord && (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL));
278
279// dfprintf((__FUNCTION__": VW %d\n", cb));
280
281 cbHwBufferAvail = vboxHwBufferAvail (pVBVA);
282
283 while (cb > 0)
284 {
285 uint32_t cbChunk = cb;
286
287 // DISPDBG((1, "VBoxDisp::vboxHwBufferWrite pVBVA->off32Free %d, pRecord->cbRecord 0x%08X, cbHwBufferAvail %d, cb %d, cbWritten %d\n",
288 // pVBVA->off32Free, pRecord->cbRecord, cbHwBufferAvail, cb, cbWritten));
289
290 if (cbChunk >= cbHwBufferAvail)
291 {
292 dfprintf((__FUNCTION__": 1) avail %d, chunk %d\n", cbHwBufferAvail, cbChunk));
293
294 vboxHwBufferFlush (pDevExt, pVbva);
295
296 cbHwBufferAvail = vboxHwBufferAvail (pVBVA);
297
298 if (cbChunk >= cbHwBufferAvail)
299 {
300 dprintf((__FUNCTION__": no place for %d bytes. Only %d bytes available after flush. Going to partial writes.\n",
301 cb, cbHwBufferAvail));
302
303 if (cbHwBufferAvail <= pVBVA->cbPartialWriteThreshold)
304 {
305 drprintf((__FUNCTION__": Buffer overflow!!!\n"));
306 pVbva->fHwBufferOverflow = TRUE;
307 Assert(FALSE);
308 return VERR_NO_MEMORY;
309 }
310
311 cbChunk = cbHwBufferAvail - pVBVA->cbPartialWriteThreshold;
312 }
313 }
314
315 Assert(cbChunk <= cb);
316 Assert(cbChunk <= vboxHwBufferAvail (pVBVA));
317
318 vboxHwBufferPlaceDataAt (pVbva->pVBVA, (uint8_t *)p + cbWritten, cbChunk, pVBVA->off32Free);
319
320 pVBVA->off32Free = (pVBVA->off32Free + cbChunk) % pVBVA->cbData;
321 pRecord->cbRecord += cbChunk;
322 cbHwBufferAvail -= cbChunk;
323
324 cb -= cbChunk;
325 cbWritten += cbChunk;
326 }
327
328 return VINF_SUCCESS;
329}
330
331/*
332 * Public writer to the hardware buffer.
333 */
334int vboxWrite (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva, const void *pv, uint32_t cb)
335{
336 return vboxHwBufferWrite (pDevExt, pVbva, pv, cb);
337}
338
339
340int vboxVbvaReportDirtyRect (PDEVICE_EXTENSION pDevExt, PVBOXWDDM_SOURCE pSrc, RECT *pRectOrig)
341{
342 VBVACMDHDR hdr;
343
344 RECT rect = *pRectOrig;
345
346// if (rect.left < 0) rect.left = 0;
347// if (rect.top < 0) rect.top = 0;
348// if (rect.right > (int)ppdev->cxScreen) rect.right = ppdev->cxScreen;
349// if (rect.bottom > (int)ppdev->cyScreen) rect.bottom = ppdev->cyScreen;
350
351 hdr.x = (int16_t)rect.left;
352 hdr.y = (int16_t)rect.top;
353 hdr.w = (uint16_t)(rect.right - rect.left);
354 hdr.h = (uint16_t)(rect.bottom - rect.top);
355
356 hdr.x += (int16_t)pSrc->VScreenPos.x;
357 hdr.y += (int16_t)pSrc->VScreenPos.y;
358
359 return vboxWrite (pDevExt, &pSrc->Vbva, &hdr, sizeof(hdr));
360}
361
362#ifdef VBOXVDMA_WITH_VBVA
363int vboxVbvaReportCmdOffset (PDEVICE_EXTENSION pDevExt, VBOXVBVAINFO *pVbva, uint32_t offCmd)
364{
365 VBOXVDMAVBVACMD cmd;
366 cmd.offCmd = offCmd;
367 return vboxWrite (pDevExt, pVbva, &cmd, sizeof(cmd));
368}
369#endif
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