From patchwork Tue Aug 11 13:00:41 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 27757 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 14A64C3306 for ; Tue, 11 Aug 2026 13:00:58 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id CD92668276; Tue, 11 Aug 2026 15:00:57 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="VPCkpJ9c"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 40AFA68264 for ; Tue, 11 Aug 2026 15:00:47 +0200 (CEST) Received: from pb-laptop.local (185.221.141.208.nat.pool.zt.hu [185.221.141.208]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 687E32345 for ; Tue, 11 Aug 2026 14:59:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1786453172; bh=4kEfIcU1lbOM39u1YsdzTKTpC5Ku/YGWGQ97t2RjVN4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VPCkpJ9c2RA9m16wXWiGYHF2wZVeZzB45j2GfdSOWFehCxfhIj46/Lz1dfGK+cu3A lmpIbY4HhMmoz2AjppfO7vOxgCTLk7hwG04ITXs8lgyl9MmOHS/PfxuKUfFLRM17FV 5ZrcYw0AHERUHxKCKzn7uoqCwTKWHXoQ+1GcTmVM= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [PATCH v3 7/8] libcamera: software_isp: debayer_egl: Avoid EGL context leaks Date: Tue, 11 Aug 2026 15:00:41 +0200 Message-ID: <20260811130042.213139-8-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260811130042.213139-1-barnabas.pocze@ideasonboard.com> References: <20260811130042.213139-1-barnabas.pocze@ideasonboard.com> MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Currently each `start()` invocation causes a new EGL context to be acquired, and only the last allocation will be properly freed in the `eGL` destructor. To avoid that, add and use `eGL::resetEGLContext()` in `stop()`; and also make `eGL` destructor stricter by requring no active EGL context. Signed-off-by: Barnabás Pőcze Reviewed-by: Milan Zamazal Reviewed-by: Jacopo Mondi --- include/libcamera/internal/egl.h | 1 + src/libcamera/egl.cpp | 27 +++++++++++++++++++--- src/libcamera/software_isp/debayer_egl.cpp | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/libcamera/internal/egl.h b/include/libcamera/internal/egl.h index 608d53151c..8f2ef296c3 100644 --- a/include/libcamera/internal/egl.h +++ b/include/libcamera/internal/egl.h @@ -104,6 +104,7 @@ public: ~eGL(); int initEGLContext(); + void resetEGLContext(); static EGLDisplay probeDisplay(); int createInputDMABufTexture2D(eGLImage &eglImage, int fd); diff --git a/src/libcamera/egl.cpp b/src/libcamera/egl.cpp index 2a1f577845..1f9e743c2f 100644 --- a/src/libcamera/egl.cpp +++ b/src/libcamera/egl.cpp @@ -73,12 +73,13 @@ eGL::eGL(EGLDisplay display) /** * \brief Destroy the EGL helper * - * Destroys the EGL context and surface if they were successfully created. + * An instance must only be destroyed if there is no active EGL context. + * + * \sa resetEGLContext() */ eGL::~eGL() { - if (context_ != EGL_NO_CONTEXT) - eglDestroyContext(display_, context_); + ASSERT(context_ == EGL_NO_CONTEXT); } /** @@ -368,6 +369,8 @@ void eGL::createOutputTexture2D(eGLImage &eglImage) * - eglCreateImageKHR / eglDestroyImageKHR * - glEGLImageTargetTexture2DOES * + * Each successful invocation must be followed by a call to resetEGLContext(). + * * \return 0 on success, or -ENODEV on failure */ int eGL::initEGLContext() @@ -444,6 +447,24 @@ fail: return -ENODEV; } +/** + * \brief Destroy the EGL context + * + * This function destroys the EGL context created by initEGLContext(). + * If there is no valid EGL context, this function has no effect. + */ +void eGL::resetEGLContext() +{ + if (context_ != EGL_NO_CONTEXT) { + assertThread(), + eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + eglDestroyContext(display_, std::exchange(context_, EGL_NO_CONTEXT)); + } + + tid_ = -1; + vtable_ = {}; +} + /** * \brief Make the EGL context current for the calling thread * diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp index 7b2b367305..97aa037935 100644 --- a/src/libcamera/software_isp/debayer_egl.cpp +++ b/src/libcamera/software_isp/debayer_egl.cpp @@ -680,6 +680,8 @@ void DebayerEGL::stop() if (programId_) glDeleteProgram(programId_); + + egl_.resetEGLContext(); } SizeRange DebayerEGL::sizes(PixelFormat inputFormat, const Size &inputSize)