main_android.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "AtomSampleViewerApplication.h"
  9. #include <AzGameFramework/Application/GameApplication.h>
  10. #include <GridMate/GridMate.h>
  11. #include <GridMate/Session/LANSession.h>
  12. #include <AzCore/Android/Utils.h>
  13. #include <AzCore/Android/AndroidEnv.h>
  14. #include <AzFramework/API/ApplicationAPI_android.h>
  15. #include <android/log.h>
  16. #include <android/native_activity.h>
  17. #include <android/native_window.h>
  18. #include <android_native_app_glue.h>
  19. #include <errno.h>
  20. #if defined(AZ_ENABLE_TRACING)
  21. #define LOG_TAG "LMBR"
  22. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
  23. #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
  24. #else
  25. #define LOGI(...)
  26. #define LOGE(...)
  27. #endif // defined(AZ_ENABLE_TRACING)
  28. #define MAIN_EXIT_FAILURE(_appState, ...) \
  29. LOGE("****************************************************************"); \
  30. LOGE("STARTUP FAILURE - EXITING"); \
  31. LOGE("REASON:"); \
  32. LOGE(__VA_ARGS__); \
  33. LOGE("****************************************************************"); \
  34. _appState->userData = nullptr; \
  35. ANativeActivity_finish(_appState->activity); \
  36. while (_appState->destroyRequested == 0) { \
  37. PumpEvents(_appState); \
  38. } \
  39. return;
  40. namespace
  41. {
  42. bool g_windowInitialized = false;
  43. //////////////////////////////////////////////////////////////////////////
  44. void PumpEvents(android_app* appState)
  45. {
  46. int events;
  47. android_poll_source* source;
  48. while (ALooper_pollAll(0, NULL, &events, reinterpret_cast<void**>(&source)) >= 0)
  49. {
  50. if (source != NULL)
  51. {
  52. source->process(appState, source);
  53. }
  54. if (appState->destroyRequested != 0)
  55. {
  56. break;
  57. }
  58. }
  59. }
  60. // ----
  61. // System handlers
  62. // ----
  63. //////////////////////////////////////////////////////////////////////////
  64. // this callback is triggered on the same thread the events are pumped
  65. void HandleApplicationLifecycleEvents(android_app* appState, int32_t command)
  66. {
  67. #if defined(AZ_ENABLE_TRACING)
  68. const char* commandNames[] = {
  69. "APP_CMD_INPUT_CHANGED",
  70. "APP_CMD_INIT_WINDOW",
  71. "APP_CMD_TERM_WINDOW",
  72. "APP_CMD_WINDOW_RESIZED",
  73. "APP_CMD_WINDOW_REDRAW_NEEDED",
  74. "APP_CMD_CONTENT_RECT_CHANGED",
  75. "APP_CMD_GAINED_FOCUS",
  76. "APP_CMD_LOST_FOCUS",
  77. "APP_CMD_CONFIG_CHANGED",
  78. "APP_CMD_LOW_MEMORY",
  79. "APP_CMD_START",
  80. "APP_CMD_RESUME",
  81. "APP_CMD_SAVE_STATE",
  82. "APP_CMD_PAUSE",
  83. "APP_CMD_STOP",
  84. "APP_CMD_DESTROY",
  85. };
  86. LOGI("Engine command received: %s", commandNames[command]);
  87. #endif
  88. AZ::Android::AndroidEnv* androidEnv = static_cast<AZ::Android::AndroidEnv*>(appState->userData);
  89. if (!androidEnv)
  90. {
  91. return;
  92. }
  93. switch (command)
  94. {
  95. case APP_CMD_GAINED_FOCUS:
  96. {
  97. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnGainedFocus);
  98. }
  99. break;
  100. case APP_CMD_LOST_FOCUS:
  101. {
  102. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnLostFocus);
  103. }
  104. break;
  105. case APP_CMD_PAUSE:
  106. {
  107. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnPause);
  108. androidEnv->SetIsRunning(false);
  109. }
  110. break;
  111. case APP_CMD_RESUME:
  112. {
  113. androidEnv->SetIsRunning(true);
  114. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnResume);
  115. }
  116. break;
  117. case APP_CMD_DESTROY:
  118. {
  119. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnDestroy);
  120. }
  121. break;
  122. case APP_CMD_INIT_WINDOW:
  123. {
  124. g_windowInitialized = true;
  125. androidEnv->SetWindow(appState->window);
  126. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnWindowInit);
  127. }
  128. break;
  129. case APP_CMD_TERM_WINDOW:
  130. {
  131. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnWindowDestroy);
  132. androidEnv->SetWindow(nullptr);
  133. }
  134. break;
  135. case APP_CMD_LOW_MEMORY:
  136. {
  137. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnLowMemory);
  138. }
  139. break;
  140. case APP_CMD_CONFIG_CHANGED:
  141. {
  142. androidEnv->UpdateConfiguration();
  143. }
  144. break;
  145. case APP_CMD_WINDOW_REDRAW_NEEDED:
  146. {
  147. EBUS_EVENT(AzFramework::AndroidLifecycleEvents::Bus, OnWindowRedrawNeeded);
  148. }
  149. break;
  150. default:
  151. {
  152. LOGE("Invalid command received %d", command);
  153. }
  154. break;
  155. }
  156. }
  157. void OnWindowRedrawNeeded(ANativeActivity* activity, ANativeWindow* rect)
  158. {
  159. android_app* app = static_cast<android_app*>(activity->instance);
  160. int8_t cmd = APP_CMD_WINDOW_REDRAW_NEEDED;
  161. if (write(app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd))
  162. {
  163. LOGE("Failure writing android_app cmd: %s\n", strerror(errno));
  164. }
  165. }
  166. }
  167. void android_init(android_app* appState)
  168. {
  169. // setup the system command handler
  170. appState->onAppCmd = HandleApplicationLifecycleEvents;
  171. // This callback will notify us when the orientation of the device changes.
  172. // While Android does have an onNativeWindowResized callback, it is never called in android_native_app_glue when the window size changes.
  173. // The onNativeConfigChanged callback is called too early(before the window size has changed), so we won't have the correct window size at that point.
  174. appState->activity->callbacks->onNativeWindowRedrawNeeded = OnWindowRedrawNeeded;
  175. // setup the android environment
  176. AZ::AllocatorInstance<AZ::OSAllocator>::Create();
  177. {
  178. AZ::Android::AndroidEnv::Descriptor descriptor;
  179. descriptor.m_jvm = appState->activity->vm;
  180. descriptor.m_activityRef = appState->activity->clazz;
  181. descriptor.m_assetManager = appState->activity->assetManager;
  182. descriptor.m_configuration = appState->config;
  183. descriptor.m_appPrivateStoragePath = appState->activity->internalDataPath;
  184. descriptor.m_appPublicStoragePath = appState->activity->externalDataPath;
  185. descriptor.m_obbStoragePath = appState->activity->obbPath;
  186. if (!AZ::Android::AndroidEnv::Create(descriptor))
  187. {
  188. AZ::Android::AndroidEnv::Destroy();
  189. AZ::AllocatorInstance<AZ::OSAllocator>::Destroy();
  190. MAIN_EXIT_FAILURE(appState, "Failed to create the AndroidEnv");
  191. }
  192. AZ::Android::AndroidEnv* androidEnv = AZ::Android::AndroidEnv::Get();
  193. appState->userData = androidEnv;
  194. androidEnv->SetIsRunning(true);
  195. }
  196. // sync the window creation
  197. {
  198. // While not ideal to have the event pump code duplicated here and in AzFramework, this
  199. // at least solves the splash screen issues when the window creation sync happened later
  200. // in initialization. It's also the lesser of 2 evils, the other requiring a change in
  201. // how the platform specific private Application implementation is created for ALL
  202. // platforms
  203. while (!g_windowInitialized)
  204. {
  205. PumpEvents(appState);
  206. }
  207. }
  208. }
  209. void android_shutdown(android_app* appState)
  210. {
  211. AZ::Android::AndroidEnv::Destroy();
  212. AZ::AllocatorInstance<AZ::OSAllocator>::Destroy();
  213. }
  214. void android_main(android_app* appState)
  215. {
  216. android_init(appState);
  217. // Android doesn't have command line arguments.
  218. RunGameCommon(0, nullptr, [appState](){
  219. // This need to called after the AzFramework is started.
  220. AzFramework::AndroidAppRequests::Bus::Broadcast(&AzFramework::AndroidAppRequests::SetAppState, reinterpret_cast<android_app*>(appData));
  221. });
  222. android_shutdown(appState);
  223. }