Tested with
    ./weston-simple-dmabuf-drm
    ./weston-simple-dmabuf-drm --import-immediate=1
    ./weston-simple-dmabuf-drm --y-inverted=1
    (and combinations)
Supports only single plane XRGB dmabufs for now.
			
			
				master
			
			
		
							parent
							
								
									2d0db16942
								
							
						
					
					
						commit
						14cdb6153f
					
				@ -0,0 +1,84 @@
 | 
				
			||||
#ifndef WLR_TYPES_WLR_LINUX_DMABUF_H
 | 
				
			||||
#define WLR_TYPES_WLR_LINUX_DMABUF_H
 | 
				
			||||
 | 
				
			||||
#define WLR_LINUX_DMABUF_MAX_PLANES 4
 | 
				
			||||
 | 
				
			||||
#include <stdint.h>
 | 
				
			||||
#include <wayland-server-protocol.h>
 | 
				
			||||
 | 
				
			||||
/* So we don't have to pull in linux specific drm headers */
 | 
				
			||||
#ifndef DRM_FORMAT_MOD_INVALID
 | 
				
			||||
#define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1)
 | 
				
			||||
#endif
 | 
				
			||||
 | 
				
			||||
struct wlr_dmabuf_buffer_attribs {
 | 
				
			||||
	/* set via params_add */
 | 
				
			||||
	int n_planes;
 | 
				
			||||
	uint32_t offset[WLR_LINUX_DMABUF_MAX_PLANES];
 | 
				
			||||
	uint32_t stride[WLR_LINUX_DMABUF_MAX_PLANES];
 | 
				
			||||
	uint64_t modifier[WLR_LINUX_DMABUF_MAX_PLANES];
 | 
				
			||||
	int fd[WLR_LINUX_DMABUF_MAX_PLANES];
 | 
				
			||||
	/* set via params_create */
 | 
				
			||||
	int32_t width;
 | 
				
			||||
	int32_t height;
 | 
				
			||||
	uint32_t format;
 | 
				
			||||
	uint32_t flags; /* enum zlinux_buffer_params_flags */
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
struct wlr_dmabuf_buffer {
 | 
				
			||||
	struct wlr_egl *egl;
 | 
				
			||||
	struct wl_resource *buffer_resource;
 | 
				
			||||
	struct wl_resource *params_resource;
 | 
				
			||||
	struct wlr_dmabuf_buffer_attribs attributes;
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
/**
 | 
				
			||||
 * Returns true if the given resource was created via the linux-dmabuf
 | 
				
			||||
 * buffer protocol, false otherwise
 | 
				
			||||
 */
 | 
				
			||||
bool wlr_dmabuf_resource_is_buffer(struct wl_resource *buffer_resource);
 | 
				
			||||
 | 
				
			||||
/**
 | 
				
			||||
 * Returns the wlr_dmabuf_buffer if the given resource was created
 | 
				
			||||
 * via the linux-dmabuf buffer protocol
 | 
				
			||||
 */
 | 
				
			||||
struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_buffer_resource(
 | 
				
			||||
	struct wl_resource *buffer_resource);
 | 
				
			||||
 | 
				
			||||
/**
 | 
				
			||||
 * Returns the wlr_dmabuf_buffer if the given resource was created
 | 
				
			||||
 * via the linux-dmabuf params protocol
 | 
				
			||||
 */
 | 
				
			||||
struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_params_resource(
 | 
				
			||||
	struct wl_resource *params_resource);
 | 
				
			||||
 | 
				
			||||
/**
 | 
				
			||||
 * Returns true if the given dmabuf has y-axis inverted, false otherwise
 | 
				
			||||
 */
 | 
				
			||||
bool wlr_dmabuf_buffer_has_inverted_y(struct wlr_dmabuf_buffer *dmabuf);
 | 
				
			||||
 | 
				
			||||
/* the protocol interface */
 | 
				
			||||
struct wlr_linux_dmabuf {
 | 
				
			||||
	struct wl_global *wl_global;
 | 
				
			||||
	struct wl_listener display_destroy;
 | 
				
			||||
	struct wlr_egl *egl;
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
/**
 | 
				
			||||
 * Create linux-dmabuf interface
 | 
				
			||||
 */
 | 
				
			||||
struct wlr_linux_dmabuf *wlr_linux_dmabuf_create(struct wl_display *display,
 | 
				
			||||
	struct wlr_egl *egl);
 | 
				
			||||
/**
 | 
				
			||||
 * Destroy the linux-dmabuf interface
 | 
				
			||||
 */
 | 
				
			||||
void wlr_linux_dmabuf_destroy(struct wlr_linux_dmabuf *linux_dmabuf);
 | 
				
			||||
 | 
				
			||||
/**
 | 
				
			||||
 * Returns the wlr_linux_dmabuf if the given resource was created
 | 
				
			||||
 * via the linux_dmabuf protocol
 | 
				
			||||
 */
 | 
				
			||||
struct wlr_linux_dmabuf *wlr_linux_dmabuf_from_resource(
 | 
				
			||||
	struct wl_resource *resource);
 | 
				
			||||
 | 
				
			||||
#endif
 | 
				
			||||
@ -0,0 +1,464 @@
 | 
				
			||||
#ifndef _POSIX_C_SOURCE
 | 
				
			||||
#define _POSIX_C_SOURCE 200809L
 | 
				
			||||
#endif
 | 
				
			||||
#include <assert.h>
 | 
				
			||||
#include <stdlib.h>
 | 
				
			||||
#include <unistd.h>
 | 
				
			||||
#include <wayland-server.h>
 | 
				
			||||
#include <wlr/render.h>
 | 
				
			||||
#include <wlr/render/egl.h>
 | 
				
			||||
#include <wlr/types/wlr_linux_dmabuf.h>
 | 
				
			||||
#include <wlr/util/log.h>
 | 
				
			||||
#include "linux-dmabuf-unstable-v1-protocol.h"
 | 
				
			||||
 | 
				
			||||
static void wl_buffer_destroy(struct wl_client *client,
 | 
				
			||||
		struct wl_resource *resource) {
 | 
				
			||||
	wl_resource_destroy(resource);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static const struct wl_buffer_interface wl_buffer_impl = {
 | 
				
			||||
	wl_buffer_destroy,
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
bool wlr_dmabuf_buffer_has_inverted_y(struct wlr_dmabuf_buffer *dmabuf) {
 | 
				
			||||
	return dmabuf->attributes.flags
 | 
				
			||||
		& ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
bool wlr_dmabuf_resource_is_buffer(struct wl_resource *buffer_resource) {
 | 
				
			||||
	if (!wl_resource_instance_of(buffer_resource, &wl_buffer_interface,
 | 
				
			||||
		&wl_buffer_impl)) {
 | 
				
			||||
		return false;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(buffer_resource);
 | 
				
			||||
	if (buffer && buffer->buffer_resource && !buffer->params_resource &&
 | 
				
			||||
		buffer->buffer_resource == buffer_resource) {
 | 
				
			||||
		return true;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	return false;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_buffer_resource(
 | 
				
			||||
		struct wl_resource *buffer_resource) {
 | 
				
			||||
	assert(wl_resource_instance_of(buffer_resource, &wl_buffer_interface,
 | 
				
			||||
			&wl_buffer_impl));
 | 
				
			||||
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(buffer_resource);
 | 
				
			||||
	assert(buffer);
 | 
				
			||||
	assert(buffer->buffer_resource);
 | 
				
			||||
	assert(!buffer->params_resource);
 | 
				
			||||
	assert(buffer->buffer_resource == buffer_resource);
 | 
				
			||||
 | 
				
			||||
	return buffer;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void linux_dmabuf_buffer_destroy(struct wlr_dmabuf_buffer *buffer) {
 | 
				
			||||
	for (int i = 0; i < buffer->attributes.n_planes; i++) {
 | 
				
			||||
		close(buffer->attributes.fd[i]);
 | 
				
			||||
		buffer->attributes.fd[i] = -1;
 | 
				
			||||
	}
 | 
				
			||||
	buffer->attributes.n_planes = 0;
 | 
				
			||||
	free(buffer);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void params_destroy(struct wl_client *client, struct wl_resource *resource) {
 | 
				
			||||
	wl_resource_destroy(resource);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void params_add(struct wl_client *client,
 | 
				
			||||
		struct wl_resource *params_resource, int32_t name_fd,
 | 
				
			||||
		uint32_t plane_idx, uint32_t offset, uint32_t stride,
 | 
				
			||||
		uint32_t modifier_hi, uint32_t modifier_lo) {
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_params_resource(
 | 
				
			||||
		params_resource);
 | 
				
			||||
 | 
				
			||||
	if (!buffer) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED,
 | 
				
			||||
			"params was already used to create a wl_buffer");
 | 
				
			||||
		close(name_fd);
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	if (plane_idx >= WLR_LINUX_DMABUF_MAX_PLANES) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_IDX,
 | 
				
			||||
			"plane index %u > %u", plane_idx, WLR_LINUX_DMABUF_MAX_PLANES);
 | 
				
			||||
		close(name_fd);
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	if (buffer->attributes.fd[plane_idx] != -1) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_SET,
 | 
				
			||||
			"a dmabuf with id %d has already been added for plane %u",
 | 
				
			||||
			buffer->attributes.fd[plane_idx],
 | 
				
			||||
			plane_idx);
 | 
				
			||||
		close(name_fd);
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	buffer->attributes.fd[plane_idx] = name_fd;
 | 
				
			||||
	buffer->attributes.offset[plane_idx] = offset;
 | 
				
			||||
	buffer->attributes.stride[plane_idx] = stride;
 | 
				
			||||
	buffer->attributes.modifier[plane_idx] = ((uint64_t)modifier_hi << 32) |
 | 
				
			||||
		modifier_lo;
 | 
				
			||||
	buffer->attributes.n_planes++;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void handle_buffer_destroy(struct wl_resource *buffer_resource)
 | 
				
			||||
{
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_buffer_resource(
 | 
				
			||||
		buffer_resource);
 | 
				
			||||
 | 
				
			||||
	linux_dmabuf_buffer_destroy(buffer);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void params_create_common(struct wl_client *client,
 | 
				
			||||
		struct wl_resource *params_resource, uint32_t buffer_id, int32_t width,
 | 
				
			||||
		int32_t height, uint32_t format, uint32_t flags) {
 | 
				
			||||
	if (!wl_resource_get_user_data(params_resource)) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED,
 | 
				
			||||
			"params was already used to create a wl_buffer");
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_params_resource(
 | 
				
			||||
		params_resource);
 | 
				
			||||
 | 
				
			||||
	/* Switch the linux_dmabuf_buffer object from params resource to
 | 
				
			||||
	 * eventually wl_buffer resource. */
 | 
				
			||||
	wl_resource_set_user_data(buffer->params_resource, NULL);
 | 
				
			||||
	buffer->params_resource = NULL;
 | 
				
			||||
 | 
				
			||||
	if (!buffer->attributes.n_planes) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
 | 
				
			||||
			"no dmabuf has been added to the params");
 | 
				
			||||
		goto err_out;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	/* TODO: support more planes */
 | 
				
			||||
	if (buffer->attributes.n_planes != 1) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
 | 
				
			||||
			"only single plane buffers supported not %d",
 | 
				
			||||
			buffer->attributes.n_planes);
 | 
				
			||||
		goto err_out;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	if (buffer->attributes.fd[0] == -1) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE,
 | 
				
			||||
			"no dmabuf has been added for plane");
 | 
				
			||||
		goto err_out;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	buffer->attributes.width = width;
 | 
				
			||||
	buffer->attributes.height = height;
 | 
				
			||||
	buffer->attributes.format = format;
 | 
				
			||||
	buffer->attributes.flags = flags;
 | 
				
			||||
 | 
				
			||||
	if (width < 1 || height < 1) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_DIMENSIONS,
 | 
				
			||||
			"invalid width %d or height %d", width, height);
 | 
				
			||||
		goto err_out;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	if ((uint64_t)buffer->attributes.offset[0] + buffer->attributes.stride[0] > UINT32_MAX) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
 | 
				
			||||
			"size overflow for plane");
 | 
				
			||||
		goto err_out;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	if ((uint64_t)buffer->attributes.offset[0] +
 | 
				
			||||
			(uint64_t) buffer->attributes.stride[0] * height > UINT32_MAX) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
 | 
				
			||||
			"size overflow for plane");
 | 
				
			||||
		goto err_out;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	off_t size = lseek(buffer->attributes.fd[0], 0, SEEK_END);
 | 
				
			||||
	if (size != -1) { /* Skip checks if kernel does no support seek on buffer */
 | 
				
			||||
		if (buffer->attributes.offset[0] >= size) {
 | 
				
			||||
			wl_resource_post_error(params_resource,
 | 
				
			||||
				ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
 | 
				
			||||
				"invalid offset %i for plane",
 | 
				
			||||
				buffer->attributes.offset[0]);
 | 
				
			||||
			goto err_out;
 | 
				
			||||
		}
 | 
				
			||||
 | 
				
			||||
		if (buffer->attributes.offset[0] + buffer->attributes.stride[0] > size) {
 | 
				
			||||
			wl_resource_post_error(params_resource,
 | 
				
			||||
				ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
 | 
				
			||||
				"invalid stride %i for plane",
 | 
				
			||||
				buffer->attributes.stride[0]);
 | 
				
			||||
			goto err_out;
 | 
				
			||||
		}
 | 
				
			||||
 | 
				
			||||
		if (buffer->attributes.offset[0] + buffer->attributes.stride[0] * height > size) {
 | 
				
			||||
			wl_resource_post_error(params_resource,
 | 
				
			||||
				ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS,
 | 
				
			||||
				"invalid buffer stride or height for plane");
 | 
				
			||||
				goto err_out;
 | 
				
			||||
		}
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	/* reject unknown flags */
 | 
				
			||||
	if (buffer->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT) {
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
				ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_FORMAT,
 | 
				
			||||
				"Unknown dmabuf flags %"PRIu32, buffer->attributes.flags);
 | 
				
			||||
		goto err_out;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	/* Check if dmabuf is usable */
 | 
				
			||||
	if (!wlr_egl_check_import_dmabuf(buffer->egl, buffer)) {
 | 
				
			||||
		goto err_failed;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	buffer->buffer_resource = wl_resource_create(client, &wl_buffer_interface,
 | 
				
			||||
		1, buffer_id);
 | 
				
			||||
	if (!buffer->buffer_resource) {
 | 
				
			||||
		wl_resource_post_no_memory(params_resource);
 | 
				
			||||
		goto err_failed;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	wl_resource_set_implementation(buffer->buffer_resource,
 | 
				
			||||
		&wl_buffer_impl, buffer, handle_buffer_destroy);
 | 
				
			||||
 | 
				
			||||
	/* send 'created' event when the request is not for an immediate
 | 
				
			||||
	 * import, that is buffer_id is zero */
 | 
				
			||||
	if (buffer_id == 0) {
 | 
				
			||||
		zwp_linux_buffer_params_v1_send_created(params_resource,
 | 
				
			||||
			buffer->buffer_resource);
 | 
				
			||||
	}
 | 
				
			||||
	return;
 | 
				
			||||
 | 
				
			||||
err_failed:
 | 
				
			||||
	if (buffer_id == 0) {
 | 
				
			||||
		zwp_linux_buffer_params_v1_send_failed(params_resource);
 | 
				
			||||
	} else {
 | 
				
			||||
		/* since the behavior is left implementation defined by the
 | 
				
			||||
		 * protocol in case of create_immed failure due to an unknown cause,
 | 
				
			||||
		 * we choose to treat it as a fatal error and immediately kill the
 | 
				
			||||
		 * client instead of creating an invalid handle and waiting for it
 | 
				
			||||
		 * to be used.
 | 
				
			||||
		 */
 | 
				
			||||
		wl_resource_post_error(params_resource,
 | 
				
			||||
			ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_WL_BUFFER,
 | 
				
			||||
			"importing the supplied dmabufs failed");
 | 
				
			||||
	}
 | 
				
			||||
err_out:
 | 
				
			||||
	linux_dmabuf_buffer_destroy(buffer);
 | 
				
			||||
	return;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void params_create(struct wl_client *client,
 | 
				
			||||
		struct wl_resource *params_resource,
 | 
				
			||||
		int32_t width, int32_t height,uint32_t format, uint32_t flags) {
 | 
				
			||||
	params_create_common(client, params_resource, 0, width, height, format, flags);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void params_create_immed(struct wl_client *client,
 | 
				
			||||
		struct wl_resource *params_resource, uint32_t buffer_id,
 | 
				
			||||
		int32_t width, int32_t height,uint32_t format, uint32_t flags) {
 | 
				
			||||
	params_create_common(client, params_resource, buffer_id, width, height, format, flags);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static const struct zwp_linux_buffer_params_v1_interface linux_buffer_params_impl = {
 | 
				
			||||
	params_destroy,
 | 
				
			||||
	params_add,
 | 
				
			||||
	params_create,
 | 
				
			||||
	params_create_immed,
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_params_resource(
 | 
				
			||||
		struct wl_resource *params_resource) {
 | 
				
			||||
	assert(wl_resource_instance_of(params_resource,
 | 
				
			||||
		&zwp_linux_buffer_params_v1_interface,
 | 
				
			||||
		&linux_buffer_params_impl));
 | 
				
			||||
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(params_resource);
 | 
				
			||||
	assert(buffer);
 | 
				
			||||
	assert(buffer->params_resource);
 | 
				
			||||
	assert(!buffer->buffer_resource);
 | 
				
			||||
	assert(buffer->params_resource == params_resource);
 | 
				
			||||
 | 
				
			||||
	return buffer;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void handle_params_destroy(struct wl_resource *params_resource) {
 | 
				
			||||
	/* Check for NULL since wlr_dmabuf_buffer_from_params_resource will choke */
 | 
				
			||||
	if (!wl_resource_get_user_data(params_resource)) {
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer =
 | 
				
			||||
		wlr_dmabuf_buffer_from_params_resource(params_resource);
 | 
				
			||||
	linux_dmabuf_buffer_destroy(buffer);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void linux_dmabuf_create_params(struct wl_client *client,
 | 
				
			||||
		struct wl_resource *linux_dmabuf_resource,
 | 
				
			||||
		uint32_t params_id) {
 | 
				
			||||
	struct wlr_linux_dmabuf *linux_dmabuf = wlr_linux_dmabuf_from_resource(
 | 
				
			||||
		linux_dmabuf_resource);
 | 
				
			||||
 | 
				
			||||
	uint32_t version = wl_resource_get_version(linux_dmabuf_resource);
 | 
				
			||||
	struct wlr_dmabuf_buffer *buffer = calloc(1, sizeof *buffer);
 | 
				
			||||
	if (!buffer) {
 | 
				
			||||
		goto err;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	for (int i = 0; i < WLR_LINUX_DMABUF_MAX_PLANES; i++) {
 | 
				
			||||
		buffer->attributes.fd[i] = -1;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	buffer->egl = linux_dmabuf->egl;
 | 
				
			||||
	buffer->params_resource = wl_resource_create(client,
 | 
				
			||||
		&zwp_linux_buffer_params_v1_interface,
 | 
				
			||||
		version, params_id);
 | 
				
			||||
	if (!buffer->params_resource) {
 | 
				
			||||
		goto err_free;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	wl_resource_set_implementation(buffer->params_resource,
 | 
				
			||||
		&linux_buffer_params_impl,buffer, handle_params_destroy);
 | 
				
			||||
	return;
 | 
				
			||||
 | 
				
			||||
err_free:
 | 
				
			||||
	free(buffer);
 | 
				
			||||
err:
 | 
				
			||||
	wl_resource_post_no_memory(linux_dmabuf_resource);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void linux_dmabuf_destroy(struct wl_client *client, struct wl_resource *resource) {
 | 
				
			||||
	wl_resource_destroy(resource);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static const struct zwp_linux_dmabuf_v1_interface linux_dmabuf_impl = {
 | 
				
			||||
	linux_dmabuf_destroy,
 | 
				
			||||
	linux_dmabuf_create_params
 | 
				
			||||
};
 | 
				
			||||
 | 
				
			||||
struct wlr_linux_dmabuf *wlr_linux_dmabuf_from_resource(
 | 
				
			||||
		struct wl_resource *resource) {
 | 
				
			||||
	assert(wl_resource_instance_of(resource, &zwp_linux_dmabuf_v1_interface,
 | 
				
			||||
			&linux_dmabuf_impl));
 | 
				
			||||
 | 
				
			||||
	struct wlr_linux_dmabuf *dmabuf = wl_resource_get_user_data(resource);
 | 
				
			||||
	assert(dmabuf);
 | 
				
			||||
	return dmabuf;
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void linux_dmabuf_send_modifiers(struct wlr_linux_dmabuf *linux_dmabuf,
 | 
				
			||||
		struct wl_resource *resource) {
 | 
				
			||||
	struct wlr_egl *egl = linux_dmabuf->egl;
 | 
				
			||||
	/*
 | 
				
			||||
	 * Use EGL_EXT_image_dma_buf_import_modifiers to query and advertise
 | 
				
			||||
	 * format/modifier codes.
 | 
				
			||||
	 */
 | 
				
			||||
	uint64_t modifier_invalid = DRM_FORMAT_MOD_INVALID;
 | 
				
			||||
	int *formats = NULL;
 | 
				
			||||
	int num_formats = wlr_egl_get_dmabuf_formats(egl, &formats);
 | 
				
			||||
 | 
				
			||||
	if (num_formats < 0) {
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	for (int i = 0; i < num_formats; i++) {
 | 
				
			||||
		int num_modifiers;
 | 
				
			||||
		uint64_t *modifiers = NULL;
 | 
				
			||||
 | 
				
			||||
		num_modifiers = wlr_egl_get_dmabuf_modifiers(egl, formats[i],
 | 
				
			||||
			&modifiers);
 | 
				
			||||
		if (num_modifiers < 0) {
 | 
				
			||||
			return;
 | 
				
			||||
		}
 | 
				
			||||
		/* send DRM_FORMAT_MOD_INVALID token when no modifiers are supported
 | 
				
			||||
		 * for this format */
 | 
				
			||||
		if (num_modifiers == 0) {
 | 
				
			||||
			num_modifiers = 1;
 | 
				
			||||
			modifiers = &modifier_invalid;
 | 
				
			||||
		}
 | 
				
			||||
		for (int j = 0; j < num_modifiers; j++) {
 | 
				
			||||
			uint32_t modifier_lo = modifiers[j] & 0xFFFFFFFF;
 | 
				
			||||
			uint32_t modifier_hi = modifiers[j] >> 32;
 | 
				
			||||
			zwp_linux_dmabuf_v1_send_modifier(resource, formats[i],
 | 
				
			||||
				modifier_hi,
 | 
				
			||||
				modifier_lo);
 | 
				
			||||
		}
 | 
				
			||||
		if (modifiers != &modifier_invalid) {
 | 
				
			||||
			free(modifiers);
 | 
				
			||||
		}
 | 
				
			||||
	}
 | 
				
			||||
	free(formats);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void linux_dmabuf_bind(struct wl_client *client,
 | 
				
			||||
		void *data, uint32_t version, uint32_t id) {
 | 
				
			||||
	struct wlr_linux_dmabuf *linux_dmabuf = data;
 | 
				
			||||
	struct wl_resource *resource = wl_resource_create(client,
 | 
				
			||||
		  &zwp_linux_dmabuf_v1_interface,
 | 
				
			||||
		  version, id);
 | 
				
			||||
 | 
				
			||||
	if (resource == NULL) {
 | 
				
			||||
		wl_client_post_no_memory(client);
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	wl_resource_set_implementation(resource, &linux_dmabuf_impl,
 | 
				
			||||
		linux_dmabuf, NULL);
 | 
				
			||||
	if (version < ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION) {
 | 
				
			||||
                return;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	linux_dmabuf_send_modifiers(linux_dmabuf, resource);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
void wlr_linux_dmabuf_destroy(struct wlr_linux_dmabuf *linux_dmabuf) {
 | 
				
			||||
	if (!linux_dmabuf) {
 | 
				
			||||
		return;
 | 
				
			||||
	}
 | 
				
			||||
	wl_list_remove(&linux_dmabuf->display_destroy.link);
 | 
				
			||||
 | 
				
			||||
	wl_global_destroy(linux_dmabuf->wl_global);
 | 
				
			||||
	free(linux_dmabuf);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
static void handle_display_destroy(struct wl_listener *listener, void *data) {
 | 
				
			||||
	struct wlr_linux_dmabuf *linux_dmabuf = wl_container_of(listener, linux_dmabuf, display_destroy);
 | 
				
			||||
	wlr_linux_dmabuf_destroy(linux_dmabuf);
 | 
				
			||||
}
 | 
				
			||||
 | 
				
			||||
struct wlr_linux_dmabuf *wlr_linux_dmabuf_create(struct wl_display *display,
 | 
				
			||||
		struct wlr_egl *egl) {
 | 
				
			||||
	struct wlr_linux_dmabuf *linux_dmabuf =
 | 
				
			||||
		calloc(1, sizeof(struct wlr_linux_dmabuf));
 | 
				
			||||
	if (linux_dmabuf == NULL) {
 | 
				
			||||
		wlr_log(L_ERROR, "could not create simple dmabuf manager");
 | 
				
			||||
		return NULL;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	linux_dmabuf->display_destroy.notify = handle_display_destroy;
 | 
				
			||||
	wl_display_add_destroy_listener(display, &linux_dmabuf->display_destroy);
 | 
				
			||||
 | 
				
			||||
	linux_dmabuf->wl_global =
 | 
				
			||||
		wl_global_create(display, &zwp_linux_dmabuf_v1_interface,
 | 
				
			||||
			3, linux_dmabuf, linux_dmabuf_bind);
 | 
				
			||||
 | 
				
			||||
	linux_dmabuf->egl = egl;
 | 
				
			||||
	if (!linux_dmabuf->wl_global) {
 | 
				
			||||
		wlr_log(L_ERROR, "could not create linux dmabuf v1 wl global");
 | 
				
			||||
		free(linux_dmabuf);
 | 
				
			||||
		return NULL;
 | 
				
			||||
	}
 | 
				
			||||
 | 
				
			||||
	return linux_dmabuf;
 | 
				
			||||
}
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue