| 1 |
/* |
| 2 |
* Clutter. |
| 3 |
* |
| 4 |
* An OpenGL based 'interactive canvas' library. |
| 5 |
* |
| 6 |
* Authored By Neil Roberts <[email protected]> |
| 7 |
* |
| 8 |
* Copyright (C) 2009 Intel Corporation. |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or |
| 11 |
* modify it under the terms of the GNU Lesser General Public |
| 12 |
* License as published by the Free Software Foundation; either |
| 13 |
* version 2 of the License, or (at your option) any later version. |
| 14 |
* |
| 15 |
* This library is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
* Lesser General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU Lesser General Public |
| 21 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include "config.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#define CLUTTER_DISABLE_DEPRECATION_WARNINGS |
| 29 |
|
| 30 |
/* This file contains the common code to check whether an interval has |
| 31 |
expired used in clutter-frame-source and clutter-timeout-pool. */ |
| 32 |
|
| 33 |
#include "clutter-timeout-interval.h" |
| 34 |
|
| 35 |
void |
| 36 |
_clutter_timeout_interval_init (ClutterTimeoutInterval *interval, |
| 37 |
guint fps) |
| 38 |
{ |
| 39 |
#if GLIB_CHECK_VERSION (2, 27, 3) |
| 40 |
interval->start_time = g_get_monotonic_time () / 1000; |
| 41 |
#else |
| 42 |
{ |
| 43 |
GTimeVal start_time; |
| 44 |
g_get_current_time (&start_time); |
| 45 |
interval->start_time = start_time.tv_sec * 1000 |
| 46 |
+ start_time.tv_usec / 1000; |
| 47 |
} |
| 48 |
#endif |
| 49 |
|
| 50 |
interval->fps = fps; |
| 51 |
interval->frame_count = 0; |
| 52 |
} |
| 53 |
|
| 54 |
static gint64 |
| 55 |
_clutter_timeout_interval_get_ticks (gint64 current_time, |
| 56 |
ClutterTimeoutInterval *interval) |
| 57 |
{ |
| 58 |
return MAX (current_time - interval->start_time, 0); |
| 59 |
} |
| 60 |
|
| 61 |
gboolean |
| 62 |
_clutter_timeout_interval_prepare (gint64 current_time, |
| 63 |
ClutterTimeoutInterval *interval, |
| 64 |
gint *delay) |
| 65 |
{ |
| 66 |
gint elapsed_time, new_frame_num; |
| 67 |
|
| 68 |
elapsed_time = _clutter_timeout_interval_get_ticks (current_time, interval); |
| 69 |
new_frame_num = elapsed_time * interval->fps / 1000; |
| 70 |
|
| 71 |
/* If time has gone backwards or the time since the last frame is |
| 72 |
greater than the two frames worth then reset the time and do a |
| 73 |
frame now */ |
| 74 |
if (new_frame_num < interval->frame_count || |
| 75 |
new_frame_num - interval->frame_count > 2) |
| 76 |
{ |
| 77 |
/* Get the frame time rounded up to the nearest ms */ |
| 78 |
guint frame_time = (1000 + interval->fps - 1) / interval->fps; |
| 79 |
|
| 80 |
/* Reset the start time */ |
| 81 |
interval->start_time = current_time; |
| 82 |
|
| 83 |
/* Move the start time as if one whole frame has elapsed */ |
| 84 |
interval->start_time -= frame_time; |
| 85 |
|
| 86 |
interval->frame_count = 0; |
| 87 |
|
| 88 |
if (delay) |
| 89 |
*delay = 0; |
| 90 |
|
| 91 |
return TRUE; |
| 92 |
} |
| 93 |
else if (new_frame_num > interval->frame_count) |
| 94 |
{ |
| 95 |
if (delay) |
| 96 |
*delay = 0; |
| 97 |
|
| 98 |
return TRUE; |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
if (delay) |
| 103 |
*delay = ((interval->frame_count + 1) * 1000 / interval->fps |
| 104 |
- elapsed_time); |
| 105 |
|
| 106 |
return FALSE; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
gboolean |
| 111 |
_clutter_timeout_interval_dispatch (ClutterTimeoutInterval *interval, |
| 112 |
GSourceFunc callback, |
| 113 |
gpointer user_data) |
| 114 |
{ |
| 115 |
if ((* callback) (user_data)) |
| 116 |
{ |
| 117 |
interval->frame_count++; |
| 118 |
|
| 119 |
return TRUE; |
| 120 |
} |
| 121 |
|
| 122 |
return FALSE; |
| 123 |
} |
| 124 |
|
| 125 |
gint |
| 126 |
_clutter_timeout_interval_compare_expiration (const ClutterTimeoutInterval *a, |
| 127 |
const ClutterTimeoutInterval *b) |
| 128 |
{ |
| 129 |
guint a_delay = 1000 / a->fps; |
| 130 |
guint b_delay = 1000 / b->fps; |
| 131 |
gint64 b_difference; |
| 132 |
gint comparison; |
| 133 |
|
| 134 |
b_difference = a->start_time - b->start_time; |
| 135 |
|
| 136 |
comparison = ((gint) ((a->frame_count + 1) * a_delay) |
| 137 |
- (gint) ((b->frame_count + 1) * b_delay + b_difference)); |
| 138 |
|
| 139 |
return (comparison < 0 ? -1 |
| 140 |
: comparison > 0 ? 1 |
| 141 |
: 0); |
| 142 |
} |