summaryrefslogtreecommitdiff
path: root/tests/conform/test-pixel-buffer.c
blob: 05e84335491439e8dd8724d8d9dd74743c8cffdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <clutter/clutter.h>
#include <cogl/cogl.h>
#include <string.h>

#include "test-conform-common.h"

#define TILE_SIZE        32.0f

enum
{
  TILE_MAP,
  TILE_SET_DATA,
  NB_TILES,
  TILE_SET_REGION,
};

typedef struct test_tile
{
  ClutterColor color;
  gfloat x, y;
  CoglHandle buffer;
  CoglHandle texture;
} TestTile;

static const ClutterColor
buffer_colors[] =
  {
  };

static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff };

typedef struct _TestState
{
  ClutterActor *stage;
  unsigned int frame;

  TestTile *tiles;

} TestState;

static CoglHandle
create_texture_from_buffer (CoglHandle buffer)
{
  CoglHandle texture;

  texture = cogl_texture_new_from_buffer (buffer,
                                          TILE_SIZE, TILE_SIZE,
                                          COGL_TEXTURE_NO_SLICING,
                                          COGL_PIXEL_FORMAT_RGBA_8888,
                                          COGL_PIXEL_FORMAT_RGBA_8888,
                                          TILE_SIZE * 4,
                                          0);

  g_assert (texture != COGL_INVALID_HANDLE);

  return texture;
}

static void
create_map_tile (TestTile *tile)
{
  CoglHandle buffer;
  guchar *map;
  unsigned int i;
  unsigned int stride = 0;
  guint8 *line;

  buffer = cogl_pixel_array_new_with_size (TILE_SIZE,
                                           TILE_SIZE,
                                           COGL_PIXEL_FORMAT_RGBA_8888,
                                           &stride);

  g_assert (cogl_is_pixel_array (buffer));
  g_assert (cogl_is_buffer (buffer));

  cogl_buffer_set_update_hint (buffer, COGL_BUFFER_UPDATE_HINT_DYNAMIC);
  g_assert_cmpint (cogl_buffer_get_update_hint (buffer),
            ==,
            COGL_BUFFER_UPDATE_HINT_DYNAMIC);

  map = cogl_buffer_map (buffer,
                         COGL_BUFFER_ACCESS_WRITE,
                         COGL_BUFFER_MAP_HINT_DISCARD);
  g_assert (map);

  line = g_alloca (TILE_SIZE * 4);
  for (i = 0; i < TILE_SIZE * 4; i += 4)
    memcpy (line + i, &tile->color, 4);

  for (i = 0; i < TILE_SIZE; i++)
    memcpy (map + stride * i, line, TILE_SIZE * 4);

  cogl_buffer_unmap (buffer);

  tile->buffer = buffer;
  tile->texture = create_texture_from_buffer (tile->buffer);
}

#if 0
static void
create_set_region_tile (TestTile *tile)
{
  CoglHandle buffer;
  ClutterColor bottom_color;
  unsigned int rowstride = 0;
  guchar *data;
  unsigned int i;

  buffer = cogl_pixel_array_with_size (TILE_SIZE,
                                       TILE_SIZE,
                                       COGL_PIXEL_FORMAT_RGBA_8888,
                                       &rowstride);

  g_assert (cogl_is_pixel_array (buffer));
  g_assert (cogl_is_buffer (buffer));

  /* while at it, set/get the hint */
  cogl_buffer_set_hint (buffer, COGL_BUFFER_HINT_STATIC_TEXTURE);
  g_assert (cogl_buffer_get_hint (buffer) == COGL_BUFFER_HINT_STATIC_TEXTURE);

  data = g_malloc (TILE_SIZE * TILE_SIZE * 4);
  /* create a buffer with the data we want to copy to the buffer */
  for (i = 0; i < TILE_SIZE * TILE_SIZE * 4; i += 4)
      memcpy (data + i, &tile->color, 4);

  cogl_pixel_array_set_region (buffer,
                                data,
                                TILE_SIZE, TILE_SIZE,
                                TILE_SIZE,
                                0, 0);

  bottom_color.red = tile->color.red;
  bottom_color.green = tile->color.blue;
  bottom_color.blue = tile->color.green;
  bottom_color.alpha = tile->color.alpha;
  for (i = 0; i < TILE_SIZE / 2; i++)
    memcpy (data + i, &bottom_color, 4);

  cogl_buffer_set_data (buffer, data, 0, TILE_SIZE * TILE_SIZE * 4 / 2);

  g_free (data);

  tile->buffer = buffer;
  tile->texture = create_texture_from_buffer (tile->buffer);
}
#endif

static void
create_set_data_tile (TestTile *tile)
{
  CoglHandle buffer;
  unsigned int rowstride = 0;
  gboolean res;
  guchar *data;
  unsigned int i;

  buffer = cogl_pixel_array_new_with_size (TILE_SIZE,
                                           TILE_SIZE,
                                           COGL_PIXEL_FORMAT_RGBA_8888,
                                           &rowstride);

  g_assert (cogl_is_pixel_array (buffer));
  g_assert (cogl_is_buffer (buffer));
  g_assert_cmpint (cogl_buffer_get_size (buffer), ==, rowstride * TILE_SIZE);

  /* create a buffer with the data we want to copy to the buffer */
  data = g_malloc (TILE_SIZE * TILE_SIZE * 4);
  for (i = 0; i < TILE_SIZE * TILE_SIZE * 4; i += 4)
      memcpy (data + i, &tile->color, 4);

  /* FIXME: this doesn't consider the rowstride */
  res = cogl_buffer_set_data (buffer, 0, data, TILE_SIZE * TILE_SIZE * 4);
  g_assert (res);

  g_free (data);

  tile->buffer = buffer;
  tile->texture = create_texture_from_buffer (tile->buffer);
}

static void
draw_frame (TestState *state)
{
  unsigned int i;

  /* Paint the textures */
  for (i = 0; i < NB_TILES; i++)
    {
      cogl_set_source_texture (state->tiles[i].texture);
      cogl_rectangle (state->tiles[i].x,
                      state->tiles[i].y,
                      state->tiles[i].x + TILE_SIZE,
                      state->tiles[i].y + TILE_SIZE);
    }

}

static gboolean
validate_tile (TestState *state,
               TestTile  *tile)
{
  int x, y;
  guchar *pixels, *p;

  p = pixels = clutter_stage_read_pixels (CLUTTER_STAGE (state->stage),
                                          tile->x,
                                          tile->y,
                                          TILE_SIZE,
                                          TILE_SIZE);

  /* Check whether the center of each division is the right color */
  for (y = 0; y < TILE_SIZE; y++)
    for (x = 0; x < TILE_SIZE; x++)
      {
        if (p[0] != tile->color.red ||
            p[1] != tile->color.green ||
            p[2] != tile->color.blue ||
            p[3] != tile->color.alpha)
          {
            return FALSE;
          }

        p += 4;
      }

  return TRUE;
}

static void
validate_result (TestState *state)
{
  unsigned int i;

  for (i = 0; i < NB_TILES; i++)
    g_assert (validate_tile (state, &state->tiles[i]));

  /* comment this if you want to see what's being drawn */
#if 1
  clutter_main_quit ();
#endif
}

static void
on_paint (ClutterActor *actor, TestState *state)
{
  int frame_num;

  draw_frame (state);

  /* XXX: validate_result calls clutter_stage_read_pixels which will result in
   * another paint run so to avoid infinite recursion we only aim to validate
   * the first frame. */
  frame_num = state->frame++;
  if (frame_num == 1)
    validate_result (state);
}

static gboolean
queue_redraw (gpointer stage)
{
  clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));

  return TRUE;
}

void
test_cogl_pixel_array (TestUtilsGTestFixture *fixture,
		       void *            data)
{
  TestState state;
  unsigned int idle_source;
  unsigned int paint_handler, i;
  static TestTile tiles[NB_TILES] =
    {
        /*         color             x  y buffer tex */

        /* MAP */
        { { 0xff, 0x00, 0x00, 0xff }, 0.0f, 0.0f, NULL, NULL },
#if 0
        /* SET_REGION */
        { { 0x7e, 0x7e, 0xff, 0x7e }, 0.0f, TILE_SIZE, NULL, NULL },
#endif
        /* SET_DATA */
        { { 0x7e, 0xff, 0x7e, 0xff }, 0.0f, TILE_SIZE, NULL, NULL }
    };

  state.frame = 0;

  state.stage = clutter_stage_get_default ();

  create_map_tile (&tiles[TILE_MAP]);
#if 0
  create_set_region_tile (&tiles[TILE_SET_REGION]);
#endif
  create_set_data_tile (&tiles[TILE_SET_DATA]);

  state.tiles = tiles;

  clutter_stage_set_color (CLUTTER_STAGE (state.stage), &stage_color);

  /* We force continuous redrawing of the stage, since we need to skip
   * the first few frames, and we wont be doing anything else that
   * will trigger redrawing. */
  idle_source = g_idle_add (queue_redraw, state.stage);

  paint_handler = g_signal_connect_after (state.stage, "paint",
                                          G_CALLBACK (on_paint), &state);

  clutter_actor_show_all (state.stage);

  clutter_main ();

  g_source_remove (idle_source);
  g_signal_handler_disconnect (state.stage, paint_handler);

  for (i = 0; i < NB_TILES; i++)
    {
      cogl_handle_unref (state.tiles[i].buffer);
      cogl_handle_unref (state.tiles[i].texture);
    }

  /* Remove all of the actors from the stage */
  clutter_container_foreach (CLUTTER_CONTAINER (state.stage),
                             (ClutterCallback) clutter_actor_destroy,
                             NULL);

  if (g_test_verbose ())
    g_print ("OK\n");
}