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
master
Alexander Orzechowski 1 year ago
parent 665055a1a0
commit cb5f67431b

@ -108,6 +108,7 @@ struct wlr_gles2_buffer {
EGLImageKHR image; EGLImageKHR image;
GLuint rbo; GLuint rbo;
GLuint fbo; GLuint fbo;
GLuint tex;
struct wlr_addon addon; struct wlr_addon addon;
}; };
@ -121,6 +122,7 @@ struct wlr_gles2_texture {
// GL_TEXTURE_2D == mutable // GL_TEXTURE_2D == mutable
// GL_TEXTURE_EXTERNAL_OES == immutable // GL_TEXTURE_EXTERNAL_OES == immutable
GLenum target; GLenum target;
bool owns_tex;
GLuint tex; GLuint tex;
EGLImageKHR image; EGLImageKHR image;
@ -130,7 +132,7 @@ struct wlr_gles2_texture {
// Only affects target == GL_TEXTURE_2D // Only affects target == GL_TEXTURE_2D
uint32_t drm_format; // used to interpret upload data uint32_t drm_format; // used to interpret upload data
// If imported from a wlr_buffer // If imported from a wlr_buffer
struct wlr_buffer *buffer; struct wlr_gles2_buffer *buffer;
struct wlr_addon buffer_addon; 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_render_timer *timer);
struct wlr_gles2_texture *gles2_get_texture( struct wlr_gles2_texture *gles2_get_texture(
struct wlr_texture *wlr_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_texture *gles2_texture_from_buffer(struct wlr_renderer *wlr_renderer,
struct wlr_buffer *buffer); struct wlr_buffer *buffer);

@ -72,6 +72,7 @@ static void destroy_buffer(struct wlr_gles2_buffer *buffer) {
glDeleteFramebuffers(1, &buffer->fbo); glDeleteFramebuffers(1, &buffer->fbo);
glDeleteRenderbuffers(1, &buffer->rbo); glDeleteRenderbuffers(1, &buffer->rbo);
glDeleteTextures(1, &buffer->tex);
pop_gles2_debug(buffer->renderer); pop_gles2_debug(buffer->renderer);
@ -131,7 +132,7 @@ GLuint gles2_buffer_get_fbo(struct wlr_gles2_buffer *buffer) {
return buffer->fbo; 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_buffer *wlr_buffer) {
struct wlr_addon *addon = struct wlr_addon *addon =
wlr_addon_find(&wlr_buffer->addons, renderer, &buffer_addon_impl); 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; 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) { if (buffer == NULL) {
return false; 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); 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) { if (!buffer) {
return NULL; return NULL;
} }

@ -139,6 +139,7 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) {
wlr_addon_finish(&texture->buffer_addon); wlr_addon_finish(&texture->buffer_addon);
} }
if (texture->owns_tex) {
struct wlr_egl_context prev_ctx; struct wlr_egl_context prev_ctx;
wlr_egl_save_context(&prev_ctx); wlr_egl_save_context(&prev_ctx);
wlr_egl_make_current(texture->renderer->egl); wlr_egl_make_current(texture->renderer->egl);
@ -146,11 +147,11 @@ void gles2_texture_destroy(struct wlr_gles2_texture *texture) {
push_gles2_debug(texture->renderer); push_gles2_debug(texture->renderer);
glDeleteTextures(1, &texture->tex); glDeleteTextures(1, &texture->tex);
wlr_egl_destroy_image(texture->renderer->egl, texture->image);
pop_gles2_debug(texture->renderer); pop_gles2_debug(texture->renderer);
wlr_egl_restore_context(&prev_ctx); wlr_egl_restore_context(&prev_ctx);
}
free(texture); free(texture);
} }
@ -160,7 +161,7 @@ static void gles2_texture_unref(struct wlr_texture *wlr_texture) {
if (texture->buffer != NULL) { if (texture->buffer != NULL) {
// Keep the texture around, in case the buffer is re-used later. We're // Keep the texture around, in case the buffer is re-used later. We're
// still listening to the buffer's destroy event. // still listening to the buffer's destroy event.
wlr_buffer_unlock(texture->buffer); wlr_buffer_unlock(texture->buffer->buffer);
} else { } else {
gles2_texture_destroy(texture); gles2_texture_destroy(texture);
} }
@ -218,6 +219,7 @@ static struct wlr_texture *gles2_texture_from_pixels(
texture->target = GL_TEXTURE_2D; texture->target = GL_TEXTURE_2D;
texture->has_alpha = drm_fmt->has_alpha; texture->has_alpha = drm_fmt->has_alpha;
texture->drm_format = fmt->drm_format; texture->drm_format = fmt->drm_format;
texture->owns_tex = true;
GLint internal_format = fmt->gl_internalformat; GLint internal_format = fmt->gl_internalformat;
if (!internal_format) { if (!internal_format) {
@ -249,102 +251,84 @@ static struct wlr_texture *gles2_texture_from_pixels(
return &texture->wlr_texture; 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( 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_dmabuf_attributes *attribs) {
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
if (!renderer->procs.glEGLImageTargetTexture2DOES) { if (!renderer->procs.glEGLImageTargetTexture2DOES) {
return NULL; 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 = struct wlr_gles2_texture *texture =
gles2_texture_create(renderer, attribs->width, attribs->height); gles2_texture_create(renderer, attribs->width, attribs->height);
if (texture == NULL) { if (texture == NULL) {
return NULL; return NULL;
} }
texture->drm_format = DRM_FORMAT_INVALID; // texture can't be written anyways
const struct wlr_pixel_format_info *drm_fmt = const struct wlr_pixel_format_info *drm_fmt =
drm_get_pixel_format_info(attribs->format); drm_get_pixel_format_info(attribs->format);
if (drm_fmt != NULL) {
texture->has_alpha = drm_fmt->has_alpha; texture->buffer = buffer;
} else { texture->drm_format = DRM_FORMAT_INVALID; // texture can't be written anyways
// We don't know, assume the texture has an alpha channel texture->has_alpha = drm_fmt ? drm_fmt->has_alpha : true;
texture->has_alpha = true; texture->image = buffer->image;
}
struct wlr_egl_context prev_ctx; struct wlr_egl_context prev_ctx;
wlr_egl_save_context(&prev_ctx); wlr_egl_save_context(&prev_ctx);
wlr_egl_make_current(renderer->egl); wlr_egl_make_current(renderer->egl);
bool external_only; texture->target = buffer->external_only ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
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;
if (buffer->tex) {
texture->tex = buffer->tex;
gles2_texture_invalidate(texture);
} else {
push_gles2_debug(renderer); push_gles2_debug(renderer);
glGenTextures(1, &texture->tex); glGenTextures(1, &texture->tex);
glBindTexture(texture->target, texture->tex); glBindTexture(texture->target, texture->tex);
glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 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_WRAP_T, GL_CLAMP_TO_EDGE);
renderer->procs.glEGLImageTargetTexture2DOES(texture->target, texture->image); renderer->procs.glEGLImageTargetTexture2DOES(texture->target, buffer->image);
glBindTexture(texture->target, 0); glBindTexture(texture->target, 0);
pop_gles2_debug(renderer); pop_gles2_debug(renderer);
buffer->tex = texture->tex;
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);
}
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_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;
} }
struct wlr_texture *wlr_texture = wlr_egl_restore_context(&prev_ctx);
gles2_texture_from_dmabuf(&renderer->wlr_renderer, dmabuf);
if (wlr_texture == NULL) {
return false;
}
struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); wlr_buffer_lock(texture->buffer->buffer);
texture->buffer = wlr_buffer_lock(buffer); wlr_addon_init(&texture->buffer_addon, &wlr_buffer->addons,
wlr_addon_init(&texture->buffer_addon, &buffer->addons,
renderer, &texture_addon_impl); renderer, &texture_addon_impl);
return &texture->wlr_texture; return &texture->wlr_texture;
} }
@ -357,7 +341,7 @@ struct wlr_texture *gles2_texture_from_buffer(struct wlr_renderer *wlr_renderer,
size_t stride; size_t stride;
struct wlr_dmabuf_attributes dmabuf; struct wlr_dmabuf_attributes dmabuf;
if (wlr_buffer_get_dmabuf(buffer, &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, } else if (wlr_buffer_begin_data_ptr_access(buffer,
WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) { WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
struct wlr_texture *tex = gles2_texture_from_pixels(wlr_renderer, struct wlr_texture *tex = gles2_texture_from_pixels(wlr_renderer,

Loading…
Cancel
Save