gtkcssnodestylecache.c 5.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GTK - The GIMP Toolkit
 * Copyright (C) 2015 Benjamin Otte <otte@gnome.org>
 *
 * 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 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/>.
 */

#include "config.h"

#include "gtkcssnodestylecacheprivate.h"

22
#include "gtkdebug.h"
23 24
#include "gtkcssstaticstyleprivate.h"

25 26 27 28 29 30
struct _GtkCssNodeStyleCache {
  guint        ref_count;
  GtkCssStyle *style;
  GHashTable  *children;
};

31 32 33 34
#define UNPACK_DECLARATION(packed) ((GtkCssNodeDeclaration *) (GPOINTER_TO_SIZE (packed) & ~0x3))
#define UNPACK_FLAGS(packed) (GPOINTER_TO_SIZE (packed) & 0x3)
#define PACK(decl, first_child, last_child) GSIZE_TO_POINTER (GPOINTER_TO_SIZE (decl) | ((first_child) ? 0x2 : 0) | ((last_child) ? 0x1 : 0))

35 36 37 38 39 40 41 42 43 44 45 46 47
GtkCssNodeStyleCache *
gtk_css_node_style_cache_new (GtkCssStyle *style)
{
  GtkCssNodeStyleCache *result;

  result = g_slice_new0 (GtkCssNodeStyleCache);

  result->ref_count = 1;
  result->style = g_object_ref (style);

  return result;
}

48 49 50 51 52 53 54 55
GtkCssNodeStyleCache *
gtk_css_node_style_cache_ref (GtkCssNodeStyleCache *cache)
{
  cache->ref_count++; 

  return cache;
}

56 57 58 59 60 61 62 63 64
void
gtk_css_node_style_cache_unref (GtkCssNodeStyleCache *cache)
{
  cache->ref_count--; 

  if (cache->ref_count > 0)
    return;

  g_object_unref (cache->style);
65 66 67 68
  if (cache->children)
    g_hash_table_unref (cache->children);

  g_slice_free (GtkCssNodeStyleCache, cache);
69 70 71 72 73 74 75 76
}

GtkCssStyle *
gtk_css_node_style_cache_get_style (GtkCssNodeStyleCache *cache)
{
  return cache->style;
}

77 78 79 80 81
static gboolean
may_be_stored_in_cache (GtkCssStyle *style)
{
  GtkCssChange change;

82 83 84 85 86 87 88 89 90 91 92 93
  /* If you run your application with
   *   GTK_DEBUG=no-css-cache
   * no caching will happen. This is slow (in particular
   * when animating), but useful for figuring out bugs.
   *
   * We achieve that by disallowing any inserts into caches here.
   */
#ifdef G_ENABLE_DEBUG
  if (GTK_DEBUG_CHECK (NO_CSS_CACHE))
    return FALSE;
#endif

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  if (!GTK_IS_CSS_STATIC_STYLE (style))
    return FALSE;

  change = gtk_css_static_style_get_change (GTK_CSS_STATIC_STYLE (style));

  /* The cache is shared between all children of the parent, so if a
   * style depends on a sibling it is not independant of the child.
   */
  if (change & GTK_CSS_CHANGE_ANY_SIBLING)
    return FALSE;

  /* Again, the cache is shared between all children of the parent.
   * If the position is relevant, no child has the same style.
   */
  if (change & (GTK_CSS_CHANGE_NTH_CHILD | GTK_CSS_CHANGE_NTH_LAST_CHILD))
    return FALSE;

  return TRUE;
}

static guint
gtk_css_node_style_cache_decl_hash (gconstpointer item)
{
  return gtk_css_node_declaration_hash (UNPACK_DECLARATION (item)) << 2
    | UNPACK_FLAGS (item);
}

static gboolean
gtk_css_node_style_cache_decl_equal (gconstpointer item1,
                                     gconstpointer item2)
{
  if (UNPACK_FLAGS (item1) != UNPACK_FLAGS (item2))
    return FALSE;

  return gtk_css_node_declaration_equal (UNPACK_DECLARATION (item1),
                                         UNPACK_DECLARATION (item2));
}

static void
gtk_css_node_style_cache_decl_free (gpointer item)
{
  gtk_css_node_declaration_unref (UNPACK_DECLARATION (item));
}

138 139 140 141 142 143 144
GtkCssNodeStyleCache *
gtk_css_node_style_cache_insert (GtkCssNodeStyleCache   *parent,
                                 GtkCssNodeDeclaration  *decl,
                                 gboolean                is_first,
                                 gboolean                is_last,
                                 GtkCssStyle            *style)
{
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  GtkCssNodeStyleCache *result;

  if (!may_be_stored_in_cache (style))
    return NULL;

  if (parent->children == NULL)
    parent->children = g_hash_table_new_full (gtk_css_node_style_cache_decl_hash,
                                              gtk_css_node_style_cache_decl_equal,
                                              gtk_css_node_style_cache_decl_free,
                                              (GDestroyNotify) gtk_css_node_style_cache_unref);

  result = gtk_css_node_style_cache_new (style);

  g_hash_table_insert (parent->children,
                       PACK (gtk_css_node_declaration_ref (decl), is_first, is_last),
                       gtk_css_node_style_cache_ref (result));

  return result;
163 164 165 166 167 168 169 170
}

GtkCssNodeStyleCache *
gtk_css_node_style_cache_lookup (GtkCssNodeStyleCache   *parent,
                                 GtkCssNodeDeclaration  *decl,
                                 gboolean                is_first,
                                 gboolean                is_last)
{
171 172 173 174 175 176 177 178 179 180
  GtkCssNodeStyleCache *result;

  if (parent->children == NULL)
    return NULL;

  result = g_hash_table_lookup (parent->children, PACK (decl, is_first, is_last));
  if (result == NULL)
    return NULL;

  return gtk_css_node_style_cache_ref (result);
181 182
}