VirtualBox

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

Last change on this file since 48070 was 48070, checked in by vboxsync, 11 years ago

wddm,vboxtray: forward-port autoresize, multimon, and seamless fixes from 4.2 r87071, r87353, r87356, r87528, r87568, r87581, r87584, r87608, r87673, r87678, r87708, r87629, r87529; additional fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
1/* $Id: VBoxDispKmt.cpp 48070 2013-08-26 18:13:22Z vboxsync $ */
2
3/** @file
4 * VBoxVideo Display D3D User mode dll
5 */
6
7/*
8 * Copyright (C) 2011-2012 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 "VBoxDispD3DBase.h"
20#include "VBoxDispKmt.h"
21
22#include <iprt/assert.h>
23#include <iprt/log.h>
24
25#ifndef NT_SUCCESS
26# define NT_SUCCESS(_Status) ((_Status) >= 0)
27#endif
28
29/**
30 * Loads a system DLL.
31 *
32 * @returns Module handle or NULL
33 * @param pszName The DLL name.
34 */
35static HMODULE loadSystemDll(const char *pszName)
36{
37 char szPath[MAX_PATH];
38 UINT cchPath = GetSystemDirectoryA(szPath, sizeof(szPath));
39 size_t cbName = strlen(pszName) + 1;
40 if (cchPath + 1 + cbName > sizeof(szPath))
41 return NULL;
42 szPath[cchPath] = '\\';
43 memcpy(&szPath[cchPath + 1], pszName, cbName);
44 return LoadLibraryA(szPath);
45}
46
47HRESULT vboxDispKmtCallbacksInit(PVBOXDISPKMT_CALLBACKS pCallbacks)
48{
49 HRESULT hr = S_OK;
50
51 memset(pCallbacks, 0, sizeof (*pCallbacks));
52
53 pCallbacks->hGdi32 = loadSystemDll("gdi32.dll");
54 if (pCallbacks->hGdi32 != NULL)
55 {
56 bool bSupported = true;
57 bool bSupportedWin8 = true;
58 pCallbacks->pfnD3DKMTOpenAdapterFromHdc = (PFND3DKMT_OPENADAPTERFROMHDC)GetProcAddress(pCallbacks->hGdi32, "D3DKMTOpenAdapterFromHdc");
59 Log((__FUNCTION__"pfnD3DKMTOpenAdapterFromHdc = %p\n", pCallbacks->pfnD3DKMTOpenAdapterFromHdc));
60 bSupported &= !!(pCallbacks->pfnD3DKMTOpenAdapterFromHdc);
61
62 pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName = (PFND3DKMT_OPENADAPTERFROMGDIDISPLAYNAME)GetProcAddress(pCallbacks->hGdi32, "D3DKMTOpenAdapterFromGdiDisplayName");
63 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName = %p\n", pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName));
64 bSupported &= !!(pCallbacks->pfnD3DKMTOpenAdapterFromGdiDisplayName);
65
66 pCallbacks->pfnD3DKMTCloseAdapter = (PFND3DKMT_CLOSEADAPTER)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCloseAdapter");
67 Log((__FUNCTION__": pfnD3DKMTCloseAdapter = %p\n", pCallbacks->pfnD3DKMTCloseAdapter));
68 bSupported &= !!(pCallbacks->pfnD3DKMTCloseAdapter);
69
70 pCallbacks->pfnD3DKMTEscape = (PFND3DKMT_ESCAPE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTEscape");
71 Log((__FUNCTION__": pfnD3DKMTEscape = %p\n", pCallbacks->pfnD3DKMTEscape));
72 bSupported &= !!(pCallbacks->pfnD3DKMTEscape);
73
74 pCallbacks->pfnD3DKMTCreateDevice = (PFND3DKMT_CREATEDEVICE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateDevice");
75 Log((__FUNCTION__": pfnD3DKMTCreateDevice = %p\n", pCallbacks->pfnD3DKMTCreateDevice));
76 bSupported &= !!(pCallbacks->pfnD3DKMTCreateDevice);
77
78 pCallbacks->pfnD3DKMTDestroyDevice = (PFND3DKMT_DESTROYDEVICE)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyDevice");
79 Log((__FUNCTION__": pfnD3DKMTDestroyDevice = %p\n", pCallbacks->pfnD3DKMTDestroyDevice));
80 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyDevice);
81
82 pCallbacks->pfnD3DKMTCreateContext = (PFND3DKMT_CREATECONTEXT)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateContext");
83 Log((__FUNCTION__": pfnD3DKMTCreateContext = %p\n", pCallbacks->pfnD3DKMTCreateContext));
84 bSupported &= !!(pCallbacks->pfnD3DKMTCreateContext);
85
86 pCallbacks->pfnD3DKMTDestroyContext = (PFND3DKMT_DESTROYCONTEXT)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyContext");
87 Log((__FUNCTION__": pfnD3DKMTDestroyContext = %p\n", pCallbacks->pfnD3DKMTDestroyContext));
88 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyContext);
89
90 pCallbacks->pfnD3DKMTRender = (PFND3DKMT_RENDER)GetProcAddress(pCallbacks->hGdi32, "D3DKMTRender");
91 Log((__FUNCTION__": pfnD3DKMTRender = %p\n", pCallbacks->pfnD3DKMTRender));
92 bSupported &= !!(pCallbacks->pfnD3DKMTRender);
93
94 pCallbacks->pfnD3DKMTCreateAllocation = (PFND3DKMT_CREATEALLOCATION)GetProcAddress(pCallbacks->hGdi32, "D3DKMTCreateAllocation");
95 Log((__FUNCTION__": pfnD3DKMTCreateAllocation = %p\n", pCallbacks->pfnD3DKMTCreateAllocation));
96 bSupported &= !!(pCallbacks->pfnD3DKMTCreateAllocation);
97
98 pCallbacks->pfnD3DKMTDestroyAllocation = (PFND3DKMT_DESTROYALLOCATION)GetProcAddress(pCallbacks->hGdi32, "D3DKMTDestroyAllocation");
99 Log((__FUNCTION__": pfnD3DKMTDestroyAllocation = %p\n", pCallbacks->pfnD3DKMTDestroyAllocation));
100 bSupported &= !!(pCallbacks->pfnD3DKMTDestroyAllocation);
101
102 pCallbacks->pfnD3DKMTLock = (PFND3DKMT_LOCK)GetProcAddress(pCallbacks->hGdi32, "D3DKMTLock");
103 Log((__FUNCTION__": pfnD3DKMTLock = %p\n", pCallbacks->pfnD3DKMTLock));
104 bSupported &= !!(pCallbacks->pfnD3DKMTLock);
105
106 pCallbacks->pfnD3DKMTUnlock = (PFND3DKMT_UNLOCK)GetProcAddress(pCallbacks->hGdi32, "D3DKMTUnlock");
107 Log((__FUNCTION__": pfnD3DKMTUnlock = %p\n", pCallbacks->pfnD3DKMTUnlock));
108 bSupported &= !!(pCallbacks->pfnD3DKMTUnlock);
109
110 pCallbacks->pfnD3DKMTInvalidateActiveVidPn = (PFND3DKMT_INVALIDATEACTIVEVIDPN)GetProcAddress(pCallbacks->hGdi32, "D3DKMTInvalidateActiveVidPn");
111 Log((__FUNCTION__": pfnD3DKMTInvalidateActiveVidPn = %p\n", pCallbacks->pfnD3DKMTInvalidateActiveVidPn));
112 bSupported &= !!(pCallbacks->pfnD3DKMTInvalidateActiveVidPn);
113
114 pCallbacks->pfnD3DKMTPollDisplayChildren = (PFND3DKMT_POLLDISPLAYCHILDREN)GetProcAddress(pCallbacks->hGdi32, "D3DKMTPollDisplayChildren");
115 Log((__FUNCTION__": pfnD3DKMTPollDisplayChildren = %p\n", pCallbacks->pfnD3DKMTPollDisplayChildren));
116 bSupported &= !!(pCallbacks->pfnD3DKMTPollDisplayChildren);
117
118 pCallbacks->pfnD3DKMTEnumAdapters = (PFND3DKMT_ENUMADAPTERS)GetProcAddress(pCallbacks->hGdi32, "D3DKMTEnumAdapters");
119 Log((__FUNCTION__": pfnD3DKMTEnumAdapters = %p\n", pCallbacks->pfnD3DKMTEnumAdapters));
120 /* this present starting win8 release preview only, so keep going if it is not available,
121 * i.e. do not clear the bSupported on its absence */
122 bSupportedWin8 &= !!(pCallbacks->pfnD3DKMTEnumAdapters);
123
124 pCallbacks->pfnD3DKMTOpenAdapterFromLuid = (PFND3DKMT_OPENADAPTERFROMLUID)GetProcAddress(pCallbacks->hGdi32, "D3DKMTOpenAdapterFromLuid");
125 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromLuid = %p\n", pCallbacks->pfnD3DKMTOpenAdapterFromLuid));
126 /* this present starting win8 release preview only, so keep going if it is not available,
127 * i.e. do not clear the bSupported on its absence */
128 bSupportedWin8 &= !!(pCallbacks->pfnD3DKMTOpenAdapterFromLuid);
129
130 /*Assert(bSupported);*/
131 if (bSupported)
132 {
133 if (bSupportedWin8)
134 pCallbacks->enmVersion = VBOXDISPKMT_CALLBACKS_VERSION_WIN8;
135 else
136 pCallbacks->enmVersion = VBOXDISPKMT_CALLBACKS_VERSION_VISTA_WIN7;
137 return S_OK;
138 }
139 else
140 {
141 Log((__FUNCTION__": one of pfnD3DKMT function pointers failed to initialize\n"));
142 hr = E_NOINTERFACE;
143 }
144
145 FreeLibrary(pCallbacks->hGdi32);
146 }
147 else
148 {
149 DWORD winEr = GetLastError();
150 hr = HRESULT_FROM_WIN32(winEr);
151 Assert(0);
152 Assert(hr != S_OK);
153 Assert(hr != S_FALSE);
154 if (hr == S_OK || hr == S_FALSE)
155 hr = E_FAIL;
156 }
157
158 return hr;
159}
160
161HRESULT vboxDispKmtCallbacksTerm(PVBOXDISPKMT_CALLBACKS pCallbacks)
162{
163 FreeLibrary(pCallbacks->hGdi32);
164 return S_OK;
165}
166
167HRESULT vboxDispKmtAdpHdcCreate(HDC *phDc)
168{
169 HRESULT hr = E_FAIL;
170 DISPLAY_DEVICE DDev;
171 memset(&DDev, 0, sizeof (DDev));
172 DDev.cb = sizeof (DDev);
173
174 *phDc = NULL;
175
176 for (int i = 0; ; ++i)
177 {
178 if (EnumDisplayDevices(NULL, /* LPCTSTR lpDevice */ i, /* DWORD iDevNum */
179 &DDev, 0 /* DWORD dwFlags*/))
180 {
181 if (DDev.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
182 {
183 HDC hDc = CreateDC(NULL, DDev.DeviceName, NULL, NULL);
184 if (hDc)
185 {
186 *phDc = hDc;
187 return S_OK;
188 }
189 else
190 {
191 DWORD winEr = GetLastError();
192 Assert(0);
193 hr = HRESULT_FROM_WIN32(winEr);
194 Assert(FAILED(hr));
195 break;
196 }
197 }
198 }
199 else
200 {
201 DWORD winEr = GetLastError();
202// BP_WARN();
203 hr = HRESULT_FROM_WIN32(winEr);
204#ifdef DEBUG_misha
205 Assert(FAILED(hr));
206#endif
207 if (!FAILED(hr))
208 {
209 hr = E_FAIL;
210 }
211 break;
212 }
213 }
214
215 return hr;
216}
217
218static HRESULT vboxDispKmtOpenAdapterViaHdc(const VBOXDISPKMT_CALLBACKS *pCallbacks, PVBOXDISPKMT_ADAPTER pAdapter)
219{
220 D3DKMT_OPENADAPTERFROMHDC OpenAdapterData = {0};
221 HRESULT hr = vboxDispKmtAdpHdcCreate(&OpenAdapterData.hDc);
222 if (!SUCCEEDED(hr))
223 return hr;
224
225 Assert(OpenAdapterData.hDc);
226 NTSTATUS Status = pCallbacks->pfnD3DKMTOpenAdapterFromHdc(&OpenAdapterData);
227 if (NT_SUCCESS(Status))
228 {
229 pAdapter->hAdapter = OpenAdapterData.hAdapter;
230 pAdapter->hDc = OpenAdapterData.hDc;
231 pAdapter->pCallbacks = pCallbacks;
232 memset(&pAdapter->Luid, 0, sizeof (pAdapter->Luid));
233 return S_OK;
234 }
235 else
236 {
237 Log((__FUNCTION__": pfnD3DKMTOpenAdapterFromGdiDisplayName failed, Status (0x%x)\n", Status));
238 hr = E_FAIL;
239 }
240
241 DeleteDC(OpenAdapterData.hDc);
242
243 return hr;
244}
245
246static HRESULT vboxDispKmtOpenAdapterViaLuid(const VBOXDISPKMT_CALLBACKS *pCallbacks, PVBOXDISPKMT_ADAPTER pAdapter)
247{
248 if (pCallbacks->enmVersion < VBOXDISPKMT_CALLBACKS_VERSION_WIN8)
249 return E_NOTIMPL;
250
251 D3DKMT_ENUMADAPTERS EnumAdapters = {0};
252 EnumAdapters.NumAdapters = RT_ELEMENTS(EnumAdapters.Adapters);
253
254 NTSTATUS Status = pCallbacks->pfnD3DKMTEnumAdapters(&EnumAdapters);
255#ifdef DEBUG_misha
256 Assert(!Status);
257#endif
258 if (!NT_SUCCESS(Status))
259 return E_FAIL;
260
261 Assert(EnumAdapters.NumAdapters);
262
263 /* try the same twice: if we fail to open the adapter containing present sources,
264 * try to open any adapter */
265 for (ULONG f = 0; f < 2; ++f)
266 {
267 for (ULONG i = 0; i < EnumAdapters.NumAdapters; ++i)
268 {
269 if (f || EnumAdapters.Adapters[i].NumOfSources)
270 {
271 D3DKMT_OPENADAPTERFROMLUID OpenAdapterData = {0};
272 OpenAdapterData.AdapterLuid = EnumAdapters.Adapters[i].AdapterLuid;
273 Status = pCallbacks->pfnD3DKMTOpenAdapterFromLuid(&OpenAdapterData);
274 #ifdef DEBUG_misha
275 Assert(!Status);
276 #endif
277 if (NT_SUCCESS(Status))
278 {
279 pAdapter->hAdapter = OpenAdapterData.hAdapter;
280 pAdapter->hDc = NULL;
281 pAdapter->Luid = EnumAdapters.Adapters[i].AdapterLuid;
282 pAdapter->pCallbacks = pCallbacks;
283 return S_OK;
284 }
285 }
286 }
287 }
288
289#ifdef DEBUG_misha
290 Assert(0);
291#endif
292 return E_FAIL;
293}
294
295HRESULT vboxDispKmtOpenAdapter(const VBOXDISPKMT_CALLBACKS *pCallbacks, PVBOXDISPKMT_ADAPTER pAdapter)
296{
297 HRESULT hr = vboxDispKmtOpenAdapterViaHdc(pCallbacks, pAdapter);
298 if (SUCCEEDED(hr))
299 return S_OK;
300
301 hr = vboxDispKmtOpenAdapterViaLuid(pCallbacks, pAdapter);
302 if (SUCCEEDED(hr))
303 return S_OK;
304
305 return hr;
306}
307
308HRESULT vboxDispKmtCloseAdapter(PVBOXDISPKMT_ADAPTER pAdapter)
309{
310 D3DKMT_CLOSEADAPTER ClosaAdapterData = {0};
311 ClosaAdapterData.hAdapter = pAdapter->hAdapter;
312 NTSTATUS Status = pAdapter->pCallbacks->pfnD3DKMTCloseAdapter(&ClosaAdapterData);
313 Assert(!Status);
314 if (!Status)
315 {
316 DeleteDC(pAdapter->hDc);
317 return S_OK;
318 }
319
320 Log((__FUNCTION__": pfnD3DKMTCloseAdapter failed, Status (0x%x)\n", Status));
321
322 return E_FAIL;
323}
324
325HRESULT vboxDispKmtCreateDevice(PVBOXDISPKMT_ADAPTER pAdapter, PVBOXDISPKMT_DEVICE pDevice)
326{
327 D3DKMT_CREATEDEVICE CreateDeviceData = {0};
328 CreateDeviceData.hAdapter = pAdapter->hAdapter;
329 NTSTATUS Status = pAdapter->pCallbacks->pfnD3DKMTCreateDevice(&CreateDeviceData);
330 Assert(!Status);
331 if (!Status)
332 {
333 pDevice->pAdapter = pAdapter;
334 pDevice->hDevice = CreateDeviceData.hDevice;
335 pDevice->pCommandBuffer = CreateDeviceData.pCommandBuffer;
336 pDevice->CommandBufferSize = CreateDeviceData.CommandBufferSize;
337 pDevice->pAllocationList = CreateDeviceData.pAllocationList;
338 pDevice->AllocationListSize = CreateDeviceData.AllocationListSize;
339 pDevice->pPatchLocationList = CreateDeviceData.pPatchLocationList;
340 pDevice->PatchLocationListSize = CreateDeviceData.PatchLocationListSize;
341
342 return S_OK;
343 }
344
345 return E_FAIL;
346}
347
348HRESULT vboxDispKmtDestroyDevice(PVBOXDISPKMT_DEVICE pDevice)
349{
350 D3DKMT_DESTROYDEVICE DestroyDeviceData = {0};
351 DestroyDeviceData.hDevice = pDevice->hDevice;
352 NTSTATUS Status = pDevice->pAdapter->pCallbacks->pfnD3DKMTDestroyDevice(&DestroyDeviceData);
353 Assert(!Status);
354 if (!Status)
355 {
356 return S_OK;
357 }
358 return E_FAIL;
359}
360
361HRESULT vboxDispKmtCreateContext(PVBOXDISPKMT_DEVICE pDevice, PVBOXDISPKMT_CONTEXT pContext,
362 VBOXWDDM_CONTEXT_TYPE enmType,
363 uint32_t crVersionMajor, uint32_t crVersionMinor,
364 HANDLE hEvent, uint64_t u64UmInfo)
365{
366 VBOXWDDM_CREATECONTEXT_INFO Info = {0};
367 Info.u32IfVersion = 9;
368 Info.enmType = enmType;
369 Info.crVersionMajor = crVersionMajor;
370 Info.crVersionMinor = crVersionMinor;
371 Info.hUmEvent = (uint64_t)hEvent;
372 Info.u64UmInfo = u64UmInfo;
373 D3DKMT_CREATECONTEXT ContextData = {0};
374 ContextData.hDevice = pDevice->hDevice;
375 ContextData.NodeOrdinal = VBOXWDDM_NODE_ID_3D_KMT;
376 ContextData.EngineAffinity = VBOXWDDM_ENGINE_ID_3D_KMT;
377 ContextData.pPrivateDriverData = &Info;
378 ContextData.PrivateDriverDataSize = sizeof (Info);
379 ContextData.ClientHint = enmType == VBOXWDDM_CONTEXT_TYPE_CUSTOM_UHGSMI_GL ? D3DKMT_CLIENTHINT_OPENGL : D3DKMT_CLIENTHINT_DX9;
380 NTSTATUS Status = pDevice->pAdapter->pCallbacks->pfnD3DKMTCreateContext(&ContextData);
381 Assert(!Status);
382 if (!Status)
383 {
384 pContext->pDevice = pDevice;
385 pContext->hContext = ContextData.hContext;
386 pContext->pCommandBuffer = ContextData.pCommandBuffer;
387 pContext->CommandBufferSize = ContextData.CommandBufferSize;
388 pContext->pAllocationList = ContextData.pAllocationList;
389 pContext->AllocationListSize = ContextData.AllocationListSize;
390 pContext->pPatchLocationList = ContextData.pPatchLocationList;
391 pContext->PatchLocationListSize = ContextData.PatchLocationListSize;
392 return S_OK;
393 }
394 return E_FAIL;
395}
396
397HRESULT vboxDispKmtDestroyContext(PVBOXDISPKMT_CONTEXT pContext)
398{
399 D3DKMT_DESTROYCONTEXT DestroyContextData = {0};
400 DestroyContextData.hContext = pContext->hContext;
401 NTSTATUS Status = pContext->pDevice->pAdapter->pCallbacks->pfnD3DKMTDestroyContext(&DestroyContextData);
402 Assert(!Status);
403 if (!Status)
404 return S_OK;
405 return E_FAIL;
406}
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