[gimp] app: throttle statusbar progress updates



commit bea27171ad56179e89fbace3ef1614e35565cbb1
Author: Ell <ell_se yahoo com>
Date:   Sat Apr 7 11:46:58 2018 -0400

    app: throttle statusbar progress updates
    
    Statusbar progress updates that update the progress bar are
    relatively expensive, slowing down operations that report progress
    frequently.  Only update the progress bar if a certain amount of
    time has passed since the last update, to counter that.

 app/display/gimpstatusbar.c |   71 ++++++++++++++++++++++++++++--------------
 app/display/gimpstatusbar.h |    1 +
 2 files changed, 48 insertions(+), 24 deletions(-)
---
diff --git a/app/display/gimpstatusbar.c b/app/display/gimpstatusbar.c
index 224ebee..49684d2 100644
--- a/app/display/gimpstatusbar.c
+++ b/app/display/gimpstatusbar.c
@@ -46,17 +46,20 @@
 #include "gimp-intl.h"
 
 
-/*  maximal width of the string holding the cursor-coordinates  */
-#define CURSOR_LEN        256
+/*  maximal width of the string holding the cursor-coordinates   */
+#define CURSOR_LEN                      256
 
-/*  the spacing of the hbox                                     */
-#define HBOX_SPACING        1
+/*  the spacing of the hbox                                      */
+#define HBOX_SPACING                      1
 
-/*  spacing between the icon and the statusbar label            */
-#define ICON_SPACING        2
+/*  spacing between the icon and the statusbar label             */
+#define ICON_SPACING                      2
 
-/*  timeout (in milliseconds) for temporary statusbar messages  */
-#define MESSAGE_TIMEOUT  8000
+/*  timeout (in milliseconds) for temporary statusbar messages   */
+#define MESSAGE_TIMEOUT                8000
+
+/*  minimal interval (in microseconds) between progress updates  */
+#define MIN_PROGRESS_UPDATE_INTERVAL  50000
 
 
 typedef struct _GimpStatusbarMsg GimpStatusbarMsg;
@@ -442,8 +445,9 @@ gimp_statusbar_progress_start (GimpProgress *progress,
       GtkWidget     *bar = statusbar->progressbar;
       GtkAllocation  allocation;
 
-      statusbar->progress_active = TRUE;
-      statusbar->progress_value  = 0.0;
+      statusbar->progress_active           = TRUE;
+      statusbar->progress_value            = 0.0;
+      statusbar->progress_last_update_time = g_get_monotonic_time ();
 
       gimp_statusbar_push (statusbar, "progress", NULL, "%s", message);
       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), 0.0);
@@ -557,21 +561,32 @@ gimp_statusbar_progress_set_value (GimpProgress *progress,
 
   if (statusbar->progress_active)
     {
-      GtkWidget     *bar = statusbar->progressbar;
-      GtkAllocation  allocation;
+      guint64 time = g_get_monotonic_time ();
+
+      if (time - statusbar->progress_last_update_time >=
+          MIN_PROGRESS_UPDATE_INTERVAL)
+        {
+          GtkWidget     *bar = statusbar->progressbar;
+          GtkAllocation  allocation;
+          gdouble        diff;
 
-      gtk_widget_get_allocation (bar, &allocation);
+          gtk_widget_get_allocation (bar, &allocation);
 
-      statusbar->progress_value = percentage;
+          statusbar->progress_value = percentage;
 
-      /* only update the progress bar if this causes a visible change */
-      if (fabs (allocation.width *
-                (percentage -
-                 gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (bar)))) > 1.0)
-        {
-          gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar), percentage);
+          diff = fabs (percentage -
+                       gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (bar)));
 
-          gimp_widget_flush_expose (bar);
+          /* only update the progress bar if this causes a visible change */
+          if (allocation.width * diff >= 1.0)
+            {
+              statusbar->progress_last_update_time = time;
+
+              gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (bar),
+                                             percentage);
+
+              gimp_widget_flush_expose (bar);
+            }
         }
     }
 }
@@ -594,11 +609,19 @@ gimp_statusbar_progress_pulse (GimpProgress *progress)
 
   if (statusbar->progress_active)
     {
-      GtkWidget *bar = statusbar->progressbar;
+      guint64 time = g_get_monotonic_time ();
 
-      gtk_progress_bar_pulse (GTK_PROGRESS_BAR (bar));
+      if (time - statusbar->progress_last_update_time >=
+          MIN_PROGRESS_UPDATE_INTERVAL)
+        {
+          GtkWidget *bar = statusbar->progressbar;
 
-      gimp_widget_flush_expose (bar);
+          statusbar->progress_last_update_time = time;
+
+          gtk_progress_bar_pulse (GTK_PROGRESS_BAR (bar));
+
+          gimp_widget_flush_expose (bar);
+        }
     }
 }
 
diff --git a/app/display/gimpstatusbar.h b/app/display/gimpstatusbar.h
index f6fe695..4e7a807 100644
--- a/app/display/gimpstatusbar.h
+++ b/app/display/gimpstatusbar.h
@@ -68,6 +68,7 @@ struct _GimpStatusbar
   gboolean             progress_active;
   gboolean             progress_shown;
   gdouble              progress_value;
+  guint64              progress_last_update_time;
 };
 
 struct _GimpStatusbarClass


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