From 620360d81da9693ab296bc0621a3bdb53f661eda Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Thu, 7 Dec 2023 13:27:47 +0100 Subject: [PATCH] Make context not-current after rendering to prevent "already connected" on ANGLE Some phones no longer have a native (E)GL implementation but use ANGLE on top of Vulkan, which is more strict about Vulkan/NativeWindow surfaces that have lingering references from an existing EGL Surface. This is especially problematic with this app that simply recreates the EGL surface every time the callback is called, e.g. in surface creation, "change", and resize: E [SurfaceView[rust.androidnativesurface/rust.androidnativesurface.MainActivity]@0#1(BLAST Consumer)1](id:5c2300000001,api:1,p:23587,c:23587) connect: already connected (cur=1 req=1) E native_window_api_connect() failed: Invalid argument (-22) D ANGLE Info:Debug.cpp:490 (insertMessage): EGL ERROR: eglPlatformCreateWindowSurface: Internal Vulkan error (-1000000001): The requested window is already connected to a VkSurfaceKHR, or to some other non-Vulkan API, in ../../src/libANGLE/renderer/vulkan/android/WindowSurfaceVkAndroid.cpp, createSurfaceVk:133. I thread '' panicked at src/support.rs:36:78: I called `Result::unwrap()` on an `Err` value: Error { raw_code: Some(12301), raw_os_message: None, kind: BadSurface } This is simply fixed by un-currentling the current context and the surface associated with it. --- android_native_surface/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/android_native_surface/src/lib.rs b/android_native_surface/src/lib.rs index 40af0df..3504df2 100644 --- a/android_native_surface/src/lib.rs +++ b/android_native_surface/src/lib.rs @@ -83,7 +83,14 @@ fn render_to_native_window(window: NativeWindow) { renderer.draw(); - gl_window.surface.swap_buffers(&gl_context).unwrap(); + gl_window + .surface + .swap_buffers(&gl_context) + .expect("Cannot swap buffers"); + + gl_context + .make_not_current() + .expect("Cannot uncurrent GL context"); } #[no_mangle]