[gtk/wip/otte/color-profiles: 16/40] gl: Move memory <=> GL format mapping




commit 78535ef2966b8dd9656be614fa7bb477191db99c
Author: Benjamin Otte <otte redhat com>
Date:   Sat Sep 25 04:47:45 2021 +0200

    gl: Move memory <=> GL format mapping
    
    Put it into gdkmemoryformat.c, where all the mapping goes.

 gdk/gdkglcontext.c           | 88 ++++++++++++--------------------------------
 gdk/gdkmemoryformat.c        | 67 ++++++++++++++++++++++++++++++++-
 gdk/gdkmemoryformatprivate.h |  5 +++
 3 files changed, 94 insertions(+), 66 deletions(-)
---
diff --git a/gdk/gdkglcontext.c b/gdk/gdkglcontext.c
index 527ba318ec..11d19e7163 100644
--- a/gdk/gdkglcontext.c
+++ b/gdk/gdkglcontext.c
@@ -235,9 +235,9 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
   GdkMemoryFormat data_format;
   GdkColorProfile *color_profile;
   guchar *copy = NULL;
-  GLint gl_internalformat;
-  GLint gl_format;
-  GLint gl_type;
+  GLenum gl_internalformat;
+  GLenum gl_format;
+  GLenum gl_type;
   gsize bpp, stride;
   const guchar *data;
 
@@ -252,82 +252,40 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
 
   data += x * bpp + y * stride;
 
-  if (color_profile != gdk_color_profile_get_srgb ())
-    {
-      goto fallback;
-    }
-  else if (!priv->use_es && data_format == GDK_MEMORY_DEFAULT) /* Cairo surface format */
-    {
-      gl_internalformat = GL_RGBA8;
-      gl_format = GL_BGRA;
-      gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
-    }
-  else if (data_format == GDK_MEMORY_R8G8B8) /* Pixmap non-alpha data */
-    {
-      gl_internalformat = GL_RGBA8;
-      gl_format = GL_RGB;
-      gl_type = GL_UNSIGNED_BYTE;
-    }
-  else if (priv->use_es && data_format == GDK_MEMORY_B8G8R8)
+  if (!gdk_memory_format_gl_format (data_format,
+                                    priv->use_es,
+                                    &gl_internalformat,
+                                    &gl_format,
+                                    &gl_type))
     {
+      data_format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
       gl_internalformat = GL_RGBA8;
-      gl_format = GL_BGR;
-      gl_type = GL_UNSIGNED_BYTE;
-    }
-  else if (data_format == GDK_MEMORY_R16G16B16)
-    {
-      gl_internalformat = GL_RGBA16;
-      gl_format = GL_RGB;
-      gl_type = GL_UNSIGNED_SHORT;
-    }
-  else if (data_format == GDK_MEMORY_R16G16B16A16_PREMULTIPLIED)
-    {
-      gl_internalformat = GL_RGBA16;
       gl_format = GL_RGBA;
-      gl_type = GL_UNSIGNED_SHORT;
-    }
-  else if (data_format == GDK_MEMORY_R16G16B16_FLOAT)
-    {
-      gl_internalformat = GL_RGB16F;
-      gl_format = GL_RGB;
-      gl_type = GL_HALF_FLOAT;
-    }
-  else if (data_format == GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED)
-    {
-      gl_internalformat = GL_RGBA16F;
-      gl_format = GL_RGBA;
-      gl_type = GL_HALF_FLOAT;
+      gl_type = GL_UNSIGNED_BYTE;
+      bpp = 4;
+
+      copy = g_malloc_n (width * 4, height);
     }
-  else if (data_format == GDK_MEMORY_R32G32B32_FLOAT)
+  else if (color_profile != gdk_color_profile_get_srgb ())
     {
-      gl_internalformat = GL_RGB32F;
-      gl_format = GL_RGB;
-      gl_type = GL_FLOAT;
+      copy = g_malloc_n (width * bpp, height);
     }
-  else if (data_format == GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED)
+  else
     {
-      gl_internalformat = GL_RGBA32F;
-      gl_format = GL_RGBA;
-      gl_type = GL_FLOAT;
+      copy = NULL;
     }
-  else /* Fall-back, convert to GLES format */
+
+  if (copy)
     {
-fallback:
-      copy = g_malloc (width * height * 4);
-      gdk_memory_convert (copy, width * 4,
-                          GDK_MEMORY_R8G8B8A8_PREMULTIPLIED,
+      gdk_memory_convert (copy, width * bpp,
+                          data_format,
                           gdk_color_profile_get_srgb (),
                           data, stride,
-                          data_format,
+                          gdk_memory_texture_get_format (memory_texture),
                           color_profile,
                           width, height);
-      data_format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
-      stride = width * 4;
       data = copy;
-      bpp = 4;
-      gl_internalformat = GL_RGBA8;
-      gl_format = GL_RGBA;
-      gl_type = GL_UNSIGNED_BYTE;
+      stride = width * 4;
     }
 
   /* GL_UNPACK_ROW_LENGTH is available on desktop GL, OpenGL ES >= 3.0, or if
diff --git a/gdk/gdkmemoryformat.c b/gdk/gdkmemoryformat.c
index e78851e5e8..1b78b745a7 100644
--- a/gdk/gdkmemoryformat.c
+++ b/gdk/gdkmemoryformat.c
@@ -24,6 +24,8 @@
 #include "gdkcolorprofileprivate.h"
 #include "gsk/ngl/fp16private.h"
 
+#include <epoxy/gl.h>
+
 typedef struct _GdkMemoryFormatDescription GdkMemoryFormatDescription;
 
 typedef enum {
@@ -176,17 +178,32 @@ struct _GdkMemoryFormatDescription
   GdkMemoryAlpha alpha;
   gsize bytes_per_pixel;
   gsize alignment;
-
+  gboolean supports_gles;
+  struct {
+    guint internal_format;
+    guint format;
+    guint type;
+  } gl;
   /* no premultiplication going on here */
   void (* to_float) (float *, const guchar*, gsize);
   void (* from_float) (guchar *, const float *, gsize);
 };
 
+#if  G_BYTE_ORDER == G_LITTLE_ENDIAN
+#  define GDK_GL_UNSIGNED_BYTE_FLIPPED GL_UNSIGNED_INT_8_8_8_8
+#elif G_BYTE_ORDER == G_BIG_ENDIAN
+#  define GDK_GL_UNSIGNED_BYTE_FLIPPED GL_UNSIGNED_INT_8_8_8_8_REV
+#else
+#  error "Define the right GL flags here"
+#endif
+
 static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
   [GDK_MEMORY_B8G8R8A8_PREMULTIPLIED] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     4,
     G_ALIGNOF (guchar),
+    FALSE,
+    { GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE },
     b8g8r8a8_premultiplied_to_float,
     b8g8r8a8_premultiplied_from_float,
   },
@@ -194,6 +211,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     4,
     G_ALIGNOF (guchar),
+    FALSE,
+    { GL_RGBA8, GL_BGRA, GDK_GL_UNSIGNED_BYTE_FLIPPED },
     a8r8g8b8_premultiplied_to_float,
     a8r8g8b8_premultiplied_from_float,
   },
@@ -201,6 +220,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     4,
     G_ALIGNOF (guchar),
+    TRUE,
+    { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE },
     r8g8b8a8_premultiplied_to_float,
     r8g8b8a8_premultiplied_from_float,
   },
@@ -208,6 +229,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
+    FALSE,
+    { GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE },
     b8g8r8a8_to_float,
     b8g8r8a8_from_float,
   },
@@ -215,6 +238,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
+    FALSE,
+    { GL_RGBA8, GL_RGBA, GDK_GL_UNSIGNED_BYTE_FLIPPED },
     a8r8g8b8_to_float,
     a8r8g8b8_from_float,
   },
@@ -222,6 +247,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
+    TRUE,
+    { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE },
     r8g8b8a8_to_float,
     r8g8b8a8_from_float,
   },
@@ -229,6 +256,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_STRAIGHT,
     4,
     G_ALIGNOF (guchar),
+    FALSE,
+    { GL_RGBA8, GL_BGRA, GDK_GL_UNSIGNED_BYTE_FLIPPED },
     a8b8g8r8_to_float,
     a8b8g8r8_from_float,
   },
@@ -236,6 +265,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     3,
     G_ALIGNOF (guchar),
+    TRUE,
+    { GL_RGBA8, GL_RGB, GL_UNSIGNED_BYTE },
     r8g8b8_to_float,
     r8g8b8_from_float,
   },
@@ -243,6 +274,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     3,
     G_ALIGNOF (guchar),
+    FALSE,
+    { GL_RGB8, GL_BGR, GL_UNSIGNED_BYTE },
     b8g8r8_to_float,
     b8g8r8_from_float,
   },
@@ -250,6 +283,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     6,
     G_ALIGNOF (guint16),
+    TRUE,
+    { GL_RGB16, GL_RGB, GL_UNSIGNED_SHORT },
     r16g16b16_to_float,
     r16g16b16_from_float,
   },
@@ -257,6 +292,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     8,
     G_ALIGNOF (guint16),
+    TRUE,
+    { GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT },
     r16g16b16a16_to_float,
     r16g16b16a16_from_float,
   },
@@ -264,6 +301,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     6,
     G_ALIGNOF (guint16),
+    TRUE,
+    { GL_RGB16F, GL_RGB, GL_HALF_FLOAT },
     r16g16b16_float_to_float,
     r16g16b16_float_from_float,
   },
@@ -271,6 +310,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     8,
     G_ALIGNOF (guint16),
+    TRUE,
+    { GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT },
     r16g16b16a16_float_to_float,
     r16g16b16a16_float_from_float,
   },
@@ -278,6 +319,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_OPAQUE,
     12,
     G_ALIGNOF (float),
+    TRUE,
+    { GL_RGB32F, GL_RGB, GL_FLOAT },
     r32g32b32_float_to_float,
     r32g32b32_float_from_float,
   },
@@ -285,6 +328,8 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     GDK_MEMORY_ALPHA_PREMULTIPLIED,
     16,
     G_ALIGNOF (float),
+    TRUE,
+    { GL_RGBA32F, GL_RGBA, GL_FLOAT },
     r32g32b32a32_float_to_float,
     r32g32b32a32_float_from_float,
   }
@@ -302,6 +347,26 @@ gdk_memory_format_alignment (GdkMemoryFormat format)
   return memory_formats[format].alignment;
 }
 
+gboolean
+gdk_memory_format_gl_format (GdkMemoryFormat  format,
+                             gboolean         gles,
+                             guint           *out_internal_format,
+                             guint           *out_format,
+                             guint           *out_type)
+{
+  *out_internal_format = memory_formats[format].gl.internal_format;
+  *out_format = memory_formats[format].gl.format;
+  *out_type = memory_formats[format].gl.type;
+
+  if (memory_formats[format].alpha == GDK_MEMORY_ALPHA_STRAIGHT)
+    return FALSE;
+
+  if (gles && !memory_formats[format].supports_gles)
+    return FALSE;
+
+  return TRUE;
+}
+
 static void
 premultiply (float *rgba,
              gsize  n)
diff --git a/gdk/gdkmemoryformatprivate.h b/gdk/gdkmemoryformatprivate.h
index ca0e4c2c1b..26e84e737b 100644
--- a/gdk/gdkmemoryformatprivate.h
+++ b/gdk/gdkmemoryformatprivate.h
@@ -26,6 +26,11 @@ G_BEGIN_DECLS
 
 gsize                   gdk_memory_format_alignment         (GdkMemoryFormat             format) 
G_GNUC_CONST;
 gsize                   gdk_memory_format_bytes_per_pixel   (GdkMemoryFormat             format) 
G_GNUC_CONST;
+gboolean                gdk_memory_format_gl_format         (GdkMemoryFormat             format,
+                                                             gboolean                    gles,
+                                                             guint                      *out_internal_format,
+                                                             guint                      *out_format,
+                                                             guint                      *out_type);
 
 void                    gdk_memory_convert                  (guchar                     *dest_data,
                                                              gsize                       dest_stride,


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