From eb5693f6da560336bd0cbf7f7f6098c4c27ba95b Mon Sep 17 00:00:00 2001 From: Solessfir <34831419+Solessfir@users.noreply.github.com> Date: Wed, 11 Nov 2026 19:22:22 +0100 Subject: [PATCH 1/2] Keep the Android app running while its window is hidden --- Engine/gapi/vulkan/vswapchain.cpp | 26 ++++++++++++++++++++++++-- Engine/gapi/vulkan/vswapchain.h | 5 +++++ Engine/system/api/androidapi.cpp | 19 ++++++++++++++----- Engine/system/api/androidapi.h | 4 ++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/Engine/gapi/vulkan/vswapchain.cpp b/Engine/gapi/vulkan/vswapchain.cpp index db677e07..b52023ef 100644 --- a/Engine/gapi/vulkan/vswapchain.cpp +++ b/Engine/gapi/vulkan/vswapchain.cpp @@ -8,7 +8,9 @@ #include "vdevice.h" -#if defined(__UNIX__) +#if defined(__ANDROID__) +#include "system/api/androidapi.h" +#elif defined(__UNIX__) #include "system/api/x11api.h" #include "system/api/waylandapi.h" #endif @@ -273,7 +275,7 @@ VkResult VSwapchain::createSurface(VkInstance instance, void* hwnd, VkSurfaceKHR #elif defined(__ANDROID__) VkAndroidSurfaceCreateInfoKHR createInfo = {}; createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; - createInfo.window = reinterpret_cast(hwnd); + createInfo.window = AndroidApi::nativeWindow(reinterpret_cast(hwnd)); return vkCreateAndroidSurfaceKHR(instance,&createInfo,nullptr,pSurface); #elif defined(__UNIX__) #if defined(TEMPEST_BUILD_WAYLAND) @@ -325,6 +327,11 @@ void VSwapchain::cleanupSurface() noexcept { if(surface!=VK_NULL_HANDLE) vkDestroySurfaceKHR(device.instance,surface,nullptr); surface = VK_NULL_HANDLE; +#if defined(__ANDROID__) + if(window!=nullptr) + ANativeWindow_release(window); + window = nullptr; +#endif } void VSwapchain::reset() { @@ -340,11 +347,21 @@ void VSwapchain::cleanup() noexcept { VkSurfaceKHR VSwapchain::createSurface(VkInstance instance, void* hwnd) { if(hwnd==nullptr) return VK_NULL_HANDLE; +#if defined(__ANDROID__) + // NativeActivity gives a new window after the activity was hidden + if(window!=AndroidApi::nativeWindow(this->hwnd)) + cleanupSurface(); +#endif if(surface!=VK_NULL_HANDLE) return surface; VkSurfaceKHR ret = VK_NULL_HANDLE; if(createSurface(instance, hwnd, &ret)!=VK_SUCCESS) throw std::system_error(Tempest::GraphicsErrc::NoDevice); +#if defined(__ANDROID__) + // The reference keeps the old window alive, so its replacement can't get the same address + window = AndroidApi::nativeWindow(this->hwnd); + ANativeWindow_acquire(window); +#endif return ret; } @@ -535,6 +552,11 @@ bool VSwapchain::isSwapchainLost(VkResult code) const { AbstractGraphicsApi::Texture* VSwapchain::currentBackBuffer() { if(currentSyncState == SyncState::S_Idle) { +#if defined(__ANDROID__) + // Drivers may keep presenting to a window Android has already replaced + if(window!=AndroidApi::nativeWindow(hwnd)) + throw SwapchainSuboptimal(); +#endif const VkResult code = implAcquireNextImage(); if(isSwapchainLost(code)) throw SwapchainSuboptimal(); diff --git a/Engine/gapi/vulkan/vswapchain.h b/Engine/gapi/vulkan/vswapchain.h index 3919eb8f..8971efd5 100644 --- a/Engine/gapi/vulkan/vswapchain.h +++ b/Engine/gapi/vulkan/vswapchain.h @@ -4,6 +4,8 @@ #include "gapi/vulkan/vtexture.h" #include "vulkan_sdk.h" +struct ANativeWindow; + namespace Tempest { namespace Detail { @@ -103,6 +105,9 @@ class VSwapchain : public AbstractGraphicsApi::Swapchain { SystemApi::Window* hwnd = nullptr; VkSurfaceKHR surface = VK_NULL_HANDLE; VkSwapchainKHR swapChain = VK_NULL_HANDLE; +#if defined(__ANDROID__) + ANativeWindow* window = nullptr; +#endif SpinLock sync; uint32_t imgIndex = 0; diff --git a/Engine/system/api/androidapi.cpp b/Engine/system/api/androidapi.cpp index 96b78295..6ca18744 100644 --- a/Engine/system/api/androidapi.cpp +++ b/Engine/system/api/androidapi.cpp @@ -157,6 +157,9 @@ void AndroidApi::onAppCmd(void*, int32_t cmd) { updateFocus(); break; case APP_CMD_TERM_WINDOW: + // The activity is only hidden. Rendering stops until NativeActivity provides a new window. + hasWindow = false; + break; case APP_CMD_DESTROY: hasWindow = false; if(mainWindow!=nullptr) { @@ -258,8 +261,8 @@ SystemApi::Window* AndroidApi::createAndroidWindow(Tempest::Window* owner) { if(isExit.load() || app->destroyRequested!=0) return nullptr; mainWindow = owner; - ANativeWindow_acquire(app->window); - return reinterpret_cast(app->window); + // NativeActivity replaces its ANativeWindow while the activity lives; the activity identifies the window + return reinterpret_cast(app->activity); } SystemApi::Window* AndroidApi::implCreateWindow(Tempest::Window* owner, uint32_t, uint32_t) { @@ -270,8 +273,7 @@ SystemApi::Window* AndroidApi::implCreateWindow(Tempest::Window* owner, ShowMode return createAndroidWindow(owner); } -void AndroidApi::implDestroyWindow(SystemApi::Window* w) { - ANativeWindow_release(reinterpret_cast(w)); +void AndroidApi::implDestroyWindow(SystemApi::Window*) { mainWindow = nullptr; touches.clear(); } @@ -281,8 +283,15 @@ void AndroidApi::implExit() { ALooper_wake(app->looper); } +ANativeWindow* AndroidApi::nativeWindow(SystemApi::Window* w) { + assert(reinterpret_cast(w)==app->activity); + return app->window; + } + Rect AndroidApi::implWindowClientRect(SystemApi::Window* w) { - const auto window = reinterpret_cast(w); + const auto window = nativeWindow(w); + if(window==nullptr) + return Rect(); return Rect(0,0,ANativeWindow_getWidth(window),ANativeWindow_getHeight(window)); } diff --git a/Engine/system/api/androidapi.h b/Engine/system/api/androidapi.h index 54d4ec55..af8adf1d 100644 --- a/Engine/system/api/androidapi.h +++ b/Engine/system/api/androidapi.h @@ -3,12 +3,16 @@ #include #include +struct ANativeWindow; + namespace Tempest { class AndroidApi final : SystemApi { public: static std::filesystem::path internalDataPath(); static std::filesystem::path externalDataPath(); + // Current window of the activity, or null while the activity is hidden + static ANativeWindow* nativeWindow(SystemApi::Window* w); private: AndroidApi() = default; From c1c1c7232d55a93d977dda5cbf6c8efb516966b7 Mon Sep 17 00:00:00 2001 From: Solessfir <34831419+Solessfir@users.noreply.github.com> Date: Wed, 11 Nov 2026 21:38:12 +0100 Subject: [PATCH 2/2] Identify the Android window by the activity object --- Engine/gapi/vulkan/vswapchain.cpp | 19 ++++++++++++------- Engine/gapi/vulkan/vswapchain.h | 5 ++++- Engine/system/api/androidapi.cpp | 7 ++++--- Engine/system/api/androidapi.h | 2 +- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/Engine/gapi/vulkan/vswapchain.cpp b/Engine/gapi/vulkan/vswapchain.cpp index b52023ef..4bfe8c2b 100644 --- a/Engine/gapi/vulkan/vswapchain.cpp +++ b/Engine/gapi/vulkan/vswapchain.cpp @@ -39,6 +39,12 @@ using namespace Tempest; using namespace Tempest::Detail; +#if defined(__ANDROID__) +void VSwapchain::WindowRelease::operator()(ANativeWindow* w) const noexcept { + ANativeWindow_release(w); + } +#endif + static VkResult VKAPI_CALL vkxRevertFence(VkDevice device, VkFence* pFence) { // HACK: revert fence state to signaled VkFenceCreateInfo fenceInfo = {}; @@ -328,9 +334,7 @@ void VSwapchain::cleanupSurface() noexcept { vkDestroySurfaceKHR(device.instance,surface,nullptr); surface = VK_NULL_HANDLE; #if defined(__ANDROID__) - if(window!=nullptr) - ANativeWindow_release(window); - window = nullptr; + window.reset(); #endif } @@ -349,7 +353,7 @@ VkSurfaceKHR VSwapchain::createSurface(VkInstance instance, void* hwnd) { return VK_NULL_HANDLE; #if defined(__ANDROID__) // NativeActivity gives a new window after the activity was hidden - if(window!=AndroidApi::nativeWindow(this->hwnd)) + if(window.get()!=AndroidApi::nativeWindow(this->hwnd)) cleanupSurface(); #endif if(surface!=VK_NULL_HANDLE) @@ -359,8 +363,9 @@ VkSurfaceKHR VSwapchain::createSurface(VkInstance instance, void* hwnd) { throw std::system_error(Tempest::GraphicsErrc::NoDevice); #if defined(__ANDROID__) // The reference keeps the old window alive, so its replacement can't get the same address - window = AndroidApi::nativeWindow(this->hwnd); - ANativeWindow_acquire(window); + ANativeWindow* current = AndroidApi::nativeWindow(this->hwnd); + ANativeWindow_acquire(current); + window.reset(current); #endif return ret; } @@ -554,7 +559,7 @@ AbstractGraphicsApi::Texture* VSwapchain::currentBackBuffer() { if(currentSyncState == SyncState::S_Idle) { #if defined(__ANDROID__) // Drivers may keep presenting to a window Android has already replaced - if(window!=AndroidApi::nativeWindow(hwnd)) + if(window.get()!=AndroidApi::nativeWindow(hwnd)) throw SwapchainSuboptimal(); #endif const VkResult code = implAcquireNextImage(); diff --git a/Engine/gapi/vulkan/vswapchain.h b/Engine/gapi/vulkan/vswapchain.h index 8971efd5..a1bbda19 100644 --- a/Engine/gapi/vulkan/vswapchain.h +++ b/Engine/gapi/vulkan/vswapchain.h @@ -106,7 +106,10 @@ class VSwapchain : public AbstractGraphicsApi::Swapchain { VkSurfaceKHR surface = VK_NULL_HANDLE; VkSwapchainKHR swapChain = VK_NULL_HANDLE; #if defined(__ANDROID__) - ANativeWindow* window = nullptr; + struct WindowRelease { + void operator()(ANativeWindow* w) const noexcept; + }; + std::unique_ptr window; #endif SpinLock sync; diff --git a/Engine/system/api/androidapi.cpp b/Engine/system/api/androidapi.cpp index 6ca18744..4e9dd8d6 100644 --- a/Engine/system/api/androidapi.cpp +++ b/Engine/system/api/androidapi.cpp @@ -261,8 +261,8 @@ SystemApi::Window* AndroidApi::createAndroidWindow(Tempest::Window* owner) { if(isExit.load() || app->destroyRequested!=0) return nullptr; mainWindow = owner; - // NativeActivity replaces its ANativeWindow while the activity lives; the activity identifies the window - return reinterpret_cast(app->activity); + // NativeActivity replaces its ANativeWindow while the activity lives; the activity object identifies the window + return reinterpret_cast(app->activity->clazz); } SystemApi::Window* AndroidApi::implCreateWindow(Tempest::Window* owner, uint32_t, uint32_t) { @@ -284,7 +284,8 @@ void AndroidApi::implExit() { } ANativeWindow* AndroidApi::nativeWindow(SystemApi::Window* w) { - assert(reinterpret_cast(w)==app->activity); + if(reinterpret_cast(w)!=app->activity->clazz) + return nullptr; return app->window; } diff --git a/Engine/system/api/androidapi.h b/Engine/system/api/androidapi.h index af8adf1d..d689cddb 100644 --- a/Engine/system/api/androidapi.h +++ b/Engine/system/api/androidapi.h @@ -11,7 +11,7 @@ class AndroidApi final : SystemApi { public: static std::filesystem::path internalDataPath(); static std::filesystem::path externalDataPath(); - // Current window of the activity, or null while the activity is hidden + // Current window of the activity, or null while it is hidden or for another activity static ANativeWindow* nativeWindow(SystemApi::Window* w); private: