diff --git a/src/backends/x11/standalone/glxcontext.cpp b/src/backends/x11/standalone/glxcontext.cpp index 0576598b59..376472d898 100644 --- a/src/backends/x11/standalone/glxcontext.cpp +++ b/src/backends/x11/standalone/glxcontext.cpp @@ -41,18 +41,23 @@ GlxContext::~GlxContext() glXDestroyContext(m_display, m_handle); } -bool GlxContext::makeCurrent() const +bool GlxContext::makeCurrent() { if (QOpenGLContext *context = QOpenGLContext::currentContext()) { // Workaround to tell Qt that no QOpenGLContext is current context->doneCurrent(); } - return glXMakeCurrent(m_display, m_window, m_handle); + const bool ret = glXMakeCurrent(m_display, m_window, m_handle); + if (ret) { + s_currentContext = this; + } + return ret; } -bool GlxContext::doneCurrent() const +void GlxContext::doneCurrent() const { - return glXMakeCurrent(m_display, None, nullptr); + glXMakeCurrent(m_display, None, nullptr); + s_currentContext = nullptr; } std::unique_ptr GlxContext::create(GlxBackend *backend, GLXFBConfig fbconfig, GLXWindow glxWindow) @@ -139,6 +144,7 @@ std::unique_ptr GlxContext::create(GlxBackend *backend, GLXFBConfig return nullptr; } auto ret = std::make_unique(backend->display(), glxWindow, handle); + s_currentContext = ret.get(); if (!ret->checkSupported()) { return nullptr; } diff --git a/src/backends/x11/standalone/glxcontext.h b/src/backends/x11/standalone/glxcontext.h index 93c2ecba16..82f12ef585 100644 --- a/src/backends/x11/standalone/glxcontext.h +++ b/src/backends/x11/standalone/glxcontext.h @@ -21,8 +21,8 @@ public: GlxContext(::Display *display, GLXWindow window, GLXContext handle); ~GlxContext() override; - bool makeCurrent() const; - bool doneCurrent() const; + bool makeCurrent(); + void doneCurrent() const; static std::unique_ptr create(GlxBackend *backend, GLXFBConfig fbconfig, GLXWindow glxWindow); diff --git a/src/opengl/eglcontext.cpp b/src/opengl/eglcontext.cpp index 022bddad63..bf57273f60 100644 --- a/src/opengl/eglcontext.cpp +++ b/src/opengl/eglcontext.cpp @@ -33,6 +33,7 @@ std::unique_ptr EglContext::create(EglDisplay *display, EGLConfig co return nullptr; } auto ret = std::make_unique(display, config, handle); + s_currentContext = ret.get(); if (!ret->checkSupported()) { return nullptr; } @@ -65,18 +66,23 @@ EglContext::~EglContext() eglDestroyContext(m_display->handle(), m_handle); } -bool EglContext::makeCurrent(EGLSurface surface) const +bool EglContext::makeCurrent(EGLSurface surface) { if (QOpenGLContext *context = QOpenGLContext::currentContext()) { // Workaround to tell Qt that no QOpenGLContext is current context->doneCurrent(); } - return eglMakeCurrent(m_display->handle(), surface, surface, m_handle) == EGL_TRUE; + const bool ret = eglMakeCurrent(m_display->handle(), surface, surface, m_handle) == EGL_TRUE; + if (ret) { + s_currentContext = this; + } + return ret; } void EglContext::doneCurrent() const { eglMakeCurrent(m_display->handle(), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + s_currentContext = nullptr; } EglDisplay *EglContext::displayObject() const diff --git a/src/opengl/eglcontext.h b/src/opengl/eglcontext.h index d39961f145..1a17b7158d 100644 --- a/src/opengl/eglcontext.h +++ b/src/opengl/eglcontext.h @@ -28,7 +28,7 @@ public: EglContext(EglDisplay *display, EGLConfig config, ::EGLContext context); ~EglContext() override; - bool makeCurrent(EGLSurface surface = EGL_NO_SURFACE) const; + bool makeCurrent(EGLSurface surface = EGL_NO_SURFACE); void doneCurrent() const; std::shared_ptr importDmaBufAsTexture(const DmaBufAttributes &attributes) const; diff --git a/src/opengl/glshadermanager.cpp b/src/opengl/glshadermanager.cpp index afc9b743c0..d04d9d46ec 100644 --- a/src/opengl/glshadermanager.cpp +++ b/src/opengl/glshadermanager.cpp @@ -20,19 +20,9 @@ namespace KWin { -std::unique_ptr ShaderManager::s_shaderManager; - ShaderManager *ShaderManager::instance() { - if (!s_shaderManager) { - s_shaderManager = std::make_unique(); - } - return s_shaderManager.get(); -} - -void ShaderManager::cleanup() -{ - s_shaderManager.reset(); + return OpenGlContext::currentContext()->shaderManager(); } ShaderManager::ShaderManager() diff --git a/src/opengl/glshadermanager.h b/src/opengl/glshadermanager.h index 6de55580cc..0caa5ee5ce 100644 --- a/src/opengl/glshadermanager.h +++ b/src/opengl/glshadermanager.h @@ -143,11 +143,6 @@ public: */ static ShaderManager *instance(); - /** - * @internal - */ - static void cleanup(); - private: void bindFragDataLocations(GLShader *shader); void bindAttributeLocations(GLShader *shader) const; @@ -159,7 +154,6 @@ private: QStack m_boundShaders; std::map> m_shaderHash; - static std::unique_ptr s_shaderManager; }; /** diff --git a/src/opengl/glutils.cpp b/src/opengl/glutils.cpp index c6e3905927..25e60ba1a9 100644 --- a/src/opengl/glutils.cpp +++ b/src/opengl/glutils.cpp @@ -121,7 +121,6 @@ void initGL(const std::function &resolveFunction) void cleanupGL() { - ShaderManager::cleanup(); GLTexturePrivate::cleanup(); GLFramebuffer::cleanup(); GLVertexBuffer::cleanup(); diff --git a/src/opengl/openglcontext.cpp b/src/opengl/openglcontext.cpp index e717351d9c..394d750e26 100644 --- a/src/opengl/openglcontext.cpp +++ b/src/opengl/openglcontext.cpp @@ -16,6 +16,8 @@ namespace KWin { +OpenGlContext *OpenGlContext::s_currentContext = nullptr; + static QSet getExtensions(OpenGlContext *context) { QSet ret; @@ -124,4 +126,9 @@ void OpenGlContext::setShaderManager(ShaderManager *manager) { m_shaderManager = manager; } + +OpenGlContext *OpenGlContext::currentContext() +{ + return s_currentContext; +} } diff --git a/src/opengl/openglcontext.h b/src/opengl/openglcontext.h index e30660077e..66e83fa3fd 100644 --- a/src/opengl/openglcontext.h +++ b/src/opengl/openglcontext.h @@ -44,10 +44,14 @@ public: */ bool checkSupported() const; + static OpenGlContext *currentContext(); + protected: bool checkTimerQuerySupport() const; void setShaderManager(ShaderManager *manager); + static OpenGlContext *s_currentContext; + const QByteArrayView m_versionString; const Version m_version; const QByteArrayView m_vendor;