Skip to contentSkip to conversation

ui/gtk: Implement dpy_gl_ctx_destroy_texture for gtk-egl and gtk-gl-area

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

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/gtk: Implement dpy_gl_ctx_destroy_texture for gtk-egl and gtk-gl-area

From: Dongwon Kim <[email protected]>

Currently, the texture associated with a DisplaySurface is not deleted when the surface is switched or freed, leading to a memory leak in the GPU process. This occurs because the GTK backend lacks a mapping for the 'dpy_gl_ctx_destroy_texture' operation.

This patch implements the necessary cleanup functions for both EGL and GL Area backends. Each implementation ensures the appropriate GL context is made current before calling surface_gl_destroy_texture(), ensuring that the underlying GL texture ID is safely released by the driver.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 include/ui/gtk.h |  4 ++++
 ui/gtk-egl.c     | 10 ++++++++++
 ui/gtk-gl-area.c |  8 ++++++++
 ui/gtk.c         |  2 ++
 4 files changed, 24 insertions(+)
include/ui/gtk.h +4 -0
index 3e6ce3cb48..f1765ad5cb 100644
@@ -170,6 +170,8 @@ void gd_egl_switch(DisplayChangeListener *dcl,
                    DisplaySurface *surface);
 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
                                     QEMUGLParams *params);
+void gd_egl_destroy_texture(DisplayGLCtx *dgc,
+                            DisplaySurface *surface);
 void gd_egl_scanout_disable(DisplayChangeListener *dcl);
 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_id,
@@ -206,6 +208,8 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
                                         QEMUGLParams *params);
 void gd_gl_area_destroy_context(DisplayGLCtx *dgc,
                                 QEMUGLContext ctx);
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc,
+                                DisplaySurface *surface);
 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
                                QemuDmaBuf *dmabuf);
 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
ui/gtk-egl.c +10 -0
index ae9239999c..c1b32bc41b 100644
@@ -225,6 +225,16 @@ QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
     return qemu_egl_create_context(dgc, params);
 }
 
+void gd_egl_destroy_texture(DisplayGLCtx *dgc, DisplaySurface *surface)
+{
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+
+    surface_gl_destroy_texture(dgc->gls, surface);
+}
+
 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
ui/gtk-gl-area.c +8 -0
index cd86022d26..84e41f4221 100644
@@ -299,6 +299,14 @@ void gd_gl_area_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
     g_clear_object(&ctx);
 }
 
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc, DisplaySurface *surface)
+{
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    surface_gl_destroy_texture(dgc->gls, surface);
+}
+
 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_id,
                                 bool backing_y_0_top,
ui/gtk.c +2 -0
index 9ebe7e8df0..217f425075 100644
@@ -636,6 +636,7 @@ static const DisplayGLCtxOps gl_area_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_gl_area_create_context,
     .dpy_gl_ctx_destroy      = gd_gl_area_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_gl_area_destroy_texture,
     .dpy_gl_ctx_make_current = gd_gl_area_make_current,
 };
 
@@ -670,6 +671,7 @@ static const DisplayGLCtxOps egl_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_egl_create_context,
     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_egl_destroy_texture,
     .dpy_gl_ctx_make_current = gd_egl_make_current,
 };
 #endif
Signature · 1 line
2.43.0
#1 lore raw

Hi

Quoted 99 lines · On Tue, Mar 3, 2026 at 4:13 AM <[email protected]> wrote:

From: Dongwon Kim <[email protected]>

Currently, the texture associated with a DisplaySurface is not deleted when the surface is switched or freed, leading to a memory leak in the GPU process. This occurs because the GTK backend lacks a mapping for the 'dpy_gl_ctx_destroy_texture' operation.

This patch implements the necessary cleanup functions for both EGL and GL Area backends. Each implementation ensures the appropriate GL context is made current before calling surface_gl_destroy_texture(), ensuring that the underlying GL texture ID is safely released by the driver.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 include/ui/gtk.h |  4 ++++
 ui/gtk-egl.c     | 10 ++++++++++
 ui/gtk-gl-area.c |  8 ++++++++
 ui/gtk.c         |  2 ++
 4 files changed, 24 insertions(+)
include/ui/gtk.h +4 -0
index 3e6ce3cb48..f1765ad5cb 100644
@@ -170,6 +170,8 @@ void gd_egl_switch(DisplayChangeListener *dcl,
                    DisplaySurface *surface);
 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
                                     QEMUGLParams *params);
+void gd_egl_destroy_texture(DisplayGLCtx *dgc,
+                            DisplaySurface *surface);
 void gd_egl_scanout_disable(DisplayChangeListener *dcl);
 void gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_id,
@@ -206,6 +208,8 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
                                         QEMUGLParams *params);
 void gd_gl_area_destroy_context(DisplayGLCtx *dgc,
                                 QEMUGLContext ctx);
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc,
+                                DisplaySurface *surface);
 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
                                QemuDmaBuf *dmabuf);
 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
ui/gtk-egl.c +10 -0
index ae9239999c..c1b32bc41b 100644
@@ -225,6 +225,16 @@ QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
     return qemu_egl_create_context(dgc, params);
 }
 
+void gd_egl_destroy_texture(DisplayGLCtx *dgc, DisplaySurface *surface)
+{
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+
+    surface_gl_destroy_texture(dgc->gls, surface);
+}
+
 void gd_egl_scanout_disable(DisplayChangeListener *dcl)
 {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
ui/gtk-gl-area.c +8 -0
index cd86022d26..84e41f4221 100644
@@ -299,6 +299,14 @@ void gd_gl_area_destroy_context(DisplayGLCtx *dgc, QEMUGLContext ctx)
     g_clear_object(&ctx);
 }
 
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc, DisplaySurface *surface)
+{
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    surface_gl_destroy_texture(dgc->gls, surface);
+}
+
 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_id,
                                 bool backing_y_0_top,
ui/gtk.c +1 -0
index 9ebe7e8df0..217f425075 100644
@@ -636,6 +636,7 @@ static const DisplayGLCtxOps gl_area_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_gl_area_create_context,
     .dpy_gl_ctx_destroy      = gd_gl_area_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_gl_area_destroy_texture,
     .dpy_gl_ctx_make_current = gd_gl_area_make_current,
 };
 

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 675

Quoted 5 lines
ui/gtk.c +1 -0
@@ -670,6 +671,7 @@ static const DisplayGLCtxOps egl_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_egl_create_context,
     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_egl_destroy_texture,
     .dpy_gl_ctx_make_current = gd_egl_make_current,

But there is no dpy_gl_ctx_create_texture(), how can dpy_gl_ctx_destroy_texture() get called?

Quoted 6 lines
ui/gtk.c +0 -1
 };
 #endif
--

2.43.0

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

Hi Marc-André,

Quoted 99 lines

Subject: Re: [PATCH] ui/gtk: Implement dpy_gl_ctx_destroy_texture for gtk- egl and gtk-gl-area

Hi

ui/gtk-egl.c QEMUGLContext

Quoted 50 lines · On Tue, Mar 3, 2026 at 4:13 AM <[email protected]> wrote:

From: Dongwon Kim <[email protected]>

Currently, the texture associated with a DisplaySurface is not deleted when the surface is switched or freed, leading to a memory leak in the GPU process. This occurs because the GTK backend lacks a mapping for the 'dpy_gl_ctx_destroy_texture' operation.

This patch implements the necessary cleanup functions for both EGL and GL Area backends. Each implementation ensures the appropriate GL context is made current before calling surface_gl_destroy_texture(), ensuring that the underlying GL texture ID is safely released by the driver.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 include/ui/gtk.h |  4 ++++
 ui/gtk-egl.c     | 10 ++++++++++
 ui/gtk-gl-area.c |  8 ++++++++
 ui/gtk.c         |  2 ++
 4 files changed, 24 insertions(+)
include/ui/gtk.h index renamed from include/ui/gtk.h +0 -0

3e6ce3cb48..f1765ad5cb 100644

include/ui/gtk.h +2 -0
@@ -170,6 +170,8 @@ void gd_egl_switch(DisplayChangeListener *dcl,
                    DisplaySurface *surface);  QEMUGLContext
 gd_egl_create_context(DisplayGLCtx *dgc,
                                     QEMUGLParams *params);
+void gd_egl_destroy_texture(DisplayGLCtx *dgc,
+                            DisplaySurface *surface);
 void gd_egl_scanout_disable(DisplayChangeListener *dcl);  void
 gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_id, @@ -206,6 +208,8 @@

QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
QEMUGLParams *params); void
gd_gl_area_destroy_context(DisplayGLCtx *dgc,

                                 QEMUGLContext ctx);
include/ui/gtk.h +2 -0
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc,
+                                DisplaySurface *surface);
 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
                                QemuDmaBuf *dmabuf);  void

gd_gl_area_scanout_texture(DisplayChangeListener *dcl, diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c index ae9239999c..c1b32bc41b 100644

ui/gtk-egl.c +0 -0
@@ -225,6 +225,16 @@ QEMUGLContext

gd_egl_create_context(DisplayGLCtx *dgc,

ui/gtk-gl-area.c void gd_gl_area_destroy_context(DisplayGLCtx

Quoted 18 lines
ui/gtk-egl.c +9 -0
     return qemu_egl_create_context(dgc, params);  }
 
+void gd_egl_destroy_texture(DisplayGLCtx *dgc, DisplaySurface
+*surface) {
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+
+    surface_gl_destroy_texture(dgc->gls, surface); }
+
 void gd_egl_scanout_disable(DisplayChangeListener *dcl)  {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
ui/gtk-gl-area.c index renamed from ui/gtk-gl-area.c +0 -0

cd86022d26..84e41f4221 100644

ui/gtk-gl-area.c +0 -0
@@ -299,6 +299,14 @@ void gd_gl_area_destroy_context(DisplayGLCtx

*dgc, QEMUGLContext ctx)

Quoted 23 lines
ui/gtk-gl-area.c +7 -0
     g_clear_object(&ctx);
 }
 
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc, DisplaySurface
+*surface) {
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    surface_gl_destroy_texture(dgc->gls, surface); }
+
 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_id,
                                 bool backing_y_0_top, diff --git

a/ui/gtk.c b/ui/gtk.c index 9ebe7e8df0..217f425075 100644

ui/gtk.c +1 -0
@@ -636,6 +636,7 @@ static const DisplayGLCtxOps gl_area_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_gl_area_create_context,
     .dpy_gl_ctx_destroy      = gd_gl_area_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_gl_area_destroy_texture,
     .dpy_gl_ctx_make_current = gd_gl_area_make_current,  };
 
Quoted 9 lines

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 675

Quoted 6 lines
ui/gtk.c +1 -0
@@ -670,6 +671,7 @@ static const DisplayGLCtxOps egl_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_egl_create_context,
     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_egl_destroy_texture,
     .dpy_gl_ctx_make_current = gd_egl_make_current,

But there is no dpy_gl_ctx_create_texture(), how can dpy_gl_ctx_destroy_texture() get called?

Textures are also created via surface_gl_create_texture in the places like,

gtk-egl.c
static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout) {
.....

       egl_fb_destroy(&vc->gfx.guest_fb);
        if (vc->gfx.surface) {
            surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
            surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
        }

surface_gl_create_texture always come after surface_gl_destroy_texture so there is no leak while the guest is running but texture would remain unremoved if we do none in dpy_gl_ctx_destroy_texture() when the guest is rebooting.

Quoted 11 lines
Quoted 7 lines
ui/gtk.c +0 -1
 };
 #endif
--

2.43.0

--
Marc-André Lureau

Thanks

#3 lore raw

Hi

Quoted 112 lines · Dongwon

Hi Marc-André,

Quoted 99 lines

Subject: Re: [PATCH] ui/gtk: Implement dpy_gl_ctx_destroy_texture for gtk- egl and gtk-gl-area

Hi

ui/gtk-egl.c QEMUGLContext

Quoted 50 lines · On Tue, Mar 3, 2026 at 4:13 AM <[email protected]> wrote:

From: Dongwon Kim <[email protected]>

Currently, the texture associated with a DisplaySurface is not deleted when the surface is switched or freed, leading to a memory leak in the GPU process. This occurs because the GTK backend lacks a mapping for the 'dpy_gl_ctx_destroy_texture' operation.

This patch implements the necessary cleanup functions for both EGL and GL Area backends. Each implementation ensures the appropriate GL context is made current before calling surface_gl_destroy_texture(), ensuring that the underlying GL texture ID is safely released by the driver.

Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 include/ui/gtk.h |  4 ++++
 ui/gtk-egl.c     | 10 ++++++++++
 ui/gtk-gl-area.c |  8 ++++++++
 ui/gtk.c         |  2 ++
 4 files changed, 24 insertions(+)
include/ui/gtk.h index renamed from include/ui/gtk.h +0 -0

3e6ce3cb48..f1765ad5cb 100644

include/ui/gtk.h +2 -0
@@ -170,6 +170,8 @@ void gd_egl_switch(DisplayChangeListener *dcl,
                    DisplaySurface *surface);  QEMUGLContext
 gd_egl_create_context(DisplayGLCtx *dgc,
                                     QEMUGLParams *params);
+void gd_egl_destroy_texture(DisplayGLCtx *dgc,
+                            DisplaySurface *surface);
 void gd_egl_scanout_disable(DisplayChangeListener *dcl);  void
 gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_id, @@ -206,6 +208,8 @@

QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
QEMUGLParams *params); void
gd_gl_area_destroy_context(DisplayGLCtx *dgc,

                                 QEMUGLContext ctx);
include/ui/gtk.h +2 -0
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc,
+                                DisplaySurface *surface);
 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
                                QemuDmaBuf *dmabuf);  void

gd_gl_area_scanout_texture(DisplayChangeListener *dcl, diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c index ae9239999c..c1b32bc41b 100644

ui/gtk-egl.c +0 -0
@@ -225,6 +225,16 @@ QEMUGLContext

gd_egl_create_context(DisplayGLCtx *dgc,

ui/gtk-gl-area.c void gd_gl_area_destroy_context(DisplayGLCtx

Quoted 18 lines
ui/gtk-egl.c +9 -0
     return qemu_egl_create_context(dgc, params);  }
 
+void gd_egl_destroy_texture(DisplayGLCtx *dgc, DisplaySurface
+*surface) {
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+
+    surface_gl_destroy_texture(dgc->gls, surface); }
+
 void gd_egl_scanout_disable(DisplayChangeListener *dcl)  {
     VirtualConsole *vc = container_of(dcl, VirtualConsole, gfx.dcl);
ui/gtk-gl-area.c index renamed from ui/gtk-gl-area.c +0 -0

cd86022d26..84e41f4221 100644

ui/gtk-gl-area.c +0 -0
@@ -299,6 +299,14 @@ void gd_gl_area_destroy_context(DisplayGLCtx

*dgc, QEMUGLContext ctx)

Quoted 23 lines
ui/gtk-gl-area.c +7 -0
     g_clear_object(&ctx);
 }
 
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc, DisplaySurface
+*surface) {
+    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    surface_gl_destroy_texture(dgc->gls, surface); }
+
 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_id,
                                 bool backing_y_0_top, diff --git

a/ui/gtk.c b/ui/gtk.c index 9ebe7e8df0..217f425075 100644

ui/gtk.c +1 -0
@@ -636,6 +636,7 @@ static const DisplayGLCtxOps gl_area_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_gl_area_create_context,
     .dpy_gl_ctx_destroy      = gd_gl_area_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_gl_area_destroy_texture,
     .dpy_gl_ctx_make_current = gd_gl_area_make_current,  };
 
Quoted 9 lines

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 675

Quoted 6 lines
ui/gtk.c +1 -0
@@ -670,6 +671,7 @@ static const DisplayGLCtxOps egl_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_egl_create_context,
     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_egl_destroy_texture,
     .dpy_gl_ctx_make_current = gd_egl_make_current,

But there is no dpy_gl_ctx_create_texture(), how can dpy_gl_ctx_destroy_texture() get called?

Quoted 2 lines

Textures are also created via surface_gl_create_texture in the places like,

But surface_gl_{create,destroy}_texture doesn't make use of dpy_gl_ctx_destroy_texture. The later is only used by dbus so far, see commit 589089feee ("ui/dbus: fix texture sharing").

Quoted 27 lines

gtk-egl.c
static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout) {
.....

       egl_fb_destroy(&vc->gfx.guest_fb);
        if (vc->gfx.surface) {
            surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
            surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
        }

surface_gl_create_texture always come after surface_gl_destroy_texture so there is no leak while the guest is running but texture would remain unremoved if we do none in dpy_gl_ctx_destroy_texture() when the guest is rebooting.

Quoted 11 lines
Quoted 7 lines
ui/gtk.c +0 -1
 };
 #endif
--

2.43.0

--
Marc-André Lureau

Thanks

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

Hi Marc-André,

Quoted 123 lines

Subject: Re: [PATCH] ui/gtk: Implement dpy_gl_ctx_destroy_texture for gtk- egl and gtk-gl-area

Hi

Quoted 21 lines · Dongwon

Hi Marc-André,

Quoted 18 lines

Subject: Re: [PATCH] ui/gtk: Implement dpy_gl_ctx_destroy_texture for gtk- egl and gtk-gl-area

Hi

Quoted 12 lines · On Tue, Mar 3, 2026 at 4:13 AM <[email protected]> wrote:

From: Dongwon Kim <[email protected]>

Currently, the texture associated with a DisplaySurface is not deleted when the surface is switched or freed, leading to a memory leak in the GPU process. This occurs because the GTK backend lacks a mapping for the 'dpy_gl_ctx_destroy_texture' operation.

This patch implements the necessary cleanup functions for both EGL and GL Area backends. Each implementation ensures the appropriate GL context is made current before calling surface_gl_destroy_texture(), ensuring that the underlying GL texture ID

is safely released by the driver.

Quoted 62 lines
Quoted 62 lines

ui/gtk-egl.c QEMUGLContext

Quoted 38 lines
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Vivek Kasireddy <[email protected]>
---
 include/ui/gtk.h |  4 ++++
 ui/gtk-egl.c     | 10 ++++++++++
 ui/gtk-gl-area.c |  8 ++++++++
 ui/gtk.c         |  2 ++
 4 files changed, 24 insertions(+)
include/ui/gtk.h index renamed from include/ui/gtk.h +0 -0

3e6ce3cb48..f1765ad5cb 100644

include/ui/gtk.h +2 -0
@@ -170,6 +170,8 @@ void gd_egl_switch(DisplayChangeListener *dcl,
                    DisplaySurface *surface);  QEMUGLContext
 gd_egl_create_context(DisplayGLCtx *dgc,
                                     QEMUGLParams *params);
+void gd_egl_destroy_texture(DisplayGLCtx *dgc,
+                            DisplaySurface *surface);
 void gd_egl_scanout_disable(DisplayChangeListener *dcl);  void
 gd_egl_scanout_texture(DisplayChangeListener *dcl,
                             uint32_t backing_id, @@ -206,6 +208,8

@@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,

                                         QEMUGLParams *params);
void gd_gl_area_destroy_context(DisplayGLCtx *dgc,
                                 QEMUGLContext ctx);
include/ui/gtk.h +2 -0
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc,
+                                DisplaySurface *surface);
 void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
                                QemuDmaBuf *dmabuf);  void

gd_gl_area_scanout_texture(DisplayChangeListener *dcl, diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c index ae9239999c..c1b32bc41b 100644

ui/gtk-egl.c +0 -0
@@ -225,6 +225,16 @@ QEMUGLContext

gd_egl_create_context(DisplayGLCtx *dgc,

ui/gtk-gl-area.c void gd_gl_area_destroy_context(DisplayGLCtx

Quoted 19 lines
ui/gtk-egl.c +10 -0
     return qemu_egl_create_context(dgc, params);  }
 
+void gd_egl_destroy_texture(DisplayGLCtx *dgc, DisplaySurface
+*surface) {
+    VirtualConsole *vc = container_of(dgc, VirtualConsole,
+gfx.dgc);
+
+    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
+                   vc->gfx.esurface, vc->gfx.ectx);
+
+    surface_gl_destroy_texture(dgc->gls, surface); }
+
 void gd_egl_scanout_disable(DisplayChangeListener *dcl)  {
     VirtualConsole *vc = container_of(dcl, VirtualConsole,

gfx.dcl); diff --git a/ui/gtk-gl-area.c b/ui/gtk-gl-area.c index cd86022d26..84e41f4221 100644

ui/gtk-gl-area.c +0 -0
@@ -299,6 +299,14 @@ void gd_gl_area_destroy_context(DisplayGLCtx

*dgc, QEMUGLContext ctx)

Quoted 3 lines
ui/gtk-gl-area.c +0 -0
     g_clear_object(&ctx);
 }
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
ui/gtk-gl-area.c +7 -0
+void gd_gl_area_destroy_texture(DisplayGLCtx *dgc, DisplaySurface
+*surface) {
+    VirtualConsole *vc = container_of(dgc, VirtualConsole,
+gfx.dgc);
+
+    gtk_gl_area_make_current(GTK_GL_AREA(vc->gfx.drawing_area));
+    surface_gl_destroy_texture(dgc->gls, surface); }
Quoted 8 lines

ui/gtk.c static const DisplayGLCtxOps gl_area_ctx_ops =

Quoted 8 lines
ui/gtk-gl-area.c +1 -0
+
 void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
                                 uint32_t backing_id,
                                 bool backing_y_0_top, diff --git

a/ui/gtk.c b/ui/gtk.c index 9ebe7e8df0..217f425075 100644

ui/gtk.c +0 -0
@@ -636,6 +636,7 @@ static const DisplayGLCtxOps gl_area_ctx_ops =

{

Quoted 16 lines
Quoted 15 lines

ui/gtk.c static const DisplayGLCtxOps egl_ctx_ops = { · line 675

Quoted 12 lines
ui/gtk.c +2 -0
     .dpy_gl_ctx_is_compatible_dcl = gd_gl_area_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_gl_area_create_context,
     .dpy_gl_ctx_destroy      = gd_gl_area_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_gl_area_destroy_texture,
     .dpy_gl_ctx_make_current = gd_gl_area_make_current,  };
 
@@ -670,6 +671,7 @@ static const DisplayGLCtxOps egl_ctx_ops = {
     .dpy_gl_ctx_is_compatible_dcl = gd_egl_is_compatible_dcl,
     .dpy_gl_ctx_create       = gd_egl_create_context,
     .dpy_gl_ctx_destroy      = qemu_egl_destroy_context,
+    .dpy_gl_ctx_destroy_texture = gd_egl_destroy_texture,
     .dpy_gl_ctx_make_current = gd_egl_make_current,

But there is no dpy_gl_ctx_create_texture(), how can dpy_gl_ctx_destroy_texture() get called?

Quoted 7 lines
Quoted 3 lines

Textures are also created via surface_gl_create_texture in the places like,

But surface_gl_{create,destroy}_texture doesn't make use of dpy_gl_ctx_destroy_texture. The later is only used by dbus so far, see commit 589089feee ("ui/dbus: fix texture sharing").

I thought adding the call back was the simplest way to avoid the mem leak we observed but now I understand this approach looks more like a WA. I will take a look inside gtk-egl.c and check if there is better way to prevent the corner case. Probably the root-cause is that the previous context was not destroyed properly.

Quoted 33 lines
Quoted 10 lines

gtk-egl.c
static void gtk_egl_set_scanout_mode(VirtualConsole *vc, bool scanout) { .....

       egl_fb_destroy(&vc->gfx.guest_fb);
        if (vc->gfx.surface) {
            surface_gl_destroy_texture(vc->gfx.gls, vc->gfx.ds);
            surface_gl_create_texture(vc->gfx.gls, vc->gfx.ds);
        }
Quoted 3 lines

surface_gl_create_texture always come after surface_gl_destroy_texture so there is no leak while the guest is running but texture would remain unremoved if we do none in dpy_gl_ctx_destroy_texture() when

the guest is rebooting.

Quoted 10 lines
Quoted 10 lines
Quoted 8 lines
ui/gtk.c +0 -1
 };
 #endif
--

2.43.0

Quoted 4 lines
Quoted 2 lines

--
Marc-André Lureau

Thanks

--
Marc-André Lureau