[PATCH 4/4] window.c: Adapt to new shm pool helper functions

Andre Heider a.heider at gmail.com
Sat Aug 4 06:23:51 PDT 2012


---
 clients/window.c |  115 ++++++++----------------------------------------------
 1 file changed, 16 insertions(+), 99 deletions(-)

diff --git a/clients/window.c b/clients/window.c
index d0b7a7d..17b373e 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -66,8 +66,6 @@
 
 #include "window.h"
 
-struct shm_pool;
-
 struct display {
 	struct wl_display *display;
 	struct wl_compositor *compositor;
@@ -150,7 +148,7 @@ struct window {
 
 	cairo_surface_t *cairo_surface;
 
-	struct shm_pool *pool;
+	struct wl_shm_pool_helper *pool;
 
 	window_key_handler_t key_handler;
 	window_keyboard_focus_handler_t keyboard_focus_handler;
@@ -295,13 +293,6 @@ struct tooltip {
 	float x, y;
 };
 
-struct shm_pool {
-	struct wl_shm_pool *pool;
-	size_t size;
-	size_t used;
-	void *data;
-};
-
 enum {
 	CURSOR_DEFAULT = 100,
 	CURSOR_UNSET
@@ -407,99 +398,21 @@ display_get_buffer_for_surface(struct display *display,
 
 struct shm_surface_data {
 	struct surface_data data;
-	struct shm_pool *pool;
+	struct wl_shm_pool_helper *pool;
 };
 
 static void
-shm_pool_destroy(struct shm_pool *pool);
-
-static void
 shm_surface_data_destroy(void *p)
 {
 	struct shm_surface_data *data = p;
 
 	wl_buffer_destroy(data->data.buffer);
 	if (data->pool)
-		shm_pool_destroy(data->pool);
+		wl_shm_pool_helper_destroy(data->pool, 1);
 
 	free(data);
 }
 
-static struct wl_shm_pool *
-make_shm_pool(struct display *display, int size, void **data)
-{
-	struct wl_shm_pool *pool;
-	int fd;
-
-	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);
-
-	close(fd);
-
-	return pool;
-}
-
-static struct shm_pool *
-shm_pool_create(struct display *display, size_t size)
-{
-	struct shm_pool *pool = malloc(sizeof *pool);
-
-	if (!pool)
-		return NULL;
-
-	pool->pool = make_shm_pool(display, size, &pool->data);
-	if (!pool->pool) {
-		free(pool);
-		return NULL;
-	}
-
-	pool->size = size;
-	pool->used = 0;
-
-	return pool;
-}
-
-static void *
-shm_pool_allocate(struct shm_pool *pool, size_t size, int *offset)
-{
-	if (pool->used + size > pool->size)
-		return NULL;
-
-	*offset = pool->used;
-	pool->used += size;
-
-	return (char *) pool->data + *offset;
-}
-
-/* destroy the pool. this does not unmap the memory though */
-static void
-shm_pool_destroy(struct shm_pool *pool)
-{
-	munmap(pool->data, pool->size);
-	wl_shm_pool_destroy(pool->pool);
-	free(pool);
-}
-
-/* Start allocating from the beginning of the pool again */
-static void
-shm_pool_reset(struct shm_pool *pool)
-{
-	pool->used = 0;
-}
-
 static int
 data_length_for_shm_surface(struct rectangle *rect)
 {
@@ -513,13 +426,15 @@ data_length_for_shm_surface(struct rectangle *rect)
 static cairo_surface_t *
 display_create_shm_surface_from_pool(struct display *display,
 				     struct rectangle *rectangle,
-				     uint32_t flags, struct shm_pool *pool)
+				     uint32_t flags,
+				     struct wl_shm_pool_helper *pool_helper)
 {
 	struct shm_surface_data *data;
 	uint32_t format;
 	cairo_surface_t *surface;
 	int stride, length, offset;
 	void *map;
+	struct wl_shm_pool *pool;
 
 	data = malloc(sizeof *data);
 	if (data == NULL)
@@ -529,7 +444,7 @@ display_create_shm_surface_from_pool(struct display *display,
 						rectangle->width);
 	length = stride * rectangle->height;
 	data->pool = NULL;
-	map = shm_pool_allocate(pool, length, &offset);
+	map = wl_shm_pool_helper_allocate(pool_helper, length, &offset);
 
 	if (!map) {
 		free(data);
@@ -550,7 +465,8 @@ display_create_shm_surface_from_pool(struct display *display,
 	else
 		format = WL_SHM_FORMAT_ARGB8888;
 
-	data->data.buffer = wl_shm_pool_create_buffer(pool->pool, offset,
+	pool = wl_shm_pool_helper_get_pool(pool_helper);
+	data->data.buffer = wl_shm_pool_create_buffer(pool, offset,
 						      rectangle->width,
 						      rectangle->height,
 						      stride, format);
@@ -564,11 +480,11 @@ display_create_shm_surface(struct display *display,
 			   struct window *window)
 {
 	struct shm_surface_data *data;
-	struct shm_pool *pool;
+	struct wl_shm_pool_helper *pool;
 	cairo_surface_t *surface;
 
 	if (window && window->pool) {
-		shm_pool_reset(window->pool);
+		wl_shm_pool_helper_reset(window->pool);
 		surface = display_create_shm_surface_from_pool(display,
 							       rectangle,
 							       flags,
@@ -577,7 +493,7 @@ display_create_shm_surface(struct display *display,
 			return surface;
 	}
 
-	pool = shm_pool_create(display,
+	pool = wl_shm_pool_helper_create(display->shm,
 			       data_length_for_shm_surface(rectangle));
 	if (!pool)
 		return NULL;
@@ -587,7 +503,7 @@ display_create_shm_surface(struct display *display,
 						     flags, pool);
 
 	if (!surface) {
-		shm_pool_destroy(pool);
+		wl_shm_pool_helper_destroy(pool, 1);
 		return NULL;
 	}
 
@@ -1642,7 +1558,8 @@ frame_button_handler(struct widget *widget,
 				   we resize.  We should probably base
 				   this number on the size of the output. */
 				window->pool =
-					shm_pool_create(display, 6 * 1024 * 1024);
+					wl_shm_pool_helper_create(display->shm,
+								  6 * 1024 * 1024);
 			}
 
 			wl_shell_surface_resize(window->shell_surface,
@@ -1990,7 +1907,7 @@ pointer_handle_enter(void *data, struct wl_pointer *pointer,
 	window = input->pointer_focus;
 
 	if (window->pool) {
-		shm_pool_destroy(window->pool);
+		wl_shm_pool_helper_destroy(window->pool, 1);
 		window->pool = NULL;
 		/* Schedule a redraw to free the pool */
 		window_schedule_redraw(window);
-- 
1.7.10.4



More information about the wayland-devel mailing list