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;
50 GdkFrameClockPhase requested;
51 GdkFrameClockPhase phase;
53 guint in_paint_idle : 1;
56 static gboolean gdk_frame_clock_flush_idle (void *data);
57 static gboolean gdk_frame_clock_paint_idle (void *data);
59 G_DEFINE_TYPE (GdkFrameClockIdle, gdk_frame_clock_idle, GDK_TYPE_FRAME_CLOCK)
61 static gint64 sleep_serial;
62 static gint64 sleep_source_prepare_time;
63 static GSource *sleep_source;
66 sleep_source_prepare (GSource *source,
69 sleep_source_prepare_time = g_source_get_time (source);
75 sleep_source_check (GSource *source)
77 if (g_source_get_time (source) != sleep_source_prepare_time)
84 sleep_source_dispatch (GSource *source,
91 static GSourceFuncs sleep_source_funcs = {
94 sleep_source_dispatch,
99 get_sleep_serial (void)
101 if (sleep_source == NULL)
103 sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));
105 g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
106 g_source_attach (sleep_source, NULL);
107 g_source_unref (sleep_source);
114 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
116 GdkFrameClockIdlePrivate *priv;
118 frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
119 GDK_TYPE_FRAME_CLOCK_IDLE,
120 GdkFrameClockIdlePrivate);
121 priv = frame_clock_idle->priv;
123 priv->freeze_count = 0;
127 gdk_frame_clock_idle_dispose (GObject *object)
129 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
131 if (priv->flush_idle_id != 0)
133 g_source_remove (priv->flush_idle_id);
134 priv->flush_idle_id = 0;
137 if (priv->paint_idle_id != 0)
139 g_source_remove (priv->paint_idle_id);
140 priv->paint_idle_id = 0;
143 G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->dispose (object);
147 compute_frame_time (GdkFrameClockIdle *idle)
149 GdkFrameClockIdlePrivate *priv = idle->priv;
150 gint64 computed_frame_time;
153 elapsed = g_get_monotonic_time () + priv->timer_base;
154 if (elapsed < priv->frame_time)
156 /* clock went backward. adapt to that by forevermore increasing
157 * timer_base. For now, assume we've gone forward in time 1ms.
159 /* hmm. just fix GTimer? */
160 computed_frame_time = priv->frame_time + 1;
161 priv->timer_base += (priv->frame_time - elapsed) + 1;
165 computed_frame_time = elapsed;
168 return computed_frame_time;
172 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
174 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
175 gint64 computed_frame_time;
177 /* can't change frame time during a paint */
178 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
179 priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
180 return priv->frame_time;
182 /* Outside a paint, pick something close to "now" */
183 computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
185 /* 16ms is 60fps. We only update frame time that often because we'd
186 * like to try to keep animations on the same start times.
187 * get_frame_time() would normally be used outside of a paint to
188 * record an animation start time for example.
190 if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
191 priv->frame_time = computed_frame_time;
193 return priv->frame_time;
196 #define RUN_FLUSH_IDLE(priv) \
197 ((priv)->freeze_count == 0 && \
198 ((priv)->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
200 /* The reason why we track updating_count separately here and don't
201 * just add GDK_FRAME_CLOCK_PHASE_UPDATE into ->request on every frame
202 * is so that we can avoid doing one more frame when an animation
205 #define RUN_PAINT_IDLE(priv) \
206 ((priv)->freeze_count == 0 && \
207 (((priv)->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 || \
208 (priv)->updating_count > 0))
211 maybe_start_idle (GdkFrameClockIdle *clock_idle)
213 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
215 if (RUN_FLUSH_IDLE (priv) || RUN_PAINT_IDLE (priv))
217 guint min_interval = 0;
219 if (priv->min_next_frame_time != 0)
221 gint64 now = compute_frame_time (clock_idle);
222 gint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
223 min_interval = (min_interval_us + 500) / 1000;
226 if (priv->flush_idle_id == 0 && RUN_FLUSH_IDLE (priv))
228 priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
230 gdk_frame_clock_flush_idle,
231 g_object_ref (clock_idle),
232 (GDestroyNotify) g_object_unref);
235 if (priv->paint_idle_id == 0 && RUN_PAINT_IDLE (priv))
237 priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
239 gdk_frame_clock_paint_idle,
240 g_object_ref (clock_idle),
241 (GDestroyNotify) g_object_unref);
247 maybe_stop_idle (GdkFrameClockIdle *clock_idle)
249 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
251 if (priv->flush_idle_id != 0 && !RUN_FLUSH_IDLE (priv))
253 g_source_remove (priv->flush_idle_id);
254 priv->flush_idle_id = 0;
257 if (priv->paint_idle_id != 0 && !RUN_PAINT_IDLE (priv))
259 g_source_remove (priv->paint_idle_id);
260 priv->paint_idle_id = 0;
265 compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
266 gint64 last_frame_time)
268 gint64 presentation_time;
269 gint64 refresh_interval;
271 gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
273 &refresh_interval, &presentation_time);
275 if (presentation_time == 0)
276 return last_frame_time + refresh_interval;
278 return presentation_time + refresh_interval / 2;
282 gdk_frame_clock_flush_idle (void *data)
284 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
285 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
286 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
288 priv->flush_idle_id = 0;
290 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
293 priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
294 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
296 g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
298 if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 ||
299 priv->updating_count > 0)
300 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
302 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
308 gdk_frame_clock_paint_idle (void *data)
310 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
311 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
312 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
313 gboolean skip_to_resume_events;
314 GdkFrameTimings *timings = NULL;
316 priv->paint_idle_id = 0;
317 priv->in_paint_idle = TRUE;
318 priv->min_next_frame_time = 0;
320 skip_to_resume_events =
321 (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0 &&
322 priv->updating_count == 0;
324 if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
326 timings = gdk_frame_clock_get_current_timings (clock);
329 if (!skip_to_resume_events)
333 case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
335 case GDK_FRAME_CLOCK_PHASE_NONE:
336 case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
337 if (priv->freeze_count == 0)
339 priv->frame_time = compute_frame_time (clock_idle);
341 _gdk_frame_clock_begin_frame (clock);
342 timings = gdk_frame_clock_get_current_timings (clock);
344 timings->frame_time = priv->frame_time;
345 timings->slept_before = priv->sleep_serial != get_sleep_serial ();
347 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
349 /* We always emit ::before-paint and ::after-paint if
350 * any of the intermediate phases are requested and
351 * they don't get repeated if you freeze/thaw while
353 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
354 g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
355 priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
357 case GDK_FRAME_CLOCK_PHASE_UPDATE:
358 if (priv->freeze_count == 0)
360 if ((priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE) != 0 ||
361 priv->updating_count > 0)
363 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
364 g_signal_emit_by_name (G_OBJECT (clock), "update");
367 case GDK_FRAME_CLOCK_PHASE_LAYOUT:
368 if (priv->freeze_count == 0)
370 #ifdef G_ENABLE_DEBUG
371 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
373 if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
374 (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
375 timings->layout_start_time = g_get_monotonic_time ();
377 #endif /* G_ENABLE_DEBUG */
379 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
380 if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
382 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
383 g_signal_emit_by_name (G_OBJECT (clock), "layout");
386 case GDK_FRAME_CLOCK_PHASE_PAINT:
387 if (priv->freeze_count == 0)
389 #ifdef G_ENABLE_DEBUG
390 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
392 if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
393 (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
394 timings->paint_start_time = g_get_monotonic_time ();
396 #endif /* G_ENABLE_DEBUG */
398 priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
399 if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
401 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
402 g_signal_emit_by_name (G_OBJECT (clock), "paint");
405 case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
406 if (priv->freeze_count == 0)
408 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
409 g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
410 /* the ::after-paint phase doesn't get repeated on freeze/thaw,
412 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
414 #ifdef G_ENABLE_DEBUG
415 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
416 timings->frame_end_time = g_get_monotonic_time ();
417 #endif /* G_ENABLE_DEBUG */
419 case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
424 #ifdef G_ENABLE_DEBUG
425 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
427 if (timings->complete)
428 _gdk_frame_clock_debug_print_timings (clock, timings);
430 #endif /* G_ENABLE_DEBUG */
432 if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
434 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
435 g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
438 if (priv->freeze_count == 0)
439 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
441 priv->in_paint_idle = FALSE;
443 /* If there is throttling in the backend layer, then we'll do another
444 * update as soon as the backend unthrottles (if there is work to do),
445 * otherwise we need to figure when the next frame should be.
447 if (priv->freeze_count == 0)
449 priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
451 maybe_start_idle (clock_idle);
454 if (priv->freeze_count == 0)
455 priv->sleep_serial = get_sleep_serial ();
461 gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
462 GdkFrameClockPhase phase)
464 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
465 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
467 priv->requested |= phase;
468 maybe_start_idle (clock_idle);
472 gdk_frame_clock_idle_begin_updating (GdkFrameClock *clock)
474 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
475 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
477 priv->updating_count++;
478 maybe_start_idle (clock_idle);
482 gdk_frame_clock_idle_end_updating (GdkFrameClock *clock)
484 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
485 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
487 g_return_if_fail (priv->updating_count > 0);
489 priv->updating_count--;
490 maybe_stop_idle (clock_idle);
494 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
496 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
497 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
499 priv->freeze_count++;
500 maybe_stop_idle (clock_idle);
504 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
506 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
507 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
509 g_return_if_fail (priv->freeze_count > 0);
511 priv->freeze_count--;
512 if (priv->freeze_count == 0)
514 maybe_start_idle (clock_idle);
515 /* If nothing is requested so we didn't start an idle, we need
516 * to skip to the end of the state chain, since the idle won't
517 * run and do it for us. */
518 if (priv->paint_idle_id == 0)
519 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
521 priv->sleep_serial = get_sleep_serial ();
526 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
528 GObjectClass *gobject_class = (GObjectClass*) klass;
529 GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
531 gobject_class->dispose = gdk_frame_clock_idle_dispose;
533 frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
534 frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
535 frame_clock_class->begin_updating = gdk_frame_clock_idle_begin_updating;
536 frame_clock_class->end_updating = gdk_frame_clock_idle_end_updating;
537 frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
538 frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
540 g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
544 _gdk_frame_clock_idle_new (void)
546 GdkFrameClockIdle *clock;
548 clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
550 return GDK_FRAME_CLOCK (clock);