]> Pileus Git - ~andy/gtk/blob - gdk/gdkframehistory.c
e993e5e3f6ea542ee762f03a1fca44c789b7f3eb
[~andy/gtk] / gdk / gdkframehistory.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2012 Red Hat, Inc.
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, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gdkframehistory.h"
21
22 #define FRAME_HISTORY_MAX_LENGTH 16
23
24 struct _GdkFrameHistory
25 {
26   GObject parent_instance;
27
28   gint64 frame_counter;
29   gint n_timings;
30   gint current;
31   GdkFrameTimings *timings[FRAME_HISTORY_MAX_LENGTH];
32 };
33
34 struct _GdkFrameHistoryClass
35 {
36   GObjectClass parent_class;
37 };
38
39 G_DEFINE_TYPE (GdkFrameHistory, gdk_frame_history, G_TYPE_OBJECT)
40
41 static void
42 gdk_frame_history_finalize (GObject *object)
43 {
44   GdkFrameHistory *history = GDK_FRAME_HISTORY (object);
45   int i;
46
47   for (i = 0; i < FRAME_HISTORY_MAX_LENGTH; i++)
48     if (history->timings[i] != 0)
49       gdk_frame_timings_unref (history->timings[i]);
50
51   G_OBJECT_CLASS (gdk_frame_history_parent_class)->finalize (object);
52 }
53
54 static void
55 gdk_frame_history_class_init (GdkFrameHistoryClass *klass)
56 {
57   GObjectClass *object_class = G_OBJECT_CLASS (klass);
58
59   object_class->finalize = gdk_frame_history_finalize;
60 }
61
62 static void
63 gdk_frame_history_init (GdkFrameHistory *history)
64 {
65   history->frame_counter = -1;
66   history->current = FRAME_HISTORY_MAX_LENGTH - 1;
67 }
68
69 GdkFrameHistory *
70 gdk_frame_history_new (void)
71 {
72   return g_object_new (GDK_TYPE_FRAME_HISTORY, NULL);
73 }
74
75 gint64
76 gdk_frame_history_get_frame_counter (GdkFrameHistory *history)
77 {
78   g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), 0);
79
80   return history->frame_counter;
81 }
82
83 gint64
84 gdk_frame_history_get_start (GdkFrameHistory *history)
85 {
86   g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), 0);
87
88   return history->frame_counter + 1 - history->n_timings;
89 }
90
91 void
92 gdk_frame_history_begin_frame (GdkFrameHistory *history)
93 {
94   g_return_if_fail (GDK_IS_FRAME_HISTORY (history));
95
96   history->frame_counter++;
97   history->current = (history->current + 1) % FRAME_HISTORY_MAX_LENGTH;
98
99   if (history->n_timings < FRAME_HISTORY_MAX_LENGTH)
100     history->n_timings++;
101   else
102     {
103       gdk_frame_timings_unref(history->timings[history->current]);
104     }
105
106   history->timings[history->current] = gdk_frame_timings_new (history->frame_counter);
107 }
108
109 GdkFrameTimings *
110 gdk_frame_history_get_timings (GdkFrameHistory *history,
111                                gint64           frame_counter)
112 {
113   gint pos;
114
115   g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), NULL);
116
117   if (frame_counter > history->frame_counter)
118     return NULL;
119
120   if (frame_counter <= history->frame_counter - history->n_timings)
121     return NULL;
122
123   pos = (history->current - (history->frame_counter - frame_counter) + FRAME_HISTORY_MAX_LENGTH) % FRAME_HISTORY_MAX_LENGTH;
124
125   return history->timings[pos];
126 }
127
128 GdkFrameTimings *
129 gdk_frame_history_get_last_complete (GdkFrameHistory *history)
130 {
131   gint i;
132
133   g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), NULL);
134
135   for (i = 0; i < history->n_timings; i++)
136     {
137       gint pos = ((history->current - i) + FRAME_HISTORY_MAX_LENGTH) % FRAME_HISTORY_MAX_LENGTH;
138       if (gdk_frame_timings_get_complete (history->timings[pos]))
139         return history->timings[pos];
140     }
141
142   return NULL;
143 }