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