~elementary-os/ubuntu-package-imports/mutter-bionic

« back to all changes in this revision

Viewing changes to cogl/tests/conform/test-pixel-buffer.c

  • Committer: RabbitBot
  • Date: 2018-04-11 14:49:36 UTC
  • Revision ID: rabbitbot@elementary.io-20180411144936-hgymqa9d8d1xfpbh
Initial import, version 3.28.0-2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cogl/cogl.h>
 
2
#include <string.h>
 
3
 
 
4
#include "test-utils.h"
 
5
 
 
6
#define BITMAP_SIZE 256
 
7
 
 
8
/*
 
9
 * Creates a 256 x 256 with image data split into four quadrants. The
 
10
 * colours of these in reading order will be: blue, green, cyan,
 
11
 * red */
 
12
static void
 
13
generate_bitmap_data (uint8_t *data,
 
14
                      int stride)
 
15
{
 
16
  int y, x;
 
17
 
 
18
  for (y = 0; y < BITMAP_SIZE; y++)
 
19
    {
 
20
      for (x = 0; x < BITMAP_SIZE; x++)
 
21
        {
 
22
          int color_num = x / (BITMAP_SIZE / 2) + y / (BITMAP_SIZE / 2) * 2 + 1;
 
23
          *(data++) = (color_num & 4) ? 255 : 0;
 
24
          *(data++) = (color_num & 2) ? 255 : 0;
 
25
          *(data++) = (color_num & 1) ? 255 : 0;
 
26
          *(data++) = 255;
 
27
        }
 
28
      data += stride - BITMAP_SIZE * 4;
 
29
    }
 
30
}
 
31
 
 
32
static CoglBitmap *
 
33
create_bitmap (void)
 
34
{
 
35
  CoglBitmap *bitmap;
 
36
  CoglBuffer *buffer;
 
37
 
 
38
  bitmap = cogl_bitmap_new_with_size (test_ctx,
 
39
                                      BITMAP_SIZE,
 
40
                                      BITMAP_SIZE,
 
41
                                      COGL_PIXEL_FORMAT_RGBA_8888);
 
42
  buffer = cogl_bitmap_get_buffer (bitmap);
 
43
 
 
44
  g_assert (cogl_is_pixel_buffer (buffer));
 
45
  g_assert (cogl_is_buffer (buffer));
 
46
 
 
47
  cogl_buffer_set_update_hint (buffer, COGL_BUFFER_UPDATE_HINT_DYNAMIC);
 
48
  g_assert_cmpint (cogl_buffer_get_update_hint (buffer),
 
49
                   ==,
 
50
                   COGL_BUFFER_UPDATE_HINT_DYNAMIC);
 
51
 
 
52
  return bitmap;
 
53
}
 
54
 
 
55
static CoglBitmap *
 
56
create_and_fill_bitmap (void)
 
57
{
 
58
  CoglBitmap *bitmap = create_bitmap ();
 
59
  CoglBuffer *buffer = cogl_bitmap_get_buffer (bitmap);
 
60
  uint8_t *map;
 
61
  unsigned int stride;
 
62
 
 
63
  stride = cogl_bitmap_get_rowstride (bitmap);
 
64
 
 
65
  map = cogl_buffer_map (buffer,
 
66
                         COGL_BUFFER_ACCESS_WRITE,
 
67
                         COGL_BUFFER_MAP_HINT_DISCARD);
 
68
  g_assert (map);
 
69
 
 
70
  generate_bitmap_data (map, stride);
 
71
 
 
72
  cogl_buffer_unmap (buffer);
 
73
 
 
74
  return bitmap;
 
75
}
 
76
 
 
77
static CoglTexture *
 
78
create_texture_from_bitmap (CoglBitmap *bitmap)
 
79
{
 
80
  CoglTexture2D *texture;
 
81
 
 
82
  texture = cogl_texture_2d_new_from_bitmap (bitmap);
 
83
 
 
84
  g_assert (texture != NULL);
 
85
 
 
86
  return texture;
 
87
}
 
88
 
 
89
static CoglPipeline *
 
90
create_pipeline_from_texture (CoglTexture *texture)
 
91
{
 
92
  CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
 
93
 
 
94
  cogl_pipeline_set_layer_texture (pipeline, 0, texture);
 
95
  cogl_pipeline_set_layer_filters (pipeline,
 
96
                                   0, /* layer_num */
 
97
                                   COGL_PIPELINE_FILTER_NEAREST,
 
98
                                   COGL_PIPELINE_FILTER_NEAREST);
 
99
 
 
100
  return pipeline;
 
101
}
 
102
 
 
103
static void
 
104
check_colours (uint32_t color0,
 
105
               uint32_t color1,
 
106
               uint32_t color2,
 
107
               uint32_t color3)
 
108
{
 
109
  int fb_width = cogl_framebuffer_get_width (test_fb);
 
110
  int fb_height = cogl_framebuffer_get_height (test_fb);
 
111
 
 
112
  test_utils_check_region (test_fb,
 
113
                           1, 1, /* x/y */
 
114
                           fb_width / 2 - 2, /* width */
 
115
                           fb_height / 2 - 2, /* height */
 
116
                           color0);
 
117
  test_utils_check_region (test_fb,
 
118
                           fb_width / 2 + 1, /* x */
 
119
                           1, /* y */
 
120
                           fb_width / 2 - 2, /* width */
 
121
                           fb_height / 2 - 2, /* height */
 
122
                           color1);
 
123
  test_utils_check_region (test_fb,
 
124
                           1, /* x */
 
125
                           fb_height / 2 + 1, /* y */
 
126
                           fb_width / 2 - 2, /* width */
 
127
                           fb_height / 2 - 2, /* height */
 
128
                           color2);
 
129
  test_utils_check_region (test_fb,
 
130
                           fb_width / 2 + 1, /* x */
 
131
                           fb_height / 2 + 1, /* y */
 
132
                           fb_width / 2 - 2, /* width */
 
133
                           fb_height / 2 - 2, /* height */
 
134
                           color3);
 
135
}
 
136
 
 
137
void
 
138
test_pixel_buffer_map (void)
 
139
{
 
140
  CoglBitmap *bitmap = create_and_fill_bitmap ();
 
141
  CoglPipeline *pipeline;
 
142
  CoglTexture *texture;
 
143
 
 
144
  texture = create_texture_from_bitmap (bitmap);
 
145
  pipeline = create_pipeline_from_texture (texture);
 
146
 
 
147
  cogl_framebuffer_draw_rectangle (test_fb,
 
148
                                   pipeline,
 
149
                                   -1.0f, 1.0f,
 
150
                                   1.0f, -1.0f);
 
151
 
 
152
  cogl_object_unref (bitmap);
 
153
  cogl_object_unref (texture);
 
154
  cogl_object_unref (pipeline);
 
155
 
 
156
  check_colours (0x0000ffff,
 
157
                 0x00ff00ff,
 
158
                 0x00ffffff,
 
159
                 0xff0000ff);
 
160
 
 
161
  if (cogl_test_verbose ())
 
162
    g_print ("OK\n");
 
163
}
 
164
 
 
165
void
 
166
test_pixel_buffer_set_data (void)
 
167
{
 
168
  CoglBitmap *bitmap = create_bitmap ();
 
169
  CoglBuffer *buffer = cogl_bitmap_get_buffer (bitmap);
 
170
  CoglPipeline *pipeline;
 
171
  CoglTexture *texture;
 
172
  uint8_t *data;
 
173
  unsigned int stride;
 
174
 
 
175
  stride = cogl_bitmap_get_rowstride (bitmap);
 
176
 
 
177
  data = g_malloc (stride * BITMAP_SIZE);
 
178
 
 
179
  generate_bitmap_data (data, stride);
 
180
 
 
181
  cogl_buffer_set_data (buffer,
 
182
                        0, /* offset */
 
183
                        data,
 
184
                        stride * (BITMAP_SIZE - 1) +
 
185
                        BITMAP_SIZE * 4);
 
186
 
 
187
  g_free (data);
 
188
 
 
189
  texture = create_texture_from_bitmap (bitmap);
 
190
  pipeline = create_pipeline_from_texture (texture);
 
191
 
 
192
  cogl_framebuffer_draw_rectangle (test_fb,
 
193
                                   pipeline,
 
194
                                   -1.0f, 1.0f,
 
195
                                   1.0f, -1.0f);
 
196
 
 
197
  cogl_object_unref (bitmap);
 
198
  cogl_object_unref (texture);
 
199
  cogl_object_unref (pipeline);
 
200
 
 
201
  check_colours (0x0000ffff,
 
202
                 0x00ff00ff,
 
203
                 0x00ffffff,
 
204
                 0xff0000ff);
 
205
 
 
206
  if (cogl_test_verbose ())
 
207
    g_print ("OK\n");
 
208
}
 
209
 
 
210
static CoglTexture *
 
211
create_white_texture (void)
 
212
{
 
213
  CoglTexture2D *texture;
 
214
  uint8_t *data = g_malloc (BITMAP_SIZE * BITMAP_SIZE * 4);
 
215
 
 
216
  memset (data, 255, BITMAP_SIZE * BITMAP_SIZE * 4);
 
217
 
 
218
  texture = cogl_texture_2d_new_from_data (test_ctx,
 
219
                                           BITMAP_SIZE,
 
220
                                           BITMAP_SIZE,
 
221
                                           COGL_PIXEL_FORMAT_RGBA_8888,
 
222
                                           BITMAP_SIZE * 4, /* rowstride */
 
223
                                           data,
 
224
                                           NULL); /* don't catch errors */
 
225
 
 
226
  g_free (data);
 
227
 
 
228
  return texture;
 
229
}
 
230
 
 
231
void
 
232
test_pixel_buffer_sub_region (void)
 
233
{
 
234
  CoglBitmap *bitmap = create_and_fill_bitmap ();
 
235
  CoglPipeline *pipeline;
 
236
  CoglTexture *texture;
 
237
 
 
238
  texture = create_white_texture ();
 
239
 
 
240
  /* Replace the top-right quadrant of the texture with the red part
 
241
   * of the bitmap */
 
242
  cogl_texture_set_region_from_bitmap (texture,
 
243
                                       BITMAP_SIZE / 2, /* src_x */
 
244
                                       BITMAP_SIZE / 2, /* src_y */
 
245
                                       BITMAP_SIZE / 2, /* dst_x */
 
246
                                       0, /* dst_y */
 
247
                                       BITMAP_SIZE / 2, /* width */
 
248
                                       BITMAP_SIZE / 2, /* height */
 
249
                                       bitmap);
 
250
 
 
251
  pipeline = create_pipeline_from_texture (texture);
 
252
 
 
253
  cogl_framebuffer_draw_rectangle (test_fb,
 
254
                                   pipeline,
 
255
                                   -1.0f, 1.0f,
 
256
                                   1.0f, -1.0f);
 
257
 
 
258
  cogl_object_unref (bitmap);
 
259
  cogl_object_unref (texture);
 
260
  cogl_object_unref (pipeline);
 
261
 
 
262
  check_colours (0xffffffff,
 
263
                 0xff0000ff,
 
264
                 0xffffffff,
 
265
                 0xffffffff);
 
266
 
 
267
  if (cogl_test_verbose ())
 
268
    g_print ("OK\n");
 
269
}