VirtualBox

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

Last change on this file since 77046 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1/* $Id: VBoxDispCm.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * VBoxVideo Display D3D User mode dll
4 */
5
6/*
7 * Copyright (C) 2011-2019 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
28typedef 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
37typedef struct VBOXDISPCM_MGR
38{
39 VBOXDISPCM_SESSION Session;
40} VBOXDISPCM_MGR, *PVBOXDISPCM_MGR;
41
42/* the cm is one per process */
43static VBOXDISPCM_MGR g_pVBoxCmMgr;
44
45HRESULT 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
62HRESULT 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
83void vboxDispCmSessionCtxAdd(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_CONTEXT pContext)
84{
85 EnterCriticalSection(&pSession->CritSect);
86 RTListAppend(&pSession->CtxList, &pContext->ListNode);
87 LeaveCriticalSection(&pSession->CritSect);
88}
89
90void vboxDispCmSessionCtxRemoveLocked(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_CONTEXT pContext)
91{
92 RT_NOREF(pSession);
93 RTListNodeRemove(&pContext->ListNode);
94}
95
96void vboxDispCmSessionCtxRemove(PVBOXDISPCM_SESSION pSession, PVBOXWDDMDISP_CONTEXT pContext)
97{
98 EnterCriticalSection(&pSession->CritSect);
99 vboxDispCmSessionCtxRemoveLocked(pSession, pContext);
100 LeaveCriticalSection(&pSession->CritSect);
101}
102
103HRESULT vboxDispCmInit()
104{
105 HRESULT hr = vboxDispCmSessionInit(&g_pVBoxCmMgr.Session);
106 Assert(hr == S_OK);
107 return hr;
108}
109
110HRESULT vboxDispCmTerm()
111{
112 HRESULT hr = vboxDispCmSessionTerm(&g_pVBoxCmMgr.Session);
113 Assert(hr == S_OK);
114 return hr;
115}
116
117HRESULT 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.u.vbox.crVersionMajor = CR_PROTOCOL_VERSION_MAJOR;
127 Info.u.vbox.crVersionMinor = CR_PROTOCOL_VERSION_MINOR;
128#else
129 WARN(("not expected"));
130 Info.u.vbox.crVersionMajor = 0;
131 Info.u.vbox.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.u.vbox.hUmEvent = (uintptr_t)g_pVBoxCmMgr.Session.hEvent;
141 Info.u.vbox.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
192HRESULT 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
209HRESULT vboxDispCmCtxDestroy(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_CONTEXT pContext)
210{
211 if (!pContext->ContextInfo.hContext)
212 return S_OK;
213
214 return vboxDispCmSessionCtxDestroy(&g_pVBoxCmMgr.Session, pDevice, pContext);
215}
216
217static HRESULT vboxDispCmSessionCmdQueryData(PVBOXDISPCM_SESSION pSession, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd)
218{
219
220 HRESULT hr = S_OK;
221 D3DDDICB_ESCAPE DdiEscape;
222 DdiEscape.Flags.Value = 0;
223 DdiEscape.pPrivateDriverData = pCmd;
224 DdiEscape.PrivateDriverDataSize = cbCmd;
225
226 pCmd->EscapeHdr.escapeCode = VBOXESC_GETVBOXVIDEOCMCMD;
227
228 PVBOXWDDMDISP_CONTEXT pContext = NULL, pCurCtx;
229
230 /* lock to ensure the context is not destroyed */
231 EnterCriticalSection(&pSession->CritSect);
232 /* use any context for identifying the kernel CmSession. We're using the first one */
233 RTListForEach(&pSession->CtxList, pCurCtx, VBOXWDDMDISP_CONTEXT, ListNode)
234 {
235 PVBOXWDDMDISP_DEVICE pDevice = pCurCtx->pDevice;
236 if (VBOXDISPMODE_IS_3D(pDevice->pAdapter))
237 {
238 pContext = pCurCtx;
239 break;
240 }
241 }
242 if (pContext)
243 {
244 PVBOXWDDMDISP_DEVICE pDevice = pContext->pDevice;
245 DdiEscape.hDevice = pDevice->hDevice;
246 DdiEscape.hContext = pContext->ContextInfo.hContext;
247 Assert (DdiEscape.hContext);
248 Assert (DdiEscape.hDevice);
249 hr = pDevice->RtCallbacks.pfnEscapeCb(pDevice->pAdapter->hAdapter, &DdiEscape);
250 LeaveCriticalSection(&pSession->CritSect);
251 Assert(hr == S_OK);
252 if (hr == S_OK)
253 {
254 if (!pCmd->Hdr.cbCmdsReturned && !pCmd->Hdr.cbRemainingFirstCmd)
255 hr = S_FALSE;
256 }
257 else
258 {
259 vboxVDbgPrint(("DispD3D: vboxDispCmSessionCmdQueryData, pfnEscapeCb failed hr (0x%x)\n", hr));
260 exit(1);
261 }
262 }
263 else
264 {
265 LeaveCriticalSection(&pSession->CritSect);
266 hr = S_FALSE;
267 }
268
269 return hr;
270}
271
272HRESULT vboxDispCmCmdSessionInterruptWait(PVBOXDISPCM_SESSION pSession)
273{
274 SetEvent(pSession->hEvent);
275 return S_OK;
276}
277
278HRESULT vboxDispCmSessionCmdGet(PVBOXDISPCM_SESSION pSession, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd, DWORD dwMilliseconds)
279{
280 Assert(cbCmd >= sizeof (VBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD));
281 if (cbCmd < sizeof (VBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD))
282 return E_INVALIDARG;
283
284 do
285 {
286
287 if (pSession->bQueryMp)
288 {
289 HRESULT hr = vboxDispCmSessionCmdQueryData(pSession, pCmd, cbCmd);
290 Assert(hr == S_OK || hr == S_FALSE);
291 if (hr == S_OK || hr != S_FALSE)
292 {
293 return hr;
294 }
295
296 pSession->bQueryMp = false;
297 }
298
299 DWORD dwResult = WaitForSingleObject(pSession->hEvent, dwMilliseconds);
300 switch(dwResult)
301 {
302 case WAIT_OBJECT_0:
303 {
304 pSession->bQueryMp = true;
305 break; /* <- query commands */
306 }
307 case WAIT_TIMEOUT:
308 {
309 Assert(!pSession->bQueryMp);
310 return WAIT_TIMEOUT;
311 }
312 default:
313 Assert(0);
314 return E_FAIL;
315 }
316 } while (1);
317
318 /* should never be here */
319 Assert(0);
320 return E_FAIL;
321}
322
323HRESULT vboxDispCmCmdGet(PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd, DWORD dwMilliseconds)
324{
325 return vboxDispCmSessionCmdGet(&g_pVBoxCmMgr.Session, pCmd, cbCmd, dwMilliseconds);
326}
327
328HRESULT vboxDispCmCmdInterruptWait()
329{
330 return vboxDispCmCmdSessionInterruptWait(&g_pVBoxCmMgr.Session);
331}
332
333void vboxDispCmLog(LPCSTR pszMsg)
334{
335 RT_NOREF(pszMsg);
336 PVBOXDISPCM_SESSION pSession = &g_pVBoxCmMgr.Session;
337
338 EnterCriticalSection(&pSession->CritSect);
339 /* use any context for identifying the kernel CmSession. We're using the first one */
340 PVBOXWDDMDISP_CONTEXT pContext = RTListGetFirst(&pSession->CtxList, VBOXWDDMDISP_CONTEXT, ListNode);
341 Assert(pContext);
342 if (pContext)
343 {
344 Assert(pContext->pDevice);
345 vboxVDbgPrint(("%s", pszMsg));
346 }
347 LeaveCriticalSection(&pSession->CritSect);
348}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette