diff options
author | Benjamin Otte <otte@redhat.com> | 2012-10-02 14:11:18 +0200 |
---|---|---|
committer | Benjamin Otte <otte@redhat.com> | 2012-10-02 14:16:37 +0200 |
commit | 4f6a55d689b9d77fcaf661d6f107dee54d99f7ef (patch) | |
tree | 3e759c39a9cae345f8c62663b2a1aef9a1da56db /gtk/gtkcssimagegradient.c | |
parent | 046d004725cf120e9c9d7524def123760019a812 (diff) | |
download | gtk+-4f6a55d689b9d77fcaf661d6f107dee54d99f7ef.tar.gz |
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
Diffstat (limited to 'gtk/gtkcssimagegradient.c')
-rw-r--r-- | gtk/gtkcssimagegradient.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/gtk/gtkcssimagegradient.c b/gtk/gtkcssimagegradient.c index 30bfab35e9..4085719585 100644 --- a/gtk/gtkcssimagegradient.c +++ b/gtk/gtkcssimagegradient.c @@ -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; |