21 #include "android.hpp"    36         return STATUS_RUNNING;
    39 VkSurfaceKHR AndroidPlatform::createSurface()
    42         PFN_vkCreateAndroidSurfaceKHR fpCreateAndroidSurfaceKHR;
    43         if (!VULKAN_SYMBOL_WRAPPER_LOAD_INSTANCE_SYMBOL(instance, 
"vkCreateAndroidSurfaceKHR", fpCreateAndroidSurfaceKHR))
    45                 LOGE(
"Couldn't find android surface creation symbol.");
    46                 return VK_NULL_HANDLE;
    49         VkAndroidSurfaceCreateInfoKHR info = { VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR };
    50         info.window = pNativeWindow;
    52         VK_CHECK(fpCreateAndroidSurfaceKHR(instance, &info, 
nullptr, &surface));
    62 double MaliSDK::OS::getCurrentTime()
    65         if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
    67                 LOGE(
"clock_gettime() failed.\n");
    71         return ts.tv_sec + ts.tv_nsec * 1e-9;
    74 unsigned MaliSDK::OS::getNumberOfCpuThreads()
    76         unsigned count = android_getCpuCount();
    77         LOGI(
"Detected %u CPUs.\n", count);
    89         return initVulkan(swapchain, { 
"VK_KHR_surface", 
"VK_KHR_android_surface" }, { 
"VK_KHR_swapchain" });
    94         vkDeviceWaitIdle(device);
    95         initSwapchain(swapchain);
    98 void AndroidPlatform::onPause()
   103 void AndroidPlatform::terminate()
   105         WSIPlatform::terminate();
   111 static int32_t engineHandleInput(android_app *, AInputEvent *)
   116 static void engineHandleCmd(android_app *pApp, int32_t cmd)
   118         auto *state = 
static_cast<AndroidState *
>(pApp->userData);
   125                 state->active = 
true;
   128                 if (state->pVulkanApp)
   130                         LOGI(
"Resuming swapchain!\n");
   131                         platform.onResume(dim);
   133                         vector<VkImage> images;
   134                         platform.getCurrentSwapchain(&images, &dim);
   135                         state->pVulkanApp->updateSwapchain(images, dim);
   142                 LOGI(
"Pausing swapchain!\n");
   143                 state->active = 
false;
   148         case APP_CMD_INIT_WINDOW:
   149                 if (pApp->window != 
nullptr)
   151                         LOGI(
"Initializing platform!\n");
   152                         if (FAILED(platform.initialize()))
   154                                 LOGE(
"Failed to initialize platform.\n");
   163                         LOGI(
"Creating window!\n");
   164                         if (FAILED(platform.createWindow(dim)))
   166                                 LOGE(
"Failed to create Vulkan window.\n");
   170                         LOGI(
"Creating application!\n");
   171                         state->pVulkanApp = createApplication();
   173                         LOGI(
"Initializing application!\n");
   174                         if (!state->pVulkanApp->initialize(&platform.getContext()))
   176                                 LOGE(
"Failed to initialize Vulkan application.\n");
   180                         LOGI(
"Updating swapchain!\n");
   181                         vector<VkImage> images;
   182                         platform.getCurrentSwapchain(&images, &dim);
   183                         state->pVulkanApp->updateSwapchain(images, dim);
   187         case APP_CMD_TERM_WINDOW:
   188                 if (state->pVulkanApp)
   190                         LOGI(
"Terminating application!\n");
   191                         state->pVulkanApp->terminate();
   192                         delete state->pVulkanApp;
   193                         state->pVulkanApp = 
nullptr;
   194                         platform.terminate();
   200 void android_main(android_app *state)
   202         LOGI(
"Entering android_main()!\n");
   208         memset(&engine, 0, 
sizeof(engine));
   211         state->userData = &engine;
   212         state->onAppCmd = engineHandleCmd;
   213         state->onInputEvent = engineHandleInput;
   217         static_cast<AndroidAssetManager &
>(OS::getAssetManager()).setAssetManager(state->activity->assetManager);
   219         unsigned frameCount = 0;
   220         double startTime = OS::getCurrentTime();
   224                 struct android_poll_source *source;
   228                 while ((ident = ALooper_pollAll(engine.
pVulkanApp && engine.
active ? 0 : -1, 
nullptr, &events,
   229                                                 (
void **)&source)) >= 0)
   232                                 source->process(state, source);
   234                         if (state->destroyRequested)
   241                         vector<VkImage> images;
   244                         Result res = platform.acquireNextImage(&index);
   245                         while (res == RESULT_ERROR_OUTDATED_SWAPCHAIN)
   247                                 platform.acquireNextImage(&index);
   248                                 platform.getCurrentSwapchain(&images, &dim);
   254                                 LOGE(
"Unrecoverable swapchain error.\n");
   259                         res = platform.presentImage(index);
   262                         if (FAILED(res) && res != RESULT_ERROR_OUTDATED_SWAPCHAIN)
   266                         if (frameCount == 100)
   268                                 double endTime = OS::getCurrentTime();
   269                                 LOGI(
"FPS: %.3f\n", frameCount / (endTime - startTime));
 The asset manager reads data from a platform specific location. This class is used internally to load...
 
VulkanApplication * pVulkanApp
The Vulkan application. 
 
virtual void render(unsigned swapchainIndex, float deltaTime)=0
Render a frame. 
 
virtual void updateSwapchain(const std::vector< VkImage > &backbuffers, const Platform::SwapchainDimensions &dimensions)=0
Called when the swapchain has been initialized. 
 
State used for the android mainloop. 
 
bool active
The application is in focus and running. 
 
struct android_app * pApp
The ANativeActivity handle. 
 
An asset manager implementation for Android. Uses AAssetManager to load assets.