commit 1256add04b6f8a719c46ba5fff67a60ddad476da
parent bd1a974fc2801c104a873b5820d80f695d4228b2
Author: Michael Forney <mforney@mforney.org>
Date: Thu, 29 Aug 2019 18:45:21 -0700
shm: Move away from global state
Diffstat:
4 files changed, 42 insertions(+), 43 deletions(-)
diff --git a/libswc/internal.h b/libswc/internal.h
@@ -43,7 +43,7 @@ struct swc {
const struct swc_bindings *const bindings;
struct wl_list screens;
struct swc_compositor *const compositor;
- struct swc_shm *const shm;
+ struct swc_shm *shm;
struct swc_drm *const drm;
struct wl_global *data_device_manager;
};
diff --git a/libswc/shm.c b/libswc/shm.c
@@ -1,6 +1,6 @@
/* swc: libswc/shm.c
*
- * Copyright (c) 2013, 2014 Michael Forney
+ * Copyright (c) 2013-2019 Michael Forney
*
* Based in part upon wayland-shm.c from wayland, which is:
*
@@ -38,14 +38,9 @@
#include <wld/pixman.h>
#include <wld/wld.h>
-struct swc_shm swc_shm;
-
-static struct {
- struct wl_global *global;
-} shm;
-
struct pool {
struct wl_resource *resource;
+ struct swc_shm *shm;
void *data;
uint32_t size;
unsigned references;
@@ -109,7 +104,7 @@ create_buffer(struct wl_client *client, struct wl_resource *resource,
}
object.ptr = (void *)((uintptr_t)pool->data + offset);
- buffer = wld_import_buffer(swc.shm->context, WLD_OBJECT_DATA, object, width, height, format_shm_to_wld(format), stride);
+ buffer = wld_import_buffer(pool->shm->context, WLD_OBJECT_DATA, object, width, height, format_shm_to_wld(format), stride);
if (!buffer)
goto error0;
@@ -150,12 +145,10 @@ resize(struct wl_client *client, struct wl_resource *resource, int32_t size)
void *data;
data = mremap(pool->data, pool->size, size, MREMAP_MAYMOVE);
-
if (data == MAP_FAILED) {
wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "mremap failed: %s", strerror(errno));
return;
}
-
pool->data = data;
pool->size = size;
}
@@ -169,27 +162,26 @@ static struct wl_shm_pool_interface shm_pool_implementation = {
static void
create_pool(struct wl_client *client, struct wl_resource *resource, uint32_t id, int32_t fd, int32_t size)
{
+ struct swc_shm *shm = wl_resource_get_user_data(resource);
struct pool *pool;
- if (!(pool = malloc(sizeof(*pool)))) {
+ pool = malloc(sizeof(*pool));
+ if (!pool) {
wl_resource_post_no_memory(resource);
goto error0;
}
-
+ pool->shm = shm;
pool->resource = wl_resource_create(client, &wl_shm_pool_interface, wl_resource_get_version(resource), id);
-
if (!pool->resource) {
wl_resource_post_no_memory(resource);
goto error1;
}
-
wl_resource_set_implementation(pool->resource, &shm_pool_implementation, pool, &destroy_pool_resource);
pool->data = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
if (pool->data == MAP_FAILED) {
wl_resource_post_error(resource, WL_SHM_ERROR_INVALID_FD, "mmap failed: %s", strerror(errno));
goto error2;
}
-
close(fd);
pool->size = size;
pool->references = 1;
@@ -210,46 +202,53 @@ static struct wl_shm_interface shm_implementation = {
static void
bind_shm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
{
+ struct swc_shm *shm = data;
struct wl_resource *resource;
if (version > 1)
version = 1;
resource = wl_resource_create(client, &wl_shm_interface, version, id);
- wl_resource_set_implementation(resource, &shm_implementation, NULL, NULL);
+ wl_resource_set_implementation(resource, &shm_implementation, shm, NULL);
wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
}
-bool
-shm_initialize(void)
+struct swc_shm *
+shm_create(struct wl_display *display)
{
- if (!(swc.shm->context = wld_pixman_create_context()))
- goto error0;
+ struct swc_shm *shm;
- if (!(swc.shm->renderer = wld_create_renderer(swc.shm->context)))
+ shm = malloc(sizeof(*shm));
+ if (!shm)
+ goto error0;
+ shm->context = wld_pixman_create_context();
+ if (!shm->context)
goto error1;
-
- shm.global = wl_global_create(swc.display, &wl_shm_interface, 1, NULL, &bind_shm);
-
- if (!shm.global)
+ shm->renderer = wld_create_renderer(shm->context);
+ if (!shm->renderer)
goto error2;
+ shm->global = wl_global_create(display, &wl_shm_interface, 1, shm, &bind_shm);
+ if (!shm->global)
+ goto error3;
- return true;
+ return shm;
+error3:
+ wld_destroy_renderer(shm->renderer);
error2:
- wld_destroy_renderer(swc.shm->renderer);
+ wld_destroy_context(shm->context);
error1:
- wld_destroy_context(swc.shm->context);
+ free(shm);
error0:
- return false;
+ return NULL;
}
void
-shm_finalize(void)
+shm_destroy(struct swc_shm *shm)
{
- wl_global_destroy(shm.global);
- wld_destroy_renderer(swc.shm->renderer);
- wld_destroy_context(swc.shm->context);
+ wl_global_destroy(shm->global);
+ wld_destroy_renderer(shm->renderer);
+ wld_destroy_context(shm->context);
}
diff --git a/libswc/shm.h b/libswc/shm.h
@@ -1,6 +1,6 @@
/* swc: libswc/shm.h
*
- * Copyright (c) 2013 Michael Forney
+ * Copyright (c) 2013-2019 Michael Forney
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -24,14 +24,15 @@
#ifndef SWC_SHM_H
#define SWC_SHM_H
-#include <stdbool.h>
+struct wl_display;
struct swc_shm {
+ struct wl_global *global;
struct wld_context *context;
struct wld_renderer *renderer;
};
-bool shm_initialize(void);
-void shm_finalize(void);
+struct swc_shm *shm_create(struct wl_display *display);
+void shm_destroy(struct swc_shm *shm);
#endif
diff --git a/libswc/swc.c b/libswc/swc.c
@@ -46,7 +46,6 @@ extern const struct swc_seat swc_seat;
extern const struct swc_bindings swc_bindings;
extern struct swc_compositor swc_compositor;
extern struct swc_drm swc_drm;
-extern struct swc_shm swc_shm;
extern struct pointer_handler screens_pointer_handler;
@@ -55,7 +54,6 @@ struct swc swc = {
.bindings = &swc_bindings,
.compositor = &swc_compositor,
.drm = &swc_drm,
- .shm = &swc_shm,
};
static void
@@ -120,7 +118,8 @@ swc_initialize(struct wl_display *display, struct wl_event_loop *event_loop, con
goto error1;
}
- if (!shm_initialize()) {
+ swc.shm = shm_create(display);
+ if (!swc.shm) {
ERROR("Could not initialize SHM\n");
goto error2;
}
@@ -192,7 +191,7 @@ error5:
error4:
bindings_finalize();
error3:
- shm_finalize();
+ shm_destroy(swc.shm);
error2:
drm_finalize();
error1:
@@ -211,7 +210,7 @@ swc_finalize(void)
compositor_finalize();
screens_finalize();
bindings_finalize();
- shm_finalize();
+ shm_destroy(swc.shm);
drm_finalize();
launch_finalize();
}