[PATCH v21 03/18] ui/egl: Don't change bound GL context when creating new context

Dmitry Osipenko posted 18 patches 1 month, 1 week ago
Maintainers: Pierrick Bouvier <pierrick.bouvier@linaro.org>, "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>, "Michael S. Tsirkin" <mst@redhat.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>
There is a newer version of this series
[PATCH v21 03/18] ui/egl: Don't change bound GL context when creating new context
Posted by Dmitry Osipenko 1 month, 1 week ago
Don't change bound GL context when creating new GL context for consistency
with behavior expected by virglrenderer that assumes context-creation doesn't
switch context. eglCreateContext() doesn't require GL context to be bound
when it's invoked. Update qemu_egl_create_context() to spawn GL sub-contexts
from a common parent GL context instead of a currently-bound context.

Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
---
 ui/dbus.c          | 2 --
 ui/egl-context.c   | 3 +--
 ui/egl-headless.c  | 2 --
 ui/gtk-egl.c       | 4 ----
 ui/spice-display.c | 2 --
 5 files changed, 1 insertion(+), 12 deletions(-)

diff --git a/ui/dbus.c b/ui/dbus.c
index 905ee6fea71b..750a064e3fbf 100644
--- a/ui/dbus.c
+++ b/ui/dbus.c
@@ -47,8 +47,6 @@ static DBusDisplay *dbus_display;
 static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
                                          QEMUGLParams *params)
 {
-    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
-                   qemu_egl_rn_ctx);
     return qemu_egl_create_context(dgc, params);
 }
 
diff --git a/ui/egl-context.c b/ui/egl-context.c
index aed3e3ba1f3f..f9a998768beb 100644
--- a/ui/egl-context.c
+++ b/ui/egl-context.c
@@ -19,8 +19,7 @@ QEMUGLContext qemu_egl_create_context(DisplayGLCtx *dgc,
    };
    bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
 
-   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
-                          eglGetCurrentContext(),
+   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config, qemu_egl_rn_ctx,
                           gles ? ctx_att_gles : ctx_att_core);
    return ctx;
 }
diff --git a/ui/egl-headless.c b/ui/egl-headless.c
index 1f6b845500dd..19955792ac95 100644
--- a/ui/egl-headless.c
+++ b/ui/egl-headless.c
@@ -42,8 +42,6 @@ static void egl_gfx_switch(DisplayChangeListener *dcl,
 static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
                                         QEMUGLParams *params)
 {
-    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
-                   qemu_egl_rn_ctx);
     return qemu_egl_create_context(dgc, params);
 }
 
diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c
index 0dbb429958e5..6b3519419039 100644
--- a/ui/gtk-egl.c
+++ b/ui/gtk-egl.c
@@ -217,10 +217,6 @@ void gd_egl_switch(DisplayChangeListener *dcl,
 QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
                                     QEMUGLParams *params)
 {
-    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
-
-    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
-                   vc->gfx.esurface, vc->gfx.ectx);
     return qemu_egl_create_context(dgc, params);
 }
 
diff --git a/ui/spice-display.c b/ui/spice-display.c
index 28399f8a8174..ae0f771c2a38 100644
--- a/ui/spice-display.c
+++ b/ui/spice-display.c
@@ -1033,8 +1033,6 @@ static void spice_gl_switch(DisplayChangeListener *dcl,
 static QEMUGLContext qemu_spice_gl_create_context(DisplayGLCtx *dgc,
                                                   QEMUGLParams *params)
 {
-    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
-                   qemu_egl_rn_ctx);
     return qemu_egl_create_context(dgc, params);
 }
 
-- 
2.52.0
Re: [PATCH v21 03/18] ui/egl: Don't change bound GL context when creating new context
Posted by Akihiko Odaki 1 month, 1 week ago
On 2026/03/02 23:59, Dmitry Osipenko wrote:
> Don't change bound GL context when creating new GL context for consistency
> with behavior expected by virglrenderer that assumes context-creation doesn't
> switch context. eglCreateContext() doesn't require GL context to be bound
> when it's invoked. Update qemu_egl_create_context() to spawn GL sub-contexts
> from a common parent GL context instead of a currently-bound context.
> 
> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> ---
>   ui/dbus.c          | 2 --
>   ui/egl-context.c   | 3 +--
>   ui/egl-headless.c  | 2 --
>   ui/gtk-egl.c       | 4 ----
>   ui/spice-display.c | 2 --
>   5 files changed, 1 insertion(+), 12 deletions(-)
> 
> diff --git a/ui/dbus.c b/ui/dbus.c
> index 905ee6fea71b..750a064e3fbf 100644
> --- a/ui/dbus.c
> +++ b/ui/dbus.c
> @@ -47,8 +47,6 @@ static DBusDisplay *dbus_display;
>   static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
>                                            QEMUGLParams *params)
>   {
> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
> -                   qemu_egl_rn_ctx);
>       return qemu_egl_create_context(dgc, params);
>   }
>   
> diff --git a/ui/egl-context.c b/ui/egl-context.c
> index aed3e3ba1f3f..f9a998768beb 100644
> --- a/ui/egl-context.c
> +++ b/ui/egl-context.c
> @@ -19,8 +19,7 @@ QEMUGLContext qemu_egl_create_context(DisplayGLCtx *dgc,
>      };
>      bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
>   
> -   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
> -                          eglGetCurrentContext(),
> +   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config, qemu_egl_rn_ctx,
>                             gles ? ctx_att_gles : ctx_att_core);
>      return ctx;
>   }
> diff --git a/ui/egl-headless.c b/ui/egl-headless.c
> index 1f6b845500dd..19955792ac95 100644
> --- a/ui/egl-headless.c
> +++ b/ui/egl-headless.c
> @@ -42,8 +42,6 @@ static void egl_gfx_switch(DisplayChangeListener *dcl,
>   static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
>                                           QEMUGLParams *params)
>   {
> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
> -                   qemu_egl_rn_ctx);
>       return qemu_egl_create_context(dgc, params);
>   }
>   
> diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c
> index 0dbb429958e5..6b3519419039 100644
> --- a/ui/gtk-egl.c
> +++ b/ui/gtk-egl.c
> @@ -217,10 +217,6 @@ void gd_egl_switch(DisplayChangeListener *dcl,
>   QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
>                                       QEMUGLParams *params)
>   {
> -    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
> -
> -    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
> -                   vc->gfx.esurface, vc->gfx.ectx);

This uses vc->gfx.ectx instead of qemu_egl_rn_ctx so it should be passed 
to qemu_egl_create_context().

Regards,
Akihiko Odaki

>       return qemu_egl_create_context(dgc, params);
>   }
>   
> diff --git a/ui/spice-display.c b/ui/spice-display.c
> index 28399f8a8174..ae0f771c2a38 100644
> --- a/ui/spice-display.c
> +++ b/ui/spice-display.c
> @@ -1033,8 +1033,6 @@ static void spice_gl_switch(DisplayChangeListener *dcl,
>   static QEMUGLContext qemu_spice_gl_create_context(DisplayGLCtx *dgc,
>                                                     QEMUGLParams *params)
>   {
> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
> -                   qemu_egl_rn_ctx);
>       return qemu_egl_create_context(dgc, params);
>   }
>
Re: [PATCH v21 03/18] ui/egl: Don't change bound GL context when creating new context
Posted by Dmitry Osipenko 1 month, 1 week ago
On 3/3/26 06:49, Akihiko Odaki wrote:
> On 2026/03/02 23:59, Dmitry Osipenko wrote:
>> Don't change bound GL context when creating new GL context for
>> consistency
>> with behavior expected by virglrenderer that assumes context-creation
>> doesn't
>> switch context. eglCreateContext() doesn't require GL context to be bound
>> when it's invoked. Update qemu_egl_create_context() to spawn GL sub-
>> contexts
>> from a common parent GL context instead of a currently-bound context.
>>
>> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
>> ---
>>   ui/dbus.c          | 2 --
>>   ui/egl-context.c   | 3 +--
>>   ui/egl-headless.c  | 2 --
>>   ui/gtk-egl.c       | 4 ----
>>   ui/spice-display.c | 2 --
>>   5 files changed, 1 insertion(+), 12 deletions(-)
>>
>> diff --git a/ui/dbus.c b/ui/dbus.c
>> index 905ee6fea71b..750a064e3fbf 100644
>> --- a/ui/dbus.c
>> +++ b/ui/dbus.c
>> @@ -47,8 +47,6 @@ static DBusDisplay *dbus_display;
>>   static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
>>                                            QEMUGLParams *params)
>>   {
>> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
>> -                   qemu_egl_rn_ctx);
>>       return qemu_egl_create_context(dgc, params);
>>   }
>>   diff --git a/ui/egl-context.c b/ui/egl-context.c
>> index aed3e3ba1f3f..f9a998768beb 100644
>> --- a/ui/egl-context.c
>> +++ b/ui/egl-context.c
>> @@ -19,8 +19,7 @@ QEMUGLContext qemu_egl_create_context(DisplayGLCtx
>> *dgc,
>>      };
>>      bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
>>   -   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
>> -                          eglGetCurrentContext(),
>> +   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
>> qemu_egl_rn_ctx,
>>                             gles ? ctx_att_gles : ctx_att_core);
>>      return ctx;
>>   }
>> diff --git a/ui/egl-headless.c b/ui/egl-headless.c
>> index 1f6b845500dd..19955792ac95 100644
>> --- a/ui/egl-headless.c
>> +++ b/ui/egl-headless.c
>> @@ -42,8 +42,6 @@ static void egl_gfx_switch(DisplayChangeListener *dcl,
>>   static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
>>                                           QEMUGLParams *params)
>>   {
>> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
>> -                   qemu_egl_rn_ctx);
>>       return qemu_egl_create_context(dgc, params);
>>   }
>>   diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c
>> index 0dbb429958e5..6b3519419039 100644
>> --- a/ui/gtk-egl.c
>> +++ b/ui/gtk-egl.c
>> @@ -217,10 +217,6 @@ void gd_egl_switch(DisplayChangeListener *dcl,
>>   QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
>>                                       QEMUGLParams *params)
>>   {
>> -    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
>> -
>> -    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
>> -                   vc->gfx.esurface, vc->gfx.ectx);
> 
> This uses vc->gfx.ectx instead of qemu_egl_rn_ctx so it should be passed
> to qemu_egl_create_context().

Good catch. AFAICS, the vc->gfx.ectx is initialized only once in the
code. Will double check.

One problem with gtk-egl display is that this is the only display type I
haven't tested because it's not obvious how to do it, The gdk-area is
what always get used on my machine with both x11 and wayland, and
forcing gtk-egl in the code instead of gdk-area results in a crash. I'd
appreciate if anyone could clarify how to make QEMU use gtk-egl or
whether it has been obsoleted and doesn't work with modern GTK.

-- 
Best regards,
Dmitry

Re: [PATCH v21 03/18] ui/egl: Don't change bound GL context when creating new context
Posted by Akihiko Odaki 1 month, 1 week ago
On 2026/03/03 21:05, Dmitry Osipenko wrote:
> On 3/3/26 06:49, Akihiko Odaki wrote:
>> On 2026/03/02 23:59, Dmitry Osipenko wrote:
>>> Don't change bound GL context when creating new GL context for
>>> consistency
>>> with behavior expected by virglrenderer that assumes context-creation
>>> doesn't
>>> switch context. eglCreateContext() doesn't require GL context to be bound
>>> when it's invoked. Update qemu_egl_create_context() to spawn GL sub-
>>> contexts
>>> from a common parent GL context instead of a currently-bound context.
>>>
>>> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
>>> ---
>>>    ui/dbus.c          | 2 --
>>>    ui/egl-context.c   | 3 +--
>>>    ui/egl-headless.c  | 2 --
>>>    ui/gtk-egl.c       | 4 ----
>>>    ui/spice-display.c | 2 --
>>>    5 files changed, 1 insertion(+), 12 deletions(-)
>>>
>>> diff --git a/ui/dbus.c b/ui/dbus.c
>>> index 905ee6fea71b..750a064e3fbf 100644
>>> --- a/ui/dbus.c
>>> +++ b/ui/dbus.c
>>> @@ -47,8 +47,6 @@ static DBusDisplay *dbus_display;
>>>    static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
>>>                                             QEMUGLParams *params)
>>>    {
>>> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
>>> -                   qemu_egl_rn_ctx);
>>>        return qemu_egl_create_context(dgc, params);
>>>    }
>>>    diff --git a/ui/egl-context.c b/ui/egl-context.c
>>> index aed3e3ba1f3f..f9a998768beb 100644
>>> --- a/ui/egl-context.c
>>> +++ b/ui/egl-context.c
>>> @@ -19,8 +19,7 @@ QEMUGLContext qemu_egl_create_context(DisplayGLCtx
>>> *dgc,
>>>       };
>>>       bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
>>>    -   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
>>> -                          eglGetCurrentContext(),
>>> +   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
>>> qemu_egl_rn_ctx,
>>>                              gles ? ctx_att_gles : ctx_att_core);
>>>       return ctx;
>>>    }
>>> diff --git a/ui/egl-headless.c b/ui/egl-headless.c
>>> index 1f6b845500dd..19955792ac95 100644
>>> --- a/ui/egl-headless.c
>>> +++ b/ui/egl-headless.c
>>> @@ -42,8 +42,6 @@ static void egl_gfx_switch(DisplayChangeListener *dcl,
>>>    static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
>>>                                            QEMUGLParams *params)
>>>    {
>>> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
>>> -                   qemu_egl_rn_ctx);
>>>        return qemu_egl_create_context(dgc, params);
>>>    }
>>>    diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c
>>> index 0dbb429958e5..6b3519419039 100644
>>> --- a/ui/gtk-egl.c
>>> +++ b/ui/gtk-egl.c
>>> @@ -217,10 +217,6 @@ void gd_egl_switch(DisplayChangeListener *dcl,
>>>    QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
>>>                                        QEMUGLParams *params)
>>>    {
>>> -    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
>>> -
>>> -    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
>>> -                   vc->gfx.esurface, vc->gfx.ectx);
>>
>> This uses vc->gfx.ectx instead of qemu_egl_rn_ctx so it should be passed
>> to qemu_egl_create_context().
> 
> Good catch. AFAICS, the vc->gfx.ectx is initialized only once in the
> code. Will double check.
> 
> One problem with gtk-egl display is that this is the only display type I
> haven't tested because it's not obvious how to do it, The gdk-area is
> what always get used on my machine with both x11 and wayland, and
> forcing gtk-egl in the code instead of gdk-area results in a crash. I'd
> appreciate if anyone could clarify how to make QEMU use gtk-egl or
> whether it has been obsoleted and doesn't work with modern GTK.
> 

The following worked for me:
GDK_BACKEND=x11 qemu-system-aarch64 -M virt -display gtk,gl=on

Regards,
Akihiko Odaki

Re: [PATCH v21 03/18] ui/egl: Don't change bound GL context when creating new context
Posted by Dmitry Osipenko 1 month, 1 week ago
On 3/3/26 15:52, Akihiko Odaki wrote:
> On 2026/03/03 21:05, Dmitry Osipenko wrote:
>> On 3/3/26 06:49, Akihiko Odaki wrote:
>>> On 2026/03/02 23:59, Dmitry Osipenko wrote:
>>>> Don't change bound GL context when creating new GL context for
>>>> consistency
>>>> with behavior expected by virglrenderer that assumes context-creation
>>>> doesn't
>>>> switch context. eglCreateContext() doesn't require GL context to be
>>>> bound
>>>> when it's invoked. Update qemu_egl_create_context() to spawn GL sub-
>>>> contexts
>>>> from a common parent GL context instead of a currently-bound context.
>>>>
>>>> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
>>>> ---
>>>>    ui/dbus.c          | 2 --
>>>>    ui/egl-context.c   | 3 +--
>>>>    ui/egl-headless.c  | 2 --
>>>>    ui/gtk-egl.c       | 4 ----
>>>>    ui/spice-display.c | 2 --
>>>>    5 files changed, 1 insertion(+), 12 deletions(-)
>>>>
>>>> diff --git a/ui/dbus.c b/ui/dbus.c
>>>> index 905ee6fea71b..750a064e3fbf 100644
>>>> --- a/ui/dbus.c
>>>> +++ b/ui/dbus.c
>>>> @@ -47,8 +47,6 @@ static DBusDisplay *dbus_display;
>>>>    static QEMUGLContext dbus_create_context(DisplayGLCtx *dgc,
>>>>                                             QEMUGLParams *params)
>>>>    {
>>>> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
>>>> -                   qemu_egl_rn_ctx);
>>>>        return qemu_egl_create_context(dgc, params);
>>>>    }
>>>>    diff --git a/ui/egl-context.c b/ui/egl-context.c
>>>> index aed3e3ba1f3f..f9a998768beb 100644
>>>> --- a/ui/egl-context.c
>>>> +++ b/ui/egl-context.c
>>>> @@ -19,8 +19,7 @@ QEMUGLContext qemu_egl_create_context(DisplayGLCtx
>>>> *dgc,
>>>>       };
>>>>       bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
>>>>    -   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
>>>> -                          eglGetCurrentContext(),
>>>> +   ctx = eglCreateContext(qemu_egl_display, qemu_egl_config,
>>>> qemu_egl_rn_ctx,
>>>>                              gles ? ctx_att_gles : ctx_att_core);
>>>>       return ctx;
>>>>    }
>>>> diff --git a/ui/egl-headless.c b/ui/egl-headless.c
>>>> index 1f6b845500dd..19955792ac95 100644
>>>> --- a/ui/egl-headless.c
>>>> +++ b/ui/egl-headless.c
>>>> @@ -42,8 +42,6 @@ static void egl_gfx_switch(DisplayChangeListener
>>>> *dcl,
>>>>    static QEMUGLContext egl_create_context(DisplayGLCtx *dgc,
>>>>                                            QEMUGLParams *params)
>>>>    {
>>>> -    eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
>>>> -                   qemu_egl_rn_ctx);
>>>>        return qemu_egl_create_context(dgc, params);
>>>>    }
>>>>    diff --git a/ui/gtk-egl.c b/ui/gtk-egl.c
>>>> index 0dbb429958e5..6b3519419039 100644
>>>> --- a/ui/gtk-egl.c
>>>> +++ b/ui/gtk-egl.c
>>>> @@ -217,10 +217,6 @@ void gd_egl_switch(DisplayChangeListener *dcl,
>>>>    QEMUGLContext gd_egl_create_context(DisplayGLCtx *dgc,
>>>>                                        QEMUGLParams *params)
>>>>    {
>>>> -    VirtualConsole *vc = container_of(dgc, VirtualConsole, gfx.dgc);
>>>> -
>>>> -    eglMakeCurrent(qemu_egl_display, vc->gfx.esurface,
>>>> -                   vc->gfx.esurface, vc->gfx.ectx);
>>>
>>> This uses vc->gfx.ectx instead of qemu_egl_rn_ctx so it should be passed
>>> to qemu_egl_create_context().
>>
>> Good catch. AFAICS, the vc->gfx.ectx is initialized only once in the
>> code. Will double check.
>>
>> One problem with gtk-egl display is that this is the only display type I
>> haven't tested because it's not obvious how to do it, The gdk-area is
>> what always get used on my machine with both x11 and wayland, and
>> forcing gtk-egl in the code instead of gdk-area results in a crash. I'd
>> appreciate if anyone could clarify how to make QEMU use gtk-egl or
>> whether it has been obsoleted and doesn't work with modern GTK.
>>
> 
> The following worked for me:
> GDK_BACKEND=x11 qemu-system-aarch64 -M virt -display gtk,gl=on

That works, thanks a lot.

-- 
Best regards,
Dmitry