1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2010. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdkframeclockidle.h"
32 #define FRAME_INTERVAL 16667 // microseconds
34 struct _GdkFrameClockIdlePrivate
37 /* timer_base is used to avoid ever going backward */
40 guint64 min_next_frame_time;
45 GdkFrameClockPhase requested;
46 GdkFrameClockPhase phase;
49 static gboolean gdk_frame_clock_paint_idle (void *data);
51 static void gdk_frame_clock_idle_finalize (GObject *object);
52 static void gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface);
54 G_DEFINE_TYPE_WITH_CODE (GdkFrameClockIdle, gdk_frame_clock_idle, G_TYPE_OBJECT,
55 G_IMPLEMENT_INTERFACE (GDK_TYPE_FRAME_CLOCK,
56 gdk_frame_clock_idle_interface_init))
59 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
61 GObjectClass *gobject_class = (GObjectClass*) klass;
63 gobject_class->finalize = gdk_frame_clock_idle_finalize;
65 g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
69 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
71 GdkFrameClockIdlePrivate *priv;
73 frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
74 GDK_TYPE_FRAME_CLOCK_IDLE,
75 GdkFrameClockIdlePrivate);
76 priv = frame_clock_idle->priv;
78 priv->timer = g_timer_new ();
79 priv->freeze_count = 0;
83 gdk_frame_clock_idle_finalize (GObject *object)
85 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
87 g_timer_destroy (priv->timer);
89 G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
93 compute_frame_time (GdkFrameClockIdle *idle)
95 GdkFrameClockIdlePrivate *priv = idle->priv;
96 guint64 computed_frame_time;
99 elapsed = g_get_monotonic_time () + priv->timer_base;
100 if (elapsed < priv->frame_time)
102 /* clock went backward. adapt to that by forevermore increasing
103 * timer_base. For now, assume we've gone forward in time 1ms.
105 /* hmm. just fix GTimer? */
106 computed_frame_time = priv->frame_time + 1;
107 priv->timer_base += (priv->frame_time - elapsed) + 1;
111 computed_frame_time = elapsed;
114 return computed_frame_time;
118 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
120 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
121 guint64 computed_frame_time;
123 /* can't change frame time during a paint */
124 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
125 priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
126 return priv->frame_time;
128 /* Outside a paint, pick something close to "now" */
129 computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
131 /* 16ms is 60fps. We only update frame time that often because we'd
132 * like to try to keep animations on the same start times.
133 * get_frame_time() would normally be used outside of a paint to
134 * record an animation start time for example.
136 if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
137 priv->frame_time = computed_frame_time;
139 return priv->frame_time;
143 maybe_start_idle (GdkFrameClockIdle *clock_idle)
145 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
147 if (priv->idle_id == 0 && priv->freeze_count == 0 && priv->requested != 0)
149 guint min_interval = 0;
151 if (priv->min_next_frame_time != 0)
153 guint64 now = compute_frame_time (clock_idle);
154 guint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
155 min_interval = (min_interval_us + 500) / 1000;
158 priv->idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
160 gdk_frame_clock_paint_idle,
161 g_object_ref (clock_idle),
162 (GDestroyNotify) g_object_unref);
164 gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
169 gdk_frame_clock_paint_idle (void *data)
171 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
172 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
173 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
179 case GDK_FRAME_CLOCK_PHASE_NONE:
180 case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
181 if (priv->freeze_count == 0)
183 priv->frame_time = compute_frame_time (clock_idle);
185 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
186 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
187 /* We always emit ::before-paint and ::after-paint even if
188 * not explicitly requested, and unlike other phases,
189 * they don't get repeated if you freeze/thaw while
191 g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
192 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
194 case GDK_FRAME_CLOCK_PHASE_LAYOUT:
195 if (priv->freeze_count == 0)
197 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
198 if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
200 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
201 g_signal_emit_by_name (G_OBJECT (clock), "layout");
204 case GDK_FRAME_CLOCK_PHASE_PAINT:
205 if (priv->freeze_count == 0)
207 priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
208 if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
210 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
211 g_signal_emit_by_name (G_OBJECT (clock), "paint");
214 case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
215 if (priv->freeze_count == 0)
217 priv->phase = GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
218 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
219 g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
220 /* the ::after-paint phase doesn't get repeated on freeze/thaw */
221 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
225 if (priv->freeze_count == 0 && priv->requested != 0)
227 /* We need to start over again immediately - this implies that there is no
228 * throttling at the backend layer, so we need to back-off ourselves.
231 priv->min_next_frame_time = priv->frame_time + FRAME_INTERVAL;
232 maybe_start_idle (clock_idle);
236 priv->min_next_frame_time = 0;
243 gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
244 GdkFrameClockPhase phase)
246 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
247 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
249 priv->requested |= phase;
250 maybe_start_idle (clock_idle);
253 static GdkFrameClockPhase
254 gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
256 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
258 return priv->requested;
262 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
264 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
266 priv->freeze_count++;
268 if (priv->freeze_count == 1)
272 g_source_remove (priv->idle_id);
279 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
281 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
282 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
284 g_return_if_fail (priv->freeze_count > 0);
286 priv->freeze_count--;
287 if (priv->freeze_count == 0)
288 maybe_start_idle (clock_idle);
292 gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
294 iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
295 iface->request_phase = gdk_frame_clock_idle_request_phase;
296 iface->get_requested = gdk_frame_clock_idle_get_requested;
297 iface->freeze = gdk_frame_clock_idle_freeze;
298 iface->thaw = gdk_frame_clock_idle_thaw;
302 _gdk_frame_clock_idle_new (void)
304 GdkFrameClockIdle *clock;
306 clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
308 return GDK_FRAME_CLOCK (clock);