From: Benjamin Gaignard benjamin.gaignard@linaro.org
The goal of this serie of patches is to add a way to use dmabuf file descriptor inside wayland and weston.
In a context where there is no Mesa EGL (and so no wl_drm protocol) wl_dmabuf could be used as an alternative to shm to share buffers between hardware devices. If your hardware device (video decoder, renderer, etc...) need physical contiguous memory (obviously don't have MMU) wl_dmabuf may save the cost of one copy compare to shm.
shm case: videodecoder --(copy into shm_buffer)--> weston(+pixman) --> HW renderer
dmabuf case: videodecoder --(directly write in dmabuf buffer)--> weston(+pixman) --> HW renderer
The server is responsible to send its supported pixel formats and the device name to be used by the client to allocate buffers.
While mmap() call on dmabuf file descriptor result isn't guaranty on all architectures both server and client should take care of it before accessing to buffer data to avoid segfault.
This series of patches include wayland and weston modifications. An example of how use wl_dmabuf is provided in weston/clients/simple-dmabuf.c
=== Wayland === Benjamin Gaignard (1): Add wl_dmabuf protocol
protocol/Makefile.am | 6 +- protocol/wayland-dmabuf.xml | 128 ++++++++++++++++++++++++ src/Makefile.am | 12 ++- src/wayland-dmabuf.c | 231 +++++++++++++++++++++++++++++++++++++++++++ src/wayland-dmabuf.h | 123 +++++++++++++++++++++++ 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 protocol/wayland-dmabuf.xml create mode 100644 src/wayland-dmabuf.c create mode 100644 src/wayland-dmabuf.h
=== Weston === Benjamin Gaignard (2): compositor-drm: allow to be a wl_dmabuf server add simple-dmabuf client
clients/Makefile.am | 11 ++ clients/simple-dmabuf.c | 469 +++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 5 + src/compositor-drm.c | 83 ++++++++- src/compositor.c | 4 +- src/compositor.h | 2 + src/pixman-renderer.c | 93 +++++++--- 7 files changed, 637 insertions(+), 30 deletions(-) create mode 100644 clients/simple-dmabuf.c
From: Benjamin Gaignard benjamin.gaignard@linaro.org
It allow to use a dmabuf file descriptor in a wayland protocol. To make as generic as possible it is up to the server to call wl_dmabuf_send_format() and/or wl_dmabuf_send_device() to signal it capabilities.
Signed-off-by: Benjamin Gaignard benjamin.gaignard@linaro.org --- protocol/Makefile.am | 6 +- protocol/wayland-dmabuf.xml | 128 ++++++++++++++++++++++++ src/Makefile.am | 12 ++- src/wayland-dmabuf.c | 231 +++++++++++++++++++++++++++++++++++++++++++ src/wayland-dmabuf.h | 123 +++++++++++++++++++++++ 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 protocol/wayland-dmabuf.xml create mode 100644 src/wayland-dmabuf.c create mode 100644 src/wayland-dmabuf.h
diff --git a/protocol/Makefile.am b/protocol/Makefile.am index e8b6290..8c9499f 100644 --- a/protocol/Makefile.am +++ b/protocol/Makefile.am @@ -1,4 +1,4 @@ -dist_pkgdata_DATA = wayland.xml wayland.dtd +dist_pkgdata_DATA = wayland.xml wayland-dmabuf.xml wayland.dtd
if HAVE_XMLLINT .PHONY: validate @@ -6,9 +6,9 @@ if HAVE_XMLLINT .%.xml.valid: %.xml $(AM_V_GEN)$(XMLLINT) --noout --dtdvalid $(srcdir)/wayland.dtd $^ > $@
-validate: .wayland.xml.valid +validate: .wayland.xml.valid .wayland-dmabuf.xml.valid
all-local: validate
-CLEANFILES = .wayland.xml.valid +CLEANFILES = .wayland.xml.valid .wayland-dmabuf.xml.valid endif diff --git a/protocol/wayland-dmabuf.xml b/protocol/wayland-dmabuf.xml new file mode 100644 index 0000000..f84edb5 --- /dev/null +++ b/protocol/wayland-dmabuf.xml @@ -0,0 +1,128 @@ +<?xml version="1.0" encoding="UTF-8"?> +<protocol name="dmabuf"> + + <copyright> + Copyright © 2008-2011 Kristian Høgsberg + Copyright © 2010-2011 Intel Corporation + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that\n the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + </copyright> + + <!-- dmabuf support. This object is created by the server and published + using the display's global event. --> + <interface name="wl_dmabuf" version="1"> + <enum name="error"> + <entry name="invalid_format" value="1"/> + <entry name="invalid_file_descriptor" value="2"/> + </enum> + + <enum name="format"> + <!-- The dmabuf format codes match the #defines in drm_fourcc.h. + The formats actually supported by the compositor will be + reported by the format event. --> + <entry name="c8" value="0x20203843"/> + <entry name="rgb332" value="0x38424752"/> + <entry name="bgr233" value="0x38524742"/> + <entry name="xrgb4444" value="0x32315258"/> + <entry name="xbgr4444" value="0x32314258"/> + <entry name="rgbx4444" value="0x32315852"/> + <entry name="bgrx4444" value="0x32315842"/> + <entry name="argb4444" value="0x32315241"/> + <entry name="abgr4444" value="0x32314241"/> + <entry name="rgba4444" value="0x32314152"/> + <entry name="bgra4444" value="0x32314142"/> + <entry name="xrgb1555" value="0x35315258"/> + <entry name="xbgr1555" value="0x35314258"/> + <entry name="rgbx5551" value="0x35315852"/> + <entry name="bgrx5551" value="0x35315842"/> + <entry name="argb1555" value="0x35315241"/> + <entry name="abgr1555" value="0x35314241"/> + <entry name="rgba5551" value="0x35314152"/> + <entry name="bgra5551" value="0x35314142"/> + <entry name="rgb565" value="0x36314752"/> + <entry name="bgr565" value="0x36314742"/> + <entry name="rgb888" value="0x34324752"/> + <entry name="bgr888" value="0x34324742"/> + <entry name="xrgb8888" value="0x34325258"/> + <entry name="xbgr8888" value="0x34324258"/> + <entry name="rgbx8888" value="0x34325852"/> + <entry name="bgrx8888" value="0x34325842"/> + <entry name="argb8888" value="0x34325241"/> + <entry name="abgr8888" value="0x34324241"/> + <entry name="rgba8888" value="0x34324152"/> + <entry name="bgra8888" value="0x34324142"/> + <entry name="xrgb2101010" value="0x30335258"/> + <entry name="xbgr2101010" value="0x30334258"/> + <entry name="rgbx1010102" value="0x30335852"/> + <entry name="bgrx1010102" value="0x30335842"/> + <entry name="argb2101010" value="0x30335241"/> + <entry name="abgr2101010" value="0x30334241"/> + <entry name="rgba1010102" value="0x30334152"/> + <entry name="bgra1010102" value="0x30334142"/> + <entry name="yuyv" value="0x56595559"/> + <entry name="yvyu" value="0x55595659"/> + <entry name="uyvy" value="0x59565955"/> + <entry name="vyuy" value="0x59555956"/> + <entry name="ayuv" value="0x56555941"/> + <entry name="nv12" value="0x3231564e"/> + <entry name="nv21" value="0x3132564e"/> + <entry name="nv16" value="0x3631564e"/> + <entry name="nv61" value="0x3136564e"/> + <entry name="yuv410" value="0x39565559"/> + <entry name="yvu410" value="0x39555659"/> + <entry name="yuv411" value="0x31315559"/> + <entry name="yvu411" value="0x31315659"/> + <entry name="yuv420" value="0x32315559"/> + <entry name="yvu420" value="0x32315659"/> + <entry name="yuv422" value="0x36315559"/> + <entry name="yvu422" value="0x36315659"/> + <entry name="yuv444" value="0x34325559"/> + <entry name="yvu444" value="0x34325659"/> + </enum> + + <!-- Create a wayland buffer for the prime fd. Use for regular and planar + buffers. Pass 0 for offset and stride for unused planes. --> + <request name="create_prime_buffer"> + <arg name="id" type="new_id" interface="wl_buffer"/> + <arg name="name" type="fd"/> + <arg name="width" type="int"/> + <arg name="height" type="int"/> + <arg name="format" type="uint"/> + <arg name="stride" type="int"/> + </request> + + <event name="format"> + <arg name="format" type="uint"/> + </event> + + <!-- Notification of the path of the drm device which is used by + the server. The client should use this device for creating + local buffers. Only buffers created from this device should + be be passed to the server using this drm object's + create_buffer request. --> + <event name="device"> + <arg name="name" type="string"/> + </event> + + </interface> + +</protocol> diff --git a/src/Makefile.am b/src/Makefile.am index 15f44a5..b3c414c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,8 +4,11 @@ noinst_LTLIBRARIES = libwayland-util.la include_HEADERS = \ wayland-util.h \ wayland-server-protocol.h \ + wayland-dmabuf-server-protocol.h \ wayland-server.h \ + wayland-dmabuf.h \ wayland-client-protocol.h \ + wayland-dmabuf-client-protocol.h \ wayland-client.h \ wayland-egl.h \ wayland-version.h @@ -22,14 +25,17 @@ libwayland_server_la_LIBADD = $(FFI_LIBS) libwayland-util.la -lrt -lm libwayland_server_la_LDFLAGS = -version-info 1:0:1 libwayland_server_la_SOURCES = \ wayland-protocol.c \ + wayland-dmabuf-protocol.c \ wayland-server.c \ wayland-shm.c \ + wayland-dmabuf.c \ event-loop.c
libwayland_client_la_LIBADD = $(FFI_LIBS) libwayland-util.la -lrt -lm libwayland_client_la_LDFLAGS = -version-info 2:0:2 libwayland_client_la_SOURCES = \ wayland-protocol.c \ + wayland-dmabuf-protocol.c \ wayland-client.c
pkgconfigdir = $(libdir)/pkgconfig @@ -65,7 +71,11 @@ endif BUILT_SOURCES = \ wayland-server-protocol.h \ wayland-client-protocol.h \ - wayland-protocol.c + wayland-protocol.c \ + wayland-dmabuf-server-protocol.h \ + wayland-dmabuf-client-protocol.h \ + wayland-dmabuf-protocol.c +
CLEANFILES = $(BUILT_SOURCES) DISTCLEANFILES = wayland-version.h diff --git a/src/wayland-dmabuf.c b/src/wayland-dmabuf.c new file mode 100644 index 0000000..ce6dc4c --- /dev/null +++ b/src/wayland-dmabuf.c @@ -0,0 +1,231 @@ +/* + * Copyright © 2011 Kristian Høgsberg + * Copyright © 2011 Benjamin Franzke + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Kristian Høgsberg krh@bitplanet.net + * Benjamin Franzke benjaminfranzke@googlemail.com + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stddef.h> +#include <unistd.h> +#include <sys/mman.h> + +#include "wayland-server.h" +#include "wayland-dmabuf.h" +#include "wayland-dmabuf-server-protocol.h" + +struct wl_dmabuf { + struct wl_global *wl_dmabuf_global; + void *user_data; + struct wl_dmabuf_callbacks *callbacks; +}; + +static void +destroy_buffer(struct wl_resource *resource) +{ + struct wl_dmabuf_buffer *buffer = wl_resource_get_user_data(resource); + + buffer->refcount--; + + if (buffer->refcount) + return; + + close(buffer->fd); + free(buffer); +} + +static void +buffer_destroy(struct wl_client *client, struct wl_resource *resource) +{ + wl_resource_destroy(resource); +} + +static const struct wl_buffer_interface dmabuf_buffer_interface = { + buffer_destroy +}; + +static void +dmabuf_create_prime_buffer(struct wl_client *client, struct wl_resource *resource, + uint32_t id, int fd, + int32_t width, int32_t height, + uint32_t format, int32_t stride) +{ + struct wl_dmabuf *dmabuf = wl_resource_get_user_data(resource); + struct wl_dmabuf_buffer *buffer; + + buffer = calloc(1, sizeof *buffer); + if (buffer == NULL) { + wl_resource_post_no_memory(resource); + return; + } + + buffer->resource = + wl_resource_create(client, &wl_buffer_interface, 1, id); + if (!buffer->resource) { + wl_resource_post_no_memory(resource); + free(buffer); + return; + } + + buffer->dmabuf = dmabuf; + buffer->width = width; + buffer->height = height; + buffer->format = format; + buffer->data = NULL; + buffer->mmapcount = 0; + buffer->refcount = 1; + /* make sure we have your own file descriptor */ + buffer->fd = dup(fd); + close(fd); + buffer->stride = stride; + + wl_resource_set_implementation(buffer->resource, + &dmabuf_buffer_interface, + buffer, destroy_buffer); +} + +static const struct wl_dmabuf_interface dmabuf_interface = { + dmabuf_create_prime_buffer +}; + +static void +bind_dmabuf(struct wl_client *client, void *data, uint32_t version, uint32_t id) +{ + struct wl_dmabuf *dmabuf = data; + struct wl_resource *resource; + + resource = wl_resource_create(client, &wl_dmabuf_interface, 1, id); + if (!resource) { + wl_client_post_no_memory(client); + return; + } + + wl_resource_set_implementation(resource, &dmabuf_interface, data, NULL); + + if (dmabuf->callbacks->send_server_info) + dmabuf->callbacks->send_server_info(dmabuf->user_data, resource); +} + +WL_EXPORT struct wl_dmabuf * +wl_dmabuf_init(struct wl_display *display, struct wl_dmabuf_callbacks *callbacks, + void *user_data, uint32_t flags) +{ + struct wl_dmabuf *dmabuf; + + dmabuf = malloc(sizeof *dmabuf); + + dmabuf->callbacks = callbacks; + dmabuf->user_data = user_data; + + dmabuf->wl_dmabuf_global = + wl_global_create(display, &wl_dmabuf_interface, 1, dmabuf, bind_dmabuf); + + return dmabuf; +} + +WL_EXPORT void +wl_dmabuf_uninit(struct wl_dmabuf *dmabuf) +{ + wl_global_destroy(dmabuf->wl_dmabuf_global); + + free(dmabuf); +} + +WL_EXPORT struct wl_dmabuf_buffer * +wl_dmabuf_buffer_get(struct wl_resource *resource) +{ + if (resource == NULL) + return NULL; + + if (wl_resource_instance_of(resource, &wl_buffer_interface, + &dmabuf_buffer_interface)) + return wl_resource_get_user_data(resource); + + return NULL; +} + +WL_EXPORT uint32_t +wl_dmabuf_buffer_get_format(struct wl_dmabuf_buffer *buffer) +{ + return buffer->format; +} + +WL_EXPORT int32_t +wl_dmabuf_buffer_get_stride(struct wl_dmabuf_buffer *buffer) +{ + return buffer->stride; +} + +WL_EXPORT void * +wl_dmabuf_buffer_get_data(struct wl_dmabuf_buffer *buffer) +{ + uint32_t size; + + buffer->refcount++; + + if (buffer->mmapcount++) + return buffer->data; + + size = buffer->width * buffer->height * 4; + buffer->data = mmap (0, size, PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0); + + return buffer->data; +} + +WL_EXPORT void +wl_dmabuf_buffer_put_data(struct wl_dmabuf_buffer *buffer) +{ + uint32_t size; + + buffer->mmapcount--; + buffer->refcount--; + + if (buffer->mmapcount) + return; + + size = buffer->width * buffer->height * 4; + munmap (buffer->data, size); + buffer->data = NULL; + + if (buffer->refcount) + return; + + close(buffer->fd); + free(buffer); +} + +WL_EXPORT int32_t +wl_dmabuf_buffer_get_width(struct wl_dmabuf_buffer *buffer) +{ + return buffer->width; +} + +WL_EXPORT int32_t +wl_dmabuf_buffer_get_height(struct wl_dmabuf_buffer *buffer) +{ + return buffer->height; +} diff --git a/src/wayland-dmabuf.h b/src/wayland-dmabuf.h new file mode 100644 index 0000000..f9eb230 --- /dev/null +++ b/src/wayland-dmabuf.h @@ -0,0 +1,123 @@ +#ifndef WAYLAND_DMABUF_H +#define WAYLAND_DMABUF_H + +#include <wayland-server.h> + +#ifndef WL_DMABUF_FORMAT_ENUM +#define WL_DMABUF_FORMAT_ENUM + +enum wl_dmabuf_format { + WL_DMABUF_FORMAT_C8 = 0x20203843, + WL_DMABUF_FORMAT_RGB332 = 0x38424752, + WL_DMABUF_FORMAT_BGR233 = 0x38524742, + WL_DMABUF_FORMAT_XRGB4444 = 0x32315258, + WL_DMABUF_FORMAT_XBGR4444 = 0x32314258, + WL_DMABUF_FORMAT_RGBX4444 = 0x32315852, + WL_DMABUF_FORMAT_BGRX4444 = 0x32315842, + WL_DMABUF_FORMAT_ARGB4444 = 0x32315241, + WL_DMABUF_FORMAT_ABGR4444 = 0x32314241, + WL_DMABUF_FORMAT_RGBA4444 = 0x32314152, + WL_DMABUF_FORMAT_BGRA4444 = 0x32314142, + WL_DMABUF_FORMAT_XRGB1555 = 0x35315258, + WL_DMABUF_FORMAT_XBGR1555 = 0x35314258, + WL_DMABUF_FORMAT_RGBX5551 = 0x35315852, + WL_DMABUF_FORMAT_BGRX5551 = 0x35315842, + WL_DMABUF_FORMAT_ARGB1555 = 0x35315241, + WL_DMABUF_FORMAT_ABGR1555 = 0x35314241, + WL_DMABUF_FORMAT_RGBA5551 = 0x35314152, + WL_DMABUF_FORMAT_BGRA5551 = 0x35314142, + WL_DMABUF_FORMAT_RGB565 = 0x36314752, + WL_DMABUF_FORMAT_BGR565 = 0x36314742, + WL_DMABUF_FORMAT_RGB888 = 0x34324752, + WL_DMABUF_FORMAT_BGR888 = 0x34324742, + WL_DMABUF_FORMAT_XRGB8888 = 0x34325258, + WL_DMABUF_FORMAT_XBGR8888 = 0x34324258, + WL_DMABUF_FORMAT_RGBX8888 = 0x34325852, + WL_DMABUF_FORMAT_BGRX8888 = 0x34325842, + WL_DMABUF_FORMAT_ARGB8888 = 0x34325241, + WL_DMABUF_FORMAT_ABGR8888 = 0x34324241, + WL_DMABUF_FORMAT_RGBA8888 = 0x34324152, + WL_DMABUF_FORMAT_BGRA8888 = 0x34324142, + WL_DMABUF_FORMAT_XRGB2101010 = 0x30335258, + WL_DMABUF_FORMAT_XBGR2101010 = 0x30334258, + WL_DMABUF_FORMAT_RGBX1010102 = 0x30335852, + WL_DMABUF_FORMAT_BGRX1010102 = 0x30335842, + WL_DMABUF_FORMAT_ARGB2101010 = 0x30335241, + WL_DMABUF_FORMAT_ABGR2101010 = 0x30334241, + WL_DMABUF_FORMAT_RGBA1010102 = 0x30334152, + WL_DMABUF_FORMAT_BGRA1010102 = 0x30334142, + WL_DMABUF_FORMAT_YUYV = 0x56595559, + WL_DMABUF_FORMAT_YVYU = 0x55595659, + WL_DMABUF_FORMAT_UYVY = 0x59565955, + WL_DMABUF_FORMAT_VYUY = 0x59555956, + WL_DMABUF_FORMAT_AYUV = 0x56555941, + WL_DMABUF_FORMAT_NV12 = 0x3231564e, + WL_DMABUF_FORMAT_NV21 = 0x3132564e, + WL_DMABUF_FORMAT_NV16 = 0x3631564e, + WL_DMABUF_FORMAT_NV61 = 0x3136564e, + WL_DMABUF_FORMAT_YUV410 = 0x39565559, + WL_DMABUF_FORMAT_YVU410 = 0x39555659, + WL_DMABUF_FORMAT_YUV411 = 0x31315559, + WL_DMABUF_FORMAT_YVU411 = 0x31315659, + WL_DMABUF_FORMAT_YUV420 = 0x32315559, + WL_DMABUF_FORMAT_YVU420 = 0x32315659, + WL_DMABUF_FORMAT_YUV422 = 0x36315559, + WL_DMABUF_FORMAT_YVU422 = 0x36315659, + WL_DMABUF_FORMAT_YUV444 = 0x34325559, + WL_DMABUF_FORMAT_YVU444 = 0x34325659, +}; +#endif /* WL_DMABUF_FORMAT_ENUM */ + +struct wl_dmabuf; + +struct wl_dmabuf_buffer { + struct wl_resource *resource; + struct wl_dmabuf *dmabuf; + int32_t width, height; + int32_t stride; + uint32_t format; + uint8_t *data; + int mmapcount; + int refcount; + int fd; +}; + +/* struct wl_dmabuf_callbacks + * + * @send_server_info: ask to the server to send all + * supported pixel formats and device name + */ +struct wl_dmabuf_callbacks { + void (*send_server_info)(void *user_data, struct wl_resource *resource); +}; + +struct wl_dmabuf * +wl_dmabuf_init(struct wl_display *display, + struct wl_dmabuf_callbacks *callbacks, + void *user_data, uint32_t flags); + +void +wl_dmabuf_uninit(struct wl_dmabuf *dmabuf); + +struct wl_dmabuf_buffer * +wl_dmabuf_buffer_get(struct wl_resource *resource); + +uint32_t +wl_dmabuf_buffer_get_format(struct wl_dmabuf_buffer *buffer); + +int32_t +wl_dmabuf_buffer_get_stride(struct wl_dmabuf_buffer *buffer); + +void * +wl_dmabuf_buffer_get_data(struct wl_dmabuf_buffer *buffer); + +void +wl_dmabuf_buffer_put_data(struct wl_dmabuf_buffer *buffer); + +int32_t +wl_dmabuf_buffer_get_width(struct wl_dmabuf_buffer *buffer); + +int32_t +wl_dmabuf_buffer_get_height(struct wl_dmabuf_buffer *buffer); + +#endif
On Tue, Jan 07, 2014 at 06:41:42PM +0100, benjamin.gaignard@linaro.org wrote:
From: Benjamin Gaignard benjamin.gaignard@linaro.org
It allow to use a dmabuf file descriptor in a wayland protocol. To make as generic as possible it is up to the server to call wl_dmabuf_send_format() and/or wl_dmabuf_send_device() to signal it capabilities.
Signed-off-by: Benjamin Gaignard benjamin.gaignard@linaro.org
protocol/Makefile.am | 6 +- protocol/wayland-dmabuf.xml | 128 ++++++++++++++++++++++++ src/Makefile.am | 12 ++- src/wayland-dmabuf.c | 231 +++++++++++++++++++++++++++++++++++++++++++ src/wayland-dmabuf.h | 123 +++++++++++++++++++++++ 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 protocol/wayland-dmabuf.xml create mode 100644 src/wayland-dmabuf.c create mode 100644 src/wayland-dmabuf.h
diff --git a/protocol/Makefile.am b/protocol/Makefile.am index e8b6290..8c9499f 100644 --- a/protocol/Makefile.am +++ b/protocol/Makefile.am @@ -1,4 +1,4 @@ -dist_pkgdata_DATA = wayland.xml wayland.dtd +dist_pkgdata_DATA = wayland.xml wayland-dmabuf.xml wayland.dtd if HAVE_XMLLINT .PHONY: validate @@ -6,9 +6,9 @@ if HAVE_XMLLINT .%.xml.valid: %.xml $(AM_V_GEN)$(XMLLINT) --noout --dtdvalid $(srcdir)/wayland.dtd $^ > $@ -validate: .wayland.xml.valid +validate: .wayland.xml.valid .wayland-dmabuf.xml.valid all-local: validate -CLEANFILES = .wayland.xml.valid +CLEANFILES = .wayland.xml.valid .wayland-dmabuf.xml.valid endif diff --git a/protocol/wayland-dmabuf.xml b/protocol/wayland-dmabuf.xml new file mode 100644 index 0000000..f84edb5 --- /dev/null +++ b/protocol/wayland-dmabuf.xml @@ -0,0 +1,128 @@ +<?xml version="1.0" encoding="UTF-8"?> +<protocol name="dmabuf">
<copyright>
- Copyright © 2008-2011 Kristian Høgsberg
- Copyright © 2010-2011 Intel Corporation
- Permission to use, copy, modify, distribute, and sell this
- software and its documentation for any purpose is hereby granted
- without fee, provided that\n the above copyright notice appear in
- all copies and that both that copyright notice and this permission
- notice appear in supporting documentation, and that the name of
- the copyright holders not be used in advertising or publicity
- pertaining to distribution of the software without specific,
- written prior permission. The copyright holders make no
- representations about the suitability of this software for any
- purpose. It is provided "as is" without express or implied
- warranty.
- THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
- SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
- SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
- AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
- ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
- THIS SOFTWARE.
</copyright>
- <!-- dmabuf support. This object is created by the server and published
using the display's global event. -->
<interface name="wl_dmabuf" version="1">
<enum name="error">
<entry name="invalid_format" value="1"/>
<entry name="invalid_file_descriptor" value="2"/>
</enum>
<enum name="format">
<!-- The dmabuf format codes match the #defines in drm_fourcc.h.
The formats actually supported by the compositor will be
reported by the format event. -->
<entry name="c8" value="0x20203843"/>
<entry name="rgb332" value="0x38424752"/>
<entry name="bgr233" value="0x38524742"/>
<entry name="xrgb4444" value="0x32315258"/>
<entry name="xbgr4444" value="0x32314258"/>
<entry name="rgbx4444" value="0x32315852"/>
<entry name="bgrx4444" value="0x32315842"/>
<entry name="argb4444" value="0x32315241"/>
<entry name="abgr4444" value="0x32314241"/>
<entry name="rgba4444" value="0x32314152"/>
<entry name="bgra4444" value="0x32314142"/>
<entry name="xrgb1555" value="0x35315258"/>
<entry name="xbgr1555" value="0x35314258"/>
<entry name="rgbx5551" value="0x35315852"/>
<entry name="bgrx5551" value="0x35315842"/>
<entry name="argb1555" value="0x35315241"/>
<entry name="abgr1555" value="0x35314241"/>
<entry name="rgba5551" value="0x35314152"/>
<entry name="bgra5551" value="0x35314142"/>
<entry name="rgb565" value="0x36314752"/>
<entry name="bgr565" value="0x36314742"/>
<entry name="rgb888" value="0x34324752"/>
<entry name="bgr888" value="0x34324742"/>
<entry name="xrgb8888" value="0x34325258"/>
<entry name="xbgr8888" value="0x34324258"/>
<entry name="rgbx8888" value="0x34325852"/>
<entry name="bgrx8888" value="0x34325842"/>
<entry name="argb8888" value="0x34325241"/>
<entry name="abgr8888" value="0x34324241"/>
<entry name="rgba8888" value="0x34324152"/>
<entry name="bgra8888" value="0x34324142"/>
<entry name="xrgb2101010" value="0x30335258"/>
<entry name="xbgr2101010" value="0x30334258"/>
<entry name="rgbx1010102" value="0x30335852"/>
<entry name="bgrx1010102" value="0x30335842"/>
<entry name="argb2101010" value="0x30335241"/>
<entry name="abgr2101010" value="0x30334241"/>
<entry name="rgba1010102" value="0x30334152"/>
<entry name="bgra1010102" value="0x30334142"/>
<entry name="yuyv" value="0x56595559"/>
<entry name="yvyu" value="0x55595659"/>
<entry name="uyvy" value="0x59565955"/>
<entry name="vyuy" value="0x59555956"/>
<entry name="ayuv" value="0x56555941"/>
<entry name="nv12" value="0x3231564e"/>
<entry name="nv21" value="0x3132564e"/>
<entry name="nv16" value="0x3631564e"/>
<entry name="nv61" value="0x3136564e"/>
<entry name="yuv410" value="0x39565559"/>
<entry name="yvu410" value="0x39555659"/>
<entry name="yuv411" value="0x31315559"/>
<entry name="yvu411" value="0x31315659"/>
<entry name="yuv420" value="0x32315559"/>
<entry name="yvu420" value="0x32315659"/>
<entry name="yuv422" value="0x36315559"/>
<entry name="yvu422" value="0x36315659"/>
<entry name="yuv444" value="0x34325559"/>
<entry name="yvu444" value="0x34325659"/>
</enum>
- <!-- Create a wayland buffer for the prime fd. Use for regular and planar
buffers. Pass 0 for offset and stride for unused planes. -->
<request name="create_prime_buffer">
<arg name="id" type="new_id" interface="wl_buffer"/>
<arg name="name" type="fd"/>
<arg name="width" type="int"/>
<arg name="height" type="int"/>
<arg name="format" type="uint"/>
<arg name="stride" type="int"/>
</request>
<event name="format">
<arg name="format" type="uint"/>
</event>
- <!-- Notification of the path of the drm device which is used by
the server. The client should use this device for creating
local buffers. Only buffers created from this device should
be be passed to the server using this drm object's
create_buffer request. -->
<event name="device">
<arg name="name" type="string"/>
At least in upstream linux there's no generic "allocate me a dma-buf" interface. Even less so one that'll work across different subsystems.
Even with that peculiarity ignored (we could have a central dma-buf allocator akin to ION) there's the issue that you need to know up front which devices will all be interested in a given buffer, at least if you care about efficiency. ION uncoditionally requires this, dma-buf per se would allow post-allocation buffer movement (but it's not really implement atm anyway).
So I don't think this will work out as-is.
</event>
</interface>
+</protocol> diff --git a/src/Makefile.am b/src/Makefile.am index 15f44a5..b3c414c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,8 +4,11 @@ noinst_LTLIBRARIES = libwayland-util.la include_HEADERS = \ wayland-util.h \ wayland-server-protocol.h \
- wayland-dmabuf-server-protocol.h \ wayland-server.h \
- wayland-dmabuf.h \ wayland-client-protocol.h \
- wayland-dmabuf-client-protocol.h \ wayland-client.h \ wayland-egl.h \ wayland-version.h
@@ -22,14 +25,17 @@ libwayland_server_la_LIBADD = $(FFI_LIBS) libwayland-util.la -lrt -lm libwayland_server_la_LDFLAGS = -version-info 1:0:1 libwayland_server_la_SOURCES = \ wayland-protocol.c \
- wayland-dmabuf-protocol.c \ wayland-server.c \ wayland-shm.c \
- wayland-dmabuf.c \ event-loop.c
libwayland_client_la_LIBADD = $(FFI_LIBS) libwayland-util.la -lrt -lm libwayland_client_la_LDFLAGS = -version-info 2:0:2 libwayland_client_la_SOURCES = \ wayland-protocol.c \
- wayland-dmabuf-protocol.c \ wayland-client.c
pkgconfigdir = $(libdir)/pkgconfig @@ -65,7 +71,11 @@ endif BUILT_SOURCES = \ wayland-server-protocol.h \ wayland-client-protocol.h \
- wayland-protocol.c
- wayland-protocol.c \
- wayland-dmabuf-server-protocol.h \
- wayland-dmabuf-client-protocol.h \
- wayland-dmabuf-protocol.c
CLEANFILES = $(BUILT_SOURCES) DISTCLEANFILES = wayland-version.h diff --git a/src/wayland-dmabuf.c b/src/wayland-dmabuf.c new file mode 100644 index 0000000..ce6dc4c --- /dev/null +++ b/src/wayland-dmabuf.c @@ -0,0 +1,231 @@ +/*
- Copyright © 2011 Kristian Høgsberg
- Copyright © 2011 Benjamin Franzke
- Permission is hereby granted, free of charge, to any person obtaining a
- copy of this software and associated documentation files (the "Software"),
- to deal in the Software without restriction, including without limitation
- the rights to use, copy, modify, merge, publish, distribute, sublicense,
- and/or sell copies of the Software, and to permit persons to whom the
- Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice (including the next
- paragraph) shall be included in all copies or substantial portions of the
- Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- DEALINGS IN THE SOFTWARE.
- Authors:
- Kristian Høgsberg krh@bitplanet.net
- Benjamin Franzke benjaminfranzke@googlemail.com
- */
+#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stddef.h> +#include <unistd.h> +#include <sys/mman.h>
+#include "wayland-server.h" +#include "wayland-dmabuf.h" +#include "wayland-dmabuf-server-protocol.h"
+struct wl_dmabuf {
- struct wl_global *wl_dmabuf_global;
- void *user_data;
- struct wl_dmabuf_callbacks *callbacks;
+};
+static void +destroy_buffer(struct wl_resource *resource) +{
- struct wl_dmabuf_buffer *buffer = wl_resource_get_user_data(resource);
- buffer->refcount--;
- if (buffer->refcount)
return;
- close(buffer->fd);
- free(buffer);
+}
+static void +buffer_destroy(struct wl_client *client, struct wl_resource *resource) +{
- wl_resource_destroy(resource);
+}
+static const struct wl_buffer_interface dmabuf_buffer_interface = {
- buffer_destroy
+};
+static void +dmabuf_create_prime_buffer(struct wl_client *client, struct wl_resource *resource,
uint32_t id, int fd,
int32_t width, int32_t height,
uint32_t format, int32_t stride)
+{
- struct wl_dmabuf *dmabuf = wl_resource_get_user_data(resource);
- struct wl_dmabuf_buffer *buffer;
- buffer = calloc(1, sizeof *buffer);
- if (buffer == NULL) {
wl_resource_post_no_memory(resource);
return;
- }
- buffer->resource =
wl_resource_create(client, &wl_buffer_interface, 1, id);
- if (!buffer->resource) {
wl_resource_post_no_memory(resource);
free(buffer);
return;
- }
- buffer->dmabuf = dmabuf;
- buffer->width = width;
- buffer->height = height;
- buffer->format = format;
- buffer->data = NULL;
- buffer->mmapcount = 0;
- buffer->refcount = 1;
- /* make sure we have your own file descriptor */
- buffer->fd = dup(fd);
- close(fd);
- buffer->stride = stride;
- wl_resource_set_implementation(buffer->resource,
&dmabuf_buffer_interface,
buffer, destroy_buffer);
+}
+static const struct wl_dmabuf_interface dmabuf_interface = {
- dmabuf_create_prime_buffer
+};
+static void +bind_dmabuf(struct wl_client *client, void *data, uint32_t version, uint32_t id) +{
- struct wl_dmabuf *dmabuf = data;
- struct wl_resource *resource;
- resource = wl_resource_create(client, &wl_dmabuf_interface, 1, id);
- if (!resource) {
wl_client_post_no_memory(client);
return;
- }
- wl_resource_set_implementation(resource, &dmabuf_interface, data, NULL);
- if (dmabuf->callbacks->send_server_info)
dmabuf->callbacks->send_server_info(dmabuf->user_data, resource);
+}
+WL_EXPORT struct wl_dmabuf * +wl_dmabuf_init(struct wl_display *display, struct wl_dmabuf_callbacks *callbacks,
void *user_data, uint32_t flags)
+{
- struct wl_dmabuf *dmabuf;
- dmabuf = malloc(sizeof *dmabuf);
- dmabuf->callbacks = callbacks;
- dmabuf->user_data = user_data;
- dmabuf->wl_dmabuf_global =
wl_global_create(display, &wl_dmabuf_interface, 1, dmabuf, bind_dmabuf);
- return dmabuf;
+}
+WL_EXPORT void +wl_dmabuf_uninit(struct wl_dmabuf *dmabuf) +{
- wl_global_destroy(dmabuf->wl_dmabuf_global);
- free(dmabuf);
+}
+WL_EXPORT struct wl_dmabuf_buffer * +wl_dmabuf_buffer_get(struct wl_resource *resource) +{
- if (resource == NULL)
return NULL;
- if (wl_resource_instance_of(resource, &wl_buffer_interface,
&dmabuf_buffer_interface))
return wl_resource_get_user_data(resource);
- return NULL;
+}
+WL_EXPORT uint32_t +wl_dmabuf_buffer_get_format(struct wl_dmabuf_buffer *buffer) +{
- return buffer->format;
+}
+WL_EXPORT int32_t +wl_dmabuf_buffer_get_stride(struct wl_dmabuf_buffer *buffer) +{
- return buffer->stride;
+}
+WL_EXPORT void * +wl_dmabuf_buffer_get_data(struct wl_dmabuf_buffer *buffer) +{
- uint32_t size;
- buffer->refcount++;
- if (buffer->mmapcount++)
return buffer->data;
- size = buffer->width * buffer->height * 4;
Fyi latest dmabuf code in the kernel has a working lseek implementation to tell you the size of a buffer. Fairly useful given that you seem to hardcode a bunch of assumptions here. -Daniel
- buffer->data = mmap (0, size, PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0);
- return buffer->data;
+}
+WL_EXPORT void +wl_dmabuf_buffer_put_data(struct wl_dmabuf_buffer *buffer) +{
- uint32_t size;
- buffer->mmapcount--;
- buffer->refcount--;
- if (buffer->mmapcount)
return;
- size = buffer->width * buffer->height * 4;
- munmap (buffer->data, size);
- buffer->data = NULL;
- if (buffer->refcount)
return;
- close(buffer->fd);
- free(buffer);
+}
+WL_EXPORT int32_t +wl_dmabuf_buffer_get_width(struct wl_dmabuf_buffer *buffer) +{
- return buffer->width;
+}
+WL_EXPORT int32_t +wl_dmabuf_buffer_get_height(struct wl_dmabuf_buffer *buffer) +{
- return buffer->height;
+} diff --git a/src/wayland-dmabuf.h b/src/wayland-dmabuf.h new file mode 100644 index 0000000..f9eb230 --- /dev/null +++ b/src/wayland-dmabuf.h @@ -0,0 +1,123 @@ +#ifndef WAYLAND_DMABUF_H +#define WAYLAND_DMABUF_H
+#include <wayland-server.h>
+#ifndef WL_DMABUF_FORMAT_ENUM +#define WL_DMABUF_FORMAT_ENUM
+enum wl_dmabuf_format {
- WL_DMABUF_FORMAT_C8 = 0x20203843,
- WL_DMABUF_FORMAT_RGB332 = 0x38424752,
- WL_DMABUF_FORMAT_BGR233 = 0x38524742,
- WL_DMABUF_FORMAT_XRGB4444 = 0x32315258,
- WL_DMABUF_FORMAT_XBGR4444 = 0x32314258,
- WL_DMABUF_FORMAT_RGBX4444 = 0x32315852,
- WL_DMABUF_FORMAT_BGRX4444 = 0x32315842,
- WL_DMABUF_FORMAT_ARGB4444 = 0x32315241,
- WL_DMABUF_FORMAT_ABGR4444 = 0x32314241,
- WL_DMABUF_FORMAT_RGBA4444 = 0x32314152,
- WL_DMABUF_FORMAT_BGRA4444 = 0x32314142,
- WL_DMABUF_FORMAT_XRGB1555 = 0x35315258,
- WL_DMABUF_FORMAT_XBGR1555 = 0x35314258,
- WL_DMABUF_FORMAT_RGBX5551 = 0x35315852,
- WL_DMABUF_FORMAT_BGRX5551 = 0x35315842,
- WL_DMABUF_FORMAT_ARGB1555 = 0x35315241,
- WL_DMABUF_FORMAT_ABGR1555 = 0x35314241,
- WL_DMABUF_FORMAT_RGBA5551 = 0x35314152,
- WL_DMABUF_FORMAT_BGRA5551 = 0x35314142,
- WL_DMABUF_FORMAT_RGB565 = 0x36314752,
- WL_DMABUF_FORMAT_BGR565 = 0x36314742,
- WL_DMABUF_FORMAT_RGB888 = 0x34324752,
- WL_DMABUF_FORMAT_BGR888 = 0x34324742,
- WL_DMABUF_FORMAT_XRGB8888 = 0x34325258,
- WL_DMABUF_FORMAT_XBGR8888 = 0x34324258,
- WL_DMABUF_FORMAT_RGBX8888 = 0x34325852,
- WL_DMABUF_FORMAT_BGRX8888 = 0x34325842,
- WL_DMABUF_FORMAT_ARGB8888 = 0x34325241,
- WL_DMABUF_FORMAT_ABGR8888 = 0x34324241,
- WL_DMABUF_FORMAT_RGBA8888 = 0x34324152,
- WL_DMABUF_FORMAT_BGRA8888 = 0x34324142,
- WL_DMABUF_FORMAT_XRGB2101010 = 0x30335258,
- WL_DMABUF_FORMAT_XBGR2101010 = 0x30334258,
- WL_DMABUF_FORMAT_RGBX1010102 = 0x30335852,
- WL_DMABUF_FORMAT_BGRX1010102 = 0x30335842,
- WL_DMABUF_FORMAT_ARGB2101010 = 0x30335241,
- WL_DMABUF_FORMAT_ABGR2101010 = 0x30334241,
- WL_DMABUF_FORMAT_RGBA1010102 = 0x30334152,
- WL_DMABUF_FORMAT_BGRA1010102 = 0x30334142,
- WL_DMABUF_FORMAT_YUYV = 0x56595559,
- WL_DMABUF_FORMAT_YVYU = 0x55595659,
- WL_DMABUF_FORMAT_UYVY = 0x59565955,
- WL_DMABUF_FORMAT_VYUY = 0x59555956,
- WL_DMABUF_FORMAT_AYUV = 0x56555941,
- WL_DMABUF_FORMAT_NV12 = 0x3231564e,
- WL_DMABUF_FORMAT_NV21 = 0x3132564e,
- WL_DMABUF_FORMAT_NV16 = 0x3631564e,
- WL_DMABUF_FORMAT_NV61 = 0x3136564e,
- WL_DMABUF_FORMAT_YUV410 = 0x39565559,
- WL_DMABUF_FORMAT_YVU410 = 0x39555659,
- WL_DMABUF_FORMAT_YUV411 = 0x31315559,
- WL_DMABUF_FORMAT_YVU411 = 0x31315659,
- WL_DMABUF_FORMAT_YUV420 = 0x32315559,
- WL_DMABUF_FORMAT_YVU420 = 0x32315659,
- WL_DMABUF_FORMAT_YUV422 = 0x36315559,
- WL_DMABUF_FORMAT_YVU422 = 0x36315659,
- WL_DMABUF_FORMAT_YUV444 = 0x34325559,
- WL_DMABUF_FORMAT_YVU444 = 0x34325659,
+}; +#endif /* WL_DMABUF_FORMAT_ENUM */
+struct wl_dmabuf;
+struct wl_dmabuf_buffer {
- struct wl_resource *resource;
- struct wl_dmabuf *dmabuf;
- int32_t width, height;
- int32_t stride;
- uint32_t format;
- uint8_t *data;
- int mmapcount;
- int refcount;
- int fd;
+};
+/* struct wl_dmabuf_callbacks
- @send_server_info: ask to the server to send all
- supported pixel formats and device name
- */
+struct wl_dmabuf_callbacks {
- void (*send_server_info)(void *user_data, struct wl_resource *resource);
+};
+struct wl_dmabuf * +wl_dmabuf_init(struct wl_display *display,
struct wl_dmabuf_callbacks *callbacks,
void *user_data, uint32_t flags);
+void +wl_dmabuf_uninit(struct wl_dmabuf *dmabuf);
+struct wl_dmabuf_buffer * +wl_dmabuf_buffer_get(struct wl_resource *resource);
+uint32_t +wl_dmabuf_buffer_get_format(struct wl_dmabuf_buffer *buffer);
+int32_t +wl_dmabuf_buffer_get_stride(struct wl_dmabuf_buffer *buffer);
+void * +wl_dmabuf_buffer_get_data(struct wl_dmabuf_buffer *buffer);
+void +wl_dmabuf_buffer_put_data(struct wl_dmabuf_buffer *buffer);
+int32_t +wl_dmabuf_buffer_get_width(struct wl_dmabuf_buffer *buffer);
+int32_t +wl_dmabuf_buffer_get_height(struct wl_dmabuf_buffer *buffer);
+#endif
1.7.9.5
wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel
From: Benjamin Gaignard benjamin.gaignard@linaro.org
Make drm compositor aware of the wl_dmabuf protocol if pixman is used. Add functions to have a wl_dmabuf server inside drm compositor. Change pixman to let it know how use a wl_dmabuf buffer.
Signed-off-by: Benjamin Gaignard benjamin.gaignard@linaro.org --- src/compositor-drm.c | 83 ++++++++++++++++++++++++++++++++++++++++--- src/compositor.c | 4 ++- src/compositor.h | 2 ++ src/pixman-renderer.c | 93 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 152 insertions(+), 30 deletions(-)
diff --git a/src/compositor-drm.c b/src/compositor-drm.c index 4f015d1..669ee45 100644 --- a/src/compositor-drm.c +++ b/src/compositor-drm.c @@ -49,6 +49,8 @@ #include "udev-seat.h" #include "launcher-util.h" #include "vaapi-recorder.h" +#include "wayland-dmabuf.h" +#include "wayland-dmabuf-server-protocol.h"
#ifndef DRM_CAP_TIMESTAMP_MONOTONIC #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 @@ -826,7 +828,8 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base, if (es->alpha != 1.0f) return NULL;
- if (wl_shm_buffer_get(es->buffer_ref.buffer->resource)) + if (wl_shm_buffer_get(es->buffer_ref.buffer->resource) + || wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) return NULL;
if (!drm_surface_transform_supported(es)) @@ -951,7 +954,8 @@ drm_output_prepare_cursor_surface(struct weston_output *output_base, if (c->cursors_are_broken) return NULL; if (es->buffer_ref.buffer == NULL || - !wl_shm_buffer_get(es->buffer_ref.buffer->resource) || + (!wl_shm_buffer_get(es->buffer_ref.buffer->resource) && + !wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource))|| es->geometry.width > 64 || es->geometry.height > 64) return NULL;
@@ -985,12 +989,25 @@ drm_output_set_cursor(struct drm_output *output) output->current_cursor ^= 1; bo = output->cursor_bo[output->current_cursor]; memset(buf, 0, sizeof buf); - stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer->shm_buffer); - s = wl_shm_buffer_get_data(es->buffer_ref.buffer->shm_buffer); + if (wl_shm_buffer_get(es->buffer_ref.buffer->resource)) { + stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer->shm_buffer); + s = wl_shm_buffer_get_data(es->buffer_ref.buffer->shm_buffer); + } + if (wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) { + stride = wl_dmabuf_buffer_get_stride(es->buffer_ref.buffer->dmabuf_buffer); + s = wl_dmabuf_buffer_get_data(es->buffer_ref.buffer->dmabuf_buffer); + if (!s) { + weston_log("failed to get data from dmabuf buffer"); + return; + } + } for (i = 0; i < es->geometry.height; i++) memcpy(buf + i * 64, s + i * stride, es->geometry.width * 4);
+ if (wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) + wl_dmabuf_buffer_put_data(es->buffer_ref.buffer->dmabuf_buffer); + if (gbm_bo_write(bo, buf, sizeof buf) < 0) weston_log("failed update cursor: %m\n");
@@ -1044,7 +1061,8 @@ drm_assign_planes(struct weston_output *output) * non-shm, or small enough to be a cursor */ if ((es->buffer_ref.buffer && - !wl_shm_buffer_get(es->buffer_ref.buffer->resource)) || + (!wl_shm_buffer_get(es->buffer_ref.buffer->resource)) + && !wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) || (es->geometry.width <= 64 && es->geometry.height <= 64)) es->keep_buffer = 1; else @@ -1268,6 +1286,57 @@ init_drm(struct drm_compositor *ec, struct udev_device *device) return 0; }
+static void dmabuf_send_supported_formats(void *user_data, struct wl_resource *resource) +{ + struct drm_compositor *ec = user_data; + drmModePlaneRes *plane_resources; + unsigned int i, j; + + weston_log("send supported formats\n"); + + plane_resources = drmModeGetPlaneResources(ec->drm.fd); + if (!plane_resources) + return; + + for (i = 0; i < plane_resources->count_planes; i++) { + drmModePlane *ovr = drmModeGetPlane(ec->drm.fd, plane_resources->planes[i]); + + for (j = 0 ; j < ovr->count_formats; j++) { + weston_log("server support format %4.4s\n", (char *)&ovr->formats[j]); + wl_dmabuf_send_format(resource, ovr->formats[j]); + } + drmModeFreePlane(ovr); + } + + drmModeFreePlaneResources(plane_resources); +} + +static void dmabuf_send_device_name(void *user_data, struct wl_resource *resource) +{ + struct drm_compositor *ec = user_data; + weston_log("send device name %s\n", ec->drm.filename); + + wl_dmabuf_send_device(resource, ec->drm.filename); +} + +static void dmabuf_send_server_info(void *user_data, struct wl_resource *resource) +{ + dmabuf_send_supported_formats(user_data, resource); + dmabuf_send_device_name(user_data, resource); +} + +static struct wl_dmabuf_callbacks wl_dmabuf_callbacks = { + dmabuf_send_server_info +}; + +static int +init_dmabuf_protocol(struct drm_compositor *ec) +{ + wl_dmabuf_init(ec->base.wl_display, + &wl_dmabuf_callbacks, ec, 0); + return 0; +} + static int init_egl(struct drm_compositor *ec) { @@ -1955,6 +2024,10 @@ create_output_for_connector(struct drm_compositor *ec, weston_log("Failed to init output pixman state\n"); goto err_output; } + if (init_dmabuf_protocol(ec) < 0) { + weston_log("Failed to init wl_dmabuf server\n"); + goto err_output; + } } else if (drm_output_init_egl(output, ec) < 0) { weston_log("Failed to init output gl state\n"); goto err_output; diff --git a/src/compositor.c b/src/compositor.c index 4cb44e5..037b695 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -1257,7 +1257,9 @@ surface_accumulate_damage(struct weston_surface *surface, pixman_region32_t *opaque) { if (surface->buffer_ref.buffer && - wl_shm_buffer_get(surface->buffer_ref.buffer->resource)) + (wl_shm_buffer_get(surface->buffer_ref.buffer->resource) + || wl_dmabuf_buffer_get(surface->buffer_ref.buffer->resource)) + ) surface->compositor->renderer->flush_damage(surface);
if (surface->transform.enabled) { diff --git a/src/compositor.h b/src/compositor.h index 0dcb604..537a9b7 100644 --- a/src/compositor.h +++ b/src/compositor.h @@ -33,6 +33,7 @@ extern "C" {
#define WL_HIDE_DEPRECATED #include <wayland-server.h> +#include <wayland-dmabuf.h>
#include "version.h" #include "matrix.h" @@ -626,6 +627,7 @@ struct weston_buffer {
union { struct wl_shm_buffer *shm_buffer; + struct wl_dmabuf_buffer *dmabuf_buffer; void *legacy_buffer; }; int32_t width, height; diff --git a/src/pixman-renderer.c b/src/pixman-renderer.c index 987c539..0a88c58 100644 --- a/src/pixman-renderer.c +++ b/src/pixman-renderer.c @@ -528,11 +528,19 @@ pixman_renderer_flush_damage(struct weston_surface *surface) /* No-op for pixman renderer */ }
+static void wl_dmabuf_pixman_destroy (pixman_image_t *image, void *data) +{ + struct wl_dmabuf_buffer *dmabuf_buffer = data; + wl_dmabuf_buffer_put_data(dmabuf_buffer); +} + static void pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer) { struct pixman_surface_state *ps = get_surface_state(es); struct wl_shm_buffer *shm_buffer; + struct wl_dmabuf_buffer *dmabuf_buffer; + pixman_format_code_t pixman_format;
weston_buffer_reference(&ps->buffer_ref, buffer); @@ -544,40 +552,77 @@ pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer)
if (!buffer) return; - + shm_buffer = wl_shm_buffer_get(buffer->resource); + dmabuf_buffer = wl_dmabuf_buffer_get(buffer->resource);
- if (! shm_buffer) { - weston_log("Pixman renderer supports only SHM buffers\n"); + if (!shm_buffer && !dmabuf_buffer) { + weston_log("Pixman renderer supports only SHM and DMABUF buffers\n"); weston_buffer_reference(&ps->buffer_ref, NULL); return; }
- switch (wl_shm_buffer_get_format(shm_buffer)) { - case WL_SHM_FORMAT_XRGB8888: - pixman_format = PIXMAN_x8r8g8b8; - break; - case WL_SHM_FORMAT_ARGB8888: - pixman_format = PIXMAN_a8r8g8b8; - break; - case WL_SHM_FORMAT_RGB565: - pixman_format = PIXMAN_r5g6b5; + if (shm_buffer) { + switch (wl_shm_buffer_get_format(shm_buffer)) { + case WL_SHM_FORMAT_XRGB8888: + pixman_format = PIXMAN_x8r8g8b8; + break; + case WL_SHM_FORMAT_ARGB8888: + pixman_format = PIXMAN_a8r8g8b8; + break; + case WL_SHM_FORMAT_RGB565: + pixman_format = PIXMAN_r5g6b5; + break; + default: + weston_log("Unsupported SHM buffer format\n"); + weston_buffer_reference(&ps->buffer_ref, NULL); + return; break; - default: - weston_log("Unsupported SHM buffer format\n"); - weston_buffer_reference(&ps->buffer_ref, NULL); - return; - break; + } + + buffer->shm_buffer = shm_buffer; + buffer->width = wl_shm_buffer_get_width(shm_buffer); + buffer->height = wl_shm_buffer_get_height(shm_buffer); + + ps->image = pixman_image_create_bits(pixman_format, + buffer->width, buffer->height, + wl_shm_buffer_get_data(shm_buffer), + wl_shm_buffer_get_stride(shm_buffer)); }
- buffer->shm_buffer = shm_buffer; - buffer->width = wl_shm_buffer_get_width(shm_buffer); - buffer->height = wl_shm_buffer_get_height(shm_buffer); + if (dmabuf_buffer) { + void *data; + switch (wl_dmabuf_buffer_get_format(dmabuf_buffer)) { + case WL_DMABUF_FORMAT_XRGB8888: + pixman_format = PIXMAN_x8r8g8b8; + break; + case WL_DMABUF_FORMAT_ARGB8888: + pixman_format = PIXMAN_a8r8g8b8; + break; + case WL_DMABUF_FORMAT_RGB565: + pixman_format = PIXMAN_r5g6b5; + break; + default: + weston_log("Unsupported DMABUF buffer format\n"); + weston_buffer_reference(&ps->buffer_ref, NULL); + return; + break; + }
- ps->image = pixman_image_create_bits(pixman_format, - buffer->width, buffer->height, - wl_shm_buffer_get_data(shm_buffer), - wl_shm_buffer_get_stride(shm_buffer)); + buffer->dmabuf_buffer = dmabuf_buffer; + buffer->width = wl_dmabuf_buffer_get_width(dmabuf_buffer); + buffer->height = wl_dmabuf_buffer_get_height(dmabuf_buffer); + + data = wl_dmabuf_buffer_get_data(dmabuf_buffer); + if (data) { + ps->image = pixman_image_create_bits(pixman_format, + buffer->width, buffer->height, data, + wl_dmabuf_buffer_get_stride(dmabuf_buffer)); + + pixman_image_set_destroy_function(ps->image, wl_dmabuf_pixman_destroy, dmabuf_buffer); + } else + weston_log("failed to get data from dmabuf buffer"); + } }
static int
I think dma-buf mmap() used without care like this should be avoided at all cost, because it will make people happily start using it without thinking about non-coherent architectures where mmap would be painfully inefficient: This is because when the accelerator uses the dma-buf it has no idea what part of it was dirtied and may need to flush a much larger region than was actually touched by the CPU. Think of pixman writing a single char to a 1280x1024 canvas, which is then used by the compositor. A smart driver non-coherent would need to flush all pages touched. A not-so-smart driver would need to flush the whole buffer. For a single character.
Also even if a coherent driver implements mmap(), the underlying dma-buf may reside in write-combined memory and using it for compositing with pixman may actually be much slower than shmem, even with the extra shmem copy.
Conclusion: Avoid using dma-buf mmap() outside of drivers that know exactly what they are doing, and avoid it at all cost.
In particular, don't build it into generic code like this.
Thanks, Thomas
On 01/07/2014 06:41 PM, benjamin.gaignard@linaro.org wrote:
From: Benjamin Gaignard benjamin.gaignard@linaro.org
Make drm compositor aware of the wl_dmabuf protocol if pixman is used. Add functions to have a wl_dmabuf server inside drm compositor. Change pixman to let it know how use a wl_dmabuf buffer.
Signed-off-by: Benjamin Gaignard benjamin.gaignard@linaro.org
src/compositor-drm.c | 83 ++++++++++++++++++++++++++++++++++++++++--- src/compositor.c | 4 ++- src/compositor.h | 2 ++ src/pixman-renderer.c | 93 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 152 insertions(+), 30 deletions(-)
diff --git a/src/compositor-drm.c b/src/compositor-drm.c index 4f015d1..669ee45 100644 --- a/src/compositor-drm.c +++ b/src/compositor-drm.c @@ -49,6 +49,8 @@ #include "udev-seat.h" #include "launcher-util.h" #include "vaapi-recorder.h" +#include "wayland-dmabuf.h" +#include "wayland-dmabuf-server-protocol.h" #ifndef DRM_CAP_TIMESTAMP_MONOTONIC #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 @@ -826,7 +828,8 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base, if (es->alpha != 1.0f) return NULL;
- if (wl_shm_buffer_get(es->buffer_ref.buffer->resource))
- if (wl_shm_buffer_get(es->buffer_ref.buffer->resource)
return NULL;|| wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource))
if (!drm_surface_transform_supported(es)) @@ -951,7 +954,8 @@ drm_output_prepare_cursor_surface(struct weston_output *output_base, if (c->cursors_are_broken) return NULL; if (es->buffer_ref.buffer == NULL ||
!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
(!wl_shm_buffer_get(es->buffer_ref.buffer->resource) &&
es->geometry.width > 64 || es->geometry.height > 64) return NULL;!wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource))||
@@ -985,12 +989,25 @@ drm_output_set_cursor(struct drm_output *output) output->current_cursor ^= 1; bo = output->cursor_bo[output->current_cursor]; memset(buf, 0, sizeof buf);
stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer->shm_buffer);
s = wl_shm_buffer_get_data(es->buffer_ref.buffer->shm_buffer);
if (wl_shm_buffer_get(es->buffer_ref.buffer->resource)) {
stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer->shm_buffer);
s = wl_shm_buffer_get_data(es->buffer_ref.buffer->shm_buffer);
}
if (wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) {
stride = wl_dmabuf_buffer_get_stride(es->buffer_ref.buffer->dmabuf_buffer);
s = wl_dmabuf_buffer_get_data(es->buffer_ref.buffer->dmabuf_buffer);
if (!s) {
weston_log("failed to get data from dmabuf buffer");
return;
}
for (i = 0; i < es->geometry.height; i++) memcpy(buf + i * 64, s + i * stride, es->geometry.width * 4);}
if (wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource))
wl_dmabuf_buffer_put_data(es->buffer_ref.buffer->dmabuf_buffer);
- if (gbm_bo_write(bo, buf, sizeof buf) < 0) weston_log("failed update cursor: %m\n");
@@ -1044,7 +1061,8 @@ drm_assign_planes(struct weston_output *output) * non-shm, or small enough to be a cursor */ if ((es->buffer_ref.buffer &&
!wl_shm_buffer_get(es->buffer_ref.buffer->resource)) ||
(!wl_shm_buffer_get(es->buffer_ref.buffer->resource))
(es->geometry.width <= 64 && es->geometry.height <= 64)) es->keep_buffer = 1; else&& !wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) ||
@@ -1268,6 +1286,57 @@ init_drm(struct drm_compositor *ec, struct udev_device *device) return 0; } +static void dmabuf_send_supported_formats(void *user_data, struct wl_resource *resource) +{
- struct drm_compositor *ec = user_data;
- drmModePlaneRes *plane_resources;
- unsigned int i, j;
- weston_log("send supported formats\n");
- plane_resources = drmModeGetPlaneResources(ec->drm.fd);
- if (!plane_resources)
return;
- for (i = 0; i < plane_resources->count_planes; i++) {
drmModePlane *ovr = drmModeGetPlane(ec->drm.fd, plane_resources->planes[i]);
for (j = 0 ; j < ovr->count_formats; j++) {
weston_log("server support format %4.4s\n", (char *)&ovr->formats[j]);
wl_dmabuf_send_format(resource, ovr->formats[j]);
}
drmModeFreePlane(ovr);
- }
- drmModeFreePlaneResources(plane_resources);
+}
+static void dmabuf_send_device_name(void *user_data, struct wl_resource *resource) +{
- struct drm_compositor *ec = user_data;
- weston_log("send device name %s\n", ec->drm.filename);
- wl_dmabuf_send_device(resource, ec->drm.filename);
+}
+static void dmabuf_send_server_info(void *user_data, struct wl_resource *resource) +{
- dmabuf_send_supported_formats(user_data, resource);
- dmabuf_send_device_name(user_data, resource);
+}
+static struct wl_dmabuf_callbacks wl_dmabuf_callbacks = {
- dmabuf_send_server_info
+};
+static int +init_dmabuf_protocol(struct drm_compositor *ec) +{
- wl_dmabuf_init(ec->base.wl_display,
&wl_dmabuf_callbacks, ec, 0);
- return 0;
+}
static int init_egl(struct drm_compositor *ec) { @@ -1955,6 +2024,10 @@ create_output_for_connector(struct drm_compositor *ec, weston_log("Failed to init output pixman state\n"); goto err_output; }
if (init_dmabuf_protocol(ec) < 0) {
weston_log("Failed to init wl_dmabuf server\n");
goto err_output;
} else if (drm_output_init_egl(output, ec) < 0) { weston_log("Failed to init output gl state\n"); goto err_output;}
diff --git a/src/compositor.c b/src/compositor.c index 4cb44e5..037b695 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -1257,7 +1257,9 @@ surface_accumulate_damage(struct weston_surface *surface, pixman_region32_t *opaque) { if (surface->buffer_ref.buffer &&
wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
(wl_shm_buffer_get(surface->buffer_ref.buffer->resource)
|| wl_dmabuf_buffer_get(surface->buffer_ref.buffer->resource))
surface->compositor->renderer->flush_damage(surface);)
if (surface->transform.enabled) { diff --git a/src/compositor.h b/src/compositor.h index 0dcb604..537a9b7 100644 --- a/src/compositor.h +++ b/src/compositor.h @@ -33,6 +33,7 @@ extern "C" { #define WL_HIDE_DEPRECATED #include <wayland-server.h> +#include <wayland-dmabuf.h> #include "version.h" #include "matrix.h" @@ -626,6 +627,7 @@ struct weston_buffer { union { struct wl_shm_buffer *shm_buffer;
void *legacy_buffer; }; int32_t width, height;struct wl_dmabuf_buffer *dmabuf_buffer;
diff --git a/src/pixman-renderer.c b/src/pixman-renderer.c index 987c539..0a88c58 100644 --- a/src/pixman-renderer.c +++ b/src/pixman-renderer.c @@ -528,11 +528,19 @@ pixman_renderer_flush_damage(struct weston_surface *surface) /* No-op for pixman renderer */ } +static void wl_dmabuf_pixman_destroy (pixman_image_t *image, void *data) +{
- struct wl_dmabuf_buffer *dmabuf_buffer = data;
- wl_dmabuf_buffer_put_data(dmabuf_buffer);
+}
static void pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer) { struct pixman_surface_state *ps = get_surface_state(es); struct wl_shm_buffer *shm_buffer;
- struct wl_dmabuf_buffer *dmabuf_buffer;
- pixman_format_code_t pixman_format;
weston_buffer_reference(&ps->buffer_ref, buffer); @@ -544,40 +552,77 @@ pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer) if (!buffer) return;
- shm_buffer = wl_shm_buffer_get(buffer->resource);
- dmabuf_buffer = wl_dmabuf_buffer_get(buffer->resource);
- if (! shm_buffer) {
weston_log("Pixman renderer supports only SHM buffers\n");
- if (!shm_buffer && !dmabuf_buffer) {
weston_buffer_reference(&ps->buffer_ref, NULL); return; }weston_log("Pixman renderer supports only SHM and DMABUF buffers\n");
- switch (wl_shm_buffer_get_format(shm_buffer)) {
- case WL_SHM_FORMAT_XRGB8888:
pixman_format = PIXMAN_x8r8g8b8;
break;
- case WL_SHM_FORMAT_ARGB8888:
pixman_format = PIXMAN_a8r8g8b8;
break;
- case WL_SHM_FORMAT_RGB565:
pixman_format = PIXMAN_r5g6b5;
- if (shm_buffer) {
switch (wl_shm_buffer_get_format(shm_buffer)) {
case WL_SHM_FORMAT_XRGB8888:
pixman_format = PIXMAN_x8r8g8b8;
break;
case WL_SHM_FORMAT_ARGB8888:
pixman_format = PIXMAN_a8r8g8b8;
break;
case WL_SHM_FORMAT_RGB565:
pixman_format = PIXMAN_r5g6b5;
break;
default:
weston_log("Unsupported SHM buffer format\n");
weston_buffer_reference(&ps->buffer_ref, NULL);
break;return;
- default:
weston_log("Unsupported SHM buffer format\n");
weston_buffer_reference(&ps->buffer_ref, NULL);
return;
- break;
}
buffer->shm_buffer = shm_buffer;
buffer->width = wl_shm_buffer_get_width(shm_buffer);
buffer->height = wl_shm_buffer_get_height(shm_buffer);
ps->image = pixman_image_create_bits(pixman_format,
buffer->width, buffer->height,
wl_shm_buffer_get_data(shm_buffer),
}wl_shm_buffer_get_stride(shm_buffer));
- buffer->shm_buffer = shm_buffer;
- buffer->width = wl_shm_buffer_get_width(shm_buffer);
- buffer->height = wl_shm_buffer_get_height(shm_buffer);
- if (dmabuf_buffer) {
void *data;
switch (wl_dmabuf_buffer_get_format(dmabuf_buffer)) {
case WL_DMABUF_FORMAT_XRGB8888:
pixman_format = PIXMAN_x8r8g8b8;
break;
case WL_DMABUF_FORMAT_ARGB8888:
pixman_format = PIXMAN_a8r8g8b8;
break;
case WL_DMABUF_FORMAT_RGB565:
pixman_format = PIXMAN_r5g6b5;
break;
default:
weston_log("Unsupported DMABUF buffer format\n");
weston_buffer_reference(&ps->buffer_ref, NULL);
return;
break;
}
- ps->image = pixman_image_create_bits(pixman_format,
buffer->width, buffer->height,
wl_shm_buffer_get_data(shm_buffer),
wl_shm_buffer_get_stride(shm_buffer));
buffer->dmabuf_buffer = dmabuf_buffer;
buffer->width = wl_dmabuf_buffer_get_width(dmabuf_buffer);
buffer->height = wl_dmabuf_buffer_get_height(dmabuf_buffer);
data = wl_dmabuf_buffer_get_data(dmabuf_buffer);
if (data) {
ps->image = pixman_image_create_bits(pixman_format,
buffer->width, buffer->height, data,
wl_dmabuf_buffer_get_stride(dmabuf_buffer));
pixman_image_set_destroy_function(ps->image, wl_dmabuf_pixman_destroy, dmabuf_buffer);
} else
weston_log("failed to get data from dmabuf buffer");
- }
} static int
On Tue, Jan 07, 2014 at 07:37:11PM +0100, Thomas Hellstrom wrote:
I think dma-buf mmap() used without care like this should be avoided at all cost, because it will make people happily start using it without thinking about non-coherent architectures where mmap would be painfully inefficient: This is because when the accelerator uses the dma-buf it has no idea what part of it was dirtied and may need to flush a much larger region than was actually touched by the CPU. Think of pixman writing a single char to a 1280x1024 canvas, which is then used by the compositor. A smart driver non-coherent would need to flush all pages touched. A not-so-smart driver would need to flush the whole buffer. For a single character.
Also even if a coherent driver implements mmap(), the underlying dma-buf may reside in write-combined memory and using it for compositing with pixman may actually be much slower than shmem, even with the extra shmem copy.
Conclusion: Avoid using dma-buf mmap() outside of drivers that know exactly what they are doing, and avoid it at all cost.
In particular, don't build it into generic code like this.
Fully agreed. Furthermore there's not even a generic way to allocate dma-bufs (see my other mail) so without a real userspace gfx driver compononent on each side (compositor+client), which then also might need to share other metadata about a buffer I don't really see how this is useful. What's your intended use-case here? -Daniel
Thanks, Thomas
On 01/07/2014 06:41 PM, benjamin.gaignard@linaro.org wrote:
From: Benjamin Gaignard benjamin.gaignard@linaro.org
Make drm compositor aware of the wl_dmabuf protocol if pixman is used. Add functions to have a wl_dmabuf server inside drm compositor. Change pixman to let it know how use a wl_dmabuf buffer.
Signed-off-by: Benjamin Gaignard benjamin.gaignard@linaro.org
src/compositor-drm.c | 83 ++++++++++++++++++++++++++++++++++++++++--- src/compositor.c | 4 ++- src/compositor.h | 2 ++ src/pixman-renderer.c | 93 ++++++++++++++++++++++++++++++++++++------------- 4 files changed, 152 insertions(+), 30 deletions(-)
diff --git a/src/compositor-drm.c b/src/compositor-drm.c index 4f015d1..669ee45 100644 --- a/src/compositor-drm.c +++ b/src/compositor-drm.c @@ -49,6 +49,8 @@ #include "udev-seat.h" #include "launcher-util.h" #include "vaapi-recorder.h" +#include "wayland-dmabuf.h" +#include "wayland-dmabuf-server-protocol.h" #ifndef DRM_CAP_TIMESTAMP_MONOTONIC #define DRM_CAP_TIMESTAMP_MONOTONIC 0x6 @@ -826,7 +828,8 @@ drm_output_prepare_overlay_surface(struct weston_output *output_base, if (es->alpha != 1.0f) return NULL;
- if (wl_shm_buffer_get(es->buffer_ref.buffer->resource))
- if (wl_shm_buffer_get(es->buffer_ref.buffer->resource)
return NULL;|| wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource))
if (!drm_surface_transform_supported(es)) @@ -951,7 +954,8 @@ drm_output_prepare_cursor_surface(struct weston_output *output_base, if (c->cursors_are_broken) return NULL; if (es->buffer_ref.buffer == NULL ||
!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
(!wl_shm_buffer_get(es->buffer_ref.buffer->resource) &&
es->geometry.width > 64 || es->geometry.height > 64) return NULL;!wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource))||
@@ -985,12 +989,25 @@ drm_output_set_cursor(struct drm_output *output) output->current_cursor ^= 1; bo = output->cursor_bo[output->current_cursor]; memset(buf, 0, sizeof buf);
stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer->shm_buffer);
s = wl_shm_buffer_get_data(es->buffer_ref.buffer->shm_buffer);
if (wl_shm_buffer_get(es->buffer_ref.buffer->resource)) {
stride = wl_shm_buffer_get_stride(es->buffer_ref.buffer->shm_buffer);
s = wl_shm_buffer_get_data(es->buffer_ref.buffer->shm_buffer);
}
if (wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) {
stride = wl_dmabuf_buffer_get_stride(es->buffer_ref.buffer->dmabuf_buffer);
s = wl_dmabuf_buffer_get_data(es->buffer_ref.buffer->dmabuf_buffer);
if (!s) {
weston_log("failed to get data from dmabuf buffer");
return;
}
for (i = 0; i < es->geometry.height; i++) memcpy(buf + i * 64, s + i * stride, es->geometry.width * 4);}
if (wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource))
wl_dmabuf_buffer_put_data(es->buffer_ref.buffer->dmabuf_buffer);
- if (gbm_bo_write(bo, buf, sizeof buf) < 0) weston_log("failed update cursor: %m\n");
@@ -1044,7 +1061,8 @@ drm_assign_planes(struct weston_output *output) * non-shm, or small enough to be a cursor */ if ((es->buffer_ref.buffer &&
!wl_shm_buffer_get(es->buffer_ref.buffer->resource)) ||
(!wl_shm_buffer_get(es->buffer_ref.buffer->resource))
(es->geometry.width <= 64 && es->geometry.height <= 64)) es->keep_buffer = 1; else&& !wl_dmabuf_buffer_get(es->buffer_ref.buffer->resource)) ||
@@ -1268,6 +1286,57 @@ init_drm(struct drm_compositor *ec, struct udev_device *device) return 0; } +static void dmabuf_send_supported_formats(void *user_data, struct wl_resource *resource) +{
- struct drm_compositor *ec = user_data;
- drmModePlaneRes *plane_resources;
- unsigned int i, j;
- weston_log("send supported formats\n");
- plane_resources = drmModeGetPlaneResources(ec->drm.fd);
- if (!plane_resources)
return;
- for (i = 0; i < plane_resources->count_planes; i++) {
drmModePlane *ovr = drmModeGetPlane(ec->drm.fd, plane_resources->planes[i]);
for (j = 0 ; j < ovr->count_formats; j++) {
weston_log("server support format %4.4s\n", (char *)&ovr->formats[j]);
wl_dmabuf_send_format(resource, ovr->formats[j]);
}
drmModeFreePlane(ovr);
- }
- drmModeFreePlaneResources(plane_resources);
+}
+static void dmabuf_send_device_name(void *user_data, struct wl_resource *resource) +{
- struct drm_compositor *ec = user_data;
- weston_log("send device name %s\n", ec->drm.filename);
- wl_dmabuf_send_device(resource, ec->drm.filename);
+}
+static void dmabuf_send_server_info(void *user_data, struct wl_resource *resource) +{
- dmabuf_send_supported_formats(user_data, resource);
- dmabuf_send_device_name(user_data, resource);
+}
+static struct wl_dmabuf_callbacks wl_dmabuf_callbacks = {
- dmabuf_send_server_info
+};
+static int +init_dmabuf_protocol(struct drm_compositor *ec) +{
- wl_dmabuf_init(ec->base.wl_display,
&wl_dmabuf_callbacks, ec, 0);
- return 0;
+}
static int init_egl(struct drm_compositor *ec) { @@ -1955,6 +2024,10 @@ create_output_for_connector(struct drm_compositor *ec, weston_log("Failed to init output pixman state\n"); goto err_output; }
if (init_dmabuf_protocol(ec) < 0) {
weston_log("Failed to init wl_dmabuf server\n");
goto err_output;
} else if (drm_output_init_egl(output, ec) < 0) { weston_log("Failed to init output gl state\n"); goto err_output;}
diff --git a/src/compositor.c b/src/compositor.c index 4cb44e5..037b695 100644 --- a/src/compositor.c +++ b/src/compositor.c @@ -1257,7 +1257,9 @@ surface_accumulate_damage(struct weston_surface *surface, pixman_region32_t *opaque) { if (surface->buffer_ref.buffer &&
wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
(wl_shm_buffer_get(surface->buffer_ref.buffer->resource)
|| wl_dmabuf_buffer_get(surface->buffer_ref.buffer->resource))
surface->compositor->renderer->flush_damage(surface);)
if (surface->transform.enabled) { diff --git a/src/compositor.h b/src/compositor.h index 0dcb604..537a9b7 100644 --- a/src/compositor.h +++ b/src/compositor.h @@ -33,6 +33,7 @@ extern "C" { #define WL_HIDE_DEPRECATED #include <wayland-server.h> +#include <wayland-dmabuf.h> #include "version.h" #include "matrix.h" @@ -626,6 +627,7 @@ struct weston_buffer { union { struct wl_shm_buffer *shm_buffer;
void *legacy_buffer; }; int32_t width, height;struct wl_dmabuf_buffer *dmabuf_buffer;
diff --git a/src/pixman-renderer.c b/src/pixman-renderer.c index 987c539..0a88c58 100644 --- a/src/pixman-renderer.c +++ b/src/pixman-renderer.c @@ -528,11 +528,19 @@ pixman_renderer_flush_damage(struct weston_surface *surface) /* No-op for pixman renderer */ } +static void wl_dmabuf_pixman_destroy (pixman_image_t *image, void *data) +{
- struct wl_dmabuf_buffer *dmabuf_buffer = data;
- wl_dmabuf_buffer_put_data(dmabuf_buffer);
+}
static void pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer) { struct pixman_surface_state *ps = get_surface_state(es); struct wl_shm_buffer *shm_buffer;
- struct wl_dmabuf_buffer *dmabuf_buffer;
- pixman_format_code_t pixman_format;
weston_buffer_reference(&ps->buffer_ref, buffer); @@ -544,40 +552,77 @@ pixman_renderer_attach(struct weston_surface *es, struct weston_buffer *buffer) if (!buffer) return;
- shm_buffer = wl_shm_buffer_get(buffer->resource);
- dmabuf_buffer = wl_dmabuf_buffer_get(buffer->resource);
- if (! shm_buffer) {
weston_log("Pixman renderer supports only SHM buffers\n");
- if (!shm_buffer && !dmabuf_buffer) {
weston_buffer_reference(&ps->buffer_ref, NULL); return; }weston_log("Pixman renderer supports only SHM and DMABUF buffers\n");
- switch (wl_shm_buffer_get_format(shm_buffer)) {
- case WL_SHM_FORMAT_XRGB8888:
pixman_format = PIXMAN_x8r8g8b8;
break;
- case WL_SHM_FORMAT_ARGB8888:
pixman_format = PIXMAN_a8r8g8b8;
break;
- case WL_SHM_FORMAT_RGB565:
pixman_format = PIXMAN_r5g6b5;
- if (shm_buffer) {
switch (wl_shm_buffer_get_format(shm_buffer)) {
case WL_SHM_FORMAT_XRGB8888:
pixman_format = PIXMAN_x8r8g8b8;
break;
case WL_SHM_FORMAT_ARGB8888:
pixman_format = PIXMAN_a8r8g8b8;
break;
case WL_SHM_FORMAT_RGB565:
pixman_format = PIXMAN_r5g6b5;
break;
default:
weston_log("Unsupported SHM buffer format\n");
weston_buffer_reference(&ps->buffer_ref, NULL);
break;return;
- default:
weston_log("Unsupported SHM buffer format\n");
weston_buffer_reference(&ps->buffer_ref, NULL);
return;
- break;
}
buffer->shm_buffer = shm_buffer;
buffer->width = wl_shm_buffer_get_width(shm_buffer);
buffer->height = wl_shm_buffer_get_height(shm_buffer);
ps->image = pixman_image_create_bits(pixman_format,
buffer->width, buffer->height,
wl_shm_buffer_get_data(shm_buffer),
}wl_shm_buffer_get_stride(shm_buffer));
- buffer->shm_buffer = shm_buffer;
- buffer->width = wl_shm_buffer_get_width(shm_buffer);
- buffer->height = wl_shm_buffer_get_height(shm_buffer);
- if (dmabuf_buffer) {
void *data;
switch (wl_dmabuf_buffer_get_format(dmabuf_buffer)) {
case WL_DMABUF_FORMAT_XRGB8888:
pixman_format = PIXMAN_x8r8g8b8;
break;
case WL_DMABUF_FORMAT_ARGB8888:
pixman_format = PIXMAN_a8r8g8b8;
break;
case WL_DMABUF_FORMAT_RGB565:
pixman_format = PIXMAN_r5g6b5;
break;
default:
weston_log("Unsupported DMABUF buffer format\n");
weston_buffer_reference(&ps->buffer_ref, NULL);
return;
break;
}
- ps->image = pixman_image_create_bits(pixman_format,
buffer->width, buffer->height,
wl_shm_buffer_get_data(shm_buffer),
wl_shm_buffer_get_stride(shm_buffer));
buffer->dmabuf_buffer = dmabuf_buffer;
buffer->width = wl_dmabuf_buffer_get_width(dmabuf_buffer);
buffer->height = wl_dmabuf_buffer_get_height(dmabuf_buffer);
data = wl_dmabuf_buffer_get_data(dmabuf_buffer);
if (data) {
ps->image = pixman_image_create_bits(pixman_format,
buffer->width, buffer->height, data,
wl_dmabuf_buffer_get_stride(dmabuf_buffer));
pixman_image_set_destroy_function(ps->image, wl_dmabuf_pixman_destroy, dmabuf_buffer);
} else
weston_log("failed to get data from dmabuf buffer");
- }
} static int
wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel
On Tue, Jan 7, 2014 at 1:37 PM, Thomas Hellstrom thellstrom@vmware.com wrote:
Conclusion: Avoid using dma-buf mmap() outside of drivers that know exactly what they are doing, and avoid it at all cost.
well, to be fair, if you are using a gpu on the client side and pixman backend on the server side, you probably deserve the results.
Not to say that we shouldn't come up with a better way for sw access to dmabuf, but just saying there are folks out there who would find Benjamin's patch useful in it's current state (for example, platforms where you have open src bits for video decoder but not for gpu).
So I wouldn't block this patch strictly because we don't have a better mmap story yet.
BR, -R
Am Freitag, den 10.01.2014, 10:23 -0500 schrieb Rob Clark:
On Tue, Jan 7, 2014 at 1:37 PM, Thomas Hellstrom thellstrom@vmware.com wrote:
Conclusion: Avoid using dma-buf mmap() outside of drivers that know exactly what they are doing, and avoid it at all cost.
well, to be fair, if you are using a gpu on the client side and pixman backend on the server side, you probably deserve the results.
Not to say that we shouldn't come up with a better way for sw access to dmabuf, but just saying there are folks out there who would find Benjamin's patch useful in it's current state (for example, platforms where you have open src bits for video decoder but not for gpu).
So I wouldn't block this patch strictly because we don't have a better mmap story yet.
Even with pixman on the server side transferring DMA-BUFs can be a big win, if your display subsystem is able to do the heavy-lifting with planes. I've a prototype where we do this exact thing: use pixman for the shell stuff and play video on a drm_plane.
And even mmap doesn't has to be as sucky as it is ATM, when someone gets some traction behind the bracketed stuff.
Regards, Lucas
On 01/10/2014 04:23 PM, Rob Clark wrote:
On Tue, Jan 7, 2014 at 1:37 PM, Thomas Hellstrom thellstrom@vmware.com wrote:
Conclusion: Avoid using dma-buf mmap() outside of drivers that know exactly what they are doing, and avoid it at all cost.
well, to be fair, if you are using a gpu on the client side and pixman backend on the server side, you probably deserve the results.
Not to say that we shouldn't come up with a better way for sw access to dmabuf, but just saying there are folks out there who would find Benjamin's patch useful in it's current state (for example, platforms where you have open src bits for video decoder but not for gpu).
So I wouldn't block this patch strictly because we don't have a better mmap story yet.
I'm not sure I am in a position to block anything here, but in any case I disagree.
Simply because if we allow this now, I'm quite sure it will spread because there are other people that will find this useful and use this implementation as an example and a motivation for doing the same thing in other places. IMHO to allow dma-buf mmap into generic code, it needs to be accompanied by a damage interface that is mandatory and makes people think twice.
/Thomas
BR, -R
On Fri, Jan 10, 2014 at 10:46 AM, Thomas Hellstrom thellstrom@vmware.com wrote:
On 01/10/2014 04:23 PM, Rob Clark wrote:
On Tue, Jan 7, 2014 at 1:37 PM, Thomas Hellstrom thellstrom@vmware.com wrote:
Conclusion: Avoid using dma-buf mmap() outside of drivers that know exactly what they are doing, and avoid it at all cost.
well, to be fair, if you are using a gpu on the client side and pixman backend on the server side, you probably deserve the results.
Not to say that we shouldn't come up with a better way for sw access to dmabuf, but just saying there are folks out there who would find Benjamin's patch useful in it's current state (for example, platforms where you have open src bits for video decoder but not for gpu).
So I wouldn't block this patch strictly because we don't have a better mmap story yet.
I'm not sure I am in a position to block anything here, but in any case I disagree.
Simply because if we allow this now, I'm quite sure it will spread because there are other people that will find this useful and use this implementation as an example and a motivation for doing the same thing in other places. IMHO to allow dma-buf mmap into generic code, it needs to be accompanied by a damage interface that is mandatory and makes people think twice.
Perhaps it would be worth including with this code a comment explaining that cpu access like this to buffers shared with a gpu is going to be a pretty bad slow-path.. for this particular case, I don't see any problem, since sw composition of gpu rendered content is a pretty lame idea. But the warning comment would discourage against using this approach more widely.
BR, -R
/Thomas
BR, -R
From: Benjamin Gaignard benjamin.gaignard@linaro.org
It is a simple example of how use wl_dmabuf protocol with weston
Signed-off-by: Benjamin Gaignard benjamin.gaignard@linaro.org --- clients/Makefile.am | 11 ++ clients/simple-dmabuf.c | 469 +++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 5 + 3 files changed, 485 insertions(+) create mode 100644 clients/simple-dmabuf.c
diff --git a/clients/Makefile.am b/clients/Makefile.am index 032d900..3840079 100644 --- a/clients/Makefile.am +++ b/clients/Makefile.am @@ -52,6 +52,17 @@ weston_multi_resource_SOURCES = multi-resource.c \ ../shared/os-compatibility.h weston_multi_resource_CPPFLAGS = $(SIMPLE_CLIENT_CFLAGS) weston_multi_resource_LDADD = $(SIMPLE_CLIENT_LIBS) -lm + +if BUILD_SIMPLE_CLIENT_DMABUF +simple_clients_programs += \ + weston-simple-dmabuf + +weston_simple_dmabuf_SOURCES = simple-dmabuf.c \ + ../shared/os-compatibility.c \ + ../shared/os-compatibility.h +weston_simple_dmabuf_CPPFLAGS = $(SIMPLE_CLIENT_CFLAGS) $(SIMPLE_CLIENT_DMABUF_CFLAGS) +weston_simple_dmabuf_LDADD = $(SIMPLE_CLIENT_LIBS) $(SIMPLE_CLIENT_DMABUF_LIBS) +endif endif
if BUILD_SIMPLE_EGL_CLIENTS diff --git a/clients/simple-dmabuf.c b/clients/simple-dmabuf.c new file mode 100644 index 0000000..7f381ae --- /dev/null +++ b/clients/simple-dmabuf.c @@ -0,0 +1,469 @@ +/* + * Copyright 2014 Benjamin Gaignard for Linaro + * Copyright © 2011 Benjamin Franzke + * Copyright © 2010 Intel Corporation + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <assert.h> +#include <unistd.h> +#include <sys/types.h> +#include <fcntl.h> +#include <sys/mman.h> +#include <signal.h> + +#include <wayland-client.h> +#include <wayland-dmabuf-client-protocol.h> +#include <xf86drm.h> + +#include "../shared/os-compatibility.h" + +struct display { + struct wl_display *display; + struct wl_registry *registry; + struct wl_compositor *compositor; + struct wl_shell *shell; + struct wl_dmabuf *dmabuf; + uint32_t format; + int drm_fd; +}; + +struct buffer { + struct wl_buffer *buffer; + int fd; + int busy; + uint32_t size; + struct display *display; + void *dmabuf_data; +}; + +struct window { + struct display *display; + int width, height; + struct wl_surface *surface; + struct wl_shell_surface *shell_surface; + struct buffer buffers[2]; + struct buffer *prev_buffer; + struct wl_callback *callback; +}; + +static void +buffer_release(void *data, struct wl_buffer *buffer) +{ + struct buffer *mybuf = data; + + mybuf->busy = 0; +} + +static const struct wl_buffer_listener buffer_listener = { + buffer_release +}; + +static int +create_dmabuf_buffer(struct display *display, struct buffer *buffer, + int width, int height, uint32_t format) +{ + struct drm_mode_create_dumb create_arg; + int ret; + int prime_fd; + uint32_t stride; + + memset (&create_arg, 0, sizeof (create_arg)); + create_arg.bpp = 32; + create_arg.width = width; + create_arg.height = height; + + ret = drmIoctl (display->drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg); + if (ret) + return ret; + + stride = create_arg.pitch; + + ret = drmPrimeHandleToFD (display->drm_fd, create_arg.handle, DRM_CLOEXEC, &prime_fd); + if (ret) + return ret; + + buffer->buffer = wl_dmabuf_create_prime_buffer (display->dmabuf, prime_fd, + width, height, format, stride); + + wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer); + + buffer->fd = prime_fd; + buffer->display = display; + + buffer->size = width * height * 4; + + buffer->dmabuf_data = mmap (0, buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED, buffer->fd, 0); + + return 0; +} + +static void +destroy_dmabuf_buffer(struct buffer *buffer) +{ + struct display *display = buffer->display; + struct drm_mode_destroy_dumb destroy_arg; + + munmap (buffer->dmabuf_data, buffer->size); + + memset (&destroy_arg, 0, sizeof destroy_arg); + drmPrimeFDToHandle (display->drm_fd, buffer->fd, &destroy_arg.handle); + drmIoctl (display->drm_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg); + close (buffer->fd); +} + +static void +handle_ping(void *data, struct wl_shell_surface *shell_surface, + uint32_t serial) +{ + wl_shell_surface_pong(shell_surface, serial); +} + +static void +handle_configure(void *data, struct wl_shell_surface *shell_surface, + uint32_t edges, int32_t width, int32_t height) +{ +} + +static void +handle_popup_done(void *data, struct wl_shell_surface *shell_surface) +{ +} + +static const struct wl_shell_surface_listener shell_surface_listener = { + handle_ping, + handle_configure, + handle_popup_done +}; + +static struct window * +create_window(struct display *display, int width, int height) +{ + struct window *window; + + window = calloc(1, sizeof *window); + if (!window) + return NULL; + + window->callback = NULL; + window->display = display; + window->width = width; + window->height = height; + window->surface = wl_compositor_create_surface(display->compositor); + window->shell_surface = wl_shell_get_shell_surface(display->shell, + window->surface); + + if (window->shell_surface) + wl_shell_surface_add_listener(window->shell_surface, + &shell_surface_listener, window); + + wl_shell_surface_set_title(window->shell_surface, "simple-dmabuf"); + + wl_shell_surface_set_toplevel(window->shell_surface); + + return window; +} + +static void +destroy_window(struct window *window) +{ + if (window->callback) + wl_callback_destroy(window->callback); + + if (window->buffers[0].buffer) + wl_buffer_destroy(window->buffers[0].buffer); + if (window->buffers[1].buffer) + wl_buffer_destroy(window->buffers[1].buffer); + + destroy_dmabuf_buffer(&window->buffers[0]); + destroy_dmabuf_buffer(&window->buffers[1]); + + wl_shell_surface_destroy(window->shell_surface); + wl_surface_destroy(window->surface); + free(window); +} + +static struct buffer * +window_next_buffer(struct window *window) +{ + struct buffer *buffer; + int ret = 0; + + if (!window->buffers[0].busy) + buffer = &window->buffers[0]; + else if (!window->buffers[1].busy) + buffer = &window->buffers[1]; + else + return NULL; + + if (!buffer->buffer) { + ret = create_dmabuf_buffer(window->display, buffer, + window->width, window->height, + WL_DMABUF_FORMAT_XRGB8888); + + if (ret) + return NULL; + + /* paint the padding */ + memset(buffer->dmabuf_data, 0xff, window->width * window->height * 4); + } + + return buffer; +} + +static void +paint_pixels(void *image, int padding, int width, int height, uint32_t time) +{ + const int halfh = padding + (height - padding * 2) / 2; + const int halfw = padding + (width - padding * 2) / 2; + int ir, or; + uint32_t *pixel = image; + int y; + + /* squared radii thresholds */ + or = (halfw < halfh ? halfw : halfh) - 8; + ir = or - 32; + or *= or; + ir *= ir; + + pixel += padding * width; + for (y = padding; y < height - padding; y++) { + int x; + int y2 = (y - halfh) * (y - halfh); + + pixel += padding; + for (x = padding; x < width - padding; x++) { + uint32_t v; + + /* squared distance from center */ + int r2 = (x - halfw) * (x - halfw) + y2; + + if (r2 < ir) + v = (r2 / 32 + time / 64) * 0x0080401; + else if (r2 < or) + v = (y + time / 32) * 0x0080401; + else + v = (x + time / 16) * 0x0080401; + v &= 0x00ffffff; + + /* cross if compositor uses X from XRGB as alpha */ + if (abs(x - y) > 6 && abs(x + y - height) > 6) + v |= 0xff000000; + + *pixel++ = v; + } + + pixel += padding; + } +} + +static const struct wl_callback_listener frame_listener; + +static void +redraw(void *data, struct wl_callback *callback, uint32_t time) +{ + struct window *window = data; + struct buffer *buffer; + + buffer = window_next_buffer(window); + if (!buffer) { + fprintf(stderr, + !callback ? "Failed to create the first buffer.\n" : + "Both buffers busy at redraw(). Server bug?\n"); + abort(); + } + + paint_pixels(buffer->dmabuf_data, 20, window->width, window->height, time); + + wl_surface_attach(window->surface, buffer->buffer, 0, 0); + wl_surface_damage(window->surface, + 20, 20, window->width - 40, window->height - 40); + + if (callback) + wl_callback_destroy(callback); + + window->callback = wl_surface_frame(window->surface); + wl_callback_add_listener(window->callback, &frame_listener, window); + wl_surface_commit(window->surface); + buffer->busy = 1; +} + +static const struct wl_callback_listener frame_listener = { + redraw +}; + +static void +dmabuf_handle_format(void *data, struct wl_dmabuf *wl_dmabuf, uint32_t format) +{ + struct display *d = data; + + fprintf(stderr, "server supported format %.4s\n", (char *) &format); + + if (format == WL_DMABUF_FORMAT_XRGB8888) + d->format = format; +} + +static void +dmabuf_handle_device (void *data, struct wl_dmabuf *wl_dmabuf, char *name) +{ + struct display *d = data; + + fprintf(stderr, "server %s\n", name); + d->drm_fd = open (name, O_RDWR, 0); +} + +struct wl_dmabuf_listener dmabuf_listener = { + dmabuf_handle_format, + dmabuf_handle_device +}; + +static void +registry_handle_global(void *data, struct wl_registry *registry, + uint32_t id, const char *interface, uint32_t version) +{ + struct display *d = data; + + if (strcmp(interface, "wl_compositor") == 0) { + d->compositor = + wl_registry_bind(registry, + id, &wl_compositor_interface, 1); + } else if (strcmp(interface, "wl_shell") == 0) { + d->shell = wl_registry_bind(registry, + id, &wl_shell_interface, 1); + } else if (strcmp(interface, "wl_dmabuf") == 0) { + d->dmabuf = wl_registry_bind(registry, + id, &wl_dmabuf_interface, 1); + wl_dmabuf_add_listener(d->dmabuf, &dmabuf_listener, d); + } +} + +static void +registry_handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) +{ +} + +static const struct wl_registry_listener registry_listener = { + registry_handle_global, + registry_handle_global_remove +}; + +static struct display * +create_display(void) +{ + struct display *display; + + display = malloc(sizeof *display); + if (display == NULL) { + fprintf(stderr, "out of memory\n"); + exit(1); + } + display->display = wl_display_connect(NULL); + assert(display->display); + + display->format = 0; + display->drm_fd = -1; + display->registry = wl_display_get_registry(display->display); + wl_registry_add_listener(display->registry, + ®istry_listener, display); + wl_display_roundtrip(display->display); + if (display->dmabuf == NULL) { + fprintf(stderr, "No wl_dmabuf global\n"); + exit(1); + } + + wl_display_roundtrip(display->display); + + if (display->format != WL_DMABUF_FORMAT_XRGB8888) { + fprintf(stderr, "WL_DMABUF_FORMAT_XRGB8888 not available\n"); + exit(1); + } + + wl_display_get_fd(display->display); + + return display; +} + +static void +destroy_display(struct display *display) +{ + if (display->dmabuf) + wl_dmabuf_destroy(display->dmabuf); + + if (display->shell) + wl_shell_destroy(display->shell); + + if (display->compositor) + wl_compositor_destroy(display->compositor); + + wl_registry_destroy(display->registry); + wl_display_flush(display->display); + wl_display_disconnect(display->display); + close(display->drm_fd); + free(display); +} + +static int running = 1; + +static void +signal_int(int signum) +{ + running = 0; +} + +int +main(int argc, char **argv) +{ + struct sigaction sigint; + struct display *display; + struct window *window; + int ret = 0; + + display = create_display(); + window = create_window(display, 250, 250); + if (!window) + return 1; + + sigint.sa_handler = signal_int; + sigemptyset(&sigint.sa_mask); + sigint.sa_flags = SA_RESETHAND; + sigaction(SIGINT, &sigint, NULL); + + /* Initialise damage to full surface, so the padding gets painted */ + wl_surface_damage(window->surface, 0, 0, + window->width, window->height); + + redraw(window, NULL, 0); + + while (running && ret != -1) + ret = wl_display_dispatch(display->display); + + fprintf(stderr, "simple-dmabuf exiting\n"); + destroy_window(window); + destroy_display(display); + + return 0; +} diff --git a/configure.ac b/configure.ac index 50bce23..da445f3 100644 --- a/configure.ac +++ b/configure.ac @@ -271,6 +271,11 @@ AC_ARG_ENABLE(simple-clients, AM_CONDITIONAL(BUILD_SIMPLE_CLIENTS, test "x$enable_simple_clients" = "xyes") if test x$enable_simple_clients = xyes; then PKG_CHECK_MODULES(SIMPLE_CLIENT, [wayland-client]) + AC_CHECK_HEADER([wayland-dmabuf.h], + [ PKG_CHECK_MODULES(SIMPLE_CLIENT_DMABUF, [libdrm libkms] , + [have_dmabuf=yes], [have_dmabuf=no])], + [have_dmabuf=no]) + AM_CONDITIONAL(BUILD_SIMPLE_CLIENT_DMABUF, test "x$have_dmabuf" = "xyes") fi
AC_ARG_ENABLE(simple-egl-clients,
Sorry if this was discussed before, but any reason this is being put in Wayland, and in the "wl_" namespace? DMA-BUF is a private Linux kernel feature that's really semi-stable, not used by all drivers, and I'd hate for it to bit-rot and tell people not to use it, the same way wl_shell went. Having it only in the weston source tree, and in a private "dmabuf" namespace seems like it should be enough for now.
On Tue, Jan 7, 2014 at 12:41 PM, benjamin.gaignard@linaro.org wrote:
From: Benjamin Gaignard benjamin.gaignard@linaro.org
The goal of this serie of patches is to add a way to use dmabuf file descriptor inside wayland and weston.
In a context where there is no Mesa EGL (and so no wl_drm protocol) wl_dmabuf could be used as an alternative to shm to share buffers between hardware devices. If your hardware device (video decoder, renderer, etc...) need physical contiguous memory (obviously don't have MMU) wl_dmabuf may save the cost of one copy compare to shm.
shm case: videodecoder --(copy into shm_buffer)--> weston(+pixman) --> HW renderer
dmabuf case: videodecoder --(directly write in dmabuf buffer)--> weston(+pixman) --> HW renderer
The server is responsible to send its supported pixel formats and the device name to be used by the client to allocate buffers.
While mmap() call on dmabuf file descriptor result isn't guaranty on all architectures both server and client should take care of it before accessing to buffer data to avoid segfault.
This series of patches include wayland and weston modifications. An example of how use wl_dmabuf is provided in weston/clients/simple-dmabuf.c
=== Wayland === Benjamin Gaignard (1): Add wl_dmabuf protocol
protocol/Makefile.am | 6 +- protocol/wayland-dmabuf.xml | 128 ++++++++++++++++++++++++ src/Makefile.am | 12 ++- src/wayland-dmabuf.c | 231 +++++++++++++++++++++++++++++++++++++++++++ src/wayland-dmabuf.h | 123 +++++++++++++++++++++++ 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 protocol/wayland-dmabuf.xml create mode 100644 src/wayland-dmabuf.c create mode 100644 src/wayland-dmabuf.h
=== Weston === Benjamin Gaignard (2): compositor-drm: allow to be a wl_dmabuf server add simple-dmabuf client
clients/Makefile.am | 11 ++ clients/simple-dmabuf.c | 469 +++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 5 + src/compositor-drm.c | 83 ++++++++- src/compositor.c | 4 +- src/compositor.h | 2 + src/pixman-renderer.c | 93 +++++++--- 7 files changed, 637 insertions(+), 30 deletions(-) create mode 100644 clients/simple-dmabuf.c
-- 1.7.9.5
wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel
I suspect that this is somehow on the same level as wl_drm (which is in mesa, not wayland). The difference here is that since you can share buffers with various things (video codecs, cameras, etc), ie. things beyond just the gpu, it makes sense to put the protocol in some sort of common place rather than duplicating it in 20 places. Perhaps the thing to do is start creating some proto trees (git://.../wayland/drmproto ?) for things like this which need a common home but yet don't belong in wayland.git?
BR, -R
On Tue, Jan 7, 2014 at 12:57 PM, Jasper St. Pierre jstpierre@mecheye.net wrote:
Sorry if this was discussed before, but any reason this is being put in Wayland, and in the "wl_" namespace? DMA-BUF is a private Linux kernel feature that's really semi-stable, not used by all drivers, and I'd hate for it to bit-rot and tell people not to use it, the same way wl_shell went. Having it only in the weston source tree, and in a private "dmabuf" namespace seems like it should be enough for now.
On Tue, Jan 7, 2014 at 12:41 PM, benjamin.gaignard@linaro.org wrote:
From: Benjamin Gaignard benjamin.gaignard@linaro.org
The goal of this serie of patches is to add a way to use dmabuf file descriptor inside wayland and weston.
In a context where there is no Mesa EGL (and so no wl_drm protocol) wl_dmabuf could be used as an alternative to shm to share buffers between hardware devices. If your hardware device (video decoder, renderer, etc...) need physical contiguous memory (obviously don't have MMU) wl_dmabuf may save the cost of one copy compare to shm.
shm case: videodecoder --(copy into shm_buffer)--> weston(+pixman) --> HW renderer
dmabuf case: videodecoder --(directly write in dmabuf buffer)--> weston(+pixman) --> HW renderer
The server is responsible to send its supported pixel formats and the device name to be used by the client to allocate buffers.
While mmap() call on dmabuf file descriptor result isn't guaranty on all architectures both server and client should take care of it before accessing to buffer data to avoid segfault.
This series of patches include wayland and weston modifications. An example of how use wl_dmabuf is provided in weston/clients/simple-dmabuf.c
=== Wayland === Benjamin Gaignard (1): Add wl_dmabuf protocol
protocol/Makefile.am | 6 +- protocol/wayland-dmabuf.xml | 128 ++++++++++++++++++++++++ src/Makefile.am | 12 ++- src/wayland-dmabuf.c | 231 +++++++++++++++++++++++++++++++++++++++++++ src/wayland-dmabuf.h | 123 +++++++++++++++++++++++ 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 protocol/wayland-dmabuf.xml create mode 100644 src/wayland-dmabuf.c create mode 100644 src/wayland-dmabuf.h
=== Weston === Benjamin Gaignard (2): compositor-drm: allow to be a wl_dmabuf server add simple-dmabuf client
clients/Makefile.am | 11 ++ clients/simple-dmabuf.c | 469 +++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 5 + src/compositor-drm.c | 83 ++++++++- src/compositor.c | 4 +- src/compositor.h | 2 + src/pixman-renderer.c | 93 +++++++--- 7 files changed, 637 insertions(+), 30 deletions(-) create mode 100644 clients/simple-dmabuf.c
-- 1.7.9.5
wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel
-- Jasper
wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel
On Fri, Jan 10, 2014 at 9:58 AM, Rob Clark robdclark@gmail.com wrote:
I suspect that this is somehow on the same level as wl_drm (which is in mesa, not wayland). The difference here is that since you can share buffers with various things (video codecs, cameras, etc), ie. things beyond just the gpu, it makes sense to put the protocol in some sort of common place rather than duplicating it in 20 places. Perhaps the thing to do is start creating some proto trees (git://.../wayland/drmproto ?) for things like this which need a
bleh, 'send' to fast.. s/drmproto/dmabufproto/
common home but yet don't belong in wayland.git?
BR, -R
On Tue, Jan 7, 2014 at 12:57 PM, Jasper St. Pierre jstpierre@mecheye.net wrote:
Sorry if this was discussed before, but any reason this is being put in Wayland, and in the "wl_" namespace? DMA-BUF is a private Linux kernel feature that's really semi-stable, not used by all drivers, and I'd hate for it to bit-rot and tell people not to use it, the same way wl_shell went. Having it only in the weston source tree, and in a private "dmabuf" namespace seems like it should be enough for now.
On Tue, Jan 7, 2014 at 12:41 PM, benjamin.gaignard@linaro.org wrote:
From: Benjamin Gaignard benjamin.gaignard@linaro.org
The goal of this serie of patches is to add a way to use dmabuf file descriptor inside wayland and weston.
In a context where there is no Mesa EGL (and so no wl_drm protocol) wl_dmabuf could be used as an alternative to shm to share buffers between hardware devices. If your hardware device (video decoder, renderer, etc...) need physical contiguous memory (obviously don't have MMU) wl_dmabuf may save the cost of one copy compare to shm.
shm case: videodecoder --(copy into shm_buffer)--> weston(+pixman) --> HW renderer
dmabuf case: videodecoder --(directly write in dmabuf buffer)--> weston(+pixman) --> HW renderer
The server is responsible to send its supported pixel formats and the device name to be used by the client to allocate buffers.
While mmap() call on dmabuf file descriptor result isn't guaranty on all architectures both server and client should take care of it before accessing to buffer data to avoid segfault.
This series of patches include wayland and weston modifications. An example of how use wl_dmabuf is provided in weston/clients/simple-dmabuf.c
=== Wayland === Benjamin Gaignard (1): Add wl_dmabuf protocol
protocol/Makefile.am | 6 +- protocol/wayland-dmabuf.xml | 128 ++++++++++++++++++++++++ src/Makefile.am | 12 ++- src/wayland-dmabuf.c | 231 +++++++++++++++++++++++++++++++++++++++++++ src/wayland-dmabuf.h | 123 +++++++++++++++++++++++ 5 files changed, 496 insertions(+), 4 deletions(-) create mode 100644 protocol/wayland-dmabuf.xml create mode 100644 src/wayland-dmabuf.c create mode 100644 src/wayland-dmabuf.h
=== Weston === Benjamin Gaignard (2): compositor-drm: allow to be a wl_dmabuf server add simple-dmabuf client
clients/Makefile.am | 11 ++ clients/simple-dmabuf.c | 469 +++++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 5 + src/compositor-drm.c | 83 ++++++++- src/compositor.c | 4 +- src/compositor.h | 2 + src/pixman-renderer.c | 93 +++++++--- 7 files changed, 637 insertions(+), 30 deletions(-) create mode 100644 clients/simple-dmabuf.c
-- 1.7.9.5
wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel
-- Jasper
wayland-devel mailing list wayland-devel@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/wayland-devel
linaro-mm-sig@lists.linaro.org