From 0e0fe5b73ac10bd6a4c01614e59a04a42750a3b0 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sat, 12 Feb 2022 12:30:12 +0000 Subject: [PATCH] pixels: Add function to convert drm format to string --- include/pixels.h | 2 ++ src/pixels.c | 36 ++++++++++++++++++++++++++++++++++++ test/test-pixels.c | 17 ++++++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/include/pixels.h b/include/pixels.h index f20feb6..ba32783 100644 --- a/include/pixels.h +++ b/include/pixels.h @@ -38,3 +38,5 @@ bool fourcc_to_pixman_fmt(pixman_format_code_t* dst, uint32_t src); bool extract_alpha_mask(uint8_t* dst, const void* src, uint32_t format, size_t len); + +const char* drm_format_to_string(uint32_t fmt); diff --git a/src/pixels.c b/src/pixels.c index b6e3589..47fdbe1 100644 --- a/src/pixels.c +++ b/src/pixels.c @@ -22,6 +22,8 @@ #define POPCOUNT(x) __builtin_popcount(x) #define UDIV_UP(a, b) (((a) + (b) - 1) / (b)) +#define XSTR(s) STR(s) +#define STR(s) #s void pixel32_to_cpixel(uint8_t* restrict dst, const struct rfb_pixel_format* dst_fmt, @@ -434,3 +436,37 @@ bool extract_alpha_mask(uint8_t* dst, const void* src, uint32_t format, return false; } + +const char* drm_format_to_string(uint32_t fmt) +{ + switch (fmt) { +#define X(x) case DRM_FORMAT_ ## x: return XSTR(x); + X(RGBA1010102) \ + X(RGBX1010102) \ + X(BGRA1010102) \ + X(BGRX1010102) \ + X(ARGB2101010) \ + X(XRGB2101010) \ + X(ABGR2101010) \ + X(XBGR2101010) \ + X(RGBA8888) \ + X(RGBX8888) \ + X(BGRA8888) \ + X(BGRX8888) \ + X(ARGB8888) \ + X(XRGB8888) \ + X(ABGR8888) \ + X(XBGR8888) \ + X(RGBA4444) \ + X(RGBX4444) \ + X(BGRA4444) \ + X(BGRX4444) \ + X(ARGB4444) \ + X(XRGB4444) \ + X(ABGR4444) \ + X(XBGR4444) \ + X(RGB565) +#undef X + } + return "UNKNOWN"; +} diff --git a/test/test-pixels.c b/test/test-pixels.c index 4782d00..946ce61 100644 --- a/test/test-pixels.c +++ b/test/test-pixels.c @@ -130,10 +130,25 @@ static bool test_extract_alpha_mask_rgba8888(void) return true; } +static bool test_drm_format_to_string(void) +{ + if (strcmp(drm_format_to_string(DRM_FORMAT_RGBA8888), "RGBA8888") != 0) + return false; + + if (strcmp(drm_format_to_string(DRM_FORMAT_RGBX8888), "RGBX8888") != 0) + return false; + + if (strcmp(drm_format_to_string(DRM_FORMAT_RGB565), "RGB565") != 0) + return false; + + return true; +} + int main() { bool ok = test_pixel32_to_cpixel_4bpp() && test_fourcc_to_pixman_fmt() && - test_extract_alpha_mask_rgba8888(); + test_extract_alpha_mask_rgba8888() && + test_drm_format_to_string(); return ok ? 0 : 1; }