2012-10-03 22:34:01 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Modified by the GTK+ Team and others 1997-2010. See the AUTHORS
|
|
|
|
* file for a list of people on the GTK+ Team. See the ChangeLog
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gdkframeclockidle.h"
|
|
|
|
#include "gdk.h"
|
|
|
|
|
2012-09-27 21:55:55 +00:00
|
|
|
#define FRAME_INTERVAL 16667 // microseconds
|
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
struct _GdkFrameClockIdlePrivate
|
|
|
|
{
|
|
|
|
GTimer *timer;
|
|
|
|
/* timer_base is used to avoid ever going backward */
|
|
|
|
guint64 timer_base;
|
|
|
|
guint64 frame_time;
|
2012-09-27 21:55:55 +00:00
|
|
|
guint64 min_next_frame_time;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
|
|
|
guint idle_id;
|
2012-10-03 23:42:13 +00:00
|
|
|
guint freeze_count;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
GdkFrameClockPhase requested;
|
|
|
|
GdkFrameClockPhase phase;
|
2012-10-03 22:34:01 +00:00
|
|
|
};
|
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
static gboolean gdk_frame_clock_paint_idle (void *data);
|
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
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))
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = (GObjectClass*) klass;
|
|
|
|
|
|
|
|
gobject_class->finalize = gdk_frame_clock_idle_finalize;
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv;
|
|
|
|
|
|
|
|
frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
|
|
|
|
GDK_TYPE_FRAME_CLOCK_IDLE,
|
|
|
|
GdkFrameClockIdlePrivate);
|
|
|
|
priv = frame_clock_idle->priv;
|
|
|
|
|
|
|
|
priv->timer = g_timer_new ();
|
2012-10-03 23:42:13 +00:00
|
|
|
priv->freeze_count = 0;
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
|
|
|
|
|
|
|
|
g_timer_destroy (priv->timer);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint64
|
|
|
|
compute_frame_time (GdkFrameClockIdle *idle)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = idle->priv;
|
|
|
|
guint64 computed_frame_time;
|
|
|
|
guint64 elapsed;
|
|
|
|
|
2012-09-27 21:55:55 +00:00
|
|
|
elapsed = g_get_monotonic_time () + priv->timer_base;
|
2012-10-03 22:34:01 +00:00
|
|
|
if (elapsed < priv->frame_time)
|
|
|
|
{
|
|
|
|
/* clock went backward. adapt to that by forevermore increasing
|
|
|
|
* timer_base. For now, assume we've gone forward in time 1ms.
|
|
|
|
*/
|
|
|
|
/* hmm. just fix GTimer? */
|
|
|
|
computed_frame_time = priv->frame_time + 1;
|
|
|
|
priv->timer_base += (priv->frame_time - elapsed) + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
computed_frame_time = elapsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return computed_frame_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
static guint64
|
|
|
|
gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
|
|
|
|
guint64 computed_frame_time;
|
|
|
|
|
|
|
|
/* can't change frame time during a paint */
|
2012-09-27 21:55:55 +00:00
|
|
|
if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
|
|
|
|
priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
|
2012-10-03 22:34:01 +00:00
|
|
|
return priv->frame_time;
|
|
|
|
|
|
|
|
/* Outside a paint, pick something close to "now" */
|
|
|
|
computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
|
|
|
|
|
|
|
|
/* 16ms is 60fps. We only update frame time that often because we'd
|
|
|
|
* like to try to keep animations on the same start times.
|
|
|
|
* get_frame_time() would normally be used outside of a paint to
|
|
|
|
* record an animation start time for example.
|
|
|
|
*/
|
2012-09-27 21:55:55 +00:00
|
|
|
if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
|
2012-10-03 22:34:01 +00:00
|
|
|
priv->frame_time = computed_frame_time;
|
|
|
|
|
|
|
|
return priv->frame_time;
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
static void
|
|
|
|
maybe_start_idle (GdkFrameClockIdle *clock_idle)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
2012-10-03 23:42:13 +00:00
|
|
|
if (priv->idle_id == 0 && priv->freeze_count == 0 && priv->requested != 0)
|
2012-10-03 23:38:40 +00:00
|
|
|
{
|
2012-09-27 21:55:55 +00:00
|
|
|
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;
|
|
|
|
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);
|
2012-10-03 23:38:40 +00:00
|
|
|
|
|
|
|
gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
priv->idle_id = 0;
|
|
|
|
|
2012-10-03 23:42:13 +00:00
|
|
|
switch (priv->phase)
|
|
|
|
{
|
|
|
|
case GDK_FRAME_CLOCK_PHASE_NONE:
|
|
|
|
case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
|
|
|
|
if (priv->freeze_count == 0)
|
|
|
|
{
|
2012-09-27 21:55:55 +00:00
|
|
|
priv->frame_time = compute_frame_time (clock_idle);
|
|
|
|
|
2012-10-03 23:42:13 +00:00
|
|
|
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");
|
2012-09-26 19:44:30 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
|
|
|
|
g_signal_emit_by_name (G_OBJECT (clock), "update");
|
|
|
|
}
|
2012-10-03 23:42:13 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
|
|
|
|
g_signal_emit_by_name (G_OBJECT (clock), "layout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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->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;
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:38:40 +00:00
|
|
|
|
2012-09-27 21:55:55 +00:00
|
|
|
if (priv->freeze_count == 0 && priv->requested != 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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv->min_next_frame_time = 0;
|
|
|
|
}
|
2012-10-03 22:34:01 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-10-03 23:38:40 +00:00
|
|
|
gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
|
|
|
|
GdkFrameClockPhase phase)
|
2012-10-03 22:34:01 +00:00
|
|
|
{
|
2012-10-03 23:38:40 +00:00
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
2012-10-03 22:34:01 +00:00
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
priv->requested |= phase;
|
|
|
|
maybe_start_idle (clock_idle);
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
static GdkFrameClockPhase
|
|
|
|
gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
|
2012-10-03 22:34:01 +00:00
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
|
|
|
|
|
2012-10-03 23:38:40 +00:00
|
|
|
return priv->requested;
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 23:42:13 +00:00
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
|
|
|
|
|
|
|
|
priv->freeze_count++;
|
|
|
|
|
|
|
|
if (priv->freeze_count == 1)
|
|
|
|
{
|
|
|
|
if (priv->idle_id)
|
|
|
|
{
|
|
|
|
g_source_remove (priv->idle_id);
|
|
|
|
priv->idle_id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
|
|
|
|
GdkFrameClockIdlePrivate *priv = clock_idle->priv;
|
|
|
|
|
|
|
|
g_return_if_fail (priv->freeze_count > 0);
|
|
|
|
|
|
|
|
priv->freeze_count--;
|
|
|
|
if (priv->freeze_count == 0)
|
|
|
|
maybe_start_idle (clock_idle);
|
|
|
|
}
|
|
|
|
|
2012-10-03 22:34:01 +00:00
|
|
|
static void
|
|
|
|
gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
|
|
|
|
{
|
|
|
|
iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
|
2012-10-03 23:38:40 +00:00
|
|
|
iface->request_phase = gdk_frame_clock_idle_request_phase;
|
|
|
|
iface->get_requested = gdk_frame_clock_idle_get_requested;
|
2012-10-03 23:42:13 +00:00
|
|
|
iface->freeze = gdk_frame_clock_idle_freeze;
|
|
|
|
iface->thaw = gdk_frame_clock_idle_thaw;
|
2012-10-03 23:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GdkFrameClock *
|
|
|
|
_gdk_frame_clock_idle_new (void)
|
|
|
|
{
|
|
|
|
GdkFrameClockIdle *clock;
|
|
|
|
|
|
|
|
clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
|
|
|
|
|
|
|
|
return GDK_FRAME_CLOCK (clock);
|
2012-10-03 22:34:01 +00:00
|
|
|
}
|