Index: /trunk/src/VBox/Frontends/VirtualBox/include/VBoxFrameBuffer.h
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/include/VBoxFrameBuffer.h	(revision 22797)
+++ /trunk/src/VBox/Frontends/VirtualBox/include/VBoxFrameBuffer.h	(revision 22798)
@@ -939,5 +939,5 @@
     }
 
-    size_t size() {return mSurfaces.size(); }
+    size_t size() const {return mSurfaces.size(); }
 
     void remove(VBoxVHWASurfaceBase *pSurf)
@@ -998,5 +998,5 @@
     }
 
-    VBoxVHWASurfaceBase * getVGA()
+    VBoxVHWASurfaceBase * getVGA() const
     {
         return mSurfVGA;
@@ -1485,16 +1485,17 @@
     void vboxDoVHWACmdExec(void *cmd);
     void vboxShowOverlay(bool show);
-    bool vboxDoCheckUpdateViewport();
+    void vboxDoCheckUpdateViewport();
     void vboxDoVHWACmd(void *cmd);
     void vboxDoUpdateRect(const QRect * pRect);
-    void vboxUpdateOverlayPosition(const QPoint & pos);
-    void vboxUpdateOverlay(const QRect & rect, bool show);
+//    void vboxUpdateOverlayPosition(const QPoint & pos);
+    void vboxCheckUpdateOverlay(const QRect & rect);
     VBoxVHWACommandElement * processCmdList(VBoxVHWACommandElement * pCmd);
     VBoxGLWidget *mpOverlayWidget;
     bool mGlOn;
+    bool mOverlayWidgetVisible;
     bool mOverlayVisible;
     bool mGlCurrent;
     bool mProcessingCommands;
-    QRect mOverlayViewportCoords;
+    QRect mOverlayViewport;
     VBoxVHWADirtyRect mMainDirtyRect;
 
Index: /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBQGL.cpp
===================================================================
--- /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBQGL.cpp	(revision 22797)
+++ /trunk/src/VBox/Frontends/VirtualBox/src/VBoxFBQGL.cpp	(revision 22798)
@@ -4663,5 +4663,9 @@
 bool VBoxGLWidget::hasSurfaces() const
 {
-    return mDisplay.overlays().size() != 0;
+    if(mDisplay.overlays().size() != 0)
+        return true;
+    if(mDisplay.primaries().size() > 1)
+        return true;
+    return mDisplay.getVGA()->handle() != VBOXVHWA_SURFHANDLE_INVALID;
 }
 
@@ -5231,4 +5235,5 @@
     : VBoxQImageFrameBuffer(aView),
       mGlOn(false),
+      mOverlayWidgetVisible(false),
       mOverlayVisible(false),
       mGlCurrent(false),
@@ -5237,5 +5242,6 @@
 {
     mpOverlayWidget = new VBoxGLWidget (aView, aView->viewport());
-    mpOverlayWidget->setVisible(false);
+    mOverlayWidgetVisible = true; /* to ensure it is set hidden with vboxShowOverlay */
+    vboxShowOverlay(false);
 }
 
@@ -5292,21 +5298,10 @@
 }
 
-bool VBoxQGLOverlayFrameBuffer::vboxDoCheckUpdateViewport()
-{
-    Assert(0);
-//    QRect vp(mView->contentsX(), mView->contentsY(), mpOverlayWidget->width(), mpOverlayWidget->height());
-//    if(vp != mpOverlayWidget->vboxViewport())
-//    {
-//        mpOverlayWidget->vboxDoUpdateViewport(vp);
-//        return true;
-//    }
-    return false;
-}
-
 void VBoxQGLOverlayFrameBuffer::paintEvent (QPaintEvent *pe)
 {
-    if(mOverlayVisible && !mProcessingCommands)
+    if(mOverlayWidgetVisible && !mProcessingCommands)
     {
         Assert(!mGlCurrent);
+        vboxDoCheckUpdateViewport();
         vboxOpExit();
     }
@@ -5347,9 +5342,4 @@
         makeCurrent();
         mpOverlayWidget->vboxDoUpdateRect(pRect);
-//        if(mOverlayVisible)
-//        {
-//            mpOverlayWidget->performDisplay();
-//            mpOverlayWidget->swapBuffers();
-//        }
         vboxOpExit();
     }
@@ -5391,5 +5381,6 @@
         Assert(!mpOverlayWidget->isVisible());
         /* just to ensure */
-        mpOverlayWidget->setVisible(false);
+        vboxShowOverlay(false);
+        mOverlayVisible = false;
         vboxSynchGl();
     }
@@ -5397,34 +5388,75 @@
     {
         VBOXQGLLOGREL(("Switching Gl mode off\n"));
+        mOverlayVisible = false;
+        vboxShowOverlay(false);
         /* for now just set the flag w/o destroying anything */
     }
 }
 
+void VBoxQGLOverlayFrameBuffer::vboxDoCheckUpdateViewport()
+{
+    Assert(0);
+    int cX = mView->contentsX();
+    int cY = mView->contentsY();
+    QRect fbVp(cX, cY, mView->viewport()->width(), mView->viewport()->height());
+    QRect overVp = fbVp.intersected(mOverlayViewport);
+
+    if(overVp.isEmpty())
+    {
+        vboxShowOverlay(false);
+    }
+    else
+    {
+        if(overVp != mpOverlayWidget->vboxViewport())
+        {
+            makeCurrent();
+            mpOverlayWidget->vboxDoUpdateViewport(overVp);
+        }
+
+        QRect rect(overVp.x() - cX, overVp.y() - cY, overVp.width(), overVp.height());
+
+        vboxCheckUpdateOverlay(rect);
+
+        vboxShowOverlay(true);
+    }
+}
+
 void VBoxQGLOverlayFrameBuffer::vboxShowOverlay(bool show)
 {
-    mpOverlayWidget->setVisible(show);
-    mOverlayVisible = show;
-}
-
-void VBoxQGLOverlayFrameBuffer::vboxUpdateOverlayPosition(const QPoint & pos)
-{
-    makeCurrent();
-
-    mpOverlayWidget->move(pos);
-
-    /* */
-    QRect rect = mpOverlayWidget->vboxViewport();
-    rect.moveTo(pos);
-    mpOverlayWidget->vboxDoUpdateViewport(rect);
-}
-
-void VBoxQGLOverlayFrameBuffer::vboxUpdateOverlay(const QRect & rect, bool show)
-{
-    mpOverlayWidget->move(rect.x(), rect.y());
-    mpOverlayWidget->resize(rect.width(), rect.height());
-
-    mpOverlayWidget->vboxDoUpdateViewport(rect);
-
-    vboxShowOverlay(show);
+    if(mOverlayWidgetVisible != show)
+    {
+        mpOverlayWidget->setVisible(show);
+        mOverlayWidgetVisible = show;
+    }
+}
+
+//void VBoxQGLOverlayFrameBuffer::vboxUpdateOverlayPosition(const QPoint & pos)
+//{
+////    makeCurrent();
+//
+//    mpOverlayWidget->move(pos);
+//
+////    /* */
+////    QRect rect = mpOverlayWidget->vboxViewport();
+////    rect.moveTo(pos);
+////    mpOverlayWidget->vboxDoUpdateViewport(rect);
+//}
+
+void VBoxQGLOverlayFrameBuffer::vboxCheckUpdateOverlay(const QRect & rect)
+{
+    QRect overRect = mpOverlayWidget->rect();
+    if(overRect.x() != rect.x() || overRect.y() != rect.y())
+    {
+        mpOverlayWidget->move(rect.x(), rect.y());
+    }
+
+    if(overRect.width() != rect.width() || overRect.height() != rect.height())
+    {
+        mpOverlayWidget->resize(rect.width(), rect.height());
+    }
+
+//    mpOverlayWidget->vboxDoUpdateViewport(rect);
+//
+//    vboxShowOverlay(show);
 }
 
@@ -5449,8 +5481,11 @@
                 vboxSetGlOn(false);
             }
-            else if(mpOverlayWidget->hasVisibleOverlays())
+            else
             {
-                QRect overRect = mpOverlayWidget->overlaysRectUnion();
-                vboxUpdateOverlay(overRect, true);
+                mOverlayVisible = mpOverlayWidget->hasVisibleOverlays();
+                if(mOverlayVisible)
+                {
+                    mOverlayViewport = mpOverlayWidget->overlaysRectUnion();
+                }
             }
         } break;
@@ -5463,4 +5498,12 @@
                 vboxSetGlOn(false);
             }
+            else
+            {
+                mOverlayVisible = mpOverlayWidget->hasVisibleOverlays();
+                if(mOverlayVisible)
+                {
+                    mOverlayViewport = mpOverlayWidget->overlaysRectUnion();
+                }
+            }
         } break;
         case VBOXVHWACMD_TYPE_SURF_LOCK:
@@ -5488,13 +5531,10 @@
             VBOXVHWACMD_SURF_OVERLAY_UPDATE * pBody = VBOXVHWACMD_BODY(pCmd, VBOXVHWACMD_SURF_OVERLAY_UPDATE);
             pCmd->rc = mpOverlayWidget->vhwaSurfaceOverlayUpdate(pBody);
-            if(mpOverlayWidget->hasVisibleOverlays())
+            mOverlayVisible = mpOverlayWidget->hasVisibleOverlays();
+            if(mOverlayVisible)
             {
-                QRect overRect = mpOverlayWidget->overlaysRectUnion();
-                vboxUpdateOverlay(overRect, true);
+                mOverlayViewport = mpOverlayWidget->overlaysRectUnion();
             }
-            else
-            {
-                vboxShowOverlay(false);
-            }
+            vboxDoCheckUpdateViewport();
         } break;
         case VBOXVHWACMD_TYPE_SURF_OVERLAY_SETPOSITION:
@@ -5502,9 +5542,10 @@
             VBOXVHWACMD_SURF_OVERLAY_SETPOSITION * pBody = VBOXVHWACMD_BODY(pCmd, VBOXVHWACMD_SURF_OVERLAY_SETPOSITION);
             pCmd->rc = mpOverlayWidget->vhwaSurfaceOverlaySetPosition(pBody);
-            if(mpOverlayWidget->hasVisibleOverlays())
+            mOverlayVisible = mpOverlayWidget->hasVisibleOverlays();
+            if(mOverlayVisible)
             {
-                QRect overRect = mpOverlayWidget->overlaysRectUnion();
-                vboxUpdateOverlayPosition(QPoint(overRect.x(), overRect.y()));
+                mOverlayViewport = mpOverlayWidget->overlaysRectUnion();
             }
+            vboxDoCheckUpdateViewport();
         } break;
         case VBOXVHWACMD_TYPE_SURF_COLORKEY_SET:
