From cc8cb8db9ddbd10761d1133b4e36059679f644be Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Fri, 16 Oct 2020 19:20:58 +0300 Subject: [PATCH] qpa: Create a pbuffer for internal windows If the surfaceless context extension is unsupported by the underlying platform, the QPA will use the EGLSurface of the first output to make OpenGL contexts current. If an internal window attempts to make an OpenGL context current while compositing is being restarted, for example it's typically the case with the composited outline visual, QPA will either try to make the context current with a no longer valid EGLSurface for the first output or will crash during the call to Platform::supportsSurfacelessContext(). The latter needs more explanation. After the compositingToggled() signal has been emitted, there is no scene and supportsSurfacelessContext() doesn't handle this case. In either case, we could return EGL_NO_SURFACE if compositing is being restarted, but if the underlying platform doesn't support the surfaceless context extension, then the composited outline will not be able to delete used textures, framebuffer objects, etc. This change addresses that problem by making sure that every platform window has a pbuffer allocated in case the surfaceless context extension is unsupported. --- plugins/qpa/window.cpp | 47 ++++++++++++++++++++++++++++++++++++++---- plugins/qpa/window.h | 5 +++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/plugins/qpa/window.cpp b/plugins/qpa/window.cpp index d307ad0abb..4996e7a532 100644 --- a/plugins/qpa/window.cpp +++ b/plugins/qpa/window.cpp @@ -8,6 +8,7 @@ SPDX-License-Identifier: GPL-2.0-or-later */ #include "window.h" +#include "eglhelpers.h" #include "platform.h" #include "screens.h" @@ -26,13 +27,23 @@ static quint32 s_windowId = 0; Window::Window(QWindow *window) : QPlatformWindow(window) + , m_eglDisplay(kwinApp()->platform()->sceneEglDisplay()) , m_windowId(++s_windowId) , m_scale(screens()->maxScale()) { + if (window->surfaceType() == QSurface::OpenGLSurface) { + // The window will use OpenGL for drawing. + if (!kwinApp()->platform()->supportsSurfacelessContext()) { + createPbuffer(); + } + } } Window::~Window() { + if (m_eglSurface != EGL_NO_SURFACE) { + eglDestroySurface(m_eglDisplay, m_eglSurface); + } unmap(); } @@ -47,6 +58,11 @@ void Window::setVisible(bool visible) QPlatformWindow::setVisible(visible); } +QSurfaceFormat Window::format() const +{ + return m_format; +} + void Window::setGeometry(const QRect &rect) { const QRect &oldRect = geometry(); @@ -123,6 +139,32 @@ void Window::createFBO() m_resized = false; } +void Window::createPbuffer() +{ + const QSurfaceFormat requestedFormat = window()->requestedFormat(); + const EGLConfig config = configFromFormat(m_eglDisplay, + requestedFormat, + EGL_PBUFFER_BIT); + if (config == EGL_NO_CONFIG_KHR) { + qCWarning(KWIN_QPA) << "Could not find any EGL config for:" << requestedFormat; + return; + } + + // The size doesn't matter as we render into a framebuffer object. + const EGLint attribs[] = { + EGL_WIDTH, 16, + EGL_HEIGHT, 16, + EGL_NONE + }; + + m_eglSurface = eglCreatePbufferSurface(m_eglDisplay, config, attribs); + if (m_eglSurface != EGL_NO_SURFACE) { + m_format = formatFromConfig(m_eglDisplay, config); + } else { + qCWarning(KWIN_QPA, "Failed to create a pbuffer for window: 0x%x", eglGetError()); + } +} + void Window::map() { if (m_handle) { @@ -146,10 +188,7 @@ void Window::unmap() EGLSurface Window::eglSurface() const { - if (kwinApp()->platform()->supportsSurfacelessContext()) { - return EGL_NO_SURFACE; - } - return kwinApp()->platform()->sceneEglSurface(); + return m_eglSurface; } } diff --git a/plugins/qpa/window.h b/plugins/qpa/window.h index e221451926..c1a7e75a35 100644 --- a/plugins/qpa/window.h +++ b/plugins/qpa/window.h @@ -33,6 +33,7 @@ public: explicit Window(QWindow *window); ~Window() override; + QSurfaceFormat format() const override; void setVisible(bool visible) override; void setGeometry(const QRect &rect) override; WId winId() const override; @@ -47,11 +48,15 @@ public: private: void createFBO(); + void createPbuffer(); void map(); void unmap(); + QSurfaceFormat m_format; QPointer m_handle; QSharedPointer m_contentFBO; + EGLDisplay m_eglDisplay = EGL_NO_DISPLAY; + EGLSurface m_eglSurface = EGL_NO_SURFACE; quint32 m_windowId; bool m_resized = false; int m_scale = 1;