renderspu_cocoa_helper.m revision 610972deee47d5e5229ccdb6c86cbb332d2b4626
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * VirtualBox OpenGL Cocoa Window System Helper implementation
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * Copyright (C) 2009 Sun Microsystems, Inc.
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * available from http://www.virtualbox.org. This file is free software;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * you can redistribute it and/or modify it under the terms of the GNU
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * General Public License (GPL) as published by the Free Software
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * additional information or have any questions.
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync#include "chromium.h" /* For the visual bits of chromium */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/* Debug macros */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync#define FBO 1 /* Disable this to see how the output is without the FBO in the middle of the processing chain. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync//#define SHOW_WINDOW_BACKGROUND 1 /* Define this to see the window background even if the window is clipped */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync//#define DEBUG_VERBOSE /* Define this could get some debug info about the messages flow. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync do {} while (0)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync do {} while (0)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync }while (0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync case GL_INVALID_ENUM: errStr = RTStrDup("GL_INVALID_ENUM"); break;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync case GL_INVALID_VALUE: errStr = RTStrDup("GL_INVALID_VALUE"); break;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync case GL_INVALID_OPERATION: errStr = RTStrDup("GL_INVALID_OPERATION"); break;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync case GL_STACK_OVERFLOW: errStr = RTStrDup("GL_STACK_OVERFLOW"); break;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync case GL_STACK_UNDERFLOW: errStr = RTStrDup("GL_STACK_UNDERFLOW"); break;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync case GL_OUT_OF_MEMORY: errStr = RTStrDup("GL_OUT_OF_MEMORY"); break;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync case GL_TABLE_TOO_LARGE: errStr = RTStrDup("GL_TABLE_TOO_LARGE"); break;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync DEBUG_MSG(("%s:%d: glError %d (%s)\n", file, line, g, errStr));
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync do {} while (0)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/* Custom OpenGL context class. This implementation doesn't allow to set a view
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * to the context, but save the view for later use. Also it saves a copy of the
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * pixel format used to create that context for later use. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/* The custom view class. This is the main class of the cocoa OpenGL
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * implementation. It manages an frame buffer object for the rendering of the
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * guest applications. The guest applications render in this frame buffer which
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * is bind to an OpenGL texture. To display the guest content, an secondary
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * shared OpenGL context of the main OpenGL context is created. The secondary
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * context is marked as non opaque & the texture is displayed on an object
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * which is composed out of the several visible region rectangles. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* FBO handling */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* For clipping */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Position/Size tracking */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (id)initWithFrame:(NSRect)frame thread:(RTTHREAD)aThread parentView:(NSView*)pParentView;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (void)setVisibleRegions:(GLint)cRects paRects:(GLint*)paRects;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/* Helper view. This view is added as a sub view of the parent view to track
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * main window changes. Whenever the main window is changed (which happens on
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * fullscreen/seamless entry/exit) the overlay window is informed & can add
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * them self as a child window again. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync-(id)initWithOverlayWindow:(OverlayWindow*)pOverlayWindow;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/* Custom window class. This is the overlay window which contains our custom
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * NSView. Its a direct child of the Qt Main window. It marks its background
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * transparent & non opaque to make clipping possible. It also disable mouse
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * events and handle frame change events of the parent view. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (id)initWithParentView:(NSView*)pParentView overlayView:(OverlayView*)pOverlayView;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (void)parentWindowFrameChanged:(NSNotification *)note;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/********************************************************************************
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync* OverlayOpenGLContext class implementation
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync********************************************************************************/
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync-(id)initWithFormat:(NSOpenGLPixelFormat*)format shareContext:(NSOpenGLContext*)share
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync self = [super initWithFormat:format shareContext:share];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync return [super view];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/********************************************************************************
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync* OverlayHelperView class implementation
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync********************************************************************************/
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync-(id)initWithOverlayWindow:(OverlayWindow*)pOverlayWindow
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [m_pOverlayWindow parentWindowChanged:[self window]];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/********************************************************************************
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync* OverlayWindow class implementation
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync********************************************************************************/
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (id)initWithParentView:(NSView*)pParentView overlayView:(OverlayView*)pOverlayView
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync if(self = [super initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO])
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_pOverlayHelperView = [[OverlayHelperView alloc] initWithOverlayWindow:self];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Add the helper view as a child of the parent view to get notifications */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Make sure this window is transparent */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* For debugging */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [self setBackgroundColor:[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:0.7]];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Disable mouse events for this window */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Initial set the position to the parents view top/left (Compiz fix). */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [m_pParentView convertPoint:NSZeroPoint toView:nil]]];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Set the overlay view as out content view */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Add ourself as a child to the parent views window */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [pParentWin addChildWindow:self ordered:NSWindowAbove];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Ask to get notifications when our parent window frame changes. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (void)parentWindowFrameChanged:(NSNotification*)pNote
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Reposition this window with the help of the OverlayView */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Ask to get notifications when our parent window frame changes. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Add us self as child window */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [pWindow addChildWindow:self ordered:NSWindowAbove];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Reshape the overlay view after a short waiting time to let the main
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * window resize itself properly. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [m_pOverlayView performSelector:@selector(reshape) withObject:nil afterDelay:0.2];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/********************************************************************************
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync* OverlayView class implementation
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync********************************************************************************/
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (id)initWithFrame:(NSRect)frame thread:(RTTHREAD)aThread parentView:(NSView*)pParentView
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Make some reasonable defaults */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync DEBUG_MSG(("Init view %X (%X)\n", (uint)self, (uint)mThread));
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// NSGraphicsContext*pC = [NSGraphicsContext currentContext];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [[NSColor blueColor] set];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// NSBezierPath *p = [[NSBezierPath alloc] bezierPathWithOvalInRect:[self frame]];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [p fill];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [[NSColor greenColor] set];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [p stroke];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// if ([self lockFocusIfCanDraw])
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [self renderFBOToView];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [self unlockFocus];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Getting the right screen coordinates of the parents frame is a little bit
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * complicated. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSPoint parentPos = [[m_pParentView window] convertBaseToScreen:[[m_pParentView superview] convertPointToBase:NSMakePoint(parentFrame.origin.x, parentFrame.origin.y + parentFrame.size.height)]];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Calculate the new screen coordinates of the overlay window. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSPoint childPos = NSMakePoint(m_Pos.x, m_Pos.y + m_Size.height);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync childPos = [[m_pParentView window] convertBaseToScreen:[[m_pParentView superview] convertPointToBase:childPos]];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Make a frame out of it. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSRect childFrame = NSMakeRect(childPos.x, childPos.y, m_Size.width, m_Size.height);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* We have to make sure that the overlay window will not be displayed out
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * of the parent window. So intersect both frames & use the result as the new
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * frame for the window. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSRect newFrame = NSIntersectionRect(parentFrame, childFrame);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Later we have to correct the texture position in the case the window is
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * out of the parents window frame. So save the shift values for later use. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_RootShift.x = parentFrame.origin.x - childFrame.origin.x;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_RootShift.y = parentFrame.origin.y - childFrame.origin.y;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// NSScrollView *pScrollView = [[[m_pParentView window] contentView] enclosingScrollView];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// if (pScrollView)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// NSRect scrollRect = [pScrollView documentVisibleRect];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// NSRect scrollRect = [m_pParentView visibleRect];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// printf ("sc rect: %d %d %d %d\n", (int) scrollRect.origin.x,(int) scrollRect.origin.y,(int) scrollRect.size.width,(int) scrollRect.size.height);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// NSRect b = [[m_pParentView superview] bounds];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// printf ("bound rect: %d %d %d %d\n", (int) b.origin.x,(int) b.origin.y,(int) b.size.width,(int) b.size.height);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// newFrame.origin.x += scrollRect.origin.x;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// newFrame.origin.y += scrollRect.origin.y;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Set the new frame. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* If not previously setup generate IDs for FBO and its associated texture. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Make sure the framebuffer extension is supported */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Get the extension name string. It is a space-delimited list of the
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * OpenGL extensions that are supported by the current renderer. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync isFBO = gluCheckExtension((const GLubyte*)"GL_EXT_framebuffer_object", strExt);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync DEBUG_MSG(("Your system does not support framebuffer extension\n"));
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Create FBO object */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* & the texture as well the depth/stencil render buffer */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync DEBUG_MSG_1(("Create FBO %d %d\n", m_FBOId, m_FBOTexId));
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glGenRenderbuffersEXT(1, &m_FBODepthId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glGenRenderbuffersEXT(1, &m_FBOStencilId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glGenRenderbuffersEXT(1, &m_FBODepthStencilPackedId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Bind to FBO */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync GLfloat imageAspectRatio = m_FBOTexSize.width / m_FBOTexSize.height;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Sanity check against maximum OpenGL texture size. If bigger adjust to
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * maximum possible size while maintain the aspect ratio. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// maxTexSize = 150;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync if (m_FBOTexSize.width > maxTexSize || m_FBOTexSize.height > maxTexSize)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_FBOTexSize.height = maxTexSize / imageAspectRatio;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_FBOTexSize.width = maxTexSize * imageAspectRatio;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Initialize FBO Texture */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, filter);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, filter);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* The GPUs like the GL_BGRA / GL_UNSIGNED_INT_8_8_8_8_REV combination
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync * others are also valid, but might incur a costly software translation. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync 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);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Now attach texture to the FBO as its color destination */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_FBODepthStencilPackedId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, m_FBOTexSize.width, m_FBOTexSize.height);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT,GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_FBODepthStencilPackedId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_FBODepthStencilPackedId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBODepthId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, r.size.width, r.size.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, m_FBODepthId, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOStencilId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, r.size.width, r.size.height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, NULL);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, m_FBOStencilId, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Initialize Depth Render Buffer */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_FBODepthId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, r.size.width, r.size.height);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* and attach it to the FBO */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_FBODepthId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, m_FBOStencilId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, r.size.width, r.size.height);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, m_FBOStencilId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glClearColor (clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glClearColor (0, 0, 0, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glClear(GL_COLOR_BUFFER_BIT);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Make sure the FBO was created succesfully. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync if (GL_FRAMEBUFFER_COMPLETE_EXT != glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT))
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync DEBUG_MSG(("Framebuffer Object creation or update failed!\n"));
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Initialize with one big visual region over the full size */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_paClipRects = (GLint*)RTMemAlloc(sizeof(GLint) * 4);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glDeleteRenderbuffersEXT(1, &m_FBODepthStencilPackedId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* We change the active view, so flush first */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// if ([NSOpenGLContext currentContext] != m_pGLCtx)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [m_pGLCtx update];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_FBOId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Create a shared context out of the main context. Use the same pixel format. */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_pSharedGLCtx = [[NSOpenGLContext alloc] initWithFormat:[(OverlayOpenGLContext*)m_pGLCtx openGLPixelFormat] shareContext:m_pGLCtx];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Set the new context as non opaque */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [m_pSharedGLCtx setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Only swap on screen refresh */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// GLint swap = 1;
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [m_pSharedGLCtx setValues:&swap forParameter:NSOpenGLCPSwapInterval];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Set this view as the drawable for the new context */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// printf ("renderFBOToView\n");
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// CGLLockContext([m_pGLCtx CGLContextObj]);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Setup all matrices */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Clear background to transparent */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync glBindTexture(GL_TEXTURE_RECTANGLE_ARB, m_FBOTexId);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// CGLUnlockContext([m_pGLCtx CGLContextObj]);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync- (void)setVisibleRegions:(GLint)cRects paRects:(GLint*)paRects
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync m_paClipRects = (GLint*)RTMemAlloc(sizeof(GLint) * 4 * cRects);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync memcpy(m_paClipRects, paRects, sizeof(GLint) * 4 * cRects);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/********************************************************************************
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync* OpenGL context management
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync********************************************************************************/
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaGLCtxCreate(NativeGLCtxRef *ppCtx, GLbitfield fVisParams)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute)24
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Mark the end */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Choose a pixel format */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSOpenGLPixelFormat* pFmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync *ppCtx = [[OverlayOpenGLContext alloc] initWithFormat:pFmt shareContext:nil];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Enable multi threaded OpenGL engine */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// CGLContextObj cglCtx = [*ppCtx CGLContextObj];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// CGLError err = CGLEnable(cglCtx, kCGLCEMPEngine);
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// if (err != kCGLNoError)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// printf ("Couldn't enable MT OpenGL engine!\n");
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// [pCtx release];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/********************************************************************************
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync* View management
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync********************************************************************************/
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaViewCreate(NativeViewRef *ppView, NativeViewRef pParentView, GLbitfield fVisParams)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Create our worker view */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync OverlayView* pView = [[OverlayView alloc] initWithFrame:NSZeroRect thread:RTThreadSelf() parentView:pParentView];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* We need a real window as container for the view */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [[OverlayWindow alloc] initWithParentView:pParentView overlayView:pView];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync /* Return the freshly created overlay view */
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaViewShow(NativeViewRef pView, GLboolean fShowIt)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaViewSetPosition(NativeViewRef pView, NativeViewRef pParentView, int x, int y)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaViewSetSize(NativeViewRef pView, int w, int h)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaViewGetGeometry(NativeViewRef pView, int *pX, int *pY, int *pW, int *pH)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaViewMakeCurrentContext(NativeViewRef pView, NativeGLCtxRef pCtx)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaViewSetVisibleRegion(NativeViewRef pView, GLint cRects, GLint* paRects)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync [(OverlayView*)pView setVisibleRegions:cRects paRects:paRects];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync/********************************************************************************
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync* Additional OpenGL wrapper
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync********************************************************************************/
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync// glFlush();
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync if ([pView respondsToSelector:@selector(flushFBO)])
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSOpenGLContext *pCtx = [NSOpenGLContext currentContext];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync if ([pView respondsToSelector:@selector(finishFBO)])
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsyncvoid cocoaBindFramebufferEXT(GLenum target, GLuint framebuffer)
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync NSAutoreleasePool *pPool = [[NSAutoreleasePool alloc] init];
610972deee47d5e5229ccdb6c86cbb332d2b4626vboxsync DEBUG_MSG_1(("glRenderspuBindFramebufferEXT called %d\n", framebuffer));