2021-02-09 19:57:47 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <drm_fourcc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <unistd.h>
|
2022-03-03 14:38:26 +00:00
|
|
|
#include <wlr/interfaces/wlr_buffer.h>
|
2021-11-09 14:42:22 +00:00
|
|
|
#include <wlr/render/allocator.h>
|
|
|
|
#include <wlr/render/drm_format_set.h>
|
2021-02-09 19:57:47 +00:00
|
|
|
#include <wlr/util/log.h>
|
2021-11-09 14:42:22 +00:00
|
|
|
|
2021-02-09 19:57:47 +00:00
|
|
|
#include "render/pixel_format.h"
|
2021-08-25 07:33:19 +00:00
|
|
|
#include "render/allocator/shm.h"
|
2021-02-09 19:57:47 +00:00
|
|
|
#include "util/shm.h"
|
|
|
|
|
|
|
|
static const struct wlr_buffer_impl buffer_impl;
|
|
|
|
|
|
|
|
static struct wlr_shm_buffer *shm_buffer_from_buffer(
|
|
|
|
struct wlr_buffer *wlr_buffer) {
|
|
|
|
assert(wlr_buffer->impl == &buffer_impl);
|
|
|
|
return (struct wlr_shm_buffer *)wlr_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_destroy(struct wlr_buffer *wlr_buffer) {
|
|
|
|
struct wlr_shm_buffer *buffer = shm_buffer_from_buffer(wlr_buffer);
|
|
|
|
munmap(buffer->data, buffer->size);
|
|
|
|
close(buffer->shm.fd);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool buffer_get_shm(struct wlr_buffer *wlr_buffer,
|
|
|
|
struct wlr_shm_attributes *shm) {
|
|
|
|
struct wlr_shm_buffer *buffer = shm_buffer_from_buffer(wlr_buffer);
|
|
|
|
memcpy(shm, &buffer->shm, sizeof(*shm));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-02 15:02:28 +00:00
|
|
|
static bool shm_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer,
|
2021-06-29 15:08:32 +00:00
|
|
|
uint32_t flags, void **data, uint32_t *format, size_t *stride) {
|
2021-02-09 19:57:47 +00:00
|
|
|
struct wlr_shm_buffer *buffer = shm_buffer_from_buffer(wlr_buffer);
|
|
|
|
*data = buffer->data;
|
2021-04-26 17:48:35 +00:00
|
|
|
*format = buffer->shm.format;
|
2021-02-09 19:57:47 +00:00
|
|
|
*stride = buffer->shm.stride;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-02 15:02:28 +00:00
|
|
|
static void shm_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
|
2021-02-09 19:57:47 +00:00
|
|
|
static const struct wlr_buffer_impl buffer_impl = {
|
|
|
|
.destroy = buffer_destroy,
|
|
|
|
.get_shm = buffer_get_shm,
|
2021-06-02 15:02:28 +00:00
|
|
|
.begin_data_ptr_access = shm_buffer_begin_data_ptr_access,
|
|
|
|
.end_data_ptr_access = shm_buffer_end_data_ptr_access,
|
2021-02-09 19:57:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct wlr_buffer *allocator_create_buffer(
|
|
|
|
struct wlr_allocator *wlr_allocator, int width, int height,
|
|
|
|
const struct wlr_drm_format *format) {
|
|
|
|
const struct wlr_pixel_format_info *info =
|
|
|
|
drm_get_pixel_format_info(format->format);
|
|
|
|
if (info == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Unsupported pixel format 0x%"PRIX32, format->format);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_shm_buffer *buffer = calloc(1, sizeof(*buffer));
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wlr_buffer_init(&buffer->base, &buffer_impl, width, height);
|
|
|
|
|
|
|
|
// TODO: consider using a single file for multiple buffers
|
2023-05-08 20:17:26 +00:00
|
|
|
int stride = pixel_format_info_min_stride(info, width); // TODO: align?
|
2021-02-09 19:57:47 +00:00
|
|
|
buffer->size = stride * height;
|
|
|
|
buffer->shm.fd = allocate_shm_file(buffer->size);
|
|
|
|
if (buffer->shm.fd < 0) {
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer->shm.format = format->format;
|
|
|
|
buffer->shm.width = width;
|
|
|
|
buffer->shm.height = height;
|
|
|
|
buffer->shm.stride = stride;
|
|
|
|
buffer->shm.offset = 0;
|
|
|
|
|
|
|
|
buffer->data = mmap(NULL, buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
|
|
buffer->shm.fd, 0);
|
|
|
|
if (buffer->data == MAP_FAILED) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "mmap failed");
|
|
|
|
close(buffer->shm.fd);
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &buffer->base;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void allocator_destroy(struct wlr_allocator *wlr_allocator) {
|
|
|
|
free(wlr_allocator);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wlr_allocator_interface allocator_impl = {
|
|
|
|
.destroy = allocator_destroy,
|
|
|
|
.create_buffer = allocator_create_buffer,
|
|
|
|
};
|
|
|
|
|
2021-04-22 01:29:33 +00:00
|
|
|
struct wlr_allocator *wlr_shm_allocator_create(void) {
|
2021-02-09 19:57:47 +00:00
|
|
|
struct wlr_shm_allocator *allocator = calloc(1, sizeof(*allocator));
|
|
|
|
if (allocator == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-05-31 18:16:32 +00:00
|
|
|
wlr_allocator_init(&allocator->base, &allocator_impl,
|
|
|
|
WLR_BUFFER_CAP_DATA_PTR | WLR_BUFFER_CAP_SHM);
|
2021-02-09 19:57:47 +00:00
|
|
|
|
2021-04-22 01:29:33 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Created shm allocator");
|
|
|
|
return &allocator->base;
|
2021-02-09 19:57:47 +00:00
|
|
|
}
|