[v2] software_isp: debayer_egl: Request input buffer alignment of 256 bytes
diff mbox series

Message ID 20260604101448.45378-1-robert.mader@collabora.com
State New
Headers show
Series
  • [v2] software_isp: debayer_egl: Request input buffer alignment of 256 bytes
Related show

Commit Message

Robert Mader June 4, 2026, 10:14 a.m. UTC
The most common reason dmabuf import with createInputDMABufTexture2D()
fails is that V4L2 drivers use stride alignments that are not sufficient
(too small) for GPUs, preventing the later from directly using input
buffers and forcing us to do relatively expensive uploads (i.e. copies)
of the buffer contents.

Thus let's request a compatible stride alignment from the V4L2 driver,
which may or may not respect the value. From the v4l2_pix_format ->
bytesperline docs:
> Both applications and drivers can set this field to request padding
> bytes at the end of each line. Drivers however may ignore the value
> requested by the application, returning width times bytes per pixel
> or a larger value required by the hardware. That implies applications
> can just set this field to zero to get a reasonable default.

The approach works as follows:
1. Before setting the input format, query into the debayer class for a
   preferred stride based on the bayer format and width. For DebayerCpu
   this returns 0, i.e. let the V4L2 driver decide, while for DebayerEGL
   the "magic" stride alignment of 256 bytes is used, which is known
   to work with all known/common GPUs and already used for output
   buffers. Right now there doesn't exist any API to query the correct
   value - in many cases it is 64 or 128 - however if e.g. a new EGL
   extension will provide it in the future, we can easily implement it
   in the new function. Alternatively a config value or env var could be
   added there if the need arises.
2. Pass the calculated stride to V4L2VideoDevice::setFormat(), which
   again sets it as the bytesperline value.
3. The V4L2 driver will update bytesperline with the value it actually
   chose to use. Compare the requested and actual strides and log a info
   message if the driver did not honor the request, hinting users at what
   could be improved about the kernel driver.

Signed-off-by: Robert Mader <robert.mader@collabora.com>

---

Changes in V2:
 - Updated the commit message to be much more verbose and hopefully easy
   to understand.
 - Minor cleanups and fixes for docs and CI.

Previous version: https://patchwork.libcamera.org/patch/26810/
---
 .../libcamera/internal/software_isp/software_isp.h  |  1 +
 src/libcamera/pipeline/simple/simple.cpp            | 12 ++++++++++++
 src/libcamera/software_isp/debayer.cpp              |  8 ++++++++
 src/libcamera/software_isp/debayer.h                |  1 +
 src/libcamera/software_isp/debayer_egl.cpp          |  7 +++++++
 src/libcamera/software_isp/debayer_egl.h            |  1 +
 src/libcamera/software_isp/software_isp.cpp         | 13 +++++++++++++
 7 files changed, 43 insertions(+)

Comments

Robert Mader June 4, 2026, 10:42 a.m. UTC | #1
On 04.06.26 12:14, Robert Mader wrote:
> ...
> diff --git a/include/libcamera/internal/software_isp/software_isp.h b/include/libcamera/internal/software_isp/software_isp.h
> index 86cb8f8de..7440937c2 100644
> --- a/include/libcamera/internal/software_isp/software_isp.h
> +++ b/include/libcamera/internal/software_isp/software_isp.h
> @@ -61,6 +61,7 @@ public:
>   
>   	std::tuple<unsigned int, unsigned int>
>   	strideAndFrameSize(const PixelFormat &outputFormat, const Size &size);
> +	uint32_t getPreferredInputStride(const PixelFormat &inputFormat, const Size &size);
>   
>   	int configure(const StreamConfiguration &inputCfg,
>   		      const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
Just figured that I should probably drop the "get", i.e. 
"preferredInputStride()" instead of "getPreferredInputStride()". Will do 
that in v3 (but first wait for further feedback).

Patch
diff mbox series

diff --git a/include/libcamera/internal/software_isp/software_isp.h b/include/libcamera/internal/software_isp/software_isp.h
index 86cb8f8de..7440937c2 100644
--- a/include/libcamera/internal/software_isp/software_isp.h
+++ b/include/libcamera/internal/software_isp/software_isp.h
@@ -61,6 +61,7 @@  public:
 
 	std::tuple<unsigned int, unsigned int>
 	strideAndFrameSize(const PixelFormat &outputFormat, const Size &size);
+	uint32_t getPreferredInputStride(const PixelFormat &inputFormat, const Size &size);
 
 	int configure(const StreamConfiguration &inputCfg,
 		      const std::vector<std::reference_wrapper<const StreamConfiguration>> &outputCfgs,
diff --git a/src/libcamera/pipeline/simple/simple.cpp b/src/libcamera/pipeline/simple/simple.cpp
index c6fe12d65..0de2fe695 100644
--- a/src/libcamera/pipeline/simple/simple.cpp
+++ b/src/libcamera/pipeline/simple/simple.cpp
@@ -1542,6 +1542,11 @@  int SimplePipelineHandler::configure(Camera *camera, CameraConfiguration *c)
 	captureFormat.fourcc = videoFormat;
 	captureFormat.size = pipeConfig->captureSize;
 
+	uint32_t requested_bpl = 0;
+	if (data->swIsp_)
+		requested_bpl = data->swIsp_->getPreferredInputStride(videoFormat.toPixelFormat(), pipeConfig->captureSize);
+	captureFormat.planes[0].bpl = requested_bpl;
+
 	ret = video->setFormat(&captureFormat);
 	if (ret)
 		return ret;
@@ -1561,6 +1566,13 @@  int SimplePipelineHandler::configure(Camera *camera, CameraConfiguration *c)
 		return -EINVAL;
 	}
 
+	if (requested_bpl && captureFormat.planes[0].bpl != requested_bpl) {
+		LOG(SimplePipeline, Info)
+			<< "Input buffer stride ignored by the driver. "
+			<< "Requested " << requested_bpl
+			<< ", got " << captureFormat.planes[0].bpl;
+	}
+
 	/* Configure the converter if needed. */
 	std::vector<std::reference_wrapper<const StreamConfiguration>> outputCfgs;
 	data->useConversion_ = config->needConversion();
diff --git a/src/libcamera/software_isp/debayer.cpp b/src/libcamera/software_isp/debayer.cpp
index 2d7abfb83..befed51df 100644
--- a/src/libcamera/software_isp/debayer.cpp
+++ b/src/libcamera/software_isp/debayer.cpp
@@ -103,6 +103,14 @@  Debayer::~Debayer()
  * there is no valid output config
  */
 
+/**
+ * \fn uint32_t Debayer::getPreferredInputStride(const PixelFormat &inputFormat, const Size &size)
+ * Get the preferred input stride in bytes for the given input format and size
+ * \param[in] inputFormat The input format
+ * \param[in] size The input size (width and height in pixels)
+ * \return The preferred input stride in bytes or 0 if there is no preference
+ */
+
 /**
  * \fn void Debayer::process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, DebayerParams params)
  * \brief Process the bayer data into the requested format
diff --git a/src/libcamera/software_isp/debayer.h b/src/libcamera/software_isp/debayer.h
index a2a17ec18..ddb4988a8 100644
--- a/src/libcamera/software_isp/debayer.h
+++ b/src/libcamera/software_isp/debayer.h
@@ -46,6 +46,7 @@  public:
 
 	virtual std::tuple<unsigned int, unsigned int>
 	strideAndFrameSize(const PixelFormat &outputFormat, const Size &size) = 0;
+	virtual uint32_t getPreferredInputStride([[maybe_unused]] const PixelFormat &inputFormat, [[maybe_unused]] const Size &size) { return 0; }
 
 	virtual void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params) = 0;
 	virtual int start() { return 0; }
diff --git a/src/libcamera/software_isp/debayer_egl.cpp b/src/libcamera/software_isp/debayer_egl.cpp
index 99825d49e..fc995cd0c 100644
--- a/src/libcamera/software_isp/debayer_egl.cpp
+++ b/src/libcamera/software_isp/debayer_egl.cpp
@@ -21,6 +21,7 @@ 
 
 #include <libcamera/formats.h>
 
+#include "libcamera/internal/formats.h"
 #include "libcamera/internal/framebuffer.h"
 
 #include "../glsl_shaders.h"
@@ -378,6 +379,12 @@  DebayerEGL::strideAndFrameSize(const PixelFormat &outputFormat, const Size &size
 	return std::make_tuple(stride, stride * size.height);
 }
 
+uint32_t DebayerEGL::getPreferredInputStride(const PixelFormat &inputFormat, const Size &size)
+{
+	const PixelFormatInfo &info = PixelFormatInfo::info(inputFormat);
+	return info.stride(size.width, 0, 256);
+}
+
 void DebayerEGL::setShaderVariableValues(const DebayerParams &params)
 {
 	/*
diff --git a/src/libcamera/software_isp/debayer_egl.h b/src/libcamera/software_isp/debayer_egl.h
index 875e7cfc5..0f673e82f 100644
--- a/src/libcamera/software_isp/debayer_egl.h
+++ b/src/libcamera/software_isp/debayer_egl.h
@@ -50,6 +50,7 @@  public:
 
 	std::vector<PixelFormat> formats(PixelFormat input) override;
 	std::tuple<unsigned int, unsigned int> strideAndFrameSize(const PixelFormat &outputFormat, const Size &size) override;
+	uint32_t getPreferredInputStride(const PixelFormat &inputFormat, const Size &size) override;
 
 	void process(uint32_t frame, FrameBuffer *input, FrameBuffer *output, const DebayerParams &params) override;
 	int start() override;
diff --git a/src/libcamera/software_isp/software_isp.cpp b/src/libcamera/software_isp/software_isp.cpp
index 781cf02f8..b54233fb0 100644
--- a/src/libcamera/software_isp/software_isp.cpp
+++ b/src/libcamera/software_isp/software_isp.cpp
@@ -255,6 +255,19 @@  SoftwareIsp::strideAndFrameSize(const PixelFormat &outputFormat, const Size &siz
 	return debayer_->strideAndFrameSize(outputFormat, size);
 }
 
+/**
+ * Get the preferred input stride in bytes for the given input format and size
+ * \param[in] inputFormat The input format
+ * \param[in] size The input size (width and height in pixels)
+ * \return The preferred input stride in bytes or 0 if there is no preference
+ */
+uint32_t SoftwareIsp::getPreferredInputStride(const PixelFormat &inputFormat, const Size &size)
+{
+	ASSERT(debayer_);
+
+	return debayer_->getPreferredInputStride(inputFormat, size);
+}
+
 /**
  * \brief Configure the SoftwareIsp object according to the passed in parameters
  * \param[in] inputCfg The input configuration