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"
38 #define FRAME_INTERVAL 16667 // microseconds
40 struct _GdkFrameClockIdlePrivate
43 /* timer_base is used to avoid ever going backward */
46 gint64 min_next_frame_time;
54 GdkFrameClockPhase requested;
55 GdkFrameClockPhase phase;
57 guint in_paint_idle : 1;
59 guint begin_period : 1;
63 static gboolean gdk_frame_clock_flush_idle (void *data);
64 static gboolean gdk_frame_clock_paint_idle (void *data);
66 G_DEFINE_TYPE (GdkFrameClockIdle, gdk_frame_clock_idle, GDK_TYPE_FRAME_CLOCK)
68 static gint64 sleep_serial;
69 static gint64 sleep_source_prepare_time;
70 static GSource *sleep_source;
73 sleep_source_prepare (GSource *source,
76 sleep_source_prepare_time = g_source_get_time (source);
82 sleep_source_check (GSource *source)
84 if (g_source_get_time (source) != sleep_source_prepare_time)
91 sleep_source_dispatch (GSource *source,
98 static GSourceFuncs sleep_source_funcs = {
101 sleep_source_dispatch,
106 get_sleep_serial (void)
108 if (sleep_source == NULL)
110 sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));
112 g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
113 g_source_attach (sleep_source, NULL);
114 g_source_unref (sleep_source);
121 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
123 GdkFrameClockIdlePrivate *priv;
125 frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
126 GDK_TYPE_FRAME_CLOCK_IDLE,
127 GdkFrameClockIdlePrivate);
128 priv = frame_clock_idle->priv;
130 priv->freeze_count = 0;
134 gdk_frame_clock_idle_dispose (GObject *object)
136 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
138 if (priv->flush_idle_id != 0)
140 g_source_remove (priv->flush_idle_id);
141 priv->flush_idle_id = 0;
144 if (priv->paint_idle_id != 0)
146 g_source_remove (priv->paint_idle_id);
147 priv->paint_idle_id = 0;
151 if (priv->begin_period)
154 priv->begin_period = FALSE;
158 G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->dispose (object);
162 compute_frame_time (GdkFrameClockIdle *idle)
164 GdkFrameClockIdlePrivate *priv = idle->priv;
165 gint64 computed_frame_time;
168 elapsed = g_get_monotonic_time () + priv->timer_base;
169 if (elapsed < priv->frame_time)
171 /* clock went backward. adapt to that by forevermore increasing
172 * timer_base. For now, assume we've gone forward in time 1ms.
174 /* hmm. just fix GTimer? */
175 computed_frame_time = priv->frame_time + 1;
176 priv->timer_base += (priv->frame_time - elapsed) + 1;
180 computed_frame_time = elapsed;
183 return computed_frame_time;
187 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
189 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
190 gint64 computed_frame_time;
192 /* can't change frame time during a paint */
193 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
194 priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
195 return priv->frame_time;
197 /* Outside a paint, pick something close to "now" */
198 computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
200 /* 16ms is 60fps. We only update frame time that often because we'd
201 * like to try to keep animations on the same start times.
202 * get_frame_time() would normally be used outside of a paint to
203 * record an animation start time for example.
205 if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
206 priv->frame_time = computed_frame_time;
208 return priv->frame_time;
211 #define RUN_FLUSH_IDLE(priv) \
212 ((priv)->freeze_count == 0 && \
213 ((priv)->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
215 /* The reason why we track updating_count separately here and don't
216 * just add GDK_FRAME_CLOCK_PHASE_UPDATE into ->request on every frame
217 * is so that we can avoid doing one more frame when an animation
220 #define RUN_PAINT_IDLE(priv) \
221 ((priv)->freeze_count == 0 && \
222 (((priv)->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 || \
223 (priv)->updating_count > 0))
226 maybe_start_idle (GdkFrameClockIdle *clock_idle)
228 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
230 if (RUN_FLUSH_IDLE (priv) || RUN_PAINT_IDLE (priv))
232 guint min_interval = 0;
234 if (priv->min_next_frame_time != 0)
236 gint64 now = compute_frame_time (clock_idle);
237 gint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
238 min_interval = (min_interval_us + 500) / 1000;
241 if (priv->flush_idle_id == 0 && RUN_FLUSH_IDLE (priv))
243 priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
245 gdk_frame_clock_flush_idle,
246 g_object_ref (clock_idle),
247 (GDestroyNotify) g_object_unref);
250 if (!priv->in_paint_idle &&
251 priv->paint_idle_id == 0 && RUN_PAINT_IDLE (priv))
253 priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
255 gdk_frame_clock_paint_idle,
256 g_object_ref (clock_idle),
257 (GDestroyNotify) g_object_unref);
263 maybe_stop_idle (GdkFrameClockIdle *clock_idle)
265 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
267 if (priv->flush_idle_id != 0 && !RUN_FLUSH_IDLE (priv))
269 g_source_remove (priv->flush_idle_id);
270 priv->flush_idle_id = 0;
273 if (priv->paint_idle_id != 0 && !RUN_PAINT_IDLE (priv))
275 g_source_remove (priv->paint_idle_id);
276 priv->paint_idle_id = 0;
281 compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
282 gint64 last_frame_time)
284 gint64 presentation_time;
285 gint64 refresh_interval;
287 gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
289 &refresh_interval, &presentation_time);
291 if (presentation_time == 0)
292 return last_frame_time + refresh_interval;
294 return presentation_time + refresh_interval / 2;
298 gdk_frame_clock_flush_idle (void *data)
300 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
301 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
302 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
304 priv->flush_idle_id = 0;
306 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
309 priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
310 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
312 g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
314 if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 ||
315 priv->updating_count > 0)
316 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
318 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
324 gdk_frame_clock_paint_idle (void *data)
326 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
327 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
328 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
329 gboolean skip_to_resume_events;
330 GdkFrameTimings *timings = NULL;
332 priv->paint_idle_id = 0;
333 priv->in_paint_idle = TRUE;
334 priv->min_next_frame_time = 0;
336 skip_to_resume_events =
337 (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0 &&
338 priv->updating_count == 0;
340 if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
342 timings = gdk_frame_clock_get_current_timings (clock);
345 if (!skip_to_resume_events)
349 case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
351 case GDK_FRAME_CLOCK_PHASE_NONE:
352 case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
353 if (priv->freeze_count == 0)
355 priv->frame_time = compute_frame_time (clock_idle);
357 _gdk_frame_clock_begin_frame (clock);
358 timings = gdk_frame_clock_get_current_timings (clock);
360 timings->frame_time = priv->frame_time;
361 timings->slept_before = priv->sleep_serial != get_sleep_serial ();
363 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
365 /* We always emit ::before-paint and ::after-paint if
366 * any of the intermediate phases are requested and
367 * they don't get repeated if you freeze/thaw while
369 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
370 g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
371 priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
373 case GDK_FRAME_CLOCK_PHASE_UPDATE:
374 if (priv->freeze_count == 0)
376 if ((priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE) != 0 ||
377 priv->updating_count > 0)
379 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
380 g_signal_emit_by_name (G_OBJECT (clock), "update");
383 case GDK_FRAME_CLOCK_PHASE_LAYOUT:
384 if (priv->freeze_count == 0)
386 #ifdef G_ENABLE_DEBUG
387 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
389 if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
390 (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
391 timings->layout_start_time = g_get_monotonic_time ();
393 #endif /* G_ENABLE_DEBUG */
395 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
396 if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
398 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
399 g_signal_emit_by_name (G_OBJECT (clock), "layout");
402 case GDK_FRAME_CLOCK_PHASE_PAINT:
403 if (priv->freeze_count == 0)
405 #ifdef G_ENABLE_DEBUG
406 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
408 if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
409 (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
410 timings->paint_start_time = g_get_monotonic_time ();
412 #endif /* G_ENABLE_DEBUG */
414 priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
415 if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
417 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
418 g_signal_emit_by_name (G_OBJECT (clock), "paint");
421 case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
422 if (priv->freeze_count == 0)
424 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
425 g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
426 /* the ::after-paint phase doesn't get repeated on freeze/thaw,
428 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
430 #ifdef G_ENABLE_DEBUG
431 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
432 timings->frame_end_time = g_get_monotonic_time ();
433 #endif /* G_ENABLE_DEBUG */
435 case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
440 #ifdef G_ENABLE_DEBUG
441 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
443 if (timings->complete)
444 _gdk_frame_clock_debug_print_timings (clock, timings);
446 #endif /* G_ENABLE_DEBUG */
448 if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
450 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
451 g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
454 if (priv->freeze_count == 0)
455 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
457 priv->in_paint_idle = FALSE;
459 /* If there is throttling in the backend layer, then we'll do another
460 * update as soon as the backend unthrottles (if there is work to do),
461 * otherwise we need to figure when the next frame should be.
463 if (priv->freeze_count == 0)
465 priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
467 maybe_start_idle (clock_idle);
470 if (priv->freeze_count == 0)
471 priv->sleep_serial = get_sleep_serial ();
477 gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
478 GdkFrameClockPhase phase)
480 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
481 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
483 priv->requested |= phase;
484 maybe_start_idle (clock_idle);
488 gdk_frame_clock_idle_begin_updating (GdkFrameClock *clock)
490 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
491 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
494 /* We need a higher resolution timer while doing animations */
495 if (priv->updating_count == 0 && !priv->begin_period)
498 priv->begin_period = TRUE;
502 priv->updating_count++;
503 maybe_start_idle (clock_idle);
507 gdk_frame_clock_idle_end_updating (GdkFrameClock *clock)
509 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
510 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
512 g_return_if_fail (priv->updating_count > 0);
514 priv->updating_count--;
515 maybe_stop_idle (clock_idle);
518 if (priv->updating_count == 0 && priv->begin_period)
521 priv->begin_period = FALSE;
527 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
529 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
530 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
532 priv->freeze_count++;
533 maybe_stop_idle (clock_idle);
537 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
539 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
540 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
542 g_return_if_fail (priv->freeze_count > 0);
544 priv->freeze_count--;
545 if (priv->freeze_count == 0)
547 maybe_start_idle (clock_idle);
548 /* If nothing is requested so we didn't start an idle, we need
549 * to skip to the end of the state chain, since the idle won't
550 * run and do it for us. */
551 if (priv->paint_idle_id == 0)
552 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
554 priv->sleep_serial = get_sleep_serial ();
559 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
561 GObjectClass *gobject_class = (GObjectClass*) klass;
562 GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
564 gobject_class->dispose = gdk_frame_clock_idle_dispose;
566 frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
567 frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
568 frame_clock_class->begin_updating = gdk_frame_clock_idle_begin_updating;
569 frame_clock_class->end_updating = gdk_frame_clock_idle_end_updating;
570 frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
571 frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
573 g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
577 _gdk_frame_clock_idle_new (void)
579 GdkFrameClockIdle *clock;
581 clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
583 return GDK_FRAME_CLOCK (clock);