[v3,5/8] libcamera: egl: Add `assertThread()`
diff mbox series

Message ID 20260811130042.213139-6-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • libcamera: software_isp: debayer_egl: Remove some leaks
Related show

Commit Message

Barnabás Pőcze Aug. 11, 2026, 1 p.m. UTC
Add a public function that does the thread check, and use that everywhere.

This is very similar to `Object::assertThreadBound()`, but without actually
making `eGL` an `Object`. That would be an option, but handling the moving
between threads with the egl context is not easily implementable currently.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 include/libcamera/internal/egl.h |  6 ++++++
 src/libcamera/egl.cpp            | 30 +++++++++++++++++++-----------
 2 files changed, 25 insertions(+), 11 deletions(-)

Comments

Milan Zamazal Aug. 11, 2026, 2:28 p.m. UTC | #1
Barnabás Pőcze <barnabas.pocze@ideasonboard.com> writes:

> Add a public function that does the thread check, and use that everywhere.
>
> This is very similar to `Object::assertThreadBound()`, but without actually
> making `eGL` an `Object`. That would be an option, but handling the moving
> between threads with the egl context is not easily implementable currently.
>
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>

Reviewed-by: Milan Zamazal <mzamazal@redhat.com>

> ---
>  include/libcamera/internal/egl.h |  6 ++++++
>  src/libcamera/egl.cpp            | 30 +++++++++++++++++++-----------
>  2 files changed, 25 insertions(+), 11 deletions(-)
>
> diff --git a/include/libcamera/internal/egl.h b/include/libcamera/internal/egl.h
> index fb77225ac4..13df65e6b2 100644
> --- a/include/libcamera/internal/egl.h
> +++ b/include/libcamera/internal/egl.h
> @@ -14,6 +14,7 @@
>  
>  #include <libcamera/base/log.h>
>  #include <libcamera/base/span.h>
> +#include <libcamera/base/thread.h>
>  #include <libcamera/base/utils.h>
>  
>  /*
> @@ -126,6 +127,11 @@ public:
>  	void syncOutput();
>  	void flushOutput();
>  
> +	void assertThread() const
> +	{
> +		ASSERT(tid_ == Thread::currentId());
> +	}
> +
>  private:
>  	LIBCAMERA_DISABLE_COPY_AND_MOVE(eGL)
>  
> diff --git a/src/libcamera/egl.cpp b/src/libcamera/egl.cpp
> index 3de5fab816..2a1f577845 100644
> --- a/src/libcamera/egl.cpp
> +++ b/src/libcamera/egl.cpp
> @@ -89,7 +89,7 @@ eGL::~eGL()
>   */
>  void eGL::syncOutput()
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	glFinish();
>  }
> @@ -102,11 +102,19 @@ void eGL::syncOutput()
>   */
>  void eGL::flushOutput()
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	glFlush();
>  }
>  
> +/**
> + * \fn eGL::assertThread()
> + * \brief Assert that the execution is in the right thread
> + *
> + * Asserts that the execution is in the thread for which
> + * the created EGL context has been made current.
> + */
> +
>  /**
>   * \brief Attach a texture to a frame-buffer-object
>   *
> @@ -167,7 +175,7 @@ int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output)
>  	EGLint drm_format;
>  	int ret = 0;
>  
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	switch (eglImage.format_) {
>  	case GL_RED:
> @@ -242,7 +250,7 @@ int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output)
>   */
>  int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	return createDMABufTexture2D(eglImage, fd, false);
>  }
> @@ -261,7 +269,7 @@ int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd)
>   */
>  int eGL::createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	return createDMABufTexture2D(eglImage, fd, true);
>  }
> @@ -278,7 +286,7 @@ int eGL::createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
>   */
>  void eGL::createTexture2D(eGLImage &eglImage, void *data)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	activateBindTexture(eglImage);
>  
> @@ -331,7 +339,7 @@ EGLDisplay eGL::probeDisplay()
>   */
>  void eGL::updateTexture2D(eGLImage &eglImage, void *data)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	activateBindTexture(eglImage);
>  
> @@ -445,7 +453,7 @@ fail:
>   */
>  void eGL::makeCurrent()
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	if (eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, context_) != EGL_TRUE) {
>  		LOG(eGL, Error) << "eglMakeCurrent fail";
> @@ -523,7 +531,7 @@ int eGL::compileShader(int shaderType, GLuint &shaderId,
>  	GLint success;
>  	size_t i;
>  
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	auto count = 1 + shaderEnv.size();
>  	auto shaderSourceData = std::make_unique<const GLchar *[]>(count);
> @@ -570,7 +578,7 @@ void eGL::dumpShaderSource(GLuint shaderId)
>  {
>  	GLint shaderLength = 0;
>  
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	glGetShaderiv(shaderId, GL_SHADER_SOURCE_LENGTH, &shaderLength);
>  
> @@ -603,7 +611,7 @@ int eGL::linkProgram(GLuint &programId, GLuint vertexshaderId, GLuint fragmentsh
>  	GLenum err;
>  	int ret = -ENODEV;
>  
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>  
>  	programId = glCreateProgram();
>  	if (!programId) {
Jacopo Mondi Aug. 11, 2026, 3:58 p.m. UTC | #2
Hi Barnabás

On Tue, Aug 11, 2026 at 03:00:39PM +0200, Barnabás Pőcze wrote:
> Add a public function that does the thread check, and use that everywhere.
>
> This is very similar to `Object::assertThreadBound()`, but without actually
> making `eGL` an `Object`. That would be an option, but handling the moving
> between threads with the egl context is not easily implementable currently.
>
> Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
> ---
>  include/libcamera/internal/egl.h |  6 ++++++
>  src/libcamera/egl.cpp            | 30 +++++++++++++++++++-----------
>  2 files changed, 25 insertions(+), 11 deletions(-)
>
> diff --git a/include/libcamera/internal/egl.h b/include/libcamera/internal/egl.h
> index fb77225ac4..13df65e6b2 100644
> --- a/include/libcamera/internal/egl.h
> +++ b/include/libcamera/internal/egl.h
> @@ -14,6 +14,7 @@
>
>  #include <libcamera/base/log.h>
>  #include <libcamera/base/span.h>
> +#include <libcamera/base/thread.h>
>  #include <libcamera/base/utils.h>
>
>  /*
> @@ -126,6 +127,11 @@ public:
>  	void syncOutput();
>  	void flushOutput();
>
> +	void assertThread() const
> +	{
> +		ASSERT(tid_ == Thread::currentId());
> +	}
> +
>  private:
>  	LIBCAMERA_DISABLE_COPY_AND_MOVE(eGL)
>
> diff --git a/src/libcamera/egl.cpp b/src/libcamera/egl.cpp
> index 3de5fab816..2a1f577845 100644
> --- a/src/libcamera/egl.cpp
> +++ b/src/libcamera/egl.cpp
> @@ -89,7 +89,7 @@ eGL::~eGL()
>   */
>  void eGL::syncOutput()
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	glFinish();
>  }
> @@ -102,11 +102,19 @@ void eGL::syncOutput()
>   */
>  void eGL::flushOutput()
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	glFlush();
>  }
>
> +/**
> + * \fn eGL::assertThread()
> + * \brief Assert that the execution is in the right thread
> + *
> + * Asserts that the execution is in the thread for which
> + * the created EGL context has been made current.

reflow to 80 cols please

Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>

> + */
> +
>  /**
>   * \brief Attach a texture to a frame-buffer-object
>   *
> @@ -167,7 +175,7 @@ int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output)
>  	EGLint drm_format;
>  	int ret = 0;
>
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	switch (eglImage.format_) {
>  	case GL_RED:
> @@ -242,7 +250,7 @@ int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output)
>   */
>  int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	return createDMABufTexture2D(eglImage, fd, false);
>  }
> @@ -261,7 +269,7 @@ int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd)
>   */
>  int eGL::createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	return createDMABufTexture2D(eglImage, fd, true);
>  }
> @@ -278,7 +286,7 @@ int eGL::createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
>   */
>  void eGL::createTexture2D(eGLImage &eglImage, void *data)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	activateBindTexture(eglImage);
>
> @@ -331,7 +339,7 @@ EGLDisplay eGL::probeDisplay()
>   */
>  void eGL::updateTexture2D(eGLImage &eglImage, void *data)
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	activateBindTexture(eglImage);
>
> @@ -445,7 +453,7 @@ fail:
>   */
>  void eGL::makeCurrent()
>  {
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	if (eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, context_) != EGL_TRUE) {
>  		LOG(eGL, Error) << "eglMakeCurrent fail";
> @@ -523,7 +531,7 @@ int eGL::compileShader(int shaderType, GLuint &shaderId,
>  	GLint success;
>  	size_t i;
>
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	auto count = 1 + shaderEnv.size();
>  	auto shaderSourceData = std::make_unique<const GLchar *[]>(count);
> @@ -570,7 +578,7 @@ void eGL::dumpShaderSource(GLuint shaderId)
>  {
>  	GLint shaderLength = 0;
>
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	glGetShaderiv(shaderId, GL_SHADER_SOURCE_LENGTH, &shaderLength);
>
> @@ -603,7 +611,7 @@ int eGL::linkProgram(GLuint &programId, GLuint vertexshaderId, GLuint fragmentsh
>  	GLenum err;
>  	int ret = -ENODEV;
>
> -	ASSERT(tid_ == Thread::currentId());
> +	assertThread();
>
>  	programId = glCreateProgram();
>  	if (!programId) {
> --
> 2.55.0
>

Patch
diff mbox series

diff --git a/include/libcamera/internal/egl.h b/include/libcamera/internal/egl.h
index fb77225ac4..13df65e6b2 100644
--- a/include/libcamera/internal/egl.h
+++ b/include/libcamera/internal/egl.h
@@ -14,6 +14,7 @@ 
 
 #include <libcamera/base/log.h>
 #include <libcamera/base/span.h>
+#include <libcamera/base/thread.h>
 #include <libcamera/base/utils.h>
 
 /*
@@ -126,6 +127,11 @@  public:
 	void syncOutput();
 	void flushOutput();
 
+	void assertThread() const
+	{
+		ASSERT(tid_ == Thread::currentId());
+	}
+
 private:
 	LIBCAMERA_DISABLE_COPY_AND_MOVE(eGL)
 
diff --git a/src/libcamera/egl.cpp b/src/libcamera/egl.cpp
index 3de5fab816..2a1f577845 100644
--- a/src/libcamera/egl.cpp
+++ b/src/libcamera/egl.cpp
@@ -89,7 +89,7 @@  eGL::~eGL()
  */
 void eGL::syncOutput()
 {
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	glFinish();
 }
@@ -102,11 +102,19 @@  void eGL::syncOutput()
  */
 void eGL::flushOutput()
 {
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	glFlush();
 }
 
+/**
+ * \fn eGL::assertThread()
+ * \brief Assert that the execution is in the right thread
+ *
+ * Asserts that the execution is in the thread for which
+ * the created EGL context has been made current.
+ */
+
 /**
  * \brief Attach a texture to a frame-buffer-object
  *
@@ -167,7 +175,7 @@  int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output)
 	EGLint drm_format;
 	int ret = 0;
 
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	switch (eglImage.format_) {
 	case GL_RED:
@@ -242,7 +250,7 @@  int eGL::createDMABufTexture2D(eGLImage &eglImage, int fd, bool output)
  */
 int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd)
 {
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	return createDMABufTexture2D(eglImage, fd, false);
 }
@@ -261,7 +269,7 @@  int eGL::createInputDMABufTexture2D(eGLImage &eglImage, int fd)
  */
 int eGL::createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
 {
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	return createDMABufTexture2D(eglImage, fd, true);
 }
@@ -278,7 +286,7 @@  int eGL::createOutputDMABufTexture2D(eGLImage &eglImage, int fd)
  */
 void eGL::createTexture2D(eGLImage &eglImage, void *data)
 {
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	activateBindTexture(eglImage);
 
@@ -331,7 +339,7 @@  EGLDisplay eGL::probeDisplay()
  */
 void eGL::updateTexture2D(eGLImage &eglImage, void *data)
 {
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	activateBindTexture(eglImage);
 
@@ -445,7 +453,7 @@  fail:
  */
 void eGL::makeCurrent()
 {
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	if (eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, context_) != EGL_TRUE) {
 		LOG(eGL, Error) << "eglMakeCurrent fail";
@@ -523,7 +531,7 @@  int eGL::compileShader(int shaderType, GLuint &shaderId,
 	GLint success;
 	size_t i;
 
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	auto count = 1 + shaderEnv.size();
 	auto shaderSourceData = std::make_unique<const GLchar *[]>(count);
@@ -570,7 +578,7 @@  void eGL::dumpShaderSource(GLuint shaderId)
 {
 	GLint shaderLength = 0;
 
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	glGetShaderiv(shaderId, GL_SHADER_SOURCE_LENGTH, &shaderLength);
 
@@ -603,7 +611,7 @@  int eGL::linkProgram(GLuint &programId, GLuint vertexshaderId, GLuint fragmentsh
 	GLenum err;
 	int ret = -ENODEV;
 
-	ASSERT(tid_ == Thread::currentId());
+	assertThread();
 
 	programId = glCreateProgram();
 	if (!programId) {