Skip to contentSkip to conversation

drm/format_helper: Add Kunit tests for drm_fb_xrgb8888_to_mono()

PATCH Superseded Arthur Grillo 2 participants 6 messages Mar 2, 2023 – Mar 8, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/format_helper: Add Kunit tests for drm_fb_xrgb8888_to_mono()

Extend the existing test cases to test the conversion from XRGB8888 to monochromatic.

---
 .../gpu/drm/tests/drm_format_helper_test.c    | 73 +++++++++++++++++++
 1 file changed, 73 insertions(+)
drivers/gpu/drm/tests/drm_format_helper_test.c +73 -0
index 9536829c6e3a..0610341e7349 100644
@@ -67,6 +67,11 @@ struct convert_to_argb2101010_result {
 	const u32 expected[TEST_BUF_SIZE];
 };
 
+struct convert_to_mono_result {
+	unsigned int dst_pitch;
+	const u8 expected[TEST_BUF_SIZE];
+};
+
 struct convert_xrgb8888_case {
 	const char *name;
 	unsigned int pitch;
@@ -82,6 +87,7 @@ struct convert_xrgb8888_case {
 	struct convert_to_argb8888_result argb8888_result;
 	struct convert_to_xrgb2101010_result xrgb2101010_result;
 	struct convert_to_argb2101010_result argb2101010_result;
+	struct convert_to_mono_result mono_result;
 };
 
 static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
@@ -131,6 +137,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		.name = "single_pixel_clip_rectangle",
@@ -181,6 +191,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		/* Well known colors: White, black, red, green, blue, magenta,
@@ -293,6 +307,15 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xFFFFFC00, 0xC00FFFFF,
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x01,
+				0x02,
+				0x00,
+				0x03,
+			},
+		},
 	},
 	{
 		/* Randomly picked colors. Full buffer within the clip area. */
@@ -392,6 +415,14 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xEA20300C, 0xDB1705CD, 0xC3844672, 0x00000000, 0x00000000,
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x00,
+				0x00,
+				0x00,
+			},
+		},
 	},
 };
 
@@ -419,6 +450,17 @@ static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
 	return dst_pitch * drm_rect_height(clip);
 }
 
+static size_t conversion_buf_size_mono(unsigned int dst_pitch, const struct drm_rect *clip)
+{
+	if (!dst_pitch) {
+		unsigned int linepixels = drm_rect_width(clip) * 1;
+
+		dst_pitch = DIV_ROUND_UP(linepixels, 8);
+	}
+
+	return dst_pitch * drm_rect_height(clip);
+}
+
 static u16 *le16buf_to_cpu(struct kunit *test, const __le16 *buf, size_t buf_size)
 {
 	u16 *dst = NULL;
@@ -789,6 +831,36 @@ static void drm_test_fb_xrgb8888_to_argb2101010(struct kunit *test)
 	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
 }
 
+static void drm_test_fb_xrgb8888_to_mono(struct kunit *test)
+{
+	const struct convert_xrgb8888_case *params = test->param_value;
+	const struct convert_to_mono_result *result = &params->mono_result;
+	size_t dst_size;
+	u8 *buf = NULL;
+	__le32 *xrgb8888 = NULL;
+	struct iosys_map dst, src;
+
+	struct drm_framebuffer fb = {
+		.format = drm_format_info(DRM_FORMAT_XRGB8888),
+		.pitches = { params->pitch, 0, 0 },
+	};
+
+	dst_size = conversion_buf_size_mono(result->dst_pitch, &params->clip);
+
+	KUNIT_ASSERT_GT(test, dst_size, 0);
+
+	buf = kunit_kzalloc(test, dst_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
+	iosys_map_set_vaddr(&dst, buf);
+
+	xrgb8888 = cpubuf_to_le32(test, params->xrgb8888, TEST_BUF_SIZE);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xrgb8888);
+	iosys_map_set_vaddr(&src, xrgb8888);
+
+	drm_fb_xrgb8888_to_mono(&dst, &result->dst_pitch, &src, &fb, &params->clip);
+	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
+}
+
 static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_gray8, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_rgb332, convert_xrgb8888_gen_params),
@@ -800,6 +872,7 @@ static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb8888, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_xrgb2101010, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb2101010, convert_xrgb8888_gen_params),
+	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_mono, convert_xrgb8888_gen_params),
 	{}
 };
 
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw

Hello Arthur,

Thanks a lot for your patch!

Quoted 5 lines · Arthur Grillo

Extend the existing test cases to test the conversion from XRGB8888 to monochromatic.

---

[...]

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

Quoted 11 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +11 -0
+static size_t conversion_buf_size_mono(unsigned int dst_pitch, const struct drm_rect *clip)
+{
+	if (!dst_pitch) {
+		unsigned int linepixels = drm_rect_width(clip) * 1;
+
+		dst_pitch = DIV_ROUND_UP(linepixels, 8);
+	}
+
+	return dst_pitch * drm_rect_height(clip);
+}
+

I don't think you need a new helper only for this. There are other formats that have sub-byte pixels, so you may want to instead make the existing conversion_buf_size() function more general.

Could you please base on the following patch that I just posted?

https://lists.freedesktop.org/archives/dri-devel/2023-March/394466.html

I believe with that you should be able to drop this format specific helper and just use the fourcc DRM_FORMAT_C1 instead.

[...]

Quoted 7 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +6 -0
+static void drm_test_fb_xrgb8888_to_mono(struct kunit *test)
+{
+	const struct convert_xrgb8888_case *params = test->param_value;
+	const struct convert_to_mono_result *result = &params->mono_result;
+	size_t dst_size;
+	u8 *buf = NULL;

drivers/gpu/drm/tests/drm_format_helper_test.c static void drm_test_fb_xrgb8888_to_argb2101010(struct kunit *test) · line 849

Quoted 10 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +10 -0
+	__le32 *xrgb8888 = NULL;
+	struct iosys_map dst, src;
+
+	struct drm_framebuffer fb = {
+		.format = drm_format_info(DRM_FORMAT_XRGB8888),
+		.pitches = { params->pitch, 0, 0 },
+	};
+
+	dst_size = conversion_buf_size_mono(result->dst_pitch, &params->clip);
+

Then here you could just do:

	dst_size = conversion_buf_size(DRM_FORMAT_C1, result->dst_pitch,
				       &params->clip);

If you do that, feel free to add:

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#2 lore raw

Javier Martinez Canillas <[email protected]> writes:

[...]

Quoted 17 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+static size_t conversion_buf_size_mono(unsigned int dst_pitch, const struct drm_rect *clip)
+{
+	if (!dst_pitch) {
+		unsigned int linepixels = drm_rect_width(clip) * 1;
+
+		dst_pitch = DIV_ROUND_UP(linepixels, 8);
+	}
+
+	return dst_pitch * drm_rect_height(clip);
+}
+

I don't think you need a new helper only for this. There are other formats that have sub-byte pixels, so you may want to instead make the existing conversion_buf_size() function more general.

Quoted 4 lines

Could you please base on the following patch that I just posted?

https://lists.freedesktop.org/archives/dri-devel/2023-March/394466.html

I've posted a v2 since the kernel robot found a build warning on v1:

https://lists.freedesktop.org/archives/dri-devel/2023-March/394473.html

This time I have also tested your patch rebased on top of my v2, and your tests are passing too:

[22:46:16] ============== drm_test_fb_xrgb8888_to_mono =============== [22:46:16] [PASSED] single_pixel_source_buffer
[22:46:16] [PASSED] single_pixel_clip_rectangle
[22:46:16] [PASSED] well_known_colors
[22:46:16] [PASSED] destination_pitch

The version of your patch I that tested is the following:

From def1b2c04c812d53ebcda17c2d34d16f311ad406 Mon Sep 17 00:00:00 2001
From: Arthur Grillo <[email protected]>
Date: Thu, 2 Mar 2023 17:01:31 -0300
Subject: [PATCH] drm/format_helper: Add Kunit tests for
drm_fb_xrgb8888_to_mono()

Extend the existing test cases to test the conversion from XRGB8888 to monochromatic.

---
 .../gpu/drm/tests/drm_format_helper_test.c    | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
drivers/gpu/drm/tests/drm_format_helper_test.c +63 -0
index 84b5cc29c8fc..c9cd8a7741ee 100644
@@ -67,6 +67,11 @@ struct convert_to_argb2101010_result {
 	const u32 expected[TEST_BUF_SIZE];
 };
 
+struct convert_to_mono_result {
+	unsigned int dst_pitch;
+	const u8 expected[TEST_BUF_SIZE];
+};
+
 struct convert_xrgb8888_case {
 	const char *name;
 	unsigned int pitch;
@@ -82,6 +87,7 @@ struct convert_xrgb8888_case {
 	struct convert_to_argb8888_result argb8888_result;
 	struct convert_to_xrgb2101010_result xrgb2101010_result;
 	struct convert_to_argb2101010_result argb2101010_result;
+	struct convert_to_mono_result mono_result;
 };
 
 static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
@@ -131,6 +137,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		.name = "single_pixel_clip_rectangle",
@@ -181,6 +191,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		/* Well known colors: White, black, red, green, blue, magenta,
@@ -293,6 +307,15 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xFFFFFC00, 0xC00FFFFF,
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x01,
+				0x02,
+				0x00,
+				0x03,
+			},
+		},
 	},
 	{
 		/* Randomly picked colors. Full buffer within the clip area. */
@@ -392,6 +415,14 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xEA20300C, 0xDB1705CD, 0xC3844672, 0x00000000, 0x00000000,
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x00,
+				0x00,
+				0x00,
+			},
+		},
 	},
 };
 
@@ -792,6 +823,37 @@ static void drm_test_fb_xrgb8888_to_argb2101010(struct kunit *test)
 	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
 }
 
+static void drm_test_fb_xrgb8888_to_mono(struct kunit *test)
+{
+	const struct convert_xrgb8888_case *params = test->param_value;
+	const struct convert_to_mono_result *result = &params->mono_result;
+	size_t dst_size;
+	u8 *buf = NULL;
+	__le32 *xrgb8888 = NULL;
+	struct iosys_map dst, src;
+
+	struct drm_framebuffer fb = {
+		.format = drm_format_info(DRM_FORMAT_XRGB8888),
+		.pitches = { params->pitch, 0, 0 },
+	};
+
+	dst_size = conversion_buf_size(DRM_FORMAT_C1, result->dst_pitch,
+				       &params->clip);
+
+	KUNIT_ASSERT_GT(test, dst_size, 0);
+
+	buf = kunit_kzalloc(test, dst_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
+	iosys_map_set_vaddr(&dst, buf);
+
+	xrgb8888 = cpubuf_to_le32(test, params->xrgb8888, TEST_BUF_SIZE);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xrgb8888);
+	iosys_map_set_vaddr(&src, xrgb8888);
+
+	drm_fb_xrgb8888_to_mono(&dst, &result->dst_pitch, &src, &fb, &params->clip);
+	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
+}
+
 static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_gray8, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_rgb332, convert_xrgb8888_gen_params),
@@ -803,6 +865,7 @@ static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb8888, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_xrgb2101010, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb2101010, convert_xrgb8888_gen_params),
+	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_mono, convert_xrgb8888_gen_params),
 	{}
 };
 
Signature · 8 lines
2.39.2

-- 
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#3 lore raw
Quoted 172 lines · Javier Martinez Canillas

Javier Martinez Canillas <[email protected]> writes:

[...]

Quoted 17 lines

Review

Quoted 12 lines
(quoted diff) +11 -0
+static size_t conversion_buf_size_mono(unsigned int dst_pitch, const struct drm_rect *clip)
+{
+	if (!dst_pitch) {
+		unsigned int linepixels = drm_rect_width(clip) * 1;
+
+		dst_pitch = DIV_ROUND_UP(linepixels, 8);
+	}
+
+	return dst_pitch * drm_rect_height(clip);
+}
+

I don't think you need a new helper only for this. There are other formats that have sub-byte pixels, so you may want to instead make the existing conversion_buf_size() function more general.

Quoted 4 lines

Could you please base on the following patch that I just posted?

https://lists.freedesktop.org/archives/dri-devel/2023-March/394466.html

I've posted a v2 since the kernel robot found a build warning on v1:

https://lists.freedesktop.org/archives/dri-devel/2023-March/394473.html

This time I have also tested your patch rebased on top of my v2, and your tests are passing too:

[22:46:16] ============== drm_test_fb_xrgb8888_to_mono =============== [22:46:16] [PASSED] single_pixel_source_buffer
[22:46:16] [PASSED] single_pixel_clip_rectangle
[22:46:16] [PASSED] well_known_colors
[22:46:16] [PASSED] destination_pitch

The version of your patch I that tested is the following:

From def1b2c04c812d53ebcda17c2d34d16f311ad406 Mon Sep 17 00:00:00 2001
From: Arthur Grillo <[email protected]>
Date: Thu, 2 Mar 2023 17:01:31 -0300
Subject: [PATCH] drm/format_helper: Add Kunit tests for
drm_fb_xrgb8888_to_mono()

Extend the existing test cases to test the conversion from XRGB8888 to monochromatic.

---
 .../gpu/drm/tests/drm_format_helper_test.c    | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
drivers/gpu/drm/tests/drm_format_helper_test.c +62 -0
index 84b5cc29c8fc..c9cd8a7741ee 100644
@@ -67,6 +67,11 @@ struct convert_to_argb2101010_result {
 	const u32 expected[TEST_BUF_SIZE];
 };
 
+struct convert_to_mono_result {
+	unsigned int dst_pitch;
+	const u8 expected[TEST_BUF_SIZE];
+};
+
 struct convert_xrgb8888_case {
 	const char *name;
 	unsigned int pitch;
@@ -82,6 +87,7 @@ struct convert_xrgb8888_case {
 	struct convert_to_argb8888_result argb8888_result;
 	struct convert_to_xrgb2101010_result xrgb2101010_result;
 	struct convert_to_argb2101010_result argb2101010_result;
+	struct convert_to_mono_result mono_result;
 };
 
 static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
@@ -131,6 +137,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		.name = "single_pixel_clip_rectangle",
@@ -181,6 +191,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		/* Well known colors: White, black, red, green, blue, magenta,
@@ -293,6 +307,15 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xFFFFFC00, 0xC00FFFFF,
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x01,
+				0x02,
+				0x00,
+				0x03,
+			},
+		},
 	},
 	{
 		/* Randomly picked colors. Full buffer within the clip area. */
@@ -392,6 +415,14 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xEA20300C, 0xDB1705CD, 0xC3844672, 0x00000000, 0x00000000,
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x00,
+				0x00,
+				0x00,
+			},
+		},
 	},
 };
 
@@ -792,6 +823,37 @@ static void drm_test_fb_xrgb8888_to_argb2101010(struct kunit *test)
 	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
 }
 
+static void drm_test_fb_xrgb8888_to_mono(struct kunit *test)
+{
+	const struct convert_xrgb8888_case *params = test->param_value;
+	const struct convert_to_mono_result *result = &params->mono_result;
+	size_t dst_size;
+	u8 *buf = NULL;
+	__le32 *xrgb8888 = NULL;
+	struct iosys_map dst, src;
+
+	struct drm_framebuffer fb = {
+		.format = drm_format_info(DRM_FORMAT_XRGB8888),
+		.pitches = { params->pitch, 0, 0 },
+	};
+
+	dst_size = conversion_buf_size(DRM_FORMAT_C1, result->dst_pitch,
+				       &params->clip);
+
+	KUNIT_ASSERT_GT(test, dst_size, 0);
+
+	buf = kunit_kzalloc(test, dst_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
+	iosys_map_set_vaddr(&dst, buf);
+
+	xrgb8888 = cpubuf_to_le32(test, params->xrgb8888, TEST_BUF_SIZE);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xrgb8888);
+	iosys_map_set_vaddr(&src, xrgb8888);
+
+	drm_fb_xrgb8888_to_mono(&dst, &result->dst_pitch, &src, &fb, &params->clip);
+	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
+}
+
 static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_gray8, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_rgb332, convert_xrgb8888_gen_params),

drivers/gpu/drm/tests/drm_format_helper_test.c static struct kunit_case drm_format_helper_test_cases[] = { · line 871

Quoted 7 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +1 -0
@@ -803,6 +865,7 @@ static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb8888, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_xrgb2101010, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb2101010, convert_xrgb8888_gen_params),
+	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_mono, convert_xrgb8888_gen_params),
 	{}
 };
 

Hi Javier. Thank you for your review :)

Thank you for your patch! I too think that a new helper is not needed. I will send the v2 when your patch is applied.

#4 lore raw
Quoted 1 line · Arthur Grillo Queiroz Cabral

On 07/03/23 18:55, Javier Martinez Canillas wrote:

[...]

Quoted 3 lines

Hi Javier. Thank you for your review :)

You are welcome!

Quoted 3 lines

Thank you for your patch! I too think that a new helper is not needed. I will send the v2 when your patch is applied.

Great.

Signature · 5 lines
Best regards,

Javier Martinez Canillas
Core Platforms
Red Hat
#5 lore raw
In reply to Arthur Grillo
Quoted 72 lines · Arthur Grillo

Extend the existing test cases to test the conversion from XRGB8888 to monochromatic.

---
 .../gpu/drm/tests/drm_format_helper_test.c    | 73 +++++++++++++++++++
 1 file changed, 73 insertions(+)
drivers/gpu/drm/tests/drm_format_helper_test.c +23 -0
index 9536829c6e3a..0610341e7349 100644
@@ -67,6 +67,11 @@ struct convert_to_argb2101010_result {
 	const u32 expected[TEST_BUF_SIZE];
 };
 
+struct convert_to_mono_result {
+	unsigned int dst_pitch;
+	const u8 expected[TEST_BUF_SIZE];
+};
+
 struct convert_xrgb8888_case {
 	const char *name;
 	unsigned int pitch;
@@ -82,6 +87,7 @@ struct convert_xrgb8888_case {
 	struct convert_to_argb8888_result argb8888_result;
 	struct convert_to_xrgb2101010_result xrgb2101010_result;
 	struct convert_to_argb2101010_result argb2101010_result;
+	struct convert_to_mono_result mono_result;
 };
 
 static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
@@ -131,6 +137,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		.name = "single_pixel_clip_rectangle",
@@ -181,6 +191,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 			.dst_pitch = 0,
 			.expected = { 0xFFF00000 },
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = { 0x00 },
+		},
 	},
 	{
 		/* Well known colors: White, black, red, green, blue, magenta,
@@ -293,6 +307,15 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xFFFFFC00, 0xC00FFFFF,
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x01,
+				0x02,
+				0x00,
+				0x03,
+			},
+		},
 	},
 	{
 		/* Randomly picked colors. Full buffer within the clip area. */
@@ -392,6 +415,14 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
 				0xEA20300C, 0xDB1705CD, 0xC3844672, 0x00000000, 0x00000000,

drivers/gpu/drm/tests/drm_format_helper_test.c static struct convert_xrgb8888_case convert_xrgb8888_cases[] = { · line 425

Quoted 10 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +8 -0
 			},
 		},
+		.mono_result = {
+			.dst_pitch = 0,
+			.expected = {
+				0x00,
+				0x00,
+				0x00,
+			},
+		},

Now I notice that this test is not so useful for this conversion. Maybe I will change the colors of the test to stop the expected array from being just zeros on the v2.

Quoted 66 lines
drivers/gpu/drm/tests/drm_format_helper_test.c +42 -0
 	},
 };
 
@@ -419,6 +450,17 @@ static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
 	return dst_pitch * drm_rect_height(clip);
 }
 
+static size_t conversion_buf_size_mono(unsigned int dst_pitch, const struct drm_rect *clip)
+{
+	if (!dst_pitch) {
+		unsigned int linepixels = drm_rect_width(clip) * 1;
+
+		dst_pitch = DIV_ROUND_UP(linepixels, 8);
+	}
+
+	return dst_pitch * drm_rect_height(clip);
+}
+
 static u16 *le16buf_to_cpu(struct kunit *test, const __le16 *buf, size_t buf_size)
 {
 	u16 *dst = NULL;
@@ -789,6 +831,36 @@ static void drm_test_fb_xrgb8888_to_argb2101010(struct kunit *test)
 	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
 }
 
+static void drm_test_fb_xrgb8888_to_mono(struct kunit *test)
+{
+	const struct convert_xrgb8888_case *params = test->param_value;
+	const struct convert_to_mono_result *result = &params->mono_result;
+	size_t dst_size;
+	u8 *buf = NULL;
+	__le32 *xrgb8888 = NULL;
+	struct iosys_map dst, src;
+
+	struct drm_framebuffer fb = {
+		.format = drm_format_info(DRM_FORMAT_XRGB8888),
+		.pitches = { params->pitch, 0, 0 },
+	};
+
+	dst_size = conversion_buf_size_mono(result->dst_pitch, &params->clip);
+
+	KUNIT_ASSERT_GT(test, dst_size, 0);
+
+	buf = kunit_kzalloc(test, dst_size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
+	iosys_map_set_vaddr(&dst, buf);
+
+	xrgb8888 = cpubuf_to_le32(test, params->xrgb8888, TEST_BUF_SIZE);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xrgb8888);
+	iosys_map_set_vaddr(&src, xrgb8888);
+
+	drm_fb_xrgb8888_to_mono(&dst, &result->dst_pitch, &src, &fb, &params->clip);
+	KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
+}
+
 static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_gray8, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_rgb332, convert_xrgb8888_gen_params),
@@ -800,6 +872,7 @@ static struct kunit_case drm_format_helper_test_cases[] = {
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb8888, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_xrgb2101010, convert_xrgb8888_gen_params),
 	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb2101010, convert_xrgb8888_gen_params),
+	KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_mono, convert_xrgb8888_gen_params),
 	{}
 };