File: clutter-timeout-interval.c

package info (click to toggle)
clutter-1.0 1.26.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,352 kB
  • sloc: ansic: 128,533; sh: 5,580; xml: 1,641; makefile: 1,613; ruby: 149; perl: 142; sed: 16
file content (142 lines) | stat: -rw-r--r-- 4,041 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Authored By Neil Roberts  <neil@linux.intel.com>
 *
 * Copyright (C) 2009  Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define CLUTTER_DISABLE_DEPRECATION_WARNINGS

/* This file contains the common code to check whether an interval has
   expired used in clutter-frame-source and clutter-timeout-pool. */

#include "clutter-timeout-interval.h"

void
_clutter_timeout_interval_init (ClutterTimeoutInterval *interval,
                                guint                   fps)
{
#if GLIB_CHECK_VERSION (2, 27, 3)
  interval->start_time = g_get_monotonic_time () / 1000;
#else
  {
    GTimeVal start_time;
    g_get_current_time (&start_time);
    interval->start_time = start_time.tv_sec * 1000
                         + start_time.tv_usec / 1000;
  }
#endif

  interval->fps = fps;
  interval->frame_count = 0;
}

static gint64
_clutter_timeout_interval_get_ticks (gint64                  current_time,
                                     ClutterTimeoutInterval *interval)
{
  return MAX (current_time - interval->start_time, 0);
}

gboolean
_clutter_timeout_interval_prepare (gint64                  current_time,
                                   ClutterTimeoutInterval *interval,
                                   gint                   *delay)
{
  gint elapsed_time, new_frame_num;

  elapsed_time = _clutter_timeout_interval_get_ticks (current_time, interval);
  new_frame_num = elapsed_time * interval->fps / 1000;

  /* If time has gone backwards or the time since the last frame is
     greater than the two frames worth then reset the time and do a
     frame now */
  if (new_frame_num < interval->frame_count ||
      new_frame_num - interval->frame_count > 2)
    {
      /* Get the frame time rounded up to the nearest ms */
      guint frame_time = (1000 + interval->fps - 1) / interval->fps;

      /* Reset the start time */
      interval->start_time = current_time;

      /* Move the start time as if one whole frame has elapsed */
      interval->start_time -= frame_time;

      interval->frame_count = 0;

      if (delay)
	*delay = 0;

      return TRUE;
    }
  else if (new_frame_num > interval->frame_count)
    {
      if (delay)
	*delay = 0;

      return TRUE;
    }
  else
    {
      if (delay)
	*delay = ((interval->frame_count + 1) * 1000 / interval->fps
               - elapsed_time);

      return FALSE;
    }
}

gboolean
_clutter_timeout_interval_dispatch (ClutterTimeoutInterval *interval,
                                    GSourceFunc             callback,
                                    gpointer                user_data)
{
  if ((* callback) (user_data))
    {
      interval->frame_count++;

      return TRUE;
    }

  return FALSE;
}

gint
_clutter_timeout_interval_compare_expiration (const ClutterTimeoutInterval *a,
                                              const ClutterTimeoutInterval *b)
{
  guint a_delay = 1000 / a->fps;
  guint b_delay = 1000 / b->fps;
  gint64 b_difference;
  gint comparison;

  b_difference = a->start_time - b->start_time;

  comparison = ((gint) ((a->frame_count + 1) * a_delay)
             - (gint) ((b->frame_count + 1) * b_delay + b_difference));

  return (comparison < 0 ? -1
                         : comparison > 0 ? 1
                                          : 0);
}