struct wl_buffer * create_shm_buffer(struct client *client, int width, int height, void **pixels) { struct wl_shm *shm = client->wl_shm; int stride = width * 4; int size = stride * height; struct wl_shm_pool *pool; struct wl_buffer *buffer; int fd; void *data; fd = os_create_anonymous_file(size); assert(fd >= 0); data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (data == MAP_FAILED) { close(fd); assert(data != MAP_FAILED); } pool = wl_shm_create_pool(shm, fd, size); buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888); wl_shm_pool_destroy(pool); close(fd); if (pixels) *pixels = data; return buffer; }
static struct wl_buffer * create_buffer() { struct wl_shm_pool *pool; int stride = WIDTH * 4; // 4 bytes per pixel int size = stride * HEIGHT; int fd; struct wl_buffer *buff; fd = os_create_anonymous_file(size); if (fd < 0) { fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); exit(1); } shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (shm_data == MAP_FAILED) { fprintf(stderr, "mmap failed: %m\n"); close(fd); exit(1); } pool = wl_shm_create_pool(shm, fd, size); buff = wl_shm_pool_create_buffer(pool, 0, WIDTH, HEIGHT, stride, WL_SHM_FORMAT_XRGB8888); //wl_buffer_add_listener(buffer, &buffer_listener, buffer); wl_shm_pool_destroy(pool); return buff; }
static struct wl_buffer * create_bad_shm_buffer(struct client *client, int width, int height) { struct wl_shm *shm = client->wl_shm; int stride = width * 4; int size = stride * height; struct wl_shm_pool *pool; struct wl_buffer *buffer; int fd; fd = os_create_anonymous_file(size); assert(fd >= 0); pool = wl_shm_create_pool(shm, fd, size); buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888); wl_shm_pool_destroy(pool); /* Truncate the file to a small size, so that the compositor * will access it out-of-bounds, and hit SIGBUS. */ assert(ftruncate(fd, 12) == 0); close(fd); return buffer; }
static void create_shm_buffer(struct touch *touch) { struct wl_shm_pool *pool; int fd, size, stride; stride = touch->width * 4; size = stride * touch->height; fd = os_create_anonymous_file(size); if (fd < 0) { fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); exit(1); } touch->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (touch->data == MAP_FAILED) { fprintf(stderr, "mmap failed: %m\n"); close(fd); exit(1); } pool = wl_shm_create_pool(touch->shm, fd, size); touch->buffer = wl_shm_pool_create_buffer(pool, 0, touch->width, touch->height, stride, WL_SHM_FORMAT_ARGB8888); wl_shm_pool_destroy(pool); close(fd); }
static struct shm_pool * shm_pool_create(struct wl_shm *shm, int size) { struct shm_pool *pool; pool = malloc(sizeof *pool); if (!pool) return NULL; pool->fd = os_create_anonymous_file(size); if (pool->fd < 0) goto err_free; pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, pool->fd, 0); if (pool->data == MAP_FAILED) goto err_close; pool->pool = wl_shm_create_pool(shm, pool->fd, size); pool->size = size; pool->used = 0; return pool; err_close: close(pool->fd); err_free: free(pool); return NULL; }
static void draw_initial_frame(struct wayland_output *output) { struct wayland_compositor *c = (struct wayland_compositor *) output->base.compositor; struct wl_shm *shm = c->parent.shm; struct wl_surface *surface = output->parent.surface; struct wl_shm_pool *pool; struct wl_buffer *buffer; int width, height, stride; int size; int fd; void *data; width = output->mode.width + c->border.left + c->border.right; height = output->mode.height + c->border.top + c->border.bottom; stride = width * 4; size = height * stride; fd = os_create_anonymous_file(size); if (fd < 0) { perror("os_create_anonymous_file"); return; } data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (data == MAP_FAILED) { perror("mmap"); close(fd); return; } pool = wl_shm_create_pool(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); memset(data, 0, size); wl_surface_attach(surface, buffer, 0, 0); /* We only need to damage some part, as its only transparant * pixels anyway. */ wl_surface_damage(surface, 0, 0, 1, 1); }
static int create_shm_buffer(struct display *display, struct buffer *buffer, int width, int height, uint32_t format) { struct wl_shm_pool *pool; int fd, size, pitch; void *data; pitch = width * 4; size = pitch * height; fd = os_create_anonymous_file(size); if (fd < 0) { fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); return -1; } 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 -1; } pool = wl_shm_create_pool(display->shm, fd, size); buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, pitch, format); wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer); wl_shm_pool_destroy(pool); close(fd); buffer->shm_data = data; return 0; }
static struct wl_buffer * create_shm_buffer(struct display *display, int width, int height, uint32_t format, void **data_out) { struct wl_shm_pool *pool; struct wl_buffer *buffer; int fd, size, stride; void *data; stride = width * 4; size = stride * height; fd = os_create_anonymous_file(size); if (fd < 0) { fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); return NULL; } 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 NULL; } pool = wl_shm_create_pool(display->shm, fd, size); buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, format); wl_shm_pool_destroy(pool); close(fd); *data_out = data; return buffer; }
sb->output = NULL; so->shm.width = width; so->shm.height = height; } if (!wl_list_empty(&so->shm.free_buffers)) { sb = container_of(so->shm.free_buffers.next, struct ss_shm_buffer, free_link); wl_list_remove(&sb->free_link); wl_list_init(&sb->free_link); return sb; } fd = os_create_anonymous_file(height * stride); if (fd < 0) { weston_log("os_create_anonymous_file: %m"); return NULL; } data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (data == MAP_FAILED) { weston_log("mmap: %m"); goto out_close; } sb = zalloc(sizeof *sb); if (!sb) goto out_unmap;