tests: introduce struct buffer for client-helper

We are growing more tests that need to handle buffers, both just images
and wl_buffers. Particularly the screenshooting facility needs these.
Currently everything is in struct surface, which contains more than we
need. It is a bit messy.

Create a new struct buffer to encapsulate the image representation, the
wl_buffer, and enough information to tear it all down (munmap) so we
don't have to leak everything. Some tests might start doing things in
loops, and leaking would accumulate.

Instead of inventing our own image representation, use pixman_image_t.
It is a well-tested library worth using, and we already rely on it in
other places.

This makes the tests depend on Pixman, which requires the fix for
building buffer-count, which would otherwise not find pixman.h.

The new create_shm_buffer_a8r8g8b8() creates an image with an explicit
format, and pixman_image_t keeps track of it. And stride and size and
data. This implementation is still a little hacky due to calling
create_shm_buffer().

A very new thing is buffer_destroy(). Previously we didn't really free
any buffers. It is not a problem when the process will exit soon anyway,
but it may become a problem if tests start iterating things.

Manual memset() on a image is converted to a pixman action, just to show
how to do it properly with pixman.

Stride and pixel format assumptions still linger all around, but those
are for another patch.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Reviewed-by: Daniel Stone <daniels@collabora.com>
This commit is contained in:
Pekka Paalanen 2016-05-20 17:25:38 +03:00
parent 37d25bb1e0
commit 924cd948ee
8 changed files with 102 additions and 32 deletions

View File

@ -1267,7 +1267,7 @@ viewporter_weston_LDADD = libtest-client.la
if ENABLE_EGL if ENABLE_EGL
weston_tests += buffer-count.weston weston_tests += buffer-count.weston
buffer_count_weston_SOURCES = tests/buffer-count-test.c buffer_count_weston_SOURCES = tests/buffer-count-test.c
buffer_count_weston_CFLAGS = $(AM_CFLAGS) $(EGL_TESTS_CFLAGS) buffer_count_weston_CFLAGS = $(AM_CFLAGS) $(EGL_TESTS_CFLAGS) $(TEST_CLIENT_CFLAGS)
buffer_count_weston_LDADD = libtest-client.la $(EGL_TESTS_LIBS) buffer_count_weston_LDADD = libtest-client.la $(EGL_TESTS_LIBS)
endif endif

View File

@ -322,7 +322,7 @@ AM_CONDITIONAL(ENABLE_VAAPI_RECORDER, test "x$have_libva" = xyes)
PKG_CHECK_MODULES(CAIRO, [cairo]) PKG_CHECK_MODULES(CAIRO, [cairo])
PKG_CHECK_MODULES(TEST_CLIENT, [wayland-client >= $WAYLAND_PREREQ_VERSION]) PKG_CHECK_MODULES(TEST_CLIENT, [wayland-client >= $WAYLAND_PREREQ_VERSION pixman-1])
AC_ARG_ENABLE(simple-clients, AC_ARG_ENABLE(simple-clients,
AS_HELP_STRING([--disable-simple-clients], AS_HELP_STRING([--disable-simple-clients],

View File

@ -413,7 +413,7 @@ TEST(buffer_release)
assert(buf2_released == 1); assert(buf2_released == 1);
/* buf3 may or may not be released */ /* buf3 may or may not be released */
wl_surface_attach(surface, client->surface->wl_buffer, 0, 0); wl_surface_attach(surface, client->surface->buffer->proxy, 0, 0);
frame_callback_set(surface, &frame); frame_callback_set(surface, &frame);
wl_surface_commit(surface); wl_surface_commit(surface);
frame_callback_wait(client, &frame); frame_callback_wait(client, &frame);

View File

@ -115,7 +115,7 @@ TEST(internal_screenshot)
match = check_surfaces_equal(screenshot, reference_bad); match = check_surfaces_equal(screenshot, reference_bad);
printf("Screenshot %s reference image\n", match? "equal to" : "different from"); printf("Screenshot %s reference image\n", match? "equal to" : "different from");
assert(!match); assert(!match);
free(reference_bad->data); buffer_destroy(reference_bad->buffer);
free(reference_bad); free(reference_bad);
/* Test check_surfaces_match_in_clip() /* Test check_surfaces_match_in_clip()
@ -130,7 +130,7 @@ TEST(internal_screenshot)
match = check_surfaces_match_in_clip(screenshot, reference_good, match = check_surfaces_match_in_clip(screenshot, reference_good,
&clip); &clip);
printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match"); printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match");
free(reference_good->data); buffer_destroy(reference_good->buffer);
free(reference_good); free(reference_good);
/* Test dumping of non-matching images */ /* Test dumping of non-matching images */

View File

@ -226,7 +226,7 @@ TEST(test_presentation_feedback_simple)
assert(client); assert(client);
wl_surface_attach(client->surface->wl_surface, wl_surface_attach(client->surface->wl_surface,
client->surface->wl_buffer, 0, 0); client->surface->buffer->proxy, 0, 0);
fb = feedback_create(client, client->surface->wl_surface); fb = feedback_create(client, client->surface->wl_surface);
wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100); wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
wl_surface_commit(client->surface->wl_surface); wl_surface_commit(client->surface->wl_surface);

View File

@ -301,7 +301,7 @@ setup_source_vs_buffer(struct client *client,
wl_surface_set_buffer_scale(surf, args->buffer_scale); wl_surface_set_buffer_scale(surf, args->buffer_scale);
wl_surface_set_buffer_transform(surf, args->buffer_transform); wl_surface_set_buffer_transform(surf, args->buffer_transform);
wl_surface_attach(surf, client->surface->wl_buffer, 0, 0); wl_surface_attach(surf, client->surface->buffer->proxy, 0, 0);
wp_viewport_set_source(vp, args->x, args->y, args->w, args->h); wp_viewport_set_source(vp, args->x, args->y, args->w, args->h);
wp_viewport_set_destination(vp, 99, 99); wp_viewport_set_destination(vp, 99, 99);
wl_surface_commit(surf); wl_surface_commit(surf);
@ -496,7 +496,7 @@ TEST(test_viewporter_outside_null_buffer)
client_roundtrip(client); client_roundtrip(client);
/* When buffer comes back, source rect matters again. */ /* When buffer comes back, source rect matters again. */
wl_surface_attach(surf, client->surface->wl_buffer, 0, 0); wl_surface_attach(surf, client->surface->buffer->proxy, 0, 0);
wl_surface_commit(surf); wl_surface_commit(surf);
expect_protocol_error(client, &wp_viewport_interface, expect_protocol_error(client, &wp_viewport_interface,
WP_VIEWPORT_ERROR_OUT_OF_BUFFER); WP_VIEWPORT_ERROR_OUT_OF_BUFFER);

View File

@ -103,7 +103,7 @@ move_client(struct client *client, int x, int y)
/* The attach here is necessary because commit() will call configure /* The attach here is necessary because commit() will call configure
* only on surfaces newly attached, and the one that sets the surface * only on surfaces newly attached, and the one that sets the surface
* position is the configure. */ * position is the configure. */
wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0); wl_surface_attach(surface->wl_surface, surface->buffer->proxy, 0, 0);
wl_surface_damage(surface->wl_surface, 0, 0, surface->width, wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
surface->height); surface->height);
@ -447,6 +447,41 @@ create_shm_buffer(struct client *client, int width, int height, void **pixels)
return buffer; return buffer;
} }
struct buffer *
create_shm_buffer_a8r8g8b8(struct client *client, int width, int height)
{
struct buffer *buf;
void *pixels;
buf = xzalloc(sizeof *buf);
buf->proxy = create_shm_buffer(client, width, height, &pixels);
buf->image = pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
pixels, width * 4);
buf->len = width * height * 4;
assert(buf->proxy);
assert(buf->image);
return buf;
}
void
buffer_destroy(struct buffer *buf)
{
void *pixels;
pixels = pixman_image_get_data(buf->image);
if (buf->proxy) {
wl_buffer_destroy(buf->proxy);
assert(munmap(pixels, buf->len) == 0);
}
assert(pixman_image_unref(buf->image));
free(buf);
}
static void static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{ {
@ -845,6 +880,8 @@ create_client_and_test_surface(int x, int y, int width, int height)
{ {
struct client *client; struct client *client;
struct surface *surface; struct surface *surface;
pixman_color_t color = { 16384, 16384, 16384, 16384 }; /* uint16_t */
pixman_image_t *solid;
client = create_client(); client = create_client();
@ -862,10 +899,18 @@ create_client_and_test_surface(int x, int y, int width, int height)
surface->width = width; surface->width = width;
surface->height = height; surface->height = height;
surface->wl_buffer = create_shm_buffer(client, width, height, surface->buffer = create_shm_buffer_a8r8g8b8(client, width, height);
&surface->data);
memset(surface->data, 64, width * height * 4); solid = pixman_image_create_solid_fill(&color);
pixman_image_composite32(PIXMAN_OP_SRC,
solid, /* src */
NULL, /* mask */
surface->buffer->image, /* dst */
0, 0, /* src x,y */
0, 0, /* mask x,y */
0, 0, /* dst x,y */
width, height);
pixman_image_unref(solid);
move_client(client, x, y); move_client(client, x, y);
@ -927,7 +972,7 @@ check_surfaces_geometry(const struct surface *a, const struct surface *b)
printf("Undefined surfaces\n"); printf("Undefined surfaces\n");
return false; return false;
} }
else if (a->data == NULL || b->data == NULL) { else if (a->buffer == NULL || b->buffer == NULL) {
printf("Undefined data\n"); printf("Undefined data\n");
return false; return false;
} }
@ -950,11 +995,16 @@ bool
check_surfaces_equal(const struct surface *a, const struct surface *b) check_surfaces_equal(const struct surface *a, const struct surface *b)
{ {
int bpp = 4; /* Assumes ARGB */ int bpp = 4; /* Assumes ARGB */
void *data_a;
void *data_b;
if (!check_surfaces_geometry(a, b)) if (!check_surfaces_geometry(a, b))
return false; return false;
return (memcmp(a->data, b->data, bpp * a->width * a->height) == 0); data_a = pixman_image_get_data(a->buffer->image);
data_b = pixman_image_get_data(b->buffer->image);
return (memcmp(data_a, data_b, bpp * a->width * a->height) == 0);
} }
/** /**
@ -972,6 +1022,8 @@ check_surfaces_match_in_clip(const struct surface *a, const struct surface *b, c
int x0, y0, x1, y1; int x0, y0, x1, y1;
void *p, *q; void *p, *q;
int bpp = 4; /* Assumes ARGB */ int bpp = 4; /* Assumes ARGB */
void *data_a;
void *data_b;
if (!check_surfaces_geometry(a, b) || clip_rect == NULL) if (!check_surfaces_geometry(a, b) || clip_rect == NULL)
return false; return false;
@ -991,10 +1043,13 @@ check_surfaces_match_in_clip(const struct surface *a, const struct surface *b, c
return true; return true;
} }
data_a = pixman_image_get_data(a->buffer->image);
data_b = pixman_image_get_data(b->buffer->image);
printf("Bytewise comparison inside clip\n"); printf("Bytewise comparison inside clip\n");
for (i=y0; i<y1; i++) { for (i=y0; i<y1; i++) {
p = a->data + i * a->width * bpp + x0 * bpp; p = data_a + i * a->width * bpp + x0 * bpp;
q = b->data + i * b->width * bpp + x0 * bpp; q = data_b + i * b->width * bpp + x0 * bpp;
if (memcmp(p, q, (x1-x0)*bpp) != 0) { if (memcmp(p, q, (x1-x0)*bpp) != 0) {
/* Dump the bad row */ /* Dump the bad row */
printf("Mismatched image on row %d\n", i); printf("Mismatched image on row %d\n", i);
@ -1025,8 +1080,10 @@ write_surface_as_png(const struct surface *weston_surface, const char *fname)
cairo_status_t status; cairo_status_t status;
int bpp = 4; /* Assume ARGB */ int bpp = 4; /* Assume ARGB */
int stride = bpp * weston_surface->width; int stride = bpp * weston_surface->width;
void *pixels;
cairo_surface = cairo_image_surface_create_for_data(weston_surface->data, pixels = pixman_image_get_data(weston_surface->buffer->image);
cairo_surface = cairo_image_surface_create_for_data(pixels,
CAIRO_FORMAT_ARGB32, CAIRO_FORMAT_ARGB32,
weston_surface->width, weston_surface->width,
weston_surface->height, weston_surface->height,
@ -1091,14 +1148,15 @@ load_surface_from_png(const char *fname)
/* Allocate new buffer for our weston reference, and copy the data from /* Allocate new buffer for our weston reference, and copy the data from
the cairo surface so we can destroy it */ the cairo surface so we can destroy it */
reference->data = zalloc(source_data_size);
if (reference->data == NULL) { reference->buffer = xzalloc(sizeof *reference->buffer);
perror("zalloc reference data"); reference->buffer->image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
cairo_surface_destroy(reference_cairo_surface); reference->width,
free(reference); reference->height,
return NULL; NULL, 0);
} assert(reference->buffer->image);
memcpy(reference->data,
memcpy(pixman_image_get_data(reference->buffer->image),
cairo_image_surface_get_data(reference_cairo_surface), cairo_image_surface_get_data(reference_cairo_surface),
source_data_size); source_data_size);
@ -1123,10 +1181,10 @@ create_screenshot_surface(struct client *client)
screenshot = zalloc(sizeof *screenshot); screenshot = zalloc(sizeof *screenshot);
if (screenshot == NULL) if (screenshot == NULL)
return NULL; return NULL;
screenshot->wl_buffer = create_shm_buffer(client,
screenshot->buffer = create_shm_buffer_a8r8g8b8(client,
client->output->width, client->output->width,
client->output->height, client->output->height);
&screenshot->data);
screenshot->height = client->output->height; screenshot->height = client->output->height;
screenshot->width = client->output->width; screenshot->width = client->output->width;
@ -1153,7 +1211,7 @@ capture_screenshot_of_output(struct client *client)
client->test->buffer_copy_done = 0; client->test->buffer_copy_done = 0;
weston_test_capture_screenshot(client->test->weston_test, weston_test_capture_screenshot(client->test->weston_test,
client->output->wl_output, client->output->wl_output,
screenshot->wl_buffer); screenshot->buffer->proxy);
while (client->test->buffer_copy_done == 0) while (client->test->buffer_copy_done == 0)
if (wl_display_dispatch(client->wl_display) < 0) if (wl_display_dispatch(client->wl_display) < 0)
break; break;

View File

@ -30,6 +30,7 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <pixman.h>
#include <wayland-client-protocol.h> #include <wayland-client-protocol.h>
#include "weston-test-runner.h" #include "weston-test-runner.h"
@ -127,15 +128,20 @@ struct output {
int initialized; int initialized;
}; };
struct buffer {
struct wl_buffer *proxy;
size_t len;
pixman_image_t *image;
};
struct surface { struct surface {
struct wl_surface *wl_surface; struct wl_surface *wl_surface;
struct wl_buffer *wl_buffer;
struct output *output; struct output *output;
int x; int x;
int y; int y;
int width; int width;
int height; int height;
void *data; struct buffer *buffer;
}; };
struct rectangle { struct rectangle {
@ -154,6 +160,12 @@ create_client_and_test_surface(int x, int y, int width, int height);
struct wl_buffer * struct wl_buffer *
create_shm_buffer(struct client *client, int width, int height, void **pixels); create_shm_buffer(struct client *client, int width, int height, void **pixels);
struct buffer *
create_shm_buffer_a8r8g8b8(struct client *client, int width, int height);
void
buffer_destroy(struct buffer *buf);
int int
surface_contains(struct surface *surface, int x, int y); surface_contains(struct surface *surface, int x, int y);