backend: use wlr_allocator_autocreate

This commit is contained in:
Simon Zeni 2021-04-28 10:11:09 -04:00 committed by Simon Ser
parent 318e3ac92c
commit cc1b66364c
4 changed files with 54 additions and 109 deletions

View File

@ -14,7 +14,7 @@
#include "backend/drm/drm.h" #include "backend/drm/drm.h"
#include "backend/drm/util.h" #include "backend/drm/util.h"
#include "render/drm_format_set.h" #include "render/drm_format_set.h"
#include "render/gbm_allocator.h" #include "render/allocator.h"
#include "render/pixel_format.h" #include "render/pixel_format.h"
#include "render/swapchain.h" #include "render/swapchain.h"
#include "render/wlr_renderer.h" #include "render/wlr_renderer.h"
@ -35,16 +35,10 @@ bool init_drm_renderer(struct wlr_drm_backend *drm,
goto error_gbm; goto error_gbm;
} }
int alloc_fd = fcntl(drm->fd, F_DUPFD_CLOEXEC, 0); renderer->allocator = wlr_allocator_autocreate(&drm->backend,
if (alloc_fd < 0) { renderer->wlr_rend);
wlr_log_errno(WLR_ERROR, "fcntl(F_DUPFD_CLOEXEC) failed");
goto error_wlr_rend;
}
renderer->allocator = wlr_gbm_allocator_create(alloc_fd);
if (renderer->allocator == NULL) { if (renderer->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create allocator"); wlr_log(WLR_ERROR, "Failed to create allocator");
close(alloc_fd);
goto error_wlr_rend; goto error_wlr_rend;
} }

View File

@ -120,15 +120,12 @@ static void handle_renderer_destroy(struct wl_listener *listener, void *data) {
} }
static bool backend_init(struct wlr_headless_backend *backend, static bool backend_init(struct wlr_headless_backend *backend,
struct wl_display *display, struct wlr_allocator *allocator, struct wl_display *display, struct wlr_renderer *renderer) {
struct wlr_renderer *renderer) {
wlr_backend_init(&backend->backend, &backend_impl); wlr_backend_init(&backend->backend, &backend_impl);
backend->display = display; backend->display = display;
wl_list_init(&backend->outputs); wl_list_init(&backend->outputs);
wl_list_init(&backend->input_devices); wl_list_init(&backend->input_devices);
backend->allocator = allocator;
if (renderer == NULL) { if (renderer == NULL) {
renderer = wlr_renderer_autocreate(&backend->backend); renderer = wlr_renderer_autocreate(&backend->backend);
if (!renderer) { if (!renderer) {
@ -138,6 +135,12 @@ static bool backend_init(struct wlr_headless_backend *backend,
} }
backend->renderer = renderer; backend->renderer = renderer;
backend->allocator = wlr_allocator_autocreate(&backend->backend, renderer);
if (!backend->allocator) {
wlr_log(WLR_ERROR, "Failed to create allocator");
return false;
}
const struct wlr_drm_format_set *formats = const struct wlr_drm_format_set *formats =
wlr_renderer_get_render_formats(backend->renderer); wlr_renderer_get_render_formats(backend->renderer);
if (formats == NULL) { if (formats == NULL) {
@ -219,33 +222,16 @@ struct wlr_backend *wlr_headless_backend_create(struct wl_display *display) {
backend->drm_fd = open_drm_render_node(); backend->drm_fd = open_drm_render_node();
if (backend->drm_fd < 0) { if (backend->drm_fd < 0) {
wlr_log(WLR_ERROR, "Failed to open DRM render node"); wlr_log(WLR_ERROR, "Failed to open DRM render node");
goto error_drm_fd;
} }
int drm_fd = fcntl(backend->drm_fd, F_DUPFD_CLOEXEC, 0); if (!backend_init(backend, display, NULL)) {
if (drm_fd < 0) {
wlr_log_errno(WLR_ERROR, "fcntl(F_DUPFD_CLOEXEC) failed");
goto error_dup;
}
struct wlr_allocator *alloc = wlr_gbm_allocator_create(drm_fd);
if (alloc == NULL) {
wlr_log(WLR_ERROR, "Failed to create GBM allocator");
close(drm_fd);
goto error_dup;
}
if (!backend_init(backend, display, alloc, NULL)) {
goto error_init; goto error_init;
} }
return &backend->backend; return &backend->backend;
error_init: error_init:
wlr_allocator_destroy(alloc);
error_dup:
close(backend->drm_fd); close(backend->drm_fd);
error_drm_fd:
free(backend); free(backend);
return NULL; return NULL;
} }
@ -265,23 +251,9 @@ struct wlr_backend *wlr_headless_backend_create_with_renderer(
backend->drm_fd = wlr_renderer_get_drm_fd(renderer); backend->drm_fd = wlr_renderer_get_drm_fd(renderer);
if (backend->drm_fd < 0) { if (backend->drm_fd < 0) {
wlr_log(WLR_ERROR, "Failed to get DRM device FD from renderer"); wlr_log(WLR_ERROR, "Failed to get DRM device FD from renderer");
goto error_drm_fd;
} }
int drm_fd = fcntl(backend->drm_fd, F_DUPFD_CLOEXEC, 0); if (!backend_init(backend, display, renderer)) {
if (drm_fd < 0) {
wlr_log_errno(WLR_ERROR, "fcntl(F_DUPFD_CLOEXEC) failed");
goto error_dup;
}
struct wlr_allocator *alloc = wlr_gbm_allocator_create(drm_fd);
if (alloc == NULL) {
wlr_log(WLR_ERROR, "Failed to create GBM allocator");
close(drm_fd);
goto error_dup;
}
if (!backend_init(backend, display, alloc, renderer)) {
goto error_init; goto error_init;
} }
@ -291,10 +263,7 @@ struct wlr_backend *wlr_headless_backend_create_with_renderer(
return &backend->backend; return &backend->backend;
error_init: error_init:
wlr_allocator_destroy(alloc);
error_dup:
close(backend->drm_fd); close(backend->drm_fd);
error_drm_fd:
free(backend); free(backend);
return NULL; return NULL;
} }

View File

@ -444,7 +444,6 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
} }
wl_event_source_check(wl->remote_display_src); wl_event_source_check(wl->remote_display_src);
wl->drm_fd = -1;
if (wl->drm_render_name != NULL) { if (wl->drm_render_name != NULL) {
wlr_log(WLR_DEBUG, "Opening DRM render node %s", wl->drm_render_name); wlr_log(WLR_DEBUG, "Opening DRM render node %s", wl->drm_render_name);
wl->drm_fd = open(wl->drm_render_name, O_RDWR | O_NONBLOCK | O_CLOEXEC); wl->drm_fd = open(wl->drm_render_name, O_RDWR | O_NONBLOCK | O_CLOEXEC);
@ -453,45 +452,40 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
wl->drm_render_name); wl->drm_render_name);
goto error_remote_display_src; goto error_remote_display_src;
} }
int drm_fd = fcntl(wl->drm_fd, F_DUPFD_CLOEXEC, 0);
if (drm_fd < 0) {
wlr_log(WLR_ERROR, "fcntl(F_DUPFD_CLOEXEC) failed");
goto error_drm_fd;
}
wl->allocator = wlr_gbm_allocator_create(drm_fd);
if (wl->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create GBM allocator");
close(drm_fd);
goto error_drm_fd;
}
} else { } else {
wlr_log(WLR_DEBUG, "No render node found, falling back to shared memory"); wl->drm_fd = -1;
wl->allocator = wlr_shm_allocator_create();
if (wl->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create shared memory allocator");
goto error_remote_display_src;
}
} }
wl->renderer = wlr_renderer_autocreate(&wl->backend); wl->renderer = wlr_renderer_autocreate(&wl->backend);
if (wl->renderer == NULL) { if (wl->renderer == NULL) {
wlr_log(WLR_ERROR, "Failed to create renderer"); wlr_log(WLR_ERROR, "Failed to create renderer");
goto error_renderer;
}
uint32_t caps = renderer_get_render_buffer_caps(wl->renderer);
wl->allocator = wlr_allocator_autocreate(&wl->backend, wl->renderer);
if (wl->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create allocator");
goto error_allocator; goto error_allocator;
} }
const struct wlr_drm_format_set *remote_formats; const struct wlr_drm_format_set *remote_formats;
if (wl->drm_fd >= 0) { if ((caps & WLR_BUFFER_CAP_DMABUF) && wl->zwp_linux_dmabuf_v1) {
remote_formats = &wl->linux_dmabuf_v1_formats; remote_formats = &wl->linux_dmabuf_v1_formats;
} else { } else if ((caps & WLR_BUFFER_CAP_DATA_PTR) && wl->shm) {
remote_formats = &wl->shm_formats; remote_formats = &wl->shm_formats;
} else {
wlr_log(WLR_ERROR,
"Failed to get remote formats (DRI3 and SHM unavailable)");
goto error_allocator;
} }
const struct wlr_drm_format_set *render_formats = const struct wlr_drm_format_set *render_formats =
wlr_renderer_get_render_formats(wl->renderer); wlr_renderer_get_render_formats(wl->renderer);
if (render_formats == NULL) { if (render_formats == NULL) {
wlr_log(WLR_ERROR, "Failed to get available render-capable formats"); wlr_log(WLR_ERROR, "Failed to get available render-capable formats");
goto error_renderer; goto error_allocator;
} }
uint32_t fmt = DRM_FORMAT_ARGB8888; uint32_t fmt = DRM_FORMAT_ARGB8888;
@ -501,21 +495,21 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
if (remote_format == NULL) { if (remote_format == NULL) {
wlr_log(WLR_ERROR, "Remote compositor doesn't support DRM format " wlr_log(WLR_ERROR, "Remote compositor doesn't support DRM format "
"0x%"PRIX32, fmt); "0x%"PRIX32, fmt);
goto error_renderer; goto error_allocator;
} }
const struct wlr_drm_format *render_format = const struct wlr_drm_format *render_format =
wlr_drm_format_set_get(render_formats, fmt); wlr_drm_format_set_get(render_formats, fmt);
if (render_format == NULL) { if (render_format == NULL) {
wlr_log(WLR_ERROR, "Renderer doesn't support DRM format 0x%"PRIX32, fmt); wlr_log(WLR_ERROR, "Renderer doesn't support DRM format 0x%"PRIX32, fmt);
goto error_renderer; goto error_allocator;
} }
wl->format = wlr_drm_format_intersect(remote_format, render_format); wl->format = wlr_drm_format_intersect(remote_format, render_format);
if (wl->format == NULL) { if (wl->format == NULL) {
wlr_log(WLR_ERROR, "Failed to intersect remote and render modifiers " wlr_log(WLR_ERROR, "Failed to intersect remote and render modifiers "
"for format 0x%"PRIX32, fmt); "for format 0x%"PRIX32, fmt);
goto error_renderer; goto error_allocator;
} }
wl->local_display_destroy.notify = handle_display_destroy; wl->local_display_destroy.notify = handle_display_destroy;
@ -523,11 +517,10 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_display *display,
return &wl->backend; return &wl->backend;
error_renderer:
wlr_renderer_destroy(wl->renderer);
error_allocator: error_allocator:
wlr_allocator_destroy(wl->allocator); wlr_allocator_destroy(wl->allocator);
error_drm_fd: error_renderer:
wlr_renderer_destroy(wl->renderer);
close(wl->drm_fd); close(wl->drm_fd);
error_remote_display_src: error_remote_display_src:
wl_event_source_remove(wl->remote_display_src); wl_event_source_remove(wl->remote_display_src);

View File

@ -610,7 +610,7 @@ struct wlr_backend *wlr_x11_backend_create(struct wl_display *display,
return false; return false;
} }
const struct wlr_drm_format_set *pixmap_formats; x11->drm_fd = -1;
if (x11->have_dri3) { if (x11->have_dri3) {
// DRI3 may return a render node (Xwayland) or an authenticated primary // DRI3 may return a render node (Xwayland) or an authenticated primary
// node (plain Glamor). // node (plain Glamor).
@ -619,31 +619,20 @@ struct wlr_backend *wlr_x11_backend_create(struct wl_display *display,
wlr_log(WLR_ERROR, "Failed to query DRI3 DRM FD"); wlr_log(WLR_ERROR, "Failed to query DRI3 DRM FD");
goto error_event; goto error_event;
} }
}
char *drm_name = drmGetDeviceNameFromFd2(x11->drm_fd); x11->renderer = wlr_renderer_autocreate(&x11->backend);
wlr_log(WLR_DEBUG, "Using DRM node %s", drm_name); if (x11->renderer == NULL) {
free(drm_name); wlr_log(WLR_ERROR, "Failed to create renderer");
int drm_fd = fcntl(x11->drm_fd, F_DUPFD_CLOEXEC, 0);
if (drm_fd < 0) {
wlr_log(WLR_ERROR, "fcntl(F_DUPFD_CLOEXEC) failed");
goto error_event; goto error_event;
} }
x11->allocator = wlr_gbm_allocator_create(drm_fd); uint32_t caps = renderer_get_render_buffer_caps(x11->renderer);
if (x11->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create GBM allocator"); const struct wlr_drm_format_set *pixmap_formats;
close(drm_fd); if (x11->have_dri3 && (caps & WLR_BUFFER_CAP_DMABUF)) {
goto error_event;
}
pixmap_formats = &x11->dri3_formats; pixmap_formats = &x11->dri3_formats;
} else if (x11->have_shm) { } else if (x11->have_shm && (caps & WLR_BUFFER_CAP_DATA_PTR)) {
x11->drm_fd = -1;
x11->allocator = wlr_shm_allocator_create();
if (x11->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create shared memory allocator");
goto error_event;
}
pixmap_formats = &x11->shm_formats; pixmap_formats = &x11->shm_formats;
} else { } else {
wlr_log(WLR_ERROR, wlr_log(WLR_ERROR,
@ -651,9 +640,9 @@ struct wlr_backend *wlr_x11_backend_create(struct wl_display *display,
goto error_event; goto error_event;
} }
x11->renderer = wlr_renderer_autocreate(&x11->backend); x11->allocator = wlr_allocator_autocreate(&x11->backend, x11->renderer);
if (x11->renderer == NULL) { if (x11->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create renderer"); wlr_log(WLR_ERROR, "Failed to create allocator");
goto error_event; goto error_event;
} }