[gtk/wip/matthiasc/gsk-hdr: 2/6] texture: Add hdr api




commit 1841b555cbe34c67ff2e5225d0466d41cc358536
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Oct 1 16:20:56 2021 -0400

    texture: Add hdr api
    
    Add private api to find out if a texture should be
    considered as HDR. For now, we consider float formats
    and >8bit formats to be HDR. The implementation for
    GL textures is reusing the machinery we have in place
    for downloading.

 gdk/gdkgltexture.c            | 57 ++++++++++++++++++++++++++++++++++---------
 gdk/gdkmemorytexture.c        | 26 ++++++++++++++++++++
 gdk/gdkmemorytextureprivate.h |  1 +
 gdk/gdktexture.c              | 14 +++++++++++
 gdk/gdktextureprivate.h       |  3 +++
 5 files changed, 89 insertions(+), 12 deletions(-)
---
diff --git a/gdk/gdkgltexture.c b/gdk/gdkgltexture.c
index e2e292964d..a3b7037976 100644
--- a/gdk/gdkgltexture.c
+++ b/gdk/gdkgltexture.c
@@ -139,17 +139,20 @@ gdk_gl_texture_get_tex_image (GdkGLTexture *self,
                      data);
     }
 }
+
+typedef struct {
+  GdkMemoryFormat format;
+  GLint gl_format;
+  GLint gl_type;
+} GdkGLTextureFormat;
+
 static void
-gdk_gl_texture_do_download_texture (gpointer texture_,
-                                    gpointer result_)
+gdk_gl_texture_get_format (gpointer texture_,
+                           gpointer result_)
 {
-  GdkTexture *texture = texture_;
-  GdkTexture **result = result_;
-  GdkMemoryFormat format;
+  GdkGLTextureFormat *result = result_;
   GLint internal_format, gl_format, gl_type;
-  guchar *data;
-  gsize stride;
-  GBytes *bytes;
+  GdkMemoryFormat format;
 
   glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format);
 
@@ -213,18 +216,36 @@ gdk_gl_texture_do_download_texture (gpointer texture_,
       break;
   }
 
-  stride = gdk_memory_format_bytes_per_pixel (format) * texture->width;
+  result->format = format;
+  result->gl_format = gl_format;
+  result->gl_type = gl_type;
+}
+
+static void
+gdk_gl_texture_do_download_texture (gpointer texture_,
+                                    gpointer result_)
+{
+  GdkTexture *texture = texture_;
+  GdkTexture **result = result_;
+  GdkGLTextureFormat format;
+  gsize stride;
+  guchar *data;
+  GBytes *bytes;
+
+  gdk_gl_texture_get_format (texture, &format);
+
+  stride = gdk_memory_format_bytes_per_pixel (format.format) * texture->width;
   data = g_malloc (stride * texture->height);
 
   gdk_gl_texture_get_tex_image (texture_,
-                                gl_format,
-                                gl_type,
+                                format.gl_format,
+                                format.gl_type,
                                 data);
 
   bytes = g_bytes_new_take (data, stride * texture->height);
   *result = gdk_memory_texture_new (texture->width,
                                     texture->height,
-                                    format,
+                                    format.format,
                                     bytes,
                                     stride);
 
@@ -319,6 +340,17 @@ gdk_gl_texture_download_float (GdkTexture *texture,
   gdk_gl_texture_run (self, gdk_gl_texture_do_download_float, data);
 }
 
+static gboolean
+gdk_gl_texture_is_hdr (GdkTexture *texture)
+{
+  GdkGLTexture *self = GDK_GL_TEXTURE (texture);
+  GdkGLTextureFormat result;
+
+  gdk_gl_texture_run (self, gdk_gl_texture_get_format, &result);
+
+  return gdk_memory_format_is_hdr (result.format);
+}
+
 static void
 gdk_gl_texture_class_init (GdkGLTextureClass *klass)
 {
@@ -328,6 +360,7 @@ gdk_gl_texture_class_init (GdkGLTextureClass *klass)
   texture_class->download_texture = gdk_gl_texture_download_texture;
   texture_class->download = gdk_gl_texture_download;
   texture_class->download_float = gdk_gl_texture_download_float;
+  texture_class->is_hdr = gdk_gl_texture_is_hdr;
   gobject_class->dispose = gdk_gl_texture_dispose;
 }
 
diff --git a/gdk/gdkmemorytexture.c b/gdk/gdkmemorytexture.c
index 6c08ef531b..941e175ab9 100644
--- a/gdk/gdkmemorytexture.c
+++ b/gdk/gdkmemorytexture.c
@@ -166,6 +166,31 @@ gdk_memory_texture_download_float (GdkTexture *texture,
                                gdk_texture_get_height (texture));
 }
 
+gboolean
+gdk_memory_format_is_hdr (GdkMemoryFormat format)
+{
+  switch ((int)format)
+    {
+    case GDK_MEMORY_R16G16B16:
+    case GDK_MEMORY_R16G16B16_FLOAT:
+    case GDK_MEMORY_R16G16B16A16_PREMULTIPLIED:
+    case GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED:
+    case GDK_MEMORY_R32G32B32_FLOAT:
+      return TRUE;
+
+    default:
+      return FALSE;
+    }
+}
+
+static gboolean
+gdk_memory_texture_is_hdr (GdkTexture *texture)
+{
+  GdkMemoryTexture *self = GDK_MEMORY_TEXTURE (texture);
+
+  return gdk_memory_format_is_hdr (self->format);
+}
+
 static void
 gdk_memory_texture_class_init (GdkMemoryTextureClass *klass)
 {
@@ -175,6 +200,7 @@ gdk_memory_texture_class_init (GdkMemoryTextureClass *klass)
   texture_class->download_texture = gdk_memory_texture_download_texture;
   texture_class->download = gdk_memory_texture_download;
   texture_class->download_float = gdk_memory_texture_download_float;
+  texture_class->is_hdr = gdk_memory_texture_is_hdr;
   gobject_class->dispose = gdk_memory_texture_dispose;
 }
 
diff --git a/gdk/gdkmemorytextureprivate.h b/gdk/gdkmemorytextureprivate.h
index ddd9fd1c37..0e14a24fc1 100644
--- a/gdk/gdkmemorytextureprivate.h
+++ b/gdk/gdkmemorytextureprivate.h
@@ -67,6 +67,7 @@ void                    gdk_memory_convert_to_float         (float
                                                              GdkMemoryFormat             src_format,
                                                              gsize                       width,
                                                              gsize                       height);
+gboolean                gdk_memory_format_is_hdr            (GdkMemoryFormat             format);
 
 
 G_END_DECLS
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index b79b671a53..32c8781c54 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -310,6 +310,12 @@ gdk_texture_dispose (GObject *object)
   G_OBJECT_CLASS (gdk_texture_parent_class)->dispose (object);
 }
 
+static gboolean
+gdk_texture_real_is_hdr (GdkTexture *texture)
+{
+  return FALSE;
+}
+
 static void
 gdk_texture_class_init (GdkTextureClass *klass)
 {
@@ -318,6 +324,7 @@ gdk_texture_class_init (GdkTextureClass *klass)
   klass->download_texture = gdk_texture_real_download_texture;
   klass->download = gdk_texture_real_download;
   klass->download_float = gdk_texture_real_download_float;
+  klass->is_hdr = gdk_texture_real_is_hdr;
 
   gobject_class->set_property = gdk_texture_set_property;
   gobject_class->get_property = gdk_texture_get_property;
@@ -973,3 +980,10 @@ gdk_texture_save_to_tiff_bytes (GdkTexture *texture)
   return gdk_save_tiff (texture);
 }
 
+gboolean
+gdk_texture_is_hdr (GdkTexture *texture)
+{
+  g_return_val_if_fail (GDK_IS_TEXTURE (texture), FALSE);
+
+  return GDK_TEXTURE_GET_CLASS (texture)->is_hdr (texture);
+}
diff --git a/gdk/gdktextureprivate.h b/gdk/gdktextureprivate.h
index 814ed5d92c..628ef625b0 100644
--- a/gdk/gdktextureprivate.h
+++ b/gdk/gdktextureprivate.h
@@ -33,10 +33,13 @@ struct _GdkTextureClass {
   void                  (* download_float)              (GdkTexture             *texture,
                                                          float                  *data,
                                                          gsize                   stride);
+  gboolean              (* is_hdr)                      (GdkTexture             *texture);
 };
 
 gboolean                gdk_texture_can_load            (GBytes                 *bytes);
 
+gboolean                gdk_texture_is_hdr              (GdkTexture             *self);
+
 GdkTexture *            gdk_texture_new_for_surface     (cairo_surface_t        *surface);
 cairo_surface_t *       gdk_texture_download_surface    (GdkTexture             *texture);
 /* NB: GdkMemoryTexture */


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