]> Pileus Git - ~andy/gtk/blob - gdk/gdkframeclockidle.c
stylecontext: Do invalidation on first resize container
[~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->in_paint_idle &&
251           priv->paint_idle_id == 0 && RUN_PAINT_IDLE (priv))
252         {
253           priv->paint_idle_id = gdk_threads_add_timeout_full (GDK_PRIORITY_REDRAW,
254                                                               min_interval,
255                                                               gdk_frame_clock_paint_idle,
256                                                               g_object_ref (clock_idle),
257                                                               (GDestroyNotify) g_object_unref);
258         }
259     }
260 }
261
262 static void
263 maybe_stop_idle (GdkFrameClockIdle *clock_idle)
264 {
265   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
266
267   if (priv->flush_idle_id != 0 && !RUN_FLUSH_IDLE (priv))
268     {
269       g_source_remove (priv->flush_idle_id);
270       priv->flush_idle_id = 0;
271     }
272
273   if (priv->paint_idle_id != 0 && !RUN_PAINT_IDLE (priv))
274     {
275       g_source_remove (priv->paint_idle_id);
276       priv->paint_idle_id = 0;
277     }
278 }
279
280 static gint64
281 compute_min_next_frame_time (GdkFrameClockIdle *clock_idle,
282                              gint64             last_frame_time)
283 {
284   gint64 presentation_time;
285   gint64 refresh_interval;
286
287   gdk_frame_clock_get_refresh_info (GDK_FRAME_CLOCK (clock_idle),
288                                     last_frame_time,
289                                     &refresh_interval, &presentation_time);
290
291   if (presentation_time == 0)
292     return last_frame_time + refresh_interval;
293   else
294     return presentation_time + refresh_interval / 2;
295 }
296
297 static gboolean
298 gdk_frame_clock_flush_idle (void *data)
299 {
300   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
301   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
302   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
303
304   priv->flush_idle_id = 0;
305
306   if (priv->phase != GDK_FRAME_CLOCK_PHASE_NONE)
307     return FALSE;
308
309   priv->phase = GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
310   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS;
311
312   g_signal_emit_by_name (G_OBJECT (clock), "flush-events");
313
314   if ((priv->requested & ~GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS) != 0 ||
315       priv->updating_count > 0)
316     priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
317   else
318     priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
319
320   return FALSE;
321 }
322
323 static gboolean
324 gdk_frame_clock_paint_idle (void *data)
325 {
326   GdkFrameClock *clock = GDK_FRAME_CLOCK (data);
327   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
328   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
329   gboolean skip_to_resume_events;
330   GdkFrameTimings *timings = NULL;
331
332   priv->paint_idle_id = 0;
333   priv->in_paint_idle = TRUE;
334   priv->min_next_frame_time = 0;
335
336   skip_to_resume_events =
337     (priv->requested & ~(GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS | GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)) == 0 &&
338     priv->updating_count == 0;
339
340   if (priv->phase > GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT)
341     {
342       timings = gdk_frame_clock_get_current_timings (clock);
343     }
344
345   if (!skip_to_resume_events)
346     {
347       switch (priv->phase)
348         {
349         case GDK_FRAME_CLOCK_PHASE_FLUSH_EVENTS:
350           break;
351         case GDK_FRAME_CLOCK_PHASE_NONE:
352         case GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT:
353           if (priv->freeze_count == 0)
354             {
355               priv->frame_time = compute_frame_time (clock_idle);
356
357               _gdk_frame_clock_begin_frame (clock);
358               timings = gdk_frame_clock_get_current_timings (clock);
359
360               timings->frame_time = priv->frame_time;
361               timings->slept_before = priv->sleep_serial != get_sleep_serial ();
362
363               priv->phase = GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
364
365               /* We always emit ::before-paint and ::after-paint if
366                * any of the intermediate phases are requested and
367                * they don't get repeated if you freeze/thaw while
368                * in them.
369                */
370               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_BEFORE_PAINT;
371               g_signal_emit_by_name (G_OBJECT (clock), "before-paint");
372               priv->phase = GDK_FRAME_CLOCK_PHASE_UPDATE;
373             }
374         case GDK_FRAME_CLOCK_PHASE_UPDATE:
375           if (priv->freeze_count == 0)
376             {
377               if ((priv->requested & GDK_FRAME_CLOCK_PHASE_UPDATE) != 0 ||
378                   priv->updating_count > 0)
379                 {
380                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_UPDATE;
381                   g_signal_emit_by_name (G_OBJECT (clock), "update");
382                 }
383             }
384         case GDK_FRAME_CLOCK_PHASE_LAYOUT:
385           if (priv->freeze_count == 0)
386             {
387 #ifdef G_ENABLE_DEBUG
388               if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
389                 {
390                   if (priv->phase != GDK_FRAME_CLOCK_PHASE_LAYOUT &&
391                       (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT))
392                     timings->layout_start_time = g_get_monotonic_time ();
393                 }
394 #endif /* G_ENABLE_DEBUG */
395
396               priv->phase = GDK_FRAME_CLOCK_PHASE_LAYOUT;
397               if (priv->requested & GDK_FRAME_CLOCK_PHASE_LAYOUT)
398                 {
399                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_LAYOUT;
400                   g_signal_emit_by_name (G_OBJECT (clock), "layout");
401                 }
402             }
403         case GDK_FRAME_CLOCK_PHASE_PAINT:
404           if (priv->freeze_count == 0)
405             {
406 #ifdef G_ENABLE_DEBUG
407               if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
408                 {
409                   if (priv->phase != GDK_FRAME_CLOCK_PHASE_PAINT &&
410                       (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT))
411                     timings->paint_start_time = g_get_monotonic_time ();
412                 }
413 #endif /* G_ENABLE_DEBUG */
414
415               priv->phase = GDK_FRAME_CLOCK_PHASE_PAINT;
416               if (priv->requested & GDK_FRAME_CLOCK_PHASE_PAINT)
417                 {
418                   priv->requested &= ~GDK_FRAME_CLOCK_PHASE_PAINT;
419                   g_signal_emit_by_name (G_OBJECT (clock), "paint");
420                 }
421             }
422         case GDK_FRAME_CLOCK_PHASE_AFTER_PAINT:
423           if (priv->freeze_count == 0)
424             {
425               priv->requested &= ~GDK_FRAME_CLOCK_PHASE_AFTER_PAINT;
426               g_signal_emit_by_name (G_OBJECT (clock), "after-paint");
427               /* the ::after-paint phase doesn't get repeated on freeze/thaw,
428                */
429               priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
430
431 #ifdef G_ENABLE_DEBUG
432               if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
433                 timings->frame_end_time = g_get_monotonic_time ();
434 #endif /* G_ENABLE_DEBUG */
435             }
436         case GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS:
437           ;
438         }
439     }
440
441 #ifdef G_ENABLE_DEBUG
442   if ((_gdk_debug_flags & GDK_DEBUG_FRAMES) != 0)
443     {
444       if (timings && timings->complete)
445         _gdk_frame_clock_debug_print_timings (clock, timings);
446     }
447 #endif /* G_ENABLE_DEBUG */
448
449   if (priv->requested & GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS)
450     {
451       priv->requested &= ~GDK_FRAME_CLOCK_PHASE_RESUME_EVENTS;
452       g_signal_emit_by_name (G_OBJECT (clock), "resume-events");
453     }
454
455   if (priv->freeze_count == 0)
456     priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
457
458   priv->in_paint_idle = FALSE;
459
460   /* If there is throttling in the backend layer, then we'll do another
461    * update as soon as the backend unthrottles (if there is work to do),
462    * otherwise we need to figure when the next frame should be.
463    */
464   if (priv->freeze_count == 0)
465     {
466       priv->min_next_frame_time = compute_min_next_frame_time (clock_idle,
467                                                                priv->frame_time);
468       maybe_start_idle (clock_idle);
469     }
470
471   if (priv->freeze_count == 0)
472     priv->sleep_serial = get_sleep_serial ();
473
474   return FALSE;
475 }
476
477 static void
478 gdk_frame_clock_idle_request_phase (GdkFrameClock      *clock,
479                                     GdkFrameClockPhase  phase)
480 {
481   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
482   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
483
484   priv->requested |= phase;
485   maybe_start_idle (clock_idle);
486 }
487
488 static void
489 gdk_frame_clock_idle_begin_updating (GdkFrameClock *clock)
490 {
491   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
492   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
493
494 #ifdef G_OS_WIN32
495   /* We need a higher resolution timer while doing animations */
496   if (priv->updating_count == 0 && !priv->begin_period)
497     {
498       timeBeginPeriod(1);
499       priv->begin_period = TRUE;
500     }
501 #endif
502
503   priv->updating_count++;
504   maybe_start_idle (clock_idle);
505 }
506
507 static void
508 gdk_frame_clock_idle_end_updating (GdkFrameClock *clock)
509 {
510   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
511   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
512
513   g_return_if_fail (priv->updating_count > 0);
514
515   priv->updating_count--;
516   maybe_stop_idle (clock_idle);
517
518 #ifdef G_OS_WIN32
519   if (priv->updating_count == 0 && priv->begin_period)
520     {
521       timeEndPeriod(1);
522       priv->begin_period = FALSE;
523     }
524 #endif
525 }
526
527 static void
528 gdk_frame_clock_idle_freeze (GdkFrameClock *clock)
529 {
530   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
531   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
532
533   priv->freeze_count++;
534   maybe_stop_idle (clock_idle);
535 }
536
537 static void
538 gdk_frame_clock_idle_thaw (GdkFrameClock *clock)
539 {
540   GdkFrameClockIdle *clock_idle = GDK_FRAME_CLOCK_IDLE (clock);
541   GdkFrameClockIdlePrivate *priv = clock_idle->priv;
542
543   g_return_if_fail (priv->freeze_count > 0);
544
545   priv->freeze_count--;
546   if (priv->freeze_count == 0)
547     {
548       maybe_start_idle (clock_idle);
549       /* If nothing is requested so we didn't start an idle, we need
550        * to skip to the end of the state chain, since the idle won't
551        * run and do it for us.
552        */
553       if (priv->paint_idle_id == 0)
554         priv->phase = GDK_FRAME_CLOCK_PHASE_NONE;
555
556       priv->sleep_serial = get_sleep_serial ();
557     }
558 }
559
560 static void
561 gdk_frame_clock_idle_class_init (GdkFrameClockIdleClass *klass)
562 {
563   GObjectClass *gobject_class = (GObjectClass*) klass;
564   GdkFrameClockClass *frame_clock_class = (GdkFrameClockClass *)klass;
565
566   gobject_class->dispose = gdk_frame_clock_idle_dispose;
567
568   frame_clock_class->get_frame_time = gdk_frame_clock_idle_get_frame_time;
569   frame_clock_class->request_phase = gdk_frame_clock_idle_request_phase;
570   frame_clock_class->begin_updating = gdk_frame_clock_idle_begin_updating;
571   frame_clock_class->end_updating = gdk_frame_clock_idle_end_updating;
572   frame_clock_class->freeze = gdk_frame_clock_idle_freeze;
573   frame_clock_class->thaw = gdk_frame_clock_idle_thaw;
574
575   g_type_class_add_private (klass, sizeof (GdkFrameClockIdlePrivate));
576 }
577
578 GdkFrameClock *
579 _gdk_frame_clock_idle_new (void)
580 {
581   GdkFrameClockIdle *clock;
582
583   clock = g_object_new (GDK_TYPE_FRAME_CLOCK_IDLE, NULL);
584
585   return GDK_FRAME_CLOCK (clock);
586 }