tests: Add a utility function for reading a pixel

Most of the conformance tests read a pixel value and assert that it
matches a known value. However they were all doing this with slightly
different methods. This adds a common test_utils_check_pixel function
which they now all use. The function takes an x and y coordinate and a
32-bit value representing the color. It is assumed that writing a
known color is most convenient as an 8 digit hex sequence which this
function allows. There is also a test_utils_check_pixel_rgb function
wrapper which takes the components as separate arguments. This is more
convenient when the expected color is also calculated by the test.

Reviewed-by: Robert Bragg <robert@linux.intel.com>
This commit is contained in:
Neil Roberts 2011-10-26 14:15:14 +01:00
parent 49e733fcdc
commit e69e41b2c7
8 changed files with 78 additions and 185 deletions

View File

@ -25,26 +25,6 @@ typedef struct _TestState
} TestState;
static void
check_pixel (int x, int y, guint32 expected_pixel)
{
guint32 pixel;
char *screen_pixel;
char *intended_pixel;
cogl_read_pixels (x, y, 1, 1, COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
(guint8 *) &pixel);
screen_pixel = g_strdup_printf ("#%06x", GUINT32_FROM_BE (pixel) >> 8);
intended_pixel = g_strdup_printf ("#%06x", expected_pixel >> 8);
g_assert_cmpstr (screen_pixel, ==, intended_pixel);
g_free (screen_pixel);
g_free (intended_pixel);
}
static void
test_blend (TestState *state,
int x,
@ -138,7 +118,7 @@ test_blend (TestState *state,
g_print (" blend constant = UNUSED\n");
}
check_pixel (x_off, y_off, expected_result);
test_utils_check_pixel (x_off, y_off, expected_result);
/*
@ -191,7 +171,7 @@ test_blend (TestState *state,
/* See what we got... */
check_pixel (x_off, y_off, expected_result);
test_utils_check_pixel (x_off, y_off, expected_result);
}
static CoglHandle
@ -308,7 +288,7 @@ test_tex_combine (TestState *state,
g_print (" combine constant = UNUSED\n");
}
check_pixel (x_off, y_off, expected_result);
test_utils_check_pixel (x_off, y_off, expected_result);
}
static void

View File

@ -15,26 +15,6 @@ typedef struct _TestState
CoglFramebuffer *fbo[NUM_FBOS];
} TestState;
static void
check_pixel (int x, int y, guint8 r, guint8 g, guint8 b)
{
guint32 pixel;
char *screen_pixel;
char *intended_pixel;
cogl_read_pixels (x, y, 1, 1, COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
(guint8 *) &pixel);
screen_pixel = g_strdup_printf ("#%06x", GUINT32_FROM_BE (pixel) >> 8);
intended_pixel = g_strdup_printf ("#%02x%02x%02x", r, g, b);
g_assert_cmpstr (screen_pixel, ==, intended_pixel);
g_free (screen_pixel);
g_free (intended_pixel);
}
static void
paint (TestState *state)
{
@ -78,11 +58,11 @@ paint (TestState *state)
{ 0x00, 0xff, 0x00, 0xff },
{ 0x00, 0x00, 0xff, 0xff } };
check_pixel (state->width * (i + 0.5f) / NUM_FBOS,
state->height / 2,
expected_colors[i][0],
expected_colors[i][1],
expected_colors[i][2]);
test_utils_check_pixel_rgb (state->width * (i + 0.5f) / NUM_FBOS,
state->height / 2,
expected_colors[i][0],
expected_colors[i][1],
expected_colors[i][2]);
}
}

View File

@ -32,28 +32,6 @@ typedef struct
float range_far;
} TestDepthState;
static void
check_pixel (GLubyte *pixel, guint32 color)
{
guint8 r = MASK_RED (color);
guint8 g = MASK_GREEN (color);
guint8 b = MASK_BLUE (color);
guint8 a = MASK_ALPHA (color);
if (g_test_verbose ())
g_print (" expected = %x, %x, %x, %x\n",
r, g, b, a);
/* FIXME - allow for hardware in-precision */
g_assert_cmpint (pixel[RED], ==, r);
g_assert_cmpint (pixel[GREEN], ==, g);
g_assert_cmpint (pixel[BLUE], ==, b);
/* FIXME
* We ignore the alpha, since we don't know if our render target is
* RGB or RGBA */
/* g_assert (pixel[ALPHA] == a); */
}
static gboolean
draw_rectangle (TestState *state,
int x,
@ -108,9 +86,6 @@ test_depth (TestState *state,
TestDepthState *rect2_state,
guint32 expected_result)
{
GLubyte pixel[4];
int y_off;
int x_off;
gboolean missing_feature = FALSE;
if (rect0_state)
@ -125,17 +100,9 @@ test_depth (TestState *state,
if (missing_feature)
return;
/* See what we got... */
y_off = y * QUAD_WIDTH + (QUAD_WIDTH / 2);
x_off = x * QUAD_WIDTH + (QUAD_WIDTH / 2);
cogl_read_pixels (x_off, y_off, 1, 1,
COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
pixel);
check_pixel (pixel, expected_result);
test_utils_check_pixel (x * QUAD_WIDTH + (QUAD_WIDTH / 2),
y * QUAD_WIDTH + (QUAD_WIDTH / 2),
expected_result);
}
static void

View File

@ -144,33 +144,13 @@ paint (TestState *state)
cogl_object_unref (pipeline);
}
static void
check_pixel (int x, int y, guint8 r, guint8 g, guint8 b)
{
guint32 pixel;
char *screen_pixel;
char *intended_pixel;
cogl_read_pixels (x, y, 1, 1, COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
(guint8 *) &pixel);
screen_pixel = g_strdup_printf ("#%06x", GUINT32_FROM_BE (pixel) >> 8);
intended_pixel = g_strdup_printf ("#%02x%02x%02x", r, g, b);
g_assert_cmpstr (screen_pixel, ==, intended_pixel);
g_free (screen_pixel);
g_free (intended_pixel);
}
static void
validate_result (void)
{
/* Non-shader version */
check_pixel (25, 25, 0, 0xff, 0);
test_utils_check_pixel (25, 25, 0x00ff0000);
/* Shader version */
check_pixel (75, 25, 0, 0xff, 0);
test_utils_check_pixel (75, 25, 0x00ff0000);
}
void

View File

@ -25,44 +25,12 @@ typedef struct _TestState
ClutterGeometry stage_geom;
} TestState;
static void
check_pixel (TestState *state, int x, int y, guint32 color)
check_quad (int quad_x, int quad_y, guint32 color)
{
GLint y_off;
GLint x_off;
GLubyte pixel[4];
guint8 r = MASK_RED (color);
guint8 g = MASK_GREEN (color);
guint8 b = MASK_BLUE (color);
guint8 a = MASK_ALPHA (color);
/* See what we got... */
/* NB: glReadPixels is done in GL screen space so y = 0 is at the bottom */
y_off = y * QUAD_WIDTH + (QUAD_WIDTH / 2);
x_off = x * QUAD_WIDTH + (QUAD_WIDTH / 2);
cogl_read_pixels (x_off, y_off, 1, 1,
COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
pixel);
if (g_test_verbose ())
g_print (" result = %02x, %02x, %02x, %02x\n",
pixel[RED], pixel[GREEN], pixel[BLUE], pixel[ALPHA]);
if (g_test_verbose ())
g_print (" expected = %x, %x, %x, %x\n",
r, g, b, a);
/* FIXME - allow for hardware in-precision */
g_assert (pixel[RED] == r);
g_assert (pixel[GREEN] == g);
g_assert (pixel[BLUE] == b);
/* FIXME
* We ignore the alpha, since we don't know if our render target is
* RGB or RGBA */
/* g_assert (pixel[ALPHA] == a); */
test_utils_check_pixel (x * QUAD_WIDTH + (QUAD_WIDTH / 2),
y * QUAD_WIDTH + (QUAD_WIDTH / 2),
color);
}
static void
@ -104,9 +72,9 @@ test_material_with_primitives (TestState *state,
cogl_pop_matrix ();
check_pixel (state, x, y, color);
check_pixel (state, x, y+1, color);
check_pixel (state, x, y+2, color);
check_quad (x, y, color);
check_quad (x, y+1, color);
check_quad (x, y+2, color);
}
static void

View File

@ -25,29 +25,6 @@ typedef struct _TestState
CoglHandle passthrough_material;
} TestState;
static void
check_pixel (GLubyte *pixel, guint32 color)
{
guint8 r = MASK_RED (color);
guint8 g = MASK_GREEN (color);
guint8 b = MASK_BLUE (color);
guint8 a = MASK_ALPHA (color);
if (g_test_verbose ())
g_print (" expected = %x, %x, %x, %x\n",
r, g, b, a);
/* FIXME - allow for hardware in-precision */
g_assert (pixel[RED] == r);
g_assert (pixel[GREEN] == g);
g_assert (pixel[BLUE] == b);
/* FIXME
* We ignore the alpha, since we don't know if our render target is
* RGB or RGBA */
/* g_assert (pixel[ALPHA] == a); */
}
static guchar *
gen_tex_data (guint32 color)
{
@ -110,23 +87,7 @@ check_texture (TestState *state,
x * QUAD_WIDTH + QUAD_WIDTH,
y * QUAD_WIDTH + QUAD_WIDTH);
/* See what we got... */
y_off = y * QUAD_WIDTH + (QUAD_WIDTH / 2);
x_off = x * QUAD_WIDTH + (QUAD_WIDTH / 2);
cogl_read_pixels (x_off, y_off, 1, 1,
COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
pixel);
if (g_test_verbose ())
{
g_print ("check texture (%d, %d):\n", x, y);
g_print (" result = %02x, %02x, %02x, %02x\n",
pixel[RED], pixel[GREEN], pixel[BLUE], pixel[ALPHA]);
}
check_pixel (pixel, expected_result);
test_utils_check_pixel (x_off, y_off, expected_result);
}
static void

View File

@ -78,3 +78,29 @@ test_utils_fini (TestUtilsGTestFixture *fixture,
if (state->ctx)
cogl_object_unref (state->ctx);
}
void
test_utils_check_pixel (int x, int y, guint32 expected_pixel)
{
guint32 pixel;
char *screen_pixel;
char *intended_pixel;
cogl_read_pixels (x, y, 1, 1, COGL_READ_PIXELS_COLOR_BUFFER,
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
(guint8 *) &pixel);
screen_pixel = g_strdup_printf ("#%06x", GUINT32_FROM_BE (pixel) >> 8);
intended_pixel = g_strdup_printf ("#%06x", expected_pixel >> 8);
g_assert_cmpstr (screen_pixel, ==, intended_pixel);
g_free (screen_pixel);
g_free (intended_pixel);
}
void
test_utils_check_pixel_rgb (int x, int y, int r, int g, int b)
{
test_utils_check_pixel (x, y, (r << 24) | (g << 16) | (b << 8));
}

View File

@ -38,4 +38,35 @@ void
test_utils_fini (TestUtilsGTestFixture *fixture,
const void *data);
/*
* test_utils_check_pixel:
* @x: x co-ordinate of the pixel to test
* @y: y co-ordinate of the pixel to test
* @pixel: An integer of the form 0xRRGGBBAA representing the expected
* pixel value
*
* This performs reads a pixel on the current cogl framebuffer and
* asserts that it matches the given color. The alpha channel of the
* color is ignored. The pixels are converted to a string and compared
* with g_assert_cmpstr so that if the comparison fails then the
* assert will display a meaningful message
*/
void
test_utils_check_pixel (int x, int y, guint32 expected_pixel);
/*
* test_utils_check_pixel:
* @x: x co-ordinate of the pixel to test
* @y: y co-ordinate of the pixel to test
* @pixel: An integer of the form 0xrrggbb representing the expected pixel value
*
* This performs reads a pixel on the current cogl framebuffer and
* asserts that it matches the given color. The alpha channel of the
* color is ignored. The pixels are converted to a string and compared
* with g_assert_cmpstr so that if the comparison fails then the
* assert will display a meaningful message
*/
void
test_utils_check_pixel_rgb (int x, int y, int r, int g, int b);
#endif /* _TEST_UTILS_H_ */