Commit b1006684 authored by Alexandros Frantzis's avatar Alexandros Frantzis
Browse files

clients: Add simple-dmabuf-egl

Add a client that uses EGL/GLESv2 to draw to dmabuf buffers, utilizing
EGLImages and FBOs. The client uses GBM to create the dmabufs buffers.

The simple-dmabuf-egl client is partly based on patch [1] that changes
dmabuf clients to use GBM instead of libdrm code, but has been greatly
simplified since in this case we don't require direct pixel access or
non-RGBA formats.

[1] https://patchwork.freedesktop.org/patch/239796/

Signed-off-by: Alexandros Frantzis's avatarAlexandros Frantzis <alexandros.frantzis@collabora.com>
parent a0d9cc64
......@@ -680,6 +680,20 @@ weston_simple_dmabuf_v4l_CFLAGS = $(AM_CFLAGS) $(SIMPLE_DMABUF_V4L_CLIENT_CFLAGS
weston_simple_dmabuf_v4l_LDADD = $(SIMPLE_DMABUF_V4L_CLIENT_LIBS) libshared.la
endif
if BUILD_SIMPLE_DMABUF_EGL_CLIENT
demo_clients += weston-simple-dmabuf-egl
weston_simple_dmabuf_egl_SOURCES = clients/simple-dmabuf-egl.c
nodist_weston_simple_dmabuf_egl_SOURCES = \
protocol/xdg-shell-unstable-v6-protocol.c \
protocol/xdg-shell-unstable-v6-client-protocol.h \
protocol/fullscreen-shell-unstable-v1-protocol.c \
protocol/fullscreen-shell-unstable-v1-client-protocol.h \
protocol/linux-dmabuf-unstable-v1-protocol.c \
protocol/linux-dmabuf-unstable-v1-client-protocol.h
weston_simple_dmabuf_egl_CFLAGS = $(AM_CFLAGS) $(SIMPLE_DMABUF_EGL_CLIENT_CFLAGS)
weston_simple_dmabuf_egl_LDADD = $(SIMPLE_DMABUF_EGL_CLIENT_LIBS) libshared.la
endif
noinst_LTLIBRARIES += libtoytoolkit.la
libtoytoolkit_la_SOURCES = \
......
......@@ -48,6 +48,25 @@ simple_clients = [
],
'dep_objs': [ dep_wayland_client, dep_libshared ]
},
{
'name': 'dmabuf-egl',
'sources': [
'simple-dmabuf-egl.c',
linux_dmabuf_unstable_v1_client_protocol_h,
linux_dmabuf_unstable_v1_protocol_c,
xdg_shell_unstable_v6_client_protocol_h,
xdg_shell_unstable_v6_protocol_c,
fullscreen_shell_unstable_v1_client_protocol_h,
fullscreen_shell_unstable_v1_protocol_c,
],
'dep_objs': [
dep_wayland_client,
dep_libdrm_headers,
dep_gbm,
dep_egl,
dep_glesv2
]
},
{
'name': 'dmabuf-v4l',
'sources': [
......
/*
* Copyright © 2011 Benjamin Franzke
* Copyright © 2010 Intel Corporation
* Copyright © 2014,2018 Collabora Ltd.
*
* 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.
*/
#include "config.h"
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <drm_fourcc.h>
#include <gbm.h>
#include <wayland-client.h>
#include "shared/platform.h"
#include "shared/zalloc.h"
#include "xdg-shell-unstable-v6-client-protocol.h"
#include "fullscreen-shell-unstable-v1-client-protocol.h"
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include "shared/weston-egl-ext.h"
#ifndef DRM_FORMAT_MOD_INVALID
#define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
#endif
/* Possible options that affect the displayed image */
#define OPT_IMMEDIATE 1 /* create wl_buffer immediately */
struct display {
struct wl_display *display;
struct wl_registry *registry;
struct wl_compositor *compositor;
struct zxdg_shell_v6 *shell;
struct zwp_fullscreen_shell_v1 *fshell;
struct zwp_linux_dmabuf_v1 *dmabuf;
int xrgb8888_format_found;
int req_dmabuf_immediate;
struct {
EGLDisplay display;
EGLContext context;
PFNEGLCREATEIMAGEKHRPROC create_image;
PFNEGLDESTROYIMAGEKHRPROC destroy_image;
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d;
} egl;
struct {
int drm_fd;
struct gbm_device *device;
} gbm;
};
struct buffer {
struct display *display;
struct wl_buffer *buffer;
int busy;
struct gbm_bo *bo;
int dmabuf_fd;
int width;
int height;
uint32_t stride;
int format;
EGLImageKHR egl_image;
GLuint gl_texture;
GLuint gl_fbo;
};
#define NUM_BUFFERS 3
struct window {
struct display *display;
int width, height;
struct wl_surface *surface;
struct zxdg_surface_v6 *xdg_surface;
struct zxdg_toplevel_v6 *xdg_toplevel;
struct buffer buffers[NUM_BUFFERS];
struct wl_callback *callback;
bool initialized;
bool wait_for_configure;
};
static sig_atomic_t running = 1;
static void
redraw(void *data, struct wl_callback *callback, uint32_t time);
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 void
buffer_free(struct buffer *buf)
{
if (buf->gl_fbo)
glDeleteFramebuffers(1, &buf->gl_fbo);
if (buf->gl_texture)
glDeleteTextures(1, &buf->gl_texture);
if (buf->egl_image) {
buf->display->egl.destroy_image(buf->display->egl.display,
buf->egl_image);
}
if (buf->buffer)
wl_buffer_destroy(buf->buffer);
if (buf->bo)
gbm_bo_destroy(buf->bo);
if (buf->dmabuf_fd >= 0)
close(buf->dmabuf_fd);
}
static void
create_succeeded(void *data,
struct zwp_linux_buffer_params_v1 *params,
struct wl_buffer *new_buffer)
{
struct buffer *buffer = data;
buffer->buffer = new_buffer;
wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
zwp_linux_buffer_params_v1_destroy(params);
}
static void
create_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
{
struct buffer *buffer = data;
buffer->buffer = NULL;
running = 0;
zwp_linux_buffer_params_v1_destroy(params);
fprintf(stderr, "Error: zwp_linux_buffer_params.create failed.\n");
}
static const struct zwp_linux_buffer_params_v1_listener params_listener = {
create_succeeded,
create_failed
};
static bool
create_fbo_for_buffer(struct display *display, struct buffer *buffer)
{
EGLint attribs[] = {
EGL_WIDTH, buffer->width,
EGL_HEIGHT, buffer->height,
EGL_LINUX_DRM_FOURCC_EXT, buffer->format,
EGL_DMA_BUF_PLANE0_FD_EXT, buffer->dmabuf_fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
EGL_DMA_BUF_PLANE0_PITCH_EXT, (int) buffer->stride,
EGL_NONE
};
buffer->egl_image = display->egl.create_image(display->egl.display,
EGL_NO_CONTEXT,
EGL_LINUX_DMA_BUF_EXT,
NULL, attribs);
if (buffer->egl_image == EGL_NO_IMAGE) {
fprintf(stderr, "EGLImageKHR creation failed\n");
return false;
}
eglMakeCurrent(display->egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE,
display->egl.context);
glGenTextures(1, &buffer->gl_texture);
glBindTexture(GL_TEXTURE_2D, buffer->gl_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
display->egl.image_target_texture_2d(GL_TEXTURE_2D, buffer->egl_image);
glGenFramebuffers(1, &buffer->gl_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, buffer->gl_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, buffer->gl_texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
fprintf(stderr, "FBO creation failed\n");
return false;
}
return true;
}
static int
create_dmabuf_buffer(struct display *display, struct buffer *buffer,
int width, int height, int format)
{
static const uint32_t flags = 0;
static const uint64_t modifier = DRM_FORMAT_MOD_INVALID;
struct zwp_linux_buffer_params_v1 *params;
buffer->display = display;
buffer->width = width;
buffer->height = height;
buffer->format = format;
buffer->bo = gbm_bo_create(display->gbm.device,
buffer->width, buffer->height,
format,
GBM_BO_USE_RENDERING);
if (!buffer->bo) {
fprintf(stderr, "create_bo failed\n");
goto error;
}
buffer->stride = gbm_bo_get_stride(buffer->bo);
buffer->dmabuf_fd = gbm_bo_get_fd(buffer->bo);
if (buffer->dmabuf_fd < 0) {
fprintf(stderr, "error: dmabuf_fd < 0\n");
goto error;
}
params = zwp_linux_dmabuf_v1_create_params(display->dmabuf);
zwp_linux_buffer_params_v1_add(params,
buffer->dmabuf_fd,
0, /* plane_idx */
0, /* offset */
buffer->stride,
modifier >> 32,
modifier & 0xffffffff);
zwp_linux_buffer_params_v1_add_listener(params, &params_listener, buffer);
if (display->req_dmabuf_immediate) {
buffer->buffer =
zwp_linux_buffer_params_v1_create_immed(params,
buffer->width,
buffer->height,
format,
flags);
wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
}
else {
zwp_linux_buffer_params_v1_create(params,
buffer->width,
buffer->height,
format,
flags);
}
if (!create_fbo_for_buffer(display, buffer))
goto error;
return 0;
error:
buffer_free(buffer);
return -1;
}
static void
xdg_surface_handle_configure(void *data, struct zxdg_surface_v6 *surface,
uint32_t serial)
{
struct window *window = data;
zxdg_surface_v6_ack_configure(surface, serial);
if (window->initialized && window->wait_for_configure)
redraw(window, NULL, 0);
window->wait_for_configure = false;
}
static const struct zxdg_surface_v6_listener xdg_surface_listener = {
xdg_surface_handle_configure,
};
static void
xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
int32_t width, int32_t height,
struct wl_array *states)
{
}
static void
xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
{
running = 0;
}
static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
xdg_toplevel_handle_configure,
xdg_toplevel_handle_close,
};
static struct window *
create_window(struct display *display, int width, int height)
{
struct window *window;
int i;
int ret;
window = zalloc(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);
if (display->shell) {
window->xdg_surface =
zxdg_shell_v6_get_xdg_surface(display->shell,
window->surface);
assert(window->xdg_surface);
zxdg_surface_v6_add_listener(window->xdg_surface,
&xdg_surface_listener, window);
window->xdg_toplevel =
zxdg_surface_v6_get_toplevel(window->xdg_surface);
assert(window->xdg_toplevel);
zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
&xdg_toplevel_listener, window);
zxdg_toplevel_v6_set_title(window->xdg_toplevel, "simple-dmabuf-egl");
window->wait_for_configure = true;
wl_surface_commit(window->surface);
} else if (display->fshell) {
zwp_fullscreen_shell_v1_present_surface(display->fshell,
window->surface,
ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
NULL);
} else {
assert(0);
}
for (i = 0; i < NUM_BUFFERS; ++i) {
ret = create_dmabuf_buffer(display, &window->buffers[i],
width, height, DRM_FORMAT_XRGB8888);
if (ret < 0)
return NULL;
}
return window;
}
static void
destroy_window(struct window *window)
{
int i;
if (window->callback)
wl_callback_destroy(window->callback);
for (i = 0; i < NUM_BUFFERS; i++) {
if (window->buffers[i].buffer)
buffer_free(&window->buffers[i]);
}
if (window->xdg_toplevel)
zxdg_toplevel_v6_destroy(window->xdg_toplevel);
if (window->xdg_surface)
zxdg_surface_v6_destroy(window->xdg_surface);
wl_surface_destroy(window->surface);
free(window);
}
static struct buffer *
window_next_buffer(struct window *window)
{
int i;
for (i = 0; i < NUM_BUFFERS; i++)
if (!window->buffers[i].busy)
return &window->buffers[i];
return NULL;
}
static const struct wl_callback_listener frame_listener;
static void
redraw(void *data, struct wl_callback *callback, uint32_t time)
{
/* With a 60Hz redraw rate this completes a cycle in 3 seconds */
static const int MAX_STEP = 180;
static int step = 0;
static int step_dir = 1;
struct window *window = data;
struct buffer *buffer;
buffer = window_next_buffer(window);
if (!buffer) {
fprintf(stderr,
!callback ? "Failed to create the first buffer.\n" :
"All buffers busy at redraw(). Server bug?\n");
abort();
}
/* Direct all GL draws to the buffer through the FBO */
glBindFramebuffer(GL_FRAMEBUFFER, buffer->gl_fbo);
/* Cycle between 0 and MAX_STEP */
step += step_dir;
if (step == 0 || step == MAX_STEP)
step_dir = -step_dir;
glClearColor(0.0,
(float) step / MAX_STEP,
1.0 - (float) step / MAX_STEP,
1.0);
glClear(GL_COLOR_BUFFER_BIT);
glFinish();
wl_surface_attach(window->surface, buffer->buffer, 0, 0);
wl_surface_damage(window->surface, 0, 0, window->width, window->height);
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_modifiers(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
uint32_t format, uint32_t modifier_hi, uint32_t modifier_lo)
{
struct display *d = data;
switch (format) {
case DRM_FORMAT_XRGB8888:
d->xrgb8888_format_found = 1;
break;
default:
break;
}
}
static void
dmabuf_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf, uint32_t format)
{
/* XXX: deprecated */
}
static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
dmabuf_format,
dmabuf_modifiers
};
static void
xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
{
zxdg_shell_v6_pong(shell, serial);
}
static const struct zxdg_shell_v6_listener xdg_shell_listener = {
xdg_shell_ping,
};
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, "zxdg_shell_v6") == 0) {
d->shell = wl_registry_bind(registry,
id, &zxdg_shell_v6_interface, 1);
zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
} else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
d->fshell = wl_registry_bind(registry,
id, &zwp_fullscreen_shell_v1_interface, 1);
} else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
if (version < 3)
return;
d->dmabuf = wl_registry_bind(registry,
id, &zwp_linux_dmabuf_v1_interface, 3);
zwp_linux_dmabuf_v1_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 void
destroy_display(struct display *display)
{
if (display->gbm.device)
gbm_device_destroy(display->gbm.device);
if (display->gbm.drm_fd >= 0)
close(display->gbm.drm_fd);
if (display->egl.context != EGL_NO_CONTEXT)
eglDestroyContext(display->egl.display, display->egl.context);
if (display->egl.display != EGL_NO_DISPLAY)
eglTerminate(display->egl.display);
if (display->dmabuf)
zwp_linux_dmabuf_v1_destroy(display->dmabuf);
if (display->shell)
zxdg_shell_v6_destroy(display->shell);