[buoh] Do not handle pixbufs in cache



commit 5a1999c21c22f90ef4288873d179d8bc1f3e61d0
Author: Jan Tojnar <jtojnar gmail com>
Date:   Fri Oct 1 06:27:31 2021 +0200

    Do not handle pixbufs in cache
    
    When the image changes, the pixbuf does too so it does not make much sense to cache the pixbuf.
    Unless it has something to do with zoom but then the cache is probably the wrong layer.

 src/buoh-comic-cache.c | 78 +++++++-------------------------------------------
 src/buoh-comic-cache.h |  3 --
 src/buoh-comic.c       | 32 +--------------------
 src/buoh-comic.h       |  4 ---
 src/buoh-view-comic.c  |  9 ------
 5 files changed, 12 insertions(+), 114 deletions(-)
---
diff --git a/src/buoh-comic-cache.c b/src/buoh-comic-cache.c
index 9aa3d3a..6c5591c 100644
--- a/src/buoh-comic-cache.c
+++ b/src/buoh-comic-cache.c
@@ -35,9 +35,6 @@ struct _BuohComicCache {
         GList      *image_list;
         GList      *image_disk;
         gulong      size;
-
-        GdkPixbuf  *current_pixbuf;
-        gchar      *current_uri;
 };
 
 #define CACHE_SIZE 1048576 /* 1MB */
@@ -100,10 +97,6 @@ buoh_comic_cache_finalize (GObject *object)
                 g_clear_pointer (&comic_cache->image_disk, g_list_free);
         }
 
-        g_clear_object (&comic_cache->current_pixbuf);
-
-        g_clear_pointer (&comic_cache->current_uri, g_free);
-
         if (G_OBJECT_CLASS (buoh_comic_cache_parent_class)->finalize) {
                 (* G_OBJECT_CLASS (buoh_comic_cache_parent_class)->finalize) (object);
         }
@@ -175,27 +168,13 @@ buoh_comic_cache_to_disk (BuohComicCache *cache,
         g_free (path);
 }
 
-static void
-buoh_comic_cache_set_current (BuohComicCache *cache,
-                              const gchar    *uri,
-                              BuohComicImage *image)
+static GdkPixbuf *
+image_to_pixbuf (BuohComicImage *image)
 {
         GdkPixbufLoader *loader;
+        GdkPixbuf *pixbuf;
         GError          *error = NULL;
 
-        if (cache->current_uri &&
-            (g_ascii_strcasecmp (uri, cache->current_uri) == 0) &&
-            GDK_IS_PIXBUF (cache->current_pixbuf)) {
-                return;
-        }
-
-        if (cache->current_pixbuf) {
-                g_object_unref (cache->current_pixbuf);
-        }
-        if (cache->current_uri) {
-                g_free (cache->current_uri);
-        }
-
         loader = gdk_pixbuf_loader_new ();
         gdk_pixbuf_loader_write (loader, image->data,
                                  image->size, &error);
@@ -203,17 +182,14 @@ buoh_comic_cache_set_current (BuohComicCache *cache,
                 g_warning ("%s", error->message);
                 g_clear_error (&error);
 
-                cache->current_pixbuf = NULL;
-                cache->current_uri = NULL;
                 gdk_pixbuf_loader_close (loader, NULL);
                 g_object_unref (loader);
 
-                return;
+                return NULL;
         }
 
-        cache->current_pixbuf =
-                gdk_pixbuf_loader_get_pixbuf (loader);
-        g_object_ref (cache->current_pixbuf);
+        pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
+        g_object_ref (pixbuf);
         gdk_pixbuf_loader_close (loader, &error);
         g_object_unref (loader);
 
@@ -221,13 +197,10 @@ buoh_comic_cache_set_current (BuohComicCache *cache,
                 g_warning ("%s", error->message);
                 g_clear_error (&error);
 
-                cache->current_pixbuf = NULL;
-                cache->current_uri = NULL;
-
-                return;
+                return NULL;
         }
 
-        cache->current_uri = g_strdup (uri);
+        return pixbuf;
 }
 
 void
@@ -244,8 +217,8 @@ buoh_comic_cache_set_image (BuohComicCache *cache,
 
         buoh_debug ("CACHE: uri %s", uri);
 
-        if ((img = g_hash_table_lookup (cache->image_hash, uri))) {
-                buoh_comic_cache_set_current (cache, uri, img);
+        if (g_hash_table_lookup (cache->image_hash, uri)) {
+                // Already in cache.
                 return;
         }
 
@@ -253,7 +226,6 @@ buoh_comic_cache_set_image (BuohComicCache *cache,
 
         if (image->size > CACHE_SIZE) {
                 buoh_comic_cache_to_disk (cache, uri, image);
-                buoh_comic_cache_set_current (cache, uri, image);
                 return;
         }
 
@@ -286,7 +258,6 @@ buoh_comic_cache_set_image (BuohComicCache *cache,
         buoh_debug ("CACHE: caching (memory) %s", key_uri);
         cache->image_list = g_list_prepend (cache->image_list, key_uri);
         g_hash_table_insert (cache->image_hash, key_uri, image);
-        buoh_comic_cache_set_current (cache, uri, image);
 
         cache->size += image->size;
         buoh_debug ("CACHE: cache size %d\n", cache->size);
@@ -338,26 +309,6 @@ buoh_comic_cache_get_image (BuohComicCache *cache,
         return NULL;
 }
 
-void
-buoh_comic_cache_set_pixbuf (BuohComicCache *cache,
-                             const gchar    *uri,
-                             GdkPixbuf      *pixbuf)
-{
-        g_return_if_fail (BUOH_IS_COMIC_CACHE (cache));
-        g_return_if_fail (uri != NULL);
-        g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
-
-        if (cache->current_uri) {
-                g_free (cache->current_uri);
-        }
-        if (cache->current_pixbuf) {
-                g_object_unref (cache->current_pixbuf);
-        }
-
-        cache->current_uri = g_strdup (uri);
-        cache->current_pixbuf = g_object_ref (pixbuf);
-}
-
 GdkPixbuf *
 buoh_comic_cache_get_pixbuf (BuohComicCache *cache,
                              const gchar    *uri)
@@ -367,17 +318,10 @@ buoh_comic_cache_get_pixbuf (BuohComicCache *cache,
         g_return_val_if_fail (BUOH_IS_COMIC_CACHE (cache), NULL);
         g_return_val_if_fail (uri != NULL, NULL);
 
-        if (cache->current_uri &&
-            g_ascii_strcasecmp (uri, cache->current_uri) == 0) {
-                buoh_debug ("is the current pixbuf");
-                return cache->current_pixbuf;
-        }
-
         image = buoh_comic_cache_get_image (cache, uri);
 
         if (image) {
-                buoh_comic_cache_set_current (cache, uri, image);
-                return cache->current_pixbuf;
+                return image_to_pixbuf (image);
         }
 
         return NULL;
diff --git a/src/buoh-comic-cache.h b/src/buoh-comic-cache.h
index 8adb8bf..bb9701c 100644
--- a/src/buoh-comic-cache.h
+++ b/src/buoh-comic-cache.h
@@ -36,9 +36,6 @@ void            buoh_comic_cache_set_image  (BuohComicCache *cache,
                                              BuohComicImage *image);
 BuohComicImage *buoh_comic_cache_get_image  (BuohComicCache *cache,
                                              const gchar    *uri);
-void            buoh_comic_cache_set_pixbuf (BuohComicCache *cache,
-                                             const gchar    *uri,
-                                             GdkPixbuf      *pixbuf);
 GdkPixbuf      *buoh_comic_cache_get_pixbuf (BuohComicCache *cache,
                                              const gchar    *uri);
 
diff --git a/src/buoh-comic.c b/src/buoh-comic.c
index 2941706..9e69f6c 100644
--- a/src/buoh-comic.c
+++ b/src/buoh-comic.c
@@ -105,7 +105,7 @@ buoh_comic_class_init (BuohComicClass *klass)
                                          g_param_spec_pointer ("pixbuf",
                                                                "Pixbuf",
                                                                "Pixbuf of the comic",
-                                                               G_PARAM_READWRITE));
+                                                               G_PARAM_READABLE));
         g_object_class_install_property (object_class,
                                          PROP_IMAGE,
                                          g_param_spec_pointer ("image",
@@ -186,14 +186,6 @@ buoh_comic_set_property (GObject      *object,
                 g_free (comic->uri);
                 comic->uri = g_value_dup_string (value);
 
-                break;
-        case PROP_PIXBUF: {
-                GdkPixbuf *pixbuf;
-
-                pixbuf = GDK_PIXBUF (g_value_get_pointer (value));
-                buoh_comic_cache_set_pixbuf (comic->cache,
-                                             comic->uri, pixbuf);
-        }
                 break;
         case PROP_IMAGE: {
                 BuohComicImage *image;
@@ -270,15 +262,6 @@ buoh_comic_set_id (BuohComic *comic, const gchar *id)
         g_object_set (G_OBJECT (comic), "id", id, NULL);
 }
 
-void
-buoh_comic_set_pixbuf (BuohComic *comic, GdkPixbuf *pixbuf)
-{
-        g_return_if_fail (BUOH_IS_COMIC (comic));
-        g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
-
-        g_object_set (G_OBJECT (comic), "pixbuf", pixbuf, NULL);
-}
-
 void
 buoh_comic_set_image (BuohComic *comic, BuohComicImage *image)
 {
@@ -297,19 +280,6 @@ buoh_comic_set_date (BuohComic *comic, GDate *date)
         g_object_set (G_OBJECT (comic), "date", date, NULL);
 }
 
-void
-buoh_comic_set_pixbuf_from_file (BuohComic *comic, const gchar *filename)
-{
-        GdkPixbuf *pixbuf = NULL;
-
-        g_return_if_fail (BUOH_IS_COMIC (comic));
-        g_return_if_fail (filename != NULL);
-
-        pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
-
-        buoh_comic_set_pixbuf (comic, pixbuf);
-}
-
 const gchar *
 buoh_comic_get_id (BuohComic *comic)
 {
diff --git a/src/buoh-comic.h b/src/buoh-comic.h
index 3c73b3f..6c20934 100644
--- a/src/buoh-comic.h
+++ b/src/buoh-comic.h
@@ -48,14 +48,10 @@ void            buoh_comic_set_id               (BuohComic      *comic,
                                                  const gchar    *id);
 void            buoh_comic_go_next              (BuohComic      *comic);
 void            buoh_comic_go_previous          (BuohComic      *comic);
-void            buoh_comic_set_pixbuf           (BuohComic      *comic,
-                                                 GdkPixbuf      *pixbuf);
 void            buoh_comic_set_image            (BuohComic      *comic,
                                                  BuohComicImage *image);
 void                buoh_comic_set_date             (BuohComic      *comic,
                                                  GDate          *date);
-void            buoh_comic_set_pixbuf_from_file (BuohComic      *comic,
-                                                 const gchar    *filename);
 
 const gchar    *buoh_comic_get_uri              (BuohComic      *comic);
 const gchar    *buoh_comic_get_id               (BuohComic      *comic);
diff --git a/src/buoh-view-comic.c b/src/buoh-view-comic.c
index d0c108c..7b7dbfd 100644
--- a/src/buoh-view-comic.c
+++ b/src/buoh-view-comic.c
@@ -683,15 +683,6 @@ buoh_view_comic_load_finished (BuohViewComic *c_view,
         if (pixbuf) {
                 buoh_view_comic_set_image_from_pixbuf (c_view, pixbuf);
 
-                if (c_view->scale == 1.0) {
-                        /* We have both the compressed and uncompressed image.
-                         * By setting the pixbuf to cache we avoid uncompressing
-                         * the image again and having a new pixbuf instead of a
-                         * reference.
-                         */
-                        buoh_comic_set_pixbuf (c_view->comic, pixbuf);
-                }
-
                 g_object_set (G_OBJECT (c_view->view),
                               "status", STATE_COMIC_LOADED,
                               NULL);


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