Commit f6ff1ecd authored by Stefano Bonicatti's avatar Stefano Bonicatti

Fix crash when switching off OpenGL

The wrong context was active when shaders were deleted.
This also fix the crash when switching scaling mode or anything that
deletes a KisOpenGLCanvas2.

Changed the KisOpenGL API that deal with contexts a bit, now is possible
to choose between switching on the shared context or a widget one.

To avoid overhead switching on a context that is already current, i've
added a check to see if the context we are trying to switch to is already current
or not (the check doesn't use any gpu command).

REVIEW:121944
CCBUG:341496
parent 4e8f40a9
......@@ -251,7 +251,7 @@ void OcioDisplayFilter::updateProcessor()
* created before the openGL canvas. This might happen when
* switching from QPainter to openGL canvas for the first time.
*/
KisOpenGL::initialMakeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
}
const int lut3DEdgeSize = cfg.ocioLutEdgeSize();
......
......@@ -376,7 +376,8 @@ void KisCanvas2::createOpenGLCanvas()
m_d->openGLFilterMode = cfg.openGLFilteringMode();
m_d->currentCanvasIsOpenGL = true;
KisOpenGL::initialMakeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
m_d->openGLImageTextures = KisOpenGLImageTextures::getImageTextures(m_d->view->image(),
m_d->displayColorConverter->monitorProfile(),
m_d->displayColorConverter->renderingIntent(),
......
......@@ -149,16 +149,18 @@ void KisOpenGL::createContext()
}
}
void KisOpenGL::initialMakeContextCurrent()
void KisOpenGL::makeSharedContextCurrent()
{
sharedContextWidget()->makeCurrent();
if (sharedContextWidget()->context() != QGLContext::currentContext()) {
sharedContextWidget()->makeCurrent();
}
}
void KisOpenGL::makeContextCurrent()
void KisOpenGL::makeContextCurrent(QGLWidget *widget)
{
#ifdef Q_OS_WIN
sharedContextWidget()->makeCurrent();
#endif
if (widget->context() != QGLContext::currentContext()) {
widget->makeCurrent();
}
}
QGLWidget *KisOpenGL::sharedContextWidget()
......
......@@ -64,9 +64,15 @@ public:
* make the context current before creating textures, display lists,
* shader objects, etc, that are to be shared by multiple QGLWidgets.
*/
static void makeContextCurrent();
static void makeSharedContextCurrent();
/**
* Make the context of the widget current. You should
* make the context current before creating textures, display lists,
* shader objects, etc, that are to be shared by multiple QGLWidgets.
*/
static void makeContextCurrent(QGLWidget *widget);
static void initialMakeContextCurrent();
/**
* Print any error messages waiting to be read from glGetError(). Use
......
......@@ -186,6 +186,7 @@ KisOpenGLCanvas2::KisOpenGLCanvas2(KisCanvas2 *canvas, KisCoordinatesConverter *
KisOpenGLCanvas2::~KisOpenGLCanvas2()
{
KisOpenGL::makeContextCurrent(this);
delete d;
}
......
......@@ -80,7 +80,7 @@ KisOpenGLImageTextures::KisOpenGLImageTextures(KisImageWSP image,
{
Q_ASSERT(renderingIntent < 4);
KisOpenGL::makeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
getTextureSize(&m_texturesInfo);
......@@ -117,7 +117,7 @@ KisOpenGLImageTexturesSP KisOpenGLImageTextures::getImageTextures(KisImageWSP im
KoColorConversionTransformation::Intent renderingIntent,
KoColorConversionTransformation::ConversionFlags conversionFlags)
{
KisOpenGL::makeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
if (imageCanShareTextures()) {
ImageTexturesMap::iterator it = imageTexturesMap.find(image);
......@@ -152,7 +152,7 @@ void KisOpenGLImageTextures::createImageTextureTiles()
{
KisConfig cfg;
KisOpenGL::makeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
destroyImageTextureTiles();
updateTextureFormat();
......@@ -189,7 +189,8 @@ void KisOpenGLImageTextures::destroyImageTextureTiles()
{
if(m_textureTiles.isEmpty()) return;
KisOpenGL::makeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
foreach(KisTextureTile *tile, m_textureTiles) {
delete tile;
}
......@@ -270,7 +271,8 @@ void KisOpenGLImageTextures::recalculateCache(KisUpdateInfoSP info)
KisOpenGLUpdateInfoSP glInfo = dynamic_cast<KisOpenGLUpdateInfo*>(info.data());
if(!glInfo) return;
KisOpenGL::makeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
KIS_OPENGL_CLEAR_ERROR();
KisTextureTileUpdateInfoSP tileInfo;
......@@ -286,7 +288,7 @@ void KisOpenGLImageTextures::recalculateCache(KisUpdateInfoSP info)
void KisOpenGLImageTextures::generateCheckerTexture(const QImage &checkImage)
{
KisOpenGL::makeContextCurrent();
KisOpenGL::makeSharedContextCurrent();
glBindTexture(GL_TEXTURE_2D, m_checkerTexture);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment