gsk: Improve test coverage

Add more tests for gsk transforms, render node
and renderer apis.
This commit is contained in:
Matthias Clasen 2023-05-10 10:38:13 -04:00
parent 845f4b1536
commit dc3003bc4d
3 changed files with 324 additions and 11 deletions

View File

@ -300,8 +300,9 @@ main (int argc, char **argv)
GdkPixbuf *pixbuf, *pixbuf2;
node2 = gsk_transform_node_new (node, gsk_transform_scale (NULL, -1, 1));
rendered_texture = gsk_renderer_render_texture (renderer, node2, NULL);
save_node (node2, node_file, "-flipped.node");
rendered_texture = gsk_renderer_render_texture (renderer, node2, NULL);
save_image (rendered_texture, node_file, "-flipped.out.png");
pixbuf = gdk_pixbuf_new_from_file (png_file, &error);
@ -316,7 +317,6 @@ main (int argc, char **argv)
if (diff_texture)
{
save_node (node2, node_file, "-flipped.node");
save_image (diff_texture, node_file, "-flipped.diff.png");
g_object_unref (diff_texture);
success = FALSE;
@ -357,6 +357,8 @@ main (int argc, char **argv)
offset_y += node_bounds.size.height;
node2 = gsk_repeat_node_new (&bounds, node, &node_bounds);
save_node (node2, node_file, "-repeated.node");
rendered_texture = gsk_renderer_render_texture (renderer, node2, NULL);
save_image (rendered_texture, node_file, "-repeated.out.png");
@ -388,7 +390,6 @@ main (int argc, char **argv)
if (diff_texture)
{
save_node (node2, node_file, "-repeated.node");
save_image (diff_texture, node_file, "-repeated.diff.png");
g_object_unref (diff_texture);
success = FALSE;
@ -405,8 +406,9 @@ main (int argc, char **argv)
GdkPixbuf *pixbuf, *pixbuf2;
node2 = gsk_transform_node_new (node, gsk_transform_rotate (NULL, 90));
rendered_texture = gsk_renderer_render_texture (renderer, node2, NULL);
save_node (node2, node_file, "-rotated.node");
rendered_texture = gsk_renderer_render_texture (renderer, node2, NULL);
save_image (rendered_texture, node_file, "-rotated.out.png");
pixbuf = gdk_pixbuf_new_from_file (png_file, &error);
@ -421,7 +423,6 @@ main (int argc, char **argv)
if (diff_texture)
{
save_node (node2, node_file, "-rotated.node");
save_image (diff_texture, node_file, "-rotated.diff.png");
g_object_unref (diff_texture);
success = FALSE;
@ -451,8 +452,9 @@ main (int argc, char **argv)
gsk_render_node_unref (mask_node);
gsk_render_node_unref (nodes[0]);
gsk_render_node_unref (nodes[1]);
rendered_texture = gsk_renderer_render_texture (renderer, node2, NULL);
save_node (node2, node_file, "-masked.node");
rendered_texture = gsk_renderer_render_texture (renderer, node2, NULL);
save_image (rendered_texture, node_file, "-masked.out.png");
pixbuf = gdk_pixbuf_new_from_file (png_file, &error);
@ -467,7 +469,6 @@ main (int argc, char **argv)
if (diff_texture)
{
save_node (node2, node_file, "-masked.node");
save_image (diff_texture, node_file, "-masked.diff.png");
g_object_unref (diff_texture);
success = FALSE;

View File

@ -1,10 +1,17 @@
#include <gtk/gtk.h>
#include "gsk/gskrendernodeprivate.h"
#ifdef GDK_RENDERING_GL
#include <gsk/gl/gskglrenderer.h>
#endif
#include <gobject/gvaluecollector.h>
static void
test_rendernode_gvalue (void)
{
GValue value = G_VALUE_INIT;
GValue value2 = G_VALUE_INIT;
GskRenderNode *node, *node2;
g_assert_false (GSK_VALUE_HOLDS_RENDER_NODE (&value));
@ -23,7 +30,45 @@ test_rendernode_gvalue (void)
g_value_reset (&value);
gsk_value_take_render_node (&value, node);
g_value_init (&value2, GSK_TYPE_RENDER_NODE);
g_value_copy (&value, &value2);
g_assert_true (gsk_value_get_render_node (&value2) == node);
gsk_value_set_render_node (&value, NULL);
gsk_value_take_render_node (&value2, NULL);
g_value_unset (&value);
g_value_unset (&value2);
}
static void
test_collect_varargs (GskRenderNode *node, ...)
{
va_list ap;
char *err = NULL;
GValue value = G_VALUE_INIT;
g_value_init (&value, GSK_TYPE_RENDER_NODE);
va_start (ap, node);
G_VALUE_COLLECT (&value, ap, 0, &err);
va_end (ap);
g_assert_true (gsk_value_get_render_node (&value) == node);
g_value_unset (&value);
}
static void
test_rendernode_varargs (void)
{
GskRenderNode *node;
node = gsk_color_node_new (&(GdkRGBA){0,1,1,1}, &GRAPHENE_RECT_INIT (0, 0, 50, 50));
test_collect_varargs (node, node, NULL);
gsk_render_node_unref (node);
}
static void
@ -82,6 +127,151 @@ test_conic_gradient_angle (void)
gsk_render_node_unref (node);
}
static void
test_container_disjoint (void)
{
GskRenderNode *node, *nodes[2];
nodes[0] = gsk_color_node_new (&(GdkRGBA){0,1,1,1}, &GRAPHENE_RECT_INIT (0, 0, 50, 50));
nodes[1] = gsk_color_node_new (&(GdkRGBA){0,1,1,1}, &GRAPHENE_RECT_INIT (50, 0, 50, 50));
node = gsk_container_node_new (nodes, 2);
g_assert_true (gsk_container_node_is_disjoint (node));
gsk_render_node_unref (node);
gsk_render_node_unref (nodes[0]);
gsk_render_node_unref (nodes[1]);
nodes[0] = gsk_color_node_new (&(GdkRGBA){0,1,1,1}, &GRAPHENE_RECT_INIT (0, 0, 50, 50));
nodes[1] = gsk_color_node_new (&(GdkRGBA){0,1,1,1}, &GRAPHENE_RECT_INIT (25, 0, 50, 50));
node = gsk_container_node_new (nodes, 2);
g_assert_false (gsk_container_node_is_disjoint (node));
gsk_render_node_unref (node);
gsk_render_node_unref (nodes[0]);
gsk_render_node_unref (nodes[1]);
}
const char shader1[] =
"uniform float progress;\n"
"uniform sampler2D u_texture1;\n"
"uniform sampler2D u_texture2;\n"
""
"vec4 getFromColor (vec2 uv) {\n"
" return GskTexture(u_texture1, uv);\n"
"}\n"
"\n"
"vec4 getToColor (vec2 uv) {\n"
" return GskTexture(u_texture2, uv);\n"
"}\n"
"\n"
"// author: bobylito\n"
"// license: MIT\n"
"const float SQRT_2 = 1.414213562373;\n"
"uniform float dots;// = 20.0;\n"
"uniform vec2 center; //= vec2(0, 0);\n"
"\n"
"uniform int test1 = -2;\n"
"uniform uint test2 = 2u; \n"
"uniform bool test3;\n"
"uniform vec3 test4;\n"
"uniform vec4 test5;\n"
"\n"
"vec4 transition(vec2 uv) {\n"
" bool nextImage = distance(fract(uv * dots), vec2(0.5, 0.5)) < ( progress / distance(uv, center));\n"
" return nextImage ? getToColor(uv) : getFromColor(uv);\n"
"}\n"
"\n"
"void mainImage(out vec4 fragColor, in vec2 fragCoord, in vec2 resolution, in vec2 uv)\n"
"{\n"
" fragColor = transition(uv);\n"
"}\n";
static void
test_renderer (GskRenderer *renderer)
{
GdkDisplay *display;
gboolean realized;
GdkSurface *surface;
gboolean res;
GError *error = NULL;
GskGLShader *shader;
GBytes *bytes;
g_assert (GSK_IS_RENDERER (renderer));
display = gdk_display_open (NULL);
g_object_get (renderer,
"realized", &realized,
"surface", &surface,
NULL);
g_assert_false (realized);
g_assert_null (surface);
g_assert_null (gsk_renderer_get_surface (renderer));
surface = gdk_surface_new_toplevel (display);
res = gsk_renderer_realize (renderer, surface, &error);
g_assert_true (res);
g_assert_true (gsk_renderer_is_realized (renderer));
g_assert_true (gsk_renderer_get_surface (renderer) == surface);
bytes = g_bytes_new_static (shader1, sizeof (shader1));
shader = gsk_gl_shader_new_from_bytes (bytes);
g_bytes_unref (bytes);
res = gsk_gl_shader_compile (shader, renderer, &error);
if (GSK_IS_GL_RENDERER (renderer))
{
g_assert_true (res);
g_assert_no_error (error);
}
else
{
g_assert_false (res);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
g_clear_error (&error);
}
gsk_renderer_unrealize (renderer);
g_assert_false (gsk_renderer_is_realized (renderer));
g_assert_null (gsk_renderer_get_surface (renderer));
gdk_surface_destroy (surface);
gdk_display_close (display);
}
static void
test_cairo_renderer (void)
{
GskRenderer *renderer;
renderer = gsk_cairo_renderer_new ();
test_renderer (renderer);
g_clear_object (&renderer);
}
static void
test_gl_renderer (void)
{
#ifdef GDK_RENDERING_GL
GskRenderer *renderer;
renderer = gsk_gl_renderer_new ();
test_renderer (renderer);
g_clear_object (&renderer);
#else
g_test_skip ("no GL support");
#endif
}
int
main (int argc, char *argv[])
{
@ -89,8 +279,12 @@ main (int argc, char *argv[])
gtk_init ();
g_test_add_func ("/rendernode/gvalue", test_rendernode_gvalue);
g_test_add_func ("/rendernode/varargs", test_rendernode_varargs);
g_test_add_func ("/rendernode/border/uniform", test_bordernode_uniform);
g_test_add_func ("/rendernode/conic-gradient/angle", test_conic_gradient_angle);
g_test_add_func ("/rendernode/container/disjoint", test_container_disjoint);
g_test_add_func ("/renderer/cairo", test_cairo_renderer);
g_test_add_func ("/renderer/gl", test_gl_renderer);
return g_test_run ();
}

View File

@ -406,6 +406,10 @@ test_print_parse (void)
}
}
}
ret = gsk_transform_parse ("rotate(45);", &parsed);
g_assert_false (ret);
g_assert_null (parsed);
}
static void
@ -735,16 +739,37 @@ test_to_2d_components (void)
gsk_transform_unref (transform);
gsk_transform_unref (transform2);
transform = gsk_transform_translate (NULL, &GRAPHENE_POINT_INIT (2, 3));
gsk_transform_to_2d_components (transform,
&skew_x, &skew_y,
&scale_x, &scale_y,
&angle,
&dx, &dy);
g_assert_cmpfloat_with_epsilon (skew_x, 0, 0.0001);
g_assert_cmpfloat_with_epsilon (skew_y, 0, 0.0001);
g_assert_cmpfloat_with_epsilon (scale_x, 1, 0.0001);
g_assert_cmpfloat_with_epsilon (scale_y, 1, 0.0001);
g_assert_cmpfloat_with_epsilon (angle, 0, 0.0001);
g_assert_cmpfloat_with_epsilon (dx, 2, 0.0001);
g_assert_cmpfloat_with_epsilon (dy, 3, 0.0001);
gsk_transform_unref (transform);
}
static void
test_transform_point (void)
{
GskTransform *t, *t2;
GskTransform *t, *t2, *t3;
graphene_point_t p;
t = gsk_transform_scale (gsk_transform_translate (NULL, &GRAPHENE_POINT_INIT (1, 2)), 2, 2);
t2 = gsk_transform_translate (gsk_transform_scale (NULL, 2, 2), &GRAPHENE_POINT_INIT (1, 2));
t3 = gsk_transform_rotate (NULL, 90);
gsk_transform_transform_point (NULL, &GRAPHENE_POINT_INIT (1,1), &p);
g_assert_true (graphene_point_equal (&p, &GRAPHENE_POINT_INIT (1, 1)));
gsk_transform_transform_point (t, &GRAPHENE_POINT_INIT (1,1), &p);
g_assert_true (graphene_point_equal (&p, &GRAPHENE_POINT_INIT (3, 4)));
@ -752,14 +777,44 @@ test_transform_point (void)
gsk_transform_transform_point (t2, &GRAPHENE_POINT_INIT (1,1), &p);
g_assert_true (graphene_point_equal (&p, &GRAPHENE_POINT_INIT (4, 6)));
gsk_transform_transform_point (t3, &GRAPHENE_POINT_INIT (2,1), &p);
g_assert_true (graphene_point_equal (&p, &GRAPHENE_POINT_INIT (-1, 2)));
gsk_transform_unref (t);
gsk_transform_unref (t2);
gsk_transform_unref (t3);
}
static void
test_scale_transform (void)
{
GskTransform *t1, *t2;
char *string;
gboolean res;
GskTransform *x;
t1 = gsk_transform_scale (NULL, 4, 9);
t2 = gsk_transform_scale (gsk_transform_scale (NULL, 2, 3), 2, 3);
g_assert_true (gsk_transform_get_category (t1) == GSK_TRANSFORM_CATEGORY_2D_AFFINE);
g_assert_true (gsk_transform_get_category (t2) == GSK_TRANSFORM_CATEGORY_2D_AFFINE);
g_assert_true (gsk_transform_equal (t1, t2));
string = gsk_transform_to_string (t1);
res = gsk_transform_parse (string, &x);
g_assert_true (res);
g_assert_true (gsk_transform_equal (t1, x));
gsk_transform_unref (t1);
gsk_transform_unref (t2);
g_free (string);
gsk_transform_unref (x);
}
static void
test_skew_transform (void)
{
GskTransform *t1, *t2, *t3;
GskTransform *t1, *t2, *t3, *t4;
char *string;
gboolean res;
GskTransform *x;
@ -767,6 +822,7 @@ test_skew_transform (void)
t1 = gsk_transform_skew (NULL, 30, 60);
t2 = gsk_transform_skew (NULL, 0, 30);
t3 = gsk_transform_skew (NULL, 0, -30);
t4 = gsk_transform_skew (NULL, 30, 0);
g_assert_true (gsk_transform_get_category (t1) == GSK_TRANSFORM_CATEGORY_2D);
g_assert_true (gsk_transform_get_category (t2) == GSK_TRANSFORM_CATEGORY_2D);
@ -779,12 +835,30 @@ test_skew_transform (void)
res = gsk_transform_parse (string, &x);
g_assert_true (res);
g_assert_true (gsk_transform_equal (t1, x));
g_free (string);
gsk_transform_unref (x);
string = gsk_transform_to_string (t3);
res = gsk_transform_parse (string, &x);
g_assert_true (res);
g_assert_true (gsk_transform_equal (t3, x));
g_free (string);
gsk_transform_unref (x);
string = gsk_transform_to_string (t4);
res = gsk_transform_parse (string, &x);
g_assert_true (res);
g_assert_true (gsk_transform_equal (t4, x));
g_free (string);
gsk_transform_unref (x);
t3 = gsk_transform_transform (t3, t4);
g_assert_true (gsk_transform_get_category (t3) == GSK_TRANSFORM_CATEGORY_2D);
gsk_transform_unref (t1);
gsk_transform_unref (t2);
gsk_transform_unref (t3);
g_free (string);
gsk_transform_unref (x);
gsk_transform_unref (t4);
}
static void
@ -811,6 +885,7 @@ static void
test_rotate_transform (void)
{
GskTransform *t1, *t2, *t3;
graphene_vec3_t vec;
t1 = gsk_transform_rotate (NULL, 60);
t2 = gsk_transform_rotate (NULL, 20);
@ -826,6 +901,11 @@ test_rotate_transform (void)
t3 = gsk_transform_rotate (NULL, -60);
g_assert_true (gsk_transform_equal (t1, t3));
gsk_transform_unref (t2);
t2 = gsk_transform_rotate_3d (NULL, -60, graphene_vec3_init (&vec, 0, 0, 1));
g_assert_true (gsk_transform_get_category (t2) == GSK_TRANSFORM_CATEGORY_2D);
g_assert_true (gsk_transform_equal (t1, t2));
gsk_transform_unref (t1);
gsk_transform_unref (t2);
gsk_transform_unref (t3);
@ -851,6 +931,42 @@ test_rotate3d_transform (void)
gsk_transform_unref (t2);
}
static void
test_matrix_transform (void)
{
GskTransform *t1, *t2;
float xx, yx, xy, yy, dx, dy;
graphene_matrix_t m;
if (!gsk_transform_parse ("matrix(2, 0, 0, 3, 1, -1)", &t1))
g_assert_not_reached ();
g_assert_true (gsk_transform_get_category (t1) == GSK_TRANSFORM_CATEGORY_2D);
gsk_transform_to_2d (t1, &xx, &yx, &xy, &yy, &dx, &dy);
g_assert_cmpfloat (xx, ==, 2.f);
g_assert_cmpfloat (yx, ==, 0.f);
g_assert_cmpfloat (xy, ==, 0.f);
g_assert_cmpfloat (yy, ==, 3.f);
g_assert_cmpfloat (dx, ==, 1.f);
g_assert_cmpfloat (dy, ==, -1.f);
graphene_matrix_init_from_float (&m,
(const float[]) { 2.f, 0.f, 0.f, 0.f,
0.f, 3.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
1., -1.f, 0.f, 1.f});
t2 = gsk_transform_matrix (NULL, &m);
g_assert_true (gsk_transform_equal (t1, t2));
t1 = gsk_transform_invert (t1);
t2 = gsk_transform_transform (t2, t1);
graphene_assert_fuzzy_transform_equal (t2, NULL, EPSILON);
gsk_transform_unref (t1);
gsk_transform_unref (t2);
}
int
main (int argc,
char *argv[])
@ -869,10 +985,12 @@ main (int argc,
g_test_add_func ("/transform/point", test_transform_point);
g_test_add_func ("/transform/to-2d", test_to_2d);
g_test_add_func ("/transform/to-2d-components", test_to_2d_components);
g_test_add_func ("/transform/scale", test_scale_transform);
g_test_add_func ("/transform/skew", test_skew_transform);
g_test_add_func ("/transform/perspective", test_perspective_transform);
g_test_add_func ("/transform/rotate", test_rotate_transform);
g_test_add_func ("/transform/rotate3d", test_rotate3d_transform);
g_test_add_func ("/transform/matrix", test_matrix_transform);
return g_test_run ();
}