From: Benjamin Otte Date: Thu, 7 Oct 2021 00:41:30 +0000 (+0200) Subject: texture: Make format a property of GdkTexture X-Git-Tag: archive/raspbian/4.6.5+ds-1+rpi1~1^2~19^2~5^2~255^2~5 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=8950c0dc960da50e7e38bcb5984b74286a9f178c;p=gtk4.git texture: Make format a property of GdkTexture For MemoryTexture, this is a simple change. For GLTexture, we need to query the format at texture creation. This sounds like a bad idea and extra work until one realizes that we'd need to do that anyway when using the texure the first time - either when downloading, or when trying to use it in a rendernode, where we will soon need that information to determine if the texture prefers high depth. --- diff --git a/gdk/gdkgltexture.c b/gdk/gdkgltexture.c index 1a321fa879..e39fcd3fbb 100644 --- a/gdk/gdkgltexture.c +++ b/gdk/gdkgltexture.c @@ -140,82 +140,29 @@ gdk_gl_texture_get_tex_image (GdkGLTexture *self, data); } } + static void gdk_gl_texture_do_download_texture (gpointer texture_, gpointer result_) { GdkTexture *texture = texture_; GdkTexture **result = result_; - GdkMemoryFormat format; - GLint internal_format, gl_format, gl_type; + guint gl_internalformat, gl_format, gl_type; guchar *data; gsize stride; GBytes *bytes; - glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format); - - switch (internal_format) - { - case GL_RGB8: - format = GDK_MEMORY_R8G8B8; - gl_format = GL_RGB; - gl_type = GL_UNSIGNED_BYTE; - break; - - case GL_RGBA8: - format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED; - gl_format = GL_RGBA; - gl_type = GL_UNSIGNED_BYTE; - break; - - case GL_RGB16: - format = GDK_MEMORY_R16G16B16; - gl_format = GL_RGB; - gl_type = GL_UNSIGNED_SHORT; - break; - - case GL_RGBA16: - format = GDK_MEMORY_R16G16B16A16_PREMULTIPLIED; - gl_format = GL_RGBA; - gl_type = GL_UNSIGNED_SHORT; - break; - - case GL_RGB16F: - format = GDK_MEMORY_R16G16B16_FLOAT; - gl_format = GL_RGB; - gl_type = GL_HALF_FLOAT; - break; - - case GL_RGBA16F: - format = GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED; - gl_format = GL_RGBA; - gl_type = GL_HALF_FLOAT; - break; - - case GL_RGB32F: - format = GDK_MEMORY_R32G32B32_FLOAT; - gl_format = GL_RGB; - gl_type = GL_FLOAT; - break; - - case GL_RGBA32F: - format = GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED; - gl_format = GL_RGBA; - gl_type = GL_FLOAT; - break; - - 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 - * so that even age old GLES can do it */ - format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED; - gl_format = GL_RGBA; - gl_type = GL_UNSIGNED_BYTE; - break; - } + if (!gdk_memory_format_gl_format (texture->format, + gdk_gl_context_get_use_es (gdk_gl_context_get_current ()), + &gl_internalformat, + &gl_format, + &gl_type)) + { + g_assert_not_reached (); + } - stride = gdk_memory_format_bytes_per_pixel (format) * texture->width; - data = g_malloc (stride * texture->height); + stride = gdk_memory_format_bytes_per_pixel (texture->format) * texture->width; + data = g_malloc_n (stride, texture->height); gdk_gl_texture_get_tex_image (texture_, gl_format, @@ -225,7 +172,7 @@ gdk_gl_texture_do_download_texture (gpointer texture_, bytes = g_bytes_new_take (data, stride * texture->height); *result = gdk_memory_texture_new (texture->width, texture->height, - format, + texture->format, bytes, stride); @@ -378,6 +325,58 @@ gdk_gl_texture_release (GdkGLTexture *self) self->id = 0; } +static void +gdk_gl_texture_do_determine_format (gpointer texture_, + gpointer unused) +{ + GdkTexture *texture = texture_; + GLint internal_format; + + glGetTexLevelParameteriv (GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &internal_format); + + switch (internal_format) + { + case GL_RGB8: + texture->format = GDK_MEMORY_R8G8B8; + break; + + case GL_RGBA8: + texture->format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED; + break; + + case GL_RGB16: + texture->format = GDK_MEMORY_R16G16B16; + break; + + case GL_RGBA16: + texture->format = GDK_MEMORY_R16G16B16A16_PREMULTIPLIED; + break; + + case GL_RGB16F: + texture->format = GDK_MEMORY_R16G16B16_FLOAT; + break; + + case GL_RGBA16F: + texture->format = GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED; + break; + + case GL_RGB32F: + texture->format = GDK_MEMORY_R32G32B32_FLOAT; + break; + + case GL_RGBA32F: + texture->format = GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED; + break; + + 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 + * so that even age old GLES can do it */ + texture->format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED; + break; + } +} + /** * gdk_gl_texture_new: * @context: a `GdkGLContext` @@ -421,6 +420,8 @@ gdk_gl_texture_new (GdkGLContext *context, self->destroy = destroy; self->data = data; + gdk_gl_texture_run (self, gdk_gl_texture_do_determine_format, NULL); + return GDK_TEXTURE (self); } diff --git a/gdk/gdkmemorytexture.c b/gdk/gdkmemorytexture.c index 33ba90fe5c..52723ef805 100644 --- a/gdk/gdkmemorytexture.c +++ b/gdk/gdkmemorytexture.c @@ -34,8 +34,6 @@ struct _GdkMemoryTexture { GdkTexture parent_instance; - GdkMemoryFormat format; - GBytes *bytes; gsize stride; }; @@ -74,7 +72,7 @@ gdk_memory_texture_download (GdkTexture *texture, GDK_MEMORY_DEFAULT, (guchar *) g_bytes_get_data (self->bytes, NULL), self->stride, - self->format, + texture->format, gdk_texture_get_width (texture), gdk_texture_get_height (texture)); } @@ -91,7 +89,7 @@ gdk_memory_texture_download_float (GdkTexture *texture, GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED, (guchar *) g_bytes_get_data (self->bytes, NULL), self->stride, - self->format, + texture->format, gdk_texture_get_width (texture), gdk_texture_get_height (texture)); } @@ -184,19 +182,13 @@ gdk_memory_texture_new (int width, "height", height, NULL); - self->format = format; + GDK_TEXTURE (self)->format = format; self->bytes = bytes; self->stride = stride; return GDK_TEXTURE (self); } -GdkMemoryFormat -gdk_memory_texture_get_format (GdkMemoryTexture *self) -{ - return self->format; -} - const guchar * gdk_memory_texture_get_data (GdkMemoryTexture *self) { diff --git a/gdk/gdkmemorytextureprivate.h b/gdk/gdkmemorytextureprivate.h index f61cccb9ef..0cd5e91f27 100644 --- a/gdk/gdkmemorytextureprivate.h +++ b/gdk/gdkmemorytextureprivate.h @@ -29,7 +29,6 @@ G_BEGIN_DECLS #define GDK_MEMORY_GDK_PIXBUF_OPAQUE GDK_MEMORY_R8G8B8 #define GDK_MEMORY_GDK_PIXBUF_ALPHA GDK_MEMORY_R8G8B8A8 -GdkMemoryFormat gdk_memory_texture_get_format (GdkMemoryTexture *self); const guchar * gdk_memory_texture_get_data (GdkMemoryTexture *self); gsize gdk_memory_texture_get_stride (GdkMemoryTexture *self); diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c index b79b671a53..f4c5928c8d 100644 --- a/gdk/gdktexture.c +++ b/gdk/gdktexture.c @@ -808,6 +808,12 @@ gdk_texture_download_texture (GdkTexture *texture) return texture; } +GdkMemoryFormat +gdk_texture_get_format (GdkTexture *self) +{ + return self->format; +} + gboolean gdk_texture_set_render_data (GdkTexture *self, gpointer key, @@ -972,4 +978,3 @@ gdk_texture_save_to_tiff_bytes (GdkTexture *texture) return gdk_save_tiff (texture); } - diff --git a/gdk/gdktextureprivate.h b/gdk/gdktextureprivate.h index 814ed5d92c..994c9901f8 100644 --- a/gdk/gdktextureprivate.h +++ b/gdk/gdktextureprivate.h @@ -3,6 +3,8 @@ #include "gdktexture.h" +#include "gdkmemorytexture.h" + G_BEGIN_DECLS #define GDK_TEXTURE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_TEXTURE, GdkTextureClass)) @@ -13,6 +15,7 @@ struct _GdkTexture { GObject parent_instance; + GdkMemoryFormat format; int width; int height; @@ -42,6 +45,7 @@ cairo_surface_t * gdk_texture_download_surface (GdkTexture /* NB: GdkMemoryTexture */ GdkTexture * gdk_texture_download_texture (GdkTexture *texture); +GdkMemoryFormat gdk_texture_get_format (GdkTexture *self); gboolean gdk_texture_set_render_data (GdkTexture *self, gpointer key, gpointer data, diff --git a/gdk/loaders/gdkpng.c b/gdk/loaders/gdkpng.c index df453fc6a3..9bdc5e3a43 100644 --- a/gdk/loaders/gdkpng.c +++ b/gdk/loaders/gdkpng.c @@ -481,7 +481,7 @@ gdk_save_png (GdkTexture *texture) height = gdk_texture_get_height (texture); mtexture = gdk_texture_download_texture (texture); - format = gdk_memory_texture_get_format (GDK_MEMORY_TEXTURE (mtexture)); + format = gdk_texture_get_format (mtexture); switch (format) { diff --git a/gdk/loaders/gdktiff.c b/gdk/loaders/gdktiff.c index ed1b0e566b..d2d8dfdbde 100644 --- a/gdk/loaders/gdktiff.c +++ b/gdk/loaders/gdktiff.c @@ -284,7 +284,7 @@ gdk_save_tiff (GdkTexture *texture) height = gdk_texture_get_height (texture); memory_texture = gdk_texture_download_texture (texture); - format = gdk_memory_texture_get_format (GDK_MEMORY_TEXTURE (memory_texture)); + format = gdk_texture_get_format (memory_texture); for (int i = 0; i < G_N_ELEMENTS (format_data); i++) { diff --git a/gsk/ngl/gsknglcommandqueue.c b/gsk/ngl/gsknglcommandqueue.c index b2a474cf59..29cbb551d0 100644 --- a/gsk/ngl/gsknglcommandqueue.c +++ b/gsk/ngl/gsknglcommandqueue.c @@ -1362,7 +1362,7 @@ gsk_ngl_command_queue_upload_texture (GskNglCommandQueue *self, { GdkMemoryTexture *memory_texture = GDK_MEMORY_TEXTURE (texture); data = gdk_memory_texture_get_data (memory_texture); - data_format = gdk_memory_texture_get_format (memory_texture); + data_format = gdk_texture_get_format (texture); data_stride = gdk_memory_texture_get_stride (memory_texture); } else