Debian Codegraph @

tests/conform/cairo-texture.c

raw
1 #include <clutter/clutter.h>
2 #include <cogl/cogl.h>
3
4 #include "test-conform-common.h"
5
6 #define BLOCK_SIZE 16
7
8 /* Number of pixels at the border of a block to skip when verifying */
9 #define TEST_INSET 1
10
11 static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff };
12
13 typedef enum
14 {
15 /* The first frame is drawn using clutter_cairo_texture_create. The
16 second frame is an update of the first frame using
17 clutter_cairo_texture_create_region. The states are stored like
18 this because the cairo drawing is done on idle and the validation
19 is done during paint and we need to synchronize the two */
20 TEST_BEFORE_DRAW_FIRST_FRAME,
21 TEST_BEFORE_VALIDATE_FIRST_FRAME,
22 TEST_BEFORE_DRAW_SECOND_FRAME,
23 TEST_BEFORE_VALIDATE_SECOND_FRAME,
24 TEST_DONE
25 } TestProgress;
26
27 typedef struct _TestState
28 {
29 ClutterActor *stage;
30 ClutterActor *ct;
31 guint frame;
32 TestProgress progress;
33 } TestState;
34
35 static void
36 validate_part (int block_x, int block_y, const ClutterColor *color)
37 {
38 guint8 data[BLOCK_SIZE * BLOCK_SIZE * 4];
39 int x, y;
40
41 cogl_read_pixels (block_x * BLOCK_SIZE,
42 block_y * BLOCK_SIZE,
43 BLOCK_SIZE, BLOCK_SIZE,
44 COGL_READ_PIXELS_COLOR_BUFFER,
45 COGL_PIXEL_FORMAT_RGBA_8888_PRE,
46 data);
47
48 for (x = 0; x < BLOCK_SIZE - TEST_INSET * 2; x++)
49 for (y = 0; y < BLOCK_SIZE - TEST_INSET * 2; y++)
50 {
51 const guint8 *p = data + ((x + TEST_INSET) * 4 +
52 (y + TEST_INSET) * BLOCK_SIZE * 4);
53
54 g_assert_cmpint (p[0], ==, color->red);
55 g_assert_cmpint (p[1], ==, color->green);
56 g_assert_cmpint (p[2], ==, color->blue);
57 }
58 }
59
60 static void
61 paint_cb (ClutterActor *actor, TestState *state)
62 {
63 static const ClutterColor red = { 0xff, 0x00, 0x00, 0xff };
64 static const ClutterColor green = { 0x00, 0xff, 0x00, 0xff };
65 static const ClutterColor blue = { 0x00, 0x00, 0xff, 0xff };
66
67 if (state->frame++ < 2)
68 return;
69
70 switch (state->progress)
71 {
72 case TEST_BEFORE_DRAW_FIRST_FRAME:
73 case TEST_BEFORE_DRAW_SECOND_FRAME:
74 case TEST_DONE:
75 /* Handled by the idle callback */
76 break;
77
78 case TEST_BEFORE_VALIDATE_FIRST_FRAME:
79 /* In the first frame there is a red rectangle next to a green
80 rectangle */
81 validate_part (0, 0, &red);
82 validate_part (1, 0, &green);
83
84 state->progress = TEST_BEFORE_DRAW_SECOND_FRAME;
85 break;
86
87 case TEST_BEFORE_VALIDATE_SECOND_FRAME:
88 /* The second frame is the same except the green rectangle is
89 replaced with a blue one */
90 validate_part (0, 0, &red);
91 validate_part (1, 0, &blue);
92
93 state->progress = TEST_DONE;
94 break;
95 }
96 }
97
98 static gboolean
99 idle_cb (gpointer data)
100 {
101 TestState *state = data;
102 cairo_t *cr;
103
104 if (state->frame < 2)
105 clutter_actor_queue_redraw (CLUTTER_ACTOR (state->stage));
106 else
107 switch (state->progress)
108 {
109 case TEST_BEFORE_DRAW_FIRST_FRAME:
110 /* Draw two different colour rectangles */
111 cr = clutter_cairo_texture_create (CLUTTER_CAIRO_TEXTURE (state->ct));
112
113 cairo_save (cr);
114 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
115
116 cairo_save (cr);
117 cairo_rectangle (cr, 0, 0, BLOCK_SIZE, BLOCK_SIZE);
118 cairo_clip (cr);
119 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
120 cairo_paint (cr);
121 cairo_restore (cr);
122
123 cairo_rectangle (cr, BLOCK_SIZE, 0, BLOCK_SIZE, BLOCK_SIZE);
124 cairo_clip (cr);
125 cairo_set_source_rgb (cr, 0.0, 1.0, 0.0);
126 cairo_paint (cr);
127
128 cairo_restore (cr);
129
130 cairo_destroy (cr);
131
132 state->progress = TEST_BEFORE_VALIDATE_FIRST_FRAME;
133
134 break;
135
136 case TEST_BEFORE_DRAW_SECOND_FRAME:
137 /* Replace the second rectangle with a blue one */
138 cr = clutter_cairo_texture_create (CLUTTER_CAIRO_TEXTURE (state->ct));
139
140 cairo_rectangle (cr, BLOCK_SIZE, 0, BLOCK_SIZE, BLOCK_SIZE);
141 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
142 cairo_fill (cr);
143
144 cairo_destroy (cr);
145
146 state->progress = TEST_BEFORE_VALIDATE_SECOND_FRAME;
147
148 break;
149
150 case TEST_BEFORE_VALIDATE_FIRST_FRAME:
151 case TEST_BEFORE_VALIDATE_SECOND_FRAME:
152 /* Handled by the paint callback */
153 break;
154
155 case TEST_DONE:
156 clutter_main_quit ();
157 break;
158 }
159
160 return G_SOURCE_CONTINUE;
161 }
162
163 void
164 texture_cairo (TestConformSimpleFixture *fixture,
165 gconstpointer data)
166 {
167 TestState state;
168 unsigned int idle_source;
169 unsigned int paint_handler;
170
171 state.frame = 0;
172 state.stage = clutter_stage_new ();
173 state.progress = TEST_BEFORE_DRAW_FIRST_FRAME;
174
175 state.ct = clutter_cairo_texture_new (BLOCK_SIZE * 2, BLOCK_SIZE);
176 clutter_container_add_actor (CLUTTER_CONTAINER (state.stage), state.ct);
177
178 clutter_stage_set_color (CLUTTER_STAGE (state.stage), &stage_color);
179
180 /* We force continuous redrawing of the stage, since we need to skip
181 * the first few frames, and we wont be doing anything else that
182 * will trigger redrawing. */
183 idle_source = clutter_threads_add_idle (idle_cb, &state);
184 paint_handler = g_signal_connect_after (state.stage, "paint",
185 G_CALLBACK (paint_cb), &state);
186
187 clutter_actor_show (state.stage);
188 clutter_main ();
189
190 g_signal_handler_disconnect (state.stage, paint_handler);
191 g_source_remove (idle_source);
192
193 if (g_test_verbose ())
194 g_print ("OK\n");
195
196 clutter_actor_destroy (state.stage);
197 }
198