1 | /* $Id: VBoxDispCm.cpp 36867 2011-04-28 07:27:03Z 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 | pContext->ContextInfo.NodeOrdinal = 0;
|
---|
122 | pContext->ContextInfo.EngineAffinity = 0;
|
---|
123 | pContext->ContextInfo.Flags.Value = 0;
|
---|
124 | pContext->ContextInfo.pPrivateDriverData = &Info;
|
---|
125 | pContext->ContextInfo.PrivateDriverDataSize = sizeof (Info);
|
---|
126 | pContext->ContextInfo.hContext = 0;
|
---|
127 | pContext->ContextInfo.pCommandBuffer = NULL;
|
---|
128 | pContext->ContextInfo.CommandBufferSize = 0;
|
---|
129 | pContext->ContextInfo.pAllocationList = NULL;
|
---|
130 | pContext->ContextInfo.AllocationListSize = 0;
|
---|
131 | pContext->ContextInfo.pPatchLocationList = NULL;
|
---|
132 | pContext->ContextInfo.PatchLocationListSize = 0;
|
---|
133 |
|
---|
134 | HRESULT hr = S_OK;
|
---|
135 | hr = pDevice->RtCallbacks.pfnCreateContextCb(pDevice->hDevice, &pContext->ContextInfo);
|
---|
136 | Assert(hr == S_OK);
|
---|
137 | if (hr == S_OK)
|
---|
138 | {
|
---|
139 | Assert(pContext->ContextInfo.hContext);
|
---|
140 | pContext->ContextInfo.pPrivateDriverData = NULL;
|
---|
141 | pContext->ContextInfo.PrivateDriverDataSize = 0;
|
---|
142 | vboxDispCmSessionCtxAdd(&g_pVBoxCmMgr.Session, pContext);
|
---|
143 | pContext->pDevice = pDevice;
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | exit(1);
|
---|
148 | }
|
---|
149 |
|
---|
150 | return hr;
|
---|
151 | }
|
---|
152 |
|
---|
153 | HRESULT vboxDispCmSessionCtxDestroy(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
154 | {
|
---|
155 | EnterCriticalSection(&pSession->CritSect);
|
---|
156 | Assert(pContext->ContextInfo.hContext);
|
---|
157 | D3DDDICB_DESTROYCONTEXT DestroyContext;
|
---|
158 | Assert(pDevice->DefaultContext.ContextInfo.hContext);
|
---|
159 | DestroyContext.hContext = pDevice->DefaultContext.ContextInfo.hContext;
|
---|
160 | HRESULT hr = pDevice->RtCallbacks.pfnDestroyContextCb(pDevice->hDevice, &DestroyContext);
|
---|
161 | Assert(hr == S_OK);
|
---|
162 | if (hr == S_OK)
|
---|
163 | {
|
---|
164 | vboxDispCmSessionCtxRemoveLocked(pSession, pContext);
|
---|
165 | }
|
---|
166 | LeaveCriticalSection(&pSession->CritSect);
|
---|
167 | return hr;
|
---|
168 | }
|
---|
169 |
|
---|
170 | HRESULT vboxDispCmCtxDestroy(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_CONTEXT pContext)
|
---|
171 | {
|
---|
172 | return vboxDispCmSessionCtxDestroy(&g_pVBoxCmMgr.Session, pDevice, pContext);
|
---|
173 | }
|
---|
174 |
|
---|
175 | static HRESULT vboxDispCmSessionCmdQueryData(PVBOXDISPCM_SESSION pSession, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd)
|
---|
176 | {
|
---|
177 |
|
---|
178 | HRESULT hr = S_OK;
|
---|
179 | D3DDDICB_ESCAPE DdiEscape;
|
---|
180 | DdiEscape.Flags.Value = 0;
|
---|
181 | DdiEscape.pPrivateDriverData = pCmd;
|
---|
182 | DdiEscape.PrivateDriverDataSize = cbCmd;
|
---|
183 |
|
---|
184 | pCmd->EscapeHdr.escapeCode = VBOXESC_GETVBOXVIDEOCMCMD;
|
---|
185 | /* lock to ensure the context is not destroyed */
|
---|
186 | EnterCriticalSection(&pSession->CritSect);
|
---|
187 | /* use any context for identifying the kernel CmSession. We're using the first one */
|
---|
188 | PVBOXWDDMDISP_CONTEXT pContext = RTListGetFirst(&pSession->CtxList, VBOXWDDMDISP_CONTEXT, ListNode);
|
---|
189 | if (pContext)
|
---|
190 | {
|
---|
191 | PVBOXWDDMDISP_DEVICE pDevice = pContext->pDevice;
|
---|
192 | DdiEscape.hDevice = pDevice->hDevice;
|
---|
193 | DdiEscape.hContext = pContext->ContextInfo.hContext;
|
---|
194 | Assert (DdiEscape.hContext);
|
---|
195 | Assert (DdiEscape.hDevice);
|
---|
196 | hr = pDevice->RtCallbacks.pfnEscapeCb(pDevice->pAdapter->hAdapter, &DdiEscape);
|
---|
197 | LeaveCriticalSection(&pSession->CritSect);
|
---|
198 | Assert(hr == S_OK);
|
---|
199 | if (hr == S_OK)
|
---|
200 | {
|
---|
201 | if (!pCmd->Hdr.cbCmdsReturned && !pCmd->Hdr.cbRemainingFirstCmd)
|
---|
202 | hr = S_FALSE;
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | vboxVDbgPrint(("DispD3D: vboxDispCmSessionCmdQueryData, pfnEscapeCb failed hr (0x%x)\n", hr));
|
---|
207 | exit(1);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | LeaveCriticalSection(&pSession->CritSect);
|
---|
213 | hr = S_FALSE;
|
---|
214 | }
|
---|
215 |
|
---|
216 | return hr;
|
---|
217 | }
|
---|
218 |
|
---|
219 | HRESULT vboxDispCmSessionCmdGet(PVBOXDISPCM_SESSION pSession, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd, DWORD dwMilliseconds)
|
---|
220 | {
|
---|
221 | Assert(cbCmd >= sizeof (VBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD));
|
---|
222 | if (cbCmd < sizeof (VBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD))
|
---|
223 | return E_INVALIDARG;
|
---|
224 |
|
---|
225 | do
|
---|
226 | {
|
---|
227 |
|
---|
228 | if (pSession->bQueryMp)
|
---|
229 | {
|
---|
230 | HRESULT hr = vboxDispCmSessionCmdQueryData(pSession, pCmd, cbCmd);
|
---|
231 | Assert(hr == S_OK || hr == S_FALSE);
|
---|
232 | if (hr == S_OK || hr != S_FALSE)
|
---|
233 | {
|
---|
234 | return hr;
|
---|
235 | }
|
---|
236 |
|
---|
237 | pSession->bQueryMp = false;
|
---|
238 | }
|
---|
239 |
|
---|
240 | DWORD dwResult = WaitForSingleObject(pSession->hEvent, dwMilliseconds);
|
---|
241 | switch(dwResult)
|
---|
242 | {
|
---|
243 | case WAIT_OBJECT_0:
|
---|
244 | {
|
---|
245 | pSession->bQueryMp = true;
|
---|
246 | break; /* <- query commands */
|
---|
247 | }
|
---|
248 | case WAIT_TIMEOUT:
|
---|
249 | {
|
---|
250 | Assert(!pSession->bQueryMp);
|
---|
251 | return WAIT_TIMEOUT;
|
---|
252 | }
|
---|
253 | default:
|
---|
254 | Assert(0);
|
---|
255 | return E_FAIL;
|
---|
256 | }
|
---|
257 | } while (1);
|
---|
258 |
|
---|
259 | /* should never be here */
|
---|
260 | Assert(0);
|
---|
261 | return E_FAIL;
|
---|
262 | }
|
---|
263 |
|
---|
264 | HRESULT vboxDispCmCmdGet(PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd, DWORD dwMilliseconds)
|
---|
265 | {
|
---|
266 | return vboxDispCmSessionCmdGet(&g_pVBoxCmMgr.Session, pCmd, cbCmd, dwMilliseconds);
|
---|
267 | }
|
---|
268 |
|
---|
269 | void vboxDispCmLog(LPCSTR pszMsg)
|
---|
270 | {
|
---|
271 | PVBOXDISPCM_SESSION pSession = &g_pVBoxCmMgr.Session;
|
---|
272 |
|
---|
273 | EnterCriticalSection(&pSession->CritSect);
|
---|
274 | /* use any context for identifying the kernel CmSession. We're using the first one */
|
---|
275 | PVBOXWDDMDISP_CONTEXT pContext = RTListGetFirst(&pSession->CtxList, VBOXWDDMDISP_CONTEXT, ListNode);
|
---|
276 | Assert(pContext);
|
---|
277 | if (pContext)
|
---|
278 | {
|
---|
279 | PVBOXWDDMDISP_DEVICE pDevice = pContext->pDevice;
|
---|
280 | Assert(pDevice);
|
---|
281 | vboxVDbgPrint(("%s", pszMsg));
|
---|
282 | }
|
---|
283 | LeaveCriticalSection(&pSession->CritSect);
|
---|
284 | }
|
---|