From 3eed61deba9d5ab488f3b9a0bd22b822d829156d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= Date: Thu, 15 Jul 2021 16:50:32 +0200 Subject: [PATCH 1/2] snapshot: Replace trivial gradients with color nodes Extend this to all existing gradient types --- gtk/gtksnapshot.c | 127 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 94 insertions(+), 33 deletions(-) diff --git a/gtk/gtksnapshot.c b/gtk/gtksnapshot.c index b0b1222c66..827d4b80e9 100644 --- a/gtk/gtksnapshot.c +++ b/gtk/gtksnapshot.c @@ -2223,9 +2223,9 @@ gtk_snapshot_append_repeating_linear_gradient (GtkSnapshot *snapshot, { GskRenderNode *node; graphene_rect_t real_bounds; - graphene_point_t real_start_point; - graphene_point_t real_end_point; float scale_x, scale_y, dx, dy; + gboolean need_gradient = FALSE; + const GdkRGBA *first_color; g_return_if_fail (snapshot != NULL); g_return_if_fail (start_point != NULL); @@ -2235,16 +2235,36 @@ gtk_snapshot_append_repeating_linear_gradient (GtkSnapshot *snapshot, gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &dx, &dy); gtk_graphene_rect_scale_affine (bounds, scale_x, scale_y, dx, dy, &real_bounds); - real_start_point.x = scale_x * start_point->x + dx; - real_start_point.y = scale_y * start_point->y + dy; - real_end_point.x = scale_x * end_point->x + dx; - real_end_point.y = scale_y * end_point->y + dy; - node = gsk_repeating_linear_gradient_node_new (&real_bounds, - &real_start_point, - &real_end_point, - stops, - n_stops); + first_color = &stops[0].color; + for (gsize i = 0; i < n_stops; i ++) + { + if (!gdk_rgba_equal (first_color, &stops[i].color)) + { + need_gradient = TRUE; + break; + } + } + + if (need_gradient) + { + graphene_point_t real_start_point, real_end_point; + + real_start_point.x = scale_x * start_point->x + dx; + real_start_point.y = scale_y * start_point->y + dy; + real_end_point.x = scale_x * end_point->x + dx; + real_end_point.y = scale_y * end_point->y + dy; + + node = gsk_repeating_linear_gradient_node_new (&real_bounds, + &real_start_point, + &real_end_point, + stops, + n_stops); + } + else + { + node = gsk_color_node_new (first_color, &real_bounds); + } gtk_snapshot_append_node_internal (snapshot, node); } @@ -2338,8 +2358,9 @@ gtk_snapshot_append_radial_gradient (GtkSnapshot *snapshot, { GskRenderNode *node; graphene_rect_t real_bounds; - graphene_point_t real_center; float scale_x, scale_y, dx, dy; + gboolean need_gradient = FALSE; + const GdkRGBA *first_color; g_return_if_fail (snapshot != NULL); g_return_if_fail (center != NULL); @@ -2348,17 +2369,37 @@ gtk_snapshot_append_radial_gradient (GtkSnapshot *snapshot, gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &dx, &dy); gtk_graphene_rect_scale_affine (bounds, scale_x, scale_y, dx, dy, &real_bounds); - real_center.x = scale_x * center->x + dx; - real_center.y = scale_y * center->y + dy; - node = gsk_radial_gradient_node_new (&real_bounds, - &real_center, - hradius * scale_x, - vradius * scale_y, - start, - end, - stops, - n_stops); + first_color = &stops[0].color; + for (gsize i = 0; i < n_stops; i ++) + { + if (!gdk_rgba_equal (first_color, &stops[i].color)) + { + need_gradient = TRUE; + break; + } + } + + if (need_gradient) + { + graphene_point_t real_center; + + real_center.x = scale_x * center->x + dx; + real_center.y = scale_y * center->y + dy; + + node = gsk_radial_gradient_node_new (&real_bounds, + &real_center, + hradius * scale_x, + vradius * scale_y, + start, + end, + stops, + n_stops); + } + else + { + node = gsk_color_node_new (first_color, &real_bounds); + } gtk_snapshot_append_node_internal (snapshot, node); } @@ -2391,8 +2432,9 @@ gtk_snapshot_append_repeating_radial_gradient (GtkSnapshot *snapshot, { GskRenderNode *node; graphene_rect_t real_bounds; - graphene_point_t real_center; float scale_x, scale_y, dx, dy; + gboolean need_gradient = FALSE; + const GdkRGBA *first_color; g_return_if_fail (snapshot != NULL); g_return_if_fail (center != NULL); @@ -2401,17 +2443,36 @@ gtk_snapshot_append_repeating_radial_gradient (GtkSnapshot *snapshot, gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &dx, &dy); gtk_graphene_rect_scale_affine (bounds, scale_x, scale_y, dx, dy, &real_bounds); - real_center.x = scale_x * center->x + dx; - real_center.y = scale_y * center->y + dy; - node = gsk_repeating_radial_gradient_node_new (&real_bounds, - &real_center, - hradius * scale_x, - vradius * scale_y, - start, - end, - stops, - n_stops); + first_color = &stops[0].color; + for (gsize i = 0; i < n_stops; i ++) + { + if (!gdk_rgba_equal (first_color, &stops[i].color)) + { + need_gradient = TRUE; + break; + } + } + + if (need_gradient) + { + graphene_point_t real_center; + + real_center.x = scale_x * center->x + dx; + real_center.y = scale_y * center->y + dy; + node = gsk_repeating_radial_gradient_node_new (&real_bounds, + &real_center, + hradius * scale_x, + vradius * scale_y, + start, + end, + stops, + n_stops); + } + else + { + node = gsk_color_node_new (first_color, &real_bounds); + } gtk_snapshot_append_node_internal (snapshot, node); } From 433233258bf1c04ab3e717dd097e3953f1d3ff84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= Date: Thu, 15 Jul 2021 16:52:38 +0200 Subject: [PATCH 2/2] snapshot: Only compute start and end point if we have to If the linear gradient results in a color node, we don't need the start and end point. Only declare and compute it if we need to. --- gtk/gtksnapshot.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/gtk/gtksnapshot.c b/gtk/gtksnapshot.c index 827d4b80e9..af8608b682 100644 --- a/gtk/gtksnapshot.c +++ b/gtk/gtksnapshot.c @@ -2159,12 +2159,9 @@ gtk_snapshot_append_linear_gradient (GtkSnapshot *snapshot, { GskRenderNode *node; graphene_rect_t real_bounds; - graphene_point_t real_start_point; - graphene_point_t real_end_point; float scale_x, scale_y, dx, dy; const GdkRGBA *first_color; gboolean need_gradient = FALSE; - int i; g_return_if_fail (snapshot != NULL); g_return_if_fail (start_point != NULL); @@ -2174,13 +2171,9 @@ gtk_snapshot_append_linear_gradient (GtkSnapshot *snapshot, gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &dx, &dy); gtk_graphene_rect_scale_affine (bounds, scale_x, scale_y, dx, dy, &real_bounds); - real_start_point.x = scale_x * start_point->x + dx; - real_start_point.y = scale_y * start_point->y + dy; - real_end_point.x = scale_x * end_point->x + dx; - real_end_point.y = scale_y * end_point->y + dy; first_color = &stops[0].color; - for (i = 0; i < n_stops; i ++) + for (gsize i = 0; i < n_stops; i ++) { if (!gdk_rgba_equal (first_color, &stops[i].color)) { @@ -2190,13 +2183,24 @@ gtk_snapshot_append_linear_gradient (GtkSnapshot *snapshot, } if (need_gradient) - node = gsk_linear_gradient_node_new (&real_bounds, - &real_start_point, - &real_end_point, - stops, - n_stops); + { + graphene_point_t real_start_point, real_end_point; + + real_start_point.x = scale_x * start_point->x + dx; + real_start_point.y = scale_y * start_point->y + dy; + real_end_point.x = scale_x * end_point->x + dx; + real_end_point.y = scale_y * end_point->y + dy; + + node = gsk_linear_gradient_node_new (&real_bounds, + &real_start_point, + &real_end_point, + stops, + n_stops); + } else - node = gsk_color_node_new (first_color, &real_bounds); + { + node = gsk_color_node_new (first_color, &real_bounds); + } gtk_snapshot_append_node_internal (snapshot, node); }