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_u16 - Internal representation of a pixel color. 18 * @y: Luma value, stored in 16 bits, without padding, using 19 * machine endianness 20 * @u: Blue difference chroma value, stored in 16 bits, without padding, using 21 * machine endianness 22 * @v: Red difference chroma value, stored in 16 bits, without padding, using 23 * machine endianness 24 */ 25 struct pixel_yuv_u16 { 26 u16 y, u, v; 27 }; 28 29 /* 30 * struct yuv_u16_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_u16_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_u16 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_u16_to_argb_u16_case yuv_u16_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 = 16, 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", { 0xffff, 0x8000, 0x8000 }, { 0xffff, 0xffff, 0xffff, 0xffff }}, 80 { "gray", { 0x8080, 0x8000, 0x8000 }, { 0xffff, 0x8080, 0x8080, 0x8080 }}, 81 { "black", { 0x0000, 0x8000, 0x8000 }, { 0xffff, 0x0000, 0x0000, 0x0000 }}, 82 { "red", { 0x4c8b, 0x54ce, 0xffff }, { 0xffff, 0xffff, 0x0000, 0x0000 }}, 83 { "green", { 0x9645, 0x2b33, 0x14d1 }, { 0xffff, 0x0000, 0xffff, 0x0000 }}, 84 { "blue", { 0x1d2f, 0xffff, 0x6b2f }, { 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 = 16, 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", { 0xeb00, 0x8000, 0x8000 }, { 0xffff, 0xffff, 0xffff, 0xffff }}, 105 { "gray", { 0x7dee, 0x8000, 0x8000 }, { 0xffff, 0x8080, 0x8080, 0x8080 }}, 106 { "black", { 0x1000, 0x8000, 0x8000 }, { 0xffff, 0x0000, 0x0000, 0x0000 }}, 107 { "red", { 0x517b, 0x5a34, 0xf000 }, { 0xffff, 0xffff, 0x0000, 0x0000 }}, 108 { "green", { 0x908e, 0x35cc, 0x2237 }, { 0xffff, 0x0000, 0xffff, 0x0000 }}, 109 { "blue", { 0x28f7, 0xf000, 0x6dc9 }, { 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 = 16, 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", { 0xffff, 0x8000, 0x8000 }, { 0xffff, 0xffff, 0xffff, 0xffff }}, 130 { "gray", { 0x8080, 0x8000, 0x8000 }, { 0xffff, 0x8080, 0x8080, 0x8080 }}, 131 { "black", { 0x0000, 0x8000, 0x8000 }, { 0xffff, 0x0000, 0x0000, 0x0000 }}, 132 { "red", { 0x366d, 0x62ac, 0xffff }, { 0xffff, 0xffff, 0x0000, 0x0000 }}, 133 { "green", { 0xb717, 0x1d55, 0x0bbd }, { 0xffff, 0x0000, 0xffff, 0x0000 }}, 134 { "blue", { 0x127c, 0xffff, 0x7443 }, { 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 * in_legal = False, 142 * in_int = True, 143 * out_bits = 16, 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", { 0xeb00, 0x8000, 0x8000 }, { 0xffff, 0xffff, 0xffff, 0xffff }}, 155 { "gray", { 0x7dee, 0x8000, 0x8000 }, { 0xffff, 0x8080, 0x8080, 0x8080 }}, 156 { "black", { 0x1000, 0x8000, 0x8000 }, { 0xffff, 0x0000, 0x0000, 0x0000 }}, 157 { "red", { 0x3e8f, 0x6656, 0xf000 }, { 0xffff, 0xffff, 0x0000, 0x0000 }}, 158 { "green", { 0xaca1, 0x29aa, 0x1a45 }, { 0xffff, 0x0000, 0xffff, 0x0000 }}, 159 { "blue", { 0x1fd0, 0xf000, 0x75bb }, { 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 = 16, 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", { 0xffff, 0x8000, 0x8000 }, { 0xffff, 0xffff, 0xffff, 0xffff }}, 180 { "gray", { 0x8080, 0x8000, 0x8000 }, { 0xffff, 0x8080, 0x8080, 0x8080 }}, 181 { "black", { 0x0000, 0x8000, 0x8000 }, { 0xffff, 0x0000, 0x0000, 0x0000 }}, 182 { "red", { 0x4340, 0x5c41, 0xffff }, { 0xffff, 0xffff, 0x0000, 0x0000 }}, 183 { "green", { 0xad91, 0x23bf, 0x0a4c }, { 0xffff, 0x0000, 0xffff, 0x0000 }}, 184 { "blue", { 0x0f2e, 0xffff, 0x75b5 }, { 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 = 16, 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", { 0xeb00, 0x8000, 0x8000 }, { 0xffff, 0xffff, 0xffff, 0xffff }}, 205 { "gray", { 0x7dee, 0x8000, 0x8000 }, { 0xffff, 0x8080, 0x8080, 0x8080 }}, 206 { "black", { 0x1000, 0x8000, 0x8000 }, { 0xffff, 0x0000, 0x0000, 0x0000 }}, 207 { "red", { 0x4988, 0x60b9, 0xf000 }, { 0xffff, 0xffff, 0x0000, 0x0000 }}, 208 { "green", { 0xa47b, 0x2f47, 0x1902 }, { 0xffff, 0x0000, 0xffff, 0x0000 }}, 209 { "blue", { 0x1cfd, 0xf000, 0x76fe }, { 0xffff, 0x0000, 0x0000, 0xffff }}, 210 } 211 }, 212 }; 213 214 /* 215 * vkms_format_test_yuv_u16_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_yuv161616 to convert YUV colors (stored in 220 * yuv_u16_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 */ 225 static void vkms_format_test_yuv_u16_to_argb_u16(struct kunit *test) 226 { 227 const struct yuv_u16_to_argb_u16_case *param = test->param_value; 228 struct pixel_argb_u16 argb; 229 230 for (size_t i = 0; i < param->n_colors; i++) { 231 const struct format_pair *color = ¶m->colors[i]; 232 struct conversion_matrix matrix; 233 234 get_conversion_matrix_to_argb_u16 235 (DRM_FORMAT_NV12, param->encoding, param->range, &matrix); 236 237 argb = argb_u16_from_yuv161616(&matrix, color->yuv.y, color->yuv.u, 238 color->yuv.v); 239 240 KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.a, color->argb.a), 0x1ff, 241 "On the A channel of the color %s expected 0x%04x, got 0x%04x", 242 color->name, color->argb.a, argb.a); 243 KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.r, color->argb.r), 0x1ff, 244 "On the R channel of the color %s expected 0x%04x, got 0x%04x", 245 color->name, color->argb.r, argb.r); 246 KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.g, color->argb.g), 0x1ff, 247 "On the G channel of the color %s expected 0x%04x, got 0x%04x", 248 color->name, color->argb.g, argb.g); 249 KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.b, color->argb.b), 0x1ff, 250 "On the B channel of the color %s expected 0x%04x, got 0x%04x", 251 color->name, color->argb.b, argb.b); 252 } 253 } 254 255 static void vkms_format_test_yuv_u16_to_argb_u16_case_desc(struct yuv_u16_to_argb_u16_case *t, 256 char *desc) 257 { 258 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s - %s", 259 drm_get_color_encoding_name(t->encoding), drm_get_color_range_name(t->range)); 260 } 261 262 KUNIT_ARRAY_PARAM(yuv_u16_to_argb_u16, yuv_u16_to_argb_u16_cases, 263 vkms_format_test_yuv_u16_to_argb_u16_case_desc 264 ); 265 266 static struct kunit_case vkms_format_test_cases[] = { 267 KUNIT_CASE_PARAM(vkms_format_test_yuv_u16_to_argb_u16, yuv_u16_to_argb_u16_gen_params), 268 {} 269 }; 270 271 static struct kunit_suite vkms_format_test_suite = { 272 .name = "vkms-format", 273 .test_cases = vkms_format_test_cases, 274 }; 275 276 kunit_test_suite(vkms_format_test_suite); 277 278 MODULE_LICENSE("GPL"); 279 MODULE_DESCRIPTION("Kunit test for vkms format conversion"); 280