Debian Codegraph @

tests/conform/timeline-interpolate.c

raw
1 #include <stdlib.h>
2 #include <glib.h>
3 #include <clutter/clutter.h>
4
5 #include "test-conform-common.h"
6
7 /* We ask for 1 frame per millisecond.
8 * Whenever this rate can't be achieved then the timeline
9 * will interpolate the number frames that should have
10 * passed between timeouts. */
11 #define TEST_TIMELINE_FPS 1000
12 #define TEST_TIMELINE_DURATION 5000
13
14 /* We are at the mercy of the system scheduler so this
15 * may not be a very reliable tolerance. */
16 #define TEST_ERROR_TOLERANCE 20
17
18 typedef struct _TestState
19 {
20 ClutterTimeline *timeline;
21 GTimeVal start_time;
22 guint new_frame_counter;
23 gint expected_frame;
24 gint completion_count;
25 gboolean passed;
26 } TestState;
27
28
29 static void
30 new_frame_cb (ClutterTimeline *timeline,
31 gint frame_num,
32 TestState *state)
33 {
34 GTimeVal current_time;
35 gint current_frame;
36 glong msec_diff;
37 gint loop_overflow = 0;
38 static gint step = 1;
39
40 g_get_current_time (&current_time);
41
42 current_frame = clutter_timeline_get_elapsed_time (state->timeline);
43
44 msec_diff = (current_time.tv_sec - state->start_time.tv_sec) * 1000;
45 msec_diff += (current_time.tv_usec - state->start_time.tv_usec)/1000;
46
47 /* If we expect to have interpolated past the end of the timeline
48 * we keep track of the overflow so we can determine when
49 * the next timeout will happen. We then clip expected_frames
50 * to TEST_TIMELINE_DURATION since clutter-timeline
51 * semantics guaranty this frame is always signaled before
52 * looping */
53 if (state->expected_frame > TEST_TIMELINE_DURATION)
54 {
55 loop_overflow = state->expected_frame - TEST_TIMELINE_DURATION;
56 state->expected_frame = TEST_TIMELINE_DURATION;
57 }
58
59 if (current_frame >= (state->expected_frame-TEST_ERROR_TOLERANCE)
60 && current_frame <= (state->expected_frame+TEST_ERROR_TOLERANCE))
61 {
62 g_test_message ("elapsed milliseconds=%-5li "
63 "expected frame=%-4i actual frame=%-4i (OK)",
64 msec_diff,
65 state->expected_frame,
66 current_frame);
67 }
68 else
69 {
70 g_test_message ("elapsed milliseconds=%-5li "
71 "expected frame=%-4i actual frame=%-4i (FAILED)",
72 msec_diff,
73 state->expected_frame,
74 current_frame);
75 state->passed = FALSE;
76 }
77
78 if (step>0)
79 {
80 state->expected_frame = current_frame + (TEST_TIMELINE_FPS / 4);
81 g_test_message ("Sleeping for 250ms "
82 "so next frame should be (%i + %i) = %i",
83 current_frame,
84 (TEST_TIMELINE_FPS / 4),
85 state->expected_frame);
86 g_usleep (250000);
87 }
88 else
89 {
90 state->expected_frame = current_frame + TEST_TIMELINE_FPS;
91 g_test_message ("Sleeping for 1sec "
92 "so next frame should be (%i + %i) = %i",
93 current_frame,
94 TEST_TIMELINE_FPS,
95 state->expected_frame);
96 g_usleep (1000000);
97 }
98
99 if (current_frame >= TEST_TIMELINE_DURATION)
100 {
101 state->expected_frame += loop_overflow;
102 state->expected_frame -= TEST_TIMELINE_DURATION;
103 g_test_message ("End of timeline reached: "
104 "Wrapping expected frame too %i",
105 state->expected_frame);
106 }
107
108 state->new_frame_counter++;
109 step = -step;
110 }
111
112
113 static void
114 completed_cb (ClutterTimeline *timeline,
115 TestState *state)
116 {
117 state->completion_count++;
118
119 if (state->completion_count == 2)
120 {
121 if (state->passed)
122 {
123 g_test_message ("Passed\n");
124 clutter_main_quit ();
125 }
126 else
127 {
128 g_test_message ("Failed\n");
129 exit (EXIT_FAILURE);
130 }
131 }
132 }
133
134 void
135 timeline_interpolation (void)
136 {
137 TestState state;
138
139 state.timeline =
140 clutter_timeline_new (TEST_TIMELINE_DURATION);
141 clutter_timeline_set_loop (state.timeline, TRUE);
142 g_signal_connect (G_OBJECT(state.timeline),
143 "new-frame",
144 G_CALLBACK(new_frame_cb),
145 &state);
146 g_signal_connect (G_OBJECT(state.timeline),
147 "completed",
148 G_CALLBACK(completed_cb),
149 &state);
150
151 state.completion_count = 0;
152 state.new_frame_counter = 0;
153 state.passed = TRUE;
154 state.expected_frame = 0;
155
156 g_get_current_time (&state.start_time);
157 clutter_timeline_start (state.timeline);
158
159 clutter_main();
160
161 g_object_unref (state.timeline);
162 }