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