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
370 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
371 g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
372 priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
374 case GDK_FRAME_CLOCK_PHASE_UPDATE:
375 if (priv->freeze_count == 0)
377 if ((priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE) != 0 ||
378 priv->updating_count > 0)
380 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
381 g_signal_emit_by_name (G_OBJECT (clock), "update");
384 case GDK_FRAME_CLOCK_PHASE_LAYOUT:
385 if (priv->freeze_count == 0)
387 #ifdef G_ENABLE_DEBUG
388 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
390 if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
391 (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
392 timings->layout_start_time = g_get_monotonic_time ();
394 #endif /* G_ENABLE_DEBUG */
396 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
397 if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
399 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
400 g_signal_emit_by_name (G_OBJECT (clock), "layout");
403 case GDK_FRAME_CLOCK_PHASE_PAINT:
404 if (priv->freeze_count == 0)
406 #ifdef G_ENABLE_DEBUG
407 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
409 if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
410 (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
411 timings->paint_start_time = g_get_monotonic_time ();
413 #endif /* G_ENABLE_DEBUG */
415 priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
416 if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
418 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
419 g_signal_emit_by_name (G_OBJECT (clock), "paint");
422 case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
423 if (priv->freeze_count == 0)
425 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
426 g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
427 /* the ::after-paint phase doesn't get repeated on freeze/thaw,
429 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
431 #ifdef G_ENABLE_DEBUG
432 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
433 timings->frame_end_time = g_get_monotonic_time ();
434 #endif /* G_ENABLE_DEBUG */
436 case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
441 #ifdef G_ENABLE_DEBUG
442 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
444 if (timings->complete)
445 _gdk_frame_clock_debug_print_timings (clock, timings);
447 #endif /* G_ENABLE_DEBUG */
449 if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
451 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
452 g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
455 if (priv->freeze_count == 0)
456 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
458 priv->in_paint_idle = FALSE;
460 /* If there is throttling in the backend layer, then we'll do another
461 * update as soon as the backend unthrottles (if there is work to do),
462 * otherwise we need to figure when the next frame should be.
464 if (priv->freeze_count == 0)
466 priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
468 maybe_start_idle (clock_idle);
471 if (priv->freeze_count == 0)
472 priv->sleep_serial = get_sleep_serial ();
478 gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
479 GdkFrameClockPhase phase)
481 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
482 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
484 priv->requested |= phase;
485 maybe_start_idle (clock_idle);
489 gdk_frame_clock_idle_begin_updating (GdkFrameClock *clock)
491 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
492 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
495 /* We need a higher resolution timer while doing animations */
496 if (priv->updating_count == 0 && !priv->begin_period)
499 priv->begin_period = TRUE;
503 priv->updating_count++;
504 maybe_start_idle (clock_idle);
508 gdk_frame_clock_idle_end_updating (GdkFrameClock *clock)
510 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
511 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
513 g_return_if_fail (priv->updating_count > 0);
515 priv->updating_count--;
516 maybe_stop_idle (clock_idle);
519 if (priv->updating_count == 0 && priv->begin_period)
522 priv->begin_period = FALSE;
528 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
530 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
531 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
533 priv->freeze_count++;
534 maybe_stop_idle (clock_idle);
538 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
540 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
541 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
543 g_return_if_fail (priv->freeze_count > 0);
545 priv->freeze_count--;
546 if (priv->freeze_count == 0)
548 maybe_start_idle (clock_idle);
549 /* If nothing is requested so we didn't start an idle, we need
550 * to skip to the end of the state chain, since the idle won't
551 * run and do it for us.
553 if (priv->paint_idle_id == 0)
554 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
556 priv->sleep_serial = get_sleep_serial ();
561 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
563 GObjectClass *gobject_class = (GObjectClass*) klass;
564 GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
566 gobject_class->dispose = gdk_frame_clock_idle_dispose;
568 frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
569 frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
570 frame_clock_class->begin_updating = gdk_frame_clock_idle_begin_updating;
571 frame_clock_class->end_updating = gdk_frame_clock_idle_end_updating;
572 frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
573 frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
575 g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
579 _gdk_frame_clock_idle_new (void)
581 GdkFrameClockIdle *clock;
583 clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
585 return GDK_FRAME_CLOCK (clock);