[plasma/kwin] src: opengl/eglcontext: mark makeCurrent as nodiscard

Xaver Hugl <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 6c523e20d1280b397d2437246ee86649fd8b0bbb by Xaver Hugl.
Committed on 03/08/2026 at 15:09.
Pushed by zamundaaa into branch 'master'.

opengl/eglcontext: mark makeCurrent as nodiscard

And fix the cases where it was wrongly ignored. Some of these would in a GPU
reset potentially call OpenGL functions without checking the context, and as
a result cause KWin to crash.

M  +1    -1    src/backends/drm/drm_egl_layer_surface.cpp
M  +1    -1    src/backends/drm/drm_virtual_egl_layer.cpp
M  +4    -3    src/backends/virtual/virtual_egl_backend.cpp
M  +1    -1    src/backends/wayland/wayland_egl_backend.cpp
M  +1    -1    src/backends/x11/x11_windowed_egl_backend.cpp
M  +1    -1    src/compositor.cpp
M  +2    -2    src/effect/offscreeneffect.cpp
M  +1    -1    src/effect/offscreenquickview.cpp
M  +4    -4    src/multigpuswapchain.cpp
M  +1    -1    src/opengl/eglcontext.cpp
M  +1    -1    src/opengl/eglcontext.h
M  +2    -2    src/opengl/glrendertimequery.cpp
M  +2    -4    src/plugins/qpa/eglplatformcontext.cpp
M  +4    -1    src/plugins/screencast/screencastbuffer.cpp
M  +4    -1    src/plugins/screencast/screencaststream.cpp
M  +1    -1    src/scene/opengl/atlas.cpp
M  +1    -1    src/scene/opengl/ninepatch.cpp
M  +3    -4    src/scripting/windowthumbnailitem.cpp

https://invent.kde.org/plasma/kwin/-/commit/6c523e20d1280b397d2437246ee86649fd8b0bbb

diff --git a/src/backends/drm/drm_egl_layer_surface.cpp b/src/backends/drm/drm_egl_layer_surface.cpp
index 49c39d2c5ac..a948b747a9c 100644
--- a/src/backends/drm/drm_egl_layer_surface.cpp
+++ b/src/backends/drm/drm_egl_layer_surface.cpp
@@ -64,7 +64,7 @@ EglGbmLayerSurface::Surface::~Surface()
 {
     importSwapchain.reset();
     if (context) {
-        context->makeCurrent();
+        (void)context->makeCurrent();
     }
 }
 
diff --git a/src/backends/drm/drm_virtual_egl_layer.cpp b/src/backends/drm/drm_virtual_egl_layer.cpp
index 7837f58ea80..7dbafac993c 100644
--- a/src/backends/drm/drm_virtual_egl_layer.cpp
+++ b/src/backends/drm/drm_virtual_egl_layer.cpp
@@ -141,7 +141,7 @@ bool VirtualEglGbmLayer::importScanoutBuffer(GraphicsBuffer *buffer, const std::
 
 void VirtualEglGbmLayer::releaseBuffers()
 {
-    m_eglBackend->openglContext()->makeCurrent();
+    (void)m_eglBackend->openglContext()->makeCurrent();
     m_gbmSwapchain.reset();
     m_oldGbmSwapchain.reset();
     m_currentSlot.reset();
diff --git a/src/backends/virtual/virtual_egl_backend.cpp b/src/backends/virtual/virtual_egl_backend.cpp
index 768a67faedb..97b729845f4 100644
--- a/src/backends/virtual/virtual_egl_backend.cpp
+++ b/src/backends/virtual/virtual_egl_backend.cpp
@@ -33,12 +33,14 @@ VirtualEglLayer::VirtualEglLayer(BackendOutput *output, VirtualEglBackend *backe
 
 VirtualEglLayer::~VirtualEglLayer()
 {
-    m_backend->openglContext()->makeCurrent();
+    (void)m_backend->openglContext()->makeCurrent();
 }
 
 std::optional<OutputLayerBeginFrameInfo> VirtualEglLayer::doBeginFrame()
 {
-    m_backend->openglContext()->makeCurrent();
+    if (!m_backend->openglContext()->makeCurrent()) {
+        return std::nullopt;
+    }
 
     const QSize nativeSize = m_output->modeSize();
     if (!m_swapchain || m_swapchain->size() != nativeSize) {
@@ -140,7 +142,6 @@ bool VirtualEglBackend::init()
 
 void VirtualEglBackend::addOutput(BackendOutput *output)
 {
-    openglContext()->makeCurrent();
     static_cast<VirtualOutput *>(output)->setOutputLayer(std::make_unique<VirtualEglLayer>(output, this));
 }
 
diff --git a/src/backends/wayland/wayland_egl_backend.cpp b/src/backends/wayland/wayland_egl_backend.cpp
index 378bac03160..0611b1b6765 100644
--- a/src/backends/wayland/wayland_egl_backend.cpp
+++ b/src/backends/wayland/wayland_egl_backend.cpp
@@ -153,7 +153,7 @@ WaylandEglCursorLayer::WaylandEglCursorLayer(WaylandOutput *output, WaylandEglBa
 
 WaylandEglCursorLayer::~WaylandEglCursorLayer()
 {
-    m_backend->openglContext()->makeCurrent();
+    (void)m_backend->openglContext()->makeCurrent();
 }
 
 std::optional<OutputLayerBeginFrameInfo> WaylandEglCursorLayer::doBeginFrame()
diff --git a/src/backends/x11/x11_windowed_egl_backend.cpp b/src/backends/x11/x11_windowed_egl_backend.cpp
index 7427df9fbaa..e0692bdd8dd 100644
--- a/src/backends/x11/x11_windowed_egl_backend.cpp
+++ b/src/backends/x11/x11_windowed_egl_backend.cpp
@@ -105,7 +105,7 @@ X11WindowedEglCursorLayer::X11WindowedEglCursorLayer(X11WindowedEglBackend *back
 
 X11WindowedEglCursorLayer::~X11WindowedEglCursorLayer()
 {
-    m_backend->openglContext()->makeCurrent();
+    (void)m_backend->openglContext()->makeCurrent();
     m_framebuffer.reset();
     m_texture.reset();
 }
diff --git a/src/compositor.cpp b/src/compositor.cpp
index ba756030cc4..16556798c8e 100644
--- a/src/compositor.cpp
+++ b/src/compositor.cpp
@@ -405,7 +405,7 @@ void Compositor::stop()
 
     if (m_backend->compositingType() == OpenGLCompositing) {
         // some layers need a context current for destruction
-        static_cast<EglBackend *>(m_backend.get())->openglContext()->makeCurrent();
+        (void)static_cast<EglBackend *>(m_backend.get())->openglContext()->makeCurrent();
     }
 
     const auto loops = m_primaryViews | std::views::transform([](const auto &pair) {
diff --git a/src/effect/offscreeneffect.cpp b/src/effect/offscreeneffect.cpp
index 8d2f18d9a50..a9d0698910e 100644
--- a/src/effect/offscreeneffect.cpp
+++ b/src/effect/offscreeneffect.cpp
@@ -86,7 +86,7 @@ void OffscreenEffect::unredirect(EffectWindow *window)
     }
 
     if (!EglContext::currentContext()) {
-        effects->openglContext()->makeCurrent();
+        (void)effects->openglContext()->makeCurrent();
     }
 
     d->windows.erase(it);
@@ -415,7 +415,7 @@ void CrossFadeEffect::unredirect(EffectWindow *window)
     }
 
     if (!EglContext::currentContext()) {
-        effects->openglContext()->makeCurrent();
+        (void)effects->openglContext()->makeCurrent();
     }
 
     d->windows.erase(it);
diff --git a/src/effect/offscreenquickview.cpp b/src/effect/offscreenquickview.cpp
index 30b322a859b..72dd363d5b1 100644
--- a/src/effect/offscreenquickview.cpp
+++ b/src/effect/offscreenquickview.cpp
@@ -399,7 +399,7 @@ void OffscreenQuickView::update(OutputFrame *frame)
         }
         d->m_glcontext->doneCurrent();
         if (previousContext) {
-            previousContext->makeCurrent();
+            (void)previousContext->makeCurrent();
         }
     }
     d->m_item->scheduleRepaint(d->m_item->rect());
diff --git a/src/multigpuswapchain.cpp b/src/multigpuswapchain.cpp
index 82219c77ace..4ab1fa0bcb2 100644
--- a/src/multigpuswapchain.cpp
+++ b/src/multigpuswapchain.cpp
@@ -97,7 +97,7 @@ std::unique_ptr<MultiGpuSwapchain> MultiGpuSwapchain::create(RenderDevice *copyD
         // creating the copy context will make it current
         const auto restoreContext = qScopeGuard([ctx = EglContext::currentContext()]() {
             if (ctx) {
-                ctx->makeCurrent();
+                (void)ctx->makeCurrent();
             }
         });
         const auto context = copyDevice->eglContext();
@@ -356,7 +356,7 @@ std::optional<MultiGpuSwapchain::Ret> MultiGpuSwapchain::copyWithEGL(GraphicsBuf
         if (previousContext) {
             // TODO make the calling code responsible for this?
             // If this makeCurrent fails, things might crash :/
-            previousContext->makeCurrent();
+            (void)previousContext->makeCurrent();
         }
     });
     if (!m_copyContext || m_copyContext->isFailed() || !m_copyContext->makeCurrent()) {
@@ -434,10 +434,10 @@ void MultiGpuSwapchain::deleteResources()
     if (m_copyContext) {
         const auto restoreContext = qScopeGuard([ctx = EglContext::currentContext()]() {
             if (ctx) {
-                ctx->makeCurrent();
+                (void)ctx->makeCurrent();
             }
         });
-        m_copyContext->makeCurrent();
+        (void)m_copyContext->makeCurrent();
         m_currentEglSlot.reset();
         m_eglSwapchain.reset();
         m_copyContext.reset();
diff --git a/src/opengl/eglcontext.cpp b/src/opengl/eglcontext.cpp
index 464da01ac23..e5c8e48ed73 100644
--- a/src/opengl/eglcontext.cpp
+++ b/src/opengl/eglcontext.cpp
@@ -114,7 +114,7 @@ EglContext::EglContext(EglDisplay *display, EGLConfig config, ::EGLContext conte
 
 EglContext::~EglContext()
 {
-    makeCurrent();
+    (void)makeCurrent();
     m_shaderManager.reset();
     m_streamingBuffer.reset();
     m_indexBuffer.reset();
diff --git a/src/opengl/eglcontext.h b/src/opengl/eglcontext.h
index 60b7b3999b5..e13bf6f1ef5 100644
--- a/src/opengl/eglcontext.h
+++ b/src/opengl/eglcontext.h
@@ -41,7 +41,7 @@ public:
     EglContext(EglDisplay *display, EGLConfig config, ::EGLContext context, EglContext *shareContext);
     ~EglContext();
 
-    bool makeCurrent();
+    [[nodiscard]] bool makeCurrent();
     bool makeCurrent(EGLSurface surface);
     void doneCurrent() const;
     std::shared_ptr<GLTexture> importDmaBufAsTexture(const DmaBufAttributes &attributes) const;
diff --git a/src/opengl/glrendertimequery.cpp b/src/opengl/glrendertimequery.cpp
index 438a97d8ba4..3e94dbebec2 100644
--- a/src/opengl/glrendertimequery.cpp
+++ b/src/opengl/glrendertimequery.cpp
@@ -39,7 +39,7 @@ GLRenderTimeQuery::~GLRenderTimeQuery()
     }
     glDeleteQueries(1, &m_gpuProbe.query);
     if (previousContext && previousContext != context.get()) {
-        previousContext->makeCurrent();
+        (void)previousContext->makeCurrent();
     }
 }
 
@@ -76,7 +76,7 @@ std::optional<RenderTimeSpan> GLRenderTimeQuery::query()
         glGetQueryObjecti64v(m_gpuProbe.query, GL_QUERY_RESULT, &end);
         m_gpuProbe.end = std::chrono::nanoseconds(end);
         if (previousContext && previousContext != context.get()) {
-            previousContext->makeCurrent();
+            (void)previousContext->makeCurrent();
         }
     }
 
diff --git a/src/plugins/qpa/eglplatformcontext.cpp b/src/plugins/qpa/eglplatformcontext.cpp
index b3736311afd..75386f3689d 100644
--- a/src/plugins/qpa/eglplatformcontext.cpp
+++ b/src/plugins/qpa/eglplatformcontext.cpp
@@ -53,7 +53,7 @@ EGLPlatformContext::~EGLPlatformContext()
         return;
     }
     if (!m_renderTargets.empty() || !m_zombieRenderTargets.empty()) {
-        m_eglContext->makeCurrent();
+        (void)m_eglContext->makeCurrent();
         m_renderTargets.clear();
         m_zombieRenderTargets.clear();
     }
@@ -202,8 +202,6 @@ void EGLPlatformContext::create(const QSurfaceFormat &format, EglContext *shareC
 
 void EGLPlatformContext::updateFormatFromContext()
 {
-    m_eglContext->makeCurrent();
-
     const char *version = reinterpret_cast<const char *>(glGetString(GL_VERSION));
     int major, minor;
     if (parseOpenGLVersion(version, major, minor)) {
@@ -260,7 +258,7 @@ void EGLPlatformContext::invalidateContext()
 {
     m_markedInvalid = true;
     if (m_eglContext) {
-        m_eglContext->makeCurrent();
+        (void)m_eglContext->makeCurrent();
         m_renderTargets.clear();
         m_zombieRenderTargets.clear();
         m_eglContext.reset();
diff --git a/src/plugins/screencast/screencastbuffer.cpp b/src/plugins/screencast/screencastbuffer.cpp
index 2bbe105ae11..fd2b9713839 100644
--- a/src/plugins/screencast/screencastbuffer.cpp
+++ b/src/plugins/screencast/screencastbuffer.cpp
@@ -57,7 +57,10 @@ DmaBufScreenCastBuffer *DmaBufScreenCastBuffer::create(pw_buffer *pwBuffer, cons
         return nullptr;
     }
 
-    backend->openglContext()->makeCurrent();
+    if (!backend->openglContext()->makeCurrent()) {
+        buffer->drop();
+        return nullptr;
+    }
 
     auto texture = backend->importDmaBufAsTexture(*attrs);
     if (!texture) {
diff --git a/src/plugins/screencast/screencaststream.cpp b/src/plugins/screencast/screencaststream.cpp
index 919e9c0039a..5a169e78d04 100644
--- a/src/plugins/screencast/screencaststream.cpp
+++ b/src/plugins/screencast/screencaststream.cpp
@@ -621,7 +621,10 @@ void ScreenCastStream::record(Contents contents)
     }
 
     EglContext *context = backend->openglContext();
-    context->makeCurrent();
+    if (!context->makeCurrent()) {
+        pw_stream_return_buffer(m_pwStream, pwBuffer);
+        return;
+    }
 
     spa_meta_sync_timeline *synctmeta = nullptr;
 
diff --git a/src/scene/opengl/atlas.cpp b/src/scene/opengl/atlas.cpp
index f179467cdfc..2926da0cd52 100644
--- a/src/scene/opengl/atlas.cpp
+++ b/src/scene/opengl/atlas.cpp
@@ -29,7 +29,7 @@ AtlasOpenGL::~AtlasOpenGL()
 {
     // FIXME: It should not be attached to the workspace scene.
     if (WorkspaceScene *scene = kwinApp()->scene()) {
-        scene->openglContext()->makeCurrent();
+        (void)scene->openglContext()->makeCurrent();
     }
 }
 
diff --git a/src/scene/opengl/ninepatch.cpp b/src/scene/opengl/ninepatch.cpp
index 1c040191a15..04de922974a 100644
--- a/src/scene/opengl/ninepatch.cpp
+++ b/src/scene/opengl/ninepatch.cpp
@@ -124,7 +124,7 @@ NinePatchOpenGL::NinePatchOpenGL(std::unique_ptr<GLTexture> &&texture)
 NinePatchOpenGL::~NinePatchOpenGL()
 {
     // FIXME: It should not be attached to the workspace scene.
-    kwinApp()->scene()->openglContext()->makeCurrent();
+    (void)kwinApp()->scene()->openglContext()->makeCurrent();
 }
 
 GLTexture *NinePatchOpenGL::texture() const
diff --git a/src/scripting/windowthumbnailitem.cpp b/src/scripting/windowthumbnailitem.cpp
index ee3ec441e24..b72c34d6fb7 100644
--- a/src/scripting/windowthumbnailitem.cpp
+++ b/src/scripting/windowthumbnailitem.cpp
@@ -65,13 +65,12 @@ WindowThumbnailSource::~WindowThumbnailSource()
     if (!m_offscreenTexture) {
         return;
     }
-    if (!QOpenGLContext::currentContext()) {
-        kwinApp()->scene()->openglContext()->makeCurrent();
-    }
+    const bool hasContext = QOpenGLContext::currentContext()
+        || kwinApp()->scene()->openglContext()->makeCurrent();
     m_offscreenTarget.reset();
     m_offscreenTexture.reset();
 
-    if (m_acquireFence) {
+    if (m_acquireFence && hasContext) {
         glDeleteSync(m_acquireFence);
         m_acquireFence = 0;
     }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.