summaryrefslogtreecommitdiff
path: root/tests/conform/test-pixel-buffer.c
blob: 96b747d024fda8b5d556259ec52b478e4e69ac2a (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
#include <cogl/cogl.h>
#include <string.h>

#include "test-utils.h"

#define BITMAP_SIZE 256

/*
 * Creates a 256 x 256 with image data split into four quadrants. The
 * colours of these in reading order will be: blue, green, cyan,
 * red */
static void
generate_bitmap_data (uint8_t *data,
                      int stride)
{
  int y, x;

  for (y = 0; y < BITMAP_SIZE; y++)
    {
      for (x = 0; x < BITMAP_SIZE; x++)
        {
          int color_num = x / (BITMAP_SIZE / 2) + y / (BITMAP_SIZE / 2) * 2 + 1;
          *(data++) = (color_num & 4) ? 255 : 0;
          *(data++) = (color_num & 2) ? 255 : 0;
          *(data++) = (color_num & 1) ? 255 : 0;
          *(data++) = 255;
        }
      data += stride - BITMAP_SIZE * 4;
    }
}

static CoglBitmap *
create_bitmap (void)
{
  CoglBitmap *bitmap;
  CoglBuffer *buffer;

  bitmap = cogl_bitmap_new_with_size (test_ctx,
                                      BITMAP_SIZE,
                                      BITMAP_SIZE,
                                      COGL_PIXEL_FORMAT_RGBA_8888);
  buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));

  g_assert (cogl_is_pixel_buffer (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);

  return bitmap;
}

static CoglBitmap *
create_and_fill_bitmap (void)
{
  CoglBitmap *bitmap = create_bitmap ();
  CoglBuffer *buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
  uint8_t *map;
  unsigned int stride;

  stride = cogl_bitmap_get_rowstride (bitmap);

  map = cogl_buffer_map (buffer,
                         COGL_BUFFER_ACCESS_WRITE,
                         COGL_BUFFER_MAP_HINT_DISCARD,
                         NULL); /* don't catch errors */
  g_assert (map);

  generate_bitmap_data (map, stride);

  cogl_buffer_unmap (COGL_BUFFER (buffer));

  return bitmap;
}

static CoglTexture *
create_texture_from_bitmap (CoglBitmap *bitmap)
{
  CoglTexture2D *texture;

  texture = cogl_texture_2d_new_from_bitmap (bitmap,
                                             COGL_PIXEL_FORMAT_RGBA_8888,
                                             NULL); /* don't catch errors */

  g_assert (texture != NULL);

  return COGL_TEXTURE (texture);
}

static CoglPipeline *
create_pipeline_from_texture (CoglTexture *texture)
{
  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);

  cogl_pipeline_set_layer_texture (pipeline, 0, texture);
  cogl_pipeline_set_layer_filters (pipeline,
                                   0, /* layer_num */
                                   COGL_PIPELINE_FILTER_NEAREST,
                                   COGL_PIPELINE_FILTER_NEAREST);

  return pipeline;
}

static void
check_colours (uint32_t color0,
               uint32_t color1,
               uint32_t color2,
               uint32_t color3)
{
  int fb_width = cogl_framebuffer_get_width (test_fb);
  int fb_height = cogl_framebuffer_get_height (test_fb);

  test_utils_check_region (test_fb,
                           1, 1, /* x/y */
                           fb_width / 2 - 2, /* width */
                           fb_height / 2 - 2, /* height */
                           color0);
  test_utils_check_region (test_fb,
                           fb_width / 2 + 1, /* x */
                           1, /* y */
                           fb_width / 2 - 2, /* width */
                           fb_height / 2 - 2, /* height */
                           color1);
  test_utils_check_region (test_fb,
                           1, /* x */
                           fb_height / 2 + 1, /* y */
                           fb_width / 2 - 2, /* width */
                           fb_height / 2 - 2, /* height */
                           color2);
  test_utils_check_region (test_fb,
                           fb_width / 2 + 1, /* x */
                           fb_height / 2 + 1, /* y */
                           fb_width / 2 - 2, /* width */
                           fb_height / 2 - 2, /* height */
                           color3);
}

void
test_pixel_buffer_map (void)
{
  CoglBitmap *bitmap = create_and_fill_bitmap ();
  CoglPipeline *pipeline;
  CoglTexture *texture;

  texture = create_texture_from_bitmap (bitmap);
  pipeline = create_pipeline_from_texture (texture);

  cogl_framebuffer_draw_rectangle (test_fb,
                                   pipeline,
                                   -1.0f, 1.0f,
                                   1.0f, -1.0f);

  cogl_object_unref (bitmap);
  cogl_object_unref (texture);
  cogl_object_unref (pipeline);

  check_colours (0x0000ffff,
                 0x00ff00ff,
                 0x00ffffff,
                 0xff0000ff);

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

void
test_pixel_buffer_set_data (void)
{
  CoglBitmap *bitmap = create_bitmap ();
  CoglBuffer *buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
  CoglPipeline *pipeline;
  CoglTexture *texture;
  uint8_t *data;
  unsigned int stride;

  stride = cogl_bitmap_get_rowstride (bitmap);

  data = g_malloc (stride * BITMAP_SIZE);

  generate_bitmap_data (data, stride);

  cogl_buffer_set_data (buffer,
                        0, /* offset */
                        data,
                        stride * (BITMAP_SIZE - 1) +
                        BITMAP_SIZE * 4,
                        NULL /* don't catch errors */);

  g_free (data);

  texture = create_texture_from_bitmap (bitmap);
  pipeline = create_pipeline_from_texture (texture);

  cogl_framebuffer_draw_rectangle (test_fb,
                                   pipeline,
                                   -1.0f, 1.0f,
                                   1.0f, -1.0f);

  cogl_object_unref (bitmap);
  cogl_object_unref (texture);
  cogl_object_unref (pipeline);

  check_colours (0x0000ffff,
                 0x00ff00ff,
                 0x00ffffff,
                 0xff0000ff);

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

static CoglTexture *
create_white_texture (void)
{
  CoglTexture2D *texture;
  uint8_t *data = g_malloc (BITMAP_SIZE * BITMAP_SIZE * 4);

  memset (data, 255, BITMAP_SIZE * BITMAP_SIZE * 4);

  texture = cogl_texture_2d_new_from_data (test_ctx,
                                           BITMAP_SIZE,
                                           BITMAP_SIZE,
                                           COGL_PIXEL_FORMAT_RGBA_8888,
                                           COGL_PIXEL_FORMAT_ANY,
                                           BITMAP_SIZE * 4, /* rowstride */
                                           data,
                                           NULL); /* don't catch errors */

  g_free (data);

  return COGL_TEXTURE (texture);
}

void
test_pixel_buffer_sub_region (void)
{
  CoglBitmap *bitmap = create_and_fill_bitmap ();
  CoglPipeline *pipeline;
  CoglTexture *texture;

  texture = create_white_texture ();

  /* Replace the top-right quadrant of the texture with the red part
   * of the bitmap */
  cogl_texture_set_region_from_bitmap (texture,
                                       BITMAP_SIZE / 2, /* src_x */
                                       BITMAP_SIZE / 2, /* src_y */
                                       BITMAP_SIZE / 2, /* width */
                                       BITMAP_SIZE / 2, /* height */
                                       bitmap,
                                       BITMAP_SIZE / 2, /* dst_x */
                                       0, /* dst_y */
                                       0, /* level */
                                       NULL /* don't catch errors */);

  pipeline = create_pipeline_from_texture (texture);

  cogl_framebuffer_draw_rectangle (test_fb,
                                   pipeline,
                                   -1.0f, 1.0f,
                                   1.0f, -1.0f);

  cogl_object_unref (bitmap);
  cogl_object_unref (texture);
  cogl_object_unref (pipeline);

  check_colours (0xffffffff,
                 0xff0000ff,
                 0xffffffff,
                 0xffffffff);

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