Implement drm (egl) buffer attaching

This commit is contained in:
nyorain 2017-08-09 21:25:34 +02:00
parent 750d0ad458
commit 67369173aa
19 changed files with 308 additions and 23 deletions

View File

@ -149,6 +149,10 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
goto error_event;
}
if (!wlr_egl_bind_display(&drm->renderer.egl, display)) {
wlr_log(L_INFO, "Failed to bind egl/wl display: %s", egl_error());
}
return backend;
error_event:

View File

@ -579,7 +579,7 @@ static bool wlr_drm_output_set_cursor(struct wlr_output_state *output,
wlr_matrix_texture(plane->matrix, plane->width, plane->height,
output->base->transform ^ WL_OUTPUT_TRANSFORM_FLIPPED_180);
plane->wlr_rend = wlr_gles2_renderer_init();
plane->wlr_rend = wlr_gles2_renderer_init(&output->renderer->egl);
if (!plane->wlr_rend) {
return false;
}

View File

@ -1,10 +1,17 @@
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES3/gl3.h>
#include <GLES2/gl2.h>
#include <gbm.h> // GBM_FORMAT_XRGB8888
#include <stdlib.h>
#include <wlr/util/log.h>
#include "backend/egl.h"
// Extension documentation
// https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_image_base.txt.
// https://cgit.freedesktop.org/mesa/mesa/tree/docs/specs/WL_bind_wayland_display.spec
struct wlr_egl *egl_global;
const char *egl_error(void) {
switch (eglGetError()) {
case EGL_SUCCESS:
@ -46,6 +53,12 @@ const char *egl_error(void) {
PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display;
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface;
PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
PFNEGLQUERYWAYLANDBUFFERWL eglQueryWaylandBufferWL;
PFNEGLBINDWAYLANDDISPLAYWL eglBindWaylandDisplayWL;
PFNEGLUNBINDWAYLANDDISPLAYWL eglUnbindWaylandDisplayWL;
static bool egl_exts() {
get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC)
eglGetProcAddress("eglGetPlatformDisplayEXT");
@ -109,7 +122,8 @@ static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLenum platform) {
return false;
}
bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display) {
bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform,
void *remote_display) {
if (!egl_exts()) {
return false;
}
@ -119,7 +133,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display) {
goto error;
}
egl->display = get_platform_display(platform, display, NULL);
egl->display = get_platform_display(platform, remote_display, NULL);
if (egl->display == EGL_NO_DISPLAY) {
wlr_log(L_ERROR, "Failed to create EGL display: %s", egl_error());
goto error;
@ -147,11 +161,31 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display) {
}
eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, egl->context);
egl->egl_exts = eglQueryString(egl->display, EGL_EXTENSIONS);
if (strstr(egl->egl_exts, "EGL_WL_bind_wayland_display") == NULL ||
strstr(egl->egl_exts, "EGL_KHR_image_base") == NULL) {
wlr_log(L_ERROR, "Required egl extensions not supported");
goto error;
}
eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)
eglGetProcAddress("eglCreateImageKHR");
eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)
eglGetProcAddress("eglDestroyImageKHR");
eglQueryWaylandBufferWL = (PFNEGLQUERYWAYLANDBUFFERWL)
(void*) eglGetProcAddress("eglQueryWaylandBufferWL");
eglBindWaylandDisplayWL = (PFNEGLBINDWAYLANDDISPLAYWL)
(void*) eglGetProcAddress("eglBindWaylandDisplayWL");
eglUnbindWaylandDisplayWL = (PFNEGLUNBINDWAYLANDDISPLAYWL)
(void*) eglGetProcAddress("eglUnbindWaylandDisplayWL");
egl_global = egl;
egl->gl_exts = (const char*) glGetString(GL_EXTENSIONS);
wlr_log(L_INFO, "Using EGL %d.%d", (int)major, (int)minor);
wlr_log(L_INFO, "Supported EGL extensions: %s", eglQueryString(egl->display,
EGL_EXTENSIONS));
wlr_log(L_INFO, "Supported EGL extensions: %s", egl->egl_exts);
wlr_log(L_INFO, "Using %s", glGetString(GL_VERSION));
wlr_log(L_INFO, "Supported OpenGL ES extensions: %s", glGetString(GL_EXTENSIONS));
wlr_log(L_INFO, "Supported OpenGL ES extensions: %s", egl->gl_exts);
return true;
error:
@ -162,11 +196,59 @@ error:
}
void wlr_egl_free(struct wlr_egl *egl) {
eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (egl->wl_display && eglUnbindWaylandDisplayWL) {
eglUnbindWaylandDisplayWL(egl->display, egl->wl_display);
}
eglDestroyContext(egl->display, egl->context);
eglTerminate(egl->display);
eglReleaseThread();
eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (egl_global == egl)
egl_global = NULL;
}
bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display) {
if (!eglBindWaylandDisplayWL) {
return false;
}
if (eglBindWaylandDisplayWL(egl->display, local_display)) {
egl->wl_display = local_display;
return true;
}
return false;
}
bool wlr_egl_query_buffer(struct wl_resource *buf, int attrib, int *value) {
if (!egl_global || !eglQueryWaylandBufferWL) {
return false;
}
return eglQueryWaylandBufferWL(egl_global->display, buf, attrib, value);
}
EGLImage wlr_egl_create_image(EGLenum target,
EGLClientBuffer buffer, const EGLint *attribs)
{
if (!egl_global || !eglCreateImageKHR) {
return false;
}
return eglCreateImageKHR(egl_global->display, egl_global->context, target,
buffer, attribs);
}
bool wlr_egl_destroy_image(EGLImage image)
{
if (!egl_global || !eglDestroyImageKHR) {
return false;
}
eglDestroyImageKHR(egl_global->display, image);
return true;
}
EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window) {

View File

@ -52,6 +52,8 @@ static bool wlr_wl_backend_init(struct wlr_backend_state* state) {
}
wlr_egl_init(&state->egl, EGL_PLATFORM_WAYLAND_EXT, state->remote_display);
wlr_egl_bind_display(&state->egl, state->local_display);
for (size_t i = 0; i < state->requested_outputs; ++i) {
wlr_wl_output_create(state->backend);
}

View File

@ -67,7 +67,7 @@ int main() {
};
compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init();
state.renderer = wlr_gles2_renderer_init(compositor.backend->egl);
wl_display_init_shm(compositor.display);
wl_compositor_init(compositor.display, &state.compositor, state.renderer);
wl_shell_init(compositor.display, &state.shell);

View File

@ -204,7 +204,7 @@ int main(int argc, char *argv[]) {
compositor.keyboard_key_cb = handle_keyboard_key;
compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init();
state.renderer = wlr_gles2_renderer_init(compositor.backend->egl);
state.cat_texture = wlr_render_texture_init(state.renderer);
wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ABGR8888,
cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data);

View File

@ -152,7 +152,7 @@ int main(int argc, char *argv[]) {
};
compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init();
state.renderer = wlr_gles2_renderer_init(compositor.backend->egl);
compositor_run(&compositor);

View File

@ -104,7 +104,7 @@ int main(int argc, char *argv[]) {
};
compositor_init(&compositor);
state.renderer = wlr_gles2_renderer_init();
state.renderer = wlr_gles2_renderer_init(compositor.backend->egl);
state.cat_texture = wlr_render_texture_init(state.renderer);
wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ARGB8888,
cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data);

View File

@ -2,17 +2,72 @@
#define WLR_BACKEND_EGL_H
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <stdbool.h>
struct wlr_egl {
EGLDisplay display;
EGLConfig config;
EGLContext context;
const char *egl_exts;
const char *gl_exts;
struct wl_display *wl_display;
};
const char *egl_error(void);
/**
* Initializes an egl context for the given platform and remote display.
* Will attempt to load all possibly required api functions.
*/
bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display);
/**
* Frees all related egl resources, makes the context not-current and
* unbinds a bound wayland display.
*/
void wlr_egl_free(struct wlr_egl *egl);
/**
* Binds the given display to the egl instance.
* This will allow clients to create egl surfaces from wayland ones and render to it.
*/
bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display);
/**
* Queries information about the given (potential egl/drm) buffer, returns
* the information in value.
* Refer to eglQueryWaylandBufferWL for more information about attrib and value.
* Makes only sense when a wl_display was bound to it since otherwise there
* cannot be any egl/drm buffers.
* Will only work after a wlr_egl objct was initialized and if the needed egl extension
* is present.
*/
bool wlr_egl_query_buffer(struct wl_resource *buf,
EGLint attrib, EGLint *value);
/**
* Returns a surface for the given native window
* The window must match the remote display the wlr_egl was created with.
*/
EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window);
/**
* Creates an egl image from the given client buffer and attributes.
* Will only work after a wlr_egl objct was initialized and if the needed egl extension
* is present.
*/
EGLImageKHR wlr_egl_create_image(EGLenum target, EGLClientBuffer buffer,
const EGLint *attribs);
/**
* Destroys an egl image created with the given wlr_egl.
*/
bool wlr_egl_destroy_image(EGLImageKHR image);
/**
* Returns a string for the last error ocurred with egl.
*/
const char *egl_error(void);
#endif

View File

@ -4,9 +4,14 @@
#include <string.h>
#include <stdbool.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <wlr/render.h>
#include <wlr/util/log.h>
extern PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
struct pixel_format {
uint32_t wl_format;
GLint gl_format, gl_type;
@ -18,6 +23,7 @@ struct wlr_texture_state {
struct wlr_texture *wlr_texture;
GLuint tex_id;
const struct pixel_format *pixel_format;
EGLImageKHR image;
};
struct shaders {
@ -25,6 +31,7 @@ struct shaders {
GLuint rgba, rgbx;
GLuint quad;
GLuint ellipse;
Gluint external;
};
extern struct shaders shaders;
@ -39,6 +46,7 @@ extern const GLchar ellipse_fragment_src[];
extern const GLchar vertex_src[];
extern const GLchar fragment_src_rgba[];
extern const GLchar fragment_src_rgbx[];
extern const GLchar fragment_src_external[];
bool _gles2_flush_errors(const char *file, int line);
#define gles2_flush_errors(...) \

View File

@ -65,7 +65,7 @@ struct wlr_texture {
* Copies pixels to this texture. The buffer is not accessed after this function
* returns.
*/
bool wlr_texture_upload_pixels(struct wlr_texture *surf,
bool wlr_texture_upload_pixels(struct wlr_texture *tex,
enum wl_shm_format format, int stride, int width, int height,
const unsigned char *pixels);
/**
@ -80,8 +80,17 @@ bool wlr_texture_update_pixels(struct wlr_texture *surf,
* Copies pixels from a wl_shm_buffer into this texture. The buffer is not
* accessed after this function returns.
*/
bool wlr_texture_upload_shm(struct wlr_texture *surf, uint32_t format,
bool wlr_texture_upload_shm(struct wlr_texture *tex, uint32_t format,
struct wl_shm_buffer *shm);
/**
* Attaches the contents from the given wl_drm wl_buffer resource onto the
* texture. The wl_resource is not used after this call.
* Will fail (return false) if the given resource is no drm buffer.
*/
bool wlr_texture_upload_drm(struct wlr_texture *tex,
struct wl_resource *drm_buffer);
/**
* Copies a rectangle of pixels from a wl_shm_buffer onto the texture. The
* buffer is not accessed after this function returns. Under some circumstances,

View File

@ -2,6 +2,7 @@
#define _WLR_GLES2_RENDERER_H
#include <wlr/render.h>
struct wlr_egl;
struct wlr_renderer *wlr_gles2_renderer_init();
#endif

View File

@ -42,7 +42,8 @@ struct wlr_texture_impl {
struct wl_shm_buffer *shm);
bool (*update_shm)(struct wlr_texture_state *surf, uint32_t format,
int x, int y, int width, int height, struct wl_shm_buffer *shm);
// TODO: egl
bool (*upload_drm)(struct wlr_texture_state *state,
struct wl_resource *drm_buf);
void (*get_matrix)(struct wlr_texture_state *state,
float (*matrix)[16], const float (*projection)[16], int x, int y);
void (*bind)(struct wlr_texture_state *state);

View File

@ -2,14 +2,17 @@
#include <stdlib.h>
#include <assert.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <wayland-util.h>
#include <wayland-server-protocol.h>
#include <wlr/render.h>
#include <wlr/render/interface.h>
#include <wlr/render/matrix.h>
#include <wlr/util/log.h>
#include "backend/egl.h"
#include "render/gles2.h"
PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = NULL;
struct shaders shaders;
static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) {
@ -65,13 +68,36 @@ static void init_default_shaders() {
if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) {
goto error;
}
if (glEGLImageTargetTexture2DOES) {
if (!compile_program(quad_vertex_src, fragment_src_external, &shaders.external)) {
goto error;
}
}
wlr_log(L_DEBUG, "Compiled default shaders");
return;
error:
wlr_log(L_ERROR, "Failed to set up default shaders!");
}
static void init_image_ext() {
if (glEGLImageTargetTexture2DOES)
return;
const char *exts = (const char*) glGetString(GL_EXTENSIONS);
if (strstr(exts, "GL_OES_EGL_image_external")) {
glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)
eglGetProcAddress("glEGLImageTargetTexture2DOES");
}
if (!glEGLImageTargetTexture2DOES) {
wlr_log(L_INFO, "Failed to load glEGLImageTargetTexture2DOES "
"Will not be able to attach drm buffers");
}
}
static void init_globals() {
init_image_ext();
init_default_shaders();
}
@ -179,7 +205,7 @@ static struct wlr_renderer_impl wlr_renderer_impl = {
.destroy = wlr_gles2_destroy
};
struct wlr_renderer *wlr_gles2_renderer_init() {
struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_egl *egl) {
init_globals();
return wlr_renderer_init(NULL, &wlr_renderer_impl);
}

View File

@ -90,3 +90,13 @@ const GLchar fragment_src_rgbx[] =
" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;"
" gl_FragColor.a = alpha;"
"}";
const GLchar fragment_src_external[] =
"#extension GL_OES_EGL_image_external : require\n"
"precision mediump float;"
"uniform samplerExternalOES texture0;"
"varying vec2 v_uv;"
"void main() {"
" vec4 col = texture2D(texture0, v_uv);"
" gl_FragColor = vec4(col.rgb, col.a);"
"}";

View File

@ -10,6 +10,26 @@
#include <wlr/render/matrix.h>
#include <wlr/util/log.h>
#include "render/gles2.h"
#include "backend/egl.h"
static struct pixel_format external_pixel_format = {
.wl_format = 0,
.depth = 0,
.bpp = 0,
.gl_format = 0,
.gl_type = 0,
.shader = &shaders.external
};
static void gles2_texture_gen_texture(struct wlr_texture_state *surface) {
if (surface->tex_id)
return;
GL_CALL(glGenTextures(1, &surface->tex_id));
GL_CALL(glBindTexture(GL_TEXTURE_2D, surface->tex_id));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
}
static bool gles2_texture_upload_pixels(struct wlr_texture_state *texture,
enum wl_shm_format format, int stride, int width, int height,
@ -24,7 +44,8 @@ static bool gles2_texture_upload_pixels(struct wlr_texture_state *texture,
texture->wlr_texture->height = height;
texture->wlr_texture->format = format;
texture->pixel_format = fmt;
GL_CALL(glGenTextures(1, &texture->tex_id));
gles2_texture_gen_texture(texture);
GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id));
GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride));
GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, fmt->gl_format, width, height, 0,
@ -72,7 +93,7 @@ static bool gles2_texture_upload_shm(struct wlr_texture_state *texture,
texture->wlr_texture->format = format;
texture->pixel_format = fmt;
GL_CALL(glGenTextures(1, &texture->tex_id));
gles2_texture_gen_texture(texture);
GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id));
GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, pitch));
GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0));
@ -109,6 +130,62 @@ static bool gles2_texture_update_shm(struct wlr_texture_state *texture,
GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0));
wl_shm_buffer_end_access(buffer);
return true;
}
static bool gles2_texture_upload_drm(struct wlr_texture_state *texture,
struct wl_resource* buf) {
if (!glEGLImageTargetTexture2DOES) {
return false;
}
EGLint format;
if (!wlr_egl_query_buffer(buf, EGL_TEXTURE_FORMAT, &format)) {
wlr_log(L_INFO, "upload_drm called with no drm buffer");
return false;
}
wlr_egl_query_buffer(buf, EGL_WIDTH, (EGLint*) &texture->wlr_texture->width);
wlr_egl_query_buffer(buf, EGL_HEIGHT, (EGLint*) &texture->wlr_texture->height);
EGLint inverted_y;
wlr_egl_query_buffer(buf, EGL_WAYLAND_Y_INVERTED_WL, &inverted_y);
GLenum target;
const struct pixel_format *pf;
switch (format) {
case EGL_TEXTURE_RGB:
case EGL_TEXTURE_RGBA:
target = GL_TEXTURE_2D;
pf = gl_format_for_wl_format(WL_SHM_FORMAT_ARGB8888);
break;
case EGL_TEXTURE_EXTERNAL_WL:
target = GL_TEXTURE_EXTERNAL_OES;
pf = &external_pixel_format;
break;
default:
wlr_log(L_ERROR, "invalid/unsupported egl buffer format");
return false;
}
gles2_texture_gen_texture(texture);
GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id));
EGLint attribs[] = { EGL_WAYLAND_PLANE_WL, 0, EGL_NONE };
texture->image = wlr_egl_create_image(EGL_WAYLAND_BUFFER_WL,
(EGLClientBuffer*) buf, attribs);
if (!texture->image) {
wlr_log(L_ERROR, "failed to create egl image: %s", egl_error());
return false;
}
GL_CALL(glActiveTexture(GL_TEXTURE0));
GL_CALL(glBindTexture(target, texture->tex_id));
GL_CALL(glEGLImageTargetTexture2DOES(target, texture->image));
texture->wlr_texture->valid = true;
texture->pixel_format = pf;
return true;
}
@ -142,6 +219,7 @@ static struct wlr_texture_impl wlr_texture_impl = {
.update_pixels = gles2_texture_update_pixels,
.upload_shm = gles2_texture_upload_shm,
.update_shm = gles2_texture_update_shm,
.upload_drm = gles2_texture_upload_drm,
.get_matrix = gles2_texture_get_matrix,
.bind = gles2_texture_bind,
.destroy = gles2_texture_destroy,

View File

@ -43,6 +43,11 @@ bool wlr_texture_update_shm(struct wlr_texture *texture, uint32_t format,
x, y, width, height, shm);
}
bool wlr_texture_upload_drm(struct wlr_texture *texture,
struct wl_resource *drm_buffer) {
return texture->impl->upload_drm(texture->state, drm_buffer);
}
void wlr_texture_get_matrix(struct wlr_texture *texture,
float (*matrix)[16], const float (*projection)[16], int x, int y) {
texture->impl->get_matrix(texture->state, matrix, projection, x, y);

View File

@ -133,6 +133,7 @@ bool wlr_output_set_cursor(struct wlr_output *output,
return true;
}
/*
wlr_log(L_INFO, "Falling back to software cursor");
output->cursor.is_sw = true;
@ -149,8 +150,9 @@ bool wlr_output_set_cursor(struct wlr_output *output,
wlr_texture_upload_pixels(output->cursor.texture, WL_SHM_FORMAT_ARGB8888,
stride, width, height, buf);
*/
return true;
return false;
}
bool wlr_output_move_cursor(struct wlr_output *output, int x, int y) {

View File

@ -99,12 +99,14 @@ static void surface_commit(struct wl_client *client,
// callbacks instead of immediately here
if (surface->current.buffer) {
struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current.buffer);
if (!buffer) {
wlr_log(L_INFO, "Unknown buffer handle attached");
} else {
if (buffer) {
uint32_t format = wl_shm_buffer_get_format(buffer);
wlr_texture_upload_shm(surface->texture, format, buffer);
wl_resource_queue_event(surface->current.buffer, WL_BUFFER_RELEASE);
} else if (wlr_texture_upload_drm(surface->texture, surface->pending.buffer)) {
wl_resource_queue_event(surface->current.buffer, WL_BUFFER_RELEASE);
} else {
wlr_log(L_INFO, "Unknown buffer handle attached");
}
}
}