[mutter/wip/nieldsg/cogl-pixel-format-include-dr] cogl: Include DRM 4CC conversions into CoglPixelFormat



commit 21f151b5b06a827e07d7be69e20913be9fa54b82
Author: Niels De Graef <niels degraef barco com>
Date:   Mon May 27 12:12:30 2019 +0200

    cogl: Include DRM 4CC conversions into CoglPixelFormat
    
    This is at the moment still part of both MetaRendererNative and
    MetaDmaBuf, which means duplicated effort in code sections only barely
    related to color formats. By doing this, we can keep more of the pixel
    format code together, which is more future-proof as we want to add
    support for new pixel formats (like YUV-based ones).

 cogl/cogl/cogl-pixel-format.c              | 63 +++++++++++++++++++++++++++
 cogl/cogl/cogl-pixel-format.h              | 34 +++++++++++++++
 meson.build                                |  4 ++
 src/backends/native/meta-renderer-native.c | 69 ------------------------------
 src/wayland/meta-wayland-dma-buf.c         | 33 ++++----------
 5 files changed, 109 insertions(+), 94 deletions(-)
---
diff --git a/cogl/cogl/cogl-pixel-format.c b/cogl/cogl/cogl-pixel-format.c
index a8c0857a6..daec41067 100644
--- a/cogl/cogl/cogl-pixel-format.c
+++ b/cogl/cogl/cogl-pixel-format.c
@@ -308,3 +308,66 @@ cogl_pixel_format_to_string (CoglPixelFormat format)
 
   g_assert_not_reached ();
 }
+
+#ifdef HAVE_LIBDRM
+
+typedef struct _PixelFormatMap {
+  uint32_t drm_format;
+  CoglPixelFormat cogl_format;
+  CoglTextureComponents cogl_components;
+} PixelFormatMap;
+
+static const PixelFormatMap pixel_format_map[] = {
+/* DRM formats are defined as little-endian, not machine endian. */
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+  { DRM_FORMAT_RGB565,   COGL_PIXEL_FORMAT_RGB_565,       COGL_TEXTURE_COMPONENTS_RGB  },
+  { DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+  { DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+  { DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+  { DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+#elif G_BYTE_ORDER == G_BIG_ENDIAN
+  /* DRM_FORMAT_RGB565 cannot be expressed. */
+  { DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+  { DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+  { DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+  { DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
+  { DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
+#else
+#error "unexpected G_BYTE_ORDER"
+#endif
+};
+
+gboolean
+cogl_pixel_format_from_drm_format (uint32_t               drm_format,
+                                   CoglPixelFormat       *out_format,
+                                   CoglTextureComponents *out_components)
+{
+  const size_t n = G_N_ELEMENTS (pixel_format_map);
+  size_t i;
+
+  for (i = 0; i < n; i++)
+    {
+      if (pixel_format_map[i].drm_format == drm_format)
+        break;
+    }
+
+  if (i == n)
+    return FALSE;
+
+  if (out_format)
+    *out_format = pixel_format_map[i].cogl_format;
+
+  if (out_components)
+    *out_components = pixel_format_map[i].cogl_components;
+
+  return TRUE;
+}
+
+#endif
diff --git a/cogl/cogl/cogl-pixel-format.h b/cogl/cogl/cogl-pixel-format.h
index c2659fee0..70f749685 100644
--- a/cogl/cogl/cogl-pixel-format.h
+++ b/cogl/cogl/cogl-pixel-format.h
@@ -38,6 +38,10 @@
 #include <stdint.h>
 #include <stddef.h>
 
+#ifdef HAVE_LIBDRM
+#include <drm_fourcc.h>
+#endif
+
 #include <cogl/cogl-defines.h>
 
 #include <glib.h>
@@ -58,6 +62,9 @@ G_BEGIN_DECLS
  *
  * Other examples of factors that can influence the layout in memory are the
  * system's endianness.
+ *
+ * This file also contains methods to map Linux DRM 4CC codes to
+ * CoglPixelFormats.
  */
 
 #define COGL_A_BIT              (1 << 4)
@@ -295,6 +302,33 @@ _cogl_pixel_format_is_endian_dependant (CoglPixelFormat format);
 const char *
 cogl_pixel_format_to_string (CoglPixelFormat format);
 
+#ifdef HAVE_LIBDRM
+
+/* added in libdrm 2.4.95 */
+#ifndef DRM_FORMAT_INVALID
+#define DRM_FORMAT_INVALID 0
+#endif
+
+/**
+ * cogl_pixel_format_from_drm_format:
+ * @drm_format: The DRM 4CC code (as specified in drm_fourcc.h)
+ * @out_format: (optional): The corresponding #CoglPixelFormat (if successful)
+ * @out_components: (optional): The corresponding #CoglPixelFormat (if sucessful)
+ *
+ * Does an internal lookup to find a #CoglPixelFormat that matches the given
+ * DRM 4CC code. If no such format could be found, this function will return
+ * %FALSE and @out_format will be untouched.
+ *
+ * Returns: %TRUE if a #CoglPixelFormat corresponding to the 4CC code exists,
+ * %FALSE otherwise.
+ */
+gboolean
+cogl_pixel_format_from_drm_format (uint32_t               drm_format,
+                                   CoglPixelFormat       *out_format,
+                                   CoglTextureComponents *out_components);
+
+#endif
+
 G_END_DECLS
 
 #endif /* __COGL_PIXEL_FORMAT_H__ */
diff --git a/meson.build b/meson.build
index 08b9ba73a..49e17bc35 100644
--- a/meson.build
+++ b/meson.build
@@ -344,6 +344,10 @@ cdata.set('HAVE_SM', have_sm)
 cdata.set('HAVE_STARTUP_NOTIFICATION', have_startup_notification)
 cdata.set('HAVE_INTROSPECTION', have_introspection)
 
+if have_native_backend
+  cdata.set_quoted('HAVE_LIBDRM', libdrm_dep.found())
+endif
+
 xkb_base = xkeyboard_config_dep.get_pkgconfig_variable('xkb_base')
 cdata.set_quoted('XKB_BASE', xkb_base)
 
diff --git a/src/backends/native/meta-renderer-native.c b/src/backends/native/meta-renderer-native.c
index 979306aa6..49b24d2b0 100644
--- a/src/backends/native/meta-renderer-native.c
+++ b/src/backends/native/meta-renderer-native.c
@@ -73,11 +73,6 @@
 #define EGL_DRM_MASTER_FD_EXT 0x333C
 #endif
 
-/* added in libdrm 2.4.95 */
-#ifndef DRM_FORMAT_INVALID
-#define DRM_FORMAT_INVALID 0
-#endif
-
 enum
 {
   PROP_0,
@@ -252,11 +247,6 @@ static void
 free_next_secondary_bo (MetaGpuKms                          *gpu_kms,
                         MetaOnscreenNativeSecondaryGpuState *secondary_gpu_state);
 
-static gboolean
-cogl_pixel_format_from_drm_format (uint32_t               drm_format,
-                                   CoglPixelFormat       *out_format,
-                                   CoglTextureComponents *out_components);
-
 static MetaBackend *
 backend_from_renderer_native (MetaRendererNative *renderer_native)
 {
@@ -2126,65 +2116,6 @@ copy_shared_framebuffer_gpu (CoglOnscreen                        *onscreen,
     }
 }
 
-typedef struct _PixelFormatMap {
-  uint32_t drm_format;
-  CoglPixelFormat cogl_format;
-  CoglTextureComponents cogl_components;
-} PixelFormatMap;
-
-static const PixelFormatMap pixel_format_map[] = {
-/* DRM formats are defined as little-endian, not machine endian. */
-#if G_BYTE_ORDER == G_LITTLE_ENDIAN
-  { DRM_FORMAT_RGB565,   COGL_PIXEL_FORMAT_RGB_565,       COGL_TEXTURE_COMPONENTS_RGB  },
-  { DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-  { DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-  { DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-  { DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-#elif G_BYTE_ORDER == G_BIG_ENDIAN
-  /* DRM_FORMAT_RGB565 cannot be expressed. */
-  { DRM_FORMAT_ABGR8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_XBGR8888, COGL_PIXEL_FORMAT_ABGR_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-  { DRM_FORMAT_ARGB8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_XRGB8888, COGL_PIXEL_FORMAT_ARGB_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-  { DRM_FORMAT_BGRA8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_BGRX8888, COGL_PIXEL_FORMAT_BGRA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-  { DRM_FORMAT_RGBA8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGBA },
-  { DRM_FORMAT_RGBX8888, COGL_PIXEL_FORMAT_RGBA_8888_PRE, COGL_TEXTURE_COMPONENTS_RGB  },
-#else
-#error "unexpected G_BYTE_ORDER"
-#endif
-};
-
-static gboolean
-cogl_pixel_format_from_drm_format (uint32_t               drm_format,
-                                   CoglPixelFormat       *out_format,
-                                   CoglTextureComponents *out_components)
-{
-  const size_t n = G_N_ELEMENTS (pixel_format_map);
-  size_t i;
-
-  for (i = 0; i < n; i++)
-    {
-      if (pixel_format_map[i].drm_format == drm_format)
-        break;
-    }
-
-  if (i == n)
-    return FALSE;
-
-  if (out_format)
-    *out_format = pixel_format_map[i].cogl_format;
-
-  if (out_components)
-    *out_components = pixel_format_map[i].cogl_components;
-
-  return TRUE;
-}
-
 static void
 copy_shared_framebuffer_cpu (CoglOnscreen                        *onscreen,
                              MetaOnscreenNativeSecondaryGpuState *secondary_gpu_state,
diff --git a/src/wayland/meta-wayland-dma-buf.c b/src/wayland/meta-wayland-dma-buf.c
index e49fba9cf..f2f06ce29 100644
--- a/src/wayland/meta-wayland-dma-buf.c
+++ b/src/wayland/meta-wayland-dma-buf.c
@@ -43,10 +43,6 @@
 
 #include "linux-dmabuf-unstable-v1-server-protocol.h"
 
-#ifndef DRM_FORMAT_MOD_INVALID
-#define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
-#endif
-
 #define META_WAYLAND_DMA_BUF_MAX_FDS 4
 
 struct _MetaWaylandDmaBufBuffer
@@ -84,28 +80,15 @@ meta_wayland_dma_buf_realize_texture (MetaWaylandBuffer  *buffer,
   if (buffer->dma_buf.texture)
     return TRUE;
 
-  switch (dma_buf->drm_format)
+  /*
+   * NOTE: The cogl_format here is only used for texture color channel
+   * swizzling as compared to COGL_PIXEL_FORMAT_ARGB. It is *not* used
+   * for accessing the buffer memory. EGL will access the buffer
+   * memory according to the DRM fourcc code. Cogl will not mmap
+   * and access the buffer memory at all.
+   */
+  if (!cogl_pixel_format_from_drm_format (dma_buf->drm_format, &cogl_format, NULL))
     {
-    /*
-     * NOTE: The cogl_format here is only used for texture color channel
-     * swizzling as compared to COGL_PIXEL_FORMAT_ARGB. It is *not* used
-     * for accessing the buffer memory. EGL will access the buffer
-     * memory according to the DRM fourcc code. Cogl will not mmap
-     * and access the buffer memory at all.
-     */
-    case DRM_FORMAT_XRGB8888:
-      cogl_format = COGL_PIXEL_FORMAT_RGB_888;
-      break;
-    case DRM_FORMAT_ARGB8888:
-      cogl_format = COGL_PIXEL_FORMAT_ARGB_8888_PRE;
-      break;
-    case DRM_FORMAT_ARGB2101010:
-      cogl_format = COGL_PIXEL_FORMAT_ARGB_2101010_PRE;
-      break;
-    case DRM_FORMAT_RGB565:
-      cogl_format = COGL_PIXEL_FORMAT_RGB_565;
-      break;
-    default:
       g_set_error (error, G_IO_ERROR,
                    G_IO_ERROR_FAILED,
                    "Unsupported buffer format %d", dma_buf->drm_format);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]