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