Debian Codegraph @

tests/conform/actor-pick.c

raw
1 #define CLUTTER_DISABLE_DEPRECATION_WARNINGS
2 #include <clutter/clutter.h>
3
4 #define STAGE_WIDTH 640
5 #define STAGE_HEIGHT 480
6 #define ACTORS_X 12
7 #define ACTORS_Y 16
8 #define SHIFT_STEP STAGE_WIDTH / ACTORS_X
9
10 typedef struct _State State;
11
12 struct _State
13 {
14 ClutterActor *stage;
15 int y, x;
16 ClutterActor *actors[ACTORS_X * ACTORS_Y];
17 guint actor_width, actor_height;
18 guint failed_pass;
19 guint failed_idx;
20 gboolean pass;
21 };
22
23 struct _ShiftEffect
24 {
25 ClutterShaderEffect parent_instance;
26 };
27
28 struct _ShiftEffectClass
29 {
30 ClutterShaderEffectClass parent_class;
31 };
32
33 typedef struct _ShiftEffect ShiftEffect;
34 typedef struct _ShiftEffectClass ShiftEffectClass;
35
36 #define TYPE_SHIFT_EFFECT (shift_effect_get_type ())
37
38 GType shift_effect_get_type (void);
39
40 G_DEFINE_TYPE (ShiftEffect,
41 shift_effect,
42 CLUTTER_TYPE_SHADER_EFFECT);
43
44 static void
45 shader_paint (ClutterEffect *effect,
46 ClutterEffectPaintFlags flags)
47 {
48 ClutterShaderEffect *shader = CLUTTER_SHADER_EFFECT (effect);
49 float tex_width;
50 ClutterActor *actor =
51 clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
52
53 if (g_test_verbose ())
54 g_debug ("shader_paint");
55
56 clutter_shader_effect_set_shader_source (shader,
57 "uniform sampler2D tex;\n"
58 "uniform float step;\n"
59 "void main (void)\n"
60 "{\n"
61 " cogl_color_out = texture2D(tex, vec2 (cogl_tex_coord_in[0].s + step,\n"
62 " cogl_tex_coord_in[0].t));\n"
63 "}\n");
64
65 tex_width = clutter_actor_get_width (actor);
66
67 clutter_shader_effect_set_uniform (shader, "tex", G_TYPE_INT, 1, 0);
68 clutter_shader_effect_set_uniform (shader, "step", G_TYPE_FLOAT, 1,
69 SHIFT_STEP / tex_width);
70
71 CLUTTER_EFFECT_CLASS (shift_effect_parent_class)->paint (effect, flags);
72 }
73
74 static void
75 shader_pick (ClutterEffect *effect,
76 ClutterEffectPaintFlags flags)
77 {
78 shader_paint (effect, flags);
79 }
80
81 static void
82 shift_effect_class_init (ShiftEffectClass *klass)
83 {
84 ClutterEffectClass *shader_class = CLUTTER_EFFECT_CLASS (klass);
85
86 shader_class->paint = shader_paint;
87 shader_class->pick = shader_pick;
88 }
89
90 static void
91 shift_effect_init (ShiftEffect *self)
92 {
93 }
94
95 static const char *test_passes[] = {
96 "No covering actor",
97 "Invisible covering actor",
98 "Clipped covering actor",
99 "Blur effect",
100 "Shift effect",
101 };
102
103 static gboolean
104 on_timeout (gpointer data)
105 {
106 State *state = data;
107 int test_num = 0;
108 int y, x;
109 ClutterActor *over_actor = NULL;
110
111 /* This will cause an unclipped pick redraw that will get buffered.
112 We'll check below that this buffer is discarded because we also need
113 to pick non-reactive actors */
114 clutter_stage_get_actor_at_pos (CLUTTER_STAGE (state->stage),
115 CLUTTER_PICK_REACTIVE, 10, 10);
116
117 clutter_stage_get_actor_at_pos (CLUTTER_STAGE (state->stage),
118 CLUTTER_PICK_REACTIVE, 10, 10);
119
120 for (test_num = 0; test_num < G_N_ELEMENTS (test_passes); test_num++)
121 {
122 if (test_num == 0)
123 {
124 if (g_test_verbose ())
125 g_print ("No covering actor:\n");
126 }
127 if (test_num == 1)
128 {
129 static const ClutterColor red = { 0xff, 0x00, 0x00, 0xff };
130 /* Create an actor that covers the whole stage but that
131 isn't visible so it shouldn't affect the picking */
132 over_actor = clutter_rectangle_new_with_color (&red);
133 clutter_actor_set_size (over_actor, STAGE_WIDTH, STAGE_HEIGHT);
134 clutter_actor_add_child (state->stage, over_actor);
135 clutter_actor_hide (over_actor);
136
137 if (g_test_verbose ())
138 g_print ("Invisible covering actor:\n");
139 }
140 else if (test_num == 2)
141 {
142 /* Make the actor visible but set a clip so that only some
143 of the actors are accessible */
144 clutter_actor_show (over_actor);
145 clutter_actor_set_clip (over_actor,
146 state->actor_width * 2,
147 state->actor_height * 2,
148 state->actor_width * (ACTORS_X - 4),
149 state->actor_height * (ACTORS_Y - 4));
150
151 if (g_test_verbose ())
152 g_print ("Clipped covering actor:\n");
153 }
154 else if (test_num == 3)
155 {
156 if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
157 continue;
158
159 clutter_actor_hide (over_actor);
160
161 clutter_actor_add_effect_with_name (CLUTTER_ACTOR (state->stage),
162 "blur",
163 clutter_blur_effect_new ());
164
165 if (g_test_verbose ())
166 g_print ("With blur effect:\n");
167 }
168 else if (test_num == 4)
169 {
170 if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
171 continue;
172
173 clutter_actor_hide (over_actor);
174 clutter_actor_remove_effect_by_name (CLUTTER_ACTOR (state->stage),
175 "blur");
176
177 clutter_actor_add_effect_with_name (CLUTTER_ACTOR (state->stage),
178 "shift",
179 g_object_new (TYPE_SHIFT_EFFECT,
180 NULL));
181
182 if (g_test_verbose ())
183 g_print ("With shift effect:\n");
184 }
185
186 for (y = 0; y < ACTORS_Y; y++)
187 {
188 if (test_num == 4)
189 x = 1;
190 else
191 x = 0;
192
193 for (; x < ACTORS_X; x++)
194 {
195 gboolean pass = FALSE;
196 gfloat pick_x;
197 ClutterActor *actor;
198
199 pick_x = x * state->actor_width + state->actor_width / 2;
200
201 if (test_num == 4)
202 pick_x -= SHIFT_STEP;
203
204 actor =
205 clutter_stage_get_actor_at_pos (CLUTTER_STAGE (state->stage),
206 CLUTTER_PICK_ALL,
207 pick_x,
208 y * state->actor_height
209 + state->actor_height / 2);
210
211 if (g_test_verbose ())
212 g_print ("% 3i,% 3i / %p -> ",
213 x, y, state->actors[y * ACTORS_X + x]);
214
215 if (actor == NULL)
216 {
217 if (g_test_verbose ())
218 g_print ("NULL: FAIL\n");
219 }
220 else if (actor == over_actor)
221 {
222 if (test_num == 2
223 && x >= 2 && x < ACTORS_X - 2
224 && y >= 2 && y < ACTORS_Y - 2)
225 pass = TRUE;
226
227 if (g_test_verbose ())
228 g_print ("over_actor: %s\n", pass ? "pass" : "FAIL");
229 }
230 else
231 {
232 if (actor == state->actors[y * ACTORS_X + x]
233 && (test_num != 2
234 || x < 2 || x >= ACTORS_X - 2
235 || y < 2 || y >= ACTORS_Y - 2))
236 pass = TRUE;
237
238 if (g_test_verbose ())
239 g_print ("%p: %s\n", actor, pass ? "pass" : "FAIL");
240 }
241
242 if (!pass)
243 {
244 state->failed_pass = test_num;
245 state->failed_idx = y * ACTORS_X + x;
246 state->pass = FALSE;
247 }
248 }
249 }
250 }
251
252 clutter_main_quit ();
253
254 return G_SOURCE_REMOVE;
255 }
256
257 static void
258 actor_pick (void)
259 {
260 int y, x;
261 State state;
262
263 state.pass = TRUE;
264
265 state.stage = clutter_test_get_stage ();
266
267 state.actor_width = STAGE_WIDTH / ACTORS_X;
268 state.actor_height = STAGE_HEIGHT / ACTORS_Y;
269
270 for (y = 0; y < ACTORS_Y; y++)
271 for (x = 0; x < ACTORS_X; x++)
272 {
273 ClutterColor color = { x * 255 / (ACTORS_X - 1),
274 y * 255 / (ACTORS_Y - 1),
275 128, 255 };
276 ClutterActor *rect = clutter_rectangle_new_with_color (&color);
277
278 clutter_actor_set_position (rect,
279 x * state.actor_width,
280 y * state.actor_height);
281 clutter_actor_set_size (rect,
282 state.actor_width,
283 state.actor_height);
284
285 clutter_actor_add_child (state.stage, rect);
286
287 state.actors[y * ACTORS_X + x] = rect;
288 }
289
290 clutter_actor_show (state.stage);
291
292 clutter_threads_add_idle (on_timeout, &state);
293
294 clutter_main ();
295
296 if (g_test_verbose ())
297 {
298 if (!state.pass)
299 g_test_message ("Failed pass: %s[%d], actor index: %d [%p]\n",
300 test_passes[state.failed_pass],
301 state.failed_pass,
302 state.failed_idx,
303 state.actors[state.failed_idx]);
304 }
305
306 g_assert (state.pass);
307 }
308
309 CLUTTER_TEST_SUITE (
310 CLUTTER_TEST_UNIT ("/actor/pick", actor_pick)
311 )