From 4e866ec06bbaee5d4ba82a8a72bb0f1784c955c0 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Fri, 23 Dec 2016 07:07:59 +0100 Subject: [PATCH] tests: Add some command-line flags to rendernode --- tests/rendernode.c | 102 +++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 37 deletions(-) diff --git a/tests/rendernode.c b/tests/rendernode.c index c7d4ed1c95..12374df2cf 100644 --- a/tests/rendernode.c +++ b/tests/rendernode.c @@ -1,8 +1,12 @@ #include +static gboolean benchmark = FALSE; +static gboolean dump_variant = FALSE; static gboolean fallback = FALSE; static GOptionEntry options[] = { + { "benchmark", 'b', 0, G_OPTION_ARG_NONE, &benchmark, "Time operations", NULL }, + { "dump-variant", 'd', 0, G_OPTION_ARG_NONE, &dump_variant, "Dump GVariant structure", NULL }, { "fallback", '\0', 0, G_OPTION_ARG_NONE, &fallback, "Draw node without a renderer", NULL }, { NULL } }; @@ -14,6 +18,7 @@ main(int argc, char **argv) GskRenderNode *node; GError *error = NULL; GBytes *bytes; + gint64 start, end; char *contents; gsize len; @@ -24,7 +29,7 @@ main(int argc, char **argv) return 1; } - if (argc != 3) + if (argc != 3 && (argc != 2 && dump_variant)) { g_printerr ("Usage: %s [OPTIONS] NODE-FILE PNG-FILE\n", argv[0]); return 1; @@ -37,7 +42,23 @@ main(int argc, char **argv) } bytes = g_bytes_new_take (contents, len); + if (dump_variant) + { + GVariant *variant = g_variant_new_from_bytes (G_VARIANT_TYPE ("(suuv)"), bytes, FALSE); + + g_variant_print (variant, FALSE); + g_variant_unref (variant); + } + + start = g_get_monotonic_time (); node = gsk_render_node_deserialize (bytes); + end = g_get_monotonic_time (); + if (benchmark) + { + char *bytes_string = g_format_size (g_bytes_get_size (bytes)); + g_print ("Loaded %s in %.4gs\n", bytes_string, (double) (end - start) / G_USEC_PER_SEC); + g_free (bytes_string); + } g_bytes_unref (bytes); if (node == NULL) @@ -46,52 +67,59 @@ main(int argc, char **argv) return 1; } - if (fallback) + if (argc > 2) { - graphene_rect_t bounds; - cairo_t *cr; + if (fallback) + { + graphene_rect_t bounds; + cairo_t *cr; - gsk_render_node_get_bounds (node, &bounds); - surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, ceil (bounds.size.width), ceil (bounds.size.height)); - cr = cairo_create (surface); + gsk_render_node_get_bounds (node, &bounds); + surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, ceil (bounds.size.width), ceil (bounds.size.height)); + cr = cairo_create (surface); - cairo_translate (cr, - bounds.origin.x, - bounds.origin.y); - gsk_render_node_draw (node, cr); + cairo_translate (cr, - bounds.origin.x, - bounds.origin.y); + gsk_render_node_draw (node, cr); - cairo_destroy (cr); - } - else - { - GskRenderer *renderer; - GdkWindow *window; - GskTexture *texture; + cairo_destroy (cr); + } + else + { + GskRenderer *renderer; + GdkWindow *window; + GskTexture *texture; - window = gdk_window_new_toplevel (gdk_display_get_default(), 0, 10 , 10); - renderer = gsk_renderer_new_for_window (window); + window = gdk_window_new_toplevel (gdk_display_get_default(), 0, 10 , 10); + renderer = gsk_renderer_new_for_window (window); - texture = gsk_renderer_render_texture (renderer, node, NULL); - surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, - gsk_texture_get_width (texture), - gsk_texture_get_height (texture)); - gsk_texture_download (texture, - cairo_image_surface_get_data (surface), - cairo_image_surface_get_stride (surface)); - cairo_surface_mark_dirty (surface); - gsk_texture_unref (texture); - g_object_unref (window); - g_object_unref (renderer); - } + start = g_get_monotonic_time (); + texture = gsk_renderer_render_texture (renderer, node, NULL); + end = g_get_monotonic_time (); + if (benchmark) + g_print ("Rendered using %s in %.4gs\n", G_OBJECT_TYPE_NAME (renderer), (double) (end - start) / G_USEC_PER_SEC); + surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, + gsk_texture_get_width (texture), + gsk_texture_get_height (texture)); + gsk_texture_download (texture, + cairo_image_surface_get_data (surface), + cairo_image_surface_get_stride (surface)); + cairo_surface_mark_dirty (surface); + gsk_texture_unref (texture); + g_object_unref (window); + g_object_unref (renderer); + } - gsk_render_node_unref (node); + gsk_render_node_unref (node); + + if (cairo_surface_write_to_png (surface, argv[2])) + { + cairo_surface_destroy (surface); + g_print ("Failed to save PNG file.\n"); + return 1; + } - if (cairo_surface_write_to_png (surface, argv[2])) - { cairo_surface_destroy (surface); - g_print ("Failed to save PNG file.\n"); - return 1; } - cairo_surface_destroy (surface); - return 0; }