]> Pileus Git - ~andy/gtk/blob - gdk/gdkframeclockidle.c
win32: Fix build
[~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 "gdkinternals.h"
30 #include "gdkframeclockprivate.h"
31 #include "gdkframeclockidle.h"
32 #include "gdk.h"
33
34 #define FRAME_INTERVAL 16667 // microseconds
35
36 struct _GdkFrameClockIdlePrivate
37 {
38   GTimer *timer;
39   /* timer_base is used to avoid ever going backward */
40   gint64 timer_base;
41   gint64 frame_time;
42   gint64 min_next_frame_time;
43   gint64 sleep_serial;
44
45   guint flush_idle_id;
46   guint paint_idle_id;
47   guint freeze_count;
48
49   GdkFrameClockPhase requested;
50   GdkFrameClockPhase phase;
51
52   guint in_paint_idle : 1;
53 };
54
55 static gboolean gdk_frame_clock_flush_idle (void *data);
56 static gboolean gdk_frame_clock_paint_idle (void *data);
57
58 G_DEFINE_TYPE (GdkFrameClockIdle, gdk_frame_clock_idle, GDK_TYPE_FRAME_CLOCK)
59
60 static gint64 sleep_serial;
61 static gint64 sleep_source_prepare_time;
62 static GSource *sleep_source;
63
64 static gboolean
65 sleep_source_prepare (GSource *source,
66                       gint    *timeout)
67 {
68   sleep_source_prepare_time = g_source_get_time (source);
69   *timeout = -1;
70   return FALSE;
71 }
72
73 static gboolean
74 sleep_source_check (GSource *source)
75 {
76   if (g_source_get_time (source) != sleep_source_prepare_time)
77     sleep_serial++;
78
79   return FALSE;
80 }
81
82 static gboolean
83 sleep_source_dispatch (GSource     *source,
84                        GSourceFunc  callback,
85                        gpointer     user_data)
86 {
87   return TRUE;
88 }
89
90 static GSourceFuncs sleep_source_funcs = {
91   sleep_source_prepare,
92   sleep_source_check,
93   sleep_source_dispatch,
94   NULL /* finalize */
95 };
96
97 static gint64
98 get_sleep_serial (void)
99 {
100   if (sleep_source == NULL)
101     {
102       sleep_source = g_source_new (&sleep_source_funcs, sizeof (GSource));
103
104       g_source_set_priority (sleep_source, G_PRIORITY_HIGH);
105       g_source_attach (sleep_source, NULL);
106       g_source_unref (sleep_source);
107     }
108
109   return sleep_serial;
110 }
111
112 static void
113 gdk_frame_clock_idle_init (GdkFrameClockIdle *frame_clock_idle)
114 {
115   GdkFrameClockIdlePrivate *priv;
116
117   frame_clock_idle->priv = G_TYPE_INSTANCE_GET_PRIVATE (frame_clock_idle,
118                                                         GDK_TYPE_FRAME_CLOCK_IDLE,
119                                                         GdkFrameClockIdlePrivate);
120   priv = frame_clock_idle->priv;
121
122   priv->freeze_count = 0;
123 }
124
125 static void
126 gdk_frame_clock_idle_dispose (GObject *object)
127 {
128   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (object)->priv;
129
130   if (priv->flush_idle_id != 0)
131     {
132       g_source_remove (priv->flush_idle_id);
133       priv->flush_idle_id = 0;
134     }
135
136   if (priv->paint_idle_id != 0)
137     {
138       g_source_remove (priv->paint_idle_id);
139       priv->paint_idle_id = 0;
140     }
141
142   G_OBJECT_CLASS (gdk_frame_clock_idle_parent_class)->dispose (object);
143 }
144
145 static gint64
146 compute_frame_time (GdkFrameClockIdle *idle)
147 {
148   GdkFrameClockIdlePrivate *priv = idle->priv;
149   gint64 computed_frame_time;
150   gint64 elapsed;
151
152   elapsed = g_get_monotonic_time () + priv->timer_base;
153   if (elapsed < priv->frame_time)
154     {
155       /* clock went backward. adapt to that by forevermore increasing
156        * timer_base.  For now, assume we've gone forward in time 1ms.
157        */
158       /* hmm. just fix GTimer? */
159       computed_frame_time = priv->frame_time + 1;
160       priv->timer_base += (priv->frame_time - elapsed) + 1;
161     }
162   else
163     {
164       computed_frame_time = elapsed;
165     }
166
167   return computed_frame_time;
168 }
169
170 static gint64
171 gdk_frame_clock_idle_get_frame_time (GdkFrameClock *clock)
172 {
173   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
174   gint64 computed_frame_time;
175
176   /* can't change frame time during a paint */
177   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE &&
178       priv->phase != GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS)
179     return priv->frame_time;
180
181   /* Outside a paint, pick something close to "now" */
182   computed_frame_time = compute_frame_time (GDK_FRAME_CLOCK_IDLE (clock));
183
184   /* 16ms is 60fps. We only update frame time that often because we'd
185    * like to try to keep animations on the same start times.
186    * get_frame_time() would normally be used outside of a paint to
187    * record an animation start time for example.
188    */
189   if ((computed_frame_time - priv->frame_time) > FRAME_INTERVAL)
190     priv->frame_time = computed_frame_time;
191
192   return priv->frame_time;
193 }
194
195 static void
196 maybe_start_idle (GdkFrameClockIdle *clock_idle)
197 {
198   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
199
200   if (priv->freeze_count == 0 && priv->requested != 0)
201     {
202       guint min_interval = 0;
203
204       if (priv->min_next_frame_time != 0)
205         {
206           gint64 now = compute_frame_time (clock_idle);
207           gint64 min_interval_us = MAX (priv->min_next_frame_time, now) - now;
208           min_interval = (min_interval_us + 500) / 1000;
209         }
210
211       if (priv->flush_idle_id == 0 &&
212           (priv->requested & GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
213         {
214           priv->flush_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_EVENTS + 1,
215                                                               min_interval,
216                                                               gdk_frame_clock_flush_idle,
217                                                               g_object_ref (clock_idle),
218                                                               (GDestroyNotify) g_object_unref);
219         }
220
221       if (priv->paint_idle_id == 0 &&
222           !priv->in_paint_idle &&
223           (priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
224         {
225           priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
226                                                               min_interval,
227                                                               gdk_frame_clock_paint_idle,
228                                                               g_object_ref (clock_idle),
229                                                               (GDestroyNotify) g_object_unref);
230         }
231     }
232 }
233
234 static gint64
235 compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
236                              gint64             last_frame_time)
237 {
238   gint64 presentation_time;
239   gint64 refresh_interval;
240
241   gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
242                                     last_frame_time,
243                                     &refresh_interval, &presentation_time);
244
245   if (presentation_time == 0)
246     return last_frame_time + refresh_interval;
247   else
248     return presentation_time + refresh_interval / 2;
249 }
250
251 static gboolean
252 gdk_frame_clock_flush_idle (void *data)
253 {
254   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
255   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
256   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
257
258   priv->flush_idle_id = 0;
259
260   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
261     return FALSE;
262
263   priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
264   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
265
266   g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
267
268   if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0)
269     priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
270   else
271     priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
272
273   return FALSE;
274 }
275
276 static gboolean
277 gdk_frame_clock_paint_idle (void *data)
278 {
279   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
280   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
281   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
282   gboolean skip_to_resume_events;
283   GdkFrameTimings *timings = NULL;
284
285   priv->paint_idle_id = 0;
286   priv->in_paint_idle = TRUE;
287   priv->min_next_frame_time = 0;
288
289   skip_to_resume_events =
290     (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0;
291
292   if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
293     {
294       timings = gdk_frame_clock_get_current_timings (clock);
295     }
296
297   if (!skip_to_resume_events)
298     {
299       switch (priv->phase)
300         {
301         case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
302           break;
303         case GDK_FRAME_CLOCK_PHASE_NONE:
304         case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
305           if (priv->freeze_count == 0)
306             {
307               priv->frame_time = compute_frame_time (clock_idle);
308
309               _gdk_frame_clock_begin_frame (clock);
310               timings = gdk_frame_clock_get_current_timings (clock);
311
312               timings->frame_time = priv->frame_time;
313               timings->slept_before = priv->sleep_serial != get_sleep_serial ();
314
315               priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
316
317               /* We always emit ::before-paint and ::after-paint if
318                * any of the intermediate phases are requested and
319                * they don't get repeated if you freeze/thaw while
320                * in them. */
321               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
322               g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
323               priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
324             }
325         case GDK_FRAME_CLOCK_PHASE_UPDATE:
326           if (priv->freeze_count == 0)
327             {
328               if (priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE)
329                 {
330                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
331                   g_signal_emit_by_name (G_OBJECT (clock), "update");
332                 }
333             }
334         case GDK_FRAME_CLOCK_PHASE_LAYOUT:
335           if (priv->freeze_count == 0)
336             {
337 #ifdef G_ENABLE_DEBUG
338               if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
339                 {
340                   if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
341                       (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
342                     timings->layout_start_time = g_get_monotonic_time ();
343                 }
344 #endif /* G_ENABLE_DEBUG */
345
346               priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
347               if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
348                 {
349                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
350                   g_signal_emit_by_name (G_OBJECT (clock), "layout");
351                 }
352             }
353         case GDK_FRAME_CLOCK_PHASE_PAINT:
354           if (priv->freeze_count == 0)
355             {
356 #ifdef G_ENABLE_DEBUG
357               if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
358                 {
359                   if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
360                       (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
361                     timings->paint_start_time = g_get_monotonic_time ();
362                 }
363 #endif /* G_ENABLE_DEBUG */
364
365               priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
366               if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
367                 {
368                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
369                   g_signal_emit_by_name (G_OBJECT (clock), "paint");
370                 }
371             }
372         case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
373           if (priv->freeze_count == 0)
374             {
375               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
376               g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
377               /* the ::after-paint phase doesn't get repeated on freeze/thaw,
378                */
379               priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
380
381 #ifdef G_ENABLE_DEBUG
382               if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
383                 timings->frame_end_time = g_get_monotonic_time ();
384 #endif /* G_ENABLE_DEBUG */
385             }
386         case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
387           ;
388         }
389     }
390
391 #ifdef G_ENABLE_DEBUG
392   if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
393     {
394       if (timings->complete)
395         _gdk_frame_clock_debug_print_timings (clock, timings);
396     }
397 #endif /* G_ENABLE_DEBUG */
398
399   if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
400     {
401       priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
402       g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
403     }
404
405   if (priv->freeze_count == 0)
406     priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
407
408   priv->in_paint_idle = FALSE;
409
410   /* If there is throttling in the backend layer, then we'll do another
411    * update as soon as the backend unthrottles (if there is work to do),
412    * otherwise we need to figure when the next frame should be.
413    */
414   if (priv->freeze_count == 0)
415     {
416       priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
417                                                                priv->frame_time);
418       maybe_start_idle (clock_idle);
419     }
420
421   if (priv->freeze_count == 0)
422     priv->sleep_serial = get_sleep_serial ();
423
424   return FALSE;
425 }
426
427 static void
428 gdk_frame_clock_idle_request_phase (GdkFrameClock      *clock,
429                                     GdkFrameClockPhase  phase)
430 {
431   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
432   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
433
434   priv->requested |= phase;
435   maybe_start_idle (clock_idle);
436 }
437
438 static void
439 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
440 {
441   GdkFrameClockIdlePrivate *priv = GDK_FRAME_CLOCK_IDLE (clock)->priv;
442
443   priv->freeze_count++;
444
445   if (priv->freeze_count == 1)
446     {
447       if (priv->flush_idle_id)
448         {
449           g_source_remove (priv->flush_idle_id);
450           priv->flush_idle_id = 0;
451         }
452       if (priv->paint_idle_id)
453         {
454           g_source_remove (priv->paint_idle_id);
455           priv->paint_idle_id = 0;
456         }
457     }
458 }
459
460 static void
461 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
462 {
463   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
464   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
465
466   g_return_if_fail (priv->freeze_count > 0);
467
468   priv->freeze_count--;
469   if (priv->freeze_count == 0)
470     {
471       maybe_start_idle (clock_idle);
472       /* If nothing is requested so we didn't start an idle, we need
473        * to skip to the end of the state chain, since the idle won't
474        * run and do it for us. */
475       if (priv->paint_idle_id == 0)
476         priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
477
478       priv->sleep_serial = get_sleep_serial ();
479     }
480 }
481
482 static void
483 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
484 {
485   GObjectClass *gobject_class = (GObjectClass*) klass;
486   GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
487
488   gobject_class->dispose = gdk_frame_clock_idle_dispose;
489
490   frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
491   frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
492   frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
493   frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
494
495   g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
496 }
497
498 GdkFrameClock *
499 _gdk_frame_clock_idle_new (void)
500 {
501   GdkFrameClockIdle *clock;
502
503   clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
504
505   return GDK_FRAME_CLOCK (clock);
506 }