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