gtk/tests/rendernode.c
Alexander Larsson 9a7e721181 GdkSurface: Rename various functions and variables
This is an automatic rename of various things related
to the window->surface rename.

Public symbols changed by this is:
 GDK_MODE_WINDOW
 gdk_device_get_window_at_position
 gdk_device_get_window_at_position_double
 gdk_device_get_last_event_window
 gdk_display_get_monitor_at_window
 gdk_drag_context_get_source_window
 gdk_drag_context_get_dest_window
 gdk_drag_context_get_drag_window
 gdk_draw_context_get_window
 gdk_drawing_context_get_window
 gdk_gl_context_get_window
 gdk_synthesize_window_state
 gdk_surface_get_window_type
 gdk_x11_display_set_window_scale
 gsk_renderer_new_for_window
 gsk_renderer_get_window
 gtk_text_view_buffer_to_window_coords
 gtk_tree_view_convert_widget_to_bin_window_coords
 gtk_tree_view_convert_tree_to_bin_window_coords

The commands that generated this are:

git sed -f g "GDK window" "GDK surface"
git sed -f g window_impl surface_impl
(cd gdk; git sed -f g impl_window impl_surface)
git sed -f g WINDOW_IMPL SURFACE_IMPL
git sed -f g GDK_MODE_WINDOW GDK_MODE_SURFACE
git sed -f g gdk_draw_context_get_window gdk_draw_context_get_surface
git sed -f g gdk_drawing_context_get_window gdk_drawing_context_get_surface
git sed -f g gdk_gl_context_get_window gdk_gl_context_get_surface
git sed -f g gsk_renderer_get_window gsk_renderer_get_surface
git sed -f g gsk_renderer_new_for_window gsk_renderer_new_for_surface

(cd gdk; git sed -f g window_type surface_type)
git sed -f g gdk_surface_get_window_type gdk_surface_get_surface_type

git sed -f g window_at_position surface_at_position
git sed -f g event_window event_surface
git sed -f g window_coord surface_coord
git sed -f g window_state surface_state
git sed -f g window_cursor surface_cursor
git sed -f g window_scale surface_scale
git sed -f g window_events surface_events
git sed -f g monitor_at_window monitor_at_surface
git sed -f g window_under_pointer surface_under_pointer
(cd gdk; git sed -f g for_window for_surface)
git sed -f g window_anchor surface_anchor
git sed -f g WINDOW_IS_TOPLEVEL SURFACE_IS_TOPLEVEL
git sed -f g native_window native_surface
git sed -f g source_window source_surface
git sed -f g dest_window dest_surface
git sed -f g drag_window drag_surface
git sed -f g input_window input_surface

git checkout NEWS* po-properties po docs/reference/gtk/migrating-3to4.xml
2018-03-20 12:05:26 +01:00

167 lines
4.8 KiB
C

#include <gtk/gtk.h>
static gboolean benchmark = FALSE;
static gboolean dump_variant = FALSE;
static gboolean fallback = FALSE;
static int runs = 1;
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 },
{ "runs", 'r', 0, G_OPTION_ARG_INT, &runs, "Render the test N times", "N" },
{ NULL }
};
int
main(int argc, char **argv)
{
cairo_surface_t *surface;
GskRenderNode *node;
GError *error = NULL;
GBytes *bytes;
gint64 start, end;
char *contents;
gsize len;
int run;
GOptionContext *context;
context = g_option_context_new ("NODE-FILE PNG-FILE");
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_printerr ("Option parsing failed: %s\n", error->message);
return 1;
}
gtk_init ();
if (runs < 1)
{
g_printerr ("Number of runs given with -r/--runs must be at least 1 and not %d.\n", runs);
return 1;
}
if (!(argc == 3 || (argc == 2 && (dump_variant || benchmark))))
{
g_printerr ("Usage: %s [OPTIONS] NODE-FILE PNG-FILE\n", argv[0]);
return 1;
}
if (!g_file_get_contents (argv[1], &contents, &len, &error))
{
g_printerr ("Could not open node file: %s\n", error->message);
return 1;
}
bytes = g_bytes_new_take (contents, len);
if (dump_variant)
{
GVariant *variant = g_variant_new_from_bytes (G_VARIANT_TYPE ("(suuv)"), bytes, FALSE);
char *s;
s = g_variant_print (variant, FALSE);
g_print ("%s\n", s);
g_free (s);
g_variant_unref (variant);
}
start = g_get_monotonic_time ();
node = gsk_render_node_deserialize (bytes, &error);
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)
{
g_printerr ("Invalid node file: %s\n", error->message);
g_clear_error (&error);
return 1;
}
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);
cairo_translate (cr, - bounds.origin.x, - bounds.origin.y);
for (run = 0; run < runs; run++)
{
if (run > 0)
{
cairo_save (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
cairo_paint (cr);
cairo_restore (cr);
}
start = g_get_monotonic_time ();
gsk_render_node_draw (node, cr);
end = g_get_monotonic_time ();
if (benchmark)
g_print ("Run %d: Rendered fallback in %.4gs\n", run, (double) (end - start) / G_USEC_PER_SEC);
}
cairo_destroy (cr);
}
else
{
GskRenderer *renderer;
GdkSurface *window;
GdkTexture *texture = NULL;
window = gdk_surface_new_toplevel (gdk_display_get_default(), 10 , 10);
renderer = gsk_renderer_new_for_surface (window);
for (run = 0; run < runs; run++)
{
if (run > 0)
g_object_unref (texture);
start = g_get_monotonic_time ();
texture = gsk_renderer_render_texture (renderer, node, NULL);
end = g_get_monotonic_time ();
if (benchmark)
g_print ("Run %u: Rendered using %s in %.4gs\n", run, G_OBJECT_TYPE_NAME (renderer), (double) (end - start) / G_USEC_PER_SEC);
}
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
gdk_texture_get_width (texture),
gdk_texture_get_height (texture));
gdk_texture_download (texture,
cairo_image_surface_get_data (surface),
cairo_image_surface_get_stride (surface));
cairo_surface_mark_dirty (surface);
gsk_renderer_unrealize (renderer);
g_object_unref (texture);
g_object_unref (window);
g_object_unref (renderer);
}
gsk_render_node_unref (node);
if (argc > 2)
{
cairo_status_t status;
status = cairo_surface_write_to_png (surface, argv[2]);
if (status != CAIRO_STATUS_SUCCESS)
{
cairo_surface_destroy (surface);
g_print ("Failed to save PNG file: %s\n", cairo_status_to_string (status));
return 1;
}
}
cairo_surface_destroy (surface);
return 0;
}