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