diff options
33 files changed, 427 insertions, 74 deletions
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 e361358e..a7e6824c 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -12,6 +12,7 @@ #include <wlr/interfaces/wlr_output.h> #include <wlr/util/list.h> #include <wlr/util/log.h> +#include <wlr/egl.h> #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) { @@ -149,6 +155,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: diff --git a/backend/drm/drm.c b/backend/drm/drm.c index b5742eca..8062a478 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(); + plane->wlr_rend = wlr_gles2_renderer_init(drm->base); if (!plane->wlr_rend) { return false; } 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 ef480c18..1a156802 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -4,6 +4,7 @@ #include <EGL/egl.h> #include <EGL/eglext.h> #include <wayland-server.h> +#include <wlr/egl.h> #include <wlr/backend/interface.h> #include <wlr/interfaces/wlr_output.h> #include <wlr/interfaces/wlr_input_device.h> @@ -52,6 +53,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); } @@ -93,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/backend/wayland/output.c b/backend/wayland/output.c index 96f71efa..2c7086e2 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -117,6 +117,7 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *_backend) { strncpy(wlr_output->model, "wayland", sizeof(wlr_output->model)); snprintf(wlr_output->name, sizeof(wlr_output->name), "WL-%zd", backend->outputs->length + 1); + wlr_output_update_matrix(wlr_output); ostate->backend = backend; ostate->wlr_output = wlr_output; diff --git a/examples/compositor.h b/examples/compositor.h index 4649a8d3..2847808f 100644 --- a/examples/compositor.h +++ b/examples/compositor.h @@ -14,6 +14,10 @@ struct wl_compositor_state { void wl_compositor_init(struct wl_display *display, struct wl_compositor_state *state, struct wlr_renderer *renderer); +struct wlr_surface; +void wl_compositor_surface_destroyed(struct wl_compositor_state *compositor, + struct wlr_surface *surface); + struct wl_shell_state { struct wl_global *wl_global; struct wl_list wl_resources; 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/compositor/wl_compositor.c b/examples/compositor/wl_compositor.c index e5b1120e..e25dddac 100644 --- a/examples/compositor/wl_compositor.c +++ b/examples/compositor/wl_compositor.c @@ -7,9 +7,8 @@ #include "compositor.h" static void destroy_surface_listener(struct wl_listener *listener, void *data) { - struct wl_compositor_state *state; - struct wlr_surface *surface = data; - state = wl_container_of(listener, state, destroy_surface_listener); + struct wlr_surface *surface = wl_resource_get_user_data(data); + struct wl_compositor_state *state = surface->compositor_data; struct wl_resource *res = NULL; wl_list_for_each(res, &state->surfaces, link) { @@ -26,8 +25,11 @@ static void wl_compositor_create_surface(struct wl_client *client, struct wl_resource *surface_resource = wl_resource_create(client, &wl_surface_interface, wl_resource_get_version(resource), id); struct wlr_surface *surface = wlr_surface_create(surface_resource, state->renderer); + surface->compositor_data = state; + surface->compositor_listener.notify = &destroy_surface_listener; + wl_resource_add_destroy_listener(surface_resource, &surface->compositor_listener); + wl_list_insert(&state->surfaces, wl_resource_get_link(surface_resource)); - wl_signal_add(&surface->signals.destroy, &state->destroy_surface_listener); } static void wl_compositor_create_region(struct wl_client *client, @@ -74,7 +76,6 @@ void wl_compositor_init(struct wl_display *display, &wl_compositor_interface, 4, state, wl_compositor_bind); state->wl_global = wl_global; state->renderer = renderer; - state->destroy_surface_listener.notify = destroy_surface_listener; wl_list_init(&state->wl_resources); wl_list_init(&state->surfaces); } 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 0be6dcb9..0651efde 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(); - + 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 <wlr/backend/session.h> #include <wlr/backend/drm.h> +#include <wlr/egl.h> #include <wlr/util/list.h> -#include <backend/egl.h> #include <backend/udev.h> #include "drm-properties.h" diff --git a/include/backend/egl.h b/include/backend/egl.h deleted file mode 100644 index 0422a52b..00000000 --- a/include/backend/egl.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef WLR_BACKEND_EGL_H -#define WLR_BACKEND_EGL_H - -#include <EGL/egl.h> -#include <stdbool.h> - -struct wlr_egl { - EGLDisplay display; - EGLConfig config; - EGLContext context; -}; - -const char *egl_error(void); -bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *display); -void wlr_egl_free(struct wlr_egl *egl); -EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window); - -#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 <wayland-client.h> #include <wayland-server.h> #include <wayland-egl.h> +#include <wlr/egl.h> #include <wlr/backend/wayland.h> #include <wlr/types/wlr_input_device.h> #include <wlr/util/list.h> -#include "backend/egl.h" struct wlr_backend_state { /* local state */ diff --git a/include/render/gles2.h b/include/render/gles2.h index 41af0593..3006872c 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -4,9 +4,16 @@ #include <string.h> #include <stdbool.h> #include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> +#include <EGL/egl.h> +#include <EGL/eglext.h> +#include <wlr/egl.h> +#include <wlr/backend.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; @@ -14,10 +21,17 @@ 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; }; struct shaders { @@ -25,6 +39,7 @@ struct shaders { GLuint rgba, rgbx; GLuint quad; GLuint ellipse; + GLuint external; }; extern struct shaders shaders; @@ -39,6 +54,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(...) \ 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 <wayland-server.h> #include <wlr/backend/session.h> +#include <wlr/egl.h> 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 <stdbool.h> #include <wlr/backend.h> +#include <wlr/egl.h> 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 <EGL/egl.h> +#include <EGL/eglext.h> +#include <stdbool.h> + +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 61169333..5e9f889e 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -44,6 +44,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. */ void wlr_renderer_destroy(struct wlr_renderer *renderer); @@ -65,7 +70,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 +85,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, diff --git a/include/wlr/render/gles2.h b/include/wlr/render/gles2.h index a779ffff..b368d6e1 100644 --- a/include/wlr/render/gles2.h +++ b/include/wlr/render/gles2.h @@ -1,7 +1,9 @@ #ifndef _WLR_GLES2_RENDERER_H #define _WLR_GLES2_RENDERER_H #include <wlr/render.h> +#include <wlr/backend.h> -struct wlr_renderer *wlr_gles2_renderer_init(); +struct wlr_egl; +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 be9ffe7a..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); }; @@ -42,7 +44,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); diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index e8e747cd..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 @@ -37,11 +38,13 @@ struct wlr_surface { float surface_to_buffer_matrix[16]; struct { - struct wl_signal destroy; struct wl_signal commit; } signals; struct wl_list frame_callback_list; // wl_surface.frame + + struct wl_listener compositor_listener; // destroy listener used by compositor + void *compositor_data; }; struct wlr_renderer; diff --git a/backend/egl.c b/render/egl.c index a547523c..73680d1d 100644 --- a/backend/egl.c +++ b/render/egl.c @@ -1,9 +1,14 @@ #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" +#include <wlr/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 const char *egl_error(void) { switch (eglGetError()) { @@ -42,23 +47,19 @@ const char *egl_error(void) { } } -// EGL extensions -PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display; -PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC create_platform_window_surface; - -static bool egl_exts() { - get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) +static bool egl_exts(struct wlr_egl *egl) { + egl->get_platform_display = (PFNEGLGETPLATFORMDISPLAYEXTPROC) eglGetProcAddress("eglGetPlatformDisplayEXT"); - if (!get_platform_display) { + if (!egl->get_platform_display) { wlr_log(L_ERROR, "Failed to load EGL extension 'eglGetPlatformDisplayEXT'"); return false; } - create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) + egl->create_platform_window_surface = (PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"); - if (!get_platform_display) { + if (!egl->get_platform_display) { wlr_log(L_ERROR, "Failed to load EGL extension 'eglCreatePlatformWindowSurfaceEXT'"); return false; @@ -109,8 +110,9 @@ 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) { - if (!egl_exts()) { +bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, + void *remote_display) { + if (!egl_exts(egl)) { return false; } @@ -119,7 +121,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 = 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; @@ -147,11 +149,29 @@ 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; + } + + 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", 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,15 +182,58 @@ 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 && 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 = create_platform_window_surface(egl->display, egl->config, + 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()); diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 703edd46..237dc67b 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -2,14 +2,18 @@ #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/egl.h> +#include <wlr/backend.h> #include <wlr/render.h> #include <wlr/render/interface.h> #include <wlr/render/matrix.h> #include <wlr/util/log.h> #include "render/gles2.h" +PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES = NULL; struct shaders shaders; static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) { @@ -65,13 +69,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(); } @@ -97,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() { @@ -169,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 = { @@ -181,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_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/shaders.c b/render/gles2/shaders.c index f5e61dd6..c1db6e20 100644 --- a/render/gles2/shaders.c +++ b/render/gles2/shaders.c @@ -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);" +"}"; diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 7a030f25..f2c88af5 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -5,12 +5,33 @@ #include <GLES2/gl2ext.h> #include <wayland-util.h> #include <wayland-server-protocol.h> +#include <wlr/egl.h> #include <wlr/render.h> #include <wlr/render/interface.h> #include <wlr/render/matrix.h> #include <wlr/util/log.h> #include "render/gles2.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, const unsigned char *pixels) { @@ -24,7 +45,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, @@ -74,7 +96,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)); @@ -113,6 +135,64 @@ 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 *tex, + struct wl_resource *buf) { + if (!glEGLImageTargetTexture2DOES) { + return false; + } + + EGLint 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(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(tex->egl, 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(tex); + GL_CALL(glBindTexture(GL_TEXTURE_2D, tex->tex_id)); + + EGLint attribs[] = { EGL_WAYLAND_PLANE_WL, 0, EGL_NONE }; + tex->image = wlr_egl_create_image(tex->egl, EGL_WAYLAND_BUFFER_WL, + (EGLClientBuffer*) buf, attribs); + 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, tex->tex_id)); + GL_CALL(glEGLImageTargetTexture2DOES(target, tex->image)); + tex->wlr_texture->valid = true; + tex->pixel_format = pf; + return true; } @@ -146,15 +226,17 @@ 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, }; -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/render/wlr_texture.c b/render/wlr_texture.c index 64f90990..0d4362dc 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -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); diff --git a/types/wlr_output.c b/types/wlr_output.c index eff4d0ef..3d3acb4f 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -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) { diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 25c8e0df..cc3b429d 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <wayland-server.h> #include <wlr/util/log.h> +#include <wlr/egl.h> #include <wlr/render/interface.h> #include <wlr/types/wlr_surface.h> @@ -123,8 +124,13 @@ void wlr_surface_flush_damage(struct wlr_surface *surface) { } struct wl_shm_buffer *buffer = wl_shm_buffer_get(surface->current.buffer); if (!buffer) { - wlr_log(L_INFO, "Unknown buffer handle attached"); - return; + if (wlr_renderer_buffer_is_drm(surface->renderer, surface->pending.buffer)) { + wlr_texture_upload_drm(surface->texture, surface->pending.buffer); + goto release; + } else { + wlr_log(L_INFO, "Unknown buffer handle attached"); + return; + } } pixman_region32_t damage = surface->current.surface_damage; if (!pixman_region32_not_empty(&damage)) { @@ -182,23 +188,23 @@ const struct wl_surface_interface surface_interface = { static void destroy_surface(struct wl_resource *resource) { struct wlr_surface *surface = wl_resource_get_user_data(resource); - wl_signal_emit(&surface->signals.destroy, surface); - wlr_texture_destroy(surface->texture); + wlr_texture_destroy(surface->texture); struct wlr_frame_callback *cb, *next; wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link) { wl_resource_destroy(cb->resource); } + free(surface); } 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); - wl_signal_init(&surface->signals.destroy); wl_list_init(&surface->frame_callback_list); wl_resource_set_implementation(res, &surface_interface, surface, destroy_surface); |