tools: Split out a function

There's a function now that creates a renderer by name - and allows NULL
for the default renderer
This commit is contained in:
Benjamin Otte 2024-01-11 14:41:29 +01:00
parent 17df646663
commit c1e2ffac83
3 changed files with 69 additions and 7 deletions

View File

@ -143,14 +143,13 @@ render_file (const char *filename,
{
GdkTexture *texture;
GskRenderer *renderer;
GdkSurface *window;
if (renderer_name)
g_object_set_data_full (G_OBJECT (gdk_display_get_default ()), "gsk-renderer",
g_strdup (renderer_name), g_free);
window = gdk_surface_new_toplevel (gdk_display_get_default ());
renderer = gsk_renderer_new_for_surface (window);
renderer = create_renderer (renderer_name, &error);
if (renderer == NULL)
{
g_printerr (_("Failed to create renderer: %s\n"), error->message);
exit (1);
}
texture = gsk_renderer_render_texture (renderer, node, NULL);

View File

@ -29,6 +29,9 @@
#include <gtk/gtk.h>
#include "gtk-rendernode-tool.h"
#ifdef GDK_WINDOWING_BROADWAY
#include <gsk/broadway/gskbroadwayrenderer.h>
#endif
static void
deserialize_error_func (const GskParseLocation *start,
@ -73,3 +76,62 @@ load_node_file (const char *filename)
return gsk_render_node_deserialize (bytes, deserialize_error_func, NULL);
}
/* keep in sync with gsk/gskrenderer.c */
static GskRenderer *
get_renderer_for_name (const char *renderer_name)
{
if (renderer_name == NULL)
return NULL;
#ifdef GDK_WINDOWING_BROADWAY
else if (g_ascii_strcasecmp (renderer_name, "broadway") == 0)
return gsk_broadway_renderer_new ();
#endif
else if (g_ascii_strcasecmp (renderer_name, "cairo") == 0)
return gsk_cairo_renderer_new ();
else if (g_ascii_strcasecmp (renderer_name, "opengl") == 0 ||
g_ascii_strcasecmp (renderer_name, "gl") == 0)
return gsk_gl_renderer_new ();
else if (g_ascii_strcasecmp (renderer_name, "ngl") == 0)
return gsk_ngl_renderer_new ();
#ifdef GDK_RENDERING_VULKAN
else if (g_ascii_strcasecmp (renderer_name, "vulkan") == 0)
return gsk_vulkan_renderer_new ();
#endif
else
return NULL;
}
GskRenderer *
create_renderer (const char *name, GError **error)
{
GskRenderer *renderer;
if (name == NULL)
{
/* awwwwwkward - there should be code to get the
* default renderer without a surface */
static GdkSurface *window = NULL;
if (window == NULL)
window = gdk_surface_new_toplevel (gdk_display_get_default ());
return gsk_renderer_new_for_surface (window);
}
renderer = get_renderer_for_name (name);
if (renderer == NULL)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"No renderer named \"%s\"", name);
return NULL;
}
if (!gsk_renderer_realize_for_display (renderer, gdk_display_get_default (), error))
{
g_object_unref (renderer);
return NULL;
}
return renderer;
}

View File

@ -6,3 +6,4 @@ void do_render (int *argc, const char ***argv);
void do_info (int *argc, const char ***argv);
GskRenderNode *load_node_file (const char *filename);
GskRenderer *create_renderer (const char *name, GError **error);