diff options
author | Alexander Orzechowski <alex@ozal.ski> | 2023-06-25 05:36:11 -0400 |
---|---|---|
committer | Alexander Orzechowski <alex@ozal.ski> | 2023-11-30 10:13:18 -0500 |
commit | cb5f67431b782ab20a9ea8dd68471740e5d20f7b (patch) | |
tree | 9a1ddc48dc41742741d8518de6fe20da7a051c74 | |
parent | 665055a1a04610c1464567d1d2eebe7fb7cfe633 (diff) |
render/gles2: Don't double import dmabuf
We can double import a dmabuf if we use it as a texture target and
a render target. Instead, let's unify render targets and texture dmabuf
imports to use wlr_gles2_buffer which manages the EGLImageKHR
-rw-r--r-- | include/render/gles2.h | 6 | ||||
-rw-r--r-- | render/gles2/renderer.c | 7 | ||||
-rw-r--r-- | render/gles2/texture.c | 144 |
3 files changed, 73 insertions, 84 deletions
diff --git a/include/render/gles2.h b/include/render/gles2.h index c8ee3142..c868fe05 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -108,6 +108,7 @@ struct wlr_gles2_buffer { EGLImageKHR image; GLuint rbo; GLuint fbo; + GLuint tex; struct wlr_addon addon; }; @@ -121,6 +122,7 @@ struct wlr_gles2_texture { // GL_TEXTURE_2D == mutable // GL_TEXTURE_EXTERNAL_OES == immutable GLenum target; + bool owns_tex; GLuint tex; EGLImageKHR image; @@ -130,7 +132,7 @@ struct wlr_gles2_texture { // 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_gles2_buffer *buffer; struct wlr_addon buffer_addon; }; @@ -157,6 +159,8 @@ struct wlr_gles2_render_timer *gles2_get_render_timer( struct wlr_render_timer *timer); struct wlr_gles2_texture *gles2_get_texture( struct wlr_texture *wlr_texture); +struct wlr_gles2_buffer *gles2_buffer_get_or_create(struct wlr_gles2_renderer *renderer, + struct wlr_buffer *wlr_buffer); struct wlr_texture *gles2_texture_from_buffer(struct wlr_renderer *wlr_renderer, struct wlr_buffer *buffer); diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index d5a57184..327d197e 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -72,6 +72,7 @@ static void destroy_buffer(struct wlr_gles2_buffer *buffer) { glDeleteFramebuffers(1, &buffer->fbo); glDeleteRenderbuffers(1, &buffer->rbo); + glDeleteTextures(1, &buffer->tex); pop_gles2_debug(buffer->renderer); @@ -131,7 +132,7 @@ GLuint gles2_buffer_get_fbo(struct wlr_gles2_buffer *buffer) { return buffer->fbo; } -static struct wlr_gles2_buffer *get_or_create_buffer(struct wlr_gles2_renderer *renderer, +struct wlr_gles2_buffer *gles2_buffer_get_or_create(struct wlr_gles2_renderer *renderer, struct wlr_buffer *wlr_buffer) { struct wlr_addon *addon = wlr_addon_find(&wlr_buffer->addons, renderer, &buffer_addon_impl); @@ -202,7 +203,7 @@ static bool gles2_bind_buffer(struct wlr_renderer *wlr_renderer, return false; } - struct wlr_gles2_buffer *buffer = get_or_create_buffer(renderer, wlr_buffer); + struct wlr_gles2_buffer *buffer = gles2_buffer_get_or_create(renderer, wlr_buffer); if (buffer == NULL) { return false; } @@ -442,7 +443,7 @@ static struct wlr_render_pass *gles2_begin_buffer_pass(struct wlr_renderer *wlr_ clock_gettime(CLOCK_MONOTONIC, &timer->cpu_start); } - struct wlr_gles2_buffer *buffer = get_or_create_buffer(renderer, wlr_buffer); + struct wlr_gles2_buffer *buffer = gles2_buffer_get_or_create(renderer, wlr_buffer); if (!buffer) { return NULL; } diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 5cf0219c..a4522129 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -139,18 +139,19 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) { wlr_addon_finish(&texture->buffer_addon); } - struct wlr_egl_context prev_ctx; - wlr_egl_save_context(&prev_ctx); - wlr_egl_make_current(texture->renderer->egl); + if (texture->owns_tex) { + struct wlr_egl_context prev_ctx; + wlr_egl_save_context(&prev_ctx); + wlr_egl_make_current(texture->renderer->egl); - push_gles2_debug(texture->renderer); + push_gles2_debug(texture->renderer); - glDeleteTextures(1, &texture->tex); - wlr_egl_destroy_image(texture->renderer->egl, texture->image); + glDeleteTextures(1, &texture->tex); - pop_gles2_debug(texture->renderer); + pop_gles2_debug(texture->renderer); - wlr_egl_restore_context(&prev_ctx); + wlr_egl_restore_context(&prev_ctx); + } free(texture); } @@ -160,7 +161,7 @@ static void gles2_texture_unref(struct wlr_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); + wlr_buffer_unlock(texture->buffer->buffer); } else { gles2_texture_destroy(texture); } @@ -218,6 +219,7 @@ static struct wlr_texture *gles2_texture_from_pixels( texture->target = GL_TEXTURE_2D; texture->has_alpha = drm_fmt->has_alpha; texture->drm_format = fmt->drm_format; + texture->owns_tex = true; GLint internal_format = fmt->gl_internalformat; if (!internal_format) { @@ -249,102 +251,84 @@ static struct wlr_texture *gles2_texture_from_pixels( return &texture->wlr_texture; } +static void texture_handle_buffer_destroy(struct wlr_addon *addon) { + struct wlr_gles2_texture *texture = + wl_container_of(addon, texture, buffer_addon); + gles2_texture_destroy(texture); +} + +static const struct wlr_addon_interface texture_addon_impl = { + .name = "wlr_gles2_texture", + .destroy = texture_handle_buffer_destroy, +}; + static struct wlr_texture *gles2_texture_from_dmabuf( - struct wlr_renderer *wlr_renderer, + struct wlr_gles2_renderer *renderer, struct wlr_buffer *wlr_buffer, struct wlr_dmabuf_attributes *attribs) { - struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); - if (!renderer->procs.glEGLImageTargetTexture2DOES) { return NULL; } + struct wlr_addon *addon = + wlr_addon_find(&wlr_buffer->addons, renderer, &texture_addon_impl); + if (addon != NULL) { + struct wlr_gles2_texture *texture = + wl_container_of(addon, texture, buffer_addon); + if (!gles2_texture_invalidate(texture)) { + wlr_log(WLR_ERROR, "Failed to invalidate texture"); + return false; + } + wlr_buffer_lock(texture->buffer->buffer); + return &texture->wlr_texture; + } + + struct wlr_gles2_buffer *buffer = gles2_buffer_get_or_create(renderer, wlr_buffer); + if (!buffer) { + return NULL; + } + struct wlr_gles2_texture *texture = gles2_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; - } + + texture->buffer = buffer; + texture->drm_format = DRM_FORMAT_INVALID; // texture can't be written anyways + texture->has_alpha = drm_fmt ? drm_fmt->has_alpha : true; + texture->image = buffer->image; 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; - - push_gles2_debug(renderer); + texture->target = buffer->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); - renderer->procs.glEGLImageTargetTexture2DOES(texture->target, texture->image); - glBindTexture(texture->target, 0); - - pop_gles2_debug(renderer); - - wlr_egl_restore_context(&prev_ctx); - - return &texture->wlr_texture; -} - -static void texture_handle_buffer_destroy(struct wlr_addon *addon) { - struct wlr_gles2_texture *texture = - wl_container_of(addon, texture, buffer_addon); - gles2_texture_destroy(texture); -} + if (buffer->tex) { + texture->tex = buffer->tex; + gles2_texture_invalidate(texture); + } else { + push_gles2_debug(renderer); -static const struct wlr_addon_interface texture_addon_impl = { - .name = "wlr_gles2_texture", - .destroy = texture_handle_buffer_destroy, -}; + 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); + renderer->procs.glEGLImageTargetTexture2DOES(texture->target, buffer->image); + glBindTexture(texture->target, 0); -static struct wlr_texture *gles2_texture_from_dmabuf_buffer( - struct wlr_gles2_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 wlr_gles2_texture *texture = - wl_container_of(addon, texture, buffer_addon); - if (!gles2_texture_invalidate(texture)) { - wlr_log(WLR_ERROR, "Failed to invalidate texture"); - return false; - } - wlr_buffer_lock(texture->buffer); - return &texture->wlr_texture; + pop_gles2_debug(renderer); + buffer->tex = texture->tex; } - struct wlr_texture *wlr_texture = - gles2_texture_from_dmabuf(&renderer->wlr_renderer, dmabuf); - if (wlr_texture == NULL) { - return false; - } + wlr_egl_restore_context(&prev_ctx); - struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); - texture->buffer = wlr_buffer_lock(buffer); - wlr_addon_init(&texture->buffer_addon, &buffer->addons, + wlr_buffer_lock(texture->buffer->buffer); + wlr_addon_init(&texture->buffer_addon, &wlr_buffer->addons, renderer, &texture_addon_impl); - return &texture->wlr_texture; } @@ -357,7 +341,7 @@ struct wlr_texture *gles2_texture_from_buffer(struct wlr_renderer *wlr_renderer, size_t stride; struct wlr_dmabuf_attributes dmabuf; if (wlr_buffer_get_dmabuf(buffer, &dmabuf)) { - return gles2_texture_from_dmabuf_buffer(renderer, buffer, &dmabuf); + return gles2_texture_from_dmabuf(renderer, buffer, &dmabuf); } else if (wlr_buffer_begin_data_ptr_access(buffer, WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) { struct wlr_texture *tex = gles2_texture_from_pixels(wlr_renderer, |