[gtk/wip/matthiasc/gsk-hdr: 1/5] texture: Add high depth api




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

    texture: Add high depth api
    
    Add private api to find out if a texture is 'deep'.
    For now, we consider float formats and >8bit formats
    to qualify. The implementation for GL textures is reusing
    the machinery we have in place for downloading.

 gdk/gdkgltexture.c            | 60 ++++++++++++++++++++++++++++++++++---------
 gdk/gdkmemorytexture.c        | 12 +++++++++
 gdk/gdkmemorytextureprivate.h |  1 -
 gdk/gdktexture.c              | 14 ++++++++++
 gdk/gdktextureprivate.h       |  3 +++
 5 files changed, 77 insertions(+), 13 deletions(-)
---
diff --git a/gdk/gdkgltexture.c b/gdk/gdkgltexture.c
index 1a321fa879..e35c48c5ce 100644
--- a/gdk/gdkgltexture.c
+++ b/gdk/gdkgltexture.c
@@ -140,17 +140,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);
 
@@ -214,18 +217,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);
 
@@ -320,6 +341,20 @@ 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_high_depth (GdkTexture *texture)
+{
+  GdkGLTexture *self = GDK_GL_TEXTURE (texture);
+  GdkGLTextureFormat result;
+
+  gdk_gl_texture_run (self, gdk_gl_texture_get_format, &result);
+
+  /* We really want to check bits-per-component.
+   * For now, this gives the right answer.
+   */
+  return gdk_memory_format_bytes_per_pixel (result.format) > 4;
+}
+
 static void
 gdk_gl_texture_class_init (GdkGLTextureClass *klass)
 {
@@ -329,6 +364,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_high_depth = gdk_gl_texture_is_high_depth;
   gobject_class->dispose = gdk_gl_texture_dispose;
 }
 
diff --git a/gdk/gdkmemorytexture.c b/gdk/gdkmemorytexture.c
index 33ba90fe5c..5c19d17b2d 100644
--- a/gdk/gdkmemorytexture.c
+++ b/gdk/gdkmemorytexture.c
@@ -96,6 +96,17 @@ gdk_memory_texture_download_float (GdkTexture *texture,
                       gdk_texture_get_height (texture));
 }
 
+static gboolean
+gdk_memory_texture_is_high_depth (GdkTexture *texture)
+{
+  GdkMemoryTexture *self = GDK_MEMORY_TEXTURE (texture);
+
+  /* We really want to check bits-per-component.
+   * For now, this gives the right answer.
+   */
+  return gdk_memory_format_bytes_per_pixel (self->format) > 4;
+}
+
 static void
 gdk_memory_texture_class_init (GdkMemoryTextureClass *klass)
 {
@@ -105,6 +116,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_high_depth = gdk_memory_texture_is_high_depth;
   gobject_class->dispose = gdk_memory_texture_dispose;
 }
 
diff --git a/gdk/gdkmemorytextureprivate.h b/gdk/gdkmemorytextureprivate.h
index f61cccb9ef..eba4b2ae8b 100644
--- a/gdk/gdkmemorytextureprivate.h
+++ b/gdk/gdkmemorytextureprivate.h
@@ -33,7 +33,6 @@ GdkMemoryFormat         gdk_memory_texture_get_format       (GdkMemoryTexture  *
 const guchar *          gdk_memory_texture_get_data         (GdkMemoryTexture  *self);
 gsize                   gdk_memory_texture_get_stride       (GdkMemoryTexture  *self);
 
-
 G_END_DECLS
 
 #endif /* __GDK_MEMORY_TEXTURE_PRIVATE_H__ */
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index b79b671a53..c5d91ed302 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_high_depth (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_high_depth = gdk_texture_real_is_high_depth;
 
   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_high_depth (GdkTexture *texture)
+{
+  g_return_val_if_fail (GDK_IS_TEXTURE (texture), FALSE);
+
+  return GDK_TEXTURE_GET_CLASS (texture)->is_high_depth (texture);
+}
diff --git a/gdk/gdktextureprivate.h b/gdk/gdktextureprivate.h
index 814ed5d92c..c9cca4e1e9 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_high_depth)               (GdkTexture             *texture);
 };
 
 gboolean                gdk_texture_can_load            (GBytes                 *bytes);
 
+gboolean                gdk_texture_is_high_depth       (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]