]> Pileus Git - ~andy/gtk/blobdiff - gdk/gdkframeclockidle.c
gdkframeclockidle: Don't expose sleep_source source funcs
[~andy/gtk] / gdk / gdkframeclockidle.c
index 37a6733620f28c8f32633678bc6a3f0dd8e2cfb7..752e72c13fe6946c53ae81ae7998184f7863336e 100644 (file)
@@ -26,6 +26,8 @@
 
 #include "config.h"
 
+#include "gdkinternals.h"
+#include "gdkframeclockprivate.h"
 #include "gdkframeclockidle.h"
 #include "gdk.h"
 
 
 struct _GdkFrameClockIdlePrivate
 {
-  GdkFrameHistory *history;
   GTimer *timer;
   /* timer_base is used to avoid ever going backward */
-  guint64 timer_base;
-  guint64 frame_time;
-  guint64 min_next_frame_time;
+  gint64 timer_base;
+  gint64 frame_time;
+  gint64 min_next_frame_time;
+  gint64 sleep_serial;
 
   guint flush_idle_id;
   guint paint_idle_id;
@@ -54,20 +56,59 @@ static gboolean gdk_frame_clock_flush_idle (void *data);
 static gboolean gdk_frame_clock_paint_idle (void *data);
 
 static void gdk_frame_clock_idle_finalize             (GObject                *object);
-static void gdk_frame_clock_idle_interface_init       (GdkFrameClockInterface *iface);
 
-G_DEFINE_TYPE_WITH_CODE (GdkFrameClockIdle, gdk_frame_clock_idle, G_TYPE_OBJECT,
-                        G_IMPLEMENT_INTERFACE (GDK_TYPE_FRAME_CLOCK,
-                                               gdk_frame_clock_idle_interface_init))
+G_DEFINE_TYPE (GdkFrameClockIdle, gdk_frame_clock_idle, GDK_TYPE_FRAME_CLOCK)
 
-static void
-gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
+static gint64 sleep_serial;
+static gint64 sleep_source_prepare_time;
+static GSource *sleep_source;
+
+static gboolean
+sleep_source_prepare (GSource *source,
+                      gint    *timeout)
 {
-  GObjectClass *gobject_class = (GObjectClass*) klass;
+  sleep_source_prepare_time = g_source_get_time (source);
+  *timeout = -1;
+  return FALSE;
+}
 
-  gobject_class->finalize     = gdk_frame_clock_idle_finalize;
+static gboolean
+sleep_source_check (GSource *source)
+{
+  if (g_source_get_time (source) != sleep_source_prepare_time)
+    sleep_serial++;
 
-  g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
+  return FALSE;
+}
+
+static gboolean
+sleep_source_dispatch (GSource     *source,
+                       GSourceFunc  callback,
+                       gpointer     user_data)
+{
+  return TRUE;
+}
+
+static GSourceFuncs sleep_source_funcs = {
+  sleep_source_prepare,
+  sleep_source_check,
+  sleep_source_dispatch,
+  NULL /* finalize */
+};
+
+static gint64
+get_sleep_serial (void)
+{
+  if (sleep_source == NULL)
+    {
+      sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));
+
+      g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
+      g_source_attach (sleep_source, NULL);
+      g_source_unref (sleep_source);
+    }
+
+  return sleep_serial;
 }
 
 static void
@@ -80,7 +121,6 @@ gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
                                                         GdkFrameClockIdlePrivate);
   priv = frame_clock_idle->priv;
 
-  priv->history = gdk_frame_history_new ();
   priv->timer = g_timer_new ();
   priv->freeze_count = 0;
 }
@@ -95,12 +135,12 @@ gdk_frame_clock_idle_finalize (GObject *object)
   G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
 }
 
-static guint64
+static gint64
 compute_frame_time (GdkFrameClockIdle *idle)
 {
   GdkFrameClockIdlePrivate *priv = idle->priv;
-  guint64 computed_frame_time;
-  guint64 elapsed;
+  gint64 computed_frame_time;
+  gint64 elapsed;
 
   elapsed = g_get_monotonic_time () + priv->timer_base;
   if (elapsed < priv->frame_time)
@@ -120,11 +160,11 @@ compute_frame_time (GdkFrameClockIdle *idle)
   return computed_frame_time;
 }
 
-static guint64
+static gint64
 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
 {
   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
-  guint64 computed_frame_time;
+  gint64 computed_frame_time;
 
   /* can't change frame time during a paint */
   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
@@ -150,14 +190,14 @@ maybe_start_idle (GdkFrameClockIdle *clock_idle)
 {
   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
 
-  if (priv->freeze_count == 0)
+  if (priv->freeze_count == 0 && priv->requested != 0)
     {
       guint min_interval = 0;
 
       if (priv->min_next_frame_time != 0)
         {
-          guint64 now = compute_frame_time (clock_idle);
-          guint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
+          gint64 now = compute_frame_time (clock_idle);
+          gint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
           min_interval = (min_interval_us + 500) / 1000;
         }
 
@@ -180,12 +220,27 @@ maybe_start_idle (GdkFrameClockIdle *clock_idle)
                                                               gdk_frame_clock_paint_idle,
                                                               g_object_ref (clock_idle),
                                                               (GDestroyNotify) g_object_unref);
-
-          gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
         }
     }
 }
 
+static gint64
+compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
+                             gint64             last_frame_time)
+{
+  gint64 presentation_time;
+  gint64 refresh_interval;
+
+  gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
+                                    last_frame_time,
+                                    &refresh_interval, &presentation_time);
+
+  if (presentation_time == 0)
+    return last_frame_time + refresh_interval;
+  else
+    return presentation_time + refresh_interval / 2;
+}
+
 static gboolean
 gdk_frame_clock_flush_idle (void *data)
 {
@@ -218,13 +273,20 @@ gdk_frame_clock_paint_idle (void *data)
   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
   gboolean skip_to_resume_events;
+  GdkFrameTimings *timings = NULL;
 
   priv->paint_idle_id = 0;
   priv->in_paint_idle = TRUE;
+  priv->min_next_frame_time = 0;
 
   skip_to_resume_events =
     (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0;
 
+  if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
+    {
+      timings = gdk_frame_clock_get_current_timings (clock);
+    }
+
   if (!skip_to_resume_events)
     {
       switch (priv->phase)
@@ -235,14 +297,13 @@ gdk_frame_clock_paint_idle (void *data)
         case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
           if (priv->freeze_count == 0)
             {
-              GdkFrameTimings *timings;
-              gint64 frame_counter;
-
               priv->frame_time = compute_frame_time (clock_idle);
-              gdk_frame_history_begin_frame (priv->history);
-              frame_counter = gdk_frame_history_get_frame_counter (priv->history);
-              timings = gdk_frame_history_get_timings (priv->history, frame_counter);
-              gdk_frame_timings_set_frame_time (timings, priv->frame_time);
+
+              _gdk_frame_clock_begin_frame (clock);
+              timings = gdk_frame_clock_get_current_timings (clock);
+
+              timings->frame_time = priv->frame_time;
+              timings->slept_before = priv->sleep_serial != get_sleep_serial ();
 
               priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
 
@@ -266,6 +327,15 @@ gdk_frame_clock_paint_idle (void *data)
         case GDK_FRAME_CLOCK_PHASE_LAYOUT:
           if (priv->freeze_count == 0)
             {
+#ifdef G_ENABLE_DEBUG
+              if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
+                {
+                  if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
+                      (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
+                    timings->layout_start_time = g_get_monotonic_time ();
+                }
+#endif /* G_ENABLE_DEBUG */
+
               priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
               if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
                 {
@@ -276,6 +346,15 @@ gdk_frame_clock_paint_idle (void *data)
         case GDK_FRAME_CLOCK_PHASE_PAINT:
           if (priv->freeze_count == 0)
             {
+#ifdef G_ENABLE_DEBUG
+              if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
+                {
+                  if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
+                      (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
+                    timings->paint_start_time = g_get_monotonic_time ();
+                }
+#endif /* G_ENABLE_DEBUG */
+
               priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
               if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
                 {
@@ -291,12 +370,25 @@ gdk_frame_clock_paint_idle (void *data)
               /* the ::after-paint phase doesn't get repeated on freeze/thaw,
                */
               priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
+
+#ifdef G_ENABLE_DEBUG
+              if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
+                timings->frame_end_time = g_get_monotonic_time ();
+#endif /* G_ENABLE_DEBUG */
             }
         case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
           ;
         }
     }
 
+#ifdef G_ENABLE_DEBUG
+  if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
+    {
+      if (timings->complete)
+        _gdk_frame_clock_debug_print_timings (clock, timings);
+    }
+#endif /* G_ENABLE_DEBUG */
+
   if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
     {
       priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
@@ -308,19 +400,19 @@ gdk_frame_clock_paint_idle (void *data)
 
   priv->in_paint_idle = FALSE;
 
-  if (priv->freeze_count == 0 && priv->requested != 0)
+  /* If there is throttling in the backend layer, then we'll do another
+   * update as soon as the backend unthrottles (if there is work to do),
+   * otherwise we need to figure when the next frame should be.
+   */
+  if (priv->freeze_count == 0)
     {
-      /* We need to start over again immediately - this implies that there is no
-       * throttling at the backend layer, so we need to back-off ourselves.
-       */
-      gdk_flush ();
-      priv->min_next_frame_time = priv->frame_time + FRAME_INTERVAL;
+      priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
+                                                               priv->frame_time);
       maybe_start_idle (clock_idle);
     }
-  else
-    {
-      priv->min_next_frame_time = 0;
-    }
+
+  if (priv->freeze_count == 0)
+    priv->sleep_serial = get_sleep_serial ();
 
   return FALSE;
 }
@@ -336,14 +428,6 @@ gdk_frame_clock_idle_request_phase (GdkFrameClock      *clock,
   maybe_start_idle (clock_idle);
 }
 
-static GdkFrameClockPhase
-gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
-{
-  GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
-
-  return priv->requested;
-}
-
 static void
 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
 {
@@ -383,27 +467,25 @@ gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
        * run and do it for us. */
       if (priv->paint_idle_id == 0)
         priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
+
+      priv->sleep_serial = get_sleep_serial ();
     }
 }
 
-static GdkFrameHistory *
-gdk_frame_clock_idle_get_history (GdkFrameClock *clock)
+static void
+gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
 {
-  GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
-  GdkFrameClockIdlePrivate *priv = clock_idle->priv;
+  GObjectClass *gobject_class = (GObjectClass*) klass;
+  GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
 
-  return priv->history;
-}
+  gobject_class->finalize     = gdk_frame_clock_idle_finalize;
 
-static void
-gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
-{
-  iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
-  iface->request_phase = gdk_frame_clock_idle_request_phase;
-  iface->get_requested = gdk_frame_clock_idle_get_requested;
-  iface->freeze = gdk_frame_clock_idle_freeze;
-  iface->thaw = gdk_frame_clock_idle_thaw;
-  iface->get_history = gdk_frame_clock_idle_get_history;
+  frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
+  frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
+  frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
+  frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
+
+  g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
 }
 
 GdkFrameClock *