diff --git a/Engine/gapi/vulkan/vswapchain.cpp b/Engine/gapi/vulkan/vswapchain.cpp index db677e07..4bfe8c2b 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 @@ -37,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 = {}; @@ -273,7 +281,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 +333,9 @@ void VSwapchain::cleanupSurface() noexcept { if(surface!=VK_NULL_HANDLE) vkDestroySurfaceKHR(device.instance,surface,nullptr); surface = VK_NULL_HANDLE; +#if defined(__ANDROID__) + window.reset(); +#endif } void VSwapchain::reset() { @@ -340,11 +351,22 @@ 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.get()!=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 + ANativeWindow* current = AndroidApi::nativeWindow(this->hwnd); + ANativeWindow_acquire(current); + window.reset(current); +#endif return ret; } @@ -535,6 +557,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.get()!=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..a1bbda19 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,12 @@ class VSwapchain : public AbstractGraphicsApi::Swapchain { SystemApi::Window* hwnd = nullptr; VkSurfaceKHR surface = VK_NULL_HANDLE; VkSwapchainKHR swapChain = VK_NULL_HANDLE; +#if defined(__ANDROID__) + struct WindowRelease { + void operator()(ANativeWindow* w) const noexcept; + }; + std::unique_ptr window; +#endif SpinLock sync; uint32_t imgIndex = 0; diff --git a/Engine/system/api/androidapi.cpp b/Engine/system/api/androidapi.cpp index 96b78295..4e9dd8d6 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 object identifies the window + return reinterpret_cast(app->activity->clazz); } 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,16 @@ void AndroidApi::implExit() { ALooper_wake(app->looper); } +ANativeWindow* AndroidApi::nativeWindow(SystemApi::Window* w) { + if(reinterpret_cast(w)!=app->activity->clazz) + return nullptr; + 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..d689cddb 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 it is hidden or for another activity + static ANativeWindow* nativeWindow(SystemApi::Window* w); private: AndroidApi() = default;