testsuite: Add some GL texture tests
authorMatthias Clasen <mclasen@redhat.com>
Fri, 24 Mar 2023 17:56:06 +0000 (13:56 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Fri, 24 Mar 2023 17:57:16 +0000 (13:57 -0400)
Tests creation and upload/download of GL
textures in various contexts.

testsuite/gdk/gltexture.c [new file with mode: 0644]
testsuite/gdk/meson.build

diff --git a/testsuite/gdk/gltexture.c b/testsuite/gdk/gltexture.c
new file mode 100644 (file)
index 0000000..475667d
--- /dev/null
@@ -0,0 +1,128 @@
+#include <gtk/gtk.h>
+#include <epoxy/gl.h>
+
+static cairo_surface_t *
+make_surface (void)
+{
+  cairo_surface_t *surface;
+  cairo_t *cr;
+  guchar *data;
+
+  data = g_malloc (64 * 64 * 4);
+  surface = cairo_image_surface_create_for_data (data,
+                                                 CAIRO_FORMAT_ARGB32,
+                                                 64, 64, 64 * 4);
+  cr = cairo_create (surface);
+  cairo_set_source_rgb (cr, 1, 0, 0);
+  cairo_paint (cr);
+  cairo_destroy (cr);
+
+  return surface;
+}
+
+enum {
+  SAME_CONTEXT,
+  NO_CONTEXT,
+  SHARED_CONTEXT
+};
+
+static void
+test_gltexture (int test)
+{
+  GdkDisplay *display;
+  GdkGLContext *context;
+  GdkGLContext *context2 = NULL;
+  GdkTexture *texture;
+  cairo_surface_t *surface;
+  GError *error = NULL;
+  unsigned int id;
+  guchar *data;
+
+  display = gdk_display_get_default ();
+  if (!gdk_display_prepare_gl (display, &error))
+    {
+      g_test_message ("no GL support: %s", error->message);
+      g_test_skip ("no GL support");
+      g_clear_error (&error);
+      return;
+    }
+
+  context = gdk_display_create_gl_context (display, &error);
+  g_assert_nonnull (context);
+  g_assert_no_error (error);
+
+  gdk_gl_context_realize (context, &error);
+  g_assert_no_error (error);
+
+  surface = make_surface ();
+
+  gdk_gl_context_make_current (context);
+
+  glGenTextures (1, &id);
+  glActiveTexture (GL_TEXTURE0);
+  glBindTexture (GL_TEXTURE_2D, id);
+  glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, 64, 64, 0, GL_BGRA, GL_UNSIGNED_BYTE,
+                cairo_image_surface_get_data (surface));
+
+  g_assert_true (glGetError () == GL_NO_ERROR);
+
+  if (test == NO_CONTEXT)
+    gdk_gl_context_clear_current ();
+  else if (test == SHARED_CONTEXT)
+    {
+      context2 = gdk_display_create_gl_context (display, &error);
+      g_assert_nonnull (context2);
+      g_assert_no_error (error);
+
+      gdk_gl_context_realize (context2, &error);
+      g_assert_no_error (error);
+
+      gdk_gl_context_make_current (context2);
+    }
+
+  texture = gdk_gl_texture_new (context, id, 64, 64, NULL, NULL);
+
+  data = g_malloc0 (64 * 64 * 4);
+  gdk_texture_download (texture, data, 64 * 4);
+
+  g_assert_true (memcmp (data, cairo_image_surface_get_data (surface), 64 * 64 * 4) == 0);
+
+  g_free (data);
+  g_object_unref (texture);
+
+  cairo_surface_destroy (surface);
+
+  g_object_unref (context);
+
+  g_clear_object (&context2);
+}
+
+static void
+test_gltexture_same_context (void)
+{
+  test_gltexture (SAME_CONTEXT);
+}
+
+static void
+test_gltexture_no_context (void)
+{
+  test_gltexture (NO_CONTEXT);
+}
+
+static void
+test_gltexture_shared_context (void)
+{
+  test_gltexture (SHARED_CONTEXT);
+}
+
+int
+main (int argc, char *argv[])
+{
+  gtk_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/gltexture/same-context", test_gltexture_same_context);
+  g_test_add_func ("/gltexture/no-context", test_gltexture_no_context);
+  g_test_add_func ("/gltexture/shared-context", test_gltexture_shared_context);
+
+  return g_test_run ();
+}
index ebaa66fcc935cc3c360786b3890e4e4ea5407b01..05f035139361cb2c4e15c8feaeb293b58408638f 100644 (file)
@@ -17,6 +17,7 @@ tests = [
   { 'name': 'displaymanager' },
   { 'name': 'encoding' },
   { 'name': 'glcontext' },
+  { 'name': 'gltexture' },
   { 'name': 'keysyms' },
   { 'name': 'memorytexture' },
   { 'name': 'rectangle' },