1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2012 Red Hat, Inc.
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.
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.
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/>.
20 #include "gdkframehistory.h"
22 #define FRAME_HISTORY_MAX_LENGTH 16
24 struct _GdkFrameHistory
26 GObject parent_instance;
31 GdkFrameTimings *timings[FRAME_HISTORY_MAX_LENGTH];
34 struct _GdkFrameHistoryClass
36 GObjectClass parent_class;
39 G_DEFINE_TYPE (GdkFrameHistory, gdk_frame_history, G_TYPE_OBJECT)
42 gdk_frame_history_finalize (GObject *object)
44 GdkFrameHistory *history = GDK_FRAME_HISTORY (object);
47 for (i = 0; i < FRAME_HISTORY_MAX_LENGTH; i++)
48 if (history->timings[i] != 0)
49 gdk_frame_timings_unref (history->timings[i]);
51 G_OBJECT_CLASS (gdk_frame_history_parent_class)->finalize (object);
55 gdk_frame_history_class_init (GdkFrameHistoryClass *klass)
57 GObjectClass *object_class = G_OBJECT_CLASS (klass);
59 object_class->finalize = gdk_frame_history_finalize;
63 gdk_frame_history_init (GdkFrameHistory *history)
65 history->frame_counter = -1;
66 history->current = FRAME_HISTORY_MAX_LENGTH - 1;
70 gdk_frame_history_new (void)
72 return g_object_new (GDK_TYPE_FRAME_HISTORY, NULL);
76 gdk_frame_history_get_frame_counter (GdkFrameHistory *history)
78 g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), 0);
80 return history->frame_counter;
84 gdk_frame_history_get_start (GdkFrameHistory *history)
86 g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), 0);
88 return history->frame_counter + 1 - history->n_timings;
92 gdk_frame_history_begin_frame (GdkFrameHistory *history)
94 g_return_if_fail (GDK_IS_FRAME_HISTORY (history));
96 history->frame_counter++;
97 history->current = (history->current + 1) % FRAME_HISTORY_MAX_LENGTH;
99 if (history->n_timings < FRAME_HISTORY_MAX_LENGTH)
100 history->n_timings++;
103 gdk_frame_timings_unref(history->timings[history->current]);
106 history->timings[history->current] = gdk_frame_timings_new (history->frame_counter);
110 gdk_frame_history_get_timings (GdkFrameHistory *history,
111 gint64 frame_counter)
115 g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), NULL);
117 if (frame_counter > history->frame_counter)
120 if (frame_counter <= history->frame_counter - history->n_timings)
123 pos = (history->current - (history->frame_counter - frame_counter) + FRAME_HISTORY_MAX_LENGTH) % FRAME_HISTORY_MAX_LENGTH;
125 return history->timings[pos];
129 gdk_frame_history_get_last_complete (GdkFrameHistory *history)
133 g_return_val_if_fail (GDK_IS_FRAME_HISTORY (history), NULL);
135 for (i = 0; i < history->n_timings; i++)
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];