VirtualBox

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

Last change on this file since 35319 was 35319, checked in by vboxsync, 14 years ago
  1. wddm: 64bit support 2. wddm: installation fixes (64bit support, unsigned driver warning) 3. propper fix for #5438 (compile with disabled CROGL)
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.6 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 = (uint64_t)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 = (void*)(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 = (uint64_t)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#ifdef DEBUG_misha
415 /* may fail with xpdm driver */
416 Assert(hr == S_OK);
417#endif
418 if (hr == S_OK)
419 {
420 hr = vboxDispKmtCreateDevice(&pHgsmi->Adapter, &pHgsmi->Device);
421 Assert(hr == S_OK);
422 if (hr == S_OK)
423 {
424 hr = vboxDispKmtCreateContext(&pHgsmi->Device, &pHgsmi->Context,
425 bD3D ? VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_3D : VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_GL,
426 NULL, 0);
427 Assert(hr == S_OK);
428 if (hr == S_OK)
429 {
430 return S_OK;
431 }
432 vboxDispKmtDestroyDevice(&pHgsmi->Device);
433 }
434 vboxDispKmtCloseAdapter(&pHgsmi->Adapter);
435 }
436 vboxDispKmtCallbacksTerm(&pHgsmi->Callbacks);
437 }
438 return hr;
439}
440HRESULT vboxUhgsmiKmtCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
441{
442 pHgsmi->BasePrivate.Base.pfnBufferCreate = vboxUhgsmiKmtBufferCreate;
443 pHgsmi->BasePrivate.Base.pfnBufferSubmitAsynch = vboxUhgsmiKmtBufferSubmitAsynch;
444#ifdef VBOX_CRHGSMI_WITH_D3DDEV
445 pHgsmi->BasePrivate.hClient = NULL;
446#endif
447 return vboxUhgsmiKmtEngineCreate(pHgsmi, bD3D);
448}
449
450HRESULT vboxUhgsmiKmtEscCreate(PVBOXUHGSMI_PRIVATE_KMT pHgsmi, BOOL bD3D)
451{
452 pHgsmi->BasePrivate.Base.pfnBufferCreate = vboxUhgsmiKmtEscBufferCreate;
453 pHgsmi->BasePrivate.Base.pfnBufferSubmitAsynch = vboxUhgsmiKmtEscBufferSubmitAsynch;
454#ifdef VBOX_CRHGSMI_WITH_D3DDEV
455 pHgsmi->BasePrivate.hClient = NULL;
456#endif
457 return vboxUhgsmiKmtEngineCreate(pHgsmi, bD3D);
458}
459
460HRESULT vboxUhgsmiKmtDestroy(PVBOXUHGSMI_PRIVATE_KMT pHgsmi)
461{
462 HRESULT hr = vboxDispKmtDestroyContext(&pHgsmi->Context);
463 Assert(hr == S_OK);
464 if (hr == S_OK)
465 {
466 hr = vboxDispKmtDestroyDevice(&pHgsmi->Device);
467 Assert(hr == S_OK);
468 if (hr == S_OK)
469 {
470 hr = vboxDispKmtCloseAdapter(&pHgsmi->Adapter);
471 Assert(hr == S_OK);
472 if (hr == S_OK)
473 {
474 hr = vboxDispKmtCallbacksTerm(&pHgsmi->Callbacks);
475 Assert(hr == S_OK);
476 if (hr == S_OK)
477 return S_OK;
478 }
479 }
480 }
481 return hr;
482}
483
484HRESULT vboxDispKmtCallbacksInit(PVBOXDISPKMT_CALLBACKS pCallbacks)
485{
486 HRESULT hr = S_OK;
487
488 memset(pCallbacks, 0, sizeof (*pCallbacks));
489
490 pCallbacks->hGdi32 = LoadLibraryW(L"gdi32.dll");
491 if (pCallbacks->hGdi32 != NULL)
492 {
493 bool bSupported = true;
494 pCallbacks->pfnD3DKMTOpenAdapterFromHdc = (PFND3DKMT_OPENADAPTERFROMHDC)GetProcAddress(pCallbacks->hGdi32, "D3DKMTOpenAdapterFromHdc");
495 Log((__FUNCTION__"pfnD3DKMTOpenAdapterFromHdc = %p\n", pCallbacks->pfnD3DKMTOpenAdapterFromHdc));
496 bSupported &= !!(pCallbacks->pfnD3DKMTOpenAdapterFromHdc);
497
498 pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName = (PFND3DKMT_OPENADAPTERFROMGDIDISPLAYNAME)GetProcAddress(pCallbacks->hGdi32, "D3DKMTOpenAdapterFromGdiDisplayName");
499 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName = %p\n", pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName));
500 bSupported &= !!(pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName);
501
502 pCallbacks->pfnD3DKMTCloseAdapter = (PFND3DKMT_CLOSEADAPTER)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCloseAdapter");
503 Log((__FUNCTION__": pfnD3DKMTCloseAdapter = %p\n", pCallbacks->pfnD3DKMTCloseAdapter));
504 bSupported &= !!(pCallbacks->pfnD3DKMTCloseAdapter);
505
506 pCallbacks->pfnD3DKMTEscape = (PFND3DKMT_ESCAPE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTEscape");
507 Log((__FUNCTION__": pfnD3DKMTEscape = %p\n", pCallbacks->pfnD3DKMTEscape));
508 bSupported &= !!(pCallbacks->pfnD3DKMTEscape);
509
510 pCallbacks->pfnD3DKMTCreateDevice = (PFND3DKMT_CREATEDEVICE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateDevice");
511 Log((__FUNCTION__": pfnD3DKMTCreateDevice = %p\n", pCallbacks->pfnD3DKMTCreateDevice));
512 bSupported &= !!(pCallbacks->pfnD3DKMTCreateDevice);
513
514 pCallbacks->pfnD3DKMTDestroyDevice = (PFND3DKMT_DESTROYDEVICE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyDevice");
515 Log((__FUNCTION__": pfnD3DKMTDestroyDevice = %p\n", pCallbacks->pfnD3DKMTDestroyDevice));
516 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyDevice);
517
518 pCallbacks->pfnD3DKMTCreateContext = (PFND3DKMT_CREATECONTEXT)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateContext");
519 Log((__FUNCTION__": pfnD3DKMTCreateContext = %p\n", pCallbacks->pfnD3DKMTCreateContext));
520 bSupported &= !!(pCallbacks->pfnD3DKMTCreateContext);
521
522 pCallbacks->pfnD3DKMTDestroyContext = (PFND3DKMT_DESTROYCONTEXT)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyContext");
523 Log((__FUNCTION__": pfnD3DKMTDestroyContext = %p\n", pCallbacks->pfnD3DKMTDestroyContext));
524 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyContext);
525
526 pCallbacks->pfnD3DKMTRender = (PFND3DKMT_RENDER)GetProcAddress(pCallbacks->hGdi32, "D3DKMTRender");
527 Log((__FUNCTION__": pfnD3DKMTRender = %p\n", pCallbacks->pfnD3DKMTRender));
528 bSupported &= !!(pCallbacks->pfnD3DKMTRender);
529
530 pCallbacks->pfnD3DKMTCreateAllocation = (PFND3DKMT_CREATEALLOCATION)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateAllocation");
531 Log((__FUNCTION__": pfnD3DKMTCreateAllocation = %p\n", pCallbacks->pfnD3DKMTCreateAllocation));
532 bSupported &= !!(pCallbacks->pfnD3DKMTCreateAllocation);
533
534 pCallbacks->pfnD3DKMTDestroyAllocation = (PFND3DKMT_DESTROYALLOCATION)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyAllocation");
535 Log((__FUNCTION__": pfnD3DKMTDestroyAllocation = %p\n", pCallbacks->pfnD3DKMTDestroyAllocation));
536 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyAllocation);
537
538 pCallbacks->pfnD3DKMTLock = (PFND3DKMT_LOCK)GetProcAddress(pCallbacks->hGdi32, "D3DKMTLock");
539 Log((__FUNCTION__": pfnD3DKMTLock = %p\n", pCallbacks->pfnD3DKMTLock));
540 bSupported &= !!(pCallbacks->pfnD3DKMTLock);
541
542 pCallbacks->pfnD3DKMTUnlock = (PFND3DKMT_UNLOCK)GetProcAddress(pCallbacks->hGdi32, "D3DKMTUnlock");
543 Log((__FUNCTION__": pfnD3DKMTUnlock = %p\n", pCallbacks->pfnD3DKMTUnlock));
544 bSupported &= !!(pCallbacks->pfnD3DKMTUnlock);
545
546 /*Assert(bSupported);*/
547 if (bSupported)
548 {
549 return S_OK;
550 }
551 else
552 {
553 Log((__FUNCTION__": one of pfnD3DKMT function pointers failed to initialize\n"));
554 hr = E_NOINTERFACE;
555 }
556
557 FreeLibrary(pCallbacks->hGdi32);
558 }
559 else
560 {
561 DWORD winEr = GetLastError();
562 hr = HRESULT_FROM_WIN32(winEr);
563 Assert(0);
564 Assert(hr != S_OK);
565 Assert(hr != S_FALSE);
566 if (hr == S_OK || hr == S_FALSE)
567 hr = E_FAIL;
568 }
569
570 return hr;
571}
572
573HRESULT vboxDispKmtCallbacksTerm(PVBOXDISPKMT_CALLBACKS pCallbacks)
574{
575 FreeLibrary(pCallbacks->hGdi32);
576 return S_OK;
577}
578
579HRESULT vboxDispKmtAdpHdcCreate(HDC *phDc)
580{
581 HRESULT hr = E_FAIL;
582 DISPLAY_DEVICE DDev;
583 memset(&DDev, 0, sizeof (DDev));
584 DDev.cb = sizeof (DDev);
585
586 *phDc = NULL;
587
588 for (int i = 0; ; ++i)
589 {
590 if (EnumDisplayDevices(NULL, /* LPCTSTR lpDevice */ i, /* DWORD iDevNum */
591 &DDev, 0 /* DWORD dwFlags*/))
592 {
593 if (DDev.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
594 {
595 HDC hDc = CreateDC(NULL, DDev.DeviceName, NULL, NULL);
596 if (hDc)
597 {
598 *phDc = hDc;
599 return S_OK;
600 }
601 else
602 {
603 DWORD winEr = GetLastError();
604 Assert(0);
605 hr = HRESULT_FROM_WIN32(winEr);
606 Assert(FAILED(hr));
607 break;
608 }
609 }
610 }
611 else
612 {
613 DWORD winEr = GetLastError();
614#ifdef DEBUG_misha
615 Assert(0);
616#endif
617 Log((__FUNCTION__": EnumDisplayDevices failed, winEr (%d)\n", winEr));
618 hr = HRESULT_FROM_WIN32(winEr);
619 Assert(FAILED(hr));
620 break;
621 }
622 }
623
624 return hr;
625}
626
627HRESULT vboxDispKmtOpenAdapter(PVBOXDISPKMT_CALLBACKS pCallbacks, PVBOXDISPKMT_ADAPTER pAdapter)
628{
629 D3DKMT_OPENADAPTERFROMHDC OpenAdapterData = {0};
630 HRESULT hr = vboxDispKmtAdpHdcCreate(&OpenAdapterData.hDc);
631 if (hr == S_OK)
632 {
633 Assert(OpenAdapterData.hDc);
634 NTSTATUS Status = pCallbacks->pfnD3DKMTOpenAdapterFromHdc(&OpenAdapterData);
635#ifdef DEBUG_misha
636 /* may fail with xpdm driver */
637 Assert(!Status);
638#endif
639 if (!Status)
640 {
641 pAdapter->hAdapter = OpenAdapterData.hAdapter;
642 pAdapter->hDc = OpenAdapterData.hDc;
643 pAdapter->pCallbacks = pCallbacks;
644 return S_OK;
645 }
646 else
647 {
648 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName failed, Status (0x%x)\n", Status));
649 hr = E_FAIL;
650 }
651
652 DeleteDC(OpenAdapterData.hDc);
653 }
654
655 return hr;
656}
657
658HRESULT vboxDispKmtCloseAdapter(PVBOXDISPKMT_ADAPTER pAdapter)
659{
660 D3DKMT_CLOSEADAPTER ClosaAdapterData = {0};
661 ClosaAdapterData.hAdapter = pAdapter->hAdapter;
662 NTSTATUS Status = pAdapter->pCallbacks->pfnD3DKMTCloseAdapter(&ClosaAdapterData);
663 Assert(!Status);
664 if (!Status)
665 {
666 DeleteDC(pAdapter->hDc);
667 return S_OK;
668 }
669
670 Log((__FUNCTION__": pfnD3DKMTCloseAdapter failed, Status (0x%x)\n", Status));
671
672 return E_FAIL;
673}
674
675HRESULT vboxDispKmtCreateDevice(PVBOXDISPKMT_ADAPTER pAdapter, PVBOXDISPKMT_DEVICE pDevice)
676{
677 D3DKMT_CREATEDEVICE CreateDeviceData = {0};
678 CreateDeviceData.hAdapter = pAdapter->hAdapter;
679 NTSTATUS Status = pAdapter->pCallbacks->pfnD3DKMTCreateDevice(&CreateDeviceData);
680 Assert(!Status);
681 if (!Status)
682 {
683 pDevice->pAdapter = pAdapter;
684 pDevice->hDevice = CreateDeviceData.hDevice;
685 pDevice->pCommandBuffer = CreateDeviceData.pCommandBuffer;
686 pDevice->CommandBufferSize = CreateDeviceData.CommandBufferSize;
687 pDevice->pAllocationList = CreateDeviceData.pAllocationList;
688 pDevice->AllocationListSize = CreateDeviceData.AllocationListSize;
689 pDevice->pPatchLocationList = CreateDeviceData.pPatchLocationList;
690 pDevice->PatchLocationListSize = CreateDeviceData.PatchLocationListSize;
691
692 return S_OK;
693 }
694
695 return E_FAIL;
696}
697
698HRESULT vboxDispKmtDestroyDevice(PVBOXDISPKMT_DEVICE pDevice)
699{
700 D3DKMT_DESTROYDEVICE DestroyDeviceData = {0};
701 DestroyDeviceData.hDevice = pDevice->hDevice;
702 NTSTATUS Status = pDevice->pAdapter->pCallbacks->pfnD3DKMTDestroyDevice(&DestroyDeviceData);
703 Assert(!Status);
704 if (!Status)
705 {
706 return S_OK;
707 }
708 return E_FAIL;
709}
710
711HRESULT vboxDispKmtCreateContext(PVBOXDISPKMT_DEVICE pDevice, PVBOXDISPKMT_CONTEXT pContext,
712 VBOXWDDM_CONTEXT_TYPE enmType, HANDLE hEvent, uint64_t u64UmInfo)
713{
714 VBOXWDDM_CREATECONTEXT_INFO Info = {0};
715 Info.u32IfVersion = 9;
716 Info.enmType = enmType;
717 Info.hUmEvent = (uint64_t)hEvent;
718 Info.u64UmInfo = u64UmInfo;
719 D3DKMT_CREATECONTEXT ContextData = {0};
720 ContextData.hDevice = pDevice->hDevice;
721 ContextData.NodeOrdinal = 0;
722 ContextData.EngineAffinity = 0;
723 ContextData.pPrivateDriverData = &Info;
724 ContextData.PrivateDriverDataSize = sizeof (Info);
725 ContextData.ClientHint = enmType == VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_GL ? D3DKMT_CLIENTHINT_OPENGL : D3DKMT_CLIENTHINT_DX9;
726 NTSTATUS Status = pDevice->pAdapter->pCallbacks->pfnD3DKMTCreateContext(&ContextData);
727 Assert(!Status);
728 if (!Status)
729 {
730 pContext->pDevice = pDevice;
731 pContext->hContext = ContextData.hContext;
732 pContext->pCommandBuffer = ContextData.pCommandBuffer;
733 pContext->CommandBufferSize = ContextData.CommandBufferSize;
734 pContext->pAllocationList = ContextData.pAllocationList;
735 pContext->AllocationListSize = ContextData.AllocationListSize;
736 pContext->pPatchLocationList = ContextData.pPatchLocationList;
737 pContext->PatchLocationListSize = ContextData.PatchLocationListSize;
738 return S_OK;
739 }
740 return E_FAIL;
741}
742
743HRESULT vboxDispKmtDestroyContext(PVBOXDISPKMT_CONTEXT pContext)
744{
745 D3DKMT_DESTROYCONTEXT DestroyContextData = {0};
746 DestroyContextData.hContext = pContext->hContext;
747 NTSTATUS Status = pContext->pDevice->pAdapter->pCallbacks->pfnD3DKMTDestroyContext(&DestroyContextData);
748 Assert(!Status);
749 if (!Status)
750 return S_OK;
751 return E_FAIL;
752}
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