From 67369173aaafd763a6369a84917ae457c7095a72 Mon Sep 17 00:00:00 2001 From: nyorain Date: Wed, 9 Aug 2017 21:25:34 +0200 Subject: Implement drm (egl) buffer attaching --- examples/tablet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/tablet.c') diff --git a/examples/tablet.c b/examples/tablet.c index 0be6dcb9..178ccc09 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -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); -- cgit v1.2.3 From 1901c3e1b64cff3440b0b3b27ad9db06645a29e6 Mon Sep 17 00:00:00 2001 From: nyorain Date: Thu, 10 Aug 2017 11:03:15 +0200 Subject: Fix example for updated gles --- examples/compositor/main.c | 2 +- examples/rotation.c | 2 +- examples/tablet.c | 3 +-- examples/touch.c | 2 +- include/render/gles2.h | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) (limited to 'examples/tablet.c') diff --git a/examples/compositor/main.c b/examples/compositor/main.c index 1fbb38ad..7ac549e5 100644 --- a/examples/compositor/main.c +++ b/examples/compositor/main.c @@ -67,7 +67,7 @@ int main() { }; compositor_init(&compositor); - state.renderer = wlr_gles2_renderer_init(compositor.backend->egl); + state.renderer = wlr_gles2_renderer_init(); wl_display_init_shm(compositor.display); wl_compositor_init(compositor.display, &state.compositor, state.renderer); wl_shell_init(compositor.display, &state.shell); diff --git a/examples/rotation.c b/examples/rotation.c index 9ece1b92..34994e04 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -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(compositor.backend->egl); + state.renderer = wlr_gles2_renderer_init(); 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); diff --git a/examples/tablet.c b/examples/tablet.c index 178ccc09..4a7d9f74 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -152,8 +152,7 @@ int main(int argc, char *argv[]) { }; compositor_init(&compositor); - state.renderer = wlr_gles2_renderer_init(compositor.backend->egl); - + state.renderer = wlr_gles2_renderer_init(); compositor_run(&compositor); wlr_renderer_destroy(state.renderer); diff --git a/examples/touch.c b/examples/touch.c index b3f32855..76b92ad2 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) { }; compositor_init(&compositor); - state.renderer = wlr_gles2_renderer_init(compositor.backend->egl); + state.renderer = wlr_gles2_renderer_init(); 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); diff --git a/include/render/gles2.h b/include/render/gles2.h index 7ad4ad2e..cb78a181 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -31,7 +31,7 @@ struct shaders { GLuint rgba, rgbx; GLuint quad; GLuint ellipse; - Gluint external; + GLuint external; }; extern struct shaders shaders; -- cgit v1.2.3 From c24351681f672a2ce561b9cb6d73172dfc36c45c Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 10 Aug 2017 22:15:37 -0400 Subject: Refactor EGL handling --- backend/backend.c | 7 ++ backend/drm/backend.c | 8 +- backend/drm/drm.c | 2 +- backend/egl.c | 260 ---------------------------------------- backend/meson.build | 1 - backend/multi/backend.c | 14 ++- backend/wayland/backend.c | 8 +- examples/compositor/main.c | 2 +- examples/rotation.c | 2 +- examples/tablet.c | 2 +- examples/touch.c | 2 +- include/backend/drm.h | 2 +- include/backend/egl.h | 73 ----------- include/backend/wayland.h | 2 +- include/render/gles2.h | 8 ++ include/wlr/backend.h | 2 + include/wlr/backend/interface.h | 2 + include/wlr/egl.h | 75 ++++++++++++ include/wlr/render.h | 5 + include/wlr/render/gles2.h | 3 +- include/wlr/render/interface.h | 2 + include/wlr/types/wlr_surface.h | 1 + render/egl.c | 243 +++++++++++++++++++++++++++++++++++++ render/gles2/renderer.c | 23 +++- render/gles2/texture.c | 35 +++--- render/meson.build | 5 +- render/wlr_renderer.c | 5 + types/wlr_surface.c | 6 +- 28 files changed, 429 insertions(+), 371 deletions(-) delete mode 100644 backend/egl.c delete mode 100644 include/backend/egl.h create mode 100644 include/wlr/egl.h create mode 100644 render/egl.c (limited to 'examples/tablet.c') diff --git a/backend/backend.c b/backend/backend.c index 4da3fe7a..dc34c80b 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -39,6 +39,13 @@ void wlr_backend_destroy(struct wlr_backend *backend) { free(backend); } +struct wlr_egl *wlr_backend_get_egl(struct wlr_backend *backend) { + if (!backend->impl->get_egl) { + return NULL; + } + return backend->impl->get_egl(backend->state); +} + static struct wlr_backend *attempt_wl_backend(struct wl_display *display) { struct wlr_backend *backend = wlr_wl_backend_create(display); if (backend) { diff --git a/backend/drm/backend.c b/backend/drm/backend.c index bcc81624..a7e6824c 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "backend/udev.h" #include "backend/drm.h" @@ -38,9 +39,14 @@ static void wlr_drm_backend_destroy(struct wlr_backend_state *drm) { free(drm); } +static struct wlr_egl *wlr_drm_backend_get_egl(struct wlr_backend_state *drm) { + return &drm->renderer.egl; +} + static struct wlr_backend_impl backend_impl = { .init = wlr_drm_backend_init, - .destroy = wlr_drm_backend_destroy + .destroy = wlr_drm_backend_destroy, + .get_egl = wlr_drm_backend_get_egl }; static void session_signal(struct wl_listener *listener, void *data) { diff --git a/backend/drm/drm.c b/backend/drm/drm.c index e11751e2..927388b0 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -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(&output->renderer->egl); + plane->wlr_rend = wlr_gles2_renderer_init(drm->base); if (!plane->wlr_rend) { return false; } diff --git a/backend/egl.c b/backend/egl.c deleted file mode 100644 index 15a2c58e..00000000 --- a/backend/egl.c +++ /dev/null @@ -1,260 +0,0 @@ -#include -#include -#include -#include // GBM_FORMAT_XRGB8888 -#include -#include -#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: - return "Success"; - case EGL_NOT_INITIALIZED: - return "Not initialized"; - case EGL_BAD_ACCESS: - return "Bad access"; - case EGL_BAD_ALLOC: - return "Bad alloc"; - case EGL_BAD_ATTRIBUTE: - return "Bad attribute"; - case EGL_BAD_CONTEXT: - return "Bad Context"; - case EGL_BAD_CONFIG: - return "Bad Config"; - case EGL_BAD_CURRENT_SURFACE: - return "Bad current surface"; - case EGL_BAD_DISPLAY: - return "Bad display"; - case EGL_BAD_SURFACE: - return "Bad surface"; - case EGL_BAD_MATCH: - return "Bad match"; - case EGL_BAD_PARAMETER: - return "Bad parameter"; - case EGL_BAD_NATIVE_PIXMAP: - return "Bad native pixmap"; - case EGL_BAD_NATIVE_WINDOW: - return "Bad native window"; - case EGL_CONTEXT_LOST: - return "Context lost"; - default: - return "Unknown"; - } -} - -// EGL extensions -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"); - - if (!get_platform_display) { - wlr_log(L_ERROR, "Failed to load EGL extension 'eglGetPlatformDisplayEXT'"); - return false; - } - - create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) - eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"); - - if (!get_platform_display) { - wlr_log(L_ERROR, - "Failed to load EGL extension 'eglCreatePlatformWindowSurfaceEXT'"); - return false; - } - - return true; -} - -static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLenum platform) { - EGLint count = 0, matched = 0, ret; - - ret = eglGetConfigs(disp, NULL, 0, &count); - if (ret == EGL_FALSE || count == 0) { - wlr_log(L_ERROR, "eglGetConfigs returned no configs"); - return false; - } - - EGLConfig configs[count]; - - ret = eglChooseConfig(disp, NULL, configs, count, &matched); - if (ret == EGL_FALSE) { - wlr_log(L_ERROR, "eglChooseConfig failed"); - return false; - } - - for (int i = 0; i < matched; ++i) { - EGLint gbm_format; - - if (platform == EGL_PLATFORM_WAYLAND_EXT) { - *out = configs[i]; - return true; - } - - if (!eglGetConfigAttrib(disp, - configs[i], - EGL_NATIVE_VISUAL_ID, - &gbm_format)) { - continue; - } - - if (gbm_format == GBM_FORMAT_ARGB8888) { - *out = configs[i]; - return true; - } - } - - wlr_log(L_ERROR, "no valid egl config found"); - return false; -} - -bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, - void *remote_display) { - if (!egl_exts()) { - return false; - } - - if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) { - wlr_log(L_ERROR, "Failed to bind to the OpenGL ES API: %s", egl_error()); - goto error; - } - - 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; - } - - EGLint major, minor; - if (eglInitialize(egl->display, &major, &minor) == EGL_FALSE) { - wlr_log(L_ERROR, "Failed to initialize EGL: %s", egl_error()); - goto error; - } - - if (!egl_get_config(egl->display, &egl->config, platform)) { - wlr_log(L_ERROR, "Failed to get EGL config"); - goto error; - } - - static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; - - egl->context = eglCreateContext(egl->display, egl->config, - EGL_NO_CONTEXT, attribs); - - if (egl->context == EGL_NO_CONTEXT) { - wlr_log(L_ERROR, "Failed to create EGL context: %s", egl_error()); - goto error; - } - - 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", egl->egl_exts); - wlr_log(L_INFO, "Using %s", glGetString(GL_VERSION)); - wlr_log(L_INFO, "Supported OpenGL ES extensions: %s", egl->gl_exts); - return true; - -error: - eglTerminate(egl->display); - eglReleaseThread(); - eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); - return false; -} - -void wlr_egl_free(struct wlr_egl *egl) { - 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) { - EGLSurface surf = create_platform_window_surface(egl->display, egl->config, - window, NULL); - if (surf == EGL_NO_SURFACE) { - wlr_log(L_ERROR, "Failed to create EGL surface: %s", egl_error()); - return EGL_NO_SURFACE; - } - return surf; -} diff --git a/backend/meson.build b/backend/meson.build index bb1b84c8..9c6b70df 100644 --- a/backend/meson.build +++ b/backend/meson.build @@ -1,6 +1,5 @@ wlr_files += files( 'backend.c', - 'egl.c', 'udev.c', 'session/direct-ipc.c', 'session/direct.c', diff --git a/backend/multi/backend.c b/backend/multi/backend.c index 19839629..9563356d 100644 --- a/backend/multi/backend.c +++ b/backend/multi/backend.c @@ -38,9 +38,21 @@ static void multi_backend_destroy(struct wlr_backend_state *state) { free(state); } +static struct wlr_egl *multi_backend_get_egl(struct wlr_backend_state *state) { + for (size_t i = 0; i < state->backends->length; ++i) { + struct subbackend_state *sub = state->backends->items[i]; + struct wlr_egl *egl = wlr_backend_get_egl(sub->backend); + if (egl) { + return egl; + } + } + return NULL; +} + struct wlr_backend_impl backend_impl = { .init = multi_backend_init, - .destroy = multi_backend_destroy + .destroy = multi_backend_destroy, + .get_egl = multi_backend_get_egl }; struct wlr_backend *wlr_multi_backend_create(struct wlr_session *session, diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index a0fb0be6..1a156802 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -95,9 +96,14 @@ static void wlr_wl_backend_destroy(struct wlr_backend_state *state) { free(state); } +static struct wlr_egl *wlr_wl_backend_get_egl(struct wlr_backend_state *state) { + return &state->egl; +} + static struct wlr_backend_impl backend_impl = { .init = wlr_wl_backend_init, - .destroy = wlr_wl_backend_destroy + .destroy = wlr_wl_backend_destroy, + .get_egl = wlr_wl_backend_get_egl }; bool wlr_backend_is_wl(struct wlr_backend *b) { diff --git a/examples/compositor/main.c b/examples/compositor/main.c index fd592600..f115d267 100644 --- a/examples/compositor/main.c +++ b/examples/compositor/main.c @@ -68,7 +68,7 @@ int main() { }; compositor_init(&compositor); - state.renderer = wlr_gles2_renderer_init(); + state.renderer = wlr_gles2_renderer_init(compositor.backend); wl_display_init_shm(compositor.display); wl_compositor_init(compositor.display, &state.compositor, state.renderer); wl_shell_init(compositor.display, &state.shell); diff --git a/examples/rotation.c b/examples/rotation.c index 34994e04..e4c3f87f 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -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); 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); diff --git a/examples/tablet.c b/examples/tablet.c index 4a7d9f74..0651efde 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -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); compositor_run(&compositor); wlr_renderer_destroy(state.renderer); diff --git a/examples/touch.c b/examples/touch.c index 76b92ad2..9dd2fe57 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -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); 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); diff --git a/include/backend/drm.h b/include/backend/drm.h index 2f724524..35265e21 100644 --- a/include/backend/drm.h +++ b/include/backend/drm.h @@ -12,9 +12,9 @@ #include #include +#include #include -#include #include #include "drm-properties.h" diff --git a/include/backend/egl.h b/include/backend/egl.h deleted file mode 100644 index b649c55b..00000000 --- a/include/backend/egl.h +++ /dev/null @@ -1,73 +0,0 @@ -#ifndef WLR_BACKEND_EGL_H -#define WLR_BACKEND_EGL_H - -#include -#include -#include - -struct wlr_egl { - EGLDisplay display; - EGLConfig config; - EGLContext context; - - const char *egl_exts; - const char *gl_exts; - - struct wl_display *wl_display; -}; - -/** - * 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 diff --git a/include/backend/wayland.h b/include/backend/wayland.h index 792bad37..e6339a23 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -4,10 +4,10 @@ #include #include #include +#include #include #include #include -#include "backend/egl.h" struct wlr_backend_state { /* local state */ diff --git a/include/render/gles2.h b/include/render/gles2.h index cb78a181..3006872c 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include @@ -19,8 +21,14 @@ struct pixel_format { GLuint *shader; }; +struct wlr_renderer_state { + struct wlr_renderer *renderer; + struct wlr_egl *egl; +}; + struct wlr_texture_state { struct wlr_texture *wlr_texture; + struct wlr_egl *egl; GLuint tex_id; const struct pixel_format *pixel_format; EGLImageKHR image; diff --git a/include/wlr/backend.h b/include/wlr/backend.h index 3910e406..ce5b2568 100644 --- a/include/wlr/backend.h +++ b/include/wlr/backend.h @@ -3,6 +3,7 @@ #include #include +#include struct wlr_backend_impl; struct wlr_backend_state; @@ -22,5 +23,6 @@ struct wlr_backend { struct wlr_backend *wlr_backend_autocreate(struct wl_display *display); bool wlr_backend_init(struct wlr_backend *backend); void wlr_backend_destroy(struct wlr_backend *backend); +struct wlr_egl *wlr_backend_get_egl(struct wlr_backend *backend); #endif diff --git a/include/wlr/backend/interface.h b/include/wlr/backend/interface.h index 15cfb6c3..e791bdcf 100644 --- a/include/wlr/backend/interface.h +++ b/include/wlr/backend/interface.h @@ -3,12 +3,14 @@ #include #include +#include struct wlr_backend_state; struct wlr_backend_impl { bool (*init)(struct wlr_backend_state *state); void (*destroy)(struct wlr_backend_state *state); + struct wlr_egl *(*get_egl)(struct wlr_backend_state *state); }; struct wlr_backend *wlr_backend_create(const struct wlr_backend_impl *impl, diff --git a/include/wlr/egl.h b/include/wlr/egl.h new file mode 100644 index 00000000..b37317a5 --- /dev/null +++ b/include/wlr/egl.h @@ -0,0 +1,75 @@ +#ifndef WLR_EGL_H +#define WLR_EGL_H + +#include +#include +#include + +struct wlr_egl { + EGLDisplay display; + EGLConfig config; + EGLContext context; + + PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display; + PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface; + + PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR; + PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR; + PFNEGLQUERYWAYLANDBUFFERWL eglQueryWaylandBufferWL; + PFNEGLBINDWAYLANDDISPLAYWL eglBindWaylandDisplayWL; + PFNEGLUNBINDWAYLANDDISPLAYWL eglUnbindWaylandDisplayWL; + + const char *egl_exts; + const char *gl_exts; + + struct wl_display *wl_display; +}; + +// TODO: Allocate and return a wlr_egl +/** + * 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); + +/** + * Refer to the eglQueryWaylandBufferWL extension function. + */ +bool wlr_egl_query_buffer(struct wlr_egl *egl, 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. + */ +EGLImageKHR wlr_egl_create_image(struct wlr_egl *egl, + EGLenum target, EGLClientBuffer buffer, const EGLint *attribs); + +/** + * Destroys an egl image created with the given wlr_egl. + */ +bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImageKHR image); + +/** + * Returns a string for the last error ocurred with egl. + */ +const char *egl_error(void); + +#endif diff --git a/include/wlr/render.h b/include/wlr/render.h index 47e2ff57..5e9f889e 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -43,6 +43,11 @@ void wlr_render_colored_ellipse(struct wlr_renderer *r, */ const enum wl_shm_format *wlr_renderer_get_formats( struct wlr_renderer *r, size_t *len); +/** + * Returns true if this wl_buffer is a DRM buffer. + */ +bool wlr_renderer_buffer_is_drm(struct wlr_renderer *renderer, + struct wl_resource *buffer); /** * Destroys this wlr_renderer. Textures must be destroyed separately. */ diff --git a/include/wlr/render/gles2.h b/include/wlr/render/gles2.h index 43d4719e..b368d6e1 100644 --- a/include/wlr/render/gles2.h +++ b/include/wlr/render/gles2.h @@ -1,8 +1,9 @@ #ifndef _WLR_GLES2_RENDERER_H #define _WLR_GLES2_RENDERER_H #include +#include struct wlr_egl; -struct wlr_renderer *wlr_gles2_renderer_init(); +struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_backend *backend); #endif diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index 4a4b22e2..69485e21 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -25,6 +25,8 @@ struct wlr_renderer_impl { const float (*color)[4], const float (*matrix)[16]); const enum wl_shm_format *(*formats)( struct wlr_renderer_state *state, size_t *len); + bool (*buffer_is_drm)(struct wlr_renderer_state *state, + struct wl_resource *buffer); void (*destroy)(struct wlr_renderer_state *state); }; diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index c9cbeba6..94b7ff1e 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -29,6 +29,7 @@ struct wlr_surface_state { struct wlr_surface { struct wl_resource *resource; + struct wlr_renderer *renderer; struct wlr_texture *texture; struct wlr_surface_state current, pending; const char *role; // the lifetime-bound role or null diff --git a/render/egl.c b/render/egl.c new file mode 100644 index 00000000..73680d1d --- /dev/null +++ b/render/egl.c @@ -0,0 +1,243 @@ +#include +#include +#include +#include // GBM_FORMAT_XRGB8888 +#include +#include +#include + +// 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 + +const char *egl_error(void) { + switch (eglGetError()) { + case EGL_SUCCESS: + return "Success"; + case EGL_NOT_INITIALIZED: + return "Not initialized"; + case EGL_BAD_ACCESS: + return "Bad access"; + case EGL_BAD_ALLOC: + return "Bad alloc"; + case EGL_BAD_ATTRIBUTE: + return "Bad attribute"; + case EGL_BAD_CONTEXT: + return "Bad Context"; + case EGL_BAD_CONFIG: + return "Bad Config"; + case EGL_BAD_CURRENT_SURFACE: + return "Bad current surface"; + case EGL_BAD_DISPLAY: + return "Bad display"; + case EGL_BAD_SURFACE: + return "Bad surface"; + case EGL_BAD_MATCH: + return "Bad match"; + case EGL_BAD_PARAMETER: + return "Bad parameter"; + case EGL_BAD_NATIVE_PIXMAP: + return "Bad native pixmap"; + case EGL_BAD_NATIVE_WINDOW: + return "Bad native window"; + case EGL_CONTEXT_LOST: + return "Context lost"; + default: + return "Unknown"; + } +} + +static bool egl_exts(struct wlr_egl *egl) { + egl->get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) + eglGetProcAddress("eglGetPlatformDisplayEXT"); + + if (!egl->get_platform_display) { + wlr_log(L_ERROR, "Failed to load EGL extension 'eglGetPlatformDisplayEXT'"); + return false; + } + + egl->create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) + eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"); + + if (!egl->get_platform_display) { + wlr_log(L_ERROR, + "Failed to load EGL extension 'eglCreatePlatformWindowSurfaceEXT'"); + return false; + } + + return true; +} + +static bool egl_get_config(EGLDisplay disp, EGLConfig *out, EGLenum platform) { + EGLint count = 0, matched = 0, ret; + + ret = eglGetConfigs(disp, NULL, 0, &count); + if (ret == EGL_FALSE || count == 0) { + wlr_log(L_ERROR, "eglGetConfigs returned no configs"); + return false; + } + + EGLConfig configs[count]; + + ret = eglChooseConfig(disp, NULL, configs, count, &matched); + if (ret == EGL_FALSE) { + wlr_log(L_ERROR, "eglChooseConfig failed"); + return false; + } + + for (int i = 0; i < matched; ++i) { + EGLint gbm_format; + + if (platform == EGL_PLATFORM_WAYLAND_EXT) { + *out = configs[i]; + return true; + } + + if (!eglGetConfigAttrib(disp, + configs[i], + EGL_NATIVE_VISUAL_ID, + &gbm_format)) { + continue; + } + + if (gbm_format == GBM_FORMAT_ARGB8888) { + *out = configs[i]; + return true; + } + } + + wlr_log(L_ERROR, "no valid egl config found"); + return false; +} + +bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, + void *remote_display) { + if (!egl_exts(egl)) { + return false; + } + + if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) { + wlr_log(L_ERROR, "Failed to bind to the OpenGL ES API: %s", egl_error()); + goto error; + } + + egl->display = egl->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; + } + + EGLint major, minor; + if (eglInitialize(egl->display, &major, &minor) == EGL_FALSE) { + wlr_log(L_ERROR, "Failed to initialize EGL: %s", egl_error()); + goto error; + } + + if (!egl_get_config(egl->display, &egl->config, platform)) { + wlr_log(L_ERROR, "Failed to get EGL config"); + goto error; + } + + static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE}; + + egl->context = eglCreateContext(egl->display, egl->config, + EGL_NO_CONTEXT, attribs); + + if (egl->context == EGL_NO_CONTEXT) { + wlr_log(L_ERROR, "Failed to create EGL context: %s", egl_error()); + goto error; + } + + 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; + } + + egl->eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC) + eglGetProcAddress("eglCreateImageKHR"); + egl->eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC) + eglGetProcAddress("eglDestroyImageKHR"); + egl->eglQueryWaylandBufferWL = (PFNEGLQUERYWAYLANDBUFFERWL) + (void*) eglGetProcAddress("eglQueryWaylandBufferWL"); + egl->eglBindWaylandDisplayWL = (PFNEGLBINDWAYLANDDISPLAYWL) + (void*) eglGetProcAddress("eglBindWaylandDisplayWL"); + egl->eglUnbindWaylandDisplayWL = (PFNEGLUNBINDWAYLANDDISPLAYWL) + (void*) eglGetProcAddress("eglUnbindWaylandDisplayWL"); + + 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", egl->egl_exts); + wlr_log(L_INFO, "Using %s", glGetString(GL_VERSION)); + wlr_log(L_INFO, "Supported OpenGL ES extensions: %s", egl->gl_exts); + return true; + +error: + eglTerminate(egl->display); + eglReleaseThread(); + eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); + return false; +} + +void wlr_egl_free(struct wlr_egl *egl) { + if (egl->wl_display && egl->eglUnbindWaylandDisplayWL) { + egl->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); +} + +bool wlr_egl_bind_display(struct wlr_egl *egl, struct wl_display *local_display) { + if (!egl->eglBindWaylandDisplayWL) { + return false; + } + + if (egl->eglBindWaylandDisplayWL(egl->display, local_display)) { + egl->wl_display = local_display; + return true; + } + + return false; +} + +bool wlr_egl_query_buffer(struct wlr_egl *egl, struct wl_resource *buf, + int attrib, int *value) { + if (!egl->eglQueryWaylandBufferWL) { + return false; + } + return egl->eglQueryWaylandBufferWL(egl->display, buf, attrib, value); +} + +EGLImage wlr_egl_create_image(struct wlr_egl *egl, EGLenum target, + EGLClientBuffer buffer, const EGLint *attribs) { + if (!egl->eglCreateImageKHR) { + return false; + } + + return egl->eglCreateImageKHR(egl->display, egl->context, target, + buffer, attribs); +} + +bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImage image) { + if (!egl->eglDestroyImageKHR) { + return false; + } + + egl->eglDestroyImageKHR(egl->display, image); + return true; +} + +EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window) { + EGLSurface surf = egl->create_platform_window_surface(egl->display, egl->config, + window, NULL); + if (surf == EGL_NO_SURFACE) { + wlr_log(L_ERROR, "Failed to create EGL surface: %s", egl_error()); + return EGL_NO_SURFACE; + } + return surf; +} diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 0de777cc..237dc67b 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -5,11 +5,12 @@ #include #include #include +#include +#include #include #include #include #include -#include "backend/egl.h" #include "render/gles2.h" PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = NULL; @@ -123,7 +124,7 @@ static void wlr_gles2_end(struct wlr_renderer_state *state) { } static struct wlr_texture *wlr_gles2_texture_init(struct wlr_renderer_state *state) { - return gles2_texture_init(); + return gles2_texture_init(state->egl); } static void draw_quad() { @@ -195,8 +196,14 @@ static const enum wl_shm_format *wlr_gles2_formats( return formats; } +static bool wlr_gles2_buffer_is_drm(struct wlr_renderer_state *state, + struct wl_resource *buffer) { + EGLint format; + return wlr_egl_query_buffer(state->egl, buffer, EGL_TEXTURE_FORMAT, &format); +} + static void wlr_gles2_destroy(struct wlr_renderer_state *state) { - // no-op + free(state); } static struct wlr_renderer_impl wlr_renderer_impl = { @@ -207,10 +214,16 @@ static struct wlr_renderer_impl wlr_renderer_impl = { .render_quad = wlr_gles2_render_quad, .render_ellipse = wlr_gles2_render_ellipse, .formats = wlr_gles2_formats, + .buffer_is_drm = wlr_gles2_buffer_is_drm, .destroy = wlr_gles2_destroy }; -struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_egl *egl) { +struct wlr_renderer *wlr_gles2_renderer_init(struct wlr_backend *backend) { init_globals(); - return wlr_renderer_init(NULL, &wlr_renderer_impl); + struct wlr_egl *egl = wlr_backend_get_egl(backend); + struct wlr_renderer_state *state = calloc(1, sizeof(struct wlr_renderer_state)); + struct wlr_renderer *renderer = wlr_renderer_init(state, &wlr_renderer_impl); + state->renderer = renderer; + state->egl = egl; + return renderer; } diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 2a2073c6..f2c88af5 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -5,12 +5,12 @@ #include #include #include +#include #include #include #include #include #include "render/gles2.h" -#include "backend/egl.h" static struct pixel_format external_pixel_format = { .wl_format = 0, @@ -139,23 +139,25 @@ static bool gles2_texture_update_shm(struct wlr_texture_state *texture, return true; } -static bool gles2_texture_upload_drm(struct wlr_texture_state *texture, - struct wl_resource* buf) { +static bool gles2_texture_upload_drm(struct wlr_texture_state *tex, + struct wl_resource *buf) { if (!glEGLImageTargetTexture2DOES) { return false; } EGLint format; - if (!wlr_egl_query_buffer(buf, EGL_TEXTURE_FORMAT, &format)) { + if (!wlr_egl_query_buffer(tex->egl, 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); + wlr_egl_query_buffer(tex->egl, buf, EGL_WIDTH, + (EGLint*)&tex->wlr_texture->width); + wlr_egl_query_buffer(tex->egl, buf, EGL_HEIGHT, + (EGLint*)&tex->wlr_texture->height); EGLint inverted_y; - wlr_egl_query_buffer(buf, EGL_WAYLAND_Y_INVERTED_WL, &inverted_y); + wlr_egl_query_buffer(tex->egl, buf, EGL_WAYLAND_Y_INVERTED_WL, &inverted_y); GLenum target; const struct pixel_format *pf; @@ -174,22 +176,22 @@ static bool gles2_texture_upload_drm(struct wlr_texture_state *texture, return false; } - gles2_texture_gen_texture(texture); - GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); + gles2_texture_gen_texture(tex); + GL_CALL(glBindTexture(GL_TEXTURE_2D, tex->tex_id)); EGLint attribs[] = { EGL_WAYLAND_PLANE_WL, 0, EGL_NONE }; - texture->image = wlr_egl_create_image(EGL_WAYLAND_BUFFER_WL, + tex->image = wlr_egl_create_image(tex->egl, EGL_WAYLAND_BUFFER_WL, (EGLClientBuffer*) buf, attribs); - if (!texture->image) { + if (!tex->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; + GL_CALL(glBindTexture(target, tex->tex_id)); + GL_CALL(glEGLImageTargetTexture2DOES(target, tex->image)); + tex->wlr_texture->valid = true; + tex->pixel_format = pf; return true; } @@ -230,10 +232,11 @@ static struct wlr_texture_impl wlr_texture_impl = { .destroy = gles2_texture_destroy, }; -struct wlr_texture *gles2_texture_init() { +struct wlr_texture *gles2_texture_init(struct wlr_egl *egl) { struct wlr_texture_state *state = calloc(sizeof(struct wlr_texture_state), 1); struct wlr_texture *texture = wlr_texture_init(state, &wlr_texture_impl); state->wlr_texture = texture; + state->egl = egl; wl_signal_init(&texture->destroy_signal); return texture; } diff --git a/render/meson.build b/render/meson.build index 93bf8b4b..a0a44ea2 100644 --- a/render/meson.build +++ b/render/meson.build @@ -1,10 +1,11 @@ wlr_files += files( + 'egl.c', 'matrix.c', - 'wlr_renderer.c', - 'wlr_texture.c', 'gles2/pixel_format.c', 'gles2/renderer.c', 'gles2/shaders.c', 'gles2/texture.c', 'gles2/util.c', + 'wlr_renderer.c', + 'wlr_texture.c', ) diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index 8418fd5b..640d737e 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -46,3 +46,8 @@ const enum wl_shm_format *wlr_renderer_get_formats( struct wlr_renderer *r, size_t *len) { return r->impl->formats(r->state, len); } + +bool wlr_renderer_buffer_is_drm(struct wlr_renderer *r, + struct wl_resource *buffer) { + return r->impl->buffer_is_drm(r->state, buffer); +} diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 1ce4926e..cc3b429d 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -2,9 +2,9 @@ #include #include #include +#include #include #include -#include "backend/egl.h" static void surface_destroy(struct wl_client *client, struct wl_resource *resource) { wl_resource_destroy(resource); @@ -124,8 +124,7 @@ void wlr_surface_flush_damage(struct wlr_surface *surface) { } struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current.buffer); if (!buffer) { - EGLint format; - if (wlr_egl_query_buffer(surface->current.buffer, EGL_TEXTURE_FORMAT, &format)) { + if (wlr_renderer_buffer_is_drm(surface->renderer, surface->pending.buffer)) { wlr_texture_upload_drm(surface->texture, surface->pending.buffer); goto release; } else { @@ -202,6 +201,7 @@ static void destroy_surface(struct wl_resource *resource) { struct wlr_surface *wlr_surface_create(struct wl_resource *res, struct wlr_renderer *renderer) { struct wlr_surface *surface = calloc(1, sizeof(struct wlr_surface)); + surface->renderer = renderer; surface->texture = wlr_render_texture_init(renderer); surface->resource = res; wl_signal_init(&surface->signals.commit); -- cgit v1.2.3