2018-06-14 09:51:38 +02:00
|
|
|
#include <assert.h>
|
2021-07-09 16:55:37 +02:00
|
|
|
#include <drm_fourcc.h>
|
2018-06-14 09:51:38 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wlr/render/wlr_renderer.h>
|
2018-07-19 22:50:00 +02:00
|
|
|
#include <wlr/types/wlr_buffer.h>
|
2021-06-30 15:43:32 +02:00
|
|
|
#include <wlr/types/wlr_drm.h>
|
2018-07-19 22:50:00 +02:00
|
|
|
#include <wlr/types/wlr_linux_dmabuf_v1.h>
|
2018-06-14 09:51:38 +02:00
|
|
|
#include <wlr/util/log.h>
|
2021-03-15 17:35:47 +01:00
|
|
|
#include "render/pixel_format.h"
|
2021-02-17 02:22:28 +01:00
|
|
|
#include "types/wlr_buffer.h"
|
2020-02-28 13:14:06 +01:00
|
|
|
#include "util/signal.h"
|
2018-06-14 09:51:38 +02:00
|
|
|
|
2020-02-28 12:32:43 +01:00
|
|
|
void wlr_buffer_init(struct wlr_buffer *buffer,
|
2020-02-28 15:25:35 +01:00
|
|
|
const struct wlr_buffer_impl *impl, int width, int height) {
|
2020-02-28 12:32:43 +01:00
|
|
|
assert(impl->destroy);
|
2021-06-02 17:02:28 +02:00
|
|
|
if (impl->begin_data_ptr_access || impl->end_data_ptr_access) {
|
|
|
|
assert(impl->begin_data_ptr_access && impl->end_data_ptr_access);
|
|
|
|
}
|
2020-02-28 12:32:43 +01:00
|
|
|
buffer->impl = impl;
|
2020-02-28 15:25:35 +01:00
|
|
|
buffer->width = width;
|
|
|
|
buffer->height = height;
|
2020-02-28 13:14:06 +01:00
|
|
|
wl_signal_init(&buffer->events.destroy);
|
2020-02-28 13:25:05 +01:00
|
|
|
wl_signal_init(&buffer->events.release);
|
2021-08-11 10:00:07 +02:00
|
|
|
wlr_addon_set_init(&buffer->addons);
|
2020-02-28 12:32:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-28 13:25:05 +01:00
|
|
|
static void buffer_consider_destroy(struct wlr_buffer *buffer) {
|
|
|
|
if (!buffer->dropped || buffer->n_locks > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-02 17:02:28 +02:00
|
|
|
assert(!buffer->accessing_data_ptr);
|
|
|
|
|
2020-02-28 13:25:05 +01:00
|
|
|
wlr_signal_emit_safe(&buffer->events.destroy, NULL);
|
2021-08-11 10:00:07 +02:00
|
|
|
wlr_addon_set_finish(&buffer->addons);
|
2020-02-28 13:25:05 +01:00
|
|
|
|
|
|
|
buffer->impl->destroy(buffer);
|
2020-02-28 12:32:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-28 13:25:05 +01:00
|
|
|
void wlr_buffer_drop(struct wlr_buffer *buffer) {
|
2020-02-28 12:32:43 +01:00
|
|
|
if (buffer == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:25:05 +01:00
|
|
|
assert(!buffer->dropped);
|
|
|
|
buffer->dropped = true;
|
|
|
|
buffer_consider_destroy(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_buffer *wlr_buffer_lock(struct wlr_buffer *buffer) {
|
|
|
|
buffer->n_locks++;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_buffer_unlock(struct wlr_buffer *buffer) {
|
|
|
|
if (buffer == NULL) {
|
2020-02-28 12:32:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:25:05 +01:00
|
|
|
assert(buffer->n_locks > 0);
|
|
|
|
buffer->n_locks--;
|
2020-02-28 13:14:06 +01:00
|
|
|
|
2020-02-28 13:25:05 +01:00
|
|
|
if (buffer->n_locks == 0) {
|
|
|
|
wl_signal_emit(&buffer->events.release, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_consider_destroy(buffer);
|
2020-02-28 12:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_buffer_get_dmabuf(struct wlr_buffer *buffer,
|
|
|
|
struct wlr_dmabuf_attributes *attribs) {
|
|
|
|
if (!buffer->impl->get_dmabuf) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return buffer->impl->get_dmabuf(buffer, attribs);
|
|
|
|
}
|
|
|
|
|
2021-06-02 17:02:28 +02:00
|
|
|
bool buffer_begin_data_ptr_access(struct wlr_buffer *buffer, void **data,
|
2021-04-26 19:48:35 +02:00
|
|
|
uint32_t *format, size_t *stride) {
|
2021-06-02 17:02:28 +02:00
|
|
|
assert(!buffer->accessing_data_ptr);
|
|
|
|
if (!buffer->impl->begin_data_ptr_access) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!buffer->impl->begin_data_ptr_access(buffer, data, format, stride)) {
|
2021-02-17 02:22:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-02 17:02:28 +02:00
|
|
|
buffer->accessing_data_ptr = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void buffer_end_data_ptr_access(struct wlr_buffer *buffer) {
|
|
|
|
assert(buffer->accessing_data_ptr);
|
|
|
|
buffer->impl->end_data_ptr_access(buffer);
|
|
|
|
buffer->accessing_data_ptr = false;
|
2021-02-17 02:22:28 +01:00
|
|
|
}
|
2020-02-28 12:32:43 +01:00
|
|
|
|
2021-02-09 21:07:10 +01:00
|
|
|
bool wlr_buffer_get_shm(struct wlr_buffer *buffer,
|
|
|
|
struct wlr_shm_attributes *attribs) {
|
|
|
|
if (!buffer->impl->get_shm) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return buffer->impl->get_shm(buffer, attribs);
|
|
|
|
}
|
|
|
|
|
2018-06-14 09:51:38 +02:00
|
|
|
bool wlr_resource_is_buffer(struct wl_resource *resource) {
|
|
|
|
return strcmp(wl_resource_get_class(resource), wl_buffer_interface.name) == 0;
|
|
|
|
}
|
|
|
|
|
2020-02-28 12:32:43 +01:00
|
|
|
static const struct wlr_buffer_impl client_buffer_impl;
|
|
|
|
|
2020-07-28 10:46:46 +02:00
|
|
|
struct wlr_client_buffer *wlr_client_buffer_get(struct wlr_buffer *buffer) {
|
|
|
|
if (buffer->impl != &client_buffer_impl) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return (struct wlr_client_buffer *)buffer;
|
|
|
|
}
|
|
|
|
|
2020-02-28 12:32:43 +01:00
|
|
|
static struct wlr_client_buffer *client_buffer_from_buffer(
|
|
|
|
struct wlr_buffer *buffer) {
|
2020-07-28 10:46:46 +02:00
|
|
|
struct wlr_client_buffer *client_buffer = wlr_client_buffer_get(buffer);
|
|
|
|
assert(client_buffer != NULL);
|
|
|
|
return client_buffer;
|
2020-02-28 12:32:43 +01:00
|
|
|
}
|
|
|
|
|
2021-07-09 16:41:27 +02:00
|
|
|
static void client_buffer_destroy(struct wlr_buffer *buffer) {
|
|
|
|
struct wlr_client_buffer *client_buffer = client_buffer_from_buffer(buffer);
|
2021-07-09 17:06:28 +02:00
|
|
|
wl_list_remove(&client_buffer->source_destroy.link);
|
2021-07-09 16:41:27 +02:00
|
|
|
wlr_texture_destroy(client_buffer->texture);
|
|
|
|
free(client_buffer);
|
2020-02-28 12:32:43 +01:00
|
|
|
}
|
2018-06-14 09:51:38 +02:00
|
|
|
|
2021-07-09 16:41:27 +02:00
|
|
|
static bool client_buffer_get_dmabuf(struct wlr_buffer *buffer,
|
2020-02-28 12:32:43 +01:00
|
|
|
struct wlr_dmabuf_attributes *attribs) {
|
2021-07-09 16:41:27 +02:00
|
|
|
struct wlr_client_buffer *client_buffer = client_buffer_from_buffer(buffer);
|
2020-02-28 12:32:43 +01:00
|
|
|
|
2021-07-09 17:06:56 +02:00
|
|
|
if (client_buffer->source == NULL) {
|
2020-02-28 12:32:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-09 17:06:56 +02:00
|
|
|
return wlr_buffer_get_dmabuf(client_buffer->source, attribs);
|
2020-02-28 12:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wlr_buffer_impl client_buffer_impl = {
|
|
|
|
.destroy = client_buffer_destroy,
|
|
|
|
.get_dmabuf = client_buffer_get_dmabuf,
|
|
|
|
};
|
|
|
|
|
2021-07-09 17:06:28 +02:00
|
|
|
static void client_buffer_handle_source_destroy(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct wlr_client_buffer *client_buffer =
|
|
|
|
wl_container_of(listener, client_buffer, source_destroy);
|
|
|
|
wl_list_remove(&client_buffer->source_destroy.link);
|
|
|
|
wl_list_init(&client_buffer->source_destroy.link);
|
|
|
|
client_buffer->source = NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:04:38 +02:00
|
|
|
static struct wlr_shm_client_buffer *shm_client_buffer_get_or_create(
|
|
|
|
struct wl_resource *resource);
|
2021-07-09 16:55:37 +02:00
|
|
|
static bool buffer_is_shm_client_buffer(struct wlr_buffer *buffer);
|
|
|
|
static struct wlr_shm_client_buffer *shm_client_buffer_from_buffer(
|
|
|
|
struct wlr_buffer *buffer);
|
|
|
|
|
2021-07-12 17:50:34 +02:00
|
|
|
/* struct wlr_buffer_resource_interface */
|
|
|
|
static struct wl_array buffer_resource_interfaces = {0};
|
|
|
|
|
|
|
|
void wlr_buffer_register_resource_interface(
|
|
|
|
const struct wlr_buffer_resource_interface *iface) {
|
|
|
|
assert(iface);
|
|
|
|
assert(iface->is_instance);
|
|
|
|
assert(iface->from_resource);
|
|
|
|
|
|
|
|
const struct wlr_buffer_resource_interface **iface_ptr;
|
|
|
|
wl_array_for_each(iface_ptr, &buffer_resource_interfaces) {
|
|
|
|
if (*iface_ptr == iface) {
|
|
|
|
wlr_log(WLR_DEBUG, "wlr_resource_buffer_interface %s has already"
|
|
|
|
"been registered", iface->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
iface_ptr = wl_array_add(&buffer_resource_interfaces, sizeof(iface));
|
|
|
|
*iface_ptr = iface;
|
|
|
|
}
|
|
|
|
|
2021-07-14 22:35:14 +02:00
|
|
|
static const struct wlr_buffer_resource_interface *get_buffer_resource_iface(
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
struct wlr_buffer_resource_interface **iface_ptr;
|
|
|
|
wl_array_for_each(iface_ptr, &buffer_resource_interfaces) {
|
|
|
|
if ((*iface_ptr)->is_instance(resource)) {
|
|
|
|
return *iface_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-12 17:39:26 +02:00
|
|
|
struct wlr_buffer *wlr_buffer_from_resource(struct wl_resource *resource) {
|
2021-07-06 23:02:04 +02:00
|
|
|
assert(resource && wlr_resource_is_buffer(resource));
|
2018-06-14 09:51:38 +02:00
|
|
|
|
2021-06-08 19:49:22 +02:00
|
|
|
struct wlr_buffer *buffer;
|
2021-04-14 20:38:17 +02:00
|
|
|
if (wl_shm_buffer_get(resource) != NULL) {
|
|
|
|
struct wlr_shm_client_buffer *shm_client_buffer =
|
2021-07-19 19:04:38 +02:00
|
|
|
shm_client_buffer_get_or_create(resource);
|
2021-04-14 20:38:17 +02:00
|
|
|
if (shm_client_buffer == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create shm client buffer");
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-08 19:49:22 +02:00
|
|
|
buffer = wlr_buffer_lock(&shm_client_buffer->base);
|
2018-07-19 22:50:00 +02:00
|
|
|
} else if (wlr_dmabuf_v1_resource_is_buffer(resource)) {
|
|
|
|
struct wlr_dmabuf_v1_buffer *dmabuf =
|
|
|
|
wlr_dmabuf_v1_buffer_from_buffer_resource(resource);
|
2021-06-08 19:49:22 +02:00
|
|
|
buffer = wlr_buffer_lock(&dmabuf->base);
|
2021-06-30 15:43:32 +02:00
|
|
|
} else if (wlr_drm_buffer_is_resource(resource)) {
|
|
|
|
struct wlr_drm_buffer *drm_buffer =
|
|
|
|
wlr_drm_buffer_from_resource(resource);
|
2021-06-08 19:49:22 +02:00
|
|
|
buffer = wlr_buffer_lock(&drm_buffer->base);
|
2018-06-14 09:51:38 +02:00
|
|
|
} else {
|
2021-07-14 22:35:14 +02:00
|
|
|
const struct wlr_buffer_resource_interface *iface =
|
|
|
|
get_buffer_resource_iface(resource);
|
|
|
|
if (!iface) {
|
|
|
|
wlr_log(WLR_ERROR, "Unknown buffer type");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_buffer *custom_buffer = iface->from_resource(resource);
|
|
|
|
if (!custom_buffer) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create %s buffer", iface->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = wlr_buffer_lock(custom_buffer);
|
2018-06-14 09:51:38 +02:00
|
|
|
}
|
|
|
|
|
2021-07-06 23:02:04 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_client_buffer *wlr_client_buffer_create(struct wlr_buffer *buffer,
|
2021-07-09 23:27:07 +02:00
|
|
|
struct wlr_renderer *renderer) {
|
2021-06-08 19:49:22 +02:00
|
|
|
struct wlr_texture *texture = wlr_texture_from_buffer(renderer, buffer);
|
2018-06-14 09:51:38 +02:00
|
|
|
if (texture == NULL) {
|
2021-06-08 19:49:22 +02:00
|
|
|
wlr_log(WLR_ERROR, "Failed to create texture");
|
2018-06-14 09:51:38 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-06-08 19:49:22 +02:00
|
|
|
struct wlr_client_buffer *client_buffer =
|
2020-02-28 12:32:43 +01:00
|
|
|
calloc(1, sizeof(struct wlr_client_buffer));
|
2021-06-08 19:49:22 +02:00
|
|
|
if (client_buffer == NULL) {
|
2018-06-16 20:01:13 +02:00
|
|
|
wlr_texture_destroy(texture);
|
2018-06-14 09:51:38 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-08 19:49:22 +02:00
|
|
|
wlr_buffer_init(&client_buffer->base, &client_buffer_impl,
|
2020-12-25 12:14:31 +01:00
|
|
|
texture->width, texture->height);
|
2021-07-09 17:06:28 +02:00
|
|
|
client_buffer->source = buffer;
|
2021-06-08 19:49:22 +02:00
|
|
|
client_buffer->texture = texture;
|
2018-06-14 09:51:38 +02:00
|
|
|
|
2021-07-09 17:06:28 +02:00
|
|
|
wl_signal_add(&buffer->events.destroy, &client_buffer->source_destroy);
|
|
|
|
client_buffer->source_destroy.notify = client_buffer_handle_source_destroy;
|
|
|
|
|
2021-07-09 16:55:37 +02:00
|
|
|
if (buffer_is_shm_client_buffer(buffer)) {
|
|
|
|
struct wlr_shm_client_buffer *shm_client_buffer =
|
|
|
|
shm_client_buffer_from_buffer(buffer);
|
|
|
|
client_buffer->shm_source_format = shm_client_buffer->format;
|
|
|
|
} else {
|
|
|
|
client_buffer->shm_source_format = DRM_FORMAT_INVALID;
|
|
|
|
}
|
|
|
|
|
2020-02-28 13:25:05 +01:00
|
|
|
// Ensure the buffer will be released before being destroyed
|
2021-06-08 19:49:22 +02:00
|
|
|
wlr_buffer_lock(&client_buffer->base);
|
|
|
|
wlr_buffer_drop(&client_buffer->base);
|
2020-02-28 13:25:05 +01:00
|
|
|
|
2021-06-08 19:49:22 +02:00
|
|
|
return client_buffer;
|
2018-06-14 09:51:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:22:11 +02:00
|
|
|
bool wlr_client_buffer_apply_damage(struct wlr_client_buffer *client_buffer,
|
|
|
|
struct wlr_buffer *next, pixman_region32_t *damage) {
|
2021-07-09 16:41:27 +02:00
|
|
|
if (client_buffer->base.n_locks > 1) {
|
2018-06-14 09:51:38 +02:00
|
|
|
// Someone else still has a reference to the buffer
|
2021-08-11 17:22:11 +02:00
|
|
|
return false;
|
2018-06-14 09:51:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:18:41 +02:00
|
|
|
if ((uint32_t)next->width != client_buffer->texture->width ||
|
|
|
|
(uint32_t)next->height != client_buffer->texture->height) {
|
2021-08-11 17:22:11 +02:00
|
|
|
return false;
|
2021-08-11 17:18:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (client_buffer->shm_source_format == DRM_FORMAT_INVALID) {
|
2018-10-15 23:56:56 +02:00
|
|
|
// Uploading only damaged regions only works for wl_shm buffers and
|
|
|
|
// mutable textures (created from wl_shm buffer)
|
2021-08-11 17:22:11 +02:00
|
|
|
return false;
|
2018-10-15 23:56:56 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:18:41 +02:00
|
|
|
void *data;
|
|
|
|
uint32_t format;
|
|
|
|
size_t stride;
|
|
|
|
if (!buffer_begin_data_ptr_access(next, &data, &format, &stride)) {
|
2021-08-11 17:22:11 +02:00
|
|
|
return false;
|
2018-06-14 09:51:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 17:18:41 +02:00
|
|
|
if (format != client_buffer->shm_source_format) {
|
|
|
|
// Uploading to textures can't change the format
|
|
|
|
buffer_end_data_ptr_access(next);
|
2021-08-11 17:22:11 +02:00
|
|
|
return false;
|
2018-06-14 09:51:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int n;
|
|
|
|
pixman_box32_t *rects = pixman_region32_rectangles(damage, &n);
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
pixman_box32_t *r = &rects[i];
|
2021-07-09 16:41:27 +02:00
|
|
|
if (!wlr_texture_write_pixels(client_buffer->texture, stride,
|
2018-06-14 09:51:38 +02:00
|
|
|
r->x2 - r->x1, r->y2 - r->y1, r->x1, r->y1,
|
|
|
|
r->x1, r->y1, data)) {
|
2021-08-11 17:18:41 +02:00
|
|
|
buffer_end_data_ptr_access(next);
|
2021-08-11 17:22:11 +02:00
|
|
|
return false;
|
2018-06-14 09:51:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 17:18:41 +02:00
|
|
|
buffer_end_data_ptr_access(next);
|
2018-06-14 09:51:38 +02:00
|
|
|
|
2021-08-11 17:22:11 +02:00
|
|
|
return true;
|
2018-06-14 09:51:38 +02:00
|
|
|
}
|
2021-04-14 19:11:35 +02:00
|
|
|
|
|
|
|
static const struct wlr_buffer_impl shm_client_buffer_impl;
|
|
|
|
|
2021-07-09 16:55:37 +02:00
|
|
|
static bool buffer_is_shm_client_buffer(struct wlr_buffer *buffer) {
|
|
|
|
return buffer->impl == &shm_client_buffer_impl;
|
|
|
|
}
|
|
|
|
|
2021-04-14 19:11:35 +02:00
|
|
|
static struct wlr_shm_client_buffer *shm_client_buffer_from_buffer(
|
|
|
|
struct wlr_buffer *buffer) {
|
2021-07-09 16:55:37 +02:00
|
|
|
assert(buffer_is_shm_client_buffer(buffer));
|
2021-04-14 19:11:35 +02:00
|
|
|
return (struct wlr_shm_client_buffer *)buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shm_client_buffer_destroy(struct wlr_buffer *wlr_buffer) {
|
|
|
|
struct wlr_shm_client_buffer *buffer =
|
|
|
|
shm_client_buffer_from_buffer(wlr_buffer);
|
|
|
|
wl_list_remove(&buffer->resource_destroy.link);
|
|
|
|
wl_list_remove(&buffer->release.link);
|
|
|
|
if (buffer->saved_shm_pool != NULL) {
|
|
|
|
wl_shm_pool_unref(buffer->saved_shm_pool);
|
|
|
|
}
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool shm_client_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer,
|
|
|
|
void **data, uint32_t *format, size_t *stride) {
|
|
|
|
struct wlr_shm_client_buffer *buffer =
|
|
|
|
shm_client_buffer_from_buffer(wlr_buffer);
|
|
|
|
*format = buffer->format;
|
|
|
|
*stride = buffer->stride;
|
|
|
|
if (buffer->shm_buffer != NULL) {
|
|
|
|
*data = wl_shm_buffer_get_data(buffer->shm_buffer);
|
|
|
|
wl_shm_buffer_begin_access(buffer->shm_buffer);
|
|
|
|
} else {
|
|
|
|
*data = buffer->saved_data;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shm_client_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) {
|
|
|
|
struct wlr_shm_client_buffer *buffer =
|
|
|
|
shm_client_buffer_from_buffer(wlr_buffer);
|
|
|
|
if (buffer->shm_buffer != NULL) {
|
|
|
|
wl_shm_buffer_end_access(buffer->shm_buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wlr_buffer_impl shm_client_buffer_impl = {
|
|
|
|
.destroy = shm_client_buffer_destroy,
|
|
|
|
.begin_data_ptr_access = shm_client_buffer_begin_data_ptr_access,
|
|
|
|
.end_data_ptr_access = shm_client_buffer_end_data_ptr_access,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void shm_client_buffer_resource_handle_destroy(
|
|
|
|
struct wl_listener *listener, void *data) {
|
|
|
|
struct wlr_shm_client_buffer *buffer =
|
|
|
|
wl_container_of(listener, buffer, resource_destroy);
|
|
|
|
|
|
|
|
// In order to still be able to access the shared memory region, we need to
|
|
|
|
// keep a reference to the wl_shm_pool
|
|
|
|
buffer->saved_shm_pool = wl_shm_buffer_ref_pool(buffer->shm_buffer);
|
|
|
|
buffer->saved_data = wl_shm_buffer_get_data(buffer->shm_buffer);
|
|
|
|
|
|
|
|
// The wl_shm_buffer destroys itself with the wl_resource
|
|
|
|
buffer->resource = NULL;
|
|
|
|
buffer->shm_buffer = NULL;
|
|
|
|
wl_list_remove(&buffer->resource_destroy.link);
|
|
|
|
wl_list_init(&buffer->resource_destroy.link);
|
2021-07-19 19:04:38 +02:00
|
|
|
|
|
|
|
// This might destroy the buffer
|
|
|
|
wlr_buffer_drop(&buffer->base);
|
2021-04-14 19:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void shm_client_buffer_handle_release(struct wl_listener *listener,
|
|
|
|
void *data) {
|
|
|
|
struct wlr_shm_client_buffer *buffer =
|
|
|
|
wl_container_of(listener, buffer, release);
|
|
|
|
if (buffer->resource != NULL) {
|
|
|
|
wl_buffer_send_release(buffer->resource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:04:38 +02:00
|
|
|
static struct wlr_shm_client_buffer *shm_client_buffer_get_or_create(
|
2021-04-14 19:11:35 +02:00
|
|
|
struct wl_resource *resource) {
|
|
|
|
struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get(resource);
|
|
|
|
assert(shm_buffer != NULL);
|
|
|
|
|
2021-07-19 19:04:38 +02:00
|
|
|
struct wl_listener *resource_destroy_listener =
|
|
|
|
wl_resource_get_destroy_listener(resource,
|
|
|
|
shm_client_buffer_resource_handle_destroy);
|
|
|
|
if (resource_destroy_listener != NULL) {
|
|
|
|
struct wlr_shm_client_buffer *buffer =
|
|
|
|
wl_container_of(resource_destroy_listener, buffer, resource_destroy);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2021-04-14 19:11:35 +02:00
|
|
|
int32_t width = wl_shm_buffer_get_width(shm_buffer);
|
|
|
|
int32_t height = wl_shm_buffer_get_height(shm_buffer);
|
|
|
|
|
|
|
|
struct wlr_shm_client_buffer *buffer = calloc(1, sizeof(*buffer));
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wlr_buffer_init(&buffer->base, &shm_client_buffer_impl, width, height);
|
|
|
|
buffer->resource = resource;
|
|
|
|
buffer->shm_buffer = shm_buffer;
|
|
|
|
|
|
|
|
enum wl_shm_format wl_shm_format = wl_shm_buffer_get_format(shm_buffer);
|
|
|
|
buffer->format = convert_wl_shm_format_to_drm(wl_shm_format);
|
|
|
|
buffer->stride = wl_shm_buffer_get_stride(shm_buffer);
|
|
|
|
|
|
|
|
buffer->resource_destroy.notify = shm_client_buffer_resource_handle_destroy;
|
|
|
|
wl_resource_add_destroy_listener(resource, &buffer->resource_destroy);
|
|
|
|
|
|
|
|
buffer->release.notify = shm_client_buffer_handle_release;
|
|
|
|
wl_signal_add(&buffer->base.events.release, &buffer->release);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2021-06-29 20:11:38 +02:00
|
|
|
|
|
|
|
static const struct wlr_buffer_impl readonly_data_buffer_impl;
|
|
|
|
|
|
|
|
static struct wlr_readonly_data_buffer *readonly_data_buffer_from_buffer(
|
|
|
|
struct wlr_buffer *buffer) {
|
|
|
|
assert(buffer->impl == &readonly_data_buffer_impl);
|
|
|
|
return (struct wlr_readonly_data_buffer *)buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readonly_data_buffer_destroy(struct wlr_buffer *wlr_buffer) {
|
|
|
|
struct wlr_readonly_data_buffer *buffer =
|
|
|
|
readonly_data_buffer_from_buffer(wlr_buffer);
|
|
|
|
free(buffer->saved_data);
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool readonly_data_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer,
|
|
|
|
void **data, uint32_t *format, size_t *stride) {
|
|
|
|
struct wlr_readonly_data_buffer *buffer =
|
|
|
|
readonly_data_buffer_from_buffer(wlr_buffer);
|
|
|
|
if (buffer->data == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*data = (void*)buffer->data;
|
|
|
|
*format = buffer->format;
|
|
|
|
*stride = buffer->stride;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readonly_data_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) {
|
|
|
|
// This space is intentionally left blank
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wlr_buffer_impl readonly_data_buffer_impl = {
|
|
|
|
.destroy = readonly_data_buffer_destroy,
|
|
|
|
.begin_data_ptr_access = readonly_data_buffer_begin_data_ptr_access,
|
|
|
|
.end_data_ptr_access = readonly_data_buffer_end_data_ptr_access,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct wlr_readonly_data_buffer *readonly_data_buffer_create(uint32_t format,
|
|
|
|
size_t stride, uint32_t width, uint32_t height, const void *data) {
|
|
|
|
struct wlr_readonly_data_buffer *buffer = calloc(1, sizeof(*buffer));
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wlr_buffer_init(&buffer->base, &readonly_data_buffer_impl, width, height);
|
|
|
|
|
|
|
|
buffer->data = data;
|
|
|
|
buffer->format = format;
|
|
|
|
buffer->stride = stride;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool readonly_data_buffer_drop(struct wlr_readonly_data_buffer *buffer) {
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
if (buffer->base.n_locks > 0) {
|
|
|
|
size_t size = buffer->stride * buffer->base.height;
|
|
|
|
buffer->saved_data = malloc(size);
|
|
|
|
if (buffer->saved_data == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
ok = false;
|
|
|
|
buffer->data = NULL;
|
|
|
|
// We can't destroy the buffer, or we risk use-after-free in the
|
|
|
|
// consumers. We can't allow accesses to buffer->data anymore, so
|
|
|
|
// set it to NULL and make subsequent begin_data_ptr_access()
|
|
|
|
// calls fail.
|
|
|
|
} else {
|
|
|
|
memcpy(buffer->saved_data, buffer->data, size);
|
|
|
|
buffer->data = buffer->saved_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_buffer_drop(&buffer->base);
|
|
|
|
return ok;
|
|
|
|
}
|
2021-06-29 21:01:11 +02:00
|
|
|
|
|
|
|
static const struct wlr_buffer_impl dmabuf_buffer_impl;
|
|
|
|
|
|
|
|
static struct wlr_dmabuf_buffer *dmabuf_buffer_from_buffer(
|
|
|
|
struct wlr_buffer *buffer) {
|
|
|
|
assert(buffer->impl == &dmabuf_buffer_impl);
|
|
|
|
return (struct wlr_dmabuf_buffer *)buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dmabuf_buffer_destroy(struct wlr_buffer *wlr_buffer) {
|
|
|
|
struct wlr_dmabuf_buffer *buffer = dmabuf_buffer_from_buffer(wlr_buffer);
|
|
|
|
if (buffer->saved) {
|
|
|
|
wlr_dmabuf_attributes_finish(&buffer->dmabuf);
|
|
|
|
}
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool dmabuf_buffer_get_dmabuf(struct wlr_buffer *wlr_buffer,
|
|
|
|
struct wlr_dmabuf_attributes *dmabuf) {
|
|
|
|
struct wlr_dmabuf_buffer *buffer = dmabuf_buffer_from_buffer(wlr_buffer);
|
|
|
|
if (buffer->dmabuf.n_planes == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*dmabuf = buffer->dmabuf;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wlr_buffer_impl dmabuf_buffer_impl = {
|
|
|
|
.destroy = dmabuf_buffer_destroy,
|
|
|
|
.get_dmabuf = dmabuf_buffer_get_dmabuf,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct wlr_dmabuf_buffer *dmabuf_buffer_create(
|
|
|
|
struct wlr_dmabuf_attributes *dmabuf) {
|
|
|
|
struct wlr_dmabuf_buffer *buffer = calloc(1, sizeof(*buffer));
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wlr_buffer_init(&buffer->base, &dmabuf_buffer_impl,
|
|
|
|
dmabuf->width, dmabuf->height);
|
|
|
|
|
|
|
|
buffer->dmabuf = *dmabuf;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dmabuf_buffer_drop(struct wlr_dmabuf_buffer *buffer) {
|
|
|
|
bool ok = true;
|
|
|
|
|
|
|
|
if (buffer->base.n_locks > 0) {
|
|
|
|
struct wlr_dmabuf_attributes saved_dmabuf = {0};
|
|
|
|
if (!wlr_dmabuf_attributes_copy(&saved_dmabuf, &buffer->dmabuf)) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to save DMA-BUF");
|
|
|
|
ok = false;
|
|
|
|
memset(&buffer->dmabuf, 0, sizeof(buffer->dmabuf));
|
|
|
|
} else {
|
|
|
|
buffer->dmabuf = saved_dmabuf;
|
|
|
|
buffer->saved = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_buffer_drop(&buffer->base);
|
|
|
|
return ok;
|
|
|
|
}
|