summaryrefslogtreecommitdiff
path: root/tests/conform/test-sub-texture.c
blob: cec5b67d75430d00cbc8c8474673bfcd03acaa01 (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
#include <cogl/cogl.h>
#include <string.h>

#include "test-utils.h"

#define SOURCE_SIZE        32
#define SOURCE_DIVISIONS_X 2
#define SOURCE_DIVISIONS_Y 2
#define DIVISION_WIDTH     (SOURCE_SIZE / SOURCE_DIVISIONS_X)
#define DIVISION_HEIGHT    (SOURCE_SIZE / SOURCE_DIVISIONS_Y)

#define TEST_INSET         1

static const uint32_t
corner_colors[SOURCE_DIVISIONS_X * SOURCE_DIVISIONS_Y] =
  {
    0xff0000ff, /* red top left */
    0x00ff00ff, /* green top right */
    0x0000ffff, /* blue bottom left */
    0xff00ffff  /* purple bottom right */
  };

typedef struct _TestState
{
  CoglTexture2D *tex;
} TestState;

static CoglTexture2D *
create_source (TestState *state)
{
  int dx, dy;
  uint8_t *data = g_malloc (SOURCE_SIZE * SOURCE_SIZE * 4);
  CoglTexture2D *tex;

  /* Create a texture with a different coloured rectangle at each
     corner */
  for (dy = 0; dy < SOURCE_DIVISIONS_Y; dy++)
    for (dx = 0; dx < SOURCE_DIVISIONS_X; dx++)
      {
        uint8_t *p = (data + dy * DIVISION_HEIGHT * SOURCE_SIZE * 4 +
                     dx * DIVISION_WIDTH * 4);
        int x, y;

        for (y = 0; y < DIVISION_HEIGHT; y++)
          {
            for (x = 0; x < DIVISION_WIDTH; x++)
              {
                uint32_t color = GUINT32_FROM_BE (corner_colors[dx + dy * SOURCE_DIVISIONS_X]);
                memcpy (p, &color, 4);
                p += 4;
              }

            p += SOURCE_SIZE * 4 - DIVISION_WIDTH * 4;
          }
      }

  tex = cogl_texture_2d_new_from_data (test_ctx,
                                       SOURCE_SIZE, SOURCE_SIZE,
                                       COGL_PIXEL_FORMAT_RGBA_8888,
                                       SOURCE_SIZE * 4,
                                       data,
                                       NULL);
  return tex;
}

static CoglTexture2D *
create_test_texture (TestState *state)
{
  CoglTexture2D *tex;
  uint8_t *data = g_malloc (256 * 256 * 4), *p = data;
  int x, y;

  /* Create a texture that is 256x256 where the red component ranges
     from 0->255 along the x axis and the green component ranges from
     0->255 along the y axis. The blue and alpha components are all
     255 */
  for (y = 0; y < 256; y++)
    for (x = 0; x < 256; x++)
      {
        *(p++) = x;
        *(p++) = y;
        *(p++) = 255;
        *(p++) = 255;
      }

  tex = cogl_texture_2d_new_from_data (test_ctx,
                                       256, 256,
                                       COGL_PIXEL_FORMAT_RGBA_8888_PRE,
                                       256 * 4,
                                       data,
                                       NULL);
  g_free (data);

  return tex;
}

static void
paint (TestState *state)
{
  CoglTexture2D *full_texture;
  CoglSubTexture *sub_texture, *sub_sub_texture;
  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);

  /* Create a sub texture of the bottom right quarter of the texture */
  sub_texture = cogl_sub_texture_new (test_ctx,
                                      state->tex,
                                      DIVISION_WIDTH,
                                      DIVISION_HEIGHT,
                                      DIVISION_WIDTH,
                                      DIVISION_HEIGHT);

  /* Paint it */
  cogl_pipeline_set_layer_texture (pipeline, 0, sub_texture);
  cogl_object_unref (sub_texture);
  cogl_framebuffer_draw_rectangle (test_fb, pipeline,
                                   0.0f, 0.0f, DIVISION_WIDTH, DIVISION_HEIGHT);


  /* Repeat a sub texture of the top half of the full texture. This is
     documented to be undefined so it doesn't technically have to work
     but it will with the current implementation */
  sub_texture = cogl_sub_texture_new (test_ctx,
                                      state->tex,
                                      0, 0,
                                      SOURCE_SIZE,
                                      DIVISION_HEIGHT);
  cogl_pipeline_set_layer_texture (pipeline, 0, sub_texture);
  cogl_object_unref (sub_texture);
  cogl_framebuffer_draw_textured_rectangle (test_fb, pipeline,
                                            0.0f,
                                            SOURCE_SIZE,
                                            SOURCE_SIZE * 2.0f,
                                            SOURCE_SIZE * 1.5f,
                                            0.0f, 0.0f,
                                            2.0f, 1.0f);

  /* Create a sub texture of a sub texture */
  full_texture = create_test_texture (state);
  sub_texture = cogl_sub_texture_new (test_ctx,
                                      full_texture,
                                      20, 10, 30, 20);
  cogl_object_unref (full_texture);
  sub_sub_texture = cogl_sub_texture_new (test_ctx,
                                          sub_texture,
                                          20, 10, 10, 10);
  cogl_object_unref (sub_texture);
  cogl_pipeline_set_layer_texture (pipeline, 0, sub_sub_texture);
  cogl_object_unref (sub_sub_texture);
  cogl_framebuffer_draw_rectangle (test_fb, pipeline,
                                   0.0f, SOURCE_SIZE * 2.0f,
                                   10.0f, SOURCE_SIZE * 2.0f + 10.0f);

  cogl_object_unref (pipeline);
}

static void
validate_part (int xpos, int ypos,
               int width, int height,
               uint32_t color)
{
  test_utils_check_region (test_fb,
                           xpos + TEST_INSET,
                           ypos + TEST_INSET,
                           width - TEST_INSET - 2,
                           height - TEST_INSET - 2,
                           color);
}

static uint8_t *
create_update_data (void)
{
  uint8_t *data = g_malloc (256 * 256 * 4), *p = data;
  int x, y;

  /* Create some image data that is 256x256 where the blue component
     ranges from 0->255 along the x axis and the alpha component
     ranges from 0->255 along the y axis. The red and green components
     are all zero */
  for (y = 0; y < 256; y++)
    for (x = 0; x < 256; x++)
      {
        *(p++) = 0;
        *(p++) = 0;
        *(p++) = x;
        *(p++) = y;
      }

  return data;
}

static void
validate_result (TestState *state)
{
  int i, division_num, x, y;
  CoglTexture2D *test_tex;
  CoglSubTexture *sub_texture;
  uint8_t *texture_data, *p;
  int tex_width, tex_height;

  /* Sub texture of the bottom right corner of the texture */
  validate_part (0, 0, DIVISION_WIDTH, DIVISION_HEIGHT,
                 corner_colors[
                 (SOURCE_DIVISIONS_Y - 1) * SOURCE_DIVISIONS_X +
                 SOURCE_DIVISIONS_X - 1]);

  /* Sub texture of the top half repeated horizontally */
  for (i = 0; i < 2; i++)
    for (division_num = 0; division_num < SOURCE_DIVISIONS_X; division_num++)
      validate_part (i * SOURCE_SIZE + division_num * DIVISION_WIDTH,
                     SOURCE_SIZE,
                     DIVISION_WIDTH, DIVISION_HEIGHT,
                     corner_colors[division_num]);

  /* Sub sub texture */
  p = texture_data = g_malloc (10 * 10 * 4);
  cogl_framebuffer_read_pixels (test_fb,
                                0, SOURCE_SIZE * 2, 10, 10,
                                COGL_PIXEL_FORMAT_RGBA_8888,
                                p);
  for (y = 0; y < 10; y++)
    for (x = 0; x < 10; x++)
      {
        g_assert (*(p++) == x + 40);
        g_assert (*(p++) == y + 20);
        p += 2;
      }
  g_free (texture_data);

  /* Try reading back the texture data */
  sub_texture = cogl_sub_texture_new (test_ctx,
                                      state->tex,
                                      SOURCE_SIZE / 4,
                                      SOURCE_SIZE / 4,
                                      SOURCE_SIZE / 2,
                                      SOURCE_SIZE / 2);
  tex_width = cogl_texture_get_width (sub_texture);
  tex_height = cogl_texture_get_height (sub_texture);
  p = texture_data = g_malloc (tex_width * tex_height * 4);
  cogl_texture_get_data (sub_texture,
                         COGL_PIXEL_FORMAT_RGBA_8888,
                         tex_width * 4,
                         texture_data);
  for (y = 0; y < tex_height; y++)
    for (x = 0; x < tex_width; x++)
      {
        int div_x = ((x * SOURCE_SIZE / 2 / tex_width + SOURCE_SIZE / 4) /
                     DIVISION_WIDTH);
        int div_y = ((y * SOURCE_SIZE / 2 / tex_height + SOURCE_SIZE / 4) /
                     DIVISION_HEIGHT);
        uint32_t reference = corner_colors[div_x + div_y * SOURCE_DIVISIONS_X] >> 8;
        uint32_t color = GUINT32_FROM_BE (*((uint32_t *)p)) >> 8;
        g_assert (color == reference);
        p += 4;
      }
  g_free (texture_data);
  cogl_object_unref (sub_texture);

  /* Create a 256x256 test texture */
  test_tex = create_test_texture (state);
  /* Create a sub texture the views the center half of the texture */
  sub_texture = cogl_sub_texture_new (test_ctx,
                                      test_tex,
                                      64, 64, 128, 128);
  /* Update the center half of the sub texture */
  texture_data = create_update_data ();
  cogl_texture_set_region (sub_texture,
                           64, 64,
                           COGL_PIXEL_FORMAT_RGBA_8888_PRE,
                           256 * 4,
                           texture_data,
                           32, 32, /* dst x, y */
                           0, /* level */
                           NULL); /* don't catch errors */

  g_free (texture_data);
  cogl_object_unref (sub_texture);
  /* Get the texture data */
  p = texture_data = g_malloc (256 * 256 * 4);
  cogl_texture_get_data (test_tex,
                         COGL_PIXEL_FORMAT_RGBA_8888_PRE,
                         256 * 4, texture_data);

  /* Verify the texture data */
  for (y = 0; y < 256; y++)
    for (x = 0; x < 256; x++)
      {
        /* If we're in the center quarter */
        if (x >= 96 && x < 160 && y >= 96 && y < 160)
          {
            g_assert ((*p++) == 0);
            g_assert ((*p++) == 0);
            g_assert ((*p++) == x - 96);
            g_assert ((*p++) == y - 96);
          }
        else
          {
            g_assert ((*p++) == x);
            g_assert ((*p++) == y);
            g_assert ((*p++) == 255);
            g_assert ((*p++) == 255);
          }
      }
  g_free (texture_data);
  cogl_object_unref (test_tex);
}

void
test_sub_texture (void)
{
  TestState state;

  state.tex = create_source (&state);

  cogl_framebuffer_orthographic (test_fb,
                                 0, 0,
                                 cogl_framebuffer_get_width (test_fb),
                                 cogl_framebuffer_get_height (test_fb),
                                 -1,
                                 100);

  paint (&state);
  validate_result (&state);

  cogl_object_unref (state.tex);

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