[Mesa-dev] [PATCH 2/2] st/dri: replace format conversion functions with single mapping table

Marek Olšák maraeo at gmail.com
Sat May 26 04:36:30 UTC 2018


On Thu, May 17, 2018 at 6:50 AM, Lucas Stach <l.stach at pengutronix.de> wrote:

> Each time I have to touch the buffer import/export functions in the dri
> state tracker I get lost in the maze of functions converting between
> DRI_IMAGE_FOURCC, DRI_IMAGE_FORMAT, DRI_IMAGE_COMPONENTS and pipe format.
>
> Rip it out and replace by a single table, which defines the correspondence
> between the different representations.
>
> Also this now stores all the known representations in the __DRIimageRec,
> to avoid the loss of information we currently have when importing a buffer
> with a fourcc, which doesn't have a corresponding dri format.
>
> Signed-off-by: Lucas Stach <l.stach at pengutronix.de>
> ---
>  src/gallium/state_trackers/dri/dri2.c       | 476 ++++++--------------
>  src/gallium/state_trackers/dri/dri_screen.h |   1 +
>  2 files changed, 138 insertions(+), 339 deletions(-)
>
> diff --git a/src/gallium/state_trackers/dri/dri2.c
> b/src/gallium/state_trackers/dri/dri2.c
> index 859161fb87ac..9c74ca54fc89 100644
> --- a/src/gallium/state_trackers/dri/dri2.c
> +++ b/src/gallium/state_trackers/dri/dri2.c
> @@ -54,295 +54,72 @@
>  #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
>  #endif
>
> -static const int fourcc_formats[] = {
> -   __DRI_IMAGE_FOURCC_ARGB2101010,
> -   __DRI_IMAGE_FOURCC_XRGB2101010,
> -   __DRI_IMAGE_FOURCC_ABGR2101010,
> -   __DRI_IMAGE_FOURCC_XBGR2101010,
> -   __DRI_IMAGE_FOURCC_ARGB8888,
> -   __DRI_IMAGE_FOURCC_ABGR8888,
> -   __DRI_IMAGE_FOURCC_SARGB8888,
> -   __DRI_IMAGE_FOURCC_XRGB8888,
> -   __DRI_IMAGE_FOURCC_XBGR8888,
> -   __DRI_IMAGE_FOURCC_ARGB1555,
> -   __DRI_IMAGE_FOURCC_RGB565,
> -   __DRI_IMAGE_FOURCC_R8,
> -   __DRI_IMAGE_FOURCC_R16,
> -   __DRI_IMAGE_FOURCC_GR88,
> -   __DRI_IMAGE_FOURCC_GR1616,
> -   __DRI_IMAGE_FOURCC_YUV410,
> -   __DRI_IMAGE_FOURCC_YUV411,
> -   __DRI_IMAGE_FOURCC_YUV420,
> -   __DRI_IMAGE_FOURCC_YUV422,
> -   __DRI_IMAGE_FOURCC_YUV444,
> -   __DRI_IMAGE_FOURCC_YVU410,
> -   __DRI_IMAGE_FOURCC_YVU411,
> -   __DRI_IMAGE_FOURCC_YVU420,
> -   __DRI_IMAGE_FOURCC_YVU422,
> -   __DRI_IMAGE_FOURCC_YVU444,
> -   __DRI_IMAGE_FOURCC_NV12,
> -   __DRI_IMAGE_FOURCC_NV16,
> -   __DRI_IMAGE_FOURCC_YUYV
> -};
> -
> -static int convert_fourcc(int format, int *dri_components_p)
> -{
> +struct dri2_format_mapping {
> +   int dri_fourcc;
> +   int dri_format;
>     int dri_components;
> -   switch(format) {
> -   case __DRI_IMAGE_FOURCC_RGB565:
> -      format = __DRI_IMAGE_FORMAT_RGB565;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGB;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ARGB8888:
> -      format = __DRI_IMAGE_FORMAT_ARGB8888;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XRGB8888:
> -      format = __DRI_IMAGE_FORMAT_XRGB8888;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGB;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ABGR8888:
> -      format = __DRI_IMAGE_FORMAT_ABGR8888;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XBGR8888:
> -      format = __DRI_IMAGE_FORMAT_XBGR8888;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGB;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ARGB2101010:
> -      format = __DRI_IMAGE_FORMAT_ARGB2101010;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XRGB2101010:
> -      format = __DRI_IMAGE_FORMAT_XRGB2101010;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGB;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ABGR2101010:
> -      format = __DRI_IMAGE_FORMAT_ABGR2101010;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGBA;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XBGR2101010:
> -      format = __DRI_IMAGE_FORMAT_XBGR2101010;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RGB;
> -      break;
> -   case __DRI_IMAGE_FOURCC_R8:
> -      format = __DRI_IMAGE_FORMAT_R8;
> -      dri_components = __DRI_IMAGE_COMPONENTS_R;
> -      break;
> -   case __DRI_IMAGE_FOURCC_GR88:
> -      format = __DRI_IMAGE_FORMAT_GR88;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RG;
> -      break;
> -   case __DRI_IMAGE_FOURCC_R16:
> -      format = __DRI_IMAGE_FORMAT_R16;
> -      dri_components = __DRI_IMAGE_COMPONENTS_R;
> -      break;
> -   case __DRI_IMAGE_FOURCC_GR1616:
> -      format = __DRI_IMAGE_FORMAT_GR1616;
> -      dri_components = __DRI_IMAGE_COMPONENTS_RG;
> -      break;
> -   case __DRI_IMAGE_FOURCC_YUYV:
> -      format = __DRI_IMAGE_FORMAT_YUYV;
> -      dri_components = __DRI_IMAGE_COMPONENTS_Y_XUXV;
> -      break;
> -   /*
> -    * For multi-planar YUV formats, we return the format of the first
> -    * plane only.  Since there is only one caller which supports multi-
> -    * planar YUV it gets to figure out the remaining planes on it's
> -    * own.
> -    */
> -   case __DRI_IMAGE_FOURCC_YUV420:
> -   case __DRI_IMAGE_FOURCC_YVU420:
> -      format = __DRI_IMAGE_FORMAT_R8;
> -      dri_components = __DRI_IMAGE_COMPONENTS_Y_U_V;
> -      break;
> -   case __DRI_IMAGE_FOURCC_NV12:
> -      format = __DRI_IMAGE_FORMAT_R8;
> -      dri_components = __DRI_IMAGE_COMPONENTS_Y_UV;
> -      break;
> -   default:
> -      return -1;
> -   }
> -   *dri_components_p = dri_components;
> -   return format;
> -}
> -
> -/* NOTE this probably isn't going to do the right thing for YUV images
> - * (but I think the same can be said for intel_query_image()).  I think
> - * only needed for exporting dmabuf's, so I think I won't loose much
> - * sleep over it.
> - */
> -static int convert_to_fourcc(int format)
> -{
> -   switch(format) {
> -   case __DRI_IMAGE_FORMAT_RGB565:
> -      format = __DRI_IMAGE_FOURCC_RGB565;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ARGB8888:
> -      format = __DRI_IMAGE_FOURCC_ARGB8888;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XRGB8888:
> -      format = __DRI_IMAGE_FOURCC_XRGB8888;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ABGR8888:
> -      format = __DRI_IMAGE_FOURCC_ABGR8888;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XBGR8888:
> -      format = __DRI_IMAGE_FOURCC_XBGR8888;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ARGB2101010:
> -      format = __DRI_IMAGE_FOURCC_ARGB2101010;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XRGB2101010:
> -      format = __DRI_IMAGE_FOURCC_XRGB2101010;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ABGR2101010:
> -      format = __DRI_IMAGE_FOURCC_ABGR2101010;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XBGR2101010:
> -      format = __DRI_IMAGE_FOURCC_XBGR2101010;
> -      break;
> -   case __DRI_IMAGE_FORMAT_R8:
> -      format = __DRI_IMAGE_FOURCC_R8;
> -      break;
> -   case __DRI_IMAGE_FORMAT_GR88:
> -      format = __DRI_IMAGE_FOURCC_GR88;
> -      break;
> -   default:
> -      return -1;
> -   }
> -   return format;
> -}
> +   enum pipe_format pipe_format;
> +};
>
> -static enum pipe_format dri2_format_to_pipe_format (int format)
> -{
> -   enum pipe_format pf;
> +static const struct dri2_format_mapping dri2_format_table[] = {
> +      { __DRI_IMAGE_FOURCC_ARGB2101010,   __DRI_IMAGE_FORMAT_ARGB2101010,
> +        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_B10G10R10A2_UNORM },
> +      { __DRI_IMAGE_FOURCC_XRGB2101010,   __DRI_IMAGE_FORMAT_XRGB2101010,
> +        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_B10G10R10X2_UNORM },
> +      { __DRI_IMAGE_FOURCC_ABGR2101010,   __DRI_IMAGE_FORMAT_ABGR2101010,
> +        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_R10G10B10A2_UNORM },
> +      { __DRI_IMAGE_FOURCC_XBGR2101010,   __DRI_IMAGE_FORMAT_XBGR2101010,
> +        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_R10G10B10X2_UNORM },
> +      { __DRI_IMAGE_FOURCC_ARGB8888,      __DRI_IMAGE_FORMAT_ARGB8888,
> +        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_BGRA8888_UNORM },
> +      { __DRI_IMAGE_FOURCC_ABGR8888,      __DRI_IMAGE_FORMAT_ABGR8888,
> +        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_RGBA8888_UNORM },
> +      { __DRI_IMAGE_FOURCC_SARGB8888,     __DRI_IMAGE_FORMAT_SARGB8,
> +        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_BGRA8888_SRGB },
> +      { __DRI_IMAGE_FOURCC_XRGB8888,      __DRI_IMAGE_FORMAT_XRGB8888,
> +        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_BGRX8888_UNORM },
> +      { __DRI_IMAGE_FOURCC_XBGR8888,      __DRI_IMAGE_FORMAT_XBGR8888,
> +        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_RGBX8888_UNORM },
> +      { __DRI_IMAGE_FOURCC_ARGB1555,      __DRI_IMAGE_FORMAT_NONE,
> +        __DRI_IMAGE_COMPONENTS_RGBA,      PIPE_FORMAT_B5G5R5A1_UNORM },
> +      { __DRI_IMAGE_FOURCC_RGB565,        __DRI_IMAGE_FORMAT_RGB565,
> +        __DRI_IMAGE_COMPONENTS_RGB,       PIPE_FORMAT_B5G6R5_UNORM },
> +      { __DRI_IMAGE_FOURCC_R8,            __DRI_IMAGE_FORMAT_R8,
> +        __DRI_IMAGE_COMPONENTS_R,         PIPE_FORMAT_R8_UNORM },
> +      { __DRI_IMAGE_FOURCC_R16,           __DRI_IMAGE_FORMAT_R16,
> +        __DRI_IMAGE_COMPONENTS_R,         PIPE_FORMAT_R16_UNORM },
> +      { __DRI_IMAGE_FOURCC_GR88,          __DRI_IMAGE_FORMAT_GR88,
> +        __DRI_IMAGE_COMPONENTS_RG,        PIPE_FORMAT_RG88_UNORM },
> +      { __DRI_IMAGE_FOURCC_GR1616,        __DRI_IMAGE_FORMAT_GR88,
> +        __DRI_IMAGE_COMPONENTS_RG,        PIPE_FORMAT_RG1616_UNORM },
> +      { __DRI_IMAGE_FOURCC_YUV420,        __DRI_IMAGE_FORMAT_NONE,
> +        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_IYUV },
> +      { __DRI_IMAGE_FOURCC_YVU420,        __DRI_IMAGE_FORMAT_NONE,
> +        __DRI_IMAGE_COMPONENTS_Y_U_V,     PIPE_FORMAT_YV12 },
> +      { __DRI_IMAGE_FOURCC_NV12,          __DRI_IMAGE_FORMAT_NONE,
> +        __DRI_IMAGE_COMPONENTS_Y_UV,      PIPE_FORMAT_NV12 },
> +      { __DRI_IMAGE_FOURCC_YUYV,          __DRI_IMAGE_FORMAT_YUYV,
> +        __DRI_IMAGE_COMPONENTS_Y_XUXV,    PIPE_FORMAT_YUYV },
> +};
>
> -   switch (format) {
> -   case __DRI_IMAGE_FORMAT_RGB565:
> -      pf = PIPE_FORMAT_B5G6R5_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XRGB8888:
> -      pf = PIPE_FORMAT_BGRX8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ARGB8888:
> -      pf = PIPE_FORMAT_BGRA8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XBGR8888:
> -      pf = PIPE_FORMAT_RGBX8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ABGR8888:
> -      pf = PIPE_FORMAT_RGBA8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XRGB2101010:
> -      pf = PIPE_FORMAT_B10G10R10X2_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ARGB2101010:
> -      pf = PIPE_FORMAT_B10G10R10A2_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_XBGR2101010:
> -      pf = PIPE_FORMAT_R10G10B10X2_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_ABGR2101010:
> -      pf = PIPE_FORMAT_R10G10B10A2_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_R8:
> -      pf = PIPE_FORMAT_R8_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_GR88:
> -      pf = PIPE_FORMAT_RG88_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_R16:
> -      pf = PIPE_FORMAT_R16_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_GR1616:
> -      pf = PIPE_FORMAT_R16G16_UNORM;
> -      break;
> -   case __DRI_IMAGE_FORMAT_YUYV:
> -      pf = PIPE_FORMAT_YUYV;
> -      break;
> -   default:
> -      pf = PIPE_FORMAT_NONE;
> -      break;
> +static const struct dri2_format_mapping *
> +dri2_get_mapping_by_fourcc(int fourcc) {
> +   for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
> +      if (dri2_format_table[i].dri_fourcc == fourcc)
> +               return &dri2_format_table[i];
>     }
>
> -   return pf;
> +   return NULL;
>  }
>
> -static enum pipe_format fourcc_to_pipe_format(int fourcc)
> -{
> -   enum pipe_format pf;
> -
> -   switch (fourcc) {
> -   case __DRI_IMAGE_FOURCC_R8:
> -      pf = PIPE_FORMAT_R8_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_GR88:
> -      pf = PIPE_FORMAT_RG88_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ARGB1555:
> -      pf = PIPE_FORMAT_B5G5R5A1_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_R16:
> -      pf = PIPE_FORMAT_R16_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_GR1616:
> -      pf = PIPE_FORMAT_RG1616_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_RGB565:
> -      pf = PIPE_FORMAT_B5G6R5_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ARGB8888:
> -      pf = PIPE_FORMAT_BGRA8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XRGB8888:
> -      pf = PIPE_FORMAT_BGRX8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ABGR8888:
> -      pf = PIPE_FORMAT_RGBA8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XBGR8888:
> -      pf = PIPE_FORMAT_RGBX8888_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ARGB2101010:
> -      pf = PIPE_FORMAT_B10G10R10A2_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XRGB2101010:
> -      pf = PIPE_FORMAT_B10G10R10X2_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_ABGR2101010:
> -      pf = PIPE_FORMAT_R10G10B10A2_UNORM;
> -      break;
> -   case __DRI_IMAGE_FOURCC_XBGR2101010:
> -      pf = PIPE_FORMAT_R10G10B10X2_UNORM;
> -      break;
> -
> -   case __DRI_IMAGE_FOURCC_NV12:
> -      pf = PIPE_FORMAT_NV12;
> -      break;
> -   case __DRI_IMAGE_FOURCC_YUYV:
> -      pf = PIPE_FORMAT_YUYV;
> -      break;
> -   case __DRI_IMAGE_FOURCC_YUV420:
> -   case __DRI_IMAGE_FOURCC_YVU420:
> -      pf = PIPE_FORMAT_YV12;
> -      break;
> -
> -   case __DRI_IMAGE_FOURCC_SARGB8888:
> -   case __DRI_IMAGE_FOURCC_YUV410:
> -   case __DRI_IMAGE_FOURCC_YUV411:
> -   case __DRI_IMAGE_FOURCC_YUV422:
> -   case __DRI_IMAGE_FOURCC_YUV444:
> -   case __DRI_IMAGE_FOURCC_NV16:
> -   case __DRI_IMAGE_FOURCC_YVU410:
> -   case __DRI_IMAGE_FOURCC_YVU411:
> -   case __DRI_IMAGE_FOURCC_YVU422:
> -   case __DRI_IMAGE_FOURCC_YVU444:
> -   default:
> -      pf = PIPE_FORMAT_NONE;
> +static const struct dri2_format_mapping *
> +dri2_get_mapping_by_format(int format) {
> +   for (unsigned i = 0; i < ARRAY_SIZE(dri2_format_table); i++) {
> +      if (dri2_format_table[i].dri_format == format)
> +               return &dri2_format_table[i];
>     }
>
> -   return pf;
> +   return NULL;
>  }
>
>  /**
> @@ -983,7 +760,7 @@ dri2_update_tex_buffer(struct dri_drawable *drawable,
>
>  static __DRIimage *
>  dri2_create_image_from_winsys(__DRIscreen *_screen,
> -                              int width, int height, int format,
> +                              int width, int height, enum pipe_format pf,
>                                int num_handles, struct winsys_handle
> *whandle,
>                                void *loaderPrivate)
>  {
> @@ -992,13 +769,8 @@ dri2_create_image_from_winsys(__DRIscreen *_screen,
>     __DRIimage *img;
>     struct pipe_resource templ;
>     unsigned tex_usage = 0;
> -   enum pipe_format pf;
>     int i;
>
> -   pf = dri2_format_to_pipe_format (format);
> -   if (pf == PIPE_FORMAT_NONE)
> -      return NULL;
> -
>     if (pscreen->is_format_supported(pscreen, pf, screen->target, 0,
>                                      PIPE_BIND_RENDER_TARGET))
>        tex_usage |= PIPE_BIND_RENDER_TARGET;
> @@ -1006,6 +778,20 @@ dri2_create_image_from_winsys(__DRIscreen *_screen,
>                                      PIPE_BIND_SAMPLER_VIEW))
>        tex_usage |= PIPE_BIND_SAMPLER_VIEW;
>
> +   if (!tex_usage && util_format_is_yuv(pf)) {
> +      /* YUV format sampling can be emulated by the Mesa state tracker by
> +       * using multiple R8/RG88 samplers. So try to rewrite the pipe
> format.
> +       */
> +      pf = PIPE_FORMAT_R8_UNORM;
> +
> +      if (pscreen->is_format_supported(pscreen, pf, screen->target, 0,
> +                                       PIPE_BIND_SAMPLER_VIEW))
> +         tex_usage |= PIPE_BIND_SAMPLER_VIEW;
> +   }
>

This doesn't seem to belong in this commit.

Other than that, the series is:

Reviewed-by: Marek Olšák <marek.olsak at amd.com>

Marek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180526/c5f0845a/attachment-0001.html>


More information about the mesa-dev mailing list