gl: Add a private way to query mipmap status
authorMatthias Clasen <mclasen@redhat.com>
Thu, 23 Mar 2023 12:44:26 +0000 (08:44 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 24 Mar 2023 03:16:54 +0000 (23:16 -0400)
Make GdkGLTexture determine if the texture has
a mipmap, and provide private API to query this
information.

This check is done in gdkgltexture.c instead of
gskgldriver.c, since we're already binding the
texture here for other reasons, so it is easy
to query a few more things.

gdk/gdkgltexture.c
gdk/gdkgltextureprivate.h

index 85ea47b680d87e816b354cdbaab9ec5b6292e9b5..3a9e230afe183c7e83a2206259a59f5ac587f003 100644 (file)
@@ -38,6 +38,7 @@ struct _GdkGLTexture {
 
   GdkGLContext *context;
   guint id;
+  gboolean has_mipmap;
 
   GdkTexture *saved;
 
@@ -284,6 +285,12 @@ gdk_gl_texture_get_id (GdkGLTexture *self)
   return self->id;
 }
 
+gboolean
+gdk_gl_texture_has_mipmap (GdkGLTexture *self)
+{
+  return self->has_mipmap;
+}
+
 /**
  * gdk_gl_texture_release:
  * @self: a `GdkTexture` wrapping a GL texture
@@ -315,6 +322,7 @@ gdk_gl_texture_determine_format (GdkGLTexture *self)
   GdkTexture *texture = GDK_TEXTURE (self);
   GLint active_texture;
   GLint internal_format;
+  GLint width, height;
 
   /* Abort if somebody else is GL-ing here... */
   if (!gdk_gl_context_is_shared (self->context, gdk_gl_context_get_current ()) ||
@@ -322,6 +330,7 @@ gdk_gl_texture_determine_format (GdkGLTexture *self)
       !gdk_gl_context_check_version (gdk_gl_context_get_current (), 0, 0, 3, 1))
     {
       texture->format = GDK_MEMORY_DEFAULT;
+      self->has_mipmap = FALSE;
       return;
     }
 
@@ -411,6 +420,20 @@ gdk_gl_texture_determine_format (GdkGLTexture *self)
       break;
   }
 
+  /* Determine if the texture has a mipmap.
+   * We do this here, since it requires binding the texture,
+   * and we're already doing that here.
+   * GL has no way to directly query 'mipmap completeness' of textures,
+   * so we just check that level 1 has the expected size, and assume
+   * that means somebody called glGenerateMipmap().
+   */
+  glGetTexLevelParameteriv (GL_TEXTURE_2D, 1, GL_TEXTURE_WIDTH, &width);
+  glGetTexLevelParameteriv (GL_TEXTURE_2D, 1, GL_TEXTURE_HEIGHT, &height);
+
+  self->has_mipmap = width == texture->width / 2 &&
+                     height == texture->height / 2;
+
+  /* restore previous state */
   glBindTexture (GL_TEXTURE_2D, active_texture);
 }
 
index 06035eea07bab15731c02eb8c74bddc19170b4c1..f00fecd4bd328fa600a732d554b1ec81b179ae1f 100644 (file)
@@ -9,6 +9,7 @@ G_BEGIN_DECLS
 
 GdkGLContext *          gdk_gl_texture_get_context      (GdkGLTexture           *self);
 guint                   gdk_gl_texture_get_id           (GdkGLTexture           *self);
+gboolean                gdk_gl_texture_has_mipmap       (GdkGLTexture           *self);
 
 G_END_DECLS