Files
ResInsight/Fwk/AppFwk/cafViewer/cafOpenGLWidget.cpp

184 lines
7.7 KiB
C++
Raw Normal View History

//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2011-2013 Ceetron AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// 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 GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
2020-06-19 07:53:59 +02:00
#include "cafOpenGLWidget.h"
#include "cvfBase.h"
#include "cvfOpenGLContextGroup.h"
2024-01-23 08:48:05 +01:00
#include "cvfqtCvfBoundQGLContext_deprecated.h"
2020-06-19 07:53:59 +02:00
namespace caf
{
//==================================================================================================
///
/// \class cvfqt::OpenGLWidget
/// \ingroup GuiQt
///
2020-06-19 07:53:59 +02:00
///
///
//==================================================================================================
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
OpenGLWidget::OpenGLWidget( cvf::OpenGLContextGroup* contextGroup,
const QGLFormat& format,
QWidget* parent,
OpenGLWidget* shareWidget,
Qt::WindowFlags f )
2024-01-23 08:48:05 +01:00
: QGLWidget( new cvfqt::CvfBoundQGLContext_deprecated( contextGroup, format ), parent, shareWidget, f )
{
2020-06-19 07:53:59 +02:00
if ( isValid() )
{
cvf::ref<cvf::OpenGLContext> myContext = cvfOpenGLContext();
2020-06-19 07:53:59 +02:00
if ( myContext.notNull() )
{
2024-01-23 08:48:05 +01:00
cvf::OpenGLContextGroup* myOwnerContextGroup = myContext->group();
2020-06-19 07:53:59 +02:00
if ( shareWidget )
{
// We need to check if we actually got a context that shares resources with shareWidget.
cvf::ref<cvf::OpenGLContext> shareContext = shareWidget->cvfOpenGLContext();
2024-01-23 08:48:05 +01:00
CVF_ASSERT( myOwnerContextGroup == shareContext->group() );
2020-06-19 07:53:59 +02:00
if ( isSharing() )
{
2024-01-23 08:48:05 +01:00
makeCurrent();
myOwnerContextGroup->initializeContextGroup( myContext.p() );
}
else
{
2020-06-19 07:53:59 +02:00
// If we didn't, we need to remove the newly created context from the group it has been added to
// since the construction process above has already optimistically added the new context to the
// existing group. In this case, the newly context is basically defunct so we just shut it down
// (which will also remove it from the group)
2024-01-23 08:48:05 +01:00
myOwnerContextGroup->contextAboutToBeShutdown( myContext.p() );
2020-06-19 07:53:59 +02:00
CVF_ASSERT( myContext->group() == nullptr );
}
}
else
{
2024-01-23 08:48:05 +01:00
makeCurrent();
contextGroup->initializeContextGroup( myContext.p() );
}
}
}
}
//--------------------------------------------------------------------------------------------------
/// Constructor
///
/// Tries to create a widget that shares OpenGL resources with \a shareWidget
/// To check if creation was actually successful, you must call isValidContext() on the context
/// of the newly created widget. For example:
/// \code
/// myNewWidget->cvfOpenGLContext()->isValidContext();
/// \endcode
///
/// If the context is not valid, sharing failed and the newly created widget/context be discarded.
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
OpenGLWidget::OpenGLWidget( OpenGLWidget* shareWidget, QWidget* parent, Qt::WindowFlags f )
2024-01-23 08:48:05 +01:00
: QGLWidget( new cvfqt::CvfBoundQGLContext_deprecated( shareWidget->cvfOpenGLContext()->group(), shareWidget->format() ),
2020-06-19 07:53:59 +02:00
parent,
shareWidget,
f )
{
2020-06-19 07:53:59 +02:00
CVF_ASSERT( shareWidget );
cvf::ref<cvf::OpenGLContext> shareContext = shareWidget->cvfOpenGLContext();
2020-06-19 07:53:59 +02:00
CVF_ASSERT( shareContext.notNull() );
cvf::ref<cvf::OpenGLContext> myContext = cvfOpenGLContext();
2020-06-19 07:53:59 +02:00
if ( myContext.notNull() )
{
2024-01-23 08:48:05 +01:00
cvf::OpenGLContextGroup* myOwnerContextGroup = myContext->group();
// We need to check if we actually got a context that shares resources with shareWidget.
2020-06-19 07:53:59 +02:00
if ( isSharing() )
{
2020-06-19 07:53:59 +02:00
if ( isValid() )
{
2020-06-19 07:53:59 +02:00
CVF_ASSERT( myContext->group() == shareContext->group() );
2024-01-23 08:48:05 +01:00
makeCurrent();
myOwnerContextGroup->initializeContextGroup( myContext.p() );
}
}
else
{
// If we didn't, we need to remove the newly created context from the group it has been added to since
// the construction process above has already optimistically added the new context to the existing group.
2020-06-19 07:53:59 +02:00
// In this case, the newly context is basically defunct so we just shut it down (which will also remove it
// from the group)
2024-01-23 08:48:05 +01:00
myOwnerContextGroup->contextAboutToBeShutdown( myContext.p() );
2020-06-19 07:53:59 +02:00
CVF_ASSERT( myContext->group() == nullptr );
}
}
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
cvf::OpenGLContext* OpenGLWidget::cvfOpenGLContext() const
{
2024-01-23 08:48:05 +01:00
const QGLContext* qglContext = context();
const cvfqt::CvfBoundQGLContext_deprecated* contextBinding =
dynamic_cast<const cvfqt::CvfBoundQGLContext_deprecated*>( qglContext );
2020-06-19 07:53:59 +02:00
CVF_ASSERT( contextBinding );
return contextBinding->cvfOpenGLContext();
}
//--------------------------------------------------------------------------------------------------
2020-06-19 07:53:59 +02:00
///
//--------------------------------------------------------------------------------------------------
void OpenGLWidget::cvfShutdownOpenGLContext()
{
2020-06-19 07:53:59 +02:00
// It should be safe to call shutdown multiple times so this call should
// amount to a no-op if the user has already shut down the context
cvf::ref<cvf::OpenGLContext> myContext = cvfOpenGLContext();
2020-06-19 07:53:59 +02:00
if ( myContext.notNull() )
{
2024-01-23 08:48:05 +01:00
cvf::OpenGLContextGroup* myOwnerContextGroup = myContext->group();
// If shutdown has already been called, the context is no longer member of any group
if ( myOwnerContextGroup )
{
makeCurrent();
myOwnerContextGroup->contextAboutToBeShutdown( myContext.p() );
}
}
}
2020-06-19 07:53:59 +02:00
} // namespace caf