[gtk/avoid-mime-sniffing] Avoid mime sniffing where possible



commit 2f17ab3ce76266062be180ca022ce2fbbc3fea02
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Feb 8 10:22:54 2020 -0500

    Avoid mime sniffing where possible
    
    When we are loading symbolic pngs or svgs, we know
    that we need to the png or svg loader, so there is
    no need to go through (surprisingly expensive) mime
    sniffing to find the right loader.

 gtk/gdkpixbufutilsprivate.h |  2 ++
 gtk/gtkcssimagerecolor.c    |  4 ++--
 gtk/tools/gdkpixbufutils.c  | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gdkpixbufutilsprivate.h b/gtk/gdkpixbufutilsprivate.h
index d68d315df7..73e1c79e96 100644
--- a/gtk/gdkpixbufutilsprivate.h
+++ b/gtk/gdkpixbufutilsprivate.h
@@ -79,11 +79,13 @@ GdkPixbuf *gtk_make_symbolic_pixbuf_from_resource (const char    *path,
                                                    int            height,
                                                    double         scale,
                                                    GError       **error);
+GdkTexture *gtk_load_symbolic_texture_from_file     (GFile         *file);
 GdkTexture *gtk_make_symbolic_texture_from_file     (GFile         *file,
                                                      int            width,
                                                      int            height,
                                                      double         scale,
                                                      GError       **error);
+GdkTexture *gtk_load_symbolic_texture_from_resource (const char    *data);
 GdkTexture *gtk_make_symbolic_texture_from_resource (const char    *path,
                                                      int            width,
                                                      int            height,
diff --git a/gtk/gtkcssimagerecolor.c b/gtk/gtkcssimagerecolor.c
index 78f91d9f53..23cbb16bbe 100644
--- a/gtk/gtkcssimagerecolor.c
+++ b/gtk/gtkcssimagerecolor.c
@@ -109,7 +109,7 @@ gtk_css_image_recolor_load_texture (GtkCssImageRecolor  *recolor,
       char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
 
       if (g_str_has_suffix (uri, ".symbolic.png"))
-        recolor->texture = gdk_texture_new_from_resource (resource_path);
+        recolor->texture = gtk_load_symbolic_texture_from_resource (resource_path);
       else
         recolor->texture = gtk_make_symbolic_texture_from_resource (resource_path, 0, 0, 1.0, NULL);
 
@@ -118,7 +118,7 @@ gtk_css_image_recolor_load_texture (GtkCssImageRecolor  *recolor,
   else
     {
       if (g_str_has_suffix (uri, ".symbolic.png"))
-        recolor->texture = gdk_texture_new_from_file (recolor->file, NULL);
+        recolor->texture = gtk_load_symbolic_texture_from_file (recolor->file);
       else
         recolor->texture = gtk_make_symbolic_texture_from_file (recolor->file, 0, 0, 1.0, NULL);
     }
diff --git a/gtk/tools/gdkpixbufutils.c b/gtk/tools/gdkpixbufutils.c
index c706d720a8..1bbde140ba 100644
--- a/gtk/tools/gdkpixbufutils.c
+++ b/gtk/tools/gdkpixbufutils.c
@@ -560,6 +560,19 @@ gtk_make_symbolic_pixbuf_from_file (GFile       *file,
   return pixbuf;
 }
 
+GdkTexture *
+gtk_load_symbolic_texture_from_resource (const char *path)
+{
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture;
+
+  pixbuf = _gdk_pixbuf_new_from_resource (path, "png", NULL);
+  texture = gdk_texture_new_for_pixbuf (pixbuf);
+  g_object_unref (pixbuf);
+
+  return texture;
+}
+
 GdkTexture *
 gtk_make_symbolic_texture_from_resource (const char  *path,
                                          int          width,
@@ -580,6 +593,28 @@ gtk_make_symbolic_texture_from_resource (const char  *path,
   return texture;
 }
 
+GdkTexture *
+gtk_load_symbolic_texture_from_file (GFile *file)
+{
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture;
+  GInputStream *stream;
+
+  stream = G_INPUT_STREAM (g_file_read (file, NULL, NULL));
+  if (stream == NULL)
+    return NULL;
+
+  pixbuf = _gdk_pixbuf_new_from_stream (stream, "png", NULL, NULL);
+  g_object_unref (stream);
+  if (pixbuf == NULL)
+    return NULL;
+
+  texture = gdk_texture_new_for_pixbuf (pixbuf);
+  g_object_unref (pixbuf);
+
+  return texture;
+}
+
 GdkTexture *
 gtk_make_symbolic_texture_from_file (GFile       *file,
                                      int          width,


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