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

Xaver Hugl <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 067646a4571a9e75d6cf5bfb64df796c29a79fcb by Xaver Hugl.
Committed on 10/08/2026 at 14:22.
Pushed by zamundaaa into branch 'Plasma/6.7'.

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.

(cherry picked from commit 6c523e20d1280b397d2437246ee86649fd8b0bbb)

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.h
M  +2    -2    src/opengl/glrendertimequery.cpp
M  +1    -3    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/067646a4571a9e75d6cf5bfb64df796c29a79fcb

diff --git a/src/backends/drm/drm_egl_layer_surface.cpp b/src/backends/drm/drm_egl_layer_surface.cpp
index 79ec6606ae7..f3ed54cb69d 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 0240ae5eb8f..77fef671d95 100644
--- a/src/backends/drm/drm_virtual_egl_layer.cpp
+++ b/src/backends/drm/drm_virtual_egl_layer.cpp
@@ -140,7 +140,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 6e17b3e2b5a..adc4d62542b 100644
--- a/src/backends/virtual/virtual_egl_backend.cpp
+++ b/src/backends/virtual/virtual_egl_backend.cpp
@@ -32,12 +32,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) {
@@ -149,7 +151,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 022fd954752..b0a26a11f99 100644
--- a/src/backends/wayland/wayland_egl_backend.cpp
+++ b/src/backends/wayland/wayland_egl_backend.cpp
@@ -148,7 +148,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 7fe3f9dbe22..f64d5d8b2f5 100644
--- a/src/backends/x11/x11_windowed_egl_backend.cpp
+++ b/src/backends/x11/x11_windowed_egl_backend.cpp
@@ -100,7 +100,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 616ee65b8cf..5e8b793c746 100644
--- a/src/compositor.cpp
+++ b/src/compositor.cpp
@@ -310,7 +310,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 c7ee1acb694..c5272543233 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);
@@ -407,7 +407,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 532b92a435f..40fdcbc3163 100644
--- a/src/effect/offscreenquickview.cpp
+++ b/src/effect/offscreenquickview.cpp
@@ -384,7 +384,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 e9e9131b94d..7b3a09ccab5 100644
--- a/src/multigpuswapchain.cpp
+++ b/src/multigpuswapchain.cpp
@@ -90,7 +90,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();
@@ -308,7 +308,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()) {
@@ -375,10 +375,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.h b/src/opengl/eglcontext.h
index 0ce983ca9f6..8cf32f25d69 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 65454525db8..cb14e9460ce 100644
--- a/src/plugins/qpa/eglplatformcontext.cpp
+++ b/src/plugins/qpa/eglplatformcontext.cpp
@@ -52,7 +52,7 @@ EGLPlatformContext::~EGLPlatformContext()
         return;
     }
     if (!m_renderTargets.empty() || !m_zombieRenderTargets.empty()) {
-        m_eglContext->makeCurrent();
+        (void)m_eglContext->makeCurrent();
         m_renderTargets.clear();
         m_zombieRenderTargets.clear();
     }
@@ -205,8 +205,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)) {
diff --git a/src/plugins/screencast/screencastbuffer.cpp b/src/plugins/screencast/screencastbuffer.cpp
index 78a10d49560..dc5dcc35ea7 100644
--- a/src/plugins/screencast/screencastbuffer.cpp
+++ b/src/plugins/screencast/screencastbuffer.cpp
@@ -56,7 +56,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 cb51ca2adf2..022e9658ccd 100644
--- a/src/plugins/screencast/screencaststream.cpp
+++ b/src/plugins/screencast/screencaststream.cpp
@@ -609,7 +609,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 651377894d2..943ddf1b921 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 a4d50a2ceb3..798dc2aa16a 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.