diff options
author | Simon Zeni <simon@bl4ckb0ne.ca> | 2021-04-23 17:06:21 -0400 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2021-04-28 20:55:57 +0200 |
commit | 318e3ac92cd3a172932370a7da8b2c2d24231758 (patch) | |
tree | 54d2b2bdda6bc9b5ae070a32d03fb10536a40954 | |
parent | 982498fab3c4d0aee9b29312559cbdd317c563a1 (diff) |
render/allocator: introduce wlr_allocator_autocreate
-rw-r--r-- | include/render/allocator.h | 7 | ||||
-rw-r--r-- | render/allocator.c | 42 |
2 files changed, 49 insertions, 0 deletions
diff --git a/include/render/allocator.h b/include/render/allocator.h index d47184af..4d8e68f4 100644 --- a/include/render/allocator.h +++ b/include/render/allocator.h @@ -7,6 +7,8 @@ #include <wlr/render/drm_format_set.h> struct wlr_allocator; +struct wlr_backend; +struct wlr_renderer; struct wlr_allocator_interface { struct wlr_buffer *(*create_buffer)(struct wlr_allocator *alloc, @@ -23,6 +25,11 @@ struct wlr_allocator { }; /** + * Creates the adequate wlr_allocator given a backend and a renderer + */ +struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend, + struct wlr_renderer *renderer); +/** * Destroy the allocator. */ void wlr_allocator_destroy(struct wlr_allocator *alloc); diff --git a/render/allocator.c b/render/allocator.c index ea1712e9..1c076bfd 100644 --- a/render/allocator.c +++ b/render/allocator.c @@ -1,6 +1,15 @@ +#define _POSIX_C_SOURCE 200809L #include <assert.h> +#include <fcntl.h> #include <stdlib.h> +#include <wlr/util/log.h> +#include <xf86drm.h> +#include "backend/backend.h" #include "render/allocator.h" +#include "render/gbm_allocator.h" +#include "render/shm_allocator.h" +#include "render/wlr_renderer.h" +#include "types/wlr_buffer.h" void wlr_allocator_init(struct wlr_allocator *alloc, const struct wlr_allocator_interface *impl) { @@ -9,6 +18,39 @@ void wlr_allocator_init(struct wlr_allocator *alloc, wl_signal_init(&alloc->events.destroy); } +struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend, + struct wlr_renderer *renderer) { + uint32_t backend_caps = backend_get_buffer_caps(backend); + uint32_t renderer_caps = renderer_get_render_buffer_caps(renderer); + int drm_fd = wlr_backend_get_drm_fd(backend); + + struct wlr_allocator *alloc = NULL; + uint32_t gbm_caps = WLR_BUFFER_CAP_DMABUF; + if ((backend_caps & gbm_caps) && (renderer_caps & gbm_caps) + && drm_fd != -1) { + wlr_log(WLR_DEBUG, "Trying to create gbm allocator"); + int fd = fcntl(drm_fd, F_DUPFD_CLOEXEC, 0); + if (fd < 0) { + wlr_log(WLR_ERROR, "fcntl(F_DUPFD_CLOEXEC) failed"); + } else if ((alloc = wlr_gbm_allocator_create(fd)) != NULL) { + return alloc; + } + wlr_log(WLR_DEBUG, "Failed to create gbm allocator"); + } + + uint32_t shm_caps = WLR_BUFFER_CAP_SHM | WLR_BUFFER_CAP_DATA_PTR; + if ((backend_caps & shm_caps) && (renderer_caps & shm_caps)) { + wlr_log(WLR_DEBUG, "Trying to create shm allocator"); + if ((alloc = wlr_shm_allocator_create()) != NULL) { + return alloc; + } + wlr_log(WLR_DEBUG, "Failed to create shm allocator"); + } + + wlr_log(WLR_ERROR, "Failed to create allocator"); + return NULL; +} + void wlr_allocator_destroy(struct wlr_allocator *alloc) { if (alloc == NULL) { return; |