Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions Engine/gapi/vulkan/vswapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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<ANativeWindow*>(hwnd);
createInfo.window = AndroidApi::nativeWindow(reinterpret_cast<SystemApi::Window*>(hwnd));
return vkCreateAndroidSurfaceKHR(instance,&createInfo,nullptr,pSurface);
#elif defined(__UNIX__)
#if defined(TEMPEST_BUILD_WAYLAND)
Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down
8 changes: 8 additions & 0 deletions Engine/gapi/vulkan/vswapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "gapi/vulkan/vtexture.h"
#include "vulkan_sdk.h"

struct ANativeWindow;

namespace Tempest {

namespace Detail {
Expand Down Expand Up @@ -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<ANativeWindow,WindowRelease> window;
#endif

SpinLock sync;
uint32_t imgIndex = 0;
Expand Down
20 changes: 15 additions & 5 deletions Engine/system/api/androidapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<SystemApi::Window*>(app->window);
// NativeActivity replaces its ANativeWindow while the activity lives; the activity object identifies the window
return reinterpret_cast<SystemApi::Window*>(app->activity->clazz);
}

SystemApi::Window* AndroidApi::implCreateWindow(Tempest::Window* owner, uint32_t, uint32_t) {
Expand All @@ -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<ANativeWindow*>(w));
void AndroidApi::implDestroyWindow(SystemApi::Window*) {
mainWindow = nullptr;
touches.clear();
}
Expand All @@ -281,8 +283,16 @@ void AndroidApi::implExit() {
ALooper_wake(app->looper);
}

ANativeWindow* AndroidApi::nativeWindow(SystemApi::Window* w) {
if(reinterpret_cast<jobject>(w)!=app->activity->clazz)
return nullptr;
return app->window;
}

Rect AndroidApi::implWindowClientRect(SystemApi::Window* w) {
const auto window = reinterpret_cast<ANativeWindow*>(w);
const auto window = nativeWindow(w);
if(window==nullptr)
return Rect();
return Rect(0,0,ANativeWindow_getWidth(window),ANativeWindow_getHeight(window));
}

Expand Down
4 changes: 4 additions & 0 deletions Engine/system/api/androidapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
#include <Tempest/SystemApi>
#include <filesystem>

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;
Expand Down