clients: Use new shm interface

This commit is contained in:
Kristian Høgsberg
2012-04-03 11:21:27 -04:00
parent 919fbf0fee
commit 16626282fd
4 changed files with 58 additions and 37 deletions
+5 -3
View File
@@ -89,6 +89,7 @@ static struct wl_buffer *
create_shm_buffer(int width, int height, void **data_out) create_shm_buffer(int width, int height, void **data_out)
{ {
char filename[] = "/tmp/wayland-shm-XXXXXX"; char filename[] = "/tmp/wayland-shm-XXXXXX";
struct wl_shm_pool *pool;
struct wl_buffer *buffer; struct wl_buffer *buffer;
int fd, size, stride; int fd, size, stride;
void *data; void *data;
@@ -115,10 +116,11 @@ create_shm_buffer(int width, int height, void **data_out)
return NULL; return NULL;
} }
buffer = wl_shm_create_buffer(shm, fd, width, height, stride, pool = wl_shm_create_pool(shm, fd, size);
WL_SHM_FORMAT_XRGB8888);
close(fd); close(fd);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
WL_SHM_FORMAT_XRGB8888);
wl_shm_pool_destroy(pool);
*data_out = data; *data_out = data;
+5 -3
View File
@@ -57,6 +57,7 @@ create_shm_buffer(struct display *display,
int width, int height, uint32_t format, void **data_out) int width, int height, uint32_t format, void **data_out)
{ {
char filename[] = "/tmp/wayland-shm-XXXXXX"; char filename[] = "/tmp/wayland-shm-XXXXXX";
struct wl_shm_pool *pool;
struct wl_buffer *buffer; struct wl_buffer *buffer;
int fd, size, stride; int fd, size, stride;
void *data; void *data;
@@ -83,9 +84,10 @@ create_shm_buffer(struct display *display,
return NULL; return NULL;
} }
buffer = wl_shm_create_buffer(display->shm, fd, pool = wl_shm_create_pool(display->shm, fd, size);
width, height, stride, format); buffer = wl_shm_pool_create_buffer(pool, 0,
width, height, stride, format);
wl_shm_pool_destroy(pool);
close(fd); close(fd);
*data_out = data; *data_out = data;
+6 -3
View File
@@ -50,6 +50,7 @@ struct touch {
static void static void
create_shm_buffer(struct touch *touch) create_shm_buffer(struct touch *touch)
{ {
struct wl_shm_pool *pool;
char filename[] = "/tmp/wayland-shm-XXXXXX"; char filename[] = "/tmp/wayland-shm-XXXXXX";
int fd, size, stride; int fd, size, stride;
@@ -76,10 +77,12 @@ create_shm_buffer(struct touch *touch)
exit(1); exit(1);
} }
pool = wl_shm_create_pool(touch->shm, fd, size);
touch->buffer = touch->buffer =
wl_shm_create_buffer(touch->shm, fd, wl_shm_pool_create_buffer(pool, 0,
touch->width, touch->height, stride, touch->width, touch->height, stride,
WL_SHM_FORMAT_ARGB8888); WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool);
close(fd); close(fd);
} }
+42 -28
View File
@@ -461,15 +461,49 @@ shm_surface_data_destroy(void *p)
munmap(data->map, data->length); munmap(data->map, data->length);
} }
static struct wl_shm_pool *
make_shm_pool(struct display *display, int size, void **data)
{
char filename[] = "/tmp/wayland-shm-XXXXXX";
struct wl_shm_pool *pool;
int fd;
fd = mkstemp(filename);
if (fd < 0) {
fprintf(stderr, "open %s failed: %m\n", filename);
return NULL;
}
if (ftruncate(fd, size) < 0) {
fprintf(stderr, "ftruncate failed: %m\n");
close(fd);
return NULL;
}
*data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
unlink(filename);
if (*data == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
return NULL;
}
pool = wl_shm_create_pool(display->shm, fd, size);
close(fd);
return pool;
}
static cairo_surface_t * static cairo_surface_t *
display_create_shm_surface(struct display *display, display_create_shm_surface(struct display *display,
struct rectangle *rectangle, uint32_t flags) struct rectangle *rectangle, uint32_t flags)
{ {
struct shm_surface_data *data; struct shm_surface_data *data;
struct wl_shm_pool *pool;
uint32_t format; uint32_t format;
cairo_surface_t *surface; cairo_surface_t *surface;
int stride, fd; int stride;
char filename[] = "/tmp/wayland-shm-XXXXXX";
data = malloc(sizeof *data); data = malloc(sizeof *data);
if (data == NULL) if (data == NULL)
@@ -478,26 +512,7 @@ display_create_shm_surface(struct display *display,
stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32,
rectangle->width); rectangle->width);
data->length = stride * rectangle->height; data->length = stride * rectangle->height;
fd = mkstemp(filename); pool = make_shm_pool(display, data->length, &data->map);
if (fd < 0) {
fprintf(stderr, "open %s failed: %m\n", filename);
return NULL;
}
if (ftruncate(fd, data->length) < 0) {
fprintf(stderr, "ftruncate failed: %m\n");
close(fd);
return NULL;
}
data->map = mmap(NULL, data->length,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
unlink(filename);
if (data->map == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
return NULL;
}
surface = cairo_image_surface_create_for_data (data->map, surface = cairo_image_surface_create_for_data (data->map,
CAIRO_FORMAT_ARGB32, CAIRO_FORMAT_ARGB32,
@@ -513,13 +528,12 @@ display_create_shm_surface(struct display *display,
else else
format = WL_SHM_FORMAT_ARGB8888; format = WL_SHM_FORMAT_ARGB8888;
data->data.buffer = wl_shm_create_buffer(display->shm, data->data.buffer = wl_shm_pool_create_buffer(pool, 0,
fd, rectangle->width,
rectangle->width, rectangle->height,
rectangle->height, stride, format);
stride, format);
close(fd); wl_shm_pool_destroy(pool);
return surface; return surface;
} }