1 | #include <VBox/VBoxCrHgsmi.h>
|
---|
2 | #include <iprt/err.h>
|
---|
3 |
|
---|
4 | #include "VBoxUhgsmiKmt.h"
|
---|
5 |
|
---|
6 | static VBOXDISPKMT_CALLBACKS g_VBoxCrHgsmiKmtCallbacks;
|
---|
7 | static int g_bVBoxKmtCallbacksInited = 0;
|
---|
8 |
|
---|
9 | #ifdef VBOX_CRHGSMI_WITH_D3DDEV
|
---|
10 | static VBOXCRHGSMI_CALLBACKS g_VBoxCrHgsmiCallbacks;
|
---|
11 | static HMODULE g_hVBoxCrHgsmiProvider = NULL;
|
---|
12 | static uint32_t g_cVBoxCrHgsmiProvider = 0;
|
---|
13 |
|
---|
14 |
|
---|
15 | typedef VBOXWDDMDISP_DECL(int) FNVBOXDISPCRHGSMI_INIT(PVBOXCRHGSMI_CALLBACKS pCallbacks);
|
---|
16 | typedef FNVBOXDISPCRHGSMI_INIT *PFNVBOXDISPCRHGSMI_INIT;
|
---|
17 |
|
---|
18 | typedef VBOXWDDMDISP_DECL(int) FNVBOXDISPCRHGSMI_TERM();
|
---|
19 | typedef FNVBOXDISPCRHGSMI_TERM *PFNVBOXDISPCRHGSMI_TERM;
|
---|
20 |
|
---|
21 | typedef VBOXWDDMDISP_DECL(HVBOXCRHGSMI_CLIENT) FNVBOXDISPCRHGSMI_QUERY_CLIENT();
|
---|
22 | typedef FNVBOXDISPCRHGSMI_QUERY_CLIENT *PFNVBOXDISPCRHGSMI_QUERY_CLIENT;
|
---|
23 |
|
---|
24 | static PFNVBOXDISPCRHGSMI_INIT g_pfnVBoxDispCrHgsmiInit = NULL;
|
---|
25 | static PFNVBOXDISPCRHGSMI_TERM g_pfnVBoxDispCrHgsmiTerm = NULL;
|
---|
26 | static PFNVBOXDISPCRHGSMI_QUERY_CLIENT g_pfnVBoxDispCrHgsmiQueryClient = NULL;
|
---|
27 |
|
---|
28 | VBOXCRHGSMI_DECL(int) VBoxCrHgsmiInit(PVBOXCRHGSMI_CALLBACKS pCallbacks)
|
---|
29 | {
|
---|
30 | if (!g_bVBoxKmtCallbacksInited)
|
---|
31 | {
|
---|
32 | HRESULT hr = vboxDispKmtCallbacksInit(&g_VBoxCrHgsmiKmtCallbacks);
|
---|
33 | Assert(hr == S_OK);
|
---|
34 | if (hr == S_OK)
|
---|
35 | g_bVBoxKmtCallbacksInited = 1;
|
---|
36 | else
|
---|
37 | g_bVBoxKmtCallbacksInited = -1;
|
---|
38 | }
|
---|
39 |
|
---|
40 | Assert(g_bVBoxKmtCallbacksInited);
|
---|
41 | if (g_bVBoxKmtCallbacksInited < 0)
|
---|
42 | {
|
---|
43 | Assert(0);
|
---|
44 | return VERR_NOT_SUPPORTED;
|
---|
45 | }
|
---|
46 |
|
---|
47 | g_VBoxCrHgsmiCallbacks = *pCallbacks;
|
---|
48 | if (!g_hVBoxCrHgsmiProvider)
|
---|
49 | {
|
---|
50 | BOOL bRc = GetModuleHandleEx(0, L"VBoxDispD3D", &g_hVBoxCrHgsmiProvider);
|
---|
51 | // g_hVBoxCrHgsmiProvider = GetModuleHandle(L"VBoxDispD3D");
|
---|
52 | if (bRc)
|
---|
53 | {
|
---|
54 | g_pfnVBoxDispCrHgsmiInit = (PFNVBOXDISPCRHGSMI_INIT)GetProcAddress(g_hVBoxCrHgsmiProvider, "VBoxDispCrHgsmiInit");
|
---|
55 | Assert(g_pfnVBoxDispCrHgsmiInit);
|
---|
56 | if (g_pfnVBoxDispCrHgsmiInit)
|
---|
57 | {
|
---|
58 | g_pfnVBoxDispCrHgsmiInit(pCallbacks);
|
---|
59 | }
|
---|
60 |
|
---|
61 | g_pfnVBoxDispCrHgsmiTerm = (PFNVBOXDISPCRHGSMI_TERM)GetProcAddress(g_hVBoxCrHgsmiProvider, "VBoxDispCrHgsmiTerm");
|
---|
62 | Assert(g_pfnVBoxDispCrHgsmiTerm);
|
---|
63 |
|
---|
64 | g_pfnVBoxDispCrHgsmiQueryClient = (PFNVBOXDISPCRHGSMI_QUERY_CLIENT)GetProcAddress(g_hVBoxCrHgsmiProvider, "VBoxDispCrHgsmiQueryClient");
|
---|
65 | Assert(g_pfnVBoxDispCrHgsmiQueryClient);
|
---|
66 | }
|
---|
67 | #ifdef DEBUG_misha
|
---|
68 | else
|
---|
69 | {
|
---|
70 | DWORD winEr = GetLastError();
|
---|
71 | Assert(0);
|
---|
72 | }
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 |
|
---|
76 | if (g_hVBoxCrHgsmiProvider)
|
---|
77 | {
|
---|
78 | if (g_pfnVBoxDispCrHgsmiInit)
|
---|
79 | {
|
---|
80 | g_pfnVBoxDispCrHgsmiInit(pCallbacks);
|
---|
81 | }
|
---|
82 | ++g_cVBoxCrHgsmiProvider;
|
---|
83 | return VINF_SUCCESS;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* we're called from ogl ICD driver*/
|
---|
87 | Assert(0);
|
---|
88 |
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 |
|
---|
92 | static __declspec(thread) PVBOXUHGSMI_PRIVATE_KMT gt_pHgsmiGL = NULL;
|
---|
93 |
|
---|
94 | VBOXCRHGSMI_DECL(HVBOXCRHGSMI_CLIENT) VBoxCrHgsmiQueryClient()
|
---|
95 | {
|
---|
96 |
|
---|
97 | HVBOXCRHGSMI_CLIENT hClient;
|
---|
98 | if (g_pfnVBoxDispCrHgsmiQueryClient)
|
---|
99 | {
|
---|
100 | hClient = g_pfnVBoxDispCrHgsmiQueryClient();
|
---|
101 | //#ifdef DEBUG_misha
|
---|
102 | // Assert(hClient);
|
---|
103 | //#endif
|
---|
104 | if (hClient)
|
---|
105 | return hClient;
|
---|
106 | }
|
---|
107 | PVBOXUHGSMI_PRIVATE_KMT pHgsmiGL = gt_pHgsmiGL;
|
---|
108 | if (pHgsmiGL)
|
---|
109 | {
|
---|
110 | Assert(pHgsmiGL->BasePrivate.hClient);
|
---|
111 | return pHgsmiGL->BasePrivate.hClient;
|
---|
112 | }
|
---|
113 | pHgsmiGL = (PVBOXUHGSMI_PRIVATE_KMT)RTMemAllocZ(sizeof (*pHgsmiGL));
|
---|
114 | if (pHgsmiGL)
|
---|
115 | {
|
---|
116 | #if 0
|
---|
117 | HRESULT hr = vboxUhgsmiKmtCreate(pHgsmiGL, TRUE /* bD3D tmp for injection thread*/);
|
---|
118 | #else
|
---|
119 | HRESULT hr = vboxUhgsmiKmtEscCreate(pHgsmiGL, TRUE /* bD3D tmp for injection thread*/);
|
---|
120 | #endif
|
---|
121 | Assert(hr == S_OK);
|
---|
122 | if (hr == S_OK)
|
---|
123 | {
|
---|
124 | hClient = g_VBoxCrHgsmiCallbacks.pfnClientCreate(&pHgsmiGL->BasePrivate.Base);
|
---|
125 | Assert(hClient);
|
---|
126 | if (hClient)
|
---|
127 | {
|
---|
128 | pHgsmiGL->BasePrivate.hClient = hClient;
|
---|
129 | gt_pHgsmiGL = pHgsmiGL;
|
---|
130 | return hClient;
|
---|
131 | }
|
---|
132 | vboxUhgsmiKmtDestroy(pHgsmiGL);
|
---|
133 | }
|
---|
134 | RTMemFree(pHgsmiGL);
|
---|
135 | }
|
---|
136 |
|
---|
137 | return NULL;
|
---|
138 | }
|
---|
139 | #else
|
---|
140 | VBOXCRHGSMI_DECL(int) VBoxCrHgsmiInit()
|
---|
141 | {
|
---|
142 | if (!g_bVBoxKmtCallbacksInited)
|
---|
143 | {
|
---|
144 | HRESULT hr = vboxDispKmtCallbacksInit(&g_VBoxCrHgsmiKmtCallbacks);
|
---|
145 | Assert(hr == S_OK);
|
---|
146 | if (hr == S_OK)
|
---|
147 | g_bVBoxKmtCallbacksInited = 1;
|
---|
148 | else
|
---|
149 | g_bVBoxKmtCallbacksInited = -1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | Assert(g_bVBoxKmtCallbacksInited);
|
---|
153 | if (g_bVBoxKmtCallbacksInited < 0)
|
---|
154 | {
|
---|
155 | Assert(0);
|
---|
156 | return VERR_NOT_SUPPORTED;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return VINF_SUCCESS;
|
---|
160 | }
|
---|
161 |
|
---|
162 | VBOXCRHGSMI_DECL(PVBOXUHGSMI) VBoxCrHgsmiCreate()
|
---|
163 | {
|
---|
164 | PVBOXUHGSMI_PRIVATE_KMT pHgsmiGL = (PVBOXUHGSMI_PRIVATE_KMT)RTMemAllocZ(sizeof (*pHgsmiGL));
|
---|
165 | if (pHgsmiGL)
|
---|
166 | {
|
---|
167 | #if 0
|
---|
168 | HRESULT hr = vboxUhgsmiKmtCreate(pHgsmiGL, TRUE /* bD3D tmp for injection thread*/);
|
---|
169 | #else
|
---|
170 | HRESULT hr = vboxUhgsmiKmtEscCreate(pHgsmiGL, TRUE /* bD3D tmp for injection thread*/);
|
---|
171 | #endif
|
---|
172 | Assert(hr == S_OK);
|
---|
173 | if (hr == S_OK)
|
---|
174 | {
|
---|
175 | return &pHgsmiGL->BasePrivate.Base;
|
---|
176 | }
|
---|
177 | RTMemFree(pHgsmiGL);
|
---|
178 | }
|
---|
179 |
|
---|
180 | return NULL;
|
---|
181 | }
|
---|
182 |
|
---|
183 | VBOXCRHGSMI_DECL(void) VBoxCrHgsmiDestroy(PVBOXUHGSMI pHgsmi)
|
---|
184 | {
|
---|
185 | PVBOXUHGSMI_PRIVATE_KMT pHgsmiGL = VBOXUHGSMIKMT_GET(pHgsmi);
|
---|
186 | HRESULT hr = vboxUhgsmiKmtDestroy(pHgsmiGL);
|
---|
187 | Assert(hr == S_OK);
|
---|
188 | if (hr == S_OK)
|
---|
189 | {
|
---|
190 | RTMemFree(pHgsmiGL);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | VBOXCRHGSMI_DECL(int) VBoxCrHgsmiTerm()
|
---|
196 | {
|
---|
197 | #if 0
|
---|
198 | PVBOXUHGSMI_PRIVATE_KMT pHgsmiGL = gt_pHgsmiGL;
|
---|
199 | if (pHgsmiGL)
|
---|
200 | {
|
---|
201 | g_VBoxCrHgsmiCallbacks.pfnClientDestroy(pHgsmiGL->BasePrivate.hClient);
|
---|
202 | vboxUhgsmiKmtDestroy(pHgsmiGL);
|
---|
203 | gt_pHgsmiGL = NULL;
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (g_pfnVBoxDispCrHgsmiTerm)
|
---|
207 | g_pfnVBoxDispCrHgsmiTerm();
|
---|
208 | #endif
|
---|
209 | if (g_bVBoxKmtCallbacksInited > 0)
|
---|
210 | {
|
---|
211 | vboxDispKmtCallbacksTerm(&g_VBoxCrHgsmiKmtCallbacks);
|
---|
212 | }
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 | VBOXCRHGSMI_DECL(void) VBoxCrHgsmiLog(char * szString)
|
---|
217 | {
|
---|
218 | VBOXDISPKMT_ADAPTER Adapter;
|
---|
219 | HRESULT hr = vboxDispKmtOpenAdapter(&g_VBoxCrHgsmiKmtCallbacks, &Adapter);
|
---|
220 | Assert(hr == S_OK);
|
---|
221 | if (hr == S_OK)
|
---|
222 | {
|
---|
223 | uint32_t cbString = (uint32_t)strlen(szString) + 1;
|
---|
224 | uint32_t cbCmd = RT_OFFSETOF(VBOXDISPIFESCAPE_DBGPRINT, aStringBuf[cbString]);
|
---|
225 | PVBOXDISPIFESCAPE_DBGPRINT pCmd = (PVBOXDISPIFESCAPE_DBGPRINT)RTMemAllocZ(cbCmd);
|
---|
226 | Assert(pCmd);
|
---|
227 | if (pCmd)
|
---|
228 | {
|
---|
229 | pCmd->EscapeHdr.escapeCode = VBOXESC_DBGPRINT;
|
---|
230 | memcpy(pCmd->aStringBuf, szString, cbString);
|
---|
231 |
|
---|
232 | D3DKMT_ESCAPE EscapeData = {0};
|
---|
233 | EscapeData.hAdapter = Adapter.hAdapter;
|
---|
234 | //EscapeData.hDevice = NULL;
|
---|
235 | EscapeData.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
|
---|
236 | // EscapeData.Flags.HardwareAccess = 1;
|
---|
237 | EscapeData.pPrivateDriverData = pCmd;
|
---|
238 | EscapeData.PrivateDriverDataSize = cbCmd;
|
---|
239 | //EscapeData.hContext = NULL;
|
---|
240 |
|
---|
241 | int Status = g_VBoxCrHgsmiKmtCallbacks.pfnD3DKMTEscape(&EscapeData);
|
---|
242 | Assert(!Status);
|
---|
243 |
|
---|
244 | RTMemFree(pCmd);
|
---|
245 | }
|
---|
246 | hr = vboxDispKmtCloseAdapter(&Adapter);
|
---|
247 | Assert(hr == S_OK);
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | ///* to be used by injection thread and by ogl ICD driver for hgsmi initialization*/
|
---|
252 | //VBOXCRHGSMI_DECL(int) VBoxCrHgsmiCustomCreate(PVBOXUHGSMI *ppHgsmi)
|
---|
253 | //{
|
---|
254 | // PVBOXUHGSMI_PRIVATE_KMT pHgsmi = RTMemAllocZ(sizeof (*pHgsmi));
|
---|
255 | // if (pHgsmi)
|
---|
256 | // {
|
---|
257 | // HRESULT hr = vboxUhgsmiKmtCreate(pHgsmi, FALSE);
|
---|
258 | // Assert(hr == S_OK);
|
---|
259 | // if (hr == S_OK)
|
---|
260 | // {
|
---|
261 | // *ppHgsmi = &pHgsmi->BasePrivate.Base;
|
---|
262 | // return VINF_SUCCESS;
|
---|
263 | // }
|
---|
264 | // RTMemFree(pHgsmi);
|
---|
265 | // return VERR_GENERAL_FAILURE;
|
---|
266 | // }
|
---|
267 | // return VERR_NO_MEMORY;
|
---|
268 | //}
|
---|
269 | //
|
---|
270 | //VBOXCRHGSMI_DECL(int) VBoxCrHgsmiCustomDestroy(PVBOXUHGSMI pHgsmi)
|
---|
271 | //{
|
---|
272 | // PVBOXUHGSMI_PRIVATE_KMT pHgsmiKmt = VBOXUHGSMIKMT_GET(pHgsmi);
|
---|
273 | // HRESULT hr = vboxUhgsmiKmtDestroy(pHgsmiKmt, FALSE);
|
---|
274 | // Assert(hr == S_OK);
|
---|
275 | // if (hr == S_OK)
|
---|
276 | // {
|
---|
277 | // RTMemFree(pHgsmiKmt);
|
---|
278 | // return VINF_SUCCESS;
|
---|
279 | // }
|
---|
280 | // return VERR_GENERAL_FAILURE;
|
---|
281 | //}
|
---|
282 |
|
---|