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 "gdkframeclock.h"
31 G_DEFINE_INTERFACE (GdkFrameClockTarget, gdk_frame_clock_target, G_TYPE_OBJECT)
34 gdk_frame_clock_target_default_init (GdkFrameClockTargetInterface *iface)
38 void gdk_frame_clock_target_set_clock (GdkFrameClockTarget *target,
41 GDK_FRAME_CLOCK_TARGET_GET_IFACE (target)->set_clock (target, clock);
46 * @Short_description: Frame clock syncs painting to a window or display
49 * A #GdkFrameClock tells the application when to repaint a window.
50 * This may be synced to the vertical refresh rate of the monitor, for
51 * example. Even when the frame clock uses a simple timer rather than
52 * a hardware-based vertical sync, the frame clock helps because it
53 * ensures everything paints at the same time (reducing the total
54 * number of frames). The frame clock can also automatically stop
55 * painting when it knows the frames will not be visible, or scale back
56 * animation framerates.
58 * #GdkFrameClock is designed to be compatible with an OpenGL-based
59 * implementation or with mozRequestAnimationFrame in Firefox,
62 * A frame clock is idle until someone requests a frame with
63 * gdk_frame_clock_request_phase(). At that time, the frame clock
64 * emits its GdkFrameClock:frame-requested signal if no frame was
67 * At some later time after the frame is requested, the frame clock
68 * MAY indicate that a frame should be painted. To paint a frame the
69 * clock will: Emit GdkFrameClock:before-paint; update the frame time
70 * in the default handler for GdkFrameClock:before-paint; emit
71 * GdkFrameClock:paint; emit GdkFrameClock:after-paint. The app
72 * should paint in a handler for the paint signal.
74 * If a given frame is not painted (the clock is idle), the frame time
75 * should still update to a conceptual "last frame." i.e. the frame
76 * time will keep moving forward roughly with wall clock time.
78 * The frame time is in milliseconds. However, it should not be
79 * thought of as having any particular relationship to wall clock
80 * time. Unlike wall clock time, it "snaps" to conceptual frame times
81 * so is low-resolution; it is guaranteed to never move backward (so
82 * say you reset your computer clock, the frame clock will not reset);
83 * and the frame clock is allowed to drift. For example nicer
84 * results when painting with vertical refresh sync may be obtained by
85 * painting as rapidly as possible, but always incrementing the frame
86 * time by the frame length on each frame. This results in a frame
87 * time that doesn't have a lot to do with wall clock time.
90 G_DEFINE_INTERFACE (GdkFrameClock, gdk_frame_clock, G_TYPE_OBJECT)
104 static guint signals[LAST_SIGNAL];
107 gdk_frame_clock_default_init (GdkFrameClockInterface *iface)
110 * GdkFrameClock::frame-requested:
111 * @clock: the frame clock emitting the signal
113 * This signal is emitted when a frame is not pending, and
114 * gdk_frame_clock_request_frame() is called to request a frame.
116 signals[FRAME_REQUESTED] =
117 g_signal_new (g_intern_static_string ("frame-requested"),
118 GDK_TYPE_FRAME_CLOCK,
122 g_cclosure_marshal_VOID__VOID,
126 * GdkFrameClock::flush-events:
127 * @clock: the frame clock emitting the signal
131 signals[FLUSH_EVENTS] =
132 g_signal_new (g_intern_static_string ("flush-events"),
133 GDK_TYPE_FRAME_CLOCK,
137 g_cclosure_marshal_VOID__VOID,
141 * GdkFrameClock::before-paint:
142 * @clock: the frame clock emitting the signal
144 * This signal is emitted immediately before the paint signal and
145 * indicates that the frame time has been updated, and signal
146 * handlers should perform any preparatory work before painting.
148 signals[BEFORE_PAINT] =
149 g_signal_new (g_intern_static_string ("before-paint"),
150 GDK_TYPE_FRAME_CLOCK,
154 g_cclosure_marshal_VOID__VOID,
158 * GdkFrameClock::update:
159 * @clock: the frame clock emitting the signal
164 g_signal_new (g_intern_static_string ("update"),
165 GDK_TYPE_FRAME_CLOCK,
169 g_cclosure_marshal_VOID__VOID,
173 * GdkFrameClock::layout:
174 * @clock: the frame clock emitting the signal
176 * This signal is emitted immediately before the paint signal and
177 * indicates that the frame time has been updated, and signal
178 * handlers should perform any preparatory work before painting.
181 g_signal_new (g_intern_static_string ("layout"),
182 GDK_TYPE_FRAME_CLOCK,
186 g_cclosure_marshal_VOID__VOID,
190 * GdkFrameClock::paint:
191 * @clock: the frame clock emitting the signal
193 * Signal handlers for this signal should paint the window, screen,
194 * or whatever they normally paint.
197 g_signal_new (g_intern_static_string ("paint"),
198 GDK_TYPE_FRAME_CLOCK,
202 g_cclosure_marshal_VOID__VOID,
206 * GdkFrameClock::after-paint:
207 * @clock: the frame clock emitting the signal
209 * This signal is emitted immediately after the paint signal and
210 * allows signal handlers to do anything they'd like to do after
211 * painting has been completed. This is a relatively good time to do
212 * "expensive" processing in order to get it done in between frames.
214 signals[AFTER_PAINT] =
215 g_signal_new (g_intern_static_string ("after-paint"),
216 GDK_TYPE_FRAME_CLOCK,
220 g_cclosure_marshal_VOID__VOID,
224 * GdkFrameClock::resume-events:
225 * @clock: the frame clock emitting the signal
229 signals[RESUME_EVENTS] =
230 g_signal_new (g_intern_static_string ("resume-events"),
231 GDK_TYPE_FRAME_CLOCK,
235 g_cclosure_marshal_VOID__VOID,
240 * gdk_frame_clock_get_frame_time:
243 * Gets the time that should currently be used for animations. Inside
244 * a paint, it's the time used to compute the animation position of
245 * everything in a frame. Outside a paint, it's the time of the
246 * conceptual "previous frame," which may be either the actual
247 * previous frame time, or if that's too old, an updated time.
249 * The returned time has no relationship to wall clock time. It
250 * increases roughly at 1 millisecond per wall clock millisecond, and
251 * it never decreases, but its value is only meaningful relative to
252 * previous frame clock times.
256 * Return value: a timestamp in milliseconds
259 gdk_frame_clock_get_frame_time (GdkFrameClock *clock)
261 g_return_val_if_fail (GDK_IS_FRAME_CLOCK (clock), 0);
263 return GDK_FRAME_CLOCK_GET_IFACE (clock)->get_frame_time (clock);
267 * gdk_frame_clock_request_phase:
270 * Asks the frame clock to paint a frame. The frame
271 * may or may not ever be painted (the frame clock may
272 * stop itself for whatever reason), but the goal in
273 * normal circumstances would be to paint the frame
274 * at the next expected frame time. For example
275 * if the clock is running at 60fps the frame would
276 * ideally be painted within 1000/60=16 milliseconds.
281 gdk_frame_clock_request_phase (GdkFrameClock *clock,
282 GdkFrameClockPhase phase)
284 g_return_if_fail (GDK_IS_FRAME_CLOCK (clock));
286 GDK_FRAME_CLOCK_GET_IFACE (clock)->request_phase (clock, phase);
291 gdk_frame_clock_freeze (GdkFrameClock *clock)
293 g_return_if_fail (GDK_IS_FRAME_CLOCK (clock));
295 GDK_FRAME_CLOCK_GET_IFACE (clock)->freeze (clock);
300 gdk_frame_clock_thaw (GdkFrameClock *clock)
302 g_return_if_fail (GDK_IS_FRAME_CLOCK (clock));
304 GDK_FRAME_CLOCK_GET_IFACE (clock)->thaw (clock);
308 * gdk_frame_clock_get_requested:
311 * Gets whether a frame paint has been requested but has not been
316 * Return value: TRUE if a frame paint is pending
319 gdk_frame_clock_get_requested (GdkFrameClock *clock)
321 g_return_val_if_fail (GDK_IS_FRAME_CLOCK (clock), FALSE);
323 return GDK_FRAME_CLOCK_GET_IFACE (clock)->get_requested (clock);
327 * gdk_frame_clock_get_frame_time_val:
329 * @timeval: #GTimeVal to fill in with frame time
331 * Like gdk_frame_clock_get_frame_time() but returns the time as a
332 * #GTimeVal which may be handy with some APIs (such as
333 * #GdkPixbufAnimation).
336 gdk_frame_clock_get_frame_time_val (GdkFrameClock *clock,
341 g_return_if_fail (GDK_IS_FRAME_CLOCK (clock));
343 time_ms = gdk_frame_clock_get_frame_time (clock);
345 timeval->tv_sec = time_ms / 1000;
346 timeval->tv_usec = (time_ms % 1000) * 1000;
350 * gdk_frame_clock_frame_requested:
353 * Emits the frame-requested signal. Used in implementations of the
354 * #GdkFrameClock interface.
357 gdk_frame_clock_frame_requested (GdkFrameClock *clock)
359 g_return_if_fail (GDK_IS_FRAME_CLOCK (clock));
361 g_signal_emit (G_OBJECT (clock),
362 signals[FRAME_REQUESTED], 0);