Skip to contentSkip to conversation

drm/format-helper: Make conversion_buf_size() support sub-byte pixel fmts

PATCH Applied Javier Martinez Canillas 4 participants 6 messages Mar 7, 2023 – Mar 16, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 drm/format-helper: Make conversion_buf_size() support sub-byte pixel fmts

There are DRM fourcc formats that have pixels smaller than a byte, but the conversion_buf_size() function assumes that pixels are a multiple of bytes and use the struct drm_format_info .cpp field to calculate the dst_pitch.

Instead, calculate it by using the bits per pixel (bpp) and divide it by 8 to account for formats that have sub-byte pixels.

---
Tested by making sure that the following command still succeeds:

./tools/testing/kunit/kunit.py run \
--kunitconfig=drivers/gpu/drm/tests/.kunitconfig

Changes in v2:
- Drop an unused variable, that was pointed out by the kernel robot.

 drivers/gpu/drm/tests/drm_format_helper_test.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +5 -2
index 9536829c6e3a..84b5cc29c8fc 100644
@@ -409,12 +409,15 @@ static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
 				  const struct drm_rect *clip)
 {
 	const struct drm_format_info *dst_fi = drm_format_info(dst_format);
+	unsigned int bpp;
 
 	if (!dst_fi)
 		return -EINVAL;
 
-	if (!dst_pitch)
-		dst_pitch = drm_rect_width(clip) * dst_fi->cpp[0];
+	if (!dst_pitch) {
+		bpp = drm_format_info_bpp(dst_fi, 0);
+		dst_pitch = DIV_ROUND_UP(drm_rect_width(clip) * bpp, 8);
+	}
 
 	return dst_pitch * drm_rect_height(clip);
 }
Signature · 1 line
2.39.2
Reviewed-by on v2 1/1
#1 lore raw

Hi,

Quoted 32 lines · Javier Martinez Canillas

There are DRM fourcc formats that have pixels smaller than a byte, but the conversion_buf_size() function assumes that pixels are a multiple of bytes and use the struct drm_format_info .cpp field to calculate the dst_pitch.

Instead, calculate it by using the bits per pixel (bpp) and divide it by 8 to account for formats that have sub-byte pixels.

---
Tested by making sure that the following command still succeeds:

./tools/testing/kunit/kunit.py run \
--kunitconfig=drivers/gpu/drm/tests/.kunitconfig

Changes in v2:
- Drop an unused variable, that was pointed out by the kernel robot.

 drivers/gpu/drm/tests/drm_format_helper_test.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -0
index 9536829c6e3a..84b5cc29c8fc 100644
@@ -409,12 +409,15 @@ static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
 				  const struct drm_rect *clip)
 {
 	const struct drm_format_info *dst_fi = drm_format_info(dst_format);
+	unsigned int bpp;
 
 	if (!dst_fi)
 		return -EINVAL;

drivers/gpu/drm/tests/drm_format_helper_test.c static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch, · line 423

Quoted 10 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +4 -2
 
-	if (!dst_pitch)
-		dst_pitch = drm_rect_width(clip) * dst_fi->cpp[0];
+	if (!dst_pitch) {
+		bpp = drm_format_info_bpp(dst_fi, 0);
+		dst_pitch = DIV_ROUND_UP(drm_rect_width(clip) * bpp, 8);
+	}
 
 	return dst_pitch * drm_rect_height(clip);
 }

Ran it on UML, arm and powerpc with my patch above it, All looks good :).

Thanks,
Grillo

Reviewed-by on v2 1/1
#2 lore raw
In reply to Javier Martinez Canillas
Quoted 8 lines · Javier Martinez Canillas

There are DRM fourcc formats that have pixels smaller than a byte, but the conversion_buf_size() function assumes that pixels are a multiple of bytes and use the struct drm_format_info .cpp field to calculate the dst_pitch.

Instead, calculate it by using the bits per pixel (bpp) and divide it by 8 to account for formats that have sub-byte pixels.

Best Regards,
- Maíra Canal

Quoted 34 lines

---
Tested by making sure that the following command still succeeds:

./tools/testing/kunit/kunit.py run \
--kunitconfig=drivers/gpu/drm/tests/.kunitconfig

Changes in v2:
- Drop an unused variable, that was pointed out by the kernel robot.

  drivers/gpu/drm/tests/drm_format_helper_test.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/gpu/drm/tests/drm_format_helper_test.c +5 -2
index 9536829c6e3a..84b5cc29c8fc 100644
@@ -409,12 +409,15 @@ static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
  				  const struct drm_rect *clip)
  {
  	const struct drm_format_info *dst_fi = drm_format_info(dst_format);
+	unsigned int bpp;
 
  	if (!dst_fi)
  		return -EINVAL;
 
-	if (!dst_pitch)
-		dst_pitch = drm_rect_width(clip) * dst_fi->cpp[0];
+	if (!dst_pitch) {
+		bpp = drm_format_info_bpp(dst_fi, 0);
+		dst_pitch = DIV_ROUND_UP(drm_rect_width(clip) * bpp, 8);
+	}
 
  	return dst_pitch * drm_rect_height(clip);
  }
#3 lore raw
In reply to Javier Martinez Canillas
Quoted 9 lines · Javier Martinez Canillas

There are DRM fourcc formats that have pixels smaller than a byte, but the conversion_buf_size() function assumes that pixels are a multiple of bytes and use the struct drm_format_info .cpp field to calculate the dst_pitch.

Instead, calculate it by using the bits per pixel (bpp) and divide it by 8 to account for formats that have sub-byte pixels.

---

Pushed to drm-misc (drm-misc-next). Thanks!

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#4 lore raw
In reply to Javier Martinez Canillas

Hi Javier,

Quoted 8 lines · Javier Martinez Canillas

There are DRM fourcc formats that have pixels smaller than a byte, but the conversion_buf_size() function assumes that pixels are a multiple of bytes and use the struct drm_format_info .cpp field to calculate the dst_pitch.

Instead, calculate it by using the bits per pixel (bpp) and divide it by 8 to account for formats that have sub-byte pixels.

Quoted 8 lines

---
Tested by making sure that the following command still succeeds:

./tools/testing/kunit/kunit.py run \
--kunitconfig=drivers/gpu/drm/tests/.kunitconfig

Changes in v2:
- Drop an unused variable, that was pointed out by the kernel robot.

Thanks for your patch!

Quoted 6 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +0 -0
@@ -409,12 +409,15 @@ static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
                                  const struct drm_rect *clip)
 {
        const struct drm_format_info *dst_fi = drm_format_info(dst_format);

drivers/gpu/drm/tests/drm_format_helper_test.c static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch, · line 419

Quoted 10 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +4 -2
+       unsigned int bpp;
 
        if (!dst_fi)
                return -EINVAL;
 
-       if (!dst_pitch)
-               dst_pitch = drm_rect_width(clip) * dst_fi->cpp[0];
+       if (!dst_pitch) {
+               bpp = drm_format_info_bpp(dst_fi, 0);
+               dst_pitch = DIV_ROUND_UP(drm_rect_width(clip) * bpp, 8);

I know I'm a bit late to the party, but here's actually a helper for that:

    dst_pitch = drm_format_info_min_pitch(info, 0, drm_rect_width(clip));

drivers/gpu/drm/tests/drm_format_helper_test.c static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch, · line 423

Quoted 4 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -0
+       }
 
        return dst_pitch * drm_rect_height(clip);
 }

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#5 lore raw

Geert Uytterhoeven <[email protected]> writes:

Hello Geert,

[...]

Quoted 8 lines

drivers/gpu/drm/tests/drm_format_helper_test.c static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch, · line 419

Quoted 3 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +3 -0
+       if (!dst_pitch) {
+               bpp = drm_format_info_bpp(dst_fi, 0);
+               dst_pitch = DIV_ROUND_UP(drm_rect_width(clip) * bpp, 8);

I know I'm a bit late to the party, but here's actually a helper for that:

    dst_pitch = drm_format_info_min_pitch(info, 0, drm_rect_width(clip));

Nice, I didn't notice this. I'll send a follow-up patch. Thanks!

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat