tests: let create_shm_buffer() handle any format

Change create_shm_buffer() to handle any pixel format known to Pixman.
Presumably in the future we might want to test e.g. RGB565 content with
screenshot tests.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Reviewed-by: Daniel Stone <daniels@collabora.com>
dev
Pekka Paalanen 9 years ago
parent 11f263307a
commit 2be0c98122
  1. 60
      tests/weston-test-client-helper.c

@ -414,55 +414,63 @@ static const struct wl_surface_listener surface_listener = {
surface_leave surface_leave
}; };
static struct wl_buffer * static struct buffer *
create_shm_buffer(struct client *client, int width, int height, void **pixels) create_shm_buffer(struct client *client, int width, int height,
pixman_format_code_t format, uint32_t wlfmt)
{ {
struct wl_shm *shm = client->wl_shm; struct wl_shm *shm = client->wl_shm;
int stride = width * 4; struct buffer *buf;
int size = stride * height; size_t stride_bytes;
struct wl_shm_pool *pool; struct wl_shm_pool *pool;
struct wl_buffer *buffer;
int fd; int fd;
void *data; void *data;
size_t bytes_pp;
assert(width > 0);
assert(height > 0);
buf = xzalloc(sizeof *buf);
bytes_pp = PIXMAN_FORMAT_BPP(format) / 8;
stride_bytes = width * bytes_pp;
/* round up to multiple of 4 bytes for Pixman */
stride_bytes = (stride_bytes + 3) & ~3u;
assert(stride_bytes / bytes_pp >= (unsigned)width);
fd = os_create_anonymous_file(size); buf->len = stride_bytes * height;
assert(buf->len / stride_bytes == (unsigned)height);
fd = os_create_anonymous_file(buf->len);
assert(fd >= 0); assert(fd >= 0);
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); data = mmap(NULL, buf->len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) { if (data == MAP_FAILED) {
close(fd); close(fd);
assert(data != MAP_FAILED); assert(data != MAP_FAILED);
} }
pool = wl_shm_create_pool(shm, fd, size); pool = wl_shm_create_pool(shm, fd, buf->len);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, buf->proxy = wl_shm_pool_create_buffer(pool, 0, width, height,
WL_SHM_FORMAT_ARGB8888); stride_bytes, wlfmt);
wl_shm_pool_destroy(pool); wl_shm_pool_destroy(pool);
close(fd); close(fd);
if (pixels) buf->image = pixman_image_create_bits(format, width, height,
*pixels = data; data, stride_bytes);
assert(buf->proxy);
assert(buf->image);
return buffer; return buf;
} }
struct buffer * struct buffer *
create_shm_buffer_a8r8g8b8(struct client *client, int width, int height) create_shm_buffer_a8r8g8b8(struct client *client, int width, int height)
{ {
struct buffer *buf; assert(client->has_argb);
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; return create_shm_buffer(client, width, height,
PIXMAN_a8r8g8b8, WL_SHM_FORMAT_ARGB8888);
} }
void void

Loading…
Cancel
Save