1 | /* $Id: VBoxDispCm.cpp 63033 2016-08-05 11:19:40Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxVideo Display D3D User mode dll
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2016 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "VBoxDispD3DCmn.h"
|
---|
19 | #include "VBoxDispD3D.h"
|
---|
20 | #include <VBoxDisplay.h>
|
---|
21 |
|
---|
22 | #include <iprt/list.h>
|
---|
23 |
|
---|
24 | #ifdef VBOX_WITH_CROGL
|
---|
25 | #include <cr_protocol.h>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | typedef struct VBOXDISPCM_SESSION
|
---|
29 | {
|
---|
30 | HANDLE hEvent;
|
---|
31 | CRITICAL_SECTION CritSect;
|
---|
32 | /** List of VBOXWDDMDISP_CONTEXT nodes. */
|
---|
33 | RTLISTANCHOR CtxList;
|
---|
34 | bool bQueryMp;
|
---|
35 | } VBOXDISPCM_SESSION, *PVBOXDISPCM_SESSION;
|
---|
36 |
|
---|
37 | typedef struct VBOXDISPCM_MGR
|
---|
38 | {
|
---|
39 | VBOXDISPCM_SESSION Session;
|
---|
40 | } VBOXDISPCM_MGR, *PVBOXDISPCM_MGR;
|
---|
41 |
|
---|
42 | /* the cm is one per process */
|
---|
43 | static VBOXDISPCM_MGR g_pVBoxCmMgr;
|
---|
44 |
|
---|
45 | HRESULT vboxDispCmSessionTerm(PVBOXDISPCM_SESSION pSession)
|
---|
46 | {
|
---|
47 | #ifdef DEBUG_misha
|
---|
48 | Assert(RTListIsEmpty(&pSession->CtxList));
|
---|
49 | #endif
|
---|
50 | BOOL bRc = CloseHandle(pSession->hEvent);
|
---|
51 | Assert(bRc);
|
---|
52 | if (bRc)
|
---|
53 | {
|
---|
54 | DeleteCriticalSection(&pSession->CritSect);
|
---|
55 | return S_OK;
|
---|
56 | }
|
---|
57 | DWORD winEr = GetLastError();
|
---|
58 | HRESULT hr = HRESULT_FROM_WIN32(winEr);
|
---|
59 | return hr;
|
---|
60 | }
|
---|
61 |
|
---|
62 | HRESULT vboxDispCmSessionInit(PVBOXDISPCM_SESSION pSession)
|
---|
63 | {
|
---|
64 | HANDLE hEvent = CreateEvent(NULL,
|
---|
65 | FALSE, /* BOOL bManualReset */
|
---|
66 | FALSE, /* BOOL bInitialState */
|
---|
67 | NULL /* LPCTSTR lpName */
|
---|
68 | );
|
---|
69 | Assert(hEvent);
|
---|
70 | if (hEvent)
|
---|
71 | {
|
---|
72 | pSession->hEvent = hEvent;
|
---|
73 | InitializeCriticalSection(&pSession->CritSect);
|
---|
74 | RTListInit(&pSession->CtxList);
|
---|
75 | pSession->bQueryMp = false;
|
---|
76 | return S_OK;
|
---|
77 | }
|
---|
78 | DWORD winEr = GetLastError();
|
---|
79 | HRESULT hr = HRESULT_FROM_WIN32(winEr);
|
---|
80 | return hr;
|
---|
81 | }
|
---|
82 |
|
---|
83 | void vboxDispCmSessionCtxAdd(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
84 | {
|
---|
85 | EnterCriticalSection(&pSession->CritSect);
|
---|
86 | RTListAppend(&pSession->CtxList, &pContext->ListNode);
|
---|
87 | LeaveCriticalSection(&pSession->CritSect);
|
---|
88 | }
|
---|
89 |
|
---|
90 | void vboxDispCmSessionCtxRemoveLocked(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
91 | {
|
---|
92 | RT_NOREF(pSession);
|
---|
93 | RTListNodeRemove(&pContext->ListNode);
|
---|
94 | }
|
---|
95 |
|
---|
96 | void vboxDispCmSessionCtxRemove(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
97 | {
|
---|
98 | EnterCriticalSection(&pSession->CritSect);
|
---|
99 | vboxDispCmSessionCtxRemoveLocked(pSession, pContext);
|
---|
100 | LeaveCriticalSection(&pSession->CritSect);
|
---|
101 | }
|
---|
102 |
|
---|
103 | HRESULT vboxDispCmInit()
|
---|
104 | {
|
---|
105 | HRESULT hr = vboxDispCmSessionInit(&g_pVBoxCmMgr.Session);
|
---|
106 | Assert(hr == S_OK);
|
---|
107 | return hr;
|
---|
108 | }
|
---|
109 |
|
---|
110 | HRESULT vboxDispCmTerm()
|
---|
111 | {
|
---|
112 | HRESULT hr = vboxDispCmSessionTerm(&g_pVBoxCmMgr.Session);
|
---|
113 | Assert(hr == S_OK);
|
---|
114 | return hr;
|
---|
115 | }
|
---|
116 |
|
---|
117 | HRESULT vboxDispCmCtxCreate(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
118 | {
|
---|
119 | BOOL fIsCrContext;
|
---|
120 | VBOXWDDM_CREATECONTEXT_INFO Info = {0};
|
---|
121 | Info.u32IfVersion = pDevice->u32IfVersion;
|
---|
122 | if (VBOXDISPMODE_IS_3D(pDevice->pAdapter))
|
---|
123 | {
|
---|
124 | Info.enmType = VBOXWDDM_CONTEXT_TYPE_CUSTOM_3D;
|
---|
125 | #ifdef VBOX_WITH_CROGL
|
---|
126 | Info.crVersionMajor = CR_PROTOCOL_VERSION_MAJOR;
|
---|
127 | Info.crVersionMinor = CR_PROTOCOL_VERSION_MINOR;
|
---|
128 | #else
|
---|
129 | WARN(("not expected"));
|
---|
130 | Info.crVersionMajor = 0;
|
---|
131 | Info.crVersionMinor = 0;
|
---|
132 | #endif
|
---|
133 | fIsCrContext = TRUE;
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | Info.enmType = VBOXWDDM_CONTEXT_TYPE_CUSTOM_2D;
|
---|
138 | fIsCrContext = FALSE;
|
---|
139 | }
|
---|
140 | Info.hUmEvent = (uintptr_t)g_pVBoxCmMgr.Session.hEvent;
|
---|
141 | Info.u64UmInfo = (uintptr_t)pContext;
|
---|
142 |
|
---|
143 | if (VBOXDISPMODE_IS_3D(pDevice->pAdapter))
|
---|
144 | {
|
---|
145 | pContext->ContextInfo.NodeOrdinal = VBOXWDDM_NODE_ID_3D;
|
---|
146 | pContext->ContextInfo.EngineAffinity = VBOXWDDM_ENGINE_ID_3D;
|
---|
147 | }
|
---|
148 | else
|
---|
149 | {
|
---|
150 | pContext->ContextInfo.NodeOrdinal = VBOXWDDM_NODE_ID_2D_VIDEO;
|
---|
151 | pContext->ContextInfo.EngineAffinity = VBOXWDDM_ENGINE_ID_2D_VIDEO;
|
---|
152 | }
|
---|
153 | pContext->ContextInfo.Flags.Value = 0;
|
---|
154 | pContext->ContextInfo.pPrivateDriverData = &Info;
|
---|
155 | pContext->ContextInfo.PrivateDriverDataSize = sizeof (Info);
|
---|
156 | pContext->ContextInfo.hContext = 0;
|
---|
157 | pContext->ContextInfo.pCommandBuffer = NULL;
|
---|
158 | pContext->ContextInfo.CommandBufferSize = 0;
|
---|
159 | pContext->ContextInfo.pAllocationList = NULL;
|
---|
160 | pContext->ContextInfo.AllocationListSize = 0;
|
---|
161 | pContext->ContextInfo.pPatchLocationList = NULL;
|
---|
162 | pContext->ContextInfo.PatchLocationListSize = 0;
|
---|
163 |
|
---|
164 | HRESULT hr = S_OK;
|
---|
165 | hr = pDevice->RtCallbacks.pfnCreateContextCb(pDevice->hDevice, &pContext->ContextInfo);
|
---|
166 | Assert(hr == S_OK);
|
---|
167 | if (hr == S_OK)
|
---|
168 | {
|
---|
169 | Assert(pContext->ContextInfo.hContext);
|
---|
170 | pContext->ContextInfo.pPrivateDriverData = NULL;
|
---|
171 | pContext->ContextInfo.PrivateDriverDataSize = 0;
|
---|
172 | vboxDispCmSessionCtxAdd(&g_pVBoxCmMgr.Session, pContext);
|
---|
173 | pContext->pDevice = pDevice;
|
---|
174 | if (fIsCrContext)
|
---|
175 | {
|
---|
176 | #ifdef VBOX_WITH_CRHGSMI
|
---|
177 | if (pDevice->pAdapter->u32VBox3DCaps & CR_VBOX_CAP_CMDVBVA)
|
---|
178 | vboxUhgsmiD3DInit(&pDevice->Uhgsmi, pDevice);
|
---|
179 | else
|
---|
180 | vboxUhgsmiD3DEscInit(&pDevice->Uhgsmi, pDevice);
|
---|
181 | #endif
|
---|
182 | }
|
---|
183 | }
|
---|
184 | else
|
---|
185 | {
|
---|
186 | exit(1);
|
---|
187 | }
|
---|
188 |
|
---|
189 | return hr;
|
---|
190 | }
|
---|
191 |
|
---|
192 | HRESULT vboxDispCmSessionCtxDestroy(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
193 | {
|
---|
194 | EnterCriticalSection(&pSession->CritSect);
|
---|
195 | Assert(pContext->ContextInfo.hContext);
|
---|
196 | D3DDDICB_DESTROYCONTEXT DestroyContext;
|
---|
197 | Assert(pDevice->DefaultContext.ContextInfo.hContext);
|
---|
198 | DestroyContext.hContext = pDevice->DefaultContext.ContextInfo.hContext;
|
---|
199 | HRESULT hr = pDevice->RtCallbacks.pfnDestroyContextCb(pDevice->hDevice, &DestroyContext);
|
---|
200 | Assert(hr == S_OK);
|
---|
201 | if (hr == S_OK)
|
---|
202 | {
|
---|
203 | vboxDispCmSessionCtxRemoveLocked(pSession, pContext);
|
---|
204 | }
|
---|
205 | LeaveCriticalSection(&pSession->CritSect);
|
---|
206 | return hr;
|
---|
207 | }
|
---|
208 |
|
---|
209 | HRESULT vboxDispCmCtxDestroy(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
210 | {
|
---|
211 | return vboxDispCmSessionCtxDestroy(&g_pVBoxCmMgr.Session, pDevice, pContext);
|
---|
212 | }
|
---|
213 |
|
---|
214 | static HRESULT vboxDispCmSessionCmdQueryData(PVBOXDISPCM_SESSION pSession, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd)
|
---|
215 | {
|
---|
216 |
|
---|
217 | HRESULT hr = S_OK;
|
---|
218 | D3DDDICB_ESCAPE DdiEscape;
|
---|
219 | DdiEscape.Flags.Value = 0;
|
---|
220 | DdiEscape.pPrivateDriverData = pCmd;
|
---|
221 | DdiEscape.PrivateDriverDataSize = cbCmd;
|
---|
222 |
|
---|
223 | pCmd->EscapeHdr.escapeCode = VBOXESC_GETVBOXVIDEOCMCMD;
|
---|
224 |
|
---|
225 | PVBOXWDDMDISP_CONTEXT pContext = NULL, pCurCtx;
|
---|
226 |
|
---|
227 | /* lock to ensure the context is not destroyed */
|
---|
228 | EnterCriticalSection(&pSession->CritSect);
|
---|
229 | /* use any context for identifying the kernel CmSession. We're using the first one */
|
---|
230 | RTListForEach(&pSession->CtxList, pCurCtx, VBOXWDDMDISP_CONTEXT, ListNode)
|
---|
231 | {
|
---|
232 | PVBOXWDDMDISP_DEVICE pDevice = pCurCtx->pDevice;
|
---|
233 | if (VBOXDISPMODE_IS_3D(pDevice->pAdapter))
|
---|
234 | {
|
---|
235 | pContext = pCurCtx;
|
---|
236 | break;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | if (pContext)
|
---|
240 | {
|
---|
241 | PVBOXWDDMDISP_DEVICE pDevice = pContext->pDevice;
|
---|
242 | DdiEscape.hDevice = pDevice->hDevice;
|
---|
243 | DdiEscape.hContext = pContext->ContextInfo.hContext;
|
---|
244 | Assert (DdiEscape.hContext);
|
---|
245 | Assert (DdiEscape.hDevice);
|
---|
246 | hr = pDevice->RtCallbacks.pfnEscapeCb(pDevice->pAdapter->hAdapter, &DdiEscape);
|
---|
247 | LeaveCriticalSection(&pSession->CritSect);
|
---|
248 | Assert(hr == S_OK);
|
---|
249 | if (hr == S_OK)
|
---|
250 | {
|
---|
251 | if (!pCmd->Hdr.cbCmdsReturned && !pCmd->Hdr.cbRemainingFirstCmd)
|
---|
252 | hr = S_FALSE;
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | vboxVDbgPrint(("DispD3D: vboxDispCmSessionCmdQueryData, pfnEscapeCb failed hr (0x%x)\n", hr));
|
---|
257 | exit(1);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | else
|
---|
261 | {
|
---|
262 | LeaveCriticalSection(&pSession->CritSect);
|
---|
263 | hr = S_FALSE;
|
---|
264 | }
|
---|
265 |
|
---|
266 | return hr;
|
---|
267 | }
|
---|
268 |
|
---|
269 | HRESULT vboxDispCmCmdSessionInterruptWait(PVBOXDISPCM_SESSION pSession)
|
---|
270 | {
|
---|
271 | SetEvent(pSession->hEvent);
|
---|
272 | return S_OK;
|
---|
273 | }
|
---|
274 |
|
---|
275 | HRESULT vboxDispCmSessionCmdGet(PVBOXDISPCM_SESSION pSession, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd, DWORD dwMilliseconds)
|
---|
276 | {
|
---|
277 | Assert(cbCmd >= sizeof (VBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD));
|
---|
278 | if (cbCmd < sizeof (VBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD))
|
---|
279 | return E_INVALIDARG;
|
---|
280 |
|
---|
281 | do
|
---|
282 | {
|
---|
283 |
|
---|
284 | if (pSession->bQueryMp)
|
---|
285 | {
|
---|
286 | HRESULT hr = vboxDispCmSessionCmdQueryData(pSession, pCmd, cbCmd);
|
---|
287 | Assert(hr == S_OK || hr == S_FALSE);
|
---|
288 | if (hr == S_OK || hr != S_FALSE)
|
---|
289 | {
|
---|
290 | return hr;
|
---|
291 | }
|
---|
292 |
|
---|
293 | pSession->bQueryMp = false;
|
---|
294 | }
|
---|
295 |
|
---|
296 | DWORD dwResult = WaitForSingleObject(pSession->hEvent, dwMilliseconds);
|
---|
297 | switch(dwResult)
|
---|
298 | {
|
---|
299 | case WAIT_OBJECT_0:
|
---|
300 | {
|
---|
301 | pSession->bQueryMp = true;
|
---|
302 | break; /* <- query commands */
|
---|
303 | }
|
---|
304 | case WAIT_TIMEOUT:
|
---|
305 | {
|
---|
306 | Assert(!pSession->bQueryMp);
|
---|
307 | return WAIT_TIMEOUT;
|
---|
308 | }
|
---|
309 | default:
|
---|
310 | Assert(0);
|
---|
311 | return E_FAIL;
|
---|
312 | }
|
---|
313 | } while (1);
|
---|
314 |
|
---|
315 | /* should never be here */
|
---|
316 | Assert(0);
|
---|
317 | return E_FAIL;
|
---|
318 | }
|
---|
319 |
|
---|
320 | HRESULT vboxDispCmCmdGet(PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd, DWORD dwMilliseconds)
|
---|
321 | {
|
---|
322 | return vboxDispCmSessionCmdGet(&g_pVBoxCmMgr.Session, pCmd, cbCmd, dwMilliseconds);
|
---|
323 | }
|
---|
324 |
|
---|
325 | HRESULT vboxDispCmCmdInterruptWait()
|
---|
326 | {
|
---|
327 | return vboxDispCmCmdSessionInterruptWait(&g_pVBoxCmMgr.Session);
|
---|
328 | }
|
---|
329 |
|
---|
330 | void vboxDispCmLog(LPCSTR pszMsg)
|
---|
331 | {
|
---|
332 | RT_NOREF(pszMsg);
|
---|
333 | PVBOXDISPCM_SESSION pSession = &g_pVBoxCmMgr.Session;
|
---|
334 |
|
---|
335 | EnterCriticalSection(&pSession->CritSect);
|
---|
336 | /* use any context for identifying the kernel CmSession. We're using the first one */
|
---|
337 | PVBOXWDDMDISP_CONTEXT pContext = RTListGetFirst(&pSession->CtxList, VBOXWDDMDISP_CONTEXT, ListNode);
|
---|
338 | Assert(pContext);
|
---|
339 | if (pContext)
|
---|
340 | {
|
---|
341 | Assert(pContext->pDevice);
|
---|
342 | vboxVDbgPrint(("%s", pszMsg));
|
---|
343 | }
|
---|
344 | LeaveCriticalSection(&pSession->CritSect);
|
---|
345 | }
|
---|