wayland/src/wayland-shm.c

291 lines
7 KiB
C
Raw Normal View History

2011-03-08 11:32:24 +01:00
/*
* Copyright © 2008 Kristian Høgsberg
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*
* Authors:
* Kristian Høgsberg <krh@bitplanet.net>
* Benjamin Franzke <benjaminfranzke@googlemail.com>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include "wayland-server.h"
struct wl_shm_pool {
struct wl_resource resource;
int refcount;
char *data;
int size;
2012-05-22 15:39:40 +03:00
int fd;
};
2011-03-08 11:32:24 +01:00
struct wl_shm_buffer {
struct wl_buffer buffer;
int32_t stride;
uint32_t format;
2012-05-22 15:39:40 +03:00
int offset;
struct wl_shm_pool *pool;
2011-03-08 11:32:24 +01:00
};
static void
shm_pool_unref(struct wl_shm_pool *pool)
{
pool->refcount--;
if (pool->refcount)
return;
munmap(pool->data, pool->size);
2012-05-22 15:39:40 +03:00
close(pool->fd);
free(pool);
}
2011-03-08 11:32:24 +01:00
static void
destroy_buffer(struct wl_resource *resource)
2011-03-08 11:32:24 +01:00
{
struct wl_shm_buffer *buffer =
container_of(resource, struct wl_shm_buffer, buffer.resource);
if (buffer->pool)
shm_pool_unref(buffer->pool);
2011-03-08 11:32:24 +01:00
free(buffer);
}
static void
shm_buffer_destroy(struct wl_client *client, struct wl_resource *resource)
2011-03-08 11:32:24 +01:00
{
wl_resource_destroy(resource);
2011-03-08 11:32:24 +01:00
}
static const struct wl_buffer_interface shm_buffer_interface = {
2011-03-08 11:32:24 +01:00
shm_buffer_destroy
};
static void
shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
uint32_t id, int32_t offset,
int32_t width, int32_t height,
int32_t stride, uint32_t format)
2011-03-08 11:32:24 +01:00
{
struct wl_shm_pool *pool = resource->data;
2011-03-08 11:32:24 +01:00
struct wl_shm_buffer *buffer;
switch (format) {
case WL_SHM_FORMAT_ARGB8888:
case WL_SHM_FORMAT_XRGB8888:
break;
default:
wl_resource_post_error(resource,
WL_SHM_ERROR_INVALID_FORMAT,
"invalid format");
return;
}
if (offset < 0 || width <= 0 || height <= 0 || stride < width ||
INT32_MAX / stride <= height ||
offset > pool->size - stride * height) {
wl_resource_post_error(resource,
WL_SHM_ERROR_INVALID_STRIDE,
"invalid width, height or stride (%dx%d, %u)",
width, height, stride);
return;
}
buffer = malloc(sizeof *buffer);
if (buffer == NULL) {
wl_resource_post_no_memory(resource);
return;
}
2011-03-08 11:32:24 +01:00
buffer->buffer.width = width;
buffer->buffer.height = height;
shm: Zero busy count for allocated buffers This avoids a valgrind error like: ==31496== Conditional jump or move depends on uninitialised value(s) ==31496== at 0x407620: weston_buffer_post_release (compositor.c:928) ==31496== by 0x406AEB: weston_surface_attach (compositor.c:725) ==31496== by 0x409EB8: pointer_attach (compositor.c:2009) ==31496== by 0x34ECE05D63: ffi_call_unix64 (unix64.S:75) ==31496== by 0x34ECE05784: ffi_call (ffi64.c:486) ==31496== by 0x5674C4D: wl_closure_invoke (connection.c:770) ==31496== by 0x566ECCB: wl_client_connection_data (wayland-server.c:255) ==31496== by 0x56722F9: wl_event_source_fd_dispatch (event-loop.c:79) ==31496== by 0x5672C99: wl_event_loop_dispatch (event-loop.c:410) ==31496== by 0x56705FF: wl_display_run (wayland-server.c:1004) ==31496== by 0x40C775: main (compositor.c:2937) ==31496== Uninitialised value was created by a heap allocation ==31496== at 0x4A074CD: malloc (vg_replace_malloc.c:236) ==31496== by 0x5670EA7: shm_pool_create_buffer (wayland-shm.c:113) ==31496== by 0x34ECE05D63: ffi_call_unix64 (unix64.S:75) ==31496== by 0x34ECE05784: ffi_call (ffi64.c:486) ==31496== by 0x5674C4D: wl_closure_invoke (connection.c:770) ==31496== by 0x566ECCB: wl_client_connection_data (wayland-server.c:255) ==31496== by 0x56722F9: wl_event_source_fd_dispatch (event-loop.c:79) ==31496== by 0x5672C99: wl_event_loop_dispatch (event-loop.c:410) ==31496== by 0x56705FF: wl_display_run (wayland-server.c:1004) ==31496== by 0x40C775: main (compositor.c:2937)
2012-05-22 18:48:14 +01:00
buffer->buffer.busy_count = 0;
buffer->format = format;
2011-03-08 11:32:24 +01:00
buffer->stride = stride;
2012-05-22 15:39:40 +03:00
buffer->offset = offset;
buffer->pool = pool;
pool->refcount++;
2011-03-08 11:32:24 +01:00
buffer->buffer.resource.object.id = id;
buffer->buffer.resource.object.interface = &wl_buffer_interface;
buffer->buffer.resource.object.implementation = (void (**)(void))
&shm_buffer_interface;
buffer->buffer.resource.data = buffer;
buffer->buffer.resource.client = resource->client;
2011-03-08 11:32:24 +01:00
buffer->buffer.resource.destroy = destroy_buffer;
wl_client_add_resource(client, &buffer->buffer.resource);
2011-03-08 11:32:24 +01:00
}
static void
destroy_pool(struct wl_resource *resource)
2011-03-08 11:32:24 +01:00
{
struct wl_shm_pool *pool = resource->data;
2011-03-08 11:32:24 +01:00
shm_pool_unref(pool);
}
static void
shm_pool_destroy(struct wl_client *client, struct wl_resource *resource)
{
wl_resource_destroy(resource);
}
2012-05-22 15:39:40 +03:00
static void
shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
int32_t size)
{
struct wl_shm_pool *pool = resource->data;
void *data;
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
pool->fd, 0);
if (data == MAP_FAILED) {
wl_resource_post_error(resource,
WL_SHM_ERROR_INVALID_FD,
"failed mmap fd %d", pool->fd);
return;
}
munmap(pool->data, pool->size);
pool->data = data;
pool->size = size;
}
struct wl_shm_pool_interface shm_pool_interface = {
shm_pool_create_buffer,
2012-05-22 15:39:40 +03:00
shm_pool_destroy,
shm_pool_resize
};
static void
shm_create_pool(struct wl_client *client, struct wl_resource *resource,
uint32_t id, int fd, int32_t size)
{
struct wl_shm_pool *pool;
pool = malloc(sizeof *pool);
if (pool == NULL) {
wl_resource_post_no_memory(resource);
2011-03-08 11:32:24 +01:00
close(fd);
return;
}
if (size <= 0) {
wl_resource_post_error(resource,
WL_SHM_ERROR_INVALID_STRIDE,
"invalid size (%d)", size);
2011-03-08 11:32:24 +01:00
close(fd);
return;
}
pool->refcount = 1;
2012-05-22 15:39:40 +03:00
pool->fd = fd;
pool->size = size;
pool->data = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (pool->data == MAP_FAILED) {
wl_resource_post_error(resource,
WL_SHM_ERROR_INVALID_FD,
"failed mmap fd %d", fd);
2011-03-08 11:32:24 +01:00
return;
}
pool->resource.object.id = id;
pool->resource.object.interface = &wl_shm_pool_interface;
pool->resource.object.implementation =
(void (**)(void)) &shm_pool_interface;
2011-03-08 11:32:24 +01:00
pool->resource.data = pool;
pool->resource.client = client;
pool->resource.destroy = destroy_pool;
wl_client_add_resource(client, &pool->resource);
2011-03-08 11:32:24 +01:00
}
static const struct wl_shm_interface shm_interface = {
shm_create_pool
2011-03-08 11:32:24 +01:00
};
static void
bind_shm(struct wl_client *client,
void *data, uint32_t version, uint32_t id)
{
struct wl_resource *resource;
resource = wl_client_add_object(client, &wl_shm_interface,
&shm_interface, id, data);
wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
}
2011-03-08 11:32:24 +01:00
WL_EXPORT int
wl_display_init_shm(struct wl_display *display)
2011-03-08 11:32:24 +01:00
{
if (!wl_display_add_global(display, &wl_shm_interface, NULL, bind_shm))
return -1;
2011-03-08 11:32:24 +01:00
return 0;
2011-03-08 11:32:24 +01:00
}
WL_EXPORT int
wl_buffer_is_shm(struct wl_buffer *buffer)
{
return buffer->resource.object.implementation ==
(void (**)(void)) &shm_buffer_interface;
}
WL_EXPORT int32_t
wl_shm_buffer_get_stride(struct wl_buffer *buffer_base)
{
struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
if (!wl_buffer_is_shm(buffer_base))
return 0;
return buffer->stride;
}
WL_EXPORT void *
wl_shm_buffer_get_data(struct wl_buffer *buffer_base)
{
struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
if (!wl_buffer_is_shm(buffer_base))
return NULL;
2012-05-22 15:39:40 +03:00
return buffer->pool->data + buffer->offset;
2011-03-08 11:32:24 +01:00
}
WL_EXPORT uint32_t
wl_shm_buffer_get_format(struct wl_buffer *buffer_base)
{
struct wl_shm_buffer *buffer = (struct wl_shm_buffer *) buffer_base;
return buffer->format;
}