2020-06-01 19:48:39 +02:00
|
|
|
#include <assert.h>
|
2021-09-01 19:05:18 +02:00
|
|
|
#include <fcntl.h>
|
2020-06-01 19:48:39 +02:00
|
|
|
#include <stdlib.h>
|
2021-09-01 19:05:18 +02:00
|
|
|
#include <unistd.h>
|
2022-05-29 14:09:06 +02:00
|
|
|
#include <wlr/config.h>
|
2022-03-03 15:38:26 +01:00
|
|
|
#include <wlr/interfaces/wlr_buffer.h>
|
2021-11-09 15:42:22 +01:00
|
|
|
#include <wlr/render/allocator.h>
|
2021-04-23 23:06:21 +02:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include <xf86drm.h>
|
2021-09-02 10:52:43 +02:00
|
|
|
#include <xf86drmMode.h>
|
2021-04-23 23:06:21 +02:00
|
|
|
#include "backend/backend.h"
|
2021-08-25 09:33:19 +02:00
|
|
|
#include "render/allocator/allocator.h"
|
|
|
|
#include "render/allocator/drm_dumb.h"
|
|
|
|
#include "render/allocator/shm.h"
|
2021-04-23 23:06:21 +02:00
|
|
|
#include "render/wlr_renderer.h"
|
2020-06-01 19:48:39 +02:00
|
|
|
|
2022-05-29 14:09:06 +02:00
|
|
|
#if WLR_HAS_GBM_ALLOCATOR
|
|
|
|
#include "render/allocator/gbm.h"
|
|
|
|
#endif
|
|
|
|
|
2020-06-01 19:48:39 +02:00
|
|
|
void wlr_allocator_init(struct wlr_allocator *alloc,
|
2021-05-31 20:16:32 +02:00
|
|
|
const struct wlr_allocator_interface *impl, uint32_t buffer_caps) {
|
2020-06-01 19:48:39 +02:00
|
|
|
assert(impl && impl->destroy && impl->create_buffer);
|
2023-07-07 14:34:56 +02:00
|
|
|
*alloc = (struct wlr_allocator){
|
|
|
|
.impl = impl,
|
|
|
|
.buffer_caps = buffer_caps,
|
|
|
|
};
|
2020-06-01 19:48:39 +02:00
|
|
|
wl_signal_init(&alloc->events.destroy);
|
|
|
|
}
|
|
|
|
|
2021-09-01 19:05:18 +02:00
|
|
|
/* Re-open the DRM node to avoid GEM handle ref'counting issues. See:
|
|
|
|
* https://gitlab.freedesktop.org/mesa/drm/-/merge_requests/110
|
|
|
|
*/
|
2021-10-02 17:16:57 +02:00
|
|
|
static int reopen_drm_node(int drm_fd, bool allow_render_node) {
|
2021-09-02 10:52:43 +02:00
|
|
|
if (drmIsMaster(drm_fd)) {
|
|
|
|
// Only recent kernels support empty leases
|
|
|
|
uint32_t lessee_id;
|
2022-03-13 12:28:55 +01:00
|
|
|
int lease_fd = drmModeCreateLease(drm_fd, NULL, 0, O_CLOEXEC, &lessee_id);
|
2021-09-02 10:52:43 +02:00
|
|
|
if (lease_fd >= 0) {
|
|
|
|
return lease_fd;
|
|
|
|
} else if (lease_fd != -EINVAL && lease_fd != -EOPNOTSUPP) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "drmModeCreateLease failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
wlr_log(WLR_DEBUG, "drmModeCreateLease failed, "
|
|
|
|
"falling back to plain open");
|
|
|
|
}
|
|
|
|
|
2021-10-02 17:16:57 +02:00
|
|
|
char *name = NULL;
|
|
|
|
if (allow_render_node) {
|
|
|
|
name = drmGetRenderDeviceNameFromFd(drm_fd);
|
|
|
|
}
|
2021-09-01 19:05:18 +02:00
|
|
|
if (name == NULL) {
|
2021-10-02 17:16:57 +02:00
|
|
|
// Either the DRM device has no render node, either the caller wants
|
|
|
|
// a primary node
|
|
|
|
name = drmGetDeviceNameFromFd2(drm_fd);
|
|
|
|
if (name == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "drmGetDeviceNameFromFd2 failed");
|
|
|
|
return -1;
|
|
|
|
}
|
2021-09-01 19:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int new_fd = open(name, O_RDWR | O_CLOEXEC);
|
|
|
|
if (new_fd < 0) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Failed to open DRM node '%s'", name);
|
2021-09-03 10:03:27 +02:00
|
|
|
free(name);
|
|
|
|
return -1;
|
2021-09-01 19:05:18 +02:00
|
|
|
}
|
2021-09-03 10:03:27 +02:00
|
|
|
|
2021-09-01 19:05:18 +02:00
|
|
|
free(name);
|
2021-09-03 10:03:27 +02:00
|
|
|
|
2023-06-19 11:43:04 +02:00
|
|
|
// If we're using a DRM primary node and we are DRM master (e.g. because
|
|
|
|
// we're running under the DRM backend), we need to use the legacy DRM
|
|
|
|
// authentication mechanism to have the permission to manipulate DRM dumb
|
|
|
|
// buffers.
|
|
|
|
if (drmIsMaster(drm_fd) && drmGetNodeTypeFromFd(new_fd) == DRM_NODE_PRIMARY) {
|
2021-09-03 10:03:27 +02:00
|
|
|
drm_magic_t magic;
|
|
|
|
if (drmGetMagic(new_fd, &magic) < 0) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "drmGetMagic failed");
|
|
|
|
close(new_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (drmAuthMagic(drm_fd, magic) < 0) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "drmAuthMagic failed");
|
|
|
|
close(new_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 19:05:18 +02:00
|
|
|
return new_fd;
|
|
|
|
}
|
|
|
|
|
2021-04-29 09:48:33 +02:00
|
|
|
struct wlr_allocator *allocator_autocreate_with_drm_fd(
|
2024-01-31 21:37:07 +01:00
|
|
|
uint32_t backend_caps, struct wlr_renderer *renderer,
|
2021-04-29 09:48:33 +02:00
|
|
|
int drm_fd) {
|
2021-04-23 23:06:21 +02:00
|
|
|
uint32_t renderer_caps = renderer_get_render_buffer_caps(renderer);
|
|
|
|
|
|
|
|
struct wlr_allocator *alloc = NULL;
|
2022-05-29 14:09:06 +02:00
|
|
|
|
2021-04-23 23:06:21 +02:00
|
|
|
uint32_t gbm_caps = WLR_BUFFER_CAP_DMABUF;
|
|
|
|
if ((backend_caps & gbm_caps) && (renderer_caps & gbm_caps)
|
2021-09-01 19:05:18 +02:00
|
|
|
&& drm_fd >= 0) {
|
2023-08-23 15:57:03 +02:00
|
|
|
#if WLR_HAS_GBM_ALLOCATOR
|
2021-04-23 23:06:21 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Trying to create gbm allocator");
|
2021-10-02 17:16:57 +02:00
|
|
|
int gbm_fd = reopen_drm_node(drm_fd, true);
|
2021-09-01 19:05:18 +02:00
|
|
|
if (gbm_fd < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((alloc = wlr_gbm_allocator_create(gbm_fd)) != NULL) {
|
2021-04-23 23:06:21 +02:00
|
|
|
return alloc;
|
|
|
|
}
|
2021-09-01 19:05:18 +02:00
|
|
|
close(gbm_fd);
|
2021-04-23 23:06:21 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Failed to create gbm allocator");
|
2023-08-23 15:57:03 +02:00
|
|
|
#else
|
|
|
|
wlr_log(WLR_DEBUG, "Skipping gbm allocator: disabled at compile-time");
|
2022-05-29 14:09:06 +02:00
|
|
|
#endif
|
2023-08-23 15:57:03 +02:00
|
|
|
}
|
2021-04-23 23:06:21 +02:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2021-01-28 04:10:59 +01:00
|
|
|
uint32_t drm_caps = WLR_BUFFER_CAP_DMABUF | WLR_BUFFER_CAP_DATA_PTR;
|
|
|
|
if ((backend_caps & drm_caps) && (renderer_caps & drm_caps)
|
2021-09-01 19:05:18 +02:00
|
|
|
&& drm_fd >= 0 && drmIsMaster(drm_fd)) {
|
2021-01-28 04:10:59 +01:00
|
|
|
wlr_log(WLR_DEBUG, "Trying to create drm dumb allocator");
|
2021-10-02 17:16:57 +02:00
|
|
|
int dumb_fd = reopen_drm_node(drm_fd, false);
|
2021-09-01 19:05:18 +02:00
|
|
|
if (dumb_fd < 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((alloc = wlr_drm_dumb_allocator_create(dumb_fd)) != NULL) {
|
2021-01-28 04:10:59 +01:00
|
|
|
return alloc;
|
|
|
|
}
|
2021-09-01 19:05:18 +02:00
|
|
|
close(dumb_fd);
|
2021-01-28 04:10:59 +01:00
|
|
|
wlr_log(WLR_DEBUG, "Failed to create drm dumb allocator");
|
|
|
|
}
|
|
|
|
|
2021-04-23 23:06:21 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to create allocator");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-29 09:48:33 +02:00
|
|
|
struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend,
|
|
|
|
struct wlr_renderer *renderer) {
|
2024-01-31 21:37:07 +01:00
|
|
|
uint32_t backend_caps = backend_get_buffer_caps(backend);
|
2021-04-29 09:48:33 +02:00
|
|
|
// Note, drm_fd may be negative if unavailable
|
|
|
|
int drm_fd = wlr_backend_get_drm_fd(backend);
|
2021-11-24 10:33:13 +01:00
|
|
|
if (drm_fd < 0) {
|
|
|
|
drm_fd = wlr_renderer_get_drm_fd(renderer);
|
|
|
|
}
|
2024-01-31 21:37:07 +01:00
|
|
|
|
|
|
|
return allocator_autocreate_with_drm_fd(backend_caps, renderer, drm_fd);
|
2021-04-29 09:48:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-01 19:48:39 +02:00
|
|
|
void wlr_allocator_destroy(struct wlr_allocator *alloc) {
|
2020-12-04 15:35:59 +01:00
|
|
|
if (alloc == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-18 13:16:16 +02:00
|
|
|
wl_signal_emit_mutable(&alloc->events.destroy, NULL);
|
2020-06-01 19:48:39 +02:00
|
|
|
alloc->impl->destroy(alloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_buffer *wlr_allocator_create_buffer(struct wlr_allocator *alloc,
|
|
|
|
int width, int height, const struct wlr_drm_format *format) {
|
2021-05-31 20:16:32 +02:00
|
|
|
struct wlr_buffer *buffer =
|
|
|
|
alloc->impl->create_buffer(alloc, width, height, format);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (alloc->buffer_caps & WLR_BUFFER_CAP_DATA_PTR) {
|
2021-06-02 17:02:28 +02:00
|
|
|
assert(buffer->impl->begin_data_ptr_access &&
|
|
|
|
buffer->impl->end_data_ptr_access);
|
2021-05-31 20:16:32 +02:00
|
|
|
}
|
|
|
|
if (alloc->buffer_caps & WLR_BUFFER_CAP_DMABUF) {
|
|
|
|
assert(buffer->impl->get_dmabuf);
|
|
|
|
}
|
|
|
|
if (alloc->buffer_caps & WLR_BUFFER_CAP_SHM) {
|
|
|
|
assert(buffer->impl->get_shm);
|
|
|
|
}
|
|
|
|
return buffer;
|
2020-06-01 19:48:39 +02:00
|
|
|
}
|