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 "gdkinternals.h"
30 #include "gdkframeclockprivate.h"
31 #include "gdkframeclockidle.h"
34 #define FRAME_INTERVAL 16667 // microseconds
36 struct _GdkFrameClockIdlePrivate
39 /* timer_base is used to avoid ever going backward */
42 gint64 min_next_frame_time;
49 GdkFrameClockPhase requested;
50 GdkFrameClockPhase phase;
52 guint in_paint_idle : 1;
55 static gboolean gdk_frame_clock_flush_idle (void *data);
56 static gboolean gdk_frame_clock_paint_idle (void *data);
58 G_DEFINE_TYPE (GdkFrameClockIdle, gdk_frame_clock_idle, GDK_TYPE_FRAME_CLOCK)
60 static gint64 sleep_serial;
61 static gint64 sleep_source_prepare_time;
62 static GSource *sleep_source;
65 sleep_source_prepare (GSource *source,
68 sleep_source_prepare_time = g_source_get_time (source);
74 sleep_source_check (GSource *source)
76 if (g_source_get_time (source) != sleep_source_prepare_time)
83 sleep_source_dispatch (GSource *source,
90 static GSourceFuncs sleep_source_funcs = {
93 sleep_source_dispatch,
98 get_sleep_serial (void)
100 if (sleep_source == NULL)
102 sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));
104 g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
105 g_source_attach (sleep_source, NULL);
106 g_source_unref (sleep_source);
113 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
115 GdkFrameClockIdlePrivate *priv;
117 frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
118 GDK_TYPE_FRAME_CLOCK_IDLE,
119 GdkFrameClockIdlePrivate);
120 priv = frame_clock_idle->priv;
122 priv->freeze_count = 0;
126 gdk_frame_clock_idle_dispose (GObject *object)
128 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
130 if (priv->flush_idle_id != 0)
132 g_source_remove (priv->flush_idle_id);
133 priv->flush_idle_id = 0;
136 if (priv->paint_idle_id != 0)
138 g_source_remove (priv->paint_idle_id);
139 priv->paint_idle_id = 0;
142 G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->dispose (object);
146 compute_frame_time (GdkFrameClockIdle *idle)
148 GdkFrameClockIdlePrivate *priv = idle->priv;
149 gint64 computed_frame_time;
152 elapsed = g_get_monotonic_time () + priv->timer_base;
153 if (elapsed < priv->frame_time)
155 /* clock went backward. adapt to that by forevermore increasing
156 * timer_base. For now, assume we've gone forward in time 1ms.
158 /* hmm. just fix GTimer? */
159 computed_frame_time = priv->frame_time + 1;
160 priv->timer_base += (priv->frame_time - elapsed) + 1;
164 computed_frame_time = elapsed;
167 return computed_frame_time;
171 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
173 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
174 gint64 computed_frame_time;
176 /* can't change frame time during a paint */
177 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
178 priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
179 return priv->frame_time;
181 /* Outside a paint, pick something close to "now" */
182 computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
184 /* 16ms is 60fps. We only update frame time that often because we'd
185 * like to try to keep animations on the same start times.
186 * get_frame_time() would normally be used outside of a paint to
187 * record an animation start time for example.
189 if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
190 priv->frame_time = computed_frame_time;
192 return priv->frame_time;
196 maybe_start_idle (GdkFrameClockIdle *clock_idle)
198 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
200 if (priv->freeze_count == 0 && priv->requested != 0)
202 guint min_interval = 0;
204 if (priv->min_next_frame_time != 0)
206 gint64 now = compute_frame_time (clock_idle);
207 gint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
208 min_interval = (min_interval_us + 500) / 1000;
211 if (priv->flush_idle_id == 0 &&
212 (priv->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
214 priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
216 gdk_frame_clock_flush_idle,
217 g_object_ref (clock_idle),
218 (GDestroyNotify) g_object_unref);
221 if (priv->paint_idle_id == 0 &&
222 !priv->in_paint_idle &&
223 (priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
225 priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
227 gdk_frame_clock_paint_idle,
228 g_object_ref (clock_idle),
229 (GDestroyNotify) g_object_unref);
235 compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
236 gint64 last_frame_time)
238 gint64 presentation_time;
239 gint64 refresh_interval;
241 gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
243 &refresh_interval, &presentation_time);
245 if (presentation_time == 0)
246 return last_frame_time + refresh_interval;
248 return presentation_time + refresh_interval / 2;
252 gdk_frame_clock_flush_idle (void *data)
254 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
255 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
256 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
258 priv->flush_idle_id = 0;
260 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
263 priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
264 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
266 g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
268 if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
269 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
271 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
277 gdk_frame_clock_paint_idle (void *data)
279 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
280 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
281 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
282 gboolean skip_to_resume_events;
283 GdkFrameTimings *timings = NULL;
285 priv->paint_idle_id = 0;
286 priv->in_paint_idle = TRUE;
287 priv->min_next_frame_time = 0;
289 skip_to_resume_events =
290 (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0;
292 if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
294 timings = gdk_frame_clock_get_current_timings (clock);
297 if (!skip_to_resume_events)
301 case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
303 case GDK_FRAME_CLOCK_PHASE_NONE:
304 case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
305 if (priv->freeze_count == 0)
307 priv->frame_time = compute_frame_time (clock_idle);
309 _gdk_frame_clock_begin_frame (clock);
310 timings = gdk_frame_clock_get_current_timings (clock);
312 timings->frame_time = priv->frame_time;
313 timings->slept_before = priv->sleep_serial != get_sleep_serial ();
315 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
317 /* We always emit ::before-paint and ::after-paint if
318 * any of the intermediate phases are requested and
319 * they don't get repeated if you freeze/thaw while
321 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
322 g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
323 priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
325 case GDK_FRAME_CLOCK_PHASE_UPDATE:
326 if (priv->freeze_count == 0)
328 if (priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE)
330 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
331 g_signal_emit_by_name (G_OBJECT (clock), "update");
334 case GDK_FRAME_CLOCK_PHASE_LAYOUT:
335 if (priv->freeze_count == 0)
337 #ifdef G_ENABLE_DEBUG
338 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
340 if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
341 (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
342 timings->layout_start_time = g_get_monotonic_time ();
344 #endif /* G_ENABLE_DEBUG */
346 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
347 if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
349 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
350 g_signal_emit_by_name (G_OBJECT (clock), "layout");
353 case GDK_FRAME_CLOCK_PHASE_PAINT:
354 if (priv->freeze_count == 0)
356 #ifdef G_ENABLE_DEBUG
357 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
359 if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
360 (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
361 timings->paint_start_time = g_get_monotonic_time ();
363 #endif /* G_ENABLE_DEBUG */
365 priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
366 if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
368 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
369 g_signal_emit_by_name (G_OBJECT (clock), "paint");
372 case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
373 if (priv->freeze_count == 0)
375 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
376 g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
377 /* the ::after-paint phase doesn't get repeated on freeze/thaw,
379 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
381 #ifdef G_ENABLE_DEBUG
382 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
383 timings->frame_end_time = g_get_monotonic_time ();
384 #endif /* G_ENABLE_DEBUG */
386 case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
391 #ifdef G_ENABLE_DEBUG
392 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
394 if (timings->complete)
395 _gdk_frame_clock_debug_print_timings (clock, timings);
397 #endif /* G_ENABLE_DEBUG */
399 if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
401 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
402 g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
405 if (priv->freeze_count == 0)
406 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
408 priv->in_paint_idle = FALSE;
410 /* If there is throttling in the backend layer, then we'll do another
411 * update as soon as the backend unthrottles (if there is work to do),
412 * otherwise we need to figure when the next frame should be.
414 if (priv->freeze_count == 0)
416 priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
418 maybe_start_idle (clock_idle);
421 if (priv->freeze_count == 0)
422 priv->sleep_serial = get_sleep_serial ();
428 gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
429 GdkFrameClockPhase phase)
431 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
432 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
434 priv->requested |= phase;
435 maybe_start_idle (clock_idle);
439 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
441 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
443 priv->freeze_count++;
445 if (priv->freeze_count == 1)
447 if (priv->flush_idle_id)
449 g_source_remove (priv->flush_idle_id);
450 priv->flush_idle_id = 0;
452 if (priv->paint_idle_id)
454 g_source_remove (priv->paint_idle_id);
455 priv->paint_idle_id = 0;
461 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
463 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
464 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
466 g_return_if_fail (priv->freeze_count > 0);
468 priv->freeze_count--;
469 if (priv->freeze_count == 0)
471 maybe_start_idle (clock_idle);
472 /* If nothing is requested so we didn't start an idle, we need
473 * to skip to the end of the state chain, since the idle won't
474 * run and do it for us. */
475 if (priv->paint_idle_id == 0)
476 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
478 priv->sleep_serial = get_sleep_serial ();
483 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
485 GObjectClass *gobject_class = (GObjectClass*) klass;
486 GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
488 gobject_class->dispose = gdk_frame_clock_idle_dispose;
490 frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
491 frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
492 frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
493 frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
495 g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
499 _gdk_frame_clock_idle_new (void)
501 GdkFrameClockIdle *clock;
503 clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
505 return GDK_FRAME_CLOCK (clock);