[gtk: 26/40] GtkImage: Preload icons during css validation



commit d1c6d78ebb1de39d448b0e8afab13ad495bcadb6
Author: Alexander Larsson <alexl redhat com>
Date:   Wed Jan 29 18:10:13 2020 +0100

    GtkImage: Preload icons during css validation
    
    At the end of GtkImage css validation (during style-updated) when the
    css properties (like the icon size) are valid we call _gtk_icon_helper_preload
    which does an async icon theme lookup and load. This will happen on a thread
    in parallel with the rest of the css machinery, and hopefully by the
    time we need the icon it will be ready. If not we will block when we need
    it, but during that blocking all the other icons will be loaded.
    
    Testing widget-factory this changes the time of snapshot() from 31 to
    25 msec, but on the other hand we also load a few more icons that we
    didn't before causing the css validation phase to be about 8 msec slower.
    This is because we're preloading all the images in the window, not only
    the ones that are visible.
    
    Unfortunately we still load a bunch of icons in snapshot(), from
    GtkCssImageIconTheme, and ideally we should try to preload those also.

 gtk/gtkiconhelper.c        | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkiconhelperprivate.h |  1 +
 gtk/gtkimage.c             |  2 ++
 3 files changed, 51 insertions(+)
---
diff --git a/gtk/gtkiconhelper.c b/gtk/gtkiconhelper.c
index b5a4983157..47b5b675b0 100644
--- a/gtk/gtkiconhelper.c
+++ b/gtk/gtkiconhelper.c
@@ -174,6 +174,54 @@ gtk_icon_helper_load_paintable (GtkIconHelper   *self,
   return paintable;
 }
 
+void
+_gtk_icon_helper_preload (GtkIconHelper *self)
+{
+  GtkIconTheme *icon_theme;
+  GtkIconLookupFlags flags = 0;
+  int size, scale;
+  GtkCssStyle *style;
+  GIcon *gicon = NULL;
+  GIcon *free_gicon = NULL;
+
+  switch (gtk_image_definition_get_storage_type (self->def))
+    {
+    case GTK_IMAGE_ICON_NAME:
+      if (self->use_fallback)
+        free_gicon = g_themed_icon_new_with_default_fallbacks (gtk_image_definition_get_icon_name 
(self->def));
+      else
+        free_gicon = g_themed_icon_new (gtk_image_definition_get_icon_name (self->def));
+      gicon = free_gicon;
+      break;
+    case GTK_IMAGE_GICON:
+      gicon = gtk_image_definition_get_gicon (self->def) ;
+     break;
+    case GTK_IMAGE_EMPTY:
+    case GTK_IMAGE_PAINTABLE:
+    default:
+      break;
+    }
+
+  if (gicon && G_IS_THEMED_ICON (gicon))
+    {
+      style = gtk_css_node_get_style (self->node);
+      icon_theme = gtk_css_icon_theme_value_get_icon_theme
+        (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_ICON_THEME));
+      flags |= get_icon_lookup_flags (self, style,
+                                      gtk_widget_get_direction (self->owner));
+      size = gtk_icon_helper_get_size (self);
+      scale = gtk_widget_get_scale_factor (self->owner);
+
+      gtk_icon_theme_choose_icon_async (icon_theme,
+                                        (const gchar **)g_themed_icon_get_names (G_THEMED_ICON (gicon)),
+                                        size, scale,
+                                        flags, NULL, NULL, NULL);
+    }
+
+  if (free_gicon)
+    g_object_unref (free_gicon);
+}
+
 static void
 gtk_icon_helper_ensure_paintable (GtkIconHelper *self)
 {
diff --git a/gtk/gtkiconhelperprivate.h b/gtk/gtkiconhelperprivate.h
index b7656bb849..9fbed2a92c 100644
--- a/gtk/gtkiconhelperprivate.h
+++ b/gtk/gtkiconhelperprivate.h
@@ -48,6 +48,7 @@ void _gtk_icon_helper_set_icon_name (GtkIconHelper *self,
                                      const gchar *icon_name);
 void _gtk_icon_helper_set_paintable (GtkIconHelper *self,
                                     GdkPaintable  *paintable);
+void _gtk_icon_helper_preload (GtkIconHelper *self);
 
 gboolean _gtk_icon_helper_set_pixel_size   (GtkIconHelper *self,
                                             gint           pixel_size);
diff --git a/gtk/gtkimage.c b/gtk/gtkimage.c
index 8a415750e9..ad59d97b14 100644
--- a/gtk/gtkimage.c
+++ b/gtk/gtkimage.c
@@ -1294,6 +1294,8 @@ gtk_image_style_updated (GtkWidget *widget)
 
   GTK_WIDGET_CLASS (gtk_image_parent_class)->style_updated (widget);
 
+  _gtk_icon_helper_preload (priv->icon_helper);
+
   priv->baseline_align = 0.0;
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]