Index: /trunk/include/VBox/HostServices/VBoxCrOpenGLSvc.h
===================================================================
--- /trunk/include/VBox/HostServices/VBoxCrOpenGLSvc.h	(revision 30439)
+++ /trunk/include/VBox/HostServices/VBoxCrOpenGLSvc.h	(revision 30440)
@@ -43,4 +43,5 @@
 #define SHCRGL_GUEST_FN_WRITE_READ  (4)
 #define SHCRGL_GUEST_FN_SET_VERSION (6)
+#define SHCRGL_GUEST_FN_INJECT      (9)
 
 /* Parameters count */
@@ -53,4 +54,5 @@
 #define SHCRGL_CPARMS_SET_VERSION (2)
 #define SHCRGL_CPARMS_SCREEN_CHANGED (1)
+#define SHCRGL_CPARMS_INJECT (2)
 
 /**
@@ -125,3 +127,18 @@
 } CRVBOXHGCMSETVERSION;
 
+/** GUEST_FN_INJECT Parameters structure. */
+typedef struct
+{
+    VBoxGuestHGCMCallInfo   hdr;
+
+    /** 32bit, in
+     *  ClientID to inject commands buffer for
+     */
+    HGCMFunctionParameter   u32ClientID;
+    /** pointer, in
+     *  Data buffer
+     */
+    HGCMFunctionParameter   pBuffer;
+} CRVBOXHGCMINJECT;
+
 #endif
Index: /trunk/src/VBox/Additions/common/crOpenGL/Makefile.kmk
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/Makefile.kmk	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/Makefile.kmk	(revision 30440)
@@ -130,5 +130,5 @@
    VBoxOGL_SOURCES.solaris += \
  	$(VBOX_PATH_CROGL_GENFILES)/solaris_glxapi_exports.asm \
- 	$(VBOX_PATH_CROGL_GENFILES)/solaris_exports_dri.asm \
+ 	$(VBOX_PATH_CROGL_GENFILES)/solaris_exports_dri.asm
    VBoxOGL_SOURCES.linux += \
  	$(VBOX_PATH_CROGL_GENFILES)/linux_glxapi_exports.asm \
@@ -136,6 +136,5 @@
    VBoxOGL_SOURCES.freebsd += \
  	$(VBOX_PATH_CROGL_GENFILES)/freebsd_glxapi_exports.asm \
- 	$(VBOX_PATH_CROGL_GENFILES)/freebsd_exports_dri.asm \
-
+ 	$(VBOX_PATH_CROGL_GENFILES)/freebsd_exports_dri.asm
   else
    VBoxOGL_SOURCES.solaris += \
Index: /trunk/src/VBox/Additions/common/crOpenGL/context.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/context.c	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/context.c	(revision 30440)
@@ -140,4 +140,7 @@
     winInfo->cVisibleRegions = 0;
 #endif
+#ifdef CR_NEWWINTRACK
+    winInfo->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
+#endif
     winInfo->spuWindow = spuWin;
 
@@ -158,5 +161,7 @@
     if (win->dpy) {
     XWindowAttributes attr;
+    XLOCK(win->dpy);
     XGetWindowAttributes(win->dpy, win->drawable, &attr);
+    XUNLOCK(win->dpy);
     return (attr.map_state != IsUnmapped);
     }
@@ -216,4 +221,7 @@
     winInfo->mapped = -1; /* don't know */
     winInfo->pOwner = NULL;
+#ifdef CR_NEWWINTRACK
+    winInfo->u32ClientID = -1;
+#endif
 #ifndef WINDOWS
     crHashtableAdd(stub.windowTable, (unsigned int) drawable, winInfo);
@@ -570,4 +578,9 @@
     //       Disabling those tripples glxgears fps, thus using xevens instead of per frame polling is much more preffered.
     //@todo: Check similiar on windows guests, though doubtfull as there're no XSync like calls on windows.
+    if (window && window->dpy)
+    {
+        XLOCK(window->dpy);
+    }
+
     if (!window
         || !window->dpy
@@ -581,4 +594,9 @@
         *x = *y = 0;
         *w = *h = 0;
+    }
+
+    if (window && window->dpy)
+    {
+        XUNLOCK(window->dpy);
     }
 }
@@ -638,4 +656,7 @@
     unsigned int mask;
     int x, y;
+
+    XLOCK(window->dpy);
+
     Bool q = XQueryPointer(window->dpy, window->drawable, &root, &child,
                                                  &rootX, &rootY, &pos[0], &pos[1], &mask);
@@ -649,4 +670,6 @@
         pos[0] = pos[1] = 0;
     }
+
+    XUNLOCK(window->dpy);
 }
 
@@ -854,4 +877,8 @@
                 /*crDebug("(1)stubMakeCurrent ctx=%p(%i) window=%p(%i)", context, context->spuContext, window, window->spuWindow);*/
                 window->spuWindow = stub.spu->dispatch_table.WindowCreate( window->dpyName, context->visBits );
+#ifdef CR_NEWWINTRACK
+                window->u32ClientID = stub.spu->dispatch_table.VBoxPackGetInjectID();
+#endif
+
             }
         }
@@ -922,8 +949,10 @@
                         unsigned int border, depth, w, h;
 
+                        XLOCK(context->currentDrawable->dpy);
                         if (!XGetGeometry(context->currentDrawable->dpy, context->currentDrawable->drawable, &root, &x, &y, &w, &h, &border, &depth))
                         {
                             crWindowDestroy((GLint)context->currentDrawable->drawable);
                         }
+                        XUNLOCK(context->currentDrawable->dpy);
 #endif
                     
@@ -980,4 +1009,6 @@
         if (stub.trackWindowSize)
             stub.spuDispatch.WindowSize( window->spuWindow, winW, winH );
+        if (stub.trackWindowPos)
+            stub.spuDispatch.WindowPosition(window->spuWindow, x, y);
         if (winW > 0 && winH > 0)
             stub.spu->dispatch_table.Viewport( 0, 0, winW, winH );
Index: /trunk/src/VBox/Additions/common/crOpenGL/fakedri_drv.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/fakedri_drv.c	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/fakedri_drv.c	(revision 30440)
@@ -334,10 +334,4 @@
 void __attribute__ ((constructor)) vbox_install_into_mesa(void)
 {
-    if (!stubInit())
-    {
-        crDebug("vboxdriInitScreen: stubInit failed");
-        return;
-    }
-
     {
         void (*pxf86Msg)(MessageType type, const char *format, ...) _printf_attribute(2,3);
@@ -354,4 +348,10 @@
             return;
         }
+    }
+
+    if (!stubInit())
+    {
+        crDebug("vboxdriInitScreen: stubInit failed");
+        return;
     }
 
Index: /trunk/src/VBox/Additions/common/crOpenGL/glx.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/glx.c	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/glx.c	(revision 30440)
@@ -457,6 +457,8 @@
         return NULL;
 
+    XLOCK(dpy);
     searchvis.visualid = XVisualIDFromVisual(DefaultVisual(dpy, screen));
     pret = XGetVisualInfo(dpy, VisualIDMask, &searchvis, &nvisuals);
+    XUNLOCK(dpy);
       
     if (nvisuals!=1) crWarning("glXChooseVisual: XGetVisualInfo returned %i visuals for %x", nvisuals, (unsigned int) searchvis.visualid);
@@ -644,5 +646,7 @@
 
         if (context && context->type == UNDECIDED) {
+            XLOCK(dpy);
             XSync(dpy, 0); /* sync to force window creation on the server */
+            XUNLOCK(dpy);
         }
     }
@@ -1452,4 +1456,5 @@
     if (pGlxPixmap)
     {
+        XLOCK(dpy);
         if (pGlxPixmap->gc)
         {
@@ -1461,4 +1466,5 @@
             XFreePixmap(dpy, pGlxPixmap->hShmPixmap);
         }
+        XUNLOCK(dpy);
 
         if (pGlxPixmap->hDamage>0)
@@ -1637,5 +1643,7 @@
     /*@todo doesn't really list all the common visuals, have to use some static list*/
     searchvis.screen = screen;
+    XLOCK(dpy);
     pVisuals = XGetVisualInfo(dpy, VisualScreenMask, &searchvis, nelements);
+    XUNLOCK(dpy);
 
     if (*nelements)
@@ -1664,5 +1672,7 @@
 
     *nelements = 1;
+    XLOCK(dpy);
     *pGLXFBConfigs = (GLXFBConfig) XVisualIDFromVisual(DefaultVisual(dpy, screen));
+    XUNLOCK(dpy);
 
     crDebug("glXGetFBConfigs returned %i configs", *nelements);
@@ -1712,5 +1722,7 @@
 
         temp.visualid = (VisualID)config;
+        XLOCK(dpy);
         pret = XGetVisualInfo(dpy, VisualIDMask, &temp, &nret);
+        XUNLOCK(dpy);
         
         if (nret!=1) crWarning("XGetVisualInfo returned %i visuals for %p", nret, config);
@@ -1781,8 +1793,9 @@
 
     /* Check for extension and pixmaps format */
-
+    XLOCK(dpy);
     if (!XShmQueryExtension(dpy))
     {
         crWarning("No XSHM extension");
+        XUNLOCK(dpy);
         return;
     }
@@ -1791,4 +1804,5 @@
     {
         crWarning("XSHM extension doesn't support pixmaps");
+        XUNLOCK(dpy);
         return;
     }
@@ -1797,6 +1811,8 @@
     {
         crWarning("XSHM extension doesn't support ZPixmap format");
+        XUNLOCK(dpy);
         return;
     }
+    XUNLOCK(dpy);
 
     /* Alloc shared memory, so far using hardcoded value...could fail for bigger displays one day */
@@ -1817,5 +1833,5 @@
     }
 
-
+    XLOCK(dpy);
     if (!XShmAttach(dpy, &stub.xshmSI))
     {
@@ -1823,6 +1839,8 @@
         shmctl(stub.xshmSI.shmid, IPC_RMID, 0);
         shmdt(stub.xshmSI.shmaddr);
+        XUNLOCK(dpy);
         return;
     }
+    XUNLOCK(dpy);
 
     stub.bShmInitFailed = GL_FALSE;
@@ -1915,4 +1933,5 @@
     CRASSERT(pContext && pCreateInfoPixmap);
 
+    XLOCK(dpy);
     if (!XGetGeometry(dpy, (Pixmap)draw, &root, &x, &y, &w, &h, &border, &depth))
     {
@@ -1921,4 +1940,5 @@
         {
             crWarning("stubInitGlxPixmap failed in call to XGetGeometry for 0x%x", (int) draw);
+            XUNLOCK(dpy);
             return NULL;
         }
@@ -1929,4 +1949,5 @@
     {
         crWarning("stubInitGlxPixmap failed to allocate memory");
+        XUNLOCK(dpy);
         return NULL;
     }
@@ -1965,4 +1986,5 @@
         pGlxPixmap->hShmPixmap = 0;
     }
+    XUNLOCK(dpy);
 
     stubInitXDamageExtension(pContext);
@@ -2036,8 +2058,10 @@
     else
     {
+        XLOCK(dpy);
         XCopyArea(dpy, (Pixmap)draw, pGlxPixmap->hShmPixmap, pGlxPixmap->gc, 
                   pGlxPixmap->x, pGlxPixmap->y, pGlxPixmap->w, pGlxPixmap->h, 0, 0);
         /* Have to make sure XCopyArea is processed */
         XSync(dpy, False);
+        XUNLOCK(dpy);
         stub.spu->dispatch_table.TexImage2D(pGlxPixmap->target, 0, pGlxPixmap->format, pGlxPixmap->w, pGlxPixmap->h, 0, 
                                             GL_BGRA, GL_UNSIGNED_BYTE, stub.xshmSI.shmaddr);
@@ -2078,8 +2102,10 @@
         GLint origUnpackRowLength;
 
+        XLOCK(dpy);
         XCopyArea(dpy, (Pixmap)draw, pGlxPixmap->hShmPixmap, pGlxPixmap->gc, 
                   pRect->x, pRect->y, pRect->width, pRect->height, 0, 0);
         /* Have to make sure XCopyArea is processed */
         XSync(dpy, False);
+        XUNLOCK(dpy);
 
         /* Save original value, doesn't cause sync as it's reported by state tracker*/
@@ -2159,5 +2185,7 @@
         /* Sync connections, note that order of syncs is important here.
          * First make sure client commands are finished, then make sure we get all the damage events back*/
+        XLOCK(dpy);
         XSync(dpy, False);
+        XUNLOCK(dpy);
         XSync(stub.currentContext->damageDpy, False);
 
@@ -2179,5 +2207,7 @@
         XImage *pxim;
 
+        XLOCK(dpy);
         pxim = XGetImage(dpy, (Pixmap)draw, pGlxPixmap->x, pGlxPixmap->y, pGlxPixmap->w, pGlxPixmap->h, AllPlanes, ZPixmap);
+        XUNLOCK(dpy);
         /*if (pxim)
         {
Index: /trunk/src/VBox/Additions/common/crOpenGL/load.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/load.c	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/load.c	(revision 30440)
@@ -19,7 +19,11 @@
 #include <string.h>
 #include <signal.h>
+#include <iprt/initterm.h>
+#include <iprt/thread.h>
+#include <iprt/err.h>
+#include <iprt/asm.h>
 #ifndef WINDOWS
-#include <sys/types.h>
-#include <unistd.h>
+# include <sys/types.h>
+# include <unistd.h>
 #endif
 #ifdef CHROMIUM_THREADSAFE
@@ -80,7 +84,8 @@
 static ScissorFunc_t origScissor;
 
-static void stubCheckWindowState(WindowInfo *window)
+static void stubCheckWindowState(WindowInfo *window, GLboolean bFlushOnChange)
 {
     bool bForceUpdate = false;
+    bool bChanged = false;
 
 #ifdef WINDOWS
@@ -102,10 +107,10 @@
 #endif
 
-    stubUpdateWindowGeometry(window, bForceUpdate);
+    bChanged = stubUpdateWindowGeometry(window, bForceUpdate) || bForceUpdate;
 
 #if defined(GLX) || defined (WINDOWS)
     if (stub.trackWindowVisibleRgn)
     {
-        stubUpdateWindowVisibileRegions(window);
+        bChanged = stubUpdateWindowVisibileRegions(window) || bChanged;
     }
 #endif
@@ -117,5 +122,11 @@
             stub.spu->dispatch_table.WindowShow(window->spuWindow, mapped);
             window->mapped = mapped;
-        }
+            bChanged = true;
+        }
+    }
+
+    if (bFlushOnChange && bChanged)
+    {
+        stub.spu->dispatch_table.Flush();
     }
 }
@@ -133,8 +144,11 @@
     unsigned int border, depth, w, h;
 
+    XLOCK(pWindow->dpy);
     if (!XGetGeometry(pWindow->dpy, pWindow->drawable, &root, &x, &y, &w, &h, &border, &depth))
     {
+        XUNLOCK(pWindow->dpy);
         return false;
     }
+    XUNLOCK(pWindow->dpy);
 #endif
 
@@ -164,5 +178,5 @@
     }
 
-    stubCheckWindowState(pWindow);
+    stubCheckWindowState(pWindow, GL_FALSE);
 }
 
@@ -174,7 +188,14 @@
         return;
 
-    stubCheckWindowState(stub.currentContext->currentDrawable);
-
+#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
+    crLockMutex(&stub.mutex);
+#endif
+
+    stubCheckWindowState(stub.currentContext->currentDrawable, GL_TRUE);
     crHashtableWalk(stub.windowTable, stubCheckWindowsCB, stub.currentContext);
+
+#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
+    crUnlockMutex(&stub.mutex);
+#endif
 }
 
@@ -253,6 +274,8 @@
         origDrawBuffer = stub.spuDispatch.DrawBuffer;
         origScissor = stub.spuDispatch.Scissor;
+#ifndef CR_NEWWINTRACK
         stub.spuDispatch.Clear = trapClear;
         stub.spuDispatch.Viewport = trapViewport;
+#endif
         if (stub.viewportHack)
             stub.spuDispatch.Scissor = trapScissor;
@@ -282,5 +305,11 @@
 
 #ifdef WINDOWS
+# ifndef CR_NEWWINTRACK
     stubUninstallWindowMessageHook();
+# endif
+#endif
+
+#ifdef CR_NEWWINTRACK
+    ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
 #endif
   
@@ -330,8 +359,29 @@
 #endif
     crDebug("stubSPUSafeTearDown");
+
+#ifdef WINDOWS
+# ifndef CR_NEWWINTRACK
+    stubUninstallWindowMessageHook();
+# endif
+#endif
+
+#if defined(WINDOWS) && defined(CR_NEWWINTRACK)
+    crUnlockMutex(mutex);
+    if (RTThreadGetState(stub.hSyncThread)!=RTTHREADSTATE_TERMINATED)
+    {
+        ASMAtomicWriteBool(&stub.bShutdownSyncThread, true);
+        if (PostThreadMessage(RTThreadGetNative(stub.hSyncThread), WM_QUIT, 0, 0))
+        {
+            RTThreadWait(stub.hSyncThread, 1000, NULL);
+        }
+        else
+        {
+            crDebug("Sync thread killed before DLL_PROCESS_DETACH");
+        }
+    }
+    crLockMutex(mutex);
+#endif
+
     crNetTearDown();
-#ifdef WINDOWS
-    stubUninstallWindowMessageHook();
-#endif
     crMemset(&stub, 0, sizeof(stub));
 #ifdef CHROMIUM_THREADSAFE
@@ -393,4 +443,9 @@
 
     stub.windowTable = crAllocHashtable();
+
+#ifdef CR_NEWWINTRACK
+    stub.bShutdownSyncThread = false;
+    stub.hSyncThread = NIL_RTTHREAD;
+#endif
 
     defaultWin = (WindowInfo *) crCalloc(sizeof(WindowInfo));
@@ -595,4 +650,84 @@
 }
 
+#ifdef CR_NEWWINTRACK
+static void stubSyncTrCheckWindowsCB(unsigned long key, void *data1, void *data2)
+{
+    WindowInfo *pWindow = (WindowInfo *) data1;
+    ContextInfo *pCtx = (ContextInfo *) data2;
+
+    if (pWindow->type!=CHROMIUM || pWindow->spuWindow==0)
+    {
+        return;
+    }
+
+    stub.spu->dispatch_table.VBoxPackSetInjectID(pWindow->u32ClientID);
+
+    if (!stubSystemWindowExist(pWindow))
+    {
+#ifdef WINDOWS
+        crWindowDestroy((GLint)pWindow->hWnd);
+#else
+        crWindowDestroy((GLint)pWindow->drawable);
+#endif
+        /*No need to flush here as crWindowDestroy does it*/
+        return;
+    }
+
+    stubCheckWindowState(pWindow, GL_TRUE);
+}
+
+static DECLCALLBACK(int) stubSyncThreadProc(RTTHREAD ThreadSelf, void *pvUser)
+{
+#ifdef WINDOWS
+    MSG msg;
+#endif
+
+    (void) pvUser;
+
+    crDebug("Sync thread started");
+#ifdef WINDOWS
+    PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
+#endif
+
+    crLockMutex(&stub.mutex);
+    stub.spu->dispatch_table.VBoxPackSetInjectThread();
+    crUnlockMutex(&stub.mutex);
+
+    RTThreadUserSignal(ThreadSelf);
+
+    while(!stub.bShutdownSyncThread)
+    {
+#ifdef WINDOWS
+        if (!PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
+        {
+            crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
+            RTThreadSleep(50);
+        }
+        else
+        {
+            if (WM_QUIT==msg.message)
+            {
+                crDebug("Sync thread got WM_QUIT");
+                break;
+            }
+            else
+            {
+                TranslateMessage(&msg);
+                DispatchMessage(&msg);
+            }
+        }
+#else
+        crLockMutex(&stub.mutex);
+        crHashtableWalk(stub.windowTable, stubSyncTrCheckWindowsCB, NULL);
+        crUnlockMutex(&stub.mutex);
+        RTThreadSleep(50);
+#endif
+    }
+
+    crDebug("Sync thread stopped");
+    return 0;
+}
+#endif
+
 /**
  * Do one-time initializations for the faker.
@@ -623,4 +758,7 @@
     stubInitVars();
 
+    crGetProcName(response, 1024);
+    crDebug("Stub launched for %s", response);
+
     /* @todo check if it'd be of any use on other than guests, no use for windows */
     app_id = crGetenv( "CR_APPLICATION_ID_NUMBER" );
@@ -644,4 +782,13 @@
             crNetFreeConnection(ns.conn);
         }
+#ifdef CR_NEWWINTRACK
+        {
+            Status st = XInitThreads();
+            if (st==0)
+            {
+                crWarning("XInitThreads returned %i", (int)st);
+            }
+        }
+#endif
     }
 #endif
@@ -692,5 +839,27 @@
 
 #ifdef WINDOWS
+# ifndef CR_NEWWINTRACK
     stubInstallWindowMessageHook();
+# endif
+#endif
+
+#ifdef CR_NEWWINTRACK
+    {
+        int rc;
+
+        RTR3Init();
+
+        crDebug("Starting sync thread");
+
+        rc = RTThreadCreate(&stub.hSyncThread, stubSyncThreadProc, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Sync");
+        if (RT_FAILURE(rc))
+        {
+            crError("Failed to start sync thread! (%x)", rc);
+        }
+        RTThreadUserWait(stub.hSyncThread, 60 * 1000);
+        RTThreadUserReset(stub.hSyncThread);
+
+        crDebug("Going on");
+    }
 #endif
 
@@ -704,6 +873,4 @@
     return true;
 }
-
-
 
 /* Sigh -- we can't do initialization at load time, since Windows forbids 
Index: /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu.h
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu.h	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu.h	(revision 30440)
@@ -35,4 +35,5 @@
     CRPackContext *packer;
     int writeback;
+    GLboolean bInjectThread;
 };
 
Index: /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_context.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_context.c	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_context.c	(revision 30440)
@@ -36,4 +36,5 @@
     thread->id = id;
     thread->currentContext = NULL;
+    thread->bInjectThread = GL_FALSE;
 
     /* connect to the server */
Index: /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_misc.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_misc.c	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_misc.c	(revision 30440)
@@ -108,12 +108,18 @@
     int writeback = 1;
 
-    crPackFlush();
-
-    if (packspuSyncOnFlushes())
-    {
-        crPackWriteback(&writeback);
+    if (!thread->bInjectThread)
+    {
+        crPackFlush();
+        if (packspuSyncOnFlushes())
+        {
+            crPackWriteback(&writeback);
+            packspuFlush( (void *) thread );
+            while (writeback)
+                crNetRecv();
+        }
+    }
+    else
+    {
         packspuFlush( (void *) thread );
-        while (writeback)
-            crNetRecv();
     }
 }
@@ -336,2 +342,83 @@
     }
 }
+
+#ifdef CHROMIUM_THREADSAFE
+void PACKSPU_APIENTRY packspu_VBoxPackSetInjectThread(void)
+{
+    crLockMutex(&_PackMutex);
+    {
+        GET_THREAD(thread);
+        CRASSERT(!thread);
+        CRASSERT((pack_spu.numThreads>0) && (pack_spu.numThreads<MAX_THREADS));
+
+        thread = &(pack_spu.thread[pack_spu.numThreads]);
+        thread->id = crThreadID();
+        thread->currentContext = NULL;
+        thread->bInjectThread = GL_TRUE;
+
+        thread->netServer.name = crStrdup(pack_spu.name);
+        thread->netServer.buffer_size = 64 * 1024;
+
+        crNetNewClient(pack_spu.thread[0].netServer.conn, &(thread->netServer));
+        CRASSERT(thread->netServer.conn);
+
+        CRASSERT(thread->packer == NULL);
+        thread->packer = crPackNewContext( pack_spu.swap );
+        CRASSERT(thread->packer);
+        crPackInitBuffer(&(thread->buffer), crNetAlloc(thread->netServer.conn),
+                         thread->netServer.conn->buffer_size, thread->netServer.conn->mtu);
+        thread->buffer.canBarf = thread->netServer.conn->Barf ? GL_TRUE : GL_FALSE;
+
+        crPackSetBuffer( thread->packer, &thread->buffer );
+        crPackFlushFunc( thread->packer, packspuFlush );
+        crPackFlushArg( thread->packer, (void *) thread );
+        crPackSendHugeFunc( thread->packer, packspuHuge );
+        crPackSetContext( thread->packer );
+
+        crSetTSD(&_PackTSD, thread);
+
+        pack_spu.numThreads++;
+    }
+    crUnlockMutex(&_PackMutex);
+}
+
+GLuint PACKSPU_APIENTRY packspu_VBoxPackGetInjectID(void)
+{
+    GLuint ret;
+
+    crLockMutex(&_PackMutex);
+    {
+        GET_THREAD(thread);
+        CRASSERT(thread && thread->netServer.conn && thread->netServer.conn->type==CR_VBOXHGCM);
+        ret = thread->netServer.conn->u32ClientID;
+    }
+    crUnlockMutex(&_PackMutex);
+
+    return ret;
+}
+
+void PACKSPU_APIENTRY packspu_VBoxPackSetInjectID(GLuint id)
+{
+    crLockMutex(&_PackMutex);
+    {
+        GET_THREAD(thread);
+        CRASSERT(thread && thread->netServer.conn && thread->netServer.conn->type==CR_VBOXHGCM);
+        thread->netServer.conn->u32InjectClientID = id;
+    }
+    crUnlockMutex(&_PackMutex);
+}
+#else  /*ifdef CHROMIUM_THREADSAFE*/
+void PACKSPU_APIENTRY packspu_VBoxPackSetInjectThread(void)
+{
+}
+
+GLuint PACKSPU_APIENTRY packspu_VBoxPackGetInjectID(void)
+{
+    return 0;
+}
+
+void PACKSPU_APIENTRY packspu_VBoxPackSetInjectID(GLuint id)
+{
+    (void) id;
+}
+#endif /*CHROMIUM_THREADSAFE*/
Index: /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_special
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_special	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/pack/packspu_special	(revision 30440)
@@ -102,2 +102,5 @@
 GetCompressedTexImageARB
 BindRenderbufferEXT
+VBoxPackSetInjectThread
+VBoxPackGetInjectID
+VBoxPackSetInjectID
Index: /trunk/src/VBox/Additions/common/crOpenGL/stub.c
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/stub.c	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/stub.c	(revision 30440)
@@ -17,4 +17,5 @@
 static void crForcedFlush()
 {
+#if 0
     GLint buffer;
     stub.spu->dispatch_table.GetIntegerv(GL_DRAW_BUFFER, &buffer);
@@ -22,4 +23,7 @@
     stub.spu->dispatch_table.Flush();
     stub.spu->dispatch_table.DrawBuffer(buffer);
+#else
+    stub.spu->dispatch_table.Flush();
+#endif
 }
 
@@ -291,4 +295,5 @@
 }
 
+# ifndef CR_NEWWINTRACK
 static void stubCBCheckWindowsInfo(unsigned long key, void *data1, void *data2)
 {
@@ -393,4 +398,6 @@
         UnhookWindowsHookEx(stub.hMessageHook);
 }
+# endif /*# ifndef CR_NEWWINTRACK*/
+
 #elif defined(GLX) //#ifdef WINDOWS
 static GLboolean stubCheckXExtensions(WindowInfo *pWindow)
@@ -398,4 +405,5 @@
     int evb, erb, vmi=0, vma=0;
 
+    XLOCK(pWindow->dpy);
     if (XCompositeQueryExtension(pWindow->dpy, &evb, &erb) 
         && XCompositeQueryVersion(pWindow->dpy, &vma, &vmi) 
@@ -410,4 +418,5 @@
         {
             crDebug("XFixes %i.%i", vma, vmi);
+            XUNLOCK(pWindow->dpy);
             return GL_TRUE;
         }
@@ -421,4 +430,5 @@
         crWarning("XComposite not found or old version (%i.%i), no VisibilityTracking", vma, vmi);
     }
+    XUNLOCK(pWindow->dpy);
     return GL_FALSE;
 }
@@ -450,7 +460,9 @@
     * it seems there's no way to get even based updates for this. Or I've failed to find the appropriate extension.
     */
+    XLOCK(pWindow->dpy);
     xreg = XCompositeCreateRegionFromBorderClip(pWindow->dpy, pWindow->drawable);
     pXRects = XFixesFetchRegion(pWindow->dpy, xreg, &cRects);
     XFixesDestroyRegion(pWindow->dpy, xreg);
+    XUNLOCK(pWindow->dpy);
 
     /* @todo For some odd reason *first* run of compiz on freshly booted VM gives us 0 cRects all the time.
Index: /trunk/src/VBox/Additions/common/crOpenGL/stub.h
===================================================================
--- /trunk/src/VBox/Additions/common/crOpenGL/stub.h	(revision 30439)
+++ /trunk/src/VBox/Additions/common/crOpenGL/stub.h	(revision 30440)
@@ -46,4 +46,19 @@
 #endif
 
+#ifdef WINDOWS
+#define CR_NEWWINTRACK
+#endif
+
+#if !defined(CHROMIUM_THREADSAFE) && defined(CR_NEWWINTRACK)
+# error CHROMIUM_THREADSAFE have to be defined
+#endif
+
+#if defined(CR_NEWWINTRACK) && !defined(WINDOWS)
+#define XLOCK(dpy) XLockDisplay(dpy)
+#define XUNLOCK(dpy) XUnlockDisplay(dpy)
+#else
+#define XLOCK(dpy)
+#define XUNLOCK(dpy)
+#endif
 
 /* When we first create a rendering context we can't be sure whether
@@ -140,9 +155,9 @@
     GLboolean mapped;
 #ifdef WINDOWS
-    HDC drawable;
-    HRGN hVisibleRegion;
-    DWORD dmPelsWidth;
-    DWORD dmPelsHeight;
-    HWND  hWnd;
+    HDC      drawable;
+    HRGN     hVisibleRegion;
+    DWORD    dmPelsWidth;
+    DWORD    dmPelsHeight;
+    HWND     hWnd;
 #elif defined(DARWIN)
     CGSConnectionID connection;
@@ -154,4 +169,7 @@
     XRectangle *pVisibleRegions;
     GLint cVisibleRegions;
+#endif
+#ifdef CR_NEWWINTRACK
+    uint32_t u32ClientID;
 #endif
 };
@@ -214,6 +232,14 @@
 
 #ifdef WINDOWS
-    HHOOK       hMessageHook;
-#endif
+# ifndef CR_NEWWINTRACK
+    HHOOK           hMessageHook;
+# endif
+#endif
+
+#ifdef CR_NEWWINTRACK
+    RTTHREAD        hSyncThread;
+    bool volatile   bShutdownSyncThread;
+#endif
+
 } Stub;
 
Index: /trunk/src/VBox/GuestHost/OpenGL/glapi_parser/APIspec.txt
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/glapi_parser/APIspec.txt	(revision 30439)
+++ /trunk/src/VBox/GuestHost/OpenGL/glapi_parser/APIspec.txt	(revision 30440)
@@ -8500,4 +8500,19 @@
 chromium    extpack
 
+name        VBoxPackSetInjectThread
+return      void
+category    VBox
+chromium    nopack
+
+name        VBoxPackGetInjectID
+return      GLuint
+category    VBox
+chromium    nopack
+
+name        VBoxPackSetInjectID
+return      void
+param       id          GLuint
+category    VBox
+chromium    nopack
 
 # OpenGL 1.5
Index: /trunk/src/VBox/GuestHost/OpenGL/glapi_parser/apiutil.py
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/glapi_parser/apiutil.py	(revision 30439)
+++ /trunk/src/VBox/GuestHost/OpenGL/glapi_parser/apiutil.py	(revision 30440)
@@ -353,5 +353,6 @@
 		cat == "1.2" or
 		cat == "Chromium" or
-		cat == "GL_chromium"):
+		cat == "GL_chromium" or
+		cat == "VBox"):
 		return ''
 	elif (cat == '1.3' or
Index: /trunk/src/VBox/GuestHost/OpenGL/include/cr_net.h
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/include/cr_net.h	(revision 30439)
+++ /trunk/src/VBox/GuestHost/OpenGL/include/cr_net.h	(revision 30440)
@@ -220,4 +220,7 @@
     uint32_t cbHostBufferAllocated;
     uint32_t cbHostBuffer;
+#ifdef IN_GUEST
+    uint32_t u32InjectClientID;
+#endif
     /* Used on host side to indicate that we are not allowed to store above pointers for later use
      * in crVBoxHGCMReceiveMessage. As those messages are going to be processed after the correspoding 
Index: /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c	(revision 30439)
+++ /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c	(revision 30440)
@@ -232,4 +232,9 @@
          * Ensures context bits are reset */
         crStateFreeContext(defaultContext);
+#ifdef CHROMIUM_THREADSAFE
+        crSetTSD(&__contextTSD, NULL);
+#else
+        __currentContext = NULL;
+#endif
     }
 
Index: /trunk/src/VBox/GuestHost/OpenGL/util/vboxhgcm.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/util/vboxhgcm.c	(revision 30439)
+++ /trunk/src/VBox/GuestHost/OpenGL/util/vboxhgcm.c	(revision 30440)
@@ -331,21 +331,48 @@
 static void crVBoxHGCMWriteExact(CRConnection *conn, const void *buf, unsigned int len)
 {
-    CRVBOXHGCMWRITE parms;
     int rc;
-
-    parms.hdr.result      = VERR_WRONG_ORDER;
-    parms.hdr.u32ClientID = conn->u32ClientID;
-    parms.hdr.u32Function = SHCRGL_GUEST_FN_WRITE;
-    parms.hdr.cParms      = SHCRGL_CPARMS_WRITE;
-
-    parms.pBuffer.type                   = VMMDevHGCMParmType_LinAddr_In;
-    parms.pBuffer.u.Pointer.size         = len;
-    parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
-
-    rc = crVBoxHGCMCall(&parms, sizeof(parms));
-
-    if (RT_FAILURE(rc) || RT_FAILURE(parms.hdr.result))
-    {
-        crWarning("SHCRGL_GUEST_FN_WRITE failed with %x %x\n", rc, parms.hdr.result);
+    int32_t callRes;
+
+#ifdef IN_GUEST
+    if (conn->u32InjectClientID)
+    {
+        CRVBOXHGCMINJECT parms;
+
+        parms.hdr.result      = VERR_WRONG_ORDER;
+        parms.hdr.u32ClientID = conn->u32ClientID;
+        parms.hdr.u32Function = SHCRGL_GUEST_FN_INJECT;
+        parms.hdr.cParms      = SHCRGL_CPARMS_INJECT;
+
+        parms.u32ClientID.type       = VMMDevHGCMParmType_32bit;
+        parms.u32ClientID.u.value32  = conn->u32InjectClientID;
+
+        parms.pBuffer.type                   = VMMDevHGCMParmType_LinAddr_In;
+        parms.pBuffer.u.Pointer.size         = len;
+        parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
+
+        rc = crVBoxHGCMCall(&parms, sizeof(parms));
+        callRes = parms.hdr.result;
+    }
+    else
+#endif
+    {
+        CRVBOXHGCMWRITE parms;
+
+        parms.hdr.result      = VERR_WRONG_ORDER;
+        parms.hdr.u32ClientID = conn->u32ClientID;
+        parms.hdr.u32Function = SHCRGL_GUEST_FN_WRITE;
+        parms.hdr.cParms      = SHCRGL_CPARMS_WRITE;
+
+        parms.pBuffer.type                   = VMMDevHGCMParmType_LinAddr_In;
+        parms.pBuffer.u.Pointer.size         = len;
+        parms.pBuffer.u.Pointer.u.linearAddr = (uintptr_t) buf;
+
+        rc = crVBoxHGCMCall(&parms, sizeof(parms));
+        callRes = parms.hdr.result;
+    }
+
+    if (RT_FAILURE(rc) || RT_FAILURE(callRes))
+    {
+        crWarning("SHCRGL_GUEST_FN_WRITE failed with %x %x\n", rc, callRes);
     }
 }
@@ -476,4 +503,5 @@
             _crVBoxHGCMWriteBytes(conn, start, len);
 #else
+            CRASSERT(!conn->u32InjectClientID);
             crDebug("SHCRGL: sending userbuf with %d bytes\n", len);
             crVBoxHGCMWriteReadExact(conn, start, len, CR_VBOXHGCM_USERALLOCATED);
@@ -493,4 +521,11 @@
      * No need to prepend it to the buffer
      */
+#ifdef IN_GUEST
+    if (conn->u32InjectClientID)
+    {
+        crVBoxHGCMWriteExact(conn, start, len);
+    }
+    else
+#endif
     crVBoxHGCMWriteReadExact(conn, start, len, hgcm_buffer->kind);
 
Index: /trunk/src/VBox/HostServices/SharedOpenGL/crserver/crservice.cpp
===================================================================
--- /trunk/src/VBox/HostServices/SharedOpenGL/crserver/crservice.cpp	(revision 30439)
+++ /trunk/src/VBox/HostServices/SharedOpenGL/crserver/crservice.cpp	(revision 30440)
@@ -277,4 +277,39 @@
         }
 
+        case SHCRGL_GUEST_FN_INJECT:
+        {
+            Log(("svcCall: SHCRGL_GUEST_FN_INJECT\n"));
+
+            /* Verify parameter count and types. */
+            if (cParms != SHCRGL_CPARMS_INJECT)
+            {
+                rc = VERR_INVALID_PARAMETER;
+            }
+            else
+            if (    paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* u32ClientID */
+                 || paParms[1].type != VBOX_HGCM_SVC_PARM_PTR   /* pBuffer */
+               )
+            {
+                rc = VERR_INVALID_PARAMETER;
+            }
+            else
+            {
+                /* Fetch parameters. */
+                uint32_t u32InjectClientID = paParms[0].u.uint32;
+                uint8_t *pBuffer  = (uint8_t *)paParms[1].u.pointer.addr;
+                uint32_t cbBuffer = paParms[1].u.pointer.size;
+
+                /* Execute the function. */
+                rc = crVBoxServerClientWrite(u32InjectClientID, pBuffer, cbBuffer);
+                if (!RT_SUCCESS(rc))
+                {
+                    Assert(VERR_NOT_SUPPORTED==rc);
+                    svcClientVersionUnsupported(0, 0);
+                }
+
+            }
+            break;
+        }
+
         case SHCRGL_GUEST_FN_READ:
         {
Index: /trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.c
===================================================================
--- /trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.c	(revision 30439)
+++ /trunk/src/VBox/HostServices/SharedOpenGL/crserverlib/server_main.c	(revision 30440)
@@ -381,5 +381,5 @@
     int32_t i;
 
-    //crDebug("crServer: [%x] ClientWrite u32ClientID=%d", crThreadID(), u32ClientID);
+    /*crDebug("=>crServer: ClientWrite u32ClientID=%d", u32ClientID);*/
 
     for (i = 0; i < cr_server.numClients; i++)
@@ -451,4 +451,6 @@
 
     CRASSERT(!pClient->conn->allow_redir_ptr || crNetNumMessages(pClient->conn)==0);
+
+    /*crDebug("<=crServer: ClientWrite u32ClientID=%d", u32ClientID);*/
 
     return VINF_SUCCESS;
