[6/6] software_isp: benchmark: Add GPU benchmarking
diff mbox series

Message ID 20261126110422.362455-7-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
Add a method for benchmarking the time taken by GPU in the softisp EGL
implementation using the EXT_disjoint_timer_query OpenGL extension.

On a Radxa ROCK 5B with Radxa Camera 4K, which uses the imx415 sensor, it
results in the following processing time for 30 frames:
- Debayer     : 45958  us/frame
- Debayer EGL : 17556  us/frame
- CPU stats   : 135974 us/frame

Signed-off-by: Remy Alipoursafari <remy.alipoursafari@collabora.com>
Reviewed-by: Robert Mader <robert.mader@collabora.com>
---
 include/libcamera/internal/egl.h              |  16 +++
 .../internal/software_isp/benchmark_egl.h     |  43 +++++++
 .../internal/software_isp/meson.build         |   1 +
 src/libcamera/egl.cpp                         | 106 ++++++++++++++++++
 src/libcamera/software_isp/benchmark_egl.cpp  |  98 ++++++++++++++++
 src/libcamera/software_isp/debayer_egl.cpp    |  37 +++++-
 src/libcamera/software_isp/debayer_egl.h      |   3 +
 src/libcamera/software_isp/meson.build        |   1 +
 8 files changed, 302 insertions(+), 3 deletions(-)
 create mode 100644 include/libcamera/internal/software_isp/benchmark_egl.h
 create mode 100644 src/libcamera/software_isp/benchmark_egl.cpp

Patch
diff mbox series

diff --git a/include/libcamera/internal/egl.h b/include/libcamera/internal/egl.h
index 6a146405..a3595cd3 100644
--- a/include/libcamera/internal/egl.h
+++ b/include/libcamera/internal/egl.h
@@ -119,6 +119,13 @@  public:
 	void updateTexture2D(eGLImage &eglImage, const void *data);
 	void createOutputTexture2D(eGLImage &eglImage);
 
+	bool timerQueryAvailable();
+	void createTimerQuery(GLuint *id);
+	void deleteTimerQuery(GLuint *id);
+	void beginTimerQuery(GLuint id);
+	void endTimerQuery();
+	int queryTimerQuery(GLuint id, GLuint64 *out);
+
 	int attachTextureToFBO(eGLImage &eglImage);
 	void activateBindTexture(eGLImage &eglImage);
 
@@ -160,10 +167,19 @@  private:
 
 	void makeCurrent();
 
+	bool disjointTimerExtensionAvailable_ = false;
+
 	struct VTable {
 		PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
 		PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
 		PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
+
+		PFNGLGENQUERIESEXTPROC glGenQueriesEXT;
+		PFNGLDELETEQUERIESEXTPROC glDeleteQueriesEXT;
+		PFNGLBEGINQUERYEXTPROC glBeginQueryEXT;
+		PFNGLENDQUERYEXTPROC glEndQueryEXT;
+		PFNGLGETQUERYOBJECTIVEXTPROC glGetQueryObjectivEXT;
+		PFNGLGETQUERYOBJECTUI64VEXTPROC glGetQueryObjectui64vEXT;
 	} vtable_ = {};
 
 	void locateProcAddresses(VTable *vtable);
diff --git a/include/libcamera/internal/software_isp/benchmark_egl.h b/include/libcamera/internal/software_isp/benchmark_egl.h
new file mode 100644
index 00000000..930139fe
--- /dev/null
+++ b/include/libcamera/internal/software_isp/benchmark_egl.h
@@ -0,0 +1,43 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2026, Collabora Inc.
+ *
+ * Authors:
+ * Remy Alipoursafari <remy.alipoursafari@collabora.com>
+ *
+ * Simple builtin benchmark to measure software ISP GPU processing times
+ */
+
+#pragma once
+
+#include <stdint.h>
+
+#include "libcamera/internal/egl.h"
+#include "libcamera/internal/software_isp/benchmark.h"
+
+#include <GLES2/gl2.h>
+
+namespace libcamera {
+
+class BenchmarkEgl : public Benchmark
+{
+public:
+	BenchmarkEgl(const CameraManager &cm, const std::string &name, eGL &egl);
+	~BenchmarkEgl();
+
+	bool hasTimerQuery(void);
+
+	void initTimer(void);
+	void deleteTimer(void);
+
+protected:
+	bool timeFrameStart(void) override;
+	bool timeFrameEnd(void) override;
+	bool timeFrameQuery(void) override;
+
+private:
+	eGL &egl_;
+	GLuint timerQuery_ = 0;
+};
+
+} /* namespace libcamera */
diff --git a/include/libcamera/internal/software_isp/meson.build b/include/libcamera/internal/software_isp/meson.build
index 89161600..48dcfc5a 100644
--- a/include/libcamera/internal/software_isp/meson.build
+++ b/include/libcamera/internal/software_isp/meson.build
@@ -2,6 +2,7 @@ 
 
 libcamera_internal_headers += files([
     'benchmark_cpu.h',
+    'benchmark_egl.h',
     'debayer_params.h',
     'software_isp.h',
     'swisp_stats.h',
diff --git a/src/libcamera/egl.cpp b/src/libcamera/egl.cpp
index df0bba8e..a0f119c5 100644
--- a/src/libcamera/egl.cpp
+++ b/src/libcamera/egl.cpp
@@ -363,6 +363,101 @@  void eGL::createOutputTexture2D(eGLImage &eglImage)
 	attachTextureToFBO(eglImage);
 }
 
+/**
+ * \brief Checks whether timer queries are available
+ *
+ * Checks whether the GL_EXT_disjoint_timer_query extension is available in the
+ * current OpenGL ES context.
+ *
+ * \sa createTimerQuery()
+ */
+bool eGL::timerQueryAvailable()
+{
+	return disjointTimerExtensionAvailable_;
+}
+
+/**
+ * \brief Create a disjoint timer query object
+ * \param[out] id Location to store the disjoint timer query
+ *
+ * Creates a disjoint timer query provided by EXT_disjoint_timer_query for GPU
+ * timing if the extension is available.
+ *
+ * \sa timerQueryAvailble()
+ * \sa deleteTimerQuery()
+ */
+void eGL::createTimerQuery(GLuint *id)
+{
+	if (!timerQueryAvailable())
+		return;
+
+	vtable_.glGenQueriesEXT(1, id);
+}
+
+/**
+ * \brief Deletes a disjoint timer query object
+ * \param[in,out] id The location of the disjoint timer query
+ *
+ * Creates a disjoint timer query provided by EXT_disjoint_timer_query for GPU
+ * timing.
+ *
+ * \sa createTimerQuery()
+ */
+void eGL::deleteTimerQuery(GLuint *id)
+{
+	vtable_.glDeleteQueriesEXT(1, id);
+}
+
+/**
+ * \brief Begins the disjoint timer query
+ * \param[in] id The ID of the disjoint timer query object
+ * \sa createTimerQuery()
+ * \sa endTimerQuery()
+ * \sa queryTimerQuery()
+ */
+void eGL::beginTimerQuery(GLuint id)
+{
+	vtable_.glBeginQueryEXT(GL_TIME_ELAPSED_EXT, id);
+}
+
+/**
+ * \brief Ends the disjoint timer query
+ *
+ * Ends the most recently started non-previously-stopped timer query.
+ *
+ * \sa beginTimerQuery()
+ * \sa queryTimerQuery()
+ */
+void eGL::endTimerQuery()
+{
+	vtable_.glEndQueryEXT(GL_TIME_ELAPSED_EXT);
+}
+
+/**
+ * \brief Gets the result of the timer query
+ * \param[in] id The ID of the disjoint timer query object
+ * \param[out] out Time from beginning query in nanoseconds
+ *
+ * Once the GPU has finished processing the timer query, this gives the GPU
+ * processing time between beginning and ending the timer query.
+ *
+ * \return 0 on success, -1 on failure (called too early?)
+ * \sa beginTimerQuery()
+ * \sa endTimerQuery()
+ */
+int eGL::queryTimerQuery(GLuint id, GLuint64 *out)
+{
+	int available = 0;
+
+	vtable_.glGetQueryObjectivEXT(id, GL_QUERY_RESULT_AVAILABLE, &available);
+	if (!available)
+		return -1;
+
+	vtable_.glGetQueryObjectui64vEXT(id, GL_QUERY_RESULT, out);
+
+	return 0;
+}
+
 /**
  * \brief Initialise the EGL context
  *
@@ -457,6 +552,8 @@  int eGL::initEGLContext()
 	CHECK_EGL_EXTENSION("EGL_EXT_image_dma_buf_import");
 	CHECK_GLES_EXTENSION("GL_OES_EGL_image");
 
+	disjointTimerExtensionAvailable_ = hasGLESExtension("GL_EXT_disjoint_timer_query");
+
 	return 0;
 fail:
 
@@ -550,6 +647,8 @@  void eGL::resetEGLContext()
 	glesMajor_ = 0;
 	glesMinor_ = 0;
 
+	disjointTimerExtensionAvailable_ = false;
+
 	tid_ = -1;
 	vtable_ = {};
 }
@@ -591,6 +690,13 @@  void eGL::locateProcAddresses(VTable *vtable)
 	GET_PROC_ADDRESS(vtable->eglCreateImageKHR, "eglCreateImageKHR", PFNEGLCREATEIMAGEKHRPROC);
 	GET_PROC_ADDRESS(vtable->eglDestroyImageKHR, "eglDestroyImageKHR", PFNEGLDESTROYIMAGEKHRPROC);
 	GET_PROC_ADDRESS(vtable->glEGLImageTargetTexture2DOES, "glEGLImageTargetTexture2DOES", PFNGLEGLIMAGETARGETTEXTURE2DOESPROC);
+
+	GET_PROC_ADDRESS(vtable->glGenQueriesEXT, "glGenQueriesEXT", PFNGLGENQUERIESEXTPROC);
+	GET_PROC_ADDRESS(vtable->glDeleteQueriesEXT, "glDeleteQueriesEXT", PFNGLDELETEQUERIESEXTPROC);
+	GET_PROC_ADDRESS(vtable->glBeginQueryEXT, "glBeginQueryEXT", PFNGLBEGINQUERYEXTPROC);
+	GET_PROC_ADDRESS(vtable->glEndQueryEXT, "glEndQueryEXT", PFNGLENDQUERYEXTPROC);
+	GET_PROC_ADDRESS(vtable->glGetQueryObjectivEXT, "glGetQueryObjectivEXT", PFNGLGETQUERYOBJECTIVEXTPROC);
+	GET_PROC_ADDRESS(vtable->glGetQueryObjectui64vEXT, "glGetQueryObjectui64vEXT", PFNGLGETQUERYOBJECTUI64VEXTPROC);
 }
 
 /**
diff --git a/src/libcamera/software_isp/benchmark_egl.cpp b/src/libcamera/software_isp/benchmark_egl.cpp
new file mode 100644
index 00000000..bfd78c1d
--- /dev/null
+++ b/src/libcamera/software_isp/benchmark_egl.cpp
@@ -0,0 +1,98 @@ 
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2026, Collabora Inc.
+ *
+ * Authors:
+ * Remy Alipoursafari <remy.alipoursafari@collabora.com>
+ *
+ * Simple builtin benchmark to measure software ISP GPU processing times
+ */
+
+#include "libcamera/internal/software_isp/benchmark_egl.h"
+
+#include <libcamera/base/log.h>
+
+namespace libcamera {
+
+LOG_DEFINE_CATEGORY(BenchmarkEgl)
+
+/**
+ * \class BenchmarkEgl
+ * \brief Simple builtin benchmark for mesuring GPU time
+ *
+ * Simple builtin benchmark to measure software ISP GPU processing times.
+ */
+
+/**
+ * \brief Constructs a BenchmarkEgl object
+ */
+BenchmarkEgl::BenchmarkEgl(const CameraManager &cm, const std::string &name, eGL &egl)
+	: Benchmark(cm, name), egl_{ egl }
+{
+}
+
+BenchmarkEgl::~BenchmarkEgl()
+{
+}
+
+/**
+ * \brief Checks if the timer query has been created
+ */
+bool BenchmarkEgl::hasTimerQuery()
+{
+	return timerQuery_ != 0;
+}
+
+/**
+ * \brief Initialises the timer query
+ *
+ * Sets up the disjoint timer query for the current OpenGL context.
+ */
+void BenchmarkEgl::initTimer()
+{
+	egl_.createTimerQuery(&timerQuery_);
+}
+
+/**
+ * \brief Deletes the timer query
+ */
+void BenchmarkEgl::deleteTimer()
+{
+	egl_.deleteTimerQuery(&timerQuery_);
+	timerQuery_ = 0;
+}
+
+/**
+ * \brief Start measuring process time for a single frame
+ */
+bool BenchmarkEgl::timeFrameStart()
+{
+	egl_.beginTimerQuery(timerQuery_);
+	return true;
+}
+
+/**
+ * \brief Finish measuring process time for a single frame
+ */
+bool BenchmarkEgl::timeFrameEnd()
+{
+	egl_.endTimerQuery();
+	return true;
+}
+
+/**
+ * \brief Query the processing time between the timer's start & end
+ */
+bool BenchmarkEgl::timeFrameQuery()
+{
+	GLuint64 elapsed = -1;
+
+	if (egl_.queryTimerQuery(timerQuery_, &elapsed)) {
+		return false;
+	}
+
+	frameProcessTime_ += elapsed;
+	return true;
+}
+
+} /* namespace libcamera */
diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp
index ca99bdc2..fc75968a 100644
--- a/src/libcamera/software_isp/debayer_egl.cpp
+++ b/src/libcamera/software_isp/debayer_egl.cpp
@@ -43,7 +43,7 @@  namespace libcamera {
  * \param[in] display The EGL display to use
  */
 DebayerEGL::DebayerEGL(std::unique_ptr<SwStatsCpu> stats, const CameraManager &cm, EGLDisplay display)
-	: Debayer(cm), stats_(std::move(stats)), egl_(display)
+	: Debayer(cm), benchEgl_(cm, "Debayer EGL", egl_), stats_(std::move(stats)), egl_(display)
 {
 }
 
@@ -51,6 +51,14 @@  DebayerEGL::~DebayerEGL()
 {
 }
 
+/**
+ * \var DebayerEGL::benchEgl_
+ * \brief Benchmarking utility instance for GPU performance measurements
+ *
+ * Used internally to track GPU timing and performance metrics during
+ * debayer processing.
+ */
+
 int DebayerEGL::getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config)
 {
 	BayerFormat bayerFormat =
@@ -606,12 +614,21 @@  int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const Debaye
 	eGLImage *eglImageIn;
 	eGLImage *eglImageOut;
 
+	if (benchEgl_.hasTimerQuery())
+		benchEgl_.startFrame();
+
 	eglImageIn = getCachedInputFrameBuffer(input, inMapped, inDmaSyncer);
-	if (!eglImageIn)
+	if (!eglImageIn) {
+		if (benchEgl_.hasTimerQuery())
+			benchEgl_.finishFrame();
 		return -ENOMEM;
+	}
 	eglImageOut = getCachedOutputFrameBuffer(output);
-	if (!eglImageOut)
+	if (!eglImageOut) {
+		if (benchEgl_.hasTimerQuery())
+			benchEgl_.finishFrame();
 		return -ENOMEM;
+	}
 
 	egl_.attachTextureToFBO(*eglImageOut);
 	setShaderVariableValues(*eglImageIn, params);
@@ -620,6 +637,9 @@  int DebayerEGL::debayerGPU(FrameBuffer *input, FrameBuffer *output, const Debaye
 	glClear(GL_COLOR_BUFFER_BIT);
 	glDrawArrays(GL_TRIANGLE_FAN, 0, DEBAYER_OPENGL_COORDS);
 
+	if (benchEgl_.hasTimerQuery())
+		benchEgl_.finishFrame();
+
 	GLenum err = glGetError();
 	if (err != GL_NO_ERROR) {
 		LOG(eGL, Error) << "Drawing scene fail " << err;
@@ -674,6 +694,9 @@  void DebayerEGL::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output
 	bench_.finishFrame();
 	bench_.queryFrame();
 
+	if (benchEgl_.hasTimerQuery())
+		benchEgl_.queryFrame();
+
 	outputBufferReady.emit(output);
 	inputBufferReady.emit(input);
 
@@ -682,6 +705,8 @@  void DebayerEGL::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output
 error:
 	bench_.finishFrame();
 	bench_.queryFrame();
+	if (benchEgl_.hasTimerQuery())
+		benchEgl_.queryFrame();
 	metadata.status = FrameMetadata::FrameError;
 	return;
 }
@@ -700,6 +725,9 @@  int DebayerEGL::start()
 	if (initBayerShaders(inputPixelFormat_, outputPixelFormat_))
 		return -EINVAL;
 
+	if (egl_.timerQueryAvailable())
+		benchEgl_.initTimer();
+
 	if (lscEnabled_) {
 		constexpr unsigned int gridSize = DebayerParams::kLscGridSize;
 		constexpr unsigned int stride = gridSize * DebayerParams::kLscBytesPerCell;
@@ -723,6 +751,9 @@  void DebayerEGL::stop()
 	eglImageLscLookup_.reset();
 	lscLutVersion_.reset();
 
+	if (benchEgl_.hasTimerQuery())
+		benchEgl_.deleteTimer();
+
 	if (programId_)
 		glDeleteProgram(programId_);
 
diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h
index 4185d32b..140d20fa 100644
--- a/src/libcamera/software_isp/debayer_egl.h
+++ b/src/libcamera/software_isp/debayer_egl.h
@@ -21,6 +21,7 @@ 
 
 #include "libcamera/internal/egl.h"
 #include "libcamera/internal/mapped_framebuffer.h"
+#include "libcamera/internal/software_isp/benchmark_egl.h"
 #include "libcamera/internal/software_isp/swstats_cpu.h"
 
 #include <EGL/egl.h>
@@ -61,6 +62,8 @@  public:
 
 	SizeRange sizes(PixelFormat inputFormat, const Size &inputSize) override;
 
+	BenchmarkEgl benchEgl_;
+
 private:
 	static int getInputConfig(PixelFormat inputFormat, DebayerInputConfig &config);
 	int initBayerShaders(PixelFormat inputFormat, PixelFormat outputFormat);
diff --git a/src/libcamera/software_isp/meson.build b/src/libcamera/software_isp/meson.build
index f6ee6700..6676db6f 100644
--- a/src/libcamera/software_isp/meson.build
+++ b/src/libcamera/software_isp/meson.build
@@ -32,6 +32,7 @@  if mesa_works
     config_h.set('HAVE_DEBAYER_EGL', 1)
     libcamera_internal_sources += files([
         '../egl.cpp',
+        'benchmark_egl.cpp',
         'debayer_egl.cpp',
     ])
     libcamera_deps += [