[gtk/modern-testsuite: 7/22] reftests: Use GdkTexture instead of cairo_surface_t




commit 6650e7bbb5b13a650893952c556cdab1460ccd66
Author: Benjamin Otte <otte redhat com>
Date:   Wed Sep 15 06:55:25 2021 +0200

    reftests: Use GdkTexture instead of cairo_surface_t
    
    This also switches the rendering code from using gsk_render_node_draw()
    to gsk_renderer_render_texture().
    
    Some tests are broken with the GL renderer, so this patch forces the
    Cairo renderer until they get fixed.

 testsuite/reftests/gtk-reftest.c      | 38 +++++++++++++++++++++------------
 testsuite/reftests/meson.build        |  2 +-
 testsuite/reftests/reftest-compare.c  | 40 +++++++++++++++++++++++++++++++++++
 testsuite/reftests/reftest-compare.h  |  3 +++
 testsuite/reftests/reftest-snapshot.c | 37 +++++++++++++++++---------------
 testsuite/reftests/reftest-snapshot.h |  2 +-
 6 files changed, 90 insertions(+), 32 deletions(-)
---
diff --git a/testsuite/reftests/gtk-reftest.c b/testsuite/reftests/gtk-reftest.c
index efe48e6480..f247ac4ef1 100644
--- a/testsuite/reftests/gtk-reftest.c
+++ b/testsuite/reftests/gtk-reftest.c
@@ -269,13 +269,13 @@ remove_extra_css (GtkStyleProvider *provider)
 }
 
 static void
-save_image (cairo_surface_t *surface,
-            const char      *test_name,
-            const char      *extension)
+save_image (GdkTexture *texture,
+            const char *test_name,
+            const char *extension)
 {
   GError *error = NULL;
   char *filename;
-  int ret;
+  gboolean ret;
   
   filename = get_output_file (test_name, extension, &error);
   if (filename == NULL)
@@ -286,8 +286,8 @@ save_image (cairo_surface_t *surface,
     }
 
   g_test_message ("Storing test result image at %s", filename);
-  ret = cairo_surface_write_to_png (surface, filename);
-  g_assert_true (ret == CAIRO_STATUS_SUCCESS);
+  ret = gdk_texture_save_to_png (texture, filename);
+  g_assert_true (ret);
 
   g_free (filename);
 }
@@ -296,7 +296,7 @@ static void
 test_ui_file (GFile *file)
 {
   char *ui_file, *reference_file;
-  cairo_surface_t *ui_image, *reference_image, *diff_image;
+  GdkTexture *ui_image, *reference_image, *diff_image;
   GtkStyleProvider *provider;
 
   ui_file = g_file_get_path (file);
@@ -306,25 +306,37 @@ test_ui_file (GFile *file)
   ui_image = reftest_snapshot_ui_file (ui_file);
 
   if ((reference_file = get_reference_image (ui_file)) != NULL)
-    reference_image = cairo_image_surface_create_from_png (reference_file);
+    {
+      GError *error = NULL;
+
+      reference_image = gdk_texture_new_from_filename (reference_file, &error);
+      if (reference_image == NULL)
+        {
+          g_test_message ("Failed to load reference image: %s", error->message);
+          g_clear_error (&error);
+          g_test_fail ();
+        }
+    }
   else if ((reference_file = get_test_file (ui_file, ".ref.ui", TRUE)) != NULL)
     reference_image = reftest_snapshot_ui_file (reference_file);
   else
     {
-      reference_image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
+      reference_image = NULL;
       g_test_message ("No reference image.");
       g_test_fail ();
     }
   g_free (reference_file);
+  if (reference_image == NULL)
+    reference_image = gdk_memory_texture_new (1, 1, GDK_MEMORY_DEFAULT, g_bytes_new ((guchar[4]) {0, 0, 0, 
0}, 4), 4);
 
-  diff_image = reftest_compare_surfaces (ui_image, reference_image);
+  diff_image = reftest_compare_textures (ui_image, reference_image);
 
   save_image (ui_image, ui_file, ".out.png");
   save_image (reference_image, ui_file, ".ref.png");
   if (diff_image)
     {
       save_image (diff_image, ui_file, ".diff.png");
-      cairo_surface_destroy (diff_image);
+      g_object_unref (diff_image);
       g_test_fail ();
     }
 
@@ -332,8 +344,8 @@ test_ui_file (GFile *file)
 
   g_free (ui_file);
 
-  cairo_surface_destroy (ui_image);
-  cairo_surface_destroy (reference_image);
+  g_clear_object (&ui_image);
+  g_clear_object (&reference_image);
 }
 
 static int
diff --git a/testsuite/reftests/meson.build b/testsuite/reftests/meson.build
index c20cc99a7a..2e079830db 100644
--- a/testsuite/reftests/meson.build
+++ b/testsuite/reftests/meson.build
@@ -467,7 +467,7 @@ xfails = [
 
 reftest_env = environment()
 reftest_env.set('GTK_A11Y', 'test')
-reftest_env.set('GSK_RENDERER', 'opengl')
+reftest_env.set('GSK_RENDERER', 'cairo')
 reftest_env.set('G_TEST_SRCDIR', meson.current_source_dir())
 reftest_env.set('G_TEST_BUILDDIR', meson.current_build_dir())
 reftest_env.set('GIO_USE_VFS', 'local')
diff --git a/testsuite/reftests/reftest-compare.c b/testsuite/reftests/reftest-compare.c
index a24ab337a6..ff39ce6a58 100644
--- a/testsuite/reftests/reftest-compare.c
+++ b/testsuite/reftests/reftest-compare.c
@@ -179,3 +179,43 @@ reftest_compare_surfaces (cairo_surface_t *surface1,
   return diff;
 }
 
+GdkTexture *
+reftest_compare_textures (GdkTexture *texture1,
+                          GdkTexture *texture2)
+{
+  int w, h;
+  guchar *data1, *data2;
+  GBytes *bytes;
+  cairo_surface_t *surface;
+  GdkTexture *diff;
+  
+  w = MAX (gdk_texture_get_width (texture1), gdk_texture_get_width (texture2));
+  h = MAX (gdk_texture_get_height (texture1), gdk_texture_get_height (texture2));
+
+  data1 = g_malloc_n (w * 4, h);
+  gdk_texture_download (texture1, data1, w * 4);
+  data2 = g_malloc_n (w * 4, h);
+  gdk_texture_download (texture2, data2, w * 4);
+
+  surface = buffer_diff_core (data1, w * 4,
+                              data2, w * 4,
+                              w, h);
+  if (surface == NULL)
+    return NULL;
+
+  bytes = g_bytes_new_with_free_func (cairo_image_surface_get_data (surface),
+                                      cairo_image_surface_get_height (surface)
+                                      * cairo_image_surface_get_stride (surface),
+                                      (GDestroyNotify) cairo_surface_destroy,
+                                      cairo_surface_reference (surface));
+  
+  diff = gdk_memory_texture_new (cairo_image_surface_get_width (surface),
+                                 cairo_image_surface_get_height (surface),
+                                 GDK_MEMORY_DEFAULT,
+                                 bytes,
+                                 cairo_image_surface_get_stride (surface));
+
+  g_bytes_unref (bytes);
+
+  return diff;
+}
diff --git a/testsuite/reftests/reftest-compare.h b/testsuite/reftests/reftest-compare.h
index 551b1c5a92..04b8031fe1 100644
--- a/testsuite/reftests/reftest-compare.h
+++ b/testsuite/reftests/reftest-compare.h
@@ -25,6 +25,9 @@ G_BEGIN_DECLS
 G_MODULE_EXPORT
 cairo_surface_t *       reftest_compare_surfaces        (cairo_surface_t        *surface1,
                                                          cairo_surface_t        *surface2);
+G_MODULE_EXPORT
+GdkTexture *            reftest_compare_textures        (GdkTexture             *texture1,
+                                                         GdkTexture             *texture2);
 
 G_END_DECLS
 
diff --git a/testsuite/reftests/reftest-snapshot.c b/testsuite/reftests/reftest-snapshot.c
index 7a7a77fd96..1787c73ab8 100644
--- a/testsuite/reftests/reftest-snapshot.c
+++ b/testsuite/reftests/reftest-snapshot.c
@@ -215,12 +215,12 @@ reftest_uninhibit_snapshot (void)
 
 static void
 draw_paintable (GdkPaintable *paintable,
-                gpointer      out_surface)
+                gpointer      out_texture)
 {
   GtkSnapshot *snapshot;
   GskRenderNode *node;
-  cairo_surface_t *surface;
-  cairo_t *cr;
+  GdkTexture *texture;
+  GskRenderer *renderer;
 
   if (inhibit_count > 0)
     return;
@@ -238,27 +238,30 @@ draw_paintable (GdkPaintable *paintable,
   if (node == NULL)
     return;
 
-  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
-                                        gdk_paintable_get_intrinsic_width (paintable),
-                                        gdk_paintable_get_intrinsic_height (paintable));
-
-  cr = cairo_create (surface);
-  gsk_render_node_draw (node, cr);
-  cairo_destroy (cr);
+  renderer = gtk_native_get_renderer (
+                 gtk_widget_get_native (
+                     gtk_widget_paintable_get_widget (GTK_WIDGET_PAINTABLE (paintable))));
+  texture = gsk_renderer_render_texture (renderer,
+                                         node,
+                                         &GRAPHENE_RECT_INIT (
+                                           0, 0,
+                                           gdk_paintable_get_intrinsic_width (paintable),
+                                           gdk_paintable_get_intrinsic_height (paintable)
+                                         ));
   gsk_render_node_unref (node);
 
-  g_signal_handlers_disconnect_by_func (paintable, draw_paintable, out_surface);
+  g_signal_handlers_disconnect_by_func (paintable, draw_paintable, out_texture);
 
-  *(cairo_surface_t **) out_surface = surface;
+  *(GdkTexture **) out_texture = texture;
 
   g_idle_add (quit_when_idle, loop);
 }
 
-static cairo_surface_t *
+static GdkTexture *
 snapshot_widget (GtkWidget *widget)
 {
   GdkPaintable *paintable;
-  cairo_surface_t *surface;
+  GdkTexture *texture = NULL;
 
   g_assert_true (gtk_widget_get_realized (widget));
 
@@ -270,17 +273,17 @@ snapshot_widget (GtkWidget *widget)
    * to delay the snapshot.
    */
   paintable = gtk_widget_paintable_new (widget);
-  g_signal_connect (paintable, "invalidate-contents", G_CALLBACK (draw_paintable), &surface);
+  g_signal_connect (paintable, "invalidate-contents", G_CALLBACK (draw_paintable), &texture);
   g_main_loop_run (loop);
 
   g_main_loop_unref (loop);
   g_object_unref (paintable);
   gtk_window_destroy (GTK_WINDOW (widget));
 
-  return surface;
+  return texture;
 }
 
-cairo_surface_t *
+GdkTexture *
 reftest_snapshot_ui_file (const char *ui_file)
 {
   GtkWidget *window;
diff --git a/testsuite/reftests/reftest-snapshot.h b/testsuite/reftests/reftest-snapshot.h
index 5a24388722..31dca9e6af 100644
--- a/testsuite/reftests/reftest-snapshot.h
+++ b/testsuite/reftests/reftest-snapshot.h
@@ -23,7 +23,7 @@
 G_BEGIN_DECLS
 
 G_MODULE_EXPORT
-cairo_surface_t *       reftest_snapshot_ui_file                (const char     *ui_file);
+GdkTexture *            reftest_snapshot_ui_file                (const char     *ui_file);
 
 G_END_DECLS
 


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