VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Miniport/wddm/VBoxVideoVdma.cpp@ 36046

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

wddm/3d: debugging

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.0 KB
Line 
1/*
2 * Copyright (C) 2010 Oracle Corporation
3 *
4 * This file is part of VirtualBox Open Source Edition (OSE), as
5 * available from http://www.virtualbox.org. This file is free software;
6 * you can redistribute it and/or modify it under the terms of the GNU
7 * General Public License (GPL) as published by the Free Software
8 * Foundation, in version 2 as it comes in the "COPYING" file of the
9 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
10 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
11 */
12
13#include "../VBoxVideo-win.h"
14#include "../Helper.h"
15
16#include <VBox/VBoxGuestLib.h>
17#include <VBox/VBoxVideo.h>
18#include "VBoxVideoVdma.h"
19#include "../VBoxVideo.h"
20
21#include <iprt/asm.h>
22
23NTSTATUS vboxVdmaPipeConstruct(PVBOXVDMAPIPE pPipe)
24{
25 KeInitializeSpinLock(&pPipe->SinchLock);
26 KeInitializeEvent(&pPipe->Event, SynchronizationEvent, FALSE);
27 InitializeListHead(&pPipe->CmdListHead);
28 pPipe->enmState = VBOXVDMAPIPE_STATE_CREATED;
29 pPipe->bNeedNotify = true;
30 return STATUS_SUCCESS;
31}
32
33NTSTATUS vboxVdmaPipeSvrOpen(PVBOXVDMAPIPE pPipe)
34{
35 NTSTATUS Status = STATUS_SUCCESS;
36 KIRQL OldIrql;
37 KeAcquireSpinLock(&pPipe->SinchLock, &OldIrql);
38 Assert(pPipe->enmState == VBOXVDMAPIPE_STATE_CREATED);
39 switch (pPipe->enmState)
40 {
41 case VBOXVDMAPIPE_STATE_CREATED:
42 pPipe->enmState = VBOXVDMAPIPE_STATE_OPENNED;
43 pPipe->bNeedNotify = false;
44 break;
45 case VBOXVDMAPIPE_STATE_OPENNED:
46 pPipe->bNeedNotify = false;
47 break;
48 default:
49 AssertBreakpoint();
50 Status = STATUS_INVALID_PIPE_STATE;
51 break;
52 }
53
54 KeReleaseSpinLock(&pPipe->SinchLock, OldIrql);
55 return Status;
56}
57
58NTSTATUS vboxVdmaPipeSvrClose(PVBOXVDMAPIPE pPipe)
59{
60 NTSTATUS Status = STATUS_SUCCESS;
61 KIRQL OldIrql;
62 KeAcquireSpinLock(&pPipe->SinchLock, &OldIrql);
63 Assert(pPipe->enmState == VBOXVDMAPIPE_STATE_CLOSED
64 || pPipe->enmState == VBOXVDMAPIPE_STATE_CLOSING);
65 switch (pPipe->enmState)
66 {
67 case VBOXVDMAPIPE_STATE_CLOSING:
68 pPipe->enmState = VBOXVDMAPIPE_STATE_CLOSED;
69 break;
70 case VBOXVDMAPIPE_STATE_CLOSED:
71 break;
72 default:
73 AssertBreakpoint();
74 Status = STATUS_INVALID_PIPE_STATE;
75 break;
76 }
77
78 KeReleaseSpinLock(&pPipe->SinchLock, OldIrql);
79 return Status;
80}
81
82NTSTATUS vboxVdmaPipeCltClose(PVBOXVDMAPIPE pPipe)
83{
84 NTSTATUS Status = STATUS_SUCCESS;
85 KIRQL OldIrql;
86 KeAcquireSpinLock(&pPipe->SinchLock, &OldIrql);
87 bool bNeedNotify = false;
88 Assert(pPipe->enmState == VBOXVDMAPIPE_STATE_OPENNED
89 || pPipe->enmState == VBOXVDMAPIPE_STATE_CREATED
90 || pPipe->enmState == VBOXVDMAPIPE_STATE_CLOSED);
91 switch (pPipe->enmState)
92 {
93 case VBOXVDMAPIPE_STATE_OPENNED:
94 pPipe->enmState = VBOXVDMAPIPE_STATE_CLOSING;
95 bNeedNotify = pPipe->bNeedNotify;
96 pPipe->bNeedNotify = false;
97 break;
98 case VBOXVDMAPIPE_STATE_CREATED:
99 pPipe->enmState = VBOXVDMAPIPE_STATE_CLOSED;
100 pPipe->bNeedNotify = false;
101 break;
102 case VBOXVDMAPIPE_STATE_CLOSED:
103 break;
104 default:
105 AssertBreakpoint();
106 Status = STATUS_INVALID_PIPE_STATE;
107 break;
108 }
109
110 KeReleaseSpinLock(&pPipe->SinchLock, OldIrql);
111
112 if (bNeedNotify)
113 {
114 KeSetEvent(&pPipe->Event, 0, FALSE);
115 }
116 return Status;
117}
118
119NTSTATUS vboxVdmaPipeDestruct(PVBOXVDMAPIPE pPipe)
120{
121 Assert(pPipe->enmState == VBOXVDMAPIPE_STATE_CLOSED
122 || pPipe->enmState == VBOXVDMAPIPE_STATE_CREATED);
123 /* ensure the pipe is closed */
124 NTSTATUS Status = vboxVdmaPipeCltClose(pPipe);
125 Assert(Status == STATUS_SUCCESS);
126
127 Assert(pPipe->enmState == VBOXVDMAPIPE_STATE_CLOSED);
128
129 return Status;
130}
131
132NTSTATUS vboxVdmaPipeSvrCmdGetList(PVBOXVDMAPIPE pPipe, PLIST_ENTRY pDetachHead)
133{
134 PLIST_ENTRY pEntry = NULL;
135 KIRQL OldIrql;
136 NTSTATUS Status = STATUS_SUCCESS;
137 VBOXVDMAPIPE_STATE enmState = VBOXVDMAPIPE_STATE_CLOSED;
138 do
139 {
140 bool bListEmpty = true;
141 KeAcquireSpinLock(&pPipe->SinchLock, &OldIrql);
142 Assert(pPipe->enmState == VBOXVDMAPIPE_STATE_OPENNED
143 || pPipe->enmState == VBOXVDMAPIPE_STATE_CLOSING);
144 Assert(pPipe->enmState >= VBOXVDMAPIPE_STATE_OPENNED);
145 enmState = pPipe->enmState;
146 if (enmState >= VBOXVDMAPIPE_STATE_OPENNED)
147 {
148 vboxVideoLeDetach(&pPipe->CmdListHead, pDetachHead);
149 bListEmpty = !!(IsListEmpty(pDetachHead));
150 pPipe->bNeedNotify = bListEmpty;
151 }
152 else
153 {
154 KeReleaseSpinLock(&pPipe->SinchLock, OldIrql);
155 Status = STATUS_INVALID_PIPE_STATE;
156 break;
157 }
158
159 KeReleaseSpinLock(&pPipe->SinchLock, OldIrql);
160
161 if (!bListEmpty)
162 {
163 Assert(Status == STATUS_SUCCESS);
164 break;
165 }
166
167 if (enmState == VBOXVDMAPIPE_STATE_OPENNED)
168 {
169 Status = KeWaitForSingleObject(&pPipe->Event, Executive, KernelMode, FALSE, NULL /* PLARGE_INTEGER Timeout */);
170 Assert(Status == STATUS_SUCCESS);
171 if (Status != STATUS_SUCCESS)
172 break;
173 }
174 else
175 {
176 Assert(enmState == VBOXVDMAPIPE_STATE_CLOSING);
177 Status = STATUS_PIPE_CLOSING;
178 break;
179 }
180 } while (1);
181
182 return Status;
183}
184
185NTSTATUS vboxVdmaPipeCltCmdPut(PVBOXVDMAPIPE pPipe, PVBOXVDMAPIPE_CMD_HDR pCmd)
186{
187 NTSTATUS Status = STATUS_SUCCESS;
188 KIRQL OldIrql;
189 bool bNeedNotify = false;
190
191 KeAcquireSpinLock(&pPipe->SinchLock, &OldIrql);
192
193 Assert(pPipe->enmState == VBOXVDMAPIPE_STATE_OPENNED);
194 if (pPipe->enmState == VBOXVDMAPIPE_STATE_OPENNED)
195 {
196 bNeedNotify = pPipe->bNeedNotify;
197 InsertHeadList(&pPipe->CmdListHead, &pCmd->ListEntry);
198 pPipe->bNeedNotify = false;
199 }
200 else
201 Status = STATUS_INVALID_PIPE_STATE;
202
203 KeReleaseSpinLock(&pPipe->SinchLock, OldIrql);
204
205 if (bNeedNotify)
206 {
207 KeSetEvent(&pPipe->Event, 0, FALSE);
208 }
209
210 return Status;
211}
212
213PVBOXVDMAPIPE_CMD_DR vboxVdmaGgCmdCreate(PVBOXVDMAGG pVdma, VBOXVDMAPIPE_CMD_TYPE enmType, uint32_t cbCmd)
214{
215 PVBOXVDMAPIPE_CMD_DR pHdr = (PVBOXVDMAPIPE_CMD_DR)vboxWddmMemAllocZero(cbCmd);
216 Assert(pHdr);
217 if (pHdr)
218 {
219 pHdr->enmType = enmType;
220 return pHdr;
221 }
222 return NULL;
223}
224
225void vboxVdmaGgCmdDestroy(PVBOXVDMAPIPE_CMD_DR pDr)
226{
227 vboxWddmMemFree(pDr);
228}
229
230DECLCALLBACK(VOID) vboxVdmaGgDdiCmdDestroy(PDEVICE_EXTENSION pDevExt, PVBOXVDMADDI_CMD pCmd, PVOID pvContext)
231{
232 vboxVdmaGgCmdDestroy((PVBOXVDMAPIPE_CMD_DR)pvContext);
233}
234
235/**
236 * helper function used for system thread creation
237 */
238static NTSTATUS vboxVdmaGgThreadCreate(PKTHREAD * ppThread, PKSTART_ROUTINE pStartRoutine, PVOID pStartContext)
239{
240 NTSTATUS fStatus;
241 HANDLE hThread;
242 OBJECT_ATTRIBUTES fObjectAttributes;
243
244 Assert(KeGetCurrentIrql() == PASSIVE_LEVEL);
245
246 InitializeObjectAttributes(&fObjectAttributes, NULL, OBJ_KERNEL_HANDLE,
247 NULL, NULL);
248
249 fStatus = PsCreateSystemThread(&hThread, THREAD_ALL_ACCESS,
250 &fObjectAttributes, NULL, NULL,
251 (PKSTART_ROUTINE) pStartRoutine, pStartContext);
252 if (!NT_SUCCESS(fStatus))
253 return fStatus;
254
255 ObReferenceObjectByHandle(hThread, THREAD_ALL_ACCESS, NULL,
256 KernelMode, (PVOID*) ppThread, NULL);
257 ZwClose(hThread);
258 return STATUS_SUCCESS;
259}
260
261DECLINLINE(void) vboxVdmaDirtyRectsCalcIntersection(const RECT *pArea, const PVBOXWDDM_RECTS_INFO pRects, PVBOXWDDM_RECTS_INFO pResult)
262{
263 uint32_t cRects = 0;
264 for (uint32_t i = 0; i < pRects->cRects; ++i)
265 {
266 if (vboxWddmRectIntersection(pArea, &pRects->aRects[i], &pResult->aRects[cRects]))
267 {
268 ++cRects;
269 }
270 }
271
272 pResult->cRects = cRects;
273}
274
275DECLINLINE(bool) vboxVdmaDirtyRectsHasIntersections(const RECT *paRects1, uint32_t cRects1, const RECT *paRects2, uint32_t cRects2)
276{
277 RECT tmpRect;
278 for (uint32_t i = 0; i < cRects1; ++i)
279 {
280 const RECT * pRect1 = &paRects1[i];
281 for (uint32_t j = 0; j < cRects2; ++j)
282 {
283 const RECT * pRect2 = &paRects2[j];
284 if (vboxWddmRectIntersection(pRect1, pRect2, &tmpRect))
285 return true;
286 }
287 }
288 return false;
289}
290
291DECLINLINE(bool) vboxVdmaDirtyRectsIsCover(const RECT *paRects, uint32_t cRects, const RECT *paRectsCovered, uint32_t cRectsCovered)
292{
293 for (uint32_t i = 0; i < cRectsCovered; ++i)
294 {
295 const RECT * pRectCovered = &paRectsCovered[i];
296 uint32_t j = 0;
297 for (; j < cRects; ++j)
298 {
299 const RECT * pRect = &paRects[j];
300 if (vboxWddmRectIsCoveres(pRect, pRectCovered))
301 break;
302 }
303 if (j == cRects)
304 return false;
305 }
306 return true;
307}
308
309NTSTATUS vboxVdmaPostHideSwapchain(PVBOXWDDM_SWAPCHAIN pSwapchain)
310{
311 Assert(KeGetCurrentIrql() < DISPATCH_LEVEL);
312 uint32_t cbCmdInternal = VBOXVIDEOCM_CMD_RECTS_INTERNAL_SIZE4CRECTS(0);
313 PVBOXVIDEOCM_CMD_RECTS_INTERNAL pCmdInternal =
314 (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)vboxVideoCmCmdCreate(&pSwapchain->pContext->CmContext, cbCmdInternal);
315 Assert(pCmdInternal);
316 if (pCmdInternal)
317 {
318 pCmdInternal->hSwapchainUm = pSwapchain->hSwapchainUm;
319 pCmdInternal->Cmd.fFlags.Value = 0;
320 pCmdInternal->Cmd.fFlags.bAddHiddenRects = 1;
321 pCmdInternal->Cmd.fFlags.bHide = 1;
322 pCmdInternal->Cmd.RectsInfo.cRects = 0;
323 vboxVideoCmCmdSubmit(pCmdInternal, VBOXVIDEOCM_SUBMITSIZE_DEFAULT);
324 return STATUS_SUCCESS;
325 }
326 return STATUS_NO_MEMORY;
327}
328
329/**
330 * @param pDevExt
331 */
332static NTSTATUS vboxVdmaGgDirtyRectsProcess(PDEVICE_EXTENSION pDevExt, PVBOXWDDM_CONTEXT pContext, PVBOXWDDM_SWAPCHAIN pSwapchain, VBOXVDMAPIPE_RECTS *pContextRects)
333{
334 PVBOXWDDM_RECTS_INFO pRects = &pContextRects->UpdateRects;
335 NTSTATUS Status = STATUS_SUCCESS;
336 PVBOXVIDEOCM_CMD_RECTS_INTERNAL pCmdInternal = NULL;
337 uint32_t cbCmdInternal = VBOXVIDEOCM_CMD_RECTS_INTERNAL_SIZE4CRECTS(pRects->cRects);
338 Assert(KeGetCurrentIrql() < DISPATCH_LEVEL);
339 ExAcquireFastMutex(&pDevExt->ContextMutex);
340 for (PLIST_ENTRY pCur = pDevExt->SwapchainList3D.Flink; pCur != &pDevExt->SwapchainList3D; pCur = pCur->Flink)
341 {
342 if (pCur != &pSwapchain->DevExtListEntry)
343 {
344 PVBOXWDDM_SWAPCHAIN pCurSwapchain = VBOXWDDMENTRY_2_SWAPCHAIN(pCur);
345 if (!pCmdInternal)
346 {
347 pCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)vboxVideoCmCmdCreate(&pCurSwapchain->pContext->CmContext, cbCmdInternal);
348 Assert(pCmdInternal);
349 if (!pCmdInternal)
350 {
351 Status = STATUS_NO_MEMORY;
352 break;
353 }
354 }
355 else
356 {
357 pCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)vboxVideoCmCmdReinitForContext(pCmdInternal, &pCurSwapchain->pContext->CmContext);
358 }
359
360 vboxVdmaDirtyRectsCalcIntersection(&pCurSwapchain->ViewRect, pRects, &pCmdInternal->Cmd.RectsInfo);
361 if (pCmdInternal->Cmd.RectsInfo.cRects)
362 {
363 bool bSend = false;
364 pCmdInternal->Cmd.fFlags.Value = 0;
365 pCmdInternal->Cmd.fFlags.bAddHiddenRects = 1;
366 if (pCurSwapchain->pLastReportedRects)
367 {
368 if (pCurSwapchain->pLastReportedRects->Cmd.fFlags.bSetVisibleRects)
369 {
370 RECT *paPrevRects;
371 uint32_t cPrevRects;
372 if (pCurSwapchain->pLastReportedRects->Cmd.fFlags.bSetViewRect)
373 {
374 paPrevRects = &pCurSwapchain->pLastReportedRects->Cmd.RectsInfo.aRects[1];
375 cPrevRects = pCurSwapchain->pLastReportedRects->Cmd.RectsInfo.cRects - 1;
376 }
377 else
378 {
379 paPrevRects = &pCurSwapchain->pLastReportedRects->Cmd.RectsInfo.aRects[0];
380 cPrevRects = pCurSwapchain->pLastReportedRects->Cmd.RectsInfo.cRects;
381 }
382
383 if (vboxVdmaDirtyRectsHasIntersections(paPrevRects, cPrevRects,
384 pCmdInternal->Cmd.RectsInfo.aRects, pCmdInternal->Cmd.RectsInfo.cRects))
385 {
386 bSend = true;
387 }
388 }
389 else
390 {
391 Assert(pCurSwapchain->pLastReportedRects->Cmd.fFlags.bAddHiddenRects);
392 if (!vboxVdmaDirtyRectsIsCover(pCurSwapchain->pLastReportedRects->Cmd.RectsInfo.aRects,
393 pCurSwapchain->pLastReportedRects->Cmd.RectsInfo.cRects,
394 pCmdInternal->Cmd.RectsInfo.aRects, pCmdInternal->Cmd.RectsInfo.cRects))
395 {
396 bSend = true;
397 }
398 }
399 }
400 else
401 bSend = true;
402
403 if (bSend)
404 {
405 if (pCurSwapchain->pLastReportedRects)
406 vboxVideoCmCmdRelease(pCurSwapchain->pLastReportedRects);
407
408 pCmdInternal->hSwapchainUm = pCurSwapchain->hSwapchainUm;
409
410 vboxVideoCmCmdRetain(pCmdInternal);
411 pCurSwapchain->pLastReportedRects = pCmdInternal;
412 vboxVideoCmCmdSubmit(pCmdInternal, VBOXVIDEOCM_CMD_RECTS_INTERNAL_SIZE4CRECTS(pCmdInternal->Cmd.RectsInfo.cRects));
413 pCmdInternal = NULL;
414 }
415 }
416 }
417 else
418 {
419 RECT * pContextRect = &pContextRects->ContextRect;
420 bool bRectShanged = (pSwapchain->ViewRect.left != pContextRect->left
421 || pSwapchain->ViewRect.top != pContextRect->top
422 || pSwapchain->ViewRect.right != pContextRect->right
423 || pSwapchain->ViewRect.bottom != pContextRect->bottom);
424 PVBOXVIDEOCM_CMD_RECTS_INTERNAL pDrCmdInternal;
425
426 bool bSend = false;
427
428 if (bRectShanged)
429 {
430 uint32_t cbDrCmdInternal = VBOXVIDEOCM_CMD_RECTS_INTERNAL_SIZE4CRECTS(pRects->cRects + 1);
431 pDrCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)vboxVideoCmCmdCreate(&pContext->CmContext, cbDrCmdInternal);
432 Assert(pDrCmdInternal);
433 if (!pDrCmdInternal)
434 {
435 Status = STATUS_NO_MEMORY;
436 break;
437 }
438 pDrCmdInternal->Cmd.fFlags.Value = 0;
439 pDrCmdInternal->Cmd.RectsInfo.cRects = pRects->cRects + 1;
440 pDrCmdInternal->Cmd.fFlags.bSetViewRect = 1;
441 pDrCmdInternal->Cmd.RectsInfo.aRects[0] = *pContextRect;
442 pSwapchain->ViewRect = *pContextRect;
443 memcpy(&pDrCmdInternal->Cmd.RectsInfo.aRects[1], pRects->aRects, sizeof (RECT) * pRects->cRects);
444 bSend = true;
445 }
446 else
447 {
448 if (pCmdInternal)
449 {
450 pDrCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)vboxVideoCmCmdReinitForContext(pCmdInternal, &pContext->CmContext);
451 pCmdInternal = NULL;
452 }
453 else
454 {
455 pDrCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)vboxVideoCmCmdCreate(&pContext->CmContext, cbCmdInternal);
456 Assert(pDrCmdInternal);
457 if (!pDrCmdInternal)
458 {
459 Status = STATUS_NO_MEMORY;
460 break;
461 }
462 }
463 pDrCmdInternal->Cmd.fFlags.Value = 0;
464 pDrCmdInternal->Cmd.RectsInfo.cRects = pRects->cRects;
465 memcpy(&pDrCmdInternal->Cmd.RectsInfo.aRects[0], pRects->aRects, sizeof (RECT) * pRects->cRects);
466
467 if (pSwapchain->pLastReportedRects)
468 {
469 if (pSwapchain->pLastReportedRects->Cmd.fFlags.bSetVisibleRects)
470 {
471 RECT *paRects;
472 uint32_t cRects;
473 if (pSwapchain->pLastReportedRects->Cmd.fFlags.bSetViewRect)
474 {
475 paRects = &pSwapchain->pLastReportedRects->Cmd.RectsInfo.aRects[1];
476 cRects = pSwapchain->pLastReportedRects->Cmd.RectsInfo.cRects - 1;
477 }
478 else
479 {
480 paRects = &pSwapchain->pLastReportedRects->Cmd.RectsInfo.aRects[0];
481 cRects = pSwapchain->pLastReportedRects->Cmd.RectsInfo.cRects;
482 }
483 bSend = (pDrCmdInternal->Cmd.RectsInfo.cRects != cRects)
484 || memcmp(paRects, pDrCmdInternal->Cmd.RectsInfo.aRects, cRects * sizeof (RECT));
485 }
486 else
487 {
488 Assert(pSwapchain->pLastReportedRects->Cmd.fFlags.bAddHiddenRects);
489 bSend = true;
490 }
491 }
492 else
493 bSend = true;
494
495 }
496
497 Assert(pRects->cRects);
498 if (bSend)
499 {
500 if (pSwapchain->pLastReportedRects)
501 vboxVideoCmCmdRelease(pSwapchain->pLastReportedRects);
502
503 pDrCmdInternal->Cmd.fFlags.bSetVisibleRects = 1;
504 pDrCmdInternal->hSwapchainUm = pSwapchain->hSwapchainUm;
505
506 vboxVideoCmCmdRetain(pDrCmdInternal);
507 pSwapchain->pLastReportedRects = pDrCmdInternal;
508 vboxVideoCmCmdSubmit(pDrCmdInternal, VBOXVIDEOCM_SUBMITSIZE_DEFAULT);
509 }
510 else
511 {
512 if (!pCmdInternal)
513 pCmdInternal = pDrCmdInternal;
514 else
515 vboxVideoCmCmdRelease(pDrCmdInternal);
516 }
517 }
518 }
519 ExReleaseFastMutex(&pDevExt->ContextMutex);
520
521
522 if (pCmdInternal)
523 vboxVideoCmCmdRelease(pCmdInternal);
524
525 return Status;
526}
527
528static NTSTATUS vboxVdmaGgDmaColorFill(PVBOXVDMAPIPE_CMD_DMACMD_CLRFILL pCF)
529{
530 NTSTATUS Status = STATUS_UNSUCCESSFUL;
531 PVBOXWDDM_CONTEXT pContext = pCF->Hdr.DdiCmd.pContext;
532 PDEVICE_EXTENSION pDevExt = pContext->pDevice->pAdapter;
533 Assert (pDevExt->pvVisibleVram);
534 if (pDevExt->pvVisibleVram)
535 {
536 PVBOXWDDM_ALLOCATION pAlloc = pCF->ClrFill.Alloc.pAlloc;
537 Assert(pAlloc->offVram != VBOXVIDEOOFFSET_VOID);
538 if (pAlloc->offVram != VBOXVIDEOOFFSET_VOID)
539 {
540 RECT UnionRect = {0};
541 uint8_t *pvMem = pDevExt->pvVisibleVram + pAlloc->offVram;
542 UINT bpp = pAlloc->SurfDesc.bpp;
543 Assert(bpp);
544 Assert(((bpp * pAlloc->SurfDesc.width) >> 3) == pAlloc->SurfDesc.pitch);
545 switch (bpp)
546 {
547 case 32:
548 {
549 uint8_t bytestPP = bpp >> 3;
550 for (UINT i = 0; i < pCF->ClrFill.Rects.cRects; ++i)
551 {
552 RECT *pRect = &pCF->ClrFill.Rects.aRects[i];
553 for (LONG ir = pRect->top; ir < pRect->bottom; ++ir)
554 {
555 uint32_t * pvU32Mem = (uint32_t*)(pvMem + (ir * pAlloc->SurfDesc.pitch) + (pRect->left * bytestPP));
556 uint32_t cRaw = pRect->right - pRect->left;
557 Assert(pRect->left >= 0);
558 Assert(pRect->right <= (LONG)pAlloc->SurfDesc.width);
559 Assert(pRect->top >= 0);
560 Assert(pRect->bottom <= (LONG)pAlloc->SurfDesc.height);
561 for (UINT j = 0; j < cRaw; ++j)
562 {
563 *pvU32Mem = pCF->ClrFill.Color;
564 ++pvU32Mem;
565 }
566 }
567 vboxWddmRectUnited(&UnionRect, &UnionRect, pRect);
568 }
569 Status = STATUS_SUCCESS;
570 break;
571 }
572 case 16:
573 case 8:
574 default:
575 AssertBreakpoint();
576 break;
577 }
578
579 if (Status == STATUS_SUCCESS)
580 {
581 if (pAlloc->SurfDesc.VidPnSourceId != D3DDDI_ID_UNINITIALIZED
582 && pAlloc->bAssigned
583#if 0//def VBOXWDDM_RENDER_FROM_SHADOW
584 && pAlloc->enmType == VBOXWDDM_ALLOC_TYPE_STD_SHADOWSURFACE
585#else
586 && pAlloc->enmType == VBOXWDDM_ALLOC_TYPE_STD_SHAREDPRIMARYSURFACE
587#endif
588 )
589 {
590 if (!vboxWddmRectIsEmpty(&UnionRect))
591 {
592 PVBOXWDDM_SOURCE pSource = &pDevExt->aSources[pCF->ClrFill.Alloc.pAlloc->SurfDesc.VidPnSourceId];
593 VBOXVBVA_OP_WITHLOCK(ReportDirtyRect, pDevExt, pSource, &UnionRect);
594 }
595 }
596 else
597 {
598 AssertBreakpoint();
599 }
600 }
601 }
602 }
603
604 return Status;
605}
606
607NTSTATUS vboxVdmaGgDmaBltPerform(PDEVICE_EXTENSION pDevExt, PVBOXWDDM_ALLOCATION pSrcAlloc, RECT* pSrcRect,
608 PVBOXWDDM_ALLOCATION pDstAlloc, RECT* pDstRect)
609{
610 uint8_t* pvVramBase = pDevExt->pvVisibleVram;
611 /* we do not support color conversion */
612 Assert(pSrcAlloc->SurfDesc.format == pDstAlloc->SurfDesc.format);
613 /* we do not support stretching */
614 uint32_t srcWidth = pSrcRect->right - pSrcRect->left;
615 uint32_t srcHeight = pSrcRect->bottom - pSrcRect->top;
616 uint32_t dstWidth = pDstRect->right - pDstRect->left;
617 uint32_t dstHeight = pDstRect->bottom - pDstRect->top;
618 Assert(srcHeight == dstHeight);
619 Assert(dstWidth == srcWidth);
620 Assert(pDstAlloc->offVram != VBOXVIDEOOFFSET_VOID);
621 Assert(pSrcAlloc->offVram != VBOXVIDEOOFFSET_VOID);
622
623 if (pSrcAlloc->SurfDesc.format != pDstAlloc->SurfDesc.format)
624 return STATUS_INVALID_PARAMETER;
625 if (srcHeight != dstHeight)
626 return STATUS_INVALID_PARAMETER;
627 if (srcWidth != dstWidth)
628 return STATUS_INVALID_PARAMETER;
629 if (pDstAlloc->offVram == VBOXVIDEOOFFSET_VOID)
630 return STATUS_INVALID_PARAMETER;
631 if (pSrcAlloc->offVram == VBOXVIDEOOFFSET_VOID)
632 return STATUS_INVALID_PARAMETER;
633
634 uint8_t *pvDstSurf = pvVramBase + pDstAlloc->offVram;
635 uint8_t *pvSrcSurf = pvVramBase + pSrcAlloc->offVram;
636
637 if (pDstAlloc->SurfDesc.width == dstWidth
638 && pSrcAlloc->SurfDesc.width == srcWidth
639 && pSrcAlloc->SurfDesc.width == pDstAlloc->SurfDesc.width)
640 {
641 Assert(!pDstRect->left);
642 Assert(!pSrcRect->left);
643 uint32_t cbOff = pDstAlloc->SurfDesc.pitch * pDstRect->top;
644 uint32_t cbSize = pDstAlloc->SurfDesc.pitch * dstHeight;
645 memcpy(pvDstSurf + cbOff, pvSrcSurf + cbOff, cbSize);
646 }
647 else
648 {
649 uint32_t offDstLineStart = pDstRect->left * pDstAlloc->SurfDesc.bpp >> 3;
650 uint32_t offDstLineEnd = ((pDstRect->left * pDstAlloc->SurfDesc.bpp + 7) >> 3) + ((pDstAlloc->SurfDesc.bpp * dstWidth + 7) >> 3);
651 uint32_t cbDstLine = offDstLineEnd - offDstLineStart;
652 uint32_t offDstStart = pDstAlloc->SurfDesc.pitch * pDstRect->top + offDstLineStart;
653 Assert(cbDstLine <= pDstAlloc->SurfDesc.pitch);
654 uint32_t cbDstSkip = pDstAlloc->SurfDesc.pitch;
655 uint8_t * pvDstStart = pvDstSurf + offDstStart;
656
657 uint32_t offSrcLineStart = pSrcRect->left * pSrcAlloc->SurfDesc.bpp >> 3;
658 uint32_t offSrcLineEnd = ((pSrcRect->left * pSrcAlloc->SurfDesc.bpp + 7) >> 3) + ((pSrcAlloc->SurfDesc.bpp * srcWidth + 7) >> 3);
659 uint32_t cbSrcLine = offSrcLineEnd - offSrcLineStart;
660 uint32_t offSrcStart = pSrcAlloc->SurfDesc.pitch * pSrcRect->top + offSrcLineStart;
661 Assert(cbSrcLine <= pSrcAlloc->SurfDesc.pitch);
662 uint32_t cbSrcSkip = pSrcAlloc->SurfDesc.pitch;
663 const uint8_t * pvSrcStart = pvSrcSurf + offSrcStart;
664
665 Assert(cbDstLine == cbSrcLine);
666
667 for (uint32_t i = 0; ; ++i)
668 {
669 memcpy (pvDstStart, pvSrcStart, cbDstLine);
670 if (i == dstHeight)
671 break;
672 pvDstStart += cbDstSkip;
673 pvSrcStart += cbSrcSkip;
674 }
675 }
676 return STATUS_SUCCESS;
677}
678
679/*
680 * @return on success the number of bytes the command contained, otherwise - VERR_xxx error code
681 */
682static NTSTATUS vboxVdmaGgDmaBlt(PVBOXVDMAPIPE_CMD_DMACMD_BLT pBlt)
683{
684 /* we do not support stretching for now */
685 Assert(pBlt->Blt.SrcRect.right - pBlt->Blt.SrcRect.left == pBlt->Blt.DstRects.ContextRect.right - pBlt->Blt.DstRects.ContextRect.left);
686 Assert(pBlt->Blt.SrcRect.bottom - pBlt->Blt.SrcRect.top == pBlt->Blt.DstRects.ContextRect.bottom - pBlt->Blt.DstRects.ContextRect.top);
687 if (pBlt->Blt.SrcRect.right - pBlt->Blt.SrcRect.left != pBlt->Blt.DstRects.ContextRect.right - pBlt->Blt.DstRects.ContextRect.left)
688 return STATUS_INVALID_PARAMETER;
689 if (pBlt->Blt.SrcRect.bottom - pBlt->Blt.SrcRect.top != pBlt->Blt.DstRects.ContextRect.bottom - pBlt->Blt.DstRects.ContextRect.top)
690 return STATUS_INVALID_PARAMETER;
691 Assert(pBlt->Blt.DstRects.UpdateRects.cRects);
692
693 NTSTATUS Status = STATUS_SUCCESS;
694 PDEVICE_EXTENSION pDevExt = pBlt->Hdr.pDevExt;
695
696 if (pBlt->Blt.DstRects.UpdateRects.cRects)
697 {
698 for (uint32_t i = 0; i < pBlt->Blt.DstRects.UpdateRects.cRects; ++i)
699 {
700 RECT DstRect;
701 RECT SrcRect;
702 vboxWddmRectTranslated(&DstRect, &pBlt->Blt.DstRects.UpdateRects.aRects[i], pBlt->Blt.DstRects.ContextRect.left, pBlt->Blt.DstRects.ContextRect.top);
703 vboxWddmRectTranslated(&SrcRect, &pBlt->Blt.DstRects.UpdateRects.aRects[i], pBlt->Blt.SrcRect.left, pBlt->Blt.SrcRect.top);
704
705 Status = vboxVdmaGgDmaBltPerform(pDevExt, pBlt->Blt.SrcAlloc.pAlloc, &SrcRect,
706 pBlt->Blt.DstAlloc.pAlloc, &DstRect);
707 Assert(Status == STATUS_SUCCESS);
708 if (Status != STATUS_SUCCESS)
709 return Status;
710 }
711 }
712 else
713 {
714 Status = vboxVdmaGgDmaBltPerform(pDevExt, pBlt->Blt.SrcAlloc.pAlloc, &pBlt->Blt.SrcRect,
715 pBlt->Blt.DstAlloc.pAlloc, &pBlt->Blt.DstRects.ContextRect);
716 Assert(Status == STATUS_SUCCESS);
717 if (Status != STATUS_SUCCESS)
718 return Status;
719 }
720
721 return Status;
722}
723
724static VOID vboxWddmBltPipeRectsTranslate(VBOXVDMAPIPE_RECTS *pRects, int x, int y)
725{
726 vboxWddmRectTranslate(&pRects->ContextRect, x, y);
727
728 for (UINT i = 0; i < pRects->UpdateRects.cRects; ++i)
729 {
730 vboxWddmRectTranslate(&pRects->UpdateRects.aRects[i], x, y);
731 }
732}
733
734static NTSTATUS vboxVdmaGgDmaCmdProcess(VBOXVDMAPIPE_CMD_DMACMD *pDmaCmd)
735{
736 PDEVICE_EXTENSION pDevExt = pDmaCmd->pDevExt;
737 PVBOXWDDM_CONTEXT pContext = pDmaCmd->DdiCmd.pContext;
738 NTSTATUS Status = STATUS_SUCCESS;
739 DXGK_INTERRUPT_TYPE enmComplType = DXGK_INTERRUPT_DMA_COMPLETED;
740 switch (pDmaCmd->enmCmd)
741 {
742 case VBOXVDMACMD_TYPE_DMA_PRESENT_BLT:
743 {
744 PVBOXVDMAPIPE_CMD_DMACMD_BLT pBlt = (PVBOXVDMAPIPE_CMD_DMACMD_BLT)pDmaCmd;
745 PVBOXWDDM_ALLOCATION pDstAlloc = pBlt->Blt.DstAlloc.pAlloc;
746 PVBOXWDDM_ALLOCATION pSrcAlloc = pBlt->Blt.SrcAlloc.pAlloc;
747 BOOLEAN bComplete = TRUE;
748 switch (pDstAlloc->enmType)
749 {
750 case VBOXWDDM_ALLOC_TYPE_STD_SHAREDPRIMARYSURFACE:
751 case VBOXWDDM_ALLOC_TYPE_UMD_RC_GENERIC:
752 {
753 if (pDstAlloc->bAssigned)
754 {
755 VBOXWDDM_SOURCE *pSource = &pDevExt->aSources[pDstAlloc->SurfDesc.VidPnSourceId];
756 Assert(pSource->pPrimaryAllocation == pDstAlloc);
757 switch (pSrcAlloc->enmType)
758 {
759 case VBOXWDDM_ALLOC_TYPE_STD_SHADOWSURFACE:
760 {
761 Assert(pContext->enmType == VBOXWDDM_CONTEXT_TYPE_SYSTEM);
762
763 if (pBlt->Hdr.fFlags.b2DRelated || pBlt->Hdr.fFlags.b3DRelated)
764 {
765 POINT pos;
766 BOOLEAN bPosMoved = FALSE;
767 if (pBlt->Hdr.fFlags.b3DRelated)
768 {
769 pos = pSource->VScreenPos;
770 if (pos.x || pos.y)
771 {
772 vboxWddmBltPipeRectsTranslate(&pBlt->Blt.DstRects, pos.x, pos.y);
773 bPosMoved = TRUE;
774 }
775 Status = vboxVdmaGgDirtyRectsProcess(pDevExt, pContext, NULL, &pBlt->Blt.DstRects);
776 Assert(Status == STATUS_SUCCESS);
777 }
778
779
780 if (pBlt->Hdr.fFlags.b2DRelated)
781 {
782 if (bPosMoved)
783 {
784 vboxWddmBltPipeRectsTranslate(&pBlt->Blt.DstRects, -pos.x, -pos.y);
785 }
786
787 RECT OverlayUnionRect;
788 RECT UpdateRect;
789 UpdateRect = pBlt->Blt.DstRects.UpdateRects.aRects[0];
790 for (UINT i = 1; i < pBlt->Blt.DstRects.UpdateRects.cRects; ++i)
791 {
792 vboxWddmRectUnite(&UpdateRect, &pBlt->Blt.DstRects.UpdateRects.aRects[i]);
793 }
794 vboxVhwaHlpOverlayDstRectUnion(pDevExt, pDstAlloc->SurfDesc.VidPnSourceId, &OverlayUnionRect);
795 Assert(pBlt->Blt.DstRects.ContextRect.left == 0); /* <-| otherwise we would probably need to translate the UpdateRects to left;top first??*/
796 Assert(pBlt->Blt.DstRects.ContextRect.top == 0); /* <--| */
797 vboxVdmaDirtyRectsCalcIntersection(&OverlayUnionRect, &pBlt->Blt.DstRects.UpdateRects, &pBlt->Blt.DstRects.UpdateRects);
798 if (pBlt->Blt.DstRects.UpdateRects.cRects)
799 {
800 vboxVdmaGgDmaBlt(pBlt);
801 }
802 VBOXVBVA_OP_WITHLOCK(ReportDirtyRect, pDevExt, pSource, &UpdateRect);
803 }
804 }
805
806 break;
807 }
808 case VBOXWDDM_ALLOC_TYPE_UMD_RC_GENERIC:
809 {
810 Assert(pContext->enmType == VBOXWDDM_CONTEXT_TYPE_CUSTOM_3D);
811 Assert(pSrcAlloc->fRcFlags.RenderTarget);
812 if (pSrcAlloc->fRcFlags.RenderTarget)
813 {
814 if (pBlt->Hdr.fFlags.b3DRelated)
815 {
816 PVBOXWDDM_SWAPCHAIN pSwapchain;
817 pSwapchain = vboxWddmSwapchainRetainByAlloc(pDevExt, pSrcAlloc);
818 if (pSwapchain)
819 {
820 Status = vboxVdmaGgDirtyRectsProcess(pDevExt, pContext, pSwapchain, &pBlt->Blt.DstRects);
821 Assert(Status == STATUS_SUCCESS);
822 vboxWddmSwapchainRelease(pSwapchain);
823 }
824 }
825 }
826 break;
827 }
828 default:
829 AssertBreakpoint();
830 break;
831 }
832 }
833 break;
834 }
835 case VBOXWDDM_ALLOC_TYPE_STD_SHADOWSURFACE:
836 {
837 if (pBlt->Hdr.fFlags.b2DRelated)
838 {
839 RECT OverlayUnionRect;
840 vboxVhwaHlpOverlayDstRectUnion(pDevExt, pDstAlloc->SurfDesc.VidPnSourceId, &OverlayUnionRect);
841 Assert(pBlt->Blt.DstRects.ContextRect.left == 0); /* <-| otherwise we would probably need to translate the UpdateRects to left;top first??*/
842 Assert(pBlt->Blt.DstRects.ContextRect.top == 0); /* <--| */
843 vboxVdmaDirtyRectsCalcIntersection(&OverlayUnionRect, &pBlt->Blt.DstRects.UpdateRects, &pBlt->Blt.DstRects.UpdateRects);
844 if (pBlt->Blt.DstRects.UpdateRects.cRects)
845 {
846 vboxVdmaGgDmaBlt(pBlt);
847 }
848 }
849 else
850 {
851 Assert(0);
852 }
853 break;
854 }
855 default:
856 Assert(0);
857 }
858
859 break;
860 }
861 case VBOXVDMACMD_TYPE_DMA_PRESENT_FLIP:
862 {
863 PVBOXVDMAPIPE_CMD_DMACMD_FLIP pFlip = (PVBOXVDMAPIPE_CMD_DMACMD_FLIP)pDmaCmd;
864 Assert(pFlip->Hdr.fFlags.b3DRelated);
865 Assert(!pFlip->Hdr.fFlags.bDecVBVAUnlock);
866 Assert(!pFlip->Hdr.fFlags.b2DRelated);
867 PVBOXWDDM_ALLOCATION pAlloc = pFlip->Flip.Alloc.pAlloc;
868 VBOXWDDM_SOURCE *pSource = &pDevExt->aSources[pAlloc->SurfDesc.VidPnSourceId];
869 if (pFlip->Hdr.fFlags.b3DRelated)
870 {
871 PVBOXWDDM_SWAPCHAIN pSwapchain;
872 pSwapchain = vboxWddmSwapchainRetainByAlloc(pDevExt, pAlloc);
873 if (pSwapchain)
874 {
875 POINT pos = pSource->VScreenPos;
876 VBOXVDMAPIPE_RECTS Rects;
877 Rects.ContextRect.left = pos.x;
878 Rects.ContextRect.top = pos.y;
879 Rects.ContextRect.right = pAlloc->SurfDesc.width + pos.x;
880 Rects.ContextRect.bottom = pAlloc->SurfDesc.height + pos.y;
881 Rects.UpdateRects.cRects = 1;
882 Rects.UpdateRects.aRects[0] = Rects.ContextRect;
883 Status = vboxVdmaGgDirtyRectsProcess(pDevExt, pContext, pSwapchain, &Rects);
884 Assert(Status == STATUS_SUCCESS);
885 vboxWddmSwapchainRelease(pSwapchain);
886 }
887 }
888
889 break;
890 }
891 case VBOXVDMACMD_TYPE_DMA_PRESENT_CLRFILL:
892 {
893 PVBOXVDMAPIPE_CMD_DMACMD_CLRFILL pCF = (PVBOXVDMAPIPE_CMD_DMACMD_CLRFILL)pDmaCmd;
894 Assert(pCF->Hdr.fFlags.b2DRelated);
895 Assert(pCF->Hdr.fFlags.bDecVBVAUnlock);
896 Assert(!pCF->Hdr.fFlags.b3DRelated);
897 Status = vboxVdmaGgDmaColorFill(pCF);
898 Assert(Status == STATUS_SUCCESS);
899 break;
900 }
901 default:
902 Assert(0);
903 break;
904 }
905
906 if (pDmaCmd->fFlags.bDecVBVAUnlock)
907 {
908 uint32_t cNew = ASMAtomicDecU32(&pDevExt->cUnlockedVBVADisabled);
909 Assert(cNew < UINT32_MAX/2);
910 }
911
912 Status = vboxVdmaDdiCmdCompleted(pDevExt, &pDevExt->DdiCmdQueue, &pDmaCmd->DdiCmd, enmComplType);
913 Assert(Status == STATUS_SUCCESS);
914
915 return Status;
916}
917
918static VOID vboxVdmaGgWorkerThread(PVOID pvUser)
919{
920 PVBOXVDMAGG pVdma = (PVBOXVDMAGG)pvUser;
921
922 NTSTATUS Status = vboxVdmaPipeSvrOpen(&pVdma->CmdPipe);
923 Assert(Status == STATUS_SUCCESS);
924 if (Status == STATUS_SUCCESS)
925 {
926 do
927 {
928 LIST_ENTRY CmdList;
929 Status = vboxVdmaPipeSvrCmdGetList(&pVdma->CmdPipe, &CmdList);
930 Assert(Status == STATUS_SUCCESS || Status == STATUS_PIPE_CLOSING);
931 if (Status == STATUS_SUCCESS)
932 {
933 for (PLIST_ENTRY pCur = CmdList.Blink; pCur != &CmdList;)
934 {
935 PVBOXVDMAPIPE_CMD_DR pDr = VBOXVDMAPIPE_CMD_DR_FROM_ENTRY(pCur);
936 RemoveEntryList(pCur);
937 pCur = CmdList.Blink;
938 switch (pDr->enmType)
939 {
940#if 0
941 case VBOXVDMAPIPE_CMD_TYPE_RECTSINFO:
942 {
943 PVBOXVDMAPIPE_CMD_RECTSINFO pRects = (PVBOXVDMAPIPE_CMD_RECTSINFO)pDr;
944 Status = vboxVdmaGgDirtyRectsProcess(pRects);
945 Assert(Status == STATUS_SUCCESS);
946 vboxVdmaGgCmdDestroy(pDr);
947 break;
948 }
949#endif
950 case VBOXVDMAPIPE_CMD_TYPE_DMACMD:
951 {
952 PVBOXVDMAPIPE_CMD_DMACMD pDmaCmd = (PVBOXVDMAPIPE_CMD_DMACMD)pDr;
953 Status = vboxVdmaGgDmaCmdProcess(pDmaCmd);
954 Assert(Status == STATUS_SUCCESS);
955 } break;
956 case VBOXVDMAPIPE_CMD_TYPE_RECTSINFO:
957 {
958 PVBOXVDMAPIPE_CMD_RECTSINFO pRects = (PVBOXVDMAPIPE_CMD_RECTSINFO)pDr;
959 Status = vboxVdmaGgDirtyRectsProcess(pRects->pDevExt, pRects->pContext, pRects->pSwapchain, &pRects->ContextsRects);
960 Assert(Status == STATUS_SUCCESS);
961 vboxVdmaGgCmdDestroy(pDr);
962 break;
963 }
964 default:
965 AssertBreakpoint();
966 }
967 }
968 }
969 else
970 break;
971 } while (1);
972 }
973
974 /* always try to close the pipe to make sure the client side is notified */
975 Status = vboxVdmaPipeSvrClose(&pVdma->CmdPipe);
976 Assert(Status == STATUS_SUCCESS);
977}
978
979NTSTATUS vboxVdmaGgConstruct(PVBOXVDMAGG pVdma)
980{
981 NTSTATUS Status = vboxVdmaPipeConstruct(&pVdma->CmdPipe);
982 Assert(Status == STATUS_SUCCESS);
983 if (Status == STATUS_SUCCESS)
984 {
985 Status = vboxVdmaGgThreadCreate(&pVdma->pThread, vboxVdmaGgWorkerThread, pVdma);
986 Assert(Status == STATUS_SUCCESS);
987 if (Status == STATUS_SUCCESS)
988 return STATUS_SUCCESS;
989
990 NTSTATUS tmpStatus = vboxVdmaPipeDestruct(&pVdma->CmdPipe);
991 Assert(tmpStatus == STATUS_SUCCESS);
992 }
993
994 /* we're here ONLY in case of an error */
995 Assert(Status != STATUS_SUCCESS);
996 return Status;
997}
998
999NTSTATUS vboxVdmaGgDestruct(PVBOXVDMAGG pVdma)
1000{
1001 /* this informs the server thread that it should complete all current commands and exit */
1002 NTSTATUS Status = vboxVdmaPipeCltClose(&pVdma->CmdPipe);
1003 Assert(Status == STATUS_SUCCESS);
1004 if (Status == STATUS_SUCCESS)
1005 {
1006 Status = KeWaitForSingleObject(pVdma->pThread, Executive, KernelMode, FALSE, NULL /* PLARGE_INTEGER Timeout */);
1007 Assert(Status == STATUS_SUCCESS);
1008 if (Status == STATUS_SUCCESS)
1009 {
1010 Status = vboxVdmaPipeDestruct(&pVdma->CmdPipe);
1011 Assert(Status == STATUS_SUCCESS);
1012 }
1013 }
1014
1015 return Status;
1016}
1017
1018NTSTATUS vboxVdmaGgCmdSubmit(PVBOXVDMAGG pVdma, PVBOXVDMAPIPE_CMD_DR pCmd)
1019{
1020 return vboxVdmaPipeCltCmdPut(&pVdma->CmdPipe, &pCmd->PipeHdr);
1021}
1022
1023/* end */
1024
1025#ifdef VBOX_WITH_VDMA
1026/*
1027 * This is currently used by VDMA. It is invisible for Vdma API clients since
1028 * Vdma transport may change if we choose to use another (e.g. more light-weight)
1029 * transport for DMA commands submission
1030 */
1031
1032#ifdef VBOXVDMA_WITH_VBVA
1033static int vboxWddmVdmaSubmitVbva(struct _DEVICE_EXTENSION* pDevExt, PVBOXVDMAINFO pInfo, HGSMIOFFSET offDr)
1034{
1035 int rc;
1036 if (vboxVbvaBufferBeginUpdate (pDevExt, &pDevExt->u.primary.Vbva))
1037 {
1038 rc = vboxVbvaReportCmdOffset(pDevExt, &pDevExt->u.primary.Vbva, offDr);
1039 vboxVbvaBufferEndUpdate (pDevExt, &pDevExt->u.primary.Vbva);
1040 }
1041 else
1042 {
1043 AssertBreakpoint();
1044 rc = VERR_INVALID_STATE;
1045 }
1046 return rc;
1047}
1048#define vboxWddmVdmaSubmit vboxWddmVdmaSubmitVbva
1049#else
1050static int vboxWddmVdmaSubmitHgsmi(struct _DEVICE_EXTENSION* pDevExt, PVBOXVDMAINFO pInfo, HGSMIOFFSET offDr)
1051{
1052 VBoxVideoCmnPortWriteUlong(commonFromDeviceExt(pDevExt)->guestCtx.port, offDr);
1053 return VINF_SUCCESS;
1054}
1055#define vboxWddmVdmaSubmit vboxWddmVdmaSubmitHgsmi
1056#endif
1057
1058static int vboxVdmaInformHost(PDEVICE_EXTENSION pDevExt, PVBOXVDMAINFO pInfo, VBOXVDMA_CTL_TYPE enmCtl)
1059{
1060 int rc = VINF_SUCCESS;
1061
1062 PVBOXVDMA_CTL pCmd = (PVBOXVDMA_CTL)VBoxSHGSMICommandAlloc(&commonFromDeviceExt(pDevExt)->guestCtx.heapCtx, sizeof (VBOXVDMA_CTL), HGSMI_CH_VBVA, VBVA_VDMA_CTL);
1063 if (pCmd)
1064 {
1065 pCmd->enmCtl = enmCtl;
1066 pCmd->u32Offset = pInfo->CmdHeap.area.offBase;
1067 pCmd->i32Result = VERR_NOT_SUPPORTED;
1068
1069 const VBOXSHGSMIHEADER* pHdr = VBoxSHGSMICommandPrepSynch(&commonFromDeviceExt(pDevExt)->guestCtx.heapCtx, pCmd);
1070 Assert(pHdr);
1071 if (pHdr)
1072 {
1073 do
1074 {
1075 HGSMIOFFSET offCmd = VBoxSHGSMICommandOffset(&commonFromDeviceExt(pDevExt)->guestCtx.heapCtx, pHdr);
1076 Assert(offCmd != HGSMIOFFSET_VOID);
1077 if (offCmd != HGSMIOFFSET_VOID)
1078 {
1079 rc = vboxWddmVdmaSubmit(pDevExt, pInfo, offCmd);
1080 AssertRC(rc);
1081 if (RT_SUCCESS(rc))
1082 {
1083 rc = VBoxSHGSMICommandDoneSynch(&commonFromDeviceExt(pDevExt)->guestCtx.heapCtx, pHdr);
1084 AssertRC(rc);
1085 if (RT_SUCCESS(rc))
1086 {
1087 rc = pCmd->i32Result;
1088 AssertRC(rc);
1089 }
1090 break;
1091 }
1092 }
1093 else
1094 rc = VERR_INVALID_PARAMETER;
1095 /* fail to submit, cancel it */
1096 VBoxSHGSMICommandCancelSynch(&commonFromDeviceExt(pDevExt)->guestCtx.heapCtx, pHdr);
1097 } while (0);
1098 }
1099
1100 VBoxSHGSMICommandFree (&commonFromDeviceExt(pDevExt)->guestCtx.heapCtx, pCmd);
1101 }
1102 else
1103 {
1104 drprintf((__FUNCTION__": HGSMIHeapAlloc failed\n"));
1105 rc = VERR_OUT_OF_RESOURCES;
1106 }
1107
1108 return rc;
1109}
1110#endif
1111
1112/* create a DMACommand buffer */
1113int vboxVdmaCreate(PDEVICE_EXTENSION pDevExt, VBOXVDMAINFO *pInfo
1114#ifdef VBOX_WITH_VDMA
1115 , ULONG offBuffer, ULONG cbBuffer
1116#endif
1117 )
1118{
1119 int rc;
1120 pInfo->fEnabled = FALSE;
1121
1122#ifdef VBOX_WITH_VDMA
1123 KeInitializeSpinLock(&pInfo->HeapLock);
1124 Assert((offBuffer & 0xfff) == 0);
1125 Assert((cbBuffer & 0xfff) == 0);
1126 Assert(offBuffer);
1127 Assert(cbBuffer);
1128
1129 if((offBuffer & 0xfff)
1130 || (cbBuffer & 0xfff)
1131 || !offBuffer
1132 || !cbBuffer)
1133 {
1134 drprintf((__FUNCTION__": invalid parameters: offBuffer(0x%x), cbBuffer(0x%x)", offBuffer, cbBuffer));
1135 return VERR_INVALID_PARAMETER;
1136 }
1137 PVOID pvBuffer;
1138
1139 rc = VBoxMapAdapterMemory (commonFromDeviceExt(pDevExt),
1140 &pvBuffer,
1141 offBuffer,
1142 cbBuffer);
1143 Assert(RT_SUCCESS(rc));
1144 if (RT_SUCCESS(rc))
1145 {
1146 /* Setup a HGSMI heap within the adapter information area. */
1147 rc = HGSMIHeapSetup (&pInfo->CmdHeap,
1148 pvBuffer,
1149 cbBuffer,
1150 offBuffer,
1151 false /*fOffsetBased*/);
1152 Assert(RT_SUCCESS(rc));
1153 if(RT_SUCCESS(rc))
1154#endif
1155 {
1156 NTSTATUS Status = vboxVdmaGgConstruct(&pInfo->DmaGg);
1157 Assert(Status == STATUS_SUCCESS);
1158 if (Status == STATUS_SUCCESS)
1159 return VINF_SUCCESS;
1160 rc = VERR_GENERAL_FAILURE;
1161 }
1162#ifdef VBOX_WITH_VDMA
1163 else
1164 drprintf((__FUNCTION__": HGSMIHeapSetup failed rc = 0x%x\n", rc));
1165
1166 VBoxUnmapAdapterMemory(commonFromDeviceExt(pDevExt), &pvBuffer);
1167 }
1168 else
1169 drprintf((__FUNCTION__": VBoxMapAdapterMemory failed rc = 0x%x\n", rc));
1170#endif
1171 return rc;
1172}
1173
1174int vboxVdmaDisable (PDEVICE_EXTENSION pDevExt, PVBOXVDMAINFO pInfo)
1175{
1176 dfprintf((__FUNCTION__"\n"));
1177
1178 Assert(pInfo->fEnabled);
1179 if (!pInfo->fEnabled)
1180 return VINF_ALREADY_INITIALIZED;
1181
1182 /* ensure nothing else is submitted */
1183 pInfo->fEnabled = FALSE;
1184#ifdef VBOX_WITH_VDMA
1185 int rc = vboxVdmaInformHost (pDevExt, pInfo, VBOXVDMA_CTL_TYPE_DISABLE);
1186 AssertRC(rc);
1187 return rc;
1188#else
1189 return VINF_SUCCESS;
1190#endif
1191}
1192
1193int vboxVdmaEnable (PDEVICE_EXTENSION pDevExt, PVBOXVDMAINFO pInfo)
1194{
1195 dfprintf((__FUNCTION__"\n"));
1196
1197 Assert(!pInfo->fEnabled);
1198 if (pInfo->fEnabled)
1199 return VINF_ALREADY_INITIALIZED;
1200#ifdef VBOX_WITH_VDMA
1201 int rc = vboxVdmaInformHost (pDevExt, pInfo, VBOXVDMA_CTL_TYPE_ENABLE);
1202 Assert(RT_SUCCESS(rc));
1203 if (RT_SUCCESS(rc))
1204 pInfo->fEnabled = TRUE;
1205
1206 return rc;
1207#else
1208 return VINF_SUCCESS;
1209#endif
1210}
1211
1212#ifdef VBOX_WITH_VDMA
1213int vboxVdmaFlush (PDEVICE_EXTENSION pDevExt, PVBOXVDMAINFO pInfo)
1214{
1215 dfprintf((__FUNCTION__"\n"));
1216
1217 Assert(pInfo->fEnabled);
1218 if (!pInfo->fEnabled)
1219 return VINF_ALREADY_INITIALIZED;
1220
1221 int rc = vboxVdmaInformHost (pDevExt, pInfo, VBOXVDMA_CTL_TYPE_FLUSH);
1222 Assert(RT_SUCCESS(rc));
1223
1224 return rc;
1225}
1226#endif
1227
1228int vboxVdmaDestroy (PDEVICE_EXTENSION pDevExt, PVBOXVDMAINFO pInfo)
1229{
1230 int rc = VINF_SUCCESS;
1231 NTSTATUS Status = vboxVdmaGgDestruct(&pInfo->DmaGg);
1232 Assert(Status == STATUS_SUCCESS);
1233 if (Status == STATUS_SUCCESS)
1234 {
1235 Assert(!pInfo->fEnabled);
1236 if (pInfo->fEnabled)
1237 rc = vboxVdmaDisable (pDevExt, pInfo);
1238#ifdef VBOX_WITH_VDMA
1239 VBoxUnmapAdapterMemory (commonFromDeviceExt(pDevExt), (void**)&pInfo->CmdHeap.area.pu8Base);
1240#endif
1241 }
1242 else
1243 rc = VERR_GENERAL_FAILURE;
1244 return rc;
1245}
1246
1247#ifdef VBOX_WITH_VDMA
1248void vboxVdmaCBufDrFree (PVBOXVDMAINFO pInfo, PVBOXVDMACBUF_DR pDr)
1249{
1250 KIRQL OldIrql;
1251 KeAcquireSpinLock(&pInfo->HeapLock, &OldIrql);
1252 VBoxSHGSMICommandFree (&pInfo->CmdHeap, pDr);
1253 KeReleaseSpinLock(&pInfo->HeapLock, OldIrql);
1254}
1255
1256PVBOXVDMACBUF_DR vboxVdmaCBufDrCreate (PVBOXVDMAINFO pInfo, uint32_t cbTrailingData)
1257{
1258 uint32_t cbDr = VBOXVDMACBUF_DR_SIZE(cbTrailingData);
1259 KIRQL OldIrql;
1260 KeAcquireSpinLock(&pInfo->HeapLock, &OldIrql);
1261 PVBOXVDMACBUF_DR pDr = (PVBOXVDMACBUF_DR)VBoxSHGSMICommandAlloc (&pInfo->CmdHeap, cbDr, HGSMI_CH_VBVA, VBVA_VDMA_CMD);
1262 KeReleaseSpinLock(&pInfo->HeapLock, OldIrql);
1263 Assert(pDr);
1264 if (pDr)
1265 memset (pDr, 0, cbDr);
1266 else
1267 drprintf((__FUNCTION__": VBoxSHGSMICommandAlloc returned NULL\n"));
1268
1269 return pDr;
1270}
1271
1272static DECLCALLBACK(void) vboxVdmaCBufDrCompletion(struct _HGSMIHEAP * pHeap, void *pvCmd, void *pvContext)
1273{
1274 PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pvContext;
1275 PVBOXVDMAINFO pInfo = &pDevExt->u.primary.Vdma;
1276
1277 vboxVdmaCBufDrFree (pInfo, (PVBOXVDMACBUF_DR)pvCmd);
1278}
1279
1280static DECLCALLBACK(void) vboxVdmaCBufDrCompletionIrq(struct _HGSMIHEAP * pHeap, void *pvCmd, void *pvContext,
1281 PFNVBOXSHGSMICMDCOMPLETION *ppfnCompletion, void **ppvCompletion)
1282{
1283 PDEVICE_EXTENSION pDevExt = (PDEVICE_EXTENSION)pvContext;
1284 PVBOXVDMAINFO pVdma = &pDevExt->u.primary.Vdma;
1285 PVBOXVDMACBUF_DR pDr = (PVBOXVDMACBUF_DR)pvCmd;
1286
1287 DXGK_INTERRUPT_TYPE enmComplType;
1288
1289 if (RT_SUCCESS(pDr->rc))
1290 {
1291 enmComplType = DXGK_INTERRUPT_DMA_COMPLETED;
1292 }
1293 else if (pDr->rc == VERR_INTERRUPTED)
1294 {
1295 Assert(0);
1296 enmComplType = DXGK_INTERRUPT_DMA_PREEMPTED;
1297 }
1298 else
1299 {
1300 Assert(0);
1301 enmComplType = DXGK_INTERRUPT_DMA_FAULTED;
1302 }
1303
1304 if (vboxVdmaDdiCmdCompletedIrq(pDevExt, &pDevExt->DdiCmdQueue, VBOXVDMADDI_CMD_FROM_BUF_DR(pDr), enmComplType))
1305 {
1306 pDevExt->bNotifyDxDpc = TRUE;
1307 }
1308
1309 /* inform SHGSMI we DO NOT want to be called at DPC later */
1310 *ppfnCompletion = NULL;
1311// *ppvCompletion = pvContext;
1312}
1313
1314int vboxVdmaCBufDrSubmit(PDEVICE_EXTENSION pDevExt, PVBOXVDMAINFO pInfo, PVBOXVDMACBUF_DR pDr)
1315{
1316 const VBOXSHGSMIHEADER* pHdr = VBoxSHGSMICommandPrepAsynchIrq (&pInfo->CmdHeap, pDr, vboxVdmaCBufDrCompletionIrq, pDevExt, VBOXSHGSMI_FLAG_GH_ASYNCH_FORCE);
1317 Assert(pHdr);
1318 int rc = VERR_GENERAL_FAILURE;
1319 if (pHdr)
1320 {
1321 do
1322 {
1323 HGSMIOFFSET offCmd = VBoxSHGSMICommandOffset(&pInfo->CmdHeap, pHdr);
1324 Assert(offCmd != HGSMIOFFSET_VOID);
1325 if (offCmd != HGSMIOFFSET_VOID)
1326 {
1327 rc = vboxWddmVdmaSubmit(pDevExt, pInfo, offCmd);
1328 AssertRC(rc);
1329 if (RT_SUCCESS(rc))
1330 {
1331 VBoxSHGSMICommandDoneAsynch(&pInfo->CmdHeap, pHdr);
1332 AssertRC(rc);
1333 break;
1334 }
1335 }
1336 else
1337 rc = VERR_INVALID_PARAMETER;
1338 /* fail to submit, cancel it */
1339 VBoxSHGSMICommandCancelAsynch(&pInfo->CmdHeap, pHdr);
1340 } while (0);
1341 }
1342 else
1343 rc = VERR_INVALID_PARAMETER;
1344 return rc;
1345}
1346
1347int vboxVdmaCBufDrSubmitSynch(PDEVICE_EXTENSION pDevExt, PVBOXVDMAINFO pInfo, PVBOXVDMACBUF_DR pDr)
1348{
1349 const VBOXSHGSMIHEADER* pHdr = VBoxSHGSMICommandPrepAsynch (&pInfo->CmdHeap, pDr, NULL, NULL, VBOXSHGSMI_FLAG_GH_SYNCH);
1350 Assert(pHdr);
1351 int rc = VERR_GENERAL_FAILURE;
1352 if (pHdr)
1353 {
1354 do
1355 {
1356 HGSMIOFFSET offCmd = VBoxSHGSMICommandOffset(&pInfo->CmdHeap, pHdr);
1357 Assert(offCmd != HGSMIOFFSET_VOID);
1358 if (offCmd != HGSMIOFFSET_VOID)
1359 {
1360 rc = vboxWddmVdmaSubmit(pDevExt, pInfo, offCmd);
1361 AssertRC(rc);
1362 if (RT_SUCCESS(rc))
1363 {
1364 VBoxSHGSMICommandDoneAsynch(&pInfo->CmdHeap, pHdr);
1365 AssertRC(rc);
1366 break;
1367 }
1368 }
1369 else
1370 rc = VERR_INVALID_PARAMETER;
1371 /* fail to submit, cancel it */
1372 VBoxSHGSMICommandCancelAsynch(&pInfo->CmdHeap, pHdr);
1373 } while (0);
1374 }
1375 else
1376 rc = VERR_INVALID_PARAMETER;
1377 return rc;
1378}
1379#endif
1380
1381
1382/* ddi dma command queue */
1383DECLINLINE(BOOLEAN) vboxVdmaDdiCmdCanComplete(PVBOXVDMADDI_CMD_QUEUE pQueue)
1384{
1385 return ASMAtomicUoReadU32(&pQueue->cQueuedCmds) == 0;
1386}
1387
1388DECLCALLBACK(VOID) vboxVdmaDdiCmdCompletionCbFree(PDEVICE_EXTENSION pDevExt, PVBOXVDMADDI_CMD pCmd, PVOID pvContext)
1389{
1390 vboxWddmMemFree(pCmd);
1391}
1392
1393static VOID vboxVdmaDdiCmdNotifyCompletedIrq(PDEVICE_EXTENSION pDevExt, PVBOXVDMADDI_CMD_QUEUE pQueue, PVBOXVDMADDI_CMD pCmd, DXGK_INTERRUPT_TYPE enmComplType)
1394{
1395 DXGKARGCB_NOTIFY_INTERRUPT_DATA notify;
1396 memset(&notify, 0, sizeof(DXGKARGCB_NOTIFY_INTERRUPT_DATA));
1397 switch (enmComplType)
1398 {
1399 case DXGK_INTERRUPT_DMA_COMPLETED:
1400 notify.InterruptType = DXGK_INTERRUPT_DMA_COMPLETED;
1401 notify.DmaCompleted.SubmissionFenceId = pCmd->u32FenceId;
1402// if (pCmd->pContext)
1403// {
1404// notify.DmaCompleted.NodeOrdinal = pCmd->pContext->NodeOrdinal;
1405// pCmd->pContext->uLastCompletedCmdFenceId = pCmd->u32FenceId;
1406// }
1407// else
1408 {
1409 pDevExt->u.primary.Vdma.uLastCompletedPagingBufferCmdFenceId = pCmd->u32FenceId;
1410 }
1411
1412 InsertTailList(&pQueue->DpcCmdQueue, &pCmd->QueueEntry);
1413
1414 break;
1415 case DXGK_INTERRUPT_DMA_PREEMPTED:
1416 Assert(0);
1417 notify.InterruptType = DXGK_INTERRUPT_DMA_PREEMPTED;
1418 notify.DmaPreempted.PreemptionFenceId = pCmd->u32FenceId;
1419// if (pCmd->pContext)
1420// {
1421// notify.DmaPreempted.LastCompletedFenceId = pCmd->pContext->uLastCompletedCmdFenceId;
1422// notify.DmaPreempted.NodeOrdinal = pCmd->pContext->NodeOrdinal;
1423// }
1424// else
1425 {
1426 notify.DmaPreempted.LastCompletedFenceId = pDevExt->u.primary.Vdma.uLastCompletedPagingBufferCmdFenceId;
1427 }
1428 break;
1429
1430 case DXGK_INTERRUPT_DMA_FAULTED:
1431 Assert(0);
1432 notify.InterruptType = DXGK_INTERRUPT_DMA_FAULTED;
1433 notify.DmaFaulted.FaultedFenceId = pCmd->u32FenceId;
1434 notify.DmaFaulted.Status = STATUS_UNSUCCESSFUL; /* @todo: better status ? */
1435 if (pCmd->pContext)
1436 {
1437 notify.DmaFaulted.NodeOrdinal = pCmd->pContext->NodeOrdinal;
1438 }
1439 break;
1440 default:
1441 Assert(0);
1442 break;
1443 }
1444
1445 pDevExt->u.primary.DxgkInterface.DxgkCbNotifyInterrupt(pDevExt->u.primary.DxgkInterface.DeviceHandle, &notify);
1446}
1447
1448DECLINLINE(VOID) vboxVdmaDdiCmdDequeueIrq(PVBOXVDMADDI_CMD_QUEUE pQueue, PVBOXVDMADDI_CMD pCmd)
1449{
1450 ASMAtomicDecU32(&pQueue->cQueuedCmds);
1451 RemoveEntryList(&pCmd->QueueEntry);
1452}
1453
1454DECLINLINE(VOID) vboxVdmaDdiCmdEnqueueIrq(PVBOXVDMADDI_CMD_QUEUE pQueue, PVBOXVDMADDI_CMD pCmd)
1455{
1456 ASMAtomicIncU32(&pQueue->cQueuedCmds);
1457 InsertTailList(&pQueue->CmdQueue, &pCmd->QueueEntry);
1458}
1459
1460VOID vboxVdmaDdiQueueInit(PDEVICE_EXTENSION pDevExt, PVBOXVDMADDI_CMD_QUEUE pQueue)
1461{
1462 pQueue->cQueuedCmds = 0;
1463 InitializeListHead(&pQueue->CmdQueue);
1464 InitializeListHead(&pQueue->DpcCmdQueue);
1465}
1466
1467BOOLEAN vboxVdmaDdiCmdCompletedIrq(PDEVICE_EXTENSION pDevExt, PVBOXVDMADDI_CMD_QUEUE pQueue, PVBOXVDMADDI_CMD pCmd, DXGK_INTERRUPT_TYPE enmComplType)
1468{
1469 if (VBOXVDMADDI_STATE_NOT_DX_CMD == pCmd->enmState)
1470 {
1471 InsertTailList(&pQueue->DpcCmdQueue, &pCmd->QueueEntry);
1472 return FALSE;
1473 }
1474
1475 BOOLEAN bQueued = pCmd->enmState > VBOXVDMADDI_STATE_NOT_QUEUED;
1476 BOOLEAN bComplete = FALSE;
1477 Assert(!bQueued || pQueue->cQueuedCmds);
1478 Assert(!bQueued || !IsListEmpty(&pQueue->CmdQueue));
1479 pCmd->enmState = VBOXVDMADDI_STATE_COMPLETED;
1480 if (bQueued)
1481 {
1482 if (pQueue->CmdQueue.Flink == &pCmd->QueueEntry)
1483 {
1484 vboxVdmaDdiCmdDequeueIrq(pQueue, pCmd);
1485 bComplete = TRUE;
1486 }
1487 }
1488 else if (IsListEmpty(&pQueue->CmdQueue))
1489 {
1490 bComplete = TRUE;
1491 }
1492 else
1493 {
1494 vboxVdmaDdiCmdEnqueueIrq(pQueue, pCmd);
1495 }
1496
1497 if (bComplete)
1498 {
1499 vboxVdmaDdiCmdNotifyCompletedIrq(pDevExt, pQueue, pCmd, enmComplType);
1500
1501 while (!IsListEmpty(&pQueue->CmdQueue))
1502 {
1503 pCmd = VBOXVDMADDI_CMD_FROM_ENTRY(pQueue->CmdQueue.Flink);
1504 if (pCmd->enmState == VBOXVDMADDI_STATE_COMPLETED)
1505 {
1506 vboxVdmaDdiCmdDequeueIrq(pQueue, pCmd);
1507 vboxVdmaDdiCmdNotifyCompletedIrq(pDevExt, pQueue, pCmd, pCmd->enmComplType);
1508 }
1509 else
1510 break;
1511 }
1512 }
1513 else
1514 {
1515 pCmd->enmState = VBOXVDMADDI_STATE_COMPLETED;
1516 pCmd->enmComplType = enmComplType;
1517 }
1518
1519 return bComplete;
1520}
1521
1522VOID vboxVdmaDdiCmdSubmittedIrq(PVBOXVDMADDI_CMD_QUEUE pQueue, PVBOXVDMADDI_CMD pCmd)
1523{
1524 BOOLEAN bQueued = pCmd->enmState >= VBOXVDMADDI_STATE_PENDING;
1525 Assert(pCmd->enmState < VBOXVDMADDI_STATE_SUBMITTED);
1526 pCmd->enmState = VBOXVDMADDI_STATE_SUBMITTED;
1527 if (!bQueued)
1528 vboxVdmaDdiCmdEnqueueIrq(pQueue, pCmd);
1529}
1530
1531typedef struct VBOXVDMADDI_CMD_COMPLETED_CB
1532{
1533 PDEVICE_EXTENSION pDevExt;
1534 PVBOXVDMADDI_CMD_QUEUE pQueue;
1535 PVBOXVDMADDI_CMD pCmd;
1536 DXGK_INTERRUPT_TYPE enmComplType;
1537} VBOXVDMADDI_CMD_COMPLETED_CB, *PVBOXVDMADDI_CMD_COMPLETED_CB;
1538
1539static BOOLEAN vboxVdmaDdiCmdCompletedCb(PVOID Context)
1540{
1541 PVBOXVDMADDI_CMD_COMPLETED_CB pdc = (PVBOXVDMADDI_CMD_COMPLETED_CB)Context;
1542 BOOLEAN bNeedDpc = vboxVdmaDdiCmdCompletedIrq(pdc->pDevExt, pdc->pQueue, pdc->pCmd, pdc->enmComplType);
1543 pdc->pDevExt->bNotifyDxDpc |= bNeedDpc;
1544
1545 return bNeedDpc;
1546}
1547
1548NTSTATUS vboxVdmaDdiCmdCompleted(PDEVICE_EXTENSION pDevExt, PVBOXVDMADDI_CMD_QUEUE pQueue, PVBOXVDMADDI_CMD pCmd, DXGK_INTERRUPT_TYPE enmComplType)
1549{
1550 VBOXVDMADDI_CMD_COMPLETED_CB context;
1551 context.pDevExt = pDevExt;
1552 context.pQueue = pQueue;
1553 context.pCmd = pCmd;
1554 context.enmComplType = enmComplType;
1555 BOOLEAN bNeedDps;
1556 NTSTATUS Status = pDevExt->u.primary.DxgkInterface.DxgkCbSynchronizeExecution(
1557 pDevExt->u.primary.DxgkInterface.DeviceHandle,
1558 vboxVdmaDdiCmdCompletedCb,
1559 &context,
1560 0, /* IN ULONG MessageNumber */
1561 &bNeedDps);
1562 Assert(Status == STATUS_SUCCESS);
1563 if (Status == STATUS_SUCCESS && bNeedDps)
1564 {
1565 BOOLEAN bRc = pDevExt->u.primary.DxgkInterface.DxgkCbQueueDpc(pDevExt->u.primary.DxgkInterface.DeviceHandle);
1566 Assert(bRc);
1567 }
1568 return Status;
1569}
1570
1571typedef struct VBOXVDMADDI_CMD_SUBMITTED_CB
1572{
1573// PDEVICE_EXTENSION pDevExt;
1574 PVBOXVDMADDI_CMD_QUEUE pQueue;
1575 PVBOXVDMADDI_CMD pCmd;
1576} VBOXVDMADDI_CMD_SUBMITTED_CB, *PVBOXVDMADDI_CMD_SUBMITTED_CB;
1577
1578static BOOLEAN vboxVdmaDdiCmdSubmittedCb(PVOID Context)
1579{
1580 PVBOXVDMADDI_CMD_SUBMITTED_CB pdc = (PVBOXVDMADDI_CMD_SUBMITTED_CB)Context;
1581 vboxVdmaDdiCmdSubmittedIrq(pdc->pQueue, pdc->pCmd);
1582
1583 return FALSE;
1584}
1585
1586NTSTATUS vboxVdmaDdiCmdSubmitted(PDEVICE_EXTENSION pDevExt, PVBOXVDMADDI_CMD_QUEUE pQueue, PVBOXVDMADDI_CMD pCmd)
1587{
1588 VBOXVDMADDI_CMD_SUBMITTED_CB context;
1589 context.pQueue = pQueue;
1590 context.pCmd = pCmd;
1591 BOOLEAN bRc;
1592 NTSTATUS Status = pDevExt->u.primary.DxgkInterface.DxgkCbSynchronizeExecution(
1593 pDevExt->u.primary.DxgkInterface.DeviceHandle,
1594 vboxVdmaDdiCmdSubmittedCb,
1595 &context,
1596 0, /* IN ULONG MessageNumber */
1597 &bRc);
1598 Assert(Status == STATUS_SUCCESS);
1599 return Status;
1600}
1601
1602typedef struct VBOXVDMADDI_CMD_COMPLETE_CB
1603{
1604 PDEVICE_EXTENSION pDevExt;
1605 PVBOXWDDM_CONTEXT pContext;
1606 uint32_t u32FenceId;
1607} VBOXVDMADDI_CMD_COMPLETE_CB, *PVBOXVDMADDI_CMD_COMPLETE_CB;
1608
1609static BOOLEAN vboxVdmaDdiCmdFenceCompleteCb(PVOID Context)
1610{
1611 PVBOXVDMADDI_CMD_COMPLETE_CB pdc = (PVBOXVDMADDI_CMD_COMPLETE_CB)Context;
1612 PDEVICE_EXTENSION pDevExt = pdc->pDevExt;
1613 DXGKARGCB_NOTIFY_INTERRUPT_DATA notify;
1614 memset(&notify, 0, sizeof(DXGKARGCB_NOTIFY_INTERRUPT_DATA));
1615
1616 notify.InterruptType = DXGK_INTERRUPT_DMA_COMPLETED;
1617 notify.DmaCompleted.SubmissionFenceId = pdc->u32FenceId;
1618 notify.DmaCompleted.NodeOrdinal = pdc->pContext->NodeOrdinal;
1619 notify.DmaCompleted.EngineOrdinal = 0;
1620
1621 pDevExt->u.primary.DxgkInterface.DxgkCbNotifyInterrupt(pDevExt->u.primary.DxgkInterface.DeviceHandle, &notify);
1622
1623 pDevExt->bNotifyDxDpc = TRUE;
1624 BOOLEAN bDpcQueued = pDevExt->u.primary.DxgkInterface.DxgkCbQueueDpc(pDevExt->u.primary.DxgkInterface.DeviceHandle);
1625
1626 return bDpcQueued;
1627}
1628
1629static NTSTATUS vboxVdmaDdiCmdFenceNotifyComplete(PDEVICE_EXTENSION pDevExt, PVBOXWDDM_CONTEXT pContext, uint32_t u32FenceId)
1630{
1631 VBOXVDMADDI_CMD_COMPLETE_CB context;
1632 context.pDevExt = pDevExt;
1633 context.pContext = pContext;
1634 context.u32FenceId = u32FenceId;
1635 BOOLEAN bRet;
1636 NTSTATUS Status = pDevExt->u.primary.DxgkInterface.DxgkCbSynchronizeExecution(
1637 pDevExt->u.primary.DxgkInterface.DeviceHandle,
1638 vboxVdmaDdiCmdFenceCompleteCb,
1639 &context,
1640 0, /* IN ULONG MessageNumber */
1641 &bRet);
1642 Assert(Status == STATUS_SUCCESS);
1643 return Status;
1644}
1645
1646NTSTATUS vboxVdmaDdiCmdFenceComplete(PDEVICE_EXTENSION pDevExt, PVBOXWDDM_CONTEXT pContext, uint32_t u32FenceId, DXGK_INTERRUPT_TYPE enmComplType)
1647{
1648 if (vboxVdmaDdiCmdCanComplete(&pDevExt->DdiCmdQueue))
1649 return vboxVdmaDdiCmdFenceNotifyComplete(pDevExt, pContext, u32FenceId);
1650
1651 PVBOXVDMADDI_CMD pCmd = (PVBOXVDMADDI_CMD)vboxWddmMemAlloc(sizeof (VBOXVDMADDI_CMD));
1652 Assert(pCmd);
1653 if (pCmd)
1654 {
1655 vboxVdmaDdiCmdInit(pCmd, u32FenceId, pContext, vboxVdmaDdiCmdCompletionCbFree, NULL);
1656 NTSTATUS Status = vboxVdmaDdiCmdCompleted(pDevExt, &pDevExt->DdiCmdQueue, pCmd, enmComplType);
1657 Assert(Status == STATUS_SUCCESS);
1658 if (Status == STATUS_SUCCESS)
1659 return STATUS_SUCCESS;
1660 vboxWddmMemFree(pCmd);
1661 return Status;
1662 }
1663 return STATUS_NO_MEMORY;
1664}
1665
1666#ifdef VBOXWDDM_RENDER_FROM_SHADOW
1667NTSTATUS vboxVdmaHlpUpdatePrimary(PDEVICE_EXTENSION pDevExt, D3DDDI_VIDEO_PRESENT_SOURCE_ID VidPnSourceId, RECT* pRect)
1668{
1669 PVBOXWDDM_SOURCE pSource = &pDevExt->aSources[VidPnSourceId];
1670 Assert(pSource->pPrimaryAllocation);
1671 Assert(pSource->pShadowAllocation);
1672 if (!pSource->pPrimaryAllocation)
1673 return STATUS_INVALID_PARAMETER;
1674 if (!pSource->pShadowAllocation)
1675 return STATUS_INVALID_PARAMETER;
1676
1677 Assert(pSource->pPrimaryAllocation->offVram != VBOXVIDEOOFFSET_VOID);
1678 Assert(pSource->pShadowAllocation->offVram != VBOXVIDEOOFFSET_VOID);
1679 if (pSource->pPrimaryAllocation->offVram == VBOXVIDEOOFFSET_VOID)
1680 return STATUS_INVALID_PARAMETER;
1681 if (pSource->pShadowAllocation->offVram == VBOXVIDEOOFFSET_VOID)
1682 return STATUS_INVALID_PARAMETER;
1683
1684 NTSTATUS Status = vboxVdmaGgDmaBltPerform(pDevExt, pSource->pShadowAllocation, pRect, pSource->pPrimaryAllocation, pRect);
1685 Assert(Status == STATUS_SUCCESS);
1686 return Status;
1687}
1688#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