]> Pileus Git - ~andy/gtk/blob - gdk/gdkframeclockidle.c
GdkFrameClock: Make the phase explicit when requesting the frame
[~andy/gtk] / gdk / gdkframeclockidle.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #include "config.h"
28
29 #include "gdkframeclockidle.h"
30 #include "gdk.h"
31
32 struct _GdkFrameClockIdlePrivate
33 {
34   GTimer *timer;
35   /* timer_base is used to avoid ever going backward */
36   guint64 timer_base;
37   guint64 frame_time;
38
39   guint idle_id;
40
41   GdkFrameClockPhase requested;
42   GdkFrameClockPhase phase;
43 };
44
45 static gboolean gdk_frame_clock_paint_idle (void *data);
46
47 static void gdk_frame_clock_idle_finalize             (GObject                *object);
48 static void gdk_frame_clock_idle_interface_init       (GdkFrameClockInterface *iface);
49
50 G_DEFINE_TYPE_WITH_CODE (GdkFrameClockIdle, gdk_frame_clock_idle, G_TYPE_OBJECT,
51                          G_IMPLEMENT_INTERFACE (GDK_TYPE_FRAME_CLOCK,
52                                                 gdk_frame_clock_idle_interface_init))
53
54 static void
55 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
56 {
57   GObjectClass *gobject_class = (GObjectClass*) klass;
58
59   gobject_class->finalize     = gdk_frame_clock_idle_finalize;
60
61   g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
62 }
63
64 static void
65 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
66 {
67   GdkFrameClockIdlePrivate *priv;
68
69   frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
70                                                         GDK_TYPE_FRAME_CLOCK_IDLE,
71                                                         GdkFrameClockIdlePrivate);
72   priv = frame_clock_idle->priv;
73
74   priv->timer = g_timer_new ();
75 }
76
77 static void
78 gdk_frame_clock_idle_finalize (GObject *object)
79 {
80   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
81
82   g_timer_destroy (priv->timer);
83
84   G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
85 }
86
87 static guint64
88 compute_frame_time (GdkFrameClockIdle *idle)
89 {
90   GdkFrameClockIdlePrivate *priv = idle->priv;
91   guint64 computed_frame_time;
92   guint64 elapsed;
93
94   elapsed = ((guint64) (g_timer_elapsed (priv->timer, NULL) * 1000)) + priv->timer_base;
95   if (elapsed < priv->frame_time)
96     {
97       /* clock went backward. adapt to that by forevermore increasing
98        * timer_base.  For now, assume we've gone forward in time 1ms.
99        */
100       /* hmm. just fix GTimer? */
101       computed_frame_time = priv->frame_time + 1;
102       priv->timer_base += (priv->frame_time - elapsed) + 1;
103     }
104   else
105     {
106       computed_frame_time = elapsed;
107     }
108
109   return computed_frame_time;
110 }
111
112 static guint64
113 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
114 {
115   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
116   guint64 computed_frame_time;
117
118   /* can't change frame time during a paint */
119   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
120     return priv->frame_time;
121
122   /* Outside a paint, pick something close to "now" */
123   computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
124
125   /* 16ms is 60fps. We only update frame time that often because we'd
126    * like to try to keep animations on the same start times.
127    * get_frame_time() would normally be used outside of a paint to
128    * record an animation start time for example.
129    */
130   if ((computed_frame_time - priv->frame_time) > 16)
131     priv->frame_time = computed_frame_time;
132
133   return priv->frame_time;
134 }
135
136 static void
137 maybe_start_idle (GdkFrameClockIdle *clock_idle)
138 {
139   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
140
141   if (priv->idle_id == 0 && priv->requested != 0)
142     {
143       priv->idle_id = gdk_threads_add_idle_full (GDK_PRIORITY_REDRAW,
144                                                  gdk_frame_clock_paint_idle,
145                                                  g_object_ref (clock_idle),
146                                                  (GDestroyNotify) g_object_unref);
147
148       gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
149     }
150 }
151
152 static gboolean
153 gdk_frame_clock_paint_idle (void *data)
154 {
155   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
156   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
157   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
158
159   priv->idle_id = 0;
160
161   priv->frame_time = compute_frame_time (clock_idle);
162
163   priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
164   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
165   g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
166   priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
167   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
168   g_signal_emit_by_name (G_OBJECT (clock), "layout");
169   priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
170   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
171   g_signal_emit_by_name (G_OBJECT (clock), "paint");
172   priv->phase = GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
173   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
174   g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
175   priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
176
177   maybe_start_idle (clock_idle);
178
179   return FALSE;
180 }
181
182 static void
183 gdk_frame_clock_idle_request_phase (GdkFrameClock      *clock,
184                                     GdkFrameClockPhase  phase)
185 {
186   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
187   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
188
189   priv->requested |= phase;
190   maybe_start_idle (clock_idle);
191 }
192
193 static GdkFrameClockPhase
194 gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
195 {
196   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
197
198   return priv->requested;
199 }
200
201 static void
202 gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
203 {
204   iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
205   iface->request_phase = gdk_frame_clock_idle_request_phase;
206   iface->get_requested = gdk_frame_clock_idle_get_requested;
207 }
208
209 GdkFrameClock *
210 _gdk_frame_clock_idle_new (void)
211 {
212   GdkFrameClockIdle *clock;
213
214   clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
215
216   return GDK_FRAME_CLOCK (clock);
217 }