Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 
0003 #include <kunit/test.h>
0004 
0005 #include <drm/drm_device.h>
0006 #include <drm/drm_file.h>
0007 #include <drm/drm_format_helper.h>
0008 #include <drm/drm_fourcc.h>
0009 #include <drm/drm_framebuffer.h>
0010 #include <drm/drm_gem_framebuffer_helper.h>
0011 #include <drm/drm_mode.h>
0012 #include <drm/drm_print.h>
0013 #include <drm/drm_rect.h>
0014 
0015 #include "../drm_crtc_internal.h"
0016 
0017 #define TEST_BUF_SIZE 50
0018 
0019 struct xrgb8888_to_rgb332_case {
0020     const char *name;
0021     unsigned int pitch;
0022     unsigned int dst_pitch;
0023     struct drm_rect clip;
0024     const u32 xrgb8888[TEST_BUF_SIZE];
0025     const u8 expected[4 * TEST_BUF_SIZE];
0026 };
0027 
0028 static struct xrgb8888_to_rgb332_case xrgb8888_to_rgb332_cases[] = {
0029     {
0030         .name = "single_pixel_source_buffer",
0031         .pitch = 1 * 4,
0032         .dst_pitch = 0,
0033         .clip = DRM_RECT_INIT(0, 0, 1, 1),
0034         .xrgb8888 = { 0x01FF0000 },
0035         .expected = { 0xE0 },
0036     },
0037     {
0038         .name = "single_pixel_clip_rectangle",
0039         .pitch = 2 * 4,
0040         .dst_pitch = 0,
0041         .clip = DRM_RECT_INIT(1, 1, 1, 1),
0042         .xrgb8888 = {
0043             0x00000000, 0x00000000,
0044             0x00000000, 0x10FF0000,
0045         },
0046         .expected = { 0xE0 },
0047     },
0048     {
0049         /* Well known colors: White, black, red, green, blue, magenta,
0050          * yellow and cyan. Different values for the X in XRGB8888 to
0051          * make sure it is ignored. Partial clip area.
0052          */
0053         .name = "well_known_colors",
0054         .pitch = 4 * 4,
0055         .dst_pitch = 0,
0056         .clip = DRM_RECT_INIT(1, 1, 2, 4),
0057         .xrgb8888 = {
0058             0x00000000, 0x00000000, 0x00000000, 0x00000000,
0059             0x00000000, 0x11FFFFFF, 0x22000000, 0x00000000,
0060             0x00000000, 0x33FF0000, 0x4400FF00, 0x00000000,
0061             0x00000000, 0x550000FF, 0x66FF00FF, 0x00000000,
0062             0x00000000, 0x77FFFF00, 0x8800FFFF, 0x00000000,
0063         },
0064         .expected = {
0065             0xFF, 0x00,
0066             0xE0, 0x1C,
0067             0x03, 0xE3,
0068             0xFC, 0x1F,
0069         },
0070     },
0071     {
0072         /* Randomly picked colors. Full buffer within the clip area. */
0073         .name = "destination_pitch",
0074         .pitch = 3 * 4,
0075         .dst_pitch = 5,
0076         .clip = DRM_RECT_INIT(0, 0, 3, 3),
0077         .xrgb8888 = {
0078             0xA10E449C, 0xB1114D05, 0xC1A80303,
0079             0xD16C7073, 0xA20E449C, 0xB2114D05,
0080             0xC2A80303, 0xD26C7073, 0xA30E449C,
0081         },
0082         .expected = {
0083             0x0A, 0x08, 0xA0, 0x00, 0x00,
0084             0x6D, 0x0A, 0x08, 0x00, 0x00,
0085             0xA0, 0x6D, 0x0A, 0x00, 0x00,
0086         },
0087     },
0088 };
0089 
0090 /*
0091  * conversion_buf_size - Return the destination buffer size required to convert
0092  * between formats.
0093  * @dst_format: destination buffer pixel format (DRM_FORMAT_*)
0094  * @dst_pitch: Number of bytes between two consecutive scanlines within dst
0095  * @clip: Clip rectangle area to convert
0096  *
0097  * Returns:
0098  * The size of the destination buffer or negative value on error.
0099  */
0100 static size_t conversion_buf_size(u32 dst_format, unsigned int dst_pitch,
0101                   const struct drm_rect *clip)
0102 {
0103     const struct drm_format_info *dst_fi = drm_format_info(dst_format);
0104 
0105     if (!dst_fi)
0106         return -EINVAL;
0107 
0108     if (!dst_pitch)
0109         dst_pitch = drm_rect_width(clip) * dst_fi->cpp[0];
0110 
0111     return dst_pitch * drm_rect_height(clip);
0112 }
0113 
0114 static void xrgb8888_to_rgb332_case_desc(struct xrgb8888_to_rgb332_case *t,
0115                      char *desc)
0116 {
0117     strscpy(desc, t->name, KUNIT_PARAM_DESC_SIZE);
0118 }
0119 
0120 KUNIT_ARRAY_PARAM(xrgb8888_to_rgb332, xrgb8888_to_rgb332_cases,
0121           xrgb8888_to_rgb332_case_desc);
0122 
0123 static void xrgb8888_to_rgb332_test(struct kunit *test)
0124 {
0125     const struct xrgb8888_to_rgb332_case *params = test->param_value;
0126     size_t dst_size;
0127     __u8 *dst = NULL;
0128 
0129     struct drm_framebuffer fb = {
0130         .format = drm_format_info(DRM_FORMAT_XRGB8888),
0131         .pitches = { params->pitch, 0, 0 },
0132     };
0133 
0134     dst_size = conversion_buf_size(DRM_FORMAT_RGB332, params->dst_pitch,
0135                        &params->clip);
0136     KUNIT_ASSERT_GT(test, dst_size, 0);
0137 
0138     dst = kunit_kzalloc(test, dst_size, GFP_KERNEL);
0139     KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dst);
0140 
0141     drm_fb_xrgb8888_to_rgb332(dst, params->dst_pitch, params->xrgb8888,
0142                   &fb, &params->clip);
0143     KUNIT_EXPECT_EQ(test, memcmp(dst, params->expected, dst_size), 0);
0144 }
0145 
0146 static struct kunit_case drm_format_helper_test_cases[] = {
0147     KUNIT_CASE_PARAM(xrgb8888_to_rgb332_test,
0148              xrgb8888_to_rgb332_gen_params),
0149     {}
0150 };
0151 
0152 static struct kunit_suite drm_format_helper_test_suite = {
0153     .name = "drm_format_helper_test",
0154     .test_cases = drm_format_helper_test_cases,
0155 };
0156 
0157 kunit_test_suite(drm_format_helper_test_suite);
0158 
0159 MODULE_DESCRIPTION("KUnit tests for the drm_format_helper APIs");
0160 MODULE_LICENSE("GPL");
0161 MODULE_AUTHOR("José Expósito <jose.exposito89@gmail.com>");