--- cursor/wayland-cursor.c | 115 ++++++++++------------------------------------- 1 file changed, 24 insertions(+), 91 deletions(-)
diff --git a/cursor/wayland-cursor.c b/cursor/wayland-cursor.c index 10e8f62..8e467f6 100644 --- a/cursor/wayland-cursor.c +++ b/cursor/wayland-cursor.c @@ -31,93 +31,12 @@ #include "../shared/os-compatibility.h" -struct shm_pool { - struct wl_shm_pool *pool; - int fd; - unsigned int size; - unsigned int used; - char *data; -}; - -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 int -shm_pool_resize(struct shm_pool *pool, int size) -{ - if (ftruncate(pool->fd, size) < 0) - return 0; - - wl_shm_pool_resize(pool->pool, size); - - munmap(pool->data, pool->size); - - pool->data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, - pool->fd, 0); - pool->size = size; - - return 1; -} - -static int -shm_pool_allocate(struct shm_pool *pool, int size) -{ - int offset; - - if (pool->used + size > pool->size) - if (!shm_pool_resize(pool, 2 * pool->size + size)) - return -1; - - offset = pool->used; - pool->used += size; - - return offset; -} - -static void -shm_pool_destroy(struct shm_pool *pool) -{ - munmap(pool->data, pool->size); - wl_shm_pool_destroy(pool->pool); - close(pool->fd); - free(pool); -} - - struct wl_cursor_theme { unsigned int cursor_count; struct wl_cursor **cursors; struct wl_shm *shm; - struct shm_pool *pool; + struct wl_shm_pool_helper *pool; + uint32_t pool_size; char *name; int size; }; @@ -145,10 +64,12 @@ wl_cursor_image_get_buffer(struct wl_cursor_image *_img) { struct cursor_image *image = (struct cursor_image *) _img; struct wl_cursor_theme *theme = image->theme; + struct wl_shm_pool *pool; if (!image->buffer) { + pool = wl_shm_pool_helper_get_pool(theme->pool); image->buffer = - wl_shm_pool_create_buffer(theme->pool->pool, + wl_shm_pool_create_buffer(pool, image->offset, _img->width, _img->height, _img->width * 4, @@ -187,7 +108,8 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images, { struct cursor *cursor; struct cursor_image *image; - int i, size; + int i, size, offset, resize; + void *p; cursor = malloc(sizeof *cursor); if (!cursor) @@ -220,9 +142,21 @@ wl_cursor_create_from_xcursor_images(XcursorImages *images, /* copy pixels to shm pool */ size = image->image.width * image->image.height * 4; - image->offset = shm_pool_allocate(theme->pool, size); - memcpy(theme->pool->data + image->offset, - images->images[i]->pixels, size); + p = wl_shm_pool_helper_allocate(theme->pool, size, &offset); + if (!p) { + resize = 2 * wl_shm_pool_helper_get_size(theme->pool) + + size; + if (!wl_shm_pool_helper_resize(theme->pool, resize)) { + free(cursor); + return NULL; + } + + p = wl_shm_pool_helper_allocate(theme->pool, size, + &offset); + } + + image->offset = offset; + memcpy(p, images->images[i]->pixels, size); } return &cursor->cursor; @@ -280,8 +214,7 @@ wl_cursor_theme_load(const char *name, int size, struct wl_shm *shm) theme->cursor_count = 0; theme->cursors = NULL; - theme->pool = - shm_pool_create(shm, size * size * 4); + theme->pool = wl_shm_pool_helper_create(shm, size * size * 4); if (!theme->pool) { free(theme->name); free(theme); @@ -305,7 +238,7 @@ wl_cursor_theme_destroy(struct wl_cursor_theme *theme) for (i = 0; i < theme->cursor_count; i++) wl_cursor_destroy(theme->cursors[i]); - shm_pool_destroy(theme->pool); + wl_shm_pool_helper_destroy(theme->pool, 1); free(theme->cursors); free(theme); -- 1.7.10.4 _______________________________________________ wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel