From f775742b78ddb0a9084558b9ed4aa2a6f94fa7ed Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Mon, 11 Dec 2023 18:06:51 +0100 Subject: [PATCH] Converted fx_framebuffer & fx_texture to their wlr counterpart --- .../sway/desktop/fx_renderer/fx_framebuffer.h | 27 -- .../sway/desktop/fx_renderer/fx_renderer.h | 105 +++++- .../desktop/fx_renderer/fx_stencilbuffer.h | 4 +- include/sway/desktop/fx_renderer/fx_texture.h | 22 -- .../sway/desktop/fx_renderer/pixel_format.h | 26 ++ sway/desktop/fx_renderer/fx_framebuffer.c | 181 +++++---- sway/desktop/fx_renderer/fx_renderer.c | 102 +++-- sway/desktop/fx_renderer/fx_stencilbuffer.c | 31 +- sway/desktop/fx_renderer/fx_texture.c | 356 ++++++++++++++++-- sway/desktop/fx_renderer/pixel_format.c | 178 +++++++++ sway/desktop/render.c | 84 +++-- sway/meson.build | 1 + 12 files changed, 906 insertions(+), 211 deletions(-) delete mode 100644 include/sway/desktop/fx_renderer/fx_framebuffer.h delete mode 100644 include/sway/desktop/fx_renderer/fx_texture.h create mode 100644 include/sway/desktop/fx_renderer/pixel_format.h create mode 100644 sway/desktop/fx_renderer/pixel_format.c diff --git a/include/sway/desktop/fx_renderer/fx_framebuffer.h b/include/sway/desktop/fx_renderer/fx_framebuffer.h deleted file mode 100644 index 3372cd00..00000000 --- a/include/sway/desktop/fx_renderer/fx_framebuffer.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef FX_FRAMEBUFFER_H -#define FX_FRAMEBUFFER_H - -#include -#include -#include - -#include "sway/desktop/fx_renderer/fx_stencilbuffer.h" -#include "sway/desktop/fx_renderer/fx_texture.h" - -struct fx_framebuffer { - GLuint fb; - struct fx_stencilbuffer stencil_buffer; - struct fx_texture texture; -}; - -struct fx_framebuffer fx_framebuffer_create(); - -void fx_framebuffer_bind(struct fx_framebuffer *buffer); - -void fx_framebuffer_update(struct fx_framebuffer *buffer, int width, int height); - -void fx_framebuffer_add_stencil_buffer(struct fx_framebuffer *buffer, int width, int height); - -void fx_framebuffer_release(struct fx_framebuffer *buffer); - -#endif diff --git a/include/sway/desktop/fx_renderer/fx_renderer.h b/include/sway/desktop/fx_renderer/fx_renderer.h index 410e3d94..6ab26aa9 100644 --- a/include/sway/desktop/fx_renderer/fx_renderer.h +++ b/include/sway/desktop/fx_renderer/fx_renderer.h @@ -5,9 +5,11 @@ #include #include #include - -#include "sway/desktop/fx_renderer/fx_framebuffer.h" -#include "sway/desktop/fx_renderer/fx_texture.h" +#include +#include +#include +#include +#include "sway/desktop/fx_renderer/fx_stencilbuffer.h" enum corner_location { TOP_LEFT, TOP_RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT, ALL, NONE }; @@ -117,6 +119,42 @@ struct tex_shader { GLint discard_transparent; }; +struct fx_framebuffer { + bool initialized; + + GLuint fbo; + GLuint rbo; + + struct wlr_buffer *wlr_buffer; + struct fx_renderer *renderer; + struct wl_list link; // fx_renderer.buffers + struct wlr_addon addon; + + EGLImageKHR image; +}; + +struct fx_texture { + struct wlr_texture wlr_texture; + struct fx_renderer *fx_renderer; + struct wl_list link; // fx_renderer.textures + + // Basically: + // GL_TEXTURE_2D == mutable + // GL_TEXTURE_EXTERNAL_OES == immutable + GLuint target; + GLuint tex; + + EGLImageKHR image; + + bool has_alpha; + + // Only affects target == GL_TEXTURE_2D + uint32_t drm_format; // used to interpret upload data + // If imported from a wlr_buffer + struct wlr_buffer *buffer; + struct wlr_addon buffer_addon; +}; + struct fx_renderer { float projection[9]; @@ -124,6 +162,17 @@ struct fx_renderer { struct wlr_output *wlr_output; + struct wlr_egl *egl; + + struct fx_stencilbuffer stencil_buffer; + + struct wl_list textures; // fx_texture.link + struct wl_list buffers; // fx_framebuffer.link + + // The FBO and texture used by wlroots + GLuint wlr_main_buffer_fbo; + struct wlr_gles2_texture_attribs wlr_main_texture_attribs; + // The framebuffer used by wlroots struct fx_framebuffer wlr_buffer; // Contains the blurred background for tiled windows @@ -143,10 +192,12 @@ struct fx_renderer { struct { bool OES_egl_image_external; + bool OES_egl_image; } exts; struct { PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; + PFNGLEGLIMAGETARGETRENDERBUFFERSTORAGEOESPROC glEGLImageTargetRenderbufferStorageOES; } procs; struct { @@ -167,6 +218,43 @@ struct fx_renderer { } shaders; }; +/// +/// fx_framebuffer +/// + +struct fx_framebuffer fx_framebuffer_create(void); + +void fx_framebuffer_bind(struct fx_framebuffer *buffer); + +void fx_framebuffer_bind_wlr_fbo(struct fx_renderer *renderer); + +void fx_framebuffer_update(struct fx_renderer *fx_renderer, struct fx_framebuffer *fx_buffer, + int width, int height); + +void fx_framebuffer_add_stencil_buffer(struct fx_framebuffer *buffer, int width, int height); + +void fx_framebuffer_release(struct fx_framebuffer *buffer); + +/// +/// fx_texture +/// + +struct fx_texture *fx_get_texture(struct wlr_texture *wlr_texture); + +struct fx_texture *fx_texture_from_buffer(struct fx_renderer *fx_renderer, + struct wlr_buffer *buffer); + +void fx_texture_destroy(struct fx_texture *texture); + +bool wlr_texture_is_fx(struct wlr_texture *wlr_texture); + +void wlr_gles2_texture_get_fx_attribs(struct fx_texture *texture, + struct wlr_gles2_texture_attribs *attribs); + +/// +/// fx_renderer +/// + struct fx_renderer *fx_renderer_create(struct wlr_egl *egl, struct wlr_output *output); void fx_renderer_fini(struct fx_renderer *renderer); @@ -179,6 +267,9 @@ void fx_renderer_clear(const float color[static 4]); void fx_renderer_scissor(struct wlr_box *box); +void fx_renderer_get_texture_attribs(struct wlr_texture *texture, + struct wlr_gles2_texture_attribs *attribs); + // Initialize the stenciling work void fx_renderer_stencil_mask_init(); @@ -188,11 +279,11 @@ void fx_renderer_stencil_mask_close(bool draw_inside_mask); // Finish stenciling and clear the buffer void fx_renderer_stencil_mask_fini(); -bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct fx_texture *fx_texture, +bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *wlr_texture, const struct wlr_fbox *src_box, const struct wlr_box *dst_box, const float matrix[static 9], struct decoration_data deco_data); -bool fx_render_texture_with_matrix(struct fx_renderer *renderer, struct fx_texture *fx_texture, +bool fx_render_texture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *wlr_texture, const struct wlr_box *dst_box, const float matrix[static 9], struct decoration_data deco_data); void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, @@ -211,7 +302,7 @@ void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *bo float blur_sigma); void fx_render_blur(struct fx_renderer *renderer, const float matrix[static 9], - struct fx_framebuffer **buffer, struct blur_shader *shader, const struct wlr_box *box, - int blur_radius); + struct wlr_gles2_texture_attribs *texture, struct blur_shader *shader, + const struct wlr_box *box, int blur_radius); #endif diff --git a/include/sway/desktop/fx_renderer/fx_stencilbuffer.h b/include/sway/desktop/fx_renderer/fx_stencilbuffer.h index 157c0282..6909f96e 100644 --- a/include/sway/desktop/fx_renderer/fx_stencilbuffer.h +++ b/include/sway/desktop/fx_renderer/fx_stencilbuffer.h @@ -11,7 +11,9 @@ struct fx_stencilbuffer { int height; }; -struct fx_stencilbuffer fx_stencilbuffer_create(); +struct fx_stencilbuffer fx_stencilbuffer_create(void); + +void fx_stencilbuffer_init(struct fx_stencilbuffer *stencil_buffer, int width, int height); void fx_stencilbuffer_release(struct fx_stencilbuffer *stencil_buffer); diff --git a/include/sway/desktop/fx_renderer/fx_texture.h b/include/sway/desktop/fx_renderer/fx_texture.h deleted file mode 100644 index 62e635e6..00000000 --- a/include/sway/desktop/fx_renderer/fx_texture.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef FX_TEXTURE_H -#define FX_TEXTURE_H - -#include -#include -#include - -struct fx_texture { - GLuint target; - GLuint id; - bool has_alpha; - int width; - int height; -}; - -struct fx_texture fx_texture_create(); - -struct fx_texture fx_texture_from_wlr_texture(struct wlr_texture *tex); - -void fx_texture_release(struct fx_texture *texture); - -#endif diff --git a/include/sway/desktop/fx_renderer/pixel_format.h b/include/sway/desktop/fx_renderer/pixel_format.h new file mode 100644 index 00000000..fe2a4737 --- /dev/null +++ b/include/sway/desktop/fx_renderer/pixel_format.h @@ -0,0 +1,26 @@ +#ifndef SWAY_PIXEL_FORMAT_H +#define SWAY_PIXEL_FORMAT_H + +#include + +struct wlr_pixel_format_info { + uint32_t drm_format; + + /* Equivalent of the format if it has an alpha channel, + * DRM_FORMAT_INVALID (0) if NA + */ + uint32_t opaque_substitute; + + /* Bits per pixels */ + uint32_t bpp; + + /* True if the format has an alpha channel */ + bool has_alpha; +}; + +const struct wlr_pixel_format_info *drm_get_pixel_format_info(uint32_t fmt); + +uint32_t convert_wl_shm_format_to_drm(enum wl_shm_format fmt); +enum wl_shm_format convert_drm_format_to_wl_shm(uint32_t fmt); + +#endif diff --git a/sway/desktop/fx_renderer/fx_framebuffer.c b/sway/desktop/fx_renderer/fx_framebuffer.c index dd8c27b1..63da23d0 100644 --- a/sway/desktop/fx_renderer/fx_framebuffer.c +++ b/sway/desktop/fx_renderer/fx_framebuffer.c @@ -1,89 +1,142 @@ -#include "log.h" -#include "sway/desktop/fx_renderer/fx_framebuffer.h" -#include "sway/desktop/fx_renderer/fx_stencilbuffer.h" -#include "sway/desktop/fx_renderer/fx_texture.h" +#include +#include +#include +#include +#include +#include -struct fx_framebuffer fx_framebuffer_create() { +#include "log.h" +#include "render/egl.h" +#include "render/gles2.h" +#include "sway/desktop/fx_renderer/fx_renderer.h" + +static void handle_buffer_destroy(struct wlr_addon *addon) { + struct fx_framebuffer *buffer = + wl_container_of(addon, buffer, addon); + fx_framebuffer_release(buffer); +} + +static const struct wlr_addon_interface buffer_addon_impl = { + .name = "fx_framebuffer", + .destroy = handle_buffer_destroy, +}; + + +struct fx_framebuffer fx_framebuffer_create(void) { return (struct fx_framebuffer) { - .fb = -1, - .stencil_buffer = fx_stencilbuffer_create(), - .texture = fx_texture_create(), + .initialized = false, + .fbo = -1, + .rbo = -1, + .wlr_buffer = NULL, + .image = NULL, }; } -void fx_framebuffer_bind(struct fx_framebuffer *buffer) { - glBindFramebuffer(GL_FRAMEBUFFER, buffer->fb); +void fx_framebuffer_bind(struct fx_framebuffer *fx_buffer) { + glBindFramebuffer(GL_FRAMEBUFFER, fx_buffer->fbo); } -void fx_framebuffer_update(struct fx_framebuffer *buffer, int width, int height) { +void fx_framebuffer_bind_wlr_fbo(struct fx_renderer *renderer) { + glBindFramebuffer(GL_FRAMEBUFFER, renderer->wlr_main_buffer_fbo); +} + +void fx_framebuffer_update(struct fx_renderer *fx_renderer, struct fx_framebuffer *fx_buffer, + int width, int height) { + struct wlr_output *output = fx_renderer->wlr_output; + + fx_buffer->renderer = fx_renderer; + bool first_alloc = false; - if (buffer->fb == (uint32_t) -1) { - glGenFramebuffers(1, &buffer->fb); + if (!fx_buffer->wlr_buffer || + fx_buffer->wlr_buffer->width != width || + fx_buffer->wlr_buffer->height != height) { + wlr_buffer_drop(fx_buffer->wlr_buffer); + fx_buffer->wlr_buffer = wlr_allocator_create_buffer(output->allocator, + width, height, output->swapchain->format); first_alloc = true; } - if (buffer->texture.id == 0) { + if (fx_buffer->fbo == (uint32_t) -1 || first_alloc) { + glGenFramebuffers(1, &fx_buffer->fbo); first_alloc = true; - glGenTextures(1, &buffer->texture.id); - glBindTexture(GL_TEXTURE_2D, buffer->texture.id); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); } - if (first_alloc || buffer->texture.width != width || buffer->texture.height != height) { - glBindTexture(GL_TEXTURE_2D, buffer->texture.id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); - - glBindFramebuffer(GL_FRAMEBUFFER, buffer->fb); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, - buffer->texture.id, 0); - buffer->texture.target = GL_TEXTURE_2D; - buffer->texture.has_alpha = false; - buffer->texture.width = width; - buffer->texture.height = height; - - GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); - if (status != GL_FRAMEBUFFER_COMPLETE) { - sway_log(SWAY_ERROR, "Framebuffer incomplete, couldn't create! (FB status: %i)", status); - return; + if (fx_buffer->rbo == (uint32_t) -1 || first_alloc) { + struct wlr_dmabuf_attributes dmabuf = {0}; + if (!wlr_buffer_get_dmabuf(fx_buffer->wlr_buffer, &dmabuf)) { + goto error_buffer; + } + + bool external_only; + fx_buffer->image = wlr_egl_create_image_from_dmabuf(fx_renderer->egl, + &dmabuf, &external_only); + if (fx_buffer->image == EGL_NO_IMAGE_KHR) { + goto error_buffer; + } + + glGenRenderbuffers(1, &fx_buffer->rbo); + glBindRenderbuffer(GL_RENDERBUFFER, fx_buffer->rbo); + fx_renderer->procs.glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, + fx_buffer->image); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + glBindFramebuffer(GL_FRAMEBUFFER, fx_buffer->fbo); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, + GL_RENDERBUFFER, fx_buffer->rbo); + GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + if (fb_status != GL_FRAMEBUFFER_COMPLETE) { + wlr_log(WLR_ERROR, "Failed to create FBO"); + goto error_image; } - sway_log(SWAY_DEBUG, "Framebuffer created, status %i", status); } - glBindTexture(GL_TEXTURE_2D, 0); + if (!fx_buffer->initialized) { + fx_buffer->initialized = true; + + wlr_addon_init(&fx_buffer->addon, &fx_buffer->wlr_buffer->addons, fx_renderer, + &buffer_addon_impl); + + wl_list_insert(&fx_renderer->buffers, &fx_buffer->link); + } + + if (first_alloc) { + wlr_log(WLR_DEBUG, "Created GL FBO for buffer %dx%d", + fx_buffer->wlr_buffer->width, fx_buffer->wlr_buffer->height); + } + + return; +error_image: + wlr_egl_destroy_image(fx_renderer->egl, fx_buffer->image); +error_buffer: + wlr_log(WLR_ERROR, "Could not create FX buffer! Aborting..."); + abort(); } -void fx_framebuffer_add_stencil_buffer(struct fx_framebuffer *buffer, int width, int height) { - bool first_alloc = false; - - if (buffer->stencil_buffer.rb == (uint32_t) -1) { - glGenRenderbuffers(1, &buffer->stencil_buffer.rb); - first_alloc = true; - } - - if (first_alloc || buffer->stencil_buffer.width != width || buffer->stencil_buffer.height != height) { - glBindRenderbuffer(GL_RENDERBUFFER, buffer->stencil_buffer.rb); - glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height); - buffer->stencil_buffer.width = width; - buffer->stencil_buffer.height = height; - } - - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, buffer->stencil_buffer.rb); -} - -void fx_framebuffer_release(struct fx_framebuffer *buffer) { +void fx_framebuffer_release(struct fx_framebuffer *fx_buffer) { // Release the framebuffer - if (buffer->fb != (uint32_t) -1 && buffer->fb) { - glDeleteFramebuffers(1, &buffer->fb); + struct wlr_egl_context prev_ctx; + if (fx_buffer->initialized) { + wl_list_remove(&fx_buffer->link); + wlr_addon_finish(&fx_buffer->addon); + + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(fx_buffer->renderer->egl); } - buffer->fb = -1; - // Release the stencil buffer - fx_stencilbuffer_release(&buffer->stencil_buffer); + glDeleteFramebuffers(1, &fx_buffer->fbo); + fx_buffer->fbo = -1; + glDeleteRenderbuffers(1, &fx_buffer->rbo); + fx_buffer->rbo = -1; - // Release the texture - fx_texture_release(&buffer->texture); + + if (fx_buffer->initialized) { + wlr_egl_destroy_image(fx_buffer->renderer->egl, fx_buffer->image); + + wlr_egl_restore_context(&prev_ctx); + } + + fx_buffer->initialized = false; } diff --git a/sway/desktop/fx_renderer/fx_renderer.c b/sway/desktop/fx_renderer/fx_renderer.c index d2fef6f1..920efade 100644 --- a/sway/desktop/fx_renderer/fx_renderer.c +++ b/sway/desktop/fx_renderer/fx_renderer.c @@ -13,10 +13,8 @@ #include #include "log.h" -#include "sway/desktop/fx_renderer/fx_framebuffer.h" #include "sway/desktop/fx_renderer/fx_renderer.h" #include "sway/desktop/fx_renderer/fx_stencilbuffer.h" -#include "sway/desktop/fx_renderer/fx_texture.h" #include "sway/desktop/fx_renderer/matrix.h" #include "sway/server.h" @@ -260,7 +258,11 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl, struct wlr_output *w return NULL; } + wl_list_init(&renderer->buffers); + wl_list_init(&renderer->textures); + renderer->wlr_output = wlr_output; + renderer->egl = egl; // TODO: wlr_egl_make_current or eglMakeCurrent? // TODO: assert instead of conditional statement? @@ -270,7 +272,11 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl, struct wlr_output *w return NULL; } - renderer->wlr_buffer = fx_framebuffer_create(); + // Create the stencil buffer + renderer->stencil_buffer = fx_stencilbuffer_create(); + + // Create the FBOs + renderer->wlr_main_buffer_fbo = -1; renderer->blur_buffer = fx_framebuffer_create(); renderer->blur_saved_pixels_buffer = fx_framebuffer_create(); renderer->effects_buffer = fx_framebuffer_create(); @@ -298,6 +304,12 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl, struct wlr_output *w "glEGLImageTargetTexture2DOES"); } + if (check_gl_ext(exts_str, "GL_OES_EGL_image")) { + renderer->exts.OES_egl_image = true; + load_gl_proc(&renderer->procs.glEGLImageTargetRenderbufferStorageOES, + "glEGLImageTargetRenderbufferStorageOES"); + } + // blur shaders if (!link_blur_program(&renderer->shaders.blur1, blur1_frag_src)) { goto error; @@ -395,6 +407,20 @@ void fx_renderer_fini(struct fx_renderer *renderer) { fx_framebuffer_release(&renderer->blur_saved_pixels_buffer); fx_framebuffer_release(&renderer->effects_buffer); fx_framebuffer_release(&renderer->effects_buffer_swapped); + + struct fx_framebuffer *fx_buffer, *fx_buffer_tmp; + wl_list_for_each_safe(fx_buffer, fx_buffer_tmp, &renderer->buffers, link) { + fx_framebuffer_release(fx_buffer); + } + + struct fx_texture *tex, *tex_tmp; + wl_list_for_each_safe(tex, tex_tmp, &renderer->textures, link) { + fx_texture_destroy(tex); + } + + fx_stencilbuffer_release(&renderer->stencil_buffer); + + free(renderer); } void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) { @@ -403,23 +429,23 @@ void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) { renderer->viewport_height = height; // Store the wlr FBO - renderer->wlr_buffer.fb = + renderer->wlr_main_buffer_fbo = wlr_gles2_renderer_get_current_fbo(renderer->wlr_output->renderer); // Get the fx_texture struct wlr_texture *wlr_texture = wlr_texture_from_buffer( renderer->wlr_output->renderer, renderer->wlr_output->back_buffer); - renderer->wlr_buffer.texture = fx_texture_from_wlr_texture(wlr_texture); + wlr_gles2_texture_get_attribs(wlr_texture, &renderer->wlr_main_texture_attribs); wlr_texture_destroy(wlr_texture); // Add the stencil to the wlr fbo - fx_framebuffer_add_stencil_buffer(&renderer->wlr_buffer, width, height); + fx_stencilbuffer_init(&renderer->stencil_buffer, width, height); - // Create the framebuffers - fx_framebuffer_update(&renderer->blur_saved_pixels_buffer, width, height); - fx_framebuffer_update(&renderer->effects_buffer, width, height); - fx_framebuffer_update(&renderer->effects_buffer_swapped, width, height); + // Create the additional FBOs + fx_framebuffer_update(renderer, &renderer->blur_saved_pixels_buffer, width, height); + fx_framebuffer_update(renderer, &renderer->effects_buffer, width, height); + fx_framebuffer_update(renderer, &renderer->effects_buffer_swapped, width, height); - // Add a stencil buffer to the main buffer & bind the main buffer - fx_framebuffer_bind(&renderer->wlr_buffer); + // Finally bind the main wlr FBO + fx_framebuffer_bind_wlr_fbo(renderer); pixman_region32_init(&renderer->blur_padding_region); @@ -449,6 +475,16 @@ void fx_renderer_scissor(struct wlr_box *box) { } } +void fx_renderer_get_texture_attribs(struct wlr_texture *texture, + struct wlr_gles2_texture_attribs *attribs) { + if (wlr_texture_is_gles2(texture)) { + wlr_gles2_texture_get_attribs(texture, attribs); + } else if (wlr_texture_is_fx(texture)) { + struct fx_texture *fx_texture = fx_get_texture(texture); + wlr_gles2_texture_get_fx_attribs(fx_texture, attribs); + } +} + void fx_renderer_stencil_mask_init() { glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); @@ -478,15 +514,22 @@ void fx_renderer_stencil_mask_fini() { glDisable(GL_STENCIL_TEST); } -bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct fx_texture *fx_texture, +bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *wlr_texture, const struct wlr_fbox *src_box, const struct wlr_box *dst_box, const float matrix[static 9], struct decoration_data deco_data) { + struct wlr_gles2_texture_attribs *texture_attrs = malloc(sizeof(struct wlr_gles2_texture_attribs)); + fx_renderer_get_texture_attribs(wlr_texture, texture_attrs); + if (!texture_attrs) { + sway_log(SWAY_ERROR, "Texture not GLES2 or FX. Aborting..."); + abort(); + } + struct tex_shader *shader = NULL; - switch (fx_texture->target) { + switch (texture_attrs->target) { case GL_TEXTURE_2D: - if (fx_texture->has_alpha) { + if (texture_attrs->has_alpha) { shader = &renderer->shaders.tex_rgba; } else { shader = &renderer->shaders.tex_rgbx; @@ -514,7 +557,7 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct fx_te wlr_matrix_transpose(gl_matrix, gl_matrix); // if there's no opacity or rounded corners we don't need to blend - if (!fx_texture->has_alpha && deco_data.alpha == 1.0 && !deco_data.corner_radius) { + if (!texture_attrs->has_alpha && deco_data.alpha == 1.0 && !deco_data.corner_radius) { glDisable(GL_BLEND); } else { glEnable(GL_BLEND); @@ -523,9 +566,9 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct fx_te glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glActiveTexture(GL_TEXTURE0); - glBindTexture(fx_texture->target, fx_texture->id); + glBindTexture(texture_attrs->target, texture_attrs->tex); - glTexParameteri(fx_texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(texture_attrs->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glUseProgram(shader->program); @@ -543,10 +586,10 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct fx_te glUniform1f(shader->saturation, deco_data.saturation); glUniform1f(shader->radius, deco_data.corner_radius); - const GLfloat x1 = src_box->x / fx_texture->width; - const GLfloat y1 = src_box->y / fx_texture->height; - const GLfloat x2 = (src_box->x + src_box->width) / fx_texture->width; - const GLfloat y2 = (src_box->y + src_box->height) / fx_texture->height; + const GLfloat x1 = src_box->x / wlr_texture->width; + const GLfloat y1 = src_box->y / wlr_texture->height; + const GLfloat x2 = (src_box->x + src_box->width) / wlr_texture->width; + const GLfloat y2 = (src_box->y + src_box->height) / wlr_texture->height; const GLfloat texcoord[] = { x2, y1, // top right x1, y1, // top left @@ -565,12 +608,12 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct fx_te glDisableVertexAttribArray(shader->pos_attrib); glDisableVertexAttribArray(shader->tex_attrib); - glBindTexture(fx_texture->target, 0); + glBindTexture(texture_attrs->target, 0); return true; } -bool fx_render_texture_with_matrix(struct fx_renderer *renderer, struct fx_texture *texture, +bool fx_render_texture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *texture, const struct wlr_box *dst_box, const float matrix[static 9], struct decoration_data deco_data) { struct wlr_fbox src_box = { @@ -771,8 +814,9 @@ void fx_render_stencil_mask(struct fx_renderer *renderer, const struct wlr_box * } // TODO: alpha input arg? -void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *box, - const float color[static 4], const float matrix[static 9], int corner_radius, +void fx_render_box_shadow(struct fx_renderer *renderer, + const struct wlr_box *box, const float color[static 4], + const float matrix[static 9], int corner_radius, float blur_sigma) { if (box->width == 0 || box->height == 0) { return; @@ -833,16 +877,16 @@ void fx_render_box_shadow(struct fx_renderer *renderer, const struct wlr_box *bo } void fx_render_blur(struct fx_renderer *renderer, const float matrix[static 9], - struct fx_framebuffer **buffer, struct blur_shader *shader, + struct wlr_gles2_texture_attribs *texture, struct blur_shader *shader, const struct wlr_box *box, int blur_radius) { glDisable(GL_BLEND); glDisable(GL_STENCIL_TEST); glActiveTexture(GL_TEXTURE0); - glBindTexture((*buffer)->texture.target, (*buffer)->texture.id); + glBindTexture(texture->target, texture->tex); - glTexParameteri((*buffer)->texture.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glUseProgram(shader->program); diff --git a/sway/desktop/fx_renderer/fx_stencilbuffer.c b/sway/desktop/fx_renderer/fx_stencilbuffer.c index 5b99ff79..86aa8f72 100644 --- a/sway/desktop/fx_renderer/fx_stencilbuffer.c +++ b/sway/desktop/fx_renderer/fx_stencilbuffer.c @@ -1,9 +1,10 @@ #include #include +#include "log.h" #include "sway/desktop/fx_renderer/fx_stencilbuffer.h" -struct fx_stencilbuffer fx_stencilbuffer_create() { +struct fx_stencilbuffer fx_stencilbuffer_create(void) { return (struct fx_stencilbuffer) { .rb = -1, .width = -1, @@ -11,6 +12,34 @@ struct fx_stencilbuffer fx_stencilbuffer_create() { }; } +void fx_stencilbuffer_init(struct fx_stencilbuffer *stencil_buffer, int width, int height) { + bool first_alloc = false; + + if (stencil_buffer->rb == (uint32_t) -1) { + glGenRenderbuffers(1, &stencil_buffer->rb); + first_alloc = true; + } + + if (first_alloc || stencil_buffer->width != width || stencil_buffer->height != height) { + glBindRenderbuffer(GL_RENDERBUFFER, stencil_buffer->rb); + glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height); + stencil_buffer->width = width; + stencil_buffer->height = height; + + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + if (status != GL_FRAMEBUFFER_COMPLETE) { + sway_log(SWAY_ERROR, + "Stencil buffer incomplete, couldn't create! (FB status: %i)", + status); + return; + } + } + + // Reattach the RenderBuffer to the FrameBuffer + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, + GL_RENDERBUFFER, stencil_buffer->rb); +} + void fx_stencilbuffer_release(struct fx_stencilbuffer *stencil_buffer) { if (stencil_buffer->rb != (uint32_t) -1 && stencil_buffer->rb) { glDeleteRenderbuffers(1, &stencil_buffer->rb); diff --git a/sway/desktop/fx_renderer/fx_texture.c b/sway/desktop/fx_renderer/fx_texture.c index cc5d14c8..ce468dad 100644 --- a/sway/desktop/fx_renderer/fx_texture.c +++ b/sway/desktop/fx_renderer/fx_texture.c @@ -1,37 +1,339 @@ #include +#include +#include #include +#include +#include -#include "sway/desktop/fx_renderer/fx_texture.h" +#include "log.h" +#include "render/egl.h" +#include "sway/desktop/fx_renderer/fx_renderer.h" +#include "sway/desktop/fx_renderer/pixel_format.h" -struct fx_texture fx_texture_create() { - return (struct fx_texture) { - .id = 0, - .target = 0, - .width = -1, - .height = -1, - }; +static const struct wlr_texture_impl texture_impl; + +bool wlr_texture_is_fx(struct wlr_texture *wlr_texture) { + return wlr_texture->impl == &texture_impl; } -struct fx_texture fx_texture_from_wlr_texture(struct wlr_texture *texture) { - assert(wlr_texture_is_gles2(texture)); - - struct wlr_gles2_texture_attribs texture_attrs; - wlr_gles2_texture_get_attribs(texture, &texture_attrs); - - return (struct fx_texture) { - .target = texture_attrs.target, - .id = texture_attrs.tex, - .has_alpha = texture_attrs.has_alpha, - .width = texture->width, - .height = texture->height, - }; +struct fx_texture *fx_get_texture(struct wlr_texture *wlr_texture) { + assert(wlr_texture_is_fx(wlr_texture)); + return (struct fx_texture *) wlr_texture; } -void fx_texture_release(struct fx_texture *texture) { - if (texture->id) { - glDeleteTextures(1, &texture->id); +static bool check_stride(const struct wlr_pixel_format_info *fmt, + uint32_t stride, uint32_t width) { + if (stride % (fmt->bpp / 8) != 0) { + sway_log(SWAY_ERROR, "Invalid stride %d (incompatible with %d " + "bytes-per-pixel)", stride, fmt->bpp / 8); + return false; } - texture->id = 0; - texture->width = -1; - texture->height = -1; + if (stride < width * (fmt->bpp / 8)) { + sway_log(SWAY_ERROR, "Invalid stride %d (too small for %d " + "bytes-per-pixel and width %d)", stride, fmt->bpp / 8, width); + return false; + } + return true; +} + +static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture, + struct wlr_buffer *buffer, pixman_region32_t *damage) { + struct fx_texture *texture = fx_get_texture(wlr_texture); + + if (texture->target != GL_TEXTURE_2D || texture->image != EGL_NO_IMAGE_KHR) { + return false; + } + + void *data; + uint32_t format; + size_t stride; + if (!wlr_buffer_begin_data_ptr_access(buffer, + WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) { + return false; + } + + if (format != texture->drm_format) { + wlr_buffer_end_data_ptr_access(buffer); + return false; + } + + const struct wlr_pixel_format_info *drm_fmt = + drm_get_pixel_format_info(texture->drm_format); + assert(drm_fmt); + + if (!check_stride(drm_fmt, stride, buffer->width)) { + wlr_buffer_end_data_ptr_access(buffer); + return false; + } + + struct wlr_egl_context prev_ctx; + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(texture->fx_renderer->egl); + + glBindTexture(GL_TEXTURE_2D, texture->tex); + + int rects_len = 0; + pixman_box32_t *rects = pixman_region32_rectangles(damage, &rects_len); + + for (int i = 0; i < rects_len; i++) { + pixman_box32_t rect = rects[i]; + + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / (drm_fmt->bpp / 8)); + glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, rect.x1); + glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, rect.y1); + + int width = rect.x2 - rect.x1; + int height = rect.y2 - rect.y1; + glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x1, rect.y1, width, height, + GL_RGBA, GL_UNSIGNED_BYTE, data); + } + + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0); + glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0); + glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0); + + glBindTexture(GL_TEXTURE_2D, 0); + + wlr_egl_restore_context(&prev_ctx); + + wlr_buffer_end_data_ptr_access(buffer); + + return true; +} + +static bool fx_texture_invalidate(struct fx_texture *texture) { + if (texture->image == EGL_NO_IMAGE_KHR) { + return false; + } + if (texture->target == GL_TEXTURE_EXTERNAL_OES) { + // External changes are immediately made visible by the GL implementation + return true; + } + + struct wlr_egl_context prev_ctx; + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(texture->fx_renderer->egl); + + glBindTexture(texture->target, texture->tex); + texture->fx_renderer->procs.glEGLImageTargetTexture2DOES(texture->target, + texture->image); + glBindTexture(texture->target, 0); + + wlr_egl_restore_context(&prev_ctx); + + return true; +} + +void fx_texture_destroy(struct fx_texture *texture) { + wl_list_remove(&texture->link); + if (texture->buffer != NULL) { + wlr_addon_finish(&texture->buffer_addon); + } + + struct wlr_egl_context prev_ctx; + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(texture->fx_renderer->egl); + + glDeleteTextures(1, &texture->tex); + wlr_egl_destroy_image(texture->fx_renderer->egl, texture->image); + + wlr_egl_restore_context(&prev_ctx); + + free(texture); +} + +static void fx_texture_unref(struct wlr_texture *wlr_texture) { + struct fx_texture *texture = fx_get_texture(wlr_texture); + if (texture->buffer != NULL) { + // Keep the texture around, in case the buffer is re-used later. We're + // still listening to the buffer's destroy event. + wlr_buffer_unlock(texture->buffer); + } else { + fx_texture_destroy(texture); + } +} + +static const struct wlr_texture_impl texture_impl = { + .update_from_buffer = fx_texture_update_from_buffer, + .destroy = fx_texture_unref, +}; + +static struct fx_texture *fx_texture_create( + struct fx_renderer *renderer, uint32_t width, uint32_t height) { + struct fx_texture *texture = calloc(1, sizeof(struct fx_texture)); + if (texture == NULL) { + sway_log_errno(SWAY_ERROR, "Allocation failed"); + return NULL; + } + wlr_texture_init(&texture->wlr_texture, &texture_impl, width, height); + texture->fx_renderer = renderer; + wl_list_insert(&renderer->textures, &texture->link); + return texture; +} + +static struct fx_texture *fx_texture_from_pixels( + struct fx_renderer *renderer, + uint32_t drm_format, uint32_t stride, uint32_t width, + uint32_t height, const void *data) { + const struct wlr_pixel_format_info *drm_fmt = + drm_get_pixel_format_info(drm_format); + assert(drm_fmt); + + if (!check_stride(drm_fmt, stride, width)) { + return NULL; + } + + struct fx_texture *texture = + fx_texture_create(renderer, width, height); + if (texture == NULL) { + return NULL; + } + texture->target = GL_TEXTURE_2D; + texture->has_alpha = false; + texture->drm_format = DRM_FORMAT_XBGR8888; + + struct wlr_egl_context prev_ctx; + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(renderer->egl); + + glGenTextures(1, &texture->tex); + glBindTexture(GL_TEXTURE_2D, texture->tex); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / (drm_fmt->bpp / 8)); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, + GL_RGBA, GL_UNSIGNED_BYTE, data); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0); + + glBindTexture(GL_TEXTURE_2D, 0); + + wlr_egl_restore_context(&prev_ctx); + + return texture; +} + +static struct wlr_texture *fx_texture_from_dmabuf( + struct fx_renderer *renderer, + struct wlr_dmabuf_attributes *attribs) { + + if (!renderer->procs.glEGLImageTargetTexture2DOES) { + return NULL; + } + + struct fx_texture *texture = + fx_texture_create(renderer, attribs->width, attribs->height); + if (texture == NULL) { + return NULL; + } + texture->drm_format = DRM_FORMAT_INVALID; // texture can't be written anyways + + const struct wlr_pixel_format_info *drm_fmt = + drm_get_pixel_format_info(attribs->format); + if (drm_fmt != NULL) { + texture->has_alpha = drm_fmt->has_alpha; + } else { + // We don't know, assume the texture has an alpha channel + texture->has_alpha = true; + } + + struct wlr_egl_context prev_ctx; + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(renderer->egl); + + bool external_only; + texture->image = + wlr_egl_create_image_from_dmabuf(renderer->egl, attribs, &external_only); + if (texture->image == EGL_NO_IMAGE_KHR) { + sway_log(SWAY_ERROR, "Failed to create EGL image from DMA-BUF"); + wlr_egl_restore_context(&prev_ctx); + wl_list_remove(&texture->link); + free(texture); + return NULL; + } + + texture->target = external_only ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D; + + glGenTextures(1, &texture->tex); + glBindTexture(texture->target, texture->tex); + glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + renderer->procs.glEGLImageTargetTexture2DOES(texture->target, texture->image); + glBindTexture(texture->target, 0); + + wlr_egl_restore_context(&prev_ctx); + + return &texture->wlr_texture; +} + +static void texture_handle_buffer_destroy(struct wlr_addon *addon) { + struct fx_texture *texture = + wl_container_of(addon, texture, buffer_addon); + fx_texture_destroy(texture); +} + +static const struct wlr_addon_interface texture_addon_impl = { + .name = "fx_texture", + .destroy = texture_handle_buffer_destroy, +}; + +static struct fx_texture *fx_texture_from_dmabuf_buffer( + struct fx_renderer *renderer, struct wlr_buffer *buffer, + struct wlr_dmabuf_attributes *dmabuf) { + struct wlr_addon *addon = + wlr_addon_find(&buffer->addons, renderer, &texture_addon_impl); + if (addon != NULL) { + struct fx_texture *texture = + wl_container_of(addon, texture, buffer_addon); + if (!fx_texture_invalidate(texture)) { + sway_log(SWAY_ERROR, "Failed to invalidate texture"); + return false; + } + wlr_buffer_lock(texture->buffer); + return texture; + } + + struct wlr_texture *wlr_texture = + fx_texture_from_dmabuf(renderer, dmabuf); + if (wlr_texture == NULL) { + return false; + } + + struct fx_texture *texture = fx_get_texture(wlr_texture); + texture->buffer = wlr_buffer_lock(buffer); + wlr_addon_init(&texture->buffer_addon, &buffer->addons, + renderer, &texture_addon_impl); + + return texture; +} + +struct fx_texture *fx_texture_from_buffer(struct fx_renderer *renderer, + struct wlr_buffer *buffer) { + void *data; + uint32_t format; + size_t stride; + struct wlr_dmabuf_attributes dmabuf; + if (wlr_buffer_get_dmabuf(buffer, &dmabuf)) { + return fx_texture_from_dmabuf_buffer(renderer, buffer, &dmabuf); + } else if (wlr_buffer_begin_data_ptr_access(buffer, + WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) { + struct fx_texture *tex = fx_texture_from_pixels(renderer, + format, stride, buffer->width, buffer->height, data); + wlr_buffer_end_data_ptr_access(buffer); + return tex; + } else { + return NULL; + } +} + +void wlr_gles2_texture_get_fx_attribs(struct fx_texture *texture, + struct wlr_gles2_texture_attribs *attribs) { + memset(attribs, 0, sizeof(*attribs)); + attribs->target = texture->target; + attribs->tex = texture->tex; + attribs->has_alpha = texture->has_alpha; } diff --git a/sway/desktop/fx_renderer/pixel_format.c b/sway/desktop/fx_renderer/pixel_format.c new file mode 100644 index 00000000..da30f157 --- /dev/null +++ b/sway/desktop/fx_renderer/pixel_format.c @@ -0,0 +1,178 @@ +#include "sway/desktop/fx_renderer/pixel_format.h" +#include + +static const struct wlr_pixel_format_info pixel_format_info[] = { + { + .drm_format = DRM_FORMAT_XRGB8888, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 32, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_ARGB8888, + .opaque_substitute = DRM_FORMAT_XRGB8888, + .bpp = 32, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_XBGR8888, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 32, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_ABGR8888, + .opaque_substitute = DRM_FORMAT_XBGR8888, + .bpp = 32, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_RGBX8888, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 32, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_RGBA8888, + .opaque_substitute = DRM_FORMAT_RGBX8888, + .bpp = 32, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_BGRX8888, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 32, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_BGRA8888, + .opaque_substitute = DRM_FORMAT_BGRX8888, + .bpp = 32, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_BGR888, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 24, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_RGBX4444, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 16, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_RGBA4444, + .opaque_substitute = DRM_FORMAT_RGBX4444, + .bpp = 16, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_RGBX5551, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 16, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_RGBA5551, + .opaque_substitute = DRM_FORMAT_RGBX5551, + .bpp = 16, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_RGB565, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 16, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_BGR565, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 16, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_XRGB2101010, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 32, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_ARGB2101010, + .opaque_substitute = DRM_FORMAT_XRGB2101010, + .bpp = 32, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_XBGR2101010, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 32, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_ABGR2101010, + .opaque_substitute = DRM_FORMAT_XBGR2101010, + .bpp = 32, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_XBGR16161616F, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 64, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_ABGR16161616F, + .opaque_substitute = DRM_FORMAT_XBGR16161616F, + .bpp = 64, + .has_alpha = true, + }, + { + .drm_format = DRM_FORMAT_XBGR16161616, + .opaque_substitute = DRM_FORMAT_INVALID, + .bpp = 64, + .has_alpha = false, + }, + { + .drm_format = DRM_FORMAT_ABGR16161616, + .opaque_substitute = DRM_FORMAT_XBGR16161616, + .bpp = 64, + .has_alpha = true, + }, +}; + +static const size_t pixel_format_info_size = + sizeof(pixel_format_info) / sizeof(pixel_format_info[0]); + +const struct wlr_pixel_format_info *drm_get_pixel_format_info(uint32_t fmt) { + for (size_t i = 0; i < pixel_format_info_size; ++i) { + if (pixel_format_info[i].drm_format == fmt) { + return &pixel_format_info[i]; + } + } + + return NULL; +} + +uint32_t convert_wl_shm_format_to_drm(enum wl_shm_format fmt) { + switch (fmt) { + case WL_SHM_FORMAT_XRGB8888: + return DRM_FORMAT_XRGB8888; + case WL_SHM_FORMAT_ARGB8888: + return DRM_FORMAT_ARGB8888; + default: + return (uint32_t)fmt; + } +} + +enum wl_shm_format convert_drm_format_to_wl_shm(uint32_t fmt) { + switch (fmt) { + case DRM_FORMAT_XRGB8888: + return WL_SHM_FORMAT_XRGB8888; + case DRM_FORMAT_ARGB8888: + return WL_SHM_FORMAT_ARGB8888; + default: + return (enum wl_shm_format)fmt; + } +} diff --git a/sway/desktop/render.c b/sway/desktop/render.c index 43abc81c..297bef62 100644 --- a/sway/desktop/render.c +++ b/sway/desktop/render.c @@ -19,7 +19,6 @@ #include "config.h" #include "log.h" #include "sway/config.h" -#include "sway/desktop/fx_renderer/fx_framebuffer.h" #include "sway/desktop/fx_renderer/fx_renderer.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" @@ -111,20 +110,29 @@ static void scissor_output(struct wlr_output *wlr_output, } static void set_scale_filter(struct wlr_output *wlr_output, - struct fx_texture *texture, enum scale_filter_mode scale_filter) { - glBindTexture(texture->target, texture->id); + struct wlr_texture *texture, enum scale_filter_mode scale_filter) { + struct wlr_gles2_texture_attribs *attribs = malloc(sizeof(struct wlr_gles2_texture_attribs)); + + fx_renderer_get_texture_attribs(texture, attribs); + if (!attribs) { + goto finish; + } + + glBindTexture(attribs->target, attribs->tex); switch (scale_filter) { case SCALE_FILTER_LINEAR: - glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(attribs->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); break; case SCALE_FILTER_NEAREST: - glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(attribs->target, GL_TEXTURE_MAG_FILTER, GL_NEAREST); break; case SCALE_FILTER_DEFAULT: case SCALE_FILTER_SMART: assert(false); // unreachable } +finish: + free(attribs); } pixman_region32_t create_damage(const struct wlr_box damage_box, pixman_region32_t *output_damage) { @@ -144,7 +152,7 @@ struct wlr_box get_monitor_box(struct wlr_output *output) { } static void render_texture(struct wlr_output *wlr_output, - pixman_region32_t *output_damage, struct fx_texture *texture, + pixman_region32_t *output_damage, struct wlr_texture *texture, const struct wlr_fbox *src_box, const struct wlr_box *dst_box, const float matrix[static 9], struct decoration_data deco_data) { struct sway_output *output = wlr_output->data; @@ -191,6 +199,15 @@ void render_blur_segments(struct fx_renderer *renderer, fx_framebuffer_bind(&renderer->effects_buffer); } + struct wlr_gles2_texture_attribs attribs; + if (*buffer) { + struct fx_texture *texture = fx_texture_from_buffer(renderer, + (*buffer)->wlr_buffer); + wlr_gles2_texture_get_fx_attribs(texture, &attribs); + } else { + attribs = renderer->wlr_main_texture_attribs; + } + if (pixman_region32_not_empty(damage)) { int nrects; pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects); @@ -198,7 +215,7 @@ void render_blur_segments(struct fx_renderer *renderer, const pixman_box32_t box = rects[i]; struct wlr_box new_box = { box.x1, box.y1, box.x2 - box.x1, box.y2 - box.y1 }; fx_renderer_scissor(&new_box); - fx_render_blur(renderer, matrix, buffer, shader, &new_box, blur_radius); + fx_render_blur(renderer, matrix, &attribs, shader, &new_box, blur_radius); } } @@ -229,16 +246,15 @@ struct fx_framebuffer *get_main_buffer_blur(struct fx_renderer *renderer, struct wlr_region_expand(&damage, &damage, config_get_blur_size()); - // Initially blur main_buffer content into the effects_buffers - struct fx_framebuffer *current_buffer = &renderer->wlr_buffer; - // Bind to blur framebuffer fx_framebuffer_bind(&renderer->effects_buffer); - glBindTexture(renderer->wlr_buffer.texture.target, renderer->wlr_buffer.texture.id); + glBindTexture(renderer->wlr_main_texture_attribs.target, + renderer->wlr_main_texture_attribs.tex); // damage region will be scaled, make a temp pixman_region32_t tempDamage; pixman_region32_init(&tempDamage); + struct fx_framebuffer *current_buffer = NULL; int blur_radius = config->blur_params.radius; int blur_passes = config->blur_params.num_passes; @@ -262,13 +278,13 @@ struct fx_framebuffer *get_main_buffer_blur(struct fx_renderer *renderer, struct pixman_region32_fini(&damage); // Bind back to the default buffer - fx_framebuffer_bind(&renderer->wlr_buffer); + fx_framebuffer_bind_wlr_fbo(renderer); return current_buffer; } struct blur_stencil_data { - struct fx_texture *stencil_texture; + struct wlr_texture *stencil_texture; const struct wlr_fbox *stencil_src_box; float *stencil_matrix; }; @@ -299,13 +315,14 @@ void render_blur(bool optimized, struct sway_output *output, } struct fx_framebuffer *buffer = &renderer->blur_buffer; - if (!buffer->texture.id || !optimized) { + if (!buffer->initialized || !optimized) { pixman_region32_translate(&translucent_region, dst_box->x, dst_box->y); pixman_region32_intersect(&translucent_region, &translucent_region, &damage); // Render the blur into its own buffer buffer = get_main_buffer_blur(renderer, output, &translucent_region, dst_box); } + struct fx_texture *blur_texture = fx_texture_from_buffer(renderer, buffer->wlr_buffer); // Get a stencil of the window ignoring transparent regions if (deco_data->discard_transparent) { @@ -327,7 +344,7 @@ void render_blur(bool optimized, struct sway_output *output, struct decoration_data blur_deco_data = get_undecorated_decoration_data(); blur_deco_data.corner_radius = deco_data->corner_radius; blur_deco_data.has_titlebar = deco_data->has_titlebar; - render_texture(wlr_output, &damage, &buffer->texture, NULL, dst_box, matrix, blur_deco_data); + render_texture(wlr_output, &damage, &blur_texture->wlr_texture, NULL, dst_box, matrix, blur_deco_data); // Finish stenciling if (deco_data->discard_transparent) { @@ -430,7 +447,6 @@ static void render_surface_iterator(struct sway_output *output, struct wlr_fbox src_box; wlr_surface_get_buffer_source_box(surface, &src_box); - struct fx_texture fx_texture = fx_texture_from_wlr_texture(texture); // render blur bool is_subsurface = view ? view->surface != surface : false; @@ -451,7 +467,7 @@ static void render_surface_iterator(struct sway_output *output, struct wlr_box monitor_box = get_monitor_box(wlr_output); wlr_box_transform(&monitor_box, &monitor_box, wlr_output_transform_invert(wlr_output->transform), monitor_box.width, monitor_box.height); - struct blur_stencil_data stencil_data = { &fx_texture, &src_box, matrix }; + struct blur_stencil_data stencil_data = { texture, &src_box, matrix }; bool should_optimize_blur = view ? !container_is_floating(view->container) || config->blur_xray : false; render_blur(should_optimize_blur, output, output_damage, &dst_box, &opaque_region, &deco_data, &stencil_data); @@ -463,7 +479,7 @@ static void render_surface_iterator(struct sway_output *output, deco_data.discard_transparent = false; // Render surface texture - render_texture(wlr_output, output_damage, &fx_texture, &src_box, &dst_box, + render_texture(wlr_output, output_damage, texture, &src_box, &dst_box, matrix, deco_data); wlr_presentation_surface_sampled_on_output(server.presentation, surface, @@ -537,7 +553,7 @@ static void render_drag_icons(struct sway_output *output, } void render_whole_output(struct fx_renderer *renderer, struct wlr_output *wlr_output, - pixman_region32_t *output_damage, struct fx_texture *texture) { + pixman_region32_t *output_damage, struct wlr_texture *texture) { struct wlr_box monitor_box = get_monitor_box(wlr_output); enum wl_output_transform transform = wlr_output_transform_invert(wlr_output->transform); float matrix[9]; @@ -556,9 +572,10 @@ void render_output_blur(struct sway_output *output, pixman_region32_t *damage) { // Render the blur struct fx_framebuffer *buffer = get_main_buffer_blur(renderer, output, &fake_damage, &monitor_box); + struct fx_texture *fx_texture = fx_texture_from_buffer(renderer, buffer->wlr_buffer); // Render the newly blurred content into the blur_buffer - fx_framebuffer_update(&renderer->blur_buffer, + fx_framebuffer_update(renderer, &renderer->blur_buffer, output->renderer->viewport_width, output->renderer->viewport_height); fx_framebuffer_bind(&renderer->blur_buffer); @@ -570,8 +587,8 @@ void render_output_blur(struct sway_output *output, pixman_region32_t *damage) { scissor_output(wlr_output, &rects[i]); fx_renderer_clear(clear_color); } - render_whole_output(renderer, wlr_output, &fake_damage, &buffer->texture); - fx_framebuffer_bind(&renderer->wlr_buffer); + render_whole_output(renderer, wlr_output, &fake_damage, &fx_texture->wlr_texture); + fx_framebuffer_bind_wlr_fbo(renderer); pixman_region32_fini(&fake_damage); @@ -816,8 +833,6 @@ static void render_saved_view(struct sway_view *view, struct sway_output *output deco_data.corner_radius *= wlr_output->scale; - struct fx_texture fx_texture = fx_texture_from_wlr_texture(saved_buf->buffer->texture); - // render blur if (deco_data.blur && config_should_parameters_blur()) { struct wlr_gles2_texture_attribs attribs; @@ -831,7 +846,7 @@ static void render_saved_view(struct sway_view *view, struct sway_output *output struct wlr_box monitor_box = get_monitor_box(wlr_output); wlr_box_transform(&monitor_box, &monitor_box, wlr_output_transform_invert(wlr_output->transform), monitor_box.width, monitor_box.height); - struct blur_stencil_data stencil_data = { &fx_texture, &saved_buf->source_box, matrix }; + struct blur_stencil_data stencil_data = { saved_buf->buffer->texture, &saved_buf->source_box, matrix }; bool should_optimize_blur = !container_is_floating(view->container) || config->blur_xray; render_blur(should_optimize_blur, output, damage, &dst_box, &opaque_region, &deco_data, &stencil_data); @@ -843,7 +858,7 @@ static void render_saved_view(struct sway_view *view, struct sway_output *output deco_data.discard_transparent = false; // Render saved surface texture - render_texture(wlr_output, damage, &fx_texture, + render_texture(wlr_output, damage, saved_buf->buffer->texture, &saved_buf->source_box, &dst_box, matrix, deco_data); } @@ -1154,8 +1169,7 @@ static void render_titlebar(struct sway_output *output, if (ob_inner_width < texture_box.width) { texture_box.width = ob_inner_width; } - struct fx_texture fx_texture = fx_texture_from_wlr_texture(marks_texture); - render_texture(output->wlr_output, output_damage, &fx_texture, + render_texture(output->wlr_output, output_damage, marks_texture, NULL, &texture_box, matrix, deco_data); // Padding above @@ -1231,8 +1245,7 @@ static void render_titlebar(struct sway_output *output, texture_box.width = ob_inner_width - ob_marks_width; } - struct fx_texture fx_texture = fx_texture_from_wlr_texture(title_texture); - render_texture(output->wlr_output, output_damage, &fx_texture, + render_texture(output->wlr_output, output_damage, title_texture, NULL, &texture_box, matrix, deco_data); // Padding above @@ -1942,9 +1955,12 @@ void output_render(struct sway_output *output, struct timespec *when, // Capture the padding pixels before blur for later use fx_framebuffer_bind(&renderer->blur_saved_pixels_buffer); // TODO: Investigate blitting instead + struct wlr_texture *back_texture + = wlr_texture_from_buffer(wlr_output->renderer, wlr_output->back_buffer); render_whole_output(renderer, wlr_output, - &renderer->blur_padding_region, &renderer->wlr_buffer.texture); - fx_framebuffer_bind(&renderer->wlr_buffer); + &renderer->blur_padding_region, back_texture); + wlr_texture_destroy(back_texture); + fx_framebuffer_bind_wlr_fbo(renderer); } } pixman_region32_fini(&blur_region); @@ -2016,9 +2032,11 @@ renderer_end: // Not needed if we damaged the whole viewport if (!renderer->blur_buffer_dirty) { // Render the saved pixels over the blur artifacts + struct fx_texture *saved_texture = + fx_texture_from_buffer(renderer, renderer->blur_saved_pixels_buffer.wlr_buffer); // TODO: Investigate blitting instead render_whole_output(renderer, wlr_output, &renderer->blur_padding_region, - &renderer->blur_saved_pixels_buffer.texture); + &saved_texture->wlr_texture); } fx_renderer_end(output->renderer); diff --git a/sway/meson.build b/sway/meson.build index 1bc0c19c..50646f2b 100644 --- a/sway/meson.build +++ b/sway/meson.build @@ -20,6 +20,7 @@ sway_sources = files( 'desktop/fx_renderer/fx_stencilbuffer.c', 'desktop/fx_renderer/fx_texture.c', 'desktop/fx_renderer/matrix.c', + 'desktop/fx_renderer/pixel_format.c', 'desktop/idle_inhibit_v1.c', 'desktop/layer_shell.c', 'desktop/output.c',