[gimp] app: improve opening images through the DBus service.



commit b2649daf11a152394f61fd742a45bc4837b8c8f3
Author: Jehan <jehan girinstud io>
Date:   Tue Nov 10 22:19:11 2020 +0100

    app: improve opening images through the DBus service.
    
    - Set the software as `initialized` later, and in particular after all
      recovered images (from crash) then all command line images were
      opened. The reason is that the DBus calls have necessarily been made
      after GIMP was started (typically could be images double-clicked
      through GUI). We don't want them to appear before the images given in
      command line (or worse, some before and some after).
    - Process DBus service's data queue as a FIFO. The image requested first
      will be loaded first.
    - When a DBus call happens while GIMP is not initialized or restored,
      switch to a timeout handler. The problem with idle handlers is that
      they would be attempted too often (probably even more during startup
      when no user event happens). This is good for actions we want to
      happen reasonably quickly (like would be normally DBus calls), but not
      when we are unsure of program availability schedule (i.e. at startup).
      Here not only the handler would run a lot uselessly but it would
      likely even slow the startup down by doing so. So while GIMP is not
      initialized, switch to half-a-second timeout handler, then only switch
      back to idle handler when we are properly initialized and GIMP is
      ready to answer calls in a timely manner.

 app/app.c                 |  9 +++++---
 app/gui/gimpdbusservice.c | 58 +++++++++++++++++++++++++++++++++++++++++++----
 app/gui/gimpdbusservice.h |  7 +++---
 3 files changed, 64 insertions(+), 10 deletions(-)
---
diff --git a/app/app.c b/app/app.c
index c2f7ee3311..136e130cb2 100644
--- a/app/app.c
+++ b/app/app.c
@@ -351,9 +351,6 @@ app_run (const gchar         *full_prog_name,
                           G_CALLBACK (app_exit_after_callback),
                           &run_loop);
 
-  /* The software is now fully loaded and ready to be used. */
-  gimp->initialized = TRUE;
-
 #ifndef GIMP_CONSOLE_COMPILATION
   if (run_loop && ! no_interface)
     {
@@ -428,6 +425,12 @@ app_run (const gchar         *full_prog_name,
             }
         }
     }
+
+  /* The software is now fully loaded and ready to be used and get
+   * external input.
+   */
+  gimp->initialized = TRUE;
+
   if (font_error)
     {
       gimp_message_literal (gimp, NULL,
diff --git a/app/gui/gimpdbusservice.c b/app/gui/gimpdbusservice.c
index 04dec1a37f..08e52a6c3f 100644
--- a/app/gui/gimpdbusservice.c
+++ b/app/gui/gimpdbusservice.c
@@ -147,6 +147,7 @@ gimp_dbus_service_dispose (GObject *object)
     {
       g_source_remove (g_source_get_id (service->source));
       service->source = NULL;
+      service->timeout_source = FALSE;
     }
 
   while (! g_queue_is_empty (service->queue))
@@ -269,6 +270,7 @@ gimp_dbus_service_queue_open (GimpDBusService *service,
   if (! service->source)
     {
       service->source = g_idle_source_new ();
+      service->timeout_source = FALSE;
 
       g_source_set_priority (service->source, G_PRIORITY_LOW);
       g_source_set_callback (service->source,
@@ -302,6 +304,7 @@ gimp_dbus_service_queue_batch (GimpDBusService *service,
   if (! service->source)
     {
       service->source = g_idle_source_new ();
+      service->timeout_source = FALSE;
 
       g_source_set_priority (service->source, G_PRIORITY_LOW);
       g_source_set_callback (service->source,
@@ -329,9 +332,35 @@ gimp_dbus_service_process_idle (GimpDBusService *service)
   IdleData *data;
 
   if (! service->gimp->initialized || ! service->gimp->restored)
-    return TRUE;
+    {
+      if (! service->timeout_source)
+        {
+          /* We are probably starting the program. No need to spam GIMP with
+           * an idle handler (which might make GIMP slower to start even
+           * with low priority).
+           * Instead let's add a timeout of half a second.
+           */
+          service->source = g_timeout_source_new (500);
+          service->timeout_source = TRUE;
+
+          g_source_set_priority (service->source, G_PRIORITY_LOW);
+          g_source_set_callback (service->source,
+                                 (GSourceFunc) gimp_dbus_service_process_idle,
+                                 service,
+                                 NULL);
+          g_source_attach (service->source, NULL);
+          g_source_unref (service->source);
+
+          return G_SOURCE_REMOVE;
+        }
+      else
+        {
+          return G_SOURCE_CONTINUE;
+        }
+    }
 
-  data = g_queue_pop_tail (service->queue);
+  /* Process data as a FIFO. */
+  data = g_queue_pop_head (service->queue);
 
   if (data)
     {
@@ -349,12 +378,33 @@ gimp_dbus_service_process_idle (GimpDBusService *service)
 
       gimp_dbus_service_idle_data_free (data);
 
-      return TRUE;
+      if (service->timeout_source)
+        {
+          /* Now GIMP is fully functional and can respond quickly to
+           * DBus calls. Switch to a usual idle source.
+           */
+          service->source = g_idle_source_new ();
+          service->timeout_source = FALSE;
+
+          g_source_set_priority (service->source, G_PRIORITY_LOW);
+          g_source_set_callback (service->source,
+                                 (GSourceFunc) gimp_dbus_service_process_idle,
+                                 service,
+                                 NULL);
+          g_source_attach (service->source, NULL);
+          g_source_unref (service->source);
+
+          return G_SOURCE_REMOVE;
+        }
+      else
+        {
+          return G_SOURCE_CONTINUE;
+        }
     }
 
   service->source = NULL;
 
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
 static IdleData *
diff --git a/app/gui/gimpdbusservice.h b/app/gui/gimpdbusservice.h
index 0b5f43387e..71f149347b 100644
--- a/app/gui/gimpdbusservice.h
+++ b/app/gui/gimpdbusservice.h
@@ -49,9 +49,10 @@ struct _GimpDBusService
 {
   GimpDBusServiceUISkeleton  parent_instance;
 
-  Gimp    *gimp;
-  GQueue  *queue;
-  GSource *source;
+  Gimp     *gimp;
+  GQueue   *queue;
+  GSource  *source;
+  gboolean  timeout_source;
 };
 
 struct _GimpDBusServiceClass


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