[PATCH weston 01/11] tests: introduce struct buffer for client-helper

Pekka Paalanen ppaalanen at gmail.com
Thu Jun 16 14:13:20 UTC 2016


From: Pekka Paalanen <pekka.paalanen at collabora.co.uk>

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 at collabora.co.uk>
---
 Makefile.am                       |   2 +-
 configure.ac                      |   2 +-
 tests/event-test.c                |   2 +-
 tests/internal-screenshot-test.c  |   4 +-
 tests/presentation-test.c         |   2 +-
 tests/viewporter-test.c           |   4 +-
 tests/weston-test-client-helper.c | 104 +++++++++++++++++++++++++++++---------
 tests/weston-test-client-helper.h |  16 +++++-
 8 files changed, 103 insertions(+), 33 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 8466a5f..6be4778 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1267,7 +1267,7 @@ viewporter_weston_LDADD = libtest-client.la
 if ENABLE_EGL
 weston_tests += buffer-count.weston
 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)
 endif
 
diff --git a/configure.ac b/configure.ac
index 5364c59..b29d1ef 100644
--- a/configure.ac
+++ b/configure.ac
@@ -322,7 +322,7 @@ AM_CONDITIONAL(ENABLE_VAAPI_RECORDER, test "x$have_libva" = xyes)
 
 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,
               AS_HELP_STRING([--disable-simple-clients],
diff --git a/tests/event-test.c b/tests/event-test.c
index 96d344c..b616028 100644
--- a/tests/event-test.c
+++ b/tests/event-test.c
@@ -413,7 +413,7 @@ TEST(buffer_release)
 	assert(buf2_released == 1);
 	/* 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);
 	wl_surface_commit(surface);
 	frame_callback_wait(client, &frame);
diff --git a/tests/internal-screenshot-test.c b/tests/internal-screenshot-test.c
index 563aa3d..77c23ea 100644
--- a/tests/internal-screenshot-test.c
+++ b/tests/internal-screenshot-test.c
@@ -97,7 +97,7 @@ TEST(internal_screenshot)
 	match = check_surfaces_equal(screenshot, reference_bad);
 	printf("Screenshot %s reference image\n", match? "equal to" : "different from");
 	assert(!match);
-	free(reference_bad->data);
+	buffer_destroy(reference_bad->buffer);
 	free(reference_bad);
 
 	/* Test check_surfaces_match_in_clip()
@@ -112,7 +112,7 @@ TEST(internal_screenshot)
 	match = check_surfaces_match_in_clip(screenshot, reference_good,
 					     &clip);
 	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);
 
 	/* Test dumping of non-matching images */
diff --git a/tests/presentation-test.c b/tests/presentation-test.c
index f8c3be5..cb5ff19 100644
--- a/tests/presentation-test.c
+++ b/tests/presentation-test.c
@@ -226,7 +226,7 @@ TEST(test_presentation_feedback_simple)
 	assert(client);
 
 	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);
 	wl_surface_damage(client->surface->wl_surface, 0, 0, 100, 100);
 	wl_surface_commit(client->surface->wl_surface);
diff --git a/tests/viewporter-test.c b/tests/viewporter-test.c
index b24276f..95fb9b4 100644
--- a/tests/viewporter-test.c
+++ b/tests/viewporter-test.c
@@ -301,7 +301,7 @@ setup_source_vs_buffer(struct client *client,
 
 	wl_surface_set_buffer_scale(surf, args->buffer_scale);
 	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_destination(vp, 99, 99);
 	wl_surface_commit(surf);
@@ -496,7 +496,7 @@ TEST(test_viewporter_outside_null_buffer)
 	client_roundtrip(client);
 
 	/* 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);
 	expect_protocol_error(client, &wp_viewport_interface,
 			      WP_VIEWPORT_ERROR_OUT_OF_BUFFER);
diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c
index cc2544e..4f610f7 100644
--- a/tests/weston-test-client-helper.c
+++ b/tests/weston-test-client-helper.c
@@ -103,7 +103,7 @@ move_client(struct client *client, int x, int y)
 	/* The attach here is necessary because commit() will call configure
 	 * only on surfaces newly attached, and the one that sets the surface
 	 * 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,
 			  surface->height);
 
@@ -447,6 +447,41 @@ create_shm_buffer(struct client *client, int width, int height, void **pixels)
 	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
 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 surface *surface;
+	pixman_color_t color = { 16384, 16384, 16384, 16384 }; /* uint16_t */
+	pixman_image_t *solid;
 
 	client = create_client();
 
@@ -862,10 +899,18 @@ create_client_and_test_surface(int x, int y, int width, int height)
 
 	surface->width = width;
 	surface->height = height;
-	surface->wl_buffer = create_shm_buffer(client, width, height,
-					       &surface->data);
-
-	memset(surface->data, 64, width * height * 4);
+	surface->buffer = create_shm_buffer_a8r8g8b8(client, width, height);
+
+	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);
 
@@ -927,7 +972,7 @@ check_surfaces_geometry(const struct surface *a, const struct surface *b)
 		printf("Undefined surfaces\n");
 		return false;
 	}
-	else if (a->data == NULL || b->data == NULL) {
+	else if (a->buffer == NULL || b->buffer == NULL) {
 		printf("Undefined data\n");
 		return false;
 	}
@@ -950,11 +995,16 @@ bool
 check_surfaces_equal(const struct surface *a, const struct surface *b)
 {
 	int bpp = 4;  /* Assumes ARGB */
+	void *data_a;
+	void *data_b;
 
 	if (!check_surfaces_geometry(a, b))
 		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;
 	void *p, *q;
 	int bpp = 4;  /* Assumes ARGB */
+	void *data_a;
+	void *data_b;
 
 	if (!check_surfaces_geometry(a, b) || clip_rect == NULL)
 		return false;
@@ -991,10 +1043,13 @@ check_surfaces_match_in_clip(const struct surface *a, const struct surface *b, c
 		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");
 	for (i=y0; i<y1; i++) {
-		p = a->data + i * a->width * bpp + x0 * bpp;
-		q = b->data + i * b->width * bpp + x0 * bpp;
+		p = data_a + i * a->width * bpp + x0 * bpp;
+		q = data_b + i * b->width * bpp + x0 * bpp;
 		if (memcmp(p, q, (x1-x0)*bpp) != 0) {
 			/* Dump the bad row */
 			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;
 	int bpp = 4; /* Assume ARGB */
 	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,
 							    weston_surface->width,
 							    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
 	   the cairo surface so we can destroy it */
-	reference->data = zalloc(source_data_size);
-	if (reference->data == NULL) {
-		perror("zalloc reference data");
-		cairo_surface_destroy(reference_cairo_surface);
-		free(reference);
-		return NULL;
-	}
-	memcpy(reference->data,
+
+	reference->buffer = xzalloc(sizeof *reference->buffer);
+	reference->buffer->image = pixman_image_create_bits(PIXMAN_a8r8g8b8,
+							    reference->width,
+							    reference->height,
+							    NULL, 0);
+	assert(reference->buffer->image);
+
+	memcpy(pixman_image_get_data(reference->buffer->image),
 	       cairo_image_surface_get_data(reference_cairo_surface),
 	       source_data_size);
 
@@ -1123,10 +1181,10 @@ create_screenshot_surface(struct client *client)
 	screenshot = zalloc(sizeof *screenshot);
 	if (screenshot == NULL)
 		return NULL;
-	screenshot->wl_buffer = create_shm_buffer(client,
-						  client->output->width,
-						  client->output->height,
-						  &screenshot->data);
+
+	screenshot->buffer = create_shm_buffer_a8r8g8b8(client,
+							client->output->width,
+							client->output->height);
 	screenshot->height = client->output->height;
 	screenshot->width = client->output->width;
 
@@ -1153,7 +1211,7 @@ capture_screenshot_of_output(struct client *client)
 	client->test->buffer_copy_done = 0;
 	weston_test_capture_screenshot(client->test->weston_test,
 				       client->output->wl_output,
-				       screenshot->wl_buffer);
+				       screenshot->buffer->proxy);
 	while (client->test->buffer_copy_done == 0)
 		if (wl_display_dispatch(client->wl_display) < 0)
 			break;
diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h
index 51ec881..6f2d05b 100644
--- a/tests/weston-test-client-helper.h
+++ b/tests/weston-test-client-helper.h
@@ -30,6 +30,7 @@
 
 #include <assert.h>
 #include <stdbool.h>
+#include <pixman.h>
 
 #include <wayland-client-protocol.h>
 #include "weston-test-runner.h"
@@ -127,15 +128,20 @@ struct output {
 	int initialized;
 };
 
+struct buffer {
+	struct wl_buffer *proxy;
+	size_t len;
+	pixman_image_t *image;
+};
+
 struct surface {
 	struct wl_surface *wl_surface;
-	struct wl_buffer *wl_buffer;
 	struct output *output;
 	int x;
 	int y;
 	int width;
 	int height;
-	void *data;
+	struct buffer *buffer;
 };
 
 struct rectangle {
@@ -154,6 +160,12 @@ create_client_and_test_surface(int x, int y, int width, int height);
 struct wl_buffer *
 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
 surface_contains(struct surface *surface, int x, int y);
 
-- 
2.7.3



More information about the wayland-devel mailing list