ImGui on Android with SDL2 and Vulkan loses surface when the app loses focus.
Version/Branch of Dear ImGui:
Master (HEAD detached at afe20dc9)
Back-ends:
imgui_impl_sdl2.cpp + imgui_impl_vulkan.cpp
Compiler, OS:
Android 15.0 (API Level 35) with NDK 25.2.9519653, running on Windows 11 Home (24H2)
Full config/build information:
See imgui_sdl2_vulkan_android. ImGui is checked out as a submodule.
Details:
My goal is to get DearImGui running in a minimal Android app which uses SDL2 and Vulkan, nothing more. You can find this exact setup here: imgui_sdl2_vulkan_android There is a lot of additional information in that project's README.
In short, while this project runs great, there is a major issue that comes up: When the app opens, you can't switch to another app and back without causing a crash. On some devices (like the Amazon Fire HD 10), you can't even enter the 'Recent Apps'/'App Overview' screen without crashing on re-entry. On other devices (like the Google Pixel 7), you can enter the 'App overview' screen and it will only crash if you actually switch to another app. Trying this on emulators has so far shown the same behavior.
Lets get specific. The Android Studio Debugger tells me that as I try to re-enter the app, it receives the Android SIGABRT Signal due to the result of vkAcquireNextImageKHR() being VK_ERROR_SURFACE_LOST_KHR, therefore triggering abort();. I assume that means that during the time that the app was not in focus, the vulkan surface has become invalid. Validation layers are enabled and shipping in my project, but what comes up seems unrelated to the problem.
When looking for this problem online, I found this problem report which seems in line with what I have observed: android-lost-focus-handling-in-vulkan
I don't really know how to go about this, but it seems that a rework of some parts of imgui_impl_vulkan.cpp would solve this issue. I would love to hear your thoughts on this. Also thank you for 10+ years of the best GUI library ever.
Screenshots/Video:
No response
Minimal, Complete and Verifiable Example code:
You can find it here: imgui_sdl2_vulkan_android
I encountered many issues with the android sample/backend. Some of them are reported some of them not but I usually manage to fix it myself.
The issue you are describing is familiar to me although I only used android+opengl3 backends. To fix it I have reworked the handling of APP_CMD_TERM_WINDOW in the provided main.cpp.
You may find all my changes in the ImRAD android template here
Hello,
Sorry for lack of answer. I'm not really sure what to make of this considering I don't have an Android device and the issue seems to be a general Vulkan/Android question rather than specific to Dear ImGui or its Vulkan backend.
It seems reasonable that vkAcquireNextImageKHR() would report a lost device.
Note that imgui_impl_vulkan.cpp, unless using multi-viewports (which would not make any sense on Android) does not call vkAcquireNextImageKHR() itself. The only call is in example's main.cpp.
So what I understand is that you wish our example code handled VK_ERROR_SURFACE_LOST_KHR ?
Wouldn't it be just a matter of changing
if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR)
g_SwapChainRebuild = true;
to
if (err == VK_ERROR_OUT_OF_DATE_KHR || err == VK_SUBOPTIMAL_KHR || err == VK_ERROR_SURFACE_LOST_KHR)
g_SwapChainRebuild = true;
Have you tried that?