Search code examples
androidandroid-ndkfocusvulkanlost-focus

Android lost focus handling in Vulkan


I'm playing with Vulkan in Android, whenever I touch Overview button (the one that is square) and get back to my app, it will crash.

Failed-Solution 1:

I tried to stop rendering When I loose focus and then resume rendering whenever I gain focus back, but It didn't work, I got crash at vkAcquireNextImageKHR.

Failed-Solution 2:

I tried to reinit all of the Vulkan objects from ground to up, but the sad funny thing is for the first loosing and gaining focus everything is OK but after twice, it crashes at the same point.


Solution

  • As hinted in my comment, check the result of vkAcquireNextImageKHR to determine if you need to recreate resources. If it's something like VK_ERROR_SURFACE_LOST_KHR you need to recreate the surface and all resources connected to it like the swap chain, etc. As a reference take a look at the window resize function of my example base class that is also called if vkAcquireNextImageKHR signals that the surface needs to be recreated.

    As for error tracking (on what you actually need to recreate) enable the validation layers. Just put them along with the lib (.so) of your application, enable validation (note that you need to manually select all layers as there is no meta layer like VK_LAYER_LUNARG_standard_validation on android) and output validation messages to e.g. logcat. You can see my debug unit as a reference.