[gnome-photos/wip/rishi/misc-fixes: 2/2] base-item, item-manager: Age out the GEGL objects when unused



commit 0dfb60096fbd70dbf071eeff54d01b29f82d981e
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Dec 15 20:35:13 2015 +0100

    base-item, item-manager: Age out the GEGL objects when unused
    
    We were never dropping the GEGL resources during the lifetime of the
    process, which is wasteful. Once a BaseItem has not been the active
    object for a certain period of time, we should drop them.

 src/photos-base-item.c    |   41 +++++++++++++++++++++++
 src/photos-base-item.h    |    4 ++
 src/photos-item-manager.c |   81 +++++++++++++++++++++++++++++++++++++++++++++
 src/photos-utils.h        |    5 +++
 4 files changed, 131 insertions(+), 0 deletions(-)
---
diff --git a/src/photos-base-item.c b/src/photos-base-item.c
index a3306f1..3b0e8da 100644
--- a/src/photos-base-item.c
+++ b/src/photos-base-item.c
@@ -101,6 +101,7 @@ struct _PhotosBaseItemPrivate
   gdouble iso_speed;
   gint64 date_created;
   gint64 height;
+  gint64 last_active;
   gint64 mtime;
   gint64 width;
 };
@@ -1782,6 +1783,7 @@ photos_base_item_init (PhotosBaseItem *self)
   g_mutex_init (&priv->mutex);
 
   priv->sel_cntrlr = photos_selection_controller_dup_singleton ();
+  priv->last_active = -1;
 }
 
 
@@ -2419,6 +2421,18 @@ photos_base_item_is_favorite (PhotosBaseItem *self)
 
 
 void
+photos_base_item_last_active_update (PhotosBaseItem *self)
+{
+  PhotosBaseItemPrivate *priv = self->priv;
+
+  if (priv->collection)
+    return;
+
+  priv->last_active = g_get_monotonic_time ();
+}
+
+
+void
 photos_base_item_load_async (PhotosBaseItem *self,
                              GCancellable *cancellable,
                              GAsyncReadyCallback callback,
@@ -2436,6 +2450,8 @@ photos_base_item_load_async (PhotosBaseItem *self,
   task = g_task_new (self, cancellable, callback, user_data);
   g_task_set_source_tag (task, photos_base_item_load_async);
 
+  priv->last_active = 0;
+
   if (priv->pipeline != NULL)
     {
       GeglNode *graph;
@@ -2871,3 +2887,28 @@ photos_base_item_trash (PhotosBaseItem *self)
   photos_delete_item_job_run (job, NULL, NULL, NULL);
   g_object_unref (job);
 }
+
+
+void
+photos_base_item_unload (PhotosBaseItem *self)
+{
+  PhotosBaseItemPrivate *priv = self->priv;
+  gint64 now;
+
+  if (priv->last_active <= 0)
+    return;
+
+  now = g_get_monotonic_time ();
+  if (now - priv->last_active < PHOTOS_ITEM_UNLOAD_TIMEOUT * 1000000)
+    return;
+
+  priv->last_active = -1;
+
+  priv->buffer_sink = NULL;
+  priv->buffer_source = NULL;
+  priv->load = NULL;
+  g_clear_object (&priv->edit_graph);
+  g_clear_object (&priv->load_graph);
+  g_clear_object (&priv->pipeline);
+  g_clear_object (&priv->processor);
+}
diff --git a/src/photos-base-item.h b/src/photos-base-item.h
index 3b297f6..021b23e 100644
--- a/src/photos-base-item.h
+++ b/src/photos-base-item.h
@@ -194,6 +194,8 @@ gboolean            photos_base_item_is_collection           (PhotosBaseItem *se
 
 gboolean            photos_base_item_is_favorite             (PhotosBaseItem *self);
 
+void                photos_base_item_last_active_update      (PhotosBaseItem *self);
+
 void                photos_base_item_load_async              (PhotosBaseItem *self,
                                                               GCancellable *cancellable,
                                                               GAsyncReadyCallback callback,
@@ -279,6 +281,8 @@ void                photos_base_item_thumbnailing_stop       (void);
 
 void                photos_base_item_trash                   (PhotosBaseItem *self);
 
+void                photos_base_item_unload                  (PhotosBaseItem *self);
+
 G_END_DECLS
 
 #endif /* PHOTOS_BASE_ITEM_H */
diff --git a/src/photos-item-manager.c b/src/photos-item-manager.c
index e160f11..9b812d5 100644
--- a/src/photos-item-manager.c
+++ b/src/photos-item-manager.c
@@ -56,6 +56,8 @@ struct _PhotosItemManager
   PhotosTrackerChangeMonitor *monitor;
   PhotosWindowMode mode;
   gboolean fullscreen;
+  gboolean unload_queue_again;
+  guint unload_timeout_id;
 };
 
 struct _PhotosItemManagerClass
@@ -89,6 +91,9 @@ G_DEFINE_TYPE (PhotosItemManager, photos_item_manager, PHOTOS_TYPE_BASE_MANAGER)
 EGG_DEFINE_COUNTER (instances, "PhotosItemManager", "Instances", "Number of PhotosItemManager instances")
 
 
+static void photos_item_manager_unload_queue (PhotosItemManager *self);
+
+
 static void
 photos_item_manager_add_object (PhotosBaseManager *mngr, GObject *object)
 {
@@ -282,6 +287,19 @@ photos_item_manager_item_load (GObject *source_object, GAsyncResult *res, gpoint
 
 
 static void
+photos_item_manager_last_active_update (PhotosItemManager *self)
+{
+  PhotosBaseItem *item;
+
+  item = PHOTOS_BASE_ITEM (photos_base_manager_get_active_object (PHOTOS_BASE_MANAGER (self)));
+  if (item == NULL)
+    return;
+
+  photos_base_item_last_active_update (item);
+}
+
+
+static void
 photos_item_manager_remove_object_by_id (PhotosBaseManager *mngr, const gchar *id)
 {
   PhotosItemManager *self = PHOTOS_ITEM_MANAGER (mngr);
@@ -301,6 +319,46 @@ photos_item_manager_remove_object_by_id (PhotosBaseManager *mngr, const gchar *i
 }
 
 
+static gboolean
+photos_item_manager_unload_timeout (gpointer user_data)
+{
+  PhotosItemManager *self = PHOTOS_ITEM_MANAGER (user_data);
+  GHashTable *items;
+  GHashTableIter iter;
+  PhotosBaseItem *item;
+
+  self->unload_timeout_id = 0;
+
+  items = photos_base_manager_get_objects (PHOTOS_BASE_MANAGER (self));
+  g_hash_table_iter_init (&iter, items);
+  while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &item))
+    photos_base_item_unload (item);
+
+  if (self->unload_queue_again)
+    {
+      self->unload_queue_again = FALSE;
+      photos_item_manager_unload_queue (self);
+    }
+
+  return G_SOURCE_REMOVE;
+}
+
+
+static void
+photos_item_manager_unload_queue (PhotosItemManager *self)
+{
+  if (self->unload_timeout_id != 0)
+    {
+      self->unload_queue_again = TRUE;
+      return;
+    }
+
+  self->unload_timeout_id = g_timeout_add_seconds (PHOTOS_ITEM_UNLOAD_TIMEOUT,
+                                                   photos_item_manager_unload_timeout,
+                                                   self);
+}
+
+
 static void
 photos_item_manager_update_fullscreen (PhotosItemManager *self)
 {
@@ -378,6 +436,8 @@ photos_item_manager_set_active_object (PhotosBaseManager *manager, GObject *obje
       start_loading = TRUE;
     }
 
+  photos_item_manager_last_active_update (self);
+
   ret_val = PHOTOS_BASE_MANAGER_CLASS (photos_item_manager_parent_class)->set_active_object (manager, 
object);
   /* We have already eliminated the possibility of failure. */
   g_assert (ret_val == TRUE);
@@ -414,6 +474,8 @@ photos_item_manager_set_active_object (PhotosBaseManager *manager, GObject *obje
       g_assert (active_item != (GObject *) self->active_collection);
     }
 
+  photos_item_manager_unload_queue (self);
+
  out:
   return ret_val;
 }
@@ -432,6 +494,12 @@ photos_item_manager_dispose (GObject *object)
       self->collection_path = NULL;
     }
 
+  if (self->unload_timeout_id != 0)
+    {
+      g_source_remove (self->unload_timeout_id);
+      self->unload_timeout_id = 0;
+    }
+
   g_clear_pointer (&self->collections, (GDestroyNotify) g_hash_table_unref);
   g_clear_object (&self->loader_cancellable);
   g_clear_object (&self->active_collection);
@@ -588,9 +656,12 @@ photos_item_manager_activate_previous_collection (PhotosItemManager *self)
     g_object_ref (collection);
   self->active_collection = PHOTOS_BASE_ITEM (collection);
 
+  photos_item_manager_last_active_update (self);
+
   PHOTOS_BASE_MANAGER_CLASS (photos_item_manager_parent_class)
     ->set_active_object (PHOTOS_BASE_MANAGER (self), (GObject *) collection);
 
+  photos_item_manager_unload_queue (self);
   g_signal_emit (self, signals[ACTIVE_COLLECTION_CHANGED], 0, self->active_collection);
 
   g_clear_object (&collection);
@@ -753,8 +824,12 @@ photos_mode_controller_go_back (PhotosModeController *self)
   if (old_mode == PHOTOS_WINDOW_MODE_PREVIEW)
     {
       self->load_state = PHOTOS_LOAD_STATE_NONE;
+      photos_item_manager_last_active_update (self);
+
       PHOTOS_BASE_MANAGER_CLASS (photos_item_manager_parent_class)
         ->set_active_object (PHOTOS_BASE_MANAGER (self), (GObject *) self->active_collection);
+
+      photos_item_manager_unload_queue (self);
     }
   else if (old_mode != PHOTOS_WINDOW_MODE_EDIT)
     {
@@ -763,10 +838,12 @@ photos_mode_controller_go_back (PhotosModeController *self)
 
       g_clear_object (&self->active_collection);
       self->load_state = PHOTOS_LOAD_STATE_NONE;
+      photos_item_manager_last_active_update (self);
 
       PHOTOS_BASE_MANAGER_CLASS (photos_item_manager_parent_class)
         ->set_active_object (PHOTOS_BASE_MANAGER (self), NULL);
 
+      photos_item_manager_unload_queue (self);
       g_signal_emit (self, signals[ACTIVE_COLLECTION_CHANGED], 0, self->active_collection);
     }
 
@@ -829,9 +906,13 @@ photos_mode_controller_set_window_mode (PhotosModeController *self, PhotosWindow
           active_collection_changed = TRUE;
         }
 
+      photos_item_manager_last_active_update (self);
+
       PHOTOS_BASE_MANAGER_CLASS (photos_item_manager_parent_class)
         ->set_active_object (PHOTOS_BASE_MANAGER (self), NULL);
 
+      photos_item_manager_unload_queue (self);
+
       if (active_collection_changed)
         g_signal_emit (self, signals[ACTIVE_COLLECTION_CHANGED], 0, self->active_collection);
     }
diff --git a/src/photos-utils.h b/src/photos-utils.h
index 1821811..3dab0b8 100644
--- a/src/photos-utils.h
+++ b/src/photos-utils.h
@@ -57,6 +57,11 @@ G_BEGIN_DECLS
   "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#image-category-screenshot";
 #define PHOTOS_EXPORT_SUBPATH "Exports"
 
+enum
+{
+  PHOTOS_ITEM_UNLOAD_TIMEOUT = 10 /* s */
+};
+
 void             photos_utils_border_pixbuf               (GdkPixbuf *pixbuf);
 
 void             photos_utils_buffer_zoom_async           (GeglBuffer *buffer,


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