Index: trunk/src/VBox/Additions/common/crOpenGL/context.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/context.c	(revision 49196)
+++ trunk/src/VBox/Additions/common/crOpenGL/context.c	(revision 49214)
@@ -451,8 +451,4 @@
 #ifdef GLX
     crFreeHashtable(context->pGLXPixmapsHash, crFree);
-    if (context->damageDpy)
-    {
-        XCloseDisplay(context->damageDpy);
-    }
 #endif
 
@@ -553,6 +549,5 @@
 #ifdef GLX
     context->pGLXPixmapsHash = crAllocHashtable();
-    context->damageInitFailed = GL_FALSE;
-    context->damageDpy = NULL;
+    context->damageQueryFailed = GL_FALSE;
     context->damageEventsBase = 0;
 #endif
Index: trunk/src/VBox/Additions/common/crOpenGL/glx.c
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/glx.c	(revision 49196)
+++ trunk/src/VBox/Additions/common/crOpenGL/glx.c	(revision 49214)
@@ -58,5 +58,5 @@
 
 static void stubXshmUpdateImageRect(Display *dpy, GLXDrawable draw, GLX_Pixmap_t *pGlxPixmap, XRectangle *pRect);
-static void stubInitXDamageExtension(ContextInfo *pContext);
+static void stubQueryXDamageExtension(Display *dpy, ContextInfo *pContext);
 
 static void
@@ -625,7 +625,5 @@
     context->direct = direct;
 
-    /* This means that clients can't hold a server grab during
-     * glXCreateContext! */
-    stubInitXDamageExtension(context);
+    stubQueryXDamageExtension(dpy, context);
 
     return (GLXContext) context->id;
@@ -1784,5 +1782,5 @@
     {
         //crDebug("Destroy: Damage for drawable 0x%x, handle 0x%x", (unsigned int) pixmap, (unsigned int) parms.pGlxPixmap->damage);
-        XDamageDestroy(parms.pCtx->damageDpy, parms.pGlxPixmap->hDamage);
+        XDamageDestroy(dpy, parms.pGlxPixmap->hDamage);
     }
 
@@ -2174,5 +2172,5 @@
 }
 
-void stubInitXDamageExtension(ContextInfo *pContext)
+void stubQueryXDamageExtension(Display *dpy, ContextInfo *pContext)
 {
     int erb, vma, vmi;
@@ -2180,42 +2178,43 @@
     CRASSERT(pContext);
     
-    if (pContext->damageInitFailed || pContext->damageDpy)
+    if (pContext->damageQueryFailed)
         return;
 
-    pContext->damageInitFailed = True;
-
-    /* Open second xserver connection to make sure we'd receive all the xdamage messages 
-     * and those wouldn't be eaten by application even queue */
-    pContext->damageDpy = XOpenDisplay(DisplayString(pContext->dpy));
-
-    if (!pContext->damageDpy)
-    {
-        crWarning("XDamage: Can't connect to display %s", DisplayString(pContext->dpy));
+    pContext->damageQueryFailed = True;
+
+    if (!XDamageQueryExtension(dpy, &pContext->damageEventsBase, &erb)
+        || !XDamageQueryVersion(dpy, &vma, &vmi))
+    {
+        crWarning("XDamage not found or old version (%i.%i), going to run *very* slow", vma, vmi);
         return;
     }
 
-    if (!XDamageQueryExtension(pContext->damageDpy, &pContext->damageEventsBase, &erb)
-        || !XDamageQueryVersion(pContext->damageDpy, &vma, &vmi))
-    {
-        crWarning("XDamage not found or old version (%i.%i), going to run *very* slow", vma, vmi);
-        XCloseDisplay(pContext->damageDpy);
-        pContext->damageDpy = NULL;
-        return;
-    }
-
     crDebug("XDamage %i.%i", vma, vmi);
-    pContext->damageInitFailed = False;
-}
-
-static void stubCheckXDamageCB(unsigned long key, void *data1, void *data2)
-{
-    GLX_Pixmap_t *pGlxPixmap = (GLX_Pixmap_t *) data1;
-    XDamageNotifyEvent *e = (XDamageNotifyEvent *) data2;
-
-    if (pGlxPixmap->hDamage==e->damage)
-    {
-        /*crDebug("Event: Damage for pixmap 0x%lx(drawable 0x%x), handle 0x%x (level=%i) [%i,%i,%i,%i]",
-                key, (unsigned int) e->drawable, (unsigned int) e->damage, (int) e->level,
-                e->area.x, e->area.y, e->area.width, e->area.height);*/
+    pContext->damageQueryFailed = False;
+}
+
+static void stubFetchDamageOnDrawable(Display *dpy, GLX_Pixmap_t *pGlxPixmap)
+{
+    Damage damage = pGlxPixmap->hDamage;
+
+    if (damage)
+    {
+        XRectangle *returnRects;
+        int        nReturnRects;
+
+        /* Get the damage region as a server region */ 
+        XserverRegion serverDamageRegion = XFixesCreateRegion (dpy, NULL, 0);
+
+        /* Unite damage region with server region and clear damage region */
+        XDamageSubtract (dpy,
+                         damage,
+                         None, /* subtract all damage from this region */
+                         serverDamageRegion /* save in serverDamageRegion */);
+
+        /* Fetch damage rectangles */
+        returnRects = XFixesFetchRegion (dpy, serverDamageRegion, &nReturnRects);
+
+        /* Delete region */
+        XFixesDestroyRegion (dpy, serverDamageRegion);
 
         if (pGlxPixmap->pDamageRegion)
@@ -2224,16 +2223,22 @@
             if (!pGlxPixmap->bPixmapImageDirty || !XEmptyRegion(pGlxPixmap->pDamageRegion))
             {
-                if (CR_MAX_DAMAGE_REGIONS_TRACKED <= pGlxPixmap->pDamageRegion->numRects)
+                int i = 0;
+                for (; i < nReturnRects; ++i)
                 {
-                    /* Mark for full update */
-                    EMPTY_REGION(pGlxPixmap->pDamageRegion);
-                }
-                else
-                {
-                    /* Add to damage regions */
-                    XUnionRectWithRegion(&e->area, pGlxPixmap->pDamageRegion, pGlxPixmap->pDamageRegion);
+                    if (CR_MAX_DAMAGE_REGIONS_TRACKED <= pGlxPixmap->pDamageRegion->numRects)
+                    {
+                        /* Mark for full update */
+                        EMPTY_REGION(pGlxPixmap->pDamageRegion);
+                    }
+                    else
+                    {
+                        /* Add to damage regions */
+                        XUnionRectWithRegion(&returnRects[i], pGlxPixmap->pDamageRegion, pGlxPixmap->pDamageRegion);
+                    }
                 }
             }
         }
+
+        XFree(returnRects);
 
         pGlxPixmap->bPixmapImageDirty = True;
@@ -2340,7 +2345,7 @@
 
     /* If there's damage extension, then get handle for damage events related to this pixmap */
-    if (pContext->damageDpy)
-    {
-        pGlxPixmap->hDamage = XDamageCreate(pContext->damageDpy, (Pixmap)draw, XDamageReportRawRectangles);
+    if (!pContext->damageQueryFailed)
+    {
+        pGlxPixmap->hDamage = XDamageCreate(dpy, (Pixmap)draw, XDamageReportNonEmpty);
         /*crDebug("Create: Damage for drawable 0x%x, handle 0x%x (level=%i)",
                  (unsigned int) draw, (unsigned int) pGlxPixmap->damage, (int) XDamageReportRawRectangles);*/
@@ -2534,22 +2539,12 @@
 
     /* If there's damage extension, then process incoming events as we need the information right now */
-    if (context->damageDpy)
-    {
-        /* 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*/
+    if (!context->damageQueryFailed)
+    {
+        /* Sync connection */
         XLOCK(dpy);
         XSync(dpy, False);
         XUNLOCK(dpy);
-        XSync(context->damageDpy, False);
-
-        while (XPending(context->damageDpy))
-        {
-            XEvent event;
-            XNextEvent(context->damageDpy, &event);
-            if (event.type==context->damageEventsBase+XDamageNotify)
-            {
-                crHashtableWalk(context->pGLXPixmapsHash, stubCheckXDamageCB, &event);
-            }
-        }
+
+        stubFetchDamageOnDrawable(dpy, pGlxPixmap);
     }
 
@@ -2599,5 +2594,5 @@
     {
         /* Check if we have damage extension */
-        if (context->damageDpy)
+        if (!context->damageQueryFailed)
         {
             if (pGlxPixmap->bPixmapImageDirty)
Index: trunk/src/VBox/Additions/common/crOpenGL/stub.h
===================================================================
--- trunk/src/VBox/Additions/common/crOpenGL/stub.h	(revision 49196)
+++ trunk/src/VBox/Additions/common/crOpenGL/stub.h	(revision 49214)
@@ -147,6 +147,5 @@
     GLXContext glxContext;
     CRHashTable *pGLXPixmapsHash;
-    Bool     damageInitFailed;
-    Display *damageDpy; /* second display connection to read xdamage extension data */
+    Bool     damageQueryFailed;
     int      damageEventsBase;
 #endif
