[PATCH v20 03/21] ui/gdk: Restore original context after new context creation

Dmitry Osipenko posted 21 patches 11 hours ago
Maintainers: Pierrick Bouvier <pierrick.bouvier@linaro.org>, "Michael S. Tsirkin" <mst@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>
[PATCH v20 03/21] ui/gdk: Restore original context after new context creation
Posted by Dmitry Osipenko 11 hours ago
Get currently bound GL context when creating new GL context and restore
it after the creation for consistency with behavior expected by virglrenderer
that assumes context-creation doesn't switch context.

Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
 ui/gtk-gl-area.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/ui/gtk-gl-area.c b/ui/gtk-gl-area.c
index c709b2ce0f63..55c2c55ee93b 100644
--- a/ui/gtk-gl-area.c
+++ b/ui/gtk-gl-area.c
@@ -250,17 +250,19 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
                                         QEMUGLParams *params)
 {
     VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
+    GdkGLContext *ctx, *current_ctx;
     GdkWindow *window;
-    GdkGLContext *ctx;
     GError *err = NULL;
     int major, minor;
 
+    current_ctx = gdk_gl_context_get_current();
+
     window = gtk_widget_get_window(vc->gfx.drawing_area);
     ctx = gdk_window_create_gl_context(window, &err);
     if (err) {
         g_printerr("Create gdk gl context failed: %s\n", err->message);
         g_error_free(err);
-        return NULL;
+        goto out;
     }
     gdk_gl_context_set_required_version(ctx,
                                         params->major_ver,
@@ -270,7 +272,7 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
         g_printerr("Realize gdk gl context failed: %s\n", err->message);
         g_error_free(err);
         g_clear_object(&ctx);
-        return NULL;
+        goto out;
     }
 
     gdk_gl_context_make_current(ctx);
@@ -283,6 +285,13 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
         g_clear_object(&ctx);
     }
 
+out:
+    if (current_ctx) {
+        gdk_gl_context_make_current(current_ctx);
+    } else {
+        gdk_gl_context_clear_current();
+    }
+
     trace_gd_gl_area_create_context(ctx, params->major_ver, params->minor_ver);
     return ctx;
 }
-- 
2.52.0
Re: [PATCH v20 03/21] ui/gdk: Restore original context after new context creation
Posted by Akihiko Odaki 2 hours ago
On 2026/03/02 5:48, Dmitry Osipenko wrote:
> Get currently bound GL context when creating new GL context and restore
> it after the creation for consistency with behavior expected by virglrenderer
> that assumes context-creation doesn't switch context.
> 
> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> ---
>   ui/gtk-gl-area.c | 15 ++++++++++++---
>   1 file changed, 12 insertions(+), 3 deletions(-)
> 
> diff --git a/ui/gtk-gl-area.c b/ui/gtk-gl-area.c
> index c709b2ce0f63..55c2c55ee93b 100644
> --- a/ui/gtk-gl-area.c
> +++ b/ui/gtk-gl-area.c
> @@ -250,17 +250,19 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
>                                           QEMUGLParams *params)
>   {
>       VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
> +    GdkGLContext *ctx, *current_ctx;
>       GdkWindow *window;
> -    GdkGLContext *ctx;
>       GError *err = NULL;
>       int major, minor;
>   
> +    current_ctx = gdk_gl_context_get_current();
> +
>       window = gtk_widget_get_window(vc->gfx.drawing_area);
>       ctx = gdk_window_create_gl_context(window, &err);
>       if (err) {
>           g_printerr("Create gdk gl context failed: %s\n", err->message);
>           g_error_free(err);
> -        return NULL;
> +        goto out;

I don't think this change is needed. We do not change the bound context yet.

>       }
>       gdk_gl_context_set_required_version(ctx,
>                                           params->major_ver,
> @@ -270,7 +272,7 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
>           g_printerr("Realize gdk gl context failed: %s\n", err->message);
>           g_error_free(err);
>           g_clear_object(&ctx);
> -        return NULL;
> +        goto out;
>       }
>   
>       gdk_gl_context_make_current(ctx);
> @@ -283,6 +285,13 @@ QEMUGLContext gd_gl_area_create_context(DisplayGLCtx *dgc,
>           g_clear_object(&ctx);
>       }
>   
> +out:
> +    if (current_ctx) {
> +        gdk_gl_context_make_current(current_ctx);
> +    } else {
> +        gdk_gl_context_clear_current();
> +    }
> +

This function already calls gdk_gl_context_clear_current() and 
gtk_gl_area_make_current(). They should be replaced.

Regards,
Akihiko Odaki

>       trace_gd_gl_area_create_context(ctx, params->major_ver, params->minor_ver);
>       return ctx;
>   }