]> Pileus Git - ~andy/gtk/blob - gdk/gdkframeclockidle.c
GdkFrameClockIdle: don't start the tiemout/idle when in a 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 #define FRAME_INTERVAL 16667 // microseconds
33
34 struct _GdkFrameClockIdlePrivate
35 {
36   GdkFrameHistory *history;
37   GTimer *timer;
38   /* timer_base is used to avoid ever going backward */
39   guint64 timer_base;
40   guint64 frame_time;
41   guint64 min_next_frame_time;
42
43   guint flush_idle_id;
44   guint paint_idle_id;
45   guint freeze_count;
46
47   GdkFrameClockPhase requested;
48   GdkFrameClockPhase phase;
49
50   guint in_paint_idle : 1;
51 };
52
53 static gboolean gdk_frame_clock_flush_idle (void *data);
54 static gboolean gdk_frame_clock_paint_idle (void *data);
55
56 static void gdk_frame_clock_idle_finalize             (GObject                *object);
57 static void gdk_frame_clock_idle_interface_init       (GdkFrameClockInterface *iface);
58
59 G_DEFINE_TYPE_WITH_CODE (GdkFrameClockIdle, gdk_frame_clock_idle, G_TYPE_OBJECT,
60                          G_IMPLEMENT_INTERFACE (GDK_TYPE_FRAME_CLOCK,
61                                                 gdk_frame_clock_idle_interface_init))
62
63 static void
64 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
65 {
66   GObjectClass *gobject_class = (GObjectClass*) klass;
67
68   gobject_class->finalize     = gdk_frame_clock_idle_finalize;
69
70   g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
71 }
72
73 static void
74 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
75 {
76   GdkFrameClockIdlePrivate *priv;
77
78   frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
79                                                         GDK_TYPE_FRAME_CLOCK_IDLE,
80                                                         GdkFrameClockIdlePrivate);
81   priv = frame_clock_idle->priv;
82
83   priv->history = gdk_frame_history_new ();
84   priv->timer = g_timer_new ();
85   priv->freeze_count = 0;
86 }
87
88 static void
89 gdk_frame_clock_idle_finalize (GObject *object)
90 {
91   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
92
93   g_timer_destroy (priv->timer);
94
95   G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->finalize (object);
96 }
97
98 static guint64
99 compute_frame_time (GdkFrameClockIdle *idle)
100 {
101   GdkFrameClockIdlePrivate *priv = idle->priv;
102   guint64 computed_frame_time;
103   guint64 elapsed;
104
105   elapsed = g_get_monotonic_time () + priv->timer_base;
106   if (elapsed < priv->frame_time)
107     {
108       /* clock went backward. adapt to that by forevermore increasing
109        * timer_base.  For now, assume we've gone forward in time 1ms.
110        */
111       /* hmm. just fix GTimer? */
112       computed_frame_time = priv->frame_time + 1;
113       priv->timer_base += (priv->frame_time - elapsed) + 1;
114     }
115   else
116     {
117       computed_frame_time = elapsed;
118     }
119
120   return computed_frame_time;
121 }
122
123 static guint64
124 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
125 {
126   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
127   guint64 computed_frame_time;
128
129   /* can't change frame time during a paint */
130   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
131       priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
132     return priv->frame_time;
133
134   /* Outside a paint, pick something close to "now" */
135   computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
136
137   /* 16ms is 60fps. We only update frame time that often because we'd
138    * like to try to keep animations on the same start times.
139    * get_frame_time() would normally be used outside of a paint to
140    * record an animation start time for example.
141    */
142   if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
143     priv->frame_time = computed_frame_time;
144
145   return priv->frame_time;
146 }
147
148 static void
149 maybe_start_idle (GdkFrameClockIdle *clock_idle)
150 {
151   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
152
153   if (priv->freeze_count == 0)
154     {
155       guint min_interval = 0;
156
157       if (priv->min_next_frame_time != 0)
158         {
159           guint64 now = compute_frame_time (clock_idle);
160           guint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
161           min_interval = (min_interval_us + 500) / 1000;
162         }
163
164       if (priv->flush_idle_id == 0 &&
165           (priv->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
166         {
167           priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
168                                                               min_interval,
169                                                               gdk_frame_clock_flush_idle,
170                                                               g_object_ref (clock_idle),
171                                                               (GDestroyNotify) g_object_unref);
172         }
173
174       if (priv->paint_idle_id == 0 &&
175           !priv->in_paint_idle &&
176           (priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
177         {
178           priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
179                                                               min_interval,
180                                                               gdk_frame_clock_paint_idle,
181                                                               g_object_ref (clock_idle),
182                                                               (GDestroyNotify) g_object_unref);
183
184           gdk_frame_clock_frame_requested (GDK_FRAME_CLOCK (clock_idle));
185         }
186     }
187 }
188
189 static gboolean
190 gdk_frame_clock_flush_idle (void *data)
191 {
192   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
193   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
194   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
195
196   priv->flush_idle_id = 0;
197
198   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
199     return FALSE;
200
201   priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
202   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
203
204   g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
205
206   if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
207     priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
208   else
209     priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
210
211   return FALSE;
212 }
213
214 static gboolean
215 gdk_frame_clock_paint_idle (void *data)
216 {
217   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
218   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
219   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
220   gboolean skip_to_resume_events;
221
222   priv->paint_idle_id = 0;
223   priv->in_paint_idle = TRUE;
224
225   skip_to_resume_events =
226     (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0;
227
228   if (!skip_to_resume_events)
229     {
230       switch (priv->phase)
231         {
232         case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
233           break;
234         case GDK_FRAME_CLOCK_PHASE_NONE:
235         case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
236           if (priv->freeze_count == 0)
237             {
238               GdkFrameTimings *timings;
239               gint64 frame_counter;
240
241               priv->frame_time = compute_frame_time (clock_idle);
242               gdk_frame_history_begin_frame (priv->history);
243               frame_counter = gdk_frame_history_get_frame_counter (priv->history);
244               timings = gdk_frame_history_get_timings (priv->history, frame_counter);
245               gdk_frame_timings_set_frame_time (timings, priv->frame_time);
246
247               priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
248
249               /* We always emit ::before-paint and ::after-paint if
250                * any of the intermediate phases are requested and
251                * they don't get repeated if you freeze/thaw while
252                * in them. */
253               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
254               g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
255               priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
256             }
257         case GDK_FRAME_CLOCK_PHASE_UPDATE:
258           if (priv->freeze_count == 0)
259             {
260               if (priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE)
261                 {
262                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
263                   g_signal_emit_by_name (G_OBJECT (clock), "update");
264                 }
265             }
266         case GDK_FRAME_CLOCK_PHASE_LAYOUT:
267           if (priv->freeze_count == 0)
268             {
269               priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
270               if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
271                 {
272                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
273                   g_signal_emit_by_name (G_OBJECT (clock), "layout");
274                 }
275             }
276         case GDK_FRAME_CLOCK_PHASE_PAINT:
277           if (priv->freeze_count == 0)
278             {
279               priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
280               if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
281                 {
282                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
283                   g_signal_emit_by_name (G_OBJECT (clock), "paint");
284                 }
285             }
286         case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
287           if (priv->freeze_count == 0)
288             {
289               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
290               g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
291               /* the ::after-paint phase doesn't get repeated on freeze/thaw,
292                */
293               priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
294             }
295         case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
296           ;
297         }
298     }
299
300   if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
301     {
302       priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
303       g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
304     }
305
306   if (priv->freeze_count == 0)
307     priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
308
309   priv->in_paint_idle = FALSE;
310
311   if (priv->freeze_count == 0 && priv->requested != 0)
312     {
313       /* We need to start over again immediately - this implies that there is no
314        * throttling at the backend layer, so we need to back-off ourselves.
315        */
316       gdk_flush ();
317       priv->min_next_frame_time = priv->frame_time + FRAME_INTERVAL;
318       maybe_start_idle (clock_idle);
319     }
320   else
321     {
322       priv->min_next_frame_time = 0;
323     }
324
325   return FALSE;
326 }
327
328 static void
329 gdk_frame_clock_idle_request_phase (GdkFrameClock      *clock,
330                                     GdkFrameClockPhase  phase)
331 {
332   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
333   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
334
335   priv->requested |= phase;
336   maybe_start_idle (clock_idle);
337 }
338
339 static GdkFrameClockPhase
340 gdk_frame_clock_idle_get_requested (GdkFrameClock *clock)
341 {
342   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
343
344   return priv->requested;
345 }
346
347 static void
348 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
349 {
350   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
351
352   priv->freeze_count++;
353
354   if (priv->freeze_count == 1)
355     {
356       if (priv->flush_idle_id)
357         {
358           g_source_remove (priv->flush_idle_id);
359           priv->flush_idle_id = 0;
360         }
361       if (priv->paint_idle_id)
362         {
363           g_source_remove (priv->paint_idle_id);
364           priv->paint_idle_id = 0;
365         }
366     }
367 }
368
369 static void
370 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
371 {
372   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
373   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
374
375   g_return_if_fail (priv->freeze_count > 0);
376
377   priv->freeze_count--;
378   if (priv->freeze_count == 0)
379     {
380       maybe_start_idle (clock_idle);
381       /* If nothing is requested so we didn't start an idle, we need
382        * to skip to the end of the state chain, since the idle won't
383        * run and do it for us. */
384       if (priv->paint_idle_id == 0)
385         priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
386     }
387 }
388
389 static GdkFrameHistory *
390 gdk_frame_clock_idle_get_history (GdkFrameClock *clock)
391 {
392   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
393   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
394
395   return priv->history;
396 }
397
398 static void
399 gdk_frame_clock_idle_interface_init (GdkFrameClockInterface *iface)
400 {
401   iface->get_frame_time = gdk_frame_clock_idle_get_frame_time;
402   iface->request_phase = gdk_frame_clock_idle_request_phase;
403   iface->get_requested = gdk_frame_clock_idle_get_requested;
404   iface->freeze = gdk_frame_clock_idle_freeze;
405   iface->thaw = gdk_frame_clock_idle_thaw;
406   iface->get_history = gdk_frame_clock_idle_get_history;
407 }
408
409 GdkFrameClock *
410 _gdk_frame_clock_idle_new (void)
411 {
412   GdkFrameClockIdle *clock;
413
414   clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
415
416   return GDK_FRAME_CLOCK (clock);
417 }