Code cleanup, return and exit conditions for Android

This commit is contained in:
saschawillems 2018-05-01 11:23:36 +02:00
parent 5056563f5d
commit 5fe9f91529
4 changed files with 29 additions and 13 deletions

View file

@ -15,8 +15,6 @@ import java.util.concurrent.Semaphore;
public class VulkanActivity extends NativeActivity { public class VulkanActivity extends NativeActivity {
VulkanActivity _activity;
static { static {
// Load native library // Load native library
System.loadLibrary("native-lib"); System.loadLibrary("native-lib");
@ -24,7 +22,6 @@ public class VulkanActivity extends NativeActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
_activity = this;
} }
// Use a semaphore to create a modal dialog // Use a semaphore to create a modal dialog
@ -33,12 +30,14 @@ public class VulkanActivity extends NativeActivity {
public void showAlert(final String message) public void showAlert(final String message)
{ {
ApplicationInfo applicationInfo = _activity.getApplicationInfo(); final VulkanActivity activity = this;
ApplicationInfo applicationInfo = activity.getApplicationInfo();
final String applicationName = applicationInfo.nonLocalizedLabel.toString(); final String applicationName = applicationInfo.nonLocalizedLabel.toString();
this.runOnUiThread(new Runnable() { this.runOnUiThread(new Runnable() {
public void run() { 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.setTitle(applicationName);
builder.setMessage(message); builder.setMessage(message);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() { builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {

View file

@ -273,7 +273,9 @@ namespace vks
vks::android::showAlert(message.c_str()); vks::android::showAlert(message.c_str());
#endif #endif
std::cerr << message << "\n"; std::cerr << message << "\n";
#if !defined(__ANDROID__)
exit(exitCode); exit(exitCode);
#endif
} }
void exitFatal(std::string message, VkResult resultCode) void exitFatal(std::string message, VkResult resultCode)

View file

@ -333,6 +333,7 @@ void VulkanExampleBase::renderLoop()
// Exit loop, example will be destroyed in application main // Exit loop, example will be destroyed in application main
if (destroy) if (destroy)
{ {
ANativeActivity_finish(androidApp->activity);
break; break;
} }
@ -556,8 +557,10 @@ void VulkanExampleBase::renderLoop()
} }
#endif #endif
// Flush device to make sure all resources can be freed // Flush device to make sure all resources can be freed
if (device != VK_NULL_HANDLE) {
vkDeviceWaitIdle(device); vkDeviceWaitIdle(device);
} }
}
void VulkanExampleBase::updateOverlay() void VulkanExampleBase::updateOverlay()
{ {
@ -830,7 +833,7 @@ VulkanExampleBase::~VulkanExampleBase()
#endif #endif
} }
void VulkanExampleBase::initVulkan() bool VulkanExampleBase::initVulkan()
{ {
VkResult err; VkResult err;
@ -838,6 +841,7 @@ void VulkanExampleBase::initVulkan()
err = createInstance(settings.validation); err = createInstance(settings.validation);
if (err) { if (err) {
vks::tools::exitFatal("Could not create Vulkan instance : \n" + vks::tools::errorString(err), err); vks::tools::exitFatal("Could not create Vulkan instance : \n" + vks::tools::errorString(err), err);
return false;
} }
#if defined(VK_USE_PLATFORM_ANDROID_KHR) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
@ -864,6 +868,7 @@ void VulkanExampleBase::initVulkan()
err = vkEnumeratePhysicalDevices(instance, &gpuCount, physicalDevices.data()); err = vkEnumeratePhysicalDevices(instance, &gpuCount, physicalDevices.data());
if (err) { if (err) {
vks::tools::exitFatal("Could not enumerate physical devices : \n" + vks::tools::errorString(err), err); vks::tools::exitFatal("Could not enumerate physical devices : \n" + vks::tools::errorString(err), err);
return false;
} }
// GPU selection // GPU selection
@ -939,6 +944,7 @@ void VulkanExampleBase::initVulkan()
VkResult res = vulkanDevice->createLogicalDevice(enabledFeatures, enabledDeviceExtensions); VkResult res = vulkanDevice->createLogicalDevice(enabledFeatures, enabledDeviceExtensions);
if (res != VK_SUCCESS) { if (res != VK_SUCCESS) {
vks::tools::exitFatal("Could not create Vulkan device: \n" + vks::tools::errorString(res), res); vks::tools::exitFatal("Could not create Vulkan device: \n" + vks::tools::errorString(res), res);
return false;
} }
device = vulkanDevice->logicalDevice; device = vulkanDevice->logicalDevice;
@ -988,6 +994,8 @@ void VulkanExampleBase::initVulkan()
}; };
LOGD("androidProduct = %s", androidProduct.c_str()); LOGD("androidProduct = %s", androidProduct.c_str());
#endif #endif
return true;
} }
#if defined(_WIN32) #if defined(_WIN32)
@ -1399,10 +1407,15 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
LOGD("APP_CMD_INIT_WINDOW"); LOGD("APP_CMD_INIT_WINDOW");
if (androidApp->window != NULL) if (androidApp->window != NULL)
{ {
vulkanExample->initVulkan(); if (vulkanExample->initVulkan()) {
vulkanExample->prepare(); vulkanExample->prepare();
assert(vulkanExample->prepared); assert(vulkanExample->prepared);
} }
else {
LOGE("Could not initialize Vulkan, exiting!");
androidApp->destroyRequested = 1;
}
}
else else
{ {
LOGE("No window assigned!"); LOGE("No window assigned!");
@ -1419,7 +1432,9 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
case APP_CMD_TERM_WINDOW: case APP_CMD_TERM_WINDOW:
// Window is hidden or closed, clean up resources // Window is hidden or closed, clean up resources
LOGD("APP_CMD_TERM_WINDOW"); LOGD("APP_CMD_TERM_WINDOW");
if (vulkanExample->prepared) {
vulkanExample->swapChain.cleanup(); vulkanExample->swapChain.cleanup();
}
break; break;
} }
} }

View file

@ -250,7 +250,7 @@ public:
virtual ~VulkanExampleBase(); virtual ~VulkanExampleBase();
// Setup the vulkan instance, enable required extensions and connect to the physical device (GPU) // Setup the vulkan instance, enable required extensions and connect to the physical device (GPU)
void initVulkan(); bool initVulkan();
#if defined(_WIN32) #if defined(_WIN32)
void setupConsole(std::string title); void setupConsole(std::string title);