tests/wayland: Draw surface with shared helper

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2416>
This commit is contained in:
Jonas Ådahl 2022-05-12 00:11:14 +02:00 committed by Marge Bot
parent 9b97c9b4d4
commit f64259ceae
9 changed files with 116 additions and 691 deletions

View File

@ -18,8 +18,6 @@
#include "config.h"
#include <glib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include "wayland-test-client-utils.h"
@ -39,105 +37,10 @@ init_surface (void)
wl_surface_commit (surface);
}
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
static void
draw (struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}
static void
draw_main (void)
{
draw (surface, 700, 500, 0xff00ff00);
draw_surface (display, surface, 700, 500, 0xff00ff00);
}
static void

View File

@ -19,8 +19,6 @@
#include <glib.h>
#include <linux/input-event-codes.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include "wayland-test-client-utils.h"
@ -41,117 +39,22 @@ static struct xdg_popup *xdg_popup;
static struct wl_surface *subsurface_surface;
static struct wl_subsurface *subsurface;
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
static void
draw (struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}
static void
draw_main (void)
{
draw (toplevel_surface, 200, 200, 0xff00ffff);
draw_surface (display, toplevel_surface, 200, 200, 0xff00ffff);
}
static void
draw_popup (void)
{
draw (popup_surface, 100, 100, 0xff005500);
draw_surface (display, popup_surface, 100, 100, 0xff005500);
}
static void
draw_subsurface (void)
{
draw (subsurface_surface, 100, 50, 0xff001f00);
draw_surface (display, subsurface_surface, 100, 50, 0xff001f00);
}
static void

View File

@ -18,8 +18,6 @@
#include "config.h"
#include <glib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include "wayland-test-client-utils.h"
@ -87,111 +85,16 @@ reset_surface (void)
state = STATE_WAIT_FOR_ACTOR_DESTROYED;
}
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
static void
draw (struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}
static void
draw_main (void)
{
draw (surface, 700, 500, 0xff00ff00);
draw_surface (display, surface, 700, 500, 0xff00ff00);
}
static void
draw_subsurface (void)
{
draw (subsurface_surface, 500, 300, 0xff007f00);
draw_surface (display, subsurface_surface, 500, 300, 0xff007f00);
}
static void

View File

@ -19,8 +19,6 @@
#include "config.h"
#include <glib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include "wayland-test-client-utils.h"
@ -51,111 +49,16 @@ static State state;
static void init_surfaces (void);
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
static void
draw (struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}
static void
draw_main (void)
{
draw (surface, 700, 500, 0xff00ff00);
draw_surface (display, surface, 700, 500, 0xff00ff00);
}
static void
draw_subsurface (void)
{
draw (subsurface_surface, 500, 300, 0xff007f00);
draw_surface (display, subsurface_surface, 500, 300, 0xff007f00);
}
static void

View File

@ -27,6 +27,7 @@
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
@ -249,3 +250,100 @@ static void
wayland_display_init (WaylandDisplay *display)
{
}
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (WaylandDisplay *display,
int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
void
draw_surface (WaylandDisplay *display,
struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (display, width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}

View File

@ -38,4 +38,10 @@ int create_anonymous_file (off_t size);
WaylandDisplay * wayland_display_new (WaylandDisplayCapabilities capabilities);
void draw_surface (WaylandDisplay *display,
struct wl_surface *surface,
int width,
int height,
uint32_t color);
#endif /* WAYLAND_TEST_CLIENT_UTILS_H */

View File

@ -18,8 +18,6 @@
#include "config.h"
#include <glib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include "wayland-test-client-utils.h"
@ -44,105 +42,10 @@ init_surface (const char *token)
wl_surface_commit (surface);
}
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
static void
draw (struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}
static void
draw_main (void)
{
draw (surface, 700, 500, 0xff00ff00);
draw_surface (display, surface, 700, 500, 0xff00ff00);
}
static void

View File

@ -18,8 +18,6 @@
#include "config.h"
#include <glib.h>
#include <sys/mman.h>
#include <unistd.h>
#include "wayland-test-client-utils.h"
@ -90,111 +88,16 @@ reset_surface (void)
state = STATE_WAIT_FOR_ACTOR_DESTROYED;
}
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
static void
draw (struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}
static void
draw_main (void)
{
draw (surface, 700, 500, 0xff00ff00);
draw_surface (display, surface, 700, 500, 0xff00ff00);
}
static void
draw_subsurface (void)
{
draw (subsurface_surface, 500, 300, 0xff007f00);
draw_surface (display, subsurface_surface, 500, 300, 0xff007f00);
}
static void

View File

@ -18,8 +18,6 @@
#include "config.h"
#include <glib.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include "wayland-test-client-utils.h"
@ -52,106 +50,11 @@ init_surface (void)
wl_surface_commit (surface);
}
static void
handle_buffer_release (void *data,
struct wl_buffer *buffer)
{
wl_buffer_destroy (buffer);
}
static const struct wl_buffer_listener buffer_listener = {
handle_buffer_release
};
static gboolean
create_shm_buffer (int width,
int height,
struct wl_buffer **out_buffer,
void **out_data,
int *out_size)
{
struct wl_shm_pool *pool;
static struct wl_buffer *buffer;
int fd, size, stride;
int bytes_per_pixel;
void *data;
bytes_per_pixel = 4;
stride = width * bytes_per_pixel;
size = stride * height;
fd = create_anonymous_file (size);
if (fd < 0)
{
fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
size);
return FALSE;
}
data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED)
{
fprintf (stderr, "mmap failed: %m\n");
close (fd);
return FALSE;
}
pool = wl_shm_create_pool (display->shm, fd, size);
buffer = wl_shm_pool_create_buffer (pool, 0,
width, height,
stride,
WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener (buffer, &buffer_listener, buffer);
wl_shm_pool_destroy (pool);
close (fd);
*out_buffer = buffer;
*out_data = data;
*out_size = size;
return TRUE;
}
static void
fill (void *buffer_data,
int width,
int height,
uint32_t color)
{
uint32_t *pixels = buffer_data;
int x, y;
for (y = 0; y < height; y++)
{
for (x = 0; x < width; x++)
pixels[y * width + x] = color;
}
}
static void
draw (struct wl_surface *surface,
int width,
int height,
uint32_t color)
{
struct wl_buffer *buffer;
void *buffer_data;
int size;
if (!create_shm_buffer (width, height,
&buffer, &buffer_data, &size))
g_error ("Failed to create shm buffer");
fill (buffer_data, width, height, color);
wl_surface_attach (surface, buffer, 0, 0);
}
static void
draw_main (int width,
int height)
{
draw (surface, width, height, 0xff00ff00);
draw_surface (display, surface, width, height, 0xff00ff00);
}
static void