Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/vkms: Change YUV helpers to support u16 inputs for conversion

Some YUV format uses 16 bit values, so change the helper function for
conversion to support those new formats.

Reviewed-by: Maíra Canal <mcanal@igalia.com>
Acked-by: Daniel Stone <daniels@collabora.com>
Link: https://lore.kernel.org/r/20250703-b4-new-color-formats-v7-6-15fd8fd2e15c@bootlin.com
Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>

+85 -84
+71 -72
drivers/gpu/drm/vkms/tests/vkms_format_test.c
··· 14 14 MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); 15 15 16 16 /** 17 - * struct pixel_yuv_u8 - Internal representation of a pixel color. 18 - * @y: Luma value, stored in 8 bits, without padding, using 17 + * struct pixel_yuv_u16 - Internal representation of a pixel color. 18 + * @y: Luma value, stored in 16 bits, without padding, using 19 19 * machine endianness 20 - * @u: Blue difference chroma value, stored in 8 bits, without padding, using 20 + * @u: Blue difference chroma value, stored in 16 bits, without padding, using 21 21 * machine endianness 22 - * @v: Red difference chroma value, stored in 8 bits, without padding, using 22 + * @v: Red difference chroma value, stored in 16 bits, without padding, using 23 23 * machine endianness 24 24 */ 25 - struct pixel_yuv_u8 { 26 - u8 y, u, v; 25 + struct pixel_yuv_u16 { 26 + u16 y, u, v; 27 27 }; 28 28 29 29 /* 30 - * struct yuv_u8_to_argb_u16_case - Reference values to test the color 30 + * struct yuv_u16_to_argb_u16_case - Reference values to test the color 31 31 * conversions in VKMS between YUV to ARGB 32 32 * 33 33 * @encoding: Encoding used to convert RGB to YUV ··· 39 39 * @format_pair.yuv: Same color as @format_pair.rgb, but converted to 40 40 * YUV using @encoding and @range. 41 41 */ 42 - struct yuv_u8_to_argb_u16_case { 42 + struct yuv_u16_to_argb_u16_case { 43 43 enum drm_color_encoding encoding; 44 44 enum drm_color_range range; 45 45 size_t n_colors; 46 46 struct format_pair { 47 47 char *name; 48 - struct pixel_yuv_u8 yuv; 48 + struct pixel_yuv_u16 yuv; 49 49 struct pixel_argb_u16 argb; 50 50 } colors[TEST_BUFF_SIZE]; 51 51 }; ··· 57 57 * For more information got to the docs: 58 58 * https://colour.readthedocs.io/en/master/generated/colour.RGB_to_YCbCr.html 59 59 */ 60 - static struct yuv_u8_to_argb_u16_case yuv_u8_to_argb_u16_cases[] = { 60 + static struct yuv_u16_to_argb_u16_case yuv_u16_to_argb_u16_cases[] = { 61 61 /* 62 62 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>, 63 63 * K=colour.WEIGHTS_YCBCR["ITU-R BT.601"], 64 64 * in_bits = 16, 65 65 * in_legal = False, 66 66 * in_int = True, 67 - * out_bits = 8, 67 + * out_bits = 16, 68 68 * out_legal = False, 69 69 * out_int = True) 70 70 * ··· 76 76 .range = DRM_COLOR_YCBCR_FULL_RANGE, 77 77 .n_colors = 6, 78 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 - }, 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 86 }, 87 87 /* 88 88 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>, ··· 90 90 * in_bits = 16, 91 91 * in_legal = False, 92 92 * in_int = True, 93 - * out_bits = 8, 93 + * out_bits = 16, 94 94 * out_legal = True, 95 95 * out_int = True) 96 96 * Tests cases for color conversion generated by converting RGB ··· 101 101 .range = DRM_COLOR_YCBCR_LIMITED_RANGE, 102 102 .n_colors = 6, 103 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 - }, 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 111 }, 112 112 /* 113 113 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>, ··· 115 115 * in_bits = 16, 116 116 * in_legal = False, 117 117 * in_int = True, 118 - * out_bits = 8, 118 + * out_bits = 16, 119 119 * out_legal = False, 120 120 * out_int = True) 121 121 * Tests cases for color conversion generated by converting RGB ··· 126 126 .range = DRM_COLOR_YCBCR_FULL_RANGE, 127 127 .n_colors = 6, 128 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 - }, 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 136 }, 137 137 /* 138 138 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>, 139 139 * K=colour.WEIGHTS_YCBCR["ITU-R BT.709"], 140 140 * in_bits = 16, 141 - * int_legal = False, 141 + * in_legal = False, 142 142 * in_int = True, 143 - * out_bits = 8, 143 + * out_bits = 16, 144 144 * out_legal = True, 145 145 * out_int = True) 146 146 * Tests cases for color conversion generated by converting RGB ··· 151 151 .range = DRM_COLOR_YCBCR_LIMITED_RANGE, 152 152 .n_colors = 6, 153 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 - }, 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 161 }, 162 162 /* 163 163 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>, ··· 165 165 * in_bits = 16, 166 166 * in_legal = False, 167 167 * in_int = True, 168 - * out_bits = 8, 168 + * out_bits = 16, 169 169 * out_legal = False, 170 170 * out_int = True) 171 171 * Tests cases for color conversion generated by converting RGB ··· 176 176 .range = DRM_COLOR_YCBCR_FULL_RANGE, 177 177 .n_colors = 6, 178 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 - }, 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 186 }, 187 187 /* 188 188 * colour.RGB_to_YCbCr(<rgb color in 16 bit form>, ··· 190 190 * in_bits = 16, 191 191 * in_legal = False, 192 192 * in_int = True, 193 - * out_bits = 8, 193 + * out_bits = 16, 194 194 * out_legal = True, 195 195 * out_int = True) 196 196 * Tests cases for color conversion generated by converting RGB ··· 201 201 .range = DRM_COLOR_YCBCR_LIMITED_RANGE, 202 202 .n_colors = 6, 203 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 - }, 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 211 }, 212 212 }; 213 213 214 214 /* 215 - * vkms_format_test_yuv_u8_to_argb_u16 - Testing the conversion between YUV 215 + * vkms_format_test_yuv_u16_to_argb_u16 - Testing the conversion between YUV 216 216 * colors to ARGB colors in VKMS 217 217 * 218 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. 219 + * argb_u16_from_yuv161616 to convert YUV colors (stored in 220 + * yuv_u16_to_argb_u16_cases) into ARGB colors. 221 221 * 222 222 * The conversion between YUV and RGB is not totally reversible, so there may be 223 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 224 */ 227 - static void vkms_format_test_yuv_u8_to_argb_u16(struct kunit *test) 225 + static void vkms_format_test_yuv_u16_to_argb_u16(struct kunit *test) 228 226 { 229 - const struct yuv_u8_to_argb_u16_case *param = test->param_value; 227 + const struct yuv_u16_to_argb_u16_case *param = test->param_value; 230 228 struct pixel_argb_u16 argb; 231 229 232 230 for (size_t i = 0; i < param->n_colors; i++) { ··· 234 236 get_conversion_matrix_to_argb_u16 235 237 (DRM_FORMAT_NV12, param->encoding, param->range, &matrix); 236 238 237 - argb = argb_u16_from_yuv888(color->yuv.y, color->yuv.u, color->yuv.v, &matrix); 239 + argb = argb_u16_from_yuv161616(&matrix, color->yuv.y, color->yuv.u, 240 + color->yuv.v); 238 241 239 242 KUNIT_EXPECT_LE_MSG(test, abs_diff(argb.a, color->argb.a), 0x1ff, 240 243 "On the A channel of the color %s expected 0x%04x, got 0x%04x", ··· 252 253 } 253 254 } 254 255 255 - static void vkms_format_test_yuv_u8_to_argb_u16_case_desc(struct yuv_u8_to_argb_u16_case *t, 256 - char *desc) 256 + static void vkms_format_test_yuv_u16_to_argb_u16_case_desc(struct yuv_u16_to_argb_u16_case *t, 257 + char *desc) 257 258 { 258 259 snprintf(desc, KUNIT_PARAM_DESC_SIZE, "%s - %s", 259 260 drm_get_color_encoding_name(t->encoding), drm_get_color_range_name(t->range)); 260 261 } 261 262 262 - KUNIT_ARRAY_PARAM(yuv_u8_to_argb_u16, yuv_u8_to_argb_u16_cases, 263 - vkms_format_test_yuv_u8_to_argb_u16_case_desc 263 + KUNIT_ARRAY_PARAM(yuv_u16_to_argb_u16, yuv_u16_to_argb_u16_cases, 264 + vkms_format_test_yuv_u16_to_argb_u16_case_desc 264 265 ); 265 266 266 267 static struct kunit_case vkms_format_test_cases[] = { 267 - KUNIT_CASE_PARAM(vkms_format_test_yuv_u8_to_argb_u16, yuv_u8_to_argb_u16_gen_params), 268 + KUNIT_CASE_PARAM(vkms_format_test_yuv_u16_to_argb_u16, yuv_u16_to_argb_u16_gen_params), 268 269 {} 269 270 }; 270 271
+12 -10
drivers/gpu/drm/vkms/vkms_formats.c
··· 269 269 return out_pixel; 270 270 } 271 271 272 - VISIBLE_IF_KUNIT struct pixel_argb_u16 argb_u16_from_yuv888(u8 y, u8 channel_1, u8 channel_2, 273 - const struct conversion_matrix *matrix) 272 + VISIBLE_IF_KUNIT 273 + struct pixel_argb_u16 argb_u16_from_yuv161616(const struct conversion_matrix *matrix, 274 + u16 y, u16 channel_1, u16 channel_2) 274 275 { 275 276 u16 r, g, b; 276 277 s64 fp_y, fp_channel_1, fp_channel_2; 277 278 s64 fp_r, fp_g, fp_b; 278 279 279 - fp_y = drm_int2fixp(((int)y - matrix->y_offset) * 257); 280 - fp_channel_1 = drm_int2fixp(((int)channel_1 - 128) * 257); 281 - fp_channel_2 = drm_int2fixp(((int)channel_2 - 128) * 257); 280 + fp_y = drm_int2fixp((int)y - matrix->y_offset * 257); 281 + fp_channel_1 = drm_int2fixp((int)channel_1 - 128 * 257); 282 + fp_channel_2 = drm_int2fixp((int)channel_2 - 128 * 257); 282 283 283 284 fp_r = drm_fixp_mul(matrix->matrix[0][0], fp_y) + 284 285 drm_fixp_mul(matrix->matrix[0][1], fp_channel_1) + ··· 301 300 302 301 return argb_u16_from_u16161616(0xffff, r, g, b); 303 302 } 304 - EXPORT_SYMBOL_IF_KUNIT(argb_u16_from_yuv888); 303 + EXPORT_SYMBOL_IF_KUNIT(argb_u16_from_yuv161616); 305 304 306 305 /** 307 306 * READ_LINE() - Generic generator for a read_line function which can be used for format with one ··· 499 498 const struct conversion_matrix *conversion_matrix = &plane->conversion_matrix; 500 499 501 500 for (int i = 0; i < count; i++) { 502 - *out_pixel = argb_u16_from_yuv888(y_plane[0], uv_plane[0], uv_plane[1], 503 - conversion_matrix); 501 + *out_pixel = argb_u16_from_yuv161616(conversion_matrix, y_plane[0] * 257, 502 + uv_plane[0] * 257, uv_plane[1] * 257); 504 503 out_pixel += 1; 505 504 y_plane += step_y; 506 505 if ((i + subsampling_offset + 1) % subsampling == 0) ··· 544 543 const struct conversion_matrix *conversion_matrix = &plane->conversion_matrix; 545 544 546 545 for (int i = 0; i < count; i++) { 547 - *out_pixel = argb_u16_from_yuv888(*y_plane, *channel_1_plane, *channel_2_plane, 548 - conversion_matrix); 546 + *out_pixel = argb_u16_from_yuv161616(conversion_matrix, 547 + *y_plane * 257, *channel_1_plane * 257, 548 + *channel_2_plane * 257); 549 549 out_pixel += 1; 550 550 y_plane += step_y; 551 551 if ((i + subsampling_offset + 1) % subsampling == 0) {
+2 -2
drivers/gpu/drm/vkms/vkms_formats.h
··· 14 14 struct conversion_matrix *matrix); 15 15 16 16 #if IS_ENABLED(CONFIG_KUNIT) 17 - struct pixel_argb_u16 argb_u16_from_yuv888(u8 y, u8 channel_1, u8 channel_2, 18 - const struct conversion_matrix *matrix); 17 + struct pixel_argb_u16 argb_u16_from_yuv161616(const struct conversion_matrix *matrix, 18 + u16 y, u16 channel_1, u16 channel_2); 19 19 #endif 20 20 21 21 #endif /* _VKMS_FORMATS_H_ */