diff mbox series

[RFC,libdrm,2/2] util: Fix 32 bpp patterns on big-endian

Message ID 1be5820fe86d6940aab8acd52a522d39a35995c3.1646684158.git.geert@linux-m68k.org (mailing list archive)
State New, archived
Headers show
Series Big-endian fixes | expand

Commit Message

Geert Uytterhoeven March 7, 2022, 8:53 p.m. UTC
DRM formats are defined to be little-endian, unless the
DRM_FORMAT_BIG_ENDIAN flag is set.  Hence writes of multi-byte pixel
values need to take endianness into account.

Introduce a cpu_to_le32() helper to convert 32-bit values from
CPU-endian to little-endian, and use them in the various pattern fill
functions for 32-bit formats.

Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
---
Works now with Linux' drm_fb_xrgb8888_to_rgb332_line(), which uses
le32_to_cpu() to read pixel values from memory.

---
 tests/util/pattern.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

Comments

Pekka Paalanen March 8, 2022, 9:29 a.m. UTC | #1
On Mon,  7 Mar 2022 21:53:42 +0100
Geert Uytterhoeven <geert@linux-m68k.org> wrote:

> DRM formats are defined to be little-endian, unless the
> DRM_FORMAT_BIG_ENDIAN flag is set.  Hence writes of multi-byte pixel
> values need to take endianness into account.
> 
> Introduce a cpu_to_le32() helper to convert 32-bit values from
> CPU-endian to little-endian, and use them in the various pattern fill
> functions for 32-bit formats.

Hi Geert,

FWIW, this explanation matches my understanding, so it sounds correct
to me. That's all I can say. I guess that means

Acked-by: Pekka Paalanen <pekka.paalanen@collabora.com>

?


Thanks,
pq

> 
> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
> ---
> Works now with Linux' drm_fb_xrgb8888_to_rgb332_line(), which uses
> le32_to_cpu() to read pixel values from memory.
> 
> ---
>  tests/util/pattern.c | 30 +++++++++++++++++++++---------
>  1 file changed, 21 insertions(+), 9 deletions(-)
> 
> diff --git a/tests/util/pattern.c b/tests/util/pattern.c
> index 42d75d700700dc3d..48677ea6d25b2676 100644
> --- a/tests/util/pattern.c
> +++ b/tests/util/pattern.c
> @@ -61,6 +61,18 @@ struct color_yuv {
>  	  .u = MAKE_YUV_601_U(r, g, b), \
>  	  .v = MAKE_YUV_601_V(r, g, b) }
>  
> +#if defined(__BIG_ENDIAN__) || defined(__sparc__) || defined(__mc68000__) || defined(__MIPSEB__)
> +static inline uint32_t cpu_to_le32(uint32_t x)
> +{
> +	return ((x & 0x000000ffU) << 24) |
> +	       ((x & 0x0000ff00U) <<  8) |
> +	       ((x & 0x00ff0000U) >>  8) |
> +	       ((x & 0xff000000U) >> 24);
> +}
> +#else
> +#define cpu_to_le32(x)	(x)
> +#endif
diff mbox series

Patch

diff --git a/tests/util/pattern.c b/tests/util/pattern.c
index 42d75d700700dc3d..48677ea6d25b2676 100644
--- a/tests/util/pattern.c
+++ b/tests/util/pattern.c
@@ -61,6 +61,18 @@  struct color_yuv {
 	  .u = MAKE_YUV_601_U(r, g, b), \
 	  .v = MAKE_YUV_601_V(r, g, b) }
 
+#if defined(__BIG_ENDIAN__) || defined(__sparc__) || defined(__mc68000__) || defined(__MIPSEB__)
+static inline uint32_t cpu_to_le32(uint32_t x)
+{
+	return ((x & 0x000000ffU) << 24) |
+	       ((x & 0x0000ff00U) <<  8) |
+	       ((x & 0x00ff0000U) >>  8) |
+	       ((x & 0xff000000U) >> 24);
+}
+#else
+#define cpu_to_le32(x)	(x)
+#endif
+
 /* This function takes 8-bit color values */
 static inline uint32_t shiftcolor8(const struct util_color_component *comp,
 				  uint32_t value)
@@ -520,26 +532,26 @@  static void fill_smpte_rgb32(const struct util_rgb_info *rgb, void *mem,
 
 	for (y = 0; y < height * 6 / 9; ++y) {
 		for (x = 0; x < width; ++x)
-			((uint32_t *)mem)[x] = colors_top[x * 7 / width];
+			((uint32_t *)mem)[x] = cpu_to_le32(colors_top[x * 7 / width]);
 		mem += stride;
 	}
 
 	for (; y < height * 7 / 9; ++y) {
 		for (x = 0; x < width; ++x)
-			((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
+			((uint32_t *)mem)[x] = cpu_to_le32(colors_middle[x * 7 / width]);
 		mem += stride;
 	}
 
 	for (; y < height; ++y) {
 		for (x = 0; x < width * 5 / 7; ++x)
 			((uint32_t *)mem)[x] =
-				colors_bottom[x * 4 / (width * 5 / 7)];
+				cpu_to_le32(colors_bottom[x * 4 / (width * 5 / 7)]);
 		for (; x < width * 6 / 7; ++x)
 			((uint32_t *)mem)[x] =
-				colors_bottom[(x - width * 5 / 7) * 3
-					      / (width / 7) + 4];
+				cpu_to_le32(colors_bottom[(x - width * 5 / 7) * 3
+							  / (width / 7) + 4]);
 		for (; x < width; ++x)
-			((uint32_t *)mem)[x] = colors_bottom[7];
+			((uint32_t *)mem)[x] = cpu_to_le32(colors_bottom[7]);
 		mem += stride;
 	}
 }
@@ -1017,7 +1029,7 @@  static void fill_tiles_rgb32(const struct util_format_info *info, void *mem,
 					  (rgb32 >> 8) & 0xff, rgb32 & 0xff,
 					  alpha);
 
-			((uint32_t *)mem)[x] = color;
+			((uint32_t *)mem)[x] = cpu_to_le32(color);
 		}
 		mem += stride;
 	}
@@ -1164,7 +1176,7 @@  static void fill_gradient_rgb32(const struct util_rgb_info *rgb,
 
 		for (j = 0; j < width / 2; j++) {
 			uint32_t value = MAKE_RGBA10(rgb, j & 0x3ff, j & 0x3ff, j & 0x3ff, 0);
-			row[2*j] = row[2*j+1] = value;
+			row[2*j] = row[2*j+1] = cpu_to_le32(value);
 		}
 		mem += stride;
 	}
@@ -1174,7 +1186,7 @@  static void fill_gradient_rgb32(const struct util_rgb_info *rgb,
 
 		for (j = 0; j < width / 2; j++) {
 			uint32_t value = MAKE_RGBA10(rgb, j & 0x3fc, j & 0x3fc, j & 0x3fc, 0);
-			row[2*j] = row[2*j+1] = value;
+			row[2*j] = row[2*j+1] = cpu_to_le32(value);
 		}
 		mem += stride;
 	}