VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiKmt.cpp@ 43490

Last change on this file since 43490 was 42499, checked in by vboxsync, 13 years ago

crOgl/wddm: per-context connections

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1/* $Id: VBoxUhgsmiKmt.cpp 42499 2012-08-01 10:26:43Z vboxsync $ */
2
3/** @file
4 * VBoxVideo Display D3D User mode dll
5 */
6
7/*
8 * Copyright (C) 2011 Oracle Corporation
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 (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include "VBoxDispD3DCmn.h"
20
21#include <iprt/mem.h>
22#include <iprt/err.h>
23
24#include <cr_protocol.h>
25
26#ifndef NT_SUCCESS
27# define NT_SUCCESS(_Status) (((NTSTATUS)(_Status)) >= 0)
28#endif
29
30#if 0
31typedef struct VBOXUHGSMI_BUFFER_PRIVATE_KMT
32{
33 VBOXUHGSMI_BUFFER_PRIVATE_BASE BasePrivate;
34 CRITICAL_SECTION CritSect;
35} VBOXUHGSMI_BUFFER_PRIVATE_KMT, *PVBOXUHGSMI_BUFFER_PRIVATE_KMT;
36
37
38#define VBOXUHGSMIKMT_GET_BUFFER(_p) VBOXUHGSMIKMT_GET_PRIVATE(_p, VBOXUHGSMI_BUFFER_PRIVATE_KMT)
39
40DECLCALLBACK(int) vboxUhgsmiKmtBufferDestroy(PVBOXUHGSMI_BUFFER pBuf)
41{
42 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuffer = VBOXUHGSMIKMT_GET_BUFFER(pBuf);
43 D3DKMT_DESTROYALLOCATION DdiDealloc;
44 DdiDealloc.hDevice = pBuffer->pHgsmi->Device.hDevice;
45 DdiDealloc.hResource = NULL;
46 DdiDealloc.phAllocationList = &pBuffer->BasePrivate.hAllocation;
47 DdiDealloc.AllocationCount = 1;
48 NTSTATUS Status = pBuffer->pHgsmi->Callbacks.pfnD3DKMTDestroyAllocation(&DdiDealloc);
49 if (NT_SUCCESS(Status))
50 {
51 if (pBuffer->BasePrivate.hSynch)
52 CloseHandle(pBuffer->BasePrivate.hSynch);
53 RTMemFree(pBuffer);
54 return VINF_SUCCESS;
55 }
56 else
57 {
58 WARN(("pfnD3DKMTDestroyAllocation failed, Status (0x%x)", Status));
59 }
60 return VERR_GENERAL_FAILURE;
61}
62
63DECLCALLBACK(int) vboxUhgsmiKmtBufferLock(PVBOXUHGSMI_BUFFER pBuf, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags, void**pvLock)
64{
65 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuffer = VBOXUHGSMIKMT_GET_BUFFER(pBuf);
66 D3DKMT_LOCK DdiLock = {0};
67 DdiLock.hDevice = pBuffer->pHgsmi->Device.hDevice;
68 DdiLock.hAllocation = pBuffer->BasePrivate.hAllocation;
69 DdiLock.PrivateDriverData = NULL;
70
71 EnterCriticalSection(&pBuffer->CritSect);
72
73 int rc = vboxUhgsmiBaseLockData(pBuf, offLock, cbLock, fFlags,
74 &DdiLock.Flags, &DdiLock.NumPages, pBuffer->aLockPageIndices);
75 AssertRC(rc);
76 if (RT_FAILURE(rc))
77 return rc;
78
79 if (DdiLock.NumPages)
80 DdiLock.pPages = pBuffer->aLockPageIndices;
81 else
82 DdiLock.pPages = NULL;
83
84 NTSTATUS Status = pBuffer->pHgsmi->Callbacks.pfnD3DKMTLock(&DdiLock);
85 LeaveCriticalSection(&pBuffer->CritSect);
86 if (NT_SUCCESS(Status))
87 {
88 *pvLock = (void*)(((uint8_t*)DdiLock.pData) + (offLock & 0xfff));
89 return VINF_SUCCESS;
90 }
91 else
92 {
93 WARN(("pfnD3DKMTLock failed, Status (0x%x)", Status));
94 }
95
96 return VERR_GENERAL_FAILURE;
97}
98
99DECLCALLBACK(int) vboxUhgsmiKmtBufferUnlock(PVBOXUHGSMI_BUFFER pBuf)
100{
101 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuffer = VBOXUHGSMIKMT_GET_BUFFER(pBuf);
102 D3DKMT_UNLOCK DdiUnlock;
103
104 DdiUnlock.hDevice = pBuffer->pHgsmi->Device.hDevice;
105 DdiUnlock.NumAllocations = 1;
106 DdiUnlock.phAllocations = &pBuffer->BasePrivate.hAllocation;
107 NTSTATUS Status = pBuffer->pHgsmi->Callbacks.pfnD3DKMTUnlock(&DdiUnlock);
108 if (NT_SUCCESS(Status))
109 return VINF_SUCCESS;
110 else
111 WARN(("pfnD3DKMTUnlock failed, Status (0x%x)", Status));
112
113 return VERR_GENERAL_FAILURE;
114}
115
116DECLCALLBACK(int) vboxUhgsmiKmtBufferCreate(PVBOXUHGSMI pHgsmi, uint32_t cbBuf, VBOXUHGSMI_BUFFER_TYPE_FLAGS fUhgsmiType, PVBOXUHGSMI_BUFFER* ppBuf)
117{
118 HANDLE hSynch = NULL;
119 if (!cbBuf)
120 return VERR_INVALID_PARAMETER;
121
122 int rc = vboxUhgsmiBaseEventChkCreate(fUhgsmiType, &hSynch);
123 AssertRC(rc);
124 if (RT_FAILURE(rc))
125 return rc;
126
127 cbBuf = VBOXWDDM_ROUNDBOUND(cbBuf, 0x1000);
128 Assert(cbBuf);
129 uint32_t cPages = cbBuf >> 12;
130 Assert(cPages);
131
132 PVBOXUHGSMI_PRIVATE_KMT pPrivate = VBOXUHGSMIKMT_GET(pHgsmi);
133 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuf = (PVBOXUHGSMI_BUFFER_PRIVATE_KMT)RTMemAllocZ(RT_OFFSETOF(VBOXUHGSMI_BUFFER_PRIVATE_KMT, aLockPageIndices[cPages]));
134 Assert(pBuf);
135 if (pBuf)
136 {
137 struct
138 {
139 D3DKMT_CREATEALLOCATION DdiAlloc;
140 D3DDDI_ALLOCATIONINFO DdiAllocInfo;
141 VBOXWDDM_ALLOCINFO AllocInfo;
142 } Buf;
143 memset(&Buf, 0, sizeof (Buf));
144 Buf.DdiAlloc.hDevice = pPrivate->Device.hDevice;
145 Buf.DdiAlloc.NumAllocations = 1;
146 Buf.DdiAlloc.pAllocationInfo = &Buf.DdiAllocInfo;
147 Buf.DdiAllocInfo.pPrivateDriverData = &Buf.AllocInfo;
148 Buf.DdiAllocInfo.PrivateDriverDataSize = sizeof (Buf.AllocInfo);
149 Buf.AllocInfo.enmType = VBOXWDDM_ALLOC_TYPE_UMD_HGSMI_BUFFER;
150 Buf.AllocInfo.cbBuffer = cbBuf;
151 Buf.AllocInfo.hSynch = (uint64_t)hSynch;
152 Buf.AllocInfo.fUhgsmiType = fUhgsmiType;
153
154 NTSTATUS Status = pPrivate->Callbacks.pfnD3DKMTCreateAllocation(&Buf.DdiAlloc);
155 if (NT_SUCCESS(Status))
156 {
157 InitializeCriticalSection(&pBuf->CritSect);
158
159 Assert(Buf.DdiAllocInfo.hAllocation);
160 pBuf->BasePrivate.Base.pfnLock = vboxUhgsmiKmtBufferLock;
161 pBuf->BasePrivate.Base.pfnUnlock = vboxUhgsmiKmtBufferUnlock;
162// pBuf->Base.pfnAdjustValidDataRange = vboxUhgsmiKmtBufferAdjustValidDataRange;
163 pBuf->BasePrivate.Base.pfnDestroy = vboxUhgsmiKmtBufferDestroy;
164
165 pBuf->BasePrivate.Base.fType = fUhgsmiType;
166 pBuf->BasePrivate.Base.cbBuffer = cbBuf;
167
168 pBuf->pHgsmi = pPrivate;
169 pBuf->BasePrivate.hAllocation = Buf.DdiAllocInfo.hAllocation;
170
171 *ppBuf = &pBuf->BasePrivate.Base;
172
173 return VINF_SUCCESS;
174 }
175 else
176 {
177 WARN(("pfnD3DKMTCreateAllocation failes, Status(0x%x)", Status));
178 rc = VERR_OUT_OF_RESOURCES;
179 }
180
181 RTMemFree(pBuf);
182 }
183 else
184 rc = VERR_NO_MEMORY;
185
186 if (hSynch)
187 CloseHandle(hSynch);
188
189 return rc;
190}
191
192DECLCALLBACK(int) vboxUhgsmiKmtBufferSubmit(PVBOXUHGSMI pHgsmi, PVBOXUHGSMI_BUFFER_SUBMIT aBuffers, uint32_t cBuffers)
193{
194 PVBOXUHGSMI_PRIVATE_KMT pHg = VBOXUHGSMIKMT_GET(pHgsmi);
195 UINT cbDmaCmd = pHg->Context.CommandBufferSize;
196 int rc = vboxUhgsmiBaseDmaFill(aBuffers, cBuffers,
197 pHg->Context.pCommandBuffer, &cbDmaCmd,
198 pHg->Context.pAllocationList, pHg->Context.AllocationListSize,
199 pHg->Context.pPatchLocationList, pHg->Context.PatchLocationListSize);
200 AssertRC(rc);
201 if (RT_FAILURE(rc))
202 return rc;
203
204 D3DKMT_RENDER DdiRender = {0};
205 DdiRender.hContext = pHg->Context.hContext;
206 DdiRender.CommandLength = cbDmaCmd;
207 DdiRender.AllocationCount = cBuffers;
208 Assert(DdiRender.CommandLength);
209 Assert(DdiRender.CommandLength < UINT32_MAX/2);
210
211 NTSTATUS Status = pHg->Callbacks.pfnD3DKMTRender(&DdiRender);
212 if (NT_SUCCESS(Status))
213 {
214 pHg->Context.CommandBufferSize = DdiRender.NewCommandBufferSize;
215 pHg->Context.pCommandBuffer = DdiRender.pNewCommandBuffer;
216 pHg->Context.AllocationListSize = DdiRender.NewAllocationListSize;
217 pHg->Context.pAllocationList = DdiRender.pNewAllocationList;
218 pHg->Context.PatchLocationListSize = DdiRender.NewPatchLocationListSize;
219 pHg->Context.pPatchLocationList = DdiRender.pNewPatchLocationList;
220
221 return VINF_SUCCESS;
222 }
223 else
224 {
225 WARN(("pfnD3DKMTRender failed, Status (0x%x)", Status));
226 }
227
228 return VERR_GENERAL_FAILURE;
229}
230#endif
231
232static HRESULT vboxUhgsmiKmtEngineCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
233{
234 HRESULT hr = vboxDispKmtCallbacksInit(&pHgsmi->Callbacks);
235 if (hr == S_OK)
236 {
237 hr = vboxDispKmtOpenAdapter(&pHgsmi->Callbacks, &pHgsmi->Adapter);
238 if (hr == S_OK)
239 {
240 hr = vboxDispKmtCreateDevice(&pHgsmi->Adapter, &pHgsmi->Device);
241 if (hr == S_OK)
242 {
243 hr = vboxDispKmtCreateContext(&pHgsmi->Device, &pHgsmi->Context,
244 bD3D ? VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_3D : VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_GL,
245 CR_PROTOCOL_VERSION_MAJOR, CR_PROTOCOL_VERSION_MINOR,
246 NULL, 0);
247 if (hr == S_OK)
248 {
249 return S_OK;
250 }
251 else
252 {
253 WARN(("vboxDispKmtCreateContext failed, hr(0x%x)", hr));
254 }
255 vboxDispKmtDestroyDevice(&pHgsmi->Device);
256 }
257 else
258 {
259 WARN(("vboxDispKmtCreateDevice failed, hr(0x%x)", hr));
260 }
261 vboxDispKmtCloseAdapter(&pHgsmi->Adapter);
262 }
263 else
264 {
265// WARN(("vboxDispKmtOpenAdapter failed, hr(0x%x)", hr));
266 }
267
268 vboxDispKmtCallbacksTerm(&pHgsmi->Callbacks);
269 }
270 else
271 {
272 WARN(("vboxDispKmtCallbacksInit failed, hr(0x%x)", hr));
273 }
274 return hr;
275}
276
277static DECLCALLBACK(int) vboxCrHhgsmiKmtEscape(struct VBOXUHGSMI_PRIVATE_BASE *pHgsmi, void *pvData, uint32_t cbData, BOOL fHwAccess)
278{
279 PVBOXUHGSMI_PRIVATE_KMT pPrivate = VBOXUHGSMIKMT_GET(pHgsmi);
280 D3DKMT_ESCAPE DdiEscape = {0};
281 DdiEscape.hAdapter = pPrivate->Adapter.hAdapter;
282 DdiEscape.hDevice = pPrivate->Device.hDevice;
283 DdiEscape.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
284 DdiEscape.Flags.HardwareAccess = !!fHwAccess;
285 DdiEscape.pPrivateDriverData = pvData;
286 DdiEscape.PrivateDriverDataSize = cbData;
287 DdiEscape.hContext = pPrivate->Context.hContext;
288
289 NTSTATUS Status = pPrivate->Callbacks.pfnD3DKMTEscape(&DdiEscape);
290 if (NT_SUCCESS(Status))
291 {
292 return VINF_SUCCESS;
293 }
294
295 WARN(("pfnD3DKMTEscape failed, Status (0x%x)", Status));
296 return VERR_GENERAL_FAILURE;
297}
298
299#if 0
300HRESULT vboxUhgsmiKmtCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
301{
302 vboxUhgsmiBaseInit(&pHgsmi->BasePrivate, vboxCrHhgsmiKmtEscape);
303#error "port me!"
304 return vboxUhgsmiKmtEngineCreate(pHgsmi, bD3D);
305}
306#endif
307
308HRESULT vboxUhgsmiKmtEscCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
309{
310 vboxUhgsmiBaseInit(&pHgsmi->BasePrivate, vboxCrHhgsmiKmtEscape);
311 return vboxUhgsmiKmtEngineCreate(pHgsmi, bD3D);
312}
313
314HRESULT vboxUhgsmiKmtDestroy(PVBOXUHGSMI_PRIVATE_KMT pHgsmi)
315{
316 HRESULT hr = vboxDispKmtDestroyContext(&pHgsmi->Context);
317 Assert(hr == S_OK);
318 if (hr == S_OK)
319 {
320 hr = vboxDispKmtDestroyDevice(&pHgsmi->Device);
321 Assert(hr == S_OK);
322 if (hr == S_OK)
323 {
324 hr = vboxDispKmtCloseAdapter(&pHgsmi->Adapter);
325 Assert(hr == S_OK);
326 if (hr == S_OK)
327 {
328 hr = vboxDispKmtCallbacksTerm(&pHgsmi->Callbacks);
329 Assert(hr == S_OK);
330 if (hr == S_OK)
331 return S_OK;
332 }
333 }
334 }
335 return hr;
336}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette