tests: Port test-premult

This ports the test-offscreen test from being a Clutter test to a
straight Cogl test.

https://bugzilla.gnome.org/show_bug.cgi?id=660617

Reviewed-by: Robert Bragg <robert@linux.intel.com>
This commit is contained in:
Luca Bruno 2012-04-03 00:41:54 +02:00 committed by Robert Bragg
parent bdb645e7f5
commit d3215b802d
3 changed files with 119 additions and 157 deletions

View File

@ -17,7 +17,6 @@ unported_test_sources = \
test-multitexture.c \ test-multitexture.c \
test-npot-texture.c \ test-npot-texture.c \
test-object.c \ test-object.c \
test-premult.c \
test-readpixels.c \ test-readpixels.c \
test-texture-get-set-data.c \ test-texture-get-set-data.c \
test-texture-mipmaps.c \ test-texture-mipmaps.c \
@ -40,6 +39,7 @@ test_sources = \
test-pipeline-user-matrix.c \ test-pipeline-user-matrix.c \
test-pipeline-uniforms.c \ test-pipeline-uniforms.c \
test-pixel-buffer.c \ test-pixel-buffer.c \
test-premult.c \
test-snippets.c \ test-snippets.c \
test-wrap-modes.c \ test-wrap-modes.c \
test-sub-texture.c \ test-sub-texture.c \

View File

@ -53,7 +53,7 @@ main (int argc, char **argv)
UNPORTED_TEST (test_materials); UNPORTED_TEST (test_materials);
ADD_TEST (test_pipeline_user_matrix, 0); ADD_TEST (test_pipeline_user_matrix, 0);
ADD_TEST (test_blend_strings, 0); ADD_TEST (test_blend_strings, 0);
UNPORTED_TEST (test_premult); ADD_TEST (test_premult, 0);
UNPORTED_TEST (test_readpixels); UNPORTED_TEST (test_readpixels);
ADD_TEST (test_path, 0); ADD_TEST (test_path, 0);
ADD_TEST (test_depth_test, 0); ADD_TEST (test_depth_test, 0);

View File

@ -1,13 +1,10 @@
#include <clutter/clutter.h>
#include <cogl/cogl.h> #include <cogl/cogl.h>
#include <string.h> #include <string.h>
#include "test-conform-common.h" #include "test-utils.h"
static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff }; #define QUAD_WIDTH 32
#define QUAD_WIDTH 20
#define RED 0 #define RED 0
#define GREEN 1 #define GREEN 1
@ -19,12 +16,6 @@ static const ClutterColor stage_color = { 0x0, 0x0, 0x0, 0xff };
#define MASK_BLUE(COLOR) ((COLOR & 0xff00) >> 8) #define MASK_BLUE(COLOR) ((COLOR & 0xff00) >> 8)
#define MASK_ALPHA(COLOR) (COLOR & 0xff) #define MASK_ALPHA(COLOR) (COLOR & 0xff)
typedef struct _TestState
{
ClutterGeometry stage_geom;
CoglHandle passthrough_material;
} TestState;
static guchar * static guchar *
gen_tex_data (guint32 color) gen_tex_data (guint32 color)
{ {
@ -47,54 +38,104 @@ gen_tex_data (guint32 color)
return tex_data; return tex_data;
} }
static CoglHandle static CoglTexture *
make_texture (guint32 color, make_texture (guint32 color,
CoglPixelFormat src_format, CoglPixelFormat src_format,
CoglPixelFormat internal_format) CoglPixelFormat internal_format)
{ {
CoglHandle tex; CoglTexture2D *tex_2d;
guchar *tex_data = gen_tex_data (color); guchar *tex_data = gen_tex_data (color);
tex = cogl_texture_new_from_data (QUAD_WIDTH, tex_2d = cogl_texture_2d_new_from_data (ctx,
QUAD_WIDTH,
QUAD_WIDTH, QUAD_WIDTH,
COGL_TEXTURE_NONE,
src_format, src_format,
internal_format, internal_format,
QUAD_WIDTH * 4, QUAD_WIDTH * 4,
tex_data); tex_data,
NULL);
g_free (tex_data); g_free (tex_data);
return tex; return COGL_TEXTURE (tex_2d);
} }
static void static void
check_texture (TestState *state, set_region (CoglTexture *tex,
guint32 color,
CoglPixelFormat format)
{
guchar *tex_data = gen_tex_data (color);
cogl_texture_set_region (tex,
0, 0, /* src x, y */
0, 0, /* dst x, y */
QUAD_WIDTH, QUAD_WIDTH, /* dst width, height */
QUAD_WIDTH, QUAD_WIDTH, /* src width, height */
format,
0, /* auto compute row stride */
tex_data);
}
static void
check_texture (CoglPipeline *pipeline,
CoglHandle material,
int x, int x,
int y, int y,
CoglHandle tex, CoglTexture *tex,
guint32 expected_result) guint32 expected_result)
{ {
guchar pixel[4]; /* Legacy */
int y_off; cogl_push_framebuffer (fb);
int x_off; cogl_material_set_layer (material, 0, tex);
cogl_set_source (material);
cogl_material_set_layer (state->passthrough_material, 0, tex);
cogl_set_source (state->passthrough_material);
cogl_rectangle (x * QUAD_WIDTH, cogl_rectangle (x * QUAD_WIDTH,
y * QUAD_WIDTH, y * QUAD_WIDTH,
x * QUAD_WIDTH + QUAD_WIDTH, x * QUAD_WIDTH + QUAD_WIDTH,
y * QUAD_WIDTH + QUAD_WIDTH); y * QUAD_WIDTH + QUAD_WIDTH);
test_utils_check_pixel (fb, x * QUAD_WIDTH + QUAD_WIDTH / 2, y * QUAD_WIDTH + QUAD_WIDTH / 2, expected_result);
cogl_pop_framebuffer ();
test_utils_check_pixel (x_off, y_off, expected_result); /* New API */
cogl_pipeline_set_layer_texture (pipeline, 0, COGL_TEXTURE (tex));
cogl_framebuffer_draw_rectangle (fb, pipeline,
x * QUAD_WIDTH,
y * QUAD_WIDTH,
x * QUAD_WIDTH + QUAD_WIDTH,
y * QUAD_WIDTH + QUAD_WIDTH);
test_utils_check_pixel (fb, x * QUAD_WIDTH + QUAD_WIDTH / 2, y * QUAD_WIDTH + QUAD_WIDTH / 2, expected_result);
} }
static void void
on_paint (ClutterActor *actor, TestState *state) test_premult (void)
{ {
CoglHandle tex; CoglPipeline *pipeline;
guchar *tex_data; CoglHandle material;
CoglTexture *tex;
cogl_framebuffer_orthographic (fb, 0, 0,
cogl_framebuffer_get_width (fb),
cogl_framebuffer_get_height (fb),
-1,
100);
cogl_framebuffer_clear4f (fb,
COGL_BUFFER_BIT_COLOR,
1.0f, 1.0f, 1.0f, 1.0f);
/* Legacy */
material = cogl_material_new ();
cogl_material_set_blend (material,
"RGBA = ADD (SRC_COLOR, 0)", NULL);
cogl_material_set_layer_combine (material, 0,
"RGBA = REPLACE (TEXTURE)", NULL);
/* New API */
pipeline = cogl_pipeline_new (ctx);
cogl_pipeline_set_blend (pipeline,
"RGBA = ADD (SRC_COLOR, 0)", NULL);
cogl_pipeline_set_layer_combine (pipeline, 0,
"RGBA = REPLACE (TEXTURE)", NULL);
/* If the user explicitly specifies an unmultiplied internal format then /* If the user explicitly specifies an unmultiplied internal format then
* Cogl shouldn't automatically premultiply the given texture data... */ * Cogl shouldn't automatically premultiply the given texture data... */
@ -104,7 +145,7 @@ on_paint (ClutterActor *actor, TestState *state)
tex = make_texture (0xff00ff80, tex = make_texture (0xff00ff80,
COGL_PIXEL_FORMAT_RGBA_8888, /* src format */ COGL_PIXEL_FORMAT_RGBA_8888, /* src format */
COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */
check_texture (state, 0, 0, /* position */ check_texture (pipeline, material, 0, 0, /* position */
tex, tex,
0xff00ff80); /* expected */ 0xff00ff80); /* expected */
@ -117,7 +158,7 @@ on_paint (ClutterActor *actor, TestState *state)
tex = make_texture (0xff00ff80, tex = make_texture (0xff00ff80,
COGL_PIXEL_FORMAT_RGBA_8888, /* src format */ COGL_PIXEL_FORMAT_RGBA_8888, /* src format */
COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */
check_texture (state, 1, 0, /* position */ check_texture (pipeline, material, 1, 0, /* position */
tex, tex,
0x80008080); /* expected */ 0x80008080); /* expected */
@ -131,7 +172,7 @@ on_paint (ClutterActor *actor, TestState *state)
tex = make_texture (0xff00ff80, tex = make_texture (0xff00ff80,
COGL_PIXEL_FORMAT_RGBA_8888, /* src format */ COGL_PIXEL_FORMAT_RGBA_8888, /* src format */
COGL_PIXEL_FORMAT_ANY); /* internal format */ COGL_PIXEL_FORMAT_ANY); /* internal format */
check_texture (state, 2, 0, /* position */ check_texture (pipeline, material, 2, 0, /* position */
tex, tex,
0x80008080); /* expected */ 0x80008080); /* expected */
@ -145,7 +186,7 @@ on_paint (ClutterActor *actor, TestState *state)
tex = make_texture (0x80008080, tex = make_texture (0x80008080,
COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* src format */ COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* src format */
COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */
check_texture (state, 3, 0, /* position */ check_texture (pipeline, material, 3, 0, /* position */
tex, tex,
0x80008080); /* expected */ 0x80008080); /* expected */
@ -158,7 +199,7 @@ on_paint (ClutterActor *actor, TestState *state)
tex = make_texture (0x80008080, tex = make_texture (0x80008080,
COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* src format */ COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* src format */
COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */
check_texture (state, 4, 0, /* position */ check_texture (pipeline, material, 4, 0, /* position */
tex, tex,
0xff00ff80); /* expected */ 0xff00ff80); /* expected */
@ -172,7 +213,7 @@ on_paint (ClutterActor *actor, TestState *state)
tex = make_texture (0x80008080, tex = make_texture (0x80008080,
COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* src format */ COGL_PIXEL_FORMAT_RGBA_8888_PRE, /* src format */
COGL_PIXEL_FORMAT_ANY); /* internal format */ COGL_PIXEL_FORMAT_ANY); /* internal format */
check_texture (state, 5, 0, /* position */ check_texture (pipeline, material, 5, 0, /* position */
tex, tex,
0x80008080); /* expected */ 0x80008080); /* expected */
@ -188,16 +229,8 @@ on_paint (ClutterActor *actor, TestState *state)
COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */
if (cogl_test_verbose ()) if (cogl_test_verbose ())
g_print ("set_region (0xff00ff80, RGBA_8888)\n"); g_print ("set_region (0xff00ff80, RGBA_8888)\n");
tex_data = gen_tex_data (0xff00ff80); set_region (tex, 0xff00ff80, COGL_PIXEL_FORMAT_RGBA_8888);
cogl_texture_set_region (tex, check_texture (pipeline, material, 6, 0, /* position */
0, 0, /* src x, y */
0, 0, /* dst x, y */
QUAD_WIDTH, QUAD_WIDTH, /* dst width, height */
QUAD_WIDTH, QUAD_WIDTH, /* src width, height */
COGL_PIXEL_FORMAT_RGBA_8888,
0, /* auto compute row stride */
tex_data);
check_texture (state, 6, 0, /* position */
tex, tex,
0xff00ff80); /* expected */ 0xff00ff80); /* expected */
@ -212,16 +245,8 @@ on_paint (ClutterActor *actor, TestState *state)
COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888); /* internal format */
if (cogl_test_verbose ()) if (cogl_test_verbose ())
g_print ("set_region (0x80008080, RGBA_8888_PRE)\n"); g_print ("set_region (0x80008080, RGBA_8888_PRE)\n");
tex_data = gen_tex_data (0x80008080); set_region (tex, 0x80008080, COGL_PIXEL_FORMAT_RGBA_8888_PRE);
cogl_texture_set_region (tex, check_texture (pipeline, material, 7, 0, /* position */
0, 0, /* src x, y */
0, 0, /* dst x, y */
QUAD_WIDTH, QUAD_WIDTH, /* dst width, height */
QUAD_WIDTH, QUAD_WIDTH, /* src width, height */
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
0, /* auto compute row stride */
tex_data);
check_texture (state, 7, 0, /* position */
tex, tex,
0xff00ff80); /* expected */ 0xff00ff80); /* expected */
@ -235,16 +260,8 @@ on_paint (ClutterActor *actor, TestState *state)
COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */
if (cogl_test_verbose ()) if (cogl_test_verbose ())
g_print ("set_region (0x80008080, RGBA_8888_PRE)\n"); g_print ("set_region (0x80008080, RGBA_8888_PRE)\n");
tex_data = gen_tex_data (0x80008080); set_region (tex, 0x80008080, COGL_PIXEL_FORMAT_RGBA_8888_PRE);
cogl_texture_set_region (tex, check_texture (pipeline, material, 8, 0, /* position */
0, 0, /* src x, y */
0, 0, /* dst x, y */
QUAD_WIDTH, QUAD_WIDTH, /* dst width, height */
QUAD_WIDTH, QUAD_WIDTH, /* src width, height */
COGL_PIXEL_FORMAT_RGBA_8888_PRE,
0, /* auto compute row stride */
tex_data);
check_texture (state, 8, 0, /* position */
tex, tex,
0x80008080); /* expected */ 0x80008080); /* expected */
@ -261,66 +278,11 @@ on_paint (ClutterActor *actor, TestState *state)
COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */ COGL_PIXEL_FORMAT_RGBA_8888_PRE); /* internal format */
if (cogl_test_verbose ()) if (cogl_test_verbose ())
g_print ("set_region (0xff00ff80, RGBA_8888)\n"); g_print ("set_region (0xff00ff80, RGBA_8888)\n");
tex_data = gen_tex_data (0xff00ff80); set_region (tex, 0xff00ff80, COGL_PIXEL_FORMAT_RGBA_8888);
cogl_texture_set_region (tex, check_texture (pipeline, material, 9, 0, /* position */
0, 0, /* src x, y */
0, 0, /* dst x, y */
QUAD_WIDTH, QUAD_WIDTH, /* dst width, height */
QUAD_WIDTH, QUAD_WIDTH, /* src width, height */
COGL_PIXEL_FORMAT_RGBA_8888,
0, /* auto compute row stride */
tex_data);
check_texture (state, 9, 0, /* position */
tex, tex,
0x80008080); /* expected */ 0x80008080); /* expected */
/* Comment this out if you want visual feedback for what this test paints */
clutter_main_quit ();
}
static gboolean
queue_redraw (gpointer stage)
{
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
return TRUE;
}
void
test_premult (TestUtilsGTestFixture *fixture,
void *data)
{
TestState state;
ClutterActor *stage;
ClutterActor *group;
unsigned int idle_source;
state.passthrough_material = cogl_material_new ();
cogl_material_set_blend (state.passthrough_material,
"RGBA = ADD (SRC_COLOR, 0)", NULL);
cogl_material_set_layer_combine (state.passthrough_material, 0,
"RGBA = REPLACE (TEXTURE)", NULL);
stage = clutter_stage_get_default ();
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
clutter_actor_get_geometry (stage, &state.stage_geom);
group = clutter_group_new ();
clutter_container_add_actor (CLUTTER_CONTAINER (stage), group);
/* We force continuous redrawing incase someone comments out the
* clutter_main_quit and wants visual feedback for the test since we
* wont be doing anything else that will trigger redrawing. */
idle_source = g_idle_add (queue_redraw, stage);
g_signal_connect (group, "paint", G_CALLBACK (on_paint), &state);
clutter_actor_show_all (stage);
clutter_main ();
g_source_remove (idle_source);
if (cogl_test_verbose ()) if (cogl_test_verbose ())
g_print ("OK\n"); g_print ("OK\n");