1
#include <clutter/clutter.h>
5
#include "test-conform-common.h"
7
#define TILE_SIZE 32.0f
17
typedef struct test_tile
25
static const ClutterColor
30
static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff };
32
typedef struct _TestState
42
create_texture_from_buffer (CoglHandle buffer)
46
texture = cogl_texture_new_from_buffer (buffer,
48
COGL_TEXTURE_NO_SLICING,
49
COGL_PIXEL_FORMAT_RGBA_8888,
50
COGL_PIXEL_FORMAT_RGBA_8888,
54
g_assert (texture != COGL_INVALID_HANDLE);
60
create_map_tile (TestTile *tile)
66
buffer = cogl_pixel_buffer_new (TILE_SIZE * TILE_SIZE * 4);
68
g_assert (cogl_is_pixel_buffer (buffer));
69
g_assert (cogl_is_buffer (buffer));
71
/* while at it, set/get the hints */
72
cogl_buffer_set_usage_hint (buffer, COGL_BUFFER_USAGE_HINT_TEXTURE);
73
g_assert_cmpint (cogl_buffer_get_usage_hint (buffer),
75
COGL_BUFFER_USAGE_HINT_TEXTURE);
76
cogl_buffer_set_update_hint (buffer, COGL_BUFFER_UPDATE_HINT_DYNAMIC);
77
g_assert_cmpint (cogl_buffer_get_update_hint (buffer),
79
COGL_BUFFER_UPDATE_HINT_DYNAMIC);
81
map = cogl_buffer_map (buffer, COGL_BUFFER_ACCESS_WRITE);
84
for (i = 0; i < TILE_SIZE * TILE_SIZE * 4; i += 4)
85
memcpy (map + i, &tile->color, 4);
87
cogl_buffer_unmap (buffer);
89
tile->buffer = buffer;
90
tile->texture = create_texture_from_buffer (tile->buffer);
95
create_set_region_tile (TestTile *tile)
98
ClutterColor bottom_color;
103
buffer = cogl_pixel_buffer_new_for_size (TILE_SIZE,
105
COGL_PIXEL_FORMAT_RGBA_8888,
108
g_assert (cogl_is_pixel_buffer (buffer));
109
g_assert (cogl_is_buffer (buffer));
111
/* while at it, set/get the hint */
112
cogl_buffer_set_hint (buffer, COGL_BUFFER_HINT_STATIC_TEXTURE);
113
g_assert (cogl_buffer_get_hint (buffer) == COGL_BUFFER_HINT_STATIC_TEXTURE);
115
data = g_malloc (TILE_SIZE * TILE_SIZE * 4);
116
/* create a buffer with the data we want to copy to the buffer */
117
for (i = 0; i < TILE_SIZE * TILE_SIZE * 4; i += 4)
118
memcpy (data + i, &tile->color, 4);
120
cogl_pixel_buffer_set_region (buffer,
122
TILE_SIZE, TILE_SIZE,
126
bottom_color.red = tile->color.red;
127
bottom_color.green = tile->color.blue;
128
bottom_color.blue = tile->color.green;
129
bottom_color.alpha = tile->color.alpha;
130
for (i = 0; i < TILE_SIZE / 2; i++)
131
memcpy (data + i, &bottom_color, 4);
133
cogl_buffer_set_data (buffer, data, 0, TILE_SIZE * TILE_SIZE * 4 / 2);
137
tile->buffer = buffer;
138
tile->texture = create_texture_from_buffer (tile->buffer);
143
create_set_data_tile (TestTile *tile)
151
buffer = cogl_pixel_buffer_new_for_size (TILE_SIZE,
153
COGL_PIXEL_FORMAT_RGBA_8888,
156
g_assert (cogl_is_pixel_buffer (buffer));
157
g_assert (cogl_is_buffer (buffer));
158
g_assert_cmpint (cogl_buffer_get_size (buffer), ==, rowstride * TILE_SIZE);
160
/* while at it, set/get the hint */
161
cogl_buffer_set_usage_hint (buffer, COGL_BUFFER_USAGE_HINT_TEXTURE);
162
g_assert_cmpint (cogl_buffer_get_usage_hint (buffer),
164
COGL_BUFFER_USAGE_HINT_TEXTURE);
166
/* create a buffer with the data we want to copy to the buffer */
167
data = g_malloc (TILE_SIZE * TILE_SIZE * 4);
168
for (i = 0; i < TILE_SIZE * TILE_SIZE * 4; i += 4)
169
memcpy (data + i, &tile->color, 4);
171
res = cogl_buffer_set_data (buffer, 0, data, TILE_SIZE * TILE_SIZE * 4);
176
tile->buffer = buffer;
177
tile->texture = create_texture_from_buffer (tile->buffer);
181
draw_frame (TestState *state)
185
/* Paint the textures */
186
for (i = 0; i < NB_TILES; i++)
188
cogl_set_source_texture (state->tiles[i].texture);
189
cogl_rectangle (state->tiles[i].x,
191
state->tiles[i].x + TILE_SIZE,
192
state->tiles[i].y + TILE_SIZE);
198
validate_tile (TestState *state,
204
p = pixels = clutter_stage_read_pixels (CLUTTER_STAGE (state->stage),
210
/* Check whether the center of each division is the right color */
211
for (y = 0; y < TILE_SIZE; y++)
212
for (x = 0; x < TILE_SIZE; x++)
214
if (p[0] != tile->color.red ||
215
p[1] != tile->color.green ||
216
p[2] != tile->color.blue ||
217
p[3] != tile->color.alpha)
229
validate_result (TestState *state)
233
for (i = 0; i < NB_TILES; i++)
234
g_assert (validate_tile (state, &state->tiles[i]));
236
/* comment this if you want to see what's being drawn */
238
clutter_main_quit ();
243
on_paint (ClutterActor *actor, TestState *state)
249
/* XXX: Experiments have shown that for some buggy drivers, when using
250
* glReadPixels there is some kind of race, so we delay our test for a
251
* few frames and a few seconds:
253
/* Need to increment frame first because clutter_stage_read_pixels
255
frame_num = state->frame++;
257
validate_result (state);
258
else if (frame_num < 2)
259
g_usleep (G_USEC_PER_SEC);
263
queue_redraw (gpointer stage)
265
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
271
test_cogl_pixel_buffer (TestConformSimpleFixture *fixture,
276
guint paint_handler, i;
277
static TestTile tiles[NB_TILES] =
279
/* color x y buffer tex */
282
{ { 0xff, 0x00, 0x00, 0xff }, 0.0f, 0.0f, NULL, NULL },
285
{ { 0x7e, 0x7e, 0xff, 0x7e }, 0.0f, TILE_SIZE, NULL, NULL },
288
{ { 0x7e, 0xff, 0x7e, 0xff }, 0.0f, TILE_SIZE, NULL, NULL }
293
state.stage = clutter_stage_get_default ();
295
create_map_tile (&tiles[TILE_MAP]);
297
create_set_region_tile (&tiles[TILE_SET_REGION]);
299
create_set_data_tile (&tiles[TILE_SET_DATA]);
303
clutter_stage_set_color (CLUTTER_STAGE (state.stage), &stage_color);
305
/* We force continuous redrawing of the stage, since we need to skip
306
* the first few frames, and we wont be doing anything else that
307
* will trigger redrawing. */
308
idle_source = g_idle_add (queue_redraw, state.stage);
310
paint_handler = g_signal_connect_after (state.stage, "paint",
311
G_CALLBACK (on_paint), &state);
313
clutter_actor_show_all (state.stage);
317
g_source_remove (idle_source);
318
g_signal_handler_disconnect (state.stage, paint_handler);
320
for (i = 0; i < NB_TILES; i++)
322
cogl_handle_unref (state.tiles[i].buffer);
323
cogl_handle_unref (state.tiles[i].texture);
326
/* Remove all of the actors from the stage */
327
clutter_container_foreach (CLUTTER_CONTAINER (state.stage),
328
(ClutterCallback) clutter_actor_destroy,
331
if (g_test_verbose ())