neuwld

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

commit 06017f56346b52237d9d04d862eca6d9930dc668
parent 64bdb1f6d958c6a25233506ce0df0ffcc5bb2986
Author: Michael Forney <mforney@mforney.org>
Date:   Thu,  5 Sep 2013 01:23:41 -0700

Separate non-wayland specific parts of wayland-drm

Now, applications can use hardware independent DRM drawables without
using wayland.

Diffstat:
MMakefile.am | 5+++++
Mconfigure.ac | 30+++++++++++++++++++-----------
Mdrm-private.h | 14+++++++++++---
Adrm.c | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adrm.h | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Mintel.c | 6+++---
Mwayland-drm.c | 115+++++++++++++++++++++++--------------------------------------------------------
Mwayland-drm.h | 25++++++++++++-------------
8 files changed, 265 insertions(+), 112 deletions(-)

diff --git a/Makefile.am b/Makefile.am @@ -19,6 +19,11 @@ if WITH_WAYLAND pkginclude_HEADERS += wayland.h endif +if ENABLE_DRM + libwld_la_SOURCES += drm.c + pkginclude_HEADERS += drm.h +endif + if ENABLE_WAYLAND_DRM AM_CPPFLAGS = -I$(top_builddir)/protocol libwld_la_SOURCES += wayland-drm.c protocol/wayland-drm-protocol.c diff --git a/configure.ac b/configure.ac @@ -47,33 +47,41 @@ if test "x$with_wayland" = "xyes" ; then fi AM_CONDITIONAL([WITH_WAYLAND], test "x$with_wayland" = "xyes") -# Wayland DRM +# DRM AC_ARG_ENABLE([drm], AS_HELP_STRING([--disable-drm], - [disable Wayland DRM support]), + [disable DRM support]), enable_drm=$enableval, - enable_drm=$with_wayland) -if test "x$enable_drm" = "xyes" ; then + enable_drm=yes) +AM_CONDITIONAL([ENABLE_DRM], [test "x$enable_drm" = "xyes"]) + +# Wayland DRM +AC_ARG_ENABLE([wayland-drm], + AS_HELP_STRING([--disable-wayland-drm], + [disable Wayland DRM support]), + enable_wayland_drm=$enableval, + enable_wayland_drm=$with_wayland) +if test "x$enable_wayland_drm" = "xyes" ; then if test "x$with_wayland" != "xyes" ; then - AC_MSG_ERROR([--enable-drm requires --with-wayland]) + AC_MSG_ERROR([--enable-wayland-drm requires --with-wayland]) fi AC_DEFINE([ENABLE_WAYLAND_DRM], [1], [Enable Wayland DRM support]) fi -AM_CONDITIONAL([ENABLE_WAYLAND_DRM], test "x$enable_drm" = "xyes") +AM_CONDITIONAL([ENABLE_WAYLAND_DRM], test "x$enable_wayland_drm" = "xyes") # Wayland SHM -AC_ARG_ENABLE([shm], - AS_HELP_STRING([--disable-shm], +AC_ARG_ENABLE([wayland-shm], + AS_HELP_STRING([--disable-wayland-shm], [disable Wayland SHM support]), enable_shm=$enableval, enable_shm=$with_wayland) -if test "x$enable_shm" = "xyes" ; then +if test "x$enable_wayland_shm" = "xyes" ; then if test "x$with_wayland" != "xyes" ; then - AC_MSG_ERROR([--enable-shm requires --with-wayland]) + AC_MSG_ERROR([--enable-wayland-shm requires --with-wayland]) fi AC_DEFINE([ENABLE_WAYLAND_SHM], [1], [Enable Wayland SHM support]) fi -AM_CONDITIONAL([ENABLE_WAYLAND_SHM], test "x$enable_shm" = "xyes") +AM_CONDITIONAL([ENABLE_WAYLAND_SHM], test "x$enable_wayland_shm" = "xyes") # Intel AC_ARG_ENABLE([intel], diff --git a/drm-private.h b/drm-private.h @@ -30,7 +30,7 @@ typedef bool (* drm_device_supported_func_t)(uint32_t vendor_id, uint32_t device_id); typedef void * (* drm_create_context_func_t)(int drm_fd); typedef void (* drm_destroy_context_func_t)(void * context); -typedef struct wld_drawable * (* drm_create_drawable_func_t) +typedef struct drm_drawable * (* drm_create_drawable_func_t) (void * context, uint32_t width, uint32_t height, uint32_t format); struct wld_drm_interface @@ -41,11 +41,16 @@ struct wld_drm_interface drm_create_drawable_func_t create_drawable; }; +struct wld_drm_context +{ + const struct wld_drm_interface * interface; + void * context; +}; + struct drm_drawable { struct wld_drawable base; - - unsigned long pitch; + struct wld_drm_context * drm; int fd; }; @@ -56,5 +61,8 @@ _Static_assert(offsetof(struct drm_drawable, base) == 0, extern const struct wld_drm_interface intel_drm; #endif +bool drm_initialize_context(struct wld_drm_context * context, int fd); +void drm_finalize_context(struct wld_drm_context * context); + #endif diff --git a/drm.c b/drm.c @@ -0,0 +1,132 @@ +/* wld: drm.c + * + * Copyright (c) 2013 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 + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "drm.h" +#include "drm-private.h" + +const static struct wld_drm_interface * drm_interfaces[] = { +#if ENABLE_INTEL + &intel_drm +#endif +}; + +static const struct wld_drm_interface * find_drm_interface(int fd) +{ + char path[64], id[32]; + uint32_t vendor_id, device_id; + char * path_part; + struct stat st; + FILE * file; + uint32_t index; + + if (fstat(fd, &st) == -1) + return NULL; + + snprintf(path, sizeof path, "/sys/dev/char/%u:%u/", + major(st.st_rdev), minor(st.st_rdev)); + path_part = path + strlen(path); + + strcpy(path_part, "device/vendor"); + file = fopen(path, "r"); + fgets(id, sizeof id, file); + fclose(file); + vendor_id = strtoul(id, NULL, 0); + + strcpy(path_part, "device/device"); + file = fopen(path, "r"); + fgets(id, sizeof id, file); + fclose(file); + device_id = strtoul(id, NULL, 0); + + for (index = 0; index < ARRAY_LENGTH(drm_interfaces); ++index) + { + if (drm_interfaces[index]->device_supported(vendor_id, device_id)) + return drm_interfaces[index]; + } + + return NULL; +} + +bool drm_initialize_context(struct wld_drm_context * drm, int fd) +{ + if (!(drm->interface = find_drm_interface(fd))) + return false; + + if (!(drm->context = drm->interface->create_context(fd))) + return false; + + return true; +} + +void drm_finalize_context(struct wld_drm_context * drm) +{ + drm->interface->destroy_context(drm->context); +} + +struct wld_drm_context * wld_drm_create_context(int fd) +{ + struct wld_drm_context * drm; + + if (!(drm = malloc(sizeof *drm))) + goto error0; + + if (!drm_initialize_context(drm, fd)) + goto error1; + + return drm; + + error1: + free(drm); + error0: + return NULL; +} + +void wld_drm_destroy_context(struct wld_drm_context * drm) +{ + drm_finalize_context(drm); + free(drm); +} + +static struct wld_drawable * finish_drawable(struct wld_drm_context * drm, + struct drm_drawable * drawable) +{ + if (!drawable) + return NULL; + + drawable->drm = drm; + + return &drawable->base; +} + +struct wld_drawable * wld_drm_create_drawable(struct wld_drm_context * drm, + uint32_t width, uint32_t height, + uint32_t format) +{ + struct drm_drawable * drawable; + + drawable = drm->interface->create_drawable(drm->context, + width, height, format); + + return finish_drawable(drm, drawable); +} + diff --git a/drm.h b/drm.h @@ -0,0 +1,50 @@ +/* wld: drm.h + * + * Copyright (c) 2013 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 + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef WLD_DRM_H +#define WLD_DRM_H 1 + +#include <stdint.h> + +struct wld_drm_context; +struct wld_drawable; + +/** + * Create a new DRM context from an opened DRM device file descriptor. + */ +struct wld_drm_context * wld_drm_create_context(int fd); + +/** + * Destroy a DRM context. + */ +void wld_drm_destroy_context(struct wld_drm_context * drm); + +/** + * Create a new DRM drawable. + */ +struct wld_drawable * wld_drm_create_drawable(struct wld_drm_context * drm, + uint32_t width, uint32_t height, + uint32_t format); + +#endif + diff --git a/intel.c b/intel.c @@ -69,7 +69,7 @@ static void intel_destroy(struct wld_drawable * drawable); static struct wld_intel_context * intel_create_context(int drm_fd); static void intel_destroy_context(struct wld_intel_context * context); static bool intel_device_supported(uint32_t vendor_id, uint32_t device_id); -static struct wld_drawable * intel_create_drawable +static struct drm_drawable * intel_create_drawable (struct wld_intel_context * context, uint32_t width, uint32_t height, uint32_t format); @@ -116,7 +116,7 @@ void intel_destroy_context(struct wld_intel_context * context) free(context); } -struct wld_drawable * intel_create_drawable +struct drm_drawable * intel_create_drawable (struct wld_intel_context * context, uint32_t width, uint32_t height, uint32_t format) { @@ -138,7 +138,7 @@ struct wld_drawable * intel_create_drawable &tiling_mode, &intel->drm.base.pitch, 0); drm_intel_bo_gem_export_to_prime(intel->bo, &intel->drm.fd); - return &intel->drm.base; + return &intel->drm; } static void intel_fill_rectangle(struct wld_drawable * drawable, uint32_t color, diff --git a/wayland-drm.c b/wayland-drm.c @@ -24,6 +24,7 @@ #include "wayland-drm.h" #include "wayland-drm-client-protocol.h" #include "wayland.h" +#include "drm.h" #include "wayland-private.h" #include "wld-private.h" #include "drm-private.h" @@ -36,17 +37,16 @@ #include <stdio.h> -struct wld_drm_context +struct wld_wayland_drm_context { + struct wld_drm_context context; + struct wl_drm * wl; struct wl_registry * registry; struct wl_array formats; uint32_t capabilities; int fd; bool authenticated; - - const struct wld_drm_interface * interface; - void * context; }; static void registry_global(void * data, struct wl_registry * registry, @@ -60,10 +60,12 @@ 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_drm_create_context, + .create_context + = (wayland_create_context_func_t) &wld_wayland_drm_create_context, .destroy_context - = (wayland_destroy_context_func_t) &wld_drm_destroy_context, - .create_drawable = (wayland_create_drawable_func_t) &wld_drm_create_drawable + = (wayland_destroy_context_func_t) &wld_wayland_drm_destroy_context, + .create_drawable + = (wayland_create_drawable_func_t) &wld_wayland_drm_create_drawable }; const static struct wl_registry_listener registry_listener = { @@ -77,53 +79,10 @@ const static struct wl_drm_listener drm_listener = { .capabilities = &drm_capabilities }; -const static struct wld_drm_interface * drm_interfaces[] = { -#if ENABLE_INTEL - &intel_drm -#endif -}; - -static const struct wld_drm_interface * find_drm_interface(int fd) -{ - char path[64], id[32]; - uint32_t vendor_id, device_id; - char * path_part; - struct stat st; - FILE * file; - uint32_t index; - - if (fstat(fd, &st) == -1) - return NULL; - - snprintf(path, sizeof path, "/sys/dev/char/%u:%u/", - major(st.st_rdev), minor(st.st_rdev)); - path_part = path + strlen(path); - - strcpy(path_part, "device/vendor"); - file = fopen(path, "r"); - fgets(id, sizeof id, file); - fclose(file); - vendor_id = strtoul(id, NULL, 0); - - strcpy(path_part, "device/device"); - file = fopen(path, "r"); - fgets(id, sizeof id, file); - fclose(file); - device_id = strtoul(id, NULL, 0); - - for (index = 0; index < ARRAY_LENGTH(drm_interfaces); ++index) - { - if (drm_interfaces[index]->device_supported(vendor_id, device_id)) - return drm_interfaces[index]; - } - - return NULL; -} - -struct wld_drm_context * wld_drm_create_context(struct wl_display * display, - struct wl_event_queue * queue) +struct wld_wayland_drm_context * wld_wayland_drm_create_context + (struct wl_display * display, struct wl_event_queue * queue) { - struct wld_drm_context * drm; + struct wld_wayland_drm_context * drm; drm = malloc(sizeof *drm); @@ -178,19 +137,9 @@ struct wld_drm_context * wld_drm_create_context(struct wl_display * display, goto error4; } - drm->interface = find_drm_interface(drm->fd); - - if (!drm->interface) - { - DEBUG("Couldn't find drawable implementation for DRM device\n"); - goto error4; - } - - drm->context = drm->interface->create_context(drm->fd); - - if (!drm->context) + if (!drm_initialize_context(&drm->context, drm->fd)) { - DEBUG("Couldn't create context for DRM drawable implementation\n"); + DEBUG("Couldn't initialize context for DRM device\n"); goto error4; } @@ -209,9 +158,9 @@ struct wld_drm_context * wld_drm_create_context(struct wl_display * display, return NULL; } -void wld_drm_destroy_context(struct wld_drm_context * drm) +void wld_wayland_drm_destroy_context(struct wld_wayland_drm_context * drm) { - drm->interface->destroy_context(drm->context); + drm_finalize_context(&drm->context); close(drm->fd); wl_drm_destroy(drm->wl); wl_registry_destroy(drm->registry); @@ -220,7 +169,8 @@ void wld_drm_destroy_context(struct wld_drm_context * drm) free(drm); } -bool wld_drm_has_format(struct wld_drm_context * drm, uint32_t format) +bool wld_wayland_drm_has_format(struct wld_wayland_drm_context * drm, + uint32_t format) { uint32_t * supported_format; @@ -233,23 +183,24 @@ bool wld_drm_has_format(struct wld_drm_context * drm, uint32_t format) return false; } -int wld_drm_get_fd(struct wld_drm_context * drm) +int wld_wayland_drm_get_fd(struct wld_wayland_drm_context * drm) { return drm->authenticated ? drm->fd : -1; } -struct wld_drawable * wld_drm_create_drawable(struct wld_drm_context * drm, - uint32_t width, uint32_t height, - enum wld_format format, - struct wl_buffer ** buffer) +struct wld_drawable * wld_wayland_drm_create_drawable + (struct wld_wayland_drm_context * drm, uint32_t width, uint32_t height, + enum wld_format format, struct wl_buffer ** buffer) { struct drm_drawable * drawable; - if (buffer && !wld_drm_has_format(drm, format)) + if (buffer && !wld_wayland_drm_has_format(drm, format)) return NULL; - drawable = (void *) drm->interface->create_drawable(drm->context, - width, height, format); + drawable = (void *) wld_drm_create_drawable(&drm->context, width, height, format); + + if (!drawable) + return NULL; if (buffer) { @@ -261,7 +212,7 @@ struct wld_drawable * wld_drm_create_drawable(struct wld_drm_context * drm, return &drawable->base; } -int wld_drm_get_prime_fd(struct wld_drawable * drawable) +int wld_wayland_drm_get_prime_fd(struct wld_drawable * drawable) { return ((struct drm_drawable *)(void *) drawable)->fd; } @@ -269,7 +220,7 @@ int wld_drm_get_prime_fd(struct wld_drawable * drawable) void registry_global(void * data, struct wl_registry * registry, uint32_t name, const char * interface, uint32_t version) { - struct wld_drm_context * drm = data; + struct wld_wayland_drm_context * drm = data; if (strcmp(interface, "wl_drm") == 0 && version >= 2) drm->wl = wl_registry_bind(registry, name, &wl_drm_interface, 2); @@ -277,7 +228,7 @@ void registry_global(void * data, struct wl_registry * registry, uint32_t name, void drm_device(void * data, struct wl_drm * wl, const char * name) { - struct wld_drm_context * drm = data; + struct wld_wayland_drm_context * drm = data; drm_magic_t magic; drm->fd = open(name, O_RDWR); @@ -294,21 +245,21 @@ void drm_device(void * data, struct wl_drm * wl, const char * name) void drm_format(void * data, struct wl_drm * wl, uint32_t format) { - struct wld_drm_context * drm = data; + struct wld_wayland_drm_context * drm = data; *((uint32_t *) wl_array_add(&drm->formats, sizeof format)) = format; } void drm_authenticated(void * data, struct wl_drm * wl) { - struct wld_drm_context * drm = data; + struct wld_wayland_drm_context * drm = data; drm->authenticated = true; } void drm_capabilities(void * data, struct wl_drm * wl, uint32_t capabilities) { - struct wld_drm_context * drm = data; + struct wld_wayland_drm_context * drm = data; drm->capabilities = capabilities; } diff --git a/wayland-drm.h b/wayland-drm.h @@ -27,7 +27,7 @@ #include <stdbool.h> #include <stdint.h> -struct wld_context; +struct wld_wayland_drm_context; struct wld_drawable; enum wld_format; @@ -39,41 +39,40 @@ 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_drm_context * wld_drm_create_context(struct wl_display * display, - struct wl_event_queue * queue); +struct wld_wayland_drm_context * wld_wayland_drm_create_context + (struct wl_display * display, struct wl_event_queue * queue); /** - * Destroy a DRM context. + * Destroy a Wayland DRM context. */ -void wld_drm_destroy_context(struct wld_drm_context * 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_drm_has_format(struct wld_drm_context * context, - enum wld_format format); +bool wld_wayland_drm_has_format(struct wld_wayland_drm_context * context, + enum wld_format format); /** * Get the opened file descriptor for the DRM device. */ -int wld_drm_get_fd(struct wld_drm_context * context); +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_drm_create_drawable(struct wld_drm_context * context, - uint32_t width, uint32_t height, - enum wld_format format, - struct wl_buffer ** buffer); +struct wld_drawable * wld_wayland_drm_create_drawable + (struct wld_wayland_drm_context * context, uint32_t width, uint32_t height, + enum wld_format format, struct wl_buffer ** buffer); /** * Get a PRIME file descriptor for this DRM drawable. * * @note The drawable must have been created with wld_drm_create_drawable */ -int wld_drm_get_prime_fd(struct wld_drawable * drawable); +int wld_wayland_drm_get_prime_fd(struct wld_drawable * drawable); #endif