VirtualBox

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

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

wddm/3d: propper hide host window on swapchain destruction

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