VirtualBox

Ignore:
Timestamp:
Jun 6, 2011 4:41:41 PM (14 years ago)
Author:
vboxsync
Message:

OpenGl-OSX: introduce double buffer support for FBO (Gnome3 fix); use glBlitFramebufferEXT for drawing; cleanup

Location:
trunk/src/VBox/HostServices/SharedOpenGL/render
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.c

    r33977 r37332  
    10771077    renderspu_SystemBindFramebufferEXT(target, framebuffer);
    10781078}
     1079
     1080void renderspuCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type)
     1081{
     1082    renderspu_SystemCopyPixels(x, y, width, height, type);
     1083}
     1084
     1085void renderspuGetIntegerv(GLenum pname, GLint * params)
     1086{
     1087    renderspu_SystemGetIntegerv(pname, params);
     1088}
     1089
     1090void renderspuDrawBuffer(GLenum mode)
     1091{
     1092    renderspu_SystemDrawBuffer(mode);
     1093}
     1094
     1095void renderspuReadBuffer(GLenum mode)
     1096{
     1097    printf("read win id: %d\n", render_spu.window_id);
     1098    renderspu_SystemReadBuffer(mode);
     1099}
    10791100# endif
    10801101#endif
     
    11211142    FILLIN( "Finish", renderspuFinish );
    11221143    FILLIN( "BindFramebufferEXT", renderspuBindFramebufferEXT );
     1144    FILLIN( "CopyPixels", renderspuCopyPixels );
     1145    FILLIN( "GetIntegerv", renderspuGetIntegerv );
     1146    FILLIN( "ReadBuffer", renderspuReadBuffer );
     1147    FILLIN( "DrawBuffer", renderspuDrawBuffer );
    11231148# endif
    11241149#endif
  • trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu.h

    r33991 r37332  
    303303extern void renderspu_SystemFinish();
    304304extern void renderspu_SystemBindFramebufferEXT(GLenum target, GLuint framebuffer);
     305extern void renderspu_SystemCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type);
     306extern void renderspu_SystemGetIntegerv(GLenum pname, GLint *params);
     307extern void renderspu_SystemReadBuffer(GLenum mode);
     308extern void renderspu_SystemDrawBuffer(GLenum mode);
    305309# endif
    306310#endif
  • trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa.c

    r33806 r37332  
    201201}
    202202
     203void renderspu_SystemCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type)
     204{
     205    cocoaCopyPixels(x, y, width, height, type);
     206}
     207
     208void renderspu_SystemGetIntegerv(GLenum pname, GLint * params)
     209{
     210    cocoaGetIntegerv(pname, params);
     211}
     212
     213void renderspu_SystemReadBuffer(GLenum mode)
     214{
     215    cocoaReadBuffer(mode);
     216}
     217
     218void renderspu_SystemDrawBuffer(GLenum mode)
     219{
     220    cocoaDrawBuffer(mode);
     221}
     222
  • trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.h

    r30126 r37332  
    4949void cocoaFinish(void);
    5050void cocoaBindFramebufferEXT(GLenum target, GLuint framebuffer);
     51void cocoaCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type);
     52void cocoaGetIntegerv(GLenum pname, GLint *params);
     53void cocoaReadBuffer(GLenum mode);
     54void cocoaDrawBuffer(GLenum mode);
    5155
    5256RT_C_DECLS_END
  • trunk/src/VBox/HostServices/SharedOpenGL/render/renderspu_cocoa_helper.m

    r36735 r37332  
    2727#include <iprt/time.h>
    2828
     29/*
     30 * How this works:
     31 * In general it is not so easy like on the other platforms, cause Cocoa
     32 * doesn't support any clipping of already painted stuff. In Mac OS X there is
     33 * the concept of translucent canvas's e.g. windows and there it is just
     34 * painted what should be visible to the user. Unfortunately this isn't the
     35 * concept of chromium. Therefor I reroute all OpenGL operation from the guest
     36 * to a frame buffer object (FBO). This is a OpenGL extension, which is
     37 * supported by all OS X versions we support (AFAIC tell). Of course the guest
     38 * doesn't know that and we have to make sure that the OpenGL state always is
     39 * in the right state to paint into the FBO and not to the front/back buffer.
     40 * Several functions below (like cocoaBindFramebufferEXT, cocoaGetIntegerv,
     41 * ...) doing this. When a swap or finish is triggered by the guest, the
     42 * content (which is already bound to an texture) is painted on the screen
     43 * within a separate OpenGL context. This allows the usage of the same
     44 * resources (texture ids, buffers ...) but at the same time having an
     45 * different internal OpenGL state. Another advantage is that we can paint a
     46 * thumbnail of the current output in a much more smaller (GPU accelerated
     47 * scale) version on a third context and use glReadPixels to get the actual
     48 * data. glReadPixels is a very slow operation, but as we just use a much more
     49 * smaller image, we can handle it (anyway this is only done 5 times per
     50 * second).
     51 *
     52 * Other things to know:
     53 * - If the guest request double buffering, we have to make sure there are two
     54 *   buffers. We use the same FBO with 2 color attachments. Also glDrawBuffer
     55 *   and glReadBuffer is intercepted to make sure it is painted/read to/from
     56 *   the correct buffers. On swap our buffers are swapped and not the
     57 *   front/back buffer.
     58 * - If the guest request a depth/stencil buffer, a combined render buffer for
     59 *   this is created.
     60 * - If the size of the guest OpenGL window changes, all FBO's, textures, ...
     61 *   need to be recreated.
     62 * - We need to track any changes to the parent window
     63 *   (create/destroy/move/resize). The various classes like OverlayHelperView,
     64 *   OverlayWindow, ... are there for.
     65 * - The HGCM service runs on a other thread than the Main GUI. Keeps this
     66 *   always in mind (see e.g. performSelectorOnMainThread in renderFBOToView)
     67 * - We make heavy use of late binding. We can not be sure that the GUI (or any
     68 *   other third party GUI), overwrite our NSOpenGLContext. So we always ask if
     69 *   this is our own one, before use. Really neat concept of Objective-C/Cocoa
     70 *   ;)
     71 */
     72
    2973/* Debug macros */
    3074#define FBO 1 /* Disable this to see how the output is without the FBO in the middle of the processing chain. */
    3175//#define SHOW_WINDOW_BACKGROUND 1 /* Define this to see the window background even if the window is clipped */
    32 //#define DEBUG_VERBOSE /* Define this could get some debug info about the messages flow. */
     76//#define DEBUG_VERBOSE /* Define this to get some debug info about the messages flow. */
    3377
    3478#ifdef DEBUG_poetzsch
     
    63107            switch (g)
    64108            {
    65                 case GL_INVALID_ENUM: errStr = RTStrDup("GL_INVALID_ENUM"); break;
    66                 case GL_INVALID_VALUE: errStr = RTStrDup("GL_INVALID_VALUE"); break;
     109                case GL_INVALID_ENUM:      errStr = RTStrDup("GL_INVALID_ENUM"); break;
     110                case GL_INVALID_VALUE:     errStr = RTStrDup("GL_INVALID_VALUE"); break;
    67111                case GL_INVALID_OPERATION: errStr = RTStrDup("GL_INVALID_OPERATION"); break;
    68                 case GL_STACK_OVERFLOW: errStr = RTStrDup("GL_STACK_OVERFLOW"); break;
    69                 case GL_STACK_UNDERFLOW: errStr = RTStrDup("GL_STACK_UNDERFLOW"); break;
    70                 case GL_OUT_OF_MEMORY: errStr = RTStrDup("GL_OUT_OF_MEMORY"); break;
    71                 case GL_TABLE_TOO_LARGE: errStr = RTStrDup("GL_TABLE_TOO_LARGE"); break;
    72                 default: errStr = RTStrDup("UNKNOWN"); break;
     112                case GL_STACK_OVERFLOW:    errStr = RTStrDup("GL_STACK_OVERFLOW"); break;
     113                case GL_STACK_UNDERFLOW:   errStr = RTStrDup("GL_STACK_UNDERFLOW"); break;
     114                case GL_OUT_OF_MEMORY:     errStr = RTStrDup("GL_OUT_OF_MEMORY"); break;
     115                case GL_TABLE_TOO_LARGE:   errStr = RTStrDup("GL_TABLE_TOO_LARGE"); break;
     116                default:                   errStr = RTStrDup("UNKNOWN"); break;
    73117            }
    74118            DEBUG_MSG(("%s:%d: glError %d (%s)\n", file, line, g, errStr));
     
    144188    RTTHREAD         mThread;
    145189
     190#ifdef FBO
     191    GLuint           m_FBOId;
    146192    /* FBO handling */
    147     GLuint           m_FBOId;
    148     GLuint           m_FBOTexId;
     193    GLuint           m_FBOTexBackId;
     194    GLuint           m_FBOTexFrontId;
     195    GLuint           m_FBOAttBackId;
     196    GLuint           m_FBOAttFrontId;
     197    GLuint           m_FBODepthStencilPackedId;
    149198    NSSize           m_FBOTexSize;
    150     GLuint           m_FBODepthStencilPackedId;
     199
     200    bool             m_fFrontDrawing;
     201#endif
    151202
    152203    /* The corresponding dock tile view of this OpenGL view & all helper
    153204     * members. */
    154205    DockOverlayView *m_DockTileView;
     206
    155207    GLuint           m_FBOThumbId;
    156208    GLuint           m_FBOThumbTexId;
     
    189241- (void)deleteFBO;
    190242
     243- (bool)isCurrentFBO;
    191244- (void)updateFBO;
    192245- (void)makeCurrentFBO;
    193246- (void)swapFBO;
    194247- (void)flushFBO;
     248- (void)stateInfo:(GLenum)pname withParams:(GLint*)params;
    195249- (void)finishFBO;
    196 - (void)bindFBO;
     250- (void)bindFBO:(GLenum)target withFrameBuffer:(GLuint)framebuffer;
     251- (void)tryDraw;
    197252- (void)renderFBOToView;
     253- (void)renderFBOToDockTile;
    198254
    199255- (void)clearVisibleRegions;
     
    317373            isPlanar:NO
    318374            colorSpaceName:NSDeviceRGBColorSpace
     375            bitmapFormat:NSAlphaFirstBitmapFormat
    319376            bytesPerRow:frame.size.width * 4
    320377            bitsPerPixel:8 * 4];
     
    332389- (void)drawRect:(NSRect)aRect
    333390{
     391    NSRect frame;
     392
    334393    [self lock];
    335394#ifdef SHOW_WINDOW_BACKGROUND
    336395    [[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.7] set];
    337     NSRect frame = [self frame];
    338     [NSBezierPath fillRect:NSMakeRect(0, 0, frame.size.width, frame.size.height)]; 
     396    frame = [self frame];
     397    [NSBezierPath fillRect:NSMakeRect(0, 0, frame.size.width, frame.size.height)];
    339398#endif /* SHOW_WINDOW_BACKGROUND */
    340399    if (m_ThumbImage != nil)
     
    370429        m_pPixelFormat = format;
    371430
     431    DEBUG_MSG(("OCTX(%p): init OverlayOpenGLContext\n", (void*)self));
     432
    372433    return self;
    373434}
    374    
     435
    375436- (void)dealloc
    376437{
    377     DEBUG_MSG(("Dealloc context %X\n", (uint)self));
     438    DEBUG_MSG(("OCTX(%p): dealloc OverlayOpenGLContext\n", (void*)self));
    378439
    379440    [m_pPixelFormat release];
     
    391452-(void)setView:(NSView*)view
    392453{
     454    DEBUG_MSG(("OCTX(%p): setView: new view: %p\n", (void*)self, (void*)view));
     455
    393456#ifdef FBO
    394457    m_pView = view;;
     
    409472-(void)clearDrawable
    410473{
     474    DEBUG_MSG(("OCTX(%p): clearDrawable\n", (void*)self));
     475
    411476    m_pView = NULL;;
    412477    [super clearDrawable];
     
    418483}
    419484
    420 @end;
     485@end
    421486
    422487/********************************************************************************
     
    433498    m_pOverlayWindow = pOverlayWindow;
    434499
     500    DEBUG_MSG(("OHVW(%p): init OverlayHelperView\n", (void*)self));
     501
    435502    return self;
    436503}
     
    438505-(void)viewDidMoveToWindow
    439506{
     507    DEBUG_MSG(("OHVW(%p): viewDidMoveToWindow: new win: %p\n", (void*)self, (void*)[self window]));
     508
    440509    [m_pOverlayWindow parentWindowChanged:[self window]];
    441510}
     
    452521- (id)initWithParentView:(NSView*)pParentView overlayView:(OverlayView*)pOverlayView
    453522{
    454     if(self = [super initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO])
     523    NSWindow *pParentWin = nil;
     524
     525    if((self = [super initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO]))
    455526    {
    456527        m_pParentView = pParentView;
     
    476547        [self setIgnoresMouseEvents:YES];
    477548
    478         NSWindow *pParentWin = [m_pParentView window];
     549        pParentWin = [m_pParentView window];
    479550
    480551        /* Initial set the position to the parents view top/left (Compiz fix). */
     
    488559        /* Add ourself as a child to the parent views window. Note: this has to
    489560         * be done last so that everything else is setup in
    490          * parentWindowChanged. */ 
     561         * parentWindowChanged. */
    491562        [pParentWin addChildWindow:self ordered:NSWindowAbove];
    492563    }
     564    DEBUG_MSG(("OWIN(%p): init OverlayWindow\n", (void*)self));
     565
    493566    return self;
    494567}
     
    496569- (void)dealloc
    497570{
    498     DEBUG_MSG(("Dealloc window %X\n", (uint)self));
     571    DEBUG_MSG(("OWIN(%p): dealloc OverlayWindow\n", (void*)self));
    499572
    500573    [[NSNotificationCenter defaultCenter] removeObserver:self];
     
    508581- (void)parentWindowFrameChanged:(NSNotification*)pNote
    509582{
     583    DEBUG_MSG(("OWIN(%p): parentWindowFrameChanged\n", (void*)self));
     584
    510585    /* Reposition this window with the help of the OverlayView. Perform the
    511586     * call in the OpenGL thread. */
    512 //    [m_pOverlayView performSelector:@selector(reshape) onThread:m_Thread withObject:nil waitUntilDone:YES];
    513     DEBUG_MSG(("parentWindowFrameChanged\n"));
     587    /*
     588    [m_pOverlayView performSelector:@selector(reshape) onThread:m_Thread withObject:nil waitUntilDone:YES];
     589    */
     590
    514591    [m_pOverlayView reshape];
    515592}
     
    517594- (void)parentWindowChanged:(NSWindow*)pWindow
    518595{
     596    DEBUG_MSG(("OWIN(%p): parentWindowChanged\n", (void*)self));
     597
    519598    [[NSNotificationCenter defaultCenter] removeObserver:self];
    520     DEBUG_MSG(("parentWindowChanged\n"));
     599
    521600    if(pWindow != nil)
    522601    {
     
    531610        /* Reshape the overlay view after a short waiting time to let the main
    532611         * window resize itself properly. */
    533 //        [m_pOverlayView performSelector:@selector(reshape) withObject:nil afterDelay:0.2];
    534 //        [NSTimer scheduledTimerWithTimeInterval:0.2 target:m_pOverlayView selector:@selector(reshape) userInfo:nil repeats:NO];
     612        /*
     613        [m_pOverlayView performSelector:@selector(reshape) withObject:nil afterDelay:0.2];
     614        [NSTimer scheduledTimerWithTimeInterval:0.2 target:m_pOverlayView selector:@selector(reshape) userInfo:nil repeats:NO];
     615        */
    535616        [m_pOverlayView reshape];
    536617    }
     
    548629- (id)initWithFrame:(NSRect)frame thread:(RTTHREAD)aThread parentView:(NSView*)pParentView
    549630{
    550     m_pParentView = pParentView;
     631    m_pParentView             = pParentView;
    551632    /* Make some reasonable defaults */
    552     m_pGLCtx = NULL;
    553     m_pSharedGLCtx = NULL;
    554     mThread = aThread;
    555     m_FBOId = 0;
    556     m_FBOTexId = 0;
    557     m_FBOTexSize = NSZeroSize;
     633    m_pGLCtx                  = nil;
     634    m_pSharedGLCtx            = nil;
     635    mThread                   = aThread;
     636#ifdef FBO
     637    m_FBOId                   = 0;
     638    m_FBOTexBackId            = 0;
     639    m_FBOTexFrontId           = 0;
     640    m_FBOAttBackId            = GL_COLOR_ATTACHMENT0_EXT;
     641    m_FBOAttFrontId           = GL_COLOR_ATTACHMENT1_EXT;
    558642    m_FBODepthStencilPackedId = 0;
    559     m_FBOThumbId = 0;
    560     m_FBOThumbTexId = 0;
    561     m_cClipRects = 0;
    562     m_paClipRects = NULL;
    563     m_Pos = NSZeroPoint;
    564     m_Size = NSZeroSize;
    565     m_RootShift = NSZeroPoint;
    566 
    567     DEBUG_MSG(("Init view %X (%X)\n", (uint)self, (uint)mThread));
    568    
     643    m_FBOTexSize              = NSZeroSize;
     644#endif
     645    m_FBOThumbId              = 0;
     646    m_FBOThumbTexId           = 0;
     647    m_cClipRects              = 0;
     648    m_paClipRects             = NULL;
     649    m_Pos                    = NSZeroPoint;
     650    m_Size                    = NSMakeSize(1, 1);
     651    m_RootShift               = NSZeroPoint;
     652
    569653    self = [super initWithFrame:frame];
    570654
     655    DEBUG_MSG(("OVIW(%p): init OverlayView\n", (void*)self));
     656
    571657    return self;
    572658}
     
    574660- (void)dealloc
    575661{
    576     DEBUG_MSG(("Dealloc view %X\n", (uint)self));
     662    DEBUG_MSG(("OVIW(%p): dealloc OverlayView\n", (void*)self));
    577663
    578664    [self deleteFBO];
     
    580666    if (m_pGLCtx)
    581667    {
    582         if ([m_pGLCtx view] == self) 
     668        if ([m_pGLCtx view] == self)
    583669            [m_pGLCtx clearDrawable];
    584670    }
    585671    if (m_pSharedGLCtx)
    586672    {
    587         if ([m_pSharedGLCtx view] == self) 
     673        if ([m_pSharedGLCtx view] == self)
    588674            [m_pSharedGLCtx clearDrawable];
    589675
     
    598684- (void)drawRect:(NSRect)aRect
    599685{
    600 //    NSGraphicsContext*pC = [NSGraphicsContext currentContext];
    601 //    [[NSColor blueColor] set];
    602 //    NSBezierPath *p = [[NSBezierPath alloc] bezierPathWithOvalInRect:[self frame]];
    603 //    [p fill];
    604 //    [[NSColor greenColor] set];
    605 //    [p stroke];
    606 //    if ([self lockFocusIfCanDraw])
    607 //    {
    608 //        [self renderFBOToView];
    609 //        [self unlockFocus];
    610 //    }
     686    /* Do nothing */
    611687}
    612688
    613689- (void)setGLCtx:(NSOpenGLContext*)pCtx
    614690{
     691    DEBUG_MSG(("OVIW(%p): setGLCtx: new ctx: %p\n", (void*)self, (void*)pCtx));
     692
    615693    m_pGLCtx = pCtx;
    616694}
     
    626704}
    627705
    628 - (void)setParentView: (NSView*)view
    629 {
    630     m_pParentView = view;
    631 }
    632 
    633 - (void)setOverlayWin: (NSWindow*)win
    634 {
    635     m_pOverlayWin = win;
     706- (void)setParentView:(NSView*)pView
     707{
     708    DEBUG_MSG(("OVIW(%p): setParentView: new view: %p\n", (void*)self, (void*)pView));
     709
     710    m_pParentView = pView;
     711}
     712
     713- (void)setOverlayWin:(NSWindow*)pWin
     714{
     715    DEBUG_MSG(("OVIW(%p): setOverlayWin: new win: %p\n", (void*)self, (void*)pWin));
     716
     717    m_pOverlayWin = pWin;
    636718}
    637719
     
    643725- (void)setPos:(NSPoint)pos
    644726{
     727    DEBUG_MSG(("OVIW(%p): setPos: new pos: %d, %d\n", (void*)self, (int)pos.x, (int)pos.y));
     728
    645729    m_Pos = pos;
     730
    646731    [self reshape];
    647732}
     
    655740{
    656741    m_Size = size;
    657    
    658     if (!m_FBOId)
    659     {
    660         DEBUG_MSG(("Set size (no fbo) %p\n", self));
    661         [self reshape];
    662         [self updateFBO];
    663     }
    664     else
    665     {
    666         DEBUG_MSG(("Set size FBO %p\n", self));
     742
     743#ifdef FBO
     744    if (m_FBOId)
     745    {
     746        DEBUG_MSG(("OVIW(%p): setSize: new size: %dx%d\n", (void*)self, (int)size.width, (int)size.height));
    667747        [self reshape];
    668748        [self updateFBO];
     
    670750        [self updateViewport];
    671751    }
     752    else
     753#endif
     754    {
     755        DEBUG_MSG(("OVIW(%p): setSize (no FBO): new size: %dx%d\n", (void*)self, (int)size.width, (int)size.height));
     756        [self reshape];
     757        [self updateFBO];
     758    }
    672759}
    673760
     
    679766- (void)updateViewport
    680767{
    681     DEBUG_MSG(("updateViewport %p\n", self));
     768    NSRect r;
     769
     770    DEBUG_MSG(("OVIW(%p): updateViewport\n", (void*)self));
     771
     772#ifdef FBO
    682773    if (m_pSharedGLCtx)
    683774    {
    684775        /* Update the viewport for our OpenGL view */
    685         DEBUG_MSG(("MakeCurrent (shared) %X\n", m_pSharedGLCtx));
     776        DEBUG_MSG(("OVIW(%p): makeCurrent (shared) %p\n", (void*)self, (void*)m_pSharedGLCtx));
    686777        [m_pSharedGLCtx makeCurrentContext];
    687778        [m_pSharedGLCtx update];
    688779
    689         NSRect r = [self frame];
     780        r = [self frame];
    690781        /* Setup all matrices */
    691782        glMatrixMode(GL_PROJECTION);
     
    693784        glViewport(0, 0, r.size.width, r.size.height);
    694785        glOrtho(0, r.size.width, 0, r.size.height, -1, 1);
    695         DEBUG_MSG_1(("frame[%i, %i, %i, %i]\n", (int)r.origin.x, (int)r.origin.x, (int)r.size.width, (int)r.size.height));
    696         DEBUG_MSG_1(("m_Pos(%i,%i) m_Size(%i,%i)\n", (int)m_Pos.x, (int)m_Pos.y, (int)m_Size.width, (int)m_Size.height));
    697         DEBUG_MSG_1(("m_RootShift(%i, %i)\n", (int)m_RootShift.x, (int)m_RootShift.y));
     786        DEBUG_MSG_1(("OVIW(%p): frame[%i, %i, %i, %i]\n", (void*)self, (int)r.origin.x, (int)r.origin.x, (int)r.size.width, (int)r.size.height));
     787        DEBUG_MSG_1(("OVIW(%p): m_Pos(%i,%i) m_Size(%i,%i)\n", (void*)self, (int)m_Pos.x, (int)m_Pos.y, (int)m_Size.width, (int)m_Size.height));
     788        DEBUG_MSG_1(("OVIW(%p): m_RootShift(%i, %i)\n", (void*)self, (int)m_RootShift.x, (int)m_RootShift.y));
    698789        glMatrixMode(GL_TEXTURE);
    699790        glLoadIdentity();
    700         glTranslatef(0.0f, m_RootShift.y, 0.0f);
    701791        glMatrixMode(GL_MODELVIEW);
    702792        glLoadIdentity();
    703         glTranslatef(-m_RootShift.x, 0.0f, 0.0f);
    704793
    705794        /* Clear background to transparent */
    706795        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    707796
    708         glEnable(GL_TEXTURE_RECTANGLE_ARB);
    709         glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
    710 
    711         DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
     797        glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, m_FBOId);
     798        glReadBuffer(m_FBOAttFrontId);
     799        glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
     800        glDrawBuffer(GL_BACK);
     801
     802        DEBUG_MSG(("OVIW(%p): makeCurrent (non shared) %p\n", (void*)self, (void*)m_pGLCtx));
    712803        [m_pGLCtx makeCurrentContext];
    713804    }
     805#endif
    714806}
    715807
    716808- (void)reshape
    717809{
    718     DEBUG_MSG(("(%p)reshape %p\n", RTThreadSelf(), self));
     810    NSRect parentFrame = NSZeroRect;
     811    NSPoint parentPos  = NSZeroPoint;
     812    NSPoint childPos   = NSZeroPoint;
     813    NSRect childFrame  = NSZeroRect;
     814    NSRect newFrame    = NSZeroRect;
     815
     816    DEBUG_MSG(("OVIW(%p): reshape\n", (void*)self));
     817
    719818    /* Getting the right screen coordinates of the parents frame is a little bit
    720819     * complicated. */
    721     NSRect parentFrame = [m_pParentView frame];
    722     NSPoint parentPos = [[m_pParentView window] convertBaseToScreen:[[m_pParentView superview] convertPointToBase:NSMakePoint(parentFrame.origin.x, parentFrame.origin.y + parentFrame.size.height)]];
     820    parentFrame = [m_pParentView frame];
     821    parentPos = [[m_pParentView window] convertBaseToScreen:[[m_pParentView superview] convertPointToBase:NSMakePoint(parentFrame.origin.x, parentFrame.origin.y + parentFrame.size.height)]];
    723822    parentFrame.origin.x = parentPos.x;
    724823    parentFrame.origin.y = parentPos.y;
    725824
    726825    /* Calculate the new screen coordinates of the overlay window. */
    727     NSPoint childPos = NSMakePoint(m_Pos.x, m_Pos.y + m_Size.height);
     826    childPos = NSMakePoint(m_Pos.x, m_Pos.y + m_Size.height);
    728827    childPos = [[m_pParentView window] convertBaseToScreen:[[m_pParentView superview] convertPointToBase:childPos]];
    729828
    730829    /* Make a frame out of it. */
    731     NSRect childFrame = NSMakeRect(childPos.x, childPos.y, m_Size.width, m_Size.height);
     830    childFrame = NSMakeRect(childPos.x, childPos.y, m_Size.width, m_Size.height);
    732831
    733832    /* We have to make sure that the overlay window will not be displayed out
    734833     * of the parent window. So intersect both frames & use the result as the new
    735834     * frame for the window. */
    736     NSRect newFrame = NSIntersectionRect(parentFrame, childFrame);
     835    newFrame = NSIntersectionRect(parentFrame, childFrame);
    737836
    738837    /* Later we have to correct the texture position in the case the window is
     
    747846        m_RootShift.y = 0;
    748847
    749 //    NSScrollView *pScrollView = [[[m_pParentView window] contentView] enclosingScrollView];
    750 //    if (pScrollView)
    751 //    {
    752 //        NSRect scrollRect = [pScrollView documentVisibleRect];
    753 //        NSRect scrollRect = [m_pParentView visibleRect];
    754 //        printf ("sc rect: %d %d %d %d\n", (int) scrollRect.origin.x,(int) scrollRect.origin.y,(int) scrollRect.size.width,(int) scrollRect.size.height);
    755 //        NSRect b = [[m_pParentView superview] bounds];
    756 //        printf ("bound rect: %d %d %d %d\n", (int) b.origin.x,(int) b.origin.y,(int) b.size.width,(int) b.size.height);
    757 //        newFrame.origin.x += scrollRect.origin.x;
    758 //        newFrame.origin.y += scrollRect.origin.y;
    759 //    }
     848    /*
     849    NSScrollView *pScrollView = [[[m_pParentView window] contentView] enclosingScrollView];
     850    if (pScrollView)
     851    {
     852        NSRect scrollRect = [pScrollView documentVisibleRect];
     853        NSRect scrollRect = [m_pParentView visibleRect];
     854        printf ("sc rect: %d %d %d %d\n", (int) scrollRect.origin.x,(int) scrollRect.origin.y,(int) scrollRect.size.width,(int) scrollRect.size.height);
     855        NSRect b = [[m_pParentView superview] bounds];
     856        printf ("bound rect: %d %d %d %d\n", (int) b.origin.x,(int) b.origin.y,(int) b.size.width,(int) b.size.height);
     857        newFrame.origin.x += scrollRect.origin.x;
     858        newFrame.origin.y += scrollRect.origin.y;
     859    }
     860    */
    760861
    761862    /* Set the new frame. */
     
    771872- (void)createFBO
    772873{
    773     GLint oldTexId;
    774     GLint oldFBId;
    775    
    776     DEBUG_MSG(("createFBO %p\n", self));
     874    GLint   oldTexId         = 0;
     875    GLint   oldFBId          = 0;
     876    NSView *pDockScreen      = nil;
     877    GLint   maxTexSize       = 0;
     878    GLfloat imageAspectRatio = 0;
     879    GLint   filter           = GL_NEAREST;
     880
    777881    [self deleteFBO];
    778882
    779     //GL_SAVE_STATE;
    780 #if 0
    781     CHECK_GL_ERROR();
    782     glPushAttrib(GL_ACCUM_BUFFER_BIT);
    783     glPopAttrib();
    784     CHECK_GL_ERROR();
    785 #endif
     883#ifdef FBO
     884    DEBUG_MSG(("OVIW(%p): createFBO\n", (void*)self));
    786885
    787886    glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &oldTexId);
     
    800899        if (!isFBO)
    801900        {
    802             DEBUG_MSG(("Your system does not support framebuffer extension\n"));
    803         }
    804        
     901            DEBUG_MSG(("Your system does not support the GL_EXT_framebuffer_object extension\n"));
     902        }
     903        isFBO = gluCheckExtension((const GLubyte*)"GL_EXT_framebuffer_blit", strExt);
     904        if (!isFBO)
     905        {
     906            DEBUG_MSG(("Your system does not support the GL_EXT_framebuffer_blit extension\n"));
     907        }
     908
    805909        /* Create FBO object */
    806910        glGenFramebuffersEXT(1, &m_FBOId);
    807911        /* & the texture as well the depth/stencil render buffer */
    808         glGenTextures(1, &m_FBOTexId);
    809         DEBUG_MSG(("Create FBO %d %d\n", m_FBOId, m_FBOTexId));
     912        glGenTextures(1, &m_FBOTexBackId);
     913        glGenTextures(1, &m_FBOTexFrontId);
     914        DEBUG_MSG(("OVIW(%p): gen numbers: FBOId=%d FBOTexBackId=%d FBOTexFrontId=%d\n", (void*)self, m_FBOId, m_FBOTexBackId, m_FBOTexFrontId));
    810915
    811916        glGenRenderbuffersEXT(1, &m_FBODepthStencilPackedId);
     
    816921    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
    817922
    818     /*glEnable(GL_TEXTURE_RECTANGLE_ARB);*/
    819    
    820     GLfloat imageAspectRatio = m_FBOTexSize.width / m_FBOTexSize.height;
     923    /*
     924    glEnable(GL_TEXTURE_RECTANGLE_ARB);
     925     */
     926
     927    imageAspectRatio = m_FBOTexSize.width / m_FBOTexSize.height;
    821928
    822929    /* Sanity check against maximum OpenGL texture size. If bigger adjust to
    823930     * maximum possible size while maintain the aspect ratio. */
    824     GLint maxTexSize;
    825931    glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
    826 //    maxTexSize = 150;
    827     GLint filter = GL_NEAREST;
    828     if (m_FBOTexSize.width > maxTexSize || m_FBOTexSize.height > maxTexSize)
     932    if (m_FBOTexSize.width > maxTexSize || m_FBOTexSize.height > maxTexSize)
    829933    {
    830934        filter = GL_LINEAR;
    831935        if (imageAspectRatio > 1)
    832936        {
    833             m_FBOTexSize.width = maxTexSize; 
     937            m_FBOTexSize.width = maxTexSize;
    834938            m_FBOTexSize.height = maxTexSize / imageAspectRatio;
    835939        }
     
    837941        {
    838942            m_FBOTexSize.width = maxTexSize * imageAspectRatio;
    839             m_FBOTexSize.height = maxTexSize;
    840         }
    841     }
    842    
    843     /* Initialize FBO Texture */
    844     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
    845     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, filter);
    846     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, filter);
    847     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
    848     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
    849    
     943            m_FBOTexSize.height = maxTexSize;
     944        }
     945    }
     946
     947    DEBUG_MSG(("OVIW(%p): tex size is: %dx%d\n", (void*)self, (int)m_FBOTexSize.width, (int)m_FBOTexSize.height));
     948
     949    /* Initialize FBO Textures */
    850950    /* The GPUs like the GL_BGRA / GL_UNSIGNED_INT_8_8_8_8_REV combination
    851951     * others are also valid, but might incur a costly software translation. */
     952    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexBackId);
    852953    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, m_FBOTexSize.width, m_FBOTexSize.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
    853     DEBUG_MSG(("m_FBOTexSize(%i,%i)\n", (int)m_FBOTexSize.width, (int)m_FBOTexSize.height));
    854        
    855     /* Now attach texture to the FBO as its color destination */
    856     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId, 0);
     954    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexFrontId);
     955    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, m_FBOTexSize.width, m_FBOTexSize.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
     956
     957    /* Now attach the textures to the FBO as its color destinations */
     958    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, m_FBOAttBackId,  GL_TEXTURE_RECTANGLE_ARB, m_FBOTexBackId, 0);
     959    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, m_FBOAttFrontId, GL_TEXTURE_RECTANGLE_ARB, m_FBOTexFrontId, 0);
    857960
    858961    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_FBODepthStencilPackedId);
    859     glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, m_FBOTexSize.width, m_FBOTexSize.height);
    860     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_FBODepthStencilPackedId);
    861     glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_FBODepthStencilPackedId);
     962    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, m_FBOTexSize.width, m_FBOTexSize.height);
     963    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_FBODepthStencilPackedId);
     964
     965    /* Bind the FBOs for reading and drawing. */
     966    glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, m_FBOId);
     967    glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, m_FBOId);
     968
     969    /* Explicitly clear the textures otherwise they would contain old memory stuff. */
     970    glDrawBuffer(m_FBOAttBackId);
     971    glClear(GL_COLOR_BUFFER_BIT);
     972    glDrawBuffer(m_FBOAttFrontId);
     973    glClear(GL_COLOR_BUFFER_BIT);
     974
     975    /* Now initially reading/drawing to the back buffer. */
     976    glReadBuffer(m_FBOAttBackId);
     977    glDrawBuffer(m_FBOAttBackId);
    862978
    863979    /* Make sure the FBO was created successfully. */
    864980    if (GL_FRAMEBUFFER_COMPLETE_EXT != glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT))
    865         DEBUG_MSG(("Framebuffer Object creation or update failed!\n"));
     981        DEBUG_MSG(("OVIW(%p): Framebuffer Object creation or update failed!\n", (void*)self));
    866982
    867983    glBindTexture(GL_TEXTURE_RECTANGLE_ARB, oldTexId);
    868     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, oldFBId ? oldFBId:m_FBOId);
     984    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (GLuint)oldFBId ? (GLuint)oldFBId : m_FBOId);
    869985
    870986    /* Is there a dock tile preview enabled in the GUI? If so setup a
    871987     * additional thumbnail view for the dock tile. */
    872     NSView *dockScreen = [self dockTileScreen];
    873     if (dockScreen)
     988    pDockScreen = [self dockTileScreen];
     989    if (pDockScreen)
    874990    {
    875991        if (!m_FBOThumbId)
     
    8861002        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
    8871003        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
    888    
     1004
    8891005        /* The GPUs like the GL_BGRA / GL_UNSIGNED_INT_8_8_8_8_REV combination
    8901006         * others are also valid, but might incur a costly software translation. */
    891         glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, m_FBOTexSize.width * m_FBOThumbScaleX, m_FBOTexSize.height * m_FBOThumbScaleY, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
     1007        glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, m_FBOTexSize.width * m_FBOThumbScaleX, m_FBOTexSize.height * m_FBOThumbScaleY, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
    8921008
    8931009        /* Now attach texture to the FBO as its color destination */
     
    8961012        /* Make sure the FBO was created successfully. */
    8971013        if (GL_FRAMEBUFFER_COMPLETE_EXT != glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT))
    898             DEBUG_MSG(("Framebuffer Thumb Object creation or update failed!\n"));
     1014            DEBUG_MSG(("OVIW(%p): Framebuffer \"Thumb\" Object creation or update failed!\n", (void*)self));
    8991015
    9001016        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, oldTexId);
    901         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, oldFBId ? oldFBId:m_FBOId);
     1017        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, (GLuint)oldFBId ? (GLuint)oldFBId : m_FBOId);
    9021018
    9031019        m_DockTileView = [[DockOverlayView alloc] init];
    9041020        [self reshapeDockTile];
    905         [dockScreen addSubview:m_DockTileView];
     1021        [pDockScreen addSubview:m_DockTileView];
    9061022    }
    9071023
     
    9141030    m_paClipRects[2] = m_FBOTexSize.width;
    9151031    m_paClipRects[3] = m_FBOTexSize.height;
    916    
    917     //GL_RESTORE_STATE;
     1032#endif
    9181033}
    9191034
    9201035- (void)deleteFBO
    9211036{
    922     DEBUG_MSG(("deleteFBO %p\n", self));
     1037    DEBUG_MSG(("OVIW(%p): deleteFBO\n", (void*)self));
     1038
    9231039    if (m_pSharedGLCtx)
    9241040    {
    925         DEBUG_MSG(("MakeCurrent (shared) %X\n", m_pSharedGLCtx));
     1041        DEBUG_MSG(("OVIW(%p): makeCurrent (shared) %p\n", (void*)self, (void*)m_pSharedGLCtx));
    9261042        [m_pSharedGLCtx makeCurrentContext];
    9271043        [m_pSharedGLCtx update];
     
    9331049    if (m_pGLCtx)
    9341050    {
    935         DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
     1051        DEBUG_MSG(("OVIW(%p): makeCurrent (non shared) %p\n", (void*)self, (void*)m_pGLCtx));
    9361052        [m_pGLCtx makeCurrentContext];
    9371053
     1054#ifdef FBO
    9381055        if (m_FBODepthStencilPackedId > 0)
    9391056        {
     
    9411058            m_FBODepthStencilPackedId = 0;
    9421059        }
    943         if (m_FBOTexId > 0)
    944         {
    945             glDeleteTextures(1, &m_FBOTexId);
    946             m_FBOTexId = 0;
     1060        if (m_FBOTexBackId > 0)
     1061        {
     1062            glDeleteTextures(1, &m_FBOTexBackId);
     1063            m_FBOTexBackId = 0;
     1064        }
     1065        if (m_FBOTexFrontId > 0)
     1066        {
     1067            glDeleteTextures(1, &m_FBOTexFrontId);
     1068            m_FBOTexFrontId = 0;
    9471069        }
    9481070        if (m_FBOId > 0)
    9491071        {
    950             GLint tmpFB;
    951             glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &tmpFB);
    952 
    953             if (tmpFB == m_FBOId)
    954             {
     1072            if ([self isCurrentFBO])
    9551073                glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    956             }
    957            
     1074
    9581075            glDeleteFramebuffersEXT(1, &m_FBOId);
    9591076            m_FBOId = 0;
    9601077        }
     1078#endif
    9611079    }
    9621080
     
    9711089- (void)updateFBO
    9721090{
    973     DEBUG_MSG(("updateFBO %p\n", self));
     1091    DEBUG_MSG(("OVIW(%p): updateFBO\n", (void*)self));
     1092
    9741093    [self makeCurrentFBO];
    975    
     1094
    9761095    if (m_pGLCtx)
    9771096    {
     
    9871106- (void)makeCurrentFBO
    9881107{
    989     DEBUG_MSG_1(("MakeCurrent called %X\n", self));
    990 
    991 #ifdef FBO
     1108    DEBUG_MSG(("OVIW(%p): makeCurrentFBO\n", (void*)self));
     1109
     1110#ifdef FBO
     1111    DEBUG_MSG(("OVIW(%p): FBOId=%d CTX=%p\n", (void*)self, m_FBOId, (void*)m_pGLCtx));
    9921112    if([NSOpenGLContext currentContext] != 0)
    9931113        glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
     
    9951115    if (m_pGLCtx)
    9961116    {
    997         if ([m_pGLCtx view] != self) 
     1117        if ([m_pGLCtx view] != self)
    9981118        {
    9991119            /* We change the active view, so flush first */
     
    10031123            CHECK_GL_ERROR();
    10041124        }
    1005 //        if ([NSOpenGLContext currentContext] != m_pGLCtx)
    1006         {
    1007             DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
     1125        /*
     1126        if ([NSOpenGLContext currentContext] != m_pGLCtx)
     1127        */
     1128        {
    10081129            [m_pGLCtx makeCurrentContext];
    10091130            CHECK_GL_ERROR();
    1010 //            [m_pGLCtx update];
     1131            /*
     1132            [m_pGLCtx update];
     1133            */
    10111134        }
    10121135    }
     
    10161139}
    10171140
     1141- (bool)isCurrentFBO
     1142{
     1143#ifdef FBO
     1144    GLint curFBOId = 0;
     1145
     1146    glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &curFBOId);
     1147    DEBUG_MSG_1(("OVIW(%p): isCurrentFBO: curFBOId=%d FBOId=%d\n", (void*)self, curFBOId, m_FBOId));
     1148    return (GLuint)curFBOId == m_FBOId;
     1149#else
     1150    return false;
     1151#endif
     1152}
     1153
     1154- (void)tryDraw
     1155{
     1156    if ([self lockFocusIfCanDraw])
     1157    {
     1158        [self renderFBOToView];
     1159        [self unlockFocus];
     1160    }
     1161}
     1162
    10181163- (void)swapFBO
    10191164{
    1020     DEBUG_MSG_1(("SwapCurrent called %X\n", self));
    1021 
    1022 #ifdef FBO
    1023     GLint tmpFB;
    1024     glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &tmpFB);
    1025     DEBUG_MSG_1(("Swap GetINT %d\n", tmpFB));
    1026     /* Don't use flush buffers cause we are using FBOs here */
    1027 //    [m_pGLCtx flushBuffer];
    1028     glFlush();
    1029 //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    1030     DEBUG_MSG_1(("swapFBO bound:%i, self:%i\n", tmpFB, m_FBOId));
    1031     if (tmpFB == m_FBOId)
    1032     {
    1033         if ([self lockFocusIfCanDraw])
    1034         {
    1035             [self renderFBOToView];
    1036             [self unlockFocus];
    1037         }
    1038     }
    1039 //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
     1165    GLint sw     = 0;
     1166    GLint readId = 0;
     1167    GLint drawId = 0;
     1168
     1169    DEBUG_MSG(("OVIW(%p): swapFBO\n", (void*)self));
     1170
     1171#ifdef FBO
     1172    glBindFramebufferEXT(GL_FRAMEBUFFER_BINDING_EXT, m_FBOId);
     1173
     1174    /* Don't use flush buffers cause we are using FBOs here! */
     1175
     1176    if ([self isCurrentFBO])
     1177    {
     1178        /* Fetch the current used read and draw buffers. */
     1179        glGetIntegerv(GL_READ_BUFFER, &readId);
     1180        glGetIntegerv(GL_DRAW_BUFFER, &drawId);
     1181
     1182        /* Do the swapping of our internal ids */
     1183        sw              = m_FBOTexFrontId;
     1184        m_FBOTexFrontId = m_FBOTexBackId;
     1185        m_FBOTexBackId  = sw;
     1186        sw              = m_FBOAttFrontId;
     1187        m_FBOAttFrontId = m_FBOAttBackId;
     1188        m_FBOAttBackId  = sw;
     1189
     1190        /* We also have to swap the real ids on the current context. */
     1191        if ((GLuint)readId == m_FBOAttFrontId)
     1192        {
     1193            glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, m_FBOId);
     1194            glReadBuffer(m_FBOAttBackId);
     1195        }
     1196        if ((GLuint)drawId == m_FBOAttFrontId)
     1197        {
     1198            glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, m_FBOId);
     1199            glDrawBuffer(m_FBOAttBackId);
     1200        }
     1201
     1202        [self tryDraw];
     1203    }
    10401204#else
    10411205    [m_pGLCtx flushBuffer];
     
    10451209- (void)flushFBO
    10461210{
    1047     GLint tmpFB;
    1048     glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &tmpFB);
     1211    GLint drawId = 0;
     1212
     1213    DEBUG_MSG(("OVIW(%p): flushFBO\n", (void*)self));
     1214
    10491215    glFlush();
    1050 //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    1051     DEBUG_MSG_1 (("Flush GetINT %d\n", tmpFB));
    1052     if (tmpFB == m_FBOId)
    1053     {
    1054         if ([self lockFocusIfCanDraw])
    1055         {
    1056             [self renderFBOToView];
    1057             [self unlockFocus];
    1058         }
    1059     }
    1060 //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
     1216#ifdef FBO
     1217    /* If at any time OpenGl operations where done in the front buffer, we need
     1218     * to reflect this in the FBO as well. This is something which on real
     1219     * hardware happens and unfortunately some applications rely on it (grrr ... Compiz). */
     1220    if (   m_fFrontDrawing
     1221        && [self isCurrentFBO])
     1222    {
     1223        /* Only reset if we aren't currently front. */
     1224        glGetIntegerv(GL_DRAW_BUFFER, &drawId);
     1225        if (drawId != m_FBOAttFrontId)
     1226            m_fFrontDrawing = false;
     1227        [self tryDraw];
     1228    }
     1229#endif
    10611230}
    10621231
    10631232- (void)finishFBO
    10641233{
    1065     GLint tmpFB;
    1066     glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &tmpFB);
     1234    DEBUG_MSG(("OVIW(%p): finishFBO\n", (void*)self));
     1235
    10671236    glFinish();
    1068         //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    1069     DEBUG_MSG_1 (("Finish GetINT %d\n", tmpFB));
    1070     if (tmpFB == m_FBOId)
    1071     {
    1072         if ([self lockFocusIfCanDraw])
    1073         {
    1074             [self renderFBOToView];
    1075             [self unlockFocus];
    1076         }
    1077     }
    1078 //    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
    1079 }
    1080 
    1081 - (void)bindFBO
    1082 {
    1083     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
     1237#ifdef FBO
     1238    if ([self isCurrentFBO])
     1239        [self tryDraw];
     1240#endif
     1241}
     1242
     1243- (void)stateInfo:(GLenum)pname withParams:(GLint*)params
     1244{
     1245    DEBUG_MSG_1(("StateInfo requested: %d\n", pname));
     1246
     1247    glGetIntegerv(pname, params);
     1248#ifdef FBO
     1249    switch(pname)
     1250    {
     1251        case GL_FRAMEBUFFER_BINDING_EXT:
     1252        case GL_READ_FRAMEBUFFER_BINDING:
     1253        case GL_READ_FRAMEBUFFER_EXT:
     1254        case GL_DRAW_FRAMEBUFFER_EXT:
     1255        {
     1256//            DEBUG_MSG_1(("StateInfo current Framebuffer: %d\n", *params));
     1257            if ((GLuint)*params == m_FBOId)
     1258                *params = 0;
     1259            break;
     1260        }
     1261        case GL_READ_BUFFER:
     1262        case GL_DRAW_BUFFER:
     1263        {
     1264//            DEBUG_MSG_1(("StateInfo current read/draw buffer: %d\n", *params));
     1265            if ((GLuint)*params == m_FBOAttFrontId)
     1266                *params = GL_FRONT;
     1267            else
     1268                if ((GLuint)*params == m_FBOAttBackId)
     1269                    *params = GL_BACK;
     1270            break;
     1271        }
     1272    }
     1273#endif
     1274}
     1275
     1276- (void)readBuffer:(GLenum)mode
     1277{
     1278#ifdef FBO
     1279    /*
     1280    if ([self isCurrentFBO])
     1281    */
     1282    {
     1283        if (mode == GL_FRONT)
     1284            glReadBuffer(m_FBOAttFrontId);
     1285        else if (mode == GL_BACK)
     1286            glReadBuffer(m_FBOAttBackId);
     1287        else
     1288            glReadBuffer(mode);
     1289    }
     1290#else
     1291    glReadBuffer(mode);
     1292#endif
     1293}
     1294
     1295- (void)drawBuffer:(GLenum)mode
     1296{
     1297#ifdef FBO
     1298    /*
     1299    if ([self isCurrentFBO])
     1300    */
     1301    {
     1302        if (mode == GL_FRONT)
     1303        {
     1304            DEBUG_MSG(("OVIW(%p): front\n", (void*)self));
     1305            glDrawBuffer(m_FBOAttFrontId);
     1306            m_fFrontDrawing = true;
     1307        }
     1308        else if (mode == GL_BACK)
     1309        {
     1310            DEBUG_MSG(("OVIW(%p): back\n", (void*)self));
     1311            glDrawBuffer(m_FBOAttBackId);
     1312        }
     1313        else
     1314        {
     1315            DEBUG_MSG(("OVIW(%p): other\n", (void*)self));
     1316            glDrawBuffer(mode);
     1317        }
     1318    }
     1319#else
     1320    glDrawBuffer(mode);
     1321#endif
     1322}
     1323
     1324- (void)bindFBO:(GLenum)target withFrameBuffer:(GLuint)framebuffer
     1325{
     1326#ifdef FBO
     1327    if (framebuffer != 0)
     1328        glBindFramebufferEXT(target, framebuffer);
     1329    else
     1330        glBindFramebufferEXT(target, m_FBOId);
     1331#else
     1332    glBindFramebufferEXT(target, framebuffer);
     1333#endif
    10841334}
    10851335
    10861336- (void)renderFBOToView
    10871337{
     1338    GLint opaque       = 0;
     1339    GLint i            = 0;
     1340    GLint oldReadFBOId = 0;
     1341    GLint oldDrawFBOId = 0;
     1342    GLint oldReadId    = 0;
     1343    GLint oldDrawId    = 0;
     1344
     1345    DEBUG_MSG(("OVIW(%p): renderFBOToView\n", (void*)self));
     1346
     1347#ifdef FBO
     1348
     1349    /* Fetch the current used read and draw buffers. */
     1350    glGetIntegerv(GL_READ_FRAMEBUFFER_EXT, &oldReadFBOId);
     1351    glGetIntegerv(GL_DRAW_FRAMEBUFFER_EXT, &oldDrawFBOId);
     1352    glGetIntegerv(GL_READ_BUFFER, &oldReadId);
     1353    glGetIntegerv(GL_DRAW_BUFFER, &oldDrawId);
     1354
    10881355    if (!m_pSharedGLCtx)
    10891356    {
     
    10921359
    10931360        /* Set the new context as non opaque */
    1094         GLint opaque = 0;
    1095         [m_pSharedGLCtx setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];       
    1096         /* Only swap on screen refresh */
    1097 //        GLint swap = 1;
    1098 //        [m_pSharedGLCtx setValues:&swap forParameter:NSOpenGLCPSwapInterval];       
     1361        [m_pSharedGLCtx setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
    10991362        /* Set this view as the drawable for the new context */
    11001363        [m_pSharedGLCtx setView: self];
     
    11051368    {
    11061369        NSRect r = [self frame];
    1107         DEBUG_MSG_1(("rF2V frame[%i, %i, %i, %i]\n", (int)r.origin.x, (int)r.origin.y, (int)r.size.width, (int)r.size.height));
    1108 
    1109         if (m_FBOTexId > 0)
     1370        DEBUG_MSG(("OVIW(%p): rF2V frame: [%i, %i, %i, %i]\n", (void*)self, (int)r.origin.x, (int)r.origin.y, (int)r.size.width, (int)r.size.height));
     1371
     1372        if (m_FBOTexFrontId > 0)
    11101373        {
    11111374            if ([m_pSharedGLCtx view] != self)
    11121375            {
    1113                 DEBUG_MSG(("renderFBOToView: not current view of shared ctx!"));
     1376                DEBUG_MSG(("OVIW(%p): not current view of shared ctx! Switching ...\n", (void*)self));
    11141377                [m_pSharedGLCtx setView: self];
    11151378                [self updateViewport];
    11161379            }
    11171380
    1118             //DEBUG_MSG(("MakeCurrent (shared) %X\n", m_pSharedGLCtx));
    11191381            [m_pSharedGLCtx makeCurrentContext];
    1120        
    1121             if (m_FBOThumbTexId > 0 &&
    1122                 [m_DockTileView thumbBitmap] != nil)
    1123             {
    1124                 /* Only update after at least 200 ms, cause glReadPixels is
    1125                  * heavy performance wise. */
    1126                 uint64_t uiNewTime = RTTimeMilliTS();
    1127                 if (uiNewTime - m_uiDockUpdateTime > 200)
    1128                 {
    1129                     m_uiDockUpdateTime = uiNewTime;
    1130 #if 0
    1131                     /* todo: check this for optimization */
    1132                     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myTextureName);
    1133                     glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_STORAGE_HINT_APPLE,
    1134                                     GL_STORAGE_SHARED_APPLE);
    1135                     glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
    1136                     glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA,
    1137                                  sizex, sizey, 0, GL_BGRA,
    1138                                  GL_UNSIGNED_INT_8_8_8_8_REV, myImagePtr);
    1139                     glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,
    1140                                         0, 0, 0, 0, 0, image_width, image_height);
    1141                     glFlush();
    1142                     // Do other work processing here, using a double or triple buffer
    1143                     glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_BGRA,
    1144                                   GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
    1145 #endif
    1146 
    1147                     GL_SAVE_STATE;
    1148                     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOThumbId);
    1149 
    1150                     /* We like to read from the primary color buffer */
    1151                     glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
    1152 
    1153                     NSRect rr = [m_DockTileView frame];
    1154 
    1155                     /* Setup all matrices */
    1156                     glMatrixMode(GL_PROJECTION);
    1157                     glLoadIdentity();
    1158                     glViewport(0, 0, rr.size.width, rr.size.height);
    1159                     glOrtho(0, rr.size.width, 0, rr.size.height, -1, 1);
    1160                     glScalef(m_FBOThumbScaleX, m_FBOThumbScaleY, 1.0f);
    1161                     glMatrixMode(GL_TEXTURE);
    1162                     glLoadIdentity();
    1163                     glTranslatef(0.0f, m_RootShift.y, 0.0f);
    1164                     glMatrixMode(GL_MODELVIEW);
    1165                     glLoadIdentity();
    1166 
    1167                     /* Clear background to transparent */
    1168                     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    1169                     glClear(GL_COLOR_BUFFER_BIT);
    1170 
    1171                     glEnable(GL_TEXTURE_RECTANGLE_ARB);
    1172                     glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
    1173                     GLint i;
    1174                     for (i = 0; i < m_cClipRects; ++i)
    1175                     {
    1176                         GLint x1 = m_paClipRects[4*i];
    1177                         GLint y1 = (r.size.height - m_paClipRects[4*i+1]);
    1178                         GLint x2 = m_paClipRects[4*i+2];
    1179                         GLint y2 = (r.size.height - m_paClipRects[4*i+3]);
    1180                         glBegin(GL_QUADS);
    1181                         {
    1182                             glTexCoord2i(x1, y1); glVertex2i(x1, y1);
    1183                             glTexCoord2i(x1, y2); glVertex2i(x1, y2);
    1184                             glTexCoord2i(x2, y2); glVertex2i(x2, y2);
    1185                             glTexCoord2i(x2, y1); glVertex2i(x2, y1);
    1186                         }
    1187                         glEnd();
    1188                     }
    1189                     glFinish();
    1190 
    1191                     /* Here the magic of reading the FBO content in our own buffer
    1192                      * happens. We have to lock this access, in the case the dock
    1193                      * is updated currently. */
    1194                     [m_DockTileView lock];
    1195                     glReadPixels(0, 0, rr.size.width, rr.size.height,
    1196                                  GL_RGBA,
    1197                                  GL_UNSIGNED_BYTE,
    1198                                  [[m_DockTileView thumbBitmap] bitmapData]);
    1199                     [m_DockTileView unlock];
    1200 
    1201                     NSDockTile *pDT = [[NSApplication sharedApplication] dockTile];
    1202 
    1203                     /* Send a display message to the dock tile in the main thread */
    1204                     [[[NSApplication sharedApplication] dockTile] performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:NO];
    1205 
    1206                     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
    1207                     GL_RESTORE_STATE;
    1208                 }
    1209             }
    1210 
     1382
     1383            glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, m_FBOId);
     1384            glReadBuffer(m_FBOAttFrontId);
     1385            glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
     1386            glDrawBuffer(GL_BACK);
     1387
     1388            /* Render FBO content to the dock tile when necessary. */
     1389            [self renderFBOToDockTile];
     1390
     1391#if 1 /* Set to 0 to see the docktile instead of the real output */
    12111392            /* Clear background to transparent */
    12121393            glClear(GL_COLOR_BUFFER_BIT);
    12131394
    1214             glEnable(GL_TEXTURE_RECTANGLE_ARB);
    1215             glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
    1216 
    12171395            /* Blit the content of the FBO to the screen. todo: check for
    12181396             * optimization with display lists. */
    1219             GLint i;
    12201397            for (i = 0; i < m_cClipRects; ++i)
    12211398            {
     
    12241401                GLint x2 = m_paClipRects[4*i+2];
    12251402                GLint y2 = r.size.height - m_paClipRects[4*i+3];
    1226                 glBegin(GL_QUADS);
    1227                 {
    1228                     glTexCoord2i(x1, y1); glVertex2i(x1, y1);
    1229                     glTexCoord2i(x1, y2); glVertex2i(x1, y2);
    1230                     glTexCoord2i(x2, y2); glVertex2i(x2, y2);
    1231                     glTexCoord2i(x2, y1); glVertex2i(x2, y1);
    1232                 }
    1233                 glEnd();
     1403                glBlitFramebufferEXT(x1, y1 + m_RootShift.y, x2, y2 + m_RootShift.y,
     1404                                     x1 - m_RootShift.x, y1, x2 - m_RootShift.x, y2,
     1405                                     GL_COLOR_BUFFER_BIT, GL_NEAREST);
     1406            }
     1407#endif
     1408//            glFinish();
     1409            [m_pSharedGLCtx flushBuffer];
     1410
     1411            [m_pGLCtx makeCurrentContext];
     1412            /* Reset to previous buffer bindings. */
     1413            if (   (GLuint)oldReadId == m_FBOAttBackId
     1414                || (GLuint)oldReadId == m_FBOAttFrontId)
     1415                glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, m_FBOId);
     1416            glReadBuffer(oldReadId);
     1417            if (   (GLuint)oldDrawId == m_FBOAttBackId
     1418                || (GLuint)oldDrawId == m_FBOAttFrontId)
     1419//                glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, oldDrawFBOId);
     1420                glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, m_FBOId);
     1421            glDrawBuffer(oldDrawId);
     1422        }
     1423    }
     1424#endif
     1425}
     1426
     1427- (void)renderFBOToDockTile
     1428{
     1429    NSRect r        = [self frame];
     1430    NSRect rr       = NSZeroRect;
     1431    GLint i         = 0;
     1432    NSDockTile *pDT = nil;
     1433
     1434#ifdef FBO
     1435    if (   m_FBOThumbId
     1436        && m_FBOThumbTexId
     1437        && [m_DockTileView thumbBitmap] != nil)
     1438    {
     1439        /* Only update after at least 200 ms, cause glReadPixels is
     1440         * heavy performance wise. */
     1441        uint64_t uiNewTime = RTTimeMilliTS();
     1442        if (uiNewTime - m_uiDockUpdateTime > 200)
     1443        {
     1444            m_uiDockUpdateTime = uiNewTime;
     1445#if 0
     1446            /* todo: check this for optimization */
     1447            glBindTexture(GL_TEXTURE_RECTANGLE_ARB, myTextureName);
     1448            glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_STORAGE_HINT_APPLE,
     1449                            GL_STORAGE_SHARED_APPLE);
     1450            glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
     1451            glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA,
     1452                         sizex, sizey, 0, GL_BGRA,
     1453                         GL_UNSIGNED_INT_8_8_8_8_REV, myImagePtr);
     1454            glCopyTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,
     1455                                0, 0, 0, 0, 0, image_width, image_height);
     1456            glFlush();
     1457            /* Do other work processing here, using a double or triple buffer */
     1458            glGetTexImage(GL_TEXTURE_RECTANGLE_ARB, 0, GL_BGRA,
     1459                          GL_UNSIGNED_INT_8_8_8_8_REV, pixels);
     1460#endif
     1461            /* Clear background to transparent */
     1462            glClear(GL_COLOR_BUFFER_BIT);
     1463
     1464            rr = [m_DockTileView frame];
     1465
     1466            for (i = 0; i < m_cClipRects; ++i)
     1467            {
     1468                GLint x1 = m_paClipRects[4*i];
     1469                GLint y1 = r.size.height - m_paClipRects[4*i+1];
     1470                GLint x2 = m_paClipRects[4*i+2];
     1471                GLint y2 = r.size.height - m_paClipRects[4*i+3];
     1472
     1473                glBlitFramebufferEXT(x1, y1 + m_RootShift.y, x2, y2 + m_RootShift.y,
     1474                                     x1 * m_FBOThumbScaleX, y1 * m_FBOThumbScaleY, x2 * m_FBOThumbScaleX, y2 * m_FBOThumbScaleY,
     1475                                     GL_COLOR_BUFFER_BIT, GL_NEAREST);
    12341476            }
    12351477            glFinish();
    1236             [m_pSharedGLCtx flushBuffer];
    1237             //DEBUG_MSG(("MakeCurrent %X\n", m_pGLCtx));
    1238             [m_pGLCtx makeCurrentContext];
    1239         }
    1240     }
     1478
     1479            glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
     1480            glReadBuffer(GL_BACK);
     1481            /* Here the magic of reading the FBO content in our own buffer
     1482             * happens. We have to lock this access, in the case the dock
     1483             * is updated currently. */
     1484            [m_DockTileView lock];
     1485            glReadPixels(0, 0, rr.size.width, rr.size.height,
     1486                         GL_BGRA,
     1487                         GL_UNSIGNED_INT_8_8_8_8,
     1488                         [[m_DockTileView thumbBitmap] bitmapData]);
     1489            [m_DockTileView unlock];
     1490
     1491            glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, m_FBOId);
     1492            glReadBuffer(m_FBOAttFrontId);
     1493
     1494            pDT = [[NSApplication sharedApplication] dockTile];
     1495
     1496            /* Send a display message to the dock tile in the main thread */
     1497            [[[NSApplication sharedApplication] dockTile] performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:NO];
     1498
     1499        }
     1500    }
     1501#endif
    12411502}
    12421503
     
    12531514- (void)setVisibleRegions:(GLint)cRects paRects:(GLint*)paRects
    12541515{
    1255     DEBUG_MSG_1(("New region received\n"));
     1516    DEBUG_MSG_1(("OVIW(%p): setVisibleRegions: cRects=%d\n", (void*)self, cRects));
    12561517
    12571518    [self clearVisibleRegions];
    12581519
    1259     if (cRects>0)
     1520    if (cRects > 0)
    12601521    {
    12611522        m_paClipRects = (GLint*)RTMemAlloc(sizeof(GLint) * 4 * cRects);
     
    12811542- (void)reshapeDockTile
    12821543{
     1544    NSRect newFrame = NSZeroRect;
     1545
    12831546    NSView *pView = [self dockTileScreen];
    12841547    if (pView != nil)
     
    12891552        m_FBOThumbScaleX = (float)dockFrame.size.width / parentFrame.size.width;
    12901553        m_FBOThumbScaleY = (float)dockFrame.size.height / parentFrame.size.height;
    1291         NSRect newFrame = NSMakeRect ((int)(m_Pos.x * m_FBOThumbScaleX), (int)(dockFrame.size.height - (m_Pos.y + m_Size.height - m_RootShift.y) * m_FBOThumbScaleY), (int)(m_Size.width * m_FBOThumbScaleX), (int)(m_Size.height * m_FBOThumbScaleY));
    1292 //    NSRect newFrame = NSMakeRect ((int)roundf(m_Pos.x * m_FBOThumbScaleX), (int)roundf(dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (int)roundf(m_Size.width * m_FBOThumbScaleX), (int)roundf(m_Size.height * m_FBOThumbScaleY));
    1293 //      NSRect newFrame = NSMakeRect ((m_Pos.x * m_FBOThumbScaleX), (dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (m_Size.width * m_FBOThumbScaleX), (m_Size.height * m_FBOThumbScaleY));
    1294 //    printf ("%f %f %f %f - %f %f\n", newFrame.origin.x, newFrame.origin.y, newFrame.size.width, newFrame.size.height, m_Size.height, m_FBOThumbScaleY);
     1554        newFrame = NSMakeRect((int)(m_Pos.x * m_FBOThumbScaleX), (int)(dockFrame.size.height - (m_Pos.y + m_Size.height - m_RootShift.y) * m_FBOThumbScaleY), (int)(m_Size.width * m_FBOThumbScaleX), (int)(m_Size.height * m_FBOThumbScaleY));
     1555        /*
     1556        NSRect newFrame = NSMakeRect ((int)roundf(m_Pos.x * m_FBOThumbScaleX), (int)roundf(dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (int)roundf(m_Size.width * m_FBOThumbScaleX), (int)roundf(m_Size.height * m_FBOThumbScaleY));
     1557        NSRect newFrame = NSMakeRect ((m_Pos.x * m_FBOThumbScaleX), (dockFrame.size.height - (m_Pos.y + m_Size.height) * m_FBOThumbScaleY), (m_Size.width * m_FBOThumbScaleX), (m_Size.height * m_FBOThumbScaleY));
     1558        printf ("%f %f %f %f - %f %f\n", newFrame.origin.x, newFrame.origin.y, newFrame.size.width, newFrame.size.height, m_Size.height, m_FBOThumbScaleY);
     1559        */
    12951560        [m_DockTileView setFrame: newFrame];
    12961561    }
     
    13061571void cocoaGLCtxCreate(NativeNSOpenGLContextRef *ppCtx, GLbitfield fVisParams)
    13071572{
     1573    NSOpenGLPixelFormat *pFmt = nil;
     1574
    13081575    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
    13091576
    1310     NSOpenGLPixelFormatAttribute attribs[24] = 
     1577    NSOpenGLPixelFormatAttribute attribs[24] =
    13111578    {
    13121579        NSOpenGLPFAWindow,
     
    13141581        NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24
    13151582    };
    1316    
     1583
    13171584    int i = 4;
     1585
    13181586    if (fVisParams & CR_ALPHA_BIT)
    13191587    {
     
    13271595        attribs[i++] = NSOpenGLPFADepthSize;
    13281596        attribs[i++] = 24;
     1597//        attribs[i++] = 32;
    13291598    }
    13301599    if (fVisParams & CR_STENCIL_BIT)
     
    13341603        attribs[i++] = 8;
    13351604    }
    1336     if (fVisParams & CR_ACCUM_BIT) 
     1605    if (fVisParams & CR_ACCUM_BIT)
    13371606    {
    13381607        DEBUG_MSG(("CR_ACCUM_BIT requested\n"));
     
    13431612            attribs[i++] = 24;
    13441613    }
    1345     if (fVisParams & CR_MULTISAMPLE_BIT) 
     1614    if (fVisParams & CR_MULTISAMPLE_BIT)
    13461615    {
    13471616        DEBUG_MSG(("CR_MULTISAMPLE_BIT requested\n"));
     
    13581627    if (fVisParams & CR_STEREO_BIT)
    13591628    {
    1360         DEBUG_MSG(("CR_DOUBLE_BIT requested\n"));
     1629        /* We don't support that.
     1630        DEBUG_MSG(("CR_STEREO_BIT requested\n"));
    13611631        attribs[i++] = NSOpenGLPFAStereo;
    1362     }
    1363    
     1632        */
     1633    }
     1634
    13641635    /* Mark the end */
    13651636    attribs[i++] = 0;
    13661637
    13671638    /* Choose a pixel format */
    1368     NSOpenGLPixelFormat* pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
    1369    
     1639    pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
     1640
    13701641    if (pFmt)
    13711642    {
     
    13731644
    13741645        /* Enable multi threaded OpenGL engine */
    1375 //        CGLContextObj cglCtx = [*ppCtx CGLContextObj];
    1376 //        CGLError err = CGLEnable(cglCtx, kCGLCEMPEngine);
    1377 //        if (err != kCGLNoError)
    1378 //            printf ("Couldn't enable MT OpenGL engine!\n");
    1379    
     1646        /*
     1647        CGLContextObj cglCtx = [*ppCtx CGLContextObj];
     1648        CGLError err = CGLEnable(cglCtx, kCGLCEMPEngine);
     1649        if (err != kCGLNoError)
     1650            printf ("Couldn't enable MT OpenGL engine!\n");
     1651        */
     1652
    13801653        DEBUG_MSG(("New context %X\n", (uint)*ppCtx));
    13811654    }
     
    14461719void cocoaViewDestroy(NativeNSViewRef pView)
    14471720{
     1721    NSWindow *pWin = nil;
     1722
    14481723    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
    14491724
     
    14511726    [pView setHidden: YES];
    14521727
    1453     NSWindow *win = [pView window];
    1454     [[NSNotificationCenter defaultCenter] removeObserver:win];
    1455     [win setContentView: nil];
    1456     [[win parentWindow] removeChildWindow: win];
    1457     int b = [win retainCount];
    1458 //    for (; b > 1; --b)
    1459 //        [win performSelector:@selector(release)]
    1460     [win performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:YES];
    1461 //        [win release];
     1728    pWin = [pView window];
     1729    [[NSNotificationCenter defaultCenter] removeObserver:pWin];
     1730    [pWin setContentView: nil];
     1731    [[pWin parentWindow] removeChildWindow: pWin];
     1732    /*
     1733    a = [pWin retainCount];
     1734    for (; a > 1; --a)
     1735        [pWin performSelector:@selector(release)]
     1736    */
     1737    [pWin performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:YES];
     1738    /*
     1739    [pWin release];
     1740    */
    14621741
    14631742    /* There seems to be a bug in the performSelector method which is called in
     
    14651744     * results in an unbalanced reference count, which is here manually
    14661745     * decremented. */
    1467     int a = [pView retainCount];
    1468 //    for (; a > 1; --a)
     1746    /*
     1747    a = [pView retainCount];
     1748    for (; a > 1; --a)
     1749    */
    14691750    [pView performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:YES];
    1470 //        [pView release];
     1751    /*
     1752    [pView release];
     1753    */
    14711754
    14721755    [pPool release];
     
    14831766
    14841767void cocoaViewDisplay(NativeNSViewRef pView)
    1485 {   
     1768{
    14861769    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
    14871770
    1488     DEBUG_MSG_1(("cocoaViewDisplay %p\n", pView));
     1771    DEBUG_MSG_1(("cocoaViewDisplay %p\n", (void*)pView));
    14891772    [(OverlayView*)pView swapFBO];
    14901773
     
    15291812
    15301813    DEBUG_MSG(("cocoaViewMakeCurrentContext(%p, %p)\n", (void*)pView, (void*)pCtx));
    1531    
     1814
    15321815    [(OverlayView*)pView setGLCtx:pCtx];
    15331816    [(OverlayView*)pView makeCurrentFBO];
     
    15501833*
    15511834********************************************************************************/
    1552 void cocoaFlush(void)
    1553 {
    1554     NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
    1555 
    1556 //    glFlush();
    1557 //    return;
    1558 
    1559     DEBUG_MSG_1(("glFlush called\n"));
    1560 
    1561 #ifdef FBO
     1835static void performSelectorOnView(SEL selector)
     1836{
    15621837    NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
     1838
    15631839    if (pCtx)
    15641840    {
     
    15661842        if (pView)
    15671843        {
    1568             if ([pView respondsToSelector:@selector(flushFBO)])
    1569                 [pView performSelector:@selector(flushFBO)];
    1570         }
    1571     }
    1572 #else
    1573     glFlush();
    1574 #endif
    1575 
    1576     [pPool release];
    1577 }
    1578 
    1579 void cocoaFinish(void)
    1580 {
    1581     NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
    1582 
    1583     DEBUG_MSG_1(("glFinish called\n"));
    1584 
    1585 #ifdef FBO
     1844            if ([pView respondsToSelector:selector])
     1845                [pView performSelector:selector];
     1846        }
     1847    }
     1848}
     1849
     1850static void performSelectorOnViewOneArg(SEL selector, id arg1)
     1851{
    15861852    NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
     1853
    15871854    if (pCtx)
    15881855    {
     
    15901857        if (pView)
    15911858        {
    1592             if ([pView respondsToSelector:@selector(finishFBO)])
    1593                 [pView performSelector:@selector(finishFBO)];
    1594         }
    1595     }
     1859            if ([pView respondsToSelector:selector])
     1860                [pView performSelector:selector withObject:arg1];
     1861        }
     1862    }
     1863}
     1864
     1865static void performSelectorOnViewTwoArgs(SEL selector, id arg1, id arg2)
     1866{
     1867    NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
     1868
     1869    if (pCtx)
     1870    {
     1871        NSView *pView = [pCtx view];
     1872        if (pView)
     1873        {
     1874            if ([pView respondsToSelector:selector])
     1875                [pView performSelector:selector withObject:arg1 withObject:arg2];
     1876        }
     1877    }
     1878}
     1879
     1880void cocoaFlush(void)
     1881{
     1882    NSOpenGLContext *pCtx = nil;
     1883
     1884    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
     1885
     1886    DEBUG_MSG_1(("glFlush called\n"));
     1887
     1888    performSelectorOnView(@selector(flushFBO));
     1889
     1890    [pPool release];
     1891}
     1892
     1893void cocoaFinish(void)
     1894{
     1895    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
     1896
     1897    DEBUG_MSG_1(("glFinish called\n"));
     1898
     1899    performSelectorOnView(@selector(finishFBO));
     1900
     1901    [pPool release];
     1902}
     1903
     1904void cocoaBindFramebufferEXT(GLenum target, GLuint framebuffer)
     1905{
     1906    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
     1907
     1908    DEBUG_MSG_1(("glBindFramebufferEXT called target: %d  fb: %d\n", target, framebuffer));
     1909
     1910    performSelectorOnViewTwoArgs(@selector(bindFBO:withFrameBuffer:), (id)target, (id)framebuffer);
     1911
     1912    [pPool release];
     1913}
     1914
     1915void cocoaCopyPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum type)
     1916{
     1917    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
     1918
     1919    DEBUG_MSG_1(("glCopyPixels called: %d,%d-%dx%d type: %d\n", x, y, width, height, type));
     1920
     1921#ifdef FBO
     1922    GLbitfield mask = GL_COLOR_BUFFER_BIT;
     1923    if (type == GL_DEPTH)
     1924        mask = GL_DEPTH_BUFFER_BIT;
     1925    else if (type == GL_STENCIL)
     1926        mask = GL_STENCIL_BUFFER_BIT;
     1927    glBlitFramebufferEXT(x, y, x + width, y + height, x, y, x + width, y + height, mask, GL_NEAREST);
    15961928#else
    1597     glFinish();
     1929    glCopyPixels(x, y, width, height, type);
    15981930#endif
    15991931
     
    16011933}
    16021934
    1603 void cocoaBindFramebufferEXT(GLenum target, GLuint framebuffer)
     1935void cocoaGetIntegerv(GLenum pname, GLint *params)
    16041936{
    16051937    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
    16061938
    1607     DEBUG_MSG(("glRenderspuBindFramebufferEXT called %d\n", framebuffer));
    1608 
    1609 #ifdef FBO
    1610     if (framebuffer != 0)
    1611         glBindFramebufferEXT(target, framebuffer);
    1612     else
    1613     {
    1614         NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
    1615         if (pCtx)
    1616         {
    1617             NSView *pView = [pCtx view];
    1618             if (pView)
    1619             {
    1620                 if ([pView respondsToSelector:@selector(bindFBO)])
    1621                     [pView performSelector:@selector(bindFBO)];
    1622             }
    1623         }
    1624     }
    1625 #else
    1626     glBindFramebufferEXT(target, framebuffer);
    1627 #endif
     1939    DEBUG_MSG_1(("getIntergerv called: %d\n", pname));
     1940
     1941    performSelectorOnViewTwoArgs(@selector(stateInfo:withParams:), (id)pname, (id)params);
    16281942
    16291943    [pPool release];
    16301944}
    16311945
     1946void cocoaReadBuffer(GLenum mode)
     1947{
     1948    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
     1949
     1950    DEBUG_MSG_1(("glReadBuffer called: %d\n", mode));
     1951
     1952    performSelectorOnViewOneArg(@selector(readBuffer:), (id)mode);
     1953
     1954    [pPool release];
     1955}
     1956
     1957void cocoaDrawBuffer(GLenum mode)
     1958{
     1959    NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
     1960
     1961    DEBUG_MSG_1(("glDrawBuffer called: %d\n", mode));
     1962
     1963    performSelectorOnViewOneArg(@selector(drawBuffer:), (id)mode);
     1964
     1965    [pPool release];
     1966}
     1967
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette