1 // SPDX-License-Identifier: GPL-2.0+
3 #include <kunit/test.h>
5 #include <drm/drm_device.h>
6 #include <drm/drm_file.h>
7 #include <drm/drm_format_helper.h>
8 #include <drm/drm_fourcc.h>
9 #include <drm/drm_framebuffer.h>
10 #include <drm/drm_gem_framebuffer_helper.h>
11 #include <drm/drm_mode.h>
12 #include <drm/drm_print.h>
13 #include <drm/drm_rect.h>
15 #include "../drm_crtc_internal.h"
17 #define TEST_BUF_SIZE 50
19 struct xrgb8888_to_rgb332_case {
22 unsigned int dst_pitch;
24 const u32 xrgb8888[TEST_BUF_SIZE];
25 const u8 expected[4 * TEST_BUF_SIZE];
28 static struct xrgb8888_to_rgb332_case xrgb8888_to_rgb332_cases[] = {
30 .name = "single_pixel_source_buffer",
33 .clip = DRM_RECT_INIT(0, 0, 1, 1),
34 .xrgb8888 = { 0x01FF0000 },
38 .name = "single_pixel_clip_rectangle",
41 .clip = DRM_RECT_INIT(1, 1, 1, 1),
43 0x00000000, 0x00000000,
44 0x00000000, 0x10FF0000,
49 /* Well known colors: White, black, red, green, blue, magenta,
50 * yellow and cyan. Different values for the X in XRGB8888 to
51 * make sure it is ignored. Partial clip area.
53 .name = "well_known_colors",
56 .clip = DRM_RECT_INIT(1, 1, 2, 4),
58 0x00000000, 0x00000000, 0x00000000, 0x00000000,
59 0x00000000, 0x11FFFFFF, 0x22000000, 0x00000000,
60 0x00000000, 0x33FF0000, 0x4400FF00, 0x00000000,
61 0x00000000, 0x550000FF, 0x66FF00FF, 0x00000000,
62 0x00000000, 0x77FFFF00, 0x8800FFFF, 0x00000000,
72 /* Randomly picked colors. Full buffer within the clip area. */
73 .name = "destination_pitch",
76 .clip = DRM_RECT_INIT(0, 0, 3, 3),
78 0xA10E449C, 0xB1114D05, 0xC1A80303,
79 0xD16C7073, 0xA20E449C, 0xB2114D05,
80 0xC2A80303, 0xD26C7073, 0xA30E449C,
83 0x0A, 0x08, 0xA0, 0x00, 0x00,
84 0x6D, 0x0A, 0x08, 0x00, 0x00,
85 0xA0, 0x6D, 0x0A, 0x00, 0x00,
91 * conversion_buf_size - Return the destination buffer size required to convert
93 * @dst_format: destination buffer pixel format (DRM_FORMAT_*)
94 * @dst_pitch: Number of bytes between two consecutive scanlines within dst
95 * @clip: Clip rectangle area to convert
98 * The size of the destination buffer or negative value on error.
100 static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
101 const struct drm_rect *clip)
103 const struct drm_format_info *dst_fi = drm_format_info(dst_format);
109 dst_pitch = drm_rect_width(clip) * dst_fi->cpp[0];
111 return dst_pitch * drm_rect_height(clip);
114 static void xrgb8888_to_rgb332_case_desc(struct xrgb8888_to_rgb332_case *t,
117 strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
120 KUNIT_ARRAY_PARAM(xrgb8888_to_rgb332, xrgb8888_to_rgb332_cases,
121 xrgb8888_to_rgb332_case_desc);
123 static void xrgb8888_to_rgb332_test(struct kunit *test)
125 const struct xrgb8888_to_rgb332_case *params = test->param_value;
129 struct drm_framebuffer fb = {
130 .format = drm_format_info(DRM_FORMAT_XRGB8888),
131 .pitches = { params->pitch, 0, 0 },
134 dst_size = conversion_buf_size(DRM_FORMAT_RGB332, params->dst_pitch,
136 KUNIT_ASSERT_GT(test, dst_size, 0);
138 dst = kunit_kzalloc(test, dst_size, GFP_KERNEL);
139 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dst);
141 drm_fb_xrgb8888_to_rgb332(dst, params->dst_pitch, params->xrgb8888,
143 KUNIT_EXPECT_EQ(test, memcmp(dst, params->expected, dst_size), 0);
146 static struct kunit_case drm_format_helper_test_cases[] = {
147 KUNIT_CASE_PARAM(xrgb8888_to_rgb332_test,
148 xrgb8888_to_rgb332_gen_params),
152 static struct kunit_suite drm_format_helper_test_suite = {
153 .name = "drm_format_helper_test",
154 .test_cases = drm_format_helper_test_cases,
157 kunit_test_suite(drm_format_helper_test_suite);
159 MODULE_DESCRIPTION("KUnit tests for the drm_format_helper APIs");
160 MODULE_LICENSE("GPL");