From b6a990da71b5b0947650a50dcf1a083acfce868c Mon Sep 17 00:00:00 2001 From: Erik Reider <35975961+ErikReider@users.noreply.github.com> Date: Sat, 30 Dec 2023 12:45:04 +0100 Subject: Added fx_texture and fx_framebuffer --- render/fx_renderer/fx_framebuffer.c | 136 +++++++++++++++ render/fx_renderer/fx_renderer.c | 92 +++++++--- render/fx_renderer/fx_texture.c | 335 ++++++++++++++++++++++++++++++++++++ render/fx_renderer/meson.build | 2 + 4 files changed, 544 insertions(+), 21 deletions(-) create mode 100644 render/fx_renderer/fx_framebuffer.c create mode 100644 render/fx_renderer/fx_texture.c (limited to 'render/fx_renderer') diff --git a/render/fx_renderer/fx_framebuffer.c b/render/fx_renderer/fx_framebuffer.c new file mode 100644 index 0000000..4de6439 --- /dev/null +++ b/render/fx_renderer/fx_framebuffer.c @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "render/egl.h" +#include "render/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) { + .initialized = false, + .fbo = -1, + .rbo = -1, + .wlr_buffer = NULL, + .image = NULL, + }; +} + +void fx_framebuffer_bind(struct fx_framebuffer *fx_buffer) { + glBindFramebuffer(GL_FRAMEBUFFER, fx_buffer->fbo); +} + +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 (!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 (fx_buffer->fbo == (uint32_t) -1 || first_alloc) { + glGenFramebuffers(1, &fx_buffer->fbo); + first_alloc = true; + } + + 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; + } + } + + 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_release(struct fx_framebuffer *fx_buffer) { + // Release the framebuffer + wl_list_remove(&fx_buffer->link); + wlr_addon_finish(&fx_buffer->addon); + + struct wlr_egl_context prev_ctx; + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(fx_buffer->renderer->egl); + + glDeleteFramebuffers(1, &fx_buffer->fbo); + fx_buffer->fbo = -1; + glDeleteRenderbuffers(1, &fx_buffer->rbo); + fx_buffer->rbo = -1; + + wlr_egl_destroy_image(fx_buffer->renderer->egl, fx_buffer->image); + + wlr_egl_restore_context(&prev_ctx); + + fx_buffer->initialized = false; +} diff --git a/render/fx_renderer/fx_renderer.c b/render/fx_renderer/fx_renderer.c index 730e314..7bc0be1 100644 --- a/render/fx_renderer/fx_renderer.c +++ b/render/fx_renderer/fx_renderer.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -178,7 +179,7 @@ static bool check_gl_ext(const char *exts, const char *ext) { static void load_gl_proc(void *proc_ptr, const char *name) { void *proc = (void *)eglGetProcAddress(name); if (proc == NULL) { - wlr_log(WLR_ERROR, "GLES2 RENDERER: eglGetProcAddress(%s) failed", name); + wlr_log(WLR_ERROR, "FX RENDERER: eglGetProcAddress(%s) failed", name); abort(); } *(void **)proc_ptr = proc; @@ -187,7 +188,19 @@ static void load_gl_proc(void *proc_ptr, const char *name) { static void fx_renderer_handle_destroy(struct wlr_addon *addon) { struct fx_renderer *renderer = wl_container_of(addon, renderer, addon); - fx_renderer_fini(renderer); + + 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); } static const struct wlr_addon_interface fx_renderer_addon_impl = { @@ -195,9 +208,9 @@ static const struct wlr_addon_interface fx_renderer_addon_impl = { .destroy = fx_renderer_handle_destroy, }; -void fx_renderer_init_addon(struct wlr_egl *egl, struct wlr_addon_set *addons, - const void * owner) { - struct fx_renderer *renderer = fx_renderer_create(egl); +void fx_renderer_init_addon(struct wlr_egl *egl, struct wlr_output *output, + struct wlr_addon_set *addons, const void * owner) { + struct fx_renderer *renderer = fx_renderer_create(egl, output); if (!renderer) { wlr_log(WLR_ERROR, "Failed to create fx_renderer"); abort(); @@ -216,32 +229,43 @@ struct fx_renderer *fx_renderer_addon_find(struct wlr_addon_set *addons, return renderer; } -struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) { +struct fx_renderer *fx_renderer_create(struct wlr_egl *egl, + struct wlr_output *output) { struct fx_renderer *renderer = calloc(1, sizeof(struct fx_renderer)); if (renderer == NULL) { return NULL; } + wl_list_init(&renderer->buffers); + wl_list_init(&renderer->textures); + + renderer->wlr_output = output; + renderer->egl = egl; + if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE, wlr_egl_get_context(egl))) { - wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not make EGL current"); + wlr_log(WLR_ERROR, "FX RENDERER: Could not make EGL current"); return NULL; } + // Create the stencil buffer renderer->stencil_buffer = fx_stencilbuffer_create(); + // Create the FBOs + renderer->wlr_main_buffer_fbo = -1; + // get extensions const char *exts_str = (const char *)glGetString(GL_EXTENSIONS); if (exts_str == NULL) { - wlr_log(WLR_ERROR, "GLES2 RENDERER: Failed to get GL_EXTENSIONS"); + wlr_log(WLR_ERROR, "FX RENDERER: Failed to get GL_EXTENSIONS"); return NULL; } - wlr_log(WLR_INFO, "Creating scenefx GLES2 renderer"); + wlr_log(WLR_INFO, "Creating scenefx FX renderer"); wlr_log(WLR_INFO, "Using %s", glGetString(GL_VERSION)); wlr_log(WLR_INFO, "GL vendor: %s", glGetString(GL_VENDOR)); wlr_log(WLR_INFO, "GL renderer: %s", glGetString(GL_RENDERER)); - wlr_log(WLR_INFO, "Supported GLES2 extensions: %s", exts_str); + wlr_log(WLR_INFO, "Supported FX extensions: %s", exts_str); // TODO: the rest of the gl checks if (check_gl_ext(exts_str, "GL_OES_EGL_image_external")) { @@ -250,6 +274,12 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) { "glEGLImageTargetTexture2DOES"); } + if (check_gl_ext(exts_str, "GL_OES_EGL_image")) { + renderer->exts.OES_egl_image = true; + load_gl_proc(&renderer->procs.glEGLImageTargetRenderbufferStorageOES, + "glEGLImageTargetRenderbufferStorageOES"); + } + // quad fragment shader if (!link_quad_program(&renderer->shaders.quad)) { goto error; @@ -276,11 +306,11 @@ struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) { if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { - wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not unset current EGL"); + wlr_log(WLR_ERROR, "FX RENDERER: Could not unset current EGL"); goto error; } - wlr_log(WLR_INFO, "GLES2 RENDERER: Shaders Initialized Successfully"); + wlr_log(WLR_INFO, "FX RENDERER: Shaders Initialized Successfully"); return renderer; error: @@ -293,24 +323,34 @@ error: if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { - wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not unset current EGL"); + wlr_log(WLR_ERROR, "FX RENDERER: Could not unset current EGL"); } // TODO: more freeing? free(renderer); - wlr_log(WLR_ERROR, "GLES2 RENDERER: Error Initializing Shaders"); + wlr_log(WLR_ERROR, "FX RENDERER: Error Initializing Shaders"); return NULL; } -void fx_renderer_fini(struct fx_renderer *renderer) { - fx_stencilbuffer_release(&renderer->stencil_buffer); -} - void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) { + glViewport(0, 0, width, height); + renderer->viewport_width = width; + renderer->viewport_height = height; + + // Store the wlr FBO + 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); + 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_stencilbuffer_init(&renderer->stencil_buffer, width, height); - glViewport(0, 0, width, height); + // Finally bind the main wlr FBO + fx_framebuffer_bind_wlr_fbo(renderer); // refresh projection matrix matrix_projection(renderer->projection, width, height, @@ -319,6 +359,9 @@ void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) { glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); } +void fx_renderer_end(struct fx_renderer *renderer) { +} + void fx_renderer_clear(const float color[static 4]) { glClearColor(color[0], color[1], color[2], color[3]); glClearStencil(0); @@ -376,9 +419,16 @@ bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, const struct wlr_box *dst_box, const float matrix[static 9], float opacity, int corner_radius) { - assert(wlr_texture_is_gles2(wlr_texture)); struct wlr_gles2_texture_attribs texture_attrs; - wlr_gles2_texture_get_attribs(wlr_texture, &texture_attrs); + if (wlr_texture_is_gles2(wlr_texture)) { + wlr_gles2_texture_get_attribs(wlr_texture, &texture_attrs); + } else if (wlr_texture_is_fx(wlr_texture)) { + struct fx_texture *fx_texture = fx_get_texture(wlr_texture); + wlr_gles2_texture_get_fx_attribs(fx_texture, &texture_attrs); + } else { + wlr_log(WLR_ERROR, "Texture not GLES2 or FX. Aborting..."); + abort(); + } struct tex_shader *shader = NULL; diff --git a/render/fx_renderer/fx_texture.c b/render/fx_renderer/fx_texture.c new file mode 100644 index 0000000..a8977fa --- /dev/null +++ b/render/fx_renderer/fx_texture.c @@ -0,0 +1,335 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "render/fx_renderer/fx_renderer.h" +#include "render/pixel_format.h" +#include "render/egl.h" + +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_get_texture(struct wlr_texture *wlr_texture) { + assert(wlr_texture_is_fx(wlr_texture)); + return (struct fx_texture *) wlr_texture; +} + +static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture, + struct wlr_buffer *buffer, const 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 (pixel_format_info_pixels_per_block(drm_fmt) != 1) { + wlr_buffer_end_data_ptr_access(buffer); + wlr_log(WLR_ERROR, "Cannot update texture: block formats are not supported"); + return false; + } + + if (!pixel_format_info_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->bytes_per_block); + 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) { + wlr_log_errno(WLR_ERROR, "Allocation failed"); + return NULL; + } + wlr_texture_init(&texture->wlr_texture, renderer->wlr_output->renderer, + &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 (pixel_format_info_pixels_per_block(drm_fmt) != 1) { + wlr_log(WLR_ERROR, "Cannot upload texture: block formats are not supported"); + return NULL; + } + + if (!pixel_format_info_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->bytes_per_block); + 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) { + wlr_log(WLR_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)) { + wlr_log(WLR_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/render/fx_renderer/meson.build b/render/fx_renderer/meson.build index 394caa3..f6d44ae 100644 --- a/render/fx_renderer/meson.build +++ b/render/fx_renderer/meson.build @@ -7,7 +7,9 @@ endif wlr_files += files( 'matrix.c', + 'fx_framebuffer.c', 'fx_stencilbuffer.c', + 'fx_texture.c', 'fx_renderer.c', ) -- cgit v1.2.3