Skip to content

Commit c904370

Browse files
kekrbytdz
authored andcommitted
drm/format-helper: Add conversion from XRGB8888 to BGR888
Add XRGB8888 emulation helper for devices that only support BGR888. Signed-off-by: Kerem Karabay <[email protected]> Signed-off-by: Aditya Garg <[email protected]> Reviewed-by: Thomas Zimmermann <[email protected]> Signed-off-by: Thomas Zimmermann <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 16e57a7 commit c904370

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

drivers/gpu/drm/drm_format_helper.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,57 @@ void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pi
702702
}
703703
EXPORT_SYMBOL(drm_fb_xrgb8888_to_rgb888);
704704

705+
static void drm_fb_xrgb8888_to_bgr888_line(void *dbuf, const void *sbuf, unsigned int pixels)
706+
{
707+
u8 *dbuf8 = dbuf;
708+
const __le32 *sbuf32 = sbuf;
709+
unsigned int x;
710+
u32 pix;
711+
712+
for (x = 0; x < pixels; x++) {
713+
pix = le32_to_cpu(sbuf32[x]);
714+
/* write red-green-blue to output in little endianness */
715+
*dbuf8++ = (pix & 0x00ff0000) >> 16;
716+
*dbuf8++ = (pix & 0x0000ff00) >> 8;
717+
*dbuf8++ = (pix & 0x000000ff) >> 0;
718+
}
719+
}
720+
721+
/**
722+
* drm_fb_xrgb8888_to_bgr888 - Convert XRGB8888 to BGR888 clip buffer
723+
* @dst: Array of BGR888 destination buffers
724+
* @dst_pitch: Array of numbers of bytes between the start of two consecutive scanlines
725+
* within @dst; can be NULL if scanlines are stored next to each other.
726+
* @src: Array of XRGB8888 source buffers
727+
* @fb: DRM framebuffer
728+
* @clip: Clip rectangle area to copy
729+
* @state: Transform and conversion state
730+
*
731+
* This function copies parts of a framebuffer to display memory and converts the
732+
* color format during the process. Destination and framebuffer formats must match. The
733+
* parameters @dst, @dst_pitch and @src refer to arrays. Each array must have at
734+
* least as many entries as there are planes in @fb's format. Each entry stores the
735+
* value for the format's respective color plane at the same index.
736+
*
737+
* This function does not apply clipping on @dst (i.e. the destination is at the
738+
* top-left corner).
739+
*
740+
* Drivers can use this function for BGR888 devices that don't natively
741+
* support XRGB8888.
742+
*/
743+
void drm_fb_xrgb8888_to_bgr888(struct iosys_map *dst, const unsigned int *dst_pitch,
744+
const struct iosys_map *src, const struct drm_framebuffer *fb,
745+
const struct drm_rect *clip, struct drm_format_conv_state *state)
746+
{
747+
static const u8 dst_pixsize[DRM_FORMAT_MAX_PLANES] = {
748+
3,
749+
};
750+
751+
drm_fb_xfrm(dst, dst_pitch, dst_pixsize, src, fb, clip, false, state,
752+
drm_fb_xrgb8888_to_bgr888_line);
753+
}
754+
EXPORT_SYMBOL(drm_fb_xrgb8888_to_bgr888);
755+
705756
static void drm_fb_xrgb8888_to_argb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
706757
{
707758
__le32 *dbuf32 = dbuf;
@@ -1104,6 +1155,9 @@ int drm_fb_blit(struct iosys_map *dst, const unsigned int *dst_pitch, uint32_t d
11041155
} else if (dst_format == DRM_FORMAT_RGB888) {
11051156
drm_fb_xrgb8888_to_rgb888(dst, dst_pitch, src, fb, clip, state);
11061157
return 0;
1158+
} else if (dst_format == DRM_FORMAT_BGR888) {
1159+
drm_fb_xrgb8888_to_bgr888(dst, dst_pitch, src, fb, clip, state);
1160+
return 0;
11071161
} else if (dst_format == DRM_FORMAT_ARGB8888) {
11081162
drm_fb_xrgb8888_to_argb8888(dst, dst_pitch, src, fb, clip, state);
11091163
return 0;

drivers/gpu/drm/tests/drm_format_helper_test.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ struct convert_to_rgb888_result {
6060
const u8 expected[TEST_BUF_SIZE];
6161
};
6262

63+
struct convert_to_bgr888_result {
64+
unsigned int dst_pitch;
65+
const u8 expected[TEST_BUF_SIZE];
66+
};
67+
6368
struct convert_to_argb8888_result {
6469
unsigned int dst_pitch;
6570
const u32 expected[TEST_BUF_SIZE];
@@ -107,6 +112,7 @@ struct convert_xrgb8888_case {
107112
struct convert_to_argb1555_result argb1555_result;
108113
struct convert_to_rgba5551_result rgba5551_result;
109114
struct convert_to_rgb888_result rgb888_result;
115+
struct convert_to_bgr888_result bgr888_result;
110116
struct convert_to_argb8888_result argb8888_result;
111117
struct convert_to_xrgb2101010_result xrgb2101010_result;
112118
struct convert_to_argb2101010_result argb2101010_result;
@@ -151,6 +157,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
151157
.dst_pitch = TEST_USE_DEFAULT_PITCH,
152158
.expected = { 0x00, 0x00, 0xFF },
153159
},
160+
.bgr888_result = {
161+
.dst_pitch = TEST_USE_DEFAULT_PITCH,
162+
.expected = { 0xFF, 0x00, 0x00 },
163+
},
154164
.argb8888_result = {
155165
.dst_pitch = TEST_USE_DEFAULT_PITCH,
156166
.expected = { 0xFFFF0000 },
@@ -217,6 +227,10 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
217227
.dst_pitch = TEST_USE_DEFAULT_PITCH,
218228
.expected = { 0x00, 0x00, 0xFF },
219229
},
230+
.bgr888_result = {
231+
.dst_pitch = TEST_USE_DEFAULT_PITCH,
232+
.expected = { 0xFF, 0x00, 0x00 },
233+
},
220234
.argb8888_result = {
221235
.dst_pitch = TEST_USE_DEFAULT_PITCH,
222236
.expected = { 0xFFFF0000 },
@@ -330,6 +344,15 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
330344
0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
331345
},
332346
},
347+
.bgr888_result = {
348+
.dst_pitch = TEST_USE_DEFAULT_PITCH,
349+
.expected = {
350+
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00,
351+
0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00,
352+
0x00, 0x00, 0xFF, 0xFF, 0x00, 0xFF,
353+
0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF,
354+
},
355+
},
333356
.argb8888_result = {
334357
.dst_pitch = TEST_USE_DEFAULT_PITCH,
335358
.expected = {
@@ -468,6 +491,17 @@ static struct convert_xrgb8888_case convert_xrgb8888_cases[] = {
468491
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
469492
},
470493
},
494+
.bgr888_result = {
495+
.dst_pitch = 15,
496+
.expected = {
497+
0x0E, 0x44, 0x9C, 0x11, 0x4D, 0x05, 0xA8, 0xF3, 0x03,
498+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
499+
0x6C, 0xF0, 0x73, 0x0E, 0x44, 0x9C, 0x11, 0x4D, 0x05,
500+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
501+
0xA8, 0x03, 0x03, 0x6C, 0xF0, 0x73, 0x0E, 0x44, 0x9C,
502+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
503+
},
504+
},
471505
.argb8888_result = {
472506
.dst_pitch = 20,
473507
.expected = {
@@ -914,6 +948,52 @@ static void drm_test_fb_xrgb8888_to_rgb888(struct kunit *test)
914948
KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
915949
}
916950

951+
static void drm_test_fb_xrgb8888_to_bgr888(struct kunit *test)
952+
{
953+
const struct convert_xrgb8888_case *params = test->param_value;
954+
const struct convert_to_bgr888_result *result = &params->bgr888_result;
955+
size_t dst_size;
956+
u8 *buf = NULL;
957+
__le32 *xrgb8888 = NULL;
958+
struct iosys_map dst, src;
959+
960+
struct drm_framebuffer fb = {
961+
.format = drm_format_info(DRM_FORMAT_XRGB8888),
962+
.pitches = { params->pitch, 0, 0 },
963+
};
964+
965+
dst_size = conversion_buf_size(DRM_FORMAT_BGR888, result->dst_pitch,
966+
&params->clip, 0);
967+
KUNIT_ASSERT_GT(test, dst_size, 0);
968+
969+
buf = kunit_kzalloc(test, dst_size, GFP_KERNEL);
970+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
971+
iosys_map_set_vaddr(&dst, buf);
972+
973+
xrgb8888 = cpubuf_to_le32(test, params->xrgb8888, TEST_BUF_SIZE);
974+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, xrgb8888);
975+
iosys_map_set_vaddr(&src, xrgb8888);
976+
977+
/*
978+
* BGR888 expected results are already in little-endian
979+
* order, so there's no need to convert the test output.
980+
*/
981+
drm_fb_xrgb8888_to_bgr888(&dst, &result->dst_pitch, &src, &fb, &params->clip,
982+
&fmtcnv_state);
983+
KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
984+
985+
buf = dst.vaddr; /* restore original value of buf */
986+
memset(buf, 0, dst_size);
987+
988+
int blit_result = 0;
989+
990+
blit_result = drm_fb_blit(&dst, &result->dst_pitch, DRM_FORMAT_BGR888, &src, &fb, &params->clip,
991+
&fmtcnv_state);
992+
993+
KUNIT_EXPECT_FALSE(test, blit_result);
994+
KUNIT_EXPECT_MEMEQ(test, buf, result->expected, dst_size);
995+
}
996+
917997
static void drm_test_fb_xrgb8888_to_argb8888(struct kunit *test)
918998
{
919999
const struct convert_xrgb8888_case *params = test->param_value;
@@ -1851,6 +1931,7 @@ static struct kunit_case drm_format_helper_test_cases[] = {
18511931
KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb1555, convert_xrgb8888_gen_params),
18521932
KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_rgba5551, convert_xrgb8888_gen_params),
18531933
KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_rgb888, convert_xrgb8888_gen_params),
1934+
KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_bgr888, convert_xrgb8888_gen_params),
18541935
KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb8888, convert_xrgb8888_gen_params),
18551936
KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_xrgb2101010, convert_xrgb8888_gen_params),
18561937
KUNIT_CASE_PARAM(drm_test_fb_xrgb8888_to_argb2101010, convert_xrgb8888_gen_params),

include/drm/drm_format_helper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ void drm_fb_xrgb8888_to_rgba5551(struct iosys_map *dst, const unsigned int *dst_
9696
void drm_fb_xrgb8888_to_rgb888(struct iosys_map *dst, const unsigned int *dst_pitch,
9797
const struct iosys_map *src, const struct drm_framebuffer *fb,
9898
const struct drm_rect *clip, struct drm_format_conv_state *state);
99+
void drm_fb_xrgb8888_to_bgr888(struct iosys_map *dst, const unsigned int *dst_pitch,
100+
const struct iosys_map *src, const struct drm_framebuffer *fb,
101+
const struct drm_rect *clip, struct drm_format_conv_state *state);
99102
void drm_fb_xrgb8888_to_argb8888(struct iosys_map *dst, const unsigned int *dst_pitch,
100103
const struct iosys_map *src, const struct drm_framebuffer *fb,
101104
const struct drm_rect *clip, struct drm_format_conv_state *state);

0 commit comments

Comments
 (0)