Index: /trunk/include/VBox/VBoxVideo.h
===================================================================
--- /trunk/include/VBox/VBoxVideo.h	(revision 49331)
+++ /trunk/include/VBox/VBoxVideo.h	(revision 49332)
@@ -1422,4 +1422,5 @@
     VBOXVDMACMD_CHROMIUM_CTL_TYPE_SAVESTATE_END,
     VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRHGSMI_SETUP_COMPLETION,
+    VBOXVDMACMD_CHROMIUM_CTL_TYPE_CRCONNECT,
     VBOXVDMACMD_CHROMIUM_CTL_TYPE_SIZEHACK = 0xfffffffe
 } VBOXVDMACMD_CHROMIUM_CTL_TYPE;
@@ -1442,4 +1443,5 @@
 } VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP, *PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP;
 
+
 typedef struct PDMIDISPLAYVBVACALLBACKS *HCRHGSMICMDCOMPLETION;
 typedef DECLCALLBACK(int) FNCRHGSMICMDCOMPLETION(HCRHGSMICMDCOMPLETION hCompletion, PVBOXVDMACMD_CHROMIUM_CMD pCmd, int rc);
@@ -1452,5 +1454,95 @@
     PFNCRHGSMICMDCOMPLETION pfnCompletion;
 } VBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_COMPLETION, *PVBOXVDMACMD_CHROMIUM_CTL_CRHGSMI_SETUP_COMPLETION;
+
+typedef struct VBOXCRCON_SERVER *HVBOXCRCON_SERVER;
+typedef struct PDMIDISPLAYVBVACALLBACKS* HVBOXCRCON_CLIENT;
+
+typedef struct VBOXCRCON_3DRGN_CLIENT* HVBOXCRCON_3DRGN_CLIENT;
+typedef struct VBOXCRCON_3DRGN_ASYNCCLIENT* HVBOXCRCON_3DRGN_ASYNCCLIENT;
+
+/* server callbacks */
+/* submit chromium cmd */
+typedef DECLCALLBACK(int) FNVBOXCRCON_SVR_CRCMD(HVBOXCRCON_SERVER hServer, PVBOXVDMACMD_CHROMIUM_CMD pCmd, uint32_t cbCmd);
+typedef FNVBOXCRCON_SVR_CRCMD *PFNVBOXCRCON_SVR_CRCMD;
+
+/* submit chromium control cmd */
+typedef DECLCALLBACK(int) FNVBOXCRCON_SVR_CRCTL(HVBOXCRCON_SERVER hServer, PVBOXVDMACMD_CHROMIUM_CTL pCtl, uint32_t cbCmd);
+typedef FNVBOXCRCON_SVR_CRCTL *PFNVBOXCRCON_SVR_CRCTL;
+
+/* request 3D data.
+ * The protocol is the following:
+ * 1. if there is no 3D data displayed on screen, returns VINF_EOF immediately w/o calling any PFNVBOXCRCON_3DRGN_XXX callbacks
+ * 2. otherwise calls PFNVBOXCRCON_3DRGN_ONSUBMIT, submits the "regions get" request to the CrOpenGL server to process it asynchronously and returns VINF_SUCCESS
+ * 2.a on "regions get" request processing calls PFNVBOXCRCON_3DRGN_BEGIN,
+ * 2.b then PFNVBOXCRCON_3DRGN_REPORT zero or more times for each 3D region,
+ * 2.c and then PFNVBOXCRCON_3DRGN_END
+ * 3. returns VERR_XXX code on failure
+ * */
+typedef DECLCALLBACK(int) FNVBOXCRCON_SVR_3DRGN_GET(HVBOXCRCON_SERVER hServer, HVBOXCRCON_3DRGN_CLIENT hRgnClient, uint32_t idScreen);
+typedef FNVBOXCRCON_SVR_3DRGN_GET *PFNVBOXCRCON_SVR_3DRGN_GET;
+
+/* 3D Regions Client callbacks */
+/* called from the PFNVBOXCRCON_SVR_3DRGN_GET callback in case server has 3D data and is going to process the request asynchronously,
+ * see comments for PFNVBOXCRCON_SVR_3DRGN_GET above */
+typedef DECLCALLBACK(int) FNVBOXCRCON_3DRGN_ONSUBMIT(HVBOXCRCON_3DRGN_CLIENT hRgnClient, uint32_t idScreen, HVBOXCRCON_3DRGN_ASYNCCLIENT *phRgnAsyncClient);
+typedef FNVBOXCRCON_3DRGN_ONSUBMIT *PFNVBOXCRCON_3DRGN_ONSUBMIT;
+
+/* called from the "regions get" command processing thread, to indicate that the "regions get" is started.
+ * see comments for PFNVBOXCRCON_SVR_3DRGN_GET above */
+typedef DECLCALLBACK(int) FNVBOXCRCON_3DRGN_BEGIN(HVBOXCRCON_3DRGN_ASYNCCLIENT hRgnAsyncClient, uint32_t idScreen);
+typedef FNVBOXCRCON_3DRGN_BEGIN *PFNVBOXCRCON_3DRGN_BEGIN;
+
+/* called from the "regions get" command processing thread, to report a 3D region.
+ * see comments for PFNVBOXCRCON_SVR_3DRGN_GET above */
+typedef DECLCALLBACK(int) FNVBOXCRCON_3DRGN_REPORT(HVBOXCRCON_3DRGN_ASYNCCLIENT hRgnAsyncClient, uint32_t idScreen, void *pvData, uint32_t cbStride, const RTRECT *pRect);
+typedef FNVBOXCRCON_3DRGN_REPORT *PFNVBOXCRCON_3DRGN_REPORT;
+
+/* called from the "regions get" command processing thread, to indicate that the "regions get" is completed.
+ * see comments for PFNVBOXCRCON_SVR_3DRGN_GET above */
+typedef DECLCALLBACK(int) FNVBOXCRCON_3DRGN_END(HVBOXCRCON_3DRGN_ASYNCCLIENT hRgnAsyncClient, uint32_t idScreen);
+typedef FNVBOXCRCON_3DRGN_END *PFNVBOXCRCON_3DRGN_END;
+
+
+/* client callbacks */
+/* complete chromium cmd */
+typedef DECLCALLBACK(int) FNVBOXCRCON_CLT_CRCTL_COMPLETE(HVBOXCRCON_CLIENT hClient, PVBOXVDMACMD_CHROMIUM_CTL pCtl, int rc);
+typedef FNVBOXCRCON_CLT_CRCTL_COMPLETE *PFNVBOXCRCON_CLT_CRCTL_COMPLETE;
+
+/* complete chromium control cmd */
+typedef DECLCALLBACK(int) FNVBOXCRCON_CLT_CRCMD_COMPLETE(HVBOXCRCON_CLIENT hClient, PVBOXVDMACMD_CHROMIUM_CMD pCmd, int rc);
+typedef FNVBOXCRCON_CLT_CRCMD_COMPLETE *PFNVBOXCRCON_CLT_CRCMD_COMPLETE;
+
+typedef struct VBOXCRCON_SERVER_CALLBACKS
+{
+    HVBOXCRCON_SERVER hServer;
+    PFNVBOXCRCON_SVR_CRCMD pfnCrCmd;
+    PFNVBOXCRCON_SVR_CRCTL pfnCrCtl;
+    PFNVBOXCRCON_SVR_3DRGN_GET pfn3DRgnGet;
+} VBOXCRCON_SERVER_CALLBACKS, *PVBOXCRCON_SERVER_CALLBACKS;
+
+typedef struct VBOXCRCON_CLIENT_CALLBACKS
+{
+    HVBOXCRCON_CLIENT hClient;
+    PFNVBOXCRCON_CLT_CRCMD_COMPLETE pfnCrCmdComplete;
+    PFNVBOXCRCON_CLT_CRCTL_COMPLETE pfnCrCtlComplete;
+    PFNVBOXCRCON_3DRGN_ONSUBMIT pfn3DRgnOnSubmit;
+    PFNVBOXCRCON_3DRGN_BEGIN pfn3DRgnBegin;
+    PFNVBOXCRCON_3DRGN_REPORT pfn3DRgnReport;
+    PFNVBOXCRCON_3DRGN_END pfn3DRgnEnd;
+} VBOXCRCON_CLIENT_CALLBACKS, *PVBOXCRCON_CLIENT_CALLBACKS;
+
+/* issued by Main to establish connection between Main and CrOpenGL service */
+typedef struct VBOXVDMACMD_CHROMIUM_CTL_CRCONNECT
+{
+    VBOXVDMACMD_CHROMIUM_CTL Hdr;
+    /*input (filled by Client) :*/
+    /*class VMMDev*/void *pVMMDev;
+    VBOXCRCON_CLIENT_CALLBACKS ClientCallbacks;
+    /*output (filled by Server) :*/
+    VBOXCRCON_SERVER_CALLBACKS ServerCallbacks;
+} VBOXVDMACMD_CHROMIUM_CTL_CRCONNECT, *PVBOXVDMACMD_CHROMIUM_CTL_CRCONNECT;
+
 # pragma pack()
+
 #endif
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiBase.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiBase.cpp	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiBase.cpp	(revision 49332)
@@ -33,5 +33,5 @@
 int vboxUhgsmiBaseBufferTerm(PVBOXUHGSMI_BUFFER_PRIVATE_ESC_BASE pBuffer)
 {
-    PVBOXUHGSMI_PRIVATE_BASE pPrivate = VBOXUHGSMIBASE_GET(pBuffer->PrivateBase.pHgsmi);
+    PVBOXUHGSMI_PRIVATE_BASE pPrivate = VBOXUHGSMIBASE_GET(pBuffer->BasePrivate.pHgsmi);
     VBOXDISPIFESCAPE_UHGSMI_DEALLOCATE DeallocInfo = {0};
     DeallocInfo.EscapeHdr.escapeCode = VBOXESC_UHGSMI_DEALLOCATE;
@@ -98,10 +98,10 @@
     pBuffer->Alloc = AllocInfo.Alloc;
     Assert(pBuffer->Alloc.pvData);
-    pBuffer->PrivateBase.pHgsmi = pPrivate;
-    pBuffer->PrivateBase.Base.pfnLock = vboxUhgsmiBaseEscBufferLock;
-    pBuffer->PrivateBase.Base.pfnUnlock = vboxUhgsmiBaseEscBufferUnlock;
-    pBuffer->PrivateBase.Base.pfnDestroy = pfnDestroy;
-    pBuffer->PrivateBase.Base.fType = fUhgsmiType;
-    pBuffer->PrivateBase.Base.cbBuffer = AllocInfo.Alloc.cbData;
+    pBuffer->BasePrivate.pHgsmi = pPrivate;
+    pBuffer->BasePrivate.Base.pfnLock = vboxUhgsmiBaseEscBufferLock;
+    pBuffer->BasePrivate.Base.pfnUnlock = vboxUhgsmiBaseEscBufferUnlock;
+    pBuffer->BasePrivate.Base.pfnDestroy = pfnDestroy;
+    pBuffer->BasePrivate.Base.fType = fUhgsmiType;
+    pBuffer->BasePrivate.Base.cbBuffer = AllocInfo.Alloc.cbData;
     pBuffer->hSynch = hSynch;
     return VINF_SUCCESS;
@@ -137,5 +137,5 @@
     if (RT_SUCCESS(rc))
     {
-        *ppBuf = &pBuffer->PrivateBase.Base;
+        *ppBuf = &pBuffer->BasePrivate.Base;
         return VINF_SUCCESS;
     }
@@ -182,5 +182,5 @@
         {
             pSubmInfo->Info.offData = 0;
-            pSubmInfo->Info.cbData = pBuf->PrivateBase.Base.cbBuffer;
+            pSubmInfo->Info.cbData = pBuf->BasePrivate.Base.cbBuffer;
         }
         else
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiBase.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiBase.h	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiBase.h	(revision 49332)
@@ -68,5 +68,5 @@
 typedef struct VBOXUHGSMI_BUFFER_PRIVATE_ESC_BASE
 {
-    VBOXUHGSMI_BUFFER_PRIVATE_BASE PrivateBase;
+    VBOXUHGSMI_BUFFER_PRIVATE_BASE BasePrivate;
     VBOXVIDEOCM_UM_ALLOC Alloc;
     HANDLE hSynch;
@@ -75,5 +75,5 @@
 typedef struct VBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE
 {
-    VBOXUHGSMI_BUFFER_PRIVATE_BASE PrivateBase;
+    VBOXUHGSMI_BUFFER_PRIVATE_BASE BasePrivate;
     D3DKMT_HANDLE hAllocation;
     UINT aLockPageIndices[1];
@@ -84,11 +84,12 @@
 #define VBOXUHGSMIBASE_GET_BUFFER(_p) VBOXUHGSMIBASE_GET_PRIVATE(_p, VBOXUHGSMI_BUFFER_PRIVATE_BASE)
 
-#define VBOXUHGSMIPRIVATEBASE_GET_PRIVATE(_p, _t) ((_t*)(((uint8_t*)_p) - RT_OFFSETOF(_t, PrivateBase.Base)))
+#define VBOXUHGSMIPRIVATEBASE_GET_PRIVATE(_p, _t) ((_t*)(((uint8_t*)_p) - RT_OFFSETOF(_t, BasePrivate.Base)))
 #define VBOXUHGSMIESCBASE_GET_BUFFER(_p) VBOXUHGSMIPRIVATEBASE_GET_PRIVATE(_p, VBOXUHGSMI_BUFFER_PRIVATE_ESC_BASE)
 #define VBOXUHGSMDXALLOCBASE_GET_BUFFER(_p) VBOXUHGSMIPRIVATEBASE_GET_PRIVATE(_p, VBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE)
 
-DECLINLINE(int) vboxUhgsmiBaseLockData(PVBOXUHGSMI_BUFFER pBuf, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags,
-                                    D3DDDICB_LOCKFLAGS *pfFlags, UINT *pNumPages, UINT* pPages)
-{
+DECLINLINE(int) vboxUhgsmiBaseDxLockData(PVBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE pPrivate, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags,
+                                    D3DDDICB_LOCKFLAGS *pfFlags, UINT *pNumPages)
+{
+    PVBOXUHGSMI_BUFFER pBuf = &pPrivate->BasePrivate.Base;
     D3DDDICB_LOCKFLAGS fLockFlags;
     fLockFlags.Value = 0;
@@ -127,5 +128,5 @@
             for (UINT i = 0, j = iFirstPage; i < cPages; ++i, ++j)
             {
-                pPages[i] = j;
+                pPrivate->aLockPageIndices[i] = j;
             }
         }
@@ -141,6 +142,5 @@
 }
 
-#if 0
-DECLINLINE(int) vboxUhgsmiBaseDmaFill(PVBOXUHGSMI_BUFFER_SUBMIT aBuffers, uint32_t cBuffers,
+DECLINLINE(int) vboxUhgsmiBaseDxDmaFill(PVBOXUHGSMI_BUFFER_SUBMIT aBuffers, uint32_t cBuffers,
         VOID* pCommandBuffer, UINT *pCommandBufferSize,
         D3DDDI_ALLOCATIONLIST *pAllocationList, UINT AllocationListSize,
@@ -170,5 +170,5 @@
     {
         PVBOXUHGSMI_BUFFER_SUBMIT pBufInfo = &aBuffers[i];
-        PVBOXUHGSMI_BUFFER_PRIVATE_BASE pBuffer = VBOXUHGSMIBASE_GET_BUFFER(pBufInfo->pBuf);
+        PVBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE pBuffer = VBOXUHGSMDXALLOCBASE_GET_BUFFER(pBufInfo->pBuf);
 
         memset(pAllocationList, 0, sizeof (D3DDDI_ALLOCATIONLIST));
@@ -181,5 +181,5 @@
         {
             pBufSubmInfo->offData = 0;
-            pBufSubmInfo->cbData = pBuffer->Base.cbBuffer;
+            pBufSubmInfo->cbData = pBuffer->BasePrivate.Base.cbBuffer;
         }
         else
@@ -195,5 +195,4 @@
     return VINF_SUCCESS;
 }
-#endif
 
 DECLCALLBACK(int) vboxUhgsmiBaseEscBufferLock(PVBOXUHGSMI_BUFFER pBuf, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags, void**pvLock);
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiDisp.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiDisp.cpp	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiDisp.cpp	(revision 49332)
@@ -22,50 +22,42 @@
 #define VBOXUHGSMID3D_GET(_p) VBOXUHGSMID3D_GET_PRIVATE(_p, VBOXUHGSMI_PRIVATE_D3D)
 
-#if 0
-#define VBOXUHGSMID3D_GET_BUFFER(_p) VBOXUHGSMID3D_GET_PRIVATE(_p, VBOXUHGSMI_BUFFER_PRIVATE_D3D)
-
 #include <iprt/mem.h>
 #include <iprt/err.h>
 
-typedef struct VBOXUHGSMI_BUFFER_PRIVATE_D3D
-{
-    VBOXUHGSMI_BUFFER_PRIVATE_BASE BasePrivate;
-    PVBOXWDDMDISP_DEVICE pDevice;
-    UINT aLockPageIndices[1];
-} VBOXUHGSMI_BUFFER_PRIVATE_D3D, *PVBOXUHGSMI_BUFFER_PRIVATE_D3D;
-
-
-
 DECLCALLBACK(int) vboxUhgsmiD3DBufferDestroy(PVBOXUHGSMI_BUFFER pBuf)
 {
-    PVBOXUHGSMI_BUFFER_PRIVATE_D3D pBuffer = VBOXUHGSMID3D_GET_BUFFER(pBuf);
+    PVBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE pBuffer = VBOXUHGSMDXALLOCBASE_GET_BUFFER(pBuf);
+    struct VBOXWDDMDISP_DEVICE *pDevice = VBOXUHGSMID3D_GET(pBuffer->BasePrivate.pHgsmi)->pDevice;
     D3DDDICB_DEALLOCATE DdiDealloc;
     DdiDealloc.hResource = 0;
     DdiDealloc.NumAllocations = 1;
-    DdiDealloc.HandleList = &pBuffer->BasePrivate.hAllocation;
-    HRESULT hr = pBuffer->pDevice->RtCallbacks.pfnDeallocateCb(pBuffer->pDevice->hDevice, &DdiDealloc);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-    {
-        if (pBuffer->BasePrivate.hSynch)
-            CloseHandle(pBuffer->BasePrivate.hSynch);
+    DdiDealloc.HandleList = &pBuffer->hAllocation;
+    HRESULT hr = pDevice->RtCallbacks.pfnDeallocateCb(pDevice->hDevice, &DdiDealloc);
+    if (hr == S_OK)
+    {
         RTMemFree(pBuffer);
         return VINF_SUCCESS;
     }
-    return VERR_GENERAL_FAILURE;
-}
-
+
+    WARN(("pfnDeallocateCb failed, hr %#x", hr));
+    return VERR_GENERAL_FAILURE;
+}
+
+/* typedef DECLCALLBACK(int) FNVBOXUHGSMI_BUFFER_LOCK(PVBOXUHGSMI_BUFFER pBuf, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags, void**pvLock); */
 DECLCALLBACK(int) vboxUhgsmiD3DBufferLock(PVBOXUHGSMI_BUFFER pBuf, uint32_t offLock, uint32_t cbLock, VBOXUHGSMI_BUFFER_LOCK_FLAGS fFlags, void**pvLock)
 {
-    PVBOXUHGSMI_BUFFER_PRIVATE_D3D pBuffer = VBOXUHGSMID3D_GET_BUFFER(pBuf);
+    PVBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE pBuffer = VBOXUHGSMDXALLOCBASE_GET_BUFFER(pBuf);
+    struct VBOXWDDMDISP_DEVICE *pDevice = VBOXUHGSMID3D_GET(pBuffer->BasePrivate.pHgsmi)->pDevice;
     D3DDDICB_LOCK DdiLock = {0};
-    DdiLock.hAllocation = pBuffer->BasePrivate.hAllocation;
+    DdiLock.hAllocation = pBuffer->hAllocation;
     DdiLock.PrivateDriverData = 0;
 
-    int rc = vboxUhgsmiBaseLockData(pBuf, offLock, cbLock, fFlags,
-                                         &DdiLock.Flags, &DdiLock.NumPages, pBuffer->aLockPageIndices);
-    AssertRC(rc);
-    if (RT_FAILURE(rc))
+    int rc = vboxUhgsmiBaseDxLockData(pBuffer, offLock, cbLock, fFlags,
+                                         &DdiLock.Flags, &DdiLock.NumPages);
+    if (!RT_SUCCESS(rc))
+    {
+        WARN(("vboxUhgsmiBaseDxLockData failed rc %d", rc));
         return rc;
+    }
 
     if (DdiLock.NumPages)
@@ -74,6 +66,5 @@
         DdiLock.pPages = NULL;
 
-    HRESULT hr = pBuffer->pDevice->RtCallbacks.pfnLockCb(pBuffer->pDevice->hDevice, &DdiLock);
-    Assert(hr == S_OK);
+    HRESULT hr = pDevice->RtCallbacks.pfnLockCb(pDevice->hDevice, &DdiLock);
     if (hr == S_OK)
     {
@@ -81,4 +72,6 @@
         return VINF_SUCCESS;
     }
+
+    WARN(("pfnLockCb failed, hr %#x", hr));
     return VERR_GENERAL_FAILURE;
 }
@@ -86,25 +79,24 @@
 DECLCALLBACK(int) vboxUhgsmiD3DBufferUnlock(PVBOXUHGSMI_BUFFER pBuf)
 {
-    PVBOXUHGSMI_BUFFER_PRIVATE_D3D pBuffer = VBOXUHGSMID3D_GET_BUFFER(pBuf);
+    PVBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE pBuffer = VBOXUHGSMDXALLOCBASE_GET_BUFFER(pBuf);
+    struct VBOXWDDMDISP_DEVICE *pDevice = VBOXUHGSMID3D_GET(pBuffer->BasePrivate.pHgsmi)->pDevice;
     D3DDDICB_UNLOCK DdiUnlock;
     DdiUnlock.NumAllocations = 1;
-    DdiUnlock.phAllocations = &pBuffer->BasePrivate.hAllocation;
-    HRESULT hr = pBuffer->pDevice->RtCallbacks.pfnUnlockCb(pBuffer->pDevice->hDevice, &DdiUnlock);
-    Assert(hr == S_OK);
-    if (hr == S_OK)
-        return VINF_SUCCESS;
-    return VERR_GENERAL_FAILURE;
-}
-
-DECLCALLBACK(int) vboxUhgsmiD3DBufferCreate(PVBOXUHGSMI pHgsmi, uint32_t cbBuf, VBOXUHGSMI_BUFFER_TYPE_FLAGS fUhgsmiType, PVBOXUHGSMI_BUFFER* ppBuf)
-{
-    HANDLE hSynch = NULL;
+    DdiUnlock.phAllocations = &pBuffer->hAllocation;
+    HRESULT hr = pDevice->RtCallbacks.pfnUnlockCb(pDevice->hDevice, &DdiUnlock);
+    if (hr == S_OK)
+        return VINF_SUCCESS;
+
+    WARN(("pfnUnlockCb failed, hr %#x", hr));
+    return VERR_GENERAL_FAILURE;
+}
+
+/*typedef DECLCALLBACK(int) FNVBOXUHGSMI_BUFFER_CREATE(PVBOXUHGSMI pHgsmi, uint32_t cbBuf, VBOXUHGSMI_BUFFER_TYPE_FLAGS fType, PVBOXUHGSMI_BUFFER* ppBuf);*/
+DECLCALLBACK(int) vboxUhgsmiD3DBufferCreate(PVBOXUHGSMI pHgsmi, uint32_t cbBuf, VBOXUHGSMI_BUFFER_TYPE_FLAGS fType, PVBOXUHGSMI_BUFFER* ppBuf)
+{
     if (!cbBuf)
         return VERR_INVALID_PARAMETER;
 
-    int rc = vboxUhgsmiBaseEventChkCreate(fUhgsmiType, &hSynch);
-    AssertRC(rc);
-    if (RT_FAILURE(rc))
-        return rc;
+    int rc = VINF_SUCCESS;
 
     cbBuf = VBOXWDDM_ROUNDBOUND(cbBuf, 0x1000);
@@ -114,41 +106,35 @@
 
     PVBOXUHGSMI_PRIVATE_D3D pPrivate = VBOXUHGSMID3D_GET(pHgsmi);
-    PVBOXUHGSMI_BUFFER_PRIVATE_D3D pBuf = (PVBOXUHGSMI_BUFFER_PRIVATE_D3D)RTMemAllocZ(RT_OFFSETOF(VBOXUHGSMI_BUFFER_PRIVATE_D3D, aLockPageIndices[cPages]));
-    Assert(pBuf);
+    PVBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE pBuf = (PVBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE)RTMemAllocZ(RT_OFFSETOF(VBOXUHGSMI_BUFFER_PRIVATE_DX_ALLOC_BASE, aLockPageIndices[cPages]));
     if (pBuf)
     {
-        struct
-        {
-            D3DDDICB_ALLOCATE DdiAlloc;
-            D3DDDI_ALLOCATIONINFO DdiAllocInfo;
-            VBOXWDDM_ALLOCINFO AllocInfo;
-        } Buf;
-        memset(&Buf, 0, sizeof (Buf));
-        Buf.DdiAlloc.hResource = NULL;
-        Buf.DdiAlloc.hKMResource = NULL;
-        Buf.DdiAlloc.NumAllocations = 1;
-        Buf.DdiAlloc.pAllocationInfo = &Buf.DdiAllocInfo;
-        Buf.DdiAllocInfo.pPrivateDriverData = &Buf.AllocInfo;
-        Buf.DdiAllocInfo.PrivateDriverDataSize = sizeof (Buf.AllocInfo);
-        Buf.AllocInfo.enmType = VBOXWDDM_ALLOC_TYPE_UMD_HGSMI_BUFFER;
-        Buf.AllocInfo.cbBuffer = cbBuf;
-        Buf.AllocInfo.hSynch = hSynch;
-        Buf.AllocInfo.fUhgsmiType = fUhgsmiType;
-
-        HRESULT hr = pPrivate->pDevice->RtCallbacks.pfnAllocateCb(pPrivate->pDevice->hDevice, &Buf.DdiAlloc);
-        Assert(hr == S_OK);
+        D3DDDICB_ALLOCATE DdiAlloc;
+        D3DDDI_ALLOCATIONINFO DdiAllocInfo;
+        VBOXWDDM_ALLOCINFO AllocInfo;
+
+        memset(&DdiAlloc, 0, sizeof (DdiAlloc));
+        DdiAlloc.hResource = NULL;
+        DdiAlloc.hKMResource = NULL;
+        DdiAlloc.NumAllocations = 1;
+        DdiAlloc.pAllocationInfo = &DdiAllocInfo;
+        DdiAllocInfo.pPrivateDriverData = &AllocInfo;
+        DdiAllocInfo.PrivateDriverDataSize = sizeof (AllocInfo);
+        AllocInfo.enmType = VBOXWDDM_ALLOC_TYPE_UMD_HGSMI_BUFFER;
+        AllocInfo.cbBuffer = cbBuf;
+        AllocInfo.hSynch = 0;
+        AllocInfo.fUhgsmiType = fType;
+
+        HRESULT hr = pPrivate->pDevice->RtCallbacks.pfnAllocateCb(pPrivate->pDevice->hDevice, &DdiAlloc);
         if (hr == S_OK)
         {
-            Assert(Buf.DdiAllocInfo.hAllocation);
+            Assert(DdiAllocInfo.hAllocation);
             pBuf->BasePrivate.Base.pfnLock = vboxUhgsmiD3DBufferLock;
             pBuf->BasePrivate.Base.pfnUnlock = vboxUhgsmiD3DBufferUnlock;
-//            pBuf->Base.pfnAdjustValidDataRange = vboxUhgsmiD3DBufferAdjustValidDataRange;
             pBuf->BasePrivate.Base.pfnDestroy = vboxUhgsmiD3DBufferDestroy;
 
-            pBuf->BasePrivate.Base.fType = fUhgsmiType;
+            pBuf->BasePrivate.Base.fType = fType;
             pBuf->BasePrivate.Base.cbBuffer = cbBuf;
 
-            pBuf->pDevice = pPrivate->pDevice;
-            pBuf->BasePrivate.hAllocation = Buf.DdiAllocInfo.hAllocation;
+            pBuf->hAllocation = DdiAllocInfo.hAllocation;
 
             *ppBuf = &pBuf->BasePrivate.Base;
@@ -156,16 +142,22 @@
             return VINF_SUCCESS;
         }
+        else
+        {
+            WARN(("pfnAllocateCb failed hr %#x"));
+            rc = VERR_GENERAL_FAILURE;
+        }
 
         RTMemFree(pBuf);
     }
     else
+    {
+        WARN(("RTMemAllocZ failed"));
         rc = VERR_NO_MEMORY;
-
-    if (hSynch)
-        CloseHandle(hSynch);
+    }
 
     return rc;
 }
 
+/* typedef DECLCALLBACK(int) FNVBOXUHGSMI_BUFFER_SUBMIT(PVBOXUHGSMI pHgsmi, PVBOXUHGSMI_BUFFER_SUBMIT aBuffers, uint32_t cBuffers); */
 DECLCALLBACK(int) vboxUhgsmiD3DBufferSubmit(PVBOXUHGSMI pHgsmi, PVBOXUHGSMI_BUFFER_SUBMIT aBuffers, uint32_t cBuffers)
 {
@@ -173,11 +165,13 @@
     PVBOXWDDMDISP_DEVICE pDevice = pHg->pDevice;
     UINT cbDmaCmd = pDevice->DefaultContext.ContextInfo.CommandBufferSize;
-    int rc = vboxUhgsmiBaseDmaFill(aBuffers, cBuffers,
+    int rc = vboxUhgsmiBaseDxDmaFill(aBuffers, cBuffers,
             pDevice->DefaultContext.ContextInfo.pCommandBuffer, &cbDmaCmd,
             pDevice->DefaultContext.ContextInfo.pAllocationList, pDevice->DefaultContext.ContextInfo.AllocationListSize,
             pDevice->DefaultContext.ContextInfo.pPatchLocationList, pDevice->DefaultContext.ContextInfo.PatchLocationListSize);
-    AssertRC(rc);
     if (RT_FAILURE(rc))
+    {
+        WARN(("vboxUhgsmiBaseDxDmaFill failed, rc %d", rc));
         return rc;
+    }
 
     D3DDDICB_RENDER DdiRender = {0};
@@ -194,5 +188,4 @@
 
     HRESULT hr = pDevice->RtCallbacks.pfnRenderCb(pDevice->hDevice, &DdiRender);
-    Assert(hr == S_OK);
     if (hr == S_OK)
     {
@@ -207,15 +200,16 @@
     }
 
-    return VERR_GENERAL_FAILURE;
-}
-
-HRESULT vboxUhgsmiD3DInit(PVBOXUHGSMI_PRIVATE_D3D pHgsmi, PVBOXWDDMDISP_DEVICE pDevice)
+    WARN(("pfnRenderCb failed, hr %#x", hr));
+    return VERR_GENERAL_FAILURE;
+}
+
+void vboxUhgsmiD3DInit(PVBOXUHGSMI_PRIVATE_D3D pHgsmi, PVBOXWDDMDISP_DEVICE pDevice)
 {
     pHgsmi->BasePrivate.Base.pfnBufferCreate = vboxUhgsmiD3DBufferCreate;
     pHgsmi->BasePrivate.Base.pfnBufferSubmit = vboxUhgsmiD3DBufferSubmit;
+    /* no escapes (for now) */
+    pHgsmi->BasePrivate.pfnEscape = NULL;
     pHgsmi->pDevice = pDevice;
-    return S_OK;
-}
-#endif
+}
 
 static DECLCALLBACK(int) vboxCrHhgsmiDispEscape(struct VBOXUHGSMI_PRIVATE_BASE *pHgsmi, void *pvData, uint32_t cbData, BOOL fHwAccess)
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiDisp.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiDisp.h	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiDisp.h	(revision 49332)
@@ -29,7 +29,5 @@
 } VBOXUHGSMI_PRIVATE_D3D, *PVBOXUHGSMI_PRIVATE_D3D;
 
-#if 0
 void vboxUhgsmiD3DInit(PVBOXUHGSMI_PRIVATE_D3D pHgsmi, struct VBOXWDDMDISP_DEVICE *pDevice);
-#endif
 
 void vboxUhgsmiD3DEscInit(PVBOXUHGSMI_PRIVATE_D3D pHgsmi, struct VBOXWDDMDISP_DEVICE *pDevice);
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiKmt.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiKmt.cpp	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/disp/wddm/VBoxUhgsmiKmt.cpp	(revision 49332)
@@ -71,6 +71,6 @@
     EnterCriticalSection(&pBuffer->CritSect);
 
-    int rc = vboxUhgsmiBaseLockData(pBuf, offLock, cbLock, fFlags,
-                                         &DdiLock.Flags, &DdiLock.NumPages, pBuffer->aLockPageIndices);
+    int rc = vboxUhgsmiBaseDxLockData(&pBuffer->BasePrivate, offLock, cbLock, fFlags,
+                                         &DdiLock.Flags, &DdiLock.NumPages);
     AssertRC(rc);
     if (RT_FAILURE(rc))
@@ -194,5 +194,5 @@
     PVBOXUHGSMI_PRIVATE_KMT pHg = VBOXUHGSMIKMT_GET(pHgsmi);
     UINT cbDmaCmd = pHg->Context.CommandBufferSize;
-    int rc = vboxUhgsmiBaseDmaFill(aBuffers, cBuffers,
+    int rc = vboxUhgsmiBaseDxDmaFill(aBuffers, cBuffers,
             pHg->Context.pCommandBuffer, &cbDmaCmd,
             pHg->Context.pAllocationList, pHg->Context.AllocationListSize,
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/Makefile.kmk	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/Makefile.kmk	(revision 49332)
@@ -118,4 +118,5 @@
  	common/VBoxMPVidModes.cpp \
  	$(PATH_ROOT)/src/VBox/Additions/common/VBoxVideo/HGSMIBase.cpp \
+ 	$(PATH_ROOT)/src/VBox/Additions/common/VBoxVideo/VBVABase.cpp \
  	$(PATH_ROOT)/src/VBox/Additions/common/VBoxVideo/Modesetting.cpp \
  	$(PATH_ROOT)/src/VBox/GuestHost/OpenGL/util/vreg.cpp
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPDevExt.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPDevExt.h	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/common/VBoxMPDevExt.h	(revision 49332)
@@ -208,9 +208,9 @@
 #ifdef VBOXWDDM_RENDER_FROM_SHADOW
     /* all memory layout info should be initialized */
-    Assert(pDevExt->aSources[0].Vbva.offVBVA);
+    Assert(pDevExt->aSources[0].Vbva.Vbva.offVRAMBuffer);
     /* page aligned */
-    Assert(!(pDevExt->aSources[0].Vbva.offVBVA & 0xfff));
-
-    return (ULONG)(pDevExt->aSources[0].Vbva.offVBVA & ~0xfffULL);
+    Assert(!(pDevExt->aSources[0].Vbva.Vbva.offVRAMBuffer & 0xfff));
+
+    return (ULONG)(pDevExt->aSources[0].Vbva.Vbva.offVRAMBuffer & ~0xfffULL);
 #else
     /* all memory layout info should be initialized */
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVbva.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVbva.cpp	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVbva.cpp	(revision 49332)
@@ -20,43 +20,4 @@
 #include "common/VBoxMPCommon.h"
 
-static int vboxVBVAInformHost (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO * pVbva, BOOL bEnable)
-{
-    int rc = VERR_NO_MEMORY;
-    void *p = VBoxHGSMIBufferAlloc (&VBoxCommonFromDeviceExt(pDevExt)->guestCtx,
-                                  sizeof (VBVAENABLE_EX),
-                                  HGSMI_CH_VBVA,
-                                  VBVA_ENABLE);
-    Assert(p);
-    if (!p)
-    {
-        LOGREL(("HGSMIHeapAlloc failed"));
-        rc = VERR_NO_MEMORY;
-    }
-    else
-    {
-        VBVAENABLE_EX *pEnableEx = (VBVAENABLE_EX *)p;
-        pEnableEx->u32ScreenId = pVbva->srcId;
-
-        VBVAENABLE *pEnable = &pEnableEx->Base;
-        pEnable->u32Flags  = bEnable? VBVA_F_ENABLE: VBVA_F_DISABLE;
-        pEnable->u32Flags |= VBVA_F_EXTENDED | VBVA_F_ABSOFFSET;
-        pEnable->u32Offset = (uint32_t)pVbva->offVBVA;
-        pEnable->i32Result = VERR_NOT_SUPPORTED;
-
-        VBoxHGSMIBufferSubmit (&VBoxCommonFromDeviceExt(pDevExt)->guestCtx, p);
-
-        if (bEnable)
-        {
-            rc = pEnable->i32Result;
-            AssertRC(rc);
-        }
-        else
-            rc = VINF_SUCCESS;
-
-        VBoxHGSMIBufferFree (&VBoxCommonFromDeviceExt(pDevExt)->guestCtx, p);
-    }
-    return rc;
-}
-
 /*
  * Public hardware buffer methods.
@@ -64,39 +25,16 @@
 int vboxVbvaEnable (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva)
 {
-        VBVABUFFER *pVBVA = pVbva->pVBVA;
+    if (VBoxVBVAEnable(&pVbva->Vbva, &VBoxCommonFromDeviceExt(pDevExt)->guestCtx,
+            pVbva->Vbva.pVBVA, pVbva->srcId))
+        return VINF_SUCCESS;
 
-//        DISPDBG((1, "VBoxDisp::vboxVbvaEnable screen %p vbva off 0x%x\n", ppdev->pjScreen, ppdev->layout.offVBVABuffer));
-
-        pVBVA->hostFlags.u32HostEvents      = 0;
-        pVBVA->hostFlags.u32SupportedOrders = 0;
-        pVBVA->off32Data          = 0;
-        pVBVA->off32Free          = 0;
-        RtlZeroMemory (pVBVA->aRecords, sizeof (pVBVA->aRecords));
-        pVBVA->indexRecordFirst   = 0;
-        pVBVA->indexRecordFree    = 0;
-        pVBVA->cbPartialWriteThreshold = 256;
-        pVBVA->cbData             = pVbva->cbVBVA - sizeof (VBVABUFFER) + sizeof (pVBVA->au8Data);
-
-        pVbva->fHwBufferOverflow = FALSE;
-        pVbva->pRecord           = NULL;
-
-        int rc = vboxVBVAInformHost (pDevExt, pVbva, TRUE);
-        AssertRC(rc);
-
-    if (!RT_SUCCESS(rc))
-        vboxVbvaDisable (pDevExt, pVbva);
-
-    return rc;
+    WARN(("VBoxVBVAEnable failed!"));
+    return VERR_GENERAL_FAILURE;
 }
 
 int vboxVbvaDisable (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva)
 {
-//    DISPDBG((1, "VBoxDisp::vbvaDisable called.\n"));
-
-    pVbva->fHwBufferOverflow = FALSE;
-    pVbva->pRecord           = NULL;
-//    ppdev->pVBVA             = NULL;
-
-    return vboxVBVAInformHost (pDevExt, pVbva, FALSE);
+    VBoxVBVADisable(&pVbva->Vbva, &VBoxCommonFromDeviceExt(pDevExt)->guestCtx, pVbva->srcId);
+    return VINF_SUCCESS;
 }
 
@@ -108,5 +46,5 @@
 
     int rc = VBoxMPCmnMapAdapterMemory(VBoxCommonFromDeviceExt(pDevExt),
-                                       (void**)&pVbva->pVBVA,
+                                       (void**)&pVbva->Vbva.pVBVA,
                                        offBuffer,
                                        cbBuffer);
@@ -114,7 +52,6 @@
     if (RT_SUCCESS(rc))
     {
-        Assert(pVbva->pVBVA);
-        pVbva->offVBVA = offBuffer;
-        pVbva->cbVBVA = cbBuffer;
+        Assert(pVbva->Vbva.pVBVA);
+        VBoxVBVASetupBufferContext(&pVbva->Vbva, offBuffer, cbBuffer);
         pVbva->srcId = srcId;
     }
@@ -126,222 +63,14 @@
 {
     int rc = VINF_SUCCESS;
-    VBoxMPCmnUnmapAdapterMemory(VBoxCommonFromDeviceExt(pDevExt), (void**)&pVbva->pVBVA);
-    memset(pVbva, 0, sizeof(VBOXVBVAINFO));
+    VBoxMPCmnUnmapAdapterMemory(VBoxCommonFromDeviceExt(pDevExt), (void**)&pVbva->Vbva.pVBVA);
+    memset(pVbva, 0, sizeof (VBOXVBVAINFO));
     return rc;
 }
 
-/*
- * Private operations.
- */
-static uint32_t vboxHwBufferAvail (const VBVABUFFER *pVBVA)
-{
-    int32_t i32Diff = pVBVA->off32Data - pVBVA->off32Free;
-
-    return i32Diff > 0? i32Diff: pVBVA->cbData + i32Diff;
-}
-
-static void vboxHwBufferFlush (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva)
-{
-    /* Issue the flush command. */
-    void *p = VBoxHGSMIBufferAlloc (&VBoxCommonFromDeviceExt(pDevExt)->guestCtx,
-                              sizeof (VBVAFLUSH),
-                              HGSMI_CH_VBVA,
-                              VBVA_FLUSH);
-    Assert(p);
-    if (!p)
-    {
-        LOGREL(("HGSMIHeapAlloc failed"));
-    }
-    else
-    {
-        VBVAFLUSH *pFlush = (VBVAFLUSH *)p;
-
-        pFlush->u32Reserved = 0;
-
-        VBoxHGSMIBufferSubmit (&VBoxCommonFromDeviceExt(pDevExt)->guestCtx, p);
-
-        VBoxHGSMIBufferFree (&VBoxCommonFromDeviceExt(pDevExt)->guestCtx, p);
-    }
-
-    return;
-}
-
-static void vboxHwBufferPlaceDataAt (VBVABUFFER *pVBVA, const void *p, uint32_t cb, uint32_t offset)
-{
-    uint32_t u32BytesTillBoundary = pVBVA->cbData - offset;
-    uint8_t  *dst                 = &pVBVA->au8Data[offset];
-    int32_t i32Diff               = cb - u32BytesTillBoundary;
-
-    if (i32Diff <= 0)
-    {
-        /* Chunk will not cross buffer boundary. */
-        memcpy (dst, p, cb);
-    }
-    else
-    {
-        /* Chunk crosses buffer boundary. */
-        memcpy (dst, p, u32BytesTillBoundary);
-        memcpy (&pVBVA->au8Data[0], (uint8_t *)p + u32BytesTillBoundary, i32Diff);
-    }
-
-    return;
-}
-
-BOOL vboxVbvaBufferBeginUpdate (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva)
-{
-    BOOL bRc = FALSE;
-
-    // DISPDBG((1, "VBoxDisp::vboxHwBufferBeginUpdate called flags = 0x%08X\n",
-    //          ppdev->pVBVA? ppdev->pVBVA->u32HostEvents: -1));
-
-    if (   pVbva->pVBVA
-        && (pVbva->pVBVA->hostFlags.u32HostEvents & VBVA_F_MODE_ENABLED))
-    {
-        uint32_t indexRecordNext;
-
-        Assert (!pVbva->fHwBufferOverflow);
-        Assert (pVbva->pRecord == NULL);
-
-        indexRecordNext = (pVbva->pVBVA->indexRecordFree + 1) % VBVA_MAX_RECORDS;
-
-        if (indexRecordNext == pVbva->pVBVA->indexRecordFirst)
-        {
-            /* All slots in the records queue are used. */
-            vboxHwBufferFlush (pDevExt, pVbva);
-        }
-
-        if (indexRecordNext == pVbva->pVBVA->indexRecordFirst)
-        {
-//            /* Even after flush there is no place. Fail the request. */
-//            LOG(("no space in the queue of records!!! first %d, last %d",
-//                  ppdev->pVBVA->indexRecordFirst, ppdev->pVBVA->indexRecordFree));
-        }
-        else
-        {
-            /* Initialize the record. */
-            VBVARECORD *pRecord = &pVbva->pVBVA->aRecords[pVbva->pVBVA->indexRecordFree];
-
-            pRecord->cbRecord = VBVA_F_RECORD_PARTIAL;
-
-            pVbva->pVBVA->indexRecordFree = indexRecordNext;
-
-            // LOG(("indexRecordNext = %d\n", indexRecordNext));
-
-            /* Remember which record we are using. */
-            pVbva->pRecord = pRecord;
-
-            bRc = TRUE;
-        }
-    }
-
-    return bRc;
-}
-
-void vboxVbvaBufferEndUpdate (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva)
-{
-    VBVARECORD *pRecord;
-
-    // LOG(("VBoxDisp::vboxHwBufferEndUpdate called"));
-
-    Assert(pVbva->pVBVA);
-
-    pRecord = pVbva->pRecord;
-    Assert (pRecord && (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL));
-
-    /* Mark the record completed. */
-    pRecord->cbRecord &= ~VBVA_F_RECORD_PARTIAL;
-
-    pVbva->fHwBufferOverflow = FALSE;
-    pVbva->pRecord = NULL;
-
-    return;
-}
-
-static int vboxHwBufferWrite (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva, const void *p, uint32_t cb)
-{
-    VBVARECORD *pRecord;
-    uint32_t cbHwBufferAvail;
-
-    uint32_t cbWritten = 0;
-
-    VBVABUFFER *pVBVA = pVbva->pVBVA;
-    Assert(pVBVA);
-
-    if (!pVBVA || pVbva->fHwBufferOverflow)
-    {
-        return VERR_INVALID_STATE;
-    }
-
-    Assert (pVBVA->indexRecordFirst != pVBVA->indexRecordFree);
-
-    pRecord = pVbva->pRecord;
-    Assert (pRecord && (pRecord->cbRecord & VBVA_F_RECORD_PARTIAL));
-
-//    LOGF(("VW %d", cb));
-
-    cbHwBufferAvail = vboxHwBufferAvail (pVBVA);
-
-    while (cb > 0)
-    {
-        uint32_t cbChunk = cb;
-
-        // LOG(("pVBVA->off32Free %d, pRecord->cbRecord 0x%08X, cbHwBufferAvail %d, cb %d, cbWritten %d\n",
-        //      pVBVA->off32Free, pRecord->cbRecord, cbHwBufferAvail, cb, cbWritten));
-
-        if (cbChunk >= cbHwBufferAvail)
-        {
-            LOG(("1) avail %d, chunk %d", cbHwBufferAvail, cbChunk));
-
-            vboxHwBufferFlush (pDevExt, pVbva);
-
-            cbHwBufferAvail = vboxHwBufferAvail (pVBVA);
-
-            if (cbChunk >= cbHwBufferAvail)
-            {
-                LOG(("no place for %d bytes. Only %d bytes available after flush. Going to partial writes.",
-                     cb, cbHwBufferAvail));
-
-                if (cbHwBufferAvail <= pVBVA->cbPartialWriteThreshold)
-                {
-                    LOGREL(("Buffer overflow!!!"));
-                    pVbva->fHwBufferOverflow = TRUE;
-                    Assert(FALSE);
-                    return VERR_NO_MEMORY;
-                }
-
-                cbChunk = cbHwBufferAvail - pVBVA->cbPartialWriteThreshold;
-            }
-        }
-
-        Assert(cbChunk <= cb);
-        Assert(cbChunk <= vboxHwBufferAvail (pVBVA));
-
-        vboxHwBufferPlaceDataAt (pVbva->pVBVA, (uint8_t *)p + cbWritten, cbChunk, pVBVA->off32Free);
-
-        pVBVA->off32Free   = (pVBVA->off32Free + cbChunk) % pVBVA->cbData;
-        pRecord->cbRecord += cbChunk;
-        cbHwBufferAvail -= cbChunk;
-
-        cb        -= cbChunk;
-        cbWritten += cbChunk;
-    }
-
-    return VINF_SUCCESS;
-}
-
-/*
- * Public writer to the hardware buffer.
- */
-int vboxWrite (PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva, const void *pv, uint32_t cb)
-{
-    return vboxHwBufferWrite (pDevExt, pVbva, pv, cb);
-}
-
-
 int vboxVbvaReportDirtyRect (PVBOXMP_DEVEXT pDevExt, PVBOXWDDM_SOURCE pSrc, RECT *pRectOrig)
 {
-        VBVACMDHDR hdr;
+    VBVACMDHDR hdr;
 
-        RECT rect = *pRectOrig;
+    RECT rect = *pRectOrig;
 
 //        if (rect.left < 0) rect.left = 0;
@@ -350,13 +79,17 @@
 //        if (rect.bottom > (int)ppdev->cyScreen) rect.bottom = ppdev->cyScreen;
 
-        hdr.x = (int16_t)rect.left;
-        hdr.y = (int16_t)rect.top;
-        hdr.w = (uint16_t)(rect.right - rect.left);
-        hdr.h = (uint16_t)(rect.bottom - rect.top);
+    hdr.x = (int16_t)rect.left;
+    hdr.y = (int16_t)rect.top;
+    hdr.w = (uint16_t)(rect.right - rect.left);
+    hdr.h = (uint16_t)(rect.bottom - rect.top);
 
-        hdr.x += (int16_t)pSrc->VScreenPos.x;
-        hdr.y += (int16_t)pSrc->VScreenPos.y;
+    hdr.x += (int16_t)pSrc->VScreenPos.x;
+    hdr.y += (int16_t)pSrc->VScreenPos.y;
 
-        return vboxWrite (pDevExt, &pSrc->Vbva, &hdr, sizeof(hdr));
+    if (VBoxVBVAWrite(&pSrc->Vbva.Vbva, &VBoxCommonFromDeviceExt(pDevExt)->guestCtx, &hdr, sizeof(hdr)))
+        return VINF_SUCCESS;
+
+    WARN(("VBoxVBVAWrite failed"));
+    return VERR_GENERAL_FAILURE;
 }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVbva.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVbva.h	(revision 49331)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Video/mp/wddm/VBoxMPVbva.h	(revision 49332)
@@ -22,9 +22,5 @@
 typedef struct VBOXVBVAINFO
 {
-    VBOXVIDEOOFFSET offVBVA;
-    uint32_t cbVBVA;
-    VBVABUFFER *pVBVA;
-    BOOL  fHwBufferOverflow;
-    VBVARECORD *pRecord;
+    VBVABUFFERCONTEXT Vbva;
     D3DDDI_VIDEO_PRESENT_SOURCE_ID srcId;
     KSPIN_LOCK Lock;
@@ -37,13 +33,11 @@
 int vboxVbvaReportCmdOffset(PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva, uint32_t offCmd);
 int vboxVbvaReportDirtyRect(PVBOXMP_DEVEXT pDevExt, struct VBOXWDDM_SOURCE *pSrc, RECT *pRectOrig);
-BOOL vboxVbvaBufferBeginUpdate(PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva);
-void vboxVbvaBufferEndUpdate(PVBOXMP_DEVEXT pDevExt, VBOXVBVAINFO *pVbva);
 
 #define VBOXVBVA_OP(_op, _pdext, _psrc, _arg) \
         do { \
-            if (vboxVbvaBufferBeginUpdate(_pdext, &(_psrc)->Vbva)) \
+            if (VBoxVBVABufferBeginUpdate(&(_psrc)->Vbva.Vbva, &VBoxCommonFromDeviceExt(_pdext)->guestCtx)) \
             { \
                 vboxVbva##_op(_pdext, _psrc, _arg); \
-                vboxVbvaBufferEndUpdate(_pdext, &(_psrc)->Vbva); \
+                VBoxVBVABufferEndUpdate(&(_psrc)->Vbva.Vbva); \
             } \
         } while (0)
