#include "config.h"
+#include "gdkinternals.h"
+#include "gdkframeclockprivate.h"
#include "gdkframeclockidle.h"
#include "gdk.h"
guint64 timer_base;
guint64 frame_time;
guint64 min_next_frame_time;
+ gint64 sleep_serial;
- guint idle_id;
+ guint flush_idle_id;
+ guint paint_idle_id;
guint freeze_count;
GdkFrameClockPhase requested;
GdkFrameClockPhase phase;
+
+ guint in_paint_idle : 1;
};
+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;
+
+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;
+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;
+}
+
+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
{
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
- if (priv->idle_id == 0 && priv->freeze_count == 0 && priv->requested != 0)
+ if (priv->freeze_count == 0 && priv->requested != 0)
{
guint min_interval = 0;
min_interval = (min_interval_us + 500) / 1000;
}
- priv->idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
- min_interval,
- gdk_frame_clock_paint_idle,
- g_object_ref (clock_idle),
- (GDestroyNotify) g_object_unref);
+ if (priv->flush_idle_id == 0 &&
+ (priv->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
+ {
+ priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
+ min_interval,
+ gdk_frame_clock_flush_idle,
+ g_object_ref (clock_idle),
+ (GDestroyNotify) g_object_unref);
+ }
+
+ if (priv->paint_idle_id == 0 &&
+ !priv->in_paint_idle &&
+ (priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
+ {
+ priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
+ min_interval,
+ gdk_frame_clock_paint_idle,
+ g_object_ref (clock_idle),
+ (GDestroyNotify) g_object_unref);
- gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
+ g_signal_emit_by_name (clock_idle, "frame-requested");
+ }
}
}
+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)
+{
+ GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
+ GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
+ GdkFrameClockIdlePrivate *priv = clock_idle->priv;
+
+ priv->flush_idle_id = 0;
+
+ if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
+ return FALSE;
+
+ priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
+ priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
+
+ g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
+
+ if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
+ priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
+ else
+ priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
+
+ return FALSE;
+}
+
static gboolean
gdk_frame_clock_paint_idle (void *data)
{
GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
+ GdkFrameHistory *history = gdk_frame_clock_get_history (clock);
+ gboolean skip_to_resume_events;
+ GdkFrameTimings *timings = NULL;
+ gint64 frame_counter = 0;
+
+ priv->paint_idle_id = 0;
+ priv->in_paint_idle = TRUE;
+ priv->min_next_frame_time = 0;
- priv->idle_id = 0;
+ skip_to_resume_events =
+ (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0;
- switch (priv->phase)
+ if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
{
- case GDK_FRAME_CLOCK_PHASE_NONE:
- case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
- if (priv->freeze_count == 0)
- {
- priv->frame_time = compute_frame_time (clock_idle);
-
- priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
- priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
- /* We always emit ::before-paint and ::after-paint even if
- * not explicitly requested, and unlike other phases,
- * they don't get repeated if you freeze/thaw while
- * in them. */
- g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
- priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
- }
- case GDK_FRAME_CLOCK_PHASE_UPDATE:
- if (priv->freeze_count == 0)
- {
- if (priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE)
+ frame_counter = gdk_frame_history_get_frame_counter (history);
+ timings = gdk_frame_history_get_timings (history, frame_counter);
+ }
+
+ if (!skip_to_resume_events)
+ {
+ switch (priv->phase)
+ {
+ case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
+ break;
+ case GDK_FRAME_CLOCK_PHASE_NONE:
+ case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
+ if (priv->freeze_count == 0)
{
- priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
- g_signal_emit_by_name (G_OBJECT (clock), "update");
+ priv->frame_time = compute_frame_time (clock_idle);
+
+ gdk_frame_history_begin_frame (history);
+ frame_counter = gdk_frame_history_get_frame_counter (history);
+ timings = gdk_frame_history_get_timings (history, frame_counter);
+
+ gdk_frame_timings_set_frame_time (timings, priv->frame_time);
+
+ gdk_frame_timings_set_slept_before (timings,
+ priv->sleep_serial != get_sleep_serial ());
+
+ priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
+
+ /* We always emit ::before-paint and ::after-paint if
+ * any of the intermediate phases are requested and
+ * they don't get repeated if you freeze/thaw while
+ * in them. */
+ priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
+ g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
+ priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
}
- }
- case GDK_FRAME_CLOCK_PHASE_LAYOUT:
- if (priv->freeze_count == 0)
- {
- priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
- if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
+ case GDK_FRAME_CLOCK_PHASE_UPDATE:
+ if (priv->freeze_count == 0)
{
- priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
- g_signal_emit_by_name (G_OBJECT (clock), "layout");
+ if (priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE)
+ {
+ priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
+ g_signal_emit_by_name (G_OBJECT (clock), "update");
+ }
}
- }
- case GDK_FRAME_CLOCK_PHASE_PAINT:
- if (priv->freeze_count == 0)
- {
- priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
- if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
+ case GDK_FRAME_CLOCK_PHASE_LAYOUT:
+ if (priv->freeze_count == 0)
{
- priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
- g_signal_emit_by_name (G_OBJECT (clock), "paint");
+#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))
+ _gdk_frame_timings_set_layout_start_time (timings, g_get_monotonic_time ());
+ }
+#endif /* G_ENABLE_DEBUG */
+
+ priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
+ if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
+ {
+ priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
+ g_signal_emit_by_name (G_OBJECT (clock), "layout");
+ }
}
- }
- case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
- if (priv->freeze_count == 0)
- {
- priv->phase = GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
- priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
- g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
- /* the ::after-paint phase doesn't get repeated on freeze/thaw */
- priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
- }
+ 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))
+ _gdk_frame_timings_set_paint_start_time (timings, g_get_monotonic_time ());
+ }
+#endif /* G_ENABLE_DEBUG */
+
+ priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
+ if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
+ {
+ priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
+ g_signal_emit_by_name (G_OBJECT (clock), "paint");
+ }
+ }
+ case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
+ if (priv->freeze_count == 0)
+ {
+ priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
+ g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
+ /* 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)
+ _gdk_frame_timings_set_frame_end_time (timings, g_get_monotonic_time ());
+#endif /* G_ENABLE_DEBUG */
+ }
+ case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
+ ;
+ }
}
- if (priv->freeze_count == 0 && priv->requested != 0)
+#ifdef G_ENABLE_DEBUG
+ if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 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;
- maybe_start_idle (clock_idle);
+ if (gdk_frame_timings_get_complete (timings))
+ _gdk_frame_history_debug_print (history, timings);
}
- else
+#endif /* G_ENABLE_DEBUG */
+
+ if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
{
- priv->min_next_frame_time = 0;
+ priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
+ g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
}
+ if (priv->freeze_count == 0)
+ priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
+
+ priv->in_paint_idle = FALSE;
+
+ /* 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)
+ {
+ priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
+ priv->frame_time);
+ maybe_start_idle (clock_idle);
+ }
+
+ if (priv->freeze_count == 0)
+ priv->sleep_serial = get_sleep_serial ();
+
return FALSE;
}
if (priv->freeze_count == 1)
{
- if (priv->idle_id)
+ if (priv->flush_idle_id)
{
- g_source_remove (priv->idle_id);
- priv->idle_id = 0;
+ g_source_remove (priv->flush_idle_id);
+ priv->flush_idle_id = 0;
+ }
+ if (priv->paint_idle_id)
+ {
+ g_source_remove (priv->paint_idle_id);
+ priv->paint_idle_id = 0;
}
}
}
priv->freeze_count--;
if (priv->freeze_count == 0)
- maybe_start_idle (clock_idle);
+ {
+ maybe_start_idle (clock_idle);
+ /* If nothing is requested so we didn't start an idle, we need
+ * to skip to the end of the state chain, since the idle won't
+ * 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 void
-gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
+gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
{
- 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;
+ GObjectClass *gobject_class = (GObjectClass*) klass;
+ GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
+
+ gobject_class->finalize = gdk_frame_clock_idle_finalize;
+
+ 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->get_requested = gdk_frame_clock_idle_get_requested;
+ 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 *