Skip to content

Commit f03bf05

Browse files
Harry Wentlandemersion
authored andcommitted
drm/vkms: Add tests for CTM handling
A whole slew of tests for CTM handling that greatly helped in debugging the CTM code. The extent of tests might seem a bit silly but they're fast and might someday help save someone else's day when debugging this. Reviewed-by: Louis Chauvet <louis.chauvet@bootlin.com> Signed-off-by: Alex Hung <alex.hung@amd.com> Signed-off-by: Harry Wentland <harry.wentland@amd.com> Reviewed-by: Daniel Stone <daniels@collabora.com> Signed-off-by: Simon Ser <contact@emersion.fr> Link: https://patch.msgid.link/20251115000237.3561250-23-alex.hung@amd.com
1 parent b7f5138 commit f03bf05

3 files changed

Lines changed: 251 additions & 1 deletion

File tree

drivers/gpu/drm/vkms/tests/vkms_color_test.c

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,258 @@ static void vkms_color_srgb_inv_srgb(struct kunit *test)
148148
}
149149
}
150150

151+
#define FIXPT_HALF (DRM_FIXED_ONE >> 1)
152+
#define FIXPT_QUARTER (DRM_FIXED_ONE >> 2)
153+
154+
static const struct drm_color_ctm_3x4 test_matrix_3x4_50_desat = { {
155+
FIXPT_HALF, FIXPT_QUARTER, FIXPT_QUARTER, 0,
156+
FIXPT_QUARTER, FIXPT_HALF, FIXPT_QUARTER, 0,
157+
FIXPT_QUARTER, FIXPT_QUARTER, FIXPT_HALF, 0
158+
} };
159+
160+
static void vkms_color_ctm_3x4_50_desat(struct kunit *test)
161+
{
162+
struct pixel_argb_s32 ref, out;
163+
164+
/* full white */
165+
ref.a = 0xffff;
166+
ref.r = 0xffff;
167+
ref.g = 0xffff;
168+
ref.b = 0xffff;
169+
170+
memcpy(&out, &ref, sizeof(out));
171+
apply_3x4_matrix(&out, &test_matrix_3x4_50_desat);
172+
173+
KUNIT_EXPECT_MEMEQ(test, &ref, &out, sizeof(out));
174+
175+
/* full black */
176+
ref.a = 0xffff;
177+
ref.r = 0x0;
178+
ref.g = 0x0;
179+
ref.b = 0x0;
180+
181+
memcpy(&out, &ref, sizeof(out));
182+
apply_3x4_matrix(&out, &test_matrix_3x4_50_desat);
183+
184+
KUNIT_EXPECT_MEMEQ(test, &ref, &out, sizeof(out));
185+
186+
/* 50% grey */
187+
ref.a = 0xffff;
188+
ref.r = 0x8000;
189+
ref.g = 0x8000;
190+
ref.b = 0x8000;
191+
192+
memcpy(&out, &ref, sizeof(out));
193+
apply_3x4_matrix(&out, &test_matrix_3x4_50_desat);
194+
195+
KUNIT_EXPECT_MEMEQ(test, &ref, &out, sizeof(out));
196+
197+
/* full red to 50% desat */
198+
ref.a = 0xffff;
199+
ref.r = 0x8000;
200+
ref.g = 0x4000;
201+
ref.b = 0x4000;
202+
203+
out.a = 0xffff;
204+
out.r = 0xffff;
205+
out.g = 0x0;
206+
out.b = 0x0;
207+
208+
apply_3x4_matrix(&out, &test_matrix_3x4_50_desat);
209+
210+
KUNIT_EXPECT_MEMEQ(test, &ref, &out, sizeof(out));
211+
}
212+
213+
/*
214+
* BT.709 encoding matrix
215+
*
216+
* Values printed from within IGT when converting
217+
* igt_matrix_3x4_bt709_enc to the fixed-point format expected
218+
* by DRM/KMS.
219+
*/
220+
static const struct drm_color_ctm_3x4 test_matrix_3x4_bt709_enc = { {
221+
0x00000000366cf400ull, 0x00000000b7175900ull, 0x0000000127bb300ull, 0,
222+
0x800000001993b3a0ull, 0x800000005609fe80ull, 0x000000006f9db200ull, 0,
223+
0x000000009d70a400ull, 0x800000008f011100ull, 0x800000000e6f9330ull, 0
224+
} };
225+
226+
static void vkms_color_ctm_3x4_bt709(struct kunit *test)
227+
{
228+
struct pixel_argb_s32 out;
229+
230+
/* full white to bt709 */
231+
out.a = 0xffff;
232+
out.r = 0xffff;
233+
out.g = 0xffff;
234+
out.b = 0xffff;
235+
236+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
237+
238+
/* Y 255 */
239+
KUNIT_EXPECT_GT(test, out.r, 0xfe00);
240+
KUNIT_EXPECT_LT(test, out.r, 0x10000);
241+
242+
/* U 0 */
243+
KUNIT_EXPECT_LT(test, out.g, 0x0100);
244+
245+
/* V 0 */
246+
KUNIT_EXPECT_LT(test, out.b, 0x0100);
247+
248+
/* full black to bt709 */
249+
out.a = 0xffff;
250+
out.r = 0x0;
251+
out.g = 0x0;
252+
out.b = 0x0;
253+
254+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
255+
256+
/* Y 0 */
257+
KUNIT_EXPECT_LT(test, out.r, 0x100);
258+
259+
/* U 0 */
260+
KUNIT_EXPECT_LT(test, out.g, 0x0100);
261+
262+
/* V 0 */
263+
KUNIT_EXPECT_LT(test, out.b, 0x0100);
264+
265+
/* gray to bt709 */
266+
out.a = 0xffff;
267+
out.r = 0x7fff;
268+
out.g = 0x7fff;
269+
out.b = 0x7fff;
270+
271+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
272+
273+
/* Y 127 */
274+
KUNIT_EXPECT_GT(test, out.r, 0x7e00);
275+
KUNIT_EXPECT_LT(test, out.r, 0x8000);
276+
277+
/* U 0 */
278+
KUNIT_EXPECT_LT(test, out.g, 0x0100);
279+
280+
/* V 0 */
281+
KUNIT_EXPECT_LT(test, out.b, 0x0100);
282+
283+
/* == red 255 - bt709 enc == */
284+
out.a = 0xffff;
285+
out.r = 0xffff;
286+
out.g = 0x0;
287+
out.b = 0x0;
288+
289+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
290+
291+
/* Y 54 */
292+
KUNIT_EXPECT_GT(test, out.r, 0x3500);
293+
KUNIT_EXPECT_LT(test, out.r, 0x3700);
294+
295+
/* U 0 */
296+
KUNIT_EXPECT_LT(test, out.g, 0x0100);
297+
298+
/* V 157 */
299+
KUNIT_EXPECT_GT(test, out.b, 0x9C00);
300+
KUNIT_EXPECT_LT(test, out.b, 0x9E00);
301+
302+
/* == green 255 - bt709 enc == */
303+
out.a = 0xffff;
304+
out.r = 0x0;
305+
out.g = 0xffff;
306+
out.b = 0x0;
307+
308+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
309+
310+
/* Y 182 */
311+
KUNIT_EXPECT_GT(test, out.r, 0xB500);
312+
KUNIT_EXPECT_LT(test, out.r, 0xB780); /* laxed by half*/
313+
314+
/* U 0 */
315+
KUNIT_EXPECT_LT(test, out.g, 0x0100);
316+
317+
/* V 0 */
318+
KUNIT_EXPECT_LT(test, out.b, 0x0100);
319+
320+
/* == blue 255 - bt709 enc == */
321+
out.a = 0xffff;
322+
out.r = 0x0;
323+
out.g = 0x0;
324+
out.b = 0xffff;
325+
326+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
327+
328+
/* Y 18 */
329+
KUNIT_EXPECT_GT(test, out.r, 0x1100);
330+
KUNIT_EXPECT_LT(test, out.r, 0x1300);
331+
332+
/* U 111 */
333+
KUNIT_EXPECT_GT(test, out.g, 0x6E00);
334+
KUNIT_EXPECT_LT(test, out.g, 0x7000);
335+
336+
/* V 0 */
337+
KUNIT_EXPECT_LT(test, out.b, 0x0100);
338+
339+
/* == red 140 - bt709 enc == */
340+
out.a = 0xffff;
341+
out.r = 0x8c8c;
342+
out.g = 0x0;
343+
out.b = 0x0;
344+
345+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
346+
347+
/* Y 30 */
348+
KUNIT_EXPECT_GT(test, out.r, 0x1D00);
349+
KUNIT_EXPECT_LT(test, out.r, 0x1F00);
350+
351+
/* U 0 */
352+
KUNIT_EXPECT_LT(test, out.g, 0x100);
353+
354+
/* V 87 */
355+
KUNIT_EXPECT_GT(test, out.b, 0x5600);
356+
KUNIT_EXPECT_LT(test, out.b, 0x5800);
357+
358+
/* == green 140 - bt709 enc == */
359+
out.a = 0xffff;
360+
out.r = 0x0;
361+
out.g = 0x8c8c;
362+
out.b = 0x0;
363+
364+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
365+
366+
/* Y 30 */
367+
KUNIT_EXPECT_GT(test, out.r, 0x6400);
368+
KUNIT_EXPECT_LT(test, out.r, 0x6600);
369+
370+
/* U 0 */
371+
KUNIT_EXPECT_LT(test, out.g, 0x100);
372+
373+
/* V 0 */
374+
KUNIT_EXPECT_LT(test, out.b, 0x100);
375+
376+
/* == blue 140 - bt709 enc == */
377+
out.a = 0xffff;
378+
out.r = 0x0;
379+
out.g = 0x0;
380+
out.b = 0x8c8c;
381+
382+
apply_3x4_matrix(&out, &test_matrix_3x4_bt709_enc);
383+
384+
/* Y 30 */
385+
KUNIT_EXPECT_GT(test, out.r, 0x900);
386+
KUNIT_EXPECT_LT(test, out.r, 0xB00);
387+
388+
/* U 61 */
389+
KUNIT_EXPECT_GT(test, out.g, 0x3C00);
390+
KUNIT_EXPECT_LT(test, out.g, 0x3E00);
391+
392+
/* V 0 */
393+
KUNIT_EXPECT_LT(test, out.b, 0x100);
394+
}
395+
151396
static struct kunit_case vkms_color_test_cases[] = {
152397
KUNIT_CASE(vkms_color_test_get_lut_index),
153398
KUNIT_CASE(vkms_color_test_lerp),
154399
KUNIT_CASE(vkms_color_test_linear),
155400
KUNIT_CASE(vkms_color_srgb_inv_srgb),
401+
KUNIT_CASE(vkms_color_ctm_3x4_50_desat),
402+
KUNIT_CASE(vkms_color_ctm_3x4_bt709),
156403
{}
157404
};
158405

drivers/gpu/drm/vkms/vkms_composer.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ static void apply_lut(const struct vkms_crtc_state *crtc_state, struct line_buff
128128
}
129129
}
130130

131-
static void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix)
131+
VISIBLE_IF_KUNIT void apply_3x4_matrix(struct pixel_argb_s32 *pixel,
132+
const struct drm_color_ctm_3x4 *matrix)
132133
{
133134
s64 rf, gf, bf;
134135
s64 r, g, b;
@@ -156,6 +157,7 @@ static void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_colo
156157
pixel->g = drm_fixp2int_round(gf);
157158
pixel->b = drm_fixp2int_round(bf);
158159
}
160+
EXPORT_SYMBOL_IF_KUNIT(apply_3x4_matrix);
159161

160162
static void apply_colorop(struct pixel_argb_s32 *pixel, struct drm_colorop *colorop)
161163
{

drivers/gpu/drm/vkms/vkms_composer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ u16 lerp_u16(u16 a, u16 b, s64 t);
2222
s64 get_lut_index(const struct vkms_color_lut *lut, u16 channel_value);
2323
u16 apply_lut_to_channel_value(const struct vkms_color_lut *lut, u16 channel_value,
2424
enum lut_channel channel);
25+
void apply_3x4_matrix(struct pixel_argb_s32 *pixel, const struct drm_color_ctm_3x4 *matrix);
2526
#endif
2627

2728
#endif /* _VKMS_COMPOSER_H_ */

0 commit comments

Comments
 (0)