avmedia/source/opengl/oglplayer.cxx                                    |   15 
+-
 avmedia/source/opengl/oglplayer.hxx                                    |    2 
 avmedia/source/opengl/oglwindow.cxx                                    |   10 -
 avmedia/source/opengl/oglwindow.hxx                                    |    4 
 canvas/source/opengl/ogl_spritedevicehelper.cxx                        |   33 
++---
 canvas/source/opengl/ogl_spritedevicehelper.hxx                        |    2 
 include/vcl/opengl/OpenGLContext.hxx                                   |   19 
+--
 slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionerImpl.cxx |    8 -
 vcl/inc/opengl/salbmp.hxx                                              |    4 
 vcl/inc/opengl/win/gdiimpl.hxx                                         |    7 -
 vcl/inc/opengl/x11/gdiimpl.hxx                                         |    4 
 vcl/inc/openglgdiimpl.hxx                                              |   10 -
 vcl/inc/salgdi.hxx                                                     |    2 
 vcl/opengl/gdiimpl.cxx                                                 |   47 
++-----
 vcl/opengl/salbmp.cxx                                                  |   16 
+-
 vcl/opengl/texture.cxx                                                 |    6 -
 vcl/opengl/win/gdiimpl.cxx                                             |   10 -
 vcl/opengl/x11/gdiimpl.cxx                                             |   10 -
 vcl/source/app/svdata.cxx                                              |   12 
--
 vcl/source/gdi/salgdilayout.cxx                                        |    2 
 vcl/source/opengl/OpenGLContext.cxx                                    |   59 
+++-------
 vcl/source/window/openglwin.cxx                                        |    7 -
 vcl/unx/generic/window/salframe.cxx                                    |    4 
 vcl/workben/vcldemo.cxx                                                |   10 -
 24 files changed, 129 insertions(+), 174 deletions(-)

New commits:
commit 56900a441de1d4cc896ad1e36a44622ed1598fad
Author: Michael Meeks <michael.me...@collabora.com>
Date:   Tue Sep 8 11:46:13 2015 +0100

    tdf#94006 - fix OpenGLContext mis-use in several places.
    
    gltf rendering, OpenGL canvas, GL transitions & GL capable (charts)
    Avoid GLX operations on un-initialized contexts.
    
    Change-Id: I7f523640f66ab656896181e5c865879234f6640e

diff --git a/avmedia/source/opengl/oglplayer.cxx 
b/avmedia/source/opengl/oglplayer.cxx
index a8a9f7d..c7bb7fb 100644
--- a/avmedia/source/opengl/oglplayer.cxx
+++ b/avmedia/source/opengl/oglplayer.cxx
@@ -28,6 +28,7 @@ namespace avmedia { namespace ogl {
 OGLPlayer::OGLPlayer()
     : Player_BASE(m_aMutex)
     , m_pHandle(NULL)
+    , m_xContext(OpenGLContext::Create())
     , m_pOGLWindow(NULL)
     , m_bIsRendering(false)
 {
@@ -38,7 +39,7 @@ OGLPlayer::~OGLPlayer()
     osl::MutexGuard aGuard(m_aMutex);
     if( m_pHandle )
     {
-        m_aContext.makeCurrent();
+        m_xContext->makeCurrent();
         gltf_renderer_release(m_pHandle);
     }
     releaseInputFiles();
@@ -258,13 +259,13 @@ uno::Reference< media::XPlayerWindow > SAL_CALL 
OGLPlayer::createPlayerWindow( c
     }
     assert(pChildWindow->GetParent());
 
-    if( !m_aContext.init(pChildWindow) )
+    if( !m_xContext->init(pChildWindow) )
     {
         SAL_WARN("avmedia.opengl", "Context initialization failed");
         return uno::Reference< media::XPlayerWindow >();
     }
 
-    if( !m_aContext.supportMultiSampling() )
+    if( !m_xContext->supportMultiSampling() )
     {
         SAL_WARN("avmedia.opengl", "Context does not support multisampling!");
         return uno::Reference< media::XPlayerWindow >();
@@ -277,7 +278,7 @@ uno::Reference< media::XPlayerWindow > SAL_CALL 
OGLPlayer::createPlayerWindow( c
     }
 
     Size aSize = pChildWindow->GetSizePixel();
-    m_aContext.setWinSize(aSize);
+    m_xContext->setWinSize(aSize);
     m_pHandle->viewport.x = 0;
     m_pHandle->viewport.y = 0;
     m_pHandle->viewport.width = aSize.Width();
@@ -294,7 +295,7 @@ uno::Reference< media::XPlayerWindow > SAL_CALL 
OGLPlayer::createPlayerWindow( c
     // The background color is white by default, but we need to separate the
     // OpenGL window from the main window so set background color to grey
     glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
-    m_pOGLWindow = new OGLWindow(*m_pHandle, m_aContext, 
*pChildWindow->GetParent());
+    m_pOGLWindow = new OGLWindow(*m_pHandle, m_xContext, 
*pChildWindow->GetParent());
     return uno::Reference< media::XPlayerWindow >( m_pOGLWindow );
 }
 
@@ -304,13 +305,13 @@ uno::Reference< media::XFrameGrabber > SAL_CALL 
OGLPlayer::createFrameGrabber()
     osl::MutexGuard aGuard(m_aMutex);
     assert(m_pHandle);
 
-    if( !m_aContext.init() )
+    if( !m_xContext->init() )
     {
         SAL_WARN("avmedia.opengl", "Offscreen context initialization failed");
         return uno::Reference< media::XFrameGrabber >();
     }
 
-    if( !m_aContext.supportMultiSampling() )
+    if( !m_xContext->supportMultiSampling() )
     {
         SAL_WARN("avmedia.opengl", "Context does not support multisampling!");
         return uno::Reference< media::XFrameGrabber >();
diff --git a/avmedia/source/opengl/oglplayer.hxx 
b/avmedia/source/opengl/oglplayer.hxx
index 64f8540..1a90f7b 100644
--- a/avmedia/source/opengl/oglplayer.hxx
+++ b/avmedia/source/opengl/oglplayer.hxx
@@ -68,7 +68,7 @@ private:
     libgltf::glTFHandle* m_pHandle;
     std::vector<libgltf::glTFFile> m_vInputFiles;
 
-    OpenGLContext m_aContext;
+    rtl::Reference<OpenGLContext> m_xContext;
     AutoTimer m_aTimer;
     OGLWindow* m_pOGLWindow;
     bool m_bIsRendering;
diff --git a/avmedia/source/opengl/oglwindow.cxx 
b/avmedia/source/opengl/oglwindow.cxx
index 956ce96..fe637f8 100644
--- a/avmedia/source/opengl/oglwindow.cxx
+++ b/avmedia/source/opengl/oglwindow.cxx
@@ -15,9 +15,9 @@ using namespace libgltf;
 
 namespace avmedia { namespace ogl {
 
-OGLWindow::OGLWindow( glTFHandle& rHandle, OpenGLContext& rContext, 
vcl::Window& rEventHandlerParent )
+OGLWindow::OGLWindow( glTFHandle& rHandle, const rtl::Reference<OpenGLContext> 
&rContext, vcl::Window& rEventHandlerParent )
     : m_rHandle( rHandle )
-    , m_rContext( rContext )
+    , m_xContext( rContext )
     , m_rEventHandler( rEventHandlerParent )
     , m_bVisible ( false )
     , m_aLastMousePos(Point(0,0))
@@ -32,7 +32,7 @@ OGLWindow::~OGLWindow()
 
 void SAL_CALL OGLWindow::update() throw (css::uno::RuntimeException, 
std::exception)
 {
-    m_rContext.makeCurrent();
+    m_xContext->makeCurrent();
     int nRet = gltf_prepare_renderer(&m_rHandle);
     if( nRet != 0 )
     {
@@ -41,7 +41,7 @@ void SAL_CALL OGLWindow::update() throw 
(css::uno::RuntimeException, std::except
     }
     gltf_renderer(&m_rHandle);
     gltf_complete_renderer(&m_rHandle);
-    m_rContext.swapBuffers();
+    m_xContext->swapBuffers();
 }
 
 sal_Bool SAL_CALL OGLWindow::setZoomLevel( css::media::ZoomLevel 
/*eZoomLevel*/ ) throw (css::uno::RuntimeException, std::exception)
@@ -98,7 +98,7 @@ void SAL_CALL OGLWindow::setPosSize( sal_Int32 nX, sal_Int32 
nY, sal_Int32 nWidt
     if( m_rHandle.viewport.x != nX || m_rHandle.viewport.x != nY ||
         m_rHandle.viewport.width != nWidth || m_rHandle.viewport.height != 
nHeight )
     {
-        m_rContext.setWinSize(Size(nWidth,nHeight));
+        m_xContext->setWinSize(Size(nWidth,nHeight));
         m_rHandle.viewport.x = nX;
         m_rHandle.viewport.y = nY;
         m_rHandle.viewport.width = nWidth;
diff --git a/avmedia/source/opengl/oglwindow.hxx 
b/avmedia/source/opengl/oglwindow.hxx
index 9abc307..71ca91a 100644
--- a/avmedia/source/opengl/oglwindow.hxx
+++ b/avmedia/source/opengl/oglwindow.hxx
@@ -27,7 +27,7 @@ namespace avmedia { namespace ogl {
 class OGLWindow : public ::cppu::WeakImplHelper< css::media::XPlayerWindow, 
css::lang::XServiceInfo >
 {
 public:
-    OGLWindow( libgltf::glTFHandle& rHandle, OpenGLContext& rContext, 
vcl::Window& rEventHandlerParent );
+    OGLWindow( libgltf::glTFHandle& rHandle, const 
rtl::Reference<OpenGLContext> & rContext, vcl::Window& rEventHandlerParent );
     virtual ~OGLWindow();
 
     virtual void SAL_CALL update() throw (css::uno::RuntimeException, 
std::exception) SAL_OVERRIDE;
@@ -66,7 +66,7 @@ private:
     DECL_LINK( CameraHandler, VclWindowEvent* );
 
     libgltf::glTFHandle& m_rHandle;
-    OpenGLContext& m_rContext;
+    rtl::Reference<OpenGLContext> m_xContext;
     vcl::Window& m_rEventHandler;
 
     bool m_bVisible;
diff --git a/canvas/source/opengl/ogl_spritedevicehelper.cxx 
b/canvas/source/opengl/ogl_spritedevicehelper.cxx
index 3c58b40..ab7e764 100644
--- a/canvas/source/opengl/ogl_spritedevicehelper.cxx
+++ b/canvas/source/opengl/ogl_spritedevicehelper.cxx
@@ -85,7 +85,8 @@ namespace oglcanvas
         mnRadialTwoColorGradientProgram(0),
         mnRadialMultiColorGradientProgram(0),
         mnRectangularTwoColorGradientProgram(0),
-        mnRectangularMultiColorGradientProgram(0)
+        mnRectangularMultiColorGradientProgram(0),
+        mxContext(OpenGLContext::Create())
     {}
 
     SpriteDeviceHelper::~SpriteDeviceHelper()
@@ -102,8 +103,8 @@ namespace oglcanvas
                 VCLUnoHelper::GetInterface(&rWindow),
                 uno::UNO_QUERY_THROW) );
 
-        maContext.requestLegacyContext();
-        maContext.init(&rWindow);
+        mxContext->requestLegacyContext();
+        mxContext->init(&rWindow);
         // init window context
         initContext();
 
@@ -125,7 +126,7 @@ namespace oglcanvas
         mnRectangularTwoColorGradientProgram =
             OpenGLHelper::LoadShaders("dummyVertexShader", 
"rectangularTwoColorGradientFragmentShader");
 
-        maContext.makeCurrent();
+        mxContext->makeCurrent();
 
         notifySizeUpdate(rViewArea);
         // TODO(E3): check for GL_ARB_imaging extension
@@ -138,7 +139,7 @@ namespace oglcanvas
         mpDevice = NULL;
         mpTextureCache.reset();
 
-        if( maContext.isInitialized() )
+        if( mxContext->isInitialized() )
         {
             glDeleteProgram( mnRectangularTwoColorGradientProgram );
             glDeleteProgram( mnRectangularMultiColorGradientProgram );
@@ -151,11 +152,11 @@ namespace oglcanvas
 
     geometry::RealSize2D SpriteDeviceHelper::getPhysicalResolution()
     {
-        if( !maContext.isInitialized() )
+        if( !mxContext->isInitialized() )
             return ::canvas::tools::createInfiniteSize2D(); // we're disposed
 
         // Map a one-by-one millimeter box to pixel
-        SystemChildWindow* pChildWindow = maContext.getChildWindow();
+        SystemChildWindow* pChildWindow = mxContext->getChildWindow();
         const MapMode aOldMapMode( pChildWindow->GetMapMode() );
         pChildWindow->SetMapMode( MapMode(MAP_MM) );
         const Size aPixelSize( pChildWindow->LogicToPixel(Size(1,1)) );
@@ -166,11 +167,11 @@ namespace oglcanvas
 
     geometry::RealSize2D SpriteDeviceHelper::getPhysicalSize()
     {
-        if( !maContext.isInitialized() )
+        if( !mxContext->isInitialized() )
             return ::canvas::tools::createInfiniteSize2D(); // we're disposed
 
         // Map the pixel dimensions of the output window to millimeter
-        SystemChildWindow* pChildWindow = maContext.getChildWindow();
+        SystemChildWindow* pChildWindow = mxContext->getChildWindow();
         const MapMode aOldMapMode( pChildWindow->GetMapMode() );
         pChildWindow->SetMapMode( MapMode(MAP_MM) );
         const Size aLogSize( 
pChildWindow->PixelToLogic(pChildWindow->GetOutputSizePixel()) );
@@ -271,13 +272,13 @@ namespace oglcanvas
     bool SpriteDeviceHelper::showBuffer( bool bIsVisible, bool /*bUpdateAll*/ )
     {
         // hidden or disposed?
-        if( !bIsVisible || !maContext.isInitialized() || !mpSpriteCanvas )
+        if( !bIsVisible || !mxContext->isInitialized() || !mpSpriteCanvas )
             return false;
 
         if( !activateWindowContext() )
             return false;
 
-        SystemChildWindow* pChildWindow = maContext.getChildWindow();
+        SystemChildWindow* pChildWindow = mxContext->getChildWindow();
         const ::Size& rOutputSize = pChildWindow->GetSizePixel();
         initTransformation(rOutputSize);
 
@@ -326,7 +327,7 @@ namespace oglcanvas
         unx::glXWaitGL();
         XSync( reinterpret_cast<unx::Display*>(mpDisplay), false );
         */
-        maContext.swapBuffers();
+        mxContext->swapBuffers();
 
         // flush texture cache, such that it does not build up
         // indefinitely.
@@ -350,7 +351,7 @@ namespace oglcanvas
 
     uno::Any SpriteDeviceHelper::getDeviceHandle() const
     {
-        const SystemChildWindow* pChildWindow = maContext.getChildWindow();
+        const SystemChildWindow* pChildWindow = mxContext->getChildWindow();
         return uno::makeAny( reinterpret_cast< sal_Int64 >(pChildWindow) );
     }
 
@@ -369,9 +370,9 @@ namespace oglcanvas
 
     void SpriteDeviceHelper::notifySizeUpdate( const awt::Rectangle& rBounds )
     {
-        if( maContext.isInitialized() )
+        if( mxContext->isInitialized() )
         {
-            SystemChildWindow* pChildWindow = maContext.getChildWindow();
+            SystemChildWindow* pChildWindow = mxContext->getChildWindow();
             pChildWindow->setPosSizePixel(
                 0,0,rBounds.Width,rBounds.Height);
         }
@@ -506,7 +507,7 @@ namespace oglcanvas
 
     bool SpriteDeviceHelper::activateWindowContext()
     {
-        maContext.makeCurrent();
+        mxContext->makeCurrent();
         return true;
     }
 
diff --git a/canvas/source/opengl/ogl_spritedevicehelper.hxx 
b/canvas/source/opengl/ogl_spritedevicehelper.hxx
index ba85f54..2a99145 100644
--- a/canvas/source/opengl/ogl_spritedevicehelper.hxx
+++ b/canvas/source/opengl/ogl_spritedevicehelper.hxx
@@ -140,7 +140,7 @@ namespace oglcanvas
         unsigned int                                       
mnRectangularTwoColorGradientProgram;
         unsigned int                                       
mnRectangularMultiColorGradientProgram;
 
-        OpenGLContext                                      maContext;
+        rtl::Reference<OpenGLContext>                      mxContext;
     };
 }
 
diff --git a/include/vcl/opengl/OpenGLContext.hxx 
b/include/vcl/opengl/OpenGLContext.hxx
index b520d2c..0addfcb 100644
--- a/include/vcl/opengl/OpenGLContext.hxx
+++ b/include/vcl/opengl/OpenGLContext.hxx
@@ -169,8 +169,8 @@ struct GLWindow
 class VCL_DLLPUBLIC OpenGLContext
 {
     friend class OpenGLTests;
-public:
     OpenGLContext();
+public:
     static rtl::Reference<OpenGLContext> Create();
     ~OpenGLContext();
     void acquire() { mnRefCount++; }
diff --git 
a/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionerImpl.cxx 
b/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionerImpl.cxx
index 78f8975..5ac45f3 100644
--- a/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionerImpl.cxx
+++ b/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionerImpl.cxx
@@ -228,7 +228,7 @@ private:
     void impl_finishTransition();
 
 private:
-    boost::shared_ptr<OpenGLContext> mpContext;
+    rtl::Reference<OpenGLContext> mpContext;
 
     /** OpenGL handle to the leaving slide's texture
     */
@@ -368,7 +368,7 @@ bool OGLTransitionerImpl::initWindowFromSlideShowView( 
const Reference< presenta
     sal_Int64 aVal = 0;
     aDeviceParams[1] >>= aVal;
 
-    mpContext = boost::make_shared<OpenGLContext>();
+    mpContext = OpenGLContext::Create();
     mpContext->requestLegacyContext();
 
     if( !mpContext->init( reinterpret_cast< vcl::Window* >( aVal ) ) )
@@ -1323,7 +1323,7 @@ void OGLTransitionerImpl::impl_dispose()
 {
     impl_finishTransition();
     disposeTextures();
-    mpContext.reset();
+    mpContext.clear();
 }
 
 // we are about to be disposed (someone call dispose() on us)
@@ -1350,7 +1350,7 @@ void OGLTransitionerImpl::disposing()
 #endif
 
 #if defined( UNX ) && !defined( MACOSX )
-    if( mbRestoreSync && bool(mpContext) ) {
+    if( mbRestoreSync && bool(mpContext.is()) ) {
         // try to reestablish synchronize state
         char* sal_synchronize = getenv("SAL_SYNCHRONIZE");
         XSynchronize( mpContext->getOpenGLWindow().dpy, sal_synchronize && 
*sal_synchronize == '1' );
diff --git a/vcl/source/opengl/OpenGLContext.cxx 
b/vcl/source/opengl/OpenGLContext.cxx
index d070e3f..432241f 100644
--- a/vcl/source/opengl/OpenGLContext.cxx
+++ b/vcl/source/opengl/OpenGLContext.cxx
@@ -1362,11 +1362,14 @@ void OpenGLContext::makeCurrent()
     TempErrorHandler aErrorHandler(m_aGLWin.dpy, unxErrorHandler);
 #endif
 
-    GLXDrawable nDrawable = mbPixmap ? m_aGLWin.glPix : m_aGLWin.win;
-    if (!glXMakeCurrent( m_aGLWin.dpy, nDrawable, m_aGLWin.ctx ))
+    if (m_aGLWin.dpy)
     {
-        SAL_WARN("vcl.opengl", "OpenGLContext::makeCurrent failed on drawable 
" << nDrawable << " pixmap? " << mbPixmap);
-        return;
+        GLXDrawable nDrawable = mbPixmap ? m_aGLWin.glPix : m_aGLWin.win;
+        if (!glXMakeCurrent( m_aGLWin.dpy, nDrawable, m_aGLWin.ctx ))
+        {
+            SAL_WARN("vcl.opengl", "OpenGLContext::makeCurrent failed on 
drawable " << nDrawable << " pixmap? " << mbPixmap);
+            return;
+        }
     }
 #endif
 
diff --git a/vcl/source/window/openglwin.cxx b/vcl/source/window/openglwin.cxx
index 46628c6..e079375 100644
--- a/vcl/source/window/openglwin.cxx
+++ b/vcl/source/window/openglwin.cxx
@@ -17,18 +17,19 @@ class OpenGLWindowImpl
 public:
     explicit OpenGLWindowImpl(vcl::Window* pWindow);
     ~OpenGLWindowImpl() { mxChildWindow.disposeAndClear(); }
-    OpenGLContext& getContext() { return maContext;}
+    OpenGLContext& getContext() { return *mxContext.get(); }
 private:
-    OpenGLContext maContext;
+    rtl::Reference<OpenGLContext> mxContext;
     VclPtr<SystemChildWindow> mxChildWindow;
 };
 
 OpenGLWindowImpl::OpenGLWindowImpl(vcl::Window* pWindow)
+    : mxContext(OpenGLContext::Create())
 {
     SystemWindowData aData = OpenGLContext::generateWinData(pWindow, false);
     mxChildWindow.reset(VclPtr<SystemChildWindow>::Create(pWindow, 0, &aData));
     mxChildWindow->Show();
-    maContext.init(mxChildWindow.get());
+    mxContext->init(mxChildWindow.get());
     pWindow->SetMouseTransparent(false);
 }
 
commit 2456cf8306be22e32130e789ab939c059e5e79e5
Author: Michael Meeks <michael.me...@collabora.com>
Date:   Mon Sep 7 22:21:15 2015 +0100

    tdf#94006 - re-factor to use rtl::Reference for OpenGLContexts.
    
    Don't use rtl::Reference for the global / list state, so the
    ref-count reflects the number of real users.
    Hold a reference during ~OpenGLContext.
    
    Change-Id: I4e57a7246159acd58ae7d5a0dfc8704b9795c894

diff --git a/include/vcl/opengl/OpenGLContext.hxx 
b/include/vcl/opengl/OpenGLContext.hxx
index c588a21..b520d2c 100644
--- a/include/vcl/opengl/OpenGLContext.hxx
+++ b/include/vcl/opengl/OpenGLContext.hxx
@@ -55,6 +55,7 @@ class NSOpenGLView;
 #include <vcl/window.hxx>
 #include <tools/gen.hxx>
 #include <vcl/syschild.hxx>
+#include <rtl/ref.hxx>
 
 #include <map>
 #include <set>
@@ -170,7 +171,10 @@ class VCL_DLLPUBLIC OpenGLContext
     friend class OpenGLTests;
 public:
     OpenGLContext();
+    static rtl::Reference<OpenGLContext> Create();
     ~OpenGLContext();
+    void acquire() { mnRefCount++; }
+    void release() { if ( --mnRefCount == 0 ) delete this; }
 
     void requestLegacyContext();
     void requestSingleBufferedRendering();
@@ -194,13 +198,7 @@ public:
     OpenGLFramebuffer* AcquireFramebuffer( const OpenGLTexture& rTexture );
     static void        ReleaseFramebuffer( OpenGLFramebuffer* pFramebuffer );
     void UnbindTextureFromFramebuffers( GLuint nTexture );
-#ifdef DBG_UTIL
-    void AddRef(SalGraphicsImpl*);
-    void DeRef(SalGraphicsImpl*);
-#else
-    void AddRef();
-    void DeRef();
-#endif
+
     void               ReleaseFramebuffer( const OpenGLTexture& rTexture );
     void               ReleaseFramebuffers();
 
@@ -288,8 +286,9 @@ public:
     vcl::Region maClipRegion;
     int mnPainting;
 
-    OpenGLContext* mpPrevContext;
-    OpenGLContext* mpNextContext;
+    // Don't hold references to ourselves:
+    OpenGLContext *mpPrevContext;
+    OpenGLContext *mpNextContext;
 };
 
 #endif
diff --git a/vcl/inc/opengl/salbmp.hxx b/vcl/inc/opengl/salbmp.hxx
index f189ea8..1c425da 100644
--- a/vcl/inc/opengl/salbmp.hxx
+++ b/vcl/inc/opengl/salbmp.hxx
@@ -39,7 +39,7 @@ class   BitmapPalette;
 class VCL_PLUGIN_PUBLIC OpenGLSalBitmap : public SalBitmap
 {
 private:
-    OpenGLContext*                      mpContext;
+    rtl::Reference<OpenGLContext>       mpContext;
     OpenGLTexture                       maTexture;
     bool                                mbDirtyTexture;
     BitmapPalette                       maPalette;
@@ -89,7 +89,7 @@ public:
 
     bool            Create( const OpenGLTexture& rTex, long nX, long nY, long 
nWidth, long nHeight );
     OpenGLTexture&  GetTexture() const;
-    static OpenGLContext* GetBitmapContext();
+    static rtl::Reference<OpenGLContext> GetBitmapContext();
 
 private:
 
diff --git a/vcl/inc/opengl/win/gdiimpl.hxx b/vcl/inc/opengl/win/gdiimpl.hxx
index e1fa195..04bb151 100644
--- a/vcl/inc/opengl/win/gdiimpl.hxx
+++ b/vcl/inc/opengl/win/gdiimpl.hxx
@@ -14,8 +14,7 @@
 
 #include "openglgdiimpl.hxx"
 #include "win/salgdi.h"
-
-class OpenGLContext;
+#include <vcl/opengl/OpenGLContext.hxx>
 
 class WinOpenGLSalGraphicsImpl : public OpenGLSalGraphicsImpl
 {
@@ -31,8 +30,8 @@ public:
                              SalGeometryProvider *mpProvider);
 
 protected:
-    virtual OpenGLContext* CreateWinContext() SAL_OVERRIDE;
-    virtual bool UseContext( OpenGLContext* pContext ) SAL_OVERRIDE;
+    virtual rtl::Reference<OpenGLContext> CreateWinContext() SAL_OVERRIDE;
+    virtual bool UseContext( const rtl::Reference<OpenGLContext> &pContext ) 
SAL_OVERRIDE;
 
 public:
     virtual void copyBits( const SalTwoRect& rPosAry, SalGraphics* 
pSrcGraphics ) SAL_OVERRIDE;
diff --git a/vcl/inc/opengl/x11/gdiimpl.hxx b/vcl/inc/opengl/x11/gdiimpl.hxx
index a87a9de..19804a8 100644
--- a/vcl/inc/opengl/x11/gdiimpl.hxx
+++ b/vcl/inc/opengl/x11/gdiimpl.hxx
@@ -28,8 +28,8 @@ public:
     virtual ~X11OpenGLSalGraphicsImpl();
 
 protected:
-    virtual OpenGLContext* CreateWinContext() SAL_OVERRIDE;
-    virtual bool UseContext( OpenGLContext* pContext ) SAL_OVERRIDE;
+    virtual rtl::Reference<OpenGLContext> CreateWinContext() SAL_OVERRIDE;
+    virtual bool UseContext( const rtl::Reference<OpenGLContext> &pContext ) 
SAL_OVERRIDE;
 
     bool RenderPixmap(X11Pixmap* pPixmap, X11Pixmap* pMask, int nX, int nY, 
TextureCombo& rCombo);
 
diff --git a/vcl/inc/openglgdiimpl.hxx b/vcl/inc/openglgdiimpl.hxx
index 62a51e1..71a9e80 100644
--- a/vcl/inc/openglgdiimpl.hxx
+++ b/vcl/inc/openglgdiimpl.hxx
@@ -52,7 +52,7 @@ class VCL_DLLPUBLIC OpenGLSalGraphicsImpl : public 
SalGraphicsImpl
     friend class OpenGLTests;
 protected:
 
-    OpenGLContext* mpContext;
+    rtl::Reference<OpenGLContext> mpContext;
     SalGraphics& mrParent;
     /// Pointer to the SalFrame or SalVirtualDevice
     SalGeometryProvider* mpProvider;
@@ -137,19 +137,19 @@ protected:
     bool ReleaseContext();
 
     // retrieve the default context for offscreen rendering
-    static OpenGLContext* GetDefaultContext();
+    static rtl::Reference<OpenGLContext> GetDefaultContext();
 
     // create a new context for window rendering
-    virtual OpenGLContext* CreateWinContext() = 0;
+    virtual rtl::Reference<OpenGLContext> CreateWinContext() = 0;
 
     // check whether the given context can be used by this instance
-    virtual bool UseContext( OpenGLContext* pContext ) = 0;
+    virtual bool UseContext( const rtl::Reference<OpenGLContext> &pContext ) = 
0;
 
 public:
     OpenGLSalGraphicsImpl(SalGraphics& pParent, SalGeometryProvider 
*pProvider);
     virtual ~OpenGLSalGraphicsImpl ();
 
-    OpenGLContext* GetOpenGLContext();
+    rtl::Reference<OpenGLContext> GetOpenGLContext();
 
     virtual void Init() SAL_OVERRIDE;
 
diff --git a/vcl/inc/salgdi.hxx b/vcl/inc/salgdi.hxx
index 1686427..0334049 100644
--- a/vcl/inc/salgdi.hxx
+++ b/vcl/inc/salgdi.hxx
@@ -93,7 +93,7 @@ public:
     virtual SalGraphicsImpl*    GetImpl() const = 0;
 
     /// Check that our mpImpl is OpenGL and return the context, otherwise NULL.
-    OpenGLContext*              GetOpenGLContext() const;
+    rtl::Reference<OpenGLContext> GetOpenGLContext() const;
 
     void                        setAntiAliasB2DDraw(bool bNew) { 
m_bAntiAliasB2DDraw = bNew; }
     bool                        getAntiAliasB2DDraw() const { return 
m_bAntiAliasB2DDraw; }
diff --git a/vcl/opengl/gdiimpl.cxx b/vcl/opengl/gdiimpl.cxx
index 9a2d72e..5487347 100644
--- a/vcl/opengl/gdiimpl.cxx
+++ b/vcl/opengl/gdiimpl.cxx
@@ -61,14 +61,14 @@ OpenGLSalGraphicsImpl::~OpenGLSalGraphicsImpl()
     ReleaseContext();
 }
 
-OpenGLContext* OpenGLSalGraphicsImpl::GetOpenGLContext()
+rtl::Reference<OpenGLContext> OpenGLSalGraphicsImpl::GetOpenGLContext()
 {
     if( !AcquireContext() )
         return NULL;
     return mpContext;
 }
 
-OpenGLContext* OpenGLSalGraphicsImpl::GetDefaultContext()
+rtl::Reference<OpenGLContext> OpenGLSalGraphicsImpl::GetDefaultContext()
 {
     return ImplGetDefaultWindow()->GetGraphics()->GetOpenGLContext();
 }
@@ -77,19 +77,14 @@ bool OpenGLSalGraphicsImpl::AcquireContext( )
 {
     ImplSVData* pSVData = ImplGetSVData();
 
-    if( mpContext )
+    if( mpContext.is() )
     {
         if( mpContext->isInitialized() )
             return true;
-#ifdef DBG_UTIL
-        mpContext->DeRef(this);
-#else
-        mpContext->DeRef();
-#endif
+        mpContext.clear();
     }
 
-
-    OpenGLContext* pContext = pSVData->maGDIData.mpLastContext;
+    OpenGLContext *pContext = pSVData->maGDIData.mpLastContext;
     while( pContext )
     {
         // check if this context can be used by this SalGraphicsImpl instance
@@ -99,31 +94,17 @@ bool OpenGLSalGraphicsImpl::AcquireContext( )
     }
 
     if( pContext )
-    {
-#ifdef DBG_UTIL
-        pContext->AddRef(this);
-#else
-        pContext->AddRef();
-#endif
-    }
+        mpContext = pContext;
     else
-        pContext = mbOffscreen ? GetDefaultContext() : CreateWinContext();
+        mpContext = mbOffscreen ? GetDefaultContext() : CreateWinContext();
 
-    mpContext = pContext;
-    return (mpContext != NULL);
+    return mpContext.is();
 }
 
 bool OpenGLSalGraphicsImpl::ReleaseContext()
 {
-    if( mpContext )
-    {
-#ifdef DBG_UTIL
-        mpContext->DeRef(this);
-#else
-        mpContext->DeRef();
-#endif
-    }
-    mpContext = NULL;
+    mpContext.clear();
+
     return true;
 }
 
@@ -132,7 +113,7 @@ void OpenGLSalGraphicsImpl::Init()
     mbOffscreen = IsOffscreen();
 
     // check if we can simply re-use the same context
-    if( mpContext )
+    if( mpContext.is() )
     {
         if( !UseContext( mpContext ) )
             ReleaseContext();
@@ -143,7 +124,7 @@ void OpenGLSalGraphicsImpl::Init()
         maOffscreenTex.GetWidth()  != GetWidth() ||
         maOffscreenTex.GetHeight() != GetHeight() )
     {
-        if( mpContext ) // valid context
+        if( mpContext.is() ) // valid context
             mpContext->ReleaseFramebuffer( maOffscreenTex );
         maOffscreenTex = OpenGLTexture();
     }
@@ -158,7 +139,7 @@ void OpenGLSalGraphicsImpl::DeInit()
     // let it know. Other eg. VirtualDevice contexts which have
     // references on and rely on this context continuing to work will
     // get a shiny new context in AcquireContext:: next PreDraw.
-    if( mpContext && !IsOffscreen() )
+    if( mpContext.is() && !IsOffscreen() )
         mpContext->reset();
 }
 
@@ -216,7 +197,7 @@ void OpenGLSalGraphicsImpl::ApplyProgramMatrices(float 
fPixelOffset)
 void OpenGLSalGraphicsImpl::freeResources()
 {
     // TODO Delete shaders, programs and textures if not shared
-    if( mbOffscreen && mpContext && mpContext->isInitialized() )
+    if( mbOffscreen && mpContext.is() && mpContext->isInitialized() )
     {
         mpContext->makeCurrent();
         mpContext->ReleaseFramebuffer( maOffscreenTex );
diff --git a/vcl/opengl/salbmp.cxx b/vcl/opengl/salbmp.cxx
index 9084ae1..fefa381 100644
--- a/vcl/opengl/salbmp.cxx
+++ b/vcl/opengl/salbmp.cxx
@@ -595,7 +595,7 @@ void OpenGLSalBitmap::updateChecksum() const
 
     OpenGLSalBitmap* pThis = const_cast<OpenGLSalBitmap*>(this);
 
-    if (!mpContext)
+    if (!mpContext.is())
     {
         pThis->CreateTexture();
     }
@@ -614,7 +614,7 @@ void OpenGLSalBitmap::updateChecksum() const
     }
 }
 
-OpenGLContext* OpenGLSalBitmap::GetBitmapContext()
+rtl::Reference<OpenGLContext> OpenGLSalBitmap::GetBitmapContext()
 {
     return ImplGetDefaultWindow()->GetGraphics()->GetOpenGLContext();
 }
@@ -624,12 +624,14 @@ void OpenGLSalBitmap::makeCurrent()
     ImplSVData* pSVData = ImplGetSVData();
 
     // TODO: make sure we can really use the last used context
-    mpContext = pSVData->maGDIData.mpLastContext;
-    while( mpContext && !mpContext->isInitialized() )
-        mpContext = mpContext->mpPrevContext;
-    if( !mpContext )
+    OpenGLContext *pContext = pSVData->maGDIData.mpLastContext;
+    while( pContext && !pContext->isInitialized() )
+        pContext = pContext->mpPrevContext;
+    if( pContext )
+        mpContext = pContext;
+    else
         mpContext = GetBitmapContext();
-    assert(mpContext && "Couldn't get an OpenGL context");
+    assert(mpContext.is() && "Couldn't get an OpenGL context");
     mpContext->makeCurrent();
 }
 
diff --git a/vcl/opengl/texture.cxx b/vcl/opengl/texture.cxx
index 57bd7a6..9b3c605 100644
--- a/vcl/opengl/texture.cxx
+++ b/vcl/opengl/texture.cxx
@@ -113,8 +113,8 @@ ImplOpenGLTexture::~ImplOpenGLTexture()
 
         // Check we have been correctly un-bound from all framebuffers.
         ImplSVData* pSVData = ImplGetSVData();
-        OpenGLContext* pContext = pSVData->maGDIData.mpLastContext;
-        if (pContext)
+        rtl::Reference<OpenGLContext> pContext = 
pSVData->maGDIData.mpLastContext;
+        if (pContext.is())
             pContext->UnbindTextureFromFramebuffers( mnTexture );
 
         glDeleteTextures( 1, &mnTexture );
@@ -375,7 +375,7 @@ void OpenGLTexture::Read( GLenum nFormat, GLenum nType, 
sal_uInt8* pData )
     {
         // Retrieve current context
         ImplSVData* pSVData = ImplGetSVData();
-        OpenGLContext* pContext = pSVData->maGDIData.mpLastContext;
+        rtl::Reference<OpenGLContext> pContext = 
pSVData->maGDIData.mpLastContext;
         OpenGLFramebuffer* pFramebuffer;
 
         pFramebuffer = pContext->AcquireFramebuffer( *this );
diff --git a/vcl/opengl/win/gdiimpl.cxx b/vcl/opengl/win/gdiimpl.cxx
index 6896a02..30088a9 100644
--- a/vcl/opengl/win/gdiimpl.cxx
+++ b/vcl/opengl/win/gdiimpl.cxx
@@ -29,21 +29,21 @@ void WinOpenGLSalGraphicsImpl::copyBits( const SalTwoRect& 
rPosAry, SalGraphics*
     OpenGLSalGraphicsImpl::DoCopyBits( rPosAry, *pImpl );
 }
 
-OpenGLContext* WinOpenGLSalGraphicsImpl::CreateWinContext()
+rtl::Reference<OpenGLContext> WinOpenGLSalGraphicsImpl::CreateWinContext()
 {
-    OpenGLContext* pContext = new OpenGLContext();
+    rtl::Reference<OpenGLContext> pContext = OpenGLContext::Create();
     pContext->requestSingleBufferedRendering();
     pContext->init( mrParent.mhLocalDC, mrParent.mhWnd );
     return pContext;
 }
 
-bool WinOpenGLSalGraphicsImpl::UseContext( OpenGLContext* pContext )
+bool WinOpenGLSalGraphicsImpl::UseContext( const rtl::Reference<OpenGLContext> 
&pContext )
 {
-    if( !pContext || !pContext->isInitialized() )
+    if( !pContext.is() || !pContext->isInitialized() )
         return false;
     if( IsOffscreen() )
         return true;
-    return ( pContext->getOpenGLWindow().hWnd == mrParent.mhWnd );
+    return pContext->getOpenGLWindow().hWnd == mrParent.mhWnd;
 }
 
 namespace
diff --git a/vcl/opengl/x11/gdiimpl.cxx b/vcl/opengl/x11/gdiimpl.cxx
index 4c59284..65b279b 100644
--- a/vcl/opengl/x11/gdiimpl.cxx
+++ b/vcl/opengl/x11/gdiimpl.cxx
@@ -44,7 +44,7 @@ void X11OpenGLSalGraphicsImpl::Init()
     OpenGLSalGraphicsImpl::Init();
 }
 
-OpenGLContext* X11OpenGLSalGraphicsImpl::CreateWinContext()
+rtl::Reference<OpenGLContext> X11OpenGLSalGraphicsImpl::CreateWinContext()
 {
     X11WindowProvider *pProvider = 
dynamic_cast<X11WindowProvider*>(mrParent.m_pFrame);
 
@@ -52,13 +52,13 @@ OpenGLContext* X11OpenGLSalGraphicsImpl::CreateWinContext()
         return NULL;
 
     Window aWin = pProvider->GetX11Window();
-    OpenGLContext* pContext = new OpenGLContext();
+    rtl::Reference<OpenGLContext> pContext = OpenGLContext::Create();
     pContext->init( mrParent.GetXDisplay(), aWin,
                     mrParent.m_nXScreen.getXScreen() );
     return pContext;
 }
 
-bool X11OpenGLSalGraphicsImpl::UseContext( OpenGLContext* pContext )
+bool X11OpenGLSalGraphicsImpl::UseContext( const rtl::Reference<OpenGLContext> 
&pContext )
 {
     X11WindowProvider *pProvider = 
dynamic_cast<X11WindowProvider*>(mrParent.m_pFrame);
 
@@ -66,9 +66,9 @@ bool X11OpenGLSalGraphicsImpl::UseContext( OpenGLContext* 
pContext )
         return false;
 
     if( !pProvider )
-        return ( pContext->getOpenGLWindow().win != None );
+        return pContext->getOpenGLWindow().win != None;
     else
-        return ( pContext->getOpenGLWindow().win == pProvider->GetX11Window() 
);
+        return pContext->getOpenGLWindow().win == pProvider->GetX11Window();
 }
 
 void X11OpenGLSalGraphicsImpl::copyBits( const SalTwoRect& rPosAry, 
SalGraphics* pSrcGraphics )
diff --git a/vcl/source/app/svdata.cxx b/vcl/source/app/svdata.cxx
index bfc2bb4..ca815a2 100644
--- a/vcl/source/app/svdata.cxx
+++ b/vcl/source/app/svdata.cxx
@@ -140,15 +140,9 @@ vcl::Window* ImplGetDefaultWindow()
             pSVData->mpDefaultWin->SetText( OUString( "VCL 
ImplGetDefaultWindow"  ) );
 
             // Add a reference to the default context so it never gets deleted
-            OpenGLContext* pContext = 
pSVData->mpDefaultWin->GetGraphics()->GetOpenGLContext();
-            if( pContext )
-            {
-#ifdef DBG_UTIL
-                pContext->AddRef(NULL);
-#else
-                pContext->AddRef();
-#endif
-            }
+            rtl::Reference<OpenGLContext> pContext = 
pSVData->mpDefaultWin->GetGraphics()->GetOpenGLContext();
+            if( pContext.is() )
+                pContext->acquire();
         }
         Application::GetSolarMutex().release();
     }
diff --git a/vcl/source/gdi/salgdilayout.cxx b/vcl/source/gdi/salgdilayout.cxx
index 46f76ff..945f7ae 100644
--- a/vcl/source/gdi/salgdilayout.cxx
+++ b/vcl/source/gdi/salgdilayout.cxx
@@ -76,7 +76,7 @@ SalGraphics::~SalGraphics()
 {
 }
 
-OpenGLContext* SalGraphics::GetOpenGLContext() const
+rtl::Reference<OpenGLContext> SalGraphics::GetOpenGLContext() const
 {
     OpenGLSalGraphicsImpl *pImpl = 
dynamic_cast<OpenGLSalGraphicsImpl*>(GetImpl());
     if (pImpl)
diff --git a/vcl/source/opengl/OpenGLContext.cxx 
b/vcl/source/opengl/OpenGLContext.cxx
index df8c6a6..d070e3f 100644
--- a/vcl/source/opengl/OpenGLContext.cxx
+++ b/vcl/source/opengl/OpenGLContext.cxx
@@ -58,7 +58,7 @@ OpenGLContext::OpenGLContext():
     mpWindow(NULL),
     m_pChildWindow(NULL),
     mbInitialized(false),
-    mnRefCount(1),
+    mnRefCount(0),
     mbRequestLegacyContext(false),
     mbUseDoubleBufferedRendering(true),
     mnFramebufferCount(0),
@@ -93,6 +93,9 @@ OpenGLContext::OpenGLContext():
 OpenGLContext::~OpenGLContext()
 {
     VCL_GL_INFO("vcl.opengl", "delete context: " << this);
+    assert (mnRefCount == 0);
+
+    mnRefCount = 1; // guard the shutdown paths.
     reset();
 
     ImplSVData* pSVData = ImplGetSVData();
@@ -106,42 +109,13 @@ OpenGLContext::~OpenGLContext()
         pSVData->maGDIData.mpLastContext = mpPrevContext;
 
     m_pChildWindow.disposeAndClear();
+    assert (mnRefCount == 1);
 }
 
-#ifdef DBG_UTIL
-void OpenGLContext::AddRef(SalGraphicsImpl* pImpl)
-{
-    assert(mnRefCount > 0);
-    mnRefCount++;
-
-    maParents.insert(pImpl);
-}
-
-void OpenGLContext::DeRef(SalGraphicsImpl* pImpl)
+rtl::Reference<OpenGLContext> OpenGLContext::Create()
 {
-
-    auto it = maParents.find(pImpl);
-    if(it != maParents.end())
-        maParents.erase(it);
-
-    assert(mnRefCount > 0);
-    if( --mnRefCount == 0 )
-        delete this;
+    return rtl::Reference<OpenGLContext>(new OpenGLContext);
 }
-#else
-void OpenGLContext::AddRef()
-{
-    assert(mnRefCount > 0);
-    mnRefCount++;
-}
-
-void OpenGLContext::DeRef()
-{
-    assert(mnRefCount > 0);
-    if( --mnRefCount == 0 )
-        delete this;
-}
-#endif
 
 void OpenGLContext::requestLegacyContext()
 {
@@ -1337,8 +1311,8 @@ void OpenGLContext::clearCurrent()
 
     // release all framebuffers from the old context so we can re-attach the
     // texture in the new context
-    OpenGLContext* pCurrentCtx = pSVData->maGDIData.mpLastContext;
-    if( pCurrentCtx && pCurrentCtx->isCurrent() )
+    rtl::Reference<OpenGLContext> pCurrentCtx = 
pSVData->maGDIData.mpLastContext;
+    if( pCurrentCtx.is() && pCurrentCtx->isCurrent() )
         pCurrentCtx->ReleaseFramebuffers();
 }
 
@@ -1348,9 +1322,9 @@ void OpenGLContext::prepareForYield()
 
     // release all framebuffers from the old context so we can re-attach the
     // texture in the new context
-    OpenGLContext* pCurrentCtx = pSVData->maGDIData.mpLastContext;
+    rtl::Reference<OpenGLContext> pCurrentCtx = 
pSVData->maGDIData.mpLastContext;
 
-    if ( !pCurrentCtx )
+    if ( !pCurrentCtx.is() )
         return;                 // Not using OpenGL
 
     SAL_INFO("vcl.opengl", "Unbinding contexts in preparation for yield");
diff --git a/vcl/unx/generic/window/salframe.cxx 
b/vcl/unx/generic/window/salframe.cxx
index 2693580..965d3b8 100644
--- a/vcl/unx/generic/window/salframe.cxx
+++ b/vcl/unx/generic/window/salframe.cxx
@@ -893,8 +893,8 @@ X11SalFrame::~X11SalFrame()
     }
 
     // reset all OpenGL contexts using this window
-    OpenGLContext* pContext = ImplGetSVData()->maGDIData.mpLastContext;
-    while( pContext )
+    rtl::Reference<OpenGLContext> pContext = 
ImplGetSVData()->maGDIData.mpLastContext;
+    while( pContext.is() )
     {
         if( pContext->getOpenGLWindow().win == mhWindow )
             pContext->reset();
diff --git a/vcl/workben/vcldemo.cxx b/vcl/workben/vcldemo.cxx
index b6c96e2..b3c6c86 100644
--- a/vcl/workben/vcldemo.cxx
+++ b/vcl/workben/vcldemo.cxx
@@ -1657,8 +1657,8 @@ class OpenGLTests
     VclPtr<WorkWindow> mxWinB;
     OpenGLSalGraphicsImpl *mpImplA;
     OpenGLSalGraphicsImpl *mpImplB;
-    OpenGLContext *mpA;
-    OpenGLContext *mpB;
+    rtl::Reference<OpenGLContext> mpA;
+    rtl::Reference<OpenGLContext> mpB;
 
     static OpenGLSalGraphicsImpl *getImpl(const VclPtr<OutputDevice> &xOut)
     {
@@ -1682,7 +1682,7 @@ public:
         mpA = mpImplA->GetOpenGLContext();
         mpB = mpImplB->GetOpenGLContext();
 
-        assert (mpA && mpB);
+        assert (mpA.is() && mpB.is());
         assert (mpA != mpB);
     }
     ~OpenGLTests()
@@ -1715,9 +1715,9 @@ public:
 
         // get some other guys to leach off this context
         VclPtrInstance<VirtualDevice> xVDev;
-        OpenGLContext *pContext = getImpl(xVDev)->GetOpenGLContext();
+        rtl::Reference<OpenGLContext> pContext = 
getImpl(xVDev)->GetOpenGLContext();
         VclPtrInstance<VirtualDevice> xVDev2;
-        OpenGLContext *pContext2 = getImpl(xVDev)->GetOpenGLContext();
+        rtl::Reference<OpenGLContext> pContext2 = 
getImpl(xVDev)->GetOpenGLContext();
 
         // sharing the same off-screen context.
         assert(pContext == pContext2);
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to