render/egl: introduce wlr_egl_unset_current

This function can be called after wlr_egl_make_current to cleanup the
EGL context. This avoids having lingering EGL contexts that make things
work by chance.

Closes: https://github.com/swaywm/wlroots/issues/2197
This commit is contained in:
Simon Ser 2020-05-19 11:54:59 +02:00 committed by Drew DeVault
parent 781ed1ff02
commit 1edc42157b
8 changed files with 62 additions and 29 deletions

View file

@ -387,6 +387,15 @@ bool wlr_egl_make_current(struct wlr_egl *egl, EGLSurface surface,
return true;
}
bool wlr_egl_unset_current(struct wlr_egl *egl) {
if (!eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT)) {
wlr_log(WLR_ERROR, "eglMakeCurrent failed");
return false;
}
return true;
}
bool wlr_egl_is_current(struct wlr_egl *egl) {
return eglGetCurrentContext() == egl->context;
}
@ -690,7 +699,7 @@ bool wlr_egl_destroy_surface(struct wlr_egl *egl, EGLSurface surface) {
// Reset the current EGL surface in case it's the one we're destroying,
// otherwise the next wlr_egl_make_current call will result in a
// use-after-free.
wlr_egl_make_current(egl, NULL, NULL);
wlr_egl_make_current(egl, EGL_NO_SURFACE, NULL);
}
return eglDestroySurface(egl->display, surface);
}

View file

@ -401,6 +401,8 @@ static void gles2_destroy(struct wlr_renderer *wlr_renderer) {
gles2_procs.glDebugMessageCallbackKHR(NULL, NULL);
}
wlr_egl_unset_current(renderer->egl);
free(renderer);
}
@ -670,6 +672,8 @@ struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) {
POP_GLES2_DEBUG;
wlr_egl_unset_current(renderer->egl);
return &renderer->wlr_renderer;
error:
@ -686,6 +690,8 @@ error:
gles2_procs.glDebugMessageCallbackKHR(NULL, NULL);
}
wlr_egl_unset_current(renderer->egl);
free(renderer);
return NULL;
}

View file

@ -29,9 +29,7 @@ struct wlr_gles2_texture *gles2_get_texture(
static struct wlr_gles2_texture *get_gles2_texture_in_context(
struct wlr_texture *wlr_texture) {
struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture);
if (!wlr_egl_is_current(texture->egl)) {
wlr_egl_make_current(texture->egl, EGL_NO_SURFACE, NULL);
}
wlr_egl_make_current(texture->egl, EGL_NO_SURFACE, NULL);
return texture;
}
@ -49,6 +47,7 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture,
if (texture->target != GL_TEXTURE_2D) {
wlr_log(WLR_ERROR, "Cannot write pixels to immutable texture");
wlr_egl_unset_current(texture->egl);
return false;
}
@ -75,6 +74,8 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture,
glBindTexture(GL_TEXTURE_2D, 0);
POP_GLES2_DEBUG;
wlr_egl_unset_current(texture->egl);
return true;
}
@ -111,11 +112,8 @@ static void gles2_texture_destroy(struct wlr_texture *wlr_texture) {
return;
}
struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture);
if (!wlr_egl_is_current(texture->egl)) {
wlr_egl_make_current(texture->egl, EGL_NO_SURFACE, NULL);
}
struct wlr_gles2_texture *texture =
get_gles2_texture_in_context(wlr_texture);
PUSH_GLES2_DEBUG;
@ -124,6 +122,8 @@ static void gles2_texture_destroy(struct wlr_texture *wlr_texture) {
POP_GLES2_DEBUG;
wlr_egl_unset_current(texture->egl);
free(texture);
}
@ -137,9 +137,7 @@ static const struct wlr_texture_impl texture_impl = {
struct wlr_texture *wlr_gles2_texture_from_pixels(struct wlr_egl *egl,
enum wl_shm_format wl_fmt, uint32_t stride, uint32_t width,
uint32_t height, const void *data) {
if (!wlr_egl_is_current(egl)) {
wlr_egl_make_current(egl, EGL_NO_SURFACE, NULL);
}
wlr_egl_make_current(egl, EGL_NO_SURFACE, NULL);
const struct wlr_gles2_pixel_format *fmt = get_gles2_format_from_wl(wl_fmt);
if (fmt == NULL) {
@ -172,14 +170,14 @@ struct wlr_texture *wlr_gles2_texture_from_pixels(struct wlr_egl *egl,
glBindTexture(GL_TEXTURE_2D, 0);
POP_GLES2_DEBUG;
wlr_egl_unset_current(egl);
return &texture->wlr_texture;
}
struct wlr_texture *wlr_gles2_texture_from_wl_drm(struct wlr_egl *egl,
struct wl_resource *resource) {
if (!wlr_egl_is_current(egl)) {
wlr_egl_make_current(egl, EGL_NO_SURFACE, NULL);
}
wlr_egl_make_current(egl, EGL_NO_SURFACE, NULL);
if (!gles2_procs.glEGLImageTargetTexture2DOES) {
return NULL;
@ -235,14 +233,14 @@ struct wlr_texture *wlr_gles2_texture_from_wl_drm(struct wlr_egl *egl,
glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
POP_GLES2_DEBUG;
wlr_egl_unset_current(egl);
return &texture->wlr_texture;
}
struct wlr_texture *wlr_gles2_texture_from_dmabuf(struct wlr_egl *egl,
struct wlr_dmabuf_attributes *attribs) {
if (!wlr_egl_is_current(egl)) {
wlr_egl_make_current(egl, EGL_NO_SURFACE, NULL);
}
wlr_egl_make_current(egl, EGL_NO_SURFACE, NULL);
if (!gles2_procs.glEGLImageTargetTexture2DOES) {
return NULL;
@ -297,6 +295,8 @@ struct wlr_texture *wlr_gles2_texture_from_dmabuf(struct wlr_egl *egl,
glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
POP_GLES2_DEBUG;
wlr_egl_unset_current(egl);
return &texture->wlr_texture;
}