diff --git a/android/examples/triangle/src/main/java/de/saschawillems/vulkanSample/VulkanActivity.java b/android/examples/triangle/src/main/java/de/saschawillems/vulkanSample/VulkanActivity.java index a13088db..b8825637 100644 --- a/android/examples/triangle/src/main/java/de/saschawillems/vulkanSample/VulkanActivity.java +++ b/android/examples/triangle/src/main/java/de/saschawillems/vulkanSample/VulkanActivity.java @@ -15,8 +15,6 @@ import java.util.concurrent.Semaphore; public class VulkanActivity extends NativeActivity { - VulkanActivity _activity; - static { // Load native library System.loadLibrary("native-lib"); @@ -24,7 +22,6 @@ public class VulkanActivity extends NativeActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - _activity = this; } // Use a semaphore to create a modal dialog @@ -33,12 +30,14 @@ public class VulkanActivity extends NativeActivity { public void showAlert(final String message) { - ApplicationInfo applicationInfo = _activity.getApplicationInfo(); + final VulkanActivity activity = this; + + ApplicationInfo applicationInfo = activity.getApplicationInfo(); final String applicationName = applicationInfo.nonLocalizedLabel.toString(); this.runOnUiThread(new Runnable() { public void run() { - AlertDialog.Builder builder = new AlertDialog.Builder(_activity, AlertDialog.THEME_HOLO_DARK); + AlertDialog.Builder builder = new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_DARK); builder.setTitle(applicationName); builder.setMessage(message); builder.setPositiveButton("Close", new DialogInterface.OnClickListener() { diff --git a/base/VulkanTools.cpp b/base/VulkanTools.cpp index 3dd28bbf..8d43d660 100644 --- a/base/VulkanTools.cpp +++ b/base/VulkanTools.cpp @@ -273,7 +273,9 @@ namespace vks vks::android::showAlert(message.c_str()); #endif std::cerr << message << "\n"; +#if !defined(__ANDROID__) exit(exitCode); +#endif } void exitFatal(std::string message, VkResult resultCode) diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index f12e533a..bb1adfa0 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -333,6 +333,7 @@ void VulkanExampleBase::renderLoop() // Exit loop, example will be destroyed in application main if (destroy) { + ANativeActivity_finish(androidApp->activity); break; } @@ -555,8 +556,10 @@ void VulkanExampleBase::renderLoop() updateOverlay(); } #endif - // Flush device to make sure all resources can be freed - vkDeviceWaitIdle(device); + // Flush device to make sure all resources can be freed + if (device != VK_NULL_HANDLE) { + vkDeviceWaitIdle(device); + } } void VulkanExampleBase::updateOverlay() @@ -830,7 +833,7 @@ VulkanExampleBase::~VulkanExampleBase() #endif } -void VulkanExampleBase::initVulkan() +bool VulkanExampleBase::initVulkan() { VkResult err; @@ -838,6 +841,7 @@ void VulkanExampleBase::initVulkan() err = createInstance(settings.validation); if (err) { vks::tools::exitFatal("Could not create Vulkan instance : \n" + vks::tools::errorString(err), err); + return false; } #if defined(VK_USE_PLATFORM_ANDROID_KHR) @@ -864,6 +868,7 @@ void VulkanExampleBase::initVulkan() err = vkEnumeratePhysicalDevices(instance, &gpuCount, physicalDevices.data()); if (err) { vks::tools::exitFatal("Could not enumerate physical devices : \n" + vks::tools::errorString(err), err); + return false; } // GPU selection @@ -939,6 +944,7 @@ void VulkanExampleBase::initVulkan() VkResult res = vulkanDevice->createLogicalDevice(enabledFeatures, enabledDeviceExtensions); if (res != VK_SUCCESS) { vks::tools::exitFatal("Could not create Vulkan device: \n" + vks::tools::errorString(res), res); + return false; } device = vulkanDevice->logicalDevice; @@ -988,6 +994,8 @@ void VulkanExampleBase::initVulkan() }; LOGD("androidProduct = %s", androidProduct.c_str()); #endif + + return true; } #if defined(_WIN32) @@ -1399,9 +1407,14 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd) LOGD("APP_CMD_INIT_WINDOW"); if (androidApp->window != NULL) { - vulkanExample->initVulkan(); - vulkanExample->prepare(); - assert(vulkanExample->prepared); + if (vulkanExample->initVulkan()) { + vulkanExample->prepare(); + assert(vulkanExample->prepared); + } + else { + LOGE("Could not initialize Vulkan, exiting!"); + androidApp->destroyRequested = 1; + } } else { @@ -1419,7 +1432,9 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd) case APP_CMD_TERM_WINDOW: // Window is hidden or closed, clean up resources LOGD("APP_CMD_TERM_WINDOW"); - vulkanExample->swapChain.cleanup(); + if (vulkanExample->prepared) { + vulkanExample->swapChain.cleanup(); + } break; } } diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index cd157389..76e8c464 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -250,7 +250,7 @@ public: virtual ~VulkanExampleBase(); // Setup the vulkan instance, enable required extensions and connect to the physical device (GPU) - void initVulkan(); + bool initVulkan(); #if defined(_WIN32) void setupConsole(std::string title);