[gtk/wip/chergert/fix-gl-rgba] gdk/gl: handle GL_RGBA/GL_UNSIGNED_NORMALIZED




commit a968de9239100f18d18f236dfee417ddf577ffaa
Author: Christian Hergert <chergert redhat com>
Date:   Wed Mar 23 20:20:02 2022 -0700

    gdk/gl: handle GL_RGBA/GL_UNSIGNED_NORMALIZED
    
    WebKit's GTK 4 port can give us textures with an internal format of
    GL_RGBA with GL_UNSIGNED_NORMALIZED and a bit-depth of 8. This fixes
    warnings for every GdkGLTexture created/delivered to the GskGLRenderer.
    
    Fixes #4783

 gdk/gdkenums.h                |  1 +
 gdk/gdkgltexture.c            | 36 ++++++++++++++++++++++++++++++++++++
 gdk/gdkmemoryformat.c         | 10 ++++++++++
 gdk/loaders/gdkpng.c          |  3 ++-
 gdk/loaders/gdktiff.c         |  1 +
 testsuite/gdk/memorytexture.c |  3 +++
 6 files changed, 53 insertions(+), 1 deletion(-)
---
diff --git a/gdk/gdkenums.h b/gdk/gdkenums.h
index 11654a513f..829baa8b4f 100644
--- a/gdk/gdkenums.h
+++ b/gdk/gdkenums.h
@@ -327,6 +327,7 @@ typedef enum {
   GDK_MEMORY_R32G32B32_FLOAT,
   GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED,
   GDK_MEMORY_R32G32B32A32_FLOAT,
+  GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED,
 
   GDK_MEMORY_N_FORMATS
 } GdkMemoryFormat;
diff --git a/gdk/gdkgltexture.c b/gdk/gdkgltexture.c
index 23bc806c0d..80b24e8bac 100644
--- a/gdk/gdkgltexture.c
+++ b/gdk/gdkgltexture.c
@@ -365,6 +365,42 @@ gdk_gl_texture_determine_format (GdkGLTexture *self)
       texture->format = GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED;
       break;
 
+    case GL_RGBA:
+      {
+        GLint red_size = 0;
+        GLint green_size = 0;
+        GLint blue_size = 0;
+        GLint alpha_size = 0;
+        GLint red_type = 0;
+        GLint green_type = 0;
+        GLint blue_type = 0;
+        GLint alpha_type = 0;
+
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_RED_TYPE, &red_type);
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_TYPE, &green_type);
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_TYPE, &blue_type);
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_TYPE, &alpha_type);
+
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_RED_SIZE, &red_size);
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_GREEN_SIZE, &green_size);
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_BLUE_SIZE, &blue_size);
+        glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_ALPHA_SIZE, &alpha_size);
+
+#define CHECK_RGBA(rt,gt,bt,at,rs,gs,bs,as) \
+        (red_type == rt && green_type == gt && blue_type == bt && alpha_type == at && \
+         red_size == rs && green_size == gs && blue_size == bs && alpha_size == as)
+
+        if (CHECK_RGBA (GL_UNSIGNED_NORMALIZED, GL_UNSIGNED_NORMALIZED, GL_UNSIGNED_NORMALIZED, 
GL_UNSIGNED_NORMALIZED, 8, 8, 8, 8))
+          {
+            texture->format = GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED;
+            break;
+          }
+
+#undef CHECK_RGBA
+      }
+
+      G_GNUC_FALLTHROUGH;
+
     default:
       g_warning ("Texture in unexpected format 0x%X (%d). File a bug about adding it to GTK", 
internal_format, internal_format);
       /* fallback to the dumbest possible format
diff --git a/gdk/gdkmemoryformat.c b/gdk/gdkmemoryformat.c
index 5bd7b228d4..f399888c00 100644
--- a/gdk/gdkmemoryformat.c
+++ b/gdk/gdkmemoryformat.c
@@ -419,6 +419,16 @@ static const GdkMemoryFormatDescription memory_formats[GDK_MEMORY_N_FORMATS] = {
     { GL_RGBA32F, GL_RGBA, GL_FLOAT },
     r32g32b32a32_float_to_float,
     r32g32b32a32_float_from_float,
+  },
+  [GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED] = {
+    GDK_MEMORY_ALPHA_PREMULTIPLIED,
+    4,
+    G_ALIGNOF (guchar),
+    FALSE,
+    FALSE,
+    { GL_RGBA, GL_RGBA, GL_UNSIGNED_NORMALIZED },
+    r8g8b8a8_premultiplied_to_float,
+    r8g8b8a8_premultiplied_from_float,
   }
 };
 
diff --git a/gdk/loaders/gdkpng.c b/gdk/loaders/gdkpng.c
index 20711210d0..2c9b232426 100644
--- a/gdk/loaders/gdkpng.c
+++ b/gdk/loaders/gdkpng.c
@@ -128,7 +128,7 @@ png_simple_warning_callback (png_structp     png,
 }
 
 /* }}} */
-/* {{{ Public API */ 
+/* {{{ Public API */
 
 GdkTexture *
 gdk_load_png (GBytes  *bytes,
@@ -313,6 +313,7 @@ gdk_save_png (GdkTexture *texture)
     case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
     case GDK_MEMORY_A8R8G8B8_PREMULTIPLIED:
     case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
+    case GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED:
     case GDK_MEMORY_B8G8R8A8:
     case GDK_MEMORY_A8R8G8B8:
     case GDK_MEMORY_R8G8B8A8:
diff --git a/gdk/loaders/gdktiff.c b/gdk/loaders/gdktiff.c
index 724e4c3a81..4d6304a61c 100644
--- a/gdk/loaders/gdktiff.c
+++ b/gdk/loaders/gdktiff.c
@@ -251,6 +251,7 @@ static const FormatData format_data[] = {
   [GDK_MEMORY_R32G32B32_FLOAT]                  = { GDK_MEMORY_R32G32B32_FLOAT,                  32, 3, 
SAMPLEFORMAT_IEEEFP, 0                      },
   [GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED] = { GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED, 32, 4, 
SAMPLEFORMAT_IEEEFP, EXTRASAMPLE_ASSOCALPHA },
   [GDK_MEMORY_R32G32B32A32_FLOAT]               = { GDK_MEMORY_R32G32B32A32_FLOAT,               32, 4, 
SAMPLEFORMAT_IEEEFP, EXTRASAMPLE_UNASSALPHA },
+  [GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED]= { GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED, 8, 4, 
SAMPLEFORMAT_UINT,   EXTRASAMPLE_UNASSALPHA },
 };
 
 /* if this fails, somebody forgot to add formats above */
diff --git a/testsuite/gdk/memorytexture.c b/testsuite/gdk/memorytexture.c
index 372cece19d..8aae5ff64a 100644
--- a/testsuite/gdk/memorytexture.c
+++ b/testsuite/gdk/memorytexture.c
@@ -43,6 +43,7 @@ gdk_memory_format_bytes_per_pixel (GdkMemoryFormat format)
     case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
     case GDK_MEMORY_A8R8G8B8_PREMULTIPLIED:
     case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
+    case GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED:
     case GDK_MEMORY_B8G8R8A8:
     case GDK_MEMORY_A8R8G8B8:
     case GDK_MEMORY_R8G8B8A8:
@@ -88,6 +89,7 @@ gdk_memory_format_has_alpha (GdkMemoryFormat format)
     case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
     case GDK_MEMORY_A8R8G8B8_PREMULTIPLIED:
     case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
+    case GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED:
     case GDK_MEMORY_B8G8R8A8:
     case GDK_MEMORY_A8R8G8B8:
     case GDK_MEMORY_R8G8B8A8:
@@ -226,6 +228,7 @@ texture_builder_set_pixel (TextureBuilder  *builder,
       set_pixel_u8 (data, 1, 2, 3, 0, TRUE, color);
       break;
     case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
+    case GDK_MEMORY_R8G8B8A8_NORMALIZED_PREMULTIPLIED:
       set_pixel_u8 (data, 0, 1, 2, 3, TRUE, color);
       break;
     case GDK_MEMORY_B8G8R8A8:


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