Android fixes

This commit is contained in:
luboslenco
2025-08-15 09:47:57 +02:00
parent eaf26aaba5
commit 01c19da29f
2 changed files with 77 additions and 81 deletions
+39 -41
View File
@@ -11,7 +11,7 @@
#include <iron_file.h> #include <iron_file.h>
#include <iron_thread.h> #include <iron_thread.h>
#include <iron_video.h> #include <iron_video.h>
#include <android/sensor.h> // #include <android/sensor.h>
#include <android/window.h> #include <android/window.h>
#include "android_native_app_glue.h" #include "android_native_app_glue.h"
#include <vulkan/vulkan_android.h> #include <vulkan/vulkan_android.h>
@@ -30,10 +30,10 @@ typedef struct {
static iron_display_t display; static iron_display_t display;
static struct android_app *app = NULL; static struct android_app *app = NULL;
static ANativeActivity *activity = NULL; static ANativeActivity *activity = NULL;
static ASensorManager *sensorManager = NULL; // static ASensorManager *sensorManager = NULL;
static const ASensor *accelerometerSensor = NULL; // static const ASensor *accelerometerSensor = NULL;
static const ASensor *gyroSensor = NULL; // static const ASensor *gyroSensor = NULL;
static ASensorEventQueue *sensorEventQueue = NULL; // static ASensorEventQueue *sensorEventQueue = NULL;
static bool started = false; static bool started = false;
static bool paused = true; static bool paused = true;
static bool displayIsInitialized = false; static bool displayIsInitialized = false;
@@ -58,8 +58,8 @@ static bool last_hat_up = false;
static bool last_hat_down = false; static bool last_hat_down = false;
#endif #endif
void iron_vulkan_init_window(); void iron_vulkan_surface_destroyed();
bool iron_vulkan_internal_get_size(int *width, int *height); bool iron_vulkan_get_size(int *width, int *height);
int iron_count_displays(void) { int iron_count_displays(void) {
return 1; return 1;
@@ -802,9 +802,8 @@ static void cmd(struct android_app *app, int32_t cmd) {
started = true; started = true;
} }
else { else {
iron_vulkan_init_window(); iron_vulkan_surface_destroyed();
} }
updateAppForegroundStatus(true, appIsForeground); updateAppForegroundStatus(true, appIsForeground);
} }
break; break;
@@ -812,22 +811,22 @@ static void cmd(struct android_app *app, int32_t cmd) {
updateAppForegroundStatus(false, appIsForeground); updateAppForegroundStatus(false, appIsForeground);
break; break;
case APP_CMD_GAINED_FOCUS: case APP_CMD_GAINED_FOCUS:
if (accelerometerSensor != NULL) { // if (accelerometerSensor != NULL) {
ASensorEventQueue_enableSensor(sensorEventQueue, accelerometerSensor); // ASensorEventQueue_enableSensor(sensorEventQueue, accelerometerSensor);
ASensorEventQueue_setEventRate(sensorEventQueue, accelerometerSensor, (1000L / 60) * 1000); // ASensorEventQueue_setEventRate(sensorEventQueue, accelerometerSensor, (1000L / 60) * 1000);
} // }
if (gyroSensor != NULL) { // if (gyroSensor != NULL) {
ASensorEventQueue_enableSensor(sensorEventQueue, gyroSensor); // ASensorEventQueue_enableSensor(sensorEventQueue, gyroSensor);
ASensorEventQueue_setEventRate(sensorEventQueue, gyroSensor, (1000L / 60) * 1000); // ASensorEventQueue_setEventRate(sensorEventQueue, gyroSensor, (1000L / 60) * 1000);
} // }
break; break;
case APP_CMD_LOST_FOCUS: case APP_CMD_LOST_FOCUS:
if (accelerometerSensor != NULL) { // if (accelerometerSensor != NULL) {
ASensorEventQueue_disableSensor(sensorEventQueue, accelerometerSensor); // ASensorEventQueue_disableSensor(sensorEventQueue, accelerometerSensor);
} // }
if (gyroSensor != NULL) { // if (gyroSensor != NULL) {
ASensorEventQueue_disableSensor(sensorEventQueue, gyroSensor); // ASensorEventQueue_disableSensor(sensorEventQueue, gyroSensor);
} // }
break; break;
case APP_CMD_START: case APP_CMD_START:
updateAppForegroundStatus(displayIsInitialized, true); updateAppForegroundStatus(displayIsInitialized, true);
@@ -847,7 +846,6 @@ static void cmd(struct android_app *app, int32_t cmd) {
iron_internal_shutdown_callback(); iron_internal_shutdown_callback();
break; break;
case APP_CMD_CONFIG_CHANGED: { case APP_CMD_CONFIG_CHANGED: {
break; break;
} }
} }
@@ -948,7 +946,7 @@ const char *iron_language() {
int iron_android_width() { int iron_android_width() {
int width, height; int width, height;
if (iron_vulkan_internal_get_size(&width, &height)) { if (iron_vulkan_get_size(&width, &height)) {
return width; return width;
} }
return ANativeWindow_getWidth(app->window); return ANativeWindow_getWidth(app->window);
@@ -956,7 +954,7 @@ int iron_android_width() {
int iron_android_height() { int iron_android_height() {
int width, height; int width, height;
if (iron_vulkan_internal_get_size(&width, &height)) { if (iron_vulkan_get_size(&width, &height)) {
return height; return height;
} }
return ANativeWindow_getHeight(app->window); return ANativeWindow_getHeight(app->window);
@@ -1017,17 +1015,17 @@ bool iron_internal_handle_messages(void) {
} }
if (ident == LOOPER_ID_USER) { if (ident == LOOPER_ID_USER) {
if (accelerometerSensor != NULL) { // if (accelerometerSensor != NULL) {
ASensorEvent event; // ASensorEvent event;
while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) { // while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0) {
if (event.type == ASENSOR_TYPE_ACCELEROMETER) { // if (event.type == ASENSOR_TYPE_ACCELEROMETER) {
// iron_internal_on_acceleration(event.acceleration.x, event.acceleration.y, event.acceleration.z); // // iron_internal_on_acceleration(event.acceleration.x, event.acceleration.y, event.acceleration.z);
} // }
else if (event.type == ASENSOR_TYPE_GYROSCOPE) { // else if (event.type == ASENSOR_TYPE_GYROSCOPE) {
// iron_internal_on_rotation(event.vector.x, event.vector.y, event.vector.z); // // iron_internal_on_rotation(event.vector.x, event.vector.y, event.vector.z);
} // }
} // }
} // }
} }
if (app->destroyRequested != 0) { if (app->destroyRequested != 0) {
@@ -1086,10 +1084,10 @@ void android_main(struct android_app *application) {
application->onAppCmd = cmd; application->onAppCmd = cmd;
application->onInputEvent = input; application->onInputEvent = input;
activity->callbacks->onNativeWindowResized = resize; activity->callbacks->onNativeWindowResized = resize;
sensorManager = ASensorManager_getInstance(); // sensorManager = ASensorManager_getInstance();
accelerometerSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER); // accelerometerSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);
gyroSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_GYROSCOPE); // gyroSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_GYROSCOPE);
sensorEventQueue = ASensorManager_createEventQueue(sensorManager, application->looper, LOOPER_ID_USER, NULL, NULL); // sensorEventQueue = ASensorManager_createEventQueue(sensorManager, application->looper, LOOPER_ID_USER, NULL, NULL);
JNIEnv *env = NULL; JNIEnv *env = NULL;
(*iron_android_get_activity()->vm)->AttachCurrentThread(iron_android_get_activity()->vm, &env, NULL); (*iron_android_get_activity()->vm)->AttachCurrentThread(iron_android_get_activity()->vm, &env, NULL);
+38 -40
View File
@@ -54,12 +54,12 @@ static bool validation_found;
static VkDebugUtilsMessengerEXT debug_messenger; static VkDebugUtilsMessengerEXT debug_messenger;
#endif #endif
static bool window_surface_destroyed; static bool surface_destroyed;
static int window_depth_bits; static int window_depth_bits;
static bool window_vsynced; static bool window_vsync;
static VkSurfaceKHR window_surface; static VkSurfaceKHR surface;
static VkSurfaceFormatKHR window_format; static VkSurfaceFormatKHR surface_format;
static VkSwapchainKHR window_swapchain; static VkSwapchainKHR swapchain;
static VkImage window_images[GPU_FRAMEBUFFER_COUNT]; static VkImage window_images[GPU_FRAMEBUFFER_COUNT];
static uint32_t framebuffer_count; static uint32_t framebuffer_count;
static bool framebuffer_acquired = false; static bool framebuffer_acquired = false;
@@ -376,8 +376,8 @@ VkSwapchainKHR cleanup_swapchain() {
// for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) { // for (int i = 0; i < GPU_FRAMEBUFFER_COUNT; ++i) {
// gpu_texture_destroy_internal(&framebuffers[i]); // gpu_texture_destroy_internal(&framebuffers[i]);
// } // }
VkSwapchainKHR chain = window_swapchain; VkSwapchainKHR chain = swapchain;
window_swapchain = VK_NULL_HANDLE; swapchain = VK_NULL_HANDLE;
return chain; return chain;
} }
@@ -445,22 +445,22 @@ void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_t
static void create_swapchain() { static void create_swapchain() {
VkSwapchainKHR old_swapchain = cleanup_swapchain(); VkSwapchainKHR old_swapchain = cleanup_swapchain();
if (window_surface_destroyed) { if (surface_destroyed) {
vkDestroySwapchainKHR(device, old_swapchain, NULL); vkDestroySwapchainKHR(device, old_swapchain, NULL);
old_swapchain = VK_NULL_HANDLE; old_swapchain = VK_NULL_HANDLE;
vkDestroySurfaceKHR(instance, window_surface, NULL); vkDestroySurfaceKHR(instance, surface, NULL);
iron_vulkan_create_surface(instance, &window_surface); iron_vulkan_create_surface(instance, &surface);
window_surface_destroyed = false; surface_destroyed = false;
} }
VkSurfaceCapabilitiesKHR caps = {0}; VkSurfaceCapabilitiesKHR caps = {0};
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, window_surface, &caps); vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, surface, &caps);
VkPresentModeKHR present_modes[256]; VkPresentModeKHR present_modes[256];
uint32_t present_mode_count; uint32_t present_mode_count;
vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, window_surface, &present_mode_count, NULL); vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &present_mode_count, NULL);
present_mode_count = present_mode_count > 256 ? 256 : present_mode_count; present_mode_count = present_mode_count > 256 ? 256 : present_mode_count;
vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, window_surface, &present_mode_count, present_modes); vkGetPhysicalDeviceSurfacePresentModesKHR(gpu, surface, &present_mode_count, present_modes);
uint32_t image_count = GPU_FRAMEBUFFER_COUNT; uint32_t image_count = GPU_FRAMEBUFFER_COUNT;
if (image_count < caps.minImageCount) { if (image_count < caps.minImageCount) {
@@ -483,10 +483,10 @@ static void create_swapchain() {
VkSwapchainCreateInfoKHR swapchain_info = { VkSwapchainCreateInfoKHR swapchain_info = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
.surface = window_surface, .surface = surface,
.minImageCount = image_count, .minImageCount = image_count,
.imageFormat = window_format.format, .imageFormat = surface_format.format,
.imageColorSpace = window_format.colorSpace, .imageColorSpace = surface_format.colorSpace,
.imageExtent.width = iron_window_width(), .imageExtent.width = iron_window_width(),
.imageExtent.height = iron_window_height(), .imageExtent.height = iron_window_height(),
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
@@ -510,11 +510,11 @@ static void create_swapchain() {
swapchain_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; swapchain_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_info.queueFamilyIndexCount = 0; swapchain_info.queueFamilyIndexCount = 0;
swapchain_info.pQueueFamilyIndices = NULL; swapchain_info.pQueueFamilyIndices = NULL;
swapchain_info.presentMode = window_vsynced ? VK_PRESENT_MODE_FIFO_KHR : VK_PRESENT_MODE_MAILBOX_KHR; swapchain_info.presentMode = window_vsync ? VK_PRESENT_MODE_FIFO_KHR : VK_PRESENT_MODE_MAILBOX_KHR;
swapchain_info.oldSwapchain = old_swapchain; swapchain_info.oldSwapchain = old_swapchain;
swapchain_info.clipped = true; swapchain_info.clipped = true;
vkCreateSwapchainKHR(device, &swapchain_info, NULL, &window_swapchain); vkCreateSwapchainKHR(device, &swapchain_info, NULL, &swapchain);
if (old_swapchain != VK_NULL_HANDLE) { if (old_swapchain != VK_NULL_HANDLE) {
gpu_execute_and_wait(); gpu_execute_and_wait();
@@ -522,14 +522,14 @@ static void create_swapchain() {
} }
int framebuffer_count = GPU_FRAMEBUFFER_COUNT; int framebuffer_count = GPU_FRAMEBUFFER_COUNT;
vkGetSwapchainImagesKHR(device, window_swapchain, &framebuffer_count, window_images); vkGetSwapchainImagesKHR(device, swapchain, &framebuffer_count, window_images);
for (uint32_t i = 0; i < framebuffer_count; i++) { for (uint32_t i = 0; i < framebuffer_count; i++) {
framebuffers[i].impl.image = window_images[i]; framebuffers[i].impl.image = window_images[i];
set_image_layout(window_images[i], VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR); set_image_layout(window_images[i], VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
VkImageViewCreateInfo color_attachment_view = { VkImageViewCreateInfo color_attachment_view = {
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
.format = window_format.format, .format = surface_format.format,
.components.r = VK_COMPONENT_SWIZZLE_R, .components.r = VK_COMPONENT_SWIZZLE_R,
.components.g = VK_COMPONENT_SWIZZLE_G, .components.g = VK_COMPONENT_SWIZZLE_G,
.components.b = VK_COMPONENT_SWIZZLE_B, .components.b = VK_COMPONENT_SWIZZLE_B,
@@ -593,9 +593,9 @@ static void create_swapchain() {
} }
static void acquire_next_image() { static void acquire_next_image() {
VkResult err = vkAcquireNextImageKHR(device, window_swapchain, UINT64_MAX, framebuffer_available_semaphore, VK_NULL_HANDLE, &framebuffer_index); VkResult err = vkAcquireNextImageKHR(device, swapchain, UINT64_MAX, framebuffer_available_semaphore, VK_NULL_HANDLE, &framebuffer_index);
if (err == VK_ERROR_SURFACE_LOST_KHR || err == VK_ERROR_OUT_OF_DATE_KHR) { if (err == VK_ERROR_SURFACE_LOST_KHR || err == VK_ERROR_OUT_OF_DATE_KHR || surface_destroyed) {
window_surface_destroyed = (err == VK_ERROR_SURFACE_LOST_KHR); surface_destroyed = surface_destroyed || (err == VK_ERROR_SURFACE_LOST_KHR);
gpu_in_use = false; gpu_in_use = false;
create_swapchain(); create_swapchain();
@@ -939,31 +939,31 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) {
} }
window_depth_bits = depth_buffer_bits; window_depth_bits = depth_buffer_bits;
window_vsynced = vsync; window_vsync = vsync;
iron_vulkan_create_surface(instance, &window_surface); iron_vulkan_create_surface(instance, &surface);
VkBool32 surface_supported; VkBool32 surface_supported;
vkGetPhysicalDeviceSurfaceSupportKHR(gpu, graphics_queue_node_index, window_surface, &surface_supported); vkGetPhysicalDeviceSurfaceSupportKHR(gpu, graphics_queue_node_index, surface, &surface_supported);
VkSurfaceFormatKHR surf_formats[256]; VkSurfaceFormatKHR surf_formats[256];
uint32_t formatCount = sizeof(surf_formats) / sizeof(surf_formats[0]); uint32_t formatCount = sizeof(surf_formats) / sizeof(surf_formats[0]);
VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, window_surface, &formatCount, surf_formats); VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &formatCount, surf_formats);
if (formatCount == 1 && surf_formats[0].format == VK_FORMAT_UNDEFINED) { if (formatCount == 1 && surf_formats[0].format == VK_FORMAT_UNDEFINED) {
window_format = surf_formats[0]; surface_format = surf_formats[0];
} }
else { else {
bool found = false; bool found = false;
for (uint32_t i = 0; i < formatCount; ++i) { for (uint32_t i = 0; i < formatCount; ++i) {
if (surf_formats[i].format != VK_FORMAT_B8G8R8A8_SRGB) { if (surf_formats[i].format != VK_FORMAT_B8G8R8A8_SRGB) {
window_format = surf_formats[i]; surface_format = surf_formats[i];
found = true; found = true;
break; break;
} }
} }
if (!found) { if (!found) {
window_format = surf_formats[0]; surface_format = surf_formats[0];
} }
} }
@@ -1000,19 +1000,17 @@ void gpu_destroy() {
vkDestroyFence(device, fence, NULL); vkDestroyFence(device, fence, NULL);
VkSwapchainKHR swapchain = cleanup_swapchain(); VkSwapchainKHR swapchain = cleanup_swapchain();
vkDestroySwapchainKHR(device, swapchain, NULL); vkDestroySwapchainKHR(device, swapchain, NULL);
vkDestroySurfaceKHR(instance, window_surface, NULL); vkDestroySurfaceKHR(instance, surface, NULL);
} }
void iron_vulkan_init_window() { void iron_vulkan_surface_destroyed() {
// This function is used in the android backend surface_destroyed = true;
window_surface_destroyed = true;
} }
bool iron_vulkan_internal_get_size(int *width, int *height) { bool iron_vulkan_get_size(int *width, int *height) {
// This function is used in the android backend if (surface) {
if (window_surface) {
VkSurfaceCapabilitiesKHR capabilities; VkSurfaceCapabilitiesKHR capabilities;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, window_surface, &capabilities); vkGetPhysicalDeviceSurfaceCapabilitiesKHR(gpu, surface, &capabilities);
*width = capabilities.currentExtent.width; *width = capabilities.currentExtent.width;
*height = capabilities.currentExtent.height; *height = capabilities.currentExtent.height;
return true; return true;
@@ -1186,7 +1184,7 @@ void gpu_present_internal() {
VkPresentInfoKHR present = { VkPresentInfoKHR present = {
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.swapchainCount = 1, .swapchainCount = 1,
.pSwapchains = &window_swapchain, .pSwapchains = &swapchain,
.pImageIndices = &framebuffer_index, .pImageIndices = &framebuffer_index,
.pWaitSemaphores = &rendering_finished_semaphores[framebuffer_index], .pWaitSemaphores = &rendering_finished_semaphores[framebuffer_index],
.waitSemaphoreCount = 1, .waitSemaphoreCount = 1,