Index: /trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m
===================================================================
--- /trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m	(revision 29426)
+++ /trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m	(revision 29427)
@@ -29,4 +29,5 @@
 //#define SHOW_WINDOW_BACKGROUND 1 /* Define this to see the window background even if the window is clipped */
 //#define DEBUG_VERBOSE /* Define this could get some debug info about the messages flow. */
+//#define DEBUG_poetzsch 1
 
 #ifdef DEBUG_poetzsch
@@ -56,5 +57,5 @@
     {
         GLenum g = glGetError();
-	    if (g != GL_NO_ERROR)
+        if (g != GL_NO_ERROR)
         {
             char *errStr;
@@ -509,4 +510,5 @@
      * call in the OpenGL thread. */
 //    [m_pOverlayView performSelector:@selector(reshape) onThread:m_Thread withObject:nil waitUntilDone:YES];
+    DEBUG_MSG(("parentWindowFrameChanged\n"));
     [m_pOverlayView reshape];
 }
@@ -515,4 +517,5 @@
 {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
+    DEBUG_MSG(("parentWindowChanged\n"));
     if(pWindow != nil)
     {
@@ -530,5 +533,4 @@
 //        [NSTimer scheduledTimerWithTimeInterval:0.2 target:m_pOverlayView selector:@selector(reshape) userInfo:nil repeats:NO];
         [m_pOverlayView reshape];
-
     }
 }
@@ -554,4 +556,6 @@
     m_FBOTexSize = NSZeroSize;
     m_FBODepthStencilPackedId = 0;
+    m_FBOThumbId = 0;
+    m_FBOThumbTexId = 0;
     m_cClipRects = 0;
     m_paClipRects = NULL;
@@ -650,6 +654,19 @@
 {
     m_Size = size;
-    [self reshape];
-    [self updateFBO];
+    
+    if (!m_FBOId)
+    {
+        DEBUG_MSG(("Set size (no fbo) %p\n", self));
+        [self reshape];
+        [self updateFBO];
+    }
+    else
+    {
+        DEBUG_MSG(("Set size FBO %p\n", self));
+        [self reshape];
+        [self updateFBO];
+        /* have to rebind GL_TEXTURE_RECTANGLE_ARB as m_FBOTexId could be changed in updateFBO call */
+        [self updateViewport];
+    }
 }
 
@@ -661,7 +678,9 @@
 - (void)updateViewport
 {
+    DEBUG_MSG(("updateViewport %p\n", self));
     if (m_pSharedGLCtx)
     {
         /* Update the viewport for our OpenGL view */
+        DEBUG_MSG(("MakeCurrent (shared) %X\n", m_pSharedGLCtx));
         [m_pSharedGLCtx makeCurrentContext];
         [m_pSharedGLCtx update];
@@ -686,4 +705,5 @@
         glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
 
+        DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
         [m_pGLCtx makeCurrentContext];
     }
@@ -692,4 +712,5 @@
 - (void)reshape
 {
+    DEBUG_MSG(("(%p)reshape %p\n", RTThreadSelf(), self));
     /* Getting the right screen coordinates of the parents frame is a little bit
      * complicated. */
@@ -745,7 +766,11 @@
 
 - (void)createFBO
-{    
+{
+    GLuint fboid = m_FBOId;
+    
+    DEBUG_MSG(("createFBO %p\n", self));
     [self deleteFBO];
 
+if (0&&!fboid)
     GL_SAVE_STATE;
 
@@ -769,5 +794,5 @@
         /* & the texture as well the depth/stencil render buffer */
         glGenTextures(1, &m_FBOTexId);
-        DEBUG_MSG_1(("Create FBO %d %d\n", m_FBOId, m_FBOTexId));
+        DEBUG_MSG(("Create FBO %d %d\n", m_FBOId, m_FBOTexId));
 
         glGenRenderbuffersEXT(1, &m_FBODepthStencilPackedId);
@@ -790,5 +815,5 @@
     if (m_FBOTexSize.width > maxTexSize || m_FBOTexSize.height > maxTexSize) 
     {
-        filter = GL_NICEST;
+        filter = GL_LINEAR;
         if (imageAspectRatio > 1)
         {
@@ -828,4 +853,5 @@
     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+    glDisable(GL_TEXTURE_RECTANGLE_ARB);
 
     /* Is there a dock tile preview enabled in the GUI? If so setup a
@@ -843,6 +869,6 @@
         /* Initialize FBO Texture */
         glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOThumbTexId);
-        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NICEST);
-        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NICEST);
+        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
         glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
@@ -875,5 +901,6 @@
     m_paClipRects[2] = m_FBOTexSize.width;
     m_paClipRects[3] = m_FBOTexSize.height;
-
+    
+if (0&&!fboid)
     GL_RESTORE_STATE;
 }
@@ -881,7 +908,19 @@
 - (void)deleteFBO
 {
-    if ([NSOpenGLContext currentContext] != nil)
-    {
-        GL_SAVE_STATE;
+    DEBUG_MSG(("deleteFBO %p\n", self));
+    if (m_pSharedGLCtx)
+    {
+        DEBUG_MSG(("MakeCurrent (shared) %X\n", m_pSharedGLCtx));
+        [m_pSharedGLCtx makeCurrentContext];
+        [m_pSharedGLCtx update];
+
+        glEnable(GL_TEXTURE_RECTANGLE_ARB);
+        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
+    }
+
+    if (m_pGLCtx)
+    {
+        DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
+        [m_pGLCtx makeCurrentContext];
 
         if (m_FBODepthStencilPackedId > 0)
@@ -899,12 +938,17 @@
         if (m_FBOId > 0)
         {
-            glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
-
+            GLint tmpFB;
+            glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &tmpFB);
+
+            if (tmpFB == m_FBOId)
+            {
+                glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+            }
+            
             glDeleteFramebuffersEXT(1, &m_FBOId);
             m_FBOId = 0;
         }
-
-        GL_RESTORE_STATE;
-    }
+    }
+
     if (m_DockTileView != nil)
     {
@@ -917,4 +961,5 @@
 - (void)updateFBO
 {
+    DEBUG_MSG(("updateFBO %p\n", self));
     [self makeCurrentFBO];
     
@@ -950,4 +995,5 @@
 //        if ([NSOpenGLContext currentContext] != m_pGLCtx)
         {
+            DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
             [m_pGLCtx makeCurrentContext];
             CHECK_GL_ERROR();
@@ -972,5 +1018,5 @@
     glFlush();
 //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
-	if (tmpFB == m_FBOId)
+    if (tmpFB == m_FBOId)
     {
         if ([self lockFocusIfCanDraw])
@@ -993,5 +1039,5 @@
 //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
     DEBUG_MSG_1 (("Flush GetINT %d\n", tmpFB));
-	if (tmpFB == m_FBOId)
+    if (tmpFB == m_FBOId)
     {
         if ([self lockFocusIfCanDraw])
@@ -1011,5 +1057,5 @@
         //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
     DEBUG_MSG_1 (("Finish GetINT %d\n", tmpFB));
-	if (tmpFB == m_FBOId)
+    if (tmpFB == m_FBOId)
     {
         if ([self lockFocusIfCanDraw])
@@ -1051,6 +1097,14 @@
         if (m_FBOTexId > 0)
         {
+            if ([m_pSharedGLCtx view] != self)
+            {
+                DEBUG_MSG(("renderFBOToView: not currect view of shared ctx!"));
+                [m_pSharedGLCtx setView: self];
+                [self updateViewport];
+            }
+
+            //DEBUG_MSG(("MakeCurrent (shared) %X\n", m_pSharedGLCtx));
             [m_pSharedGLCtx makeCurrentContext];
-        
+       
             if (m_FBOThumbTexId > 0 &&
                 [m_DockTileView thumbBitmap] != nil)
@@ -1146,4 +1200,7 @@
             glClear(GL_COLOR_BUFFER_BIT);
 
+            glEnable(GL_TEXTURE_RECTANGLE_ARB);
+            glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
+
             /* Blit the content of the FBO to the screen. todo: check for
              * optimization with display lists. */
@@ -1166,4 +1223,5 @@
             glFinish();
             [m_pSharedGLCtx flushBuffer];
+            //DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
             [m_pGLCtx makeCurrentContext];
         }
@@ -1457,4 +1515,6 @@
     NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
 
+    DEBUG_MSG(("cocoaViewMakeCurrentContext(%p, %p)\n", pView, pCtx));
+    
     [(OverlayView*)pView setGLCtx:pCtx];
     [(OverlayView*)pView makeCurrentFBO];
@@ -1532,5 +1592,5 @@
     NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
 
-    DEBUG_MSG_1(("glRenderspuBindFramebufferEXT called %d\n", framebuffer));
+    DEBUG_MSG(("glRenderspuBindFramebufferEXT called %d\n", framebuffer));
 
 #ifdef FBO
