From b7b86ba5f4a819f967a58475ee7c15907dbd057a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 10 Dec 2009 17:52:40 +0000 Subject: [PATCH] From Stephan Huber, "attached you'll find the missing GraphicsHandleCocoa-implementation." --- .../osgViewer/api/Cocoa/GraphicsHandleCocoa | 53 +++++++++++++++++++ .../osgViewer/api/Cocoa/GraphicsWindowCocoa | 8 +-- include/osgViewer/api/Cocoa/PixelBufferCocoa | 12 ++--- src/osgViewer/CMakeLists.txt | 1 + src/osgViewer/GraphicsWindowCocoa.mm | 13 ++--- src/osgViewer/PixelBufferCocoa.mm | 50 ++++++++--------- 6 files changed, 88 insertions(+), 49 deletions(-) create mode 100644 include/osgViewer/api/Cocoa/GraphicsHandleCocoa diff --git a/include/osgViewer/api/Cocoa/GraphicsHandleCocoa b/include/osgViewer/api/Cocoa/GraphicsHandleCocoa new file mode 100644 index 000000000..d8558c5b5 --- /dev/null +++ b/include/osgViewer/api/Cocoa/GraphicsHandleCocoa @@ -0,0 +1,53 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2009 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGVIEWER_GRAPHICSHANDLECOCOA +#define OSGVIEWER_GRAPHICSHANDLECOCOA 1 + +#include + + +#ifdef __OBJC__ +@class NSOpenGLContext; +#else +class NSOpenGLContext; +#endif + +namespace osgViewer +{ + +/** Class to encapsulate platform-specific OpenGL context handle variables. + * Derived osg::GraphicsContext classes can inherit from this class to + * share OpenGL resources.*/ + +class OSGVIEWER_EXPORT GraphicsHandleCocoa +{ + public: + + GraphicsHandleCocoa(): + _context(0) {} + + /** Set native AGL graphics context.*/ + inline void setNSOpenGLContext(NSOpenGLContext* context) { _context = context; } + + /** Get native AGL graphics context.*/ + inline NSOpenGLContext* getNSOpenGLContext() const { return _context; } + + protected: + + NSOpenGLContext* _context; +}; + +} + +#endif diff --git a/include/osgViewer/api/Cocoa/GraphicsWindowCocoa b/include/osgViewer/api/Cocoa/GraphicsWindowCocoa index e122624b4..965ce3a1b 100755 --- a/include/osgViewer/api/Cocoa/GraphicsWindowCocoa +++ b/include/osgViewer/api/Cocoa/GraphicsWindowCocoa @@ -36,18 +36,21 @@ class NSView; #endif #include +#include // we may not include any cocoa-header here, because this will pollute the name-sapce and tend to compile-errors namespace osgViewer { -class GraphicsWindowCocoa : public osgViewer::GraphicsWindow +class GraphicsWindowCocoa : public osgViewer::GraphicsWindow, public osgViewer::GraphicsHandleCocoa { public: class Implementation; GraphicsWindowCocoa(osg::GraphicsContext::Traits* traits): + osgViewer::GraphicsWindow(), + osgViewer::GraphicsHandleCocoa(), _valid(false), _initialized(false), _realized(false), @@ -55,8 +58,7 @@ class GraphicsWindowCocoa : public osgViewer::GraphicsWindow _checkForEvents(true), _ownsWindow(true), _currentCursor(RightArrowCursor), - _window(NULL), - _context(NULL) + _window(NULL) { _traits = traits; diff --git a/include/osgViewer/api/Cocoa/PixelBufferCocoa b/include/osgViewer/api/Cocoa/PixelBufferCocoa index c068bb6ff..6a1a51731 100755 --- a/include/osgViewer/api/Cocoa/PixelBufferCocoa +++ b/include/osgViewer/api/Cocoa/PixelBufferCocoa @@ -20,23 +20,19 @@ #include #include - - -#ifdef __OBJC__ -@class NSOpenGLContext; -#else -class NSOpenGLContext; -#endif +#include namespace osgViewer { -class OSGVIEWER_EXPORT PixelBufferCocoa : public osg::GraphicsContext +class OSGVIEWER_EXPORT PixelBufferCocoa : public osg::GraphicsContext, public osgViewer::GraphicsHandleCocoa { public: struct Implementation; PixelBufferCocoa(osg::GraphicsContext::Traits* traits): + osg::GraphicsContext(), + osgViewer::GraphicsHandleCocoa(), _valid(false), _initialized(false), _realized(false), diff --git a/src/osgViewer/CMakeLists.txt b/src/osgViewer/CMakeLists.txt index 53cc4305d..ffc42f63b 100644 --- a/src/osgViewer/CMakeLists.txt +++ b/src/osgViewer/CMakeLists.txt @@ -72,6 +72,7 @@ ELSE() IF(${OSG_WINDOWING_SYSTEM} STREQUAL "Cocoa") ADD_DEFINITIONS(-DUSE_DARWIN_COCOA_IMPLEMENTATION) SET(LIB_PUBLIC_HEADERS ${LIB_PUBLIC_HEADERS} + ${HEADER_PATH}/api/Cocoa/GraphicsHandleCocoa ${HEADER_PATH}/api/Cocoa/GraphicsWindowCocoa ${HEADER_PATH}/api/Cocoa/PixelBufferCocoa ) diff --git a/src/osgViewer/GraphicsWindowCocoa.mm b/src/osgViewer/GraphicsWindowCocoa.mm index c90b23750..f5c736db1 100755 --- a/src/osgViewer/GraphicsWindowCocoa.mm +++ b/src/osgViewer/GraphicsWindowCocoa.mm @@ -971,17 +971,10 @@ bool GraphicsWindowCocoa::realizeImplementation() // create the context NSOpenGLContext* sharedContext = NULL; - GraphicsWindowCocoa* graphicsWindowCocoa = dynamic_cast(_traits->sharedContext); - if (graphicsWindowCocoa) + GraphicsHandleCocoa* graphicsHandleCocoa = dynamic_cast(_traits->sharedContext); + if (graphicsHandleCocoa) { - sharedContext = graphicsWindowCocoa->getContext(); - } - else - { - PixelBufferCocoa* pixelbuffer = dynamic_cast(_traits->sharedContext); - if (pixelbuffer) { - sharedContext = pixelbuffer->getContext(); - } + sharedContext = graphicsHandleCocoa->getNSOpenGLContext(); } NSOpenGLPixelFormat* pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr ]; diff --git a/src/osgViewer/PixelBufferCocoa.mm b/src/osgViewer/PixelBufferCocoa.mm index d25915860..a16cef0a1 100755 --- a/src/osgViewer/PixelBufferCocoa.mm +++ b/src/osgViewer/PixelBufferCocoa.mm @@ -17,20 +17,20 @@ namespace osgViewer { void PixelBufferCocoa::init() { - //std::cout << "PixelBufferCocoa :: init not implemented yet " << std::endl; - - _valid = _initialized = true; - + //std::cout << "PixelBufferCocoa :: init not implemented yet " << std::endl; + + _valid = _initialized = true; + } bool PixelBufferCocoa::realizeImplementation() { - std::cout << "PixelBufferCocoa :: realizeImplementation not implemented yet " << std::endl; - + std::cout << "PixelBufferCocoa :: realizeImplementation not implemented yet " << std::endl; + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; - - NSOpenGLPixelFormatAttribute attr[32]; + + NSOpenGLPixelFormatAttribute attr[32]; int i = 0; attr[i++] = NSOpenGLPFADepthSize; @@ -65,20 +65,14 @@ bool PixelBufferCocoa::realizeImplementation() // create the context NSOpenGLContext* sharedContext = NULL; - GraphicsWindowCocoa* graphicsWindowCocoa = dynamic_cast(_traits->sharedContext); - if (graphicsWindowCocoa) + GraphicsHandleCocoa* graphicsHandleCocoa = dynamic_cast(_traits->sharedContext); + if (graphicsHandleCocoa) { - sharedContext = graphicsWindowCocoa->getContext(); + sharedContext = graphicsHandleCocoa->getNSOpenGLContext(); } - else - { - PixelBufferCocoa* pixelbuffer = dynamic_cast(_traits->sharedContext); - if (pixelbuffer) { - sharedContext = pixelbuffer->getContext(); - } - } - - NSOpenGLPixelFormat* pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr ]; + + + NSOpenGLPixelFormat* pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attr ]; _context = [[NSOpenGLContext alloc] initWithFormat: pixelformat shareContext: sharedContext]; NSOpenGLPixelBuffer* pbuffer = [[NSOpenGLPixelBuffer alloc] initWithTextureTarget: _traits->target textureInternalFormat: _traits->format textureMaxMipMapLevel: _traits->level pixelsWide: _traits->width pixelsHigh: _traits->height]; @@ -93,7 +87,7 @@ bool PixelBufferCocoa::realizeImplementation() void PixelBufferCocoa::closeImplementation() { - _realized = false; + _realized = false; } @@ -103,23 +97,23 @@ void PixelBufferCocoa::closeImplementation() /** Make this graphics context current.*/ bool PixelBufferCocoa::makeCurrentImplementation() { - // osg::notify(osg::INFO) << "PixelBufferCocoa::makeCurrentImplementation" << std::endl; + // osg::notify(osg::INFO) << "PixelBufferCocoa::makeCurrentImplementation" << std::endl; [_context makeCurrentContext]; - return true; + return true; } /** Make this graphics context current with specified read context implementation. */ bool PixelBufferCocoa::makeContextCurrentImplementation(osg::GraphicsContext* readContext) { - return makeCurrentImplementation(); + return makeCurrentImplementation(); } /** Release the graphics context.*/ bool PixelBufferCocoa::releaseContextImplementation() { - // osg::notify(osg::INFO) << "PixelBufferCocoa::releaseContextImplementation" << std::endl; + // osg::notify(osg::INFO) << "PixelBufferCocoa::releaseContextImplementation" << std::endl; [NSOpenGLContext clearCurrentContext]; return true; @@ -128,18 +122,18 @@ bool PixelBufferCocoa::releaseContextImplementation() /** Bind the graphics context to associated texture implementation.*/ void PixelBufferCocoa::bindPBufferToTextureImplementation( GLenum buffer ) { - std::cout << "PixelBufferCocoa :: bindPBufferToTextureImplementation not implemented yet " << std::endl; + std::cout << "PixelBufferCocoa :: bindPBufferToTextureImplementation not implemented yet " << std::endl; } /** Swap the front and back buffers.*/ void PixelBufferCocoa::swapBuffersImplementation() { - osg::notify(osg::INFO) << "PixelBufferCocoa::swapBuffersImplementation" << std::endl; + osg::notify(osg::INFO) << "PixelBufferCocoa::swapBuffersImplementation" << std::endl; [_context flushBuffer]; } PixelBufferCocoa::~PixelBufferCocoa() - { + { [_context release]; }