Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/common/VBoxVideoLog.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/common/VBoxVideoLog.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/common/VBoxVideoLog.h	(revision 38112)
@@ -39,7 +39,7 @@
 
 #ifdef DEBUG_misha
-# define BREAK_WARN() AssertFailed()
+# define BP_WARN() AssertFailed()
 #else
-# define BREAK_WARN() do {} while(0)
+# define BP_WARN() do {} while(0)
 #endif
 
@@ -58,5 +58,5 @@
     } while (0)
 
-#define WARN(_a)                                                                  \
+#define WARN_NOBP(_a)                                                          \
     do                                                                            \
     {                                                                             \
@@ -64,5 +64,11 @@
         Log(_a);                                                                  \
         Log((VBOX_VIDEO_LOG_SUFFIX_FMT VBOX_VIDEO_LOG_SUFFIX_PARMS));             \
-        BREAK_WARN(); \
+    } while (0)
+
+#define WARN(_a)                                                                  \
+    do                                                                            \
+    {                                                                             \
+        WARN_NOBP(_a);                                                         \
+        BP_WARN();                                                             \
     } while (0)
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/common/wddm/VBoxMPIf.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/common/wddm/VBoxMPIf.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/common/wddm/VBoxMPIf.h	(revision 38112)
@@ -292,9 +292,18 @@
 #define VBOXWDDM_RECTS_INFO_SIZE(_pRects) (VBOXVIDEOCM_CMD_RECTS_SIZE4CRECTS((_pRects)->cRects))
 
+typedef enum
+{
+    /* command to be post to user mode */
+    VBOXVIDEOCM_CMD_TYPE_UM = 0,
+    /* control command processed in kernel mode */
+    VBOXVIDEOCM_CMD_TYPE_CTL_KM,
+    VBOXVIDEOCM_CMD_DUMMY_32BIT = 0x7fffffff
+} VBOXVIDEOCM_CMD_TYPE;
+
 typedef struct VBOXVIDEOCM_CMD_HDR
 {
     uint64_t u64UmData;
     uint32_t cbCmd;
-    uint32_t u32CmdSpecific;
+    VBOXVIDEOCM_CMD_TYPE enmType;
 }VBOXVIDEOCM_CMD_HDR, *PVBOXVIDEOCM_CMD_HDR;
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.cpp	(revision 38112)
@@ -1023,10 +1023,18 @@
 }
 
+#define VBOX_WDDM_SHRC_WO_NOTIFY
 static BOOLEAN vboxWddmDalCheckAdd(PVBOXWDDMDISP_DEVICE pDevice, PVBOXWDDMDISP_ALLOCATION pAlloc, BOOLEAN fWrite)
 {
     if (!pAlloc->hSharedHandle /* only shared resources matter */
-            || !fWrite) /* only write op matter */
-    {
+#ifdef VBOX_WDDM_SHRC_WO_NOTIFY
+            || !fWrite /* only write op matter */
+#endif
+            )
+    {
+#ifdef VBOX_WDDM_SHRC_WO_NOTIFY
         Assert(!pAlloc->DirtyAllocListEntry.pNext || (!fWrite && pAlloc->hSharedHandle && pAlloc->fDirtyWrite));
+#else
+        Assert(!pAlloc->DirtyAllocListEntry.pNext);
+#endif
         return FALSE;
     }
@@ -1708,5 +1716,5 @@
 
     Assert(cAllocsKm == Buf.SwapchainInfo.SwapchainInfo.cAllocs || !cAllocsKm);
-
+    HRESULT hr = S_OK;
     if (cAllocsKm == Buf.SwapchainInfo.SwapchainInfo.cAllocs)
     {
@@ -1717,5 +1725,5 @@
         DdiEscape.pPrivateDriverData = &Buf.SwapchainInfo;
         DdiEscape.PrivateDriverDataSize = RT_OFFSETOF(VBOXDISPIFESCAPE_SWAPCHAININFO, SwapchainInfo.ahAllocs[Buf.SwapchainInfo.SwapchainInfo.cAllocs]);
-        HRESULT hr = pDevice->RtCallbacks.pfnEscapeCb(pDevice->pAdapter->hAdapter, &DdiEscape);
+        hr = pDevice->RtCallbacks.pfnEscapeCb(pDevice->pAdapter->hAdapter, &DdiEscape);
 #ifdef DEBUG_misha
         Assert(hr == S_OK);
@@ -1725,7 +1733,6 @@
             pSwapchain->hSwapchainKm = Buf.SwapchainInfo.SwapchainInfo.hSwapchainKm;
         }
-
-        return hr;
-    }
+    }
+
     return S_OK;
 }
@@ -1777,6 +1784,9 @@
         pSwapchain->aRTs[i].pAlloc->pSwapchain = NULL;
     }
+
+    /* first do a Km destroy to ensure all km->um region submissions are completed */
+    vboxWddmSwapchainKmDestroy(pDevice, pSwapchain);
+    vboxDispMpInternalCancel(&pDevice->DefaultContext, pSwapchain);
     vboxWddmSwapchainDestroyIf(pDevice, pSwapchain);
-    vboxWddmSwapchainKmDestroy(pDevice, pSwapchain);
     vboxWddmSwapchainInit(pSwapchain);
 }
@@ -2843,6 +2853,11 @@
                     if (hr == S_OK)
                     {
-                        vboxVDbgPrint(("VBoxDispD3D: DLL loaded OK\n"));
-                        return TRUE;
+                        hr = vboxDispMpInternalInit();
+                        Assert(hr == S_OK);
+                        if (hr == S_OK)
+                        {
+                            vboxVDbgPrint(("VBoxDispD3D: DLL loaded OK\n"));
+                            return TRUE;
+                        }
                     }
                     VbglR3Term();
@@ -2857,11 +2872,16 @@
             vboxVDbgVEHandlerUnregister();
 #endif
-            HRESULT hr = vboxDispCmTerm();
+            HRESULT hr = vboxDispMpInternalTerm();
             Assert(hr == S_OK);
             if (hr == S_OK)
             {
-                VbglR3Term();
-                /// @todo RTR3Term();
-                return TRUE;
+                hr = vboxDispCmTerm();
+                Assert(hr == S_OK);
+                if (hr == S_OK)
+                {
+                    VbglR3Term();
+                    /// @todo RTR3Term();
+                    return TRUE;
+                }
             }
 
@@ -3299,4 +3319,5 @@
             pD3DIfTex = (IDirect3DTexture9*)pRc->aAllocations[0].pD3DIf;
 
+            VBOXVDBG_BREAK_SHARED(pRc);
             VBOXVDBG_DUMP_SETTEXTURE(pRc);
         }
@@ -3305,4 +3326,5 @@
             pD3DIfTex = (IDirect3DCubeTexture9*)pRc->aAllocations[0].pD3DIf;
 
+            VBOXVDBG_BREAK_SHARED(pRc);
             VBOXVDBG_DUMP_SETTEXTURE(pRc);
         }
@@ -3433,5 +3455,5 @@
     HRESULT hr = S_OK;
 
-    VBOXVDBG_DUMP_DRAWPRIM_ENTER(pDevice9If);
+    VBOXVDBG_DUMP_DRAWPRIM_ENTER(pDevice);
 
     if (!pDevice->cStreamSources)
@@ -3491,5 +3513,5 @@
     vboxWddmDalCheckAddRts(pDevice);
 
-    VBOXVDBG_DUMP_DRAWPRIM_LEAVE(pDevice9If);
+    VBOXVDBG_DUMP_DRAWPRIM_LEAVE(pDevice);
 
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p), hr(0x%x)\n", hDevice, hr));
@@ -3506,5 +3528,5 @@
 
     IDirect3DDevice9 * pDevice9If = VBOXDISP_D3DEV(pDevice);
-    VBOXVDBG_DUMP_DRAWPRIM_ENTER(pDevice9If);
+    VBOXVDBG_DUMP_DRAWPRIM_ENTER(pDevice);
 
 #ifdef DEBUG
@@ -3542,5 +3564,5 @@
     vboxWddmDalCheckAddRts(pDevice);
 
-    VBOXVDBG_DUMP_DRAWPRIM_LEAVE(pDevice9If);
+    VBOXVDBG_DUMP_DRAWPRIM_LEAVE(pDevice);
 
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p), hr(0x%x)\n", hDevice, hr));
@@ -3614,5 +3636,5 @@
     hr = pDevice9If->DrawPrimitive(pData->PrimitiveType, pData->FirstVertexOffset, pData->PrimitiveCount);
 #else
-    VBOXVDBG_DUMP_DRAWPRIM_ENTER(pDevice9If);
+    VBOXVDBG_DUMP_DRAWPRIM_ENTER(pDevice);
 
 #ifdef DEBUG
@@ -3662,5 +3684,5 @@
     vboxWddmDalCheckAddRts(pDevice);
 
-    VBOXVDBG_DUMP_DRAWPRIM_LEAVE(pDevice9If);
+    VBOXVDBG_DUMP_DRAWPRIM_LEAVE(pDevice);
 
     Assert(hr == S_OK);
@@ -4191,4 +4213,6 @@
                     Assert(pRc->RcDesc.enmPool == D3DDDIPOOL_SYSTEMMEM);
                 }
+
+                VBOXVDBG_DUMP_LOCK_ST(pData);
             }
         }
@@ -4475,4 +4499,6 @@
             Assert(pData->SubResourceIndex < pRc->cAllocations);
             PVBOXWDDMDISP_ALLOCATION pLockAlloc = &pRc->aAllocations[pData->SubResourceIndex];
+
+            VBOXVDBG_DUMP_UNLOCK_ST(pData);
 
             --pLockAlloc->LockInfo.cLocks;
@@ -4993,4 +5019,11 @@
                             Assert(!!(pResource->Flags.SharedResource) == !!(hSharedHandle));
                             pAllocation->hSharedHandle = hSharedHandle;
+#ifdef DEBUG_misha
+                            if (pResource->Flags.SharedResource)
+                            {
+                                vboxVDbgPrint(("\n\n********\nShared Texture pAlloc(0x%p) Handle(0x%x), (0n%d) CREATED, pid (0x%x), (0n%d)\n***********\n\n",
+                                        pAllocation, hSharedHandle, hSharedHandle, GetCurrentProcessId(), GetCurrentProcessId()));
+                            }
+#endif
                         }
 
@@ -5302,4 +5335,11 @@
         {
             PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[i];
+#ifdef DEBUG_misha
+            if (pAlloc->hSharedHandle)
+            {
+                vboxVDbgPrint(("\n\n********\nShared Texture pAlloc(0x%p) Handle(0x%x), (0n%d) DESTROYED, pid (0x%x), (0n%d)\n***********\n\n",
+                        pAlloc, pAlloc->hSharedHandle, pAlloc->hSharedHandle, GetCurrentProcessId(), GetCurrentProcessId()));
+            }
+#endif
             if (pAlloc->pD3DIf)
                 pAlloc->pD3DIf->Release();
@@ -5585,5 +5625,5 @@
         vboxWddmDalNotifyChange(pDevice);
 
-        VBOXVDBG_DUMP_FLUSH(pDevice->pDevice9If);
+        VBOXVDBG_DUMP_FLUSH(pDevice);
     }
     vboxVDbgPrintF(("<== "__FUNCTION__", hDevice(0x%p), hr(0x%x)\n", hDevice, hr));
@@ -5970,5 +6010,7 @@
                 Assert(pSrcSurfIf);
 
-                VBOXVDBG_DUMP_BLT_ENTER(pSrcRc, pSrcSurfIf, &pData->SrcRect, pDstRc, pDstSurfIf, &pData->DstRect);
+                VBOXVDBG_BREAK_SHARED(pSrcRc);
+                VBOXVDBG_BREAK_SHARED(pDstRc);
+                VBOXVDBG_DUMP_BLT_ENTER(pSrcAlloc, pSrcSurfIf, &pData->SrcRect, pDstAlloc, pDstSurfIf, &pData->DstRect);
 
                 /* we support only Point & Linear, we ignore [Begin|Continue|End]PresentToDwm */
@@ -5981,5 +6023,5 @@
                 Assert(hr == S_OK);
 
-                VBOXVDBG_DUMP_BLT_LEAVE(pSrcRc, pSrcSurfIf, &pData->SrcRect, pDstRc, pDstSurfIf, &pData->DstRect);
+                VBOXVDBG_DUMP_BLT_LEAVE(pSrcAlloc, pSrcSurfIf, &pData->SrcRect, pDstAlloc, pDstSurfIf, &pData->DstRect);
 
                 pSrcSurfIf->Release();
@@ -6453,9 +6495,10 @@
     {
 //    Assert(!pDevice->cScreens);
+        /* destroy the device first, since destroying PVBOXWDDMDISP_SWAPCHAIN would result in a device window termination */
+        if (pDevice->pDevice9If)
+        {
+            pDevice->pDevice9If->Release();
+        }
         vboxWddmSwapchainDestroyAll(pDevice);
-        if (pDevice->pDevice9If)
-        {
-            pDevice->pDevice9If->Release();
-        }
     }
 
@@ -6814,5 +6857,7 @@
 
 #ifdef DEBUG_misha
-                    vboxVDbgPrint(("\n\n********\nShared Resource (0x%x), (0n%d) openned\n\n\n", hSharedHandle, hSharedHandle));
+                    vboxVDbgPrint(("\n\n********\nShared Texture pAlloc(0x%p) Handle(0x%x), (0n%d) OPENED, pid (0x%x), (0n%d)\n***********\n\n",
+                            pAllocation, hSharedHandle, hSharedHandle, GetCurrentProcessId(), GetCurrentProcessId()));
+
 #endif
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3D.h	(revision 38112)
@@ -207,4 +207,7 @@
     D3DDDI_LOCKFLAGS fFlags;
     D3DLOCKED_RECT LockedRect;
+#ifdef VBOXWDDMDISP_DEBUG
+    PVOID pvData;
+#endif
 } VBOXWDDMDISP_LOCKINFO;
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DCmn.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DCmn.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DCmn.h	(revision 38112)
@@ -38,4 +38,5 @@
 #include "common/wddm/VBoxMPIf.h"
 #include "VBoxDispCm.h"
+#include "VBoxDispMpInternal.h"
 #include "VBoxDispKmt.h"
 #ifdef VBOX_WITH_CRHGSMI
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispD3DIf.cpp	(revision 38112)
@@ -269,5 +269,5 @@
     if (!GetClassInfo(hInstance, VBOXDISPWND_NAME, &wc))
     {
-        wc.style = CS_OWNDC;
+        wc.style = 0;//CS_OWNDC;
         wc.lpfnWndProc = WindowProc;
         wc.cbClsExtra = 0;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.cpp	(revision 38112)
@@ -50,11 +50,16 @@
 
 #ifdef VBOXWDDMDISP_DEBUG
-DWORD g_VBoxVDbgFDumpSetTexture = 0;
-DWORD g_VBoxVDbgFDumpDrawPrim = 0;
-DWORD g_VBoxVDbgFDumpTexBlt = 0;
-DWORD g_VBoxVDbgFDumpBlt = 0;
-DWORD g_VBoxVDbgFDumpRtSynch = 0;
-DWORD g_VBoxVDbgFDumpFlush = 0;
-DWORD g_VBoxVDbgFDumpShared = 0;
+#define VBOXWDDMDISP_DEBUG_DUMP_DEFAULT 1
+DWORD g_VBoxVDbgFDumpSetTexture = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpDrawPrim = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpTexBlt = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpBlt = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpRtSynch = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpFlush = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpShared = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpLock = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+DWORD g_VBoxVDbgFDumpUnlock = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
+
+DWORD g_VBoxVDbgFBreakShared = VBOXWDDMDISP_DEBUG_DUMP_DEFAULT;
 
 DWORD g_VBoxVDbgPid = 0;
@@ -175,4 +180,11 @@
 {
     vboxVDbgDoDumpSurfRectByRc(pPrefix, pAlloc->pRc, pAlloc->iAlloc, pRect, pSuffix);
+}
+
+VOID vboxVDbgDoPrintDumpCmd(const void *pvData, uint32_t width, uint32_t height, uint32_t bpp, uint32_t pitch)
+{
+    vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
+            pvData, width, height, bpp, pitch,
+            pvData, width, height, bpp, pitch));
 }
 
@@ -207,7 +219,5 @@
     {
         UINT bpp = vboxWddmCalcBitsPerPixel(pAlloc->SurfDesc.format);
-        vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
-                LockData.pData, pAlloc->D3DWidth, pAlloc->SurfDesc.height, bpp, pAlloc->SurfDesc.pitch,
-                LockData.pData, pAlloc->D3DWidth, pAlloc->SurfDesc.height, bpp, pAlloc->SurfDesc.pitch));
+        vboxVDbgDoPrintDumpCmd(LockData.pData, pAlloc->D3DWidth, pAlloc->SurfDesc.height, bpp, pAlloc->SurfDesc.pitch);
         if (pRect)
         {
@@ -215,9 +225,6 @@
             Assert(pRect->bottom > pRect->top);
             vboxVDbgDoPrintRect("rect: ", pRect, "\n");
-            vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
-                    ((uint8_t*)LockData.pData) + (pRect->top * pAlloc->SurfDesc.pitch) + ((pRect->left * bpp) >> 3),
-                    pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, pAlloc->SurfDesc.pitch,
-                    ((uint8_t*)LockData.pData) + (pRect->top * pAlloc->SurfDesc.pitch) + ((pRect->left * bpp) >> 3),
-                    pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, pAlloc->SurfDesc.pitch));
+            vboxVDbgDoPrintDumpCmd(((uint8_t*)LockData.pData) + (pRect->top * pAlloc->SurfDesc.pitch) + ((pRect->left * bpp) >> 3),
+                    pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, pAlloc->SurfDesc.pitch);
         }
         Assert(0);
@@ -270,7 +277,5 @@
         {
             UINT bpp = vboxWddmCalcBitsPerPixel((D3DDDIFORMAT)Desc.Format);
-            vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">surface info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
-                    Lr.pBits, Desc.Width, Desc.Height, bpp, Lr.Pitch,
-                    Lr.pBits, Desc.Width, Desc.Height, bpp, Lr.Pitch));
+            vboxVDbgDoPrintDumpCmd(Lr.pBits, Desc.Width, Desc.Height, bpp, Lr.Pitch);
             if (pRect)
             {
@@ -278,9 +283,6 @@
                 Assert(pRect->bottom > pRect->top);
                 vboxVDbgDoPrintRect("rect: ", pRect, "\n");
-                vboxVDbgPrint(("<?dml?><exec cmd=\"!vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d\">rect info</exec>, ( !vbvdbg.ms 0x%p 0n%d 0n%d 0n%d 0n%d )\n",
-                        ((uint8_t*)Lr.pBits) + (pRect->top * Lr.Pitch) + ((pRect->left * bpp) >> 3),
-                        pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, Lr.Pitch,
-                        ((uint8_t*)Lr.pBits) + (pRect->top * Lr.Pitch) + ((pRect->left * bpp) >> 3),
-                        pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, Lr.Pitch));
+                vboxVDbgDoPrintDumpCmd(((uint8_t*)Lr.pBits) + (pRect->top * Lr.Pitch) + ((pRect->left * bpp) >> 3),
+                        pRect->right - pRect->left, pRect->bottom - pRect->top, bpp, Lr.Pitch);
             }
 
@@ -386,4 +388,5 @@
             IDirect3DSurface9 *pSurf = (IDirect3DSurface9 *)pRc;
             vboxVDbgDoDumpSurfRect("", pSurf, pRect, "\n", true);
+            break;
         }
         default:
@@ -398,7 +401,22 @@
 }
 
+VOID vboxVDbgDoDumpRcRectByAlloc(const char * pPrefix, const PVBOXWDDMDISP_ALLOCATION pAlloc, IDirect3DResource9 *pD3DIf, const RECT *pRect, const char* pSuffix)
+{
+    if (pPrefix)
+        vboxVDbgPrint(("%s", pPrefix));
+
+    if (!pD3DIf)
+    {
+        pD3DIf = (IDirect3DResource9*)pAlloc->pD3DIf;
+    }
+
+    vboxVDbgPrint(("Rc(0x%p), pAlloc(0x%x), pD3DIf(0x%p), SharedHandle(0x%p)\n", pAlloc->pRc, pAlloc, pD3DIf, pAlloc->pRc->aAllocations[0].hSharedHandle));
+
+    vboxVDbgDoDumpRcRect("", pD3DIf, pRect, pSuffix);
+}
+
 VOID vboxVDbgDoDumpRcRectByRc(const char * pPrefix, const PVBOXWDDMDISP_RESOURCE pRc, const RECT *pRect, const char* pSuffix)
 {
-    vboxVDbgDoDumpRcRect(pPrefix, (IDirect3DResource9*)pRc->aAllocations[0].pD3DIf, pRect, pSuffix);
+    vboxVDbgDoDumpRcRectByAlloc(pPrefix, &pRc->aAllocations[0], NULL, pRect, pSuffix);
 }
 
@@ -408,17 +426,97 @@
 }
 
-VOID vboxVDbgDoDumpRt(const char * pPrefix, IDirect3DDevice9 *pDevice, const char * pSuffix)
-{
-    IDirect3DSurface9 *pRt;
-    HRESULT hr = pDevice->GetRenderTarget(0, &pRt);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        vboxVDbgDoDumpSurf(pPrefix, pRt, pSuffix);
+VOID vboxVDbgDoDumpRt(const char * pPrefix, PVBOXWDDMDISP_DEVICE pDevice, const char * pSuffix)
+{
+    for (UINT i = 0; i < pDevice->cRTs; ++i)
+    {
+        IDirect3DSurface9 *pRt;
+        PVBOXWDDMDISP_ALLOCATION pAlloc = pDevice->apRTs[i];
+        IDirect3DDevice9 *pDeviceIf = pDevice->pDevice9If;
+        HRESULT hr = pDeviceIf->GetRenderTarget(i, &pRt);
+        Assert(hr == S_OK);
+        if (hr == S_OK)
+        {
+//            Assert(pAlloc->pD3DIf == pRt);
+            vboxVDbgDoDumpRcRectByAlloc(pPrefix, pAlloc, NULL, NULL, "\n");
+            pRt->Release();
+        }
+        else
+        {
+            vboxVDbgPrint((__FUNCTION__": ERROR getting rt: 0x%x", hr));
+        }
+    }
+}
+
+VOID vboxVDbgDoDumpLockUnlockSurfTex(const char * pPrefix, const PVBOXWDDMDISP_ALLOCATION pAlloc, const char * pSuffix, bool fBreak)
+{
+    if (pPrefix)
+    {
+        vboxVDbgPrint(("%s", pPrefix));
+    }
+
+    Assert(!pAlloc->hSharedHandle);
+
+    UINT bpp = vboxWddmCalcBitsPerPixel(pAlloc->SurfDesc.format);
+    uint32_t width, height, pitch;
+    RECT Rect, *pRect;
+    void *pvData;
+    Assert(!pAlloc->LockInfo.fFlags.RangeValid);
+    Assert(!pAlloc->LockInfo.fFlags.BoxValid);
+    if (pAlloc->LockInfo.fFlags.AreaValid)
+    {
+        pRect = &pAlloc->LockInfo.Area;
+        width = pAlloc->LockInfo.Area.left - pAlloc->LockInfo.Area.right;
+        height = pAlloc->LockInfo.Area.bottom - pAlloc->LockInfo.Area.top;
     }
     else
     {
-        vboxVDbgPrint((__FUNCTION__": ERROR getting rt: 0x%x", hr));
-    }
+        width = pAlloc->SurfDesc.width;
+        height = pAlloc->SurfDesc.height;
+        Rect.top = 0;
+        Rect.bottom = height;
+        Rect.left = 0;
+        Rect.right = width;
+        pRect = &Rect;
+    }
+
+    if (pAlloc->LockInfo.fFlags.NotifyOnly)
+    {
+        pitch = pAlloc->SurfDesc.pitch;
+        pvData = ((uint8_t*)pAlloc->pvMem) + pitch*pRect->top + ((bpp*pRect->left) >> 3);
+    }
+    else
+    {
+        pvData = pAlloc->LockInfo.pvData;
+    }
+
+    vboxVDbgPrint(("pRc(0x%p) iAlloc(%d), type(%d), cLocks(%d)\n", pAlloc->pRc, pAlloc->iAlloc, pAlloc->enmD3DIfType, pAlloc->LockInfo.cLocks));
+
+    vboxVDbgDoPrintDumpCmd(pvData, width, height, bpp, pitch);
+
+    if (fBreak)
+    {
+        Assert(0);
+    }
+
+    if (pSuffix)
+    {
+        vboxVDbgPrint(("%s", pSuffix));
+    }
+
+}
+
+VOID vboxVDbgDoDumpLockSurfTex(const char * pPrefix, const D3DDDIARG_LOCK* pData, const char * pSuffix, bool fBreak)
+{
+    const PVBOXWDDMDISP_RESOURCE pRc = (const PVBOXWDDMDISP_RESOURCE)pData->hResource;
+    const PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[pData->SubResourceIndex];
+    pAlloc->LockInfo.pvData = pData->pSurfData;
+    vboxVDbgDoDumpLockUnlockSurfTex(pPrefix, pAlloc, pSuffix, fBreak);
+}
+
+VOID vboxVDbgDoDumpUnlockSurfTex(const char * pPrefix, const D3DDDIARG_UNLOCK* pData, const char * pSuffix, bool fBreak)
+{
+    const PVBOXWDDMDISP_RESOURCE pRc = (const PVBOXWDDMDISP_RESOURCE)pData->hResource;
+    const PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[pData->SubResourceIndex];
+    vboxVDbgDoDumpLockUnlockSurfTex(pPrefix, pAlloc, pSuffix, fBreak);
 }
 
@@ -426,5 +524,5 @@
 {
     Assert(pRc->cAllocations > iAlloc);
-    PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[iAlloc];
+    const PVBOXWDDMDISP_ALLOCATION pAlloc = &pRc->aAllocations[iAlloc];
     BOOL bPrimary = pRc->RcDesc.fFlags.Primary;
     BOOL bFrontBuf = FALSE;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispDbg.h	(revision 38112)
@@ -111,4 +111,8 @@
 extern DWORD g_VBoxVDbgFDumpFlush;
 extern DWORD g_VBoxVDbgFDumpShared;
+extern DWORD g_VBoxVDbgFDumpLock;
+extern DWORD g_VBoxVDbgFDumpUnlock;
+
+extern DWORD g_VBoxVDbgFBreakShared;
 
 void vboxDispLogDrvF(char * szString, ...);
@@ -126,10 +130,14 @@
 VOID vboxVDbgDoDumpRcRect(const char * pPrefix, IDirect3DResource9 *pRc, const RECT *pRect, const char * pSuffix);
 VOID vboxVDbgDoDumpRcRectByRc(const char * pPrefix, const PVBOXWDDMDISP_RESOURCE pRc, const RECT *pRect, const char* pSuffix);
+VOID vboxVDbgDoDumpRcRectByAlloc(const char * pPrefix, const PVBOXWDDMDISP_ALLOCATION pAlloc, IDirect3DResource9 *pD3DIf, const RECT *pRect, const char* pSuffix);
 VOID vboxVDbgDoDumpTex(const char * pPrefix, IDirect3DBaseTexture9 *pTexBase, const char * pSuffix);
-VOID vboxVDbgDoDumpRt(const char * pPrefix, IDirect3DDevice9 *pDevice, const char * pSuffix);
+VOID vboxVDbgDoDumpRt(const char * pPrefix, struct VBOXWDDMDISP_DEVICE *pDevice, const char * pSuffix);
 
 void vboxVDbgDoPrintRect(const char * pPrefix, const RECT *pRect, const char * pSuffix);
 void vboxVDbgDoPrintAlloc(const char * pPrefix, const PVBOXWDDMDISP_RESOURCE pRc, uint32_t iAlloc, const char * pSuffix);
-VOID vboxVDbgDoDumpRtData(char * pPrefix, IDirect3DDevice9 *pDevice, char * pSuffix);
+
+VOID vboxVDbgDoDumpLockSurfTex(const char * pPrefix, const D3DDDIARG_LOCK* pData, const char * pSuffix, bool fBreak);
+VOID vboxVDbgDoDumpUnlockSurfTex(const char * pPrefix, const D3DDDIARG_UNLOCK* pData, const char * pSuffix, bool fBreak);
+
 
 extern DWORD g_VBoxVDbgPid;
@@ -144,4 +152,11 @@
         )
 
+#define VBOXVDBG_IS_BREAK_ALLOWED(_type) ( \
+        g_VBoxVDbgFBreak##_type \
+        && (g_VBoxVDbgFBreak##_type == 1 \
+                || VBOXVDBG_IS_DUMP_ALLOWED_PID(g_VBoxVDbgFBreak##_type) \
+           ) \
+        )
+
 #define VBOXVDBG_IS_DUMP_SHARED_ALLOWED(_pRc) (\
         (_pRc)->RcDesc.fFlags.SharedResource \
@@ -149,4 +164,16 @@
         )
 
+#define VBOXVDBG_IS_BREAK_SHARED_ALLOWED(_pRc) (\
+        (_pRc)->RcDesc.fFlags.SharedResource \
+        && VBOXVDBG_IS_BREAK_ALLOWED(Shared) \
+        )
+
+#define VBOXVDBG_BREAK_SHARED(_pRc) do { \
+        if (VBOXVDBG_IS_BREAK_SHARED_ALLOWED(_pRc)) { \
+            vboxVDbgPrint(("Break on shared access: Rc(0x%p), SharedHandle(0x%p)\n", (_pRc), (_pRc)->aAllocations[0].hSharedHandle)); \
+            AssertFailed(); \
+        } \
+    } while (0)
+
 #define VBOXVDBG_DUMP_DRAWPRIM_ENTER(_pDevice) do { \
         if (VBOXVDBG_IS_DUMP_ALLOWED(DrawPrim)) \
@@ -168,6 +195,5 @@
                 ) \
         { \
-            vboxVDbgPrint(("== "__FUNCTION__": Texture Dump, SharedHandle(0x%p)\n", (_pRc)->aAllocations[0].hSharedHandle)); \
-            vboxVDbgDoDumpRcRectByRc("", _pRc, NULL, "\n"); \
+            vboxVDbgDoDumpRcRectByRc("== "__FUNCTION__": ", _pRc, NULL, "\n"); \
         } \
     } while (0)
@@ -181,6 +207,6 @@
             RECT _DstRect; \
             vboxWddmRectMoved(&_DstRect, (_pSrcRect), (_pDstPoint)->x, (_pDstPoint)->y); \
-            vboxVDbgDoDumpRcRectByRc("==>"__FUNCTION__" Src:\n", (_pSrcRc), (_pSrcRect), "\n"); \
-            vboxVDbgDoDumpRcRectByRc("==>"__FUNCTION__" Dst:\n", (_pDstRc), &_DstRect, "\n"); \
+            vboxVDbgDoDumpRcRectByRc("==>"__FUNCTION__" Src: ", (_pSrcRc), (_pSrcRect), "\n"); \
+            vboxVDbgDoDumpRcRectByRc("==>"__FUNCTION__" Dst: ", (_pDstRc), &_DstRect, "\n"); \
         } \
     } while (0)
@@ -194,32 +220,28 @@
             RECT _DstRect; \
             vboxWddmRectMoved(&_DstRect, (_pSrcRect), (_pDstPoint)->x, (_pDstPoint)->y); \
-            vboxVDbgDoDumpRcRectByRc("<=="__FUNCTION__" Src:\n", (_pSrcRc), (_pSrcRect), "\n"); \
-            vboxVDbgDoDumpRcRectByRc("<=="__FUNCTION__" Dst:\n", (_pDstRc), &_DstRect, "\n"); \
-        } \
-    } while (0)
-
-#define VBOXVDBG_DUMP_BLT_ENTER(_pSrcRc, _pSrcSurf, _pSrcRect, _pDstRc, _pDstSurf, _pDstRect) do { \
+            vboxVDbgDoDumpRcRectByRc("<=="__FUNCTION__" Src: ", (_pSrcRc), (_pSrcRect), "\n"); \
+            vboxVDbgDoDumpRcRectByRc("<=="__FUNCTION__" Dst: ", (_pDstRc), &_DstRect, "\n"); \
+        } \
+    } while (0)
+
+#define VBOXVDBG_DUMP_BLT_ENTER(_pSrcAlloc, _pSrcSurf, _pSrcRect, _pDstAlloc, _pDstSurf, _pDstRect) do { \
         if (VBOXVDBG_IS_DUMP_ALLOWED(Blt) \
-                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED(_pSrcRc) \
-                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED(_pDstRc) \
-                ) \
-        { \
-            vboxVDbgPrint(("==>"__FUNCTION__" Src: SharedHandle(0x%p)\n", (_pSrcRc)->aAllocations[0].hSharedHandle)); \
-            vboxVDbgDoDumpSurfRect("", (_pSrcSurf), (_pSrcRect), "\n", true); \
-            vboxVDbgPrint(("==>"__FUNCTION__" Dst: SharedHandle(0x%p)\n", (_pDstRc)->aAllocations[0].hSharedHandle)); \
-            vboxVDbgDoDumpSurfRect("", (_pDstSurf), (_pDstRect), "\n", true); \
-        } \
-    } while (0)
-
-#define VBOXVDBG_DUMP_BLT_LEAVE(_pSrcRc, _pSrcSurf, _pSrcRect, _pDstRc, _pDstSurf, _pDstRect) do { \
+                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED((_pSrcAlloc)->pRc) \
+                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED((_pDstAlloc)->pRc) \
+                ) \
+        { \
+            vboxVDbgDoDumpRcRectByAlloc("==>"__FUNCTION__" Src: ", (_pSrcAlloc), (_pSrcSurf), (_pSrcRect), "\n"); \
+            vboxVDbgDoDumpRcRectByAlloc("==>"__FUNCTION__" Dst: ", (_pDstAlloc), (_pDstSurf), (_pDstRect), "\n"); \
+        } \
+    } while (0)
+
+#define VBOXVDBG_DUMP_BLT_LEAVE(_pSrcAlloc, _pSrcSurf, _pSrcRect, _pDstAlloc, _pDstSurf, _pDstRect) do { \
         if (VBOXVDBG_IS_DUMP_ALLOWED(Blt) \
-                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED(_pSrcRc) \
-                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED(_pDstRc) \
-                ) \
-        { \
-            vboxVDbgPrint(("<=="__FUNCTION__" Src: SharedHandle(0x%p)\n", (_pSrcRc)->aAllocations[0].hSharedHandle)); \
-            vboxVDbgDoDumpSurfRect("", (_pSrcSurf), (_pSrcRect), "\n", true); \
-            vboxVDbgPrint(("<=="__FUNCTION__" Dst: SharedHandle(0x%p)\n", (_pDstRc)->aAllocations[0].hSharedHandle)); \
-            vboxVDbgDoDumpSurfRect("", (_pDstSurf), (_pDstRect), "\n", true); \
+                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED((_pSrcAlloc)->pRc) \
+                || VBOXVDBG_IS_DUMP_SHARED_ALLOWED((_pDstAlloc)->pRc) \
+                ) \
+        { \
+            vboxVDbgDoDumpRcRectByAlloc("<=="__FUNCTION__" Src: ", (_pSrcAlloc), (_pSrcSurf), (_pSrcRect), "\n"); \
+            vboxVDbgDoDumpRcRectByAlloc("<=="__FUNCTION__" Dst: ", (_pDstAlloc), (_pDstSurf), (_pDstRect), "\n"); \
         } \
     } while (0)
@@ -238,4 +260,22 @@
         }\
     } while (0)
+
+#define VBOXVDBG_DUMP_LOCK_ST(_pData) do { \
+        if (VBOXVDBG_IS_DUMP_ALLOWED(Lock) \
+                || VBOXVDBG_IS_DUMP_ALLOWED(Unlock) \
+                ) \
+        { \
+            vboxVDbgDoDumpLockSurfTex("== "__FUNCTION__": ", (_pData), "\n", VBOXVDBG_IS_DUMP_ALLOWED(Lock)); \
+        } \
+    } while (0)
+
+#define VBOXVDBG_DUMP_UNLOCK_ST(_pData) do { \
+        if (VBOXVDBG_IS_DUMP_ALLOWED(Unlock) \
+                ) \
+        { \
+            vboxVDbgDoDumpUnlockSurfTex("== "__FUNCTION__": ", (_pData), "\n", true); \
+        } \
+    } while (0)
+
 #else
 #define VBOXVDBG_DUMP_DRAWPRIM_ENTER(_pDevice) do { } while (0)
@@ -248,4 +288,6 @@
 #define VBOXVDBG_DUMP_SYNC_RT(_pBbSurf) do { } while (0)
 #define VBOXVDBG_DUMP_FLUSH(_pDevice) do { } while (0)
+#define VBOXVDBG_DUMP_LOCK_ST(_pData) do { } while (0)
+#define VBOXVDBG_DUMP_UNLOCK_ST(_pData) do { } while (0)
 #endif
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispMp.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispMp.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxDispMp.cpp	(revision 38112)
@@ -30,4 +30,5 @@
 typedef struct VBOXDISPMP
 {
+    CRITICAL_SECTION CritSect;
     PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pEscapeCmd;
     uint32_t cbEscapeCmd;
@@ -54,4 +55,10 @@
 }
 
+DECLINLINE(VOID) vboxVideoCmIterCopyToBack(PVBOXVIDEOCM_ITERATOR pIter, PVBOXVIDEOCM_CMD_HDR pCur)
+{
+    memcpy((((uint8_t*)pIter->pCur) + pIter->cbRemain), pCur, pCur->cbCmd);
+    pIter->cbRemain += pCur->cbCmd;
+}
+
 DECLINLINE(bool) vboxVideoCmIterHasNext(PVBOXVIDEOCM_ITERATOR pIter)
 {
@@ -63,4 +70,5 @@
 DECLCALLBACK(HRESULT) vboxDispMpEnableEvents()
 {
+    EnterCriticalSection(&g_VBoxDispMp.CritSect);
     g_VBoxDispMp.pEscapeCmd = NULL;
     g_VBoxDispMp.cbEscapeCmd = 0;
@@ -69,4 +77,5 @@
     vboxUhgsmiGlobalSetCurrent();
 #endif
+    LeaveCriticalSection(&g_VBoxDispMp.CritSect);
     return S_OK;
 }
@@ -75,9 +84,14 @@
 DECLCALLBACK(HRESULT) vboxDispMpDisableEvents()
 {
+    EnterCriticalSection(&g_VBoxDispMp.CritSect);
     if (g_VBoxDispMp.pEscapeCmd)
+    {
         RTMemFree(g_VBoxDispMp.pEscapeCmd);
+        g_VBoxDispMp.pEscapeCmd = NULL;
+    }
 #ifdef VBOX_WITH_CRHGSMI
     vboxUhgsmiGlobalClearCurrent();
 #endif
+    LeaveCriticalSection(&g_VBoxDispMp.CritSect);
     return S_OK;
 }
@@ -90,4 +104,5 @@
 {
     HRESULT hr = S_OK;
+    EnterCriticalSection(&g_VBoxDispMp.CritSect);
     PVBOXVIDEOCM_CMD_HDR pHdr = vboxVideoCmIterNext(&g_VBoxDispMp.Iterator);
     if (!pHdr)
@@ -100,5 +115,8 @@
                 g_VBoxDispMp.cbEscapeCmd = VBOXDISPMP_BUF_INITSIZE;
             else
+            {
+                LeaveCriticalSection(&g_VBoxDispMp.CritSect);
                 return E_OUTOFMEMORY;
+            }
         }
 
@@ -150,9 +168,12 @@
         PVBOXVIDEOCM_CMD_RECTS_INTERNAL pCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)(((uint8_t*)pHdr) + sizeof (VBOXVIDEOCM_CMD_HDR));
         PVBOXWDDMDISP_SWAPCHAIN pSwapchain = (PVBOXWDDMDISP_SWAPCHAIN)pCmdInternal->hSwapchainUm;
-        /* todo: synchronization */
+        /* the miniport driver should ensure all swapchain-involved commands are completed before swapchain termination,
+         * so we should have it always valid here */
         Assert(pSwapchain);
+        Assert(pSwapchain->hWnd);
         pRegions->hWnd = pSwapchain->hWnd;
         pRegions->pRegions = &pCmdInternal->Cmd;
     }
+    LeaveCriticalSection(&g_VBoxDispMp.CritSect);
     return hr;
 }
@@ -175,2 +196,107 @@
     return S_OK;
 }
+
+HRESULT vboxDispMpInternalInit()
+{
+    memset(&g_VBoxDispMp, 0, sizeof (g_VBoxDispMp));
+    InitializeCriticalSection(&g_VBoxDispMp.CritSect);
+    return S_OK;
+}
+
+HRESULT vboxDispMpInternalTerm()
+{
+    vboxDispMpDisableEvents();
+    DeleteCriticalSection(&g_VBoxDispMp.CritSect);
+    return S_OK;
+}
+
+HRESULT vboxDispMpInternalCancel(VBOXWDDMDISP_CONTEXT *pContext, PVBOXWDDMDISP_SWAPCHAIN pSwapchain)
+{
+    HRESULT hr = S_OK;
+    EnterCriticalSection(&g_VBoxDispMp.CritSect);
+    do
+    {
+        /* the pEscapeCmd is used as an indicator to whether the events capturing is active */
+        if (!g_VBoxDispMp.pEscapeCmd)
+            break;
+
+        /* copy the iterator data to restore it back later */
+        VBOXVIDEOCM_ITERATOR IterCopy = g_VBoxDispMp.Iterator;
+
+        /* first check if we have matching elements */
+        PVBOXVIDEOCM_CMD_HDR pHdr;
+        bool fHasMatch = false;
+        while (pHdr = vboxVideoCmIterNext(&g_VBoxDispMp.Iterator))
+        {
+            VBOXWDDMDISP_CONTEXT *pCurContext = (VBOXWDDMDISP_CONTEXT*)pHdr->u64UmData;
+            if (pCurContext != pContext)
+                continue;
+
+            if (!pSwapchain)
+            {
+                fHasMatch = true;
+                break;
+            }
+
+            PVBOXVIDEOCM_CMD_RECTS_INTERNAL pCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)(((uint8_t*)pHdr) + sizeof (VBOXVIDEOCM_CMD_HDR));
+            PVBOXWDDMDISP_SWAPCHAIN pCurSwapchain = (PVBOXWDDMDISP_SWAPCHAIN)pCmdInternal->hSwapchainUm;
+            if (pCurSwapchain != pSwapchain)
+                continue;
+
+            fHasMatch = true;
+            break;
+        }
+
+        /* restore the iterator */
+        g_VBoxDispMp.Iterator = IterCopy;
+
+        if (!fHasMatch)
+            break;
+
+        /* there are elements to remove */
+        PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pEscapeCmd = (PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD)RTMemAlloc(g_VBoxDispMp.cbEscapeCmd);
+        if (!pEscapeCmd)
+        {
+            WARN(("no memory"));
+            hr = E_OUTOFMEMORY;
+            break;
+        }
+        /* copy the header data */
+        *pEscapeCmd = *g_VBoxDispMp.pEscapeCmd;
+        /* now copy the command data filtering out the canceled commands */
+        pHdr = (PVBOXVIDEOCM_CMD_HDR)(((uint8_t*)pEscapeCmd) + sizeof (VBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD));
+        vboxVideoCmIterInit(&g_VBoxDispMp.Iterator, pHdr, 0);
+        while (pHdr = vboxVideoCmIterNext(&IterCopy))
+        {
+            VBOXWDDMDISP_CONTEXT *pCurContext = (VBOXWDDMDISP_CONTEXT*)pHdr->u64UmData;
+            if (pCurContext != pContext)
+            {
+                vboxVideoCmIterCopyToBack(&g_VBoxDispMp.Iterator, pHdr);
+                continue;
+            }
+
+            if (!pSwapchain)
+            {
+                /* match, just continue */
+                continue;
+            }
+
+            PVBOXVIDEOCM_CMD_RECTS_INTERNAL pCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)(((uint8_t*)pHdr) + sizeof (VBOXVIDEOCM_CMD_HDR));
+            PVBOXWDDMDISP_SWAPCHAIN pCurSwapchain = (PVBOXWDDMDISP_SWAPCHAIN)pCmdInternal->hSwapchainUm;
+            if (pCurSwapchain != pSwapchain)
+            {
+                vboxVideoCmIterCopyToBack(&g_VBoxDispMp.Iterator, pHdr);
+                continue;
+            }
+            /* match, just continue */
+        }
+
+        Assert(g_VBoxDispMp.pEscapeCmd);
+        RTMemFree(g_VBoxDispMp.pEscapeCmd);
+        Assert(pEscapeCmd);
+        g_VBoxDispMp.pEscapeCmd = pEscapeCmd;
+    } while (0);
+
+    LeaveCriticalSection(&g_VBoxDispMp.CritSect);
+    return hr;
+}
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxScreen.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxScreen.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxScreen.cpp	(revision 38112)
@@ -282,5 +282,5 @@
     if (!GetClassInfo(hInstance, VBOXSCREENMONWND_NAME, &wc))
     {
-        wc.style = CS_OWNDC;
+        wc.style = 0;//CS_OWNDC;
         wc.lpfnWndProc = vboxScreenMonWndProc;
         wc.cbClsExtra = 0;
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPUtils.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPUtils.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPUtils.h	(revision 38112)
@@ -103,9 +103,16 @@
 #define VBE_DISPI_TOTAL_VIDEO_MEMORY_BYTES 4*_1M
 
+#define VBOXMP_WARN_VPS_NOBP(_vps)     \
+if ((_vps) != NO_ERROR)           \
+{                                 \
+    WARN_NOBP(("vps(%#x)!=NO_ERROR", _vps)); \
+}
+
 #define VBOXMP_WARN_VPS(_vps)     \
 if ((_vps) != NO_ERROR)           \
 {                                 \
-    WARN(("vps(%#x)!=NO_ERROR")); \
+    WARN(("vps(%#x)!=NO_ERROR", _vps)); \
 }
+
 
 #define VBOXMP_CHECK_VPS_BREAK(_vps)    \
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPVidModes.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPVidModes.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPVidModes.cpp	(revision 38112)
@@ -156,9 +156,9 @@
             /*First name without a suffix*/
             rc = VBoxMPCmnRegQueryDword(Registry, L"CustomXRes", &CustomXRes);
-            VBOXMP_WARN_VPS(rc);
+            VBOXMP_WARN_VPS_NOBP(rc);
             rc = VBoxMPCmnRegQueryDword(Registry, L"CustomYRes", &CustomYRes);
-            VBOXMP_WARN_VPS(rc);
+            VBOXMP_WARN_VPS_NOBP(rc);
             rc = VBoxMPCmnRegQueryDword(Registry, L"CustomBPP", &CustomBPP);
-            VBOXMP_WARN_VPS(rc);
+            VBOXMP_WARN_VPS_NOBP(rc);
         }
         else
@@ -167,11 +167,11 @@
             swprintf(keyname, L"CustomXRes%d", iMode);
             rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomXRes);
-            VBOXMP_WARN_VPS(rc);
+            VBOXMP_WARN_VPS_NOBP(rc);
             swprintf(keyname, L"CustomYRes%d", iMode);
             rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomYRes);
-            VBOXMP_WARN_VPS(rc);
+            VBOXMP_WARN_VPS_NOBP(rc);
             swprintf(keyname, L"CustomBPP%d", iMode);
             rc = VBoxMPCmnRegQueryDword(Registry, keyname, &CustomBPP);
-            VBOXMP_WARN_VPS(rc);
+            VBOXMP_WARN_VPS_NOBP(rc);
         }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.cpp	(revision 38112)
@@ -29,4 +29,22 @@
 } VBOXVIDEOCM_CMD_DR, *PVBOXVIDEOCM_CMD_DR;
 
+typedef enum
+{
+    VBOXVIDEOCM_CMD_CTL_KM_TYPE_POST_INVOKE = 1,
+    VBOXVIDEOCM_CMD_CTL_KM_TYPE_PRE_INVOKE,
+    VBOXVIDEOCM_CMD_CTL_KM_TYPE_DUMMY_32BIT = 0x7fffffff
+} VBOXVIDEOCM_CMD_CTL_KM_TYPE;
+
+typedef DECLCALLBACK(VOID) FNVBOXVIDEOCM_CMD_CB(PVBOXVIDEOCM_CTX pContext, struct VBOXVIDEOCM_CMD_CTL_KM *pCmd, PVOID pvContext);
+typedef FNVBOXVIDEOCM_CMD_CB *PFNVBOXVIDEOCM_CMD_CB;
+
+typedef struct VBOXVIDEOCM_CMD_CTL_KM
+{
+    VBOXVIDEOCM_CMD_CTL_KM_TYPE enmType;
+    uint32_t u32Reserved;
+    PFNVBOXVIDEOCM_CMD_CB pfnCb;
+    PVOID pvCb;
+} VBOXVIDEOCM_CMD_CTL_KM, *PVBOXVIDEOCM_CMD_CTL_KM;
+
 AssertCompile(VBOXWDDM_ROUNDBOUND(RT_OFFSETOF(VBOXVIDEOCM_CMD_DR, CmdHdr), 8) == RT_OFFSETOF(VBOXVIDEOCM_CMD_DR, CmdHdr));
 
@@ -49,4 +67,6 @@
     /* commands list  */
     LIST_ENTRY CommandsList;
+    /* post process commands list  */
+    LIST_ENTRY PpCommandsList;
     /* event used to notify UMD about pending commands */
     PKEVENT pUmEvent;
@@ -95,4 +115,41 @@
 }
 
+static PVBOXVIDEOCM_CMD_CTL_KM vboxVideoCmCmdCreateKm(PVBOXVIDEOCM_CTX pContext, VBOXVIDEOCM_CMD_CTL_KM_TYPE enmType,
+        PFNVBOXVIDEOCM_CMD_CB pfnCb, PVOID pvCb,
+        uint32_t cbSize)
+{
+    PVBOXVIDEOCM_CMD_CTL_KM pCmd = (PVBOXVIDEOCM_CMD_CTL_KM)vboxVideoCmCmdCreate(pContext, cbSize + sizeof (*pCmd));
+    pCmd->enmType = enmType;
+    pCmd->pfnCb = pfnCb;
+    pCmd->pvCb = pvCb;
+    PVBOXVIDEOCM_CMD_DR pHdr = VBOXVIDEOCM_HEAD(pCmd);
+    pHdr->CmdHdr.enmType = VBOXVIDEOCM_CMD_TYPE_CTL_KM;
+    return pCmd;
+}
+
+static DECLCALLBACK(VOID) vboxVideoCmCmdCbSetEventAndDereference(PVBOXVIDEOCM_CTX pContext, PVBOXVIDEOCM_CMD_CTL_KM pCmd, PVOID pvContext)
+{
+    PKEVENT pEvent = (PKEVENT)pvContext;
+    KeSetEvent(pEvent, 0, FALSE);
+    ObDereferenceObject(pEvent);
+    vboxVideoCmCmdRelease(pCmd);
+}
+
+NTSTATUS vboxVideoCmCmdSubmitCompleteEvent(PVBOXVIDEOCM_CTX pContext, PKEVENT pEvent)
+{
+    Assert(pEvent);
+    PVBOXVIDEOCM_CMD_CTL_KM pCmd = vboxVideoCmCmdCreateKm(pContext, VBOXVIDEOCM_CMD_CTL_KM_TYPE_POST_INVOKE,
+            vboxVideoCmCmdCbSetEventAndDereference, pEvent, 0);
+    if (!pCmd)
+    {
+        WARN(("vboxVideoCmCmdCreateKm failed"));
+        return STATUS_NO_MEMORY;
+    }
+
+    vboxVideoCmCmdSubmit(pCmd, VBOXVIDEOCM_SUBMITSIZE_DEFAULT);
+
+    return STATUS_SUCCESS;
+}
+
 DECLINLINE(void) vboxVideoCmCmdRetainByHdr(PVBOXVIDEOCM_CMD_DR pHdr)
 {
@@ -162,5 +219,5 @@
 }
 
-NTSTATUS vboxVideoCmCmdVisit(PVBOXVIDEOCM_CTX pContext, BOOL bEntireSession, PFNVBOXVIDEOCMCMDVISITOR pfnVisitor, PVOID pvVisitor)
+NTSTATUS vboxVideoCmCmdVisit(PVBOXVIDEOCM_CTX pContext, BOOLEAN bEntireSession, PFNVBOXVIDEOCMCMDVISITOR pfnVisitor, PVOID pvVisitor)
 {
     PVBOXVIDEOCM_SESSION pSession = pContext->pSession;
@@ -179,12 +236,19 @@
             if (bEntireSession || pHdr->pContext == pContext)
             {
-                void * pvBody = VBOXVIDEOCM_BODY(pHdr, void);
-                UINT fRet = pfnVisitor(pHdr->pContext, pvBody, pHdr->CmdHdr.cbCmd, pvVisitor);
-                if (fRet & VBOXVIDEOCMCMDVISITOR_RETURN_RMCMD)
+                if (pHdr->CmdHdr.enmType == VBOXVIDEOCM_CMD_TYPE_UM)
                 {
-                    RemoveEntryList(&pHdr->QueueList);
+                    void * pvBody = VBOXVIDEOCM_BODY(pHdr, void);
+                    UINT fRet = pfnVisitor(pHdr->pContext, pvBody, pHdr->CmdHdr.cbCmd, pvVisitor);
+                    if (fRet & VBOXVIDEOCMCMDVISITOR_RETURN_RMCMD)
+                    {
+                        RemoveEntryList(&pHdr->QueueList);
+                    }
+                    if ((fRet & VBOXVIDEOCMCMDVISITOR_RETURN_BREAK))
+                        break;
                 }
-                if (!(fRet & VBOXVIDEOCMCMDVISITOR_RETURN_CONTINUE))
-                    break;
+                else
+                {
+                    WARN(("non-um cmd on visit, skipping"));
+                }
             }
         }
@@ -231,8 +295,44 @@
     Assert(IsListEmpty(&pSession->ContextList));
     Assert(IsListEmpty(&pSession->CommandsList));
+    Assert(IsListEmpty(&pSession->PpCommandsList));
     RemoveEntryList(&pSession->QueueEntry);
     vboxWddmMemFree(pSession);
 }
 
+static void vboxVideoCmSessionCtxPpList(PVBOXVIDEOCM_CTX pContext, PLIST_ENTRY pHead)
+{
+    LIST_ENTRY *pCur;
+    for (pCur = pHead->Flink; pCur != pHead; pCur = pHead->Flink)
+    {
+        RemoveEntryList(pCur);
+        PVBOXVIDEOCM_CMD_DR pHdr = VBOXCMENTRY_2_CMD(pCur);
+        PVBOXVIDEOCM_CMD_CTL_KM pCmd = VBOXVIDEOCM_BODY(pHdr, VBOXVIDEOCM_CMD_CTL_KM);
+        pCmd->pfnCb(pContext, pCmd, pCmd->pvCb);
+    }
+}
+
+static void vboxVideoCmSessionCtxDetachCmdsLocked(PLIST_ENTRY pEntriesHead, PVBOXVIDEOCM_CTX pContext, PLIST_ENTRY pDstHead)
+{
+    LIST_ENTRY *pCur;
+    LIST_ENTRY *pPrev;
+    pCur = pEntriesHead->Flink;
+    pPrev = pEntriesHead;
+    while (pCur != pEntriesHead)
+    {
+        PVBOXVIDEOCM_CMD_DR pCmd = VBOXCMENTRY_2_CMD(pCur);
+        if (pCmd->pContext == pContext)
+        {
+            RemoveEntryList(pCur);
+            InsertTailList(pDstHead, pCur);
+            pCur = pPrev;
+            /* pPrev - remains unchanged */
+        }
+        else
+        {
+            pPrev = pCur;
+        }
+        pCur = pCur->Flink;
+    }
+}
 /**
  * @return true iff the given session is destroyed
@@ -242,7 +342,8 @@
     bool bDestroy;
     LIST_ENTRY RemainedList;
+    LIST_ENTRY RemainedPpList;
     LIST_ENTRY *pCur;
-    LIST_ENTRY *pPrev;
     InitializeListHead(&RemainedList);
+    InitializeListHead(&RemainedPpList);
     Assert(KeGetCurrentIrql() < DISPATCH_LEVEL);
     ExAcquireFastMutex(&pSession->Mutex);
@@ -254,25 +355,10 @@
     {
         vboxVideoLeDetach(&pSession->CommandsList, &RemainedList);
+        vboxVideoLeDetach(&pSession->PpCommandsList, &RemainedPpList);
     }
     else
     {
-        pCur = pSession->CommandsList.Flink;
-        pPrev = &pSession->CommandsList;
-        while (pCur != &pSession->CommandsList)
-        {
-            PVBOXVIDEOCM_CMD_DR pCmd = VBOXCMENTRY_2_CMD(pCur);
-            if (pCmd->pContext == pContext)
-            {
-                RemoveEntryList(pCur);
-                InsertHeadList(&RemainedList, pCur);
-                pCur = pPrev;
-                /* pPrev - remains unchanged */
-            }
-            else
-            {
-                pPrev = pCur;
-            }
-            pCur = pCur->Flink;
-        }
+        vboxVideoCmSessionCtxDetachCmdsLocked(&pSession->CommandsList, pContext, &RemainedList);
+        vboxVideoCmSessionCtxDetachCmdsLocked(&pSession->PpCommandsList, pContext, &RemainedPpList);
     }
     ExReleaseFastMutex(&pSession->Mutex);
@@ -284,4 +370,6 @@
         vboxVideoCmCmdCancel(pCmd);
     }
+
+    vboxVideoCmSessionCtxPpList(pContext, &RemainedPpList);
 
     if (bDestroy)
@@ -303,4 +391,5 @@
         InitializeListHead(&pSession->ContextList);
         InitializeListHead(&pSession->CommandsList);
+        InitializeListHead(&pSession->PpCommandsList);
         pSession->pUmEvent = pUmEvent;
         Assert(KeGetCurrentIrql() < DISPATCH_LEVEL);
@@ -411,4 +500,33 @@
 }
 
+VOID vboxVideoCmProcessKm(PVBOXVIDEOCM_CTX pContext, PVBOXVIDEOCM_CMD_CTL_KM pCmd)
+{
+    PVBOXVIDEOCM_SESSION pSession = pContext->pSession;
+
+    switch (pCmd->enmType)
+    {
+        case VBOXVIDEOCM_CMD_CTL_KM_TYPE_PRE_INVOKE:
+        {
+            pCmd->pfnCb(pContext, pCmd, pCmd->pvCb);
+            break;
+        }
+
+        case VBOXVIDEOCM_CMD_CTL_KM_TYPE_POST_INVOKE:
+        {
+            PVBOXVIDEOCM_CMD_DR pHdr = VBOXVIDEOCM_HEAD(pCmd);
+            ExAcquireFastMutex(&pSession->Mutex);
+            InsertTailList(&pSession->PpCommandsList, &pHdr->QueueList);
+            ExReleaseFastMutex(&pSession->Mutex);
+            break;
+        }
+
+        default:
+        {
+            WARN(("unsupported cmd type %d", pCmd->enmType));
+            break;
+        }
+    }
+}
+
 NTSTATUS vboxVideoCmEscape(PVBOXVIDEOCM_CTX pContext, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd)
 {
@@ -420,4 +538,5 @@
     PVBOXVIDEOCM_CMD_DR pHdr;
     LIST_ENTRY DetachedList;
+    LIST_ENTRY DetachedPpList;
     PLIST_ENTRY pCurEntry = NULL;
     uint32_t cbCmdsReturned = 0;
@@ -428,8 +547,11 @@
     bool bDetachMode = true;
     InitializeListHead(&DetachedList);
+    InitializeListHead(&DetachedPpList);
 //    PVBOXWDDM_GETVBOXVIDEOCMCMD_HDR *pvCmd
 
     Assert(KeGetCurrentIrql() < DISPATCH_LEVEL);
     ExAcquireFastMutex(&pSession->Mutex);
+
+    vboxVideoCmSessionCtxDetachCmdsLocked(&pSession->PpCommandsList, pContext, &DetachedPpList);
 
     do
@@ -442,14 +564,16 @@
                 pHdr = VBOXCMENTRY_2_CMD(pSession->CommandsList.Blink);
                 Assert(pHdr->CmdHdr.cbCmd);
-                if (cbData >= pHdr->CmdHdr.cbCmd)
+                uint32_t cbUserCmd = pHdr->CmdHdr.enmType == VBOXVIDEOCM_CMD_TYPE_UM ? pHdr->CmdHdr.cbCmd : 0;
+                if (cbData >= cbUserCmd)
                 {
                     RemoveEntryList(&pHdr->QueueList);
                     InsertHeadList(&DetachedList, &pHdr->QueueList);
-                    cbData -= pHdr->CmdHdr.cbCmd;
+                    cbData -= cbUserCmd;
                 }
                 else
                 {
-                    cbRemainingFirstCmd = pHdr->CmdHdr.cbCmd;
-                    cbRemainingCmds = pHdr->CmdHdr.cbCmd;
+                    Assert(cbUserCmd);
+                    cbRemainingFirstCmd = cbUserCmd;
+                    cbRemainingCmds = cbUserCmd;
                     pCurEntry = pHdr->QueueList.Blink;
                     bDetachMode = false;
@@ -468,6 +592,7 @@
             {
                 pHdr = VBOXCMENTRY_2_CMD(pCurEntry);
+                uint32_t cbUserCmd = pHdr->CmdHdr.enmType == VBOXVIDEOCM_CMD_TYPE_UM ? pHdr->CmdHdr.cbCmd : 0;
                 Assert(cbRemainingFirstCmd);
-                cbRemainingCmds += pHdr->CmdHdr.cbCmd;
+                cbRemainingCmds += cbUserCmd;
                 pCurEntry = pHdr->QueueList.Blink;
             }
@@ -483,13 +608,34 @@
     ExReleaseFastMutex(&pSession->Mutex);
 
+    vboxVideoCmSessionCtxPpList(pContext, &DetachedPpList);
+
     pCmd->Hdr.cbCmdsReturned = 0;
     for (pCurEntry = DetachedList.Blink; pCurEntry != &DetachedList; pCurEntry = DetachedList.Blink)
     {
         pHdr = VBOXCMENTRY_2_CMD(pCurEntry);
-        memcpy(pvData, &pHdr->CmdHdr, pHdr->CmdHdr.cbCmd);
-        pvData += pHdr->CmdHdr.cbCmd;
-        pCmd->Hdr.cbCmdsReturned += pHdr->CmdHdr.cbCmd;
         RemoveEntryList(pCurEntry);
-        vboxVideoCmCmdReleaseByHdr(pHdr);
+        switch (pHdr->CmdHdr.enmType)
+        {
+            case VBOXVIDEOCM_CMD_TYPE_UM:
+            {
+                memcpy(pvData, &pHdr->CmdHdr, pHdr->CmdHdr.cbCmd);
+                pvData += pHdr->CmdHdr.cbCmd;
+                pCmd->Hdr.cbCmdsReturned += pHdr->CmdHdr.cbCmd;
+                vboxVideoCmCmdReleaseByHdr(pHdr);
+                break;
+            }
+
+            case VBOXVIDEOCM_CMD_TYPE_CTL_KM:
+            {
+                vboxVideoCmProcessKm(pContext, VBOXVIDEOCM_BODY(pHdr, VBOXVIDEOCM_CMD_CTL_KM));
+                break;
+            }
+
+            default:
+            {
+                WARN(("unsupported cmd type %d", pHdr->CmdHdr.enmType));
+                break;
+            }
+        }
     }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPCm.h	(revision 38112)
@@ -42,4 +42,5 @@
 NTSTATUS vboxVideoCmTerm(PVBOXVIDEOCM_MGR pMgr);
 
+NTSTATUS vboxVideoCmCmdSubmitCompleteEvent(PVBOXVIDEOCM_CTX pContext, PKEVENT pEvent);
 void* vboxVideoCmCmdCreate(PVBOXVIDEOCM_CTX pContext, uint32_t cbSize);
 void* vboxVideoCmCmdReinitForContext(void *pvCmd, PVBOXVIDEOCM_CTX pContext);
@@ -49,9 +50,9 @@
 void vboxVideoCmCmdSubmit(void *pvCmd, uint32_t cbSize);
 
-#define VBOXVIDEOCMCMDVISITOR_RETURN_CONTINUE 0x00000001
+#define VBOXVIDEOCMCMDVISITOR_RETURN_BREAK    0x00000001
 #define VBOXVIDEOCMCMDVISITOR_RETURN_RMCMD    0x00000002
 typedef DECLCALLBACK(UINT) FNVBOXVIDEOCMCMDVISITOR(PVBOXVIDEOCM_CTX pContext, PVOID pvCmd, uint32_t cbCmd, PVOID pvVisitor);
 typedef FNVBOXVIDEOCMCMDVISITOR *PFNVBOXVIDEOCMCMDVISITOR;
-NTSTATUS vboxVideoCmCmdVisit(PVBOXVIDEOCM_CTX pContext, BOOL bEntireSession, PFNVBOXVIDEOCMCMDVISITOR pfnVisitor, PVOID pvVisitor);
+NTSTATUS vboxVideoCmCmdVisit(PVBOXVIDEOCM_CTX pContext, BOOLEAN bEntireSession, PFNVBOXVIDEOCMCMDVISITOR pfnVisitor, PVOID pvVisitor);
 
 NTSTATUS vboxVideoCmEscape(PVBOXVIDEOCM_CTX pContext, PVBOXDISPIFESCAPE_GETVBOXVIDEOCMCMD pCmd, uint32_t cbCmd);
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPMisc.cpp	(revision 38112)
@@ -216,14 +216,8 @@
 DECLINLINE(VOID) vboxWddmSwapchainRelease(PVBOXWDDM_SWAPCHAIN pSwapchain)
 {
-    uint32_t cRefs = ASMAtomicDecU32(&pSwapchain->cRefs);
+    const uint32_t cRefs = ASMAtomicDecU32(&pSwapchain->cRefs);
     Assert(cRefs < UINT32_MAX/2);
     if (!cRefs)
     {
-        Assert(pSwapchain->pContext);
-        if (pSwapchain->pContext)
-        {
-            NTSTATUS tmpStatus = vboxVdmaPostHideSwapchain(pSwapchain);
-            Assert(tmpStatus == STATUS_SUCCESS);
-        }
         vboxWddmMemFree(pSwapchain);
     }
@@ -312,4 +306,15 @@
 {
     vboxWddmSwapchainAllocRemoveAllInternal(pDevExt, pSwapchain, TRUE);
+
+    Assert(pSwapchain->pContext);
+    if (pSwapchain->pContext)
+    {
+        NTSTATUS tmpStatus = vboxVdmaGgCmdCancel(pDevExt, pSwapchain->pContext, pSwapchain);
+        if (tmpStatus != STATUS_SUCCESS)
+        {
+            WARN(("vboxVdmaGgCmdCancel returned Status (0x%x)", tmpStatus));
+        }
+    }
+
     vboxWddmSwapchainRelease(pSwapchain);
 }
@@ -397,94 +402,122 @@
         return STATUS_INVALID_PARAMETER;
 
-    PVBOXWDDM_SWAPCHAIN pSwapchain;
+    PVBOXWDDM_SWAPCHAIN pSwapchain = NULL;
     PVBOXWDDM_ALLOCATION *apAlloc = NULL;
-    if (pSwapchainInfo->SwapchainInfo.cAllocs)
-    {
-        apAlloc = (PVBOXWDDM_ALLOCATION *)vboxWddmMemAlloc(sizeof (PVBOXWDDM_ALLOCATION) * pSwapchainInfo->SwapchainInfo.cAllocs);
-        Assert(apAlloc);
-        if (!apAlloc)
-            return STATUS_NO_MEMORY;
-        for (UINT i = 0; i < pSwapchainInfo->SwapchainInfo.cAllocs; ++i)
-        {
-            DXGKARGCB_GETHANDLEDATA GhData;
-            GhData.hObject = pSwapchainInfo->SwapchainInfo.ahAllocs[i];
-            GhData.Type = DXGK_HANDLE_ALLOCATION;
-            GhData.Flags.Value = 0;
-            PVBOXWDDM_ALLOCATION pAlloc = (PVBOXWDDM_ALLOCATION)pDevExt->u.primary.DxgkInterface.DxgkCbGetHandleData(&GhData);
-            Assert(pAlloc);
-            if (!pAlloc)
-                return STATUS_INVALID_PARAMETER;
-            apAlloc[i] = pAlloc;
-        }
-    }
-
-    if (pSwapchainInfo->SwapchainInfo.hSwapchainKm)
-    {
-        ExAcquireFastMutex(&pDevExt->ContextMutex);
-        pSwapchain = (PVBOXWDDM_SWAPCHAIN)vboxWddmHTableGet(&pContext->Swapchains, (VBOXWDDM_HANDLE)pSwapchainInfo->SwapchainInfo.hSwapchainKm);
-        Assert(pSwapchain);
-        if (!pSwapchain)
-        {
-            ExReleaseFastMutex(&pDevExt->ContextMutex);
-            return STATUS_INVALID_PARAMETER;
-        }
-        Assert(pSwapchain->hSwapchainKm == pSwapchainInfo->SwapchainInfo.hSwapchainKm);
-        Assert(pSwapchain->pContext == pContext);
-        if (pSwapchain->pContext != pContext)
-        {
-            ExReleaseFastMutex(&pDevExt->ContextMutex);
-            return STATUS_INVALID_PARAMETER;
-        }
-    }
-    else if (pSwapchainInfo->SwapchainInfo.cAllocs)
-    {
-        pSwapchain = vboxWddmSwapchainCreate();
-        if (!pSwapchain)
-            return STATUS_NO_MEMORY;
-
-        ExAcquireFastMutex(&pDevExt->ContextMutex);
-        BOOLEAN bRc = vboxWddmSwapchainCtxAddLocked(pDevExt, pContext, pSwapchain);
-        Assert(bRc);
-    }
-    else
-        return STATUS_INVALID_PARAMETER;
-
-    memset(&pSwapchain->ViewRect, 0, sizeof (pSwapchain->ViewRect));
-    if (pSwapchain->pLastReportedRects)
-    {
-        vboxVideoCmCmdRelease(pSwapchain->pLastReportedRects);
-        pSwapchain->pLastReportedRects = NULL;
-    }
-
-    vboxWddmSwapchainAllocRemoveAll(pDevExt, pSwapchain);
-
-    if (pSwapchainInfo->SwapchainInfo.cAllocs)
-    {
-        for (UINT i = 0; i < pSwapchainInfo->SwapchainInfo.cAllocs; ++i)
-        {
-            vboxWddmSwapchainAllocAdd(pDevExt, pSwapchain, apAlloc[i]);
-        }
-        pSwapchain->hSwapchainUm = pSwapchainInfo->SwapchainInfo.hSwapchainUm;
-    }
-    else
-    {
-        vboxWddmSwapchainCtxRemoveLocked(pDevExt, pContext, pSwapchain);
-    }
-
-    ExReleaseFastMutex(&pDevExt->ContextMutex);
-
-    if (pSwapchainInfo->SwapchainInfo.cAllocs)
-    {
-        Assert(pSwapchain->pContext);
-        Assert(pSwapchain->hSwapchainKm);
-        pSwapchainInfo->SwapchainInfo.hSwapchainKm = pSwapchain->hSwapchainKm;
-    }
-    else
-    {
-        vboxWddmSwapchainDestroy(pDevExt, pSwapchain);
-        pSwapchainInfo->SwapchainInfo.hSwapchainKm = 0;
-    }
-
-    return STATUS_SUCCESS;
+    Assert(KeGetCurrentIrql() == PASSIVE_LEVEL);
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    do {
+        if (pSwapchainInfo->SwapchainInfo.cAllocs)
+        {
+            apAlloc = (PVBOXWDDM_ALLOCATION *)vboxWddmMemAlloc(sizeof (PVBOXWDDM_ALLOCATION) * pSwapchainInfo->SwapchainInfo.cAllocs);
+            Assert(apAlloc);
+            if (!apAlloc)
+            {
+                Status = STATUS_NO_MEMORY;
+                break;
+            }
+            for (UINT i = 0; i < pSwapchainInfo->SwapchainInfo.cAllocs; ++i)
+            {
+                DXGKARGCB_GETHANDLEDATA GhData;
+                GhData.hObject = pSwapchainInfo->SwapchainInfo.ahAllocs[i];
+                GhData.Type = DXGK_HANDLE_ALLOCATION;
+                GhData.Flags.Value = 0;
+                PVBOXWDDM_ALLOCATION pAlloc = (PVBOXWDDM_ALLOCATION)pDevExt->u.primary.DxgkInterface.DxgkCbGetHandleData(&GhData);
+                Assert(pAlloc);
+                if (!pAlloc)
+                {
+                    Status = STATUS_INVALID_PARAMETER;
+                    break;
+                }
+                apAlloc[i] = pAlloc;
+            }
+
+            if (!NT_SUCCESS(Status))
+                break;
+        }
+
+        if (pSwapchainInfo->SwapchainInfo.hSwapchainKm)
+        {
+            ExAcquireFastMutex(&pDevExt->ContextMutex);
+            pSwapchain = (PVBOXWDDM_SWAPCHAIN)vboxWddmHTableGet(&pContext->Swapchains, (VBOXWDDM_HANDLE)pSwapchainInfo->SwapchainInfo.hSwapchainKm);
+            Assert(pSwapchain);
+            if (!pSwapchain)
+            {
+                ExReleaseFastMutex(&pDevExt->ContextMutex);
+                Status = STATUS_INVALID_PARAMETER;
+                break;
+            }
+            Assert(pSwapchain->hSwapchainKm == pSwapchainInfo->SwapchainInfo.hSwapchainKm);
+            Assert(pSwapchain->pContext == pContext);
+            if (pSwapchain->pContext != pContext)
+            {
+                ExReleaseFastMutex(&pDevExt->ContextMutex);
+                Status = STATUS_INVALID_PARAMETER;
+                break;
+            }
+        }
+        else if (pSwapchainInfo->SwapchainInfo.cAllocs)
+        {
+            pSwapchain = vboxWddmSwapchainCreate();
+            if (!pSwapchain)
+            {
+                Status = STATUS_NO_MEMORY;
+                break;
+            }
+
+            ExAcquireFastMutex(&pDevExt->ContextMutex);
+            BOOLEAN bRc = vboxWddmSwapchainCtxAddLocked(pDevExt, pContext, pSwapchain);
+            Assert(bRc);
+        }
+        else
+        {
+            Status = STATUS_INVALID_PARAMETER;
+            break;
+        }
+
+        memset(&pSwapchain->ViewRect, 0, sizeof (pSwapchain->ViewRect));
+        if (pSwapchain->pLastReportedRects)
+        {
+            vboxVideoCmCmdRelease(pSwapchain->pLastReportedRects);
+            pSwapchain->pLastReportedRects = NULL;
+        }
+
+        vboxWddmSwapchainAllocRemoveAll(pDevExt, pSwapchain);
+
+        if (pSwapchainInfo->SwapchainInfo.cAllocs)
+        {
+            for (UINT i = 0; i < pSwapchainInfo->SwapchainInfo.cAllocs; ++i)
+            {
+                vboxWddmSwapchainAllocAdd(pDevExt, pSwapchain, apAlloc[i]);
+            }
+            pSwapchain->hSwapchainUm = pSwapchainInfo->SwapchainInfo.hSwapchainUm;
+        }
+        else
+        {
+            vboxWddmSwapchainCtxRemoveLocked(pDevExt, pContext, pSwapchain);
+        }
+
+        ExReleaseFastMutex(&pDevExt->ContextMutex);
+
+        if (pSwapchainInfo->SwapchainInfo.cAllocs)
+        {
+            Assert(pSwapchain->pContext);
+            Assert(pSwapchain->hSwapchainKm);
+            pSwapchainInfo->SwapchainInfo.hSwapchainKm = pSwapchain->hSwapchainKm;
+        }
+        else
+        {
+            vboxWddmSwapchainDestroy(pDevExt, pSwapchain);
+            pSwapchainInfo->SwapchainInfo.hSwapchainKm = 0;
+        }
+
+        Assert(Status == STATUS_SUCCESS);
+    } while (0);
+
+    /* cleanup */
+    if (apAlloc)
+        vboxWddmMemFree(apAlloc);
+
+    return Status;
 }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.cpp
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.cpp	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.cpp	(revision 38112)
@@ -927,4 +927,5 @@
             break;
         }
+
         default:
             Assert(0);
@@ -1064,4 +1065,15 @@
 
     return Status;
+}
+
+static DECLCALLBACK(UINT) vboxVdmaGgCmdCancelVisitor(PVBOXVIDEOCM_CTX pContext, PVOID pvCmd, uint32_t cbCmd, PVOID pvVisitor)
+{
+    PVBOXWDDM_SWAPCHAIN pSwapchain = (PVBOXWDDM_SWAPCHAIN)pvVisitor;
+    if (!pSwapchain)
+        return VBOXVIDEOCMCMDVISITOR_RETURN_RMCMD;
+    PVBOXVIDEOCM_CMD_RECTS_INTERNAL pCmdInternal = (PVBOXVIDEOCM_CMD_RECTS_INTERNAL)pvCmd;
+    if (pCmdInternal->hSwapchainUm == pSwapchain->hSwapchainUm)
+        return VBOXVIDEOCMCMDVISITOR_RETURN_RMCMD;
+    return 0;
 }
 
@@ -1103,4 +1115,31 @@
                             break;
                         }
+                        case VBOXVDMAPIPE_CMD_TYPE_FINISH:
+                        {
+                            PVBOXVDMAPIPE_CMD_FINISH pCmd = (PVBOXVDMAPIPE_CMD_FINISH)pDr;
+                            PVBOXWDDM_CONTEXT pContext = pCmd->pContext;
+                            Assert(pCmd->pEvent);
+                            Status = vboxVideoCmCmdSubmitCompleteEvent(&pContext->CmContext, pCmd->pEvent);
+                            if (Status != STATUS_SUCCESS)
+                            {
+                                WARN(("vboxVideoCmCmdWaitCompleted failedm Status (0x%x)", Status));
+                            }
+                            vboxVdmaGgCmdDestroy(pDevExt, &pCmd->Hdr);
+                            break;
+                        }
+                        case VBOXVDMAPIPE_CMD_TYPE_CANCEL:
+                        {
+                            PVBOXVDMAPIPE_CMD_CANCEL pCmd = (PVBOXVDMAPIPE_CMD_CANCEL)pDr;
+                            PVBOXWDDM_CONTEXT pContext = pCmd->pContext;
+                            Status = vboxVideoCmCmdVisit(&pContext->CmContext, FALSE, vboxVdmaGgCmdCancelVisitor, pCmd->pSwapchain);
+                            if (Status != STATUS_SUCCESS)
+                            {
+                                WARN(("vboxVideoCmCmdWaitCompleted failedm Status (0x%x)", Status));
+                            }
+                            Assert(pCmd->pEvent);
+                            KeSetEvent(pCmd->pEvent, 0, FALSE);
+                            vboxVdmaGgCmdDestroy(pDevExt, &pCmd->Hdr);
+                            break;
+                        }
                         default:
                             AssertBreakpoint();
@@ -1217,4 +1256,58 @@
 #endif
     vboxVdmaDdiCmdInit(pDdiCmd, u32NodeOrdinal, u32FenceId, pfnComplete, pvComplete);
+}
+
+NTSTATUS vboxVdmaGgCmdFinish(PVBOXMP_DEVEXT pDevExt, VBOXWDDM_CONTEXT *pContext, PKEVENT pEvent)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    PVBOXVDMAPIPE_CMD_FINISH pCmd = (PVBOXVDMAPIPE_CMD_FINISH)vboxVdmaGgCmdCreate(pDevExt, VBOXVDMAPIPE_CMD_TYPE_FINISH, sizeof (*pCmd));
+    if (pCmd)
+    {
+        pCmd->pContext = pContext;
+        pCmd->pEvent = pEvent;
+        Status = vboxVdmaGgCmdSubmit(pDevExt, &pCmd->Hdr);
+        if (!NT_SUCCESS(Status))
+        {
+            WARN(("vboxVdmaGgCmdSubmit returned 0x%x", Status));
+        }
+    }
+    else
+    {
+        WARN(("vboxVdmaGgCmdCreate failed"));
+        Status = STATUS_NO_MEMORY;
+    }
+    return Status;
+}
+
+NTSTATUS vboxVdmaGgCmdCancel(PVBOXMP_DEVEXT pDevExt, VBOXWDDM_CONTEXT *pContext, PVBOXWDDM_SWAPCHAIN pSwapchain)
+{
+    NTSTATUS Status = STATUS_SUCCESS;
+
+    PVBOXVDMAPIPE_CMD_CANCEL pCmd = (PVBOXVDMAPIPE_CMD_CANCEL)vboxVdmaGgCmdCreate(pDevExt, VBOXVDMAPIPE_CMD_TYPE_CANCEL, sizeof (*pCmd));
+    if (pCmd)
+    {
+        KEVENT Event;
+        KeInitializeEvent(&Event, NotificationEvent, FALSE);
+        pCmd->pContext = pContext;
+        pCmd->pSwapchain = pSwapchain;
+        pCmd->pEvent = &Event;
+        Status = vboxVdmaGgCmdSubmit(pDevExt, &pCmd->Hdr);
+        if (NT_SUCCESS(Status))
+        {
+            Status = KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
+            Assert(Status == STATUS_SUCCESS);
+        }
+        else
+        {
+            WARN(("vboxVdmaGgCmdSubmit returned 0x%x", Status));
+        }
+    }
+    else
+    {
+        WARN(("vboxVdmaGgCmdCreate failed"));
+        Status = STATUS_NO_MEMORY;
+    }
+    return Status;
 }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVdma.h	(revision 38112)
@@ -161,5 +161,7 @@
     VBOXVDMAPIPE_CMD_TYPE_UNDEFINED = 0,
     VBOXVDMAPIPE_CMD_TYPE_RECTSINFO,
-    VBOXVDMAPIPE_CMD_TYPE_DMACMD
+    VBOXVDMAPIPE_CMD_TYPE_DMACMD,
+    VBOXVDMAPIPE_CMD_TYPE_FINISH, /* ensures all previously submitted commands are completed */
+    VBOXVDMAPIPE_CMD_TYPE_CANCEL
 } VBOXVDMAPIPE_CMD_TYPE;
 
@@ -193,4 +195,19 @@
     VBOXVDMAPIPE_RECTS ContextsRects;
 } VBOXVDMAPIPE_CMD_RECTSINFO, *PVBOXVDMAPIPE_CMD_RECTSINFO;
+
+typedef struct VBOXVDMAPIPE_CMD_FINISH
+{
+    VBOXVDMAPIPE_CMD_DR Hdr;
+    PVBOXWDDM_CONTEXT pContext;
+    PKEVENT pEvent;
+} VBOXVDMAPIPE_CMD_FINISH, *PVBOXVDMAPIPE_CMD_FINISH;
+
+typedef struct VBOXVDMAPIPE_CMD_CANCEL
+{
+    VBOXVDMAPIPE_CMD_DR Hdr;
+    PVBOXWDDM_CONTEXT pContext;
+    PVBOXWDDM_SWAPCHAIN pSwapchain;
+    PKEVENT pEvent;
+} VBOXVDMAPIPE_CMD_CANCEL, *PVBOXVDMAPIPE_CMD_CANCEL;
 
 typedef struct VBOXVDMAPIPE_FLAGS_DMACMD
@@ -313,4 +330,6 @@
 PVBOXVDMAPIPE_CMD_DR vboxVdmaGgCmdCreate(PVBOXMP_DEVEXT pDevExt, VBOXVDMAPIPE_CMD_TYPE enmType, uint32_t cbCmd);
 void vboxVdmaGgCmdDestroy(PVBOXMP_DEVEXT pDevExt, PVBOXVDMAPIPE_CMD_DR pDr);
+NTSTATUS vboxVdmaGgCmdFinish(PVBOXMP_DEVEXT pDevExt, struct VBOXWDDM_CONTEXT *pContext, PKEVENT pEvent);
+NTSTATUS vboxVdmaGgCmdCancel(PVBOXMP_DEVEXT pDevExt, VBOXWDDM_CONTEXT *pContext, PVBOXWDDM_SWAPCHAIN pSwapchain);
 
 NTSTATUS vboxVdmaPostHideSwapchain(PVBOXWDDM_SWAPCHAIN pSwapchain);
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/Makefile.kmk
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/Makefile.kmk	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/Makefile.kmk	(revision 38112)
@@ -188,5 +188,5 @@
 wined3dwddm_DEFS           = $(subst __i386__,,$(wined3d_DEFS)) VBOX_WITH_WDDM VBOX_WINE_WITHOUT_LIBWINE
 wined3dwddm_DEFS.x86       = $(NO_SUCH_VARIABLE)
-wined3dwddm_SOURCES        = $(subst wined3d.def,wined3dwddm.def,$(wined3d_SOURCES)) vbox/libWineStub/debug.c
+wined3dwddm_SOURCES        = $(subst wined3d.def,wined3dwddm.def,$(wined3d_SOURCES)) vbox/libWineStub/debug.c wined3d/vboxext.c
 wined3dwddm_SOURCES.x86    = $(NO_SUCH_VARIABLE)
 wined3dwddm_SOURCES.amd64  = $(NO_SUCH_VARIABLE)
@@ -199,4 +199,7 @@
 							$(VBOX_LIB_VBGL_R3)
 wined3dwddm_DEFS          += VBOX_WINE_WITH_IPRT
+endif
+ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+wined3dwddm_DEFS          += VBOX_WINE_WITH_SINGLE_CONTEXT
 endif
 
@@ -316,4 +319,10 @@
 VBoxD3D9wddm_SDKS           = WINDDKWLH
 VBoxD3D9wddm_SDKS.amd64     = $(NO_SUCH_VARIABLE)
+ifdef VBOX_WINE_WITH_IPRT
+VBoxD3D9wddm_LIBS          += \
+							$(VBOX_LIB_IPRT_GUEST_R3) \
+							$(VBOX_LIB_VBGL_R3)
+VBoxD3D9wddm_DEFS          += VBOX_WINE_WITH_IPRT
+endif
 
 DLLS.amd64 += VBoxD3D9wddm-x86
@@ -322,4 +331,9 @@
 VBoxD3D9wddm-x86_LIBS         = $(PATH_STAGE_LIB)/wined3dwddm-x86$(VBOX_SUFF_LIB)
 VBoxD3D9wddm-x86_DEFS         = $(VBoxD3D9wddm_DEFS) VBOX_WDDM_WOW64
+ifdef VBOX_WINE_WITH_IPRT
+VBoxD3D9wddm-x86_LIBS        += \
+						$(VBOX_LIB_IPRT_GUEST_R3_X86) \
+						$(VBOX_LIB_VBGL_R3_X86)
+endif
 endif
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/debug.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/debug.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/include/wine/debug.h	(revision 38112)
@@ -37,4 +37,14 @@
 #endif
 
+#ifdef VBOX_WITH_WDDM
+# ifdef VBOX_WINE_WITH_IPRT
+#  include <iprt/assert.h>
+# else
+#  define AssertBreakpoint() do { } while (0)
+#  define Assert(_expr) do { } while (0)
+#  define RT_BREAKPOINT()
+# endif
+#endif
+
 #ifdef __WINE_WINE_TEST_H
 #error This file should not be used in Wine tests
@@ -75,12 +85,24 @@
 #ifndef WINE_NO_DEBUG_MSGS
 # define __WINE_GET_DEBUGGING_WARN(dbch)  ((dbch)->flags & (1 << __WINE_DBCL_WARN))
-# define __WINE_GET_DEBUGGING_FIXME(dbch) ((dbch)->flags & (1 << __WINE_DBCL_FIXME))
+# if defined(VBOX_WITH_WDDM) && defined(DEBUG_misha)
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) (RT_BREAKPOINT(), ((dbch)->flags & (1 << __WINE_DBCL_FIXME)))
+# else
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) ((dbch)->flags & (1 << __WINE_DBCL_FIXME))
+# endif
 #else
 # define __WINE_GET_DEBUGGING_WARN(dbch)  0
-# define __WINE_GET_DEBUGGING_FIXME(dbch) 0
+# if defined(VBOX_WITH_WDDM) && defined(DEBUG_misha)
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) (RT_BREAKPOINT(), 0)
+# else
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) 0
+# endif
 #endif
 
 /* define error macro regardless of what is configured */
+#if defined(VBOX_WITH_WDDM) && defined(DEBUG_misha)
+#define __WINE_GET_DEBUGGING_ERR(dbch)  (RT_BREAKPOINT(), ((dbch)->flags & (1 << __WINE_DBCL_ERR)))
+#else
 #define __WINE_GET_DEBUGGING_ERR(dbch)  ((dbch)->flags & (1 << __WINE_DBCL_ERR))
+#endif
 
 #define __WINE_GET_DEBUGGING(dbcl,dbch)  __WINE_GET_DEBUGGING##dbcl(dbch)
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/debug.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/debug.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/vbox/libWineStub/include/wine/debug.h	(revision 38112)
@@ -41,4 +41,14 @@
 #endif
 
+#ifdef VBOX_WITH_WDDM
+# ifdef VBOX_WINE_WITH_IPRT
+#  include <iprt/assert.h>
+# else
+#  define AssertBreakpoint() do { } while (0)
+#  define Assert(_expr) do { } while (0)
+#  define RT_BREAKPOINT()
+# endif
+#endif
+
 #ifdef __cplusplus
 extern "C" {
@@ -80,12 +90,24 @@
 #ifndef WINE_NO_DEBUG_MSGS
 # define __WINE_GET_DEBUGGING_WARN(dbch)  ((dbch)->flags & (1 << __WINE_DBCL_WARN))
-# define __WINE_GET_DEBUGGING_FIXME(dbch) ((dbch)->flags & (1 << __WINE_DBCL_FIXME))
+# if defined(VBOX_WITH_WDDM) && defined(DEBUG_misha)
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) (RT_BREAKPOINT(), ((dbch)->flags & (1 << __WINE_DBCL_FIXME)))
+# else
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) ((dbch)->flags & (1 << __WINE_DBCL_FIXME))
+# endif
 #else
 # define __WINE_GET_DEBUGGING_WARN(dbch)  0
-# define __WINE_GET_DEBUGGING_FIXME(dbch) 0
+# if defined(VBOX_WITH_WDDM) && defined(DEBUG_misha)
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) (RT_BREAKPOINT(), 0)
+# else
+#  define __WINE_GET_DEBUGGING_FIXME(dbch) 0
+# endif
 #endif
 
 /* define error macro regardless of what is configured */
+#if defined(VBOX_WITH_WDDM) && defined(DEBUG_misha)
+#define __WINE_GET_DEBUGGING_ERR(dbch)  (RT_BREAKPOINT(), ((dbch)->flags & (1 << __WINE_DBCL_ERR)))
+#else
 #define __WINE_GET_DEBUGGING_ERR(dbch)  ((dbch)->flags & (1 << __WINE_DBCL_ERR))
+#endif
 
 #define __WINE_GET_DEBUGGING(dbcl,dbch)  __WINE_GET_DEBUGGING##dbcl(dbch)
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 38112)
@@ -767,6 +767,4 @@
         {
             ERR("Lost swapchain dc %p for window %p.\n", swapchain->hDC, swapchain->win_handle);
-            swapchain->hDC = GetDC(swapchain->win_handle);
-            Assert(swapchain->hDC && (WindowFromDC(swapchain->hDC)==swapchain->win_handle));
         }
     }
@@ -815,6 +813,7 @@
     if (wnd != context->win_handle)
     {
-        WARN("DC %p belongs to window %p instead of %p.\n",
-                context->hdc, wnd, context->win_handle);
+        DWORD winEr = GetLastError();
+        ERR("DC %p belongs to window %p instead of %p., winEr(%d)\n",
+                context->hdc, wnd, context->win_handle, winEr);
         context->valid = 0;
     }
@@ -952,7 +951,11 @@
         ERR("Failed to disable GL context.\n");
     }
-
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
+# ifndef VBOX_WITH_WDDM
     ReleaseDC(context->win_handle, context->hdc);
-
+# else
+    VBoxExtReleaseDC(context->win_handle, context->hdc);
+# endif
+#endif
     if (!pwglDeleteContext(context->glCtx))
     {
@@ -972,21 +975,61 @@
 }
 
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+static struct wined3d_context *context_get_current_ex(DWORD adjustTid)
+{
+    struct wined3d_context *ctx = TlsGetValue(wined3d_context_tls_idx);
+    if (!adjustTid)
+        return ctx;
+    if (!ctx || ctx->tid == adjustTid)
+        return ctx;
+    if (context_set_current(ctx))
+    {
+        Assert(ctx->tid == adjustTid);
+        return ctx;
+    }
+    ERR("context_set_current failed\n");
+    return NULL;
+}
+#endif
+
 struct wined3d_context *context_get_current(void)
 {
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
     return TlsGetValue(wined3d_context_tls_idx);
-}
+#else
+    DWORD tid = GetCurrentThreadId();
+    return context_get_current_ex(tid);
+#endif
+}
+
 
 BOOL context_set_current(struct wined3d_context *ctx)
 {
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+    struct wined3d_context *old = context_get_current_ex(0);
+    DWORD tid = GetCurrentThreadId();
+#else
     struct wined3d_context *old = context_get_current();
-
+#endif
     if (old == ctx)
     {
-        TRACE("Already using D3D context %p.\n", ctx);
-        return TRUE;
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+        if (ctx && ctx->tid != tid)
+        {
+            old = NULL;
+        }
+        else
+#endif
+        {
+            TRACE("Already using D3D context %p.\n", ctx);
+            return TRUE;
+        }
     }
 
     if (old)
     {
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+        old->tid = 0;
+#endif
         if (old->destroyed)
         {
@@ -1026,5 +1069,22 @@
     }
 
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
     return TlsSetValue(wined3d_context_tls_idx, ctx);
+#else
+    if (TlsSetValue(wined3d_context_tls_idx, ctx))
+    {
+        if (ctx)
+        {
+            ctx->tid = tid;
+        }
+        return TRUE;
+    }
+    else
+    {
+        DWORD err = GetLastError();
+        ERR("Failed to set tls value, last error %#x.\n", err);
+    }
+    return FALSE;
+#endif
 }
 
@@ -1044,4 +1104,8 @@
     {
         TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+        /* should not be here*/
+        ERR("should not be here\n");
+#endif
         if (!pwglMakeCurrent(context->restore_dc, context->restore_ctx))
         {
@@ -1050,4 +1114,32 @@
                     context->restore_ctx, context->restore_dc, err);
         }
+#ifdef VBOX_WITH_WDDM
+        else
+        {
+            /* success branch */
+            /* sync back our tls with gl settings  */
+            const struct wined3d_context *current_context = context_get_current();
+            if (current_context && current_context->glCtx != context->restore_ctx)
+            {
+                IWineD3DDeviceImpl *device = context->device;
+                int i = 0;
+                for (; i < device->numContexts; ++i)
+                {
+                    const struct wined3d_context *ctx = device->contexts[i];
+                    if (ctx->glCtx == context->restore_ctx)
+                    {
+                        context_set_current(ctx);
+                        break;
+                    }
+                }
+
+                if (i == device->numContexts)
+                {
+                    context_set_current(NULL);
+                }
+            }
+        }
+#endif
+
         context->restore_ctx = NULL;
         context->restore_dc = NULL;
@@ -1066,4 +1158,8 @@
         if (current_gl && (!current_context || current_context->glCtx != current_gl))
         {
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+            /* should not be here*/
+            ERR("should not be here\n");
+#endif
             TRACE("Another GL context (%p on device context %p) is already current.\n",
                     current_gl, pwglGetCurrentDC());
@@ -1305,5 +1401,12 @@
     }
 
-    if (!(hdc = GetDC(swapchain->win_handle)))
+    if (!(hdc =
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+            swapchain->hDC
+#else
+            GetDC(swapchain->win_handle)
+#endif
+            )
+            )
     {
         ERR("Failed to retrieve a device context.\n");
@@ -1426,5 +1529,7 @@
 #endif
     ret->current_rt = (IWineD3DSurface *)target;
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
     ret->tid = GetCurrentThreadId();
+#endif
 
     ret->render_offscreen = surface_is_offscreen((IWineD3DSurface *) target);
@@ -1434,4 +1539,7 @@
     ret->glCtx = ctx;
     ret->win_handle = swapchain->win_handle;
+#ifdef VBOX_WITH_WDDM
+    Assert(WindowFromDC(hdc) == ret->win_handle);
+#endif
     ret->hdc = hdc;
     ret->pixel_format = pixel_format;
@@ -1656,8 +1764,11 @@
         const struct wined3d_format_desc *ds_format_desc)
 {
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
     UINT i;
     DWORD tid = GetCurrentThreadId();
+#endif
     struct wined3d_context *context = NULL;
 
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
     for(i = 0 ; i < device->numContexts ; i ++)
     {
@@ -1667,4 +1778,7 @@
         }
     }
+#else
+    context = device->numContexts ? device->contexts[0] : NULL;
+#endif
 
     if (!context)
@@ -1961,8 +2075,17 @@
  *
  *****************************************************************************/
-static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid)
-{
+static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
+        , DWORD tid
+#endif
+        )
+{
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+    IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
+    if (device->numContexts)
+        return device->contexts[0];
+#else
     unsigned int i;
-#ifdef VBOX_WITH_WDDM
+# ifdef VBOX_WITH_WDDM
     IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
     for (i = 0; i < device->numContexts; ++i)
@@ -1971,5 +2094,5 @@
             return device->contexts[i];
     }
-#else
+# else
     for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
         if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
@@ -1977,4 +2100,5 @@
         }
     }
+# endif
 #endif
 
@@ -1983,4 +2107,5 @@
 }
 
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
 /*****************************************************************************
  * FindContext
@@ -1999,5 +2124,7 @@
     IWineD3DSwapChain *swapchain = NULL;
     struct wined3d_context *current_context = context_get_current();
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
     DWORD tid = GetCurrentThreadId();
+#endif
     struct wined3d_context *context;
 
@@ -2049,5 +2176,9 @@
         TRACE("Rendering onscreen\n");
 
-        context = findThreadContextForSwapChain(swapchain, tid);
+        context = findThreadContextForSwapChain(swapchain
+#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
+                , tid
+#endif
+                );
 #ifdef VBOX_WITH_WDDM
         context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
@@ -2073,5 +2204,9 @@
              * is perfect to call. */
 #ifdef VBOX_WITH_WDDM /* tmp work-around */
-            context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1], tid);
+            context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1]
+# ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
+                                                                     , tid
+# endif
+                                                                     );
 #else
             context = findThreadContextForSwapChain(This->swapchains[0], tid);
@@ -2091,4 +2226,110 @@
     return context;
 }
+#else
+/*****************************************************************************
+ * FindContext
+ *
+ * Finds a context for the current render target and thread
+ *
+ * Parameters:
+ *  target: Render target to find the context for
+ *  tid: Thread to activate the context for
+ *
+ * Returns: The needed context
+ *
+ *****************************************************************************/
+static struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target)
+{
+    IWineD3DSwapChain *swapchain = NULL;
+    struct wined3d_context *context = This->numContexts ? This->contexts[0] : NULL;
+
+    if (context && context->destroyed)
+    {
+        ERR("context is destroyed");
+    }
+
+    if (!target)
+    {
+        if (context
+            && context->current_rt
+            && context_get_device(context) == This
+           )
+        {
+            target = context->current_rt;
+        }
+        else
+        {
+#ifdef VBOX_WITH_WDDM
+            /* tmp work-around */
+            IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1];
+#else
+            IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[0];
+#endif
+            if (swapchain->backBuffer) target = swapchain->backBuffer[0];
+            else target = swapchain->frontBuffer;
+        }
+    }
+
+    if (context && context->current_rt == target)
+    {
+#ifdef VBOX_WITH_WDDM
+        IWineD3DSwapChain *swapchain = NULL;
+        if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
+            context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
+            IWineD3DSwapChain_Release(swapchain);
+        }
+        else {
+                                                                                                    /* tmp work-around */
+            context_validate(context,
+                    NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
+                                                                                );
+        }
+#else
+        context_validate(context);
+#endif
+        return context;
+    }
+
+    if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
+        TRACE("Rendering onscreen\n");
+
+        context = findThreadContextForSwapChain(swapchain);
+#ifdef VBOX_WITH_WDDM
+        context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
+#endif
+        IWineD3DSwapChain_Release(swapchain);
+    }
+    else
+    {
+        TRACE("Rendering offscreen\n");
+
+        if (!context)
+        {
+            /* This may happen if the app jumps straight into offscreen rendering
+             * Start using the context of the primary swapchain. tid == 0 is no problem
+             * for findThreadContextForSwapChain.
+             *
+             * Can also happen on thread switches - in that case findThreadContextForSwapChain
+             * is perfect to call. */
+#ifdef VBOX_WITH_WDDM /* tmp work-around */
+            context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1]);
+#else
+            context = findThreadContextForSwapChain(This->swapchains[0], tid);
+#endif
+        }
+#ifdef VBOX_WITH_WDDM
+        context_validate(context,
+                NULL //(IWineD3DSwapChainImpl*)This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
+                                 );
+#endif
+    }
+
+#ifndef VBOX_WITH_WDDM
+    context_validate(context);
+#endif
+
+    return context;
+}
+#endif
 
 /* Context activation is done by the caller. */
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/directx.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/directx.c	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/directx.c	(revision 38112)
@@ -392,4 +392,8 @@
         }
         HeapFree(GetProcessHeap(), 0, This);
+
+#ifdef VBOX_WITH_WDDM
+        VBoxExtCheckTerm();
+#endif
     }
 
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/query.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/query.c	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/query.c	(revision 38112)
@@ -64,5 +64,9 @@
     if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
     {
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+        ERR("Event query tested from wrong thread\n");
+#else
         WARN("Event query tested from wrong thread\n");
+#endif
         return WINED3D_EVENT_QUERY_WRONG_THREAD;
     }
@@ -141,5 +145,9 @@
          * to find its own way to cope with the thread switch
          */
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+        ERR("Event query finished from wrong thread\n");
+#else
         WARN("Event query finished from wrong thread\n");
+#endif
         return WINED3D_EVENT_QUERY_WRONG_THREAD;
     }
@@ -198,4 +206,7 @@
         if (!query->context->gl_info->supported[ARB_SYNC] && query->context->tid != GetCurrentThreadId())
         {
+#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
+            ERR("unexpected\n");
+#endif
             context_free_event_query(query);
             context = context_acquire(device, NULL, CTXUSAGE_RESOURCELOAD);
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 38112)
@@ -553,11 +553,4 @@
 {
     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
-
-#if defined(DEBUG_misha) && defined (VBOX_WITH_WDDM)
-    if (VBOXSHRC_IS_SHARED_UNLOCKED(This))
-    {
-        Assert(0);
-    }
-#endif
 
     This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 38112)
@@ -108,5 +108,6 @@
 #ifdef VBOX_WITH_WDDM
     if(This->device_window) {
-        ReleaseDC(This->device_window, This->hDC);
+        /* see VBoxExtGet/ReleaseDC for comments */
+        VBoxExtReleaseDC(This->device_window, This->hDC);
     }
 #else
@@ -376,13 +377,5 @@
         Assert(wnd==context->win_handle);
         Assert(IsWindow(context->win_handle));
-
-        if (wnd != context->win_handle)
-        {
-            extern void context_update_window(struct wined3d_context *context, IWineD3DSwapChainImpl *swapchain);
-
-            context->valid = 0;
-            Assert(context->currentSwapchain == This);
-            context_update_window(context, context->currentSwapchain);
-        }
+        Assert(wnd == context->win_handle);
     }
 #endif
@@ -739,5 +732,6 @@
 #ifdef VBOX_WITH_WDDM
     Assert(window);
-    swapchain->hDC = GetDC(window);
+    /* see VBoxExtGet/ReleaseDC for comments */
+    swapchain->hDC = VBoxExtGetDC(window);
     if (!swapchain->hDC)
     {
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 38112)
@@ -2,5 +2,5 @@
 /** @file
  *
- * VBox extension to Wine D3D
+ * VBox extension to Wine D3D - shared resource
  *
  * Copyright (C) 2010 Oracle Corporation
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_main.c
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_main.c	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_main.c	(revision 38112)
@@ -95,8 +95,20 @@
     HRESULT hr;
 
+#ifdef VBOX_WITH_WDDM
+    hr = VBoxExtCheckInit();
+    if (FAILED(hr))
+    {
+        ERR("VBoxExtCheckInit failed, hr (0x%x)\n", hr);
+        return NULL;
+    }
+#endif
+
     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
     if (!object)
     {
         ERR("Failed to allocate wined3d object memory.\n");
+#ifdef VBOX_WITH_WDDM
+        VBoxExtCheckTerm();
+#endif
         return NULL;
     }
@@ -107,4 +119,7 @@
         WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
         HeapFree(GetProcessHeap(), 0, object);
+#ifdef VBOX_WITH_WDDM
+        VBoxExtCheckTerm();
+#endif
         return NULL;
     }
Index: trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h
===================================================================
--- trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 38107)
+++ trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/wined3d_private.h	(revision 38112)
@@ -51,4 +51,8 @@
 #include "wine/debug.h"
 #include "wine/unicode.h"
+#ifdef VBOX_WITH_WDDM
+# include "vboxext.h"
+#endif
+
 
 #ifndef VBOX_WINE_WITHOUT_LIBWINE
