2020-06-01 17:48:39 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2021-04-23 21:06:21 +00:00
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include <xf86drm.h>
|
|
|
|
#include "backend/backend.h"
|
2021-08-25 07:33:19 +00:00
|
|
|
#include "render/allocator/allocator.h"
|
|
|
|
#include "render/allocator/drm_dumb.h"
|
|
|
|
#include "render/allocator/gbm.h"
|
|
|
|
#include "render/allocator/shm.h"
|
2021-04-23 21:06:21 +00:00
|
|
|
#include "render/wlr_renderer.h"
|
2020-06-01 17:48:39 +00:00
|
|
|
|
|
|
|
void wlr_allocator_init(struct wlr_allocator *alloc,
|
2021-05-31 18:16:32 +00:00
|
|
|
const struct wlr_allocator_interface *impl, uint32_t buffer_caps) {
|
2020-06-01 17:48:39 +00:00
|
|
|
assert(impl && impl->destroy && impl->create_buffer);
|
|
|
|
alloc->impl = impl;
|
2021-05-31 18:16:32 +00:00
|
|
|
alloc->buffer_caps = buffer_caps;
|
2020-06-01 17:48:39 +00:00
|
|
|
wl_signal_init(&alloc->events.destroy);
|
|
|
|
}
|
|
|
|
|
2021-04-29 07:48:33 +00:00
|
|
|
struct wlr_allocator *allocator_autocreate_with_drm_fd(
|
|
|
|
struct wlr_backend *backend, struct wlr_renderer *renderer,
|
|
|
|
int drm_fd) {
|
2021-04-23 21:06:21 +00:00
|
|
|
uint32_t backend_caps = backend_get_buffer_caps(backend);
|
|
|
|
uint32_t renderer_caps = renderer_get_render_buffer_caps(renderer);
|
|
|
|
|
|
|
|
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");
|
2021-04-29 16:01:28 +00:00
|
|
|
if ((alloc = wlr_gbm_allocator_create(drm_fd)) != NULL) {
|
2021-04-23 21:06:21 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2021-01-28 03:10:59 +00:00
|
|
|
uint32_t drm_caps = WLR_BUFFER_CAP_DMABUF | WLR_BUFFER_CAP_DATA_PTR;
|
|
|
|
if ((backend_caps & drm_caps) && (renderer_caps & drm_caps)
|
|
|
|
&& drm_fd != -1) {
|
|
|
|
wlr_log(WLR_DEBUG, "Trying to create drm dumb allocator");
|
|
|
|
if ((alloc = wlr_drm_dumb_allocator_create(drm_fd)) != NULL) {
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
wlr_log(WLR_DEBUG, "Failed to create drm dumb allocator");
|
|
|
|
}
|
|
|
|
|
2021-04-23 21:06:21 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to create allocator");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-29 07:48:33 +00:00
|
|
|
struct wlr_allocator *wlr_allocator_autocreate(struct wlr_backend *backend,
|
|
|
|
struct wlr_renderer *renderer) {
|
|
|
|
// Note, drm_fd may be negative if unavailable
|
|
|
|
int drm_fd = wlr_backend_get_drm_fd(backend);
|
|
|
|
return allocator_autocreate_with_drm_fd(backend, renderer, drm_fd);
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:48:39 +00:00
|
|
|
void wlr_allocator_destroy(struct wlr_allocator *alloc) {
|
2020-12-04 14:35:59 +00:00
|
|
|
if (alloc == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-01 17:48:39 +00:00
|
|
|
wl_signal_emit(&alloc->events.destroy, NULL);
|
|
|
|
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 18:16:32 +00: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 15:02:28 +00:00
|
|
|
assert(buffer->impl->begin_data_ptr_access &&
|
|
|
|
buffer->impl->end_data_ptr_access);
|
2021-05-31 18:16:32 +00: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 17:48:39 +00:00
|
|
|
}
|