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