VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispDbg.cpp@ 35657

Last change on this file since 35657 was 35657, checked in by vboxsync, 14 years ago

adds build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.6 KB
Line 
1#include "VBoxDispD3DCmn.h"
2
3#include <stdio.h>
4#include <stdarg.h>
5
6#include <iprt/asm.h>
7
8#ifdef VBOXWDDMDISP_DEBUG
9bool g_bVBoxVDbgFDumpSetTexture = false;
10bool g_bVBoxVDbgFDumpDrawPrim = false;
11bool g_bVBoxVDbgFDumpTexBlt = false;
12bool g_bVBoxVDbgFDumpBlt = false;
13
14typedef enum
15{
16 VBOXDISPDBG_STATE_UNINITIALIZED = 0,
17 VBOXDISPDBG_STATE_INITIALIZING,
18 VBOXDISPDBG_STATE_INITIALIZED,
19} VBOXDISPDBG_STATE;
20
21typedef struct VBOXDISPDBG
22{
23 VBOXDISPKMT_CALLBACKS KmtCallbacks;
24 VBOXDISPDBG_STATE enmState;
25} VBOXDISPDBG, *PVBOXDISPDBG;
26
27static VBOXDISPDBG g_VBoxDispDbg = {0};
28
29PVBOXDISPDBG vboxDispDbgGet()
30{
31 if (ASMAtomicCmpXchgU32((volatile uint32_t *)g_VBoxDispDbg.enmState, VBOXDISPDBG_STATE_UNINITIALIZED, VBOXDISPDBG_STATE_INITIALIZING))
32 {
33 HRESULT hr = vboxDispKmtCallbacksInit(&g_VBoxDispDbg.KmtCallbacks);
34 Assert(hr == S_OK);
35 if (hr == S_OK)
36 {
37 ASMAtomicWriteU32((volatile uint32_t *)g_VBoxDispDbg.enmState, VBOXDISPDBG_STATE_INITIALIZED);
38 return &g_VBoxDispDbg;
39 }
40 else
41 {
42 ASMAtomicWriteU32((volatile uint32_t *)g_VBoxDispDbg.enmState, VBOXDISPDBG_STATE_UNINITIALIZED);
43 }
44 }
45 else if (ASMAtomicReadU32((volatile uint32_t *)g_VBoxDispDbg.enmState) == VBOXDISPDBG_STATE_INITIALIZED)
46 {
47 return &g_VBoxDispDbg;
48 }
49 Assert(0);
50 return NULL;
51}
52
53void vboxDispLogDrv(char * szString)
54{
55 PVBOXDISPDBG pDbg = vboxDispDbgGet();
56#ifdef DEBUG_misha
57 Assert(pDbg);
58#endif
59 if (!pDbg)
60 return;
61
62 VBOXDISPKMT_ADAPTER Adapter;
63 HRESULT hr = vboxDispKmtOpenAdapter(&pDbg->KmtCallbacks, &Adapter);
64 Assert(hr == S_OK);
65 if (hr == S_OK)
66 {
67 uint32_t cbString = (uint32_t)strlen(szString) + 1;
68 uint32_t cbCmd = RT_OFFSETOF(VBOXDISPIFESCAPE_DBGPRINT, aStringBuf[cbString]);
69 PVBOXDISPIFESCAPE_DBGPRINT pCmd = (PVBOXDISPIFESCAPE_DBGPRINT)RTMemAllocZ(cbCmd);
70 Assert(pCmd);
71 if (pCmd)
72 {
73 pCmd->EscapeHdr.escapeCode = VBOXESC_DBGPRINT;
74 memcpy(pCmd->aStringBuf, szString, cbString);
75
76 D3DKMT_ESCAPE EscapeData = {0};
77 EscapeData.hAdapter = Adapter.hAdapter;
78 //EscapeData.hDevice = NULL;
79 EscapeData.Type = D3DKMT_ESCAPE_DRIVERPRIVATE;
80 // EscapeData.Flags.HardwareAccess = 1;
81 EscapeData.pPrivateDriverData = pCmd;
82 EscapeData.PrivateDriverDataSize = cbCmd;
83 //EscapeData.hContext = NULL;
84
85 int Status = pDbg->KmtCallbacks.pfnD3DKMTEscape(&EscapeData);
86 Assert(!Status);
87
88 RTMemFree(pCmd);
89 }
90 hr = vboxDispKmtCloseAdapter(&Adapter);
91 Assert(hr == S_OK);
92 }
93}
94
95void vboxDispLogDrvF(char * szString, ...)
96{
97 char szBuffer[4096] = {0};
98 va_list pArgList;
99 va_start(pArgList, szString);
100 _vsnprintf(szBuffer, sizeof(szBuffer) / sizeof(szBuffer[0]), szString, pArgList);
101 va_end(pArgList);
102
103 vboxDispLogDrv(szBuffer);
104}
105
106VOID vboxVDbgDoDumpSurfRectByAlloc(const char * pPrefix, PVBOXWDDMDISP_ALLOCATION pAlloc, const RECT *pRect, const char* pSuffix)
107{
108 vboxVDbgDoDumpSurfRectByRc(pPrefix, pAlloc->pRc, pAlloc->iAlloc, pRect, pSuffix);
109}
110
111VOID vboxVDbgDoDumpAllocRect(const char * pPrefix, PVBOXWDDMDISP_ALLOCATION pAlloc, const RECT *pRect, const char* pSuffix)
112{
113 if (pPrefix)
114 {
115 vboxVDbgPrint(("%s", pPrefix));
116 }
117
118 Assert(pAlloc->hAllocation);
119
120 D3DDDICB_LOCK LockData;
121 LockData.hAllocation = pAlloc->hAllocation;
122 LockData.PrivateDriverData = 0;
123 LockData.NumPages = 0;
124 LockData.pPages = NULL;
125 LockData.pData = NULL; /* out */
126 LockData.Flags.Value = 0;
127 LockData.Flags.LockEntire =1;
128 LockData.Flags.ReadOnly = 1;
129
130 PVBOXWDDMDISP_DEVICE pDevice = pAlloc->pRc->pDevice;
131
132 HRESULT hr = pDevice->RtCallbacks.pfnLockCb(pDevice->hDevice, &LockData);
133 Assert(hr == S_OK);
134 if (hr == S_OK)
135 {
136 UINT bpp = vboxWddmCalcBitsPerPixel(pAlloc->SurfDesc.format);
137 vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>\n",
138 LockData.pData, pAlloc->D3DWidth, pAlloc->SurfDesc.height, bpp, pAlloc->SurfDesc.pitch));
139 if (pRect)
140 {
141 Assert(pRect->right > pRect->left);
142 Assert(pRect->bottom > pRect->top);
143 vboxVDbgDoPrintRect("rect: ", pRect, "\n");
144 vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>\n",
145 ((uint8_t*)LockData.pData) + (pRect->top * pAlloc->SurfDesc.pitch) + ((pRect->left * bpp) >> 3),
146 pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, pAlloc->SurfDesc.pitch));
147 }
148 Assert(0);
149
150 D3DDDICB_UNLOCK DdiUnlock;
151
152 DdiUnlock.NumAllocations = 1;
153 DdiUnlock.phAllocations = &pAlloc->hAllocation;
154
155 hr = pDevice->RtCallbacks.pfnUnlockCb(pDevice->hDevice, &DdiUnlock);
156 Assert(hr == S_OK);
157 }
158 if (pSuffix)
159 {
160 vboxVDbgPrint(("%s\n", pSuffix));
161 }
162}
163
164
165VOID vboxVDbgDoDumpSurfRectByRc(const char * pPrefix, const PVBOXWDDMDISP_RESOURCE pRc, uint32_t iAlloc, const RECT *pRect, const char* pSuffix)
166{
167 Assert(pRc->cAllocations > iAlloc);
168 BOOL bReleaseSurf = false;
169 IDirect3DSurface9 *pSurf;
170 HRESULT hr = vboxWddmSurfGet(pRc, iAlloc, &pSurf);
171 Assert(hr == S_OK);
172 if (hr == S_OK)
173 {
174 vboxVDbgDoDumpSurfRect(pPrefix, pSurf, pRect, pSuffix, true);
175 pSurf->Release();
176 }
177}
178
179VOID vboxVDbgDoDumpSurfRect(const char * pPrefix, IDirect3DSurface9 *pSurf, const RECT *pRect, const char * pSuffix, bool bBreak)
180{
181 if (pPrefix)
182 {
183 vboxVDbgPrint(("%s", pPrefix));
184 }
185
186 D3DSURFACE_DESC Desc;
187 HRESULT hr = pSurf->GetDesc(&Desc);
188 Assert(hr == S_OK);
189 if (hr == S_OK)
190 {
191 D3DLOCKED_RECT Lr;
192 hr = pSurf->LockRect(&Lr, NULL, D3DLOCK_READONLY);
193 Assert(hr == S_OK);
194 if (hr == S_OK)
195 {
196 UINT bpp = vboxWddmCalcBitsPerPixel((D3DDDIFORMAT)Desc.Format);
197 vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>\n",
198 Lr.pBits, Desc.Width, Desc.Height, bpp, Lr.Pitch));
199 if (pRect)
200 {
201 Assert(pRect->right > pRect->left);
202 Assert(pRect->bottom > pRect->top);
203 vboxVDbgDoPrintRect("rect: ", pRect, "\n");
204 vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>\n",
205 ((uint8_t*)Lr.pBits) + (pRect->top * Lr.Pitch) + ((pRect->left * bpp) >> 3),
206 pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, Lr.Pitch));
207 }
208
209 if (bBreak)
210 {
211 Assert(0);
212
213 hr = pSurf->UnlockRect();
214 Assert(hr == S_OK);
215 }
216 }
217 }
218
219 if (pSuffix)
220 {
221 vboxVDbgPrint(("%s", pSuffix));
222 }
223}
224
225VOID vboxVDbgDoDumpSurf(const char * pPrefix, IDirect3DSurface9 *pSurf, const char * pSuffix)
226{
227 vboxVDbgDoDumpSurfRect(pPrefix, pSurf, NULL, pSuffix, true);
228}
229
230#define VBOXVDBG_STRCASE(_t) \
231 case _t: return #_t;
232#define VBOXVDBG_STRCASE_UNKNOWN() \
233 default: Assert(0); return "Unknown";
234
235const char* vboxVDbgStrCubeFaceType(D3DCUBEMAP_FACES enmFace)
236{
237 switch (enmFace)
238 {
239 VBOXVDBG_STRCASE(D3DCUBEMAP_FACE_POSITIVE_X);
240 VBOXVDBG_STRCASE(D3DCUBEMAP_FACE_NEGATIVE_X);
241 VBOXVDBG_STRCASE(D3DCUBEMAP_FACE_POSITIVE_Y);
242 VBOXVDBG_STRCASE(D3DCUBEMAP_FACE_NEGATIVE_Y);
243 VBOXVDBG_STRCASE(D3DCUBEMAP_FACE_POSITIVE_Z);
244 VBOXVDBG_STRCASE(D3DCUBEMAP_FACE_NEGATIVE_Z);
245 VBOXVDBG_STRCASE_UNKNOWN();
246 }
247}
248
249VOID vboxVDbgDoDumpRcRect(const char * pPrefix, IDirect3DResource9 *pRc, const RECT *pRect, const char * pSuffix)
250{
251 if (pPrefix)
252 {
253 vboxVDbgPrint(("%s", pPrefix));
254 }
255
256 switch (pRc->GetType())
257 {
258 case D3DRTYPE_TEXTURE:
259 {
260 vboxVDbgPrint(("this is a texture\n"));
261
262 IDirect3DTexture9 *pTex = (IDirect3DTexture9*)pRc;
263 IDirect3DSurface9 *pSurf;
264 HRESULT hr = pTex->GetSurfaceLevel(0, &pSurf);
265 Assert(hr == S_OK);
266 if (hr == S_OK)
267 {
268 vboxVDbgDoDumpSurfRect("", pSurf, pRect, "\n", true);
269 pSurf->Release();
270 }
271 break;
272 }
273 case D3DRTYPE_CUBETEXTURE:
274 {
275 vboxVDbgPrint(("this is a cube texture\n"));
276
277 IDirect3DCubeTexture9 *pCubeTex = (IDirect3DCubeTexture9*)pRc;
278 IDirect3DSurface9 *apSurf[6] = {0};
279 for (UINT i = D3DCUBEMAP_FACE_POSITIVE_X; i < D3DCUBEMAP_FACE_POSITIVE_X + 6; ++i)
280 {
281 vboxVDbgPrint(("face %s: ", vboxVDbgStrCubeFaceType((D3DCUBEMAP_FACES)i)));
282
283 HRESULT hr = pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0, &apSurf[i]);
284 Assert(hr == S_OK);
285 if (hr == S_OK)
286 {
287 vboxVDbgDoDumpSurfRect("", apSurf[i], pRect, "\n", false);
288 }
289 else
290 {
291 Assert(0);
292 }
293 }
294
295 Assert(0);
296
297 for (UINT i = D3DCUBEMAP_FACE_POSITIVE_X; i < D3DCUBEMAP_FACE_POSITIVE_X + 6; ++i)
298 {
299 apSurf[i]->UnlockRect();
300 apSurf[i]->Release();
301 }
302
303 break;
304 }
305 case D3DRTYPE_SURFACE:
306 {
307 vboxVDbgPrint(("this is a surface\n"));
308 IDirect3DSurface9 *pSurf = (IDirect3DSurface9 *)pRc;
309 vboxVDbgDoDumpSurfRect("", pSurf, pRect, "\n", true);
310 }
311 default:
312 vboxVDbgPrint(("unsupported rc type\n"));
313 Assert(0);
314 }
315
316 if (pSuffix)
317 {
318 vboxVDbgPrint(("%s", pSuffix));
319 }
320}
321
322VOID vboxVDbgDoDumpRcRectByRc(const char * pPrefix, const PVBOXWDDMDISP_RESOURCE pRc, const RECT *pRect, const char* pSuffix)
323{
324 vboxVDbgDoDumpRcRect(pPrefix, (IDirect3DResource9*)pRc->aAllocations[0].pD3DIf, pRect, pSuffix);
325}
326
327VOID vboxVDbgDoDumpTex(const char * pPrefix, IDirect3DBaseTexture9 *pTexBase, const char * pSuffix)
328{
329 vboxVDbgDoDumpRcRect(pPrefix, pTexBase, NULL, pSuffix);
330}
331
332VOID vboxVDbgDoDumpRt(const char * pPrefix, IDirect3DDevice9 *pDevice, const char * pSuffix)
333{
334 IDirect3DSurface9 *pRt;
335 HRESULT hr = pDevice->GetRenderTarget(0, &pRt);
336 Assert(hr == S_OK);
337 if (hr == S_OK)
338 {
339 vboxVDbgDoDumpSurf(pPrefix, pRt, pSuffix);
340 }
341 else
342 {
343 vboxVDbgPrint((__FUNCTION__": ERROR getting rt: 0x%x", hr));
344 }
345}
346
347void vboxVDbgDoPrintAlloc(const char * pPrefix, const PVBOXWDDMDISP_RESOURCE pRc, uint32_t iAlloc, const char * pSuffix)
348{
349 Assert(pRc->cAllocations > iAlloc);
350 PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[iAlloc];
351 BOOL bPrimary = pRc->RcDesc.fFlags.Primary;
352 BOOL bFrontBuf = FALSE;
353 if (bPrimary)
354 {
355 PVBOXWDDMDISP_SWAPCHAIN pSwapchain = vboxWddmSwapchainForAlloc(pAlloc);
356 Assert(pSwapchain);
357 bFrontBuf = (vboxWddmSwapchainGetFb(pSwapchain)->pAlloc == pAlloc);
358 }
359 vboxVDbgPrint(("%s D3DWidth(%d), width(%d), height(%d), format(%d), usage(%s), %s", pPrefix,
360 pAlloc->D3DWidth, pAlloc->SurfDesc.width, pAlloc->SurfDesc.height, pAlloc->SurfDesc.format,
361 bPrimary ?
362 (bFrontBuf ? "Front Buffer" : "Back Buffer")
363 : "?Everage? Alloc",
364 pSuffix));
365}
366
367void vboxVDbgDoPrintRect(const char * pPrefix, const RECT *pRect, const char * pSuffix)
368{
369 vboxVDbgPrint(("%s left(%d), top(%d), right(%d), bottom(%d) %s", pPrefix, pRect->left, pRect->top, pRect->right, pRect->bottom, pSuffix));
370}
371#endif
372
373#ifdef VBOXWDDMDISP_DEBUG_VEHANDLER
374
375static PVOID g_VBoxWDbgVEHandler = NULL;
376LONG WINAPI vboxVDbgVectoredHandler(struct _EXCEPTION_POINTERS *pExceptionInfo)
377{
378 PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
379 PCONTEXT pContextRecord = pExceptionInfo->ContextRecord;
380 switch (pExceptionRecord->ExceptionCode)
381 {
382 case 0x40010006: /* <- OutputDebugString exception, ignore */
383 case 0xe06d7363: /* <- ms compiler - generated exception related to C++ exception */
384 case 0x000006d9: /* <- RPC exception, ignore */
385 break;
386 default:
387 AssertRelease(0);
388 break;
389 }
390 return EXCEPTION_CONTINUE_SEARCH;
391}
392
393void vboxVDbgVEHandlerRegister()
394{
395 Assert(!g_VBoxWDbgVEHandler);
396 g_VBoxWDbgVEHandler = AddVectoredExceptionHandler(1,vboxVDbgVectoredHandler);
397 Assert(g_VBoxWDbgVEHandler);
398}
399
400void vboxVDbgVEHandlerUnregister()
401{
402 Assert(g_VBoxWDbgVEHandler);
403 ULONG uResult = RemoveVectoredExceptionHandler(g_VBoxWDbgVEHandler);
404 Assert(uResult);
405 g_VBoxWDbgVEHandler = NULL;
406}
407
408#endif
Note: See TracBrowser for help on using the repository browser.

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