]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkicontheme.c
filechooser: Oops, cancel a file operation that was missing
[~andy/gtk] / gtk / gtkicontheme.c
index 764320b982da8cef6397fcb3f2a5c6a85a1772ff..0520686206cdd467cbc034b9af24d4fcaf32c7b9 100644 (file)
@@ -24,6 +24,7 @@
 #endif
 #include <string.h>
 #include <stdlib.h>
+#include <math.h>
 #include <glib.h>
 #include <glib/gstdio.h>
 
@@ -160,9 +161,18 @@ typedef enum
   HAS_ICON_FILE = 1 << 3
 } IconSuffix;
 
+#define INFO_CACHE_LRU_SIZE 32
+#if 0
+#define DEBUG_CACHE(args) g_print args
+#else
+#define DEBUG_CACHE(args)
+#endif
 
 struct _GtkIconThemePrivate
 {
+  GHashTable *info_cache;
+  GList *info_cache_lru;
+
   gchar *current_theme;
   gchar *fallback_theme;
   gchar **search_path;
@@ -197,10 +207,31 @@ struct _GtkIconThemePrivate
   gulong reset_styles_idle;
 };
 
+typedef struct {
+  gchar **icon_names;
+  gint size;
+  GtkIconLookupFlags flags;
+} IconInfoKey;
+
+typedef struct _SymbolicPixbufCache SymbolicPixbufCache;
+
+struct _SymbolicPixbufCache {
+  GdkPixbuf *pixbuf;
+  GdkPixbuf *proxy_pixbuf;
+  GdkRGBA  fg;
+  GdkRGBA  success_color;
+  GdkRGBA  warning_color;
+  GdkRGBA  error_color;
+  SymbolicPixbufCache *next;
+};
+
 struct _GtkIconInfo
 {
   /* Information about the source
    */
+  IconInfoKey key;
+  GtkIconTheme *in_cache;
+
   gchar *filename;
   GFile *icon_file;
   GLoadableIcon *loadable;
@@ -231,9 +262,12 @@ struct _GtkIconInfo
    * the icon.
    */
   GdkPixbuf *pixbuf;
+  GdkPixbuf *proxy_pixbuf;
   GError *load_error;
   gdouble scale;
 
+  SymbolicPixbufCache *symbolic_pixbuf_cache;
+
   GtkRequisition *symbolic_pixbuf_size;
 };
 
@@ -330,6 +364,8 @@ static BuiltinIcon *find_builtin_icon (const gchar *icon_name,
                                       gint        size,
                                       gint        *min_difference_p,
                                       gboolean    *has_larger_p);
+static void remove_from_lru_cache (GtkIconTheme *icon_theme,
+                                  GtkIconInfo *icon_info);
 
 static guint signal_changed = 0;
 
@@ -339,6 +375,46 @@ static GHashTable *icon_theme_builtin_icons;
 GtkIconCache *_builtin_cache = NULL;
 static GList *builtin_dirs = NULL;
 
+static guint
+icon_info_key_hash (gconstpointer _key)
+{
+  const IconInfoKey *key = _key;
+  guint h = 0;
+  int i;
+  for (i = 0; key->icon_names[i] != NULL; i++)
+    h ^= g_str_hash (key->icon_names[i]);
+
+  h ^= key->size * 0x10001;
+  h ^= key->flags * 0x1000010;
+
+  return h;
+}
+
+static gboolean
+icon_info_key_equal (gconstpointer  _a,
+                    gconstpointer  _b)
+{
+  const IconInfoKey *a = _a;
+  const IconInfoKey *b = _b;
+  int i;
+
+  if (a->size != b->size)
+    return FALSE;
+
+  if (a->flags != b->flags)
+    return FALSE;
+
+  for (i = 0;
+       a->icon_names[i] != NULL &&
+       b->icon_names[i] != NULL; i++)
+    {
+      if (strcmp (a->icon_names[i], b->icon_names[i]) != 0)
+       return FALSE;
+    }
+
+  return a->icon_names[i] == NULL && b->icon_names[i] == NULL;
+}
+
 G_DEFINE_TYPE (GtkIconTheme, gtk_icon_theme, G_TYPE_OBJECT)
 
 /**
@@ -641,6 +717,25 @@ pixbuf_supports_svg (void)
   return found_svg;
 }
 
+/* The icon info was removed from the icon_info_hash hash table */
+static void
+icon_info_uncached (GtkIconInfo *icon_info)
+{
+  GtkIconTheme *icon_theme = icon_info->in_cache;
+
+  DEBUG_CACHE (("removing %p (%s %d 0x%x) from cache (icon_them: %p)  (cache size %d)\n",
+               icon_info,
+               g_strjoinv (",", icon_info->key.icon_names),
+               icon_info->key.size, icon_info->key.flags,
+               icon_theme,
+               icon_theme != NULL ? g_hash_table_size (icon_theme->priv->info_cache) : 0));
+
+  icon_info->in_cache = NULL;
+
+  if (icon_theme != NULL)
+    remove_from_lru_cache (icon_theme, icon_info);
+}
+
 static void
 gtk_icon_theme_init (GtkIconTheme *icon_theme)
 {
@@ -653,6 +748,9 @@ gtk_icon_theme_init (GtkIconTheme *icon_theme)
                                       GtkIconThemePrivate);
   icon_theme->priv = priv;
 
+  priv->info_cache = g_hash_table_new_full (icon_info_key_hash, icon_info_key_equal, NULL,
+                                           (GDestroyNotify)icon_info_uncached);
+
   priv->custom_theme = FALSE;
 
   xdg_data_dirs = g_get_system_data_dirs ();
@@ -712,6 +810,8 @@ do_theme_change (GtkIconTheme *icon_theme)
 {
   GtkIconThemePrivate *priv = icon_theme->priv;
 
+  g_hash_table_remove_all (priv->info_cache);
+
   if (!priv->themes_valid)
     return;
   
@@ -755,6 +855,9 @@ gtk_icon_theme_finalize (GObject *object)
   icon_theme = GTK_ICON_THEME (object);
   priv = icon_theme->priv;
 
+  g_hash_table_destroy (priv->info_cache);
+  g_assert (priv->info_cache_lru == NULL);
+
   if (priv->reset_styles_idle)
     {
       g_source_remove (priv->reset_styles_idle);
@@ -1306,6 +1409,173 @@ ensure_valid_themes (GtkIconTheme *icon_theme)
   priv->loading_themes = FALSE;
 }
 
+/* The LRU cache is a short list of IconInfos that are kept
+   alive even though their IconInfo would otherwise have
+   been freed, so that we can avoid reloading these
+   constantly.
+   We put infos on the lru list when nothing otherwise
+   references the info. So, when we get a cache hit
+   we remove it from the list, and when the proxy
+   pixmap is released we put it on the list.
+*/
+
+static void
+ensure_lru_cache_space (GtkIconTheme *icon_theme)
+{
+  GtkIconThemePrivate *priv = icon_theme->priv;
+  GList *l;
+
+  /* Remove last item if LRU full */
+  l = g_list_nth (priv->info_cache_lru, INFO_CACHE_LRU_SIZE - 1);
+  if (l)
+    {
+      GtkIconInfo *icon_info = l->data;
+
+      DEBUG_CACHE (("removing (due to out of space) %p (%s %d 0x%x) from LRU cache (cache size %d)\n",
+                   icon_info,
+                   g_strjoinv (",", icon_info->key.icon_names),
+                   icon_info->key.size, icon_info->key.flags,
+                   g_list_length (priv->info_cache_lru)));
+
+      priv->info_cache_lru = g_list_delete_link (priv->info_cache_lru, l);
+      gtk_icon_info_free (icon_info);
+    }
+}
+
+static void
+add_to_lru_cache (GtkIconTheme *icon_theme,
+                 GtkIconInfo *icon_info)
+{
+  GtkIconThemePrivate *priv = icon_theme->priv;
+
+  DEBUG_CACHE (("adding  %p (%s %d 0x%x) to LRU cache (cache size %d)\n",
+               icon_info,
+               g_strjoinv (",", icon_info->key.icon_names),
+               icon_info->key.size, icon_info->key.flags,
+               g_list_length (priv->info_cache_lru)));
+
+  g_assert (g_list_find (priv->info_cache_lru, icon_info) == NULL);
+
+  ensure_lru_cache_space (icon_theme);
+  /* prepend new info to LRU */
+  priv->info_cache_lru = g_list_prepend (priv->info_cache_lru,
+                                        gtk_icon_info_copy (icon_info));
+}
+
+static void
+ensure_in_lru_cache (GtkIconTheme *icon_theme,
+                    GtkIconInfo *icon_info)
+{
+  GtkIconThemePrivate *priv = icon_theme->priv;
+  GList *l;
+
+  l = g_list_find (priv->info_cache_lru, icon_info);
+  if (l)
+    {
+      /* Move to front of LRU if already in it */
+      priv->info_cache_lru = g_list_remove_link (priv->info_cache_lru, l);
+      priv->info_cache_lru = g_list_concat (l, priv->info_cache_lru);
+    }
+  else
+    add_to_lru_cache (icon_theme, icon_info);
+}
+
+static void
+remove_from_lru_cache (GtkIconTheme *icon_theme,
+                      GtkIconInfo *icon_info)
+{
+  GtkIconThemePrivate *priv = icon_theme->priv;
+  if (g_list_find (priv->info_cache_lru, icon_info))
+    {
+      DEBUG_CACHE (("removing %p (%s %d 0x%x) from LRU cache (cache size %d)\n",
+                   icon_info,
+                   g_strjoinv (",", icon_info->key.icon_names),
+                   icon_info->key.size, icon_info->key.flags,
+                   g_list_length (priv->info_cache_lru)));
+
+      priv->info_cache_lru = g_list_remove (priv->info_cache_lru, icon_info);
+      gtk_icon_info_free (icon_info);
+    }
+}
+
+static SymbolicPixbufCache *
+symbolic_pixbuf_cache_new (GdkPixbuf *pixbuf,
+                          const GdkRGBA  *fg,
+                          const GdkRGBA  *success_color,
+                          const GdkRGBA  *warning_color,
+                          const GdkRGBA  *error_color,
+                          SymbolicPixbufCache *next)
+{
+  SymbolicPixbufCache *cache;
+
+  cache = g_new0 (SymbolicPixbufCache, 1);
+  cache->pixbuf = g_object_ref (pixbuf);
+  if (fg)
+    cache->fg = *fg;
+  if (success_color)
+    cache->success_color = *success_color;
+  if (warning_color)
+    cache->warning_color = *warning_color;
+  if (error_color)
+    cache->error_color = *error_color;
+  cache->next = next;
+  return cache;
+}
+
+static gboolean
+rgba_matches (const GdkRGBA  *a, const GdkRGBA  *b)
+{
+  GdkRGBA transparent = { 0 };
+
+  /* For matching we treat unset colors as transparent rather
+     than default, which works as well, because transparent
+     will never be used for real symbolic icon colors */
+  if (a == NULL)
+    a = &transparent;
+
+  return
+    fabs(a->red - b->red) < 0.0001 &&
+    fabs(a->green - b->green) < 0.0001 &&
+    fabs(a->blue - b->blue) < 0.0001 &&
+    fabs(a->alpha - b->alpha) < 0.0001;
+}
+
+static SymbolicPixbufCache *
+symbolic_pixbuf_cache_matches (SymbolicPixbufCache *cache,
+                              const GdkRGBA  *fg,
+                              const GdkRGBA  *success_color,
+                              const GdkRGBA  *warning_color,
+                              const GdkRGBA  *error_color)
+{
+  while (cache != NULL)
+    {
+      if (rgba_matches (fg, &cache->fg) &&
+         rgba_matches (success_color, &cache->success_color) &&
+         rgba_matches (warning_color, &cache->warning_color) &&
+         rgba_matches (error_color, &cache->error_color))
+       return cache;
+
+      cache = cache->next;
+    }
+
+  return NULL;
+}
+
+static void
+symbolic_pixbuf_cache_free (SymbolicPixbufCache *cache)
+{
+  SymbolicPixbufCache *next;
+
+  while (cache != NULL)
+    {
+      next = cache->next;
+      g_object_unref (cache->pixbuf);
+      g_free (cache);
+
+      cache = next;
+    }
+}
+
 static GtkIconInfo *
 choose_icon (GtkIconTheme       *icon_theme,
             const gchar        *icon_names[],
@@ -1319,9 +1589,31 @@ choose_icon (GtkIconTheme       *icon_theme,
   gboolean allow_svg;
   gboolean use_builtin;
   gint i;
+  IconInfoKey key;
 
   priv = icon_theme->priv;
 
+  ensure_valid_themes (icon_theme);
+
+  key.icon_names = (char **)icon_names;
+  key.size = size;
+  key.flags = flags;
+
+  icon_info = g_hash_table_lookup (priv->info_cache, &key);
+  if (icon_info != NULL)
+    {
+      DEBUG_CACHE (("cache hit %p (%s %d 0x%x) (cache size %d)\n",
+                   icon_info,
+                   g_strjoinv (",", icon_info->key.icon_names),
+                   icon_info->key.size, icon_info->key.flags,
+                   g_hash_table_size (priv->info_cache)));
+
+      icon_info = gtk_icon_info_copy (icon_info);
+      remove_from_lru_cache (icon_theme, icon_info);
+
+      return icon_info;
+    }
+
   if (flags & GTK_ICON_LOOKUP_NO_SVG)
     allow_svg = FALSE;
   else if (flags & GTK_ICON_LOOKUP_FORCE_SVG)
@@ -1330,8 +1622,6 @@ choose_icon (GtkIconTheme       *icon_theme,
     allow_svg = priv->pixbuf_supports_svg;
 
   use_builtin = flags & GTK_ICON_LOOKUP_USE_BUILTIN;
-  
-  ensure_valid_themes (icon_theme);
 
   /* for symbolic icons, do a search in all registered themes first;
    * a theme that inherits them from a parent theme might provide
@@ -1417,10 +1707,21 @@ choose_icon (GtkIconTheme       *icon_theme,
     }
 
  out:
-  if (icon_info) 
+  if (icon_info)
     {
       icon_info->desired_size = size;
       icon_info->forced_size = (flags & GTK_ICON_LOOKUP_FORCE_SIZE) != 0;
+
+      icon_info->key.icon_names = g_strdupv ((char **)icon_names);
+      icon_info->key.size = size;
+      icon_info->key.flags = flags;
+      icon_info->in_cache = icon_theme;
+      DEBUG_CACHE (("adding %p (%s %d 0x%x) to cache (cache size %d)\n",
+                   icon_info,
+                   g_strjoinv (",", icon_info->key.icon_names),
+                   icon_info->key.size, icon_info->key.flags,
+                   g_hash_table_size (priv->info_cache)));
+     g_hash_table_insert (priv->info_cache, &icon_info->key, icon_info);
     }
   else
     {
@@ -2735,7 +3036,12 @@ gtk_icon_info_free (GtkIconInfo *icon_info)
   icon_info->ref_count--;
   if (icon_info->ref_count > 0)
     return;
+
+  if (icon_info->in_cache)
+    g_hash_table_remove (icon_info->in_cache->priv->info_cache, &icon_info->key);
+
+  g_strfreev (icon_info->key.icon_names);
+
   g_free (icon_info->filename);
   g_clear_object (&icon_info->icon_file);
 
@@ -2749,6 +3055,8 @@ gtk_icon_info_free (GtkIconInfo *icon_info)
   if (icon_info->symbolic_pixbuf_size)
     gtk_requisition_free (icon_info->symbolic_pixbuf_size);
 
+  symbolic_pixbuf_cache_free (icon_info->symbolic_pixbuf_cache);
+
   g_slice_free (GtkIconInfo, icon_info);
 }
 
@@ -3095,6 +3403,22 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo  *icon_info,
   return TRUE;
 }
 
+static void
+proxy_pixbuf_destroy (guchar *pixels, gpointer data)
+{
+  GtkIconInfo *icon_info = data;
+  GtkIconTheme *icon_theme = icon_info->in_cache;
+
+  g_assert (icon_info->proxy_pixbuf != NULL);
+  icon_info->proxy_pixbuf = NULL;
+
+  /* Keep it alive a bit longer */
+  if (icon_theme != NULL)
+    ensure_in_lru_cache (icon_theme, icon_info);
+
+  gtk_icon_info_free (icon_info);
+}
+
 /**
  * gtk_icon_info_load_icon:
  * @icon_info: a #GtkIconInfo structure from gtk_icon_theme_lookup_icon()
@@ -3140,7 +3464,28 @@ gtk_icon_info_load_icon (GtkIconInfo *icon_info,
       return NULL;
     }
 
-  return g_object_ref (icon_info->pixbuf);
+  /* Instead of returning the pixbuf directly we
+     return a proxy to it that we don't own (but that
+     shares the data with the one we own). This way
+     we can know when it is freed and ensure the
+     IconInfo is alive (and thus cached) while
+     the pixbuf is still alive. */
+
+  if (icon_info->proxy_pixbuf != NULL)
+    return g_object_ref (icon_info->proxy_pixbuf);
+
+  icon_info->proxy_pixbuf =
+    gdk_pixbuf_new_from_data (gdk_pixbuf_get_pixels (icon_info->pixbuf),
+                             gdk_pixbuf_get_colorspace (icon_info->pixbuf),
+                             gdk_pixbuf_get_has_alpha (icon_info->pixbuf),
+                             gdk_pixbuf_get_bits_per_sample (icon_info->pixbuf),
+                             gdk_pixbuf_get_width (icon_info->pixbuf),
+                             gdk_pixbuf_get_height (icon_info->pixbuf),
+                             gdk_pixbuf_get_rowstride (icon_info->pixbuf),
+                             proxy_pixbuf_destroy,
+                             gtk_icon_info_copy (icon_info));
+
+  return icon_info->proxy_pixbuf;
 }
 
 static gchar *
@@ -3162,40 +3507,109 @@ gdk_rgba_to_css (const GdkRGBA *color)
                           (gint)(color->blue * 255));
 }
 
+static void
+proxy_symbolic_pixbuf_destroy (guchar *pixels, gpointer data)
+{
+  GtkIconInfo *icon_info = data;
+  GtkIconTheme *icon_theme = icon_info->in_cache;
+  SymbolicPixbufCache *symbolic_cache;
+
+  for (symbolic_cache = icon_info->symbolic_pixbuf_cache;
+       symbolic_cache != NULL;
+       symbolic_cache = symbolic_cache->next)
+    {
+      if (symbolic_cache->proxy_pixbuf != NULL &&
+         gdk_pixbuf_get_pixels (symbolic_cache->proxy_pixbuf) == pixels)
+       break;
+    }
+
+  g_assert (symbolic_cache != NULL);
+  g_assert (symbolic_cache->proxy_pixbuf != NULL);
+
+  symbolic_cache->proxy_pixbuf = NULL;
+
+  /* Keep it alive a bit longer */
+  if (icon_theme != NULL)
+    ensure_in_lru_cache (icon_theme, icon_info);
+
+  gtk_icon_info_free (icon_info);
+}
+
+static GdkPixbuf *
+symbolic_cache_get_proxy (SymbolicPixbufCache *symbolic_cache,
+                         GtkIconInfo  *icon_info)
+{
+  if (symbolic_cache->proxy_pixbuf)
+    return g_object_ref (symbolic_cache->proxy_pixbuf);
+
+  symbolic_cache->proxy_pixbuf =
+    gdk_pixbuf_new_from_data (gdk_pixbuf_get_pixels (symbolic_cache->pixbuf),
+                             gdk_pixbuf_get_colorspace (symbolic_cache->pixbuf),
+                             gdk_pixbuf_get_has_alpha (symbolic_cache->pixbuf),
+                             gdk_pixbuf_get_bits_per_sample (symbolic_cache->pixbuf),
+                             gdk_pixbuf_get_width (symbolic_cache->pixbuf),
+                             gdk_pixbuf_get_height (symbolic_cache->pixbuf),
+                             gdk_pixbuf_get_rowstride (symbolic_cache->pixbuf),
+                             proxy_symbolic_pixbuf_destroy,
+                             gtk_icon_info_copy (icon_info));
+
+  return symbolic_cache->proxy_pixbuf;
+}
+
 static GdkPixbuf *
 _gtk_icon_info_load_symbolic_internal (GtkIconInfo  *icon_info,
-                                       const gchar  *css_fg,
-                                       const gchar  *css_success,
-                                       const gchar  *css_warning,
-                                       const gchar  *css_error,
+                                      const GdkRGBA  *fg,
+                                      const GdkRGBA  *success_color,
+                                      const GdkRGBA  *warning_color,
+                                      const GdkRGBA  *error_color,
                                        GError      **error)
 {
   GInputStream *stream;
   GdkPixbuf *pixbuf;
+  gchar *css_fg;
+  gchar *css_success;
+  gchar *css_warning;
+  gchar *css_error;
   gchar *data;
-  gchar *success, *warning, *err;
   gchar *width, *height, *uri;
+  SymbolicPixbufCache *symbolic_cache;
+
+  symbolic_cache = symbolic_pixbuf_cache_matches (icon_info->symbolic_pixbuf_cache,
+                                                 fg, success_color, warning_color, error_color);
+  if (symbolic_cache)
+    return symbolic_cache_get_proxy (symbolic_cache, icon_info);
 
   /* css_fg can't possibly have failed, otherwise
    * that would mean we have a broken style */
-  g_return_val_if_fail (css_fg != NULL, NULL);
+  g_return_val_if_fail (fg != NULL, NULL);
+
+  css_fg = gdk_rgba_to_css (fg);
+
+  css_success = css_warning = css_error = NULL;
+
+  if (warning_color)
+    css_warning = gdk_rgba_to_css (warning_color);
+
+  if (error_color)
+    css_error = gdk_rgba_to_css (error_color);
 
-  success = warning = err = NULL;
+  if (success_color)
+    css_success = gdk_rgba_to_css (success_color);
 
   if (!css_success)
     {
       GdkColor success_default_color = { 0, 0x4e00, 0x9a00, 0x0600 };
-      success = gdk_color_to_css (&success_default_color);
+      css_success = gdk_color_to_css (&success_default_color);
     }
   if (!css_warning)
     {
       GdkColor warning_default_color = { 0, 0xf500, 0x7900, 0x3e00 };
-      warning = gdk_color_to_css (&warning_default_color);
+      css_warning = gdk_color_to_css (&warning_default_color);
     }
   if (!css_error)
     {
       GdkColor error_default_color = { 0, 0xcc00, 0x0000, 0x0000 };
-      err = gdk_color_to_css (&error_default_color);
+      css_error = gdk_color_to_css (&error_default_color);
     }
 
   if (!icon_info->symbolic_pixbuf_size)
@@ -3233,21 +3647,22 @@ _gtk_icon_info_load_symbolic_internal (GtkIconInfo  *icon_info,
                       "      fill: ", css_fg," !important;\n"
                       "    }\n"
                       "    .warning {\n"
-                      "      fill: ", css_warning ? css_warning : warning," !important;\n"
+                      "      fill: ", css_warning" !important;\n"
                       "    }\n"
                       "    .error {\n"
-                      "      fill: ", css_error ? css_error : err," !important;\n"
+                      "      fill: ", css_error ," !important;\n"
                       "    }\n"
                       "    .success {\n"
-                      "      fill: ", css_success ? css_success : success," !important;\n"
+                      "      fill: ", css_success" !important;\n"
                       "    }\n"
                       "  </style>\n"
                       "  <xi:include href=\"", uri, "\"/>\n"
                       "</svg>",
                       NULL);
-  g_free (warning);
-  g_free (err);
-  g_free (success);
+  g_free (css_fg);
+  g_free (css_warning);
+  g_free (css_error);
+  g_free (css_success);
   g_free (width);
   g_free (height);
   g_free (uri);
@@ -3261,7 +3676,16 @@ _gtk_icon_info_load_symbolic_internal (GtkIconInfo  *icon_info,
                                                 error);
   g_object_unref (stream);
 
-  return pixbuf;
+  if (pixbuf != NULL)
+    {
+      icon_info->symbolic_pixbuf_cache =
+       symbolic_pixbuf_cache_new (pixbuf, fg, success_color, warning_color, error_color,
+                                  icon_info->symbolic_pixbuf_cache);
+
+      return symbolic_cache_get_proxy (icon_info->symbolic_pixbuf_cache, icon_info);
+    }
+
+  return NULL;
 }
 
 /**
@@ -3310,14 +3734,10 @@ gtk_icon_info_load_symbolic (GtkIconInfo    *icon_info,
                              gboolean       *was_symbolic,
                              GError        **error)
 {
-  GdkPixbuf *pixbuf;
-  gchar *css_fg;
-  gchar *css_success;
-  gchar *css_warning;
-  gchar *css_error;
   gchar *icon_uri;
   gboolean is_symbolic;
 
+  g_return_val_if_fail (icon_info != NULL, NULL);
   g_return_val_if_fail (fg != NULL, NULL);
 
   icon_uri = NULL;
@@ -3333,29 +3753,10 @@ gtk_icon_info_load_symbolic (GtkIconInfo    *icon_info,
   if (!is_symbolic)
     return gtk_icon_info_load_icon (icon_info, error);
 
-  css_fg = gdk_rgba_to_css (fg);
-
-  css_success = css_warning = css_error = NULL;
-
-  if (warning_color)
-    css_warning = gdk_rgba_to_css (warning_color);
-
-  if (error_color)
-    css_error = gdk_rgba_to_css (error_color);
-
-  if (success_color)
-    css_success = gdk_rgba_to_css (success_color);
-
-  pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
-                                                  css_fg, css_success,
-                                                  css_warning, css_error,
-                                                  error);
-  g_free (css_fg);
-  g_free (css_warning);
-  g_free (css_success);
-  g_free (css_error);
-
-  return pixbuf;
+  return _gtk_icon_info_load_symbolic_internal (icon_info,
+                                               fg, success_color,
+                                               warning_color, error_color,
+                                               error);
 }
 
 /**
@@ -3389,15 +3790,22 @@ gtk_icon_info_load_symbolic_for_context (GtkIconInfo      *icon_info,
                                          gboolean         *was_symbolic,
                                          GError          **error)
 {
-  GdkPixbuf *pixbuf;
   GdkRGBA *color = NULL;
-  GdkRGBA rgba;
-  gchar *css_fg = NULL, *css_success;
-  gchar *css_warning, *css_error;
+  GdkRGBA fg;
+  GdkRGBA *fgp;
+  GdkRGBA success_color;
+  GdkRGBA *success_colorp;
+  GdkRGBA warning_color;
+  GdkRGBA *warning_colorp;
+  GdkRGBA error_color;
+  GdkRGBA *error_colorp;
   GtkStateFlags state;
   gchar *icon_uri;
   gboolean is_symbolic;
 
+  g_return_val_if_fail (icon_info != NULL, NULL);
+  g_return_val_if_fail (context != NULL, NULL);
+
   icon_uri = NULL;
   if (icon_info->icon_file)
     icon_uri = g_file_get_uri (icon_info->icon_file);
@@ -3411,36 +3819,40 @@ gtk_icon_info_load_symbolic_for_context (GtkIconInfo      *icon_info,
   if (!is_symbolic)
     return gtk_icon_info_load_icon (icon_info, error);
 
+  fgp = success_colorp = warning_colorp = error_colorp = NULL;
+
   state = gtk_style_context_get_state (context);
   gtk_style_context_get (context, state, "color", &color, NULL);
   if (color)
     {
-      css_fg = gdk_rgba_to_css (color);
+      fg = *color;
+      fgp = &fg;
       gdk_rgba_free (color);
     }
 
-  css_success = css_warning = css_error = NULL;
-
-  if (gtk_style_context_lookup_color (context, "success_color", &rgba))
-    css_success = gdk_rgba_to_css (&rgba);
-
-  if (gtk_style_context_lookup_color (context, "warning_color", &rgba))
-    css_warning = gdk_rgba_to_css (&rgba);
+  if (gtk_style_context_lookup_color (context, "success_color", &success_color))
+    success_colorp = &success_color;
 
-  if (gtk_style_context_lookup_color (context, "error_color", &rgba))
-    css_error = gdk_rgba_to_css (&rgba);
+  if (gtk_style_context_lookup_color (context, "warning_color", &warning_color))
+    warning_colorp = &warning_color;
 
-  pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
-                                                  css_fg, css_success,
-                                                  css_warning, css_error,
-                                                  error);
+  if (gtk_style_context_lookup_color (context, "error_color", &error_color))
+    error_colorp = &error_color;
 
-  g_free (css_fg);
-  g_free (css_success);
-  g_free (css_warning);
-  g_free (css_error);
+  return _gtk_icon_info_load_symbolic_internal (icon_info,
+                                               fgp, success_colorp,
+                                               warning_colorp, error_colorp,
+                                               error);
+}
 
-  return pixbuf;
+static GdkRGBA *
+color_to_rgba (GdkColor *color, GdkRGBA *rgba)
+{
+  rgba->red = color->red / 65535.0;
+  rgba->green = color->green / 65535.0;
+  rgba->blue = color->blue / 65535.0;
+  rgba->alpha = 1.0;
+  return rgba;
 }
 
 /**
@@ -3475,16 +3887,20 @@ gtk_icon_info_load_symbolic_for_style (GtkIconInfo   *icon_info,
                                        gboolean      *was_symbolic,
                                        GError       **error)
 {
-  GdkPixbuf *pixbuf;
-  GdkColor success_color;
-  GdkColor warning_color;
-  GdkColor error_color;
-  GdkColor *fg;
-  gchar *css_fg, *css_success;
-  gchar *css_warning, *css_error;
+  GdkColor color;
+  GdkRGBA fg;
+  GdkRGBA success_color;
+  GdkRGBA *success_colorp;
+  GdkRGBA warning_color;
+  GdkRGBA *warning_colorp;
+  GdkRGBA error_color;
+  GdkRGBA *error_colorp;
   gchar *icon_uri;
   gboolean is_symbolic;
 
+  g_return_val_if_fail (icon_info != NULL, NULL);
+  g_return_val_if_fail (style != NULL, NULL);
+
   icon_uri = NULL;
   if (icon_info->icon_file)
     icon_uri = g_file_get_uri (icon_info->icon_file);
@@ -3498,31 +3914,23 @@ gtk_icon_info_load_symbolic_for_style (GtkIconInfo   *icon_info,
   if (!is_symbolic)
     return gtk_icon_info_load_icon (icon_info, error);
 
-  fg = &style->fg[state];
-  css_fg = gdk_color_to_css (fg);
-
-  css_success = css_warning = css_error = NULL;
+  color_to_rgba (&style->fg[state], &fg);
 
-  if (gtk_style_lookup_color (style, "success_color", &success_color))
-    css_success = gdk_color_to_css (&success_color);
+  success_colorp = warning_colorp = error_colorp = NULL;
 
-  if (gtk_style_lookup_color (style, "warning_color", &warning_color))
-    css_warning = gdk_color_to_css (&warning_color);
+  if (gtk_style_lookup_color (style, "success_color", &color))
+    success_colorp = color_to_rgba (&color, &success_color);
 
-  if (gtk_style_lookup_color (style, "error_color", &error_color))
-    css_error = gdk_color_to_css (&error_color);
+  if (gtk_style_lookup_color (style, "warning_color", &color))
+    warning_colorp = color_to_rgba (&color, &warning_color);
 
-  pixbuf = _gtk_icon_info_load_symbolic_internal (icon_info,
-                                                  css_fg, css_success,
-                                                  css_warning, css_error,
-                                                  error);
+  if (gtk_style_lookup_color (style, "error_color", &color))
+    error_colorp = color_to_rgba (&color, &error_color);
 
-  g_free (css_fg);
-  g_free (css_success);
-  g_free (css_warning);
-  g_free (css_error);
-
-  return pixbuf;
+  return _gtk_icon_info_load_symbolic_internal (icon_info,
+                                               &fg, success_colorp,
+                                               warning_colorp, error_colorp,
+                                               error);
 }
 
 /**