VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxUhgsmiKmt.cpp@ 33714

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

wddm/3d: CrHgsmi cleanup, free resources correctly; 2D fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.9 KB
Line 
1/** @file
2 *
3 * VBoxVideo Display D3D User mode dll
4 *
5 * Copyright (C) 2010 Oracle Corporation
6 *
7 * This file is part of VirtualBox Open Source Edition (OSE), as
8 * available from http://www.virtualbox.org. This file is free software;
9 * you can redistribute it and/or modify it under the terms of the GNU
10 * General Public License (GPL) as published by the Free Software
11 * Foundation, in version 2 as it comes in the "COPYING" file of the
12 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
13 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
14 */
15#include "VBoxDispD3DCmn.h"
16
17#include <iprt/mem.h>
18#include <iprt/err.h>
19
20typedef struct VBOXUHGSMI_BUFFER_PRIVATE_KMT
21{
22 VBOXUHGSMI_BUFFER_PRIVATE_BASE BasePrivate;
23 PVBOXUHGSMI_PRIVATE_KMT pHgsmi;
24 CRITICAL_SECTION CritSect;
25 UINT aLockPageIndices[1];
26} VBOXUHGSMI_BUFFER_PRIVATE_KMT, *PVBOXUHGSMI_BUFFER_PRIVATE_KMT;
27
28typedef struct VBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC
29{
30 VBOXUHGSMI_BUFFER Base;
31 PVBOXUHGSMI_PRIVATE_KMT pHgsmi;
32 VBOXVIDEOCM_UM_ALLOC Alloc;
33} VBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC, *PVBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC;
34
35#define VBOXUHGSMIKMT_GET_BUFFER(_p) VBOXUHGSMIKMT_GET_PRIVATE(_p, VBOXUHGSMI_BUFFER_PRIVATE_KMT)
36#define VBOXUHGSMIKMTESC_GET_PRIVATE(_p, _t) ((_t*)(((uint8_t*)_p) - RT_OFFSETOF(_t, Base)))
37#define VBOXUHGSMIKMTESC_GET_BUFFER(_p) VBOXUHGSMIKMTESC_GET_PRIVATE(_p, VBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC)
38
39DECLCALLBACK(int) vboxUhgsmiKmtBufferDestroy(PVBOXUHGSMI_BUFFER pBuf)
40{
41 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuffer = VBOXUHGSMIKMT_GET_BUFFER(pBuf);
42 D3DKMT_DESTROYALLOCATION DdiDealloc;
43 DdiDealloc.hDevice = pBuffer->pHgsmi->Device.hDevice;
44 DdiDealloc.hResource = NULL;
45 DdiDealloc.phAllocationList = &pBuffer->BasePrivate.hAllocation;
46 DdiDealloc.AllocationCount = 1;
47 NTSTATUS Status = pBuffer->pHgsmi->Callbacks.pfnD3DKMTDestroyAllocation(&DdiDealloc);
48 Assert(!Status);
49 if (!Status)
50 {
51 if (pBuffer->BasePrivate.Base.bSynchCreated)
52 {
53 CloseHandle(pBuffer->BasePrivate.Base.hSynch);
54 }
55 RTMemFree(pBuffer);
56 return VINF_SUCCESS;
57 }
58 return VERR_GENERAL_FAILURE;
59}
60
61DECLCALLBACK(int) vboxUhgsmiKmtBufferLock(PVBOXUHGSMI_BUFFER pBuf, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags, void**pvLock)
62{
63 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuffer = VBOXUHGSMIKMT_GET_BUFFER(pBuf);
64 D3DKMT_LOCK DdiLock = {0};
65 DdiLock.hDevice = pBuffer->pHgsmi->Device.hDevice;
66 DdiLock.hAllocation = pBuffer->BasePrivate.hAllocation;
67 DdiLock.PrivateDriverData = NULL;
68
69 EnterCriticalSection(&pBuffer->CritSect);
70
71 int rc = vboxUhgsmiBaseLockData(pBuf, offLock, cbLock, fFlags,
72 &DdiLock.Flags, &DdiLock.NumPages, pBuffer->aLockPageIndices);
73 AssertRC(rc);
74 if (RT_FAILURE(rc))
75 return rc;
76
77 if (DdiLock.NumPages)
78 DdiLock.pPages = pBuffer->aLockPageIndices;
79 else
80 DdiLock.pPages = NULL;
81
82 NTSTATUS Status = pBuffer->pHgsmi->Callbacks.pfnD3DKMTLock(&DdiLock);
83 Assert(!Status);
84 LeaveCriticalSection(&pBuffer->CritSect);
85 if (!Status)
86 {
87 *pvLock = (void*)(((uint8_t*)DdiLock.pData) + (offLock & 0xfff));
88 return VINF_SUCCESS;
89 }
90 return VERR_GENERAL_FAILURE;
91}
92
93DECLCALLBACK(int) vboxUhgsmiKmtBufferUnlock(PVBOXUHGSMI_BUFFER pBuf)
94{
95 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuffer = VBOXUHGSMIKMT_GET_BUFFER(pBuf);
96 D3DKMT_UNLOCK DdiUnlock;
97
98 DdiUnlock.hDevice = pBuffer->pHgsmi->Device.hDevice;
99 DdiUnlock.NumAllocations = 1;
100 DdiUnlock.phAllocations = &pBuffer->BasePrivate.hAllocation;
101 NTSTATUS Status = pBuffer->pHgsmi->Callbacks.pfnD3DKMTUnlock(&DdiUnlock);
102 Assert(!Status);
103 if (!Status)
104 return VINF_SUCCESS;
105 return VERR_GENERAL_FAILURE;
106}
107
108DECLCALLBACK(int) vboxUhgsmiKmtBufferCreate(PVBOXUHGSMI pHgsmi, uint32_t cbBuf,
109 VBOXUHGSMI_SYNCHOBJECT_TYPE enmSynchType, HVBOXUHGSMI_SYNCHOBJECT hSynch,
110 PVBOXUHGSMI_BUFFER* ppBuf)
111{
112 bool bSynchCreated = false;
113 if (!cbBuf)
114 return VERR_INVALID_PARAMETER;
115
116 int rc = vboxUhgsmiBaseEventChkCreate(enmSynchType, &hSynch, &bSynchCreated);
117 AssertRC(rc);
118 if (RT_FAILURE(rc))
119 return rc;
120
121 cbBuf = VBOXWDDM_ROUNDBOUND(cbBuf, 0x1000);
122 Assert(cbBuf);
123 uint32_t cPages = cbBuf >> 12;
124 Assert(cPages);
125
126 PVBOXUHGSMI_PRIVATE_KMT pPrivate = VBOXUHGSMIKMT_GET(pHgsmi);
127 PVBOXUHGSMI_BUFFER_PRIVATE_KMT pBuf = (PVBOXUHGSMI_BUFFER_PRIVATE_KMT)RTMemAllocZ(RT_OFFSETOF(VBOXUHGSMI_BUFFER_PRIVATE_KMT, aLockPageIndices[cPages]));
128 Assert(pBuf);
129 if (pBuf)
130 {
131 struct
132 {
133 D3DKMT_CREATEALLOCATION DdiAlloc;
134 D3DDDI_ALLOCATIONINFO DdiAllocInfo;
135 VBOXWDDM_ALLOCINFO AllocInfo;
136 } Buf;
137 memset(&Buf, 0, sizeof (Buf));
138 Buf.DdiAlloc.hDevice = pPrivate->Device.hDevice;
139 Buf.DdiAlloc.NumAllocations = 1;
140 Buf.DdiAlloc.pAllocationInfo = &Buf.DdiAllocInfo;
141 Buf.DdiAllocInfo.pPrivateDriverData = &Buf.AllocInfo;
142 Buf.DdiAllocInfo.PrivateDriverDataSize = sizeof (Buf.AllocInfo);
143 Buf.AllocInfo.enmType = VBOXWDDM_ALLOC_TYPE_UMD_HGSMI_BUFFER;
144 Buf.AllocInfo.cbBuffer = cbBuf;
145 Buf.AllocInfo.hSynch = hSynch;
146 Buf.AllocInfo.enmSynchType = enmSynchType;
147
148 HRESULT hr = pPrivate->Callbacks.pfnD3DKMTCreateAllocation(&Buf.DdiAlloc);
149 Assert(hr == S_OK);
150 if (hr == S_OK)
151 {
152 InitializeCriticalSection(&pBuf->CritSect);
153
154 Assert(Buf.DdiAllocInfo.hAllocation);
155 pBuf->BasePrivate.Base.pfnLock = vboxUhgsmiKmtBufferLock;
156 pBuf->BasePrivate.Base.pfnUnlock = vboxUhgsmiKmtBufferUnlock;
157// pBuf->Base.pfnAdjustValidDataRange = vboxUhgsmiKmtBufferAdjustValidDataRange;
158 pBuf->BasePrivate.Base.pfnDestroy = vboxUhgsmiKmtBufferDestroy;
159
160 pBuf->BasePrivate.Base.hSynch = hSynch;
161 pBuf->BasePrivate.Base.enmSynchType = enmSynchType;
162 pBuf->BasePrivate.Base.cbBuffer = cbBuf;
163 pBuf->BasePrivate.Base.bSynchCreated = bSynchCreated;
164
165 pBuf->pHgsmi = pPrivate;
166 pBuf->BasePrivate.hAllocation = Buf.DdiAllocInfo.hAllocation;
167
168 *ppBuf = &pBuf->BasePrivate.Base;
169
170 return VINF_SUCCESS;
171 }
172 else
173 {
174 rc = VERR_OUT_OF_RESOURCES;
175 }
176
177 RTMemFree(pBuf);
178 }
179 else
180 rc = VERR_NO_MEMORY;
181
182 if (bSynchCreated)
183 CloseHandle(hSynch);
184
185 return rc;
186}
187
188DECLCALLBACK(int) vboxUhgsmiKmtBufferSubmitAsynch(PVBOXUHGSMI pHgsmi, PVBOXUHGSMI_BUFFER_SUBMIT aBuffers, uint32_t cBuffers)
189{
190 PVBOXUHGSMI_PRIVATE_KMT pHg = VBOXUHGSMIKMT_GET(pHgsmi);
191 UINT cbDmaCmd = pHg->Context.CommandBufferSize;
192 int rc = vboxUhgsmiBaseDmaFill(aBuffers, cBuffers,
193 pHg->Context.pCommandBuffer, &cbDmaCmd,
194 pHg->Context.pAllocationList, pHg->Context.AllocationListSize,
195 pHg->Context.pPatchLocationList, pHg->Context.PatchLocationListSize);
196 AssertRC(rc);
197 if (RT_FAILURE(rc))
198 return rc;
199
200 D3DKMT_RENDER DdiRender = {0};
201 DdiRender.hContext = pHg->Context.hContext;
202 DdiRender.CommandLength = cbDmaCmd;
203 DdiRender.AllocationCount = cBuffers;
204 Assert(DdiRender.CommandLength);
205 Assert(DdiRender.CommandLength < UINT32_MAX/2);
206
207 HRESULT hr = pHg->Callbacks.pfnD3DKMTRender(&DdiRender);
208 Assert(hr == S_OK);
209 if (hr == S_OK)
210 {
211 pHg->Context.CommandBufferSize = DdiRender.NewCommandBufferSize;
212 pHg->Context.pCommandBuffer = DdiRender.pNewCommandBuffer;
213 pHg->Context.AllocationListSize = DdiRender.NewAllocationListSize;
214 pHg->Context.pAllocationList = DdiRender.pNewAllocationList;
215 pHg->Context.PatchLocationListSize = DdiRender.NewPatchLocationListSize;
216 pHg->Context.pPatchLocationList = DdiRender.pNewPatchLocationList;
217
218 return VINF_SUCCESS;
219 }
220
221 return VERR_GENERAL_FAILURE;
222}
223
224
225DECLCALLBACK(int) vboxUhgsmiKmtEscBufferLock(PVBOXUHGSMI_BUFFER pBuf, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags, void**pvLock)
226{
227 PVBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC pBuffer = VBOXUHGSMIKMTESC_GET_BUFFER(pBuf);
228 *pvLock = pBuffer->Alloc.pvData + offLock;
229 return VINF_SUCCESS;
230}
231
232DECLCALLBACK(int) vboxUhgsmiKmtEscBufferUnlock(PVBOXUHGSMI_BUFFER pBuf)
233{
234 return VINF_SUCCESS;
235}
236
237DECLCALLBACK(int) vboxUhgsmiKmtEscBufferDestroy(PVBOXUHGSMI_BUFFER pBuf)
238{
239 PVBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC pBuffer = VBOXUHGSMIKMTESC_GET_BUFFER(pBuf);
240 PVBOXUHGSMI_PRIVATE_KMT pPrivate = pBuffer->pHgsmi;
241 D3DKMT_ESCAPE DdiEscape = {0};
242 VBOXDISPIFESCAPE_UHGSMI_DEALLOCATE DeallocInfo = {0};
243 DdiEscape.hAdapter = pPrivate->Adapter.hAdapter;
244 DdiEscape.hDevice = pPrivate->Device.hDevice;
245 DdiEscape.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
246 //Buf.DdiEscape.Flags.HardwareAccess = 1;
247 DdiEscape.pPrivateDriverData = &DeallocInfo;
248 DdiEscape.PrivateDriverDataSize = sizeof (DeallocInfo);
249 DdiEscape.hContext = pPrivate->Context.hContext;
250
251 DeallocInfo.EscapeHdr.escapeCode = VBOXESC_UHGSMI_DEALLOCATE;
252 DeallocInfo.hAlloc = pBuffer->Alloc.hAlloc;
253
254 HRESULT hr = pPrivate->Callbacks.pfnD3DKMTEscape(&DdiEscape);
255 Assert(hr == S_OK);
256 if (hr == S_OK)
257 {
258 if (pBuffer->Base.bSynchCreated)
259 {
260 CloseHandle(pBuffer->Base.hSynch);
261 }
262 RTMemFree(pBuffer);
263 return VINF_SUCCESS;
264 }
265
266 return VERR_GENERAL_FAILURE;
267}
268
269DECLCALLBACK(int) vboxUhgsmiKmtEscBufferCreate(PVBOXUHGSMI pHgsmi, uint32_t cbBuf,
270 VBOXUHGSMI_SYNCHOBJECT_TYPE enmSynchType, HVBOXUHGSMI_SYNCHOBJECT hSynch,
271 PVBOXUHGSMI_BUFFER* ppBuf)
272{
273 bool bSynchCreated = false;
274 if (!cbBuf)
275 return VERR_INVALID_PARAMETER;
276
277 int rc = vboxUhgsmiBaseEventChkCreate(enmSynchType, &hSynch, &bSynchCreated);
278 AssertRC(rc);
279 if (RT_FAILURE(rc))
280 return rc;
281
282 cbBuf = VBOXWDDM_ROUNDBOUND(cbBuf, 0x1000);
283 Assert(cbBuf);
284 uint32_t cPages = cbBuf >> 12;
285 Assert(cPages);
286
287 PVBOXUHGSMI_PRIVATE_KMT pPrivate = VBOXUHGSMIKMT_GET(pHgsmi);
288 PVBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC pBuf = (PVBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC)RTMemAllocZ(RT_OFFSETOF(VBOXUHGSMI_BUFFER_PRIVATE_KMT, aLockPageIndices[cPages]));
289 Assert(pBuf);
290 if (pBuf)
291 {
292 struct
293 {
294 D3DKMT_ESCAPE DdiEscape;
295 VBOXDISPIFESCAPE_UHGSMI_ALLOCATE AllocInfo;
296 } Buf;
297 memset(&Buf, 0, sizeof (Buf));
298 Buf.DdiEscape.hAdapter = pPrivate->Adapter.hAdapter;
299 Buf.DdiEscape.hDevice = pPrivate->Device.hDevice;
300 Buf.DdiEscape.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
301 //Buf.DdiEscape.Flags.HardwareAccess = 1;
302 Buf.DdiEscape.pPrivateDriverData = &Buf.AllocInfo;
303 Buf.DdiEscape.PrivateDriverDataSize = sizeof (Buf.AllocInfo);
304 Buf.DdiEscape.hContext = pPrivate->Context.hContext;
305
306 Buf.AllocInfo.EscapeHdr.escapeCode = VBOXESC_UHGSMI_ALLOCATE;
307 Buf.AllocInfo.Alloc.cbData = cbBuf;
308 Buf.AllocInfo.Alloc.hSynch = hSynch;
309 Buf.AllocInfo.Alloc.enmSynchType = enmSynchType;
310
311 HRESULT hr = pPrivate->Callbacks.pfnD3DKMTEscape(&Buf.DdiEscape);
312 Assert(hr == S_OK);
313 if (hr == S_OK)
314 {
315 pBuf->Alloc = Buf.AllocInfo.Alloc;
316 Assert(pBuf->Alloc.pvData);
317 pBuf->pHgsmi = pPrivate;
318 pBuf->Base.pfnLock = vboxUhgsmiKmtEscBufferLock;
319 pBuf->Base.pfnUnlock = vboxUhgsmiKmtEscBufferUnlock;
320// pBuf->Base.pfnAdjustValidDataRange = vboxUhgsmiKmtBufferAdjustValidDataRange;
321 pBuf->Base.pfnDestroy = vboxUhgsmiKmtEscBufferDestroy;
322
323 pBuf->Base.hSynch = hSynch;
324 pBuf->Base.enmSynchType = enmSynchType;
325 pBuf->Base.cbBuffer = Buf.AllocInfo.Alloc.cbData;
326 pBuf->Base.bSynchCreated = bSynchCreated;
327
328 *ppBuf = &pBuf->Base;
329
330 return VINF_SUCCESS;
331 }
332 else
333 {
334 rc = VERR_OUT_OF_RESOURCES;
335 }
336
337 RTMemFree(pBuf);
338 }
339 else
340 rc = VERR_NO_MEMORY;
341
342 if (bSynchCreated)
343 CloseHandle(hSynch);
344
345 return rc;
346}
347
348DECLCALLBACK(int) vboxUhgsmiKmtEscBufferSubmitAsynch(PVBOXUHGSMI pHgsmi, PVBOXUHGSMI_BUFFER_SUBMIT aBuffers, uint32_t cBuffers)
349{
350 /* we no chromium will not submit more than three buffers actually,
351 * for simplicity allocate it statically on the stack */
352 struct
353 {
354 VBOXDISPIFESCAPE_UHGSMI_SUBMIT SubmitInfo;
355 VBOXWDDM_UHGSMI_BUFFER_UI_INFO_ESCAPE aBufInfos[3];
356 } Buf;
357
358 if (cBuffers > RT_ELEMENTS(Buf.aBufInfos) + 1)
359 {
360 Assert(0);
361 return VERR_INVALID_PARAMETER;
362 }
363
364
365 PVBOXUHGSMI_PRIVATE_KMT pPrivate = VBOXUHGSMIKMT_GET(pHgsmi);
366 D3DKMT_ESCAPE DdiEscape = {0};
367
368 DdiEscape.hAdapter = pPrivate->Adapter.hAdapter;
369 DdiEscape.hDevice = pPrivate->Device.hDevice;
370 DdiEscape.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
371 //Buf.DdiEscape.Flags.HardwareAccess = 1;
372 DdiEscape.pPrivateDriverData = &Buf.SubmitInfo;
373 DdiEscape.PrivateDriverDataSize = RT_OFFSETOF(VBOXDISPIFESCAPE_UHGSMI_SUBMIT, aBuffers[cBuffers]);
374 DdiEscape.hContext = pPrivate->Context.hContext;
375
376 Buf.SubmitInfo.EscapeHdr.escapeCode = VBOXESC_UHGSMI_SUBMIT;
377 Buf.SubmitInfo.EscapeHdr.u32CmdSpecific = cBuffers;
378 for (UINT i = 0; i < cBuffers; ++i)
379 {
380 VBOXWDDM_UHGSMI_BUFFER_UI_INFO_ESCAPE *pSubmInfo = &Buf.SubmitInfo.aBuffers[i];
381 PVBOXUHGSMI_BUFFER_SUBMIT pBufInfo = &aBuffers[i];
382 PVBOXUHGSMI_BUFFER_PRIVATE_KMT_ESC pBuf = VBOXUHGSMIKMTESC_GET_BUFFER(pBufInfo->pBuf);
383 pSubmInfo->hAlloc = pBuf->Alloc.hAlloc;
384 pSubmInfo->Info.fSubFlags = pBufInfo->fFlags;
385 if (pBufInfo->fFlags.bEntireBuffer)
386 {
387 pSubmInfo->Info.offData = 0;
388 pSubmInfo->Info.cbData = pBuf->Base.cbBuffer;
389 }
390 else
391 {
392 pSubmInfo->Info.offData = pBufInfo->offData;
393 pSubmInfo->Info.cbData = pBufInfo->cbData;
394 }
395 }
396
397 HRESULT hr = pPrivate->Callbacks.pfnD3DKMTEscape(&DdiEscape);
398 Assert(hr == S_OK);
399 if (hr == S_OK)
400 {
401 return VINF_SUCCESS;
402 }
403
404 return VERR_GENERAL_FAILURE;
405}
406
407static HRESULT vboxUhgsmiKmtEngineCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
408{
409 HRESULT hr = vboxDispKmtCallbacksInit(&pHgsmi->Callbacks);
410 Assert(hr == S_OK);
411 if (hr == S_OK)
412 {
413 hr = vboxDispKmtOpenAdapter(&pHgsmi->Callbacks, &pHgsmi->Adapter);
414 Assert(hr == S_OK);
415 if (hr == S_OK)
416 {
417 hr = vboxDispKmtCreateDevice(&pHgsmi->Adapter, &pHgsmi->Device);
418 Assert(hr == S_OK);
419 if (hr == S_OK)
420 {
421 hr = vboxDispKmtCreateContext(&pHgsmi->Device, &pHgsmi->Context, bD3D);
422 Assert(hr == S_OK);
423 if (hr == S_OK)
424 {
425 return S_OK;
426 }
427 vboxDispKmtDestroyDevice(&pHgsmi->Device);
428 }
429 vboxDispKmtCloseAdapter(&pHgsmi->Adapter);
430 }
431 vboxDispKmtCallbacksTerm(&pHgsmi->Callbacks);
432 }
433 return hr;
434}
435HRESULT vboxUhgsmiKmtCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
436{
437 pHgsmi->BasePrivate.Base.pfnBufferCreate = vboxUhgsmiKmtBufferCreate;
438 pHgsmi->BasePrivate.Base.pfnBufferSubmitAsynch = vboxUhgsmiKmtBufferSubmitAsynch;
439#ifdef VBOX_CRHGSMI_WITH_D3DDEV
440 pHgsmi->BasePrivate.hClient = NULL;
441#endif
442 return vboxUhgsmiKmtEngineCreate(pHgsmi, bD3D);
443}
444
445HRESULT vboxUhgsmiKmtEscCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
446{
447 pHgsmi->BasePrivate.Base.pfnBufferCreate = vboxUhgsmiKmtEscBufferCreate;
448 pHgsmi->BasePrivate.Base.pfnBufferSubmitAsynch = vboxUhgsmiKmtEscBufferSubmitAsynch;
449#ifdef VBOX_CRHGSMI_WITH_D3DDEV
450 pHgsmi->BasePrivate.hClient = NULL;
451#endif
452 return vboxUhgsmiKmtEngineCreate(pHgsmi, bD3D);
453}
454
455HRESULT vboxUhgsmiKmtDestroy(PVBOXUHGSMI_PRIVATE_KMT pHgsmi)
456{
457 Assert(0);
458 HRESULT hr = vboxDispKmtDestroyContext(&pHgsmi->Context);
459 Assert(hr == S_OK);
460 if (hr == S_OK)
461 {
462 hr = vboxDispKmtDestroyDevice(&pHgsmi->Device);
463 Assert(hr == S_OK);
464 if (hr == S_OK)
465 {
466 hr = vboxDispKmtCloseAdapter(&pHgsmi->Adapter);
467 Assert(hr == S_OK);
468 if (hr == S_OK)
469 {
470 hr = vboxDispKmtCallbacksTerm(&pHgsmi->Callbacks);
471 Assert(hr == S_OK);
472 if (hr == S_OK)
473 return S_OK;
474 }
475 }
476 }
477 return hr;
478}
479
480HRESULT vboxDispKmtCallbacksInit(PVBOXDISPKMT_CALLBACKS pCallbacks)
481{
482 HRESULT hr = S_OK;
483
484 memset(pCallbacks, 0, sizeof (*pCallbacks));
485
486 pCallbacks->hGdi32 = LoadLibraryW(L"gdi32.dll");
487 if (pCallbacks->hGdi32 != NULL)
488 {
489 bool bSupported = true;
490 pCallbacks->pfnD3DKMTOpenAdapterFromHdc = (PFND3DKMT_OPENADAPTERFROMHDC)GetProcAddress(pCallbacks->hGdi32, "D3DKMTOpenAdapterFromHdc");
491 Log((__FUNCTION__"pfnD3DKMTOpenAdapterFromHdc = %p\n", pCallbacks->pfnD3DKMTOpenAdapterFromHdc));
492 bSupported &= !!(pCallbacks->pfnD3DKMTOpenAdapterFromHdc);
493
494 pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName = (PFND3DKMT_OPENADAPTERFROMGDIDISPLAYNAME)GetProcAddress(pCallbacks->hGdi32, "D3DKMTOpenAdapterFromGdiDisplayName");
495 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName = %p\n", pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName));
496 bSupported &= !!(pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName);
497
498 pCallbacks->pfnD3DKMTCloseAdapter = (PFND3DKMT_CLOSEADAPTER)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCloseAdapter");
499 Log((__FUNCTION__": pfnD3DKMTCloseAdapter = %p\n", pCallbacks->pfnD3DKMTCloseAdapter));
500 bSupported &= !!(pCallbacks->pfnD3DKMTCloseAdapter);
501
502 pCallbacks->pfnD3DKMTEscape = (PFND3DKMT_ESCAPE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTEscape");
503 Log((__FUNCTION__": pfnD3DKMTEscape = %p\n", pCallbacks->pfnD3DKMTEscape));
504 bSupported &= !!(pCallbacks->pfnD3DKMTEscape);
505
506 pCallbacks->pfnD3DKMTCreateDevice = (PFND3DKMT_CREATEDEVICE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateDevice");
507 Log((__FUNCTION__": pfnD3DKMTCreateDevice = %p\n", pCallbacks->pfnD3DKMTCreateDevice));
508 bSupported &= !!(pCallbacks->pfnD3DKMTCreateDevice);
509
510 pCallbacks->pfnD3DKMTDestroyDevice = (PFND3DKMT_DESTROYDEVICE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyDevice");
511 Log((__FUNCTION__": pfnD3DKMTDestroyDevice = %p\n", pCallbacks->pfnD3DKMTDestroyDevice));
512 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyDevice);
513
514 pCallbacks->pfnD3DKMTCreateContext = (PFND3DKMT_CREATECONTEXT)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateContext");
515 Log((__FUNCTION__": pfnD3DKMTCreateContext = %p\n", pCallbacks->pfnD3DKMTCreateContext));
516 bSupported &= !!(pCallbacks->pfnD3DKMTCreateContext);
517
518 pCallbacks->pfnD3DKMTDestroyContext = (PFND3DKMT_DESTROYCONTEXT)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyContext");
519 Log((__FUNCTION__": pfnD3DKMTDestroyContext = %p\n", pCallbacks->pfnD3DKMTDestroyContext));
520 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyContext);
521
522 pCallbacks->pfnD3DKMTRender = (PFND3DKMT_RENDER)GetProcAddress(pCallbacks->hGdi32, "D3DKMTRender");
523 Log((__FUNCTION__": pfnD3DKMTRender = %p\n", pCallbacks->pfnD3DKMTRender));
524 bSupported &= !!(pCallbacks->pfnD3DKMTRender);
525
526 pCallbacks->pfnD3DKMTCreateAllocation = (PFND3DKMT_CREATEALLOCATION)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateAllocation");
527 Log((__FUNCTION__": pfnD3DKMTCreateAllocation = %p\n", pCallbacks->pfnD3DKMTCreateAllocation));
528 bSupported &= !!(pCallbacks->pfnD3DKMTCreateAllocation);
529
530 pCallbacks->pfnD3DKMTDestroyAllocation = (PFND3DKMT_DESTROYALLOCATION)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyAllocation");
531 Log((__FUNCTION__": pfnD3DKMTDestroyAllocation = %p\n", pCallbacks->pfnD3DKMTDestroyAllocation));
532 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyAllocation);
533
534 pCallbacks->pfnD3DKMTLock = (PFND3DKMT_LOCK)GetProcAddress(pCallbacks->hGdi32, "D3DKMTLock");
535 Log((__FUNCTION__": pfnD3DKMTLock = %p\n", pCallbacks->pfnD3DKMTLock));
536 bSupported &= !!(pCallbacks->pfnD3DKMTLock);
537
538 pCallbacks->pfnD3DKMTUnlock = (PFND3DKMT_UNLOCK)GetProcAddress(pCallbacks->hGdi32, "D3DKMTUnlock");
539 Log((__FUNCTION__": pfnD3DKMTUnlock = %p\n", pCallbacks->pfnD3DKMTUnlock));
540 bSupported &= !!(pCallbacks->pfnD3DKMTUnlock);
541
542 Assert(bSupported);
543 if (bSupported)
544 {
545 return S_OK;
546 }
547 else
548 {
549 Log((__FUNCTION__": one of pfnD3DKMT function pointers failed to initialize\n"));
550 hr = E_NOINTERFACE;
551 }
552
553 FreeLibrary(pCallbacks->hGdi32);
554 }
555 else
556 {
557 DWORD winEr = GetLastError();
558 hr = HRESULT_FROM_WIN32(winEr);
559 Assert(0);
560 Assert(hr != S_OK);
561 Assert(hr != S_FALSE);
562 if (hr == S_OK || hr == S_FALSE)
563 hr = E_FAIL;
564 }
565
566 return hr;
567}
568
569HRESULT vboxDispKmtCallbacksTerm(PVBOXDISPKMT_CALLBACKS pCallbacks)
570{
571 FreeLibrary(pCallbacks->hGdi32);
572 return S_OK;
573}
574
575HRESULT vboxDispKmtOpenAdapter(PVBOXDISPKMT_CALLBACKS pCallbacks, PVBOXDISPKMT_ADAPTER pAdapter)
576{
577 D3DKMT_OPENADAPTERFROMGDIDISPLAYNAME OpenAdapterData = {0};
578 wcsncpy(OpenAdapterData.DeviceName, L"\\\\.\\DISPLAY1", RT_ELEMENTS(OpenAdapterData.DeviceName) - 1 /* the last one is always \0 */);
579 HRESULT hr = S_OK;
580 NTSTATUS Status = pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName(&OpenAdapterData);
581 Assert(!Status);
582 if (!Status)
583 {
584 pAdapter->hAdapter = OpenAdapterData.hAdapter;
585 pAdapter->pCallbacks = pCallbacks;
586 return S_OK;
587 }
588 else
589 {
590 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName failed, Status (0x%x)\n", Status));
591 hr = E_FAIL;
592 }
593
594 return hr;
595
596}
597
598HRESULT vboxDispKmtCloseAdapter(PVBOXDISPKMT_ADAPTER pAdapter)
599{
600 D3DKMT_CLOSEADAPTER ClosaAdapterData = {0};
601 ClosaAdapterData.hAdapter = pAdapter->hAdapter;
602 NTSTATUS Status = pAdapter->pCallbacks->pfnD3DKMTCloseAdapter(&ClosaAdapterData);
603 Assert(!Status);
604 if (!Status)
605 {
606 return S_OK;
607 }
608
609 Log((__FUNCTION__": pfnD3DKMTCloseAdapter failed, Status (0x%x)\n", Status));
610 /* ignore */
611 Status = 0;
612 return E_FAIL;
613}
614
615HRESULT vboxDispKmtCreateDevice(PVBOXDISPKMT_ADAPTER pAdapter, PVBOXDISPKMT_DEVICE pDevice)
616{
617 D3DKMT_CREATEDEVICE CreateDeviceData = {0};
618 CreateDeviceData.hAdapter = pAdapter->hAdapter;
619 NTSTATUS Status = pAdapter->pCallbacks->pfnD3DKMTCreateDevice(&CreateDeviceData);
620 Assert(!Status);
621 if (!Status)
622 {
623 pDevice->pAdapter = pAdapter;
624 pDevice->hDevice = CreateDeviceData.hDevice;
625 pDevice->pCommandBuffer = CreateDeviceData.pCommandBuffer;
626 pDevice->CommandBufferSize = CreateDeviceData.CommandBufferSize;
627 pDevice->pAllocationList = CreateDeviceData.pAllocationList;
628 pDevice->AllocationListSize = CreateDeviceData.AllocationListSize;
629 pDevice->pPatchLocationList = CreateDeviceData.pPatchLocationList;
630 pDevice->PatchLocationListSize = CreateDeviceData.PatchLocationListSize;
631
632 return S_OK;
633 }
634
635 return E_FAIL;
636}
637
638HRESULT vboxDispKmtDestroyDevice(PVBOXDISPKMT_DEVICE pDevice)
639{
640 D3DKMT_DESTROYDEVICE DestroyDeviceData = {0};
641 DestroyDeviceData.hDevice = pDevice->hDevice;
642 NTSTATUS Status = pDevice->pAdapter->pCallbacks->pfnD3DKMTDestroyDevice(&DestroyDeviceData);
643 Assert(!Status);
644 if (!Status)
645 {
646 return S_OK;
647 }
648 return E_FAIL;
649}
650
651HRESULT vboxDispKmtCreateContext(PVBOXDISPKMT_DEVICE pDevice, PVBOXDISPKMT_CONTEXT pContext, BOOL bD3D)
652{
653 VBOXWDDM_CREATECONTEXT_INFO Info = {0};
654 Info.u32IfVersion = 9;
655 Info.enmType = bD3D ? VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_3D : VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_GL;
656 D3DKMT_CREATECONTEXT ContextData = {0};
657 ContextData.hDevice = pDevice->hDevice;
658 ContextData.NodeOrdinal = 0;
659 ContextData.EngineAffinity = 0;
660 ContextData.pPrivateDriverData = &Info;
661 ContextData.PrivateDriverDataSize = sizeof (Info);
662 ContextData.ClientHint = bD3D ? D3DKMT_CLIENTHINT_DX9 : D3DKMT_CLIENTHINT_OPENGL;
663 NTSTATUS Status = pDevice->pAdapter->pCallbacks->pfnD3DKMTCreateContext(&ContextData);
664 Assert(!Status);
665 if (!Status)
666 {
667 pContext->pDevice = pDevice;
668 pContext->hContext = ContextData.hContext;
669 pContext->pCommandBuffer = ContextData.pCommandBuffer;
670 pContext->CommandBufferSize = ContextData.CommandBufferSize;
671 pContext->pAllocationList = ContextData.pAllocationList;
672 pContext->AllocationListSize = ContextData.AllocationListSize;
673 pContext->pPatchLocationList = ContextData.pPatchLocationList;
674 pContext->PatchLocationListSize = ContextData.PatchLocationListSize;
675 return S_OK;
676 }
677 return E_FAIL;
678}
679
680HRESULT vboxDispKmtDestroyContext(PVBOXDISPKMT_CONTEXT pContext)
681{
682 D3DKMT_DESTROYCONTEXT DestroyContextData = {0};
683 DestroyContextData.hContext = pContext->hContext;
684 NTSTATUS Status = pContext->pDevice->pAdapter->pCallbacks->pfnD3DKMTDestroyContext(&DestroyContextData);
685 Assert(!Status);
686 if (!Status)
687 return S_OK;
688 return E_FAIL;
689}
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