Simplify test-pixel-buffer
test-pixel-buffer previously had two tests, one to check filling the pixel buffer by mapping it and another to fill it by just setting the data. These tests were set up in a kind of confusing way where it would try to paint both steps and then validate them together using colors looked up from a table. This patch separates out the two tests and gets rid of the tables which hopefully makes them a bit easier to follow. The contents of the bitmap are now set to an image with has a different colour for each of its four quadrants instead of just a single colour in the hope that this will be a bit more of an extensive test. The old code had a third test that was commented out. This test has been removed. The textures are now created using cogl_texture_2d_new_* which means they won't be in the atlas. This exposes a bug where setting the entire contents of the texture won't handle errors properly and it will hit an assertion. The previous code using the atlas would end up only setting a sub-region of the larger atlas texture so the bug wouldn't be hit. To make sure we still test this code path there is now a third test which explicitly sets a sub-region of the texture using the bitmap. Reviewed-by: Robert Bragg <robert@linux.intel.com> (cherry picked from commit 8beb3a4cc20f539a50645166485b95e8e5b25779)
This commit is contained in:
parent
4b41743b92
commit
c72ede0560
@ -68,7 +68,9 @@ main (int argc, char **argv)
|
||||
UNPORTED_TEST (test_multitexture);
|
||||
UNPORTED_TEST (test_texture_mipmaps);
|
||||
ADD_TEST (test_sub_texture, 0, 0);
|
||||
ADD_TEST (test_pixel_buffer, 0, 0);
|
||||
ADD_TEST (test_pixel_buffer_map, 0, TEST_KNOWN_FAILURE);
|
||||
ADD_TEST (test_pixel_buffer_set_data, 0, TEST_KNOWN_FAILURE);
|
||||
ADD_TEST (test_pixel_buffer_sub_region, 0, 0);
|
||||
UNPORTED_TEST (test_texture_rectangle);
|
||||
ADD_TEST (test_texture_3d, TEST_REQUIREMENT_TEXTURE_3D, 0);
|
||||
ADD_TEST (test_wrap_modes, 0, 0);
|
||||
|
@ -3,62 +3,43 @@
|
||||
|
||||
#include "test-utils.h"
|
||||
|
||||
#define TILE_SIZE 32.0f
|
||||
#define BITMAP_SIZE 256
|
||||
|
||||
enum
|
||||
/*
|
||||
* 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)
|
||||
{
|
||||
TILE_MAP,
|
||||
TILE_SET_DATA,
|
||||
NB_TILES,
|
||||
TILE_SET_REGION,
|
||||
};
|
||||
int y, x;
|
||||
|
||||
typedef struct test_tile
|
||||
{
|
||||
uint8_t color[4];
|
||||
gfloat x, y;
|
||||
CoglBuffer *buffer;
|
||||
CoglTexture *texture;
|
||||
} TestTile;
|
||||
|
||||
typedef struct _TestState
|
||||
{
|
||||
TestTile *tiles;
|
||||
int width;
|
||||
int height;
|
||||
} TestState;
|
||||
|
||||
static CoglTexture *
|
||||
create_texture_from_bitmap (CoglBitmap *bitmap)
|
||||
{
|
||||
CoglTexture *texture;
|
||||
|
||||
texture = cogl_texture_new_from_bitmap (bitmap,
|
||||
COGL_TEXTURE_NONE,
|
||||
COGL_PIXEL_FORMAT_RGBA_8888);
|
||||
|
||||
g_assert (texture != NULL);
|
||||
|
||||
return texture;
|
||||
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 void
|
||||
create_map_tile (CoglContext *context,
|
||||
TestTile *tile)
|
||||
static CoglBitmap *
|
||||
create_bitmap (void)
|
||||
{
|
||||
CoglBitmap *bitmap;
|
||||
CoglBuffer *buffer;
|
||||
guchar *map;
|
||||
unsigned int i;
|
||||
unsigned int stride;
|
||||
uint8_t *line;
|
||||
|
||||
bitmap = cogl_bitmap_new_with_size (context,
|
||||
TILE_SIZE,
|
||||
TILE_SIZE,
|
||||
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));
|
||||
stride = cogl_bitmap_get_rowstride (bitmap);
|
||||
|
||||
g_assert (cogl_is_pixel_buffer (buffer));
|
||||
g_assert (cogl_is_buffer (buffer));
|
||||
@ -68,207 +49,224 @@ create_map_tile (CoglContext *context,
|
||||
==,
|
||||
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);
|
||||
g_assert (map);
|
||||
|
||||
line = g_alloca (TILE_SIZE * 4);
|
||||
for (i = 0; i < TILE_SIZE * 4; i += 4)
|
||||
memcpy (line + i, tile->color, 4);
|
||||
generate_bitmap_data (map, stride);
|
||||
|
||||
for (i = 0; i < TILE_SIZE; i++)
|
||||
memcpy (map + stride * i, line, TILE_SIZE * 4);
|
||||
cogl_buffer_unmap (COGL_BUFFER (buffer));
|
||||
|
||||
cogl_buffer_unmap (buffer);
|
||||
|
||||
tile->buffer = cogl_object_ref (buffer);
|
||||
tile->texture = create_texture_from_bitmap (bitmap);
|
||||
|
||||
cogl_object_unref (bitmap);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static void
|
||||
create_set_region_tile (CoglContext *context,
|
||||
TestTile *tile)
|
||||
static CoglTexture *
|
||||
create_texture_from_bitmap (CoglBitmap *bitmap)
|
||||
{
|
||||
CoglBitmap *bitmap;
|
||||
CoglBuffer *buffer;
|
||||
uint8_t bottom_color[4];
|
||||
unsigned int rowstride = 0;
|
||||
guchar *data;
|
||||
unsigned int i;
|
||||
CoglTexture2D *texture;
|
||||
|
||||
bitmap = cogl_bitmap_new_with_size (context,
|
||||
TILE_SIZE,
|
||||
TILE_SIZE,
|
||||
COGL_PIXEL_FORMAT_RGBA_8888);
|
||||
buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
|
||||
rowstride = cogl_bitmap_get_rowstride (bitmap);
|
||||
texture = cogl_texture_2d_new_from_bitmap (bitmap,
|
||||
COGL_PIXEL_FORMAT_RGBA_8888,
|
||||
NULL); /* don't catch errors */
|
||||
|
||||
g_assert (cogl_is_pixel_buffer (buffer));
|
||||
g_assert (cogl_is_buffer (buffer));
|
||||
g_assert (texture != NULL);
|
||||
|
||||
/* while at it, set/get the hint */
|
||||
cogl_buffer_set_update_hint (buffer, COGL_BUFFER_UPDATE_HINT_STATIC);
|
||||
g_assert (cogl_buffer_get_update_hint (buffer) ==
|
||||
COGL_BUFFER_UPDATE_HINT_STATIC);
|
||||
|
||||
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);
|
||||
|
||||
memcpy (bottom_color, tile->color, 4);
|
||||
for (i = 0; i < TILE_SIZE / 2; i++)
|
||||
memcpy (data + i, bottom_color, 4);
|
||||
|
||||
cogl_buffer_set_data (buffer, 0, data, TILE_SIZE * TILE_SIZE * 4 / 2);
|
||||
|
||||
g_free (data);
|
||||
|
||||
tile->buffer = cogl_object_ref (buffer);
|
||||
tile->texture = create_texture_from_bitmap (bitmap);
|
||||
|
||||
cogl_object_unref (bitmap);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
create_set_data_tile (CoglContext *context,
|
||||
TestTile *tile)
|
||||
{
|
||||
CoglBitmap *bitmap;
|
||||
CoglBuffer *buffer;
|
||||
unsigned int rowstride = 0;
|
||||
CoglBool res;
|
||||
guchar *data;
|
||||
unsigned int i;
|
||||
|
||||
bitmap = cogl_bitmap_new_with_size (context,
|
||||
TILE_SIZE,
|
||||
TILE_SIZE,
|
||||
COGL_PIXEL_FORMAT_RGBA_8888);
|
||||
buffer = COGL_BUFFER (cogl_bitmap_get_buffer (bitmap));
|
||||
rowstride = cogl_bitmap_get_rowstride (bitmap);
|
||||
|
||||
g_assert (cogl_is_pixel_buffer (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 = cogl_object_ref (buffer);
|
||||
tile->texture = create_texture_from_bitmap (bitmap);
|
||||
|
||||
cogl_object_unref (bitmap);
|
||||
return COGL_TEXTURE (texture);
|
||||
}
|
||||
|
||||
static void
|
||||
draw_frame (TestState *state)
|
||||
static CoglPipeline *
|
||||
create_pipeline_from_texture (CoglTexture *texture)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
/* Paint the textures */
|
||||
for (i = 0; i < NB_TILES; i++)
|
||||
{
|
||||
CoglPipeline *pipeline = cogl_pipeline_new (test_ctx);
|
||||
cogl_pipeline_set_layer_texture (pipeline, 0, state->tiles[i].texture);
|
||||
cogl_framebuffer_draw_rectangle (test_fb,
|
||||
pipeline,
|
||||
state->tiles[i].x,
|
||||
state->tiles[i].y,
|
||||
state->tiles[i].x + TILE_SIZE,
|
||||
state->tiles[i].y + TILE_SIZE);
|
||||
cogl_object_unref (pipeline);
|
||||
}
|
||||
|
||||
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
|
||||
validate_tile (TestState *state,
|
||||
TestTile *tile)
|
||||
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,
|
||||
tile->x, tile->y,
|
||||
TILE_SIZE, TILE_SIZE,
|
||||
(tile->color[0] << 24) |
|
||||
(tile->color[1] << 16) |
|
||||
(tile->color[2] << 8) |
|
||||
0xff);
|
||||
}
|
||||
|
||||
static void
|
||||
validate_result (TestState *state)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < NB_TILES; i++)
|
||||
validate_tile (state, &state->tiles[i]);
|
||||
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 (void)
|
||||
test_pixel_buffer_map (void)
|
||||
{
|
||||
TestState state;
|
||||
int i;
|
||||
static TestTile tiles[NB_TILES] =
|
||||
{
|
||||
/* color x y buffer tex */
|
||||
CoglBitmap *bitmap = create_and_fill_bitmap ();
|
||||
CoglPipeline *pipeline;
|
||||
CoglTexture *texture;
|
||||
|
||||
/* 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 }
|
||||
};
|
||||
texture = create_texture_from_bitmap (bitmap);
|
||||
pipeline = create_pipeline_from_texture (texture);
|
||||
|
||||
state.width = cogl_framebuffer_get_width (test_fb);
|
||||
state.height = cogl_framebuffer_get_height (test_fb);
|
||||
cogl_framebuffer_orthographic (test_fb,
|
||||
0, 0,
|
||||
state.width,
|
||||
state.height,
|
||||
-1,
|
||||
100);
|
||||
cogl_framebuffer_draw_rectangle (test_fb,
|
||||
pipeline,
|
||||
-1.0f, 1.0f,
|
||||
1.0f, -1.0f);
|
||||
|
||||
create_map_tile (test_ctx, &tiles[TILE_MAP]);
|
||||
#if 0
|
||||
create_set_region_tile (shared_state->ctx, &tiles[TILE_SET_REGION]);
|
||||
#endif
|
||||
create_set_data_tile (test_ctx, &tiles[TILE_SET_DATA]);
|
||||
cogl_object_unref (bitmap);
|
||||
cogl_object_unref (texture);
|
||||
cogl_object_unref (pipeline);
|
||||
|
||||
state.tiles = tiles;
|
||||
|
||||
draw_frame (&state);
|
||||
validate_result (&state);
|
||||
|
||||
for (i = 0; i < NB_TILES; i++)
|
||||
{
|
||||
cogl_object_unref (state.tiles[i].buffer);
|
||||
cogl_object_unref (state.tiles[i].texture);
|
||||
}
|
||||
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);
|
||||
|
||||
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, /* dst_x */
|
||||
0, /* dst_y */
|
||||
BITMAP_SIZE / 2, /* dst_width */
|
||||
BITMAP_SIZE / 2, /* dst_height */
|
||||
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 (0xffffffff,
|
||||
0xff0000ff,
|
||||
0xffffffff,
|
||||
0xffffffff);
|
||||
|
||||
if (cogl_test_verbose ())
|
||||
g_print ("OK\n");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user