2017-12-25 11:56:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <stdlib.h>
|
2021-02-13 18:21:25 +00:00
|
|
|
#include "../reftests/reftest-compare.h"
|
2017-12-25 11:56:21 +00:00
|
|
|
|
2019-06-03 00:40:43 +00:00
|
|
|
static char *arg_output_dir = NULL;
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
get_output_dir (void)
|
|
|
|
{
|
|
|
|
static const char *output_dir = NULL;
|
|
|
|
GError *error = NULL;
|
2019-06-09 19:49:44 +00:00
|
|
|
GFile *file;
|
2019-06-03 00:40:43 +00:00
|
|
|
|
|
|
|
if (output_dir)
|
|
|
|
return output_dir;
|
|
|
|
|
|
|
|
if (arg_output_dir)
|
|
|
|
{
|
2020-05-18 09:44:38 +00:00
|
|
|
GFile *arg_file = g_file_new_for_commandline_arg (arg_output_dir);
|
2020-05-15 13:18:35 +00:00
|
|
|
const char *subdir;
|
|
|
|
|
|
|
|
subdir = g_getenv ("TEST_OUTPUT_SUBDIR");
|
|
|
|
if (subdir)
|
|
|
|
{
|
2020-05-18 09:44:38 +00:00
|
|
|
GFile *child = g_file_get_child (arg_file, subdir);
|
|
|
|
g_object_unref (arg_file);
|
|
|
|
arg_file = child;
|
2020-05-15 13:18:35 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 09:44:38 +00:00
|
|
|
output_dir = g_file_get_path (arg_file);
|
|
|
|
g_object_unref (arg_file);
|
2019-06-03 00:40:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
output_dir = g_get_tmp_dir ();
|
|
|
|
}
|
|
|
|
|
2019-06-09 19:49:44 +00:00
|
|
|
/* Just try to create the output directory.
|
|
|
|
* If it already exists, that's exactly what we wanted to check,
|
|
|
|
* so we can happily skip that error.
|
|
|
|
*/
|
|
|
|
file = g_file_new_for_path (output_dir);
|
|
|
|
if (!g_file_make_directory_with_parents (file, NULL, &error))
|
2019-06-03 00:40:43 +00:00
|
|
|
{
|
2019-06-09 19:49:44 +00:00
|
|
|
g_object_unref (file);
|
2019-06-03 00:40:43 +00:00
|
|
|
|
2019-06-09 19:49:44 +00:00
|
|
|
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
|
2019-06-03 00:40:43 +00:00
|
|
|
{
|
|
|
|
g_error ("Failed to create output dir: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-06-09 19:49:44 +00:00
|
|
|
g_error_free (error);
|
2019-06-03 00:40:43 +00:00
|
|
|
}
|
2019-07-07 05:23:05 +00:00
|
|
|
else
|
|
|
|
g_object_unref (file);
|
2019-06-09 19:49:44 +00:00
|
|
|
|
2019-06-03 00:40:43 +00:00
|
|
|
return output_dir;
|
|
|
|
}
|
|
|
|
|
2020-05-18 09:44:38 +00:00
|
|
|
static char *
|
2017-12-25 11:56:21 +00:00
|
|
|
file_replace_extension (const char *old_file,
|
|
|
|
const char *old_ext,
|
|
|
|
const char *new_ext)
|
|
|
|
{
|
|
|
|
GString *file = g_string_new (NULL);
|
|
|
|
|
|
|
|
if (g_str_has_suffix (old_file, old_ext))
|
|
|
|
g_string_append_len (file, old_file, strlen (old_file) - strlen (old_ext));
|
|
|
|
else
|
|
|
|
g_string_append (file, old_file);
|
|
|
|
|
|
|
|
g_string_append (file, new_ext);
|
|
|
|
|
|
|
|
return g_string_free (file, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_output_file (const char *file,
|
|
|
|
const char *orig_ext,
|
|
|
|
const char *new_ext)
|
|
|
|
{
|
|
|
|
const char *dir;
|
|
|
|
char *result, *base;
|
|
|
|
char *name;
|
|
|
|
|
2019-06-03 00:40:43 +00:00
|
|
|
dir = get_output_dir ();
|
2017-12-25 11:56:21 +00:00
|
|
|
base = g_path_get_basename (file);
|
|
|
|
name = file_replace_extension (base, orig_ext, new_ext);
|
|
|
|
|
|
|
|
result = g_strconcat (dir, G_DIR_SEPARATOR_S, name, NULL);
|
|
|
|
|
|
|
|
g_free (base);
|
|
|
|
g_free (name);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-09-15 20:13:57 +00:00
|
|
|
save_image (GdkTexture *texture,
|
|
|
|
const char *test_name,
|
|
|
|
const char *extension)
|
2017-12-25 11:56:21 +00:00
|
|
|
{
|
|
|
|
char *filename = get_output_file (test_name, ".node", extension);
|
2021-09-15 20:13:57 +00:00
|
|
|
gboolean result;
|
2017-12-25 11:56:21 +00:00
|
|
|
|
2019-03-19 02:57:04 +00:00
|
|
|
g_print ("Storing test result image at %s\n", filename);
|
2021-09-15 20:13:57 +00:00
|
|
|
result = gdk_texture_save_to_png (texture, filename);
|
|
|
|
g_assert_true (result);
|
2017-12-25 11:56:21 +00:00
|
|
|
g_free (filename);
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:30:46 +00:00
|
|
|
static void
|
2020-11-17 03:21:27 +00:00
|
|
|
deserialize_error_func (const GskParseLocation *start,
|
|
|
|
const GskParseLocation *end,
|
|
|
|
const GError *error,
|
|
|
|
gpointer user_data)
|
2019-04-24 16:30:46 +00:00
|
|
|
{
|
2020-11-17 03:21:27 +00:00
|
|
|
GString *string = g_string_new ("<data>");
|
2019-04-24 16:30:46 +00:00
|
|
|
|
2020-11-17 03:21:27 +00:00
|
|
|
g_string_append_printf (string, ":%zu:%zu",
|
|
|
|
start->lines + 1, start->line_chars + 1);
|
|
|
|
if (start->lines != end->lines || start->line_chars != end->line_chars)
|
|
|
|
{
|
|
|
|
g_string_append (string, "-");
|
|
|
|
if (start->lines != end->lines)
|
|
|
|
g_string_append_printf (string, "%zu:", end->lines + 1);
|
|
|
|
g_string_append_printf (string, "%zu", end->line_chars + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_warning ("Error at %s: %s", string->str, error->message);
|
2019-04-24 16:30:46 +00:00
|
|
|
|
2020-11-17 03:21:27 +00:00
|
|
|
g_string_free (string, TRUE);
|
2019-04-24 16:30:46 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 00:40:43 +00:00
|
|
|
static const GOptionEntry options[] = {
|
|
|
|
{ "output", 0, 0, G_OPTION_ARG_FILENAME, &arg_output_dir,
|
|
|
|
"Directory to save image files to", "DIR" },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2017-12-25 11:56:21 +00:00
|
|
|
/*
|
2019-06-03 00:40:43 +00:00
|
|
|
* Non-option arguments:
|
2017-12-25 11:56:21 +00:00
|
|
|
* 1) .node file to compare
|
|
|
|
* 2) .png file to compare the rendered .node file to
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2021-09-15 20:13:57 +00:00
|
|
|
GdkTexture *reference_texture;
|
|
|
|
GdkTexture *rendered_texture;
|
2017-12-25 11:56:21 +00:00
|
|
|
GskRenderer *renderer;
|
2018-03-20 10:40:08 +00:00
|
|
|
GdkSurface *window;
|
2017-12-25 11:56:21 +00:00
|
|
|
GskRenderNode *node;
|
|
|
|
const char *node_file;
|
|
|
|
const char *png_file;
|
2019-05-20 01:29:11 +00:00
|
|
|
gboolean success = TRUE;
|
2019-06-03 00:40:43 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
GOptionContext *context;
|
|
|
|
|
2021-09-14 20:10:39 +00:00
|
|
|
(g_test_init) (&argc, &argv, NULL);
|
|
|
|
|
2019-07-25 17:48:52 +00:00
|
|
|
context = g_option_context_new ("NODE REF - run GSK node tests");
|
2019-06-03 00:40:43 +00:00
|
|
|
g_option_context_add_main_entries (context, options, NULL);
|
|
|
|
g_option_context_set_ignore_unknown_options (context, TRUE);
|
|
|
|
|
|
|
|
if (!g_option_context_parse (context, &argc, &argv, &error))
|
|
|
|
{
|
|
|
|
g_error ("Option parsing failed: %s\n", error->message);
|
|
|
|
return 1;
|
|
|
|
}
|
2019-07-25 17:48:52 +00:00
|
|
|
else if (argc != 3)
|
|
|
|
{
|
|
|
|
char *help = g_option_context_get_help (context, TRUE, NULL);
|
|
|
|
g_print ("%s", help);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-12-25 11:56:21 +00:00
|
|
|
|
2020-05-24 15:14:18 +00:00
|
|
|
g_option_context_free (context);
|
|
|
|
|
2017-12-25 11:56:21 +00:00
|
|
|
gtk_init ();
|
|
|
|
|
|
|
|
node_file = argv[1];
|
|
|
|
png_file = argv[2];
|
|
|
|
|
2020-07-30 21:06:59 +00:00
|
|
|
window = gdk_surface_new_toplevel (gdk_display_get_default());
|
2018-03-20 11:05:26 +00:00
|
|
|
renderer = gsk_renderer_new_for_surface (window);
|
2017-12-25 11:56:21 +00:00
|
|
|
|
2019-03-19 02:57:04 +00:00
|
|
|
g_print ("Node file: '%s'\n", node_file);
|
|
|
|
g_print ("PNG file: '%s'\n", png_file);
|
2017-12-25 11:56:21 +00:00
|
|
|
|
|
|
|
/* Load the render node from the given .node file */
|
|
|
|
{
|
|
|
|
GBytes *bytes;
|
|
|
|
gsize len;
|
|
|
|
char *contents;
|
|
|
|
|
|
|
|
if (!g_file_get_contents (node_file, &contents, &len, &error))
|
|
|
|
{
|
2019-03-19 02:57:04 +00:00
|
|
|
g_print ("Could not open node file: %s\n", error->message);
|
2017-12-25 11:56:21 +00:00
|
|
|
g_clear_error (&error);
|
2019-05-20 01:29:11 +00:00
|
|
|
return 1;
|
2017-12-25 11:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes = g_bytes_new_take (contents, len);
|
2019-05-20 01:29:11 +00:00
|
|
|
node = gsk_render_node_deserialize (bytes, deserialize_error_func, &success);
|
2017-12-25 11:56:21 +00:00
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
2019-02-23 06:57:12 +00:00
|
|
|
g_assert_no_error (error);
|
2021-04-12 19:30:29 +00:00
|
|
|
g_assert_nonnull (node);
|
2017-12-25 11:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Render the .node file and download to cairo surface */
|
2021-09-15 20:13:57 +00:00
|
|
|
rendered_texture = gsk_renderer_render_texture (renderer, node, NULL);
|
|
|
|
g_assert_nonnull (rendered_texture);
|
2017-12-25 11:56:21 +00:00
|
|
|
|
|
|
|
/* Load the given reference png file */
|
2021-09-15 20:13:57 +00:00
|
|
|
reference_texture = gdk_texture_new_from_filename (png_file, &error);
|
|
|
|
if (reference_texture == NULL)
|
2019-05-20 01:29:11 +00:00
|
|
|
{
|
2021-09-15 20:13:57 +00:00
|
|
|
g_print ("Error loading reference surface: %s\n", error->message);
|
|
|
|
g_clear_error (&error);
|
2019-05-20 01:29:11 +00:00
|
|
|
success = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-15 20:13:57 +00:00
|
|
|
GdkTexture *diff_texture;
|
|
|
|
|
2019-05-20 01:29:11 +00:00
|
|
|
/* Now compare the two */
|
2021-09-15 20:13:57 +00:00
|
|
|
diff_texture = reftest_compare_textures (rendered_texture, reference_texture);
|
2019-05-20 01:29:11 +00:00
|
|
|
|
2021-09-15 20:13:57 +00:00
|
|
|
if (diff_texture)
|
2019-05-20 01:29:11 +00:00
|
|
|
{
|
2021-09-15 20:13:57 +00:00
|
|
|
save_image (diff_texture, node_file, ".diff.png");
|
|
|
|
g_object_unref (diff_texture);
|
2019-05-20 01:29:11 +00:00
|
|
|
success = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2017-12-25 11:56:21 +00:00
|
|
|
|
2021-09-15 20:13:57 +00:00
|
|
|
save_image (rendered_texture, node_file, ".out.png");
|
2017-12-25 11:56:21 +00:00
|
|
|
|
2021-09-15 20:13:57 +00:00
|
|
|
g_object_unref (reference_texture);
|
|
|
|
g_object_unref (rendered_texture);
|
2017-12-25 11:56:21 +00:00
|
|
|
|
2020-05-24 15:14:18 +00:00
|
|
|
gsk_render_node_unref (node);
|
|
|
|
|
2019-05-20 01:29:11 +00:00
|
|
|
return success ? 0 : 1;
|
2017-12-25 11:56:21 +00:00
|
|
|
}
|