From: Marek Olšák Date: Mon, 15 Mar 2021 23:55:35 +0000 (-0400) Subject: util: add new helper util_format_rgb_to_bgr X-Git-Tag: upstream/21.2.3~6199 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e3e66e1fab0286715b7d8b3130e2ffc2b44dda11;p=platform%2Fupstream%2Fmesa.git util: add new helper util_format_rgb_to_bgr We have BGR formats for all RGB formats where it matters except USCALED/SSCALED. radeonsi will use it. Reviewed-by: Bas Nieuwenhuizen Part-of: --- diff --git a/src/util/format/u_format.c b/src/util/format/u_format.c index dd7247f..43f57ca 100644 --- a/src/util/format/u_format.c +++ b/src/util/format/u_format.c @@ -994,3 +994,104 @@ util_format_snorm8_to_sint8(enum pipe_format format) return format; } } + +/** + * If the format is RGB, return BGR. If the format is BGR, return RGB. + * This may fail by returning PIPE_FORMAT_NONE. + */ +enum pipe_format +util_format_rgb_to_bgr(enum pipe_format format) +{ +#define REMAP_RGB_ONE(r, rs, g, gs, b, bs, type) \ + case PIPE_FORMAT_##r##rs##g##gs##b##bs##_##type: \ + return PIPE_FORMAT_##b##bs##g##gs##r##rs##_##type; + +#define REMAP_RGB(rs, gs, bs, type) \ + REMAP_RGB_ONE(R, rs, G, gs, B, bs, type) \ + REMAP_RGB_ONE(B, bs, G, gs, R, rs, type) \ + +#define REMAP_RGBA_ONE(r, rs, g, gs, b, bs, a, as, type) \ + case PIPE_FORMAT_##r##rs##g##gs##b##bs##a##as##_##type: \ + return PIPE_FORMAT_##b##bs##g##gs##r##rs##a##as##_##type; + +#define REMAP_ARGB_ONE(a, as, r, rs, g, gs, b, bs, type) \ + case PIPE_FORMAT_##a##as##r##rs##g##gs##b##bs##_##type: \ + return PIPE_FORMAT_##a##as##b##bs##g##gs##r##rs##_##type; + +#define REMAP_RGB_AX(A, rs, gs, bs, as, type) \ + REMAP_RGBA_ONE(R, rs, G, gs, B, bs, A, as, type) \ + REMAP_RGBA_ONE(B, bs, G, gs, R, rs, A, as, type) \ + +#define REMAP_AX_RGB(A, rs, gs, bs, as, type) \ + REMAP_ARGB_ONE(A, as, R, rs, G, gs, B, bs, type) \ + REMAP_ARGB_ONE(A, as, B, bs, G, gs, R, rs, type) \ + +#define REMAP_RGBA(rs, gs, bs, as, type) REMAP_RGB_AX(A, rs, gs, bs, as, type) +#define REMAP_RGBX(rs, gs, bs, as, type) REMAP_RGB_AX(X, rs, gs, bs, as, type) +#define REMAP_ARGB(rs, gs, bs, as, type) REMAP_AX_RGB(A, rs, gs, bs, as, type) +#define REMAP_XRGB(rs, gs, bs, as, type) REMAP_AX_RGB(X, rs, gs, bs, as, type) + +#define REMAP_RGBA_ALL(rs, gs, bs, as, type) \ + REMAP_RGBA(rs, gs, bs, as, type) \ + REMAP_RGBX(rs, gs, bs, as, type) \ + REMAP_ARGB(rs, gs, bs, as, type) \ + REMAP_XRGB(rs, gs, bs, as, type) + + switch (format) { + REMAP_RGB(3, 3, 2, UNORM); + REMAP_RGB(3, 3, 2, UINT); + REMAP_RGB(5, 6, 5, SRGB); + REMAP_RGB(5, 6, 5, UNORM); + REMAP_RGB(5, 6, 5, UINT); + REMAP_RGB(8, 8, 8, SRGB); + REMAP_RGB(8, 8, 8, UNORM); + REMAP_RGB(8, 8, 8, SNORM); + REMAP_RGB(8, 8, 8, UINT); + REMAP_RGB(8, 8, 8, SINT); + REMAP_RGB(8, 8, 8, USCALED); + REMAP_RGB(8, 8, 8, SSCALED); + + /* Complete format sets. */ + REMAP_RGBA_ALL(5, 5, 5, 1, UNORM); + REMAP_RGBA_ALL(8, 8, 8, 8, SRGB); + REMAP_RGBA_ALL(8, 8, 8, 8, UNORM); + REMAP_RGBA_ALL(8, 8, 8, 8, SNORM); + REMAP_RGBA_ALL(8, 8, 8, 8, SINT); + + /* Format sets missing XRGB/XBGR. */ + REMAP_RGBA(4, 4, 4, 4, UNORM); + REMAP_RGBX(4, 4, 4, 4, UNORM); + REMAP_ARGB(4, 4, 4, 4, UNORM); + + REMAP_RGBA(8, 8, 8, 8, UINT); + REMAP_RGBX(8, 8, 8, 8, UINT); + REMAP_ARGB(8, 8, 8, 8, UINT); + + REMAP_RGBA(10, 10, 10, 2, UNORM); + REMAP_RGBX(10, 10, 10, 2, UNORM); + REMAP_ARGB(10, 10, 10, 2, UNORM); + + /* Format sets missing a half of combinations. */ + REMAP_RGBA(4, 4, 4, 4, UINT); + REMAP_ARGB(4, 4, 4, 4, UINT); + + REMAP_RGBA(5, 5, 5, 1, UINT); + REMAP_ARGB(5, 5, 5, 1, UINT); + + REMAP_RGBA(10, 10, 10, 2, SNORM); + REMAP_RGBX(10, 10, 10, 2, SNORM); + + REMAP_RGBA(10, 10, 10, 2, UINT); + REMAP_ARGB(10, 10, 10, 2, UINT); + + /* Format sets having only RGBA/BGRA. */ + REMAP_RGBA(8, 8, 8, 8, USCALED); + REMAP_RGBA(8, 8, 8, 8, SSCALED); + REMAP_RGBA(10, 10, 10, 2, SINT); + REMAP_RGBA(10, 10, 10, 2, USCALED); + REMAP_RGBA(10, 10, 10, 2, SSCALED); + + default: + return PIPE_FORMAT_NONE; + } +} diff --git a/src/util/format/u_format.h b/src/util/format/u_format.h index 984f846..9911ff5 100644 --- a/src/util/format/u_format.h +++ b/src/util/format/u_format.h @@ -1625,6 +1625,13 @@ util_copy_rect(ubyte * dst, enum pipe_format format, unsigned width, unsigned height, const ubyte * src, int src_stride, unsigned src_x, unsigned src_y); +/** + * If the format is RGB, return BGR. If the format is BGR, return RGB. + * This may fail by returning PIPE_FORMAT_NONE. + */ +enum pipe_format +util_format_rgb_to_bgr(enum pipe_format format); + #ifdef __cplusplus } // extern "C" { #endif