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