[1/6] libcamera: egl: Add GL extension & version checks
diff mbox series

Message ID 20261126110422.362455-2-remy.alipoursafari@collabora.com
State New
Headers show
Series
  • benchmark: Add GPU benchmarking to SoftISP eGL
Related show

Commit Message

Remy Alipoursafari Nov. 26, 2026, 11:03 a.m. UTC
From: Loïc Molinari <loic.molinari@collabora.com>

Allow EGL and OpenGL ES extensions to be queried for whether or not they
exist in the current context.
Also allows for checking the OpenGL ES context's version.

Signed-off-by: Loïc Molinari <loic.molinari@collabora.com>
Signed-off-by: Remy Alipoursafari <remy.alipoursafari@collabora.com>
Reviewed-by: Robert Mader <robert.mader@collabora.com>
---
 include/libcamera/internal/egl.h | 11 ++++
 src/libcamera/egl.cpp            | 89 +++++++++++++++++++++++++++++++-
 2 files changed, 98 insertions(+), 2 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/internal/egl.h b/include/libcamera/internal/egl.h
index d6fb6fe0..10a9a3c5 100644
--- a/include/libcamera/internal/egl.h
+++ b/include/libcamera/internal/egl.h
@@ -109,6 +109,10 @@  public:
 	void resetEGLContext();
 	static EGLDisplay probeDisplay();
 
+	bool hasEGLExtension(const char *name) const;
+	bool hasGLESExtension(const char *name) const;
+	bool hasGLESVersion(unsigned int major, unsigned int minor) const;
+
 	int createInputDMABufTexture2D(eGLImage &eglImage, int fd);
 	int createOutputDMABufTexture2D(eGLImage &eglImage, int fd);
 	void createTexture2D(eGLImage &eglImage, const void *data, GLint filter);
@@ -142,6 +146,13 @@  private:
 	EGLDisplay display_ = EGL_NO_DISPLAY;
 	EGLContext context_ = EGL_NO_CONTEXT;
 
+	const char *eglExtensions_ = NULL;
+	const char *glesExtensions_ = NULL;
+	unsigned int glesMajor_ = 0;
+	unsigned int glesMinor_ = 0;
+
+	static bool hasExtension(const char *extensions, const char *name);
+
 	int compileShader(int shaderType, GLuint &shaderId, std::span<const unsigned char> shaderData,
 			  std::span<const std::string> shaderEnv);
 
diff --git a/src/libcamera/egl.cpp b/src/libcamera/egl.cpp
index ff9e54a1..8bc70b7d 100644
--- a/src/libcamera/egl.cpp
+++ b/src/libcamera/egl.cpp
@@ -10,6 +10,7 @@ 
 #include "libcamera/internal/egl.h"
 
 #include <fcntl.h>
+#include <string.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <unistd.h>
@@ -397,6 +398,7 @@  int eGL::initEGLContext()
 	VTable vtable = {};
 	EGLint numConfigs;
 	EGLConfig config;
+	const char *glesVersion;
 
 	if (context_ != EGL_NO_CONTEXT)
 		return -EEXIST;
@@ -409,7 +411,8 @@  int eGL::initEGLContext()
 	LOG(eGL, Info) << "EGL: EGL_VERSION: " << eglQueryString(display_, EGL_VERSION);
 	LOG(eGL, Info) << "EGL: EGL_VENDOR: " << eglQueryString(display_, EGL_VENDOR);
 	LOG(eGL, Info) << "EGL: EGL_CLIENT_APIS: " << eglQueryString(display_, EGL_CLIENT_APIS);
-	LOG(eGL, Info) << "EGL: EGL_EXTENSIONS: " << eglQueryString(display_, EGL_EXTENSIONS);
+	eglExtensions_ = eglQueryString(display_, EGL_EXTENSIONS);
+	LOG(eGL, Info) << "EGL: EGL_EXTENSIONS: " << eglExtensions_;
 
 	vtable.eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");
 	if (!vtable.eglCreateImageKHR) {
@@ -446,7 +449,16 @@  int eGL::initEGLContext()
 	makeCurrent();
 
 	LOG(eGL, Info) << "EGL: GL_RENDERER: " << glGetString(GL_RENDERER);
-	LOG(eGL, Info) << "EGL: GL_VERSION: " << glGetString(GL_VERSION);
+	glesVersion = (const char *)glGetString(GL_VERSION);
+	LOG(eGL, Info) << "EGL: GL_VERSION: " << glesVersion;
+	glesExtensions_ = (const char *)glGetString(GL_EXTENSIONS);
+	LOG(eGL, Info) << "EGL: GL_EXTENSIONS: " << glesExtensions_;
+
+	if (sscanf(glesVersion, "OpenGL ES %u.%u",
+		   &glesMajor_, &glesMinor_) != 2) {
+		LOG(eGL, Error) << "glGetString(GL_VERSION) invalid";
+		goto fail;
+	}
 
 	return 0;
 fail:
@@ -454,6 +466,74 @@  fail:
 	return -ENODEV;
 }
 
+/**
+ * \brief Check EGL extension availability
+ * \param[in] name Extension name
+ *
+ * Checks whether a given EGL extension is currently available.
+ *
+ * \return True when available, false if not
+ */
+bool eGL::hasEGLExtension(const char *name) const
+{
+	return hasExtension(eglExtensions_, name);
+}
+
+/**
+ * \brief Check GLES extension availability
+ * \param[in] name Extension name
+ *
+ * Checks whether a given OpenGL ES extension is currently available in the
+ * context.
+ *
+ * \return True when available, false if not
+ */
+bool eGL::hasGLESExtension(const char *name) const
+{
+	return hasExtension(glesExtensions_, name);
+}
+
+/**
+ * \brief Checks extension availability
+ * \param[in] extensions Space separated string listing available extensions
+ * \param[in] name Extension name to check against
+ *
+ * Checks whether a given extension is within the extensions list.
+ *
+ * \return True when available, false if not
+ */
+bool eGL::hasExtension(const char *extensions, const char *name)
+{
+	size_t len = strlen(name);
+	const char *p = extensions;
+
+	if (!extensions)
+		return false;
+
+	// Full match search (separated by spaces or begin/end of string).
+	while ((p = strstr(p, name))) {
+		if ((p == extensions || p[-1] == ' ') &&
+		    (p[len] == ' ' || p[len] == '\0')) {
+			return true;
+		}
+		p += len;
+	}
+
+	return false;
+}
+
+/**
+ * \brief Whether the GLES version is at or above a minimum version
+ * \param[in] major Major version number
+ * \param[in] minor Minor version number
+ *
+ * \return True when at or beyond specified version, false otherwise
+ */
+bool eGL::hasGLESVersion(unsigned int major, unsigned int minor) const
+{
+	return glesMajor_ > major || (glesMajor_ == major && glesMinor_ >= minor);
+}
+
 /**
  * \brief Destroy the EGL context
  *
@@ -468,6 +548,11 @@  void eGL::resetEGLContext()
 		eglDestroyContext(display_, std::exchange(context_, EGL_NO_CONTEXT));
 	}
 
+	eglExtensions_ = NULL;
+	glesExtensions_ = NULL;
+	glesMajor_ = 0;
+	glesMinor_ = 0;
+
 	tid_ = -1;
 	vtable_ = {};
 }