xref: /linux/drivers/gpu/drm/vkms/tests/vkms_format_test.c (revision 8d2b0853add1d7534dc0794e3c8e0b9e8c4ec640)
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <kunit/test.h>
4 
5 #include <drm/drm_fixed.h>
6 #include <drm/drm_fourcc.h>
7 
8 #include "../../drm_crtc_internal.h"
9 
10 #include "../vkms_formats.h"
11 
12 #define TEST_BUFF_SIZE 50
13 
14 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
15 
16 /**
17  * struct pixel_yuv_u8 - Internal representation of a pixel color.
18  * @y: Luma value, stored in 8 bits, without padding, using
19  *     machine endianness
20  * @u: Blue difference chroma value, stored in 8 bits, without padding, using
21  *     machine endianness
22  * @v: Red difference chroma value, stored in 8 bits, without padding, using
23  *     machine endianness
24  */
25 struct pixel_yuv_u8 {
26 	u8 y, u, v;
27 };
28 
29 /*
30  * struct yuv_u8_to_argb_u16_case - Reference values to test the color
31  * conversions in VKMS between YUV to ARGB
32  *
33  * @encoding: Encoding used to convert RGB to YUV
34  * @range: Range used to convert RGB to YUV
35  * @n_colors: Count of test colors in this case
36  * @format_pair.name: Name used for this color conversion, used to
37  *                    clarify the test results
38  * @format_pair.rgb: RGB color tested
39  * @format_pair.yuv: Same color as @format_pair.rgb, but converted to
40  *                   YUV using @encoding and @range.
41  */
42 struct yuv_u8_to_argb_u16_case {
43 	enum drm_color_encoding encoding;
44 	enum drm_color_range range;
45 	size_t n_colors;
46 	struct format_pair {
47 		char *name;
48 		struct pixel_yuv_u8 yuv;
49 		struct pixel_argb_u16 argb;
50 	} colors[TEST_BUFF_SIZE];
51 };
52 
53 /*
54  * The YUV color representation were acquired via the colour python framework.
55  * Below are the function calls used for generating each case.
56  *
57  * For more information got to the docs:
58  * https://colour.readthedocs.io/en/master/generated/colour.RGB_to_YCbCr.html
59  */
60 static struct yuv_u8_to_argb_u16_case yuv_u8_to_argb_u16_cases[] = {
61 	/*
62 	 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>,
63 	 *                     K=colour.WEIGHTS_YCBCR["ITU-R BT.601"],
64 	 *                     in_bits = 16,
65 	 *                     in_legal = False,
66 	 *                     in_int = True,
67 	 *                     out_bits = 8,
68 	 *                     out_legal = False,
69 	 *                     out_int = True)
70 	 *
71 	 * Tests cases for color conversion generated by converting RGB
72 	 * values to YUV BT601 full range using the ITU-R BT.601 weights.
73 	 */
74 	{
75 		.encoding = DRM_COLOR_YCBCR_BT601,
76 		.range = DRM_COLOR_YCBCR_FULL_RANGE,
77 		.n_colors = 6,
78 		.colors = {
79 			{ "white", { 0xff, 0x80, 0x80 }, { 0xffff, 0xffff, 0xffff, 0xffff }},
80 			{ "gray",  { 0x80, 0x80, 0x80 }, { 0xffff, 0x8080, 0x8080, 0x8080 }},
81 			{ "black", { 0x00, 0x80, 0x80 }, { 0xffff, 0x0000, 0x0000, 0x0000 }},
82 			{ "red",   { 0x4c, 0x55, 0xff }, { 0xffff, 0xffff, 0x0000, 0x0000 }},
83 			{ "green", { 0x96, 0x2c, 0x15 }, { 0xffff, 0x0000, 0xffff, 0x0000 }},
84 			{ "blue",  { 0x1d, 0xff, 0x6b }, { 0xffff, 0x0000, 0x0000, 0xffff }},
85 		},
86 	},
87 	/*
88 	 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>,
89 	 *                     K=colour.WEIGHTS_YCBCR["ITU-R BT.601"],
90 	 *                     in_bits = 16,
91 	 *                     in_legal = False,
92 	 *                     in_int = True,
93 	 *                     out_bits = 8,
94 	 *                     out_legal = True,
95 	 *                     out_int = True)
96 	 * Tests cases for color conversion generated by converting RGB
97 	 * values to YUV BT601 limited range using the ITU-R BT.601 weights.
98 	 */
99 	{
100 		.encoding = DRM_COLOR_YCBCR_BT601,
101 		.range = DRM_COLOR_YCBCR_LIMITED_RANGE,
102 		.n_colors = 6,
103 		.colors = {
104 			{ "white", { 0xeb, 0x80, 0x80 }, { 0xffff, 0xffff, 0xffff, 0xffff }},
105 			{ "gray",  { 0x7e, 0x80, 0x80 }, { 0xffff, 0x8080, 0x8080, 0x8080 }},
106 			{ "black", { 0x10, 0x80, 0x80 }, { 0xffff, 0x0000, 0x0000, 0x0000 }},
107 			{ "red",   { 0x51, 0x5a, 0xf0 }, { 0xffff, 0xffff, 0x0000, 0x0000 }},
108 			{ "green", { 0x91, 0x36, 0x22 }, { 0xffff, 0x0000, 0xffff, 0x0000 }},
109 			{ "blue",  { 0x29, 0xf0, 0x6e }, { 0xffff, 0x0000, 0x0000, 0xffff }},
110 		},
111 	},
112 	/*
113 	 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>,
114 	 *                     K=colour.WEIGHTS_YCBCR["ITU-R BT.709"],
115 	 *                     in_bits = 16,
116 	 *                     in_legal = False,
117 	 *                     in_int = True,
118 	 *                     out_bits = 8,
119 	 *                     out_legal = False,
120 	 *                     out_int = True)
121 	 * Tests cases for color conversion generated by converting RGB
122 	 * values to YUV BT709 full range using the ITU-R BT.709 weights.
123 	 */
124 	{
125 		.encoding = DRM_COLOR_YCBCR_BT709,
126 		.range = DRM_COLOR_YCBCR_FULL_RANGE,
127 		.n_colors = 6,
128 		.colors = {
129 			{ "white", { 0xff, 0x80, 0x80 }, { 0xffff, 0xffff, 0xffff, 0xffff }},
130 			{ "gray",  { 0x80, 0x80, 0x80 }, { 0xffff, 0x8080, 0x8080, 0x8080 }},
131 			{ "black", { 0x00, 0x80, 0x80 }, { 0xffff, 0x0000, 0x0000, 0x0000 }},
132 			{ "red",   { 0x36, 0x63, 0xff }, { 0xffff, 0xffff, 0x0000, 0x0000 }},
133 			{ "green", { 0xb6, 0x1e, 0x0c }, { 0xffff, 0x0000, 0xffff, 0x0000 }},
134 			{ "blue",  { 0x12, 0xff, 0x74 }, { 0xffff, 0x0000, 0x0000, 0xffff }},
135 		},
136 	},
137 	/*
138 	 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>,
139 	 *                     K=colour.WEIGHTS_YCBCR["ITU-R BT.709"],
140 	 *                     in_bits = 16,
141 	 *                     int_legal = False,
142 	 *                     in_int = True,
143 	 *                     out_bits = 8,
144 	 *                     out_legal = True,
145 	 *                     out_int = True)
146 	 * Tests cases for color conversion generated by converting RGB
147 	 * values to YUV BT709 limited range using the ITU-R BT.709 weights.
148 	 */
149 	{
150 		.encoding = DRM_COLOR_YCBCR_BT709,
151 		.range = DRM_COLOR_YCBCR_LIMITED_RANGE,
152 		.n_colors = 6,
153 		.colors = {
154 			{ "white", { 0xeb, 0x80, 0x80 }, { 0xffff, 0xffff, 0xffff, 0xffff }},
155 			{ "gray",  { 0x7e, 0x80, 0x80 }, { 0xffff, 0x8080, 0x8080, 0x8080 }},
156 			{ "black", { 0x10, 0x80, 0x80 }, { 0xffff, 0x0000, 0x0000, 0x0000 }},
157 			{ "red",   { 0x3f, 0x66, 0xf0 }, { 0xffff, 0xffff, 0x0000, 0x0000 }},
158 			{ "green", { 0xad, 0x2a, 0x1a }, { 0xffff, 0x0000, 0xffff, 0x0000 }},
159 			{ "blue",  { 0x20, 0xf0, 0x76 }, { 0xffff, 0x0000, 0x0000, 0xffff }},
160 		},
161 	},
162 	/*
163 	 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>,
164 	 *                     K=colour.WEIGHTS_YCBCR["ITU-R BT.2020"],
165 	 *                     in_bits = 16,
166 	 *                     in_legal = False,
167 	 *                     in_int = True,
168 	 *                     out_bits = 8,
169 	 *                     out_legal = False,
170 	 *                     out_int = True)
171 	 * Tests cases for color conversion generated by converting RGB
172 	 * values to YUV BT2020 full range using the ITU-R BT.2020 weights.
173 	 */
174 	{
175 		.encoding = DRM_COLOR_YCBCR_BT2020,
176 		.range = DRM_COLOR_YCBCR_FULL_RANGE,
177 		.n_colors = 6,
178 		.colors = {
179 			{ "white", { 0xff, 0x80, 0x80 }, { 0xffff, 0xffff, 0xffff, 0xffff }},
180 			{ "gray",  { 0x80, 0x80, 0x80 }, { 0xffff, 0x8080, 0x8080, 0x8080 }},
181 			{ "black", { 0x00, 0x80, 0x80 }, { 0xffff, 0x0000, 0x0000, 0x0000 }},
182 			{ "red",   { 0x43, 0x5c, 0xff }, { 0xffff, 0xffff, 0x0000, 0x0000 }},
183 			{ "green", { 0xad, 0x24, 0x0b }, { 0xffff, 0x0000, 0xffff, 0x0000 }},
184 			{ "blue",  { 0x0f, 0xff, 0x76 }, { 0xffff, 0x0000, 0x0000, 0xffff }},
185 		},
186 	},
187 	/*
188 	 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>,
189 	 *                     K=colour.WEIGHTS_YCBCR["ITU-R BT.2020"],
190 	 *                     in_bits = 16,
191 	 *                     in_legal = False,
192 	 *                     in_int = True,
193 	 *                     out_bits = 8,
194 	 *                     out_legal = True,
195 	 *                     out_int = True)
196 	 * Tests cases for color conversion generated by converting RGB
197 	 * values to YUV BT2020 limited range using the ITU-R BT.2020 weights.
198 	 */
199 	{
200 		.encoding = DRM_COLOR_YCBCR_BT2020,
201 		.range = DRM_COLOR_YCBCR_LIMITED_RANGE,
202 		.n_colors = 6,
203 		.colors = {
204 			{ "white", { 0xeb, 0x80, 0x80 }, { 0xffff, 0xffff, 0xffff, 0xffff }},
205 			{ "gray",  { 0x7e, 0x80, 0x80 }, { 0xffff, 0x8080, 0x8080, 0x8080 }},
206 			{ "black", { 0x10, 0x80, 0x80 }, { 0xffff, 0x0000, 0x0000, 0x0000 }},
207 			{ "red",   { 0x4a, 0x61, 0xf0 }, { 0xffff, 0xffff, 0x0000, 0x0000 }},
208 			{ "green", { 0xa4, 0x2f, 0x19 }, { 0xffff, 0x0000, 0xffff, 0x0000 }},
209 			{ "blue",  { 0x1d, 0xf0, 0x77 }, { 0xffff, 0x0000, 0x0000, 0xffff }},
210 		},
211 	},
212 };
213 
214 /*
215  * vkms_format_test_yuv_u8_to_argb_u16 - Testing the conversion between YUV
216  * colors to ARGB colors in VKMS
217  *
218  * This test will use the functions get_conversion_matrix_to_argb_u16 and
219  * argb_u16_from_yuv888 to convert YUV colors (stored in
220  * yuv_u8_to_argb_u16_cases) into ARGB colors.
221  *
222  * The conversion between YUV and RGB is not totally reversible, so there may be
223  * some difference between the expected value and the result.
224  * In addition, there may be some rounding error as the input color is 8 bits
225  * and output color is 16 bits.
226  */
227 static void vkms_format_test_yuv_u8_to_argb_u16(struct kunit *test)
228 {
229 	const struct yuv_u8_to_argb_u16_case *param = test->param_value;
230 	struct pixel_argb_u16 argb;
231 
232 	for (size_t i = 0; i < param->n_colors; i++) {
233 		const struct format_pair *color = &param->colors[i];
234 		struct conversion_matrix matrix;
235 
236 		get_conversion_matrix_to_argb_u16
237 			(DRM_FORMAT_NV12, param->encoding, param->range, &matrix);
238 
239 		argb = argb_u16_from_yuv888(color->yuv.y, color->yuv.u, color->yuv.v, &matrix);
240 
241 		KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.a, color->argb.a), 0x1ff,
242 				    "On the A channel of the color %s expected 0x%04x, got 0x%04x",
243 				    color->name, color->argb.a, argb.a);
244 		KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.r, color->argb.r), 0x1ff,
245 				    "On the R channel of the color %s expected 0x%04x, got 0x%04x",
246 				    color->name, color->argb.r, argb.r);
247 		KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.g, color->argb.g), 0x1ff,
248 				    "On the G channel of the color %s expected 0x%04x, got 0x%04x",
249 				    color->name, color->argb.g, argb.g);
250 		KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.b, color->argb.b), 0x1ff,
251 				    "On the B channel of the color %s expected 0x%04x, got 0x%04x",
252 				    color->name, color->argb.b, argb.b);
253 	}
254 }
255 
256 static void vkms_format_test_yuv_u8_to_argb_u16_case_desc(struct yuv_u8_to_argb_u16_case *t,
257 							  char *desc)
258 {
259 	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s - %s",
260 		 drm_get_color_encoding_name(t->encoding), drm_get_color_range_name(t->range));
261 }
262 
263 KUNIT_ARRAY_PARAM(yuv_u8_to_argb_u16, yuv_u8_to_argb_u16_cases,
264 		  vkms_format_test_yuv_u8_to_argb_u16_case_desc
265 );
266 
267 static struct kunit_case vkms_format_test_cases[] = {
268 	KUNIT_CASE_PARAM(vkms_format_test_yuv_u8_to_argb_u16, yuv_u8_to_argb_u16_gen_params),
269 	{}
270 };
271 
272 static struct kunit_suite vkms_format_test_suite = {
273 	.name = "vkms-format",
274 	.test_cases = vkms_format_test_cases,
275 };
276 
277 kunit_test_suite(vkms_format_test_suite);
278 
279 MODULE_LICENSE("GPL");
280 MODULE_DESCRIPTION("Kunit test for vkms format conversion");
281