[gtk/matthiasc/for-master: 4/4] Add a test for various texture uploads




commit 915e2e0a8585321781c6dd7f54125b92bf59bca4
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Sep 25 23:02:19 2020 -0400

    Add a test for various texture uploads
    
    Create pixbufs with various characteristics that
    trigger different code paths in the gl texture upload
    function, and show the resulting images. If all goes
    well, they all should look the same.
    
    On my system, this tests texture upload for memory
    formats GDK_MEMORY_B8G8R8A8_PREMULTIPLIED,
    GDK_MEMORY_R8G8B8A8, and GDK_MEMORY_R8G8B8.

 tests/meson.build  |   1 +
 tests/testupload.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 202 insertions(+)
---
diff --git a/tests/meson.build b/tests/meson.build
index 17300c85b8..99f95009f0 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -1,5 +1,6 @@
 gtk_tests = [
   # testname, optional extra sources
+  ['testupload'],
   ['testtransform'],
   ['testdropdown'],
   ['rendernode'],
diff --git a/tests/testupload.c b/tests/testupload.c
new file mode 100644
index 0000000000..fae2d1a2b6
--- /dev/null
+++ b/tests/testupload.c
@@ -0,0 +1,201 @@
+#include <gtk/gtk.h>
+
+static void
+add_to_grid (GtkWidget *grid,
+             int        left,
+             int        top,
+             int        n_channels,
+             gboolean   premul,
+             int        width,
+             int        height,
+             int        stride,
+             GtkWidget *picture)
+{
+  char *text;
+
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Channels"), left, top, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Width"), left, top + 1, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Height"), left, top + 2, 1, 1);
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new ("Stride"), left, top + 3, 1, 1);
+
+  text = g_strdup_printf ("%d%s", n_channels, premul ? " (premul)" : "");
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top, 1, 1);
+  g_free (text);
+  text = g_strdup_printf ("%d", width);
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top + 1, 1, 1);
+  g_free (text);
+  text = g_strdup_printf ("%d", height);
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top + 2, 1, 1);
+  g_free (text);
+  text = g_strdup_printf ("%d", stride);
+  gtk_grid_attach (GTK_GRID (grid), gtk_label_new (text), left + 1, top + 3, 1, 1);
+  g_free (text);
+
+  gtk_grid_attach (GTK_GRID (grid), picture, left + 2, top + 0, 1, 4);
+}
+
+int
+main (int argc, char *argv[])
+{
+  GtkWidget *window, *grid;
+  GdkPixbuf *source, *pb;
+  GError *error = NULL;
+  cairo_t *cr;
+  cairo_surface_t *surface;
+  GBytes *bytes;
+  GdkTexture *texture;
+
+  gtk_init ();
+
+  window = gtk_window_new ();
+  grid = gtk_grid_new ();
+  gtk_widget_set_margin_top (grid, 10);
+  gtk_widget_set_margin_bottom (grid, 10);
+  gtk_widget_set_margin_start (grid, 10);
+  gtk_widget_set_margin_end (grid, 10);
+  gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
+  gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
+  gtk_window_set_child (GTK_WINDOW (window), grid);
+
+  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
+                                              200, 200, TRUE,
+                                              &error);
+  if (!source)
+    g_error ("%s", error->message);
+
+  add_to_grid (grid, 0, 0,
+               gdk_pixbuf_get_n_channels (source),
+               FALSE,
+               gdk_pixbuf_get_width (source),
+               gdk_pixbuf_get_height (source),
+               gdk_pixbuf_get_rowstride (source),
+               gtk_picture_new_for_pixbuf (source));
+
+  g_object_unref (source);
+
+  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
+                                              199, 199, TRUE,
+                                              &error);
+  if (!source)
+    g_error ("%s", error->message);
+
+  add_to_grid (grid, 4, 0,
+               gdk_pixbuf_get_n_channels (source),
+               FALSE,
+               gdk_pixbuf_get_width (source),
+               gdk_pixbuf_get_height (source),
+               gdk_pixbuf_get_rowstride (source),
+               gtk_picture_new_for_pixbuf (source));
+
+  g_object_unref (source);
+
+  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
+                                              200, 200, TRUE,
+                                              &error);
+  if (!source)
+    g_error ("%s", error->message);
+
+  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
+
+  add_to_grid (grid, 0, 5,
+               gdk_pixbuf_get_n_channels (pb),
+               FALSE,
+               gdk_pixbuf_get_width (pb),
+               gdk_pixbuf_get_height (pb),
+               gdk_pixbuf_get_rowstride (pb),
+               gtk_picture_new_for_pixbuf (pb));
+
+  g_object_unref (source);
+  g_object_unref (pb);
+
+  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
+                                              199, 199, TRUE,
+                                              &error);
+  if (!source)
+    g_error ("%s", error->message);
+
+  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
+
+  add_to_grid (grid, 4, 5,
+               gdk_pixbuf_get_n_channels (pb),
+               FALSE,
+               gdk_pixbuf_get_width (pb),
+               gdk_pixbuf_get_height (pb),
+               gdk_pixbuf_get_rowstride (pb),
+               gtk_picture_new_for_pixbuf (pb));
+
+  g_object_unref (source);
+  g_object_unref (pb);
+
+  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
+                                              200, 200, TRUE,
+                                              &error);
+  if (!source)
+    g_error ("%s", error->message);
+
+  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
+  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width (pb), 
gdk_pixbuf_get_height (pb));
+  cr = cairo_create (surface);
+  gdk_cairo_set_source_pixbuf (cr, pb, 0, 0);
+  cairo_paint (cr);
+  cairo_destroy (cr);
+  bytes = g_bytes_new (cairo_image_surface_get_data (surface),
+                       cairo_image_surface_get_stride (surface) *
+                       cairo_image_surface_get_height (surface));
+  texture = 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);
+
+  add_to_grid (grid, 0, 10,
+               4, TRUE,
+               cairo_image_surface_get_width (surface),
+               cairo_image_surface_get_height (surface),
+               cairo_image_surface_get_stride (surface),
+               gtk_picture_new_for_paintable (GDK_PAINTABLE (texture)));
+
+  g_object_unref (source);
+  g_object_unref (pb);
+  cairo_surface_destroy (surface);
+
+  source = gdk_pixbuf_new_from_file_at_scale ("tests/portland-rose.jpg",
+                                              199, 199, TRUE,
+                                              &error);
+  if (!source)
+    g_error ("%s", error->message);
+
+  pb = gdk_pixbuf_add_alpha (source, FALSE, 0, 0, 0);
+  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, gdk_pixbuf_get_width (pb), 
gdk_pixbuf_get_height (pb));
+  cr = cairo_create (surface);
+  gdk_cairo_set_source_pixbuf (cr, pb, 0, 0);
+  cairo_paint (cr);
+  cairo_destroy (cr);
+  bytes = g_bytes_new (cairo_image_surface_get_data (surface),
+                       cairo_image_surface_get_stride (surface) *
+                       cairo_image_surface_get_height (surface));
+  texture = 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);
+
+  add_to_grid (grid, 4, 10,
+               4, TRUE,
+               cairo_image_surface_get_width (surface),
+               cairo_image_surface_get_height (surface),
+               cairo_image_surface_get_stride (surface),
+               gtk_picture_new_for_paintable (GDK_PAINTABLE (texture)));
+
+  g_object_unref (source);
+  g_object_unref (pb);
+  cairo_surface_destroy (surface);
+  gtk_window_present (GTK_WINDOW (window));
+
+  while (g_list_model_get_n_items (gtk_window_get_toplevels ()) > 0)
+    g_main_context_iteration (NULL, TRUE);
+
+  return 0;
+}


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