Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.c
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.c	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.c	(revision 44766)
@@ -129,8 +129,27 @@
 }
 
-static ContextInfo * renderspuCreateContextInternal(const char *dpyName, GLint visBits, ContextInfo * sharedContext)
+static ContextInfo * renderspuCreateContextInternal(const char *dpyName, GLint visBits, GLint idCtx, ContextInfo * sharedContext)
 {
     ContextInfo *context;
     VisualInfo *visual;
+
+    if (idCtx <= 0)
+    {
+        idCtx = (GLint)crHashtableAllocKeys(render_spu.contextTable, 1);
+        if (idCtx <= 0)
+        {
+            crWarning("failed to allocate context id");
+            return NULL;
+        }
+    }
+    else
+    {
+        if (crHashtableIsKeyUsed(render_spu.contextTable, idCtx))
+        {
+            crWarning("the specified ctx key %d is in use", idCtx);
+            return NULL;
+        }
+    }
+
 
     if (!dpyName || crStrlen(render_spu.display_string)>0)
@@ -144,11 +163,10 @@
     if (!context)
         return NULL;
-    context->BltInfo.Base.id = render_spu.context_id;
+    context->BltInfo.Base.id = idCtx;
     context->shared = sharedContext;
     if (!renderspu_SystemCreateContext(visual, context, sharedContext))
         return NULL;
 
-    crHashtableAdd(render_spu.contextTable, render_spu.context_id, context);
-    render_spu.context_id++;
+    crHashtableAdd(render_spu.contextTable, idCtx, context);
 
     context->BltInfo.Base.visualBits = visual->visAttribs;
@@ -161,4 +179,20 @@
 }
 
+GLint renderspuCreateContextEx(const char *dpyName, GLint visBits, GLint id, GLint shareCtx)
+{
+    ContextInfo *context, *sharedContext = NULL;
+
+    if (shareCtx) {
+        sharedContext
+            = (ContextInfo *) crHashtableSearch(render_spu.contextTable, shareCtx);
+        CRASSERT(sharedContext);
+    }
+
+    context = renderspuCreateContextInternal(dpyName, visBits, id, sharedContext);
+    if (context)
+        return context->BltInfo.Base.id;
+    return -1;
+}
+
 /*
  * Context functions
@@ -168,27 +202,5 @@
 renderspuCreateContext(const char *dpyName, GLint visBits, GLint shareCtx)
 {
-    ContextInfo *context, *sharedContext = NULL;
-    bool fHasShared;
-
-    if (shareCtx > 0)
-        fHasShared = true;
-    else if (shareCtx == -1)
-    {
-        shareCtx = 0;
-        fHasShared = true;
-    }
-    else
-        fHasShared = false;
-
-    if (fHasShared) {
-        sharedContext
-            = (ContextInfo *) crHashtableSearch(render_spu.contextTable, shareCtx);
-        CRASSERT(sharedContext);
-    }
-
-    context = renderspuCreateContextInternal(dpyName, visBits, sharedContext);
-    if (context)
-        return context->BltInfo.Base.id;
-    return -1;
+    return renderspuCreateContextEx(dpyName, visBits, 0, shareCtx);
 }
 
@@ -200,5 +212,5 @@
     CRASSERT(ctx);
 
-    if (ctx == 0)
+    if (ctx == CR_RENDER_DEFAULT_CONTEXT_ID)
     {
         crWarning("request to destroy a default context, ignoring");
@@ -213,5 +225,5 @@
     if (curCtx == context)
     {
-        renderspuMakeCurrent( 0, 0, 0 );
+        renderspuMakeCurrent( CR_RENDER_DEFAULT_WINDOW_ID, 0, CR_RENDER_DEFAULT_CONTEXT_ID );
         curCtx = GET_CONTEXT_VAL();
 		Assert(curCtx);
@@ -277,7 +289,7 @@
             context->everCurrent = GL_TRUE;
         }
-        if (crWindow == 0 && window->mapPending &&
+        if (crWindow == CR_RENDER_DEFAULT_WINDOW_ID && window->mapPending &&
                 !render_spu.render_to_app_window && !render_spu.render_to_crut_window) {
-            /* Window[0] is special, it's the default window and normally hidden.
+            /* Window[CR_RENDER_DEFAULT_CONTEXT_ID] is special, it's the default window and normally hidden.
              * If the mapPending flag is set, then we should now make the window
              * visible.
@@ -288,5 +300,5 @@
         window->everCurrent = GL_TRUE;
     }
-    else
+    else if (!crWindow && !ctx)
     {
 #ifdef CHROMIUM_THREADSAFE
@@ -295,4 +307,9 @@
         render_spu.currentContext = NULL;
 #endif
+        renderspu_SystemMakeCurrent( NULL, 0, NULL );
+    }
+    else
+    {
+        crError("renderspuMakeCurrent invalid ids: crWindow(%d), ctx(%d)", crWindow, ctx);
     }
 }
@@ -303,6 +320,5 @@
  */
 
-GLint RENDER_APIENTRY
-renderspuWindowCreate( const char *dpyName, GLint visBits )
+GLint renderspuWindowCreateEx( const char *dpyName, GLint visBits, GLint id )
 {
     WindowInfo *window;
@@ -310,4 +326,23 @@
     GLboolean showIt;
 
+    if (id <= 0)
+    {
+        id = (GLint)crHashtableAllocKeys(render_spu.windowTable, 1);
+        if (id <= 0)
+        {
+            crWarning("failed to allocate window id");
+            return -1;
+        }
+    }
+    else
+    {
+        if (crHashtableIsKeyUsed(render_spu.windowTable, id))
+        {
+            crWarning("the specified window key %d is in use", id);
+            return -1;
+        }
+    }
+
+
     if (!dpyName || crStrlen(render_spu.display_string) > 0)
         dpyName = render_spu.display_string;
@@ -328,7 +363,9 @@
     }
 
-    crHashtableAdd(render_spu.windowTable, render_spu.window_id, window);
-    window->BltInfo.Base.id = render_spu.window_id;
-    render_spu.window_id++;
+    RTCritSectInit(&window->CompositorLock);
+    window->pCompositor = NULL;
+
+    crHashtableAdd(render_spu.windowTable, id, window);
+    window->BltInfo.Base.id = id;
 
     window->x = render_spu.defaultX;
@@ -341,5 +378,5 @@
         showIt = 0;
     else
-        showIt = window->BltInfo.Base.id > 0;
+        showIt = window->BltInfo.Base.id != CR_RENDER_DEFAULT_WINDOW_ID;
 
     /* Set window->title, replacing %i with the window ID number */
@@ -381,4 +418,10 @@
 }
 
+GLint RENDER_APIENTRY
+renderspuWindowCreate( const char *dpyName, GLint visBits )
+{
+    return renderspuWindowCreateEx( dpyName, visBits, 0 );
+}
+
 static void renderspuCheckCurrentCtxWindowCB(unsigned long key, void *data1, void *data2)
 {
@@ -389,5 +432,5 @@
     if (pCtx->currentWindow==pWindow)
     {
-        renderspuMakeCurrent(0, 0, pCtx->BltInfo.Base.id);
+        renderspuMakeCurrent(CR_RENDER_DEFAULT_WINDOW_ID, 0, pCtx->BltInfo.Base.id);
         pCtx->currentWindow=0;
     }
@@ -401,5 +444,5 @@
 
     CRASSERT(win >= 0);
-    if (win == 0)
+    if (win == CR_RENDER_DEFAULT_WINDOW_ID)
     {
         crWarning("request to destroy a default mural, ignoring");
@@ -410,4 +453,8 @@
         crDebug("Render SPU: Destroy window (%d)", win);
         renderspu_SystemDestroyWindow( window );
+
+        RTCritSectDelete(&window->CompositorLock);
+        window->pCompositor = NULL;
+
         /* remove window info from hash table, and free it */
         crHashtableDelete(render_spu.windowTable, win, crFree);
@@ -421,6 +468,6 @@
             if (pNewCtx!=pOldCtx)
             {
-                renderspuMakeCurrent(pOldCtx&&pOldCtx->currentWindow ? pOldCtx->currentWindow->BltInfo.Base.id:0, 0,
-                                     pOldCtx ? pOldCtx->BltInfo.Base.id:0);
+                renderspuMakeCurrent(pOldCtx&&pOldCtx->currentWindow ? pOldCtx->currentWindow->BltInfo.Base.id:CR_RENDER_DEFAULT_WINDOW_ID, 0,
+                                     pOldCtx ? pOldCtx->BltInfo.Base.id:CR_RENDER_DEFAULT_CONTEXT_ID);
             }
         }
@@ -509,4 +556,7 @@
     window = (WindowInfo *) crHashtableSearch(render_spu.windowTable, win);
     if (window) {
+        renderspuVBoxCompositorSet( window, pCompositor);
+        /* renderspuVBoxPresentComposition can be invoked from the chromium thread only and is not reentrant,
+         * no need to acquire a compositor lock here */
         renderspu_SystemVBoxPresentComposition(window, pCompositor, pChangedEntry);
     }
@@ -574,4 +624,5 @@
         if (render_spu.blitterTable)
         {
+            crHashtableLock(render_spu.blitterTable);
             pBlitter = (PCR_BLITTER)crHashtableSearch(render_spu.blitterTable, window->visual->visAttribs);
         }
@@ -599,5 +650,13 @@
                 return NULL;
             }
-        }
+
+            if (render_spu.blitterTable)
+            {
+                crHashtableAdd( render_spu.blitterTable, window->visual->visAttribs, pBlitter );
+            }
+        }
+
+        if (render_spu.blitterTable)
+            crHashtableUnlock(render_spu.blitterTable);
 
         Assert(pBlitter);
@@ -612,19 +671,18 @@
 {
     int rc;
+    PCR_BLITTER_CONTEXT pCtxInfo = NULL;
+    PCR_BLITTER_WINDOW pWindowInfo = NULL;
     GET_CONTEXT(pCtx);
 
-    if (!pCtx)
-    {
-        crWarning("renderspuVBoxPresentBlitterEnter: no current context!");
-        return VERR_INVALID_STATE;
-    }
-
-    if (!pCtx->currentWindow)
-    {
-        crWarning("renderspuVBoxPresentBlitterEnter: no current window!");
-        return VERR_INVALID_STATE;
-    }
-
-    rc = CrBltEnter(pBlitter, &pCtx->BltInfo, &pCtx->currentWindow->BltInfo);
+    if (pCtx)
+    {
+        if (pCtx->currentWindow)
+        {
+            pCtxInfo = &pCtx->BltInfo;
+            pWindowInfo =  &pCtx->currentWindow->BltInfo;
+        }
+    }
+
+    rc = CrBltEnter(pBlitter, pCtxInfo, pWindowInfo);
     if (!RT_SUCCESS(rc))
     {
@@ -647,4 +705,45 @@
     }
     return NULL;
+}
+
+PCR_BLITTER renderspuVBoxPresentBlitterEnsureCreated( WindowInfo *window )
+{
+    if (!window->pBlitter)
+    {
+        struct VBOXVR_SCR_COMPOSITOR * pTmpCompositor;
+        /* just use compositor lock to synchronize */
+        pTmpCompositor = renderspuVBoxCompositorAcquire(window);
+        CRASSERT(pTmpCompositor);
+        if (pTmpCompositor)
+        {
+            PCR_BLITTER pBlitter = renderspuVBoxPresentBlitterGet( window );
+            if (pBlitter)
+            {
+                if (!CrBltIsEverEntered(pBlitter))
+                {
+                    int rc = renderspuVBoxPresentBlitterEnter(pBlitter);
+                    if (RT_SUCCESS(rc))
+                    {
+                        CrBltLeave(pBlitter);
+                    }
+                    else
+                    {
+                        crWarning("renderspuVBoxPresentBlitterEnter failed rc %d", rc);
+                    }
+                }
+            }
+            else
+            {
+                crWarning("renderspuVBoxPresentBlitterGet failed");
+            }
+
+            renderspuVBoxCompositorRelease(window);
+        }
+        else
+        {
+            crWarning("renderspuVBoxCompositorAcquire failed");
+        }
+    }
+    return window->pBlitter;
 }
 
@@ -657,8 +756,81 @@
     renderspuVBoxCompositorBlit(pCompositor, pBlitter);
 
-    CrBltPresent(pBlitter);
+    renderspu_SystemSwapBuffers(window, 0);
 
     CrBltLeave(pBlitter);
 }
+
+void renderspuVBoxCompositorSet( WindowInfo *window, struct VBOXVR_SCR_COMPOSITOR * pCompositor)
+{
+    int rc;
+    /* renderspuVBoxCompositorSet can be invoked from the chromium thread only and is not reentrant,
+     * no need to synch here
+     * the lock is actually needed to ensure we're in synch with the redraw thread */
+    if (window->pCompositor == pCompositor)
+        return;
+    rc = RTCritSectEnter(&window->CompositorLock);
+    if (RT_SUCCESS(rc))
+    {
+        window->pCompositor = pCompositor;
+        RTCritSectLeave(&window->CompositorLock);
+        return;
+    }
+    else
+    {
+        crWarning("RTCritSectEnter failed rc %d", rc);
+    }
+}
+
+struct VBOXVR_SCR_COMPOSITOR * renderspuVBoxCompositorAcquire( WindowInfo *window)
+{
+    int rc = RTCritSectEnter(&window->CompositorLock);
+    if (RT_SUCCESS(rc))
+    {
+        VBOXVR_SCR_COMPOSITOR * pCompositor = window->pCompositor;
+        if (pCompositor)
+            return pCompositor;
+
+        /* if no compositor is set, release the lock and return */
+        RTCritSectLeave(&window->CompositorLock);
+    }
+    else
+    {
+        crWarning("RTCritSectEnter failed rc %d", rc);
+    }
+    return NULL;
+}
+
+int renderspuVBoxCompositorTryAcquire(WindowInfo *window, struct VBOXVR_SCR_COMPOSITOR **ppCompositor)
+{
+    int rc = RTCritSectTryEnter(&window->CompositorLock);
+    if (RT_SUCCESS(rc))
+    {
+        *ppCompositor = window->pCompositor;
+        if (*ppCompositor)
+            return VINF_SUCCESS;
+
+        /* if no compositor is set, release the lock and return */
+        RTCritSectLeave(&window->CompositorLock);
+        rc = VERR_INVALID_STATE;
+    }
+    else
+    {
+        *ppCompositor = NULL;
+        crWarning("RTCritSectTryEnter failed rc %d", rc);
+    }
+    return rc;
+}
+
+void renderspuVBoxCompositorRelease( WindowInfo *window)
+{
+    int rc;
+    Assert(window->pCompositor);
+    rc = RTCritSectLeave(&window->CompositorLock);
+    if (!RT_SUCCESS(rc))
+    {
+        crWarning("RTCritSectLeave failed rc %d", rc);
+    }
+}
+
 
 /*
@@ -1051,5 +1223,5 @@
     case GL_WINDOW_SIZE_CR:
         /* XXX this is old code that should be removed.
-         * NOTE: we can only resize the default (id=0) window!!!
+         * NOTE: we can only resize the default (id=CR_RENDER_DEFAULT_WINDOW_ID) window!!!
          */
         {
@@ -1061,5 +1233,5 @@
             w = ((GLint*)values)[0];
             h = ((GLint*)values)[1];
-            window = (WindowInfo *) crHashtableSearch(render_spu.windowTable, 0);
+            window = (WindowInfo *) crHashtableSearch(render_spu.windowTable, CR_RENDER_DEFAULT_WINDOW_ID);
             if (window)
             {
Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.h
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.h	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.h	(revision 44766)
@@ -31,4 +31,5 @@
 
 #include <iprt/cdefs.h>
+#include <iprt/critsect.h>
 
 #define MAX_VISUALS 32
@@ -90,4 +91,11 @@
     char *title;
 
+    PVBOXVR_SCR_COMPOSITOR pCompositor;
+    /* the composotor lock is used to synchronize the current compositor access,
+     * i.e. the compositor can be accessed by a gui refraw thread,
+     * while chromium thread might try to set a new compositor
+     * note that the compositor internally has its own lock to be used for accessing its data
+     * see CrVrScrCompositorLock/Unlock; renderspu and crserverlib would use it for compositor data access */
+    RTCRITSECT CompositorLock;
     PCR_BLITTER pBlitter;
 #if defined(WINDOWS)
@@ -165,7 +173,4 @@
     SPUDispatchTable self;
     int id;
-
-    unsigned int window_id;
-    unsigned int context_id;
 
     /** config options */
@@ -330,10 +335,17 @@
 extern void renderspu_GCWindow(void);
 extern int renderspuCreateFunctions( SPUNamedFunctionTable table[] );
+extern void renderspuVBoxCompositorSet( WindowInfo *window, struct VBOXVR_SCR_COMPOSITOR * pCompositor);
+extern struct VBOXVR_SCR_COMPOSITOR * renderspuVBoxCompositorAcquire( WindowInfo *window);
+extern int renderspuVBoxCompositorTryAcquire(WindowInfo *window, struct VBOXVR_SCR_COMPOSITOR **ppCompositor);
+extern void renderspuVBoxCompositorRelease( WindowInfo *window);
 extern void renderspuVBoxPresentCompositionGeneric( WindowInfo *window, struct VBOXVR_SCR_COMPOSITOR * pCompositor, struct VBOXVR_SCR_COMPOSITOR_ENTRY *pChangedEntry );
 extern PCR_BLITTER renderspuVBoxPresentBlitterGet( WindowInfo *window );
 extern int renderspuVBoxPresentBlitterEnter( PCR_BLITTER pBlitter );
 extern PCR_BLITTER renderspuVBoxPresentBlitterGetAndEnter( WindowInfo *window );
+extern PCR_BLITTER renderspuVBoxPresentBlitterEnsureCreated( WindowInfo *window );
 extern void renderspuVBoxCompositorBlit ( struct VBOXVR_SCR_COMPOSITOR * pCompositor, PCR_BLITTER pBlitter);
 extern void renderspuVBoxCompositorBlitStretched ( struct VBOXVR_SCR_COMPOSITOR * pCompositor, PCR_BLITTER pBlitter, GLfloat scaleX, GLfloat scaleY);
+extern GLint renderspuCreateContextEx(const char *dpyName, GLint visBits, GLint id, GLint shareCtx);
+extern GLint renderspuWindowCreateEx( const char *dpyName, GLint visBits, GLint id );
 
 extern GLint RENDER_APIENTRY renderspuWindowCreate( const char *dpyName, GLint visBits );
Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa.c
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa.c	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa.c	(revision 44766)
@@ -159,11 +159,11 @@
 void renderspu_SystemMakeCurrent(WindowInfo *pWinInfo, GLint nativeWindow, ContextInfo *pCtxInfo)
 {
-    CRASSERT(pWinInfo);
-    CRASSERT(pCtxInfo);
-
 /*    if(pWinInfo->visual != pCtxInfo->visual)*/
 /*        printf ("visual mismatch .....................\n");*/
 
-    cocoaViewMakeCurrentContext(pWinInfo->window, pCtxInfo->context);
+    if (pWinInfo && pCtxInfo)
+        cocoaViewMakeCurrentContext(pWinInfo->window, pCtxInfo->context);
+    else
+        cocoaViewMakeCurrentContext(NULL, NULL);
 }
 
Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m	(revision 44766)
@@ -1593,6 +1593,13 @@
     DEBUG_MSG(("cocoaViewMakeCurrentContext(%p, %p)\n", (void*)pView, (void*)pCtx));
 
-    [(OverlayView*)pView setGLCtx:pCtx];
-    [(OverlayView*)pView makeCurrentFBO];
+    if (pView)
+    {
+        [(OverlayView*)pView setGLCtx:pCtx];
+        [(OverlayView*)pView makeCurrentFBO];
+    }
+    else
+    {
+        vboxCtxSetCurrent(NULL);
+    }
 
     [pPool release];
Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_config.c
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_config.c	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_config.c	(revision 44766)
@@ -322,5 +322,4 @@
 void renderspuSetVBoxConfiguration( RenderSPU *render_spu )
 {
-    CRConnection *conn;
     int a;
 
Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_glx.c
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_glx.c	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_glx.c	(revision 44766)
@@ -1177,5 +1177,4 @@
 
     CRASSERT(render_spu.ws.glXMakeCurrent);
-    window->appWindow = nativeWindow;
 
     /*crDebug("%s nativeWindow=0x%x", __FUNCTION__, (int) nativeWindow);*/
@@ -1192,4 +1191,6 @@
 
     if (window && context) {
+        window->appWindow = nativeWindow;
+
         if (window->visual != context->visual) {
             crDebug("Render SPU: MakeCurrent visual mismatch (win(%d) bits:0x%x != ctx(%d) bits:0x%x); remaking window.",
@@ -1344,4 +1345,16 @@
         }
 #endif
+    }
+    else
+    {
+        GET_CONTEXT(pCurCtx);
+        if (pCurCtx)
+        {
+            b = render_spu.ws.glXMakeCurrent( pCurCtx->currentWindow->visual->dpy, None, NULL);
+            if (!b) {
+                crWarning("glXMakeCurrent(%p, None, NULL) failed!", pCurCtx->currentWindow->visual->dpy);
+            }
+        }
+
     }
 
Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_init.c
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_init.c	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_init.c	(revision 44766)
@@ -96,13 +96,14 @@
             {
                 LPCREATESTRUCT pCS = (LPCREATESTRUCT) msg.lParam;
-                HWND *phWnd;
+                HWND hWnd;
+                WindowInfo *pWindow = (WindowInfo *)pCS->lpCreateParams;
 
                 CRASSERT(msg.lParam && !msg.wParam && pCS->lpCreateParams);
 
-                phWnd = pCS->lpCreateParams;
-
-                *phWnd = CreateWindowEx(pCS->dwExStyle, pCS->lpszName, pCS->lpszClass, pCS->style,
+                hWnd = CreateWindowEx(pCS->dwExStyle, pCS->lpszName, pCS->lpszClass, pCS->style,
                                         pCS->x, pCS->y, pCS->cx, pCS->cy,
                                         pCS->hwndParent, pCS->hMenu, pCS->hInstance, &render_spu);
+
+                pWindow->hWnd = hWnd;
 
                 SetEvent(render_spu.hWinThreadReadyEvent);
@@ -198,8 +199,6 @@
 #endif
 
-    render_spu.window_id = 0;
-    render_spu.context_id = 0;
-    render_spu.contextTable = crAllocHashtable();
-    render_spu.windowTable = crAllocHashtable();
+    render_spu.contextTable = crAllocHashtableEx(1, INT32_MAX);
+    render_spu.windowTable = crAllocHashtableEx(1, INT32_MAX);
 
     pcpwSetting = crGetenv("CR_RENDER_ENABLE_PRESENT_CONTEXT_PER_WINDOW");
@@ -212,9 +211,10 @@
     {
         /* default is enable for OSX */
-#if defined(DARWIN) && defined(VBOX_WITH_COCOA_QT)
+#if 0 //defined(DARWIN) && defined(VBOX_WITH_COCOA_QT)
         pcpwSetting = (char*)1;
 #endif
 
     }
+
 
     if (pcpwSetting)
@@ -275,6 +275,6 @@
     crDebug("Render SPU: Creating default window (visBits=0x%x, id=0)",
             render_spu.default_visual);
-    defaultWin = renderspuWindowCreate( NULL, render_spu.default_visual );
-    if (defaultWin != 0) {
+    defaultWin = renderspuWindowCreateEx( NULL, render_spu.default_visual, CR_RENDER_DEFAULT_WINDOW_ID );
+    if (defaultWin != CR_RENDER_DEFAULT_WINDOW_ID) {
         crError("Render SPU: Couldn't get a double-buffered, RGB visual with Z!");
         return NULL;
@@ -284,11 +284,14 @@
     crDebug("Render SPU: Creating default context, visBits=0x%x",
             render_spu.default_visual );
-    defaultCtx = renderspuCreateContext( NULL, render_spu.default_visual, 0 );
-    CRASSERT(defaultCtx == 0);
+    defaultCtx = renderspuCreateContextEx( NULL, render_spu.default_visual, CR_RENDER_DEFAULT_CONTEXT_ID, 0 );
+    if (defaultCtx != CR_RENDER_DEFAULT_CONTEXT_ID) {
+        crError("Render SPU: failed to create default context!");
+        return NULL;
+    }
 
     renderspuMakeCurrent( defaultWin, 0, defaultCtx );
 
     /* Get windowInfo for the default window */
-    windowInfo = (WindowInfo *) crHashtableSearch(render_spu.windowTable, 0);
+    windowInfo = (WindowInfo *) crHashtableSearch(render_spu.windowTable, CR_RENDER_DEFAULT_WINDOW_ID);
     CRASSERT(windowInfo);
     windowInfo->mapPending = GL_TRUE;
Index: trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_wgl.c
===================================================================
--- trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_wgl.c	(revision 44756)
+++ trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_wgl.c	(revision 44766)
@@ -426,5 +426,45 @@
 
     switch ( uMsg ) {
-
+        case WM_PAINT:
+        {
+            WindowInfo *pWindow = (WindowInfo *)GetWindowLongPtr(hWnd, GWLP_USERDATA);
+            if (pWindow)
+            {
+                struct VBOXVR_SCR_COMPOSITOR * pCompositor;
+
+                pCompositor = renderspuVBoxCompositorAcquire(pWindow);
+                if (pCompositor)
+                {
+                    HDC hDC, hOldDC = pWindow->device_context;
+                    PAINTSTRUCT Paint;
+
+                    Assert(pWindow->device_context);
+                    hDC = BeginPaint(pWindow->hWnd, &Paint);
+                    if (hDC)
+                    {
+                        BOOL bRc;
+                        pWindow->device_context = hDC;
+
+                        renderspuVBoxPresentCompositionGeneric(pWindow, pCompositor, NULL);
+                        renderspuVBoxCompositorRelease(pWindow);
+
+                        bRc = EndPaint(pWindow->hWnd, &Paint);
+                        if (!bRc)
+                        {
+                            DWORD winEr = GetLastError();
+                            crWarning("EndPaint failed, winEr %d", winEr);
+                        }
+
+                        pWindow->device_context = hOldDC;
+                    }
+                    else
+                    {
+                        DWORD winEr = GetLastError();
+                        crWarning("BeginPaint failed, winEr %d", winEr);
+                    }
+                }
+            }
+            break;
+        }
         case WM_SIZE:
             /* w = LOWORD( lParam ); 
@@ -558,5 +598,9 @@
     crDebug("Render SPU: wglChoosePixelFormatEXT (vis 0x%x, LastError 0x%x, pixelFormat 0x%x", vis, GetLastError(), pixelFormat);
 
+#ifdef VBOX_CR_SERVER_FORCE_WGL
     render_spu.ws.wglSetPixelFormat( hdc, pixelFormat, &ppfd );
+#else
+    SetPixelFormat( hdc, pixelFormat, &ppfd );
+#endif
 
     crDebug("Render SPU: wglSetPixelFormat (Last error 0x%x)", GetLastError());
@@ -617,4 +661,5 @@
      * versions.
      */
+#ifdef VBOX_CR_SERVER_FORCE_WGL
     if (crGetenv( "CR_WGL_DO_NOT_USE_GDI" ) != NULL)
     {
@@ -634,4 +679,5 @@
     }
     else
+#endif
     {
         /* Okay, we were loaded manually.  Call the GDI functions. */
@@ -879,4 +925,9 @@
 
     window->device_context = GetDC( window->hWnd );
+    if (!window->device_context)
+    {
+        DWORD winEr = GetLastError();
+        crWarning("GetDC failed, winEr %d", winEr);
+    }
 
     crDebug( "Render SPU: Got the DC: 0x%x", window->device_context );
@@ -1053,5 +1104,5 @@
         CREATESTRUCT cs;
 
-        cs.lpCreateParams = &window->hWnd;
+        cs.lpCreateParams = window;
 
         cs.dwExStyle    = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY;
@@ -1152,4 +1203,9 @@
 
     window->device_context = GetDC( window->hWnd );
+    if (!window->device_context)
+    {
+        DWORD winEr = GetLastError();
+        crWarning("GetDC failed, winEr %d", winEr);
+    }
 
     crDebug( "Render SPU: Got the DC: 0x%x", window->device_context );
@@ -1159,4 +1215,10 @@
         crError( "Render SPU: Couldn't set up the device context!  Yikes!" );
         return GL_FALSE;
+    }
+
+    /* set the window pointer data at the last step to ensure our WM_PAINT callback does not do anything until we are fully initialized */
+    {
+        LONG_PTR oldVal = SetWindowLongPtr(window->hWnd, GWLP_USERDATA, (LONG_PTR)window);
+        Assert(!oldVal && GetLastError() == NO_ERROR);
     }
 
@@ -1185,5 +1247,24 @@
 void renderspu_SystemVBoxPresentComposition( WindowInfo *window, struct VBOXVR_SCR_COMPOSITOR * pCompositor, struct VBOXVR_SCR_COMPOSITOR_ENTRY *pChangedEntry )
 {
-    renderspuVBoxPresentCompositionGeneric(window, pCompositor, pChangedEntry);
+    struct VBOXVR_SCR_COMPOSITOR *pCurCompositor;
+    /* we do not want to be blocked with the GUI thread here, so only draw her eif we are really able to do that w/o bllocking */
+    int rc = renderspuVBoxCompositorTryAcquire(window, &pCurCompositor);
+    if (RT_SUCCESS(rc))
+    {
+        Assert(pCurCompositor == pCompositor);
+        renderspuVBoxPresentCompositionGeneric(window, pCompositor, pChangedEntry);
+        renderspuVBoxCompositorRelease(window);
+    }
+    else if (rc != VERR_SEM_BUSY)
+    {
+        render_spu.self.Flush();
+        renderspuVBoxPresentBlitterEnsureCreated(window);
+        RedrawWindow(window->hWnd, NULL, NULL, RDW_INTERNALPAINT);
+    }
+    else
+    {
+        /* this is somewhat we do not expect */
+        crWarning("renderspuVBoxCompositorTryAcquire failed rc %d", rc);
+    }
 }
 
@@ -1341,5 +1422,10 @@
                 {
                     /* share lists */
-                    render_spu.ws.wglShareLists(context->shared->hRC, context->hRC);
+                    BOOL bRc = render_spu.ws.wglShareLists(context->shared->hRC, context->hRC);
+                    if (!bRc)
+                    {
+                        DWORD winEr = GetLastError();
+                        crWarning("wglShareLists failed, winEr %d", winEr);
+                    }
                 }
 
@@ -1506,5 +1592,5 @@
 
     /* peek at the windows message queue */
-    renderspuHandleWindowMessages( w->hWnd );
+//    renderspuHandleWindowMessages( w->hWnd );
 
     /* render_to_app_window:
@@ -1515,5 +1601,9 @@
      */
     if (render_spu.render_to_app_window && w->nativeWindow) {
+#ifdef VBOX_CR_SERVER_FORCE_WGL
         return_value = render_spu.ws.wglSwapBuffers( w->nativeWindow );
+#else
+        return_value = SwapBuffers( w->nativeWindow );
+#endif
     } else {
         /*
@@ -1558,5 +1648,9 @@
         crDebug("rcClip(%d, %d, %d, %d)", rcClip.left, rcClip.top, rcClip.right, rcClip.bottom);
         */
+#ifdef VBOX_CR_SERVER_FORCE_WGL
         return_value = render_spu.ws.wglSwapBuffers( w->device_context );
+#else
+        return_value = SwapBuffers( w->device_context );
+#endif
     }
     if (!return_value)
