cssimage: Implement proper cross-fades for gradients

This is intended mainly to speed up the current situation with spinners
on debug kernels. Because we now don't use a cross-fade to draw the
transition but instead have a real gradient that we draw, we don't need
to use the slow cross-fade code.

https://bugzilla.gnome.org/show_bug.cgi?id=684639
This commit is contained in:
Benjamin Otte 2012-10-02 14:11:18 +02:00
parent 046d004725
commit 4f6a55d689
4 changed files with 227 additions and 1 deletions

View File

@ -48,6 +48,136 @@ gtk_css_image_gradient_compute (GtkCssImage *image,
return GTK_CSS_IMAGE (copy);
}
cairo_pattern_t *
fade_pattern (cairo_pattern_t *pattern,
double opacity)
{
double x0, y0, x1, y1, r0, r1;
cairo_pattern_t *result;
int i, n;
switch (cairo_pattern_get_type (pattern))
{
case CAIRO_PATTERN_TYPE_LINEAR:
cairo_pattern_get_linear_points (pattern, &x0, &y0, &x1, &y1);
result = cairo_pattern_create_linear (x0, y0, x1, y1);
break;
case CAIRO_PATTERN_TYPE_RADIAL:
cairo_pattern_get_radial_circles (pattern, &x0, &y0, &r0, &x1, &y1, &r1);
result = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
break;
default:
g_return_val_if_reached (NULL);
}
cairo_pattern_get_color_stop_count (pattern, &n);
for (i = 0; i < n; i++)
{
double o, r, g, b, a;
cairo_pattern_get_color_stop_rgba (pattern, i, &o, &r, &g, &b, &a);
cairo_pattern_add_color_stop_rgba (result, o, r, g, b, a * opacity);
}
return pattern;
}
cairo_pattern_t *
transition_pattern (cairo_pattern_t *start,
cairo_pattern_t *end,
double progress)
{
double sx0, sy0, sx1, sy1, sr0, sr1, ex0, ey0, ex1, ey1, er0, er1;
cairo_pattern_t *result;
int i, n;
progress = CLAMP (progress, 0.0, 1.0);
if (end == NULL)
return fade_pattern (start, 1.0 - progress);
g_assert (cairo_pattern_get_type (start) == cairo_pattern_get_type (end));
switch (cairo_pattern_get_type (start))
{
case CAIRO_PATTERN_TYPE_LINEAR:
cairo_pattern_get_linear_points (start, &sx0, &sy0, &sx1, &sy1);
cairo_pattern_get_linear_points (start, &ex0, &ey0, &ex1, &ey1);
result = cairo_pattern_create_linear ((1 - progress) * sx0 + progress * ex0,
(1 - progress) * sx1 + progress * ex1,
(1 - progress) * sy0 + progress * ey0,
(1 - progress) * sy1 + progress * ey1);
break;
case CAIRO_PATTERN_TYPE_RADIAL:
cairo_pattern_get_radial_circles (start, &sx0, &sy0, &sr0, &sx1, &sy1, &sr1);
cairo_pattern_get_radial_circles (start, &ex0, &ey0, &er0, &ex1, &ey1, &er1);
result = cairo_pattern_create_radial ((1 - progress) * sx0 + progress * ex0,
(1 - progress) * sy0 + progress * ey0,
(1 - progress) * sr0 + progress * er0,
(1 - progress) * sx1 + progress * ex1,
(1 - progress) * sy1 + progress * ey1,
(1 - progress) * sr1 + progress * er1);
break;
default:
g_return_val_if_reached (NULL);
}
cairo_pattern_get_color_stop_count (start, &n);
for (i = 0; i < n; i++)
{
double so, sr, sg, sb, sa, eo, er, eg, eb, ea;
cairo_pattern_get_color_stop_rgba (start, i, &so, &sr, &sg, &sb, &sa);
cairo_pattern_get_color_stop_rgba (start, i, &eo, &er, &eg, &eb, &ea);
cairo_pattern_add_color_stop_rgba (result,
(1 - progress) * so + progress * eo,
(1 - progress) * sr + progress * er,
(1 - progress) * sg + progress * eg,
(1 - progress) * sb + progress * eb,
(1 - progress) * sa + progress * ea);
}
return result;
}
GtkCssImage *
gtk_css_image_gradient_transition (GtkCssImage *start_image,
GtkCssImage *end_image,
guint property_id,
double progress)
{
GtkGradient *start_gradient, *end_gradient, *gradient;
cairo_pattern_t *start_pattern, *end_pattern;
GtkCssImageGradient *result;
start_gradient = GTK_CSS_IMAGE_GRADIENT (start_image)->gradient;
start_pattern = GTK_CSS_IMAGE_GRADIENT (start_image)->pattern;
if (end_image == NULL)
{
end_gradient = NULL;
end_pattern = NULL;
}
else
{
if (!GTK_IS_CSS_IMAGE_GRADIENT (end_image))
return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);
end_gradient = GTK_CSS_IMAGE_GRADIENT (end_image)->gradient;
end_pattern = GTK_CSS_IMAGE_GRADIENT (end_image)->pattern;
}
gradient = _gtk_gradient_transition (start_gradient, end_gradient, property_id, progress);
if (gradient == NULL)
return GTK_CSS_IMAGE_CLASS (_gtk_css_image_gradient_parent_class)->transition (start_image, end_image, property_id, progress);
result = g_object_new (GTK_TYPE_CSS_IMAGE_GRADIENT, NULL);
result->gradient = gradient;
result->pattern = transition_pattern (start_pattern, end_pattern, progress);
return GTK_CSS_IMAGE (result);
}
static gboolean
gtk_css_image_gradient_draw_circle (GtkCssImageGradient *image,
cairo_t *cr,
@ -169,6 +299,7 @@ _gtk_css_image_gradient_class_init (GtkCssImageGradientClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass);
image_class->compute = gtk_css_image_gradient_compute;
image_class->transition = gtk_css_image_gradient_transition;
image_class->draw = gtk_css_image_gradient_draw;
image_class->parse = gtk_css_image_gradient_parse;
image_class->print = gtk_css_image_gradient_print;

View File

@ -440,3 +440,91 @@ gtk_gradient_to_string (GtkGradient *gradient)
return g_string_free (str, FALSE);
}
static GtkGradient *
gtk_gradient_fade (GtkGradient *gradient,
double opacity)
{
GtkGradient *faded;
guint i;
faded = g_slice_new (GtkGradient);
faded->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
faded->x0 = gradient->x0;
faded->y0 = gradient->y0;
faded->x1 = gradient->x1;
faded->y1 = gradient->y1;
faded->radius0 = gradient->radius0;
faded->radius1 = gradient->radius1;
faded->ref_count = 1;
for (i = 0; i < gradient->stops->len; i++)
{
GtkSymbolicColor *color;
ColorStop *stop;
stop = &g_array_index (gradient->stops, ColorStop, i);
color = gtk_symbolic_color_new_alpha (stop->color, opacity);
gtk_gradient_add_color_stop (gradient, stop->offset, color);
gtk_symbolic_color_unref (color);
}
return faded;
}
GtkGradient *
_gtk_gradient_transition (GtkGradient *start,
GtkGradient *end,
guint property_id,
double progress)
{
GtkGradient *gradient;
guint i;
g_return_val_if_fail (start != NULL, NULL);
if (end == NULL)
return gtk_gradient_fade (start, 1.0 - CLAMP (progress, 0.0, 1.0));
if (start->stops->len != end->stops->len)
return NULL;
/* check both are radial/linear */
if ((start->radius0 == 0 && start->radius1 == 0) != (end->radius0 == 0 && end->radius1 == 0))
return NULL;
gradient = g_slice_new (GtkGradient);
gradient->stops = g_array_new (FALSE, FALSE, sizeof (ColorStop));
gradient->x0 = (1 - progress) * start->x0 + progress * end->x0;
gradient->y0 = (1 - progress) * start->y0 + progress * end->y0;
gradient->x1 = (1 - progress) * start->x1 + progress * end->x1;
gradient->y1 = (1 - progress) * start->y1 + progress * end->y1;
gradient->radius0 = (1 - progress) * start->radius0 + progress * end->radius0;
gradient->radius1 = (1 - progress) * start->radius1 + progress * end->radius1;
gradient->ref_count = 1;
for (i = 0; i < start->stops->len; i++)
{
ColorStop *start_stop, *end_stop;
GtkSymbolicColor *color;
double offset;
start_stop = &g_array_index (start->stops, ColorStop, i);
end_stop = &g_array_index (end->stops, ColorStop, i);
offset = (1 - progress) * start_stop->offset + progress * end_stop->offset;
color = (GtkSymbolicColor *) _gtk_css_value_transition ((GtkCssValue *) start_stop->color,
(GtkCssValue *) end_stop->color,
property_id,
progress);
g_assert (color);
gtk_gradient_add_color_stop (gradient, offset, color);
gtk_symbolic_color_unref (color);
}
return gradient;
}

View File

@ -29,6 +29,11 @@ cairo_pattern_t * _gtk_gradient_resolve_full (GtkGradient
GtkCssComputedValues *parent_values,
GtkCssDependencies *dependencies);
GtkGradient * _gtk_gradient_transition (GtkGradient *start,
GtkGradient *end,
guint property_id,
double progress);
G_END_DECLS
#endif /* __GTK_STYLE_PROPERTIES_PRIVATE_H__ */

View File

@ -249,7 +249,9 @@ gtk_css_value_symbolic_transition (GtkCssValue *start,
guint property_id,
double progress)
{
return NULL;
return (GtkCssValue *) gtk_symbolic_color_new_mix ((GtkSymbolicColor *) start,
(GtkSymbolicColor *) end,
progress);
}
static void