[Mesa-dev] [PATCH v3 2/8] gbm: Pull out FourCC <-> DRIimage format table

Daniel Stone daniels at collabora.com
Fri Jul 14 13:36:36 UTC 2017


Rather than duplicated (yet asymmetric) open-coded tables, pull them out
to a common structure.

Reviewed-by: Emil Velikov <emil.velikov at collabora.com>
Signed-off-by: Daniel Stone <daniels at collabora.com>
---
 src/gbm/Makefile.am            |   1 +
 src/gbm/backends/dri/gbm_dri.c | 154 ++++++++++++++++++-----------------------
 2 files changed, 70 insertions(+), 85 deletions(-)

v3: Add gbm_format_canonicalize(), use it everywhere we accept GBM formats.

diff --git a/src/gbm/Makefile.am b/src/gbm/Makefile.am
index 60b0924506..de8396000b 100644
--- a/src/gbm/Makefile.am
+++ b/src/gbm/Makefile.am
@@ -5,6 +5,7 @@ pkgconfig_DATA = main/gbm.pc
 
 AM_CFLAGS = \
 	-I$(top_srcdir)/include \
+	-I$(top_srcdir)/src \
 	-I$(top_srcdir)/src/loader \
 	-I$(top_srcdir)/src/gbm/main \
 	$(DLOPEN_CFLAGS) \
diff --git a/src/gbm/backends/dri/gbm_dri.c b/src/gbm/backends/dri/gbm_dri.c
index d4bf243034..dab7ce0e09 100644
--- a/src/gbm/backends/dri/gbm_dri.c
+++ b/src/gbm/backends/dri/gbm_dri.c
@@ -48,6 +48,7 @@
 
 #include "gbmint.h"
 #include "loader.h"
+#include "util/macros.h"
 
 /* For importing wl_buffer */
 #if HAVE_WAYLAND_PLATFORM
@@ -541,6 +542,65 @@ dri_screen_create_sw(struct gbm_dri_device *dri)
    return dri_screen_create_swrast(dri);
 }
 
+static const struct {
+   uint32_t gbm_format;
+   int dri_image_format;
+} gbm_to_dri_image_formats[] = {
+   { GBM_FORMAT_R8,          __DRI_IMAGE_FORMAT_R8          },
+   { GBM_FORMAT_GR88,        __DRI_IMAGE_FORMAT_GR88        },
+   { GBM_FORMAT_RGB565,      __DRI_IMAGE_FORMAT_RGB565      },
+   { GBM_FORMAT_XRGB8888,    __DRI_IMAGE_FORMAT_XRGB8888    },
+   { GBM_FORMAT_ARGB8888,    __DRI_IMAGE_FORMAT_ARGB8888    },
+   { GBM_FORMAT_XBGR8888,    __DRI_IMAGE_FORMAT_XBGR8888    },
+   { GBM_FORMAT_ABGR8888,    __DRI_IMAGE_FORMAT_ABGR8888    },
+   { GBM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010 },
+   { GBM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010 },
+};
+
+/* The two GBM_BO_FORMAT_[XA]RGB8888 formats alias the GBM_FORMAT_*
+ * formats of the same name. We want to accept them whenever someone
+ * has a GBM format, but never return them to the user. */
+static int
+gbm_format_canonicalize(uint32_t gbm_format)
+{
+   switch (gbm_format) {
+   case GBM_BO_FORMAT_XRGB8888:
+      return GBM_FORMAT_XRGB8888;
+   case GBM_BO_FORMAT_ARGB8888:
+      return GBM_FORMAT_ARGB8888;
+   default:
+      return gbm_format;
+   }
+}
+
+static int
+gbm_format_to_dri_format(uint32_t gbm_format)
+{
+   int i;
+
+   gbm_format = gbm_format_canonicalize(gbm_format);
+   for (i = 0; i < ARRAY_SIZE(gbm_to_dri_image_formats); i++) {
+      if (gbm_to_dri_image_formats[i].gbm_format == gbm_format)
+         return gbm_to_dri_image_formats[i].dri_image_format;
+   }
+
+   return 0;
+}
+
+static uint32_t
+gbm_dri_to_gbm_format(int dri_format)
+{
+   int i;
+
+   for (i = 0; i < ARRAY_SIZE(gbm_to_dri_image_formats); i++) {
+      if (gbm_to_dri_image_formats[i].dri_image_format == dri_format)
+         return gbm_to_dri_image_formats[i].gbm_format;
+   }
+
+   return 0;
+}
+
+
 static int
 gbm_dri_is_format_supported(struct gbm_device *gbm,
                             uint32_t format,
@@ -786,35 +846,6 @@ gbm_dri_bo_destroy(struct gbm_bo *_bo)
    free(bo);
 }
 
-static uint32_t
-gbm_dri_to_gbm_format(uint32_t dri_format)
-{
-   uint32_t ret = 0;
-
-   switch (dri_format) {
-   case __DRI_IMAGE_FORMAT_RGB565:
-      ret = GBM_FORMAT_RGB565;
-      break;
-   case __DRI_IMAGE_FORMAT_XRGB8888:
-      ret = GBM_FORMAT_XRGB8888;
-      break;
-   case __DRI_IMAGE_FORMAT_ARGB8888:
-      ret = GBM_FORMAT_ARGB8888;
-      break;
-   case __DRI_IMAGE_FORMAT_XBGR8888:
-      ret = GBM_FORMAT_XBGR8888;
-      break;
-   case __DRI_IMAGE_FORMAT_ABGR8888:
-      ret = GBM_FORMAT_ABGR8888;
-      break;
-   default:
-      ret = 0;
-      break;
-   }
-
-   return ret;
-}
-
 static struct gbm_bo *
 gbm_dri_bo_import(struct gbm_device *gbm,
                   uint32_t type, void *buffer, uint32_t usage)
@@ -885,18 +916,8 @@ gbm_dri_bo_import(struct gbm_device *gbm,
 
       /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
        * tokens accepted by createImageFromFds, except for not supporting
-       * the sARGB format. Also, GBM_BO_FORMAT_* are defined differently to
-       * their GBM_FORMAT_* equivalents, so remap them here. */
-      switch (fd_data->format) {
-      case GBM_BO_FORMAT_XRGB8888:
-         fourcc = GBM_FORMAT_XRGB8888;
-         break;
-      case GBM_BO_FORMAT_ARGB8888:
-         fourcc = GBM_FORMAT_ARGB8888;
-         break;
-      default:
-         fourcc = fd_data->format;
-      }
+       * the sARGB format. */
+      fourcc = gbm_format_canonicalize(fd_data->format);
 
       image = dri->image->createImageFromFds(dri->screen,
                                              fd_data->width,
@@ -928,18 +949,8 @@ gbm_dri_bo_import(struct gbm_device *gbm,
 
       /* GBM's GBM_FORMAT_* tokens are a strict superset of the DRI FourCC
        * tokens accepted by createImageFromDmaBufs2, except for not supporting
-       * the sARGB format. Also, GBM_BO_FORMAT_* are defined differently to
-       * their GBM_FORMAT_* equivalents, so remap them here. */
-      switch (fd_data->format) {
-      case GBM_BO_FORMAT_XRGB8888:
-         fourcc = GBM_FORMAT_XRGB8888;
-         break;
-      case GBM_BO_FORMAT_ARGB8888:
-         fourcc = GBM_FORMAT_ARGB8888;
-         break;
-      default:
-         fourcc = fd_data->format;
-      }
+       * the sARGB format. */
+      fourcc = gbm_format_canonicalize(fd_data->format);
 
       image = dri->image->createImageFromDmaBufs2(dri->screen, fd_data->width,
                                                   fd_data->height, fourcc,
@@ -1105,6 +1116,8 @@ gbm_dri_bo_create(struct gbm_device *gbm,
     */
    assert(!(usage && count));
 
+   format = gbm_format_canonicalize(format);
+
    if (usage & GBM_BO_USE_WRITE || dri->image == NULL)
       return create_dumb(gbm, width, height, format, usage);
 
@@ -1117,37 +1130,8 @@ gbm_dri_bo_create(struct gbm_device *gbm,
    bo->base.height = height;
    bo->base.format = format;
 
-   switch (format) {
-   case GBM_FORMAT_R8:
-      dri_format = __DRI_IMAGE_FORMAT_R8;
-      break;
-   case GBM_FORMAT_GR88:
-      dri_format = __DRI_IMAGE_FORMAT_GR88;
-      break;
-   case GBM_FORMAT_RGB565:
-      dri_format = __DRI_IMAGE_FORMAT_RGB565;
-      break;
-   case GBM_FORMAT_XRGB8888:
-   case GBM_BO_FORMAT_XRGB8888:
-      dri_format = __DRI_IMAGE_FORMAT_XRGB8888;
-      break;
-   case GBM_FORMAT_ARGB8888:
-   case GBM_BO_FORMAT_ARGB8888:
-      dri_format = __DRI_IMAGE_FORMAT_ARGB8888;
-      break;
-   case GBM_FORMAT_ABGR8888:
-      dri_format = __DRI_IMAGE_FORMAT_ABGR8888;
-      break;
-   case GBM_FORMAT_XBGR8888:
-      dri_format = __DRI_IMAGE_FORMAT_XBGR8888;
-      break;
-   case GBM_FORMAT_ARGB2101010:
-      dri_format = __DRI_IMAGE_FORMAT_ARGB2101010;
-      break;
-   case GBM_FORMAT_XRGB2101010:
-      dri_format = __DRI_IMAGE_FORMAT_XRGB2101010;
-      break;
-   default:
+   dri_format = gbm_format_to_dri_format(format);
+   if (dri_format == 0) {
       errno = EINVAL;
       goto failed;
    }
@@ -1315,7 +1299,7 @@ gbm_dri_surface_create(struct gbm_device *gbm,
    surf->base.gbm = gbm;
    surf->base.width = width;
    surf->base.height = height;
-   surf->base.format = format;
+   surf->base.format = gbm_format_canonicalize(format);
    surf->base.flags = flags;
    if (!modifiers) {
       assert(!count);
-- 
2.13.0



More information about the mesa-dev mailing list