neuwld

patched neuwld library (tracked)
git clone https://git.daat.foo/neuwld.git
Log | Files | Refs | README | LICENSE

commit ea86c0dd87efa8eb99dd42869d1178b1a86d5232
parent 22f8d81948c8f73e0dcbecc28bb7b5ab06d32622
Author: Michael Forney <mforney@mforney.org>
Date:   Mon, 13 Jan 2014 14:20:00 -0800

wayland-{shm,drm}: Use wld_context

Diffstat:
Mwayland-drm.c | 138++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
Mwayland-drm.h | 23+++--------------------
Mwayland-private.h | 16++++------------
Mwayland-shm.c | 131++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Mwayland-shm.h | 23+++--------------------
Mwayland.c | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mwayland.h | 5+++++
7 files changed, 244 insertions(+), 167 deletions(-)

diff --git a/wayland-drm.c b/wayland-drm.c @@ -37,8 +37,9 @@ #include <stdio.h> -struct wld_wayland_drm_context +struct drm_context { + struct wld_context base; struct wld_context * driver_context; struct wl_drm * wl; struct wl_registry * registry; @@ -48,6 +49,9 @@ struct wld_wayland_drm_context bool authenticated; }; +#include "interface/context.h" +IMPL(drm, context); + static void registry_global(void * data, struct wl_registry * registry, uint32_t name, const char * interface, uint32_t version); @@ -61,12 +65,7 @@ static void drm_capabilities(void * data, struct wl_drm * wl, uint32_t capabilities); const struct wld_wayland_interface wayland_drm_interface = { - .create_context - = (wayland_create_context_func_t) &wld_wayland_drm_create_context, - .destroy_context - = (wayland_destroy_context_func_t) &wld_wayland_drm_destroy_context, - .create_drawable - = (wayland_create_drawable_func_t) &wld_wayland_drm_create_drawable + .create_context = &wld_wayland_drm_create_context, }; const static struct wl_registry_listener registry_listener = { @@ -82,16 +81,17 @@ const static struct wl_drm_listener drm_listener = { }; EXPORT -struct wld_wayland_drm_context * wld_wayland_drm_create_context - (struct wl_display * display, struct wl_event_queue * queue) +struct wld_context * wld_wayland_drm_create_context(struct wl_display * display, + struct wl_event_queue * queue) { - struct wld_wayland_drm_context * drm; + struct drm_context * drm; drm = malloc(sizeof *drm); if (!drm) goto error0; + context_initialize(&drm->base, &context_impl); drm->wl = NULL; drm->fd = -1; drm->capabilities = 0; @@ -146,7 +146,7 @@ struct wld_wayland_drm_context * wld_wayland_drm_create_context goto error4; } - return drm; + return &drm->base; error4: close(drm->fd); @@ -162,24 +162,12 @@ struct wld_wayland_drm_context * wld_wayland_drm_create_context } EXPORT -void wld_wayland_drm_destroy_context(struct wld_wayland_drm_context * drm) -{ - wld_destroy_context(&drm->driver_context); - close(drm->fd); - wl_drm_destroy(drm->wl); - wl_registry_destroy(drm->registry); - wl_array_release(&drm->formats); - - free(drm); -} - -EXPORT -bool wld_wayland_drm_has_format(struct wld_wayland_drm_context * drm, - uint32_t format) +bool wld_wayland_drm_has_format(struct wld_context * base, uint32_t format) { + struct drm_context * context = drm_context(base); uint32_t * supported_format; - wl_array_for_each(supported_format, &drm->formats) + wl_array_for_each(supported_format, &context->formats) { if (*supported_format == format) return true; @@ -189,47 +177,83 @@ bool wld_wayland_drm_has_format(struct wld_wayland_drm_context * drm, } EXPORT -int wld_wayland_drm_get_fd(struct wld_wayland_drm_context * drm) +int wld_wayland_drm_get_fd(struct wld_context * base) { - return drm->authenticated ? drm->fd : -1; + struct drm_context * context = drm_context(base); + + return context->authenticated ? context->fd : -1; } -EXPORT -struct wld_drawable * wld_wayland_drm_create_drawable - (struct wld_wayland_drm_context * drm, uint32_t width, uint32_t height, - uint32_t format, struct wl_buffer ** buffer) +struct wld_drawable * context_create_drawable(struct wld_context * base, + uint32_t width, uint32_t height, + uint32_t format) { + struct drm_context * context = drm_context(base); struct wld_drawable * drawable; + struct wld_exporter * exporter; + union wld_object object; + struct wl_buffer * wl; - if (buffer && !wld_wayland_drm_has_format(drm, format)) - return NULL; + if (!wld_wayland_drm_has_format(&context->base, format)) + goto error0; - drawable = wld_create_drawable(drm->driver_context, width, height, format); + drawable = wld_create_drawable(context->driver_context, width, height, format); if (!drawable) - return NULL; + goto error0; - if (buffer) - { - union wld_object object; + if (!wld_export(drawable, WLD_DRM_OBJECT_PRIME_FD, &object)) + goto error1; - wld_export(drawable, WLD_DRM_OBJECT_PRIME_FD, &object); - *buffer = wl_drm_create_prime_buffer(drm->wl, object.i, - width, height, format, - 0, drawable->pitch, 0, 0, 0, 0); - close(prime_fd); - } + wl = wl_drm_create_prime_buffer(context->wl, object.i, width, height, + format, 0, drawable->pitch, 0, 0, 0, 0); + close(object.i); + + if (!wl) + goto error1; + + if (!(exporter = wayland_create_exporter(wl))) + goto error2; + + drawable_add_exporter(drawable, exporter); return drawable; + + error2: + wl_buffer_destroy(wl); + error1: + wld_destroy_drawable(drawable); + error0: + return NULL; +} + +struct wld_drawable * context_import(struct wld_context * context, + uint32_t type, union wld_object object, + uint32_t width, uint32_t height, + uint32_t format, uint32_t pitch) +{ + return NULL; +} + +void context_destroy(struct wld_context * base) +{ + struct drm_context * context = drm_context(base); + + wld_destroy_context(context->driver_context); + close(context->fd); + wl_drm_destroy(context->wl); + wl_registry_destroy(context->registry); + wl_array_release(&context->formats); + free(context); } void registry_global(void * data, struct wl_registry * registry, uint32_t name, const char * interface, uint32_t version) { - struct wld_wayland_drm_context * drm = data; + struct drm_context * context = data; if (strcmp(interface, "wl_drm") == 0 && version >= 2) - drm->wl = wl_registry_bind(registry, name, &wl_drm_interface, 2); + context->wl = wl_registry_bind(registry, name, &wl_drm_interface, 2); } void registry_global_remove(void * data, struct wl_registry * registry, @@ -239,39 +263,39 @@ void registry_global_remove(void * data, struct wl_registry * registry, void drm_device(void * data, struct wl_drm * wl, const char * name) { - struct wld_wayland_drm_context * drm = data; + struct drm_context * context = data; drm_magic_t magic; - drm->fd = open(name, O_RDWR); + context->fd = open(name, O_RDWR); - if (drm->fd == -1) + if (context->fd == -1) { DEBUG("Couldn't open DRM device '%s'\n", name); return; } - drmGetMagic(drm->fd, &magic); + drmGetMagic(context->fd, &magic); wl_drm_authenticate(wl, magic); } void drm_format(void * data, struct wl_drm * wl, uint32_t format) { - struct wld_wayland_drm_context * drm = data; + struct drm_context * context = data; - *((uint32_t *) wl_array_add(&drm->formats, sizeof format)) = format; + *((uint32_t *) wl_array_add(&context->formats, sizeof format)) = format; } void drm_authenticated(void * data, struct wl_drm * wl) { - struct wld_wayland_drm_context * drm = data; + struct drm_context * context = data; - drm->authenticated = true; + context->authenticated = true; } void drm_capabilities(void * data, struct wl_drm * wl, uint32_t capabilities) { - struct wld_wayland_drm_context * drm = data; + struct drm_context * context = data; - drm->capabilities = capabilities; + context->capabilities = capabilities; } diff --git a/wayland-drm.h b/wayland-drm.h @@ -27,44 +27,27 @@ #include <stdbool.h> #include <stdint.h> -struct wld_wayland_drm_context; -struct wld_drawable; - struct wl_display; struct wl_event_queue; -struct wl_buffer; /** * Create a new drawable context which creates Wayland buffers through the * wl_drm interface, backed by hardware specific drawable implementations. */ -struct wld_wayland_drm_context * wld_wayland_drm_create_context +struct wld_context * wld_wayland_drm_create_context (struct wl_display * display, struct wl_event_queue * queue); /** - * Destroy a Wayland DRM context. - */ -void wld_wayland_drm_destroy_context(struct wld_wayland_drm_context * context); - -/** * Check if the wl_drm global has the specified pixel format. * * @see enum wld_format */ -bool wld_wayland_drm_has_format(struct wld_wayland_drm_context * context, - uint32_t format); +bool wld_wayland_drm_has_format(struct wld_context * context, uint32_t format); /** * Get the opened file descriptor for the DRM device. */ -int wld_wayland_drm_get_fd(struct wld_wayland_drm_context * context); - -/** - * Create a new DRM drawable with the specified dimensions. - */ -struct wld_drawable * wld_wayland_drm_create_drawable - (struct wld_wayland_drm_context * context, uint32_t width, uint32_t height, - uint32_t format, struct wl_buffer ** buffer); +int wld_wayland_drm_get_fd(struct wld_context * context); #endif diff --git a/wayland-private.h b/wayland-private.h @@ -26,24 +26,14 @@ #include <stdint.h> -struct wld_drawable; - struct wl_display; struct wl_event_queue; struct wl_buffer; -typedef void * (* wayland_create_context_func_t)(struct wl_display * display, - struct wl_event_queue * queue); -typedef void (* wayland_destroy_context_func_t)(void * context); -typedef struct wld_drawable * (* wayland_create_drawable_func_t) - (void * context, uint32_t width, uint32_t height, uint32_t format, - struct wl_buffer ** buffer); - struct wld_wayland_interface { - wayland_create_context_func_t create_context; - wayland_destroy_context_func_t destroy_context; - wayland_create_drawable_func_t create_drawable; + struct wld_context * (* create_context)(struct wl_display * display, + struct wl_event_queue * queue); }; #if WITH_WAYLAND_DRM @@ -60,5 +50,7 @@ extern const struct wld_wayland_interface wayland_shm_interface; int wayland_roundtrip(struct wl_display * display, struct wl_event_queue * queue); +struct wld_exporter * wayland_create_exporter(struct wl_buffer * buffer); + #endif diff --git a/wayland-shm.c b/wayland-shm.c @@ -36,8 +36,10 @@ #include <sys/mman.h> #include <wayland-client.h> -struct wld_shm_context +struct shm_context { + struct wld_context base; + struct wl_registry * registry; struct wl_shm * wl; struct wl_array formats; @@ -45,6 +47,9 @@ struct wld_shm_context struct wld_context * pixman_context; }; +#include "interface/context.h" +IMPL(shm, context) + static void registry_global(void * data, struct wl_registry * registry, uint32_t name, const char * interface, uint32_t version); @@ -54,10 +59,7 @@ static void registry_global_remove(void * data, struct wl_registry * registry, static void shm_format(void * data, struct wl_shm * wl, uint32_t format); const struct wld_wayland_interface wayland_shm_interface = { - .create_context = (wayland_create_context_func_t) &wld_shm_create_context, - .destroy_context - = (wayland_destroy_context_func_t) &wld_shm_destroy_context, - .create_drawable = (wayland_create_drawable_func_t) &wld_shm_create_drawable + .create_context = &wld_shm_create_context, }; const static struct wl_registry_listener registry_listener = { @@ -83,74 +85,61 @@ static inline uint32_t wayland_format(uint32_t format) } EXPORT -struct wld_shm_context * wld_shm_create_context(struct wl_display * display, - struct wl_event_queue * queue) +struct wld_context * wld_shm_create_context(struct wl_display * display, + struct wl_event_queue * queue) { - struct wld_shm_context * shm; + struct shm_context * context; - shm = malloc(sizeof *shm); - - if (!shm) + if (!(context = malloc(sizeof *context))) goto error0; - shm->wl = NULL; - wl_array_init(&shm->formats); - - shm->registry = wl_display_get_registry(display); + context_initialize(&context->base, &context_impl); + context->wl = NULL; + wl_array_init(&context->formats); - if (!shm->registry) + if (!(context->registry = wl_display_get_registry(display))) { DEBUG("Couldn't get registry\n"); goto error1; } - wl_registry_add_listener(shm->registry, &registry_listener, shm); - wl_proxy_set_queue((struct wl_proxy *) shm->registry, queue); + wl_registry_add_listener(context->registry, &registry_listener, context); + wl_proxy_set_queue((struct wl_proxy *) context->registry, queue); - shm->pixman_context = wld_pixman_create_context(); + context->pixman_context = wld_pixman_create_context(); /* Wait for wl_shm global. */ wayland_roundtrip(display, queue); - if (!shm->wl) + if (!context->wl) { DEBUG("No wl_shm global\n"); goto error2; } - wl_shm_add_listener(shm->wl, &shm_listener, shm); + wl_shm_add_listener(context->wl, &shm_listener, context); /* Wait for SHM formats. */ wayland_roundtrip(display, queue); - return shm; + return &context->base; error2: - wl_registry_destroy(shm->registry); + wl_registry_destroy(context->registry); error1: - wl_array_release(&shm->formats); - free(shm); + wl_array_release(&context->formats); + free(context); error0: return NULL; } EXPORT -void wld_shm_destroy_context(struct wld_shm_context * shm) -{ - wld_destroy_context(shm->pixman_context); - wl_shm_destroy(shm->wl); - wl_registry_destroy(shm->registry); - wl_array_release(&shm->formats); - - free(shm); -} - -EXPORT -bool wld_shm_has_format(struct wld_shm_context * shm, uint32_t format) +bool wld_shm_has_format(struct wld_context * base, uint32_t format) { + struct shm_context * context = shm_context(base); uint32_t * supported_format; - wl_array_for_each(supported_format, &shm->formats) + wl_array_for_each(supported_format, &context->formats) { if (*supported_format == format) return true; @@ -159,21 +148,22 @@ bool wld_shm_has_format(struct wld_shm_context * shm, uint32_t format) return false; } -EXPORT -struct wld_drawable * wld_shm_create_drawable(struct wld_shm_context * shm, +struct wld_drawable * context_create_drawable(struct wld_context * base, uint32_t width, uint32_t height, - uint32_t format, - struct wl_buffer ** buffer) + uint32_t format) { + struct shm_context * context = shm_context(base); + struct wld_drawable * drawable; + struct wld_exporter * exporter; char name[] = "/tmp/wld-XXXXXX"; uint32_t pitch = width * format_bytes_per_pixel(format); uint32_t size = height * pitch; int fd; void * data; - struct wl_shm_pool * pool; - struct wld_drawable * drawable; uint32_t shm_format = wayland_format(format); union wld_object object; + struct wl_shm_pool * pool; + struct wl_buffer * wl; fd = mkostemp(name, O_CLOEXEC); @@ -191,30 +181,67 @@ struct wld_drawable * wld_shm_create_drawable(struct wld_shm_context * shm, goto error1; object.ptr = data; - drawable = wld_import(shm->pixman_context, WLD_OBJECT_DATA, object, + drawable = wld_import(context->pixman_context, WLD_OBJECT_DATA, object, width, height, format, pitch); - pool = wl_shm_create_pool(shm->wl, fd, size); - *buffer = wl_shm_pool_create_buffer(pool, 0, width, height, pitch, - shm_format); + if (!drawable) + goto error2; + + if (!(pool = wl_shm_create_pool(context->wl, fd, size))) + goto error3; + + wl = wl_shm_pool_create_buffer(pool, 0, width, height, pitch, shm_format); wl_shm_pool_destroy(pool); + + if (!wl) + goto error3; + + if (!(exporter = wayland_create_exporter(wl))) + goto error4; + + drawable_add_exporter(drawable, exporter); close(fd); return drawable; + error4: + wl_buffer_destroy(wl); + error3: + wld_destroy_drawable(drawable); + error2: + munmap(object.ptr, size); error1: close(fd); error0: return NULL; } +struct wld_drawable * context_import(struct wld_context * context, + uint32_t type, union wld_object object, + uint32_t width, uint32_t height, + uint32_t format, uint32_t pitch) +{ + return NULL; +} + +void context_destroy(struct wld_context * base) +{ + struct shm_context * context = shm_context(base); + + wld_destroy_context(context->pixman_context); + wl_shm_destroy(context->wl); + wl_registry_destroy(context->registry); + wl_array_release(&context->formats); + free(context); +} + void registry_global(void * data, struct wl_registry * registry, uint32_t name, const char * interface, uint32_t version) { - struct wld_shm_context * shm = data; + struct shm_context * context = data; if (strcmp(interface, "wl_shm") == 0) - shm->wl = wl_registry_bind(registry, name, &wl_shm_interface, 1); + context->wl = wl_registry_bind(registry, name, &wl_shm_interface, 1); } void registry_global_remove(void * data, struct wl_registry * registry, @@ -224,8 +251,8 @@ void registry_global_remove(void * data, struct wl_registry * registry, void shm_format(void * data, struct wl_shm * wl, uint32_t format) { - struct wld_shm_context * shm = data; + struct shm_context * context = data; - *((uint32_t *) wl_array_add(&shm->formats, sizeof format)) = format; + *((uint32_t *) wl_array_add(&context->formats, sizeof format)) = format; } diff --git a/wayland-shm.h b/wayland-shm.h @@ -27,39 +27,22 @@ #include <stdbool.h> #include <stdint.h> -struct wld_shm_context; -struct wld_drawable; - struct wl_display; struct wl_event_queue; -struct wl_buffer; /** * Create a new drawable context which creates Wayland buffers through the * wl_shm interface, back by Pixman drawables. */ -struct wld_shm_context * wld_shm_create_context(struct wl_display * display, - struct wl_event_queue * queue); - -/** - * Destroy an SHM context. - */ -void wld_shm_destroy_context(struct wld_shm_context * context); +struct wld_context * wld_shm_create_context(struct wl_display * display, + struct wl_event_queue * queue); /** * Check if the wl_shm global has the specified pixel format. * * @see enum wld_format */ -bool wld_shm_has_format(struct wld_shm_context * context, uint32_t format); - -/** - * Create a new SHM drawable with the specified dimensions. - */ -struct wld_drawable * wld_shm_create_drawable(struct wld_shm_context * context, - uint32_t width, uint32_t height, - uint32_t format, - struct wl_buffer ** buffer); +bool wld_shm_has_format(struct wld_context * context, uint32_t format); #endif diff --git a/wayland.c b/wayland.c @@ -33,10 +33,11 @@ struct wld_wayland_context { + struct wld_context base; struct wl_display * display; struct wl_event_queue * queue; const struct wld_wayland_interface * interface; - void * context; + struct wld_context * context; }; struct wayland_drawable @@ -56,6 +57,12 @@ struct wayland_drawable uint32_t damage_tracking; }; +struct wayland_exporter +{ + struct wld_exporter base; + struct wl_buffer * buffer; +}; + static void sync_done(void * data, struct wl_callback * callback, uint32_t msecs); @@ -63,6 +70,8 @@ static void buffer_release(void * data, struct wl_buffer * buffer); #define DRAWABLE_IMPLEMENTS_REGION #include "interface/drawable.h" +#include "interface/exporter.h" +IMPL(wayland, exporter) const struct wl_callback_listener sync_listener = { .done = &sync_done @@ -162,7 +171,7 @@ struct wld_wayland_context * wld_wayland_create_context EXPORT void wld_wayland_destroy_context(struct wld_wayland_context * wayland) { - wayland->interface->destroy_context(wayland->context); + wld_destroy_context(wayland->context); free(wayland); } @@ -172,6 +181,7 @@ struct wld_drawable * wld_wayland_create_drawable uint32_t width, uint32_t height, uint32_t format, uint32_t damage_flags) { struct wayland_drawable * wayland; + union wld_object object; wayland = malloc(sizeof *wayland); @@ -179,18 +189,34 @@ struct wld_drawable * wld_wayland_create_drawable goto error0; wayland->context = context; - wayland->buffers[0].drawable = context->interface->create_drawable - (context->context, width, height, format, &wayland->buffers[0].wl); + wayland->buffers[0].drawable = wld_create_drawable(context->context, + width, height, format); if (!wayland->buffers[0].drawable) goto error1; - wayland->buffers[1].drawable = context->interface->create_drawable - (context->context, width, height, format, &wayland->buffers[1].wl); + wayland->buffers[1].drawable = wld_create_drawable(context->context, + width, height, format); if (!wayland->buffers[1].drawable) goto error2; + if (!wld_export(wayland->buffers[0].drawable, + WLD_WAYLAND_OBJECT_BUFFER, &object)) + { + goto error3; + } + + wayland->buffers[0].wl = object.ptr; + + if (!wld_export(wayland->buffers[1].drawable, + WLD_WAYLAND_OBJECT_BUFFER, &object)) + { + goto error3; + } + + wayland->buffers[1].wl = object.ptr; + wayland->buffers[0].busy = false; wayland->buffers[1].busy = false; pixman_region32_init(&wayland->buffers[0].damage); @@ -210,6 +236,8 @@ struct wld_drawable * wld_wayland_create_drawable return &wayland->base; + error3: + wld_destroy_drawable(wayland->buffers[1].drawable); error2: wld_destroy_drawable(wayland->buffers[0].drawable); error1: @@ -253,6 +281,41 @@ int wayland_roundtrip(struct wl_display * display, return ret; } +struct wld_exporter * wayland_create_exporter(struct wl_buffer * buffer) +{ + struct wayland_exporter * exporter; + + if (!(exporter = malloc(sizeof *exporter))) + return NULL; + + exporter_initialize(&exporter->base, &exporter_impl); + exporter->buffer = buffer; + + return &exporter->base; +} + +bool exporter_export(struct wld_exporter * base, struct wld_drawable * drawable, + uint32_t type, union wld_object * object) +{ + struct wayland_exporter * exporter = wayland_exporter(base); + + switch (type) + { + case WLD_WAYLAND_OBJECT_BUFFER: + object->ptr = exporter->buffer; + return true; + default: return false; + } +} + +void exporter_destroy(struct wld_exporter * base) +{ + struct wayland_exporter * exporter = wayland_exporter(base); + + wl_buffer_destroy(exporter->buffer); + free(exporter); +} + void sync_done(void * data, struct wl_callback * callback, uint32_t msecs) { bool * done = data; diff --git a/wayland.h b/wayland.h @@ -48,6 +48,11 @@ enum wld_wayland_interface_id WLD_SHM }; +enum wld_wayland_object_type +{ + WLD_WAYLAND_OBJECT_BUFFER = 0x00020000 +}; + enum wld_wayland_damage_flags { /**