VirtIO-GPU commands may be processed outside of main-loop, from a CPU
thread. This results in GL context switch failure because context is held
by main-loop thread when GL is invoked from CPU thread. Always release GL
context after performing GL operations such that other threads could take
over the context.

Signed-off-by: Dmitry Osipenko <[email protected]>
---
 ui/gtk-gl-area.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/ui/gtk-gl-area.c b/ui/gtk-gl-area.c
index c709b2ce0f63..4d8ccb605f17 100644
--- a/ui/gtk-gl-area.c
+++ b/ui/gtk-gl-area.c
@@ -140,6 +140,8 @@ void gd_gl_area_draw(VirtualConsole *vc)
         surface_gl_setup_viewport(vc->gfx.gls, vc->gfx.ds, pw, ph);
         surface_gl_render_texture(vc->gfx.gls, vc->gfx.ds);
     }
+
+    gdk_gl_context_clear_current();
 }
 
 void gd_gl_area_update(DisplayChangeListener *dcl,
@@ -201,6 +203,8 @@ void gd_gl_area_refresh(DisplayChangeListener *dcl)
         vc->gfx.glupdates = 0;
         gtk_gl_area_queue_render(GTK_GL_AREA(vc->gfx.drawing_area));
     }
+
+    gdk_gl_context_clear_current();
 }
 
 void gd_gl_area_switch(DisplayChangeListener *dcl,
@@ -227,6 +231,8 @@ void gd_gl_area_switch(DisplayChangeListener *dcl,
     if (resized) {
         gd_update_windowsize(vc);
     }
+
+    gdk_gl_context_clear_current();
 }
 
 static int gd_cmp_gl_context_version(int major, int minor, QEMUGLParams 
*params)
@@ -325,6 +331,8 @@ void gd_gl_area_scanout_texture(DisplayChangeListener *dcl,
     gtk_gl_area_set_scanout_mode(vc, true);
     egl_fb_setup_for_tex(&vc->gfx.guest_fb, backing_width, backing_height,
                          backing_id, false);
+
+    gdk_gl_context_clear_current();
 }
 
 void gd_gl_area_scanout_disable(DisplayChangeListener *dcl)
@@ -378,6 +386,8 @@ void gd_gl_area_scanout_dmabuf(DisplayChangeListener *dcl,
     if (qemu_dmabuf_get_allow_fences(dmabuf)) {
         vc->gfx.guest_fb.dmabuf = dmabuf;
     }
+
+    gdk_gl_context_clear_current();
 #endif
 }
 
@@ -389,6 +399,11 @@ void gtk_gl_area_init(void)
 int gd_gl_area_make_current(DisplayGLCtx *dgc,
                             QEMUGLContext ctx)
 {
-    gdk_gl_context_make_current(ctx);
+    if (ctx) {
+        gdk_gl_context_make_current(ctx);
+    } else {
+        gdk_gl_context_clear_current();
+    }
+
     return 0;
 }
-- 
2.52.0


Reply via email to