From d08ecb56e9779d5190bc6423a1cad596756881ef Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Wed, 5 Feb 2025 12:13:36 +0200 Subject: PixelFormats: Switch to non-fourcc PixelFormat enum Signed-off-by: Tomi Valkeinen --- kms++/inc/kms++/pixelformats.h | 108 ++++++++++++++++++----------------------- kms++/src/pixelformats.cpp | 24 +++++++++ kms++/src/plane.cpp | 6 ++- 3 files changed, 76 insertions(+), 62 deletions(-) (limited to 'kms++') diff --git a/kms++/inc/kms++/pixelformats.h b/kms++/inc/kms++/pixelformats.h index 6c58f79..89d8d7e 100644 --- a/kms++/inc/kms++/pixelformats.h +++ b/kms++/inc/kms++/pixelformats.h @@ -26,84 +26,70 @@ constexpr std::string fourcc_to_str(uint32_t fourcc) return std::string(buf, 4); } -enum class PixelFormat : uint32_t { +enum class PixelFormat { Undefined = 0, - NV12 = str_to_fourcc("NV12"), - NV21 = str_to_fourcc("NV21"), - NV16 = str_to_fourcc("NV16"), - NV61 = str_to_fourcc("NV61"), + NV12, + NV21, + NV16, + NV61, - YUV420 = str_to_fourcc("YU12"), - YVU420 = str_to_fourcc("YV12"), - YUV422 = str_to_fourcc("YU16"), - YVU422 = str_to_fourcc("YV16"), - YUV444 = str_to_fourcc("YU24"), - YVU444 = str_to_fourcc("YV24"), + YUV420, + YVU420, + YUV422, + YVU422, + YUV444, + YVU444, - UYVY = str_to_fourcc("UYVY"), - YUYV = str_to_fourcc("YUYV"), - YVYU = str_to_fourcc("YVYU"), - VYUY = str_to_fourcc("VYUY"), + UYVY, + YUYV, + YVYU, + VYUY, - Y210 = str_to_fourcc("Y210"), - Y212 = str_to_fourcc("Y212"), - Y216 = str_to_fourcc("Y216"), + Y210, + Y212, + Y216, - XRGB8888 = str_to_fourcc("XR24"), - XBGR8888 = str_to_fourcc("XB24"), - RGBX8888 = str_to_fourcc("RX24"), - BGRX8888 = str_to_fourcc("BX24"), + XRGB8888, + XBGR8888, + RGBX8888, + BGRX8888, - ARGB8888 = str_to_fourcc("AR24"), - ABGR8888 = str_to_fourcc("AB24"), - RGBA8888 = str_to_fourcc("RA24"), - BGRA8888 = str_to_fourcc("BA24"), + ARGB8888, + ABGR8888, + RGBA8888, + BGRA8888, - RGB888 = str_to_fourcc("RG24"), - BGR888 = str_to_fourcc("BG24"), + RGB888, + BGR888, - RGB332 = str_to_fourcc("RGB8"), + RGB332, - RGB565 = str_to_fourcc("RG16"), - BGR565 = str_to_fourcc("BG16"), + RGB565, + BGR565, - XRGB4444 = str_to_fourcc("XR12"), - XRGB1555 = str_to_fourcc("XR15"), + XRGB4444, + XRGB1555, - ARGB4444 = str_to_fourcc("AR12"), - ARGB1555 = str_to_fourcc("AR15"), + ARGB4444, + ARGB1555, - XRGB2101010 = str_to_fourcc("XR30"), - XBGR2101010 = str_to_fourcc("XB30"), - RGBX1010102 = str_to_fourcc("RX30"), - BGRX1010102 = str_to_fourcc("BX30"), + XRGB2101010, + XBGR2101010, + RGBX1010102, + BGRX1010102, - ARGB2101010 = str_to_fourcc("AR30"), - ABGR2101010 = str_to_fourcc("AB30"), - RGBA1010102 = str_to_fourcc("RA30"), - BGRA1010102 = str_to_fourcc("BA30"), + ARGB2101010, + ABGR2101010, + RGBA1010102, + BGRA1010102, }; -inline PixelFormat fourcc_to_pixel_format(uint32_t fourcc) -{ - return (PixelFormat)fourcc; -} - -inline uint32_t pixel_format_to_fourcc(PixelFormat f) -{ - return (uint32_t)f; -} +PixelFormat fourcc_to_pixel_format(uint32_t fourcc); +uint32_t pixel_format_to_fourcc(PixelFormat f); -inline PixelFormat fourcc_str_to_pixel_format(const std::string& fourcc) -{ - return (PixelFormat)str_to_fourcc(fourcc.c_str()); -} - -inline std::string pixel_format_to_fourcc_str(PixelFormat f) -{ - return fourcc_to_str((uint32_t)f); -} +PixelFormat fourcc_str_to_pixel_format(const std::string& fourcc); +std::string pixel_format_to_fourcc_str(PixelFormat f); enum class PixelColorType { Undefined, diff --git a/kms++/src/pixelformats.cpp b/kms++/src/pixelformats.cpp index 8e3a120..cdd742c 100644 --- a/kms++/src/pixelformats.cpp +++ b/kms++/src/pixelformats.cpp @@ -235,6 +235,30 @@ const struct PixelFormatInfo& get_pixel_format_info(PixelFormat format) return format_info_array.at(format); } +PixelFormat fourcc_to_pixel_format(uint32_t fourcc) +{ + for (const auto& [fmt, pfi] : format_info_array) { + if (pfi.drm_fourcc == fourcc) + return fmt; + } + + throw invalid_argument("FourCC not supported"); +} + +uint32_t pixel_format_to_fourcc(PixelFormat f) +{ + return format_info_array.at(f).drm_fourcc; +} + +PixelFormat fourcc_str_to_pixel_format(const std::string& fourcc) +{ + return fourcc_to_pixel_format(str_to_fourcc(fourcc)); +} + +std::string pixel_format_to_fourcc_str(PixelFormat f) +{ + return fourcc_to_str(format_info_array.at(f).drm_fourcc); +} static constexpr uint32_t _div_round_up(uint32_t a, uint32_t b) { diff --git a/kms++/src/plane.cpp b/kms++/src/plane.cpp index 93b3479..7a4f26d 100644 --- a/kms++/src/plane.cpp +++ b/kms++/src/plane.cpp @@ -96,7 +96,11 @@ vector Plane::get_formats() const vector r; for (unsigned i = 0; i < p->count_formats; ++i) - r.push_back(fourcc_to_pixel_format(p->formats[i])); + try { + r.push_back(fourcc_to_pixel_format(p->formats[i])); + } catch (const std::invalid_argument&) { + // skip formats that are not supported + } return r; } -- cgit v1.2.3