summaryrefslogtreecommitdiff
path: root/cogl
diff options
context:
space:
mode:
Diffstat (limited to 'cogl')
-rw-r--r--cogl/cogl/cogl-pixel-format.c64
-rw-r--r--cogl/cogl/cogl-pixel-format.h35
-rw-r--r--cogl/cogl/meson.build3
3 files changed, 102 insertions, 0 deletions
diff --git a/cogl/cogl/cogl-pixel-format.c b/cogl/cogl/cogl-pixel-format.c
index a8c0857a6..0905941b7 100644
--- a/cogl/cogl/cogl-pixel-format.c
+++ b/cogl/cogl/cogl-pixel-format.c
@@ -29,6 +29,7 @@
*/
#include "cogl-config.h"
+#include "cogl-defines.h"
#include <string.h>
#include <math.h>
@@ -308,3 +309,66 @@ cogl_pixel_format_to_string (CoglPixelFormat format)
g_assert_not_reached ();
}
+
+#ifdef COGL_HAS_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..2abb22279 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 COGL_HAS_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,34 @@ _cogl_pixel_format_is_endian_dependant (CoglPixelFormat format);
const char *
cogl_pixel_format_to_string (CoglPixelFormat format);
+#ifdef COGL_HAS_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 #CoglTextureComponents (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 the output parameters will stay 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/cogl/cogl/meson.build b/cogl/cogl/meson.build
index a89f6448e..65bcdb119 100644
--- a/cogl/cogl/meson.build
+++ b/cogl/cogl/meson.build
@@ -12,6 +12,9 @@ cdata.set('COGL_HAS_X11', have_x11)
cdata.set('COGL_HAS_X11_SUPPORT', have_x11)
cdata.set('COGL_HAS_XLIB', have_x11)
cdata.set('COGL_HAS_XLIB_SUPPORT', have_x11)
+if have_native_backend
+ cdata.set('COGL_HAS_LIBDRM', libdrm_dep.found())
+endif
cogl_defines_h = configure_file(
input: 'cogl-defines.h.meson',