Index: /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 32060)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxDispD3D.cpp	(revision 32061)
@@ -1359,4 +1359,21 @@
 }
 
+static CRITICAL_SECTION g_VBoxCritSect;
+
+void vboxDispLock()
+{
+    EnterCriticalSection(&g_VBoxCritSect);
+}
+
+void vboxDispUnlock()
+{
+    LeaveCriticalSection(&g_VBoxCritSect);
+}
+
+void vboxDispLockInit()
+{
+    InitializeCriticalSection(&g_VBoxCritSect);
+}
+
 /**
  * DLL entry point.
@@ -1370,4 +1387,6 @@
         case DLL_PROCESS_ATTACH:
         {
+            vboxDispLockInit();
+
             vboxVDbgPrint(("VBoxDispD3D: DLL loaded.\n"));
 #ifdef VBOXWDDMDISP_DEBUG
@@ -1386,6 +1405,6 @@
 #endif
                 {
-                    hr = VBoxScreenMRunnerStart(&g_VBoxScreenMonRunner);
-                    Assert(hr == S_OK);
+//                    hr = VBoxScreenMRunnerStart(&g_VBoxScreenMonRunner);
+//                    Assert(hr == S_OK);
                     /* succeed in any way */
                     hr = S_OK;
@@ -1410,7 +1429,8 @@
             vboxVDbgVEHandlerUnregister();
 #endif
-            HRESULT hr = VBoxScreenMRunnerStop(&g_VBoxScreenMonRunner);
-            Assert(hr == S_OK);
-            if (hr == S_OK)
+            HRESULT hr = S_OK;
+//            hr = VBoxScreenMRunnerStop(&g_VBoxScreenMonRunner);
+//            Assert(hr == S_OK);
+//            if (hr == S_OK)
             {
 #ifdef VBOXDISPMP_TEST
@@ -1850,4 +1870,12 @@
         Assert(pRc->aAllocations[0].enmD3DIfType == VBOXDISP_D3DIFTYPE_TEXTURE);
         pD3DIfTex = (IDirect3DTexture9*)pRc->aAllocations[0].pD3DIf;
+#ifdef DEBUG_misha
+        bool bDo = false;
+
+        if (bDo)
+        {
+            vboxVDbgDumpSurfData((pDevice, "SetTexture:\n", pRc, 0 /* alloc index*/, NULL, NULL, "\n"));
+        }
+#endif
     }
     else
@@ -5722,14 +5750,22 @@
     vboxVDbgPrint(("==> "__FUNCTION__"\n"));
 
+//    vboxDispLock();
+
+    HRESULT hr = E_FAIL;
+
+    do
+    {
+
     VBOXWDDM_QI Query;
     D3DDDICB_QUERYADAPTERINFO DdiQuery;
     DdiQuery.PrivateDriverDataSize = sizeof(Query);
     DdiQuery.pPrivateDriverData = &Query;
-    HRESULT hr = pOpenData->pAdapterCallbacks->pfnQueryAdapterInfoCb(pOpenData->hAdapter, &DdiQuery);
+    hr = pOpenData->pAdapterCallbacks->pfnQueryAdapterInfoCb(pOpenData->hAdapter, &DdiQuery);
     Assert(hr == S_OK);
     if (hr != S_OK)
     {
         vboxVDbgPrintR((__FUNCTION__": pfnQueryAdapterInfoCb failed, hr (%d)\n", hr));
-        return E_FAIL;
+        hr = E_FAIL;
+        break;
     }
 
@@ -5740,5 +5776,6 @@
                 VBOXVIDEOIF_VERSION,
                 Query.u32Version));
-        return E_FAIL;
+        hr = E_FAIL;
+        break;
     }
 
@@ -5782,4 +5819,5 @@
                 if (hr == S_OK)
                 {
+//                    Assert(0);
                     hr = pAdapter->D3D.pfnDirect3DCreate9Ex(D3D_SDK_VERSION, &pAdapter->pD3D9If);
                     Assert(hr == S_OK);
@@ -5823,4 +5861,8 @@
     }
 
+    } while (0);
+
+//    vboxDispUnlock();
+
     vboxVDbgPrint(("<== "__FUNCTION__", hr (%d)\n", hr));
 
@@ -5851,4 +5893,12 @@
         Assert(pRect->bottom > pRect->top);
         vboxVDbgMpPrintRect((pDevice, "rect: ", pRect, "\n"));
+    }
+
+    BOOL bReleaseSurf = false;
+    if (!pSurf)
+    {
+        HRESULT tmpHr = vboxWddmSurfGet(pRc, iAlloc, &pSurf);
+        Assert(tmpHr == S_OK);
+        bReleaseSurf = TRUE;
     }
     HRESULT srcHr = pSurf->LockRect(&Lr, NULL, D3DLOCK_READONLY);
@@ -5876,4 +5926,7 @@
         vboxVDbgMpPrint((pDevice, "%s\n", pSuffix));
     }
+
+    if (bReleaseSurf)
+        pSurf->Release();
 }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxScreen.cpp
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxScreen.cpp	(revision 32060)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxScreen.cpp	(revision 32061)
@@ -479,4 +479,15 @@
 {
     PVBOXSCREENMONRUNNER pRunner = (PVBOXSCREENMONRUNNER)pvUser;
+    Assert(0);
+
+    BOOL bRc = SetEvent(pRunner->hEvent);
+    if (!bRc)
+    {
+        DWORD winErr = GetLastError();
+        Log((__FUNCTION__": SetEvent failed, winErr = (%d)", winErr));
+        HRESULT tmpHr = HRESULT_FROM_WIN32(winErr);
+        Assert(0);
+        Assert(tmpHr != S_OK);
+    }
 
     HRESULT hr = vboxScreenMonInit();
@@ -497,18 +508,40 @@
     memset(pMon, 0, sizeof (VBOXSCREENMONRUNNER));
 
-    pMon->hThread = CreateThread(NULL /* LPSECURITY_ATTRIBUTES lpThreadAttributes */,
-                                          0 /* SIZE_T dwStackSize */,
-                                          vboxScreenMRunnerThread,
-                                          pMon,
-                                          0 /* DWORD dwCreationFlags */,
-                                          &pMon->idThread);
-    if (pMon->hThread)
-        return S_OK;
-
-    DWORD winErr = GetLastError();
-    Log((__FUNCTION__": CreateThread failed, winErr = (%d)", winErr));
-    hr = HRESULT_FROM_WIN32(winErr);
-    Assert(0);
-    Assert(hr != S_OK);
+    pMon->hEvent = CreateEvent(NULL, /* LPSECURITY_ATTRIBUTES lpEventAttributes*/
+            FALSE, /* BOOL bManualReset*/
+            FALSE, /* BOOL bInitialState */
+            NULL /* LPCTSTR lpName */
+          );
+    if (pMon->hEvent)
+    {
+        pMon->hThread = CreateThread(NULL /* LPSECURITY_ATTRIBUTES lpThreadAttributes */,
+                                              0 /* SIZE_T dwStackSize */,
+                                              vboxScreenMRunnerThread,
+                                              pMon,
+                                              0 /* DWORD dwCreationFlags */,
+                                              &pMon->idThread);
+        if (pMon->hThread)
+        {
+            Assert(0);
+            return S_OK;
+        }
+        else
+        {
+            DWORD winErr = GetLastError();
+            Log((__FUNCTION__": CreateThread failed, winErr = (%d)", winErr));
+            hr = HRESULT_FROM_WIN32(winErr);
+            Assert(0);
+            Assert(hr != S_OK);
+        }
+        CloseHandle(pMon->hEvent);
+    }
+    else
+    {
+        DWORD winErr = GetLastError();
+        Log((__FUNCTION__": CreateEvent failed, winErr = (%d)", winErr));
+        hr = HRESULT_FROM_WIN32(winErr);
+        Assert(0);
+        Assert(hr != S_OK);
+    }
 
     return hr;
@@ -520,31 +553,62 @@
         return S_OK;
 
-    BOOL bResult = PostThreadMessage(pMon->idThread, WM_QUIT, 0, 0);
-    HRESULT hr = S_OK;
-    if (bResult)
-    {
-        DWORD dwErr = WaitForSingleObject(pMon->hThread, INFINITE);
-        if (dwErr == WAIT_OBJECT_0)
-        {
-            CloseHandle(pMon->hThread);
-            pMon->hThread = 0;
+    Assert(0);
+
+    HANDLE ahHandles[2];
+    ahHandles[0] = pMon->hThread;
+    ahHandles[1] = pMon->hEvent;
+    DWORD dwResult = WaitForMultipleObjects(2, ahHandles,
+      FALSE, /* BOOL bWaitAll */
+      INFINITE /* DWORD dwMilliseconds */
+    );
+    HRESULT hr = E_FAIL;
+    if (dwResult == WAIT_OBJECT_0 + 1) /* Event is signaled */
+    {
+        BOOL bResult = PostThreadMessage(pMon->idThread, WM_QUIT, 0, 0);
+        if (bResult)
+        {
+            DWORD dwErr = WaitForSingleObject(pMon->hThread, INFINITE);
+            if (dwErr == WAIT_OBJECT_0)
+            {
+                hr = S_OK;
+            }
+            else
+            {
+                DWORD winErr = GetLastError();
+                hr = HRESULT_FROM_WIN32(winErr);
+                Assert(0);
+            }
         }
         else
         {
             DWORD winErr = GetLastError();
-            hr = HRESULT_FROM_WIN32(winErr);
-            Assert(0);
-        }
+            Assert(winErr != ERROR_SUCCESS);
+            if (winErr == ERROR_INVALID_THREAD_ID)
+            {
+                hr = S_OK;
+            }
+            else
+            {
+                hr = HRESULT_FROM_WIN32(winErr);
+                Assert(0);
+            }
+        }
+    }
+    else if (dwResult == WAIT_OBJECT_0)
+    {
+        /* thread has terminated already */
+        hr = S_OK;
     }
     else
     {
-        DWORD winErr = GetLastError();
-        Assert(winErr != ERROR_SUCCESS);
-        if (winErr != ERROR_INVALID_THREAD_ID)
-        {
-            hr = HRESULT_FROM_WIN32(winErr);
-            Assert(0);
-        }
-        /* else - treat as OK */
+        Assert(0);
+    }
+
+    if (hr == S_OK)
+    {
+        CloseHandle(pMon->hThread);
+        pMon->hThread = 0;
+        CloseHandle(pMon->hEvent);
+        pMon->hThread = 0;
     }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxScreen.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxScreen.h	(revision 32060)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Display/wddm/VBoxScreen.h	(revision 32061)
@@ -56,4 +56,5 @@
     HANDLE hThread;
     DWORD idThread;
+    HANDLE hEvent;
 } VBOXSCREENMONRUNNER, *PVBOXSCREENMONRUNNER;
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 32060)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 32061)
@@ -482,10 +482,4 @@
         return hr;
     }
-#ifdef VBOXWDDM
-    if (shared_handle && !*shared_handle)
-    {
-        *shared_handle = VBOXSHRC_GET_SHAREHANDLE(surface);
-    }
-#endif
 
     return hr;
@@ -497,5 +491,5 @@
 
 #if defined(DEBUG_misha) && defined (VBOXWDDM)
-    if (VBOXSHRC_IS_SHARED(This))
+    if (VBOXSHRC_IS_INITIALIZED(This))
     {
         Assert(0);
@@ -2025,15 +2019,20 @@
     ENTER_GL();
     glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
-    if(This->texture_name)
-    {
-        surface_bind_and_dirtify(This, FALSE);
-        glTexImage2D(This->texture_target, This->texture_level,
-                     GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
-    }
-    if(This->texture_name_srgb)
-    {
-        surface_bind_and_dirtify(This, TRUE);
-        glTexImage2D(This->texture_target, This->texture_level,
-                     GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+#ifdef VBOXWDDM
+    if (!VBOXSHRC_IS_SHARED_OPENED(This))
+#endif
+    {
+        if(This->texture_name)
+        {
+            surface_bind_and_dirtify(This, FALSE);
+            glTexImage2D(This->texture_target, This->texture_level,
+                         GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+        }
+        if(This->texture_name_srgb)
+        {
+            surface_bind_and_dirtify(This, TRUE);
+            glTexImage2D(This->texture_target, This->texture_level,
+                         GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+        }
     }
     glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
@@ -4280,11 +4279,4 @@
         if(((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) ||
            ((This->Flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX))) {
-#ifdef VBOXWDDM
-            if (VBOXSHRC_IS_SHARED(This))
-            {
-                IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
-            }
-            else
-#endif
             if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
                 TRACE("Passing to container\n");
@@ -4293,4 +4285,5 @@
             }
         }
+
         This->Flags &= ~SFLAG_LOCATIONS;
         This->Flags |= flag;
@@ -4304,11 +4297,4 @@
     } else {
         if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))) {
-#ifdef VBOXWDDM
-            if (VBOXSHRC_IS_SHARED(This))
-            {
-                IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
-            }
-            else
-#endif
             if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
                 TRACE("Passing to container\n");
@@ -4317,6 +4303,22 @@
             }
         }
+
         This->Flags &= ~flag;
     }
+
+#ifdef VBOXWDDM
+    if(VBOXSHRC_IS_INITIALIZED(This)) {
+        /* with the shared resource only texture can be considered valid
+         * to make sure changes done to the resource in the other device context are visible
+         * because the resource contents is shared via texture.
+         * This is why we ensure texture location is the one and only which is always valid */
+        if(!(This->Flags & SFLAG_INTEXTURE)) {
+            IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
+        } else {
+            This->Flags &= ~SFLAG_LOCATIONS;
+            This->Flags |= SFLAG_INTEXTURE;
+        }
+    }
+#endif
 
     if(!(This->Flags & SFLAG_LOCATIONS)) {
@@ -4619,11 +4621,32 @@
     }
 
-    if(rect == NULL) {
-        This->Flags |= flag;
-    }
-
-    if (in_fbo && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
-        /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
-        This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
+#ifdef VBOXWDDM
+    if (VBOXSHRC_IS_INITIALIZED(This))
+    {
+        /* with the shared resource only texture can be considered valid
+         * to make sure changes done to the resource in the other device context are visible
+         * because the resource contents is shared via texture.
+         * One can load and use other locations as needed,
+         * but they should be reloaded each time on each usage */
+        Assert(!!(This->Flags & SFLAG_INTEXTURE) || !!(flag & SFLAG_INTEXTURE));
+        This->Flags &= ~SFLAG_LOCATIONS;
+        This->Flags |= SFLAG_INTEXTURE;
+        /* @todo: SFLAG_INSRGBTEX ?? */
+//        if (in_fbo)
+//        {
+//            This->Flags |= SFLAG_INDRAWABLE;
+//        }
+    }
+    else
+#endif
+    {
+        if(rect == NULL) {
+            This->Flags |= flag;
+        }
+
+        if (in_fbo && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
+            /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
+            This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
+        }
     }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 32060)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/texture.c	(revision 32061)
@@ -620,4 +620,5 @@
     {
         Assert(shared_handle);
+        VBOXSHRC_SET_INITIALIZED(texture);
         IWineD3DSurface_LoadLocation(texture->surfaces[0], SFLAG_INTEXTURE, NULL);
         if (!VBOXSHRC_IS_SHARED_OPENED(texture))
@@ -629,4 +630,5 @@
         {
             Assert(*shared_handle);
+            Assert(*shared_handle == VBOXSHRC_GET_SHAREHANDLE(texture));
         }
 #ifdef DEBUG
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 32060)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/vboxsharedrc.h	(revision 32061)
@@ -17,12 +17,15 @@
 #define ___vboxsharedrc_h___
 
-#define VBOXSHRC_F_SHARED         0x00000001 /* shared rc */
-#define VBOXSHRC_F_SHARED_OPENED  0x00000002 /* if set shared rc is opened, otherwise it is created */
+#define VBOXSHRC_F_SHARED              0x00000001 /* shared rc */
+#define VBOXSHRC_F_SHARED_OPENED       0x00000002 /* if set shared rc is opened, otherwise it is created */
+#define VBOXSHRC_F_INITIALIZED         0x00000004 /* set once shared rc is initialized */
 
 #define VBOXSHRC_GET_SHAREFLAFS(_o) ((_o)->resource.sharerc_flags)
 #define VBOXSHRC_GET_SHAREHANDLE(_o) ((_o)->resource.sharerc_handle)
 #define VBOXSHRC_SET_SHAREHANDLE(_o, _h) ((_o)->resource.sharerc_handle = (_h))
+#define VBOXSHRC_SET_INITIALIZED(_o) (VBOXSHRC_GET_SHAREFLAFS(_o) |= VBOXSHRC_F_INITIALIZED)
 #define VBOXSHRC_IS_SHARED(_o) (!!(VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_SHARED))
 #define VBOXSHRC_IS_SHARED_OPENED(_o) (!!(VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_SHARED_OPENED))
+#define VBOXSHRC_IS_INITIALIZED(_o) (!!(VBOXSHRC_GET_SHAREFLAFS(_o) & VBOXSHRC_F_INITIALIZED))
 
 #ifdef DEBUG_misha
