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 "gdkframeclockidle.h"
33 #define FRAME_INTERVAL 16667 // microseconds
35 struct _GdkFrameClockIdlePrivate
37 GdkFrameHistory *history;
39 /* timer_base is used to avoid ever going backward */
42 guint64 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 static void gdk_frame_clock_idle_finalize (GObject *object);
59 static void gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface);
61 G_DEFINE_TYPE_WITH_CODE (GdkFrameClockIdle, gdk_frame_clock_idle, G_TYPE_OBJECT,
62 G_IMPLEMENT_INTERFACE (GDK_TYPE_FRAME_CLOCK,
63 gdk_frame_clock_idle_interface_init))
65 static gint64 sleep_serial;
66 static gint64 sleep_source_prepare_time;
67 static GSource *sleep_source;
70 sleep_source_prepare (GSource *source,
73 sleep_source_prepare_time = g_source_get_time (source);
79 sleep_source_check (GSource *source)
81 if (g_source_get_time (source) != sleep_source_prepare_time)
88 sleep_source_dispatch (GSource *source,
95 static GSourceFuncs sleep_source_funcs = {
98 sleep_source_dispatch,
103 get_sleep_serial (void)
105 if (sleep_source == NULL)
107 sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));
109 g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
110 g_source_attach (sleep_source, NULL);
111 g_source_unref (sleep_source);
118 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
120 GObjectClass *gobject_class = (GObjectClass*) klass;
122 gobject_class->finalize = gdk_frame_clock_idle_finalize;
124 g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
128 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
130 GdkFrameClockIdlePrivate *priv;
132 frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
133 GDK_TYPE_FRAME_CLOCK_IDLE,
134 GdkFrameClockIdlePrivate);
135 priv = frame_clock_idle->priv;
137 priv->history = gdk_frame_history_new ();
138 priv->timer = g_timer_new ();
139 priv->freeze_count = 0;
143 gdk_frame_clock_idle_finalize (GObject *object)
145 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
147 g_timer_destroy (priv->timer);
149 G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
153 compute_frame_time (GdkFrameClockIdle *idle)
155 GdkFrameClockIdlePrivate *priv = idle->priv;
156 guint64 computed_frame_time;
159 elapsed = g_get_monotonic_time () + priv->timer_base;
160 if (elapsed < priv->frame_time)
162 /* clock went backward. adapt to that by forevermore increasing
163 * timer_base. For now, assume we've gone forward in time 1ms.
165 /* hmm. just fix GTimer? */
166 computed_frame_time = priv->frame_time + 1;
167 priv->timer_base += (priv->frame_time - elapsed) + 1;
171 computed_frame_time = elapsed;
174 return computed_frame_time;
178 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
180 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
181 guint64 computed_frame_time;
183 /* can't change frame time during a paint */
184 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
185 priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
186 return priv->frame_time;
188 /* Outside a paint, pick something close to "now" */
189 computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
191 /* 16ms is 60fps. We only update frame time that often because we'd
192 * like to try to keep animations on the same start times.
193 * get_frame_time() would normally be used outside of a paint to
194 * record an animation start time for example.
196 if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
197 priv->frame_time = computed_frame_time;
199 return priv->frame_time;
203 maybe_start_idle (GdkFrameClockIdle *clock_idle)
205 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
207 if (priv->freeze_count == 0 && priv->requested != 0)
209 guint min_interval = 0;
211 if (priv->min_next_frame_time != 0)
213 guint64 now = compute_frame_time (clock_idle);
214 guint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
215 min_interval = (min_interval_us + 500) / 1000;
218 if (priv->flush_idle_id == 0 &&
219 (priv->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
221 priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
223 gdk_frame_clock_flush_idle,
224 g_object_ref (clock_idle),
225 (GDestroyNotify) g_object_unref);
228 if (priv->paint_idle_id == 0 &&
229 !priv->in_paint_idle &&
230 (priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
232 priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
234 gdk_frame_clock_paint_idle,
235 g_object_ref (clock_idle),
236 (GDestroyNotify) g_object_unref);
238 gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
244 compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
245 gint64 last_frame_time)
247 gint64 presentation_time;
248 gint64 refresh_interval;
250 gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
252 &refresh_interval, &presentation_time);
254 if (presentation_time == 0)
255 return last_frame_time + refresh_interval;
257 return presentation_time + refresh_interval / 2;
261 gdk_frame_clock_flush_idle (void *data)
263 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
264 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
265 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
267 priv->flush_idle_id = 0;
269 if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
272 priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
273 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
275 g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
277 if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
278 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
280 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
286 gdk_frame_clock_paint_idle (void *data)
288 GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
289 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
290 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
291 gboolean skip_to_resume_events;
292 GdkFrameTimings *timings = NULL;
293 gint64 frame_counter = 0;
295 priv->paint_idle_id = 0;
296 priv->in_paint_idle = TRUE;
297 priv->min_next_frame_time = 0;
299 skip_to_resume_events =
300 (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0;
302 if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
304 frame_counter = gdk_frame_history_get_frame_counter (priv->history);
305 timings = gdk_frame_history_get_timings (priv->history, frame_counter);
308 if (!skip_to_resume_events)
312 case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
314 case GDK_FRAME_CLOCK_PHASE_NONE:
315 case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
316 if (priv->freeze_count == 0)
318 priv->frame_time = compute_frame_time (clock_idle);
320 gdk_frame_history_begin_frame (priv->history);
321 frame_counter = gdk_frame_history_get_frame_counter (priv->history);
322 timings = gdk_frame_history_get_timings (priv->history, frame_counter);
324 gdk_frame_timings_set_frame_time (timings, priv->frame_time);
326 gdk_frame_timings_set_slept_before (timings,
327 priv->sleep_serial != get_sleep_serial ());
329 priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
331 /* We always emit ::before-paint and ::after-paint if
332 * any of the intermediate phases are requested and
333 * they don't get repeated if you freeze/thaw while
335 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
336 g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
337 priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
339 case GDK_FRAME_CLOCK_PHASE_UPDATE:
340 if (priv->freeze_count == 0)
342 if (priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE)
344 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
345 g_signal_emit_by_name (G_OBJECT (clock), "update");
348 case GDK_FRAME_CLOCK_PHASE_LAYOUT:
349 if (priv->freeze_count == 0)
351 #ifdef G_ENABLE_DEBUG
352 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
354 if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
355 (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
356 _gdk_frame_timings_set_layout_start_time (timings, g_get_monotonic_time ());
358 #endif /* G_ENABLE_DEBUG */
360 priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
361 if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
363 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
364 g_signal_emit_by_name (G_OBJECT (clock), "layout");
367 case GDK_FRAME_CLOCK_PHASE_PAINT:
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_PAINT &&
374 (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
375 _gdk_frame_timings_set_paint_start_time (timings, g_get_monotonic_time ());
377 #endif /* G_ENABLE_DEBUG */
379 priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
380 if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
382 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
383 g_signal_emit_by_name (G_OBJECT (clock), "paint");
386 case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
387 if (priv->freeze_count == 0)
389 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
390 g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
391 /* the ::after-paint phase doesn't get repeated on freeze/thaw,
393 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
395 #ifdef G_ENABLE_DEBUG
396 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
397 _gdk_frame_timings_set_frame_end_time (timings, g_get_monotonic_time ());
398 #endif /* G_ENABLE_DEBUG */
400 case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
405 #ifdef G_ENABLE_DEBUG
406 if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
408 if (gdk_frame_timings_get_complete (timings))
409 _gdk_frame_history_debug_print (priv->history, timings);
411 #endif /* G_ENABLE_DEBUG */
413 if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
415 priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
416 g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
419 if (priv->freeze_count == 0)
420 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
422 priv->in_paint_idle = FALSE;
424 /* If there is throttling in the backend layer, then we'll do another
425 * update as soon as the backend unthrottles (if there is work to do),
426 * otherwise we need to figure when the next frame should be.
428 if (priv->freeze_count == 0)
430 priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
432 maybe_start_idle (clock_idle);
435 if (priv->freeze_count == 0)
436 priv->sleep_serial = get_sleep_serial ();
442 gdk_frame_clock_idle_request_phase (GdkFrameClock *clock,
443 GdkFrameClockPhase phase)
445 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
446 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
448 priv->requested |= phase;
449 maybe_start_idle (clock_idle);
452 static GdkFrameClockPhase
453 gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
455 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
457 return priv->requested;
461 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
463 GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
465 priv->freeze_count++;
467 if (priv->freeze_count == 1)
469 if (priv->flush_idle_id)
471 g_source_remove (priv->flush_idle_id);
472 priv->flush_idle_id = 0;
474 if (priv->paint_idle_id)
476 g_source_remove (priv->paint_idle_id);
477 priv->paint_idle_id = 0;
483 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
485 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
486 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
488 g_return_if_fail (priv->freeze_count > 0);
490 priv->freeze_count--;
491 if (priv->freeze_count == 0)
493 maybe_start_idle (clock_idle);
494 /* If nothing is requested so we didn't start an idle, we need
495 * to skip to the end of the state chain, since the idle won't
496 * run and do it for us. */
497 if (priv->paint_idle_id == 0)
498 priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
500 priv->sleep_serial = get_sleep_serial ();
504 static GdkFrameHistory *
505 gdk_frame_clock_idle_get_history (GdkFrameClock *clock)
507 GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
508 GdkFrameClockIdlePrivate *priv = clock_idle->priv;
510 return priv->history;
514 gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
516 iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
517 iface->request_phase = gdk_frame_clock_idle_request_phase;
518 iface->get_requested = gdk_frame_clock_idle_get_requested;
519 iface->freeze = gdk_frame_clock_idle_freeze;
520 iface->thaw = gdk_frame_clock_idle_thaw;
521 iface->get_history = gdk_frame_clock_idle_get_history;
525 _gdk_frame_clock_idle_new (void)
527 GdkFrameClockIdle *clock;
529 clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
531 return GDK_FRAME_CLOCK (clock);