Re: [PATCH 03/16] media: rockchip: rga: align stride to 16 bytes
From: Sven Püschel
Date: Tue Dec 02 2025 - 09:37:14 EST
Hi Nicolas,
On 10/7/25 8:19 PM, Nicolas Dufresne wrote:
Hi,
Le mardi 07 octobre 2025 à 10:31 +0200, Sven Püschel a écrit :
Align the stride to a multiple of 16 according to the RGA3 requirements
mentioned in the datasheet. This also ensures that the stride of the RGA2
is aligned to 4 bytes, as it needs to divide the value by 4 (one word)
before storing it in the register.
Increasing the stride for the alignment also requires to increase the
sizeimage value. This is usually handled by v4l2_fill_pixfmt_mp, but
it doesn't allow to set a stride alignment. Therefore use the generated
values to calculate the total number of lines to properly update the
sizeimage value after the bytesperline has been aligned.
Signed-off-by: Sven Püschel <s.pueschel@xxxxxxxxxxxxxx>
---
drivers/media/platform/rockchip/rga/rga.c | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/drivers/media/platform/rockchip/rga/rga.c
b/drivers/media/platform/rockchip/rga/rga.c
index
6438119a6c7aeff1e89e7aa95dcd5d2921fefa08..3cb7ce470c47e39d694e8176875a75fad271
7f96 100644
--- a/drivers/media/platform/rockchip/rga/rga.c
+++ b/drivers/media/platform/rockchip/rga/rga.c
@@ -459,6 +459,25 @@ static int vidioc_enum_fmt(struct file *file, void *priv,
struct v4l2_fmtdesc *f
return 0;
}
+static void align_pixfmt(struct v4l2_pix_format_mplane *pix_fmt)
+{
+ int lines;
+ struct v4l2_plane_pix_format *fmt;
+
+ /*
+ * Align stride to 16 for the RGA3 (based on the datasheet)
+ * To not dismiss the v4l2_fill_pixfmt_mp helper
+ * (and manually write it again), we're approximating the new
sizeimage
+ */
+ for (fmt = pix_fmt->plane_fmt;
+ fmt < pix_fmt->plane_fmt + pix_fmt->num_planes;
+ fmt++) {
+ lines = DIV_ROUND_UP(fmt->sizeimage, fmt->bytesperline);
+ fmt->bytesperline = (fmt->bytesperline + 0xf) & ~0xf;
+ fmt->sizeimage = fmt->bytesperline * lines;
Instead of open coding this, describe this with struct v4l2_frmsize_stepwise and
then use v4l2_apply_frmsize_constraints().
Looking into v4l2_frmsize_stepwise, it only applies to the width/height
values, whereas I'm interested to control the stride
byte-alignment/stepping (allowing free range widths/heights).
Do you intent that I (mis)use it like this:
v4l2_apply_frmsize_constraints(&fmt->bytesperline, &lines, &constraints) ?
This only replaces one of the three lines in the for loop (where i
manually do the alignment with & ~0xf) and would also look odd, as i use
a value in bytes as a width parameter (expected to be in pixels).
Technically the core problem is that the v4l2_fill_pixfmt_mp helper
doesn't allow to specify a stride byte alignment and just aligns the
stride based on the format requirements. Therefore I think that maybe
creating a new v4l2_fill_pixfmt_mp_aligned helper would probably be the
better solution to get the correct bytesperline value right from the
start instead of adjusting it afterwards.
Sincerely
Sven
Nicolas
+ }
+}
+
static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
{
struct v4l2_pix_format_mplane *pix_fmt = &f->fmt.pix_mp;
@@ -474,6 +493,7 @@ static int vidioc_g_fmt(struct file *file, void *priv,
struct v4l2_format *f)
return PTR_ERR(frm);
v4l2_fill_pixfmt_mp(pix_fmt, frm->fmt->fourcc, frm->width, frm-
height);
+ align_pixfmt(pix_fmt);
pix_fmt->field = V4L2_FIELD_NONE;
pix_fmt->colorspace = frm->colorspace;
@@ -496,6 +516,7 @@ static int vidioc_try_fmt(struct file *file, void *priv,
struct v4l2_format *f)
(u32)MIN_HEIGHT, (u32)MAX_HEIGHT);
v4l2_fill_pixfmt_mp(pix_fmt, fmt->fourcc, pix_fmt->width, pix_fmt-
height);
+ align_pixfmt(pix_fmt);
pix_fmt->field = V4L2_FIELD_NONE;
return 0;