Index: /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/PixelBufferCocoa
===================================================================
--- /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/PixelBufferCocoa (revision 10858)
+++ /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/PixelBufferCocoa (revision 10887)
@@ -21,16 +21,10 @@
 #include <osg/GraphicsContext>
 #include <osgViewer/Export>
-
-
-#ifdef __OBJC__
-@class NSOpenGLContext;
-#else
-class NSOpenGLContext;
-#endif
+#include <osgViewer/api/Cocoa/GraphicsHandleCocoa>
 
 namespace osgViewer
 {
 
-class OSGVIEWER_EXPORT PixelBufferCocoa : public osg::GraphicsContext
+class OSGVIEWER_EXPORT PixelBufferCocoa : public osg::GraphicsContext, public osgViewer::GraphicsHandleCocoa
 {
     public:
@@ -38,4 +32,6 @@
 
         PixelBufferCocoa(osg::GraphicsContext::Traits* traits):
+            osg::GraphicsContext(),
+            osgViewer::GraphicsHandleCocoa(),
             _valid(false),
             _initialized(false),
Index: /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/GraphicsHandleCocoa
===================================================================
--- /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/GraphicsHandleCocoa (revision 10887)
+++ /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/GraphicsHandleCocoa (revision 10887)
@@ -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 <osgViewer/Export>
+
+
+#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
Index: /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/GraphicsWindowCocoa
===================================================================
--- /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/GraphicsWindowCocoa (revision 10456)
+++ /OpenSceneGraph/trunk/include/osgViewer/api/Cocoa/GraphicsWindowCocoa (revision 10887)
@@ -37,4 +37,5 @@
 
 #include <osgViewer/GraphicsWindow>
+#include <osgViewer/api/Cocoa/GraphicsHandleCocoa>
 
 // we may not include any cocoa-header here, because this will pollute the name-sapce and tend to compile-errors
@@ -43,5 +44,5 @@
 {
 
-class GraphicsWindowCocoa : public osgViewer::GraphicsWindow
+class GraphicsWindowCocoa : public osgViewer::GraphicsWindow, public osgViewer::GraphicsHandleCocoa
 {
     public:
@@ -49,4 +50,6 @@
 
         GraphicsWindowCocoa(osg::GraphicsContext::Traits* traits):
+            osgViewer::GraphicsWindow(),
+            osgViewer::GraphicsHandleCocoa(),
             _valid(false),
             _initialized(false),
@@ -56,6 +59,5 @@
             _ownsWindow(true),
             _currentCursor(RightArrowCursor),
-            _window(NULL),
-            _context(NULL)
+            _window(NULL)
         {
             _traits = traits;
Index: /OpenSceneGraph/trunk/src/osgViewer/PixelBufferCocoa.mm
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/PixelBufferCocoa.mm (revision 9879)
+++ /OpenSceneGraph/trunk/src/osgViewer/PixelBufferCocoa.mm (revision 10887)
@@ -18,8 +18,8 @@
 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;    
+    
 
 }
@@ -27,9 +27,9 @@
 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;
     
@@ -66,18 +66,12 @@
     NSOpenGLContext* sharedContext = NULL;
     
-    GraphicsWindowCocoa* graphicsWindowCocoa = dynamic_cast<GraphicsWindowCocoa*>(_traits->sharedContext);
-    if (graphicsWindowCocoa) 
+    GraphicsHandleCocoa* graphicsHandleCocoa = dynamic_cast<GraphicsHandleCocoa*>(_traits->sharedContext);
+    if (graphicsHandleCocoa) 
     {
-        sharedContext = graphicsWindowCocoa->getContext();
+        sharedContext = graphicsHandleCocoa->getNSOpenGLContext();
     }
-    else
-    {
-        PixelBufferCocoa* pixelbuffer = dynamic_cast<PixelBufferCocoa*>(_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];
@@ -94,5 +88,5 @@
 void PixelBufferCocoa::closeImplementation()
 {
-	_realized = false;
+    _realized = false;
     
     
@@ -104,8 +98,8 @@
 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;
 }
 
@@ -114,5 +108,5 @@
 bool PixelBufferCocoa::makeContextCurrentImplementation(osg::GraphicsContext* readContext)
 {
-	return makeCurrentImplementation();
+    return makeCurrentImplementation();
 }
 
@@ -120,5 +114,5 @@
 bool PixelBufferCocoa::releaseContextImplementation()
 {
-	// osg::notify(osg::INFO) << "PixelBufferCocoa::releaseContextImplementation" << std::endl;
+    // osg::notify(osg::INFO) << "PixelBufferCocoa::releaseContextImplementation" << std::endl;
     
     [NSOpenGLContext clearCurrentContext];
@@ -129,5 +123,5 @@
 void PixelBufferCocoa::bindPBufferToTextureImplementation( GLenum buffer )
 {
-	std::cout << "PixelBufferCocoa :: bindPBufferToTextureImplementation not implemented yet " << std::endl;
+    std::cout << "PixelBufferCocoa :: bindPBufferToTextureImplementation not implemented yet " << std::endl;
 }
 
@@ -135,10 +129,10 @@
 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];
  }
Index: /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm (revision 10456)
+++ /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCocoa.mm (revision 10887)
@@ -972,15 +972,8 @@
     NSOpenGLContext* sharedContext = NULL;
     
-    GraphicsWindowCocoa* graphicsWindowCocoa = dynamic_cast<GraphicsWindowCocoa*>(_traits->sharedContext);
-    if (graphicsWindowCocoa) 
-    {
-        sharedContext = graphicsWindowCocoa->getContext();
-    }
-    else
-    {
-        PixelBufferCocoa* pixelbuffer = dynamic_cast<PixelBufferCocoa*>(_traits->sharedContext);
-        if (pixelbuffer) {
-            sharedContext = pixelbuffer->getContext();
-        }
+    GraphicsHandleCocoa* graphicsHandleCocoa = dynamic_cast<GraphicsHandleCocoa*>(_traits->sharedContext);
+    if (graphicsHandleCocoa) 
+    {
+        sharedContext = graphicsHandleCocoa->getNSOpenGLContext();
     }
     
Index: /OpenSceneGraph/trunk/src/osgViewer/CMakeLists.txt
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/CMakeLists.txt (revision 10813)
+++ /OpenSceneGraph/trunk/src/osgViewer/CMakeLists.txt (revision 10887)
@@ -73,4 +73,5 @@
         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
