Skip to contentSkip to conversation

ui/egl-helpers: Fix FBO recreation and prevent texture accidental deletion

PATCH In review [email protected] 2 participants 3 messages Mar 3 – Mar 25

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/egl-helpers: Fix FBO recreation and prevent texture accidental deletion

From: Dongwon Kim <[email protected]>

When egl_fb_setup_for_tex is called, we must handle cases where the texture ID is reused across different GL contexts.

Texture Preservation - If the new texture ID matches the cached ID, we must skip egl_fb_delete_texture to avoid destroying the texture we are about to use.

FBO Recreation - Because FBOs are context-local and not shared, a cached FBO ID from a previous context is invalid. We must generate a new FBO handle if we are re-validating the same texture ID in a potentially new context.

This prevents stale FBO usage and unintended texture deletion during context transitions.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 ui/egl-helpers.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
ui/egl-helpers.c +12 -4
index e3f2872cc1..7ac64f3ba8 100644
@@ -111,15 +111,23 @@ void egl_fb_setup_default(egl_fb *fb, int width, int height, int x, int y)
 void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
                           GLuint texture, bool delete)
 {
-    egl_fb_delete_texture(fb);
+    if (fb->texture != texture) {
+        egl_fb_delete_texture(fb);
+    }
+
+    /*
+     * If fb->texture == texture, the existing fb->framebuffer is tied to
+     * a previous GL context. Since FBOs are not shared across contexts,
+     * we must create a new FBO for the current context.
+     */
+    if (!fb->framebuffer || (fb->texture == texture)) {
+        glGenFramebuffers(1, &fb->framebuffer);
+    }
 
     fb->width = width;
     fb->height = height;
     fb->texture = texture;
     fb->delete_texture = delete;
-    if (!fb->framebuffer) {
-        glGenFramebuffers(1, &fb->framebuffer);
-    }
 
     glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
Signature · 1 line
2.43.0
#1 lore raw

Hi

Quoted 31 lines · On Tue, Mar 3, 2026 at 5:14 AM <[email protected]> wrote:

From: Dongwon Kim <[email protected]>

When egl_fb_setup_for_tex is called, we must handle cases where the texture ID is reused across different GL contexts.

Texture Preservation - If the new texture ID matches the cached ID, we must skip egl_fb_delete_texture to avoid destroying the texture we are about to use.

FBO Recreation - Because FBOs are context-local and not shared, a cached FBO ID from a previous context is invalid. We must generate a new FBO handle if we are re-validating the same texture ID in a potentially new context.

This prevents stale FBO usage and unintended texture deletion during context transitions.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 ui/egl-helpers.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
ui/egl-helpers.c +0 -0
index e3f2872cc1..7ac64f3ba8 100644

ui/egl-helpers.c void egl_fb_setup_default(egl_fb *fb, int width, int height, int x, int y) · line 116

Quoted 7 lines
ui/egl-helpers.c +3 -1
@@ -111,15 +111,23 @@ void egl_fb_setup_default(egl_fb *fb, int width, int height, int x, int y)
 void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
                           GLuint texture, bool delete)
 {
-    egl_fb_delete_texture(fb);
+    if (fb->texture != texture) {
+        egl_fb_delete_texture(fb);
+    }

That change makes sense, and prevents reusing a destroyed texture.

ui/egl-helpers.c void egl_fb_setup_default(egl_fb *fb, int width, int height, int x, int y) · line 125

Quoted 9 lines
ui/egl-helpers.c +9 -0
+
+    /*
+     * If fb->texture == texture, the existing fb->framebuffer is tied to
+     * a previous GL context. Since FBOs are not shared across contexts,
+     * we must create a new FBO for the current context.
+     */
+    if (!fb->framebuffer || (fb->texture == texture)) {
+        glGenFramebuffers(1, &fb->framebuffer);
+    }

This, I don't understand. Assuming the framebuffer is used in a different context, shouldn't it generate a new framebuffer regardless of the texture?

Also, aren't these leaking framebuffers?

Can you identify cases where egl_fb_setup_for_tex() is reused in a different context? shouldn't we prevent this instead?

thanks

Quoted 15 lines
ui/egl-helpers.c +0 -4
     fb->width = width;
     fb->height = height;
     fb->texture = texture;
     fb->delete_texture = delete;
-    if (!fb->framebuffer) {
-        glGenFramebuffers(1, &fb->framebuffer);
-    }
 
     glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
--

2.43.0

Signature · 1 line
Marc-André Lureau
#2 lore raw

Hi Marc-André,

Quoted 62 lines

-----Original Message-----
From: Marc-André Lureau <[email protected]>
Sent: Tuesday, March 17, 2026 5:51 AM
To: Kim, Dongwon <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] ui/egl-helpers: Fix FBO recreation and prevent texture accidental deletion

Hi

Quoted 31 lines · On Tue, Mar 3, 2026 at 5:14 AM <[email protected]> wrote:

From: Dongwon Kim <[email protected]>

When egl_fb_setup_for_tex is called, we must handle cases where the texture ID is reused across different GL contexts.

Texture Preservation - If the new texture ID matches the cached ID, we must skip egl_fb_delete_texture to avoid destroying the texture we are about to use.

FBO Recreation - Because FBOs are context-local and not shared, a cached FBO ID from a previous context is invalid. We must generate a new FBO handle if we are re-validating the same texture ID in a potentially new context.

This prevents stale FBO usage and unintended texture deletion during context transitions.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
---
 ui/egl-helpers.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
ui/egl-helpers.c index renamed from ui/egl-helpers.c +0 -0

e3f2872cc1..7ac64f3ba8 100644

ui/egl-helpers.c +0 -0
@@ -111,15 +111,23 @@ void egl_fb_setup_default(egl_fb *fb, int width,
Quoted 1 line

int height, int x, int y) void egl_fb_setup_for_tex(egl_fb *fb, int width, int

height,

ui/egl-helpers.c void egl_fb_setup_default(egl_fb *fb, int width, int height, int x, int y) · line 116

Quoted 5 lines
                           GLuint texture, bool delete)  {
ui/egl-helpers.c +3 -1
-    egl_fb_delete_texture(fb);
+    if (fb->texture != texture) {
+        egl_fb_delete_texture(fb);
+    }

That change makes sense, and prevents reusing a destroyed texture.

ui/egl-helpers.c void egl_fb_setup_default(egl_fb *fb, int width, int height, int x, int y) · line 125

Quoted 9 lines
ui/egl-helpers.c +9 -0
+
+    /*
+     * If fb->texture == texture, the existing fb->framebuffer is tied to
+     * a previous GL context. Since FBOs are not shared across contexts,
+     * we must create a new FBO for the current context.
+     */
+    if (!fb->framebuffer || (fb->texture == texture)) {
+        glGenFramebuffers(1, &fb->framebuffer);
+    }
Quoted 2 lines

This, I don't understand. Assuming the framebuffer is used in a different context, shouldn't it generate a new framebuffer regardless of the texture?

So this condition "fb->texture == texture" is used to determine whether the existing framebuffer and texture (fb->framebuffer and fb->texture) are destroyed context's. But I just realized this can't be always true as there will be cases where textures are different but context switching has happened. I initially came with this idea to avoid adding too much for tracking context (i.e fb->context) but I guess this is what we should do. I will take a look.

Quoted 2 lines

Also, aren't these leaking framebuffers?

Old context is destroyed at this point already so the assumption is that all resources are unreferenced.

Quoted 3 lines

Can you identify cases where egl_fb_setup_for_tex() is reused in a different context? shouldn't we prevent this instead?

This happens when the user close the untabified window (gd_tab_window_close), which is an asynchronous event.

Quoted 23 lines

thanks

Quoted 11 lines
     fb->width = width;
     fb->height = height;
     fb->texture = texture;
     fb->delete_texture = delete;
ui/egl-helpers.c +0 -3
-    if (!fb->framebuffer) {
-        glGenFramebuffers(1, &fb->framebuffer);
-    }
 
     glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
     glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
Quoted 5 lines

GL_COLOR_ATTACHMENT0_EXT,
--
2.43.0

--
Marc-André Lureau