gtk2/gtk/gtkcsscomputedvalues.c
Benjamin Otte aaedc7b67f stylecontext: Compute if things changed before invalidating
This is only a small performance boost by itself, but it's necessary
for animations, so we need it.

Benchmark numbers for my Glade benchmark for interested people:
        GTK 3.4.0    last commit  this commit
Raleigh
  real  0m41.879s    0m10.176s    0m9.900s
  user  0m41.394s    0m9.895s     0m9.628s
  sys   0m0.111s     0m0.096s     0m0.102s

Adwaita                                        (*)
  real  0m51.049s    0m13.432s    0m14.848s    0m12.253s
  user  0m50.487s    0m13.034s    0m13.218s    0m11.927s
  sys   0m0.117s     0m0.151s     0m0.147s     0m0.107s

Ambiance (patched to not use private GTK APIs)
  real  0m52.167s    0m13.115s    0m13.117s    0m12.944s
  user  0m51.576s    0m12.739s    0m12.768s    0m12.651s
  sys   0m0.119s     0m0.137s     0m0.136s     0m0.118s

(*) Adwaita and unico currently use custom properties, and
_gtk_css_value_compare() for custom properties always returns FALSE,
which makes this optimization never trigger. So I modified
_gtk_css_value_compare() to return TRUE for these properties instead and
reran the benchmark. Those are the numbers.
2012-04-17 08:59:21 +02:00

252 lines
7.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright © 2012 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Benjamin Otte <otte@gnome.org>
*/
#include "config.h"
#include "gtkcsscomputedvaluesprivate.h"
#include "gtkcssinheritvalueprivate.h"
#include "gtkcssinitialvalueprivate.h"
#include "gtkcssstylepropertyprivate.h"
G_DEFINE_TYPE (GtkCssComputedValues, _gtk_css_computed_values, G_TYPE_OBJECT)
static void
gtk_css_computed_values_dispose (GObject *object)
{
GtkCssComputedValues *values = GTK_CSS_COMPUTED_VALUES (object);
if (values->values)
{
g_ptr_array_unref (values->values);
values->values = NULL;
}
if (values->sections)
{
g_ptr_array_unref (values->sections);
values->sections = NULL;
}
G_OBJECT_CLASS (_gtk_css_computed_values_parent_class)->dispose (object);
}
static void
_gtk_css_computed_values_class_init (GtkCssComputedValuesClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = gtk_css_computed_values_dispose;
}
static void
_gtk_css_computed_values_init (GtkCssComputedValues *computed_values)
{
}
GtkCssComputedValues *
_gtk_css_computed_values_new (void)
{
return g_object_new (GTK_TYPE_CSS_COMPUTED_VALUES, NULL);
}
static void
maybe_unref_section (gpointer section)
{
if (section)
gtk_css_section_unref (section);
}
void
_gtk_css_computed_values_compute_value (GtkCssComputedValues *values,
GtkStyleContext *context,
guint id,
GtkCssValue *specified,
GtkCssSection *section)
{
GtkCssStyleProperty *prop;
GtkStyleContext *parent;
g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
prop = _gtk_css_style_property_lookup_by_id (id);
parent = gtk_style_context_get_parent (context);
if (values->values == NULL)
values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
if (id <= values->values->len)
g_ptr_array_set_size (values->values, id + 1);
/* http://www.w3.org/TR/css3-cascade/#cascade
* Then, for every element, the value for each property can be found
* by following this pseudo-algorithm:
* 1) Identify all declarations that apply to the element
*/
if (specified != NULL)
{
if (_gtk_css_value_is_inherit (specified))
{
/* 3) if the value of the winning declaration is inherit,
* the inherited value (see below) becomes the specified value.
*/
specified = NULL;
}
else if (_gtk_css_value_is_initial (specified))
{
/* if the value of the winning declaration is initial,
* the initial value (see below) becomes the specified value.
*/
specified = _gtk_css_style_property_get_initial_value (prop);
}
/* 2) If the cascading process (described below) yields a winning
* declaration and the value of the winning declaration is not
* initial or inherit, the value of the winning declaration
* becomes the specified value.
*/
}
else
{
if (_gtk_css_style_property_is_inherit (prop))
{
/* 4) if the property is inherited, the inherited value becomes
* the specified value.
*/
specified = NULL;
}
else
{
/* 5) Otherwise, the initial value becomes the specified value.
*/
specified = _gtk_css_style_property_get_initial_value (prop);
}
}
if (specified == NULL && parent == NULL)
{
/* If the inherit value is set on the root element, the property is
* assigned its initial value. */
specified = _gtk_css_style_property_get_initial_value (prop);
}
if (specified)
{
g_ptr_array_index (values->values, id) =
_gtk_css_style_property_compute_value (prop,
context,
specified);
}
else
{
GtkCssValue *parent_value;
/* Set NULL here and do the inheritance upon lookup? */
parent_value = _gtk_style_context_peek_property (parent, id);
g_ptr_array_index (values->values, id) = _gtk_css_value_ref (parent_value);
}
if (section)
{
if (values->sections == NULL)
values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
if (values->sections->len <= id)
g_ptr_array_set_size (values->sections, id + 1);
g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
}
}
void
_gtk_css_computed_values_set_value (GtkCssComputedValues *values,
guint id,
GtkCssValue *value,
GtkCssSection *section)
{
g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
if (values->values == NULL)
values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
if (id >= values->values->len)
g_ptr_array_set_size (values->values, id + 1);
if (g_ptr_array_index (values->values, id))
_gtk_css_value_unref (g_ptr_array_index (values->values, id));
g_ptr_array_index (values->values, id) = _gtk_css_value_ref (value);
if (section)
{
if (values->sections == NULL)
values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
if (values->sections->len <= id)
g_ptr_array_set_size (values->sections, id + 1);
g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
}
}
GtkCssValue *
_gtk_css_computed_values_get_value (GtkCssComputedValues *values,
guint id)
{
g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
if (values->values == NULL ||
id >= values->values->len)
return NULL;
return g_ptr_array_index (values->values, id);
}
GtkCssSection *
_gtk_css_computed_values_get_section (GtkCssComputedValues *values,
guint id)
{
g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
if (values->sections == NULL ||
id >= values->sections->len)
return NULL;
return g_ptr_array_index (values->sections, id);
}
GtkBitmask *
_gtk_css_computed_values_get_difference (GtkCssComputedValues *values,
GtkCssComputedValues *other)
{
GtkBitmask *result;
guint i, len;
len = MIN (values->values->len, other->values->len);
result = _gtk_bitmask_new ();
if (values->values->len != other->values->len)
result = _gtk_bitmask_invert_range (result, len, MAX (values->values->len, other->values->len));
for (i = 0; i < len; i++)
{
if (!_gtk_css_value_equal (g_ptr_array_index (values->values, i),
g_ptr_array_index (other->values, i)))
result = _gtk_bitmask_set (result, i, TRUE);
}
return result;
}