commit 1256add

Michael Forney  ·  2019-08-30 01:45:21 +0000 UTC
parent bd1a974
shm: Move away from global state
4 files changed,  +42, -43
+1, -1
1@@ -43,7 +43,7 @@ struct swc {
2 	const struct swc_bindings *const bindings;
3 	struct wl_list screens;
4 	struct swc_compositor *const compositor;
5-	struct swc_shm *const shm;
6+	struct swc_shm *shm;
7 	struct swc_drm *const drm;
8 	struct wl_global *data_device_manager;
9 };
+32, -33
  1@@ -1,6 +1,6 @@
  2 /* swc: libswc/shm.c
  3  *
  4- * Copyright (c) 2013, 2014 Michael Forney
  5+ * Copyright (c) 2013-2019 Michael Forney
  6  *
  7  * Based in part upon wayland-shm.c from wayland, which is:
  8  *
  9@@ -38,14 +38,9 @@
 10 #include <wld/pixman.h>
 11 #include <wld/wld.h>
 12 
 13-struct swc_shm swc_shm;
 14-
 15-static struct {
 16-	struct wl_global *global;
 17-} shm;
 18-
 19 struct pool {
 20 	struct wl_resource *resource;
 21+	struct swc_shm *shm;
 22 	void *data;
 23 	uint32_t size;
 24 	unsigned references;
 25@@ -109,7 +104,7 @@ create_buffer(struct wl_client *client, struct wl_resource *resource,
 26 	}
 27 
 28 	object.ptr = (void *)((uintptr_t)pool->data + offset);
 29-	buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, width, height, format_shm_to_wld(format), stride);
 30+	buffer = wld_import_buffer(pool->shm->context, WLD_OBJECT_DATA, object, width, height, format_shm_to_wld(format), stride);
 31 
 32 	if (!buffer)
 33 		goto error0;
 34@@ -150,12 +145,10 @@ resize(struct wl_client *client, struct wl_resource *resource, int32_t size)
 35 	void *data;
 36 
 37 	data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
 38-
 39 	if (data == MAP_FAILED) {
 40 		wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "mremap failed: %s", strerror(errno));
 41 		return;
 42 	}
 43-
 44 	pool->data = data;
 45 	pool->size = size;
 46 }
 47@@ -169,27 +162,26 @@ static struct wl_shm_pool_interface shm_pool_implementation = {
 48 static void
 49 create_pool(struct wl_client *client, struct wl_resource *resource, uint32_t id, int32_t fd, int32_t size)
 50 {
 51+	struct swc_shm *shm = wl_resource_get_user_data(resource);
 52 	struct pool *pool;
 53 
 54-	if (!(pool = malloc(sizeof(*pool)))) {
 55+	pool = malloc(sizeof(*pool));
 56+	if (!pool) {
 57 		wl_resource_post_no_memory(resource);
 58 		goto error0;
 59 	}
 60-
 61+	pool->shm = shm;
 62 	pool->resource = wl_resource_create(client, &wl_shm_pool_interface, wl_resource_get_version(resource), id);
 63-
 64 	if (!pool->resource) {
 65 		wl_resource_post_no_memory(resource);
 66 		goto error1;
 67 	}
 68-
 69 	wl_resource_set_implementation(pool->resource, &shm_pool_implementation, pool, &destroy_pool_resource);
 70 	pool->data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
 71 	if (pool->data == MAP_FAILED) {
 72 		wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "mmap failed: %s", strerror(errno));
 73 		goto error2;
 74 	}
 75-
 76 	close(fd);
 77 	pool->size = size;
 78 	pool->references = 1;
 79@@ -210,46 +202,53 @@ static struct wl_shm_interface shm_implementation = {
 80 static void
 81 bind_shm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
 82 {
 83+	struct swc_shm *shm = data;
 84 	struct wl_resource *resource;
 85 
 86 	if (version > 1)
 87 		version = 1;
 88 
 89 	resource = wl_resource_create(client, &wl_shm_interface, version, id);
 90-	wl_resource_set_implementation(resource, &shm_implementation, NULL, NULL);
 91+	wl_resource_set_implementation(resource, &shm_implementation, shm, NULL);
 92 
 93 	wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
 94 	wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
 95 }
 96 
 97-bool
 98-shm_initialize(void)
 99+struct swc_shm *
100+shm_create(struct wl_display *display)
101 {
102-	if (!(swc.shm->context = wld_pixman_create_context()))
103-		goto error0;
104+	struct swc_shm *shm;
105 
106-	if (!(swc.shm->renderer = wld_create_renderer(swc.shm->context)))
107+	shm = malloc(sizeof(*shm));
108+	if (!shm)
109+		goto error0;
110+	shm->context = wld_pixman_create_context();
111+	if (!shm->context)
112 		goto error1;
113-
114-	shm.global = wl_global_create(swc.display, &wl_shm_interface, 1, NULL, &bind_shm);
115-
116-	if (!shm.global)
117+	shm->renderer = wld_create_renderer(shm->context);
118+	if (!shm->renderer)
119 		goto error2;
120+	shm->global = wl_global_create(display, &wl_shm_interface, 1, shm, &bind_shm);
121+	if (!shm->global)
122+		goto error3;
123 
124-	return true;
125+	return shm;
126 
127+error3:
128+	wld_destroy_renderer(shm->renderer);
129 error2:
130-	wld_destroy_renderer(swc.shm->renderer);
131+	wld_destroy_context(shm->context);
132 error1:
133-	wld_destroy_context(swc.shm->context);
134+	free(shm);
135 error0:
136-	return false;
137+	return NULL;
138 }
139 
140 void
141-shm_finalize(void)
142+shm_destroy(struct swc_shm *shm)
143 {
144-	wl_global_destroy(shm.global);
145-	wld_destroy_renderer(swc.shm->renderer);
146-	wld_destroy_context(swc.shm->context);
147+	wl_global_destroy(shm->global);
148+	wld_destroy_renderer(shm->renderer);
149+	wld_destroy_context(shm->context);
150 }
+5, -4
 1@@ -1,6 +1,6 @@
 2 /* swc: libswc/shm.h
 3  *
 4- * Copyright (c) 2013 Michael Forney
 5+ * Copyright (c) 2013-2019 Michael Forney
 6  *
 7  * Permission is hereby granted, free of charge, to any person obtaining a copy
 8  * of this software and associated documentation files (the "Software"), to deal
 9@@ -24,14 +24,15 @@
10 #ifndef SWC_SHM_H
11 #define SWC_SHM_H
12 
13-#include <stdbool.h>
14+struct wl_display;
15 
16 struct swc_shm {
17+	struct wl_global *global;
18 	struct wld_context *context;
19 	struct wld_renderer *renderer;
20 };
21 
22-bool shm_initialize(void);
23-void shm_finalize(void);
24+struct swc_shm *shm_create(struct wl_display *display);
25+void shm_destroy(struct swc_shm *shm);
26 
27 #endif
+4, -5
 1@@ -46,7 +46,6 @@ extern const struct swc_seat swc_seat;
 2 extern const struct swc_bindings swc_bindings;
 3 extern struct swc_compositor swc_compositor;
 4 extern struct swc_drm swc_drm;
 5-extern struct swc_shm swc_shm;
 6 
 7 extern struct pointer_handler screens_pointer_handler;
 8 
 9@@ -55,7 +54,6 @@ struct swc swc = {
10 	.bindings = &swc_bindings,
11 	.compositor = &swc_compositor,
12 	.drm = &swc_drm,
13-	.shm = &swc_shm,
14 };
15 
16 static void
17@@ -120,7 +118,8 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, con
18 		goto error1;
19 	}
20 
21-	if (!shm_initialize()) {
22+	swc.shm = shm_create(display);
23+	if (!swc.shm) {
24 		ERROR("Could not initialize SHM\n");
25 		goto error2;
26 	}
27@@ -192,7 +191,7 @@ error5:
28 error4:
29 	bindings_finalize();
30 error3:
31-	shm_finalize();
32+	shm_destroy(swc.shm);
33 error2:
34 	drm_finalize();
35 error1:
36@@ -211,7 +210,7 @@ swc_finalize(void)
37 	compositor_finalize();
38 	screens_finalize();
39 	bindings_finalize();
40-	shm_finalize();
41+	shm_destroy(swc.shm);
42 	drm_finalize();
43 	launch_finalize();
44 }