]> Pileus Git - ~andy/gtk/blob - gdk/gdkframeclockidle.c
GdkFrameClockIdle: add throttling to 60fps
[~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 #define FRAME_INTERVAL 16667 // microseconds
33
34 struct _GdkFrameClockIdlePrivate
35 {
36   GTimer *timer;
37   /* timer_base is used to avoid ever going backward */
38   guint64 timer_base;
39   guint64 frame_time;
40   guint64 min_next_frame_time;
41
42   guint idle_id;
43   guint freeze_count;
44
45   GdkFrameClockPhase requested;
46   GdkFrameClockPhase phase;
47 };
48
49 static gboolean gdk_frame_clock_paint_idle (void *data);
50
51 static void gdk_frame_clock_idle_finalize             (GObject                *object);
52 static void gdk_frame_clock_idle_interface_init       (GdkFrameClockInterface *iface);
53
54 G_DEFINE_TYPE_WITH_CODE (GdkFrameClockIdle, gdk_frame_clock_idle, G_TYPE_OBJECT,
55                          G_IMPLEMENT_INTERFACE (GDK_TYPE_FRAME_CLOCK,
56                                                 gdk_frame_clock_idle_interface_init))
57
58 static void
59 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
60 {
61   GObjectClass *gobject_class = (GObjectClass*) klass;
62
63   gobject_class->finalize     = gdk_frame_clock_idle_finalize;
64
65   g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
66 }
67
68 static void
69 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
70 {
71   GdkFrameClockIdlePrivate *priv;
72
73   frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
74                                                         GDK_TYPE_FRAME_CLOCK_IDLE,
75                                                         GdkFrameClockIdlePrivate);
76   priv = frame_clock_idle->priv;
77
78   priv->timer = g_timer_new ();
79   priv->freeze_count = 0;
80 }
81
82 static void
83 gdk_frame_clock_idle_finalize (GObject *object)
84 {
85   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
86
87   g_timer_destroy (priv->timer);
88
89   G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
90 }
91
92 static guint64
93 compute_frame_time (GdkFrameClockIdle *idle)
94 {
95   GdkFrameClockIdlePrivate *priv = idle->priv;
96   guint64 computed_frame_time;
97   guint64 elapsed;
98
99   elapsed = g_get_monotonic_time () + priv->timer_base;
100   if (elapsed < priv->frame_time)
101     {
102       /* clock went backward. adapt to that by forevermore increasing
103        * timer_base.  For now, assume we've gone forward in time 1ms.
104        */
105       /* hmm. just fix GTimer? */
106       computed_frame_time = priv->frame_time + 1;
107       priv->timer_base += (priv->frame_time - elapsed) + 1;
108     }
109   else
110     {
111       computed_frame_time = elapsed;
112     }
113
114   return computed_frame_time;
115 }
116
117 static guint64
118 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
119 {
120   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
121   guint64 computed_frame_time;
122
123   /* can't change frame time during a paint */
124   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
125       priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
126     return priv->frame_time;
127
128   /* Outside a paint, pick something close to "now" */
129   computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
130
131   /* 16ms is 60fps. We only update frame time that often because we'd
132    * like to try to keep animations on the same start times.
133    * get_frame_time() would normally be used outside of a paint to
134    * record an animation start time for example.
135    */
136   if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
137     priv->frame_time = computed_frame_time;
138
139   return priv->frame_time;
140 }
141
142 static void
143 maybe_start_idle (GdkFrameClockIdle *clock_idle)
144 {
145   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
146
147   if (priv->idle_id == 0 && priv->freeze_count == 0 && priv->requested != 0)
148     {
149       guint min_interval = 0;
150
151       if (priv->min_next_frame_time != 0)
152         {
153           guint64 now = compute_frame_time (clock_idle);
154           guint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
155           min_interval = (min_interval_us + 500) / 1000;
156         }
157
158       priv->idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
159                                                     min_interval,
160                                                     gdk_frame_clock_paint_idle,
161                                                     g_object_ref (clock_idle),
162                                                     (GDestroyNotify) g_object_unref);
163
164       gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
165     }
166 }
167
168 static gboolean
169 gdk_frame_clock_paint_idle (void *data)
170 {
171   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
172   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
173   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
174
175   priv->idle_id = 0;
176
177   switch (priv->phase)
178     {
179     case GDK_FRAME_CLOCK_PHASE_NONE:
180     case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
181       if (priv->freeze_count == 0)
182         {
183           priv->frame_time = compute_frame_time (clock_idle);
184
185           priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
186           priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
187           /* We always emit ::before-paint and ::after-paint even if
188            * not explicitly requested, and unlike other phases,
189            * they don't get repeated if you freeze/thaw while
190            * in them. */
191           g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
192           priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
193         }
194     case GDK_FRAME_CLOCK_PHASE_LAYOUT:
195       if (priv->freeze_count == 0)
196         {
197           priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
198           if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
199             {
200               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
201               g_signal_emit_by_name (G_OBJECT (clock), "layout");
202             }
203         }
204     case GDK_FRAME_CLOCK_PHASE_PAINT:
205       if (priv->freeze_count == 0)
206         {
207           priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
208           if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
209             {
210               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
211               g_signal_emit_by_name (G_OBJECT (clock), "paint");
212             }
213         }
214     case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
215       if (priv->freeze_count == 0)
216         {
217           priv->phase = GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
218           priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
219           g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
220           /* the ::after-paint phase doesn't get repeated on freeze/thaw */
221           priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
222         }
223     }
224
225   if (priv->freeze_count == 0 && priv->requested != 0)
226     {
227       /* We need to start over again immediately - this implies that there is no
228        * throttling at the backend layer, so we need to back-off ourselves.
229        */
230       gdk_flush ();
231       priv->min_next_frame_time = priv->frame_time + FRAME_INTERVAL;
232       maybe_start_idle (clock_idle);
233     }
234   else
235     {
236       priv->min_next_frame_time = 0;
237     }
238
239   return FALSE;
240 }
241
242 static void
243 gdk_frame_clock_idle_request_phase (GdkFrameClock      *clock,
244                                     GdkFrameClockPhase  phase)
245 {
246   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
247   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
248
249   priv->requested |= phase;
250   maybe_start_idle (clock_idle);
251 }
252
253 static GdkFrameClockPhase
254 gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
255 {
256   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
257
258   return priv->requested;
259 }
260
261 static void
262 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
263 {
264   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
265
266   priv->freeze_count++;
267
268   if (priv->freeze_count == 1)
269     {
270       if (priv->idle_id)
271         {
272           g_source_remove (priv->idle_id);
273           priv->idle_id = 0;
274         }
275     }
276 }
277
278 static void
279 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
280 {
281   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
282   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
283
284   g_return_if_fail (priv->freeze_count > 0);
285
286   priv->freeze_count--;
287   if (priv->freeze_count == 0)
288     maybe_start_idle (clock_idle);
289 }
290
291 static void
292 gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
293 {
294   iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
295   iface->request_phase = gdk_frame_clock_idle_request_phase;
296   iface->get_requested = gdk_frame_clock_idle_get_requested;
297   iface->freeze = gdk_frame_clock_idle_freeze;
298   iface->thaw = gdk_frame_clock_idle_thaw;
299 }
300
301 GdkFrameClock *
302 _gdk_frame_clock_idle_new (void)
303 {
304   GdkFrameClockIdle *clock;
305
306   clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
307
308   return GDK_FRAME_CLOCK (clock);
309 }