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
36 GdkFrameHistory *history;
38 /* timer_base is used to avoid ever going backward */
41 guint64 min_next_frame_time;
47 GdkFrameClockPhase requested;
48 GdkFrameClockPhase phase;
50 guint in_paint_idle : 1;
53 static gboolean gdk_frame_clock_flush_idle (void *data);
54 static gboolean gdk_frame_clock_paint_idle (void *data);
56 static void gdk_frame_clock_idle_finalize (GObject *object);
57 static void gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface);
59 G_DEFINE_TYPE_WITH_CODE (GdkFrameClockIdle, gdk_frame_clock_idle, G_TYPE_OBJECT,
60 G_IMPLEMENT_INTERFACE (GDK_TYPE_FRAME_CLOCK,
61 gdk_frame_clock_idle_interface_init))
64 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
66 GObjectClass *gobject_class = (GObjectClass*) klass;
68 gobject_class->finalize = gdk_frame_clock_idle_finalize;
70 g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
74 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
76 GdkFrameClockIdlePrivate *priv;
78 frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
79 GDK_TYPE_FRAME_CLOCK_IDLE,
80 GdkFrameClockIdlePrivate);
81 priv = frame_clock_idle->priv;
83 priv->history = gdk_frame_history_new ();
84 priv->timer = g_timer_new ();
85 priv->freeze_count = 0;
89 gdk_frame_clock_idle_finalize (GObject *object)
91 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
93 g_timer_destroy (priv->timer);
95 G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
99 compute_frame_time (GdkFrameClockIdle *idle)
101 GdkFrameClockIdlePrivate *priv = idle->priv;
102 guint64 computed_frame_time;
105 elapsed = g_get_monotonic_time () + priv->timer_base;
106 if (elapsed < priv->frame_time)
108 /* clock went backward. adapt to that by forevermore increasing
109 * timer_base. For now, assume we've gone forward in time 1ms.
111 /* hmm. just fix GTimer? */
112 computed_frame_time = priv->frame_time + 1;
113 priv->timer_base += (priv->frame_time - elapsed) + 1;
117 computed_frame_time = elapsed;
120 return computed_frame_time;
124 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
126 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
127 guint64 computed_frame_time;
129 /* can't change frame time during a paint */
130 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
131 priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
132 return priv->frame_time;
134 /* Outside a paint, pick something close to "now" */
135 computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
137 /* 16ms is 60fps. We only update frame time that often because we'd
138 * like to try to keep animations on the same start times.
139 * get_frame_time() would normally be used outside of a paint to
140 * record an animation start time for example.
142 if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
143 priv->frame_time = computed_frame_time;
145 return priv->frame_time;
149 maybe_start_idle (GdkFrameClockIdle *clock_idle)
151 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
153 if (priv->freeze_count == 0)
155 guint min_interval = 0;
157 if (priv->min_next_frame_time != 0)
159 guint64 now = compute_frame_time (clock_idle);
160 guint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
161 min_interval = (min_interval_us + 500) / 1000;
164 if (priv->flush_idle_id == 0 &&
165 (priv->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
167 priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
169 gdk_frame_clock_flush_idle,
170 g_object_ref (clock_idle),
171 (GDestroyNotify) g_object_unref);
174 if (priv->paint_idle_id == 0 &&
175 !priv->in_paint_idle &&
176 (priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
178 priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
180 gdk_frame_clock_paint_idle,
181 g_object_ref (clock_idle),
182 (GDestroyNotify) g_object_unref);
184 gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
190 gdk_frame_clock_flush_idle (void *data)
192 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
193 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
194 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
196 priv->flush_idle_id = 0;
198 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
201 priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
202 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
204 g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
206 if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
207 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
209 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
215 gdk_frame_clock_paint_idle (void *data)
217 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
218 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
219 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
220 gboolean skip_to_resume_events;
222 priv->paint_idle_id = 0;
223 priv->in_paint_idle = TRUE;
225 skip_to_resume_events =
226 (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0;
228 if (!skip_to_resume_events)
232 case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
234 case GDK_FRAME_CLOCK_PHASE_NONE:
235 case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
236 if (priv->freeze_count == 0)
238 GdkFrameTimings *timings;
239 gint64 frame_counter;
241 priv->frame_time = compute_frame_time (clock_idle);
242 gdk_frame_history_begin_frame (priv->history);
243 frame_counter = gdk_frame_history_get_frame_counter (priv->history);
244 timings = gdk_frame_history_get_timings (priv->history, frame_counter);
245 gdk_frame_timings_set_frame_time (timings, priv->frame_time);
247 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
249 /* We always emit ::before-paint and ::after-paint if
250 * any of the intermediate phases are requested and
251 * they don't get repeated if you freeze/thaw while
253 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
254 g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
255 priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
257 case GDK_FRAME_CLOCK_PHASE_UPDATE:
258 if (priv->freeze_count == 0)
260 if (priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE)
262 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
263 g_signal_emit_by_name (G_OBJECT (clock), "update");
266 case GDK_FRAME_CLOCK_PHASE_LAYOUT:
267 if (priv->freeze_count == 0)
269 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
270 if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
272 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
273 g_signal_emit_by_name (G_OBJECT (clock), "layout");
276 case GDK_FRAME_CLOCK_PHASE_PAINT:
277 if (priv->freeze_count == 0)
279 priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
280 if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
282 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
283 g_signal_emit_by_name (G_OBJECT (clock), "paint");
286 case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
287 if (priv->freeze_count == 0)
289 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
290 g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
291 /* the ::after-paint phase doesn't get repeated on freeze/thaw,
293 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
295 case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
300 if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
302 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
303 g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
306 if (priv->freeze_count == 0)
307 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
309 priv->in_paint_idle = FALSE;
311 if (priv->freeze_count == 0 && priv->requested != 0)
313 /* We need to start over again immediately - this implies that there is no
314 * throttling at the backend layer, so we need to back-off ourselves.
317 priv->min_next_frame_time = priv->frame_time + FRAME_INTERVAL;
318 maybe_start_idle (clock_idle);
322 priv->min_next_frame_time = 0;
329 gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
330 GdkFrameClockPhase phase)
332 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
333 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
335 priv->requested |= phase;
336 maybe_start_idle (clock_idle);
339 static GdkFrameClockPhase
340 gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
342 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
344 return priv->requested;
348 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
350 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
352 priv->freeze_count++;
354 if (priv->freeze_count == 1)
356 if (priv->flush_idle_id)
358 g_source_remove (priv->flush_idle_id);
359 priv->flush_idle_id = 0;
361 if (priv->paint_idle_id)
363 g_source_remove (priv->paint_idle_id);
364 priv->paint_idle_id = 0;
370 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
372 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
373 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
375 g_return_if_fail (priv->freeze_count > 0);
377 priv->freeze_count--;
378 if (priv->freeze_count == 0)
380 maybe_start_idle (clock_idle);
381 /* If nothing is requested so we didn't start an idle, we need
382 * to skip to the end of the state chain, since the idle won't
383 * run and do it for us. */
384 if (priv->paint_idle_id == 0)
385 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
389 static GdkFrameHistory *
390 gdk_frame_clock_idle_get_history (GdkFrameClock *clock)
392 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
393 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
395 return priv->history;
399 gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
401 iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
402 iface->request_phase = gdk_frame_clock_idle_request_phase;
403 iface->get_requested = gdk_frame_clock_idle_get_requested;
404 iface->freeze = gdk_frame_clock_idle_freeze;
405 iface->thaw = gdk_frame_clock_idle_thaw;
406 iface->get_history = gdk_frame_clock_idle_get_history;
410 _gdk_frame_clock_idle_new (void)
412 GdkFrameClockIdle *clock;
414 clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
416 return GDK_FRAME_CLOCK (clock);