App.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Core/Common.h>
  7. #include <AnKi/Util/Allocator.h>
  8. #include <AnKi/Util/String.h>
  9. #include <AnKi/Util/Ptr.h>
  10. #include <AnKi/Ui/UiImmediateModeBuilder.h>
  11. #if ANKI_OS_ANDROID
  12. # include <android_native_app_glue.h>
  13. #endif
  14. namespace anki
  15. {
  16. #if ANKI_OS_ANDROID
  17. extern android_app* gAndroidApp;
  18. #endif
  19. // Forward
  20. class CoreTracer;
  21. class ConfigSet;
  22. class ThreadHive;
  23. class NativeWindow;
  24. class Input;
  25. class GrManager;
  26. class MainRenderer;
  27. class PhysicsWorld;
  28. class SceneGraph;
  29. class ScriptManager;
  30. class ResourceManager;
  31. class ResourceFilesystem;
  32. class StagingGpuMemoryManager;
  33. class UiManager;
  34. class UiQueueElement;
  35. class RenderQueue;
  36. /// The core class of the engine.
  37. class App
  38. {
  39. public:
  40. App();
  41. virtual ~App();
  42. ANKI_USE_RESULT Error init(const ConfigSet& config, AllocAlignedCallback allocCb, void* allocCbUserData);
  43. Second getTimerTick() const
  44. {
  45. return m_timerTick;
  46. }
  47. void setTimerTick(const F32 x)
  48. {
  49. m_timerTick = x;
  50. }
  51. const String& getSettingsDirectory() const
  52. {
  53. return m_settingsDir;
  54. }
  55. const String& getCacheDirectory() const
  56. {
  57. return m_cacheDir;
  58. }
  59. AllocAlignedCallback getAllocationCallback() const
  60. {
  61. return m_allocCb;
  62. }
  63. void* getAllocationCallbackData() const
  64. {
  65. return m_allocCbData;
  66. }
  67. ThreadHive& getThreadHive()
  68. {
  69. return *m_threadHive;
  70. }
  71. HeapAllocator<U8>& getAllocator()
  72. {
  73. return m_heapAlloc;
  74. }
  75. Timestamp getGlobalTimestamp() const
  76. {
  77. return m_globalTimestamp;
  78. }
  79. /// Run the main loop.
  80. ANKI_USE_RESULT Error mainLoop();
  81. /// The user code to run along with the other main loop code.
  82. virtual ANKI_USE_RESULT Error userMainLoop(Bool& quit, Second elapsedTime)
  83. {
  84. // Do nothing
  85. return Error::NONE;
  86. }
  87. Input& getInput()
  88. {
  89. return *m_input;
  90. }
  91. SceneGraph& getSceneGraph()
  92. {
  93. return *m_scene;
  94. }
  95. MainRenderer& getMainRenderer()
  96. {
  97. return *m_renderer;
  98. }
  99. ResourceManager& getResourceManager()
  100. {
  101. return *m_resources;
  102. }
  103. ScriptManager& getScriptManager()
  104. {
  105. return *m_script;
  106. }
  107. PhysicsWorld& getPhysicsWorld()
  108. {
  109. return *m_physics;
  110. }
  111. HeapAllocator<U8> getAllocator() const
  112. {
  113. return m_heapAlloc;
  114. }
  115. void setDisplayStats(Bool enable)
  116. {
  117. m_displayStats = enable;
  118. }
  119. Bool getDisplayStats() const
  120. {
  121. return m_displayStats;
  122. }
  123. void setDisplayDeveloperConsole(Bool display)
  124. {
  125. m_consoleEnabled = display;
  126. }
  127. Bool getDisplayDeveloperConsole() const
  128. {
  129. return m_consoleEnabled;
  130. }
  131. private:
  132. class StatsUi;
  133. // Allocation
  134. AllocAlignedCallback m_allocCb;
  135. void* m_allocCbData;
  136. HeapAllocator<U8> m_heapAlloc;
  137. // Sybsystems
  138. #if ANKI_ENABLE_TRACE
  139. CoreTracer* m_coreTracer = nullptr;
  140. #endif
  141. NativeWindow* m_window = nullptr;
  142. Input* m_input = nullptr;
  143. GrManager* m_gr = nullptr;
  144. StagingGpuMemoryManager* m_stagingMem = nullptr;
  145. PhysicsWorld* m_physics = nullptr;
  146. ResourceFilesystem* m_resourceFs = nullptr;
  147. ResourceManager* m_resources = nullptr;
  148. UiManager* m_ui = nullptr;
  149. MainRenderer* m_renderer = nullptr;
  150. SceneGraph* m_scene = nullptr;
  151. ScriptManager* m_script = nullptr;
  152. // Misc
  153. UiImmediateModeBuilderPtr m_statsUi;
  154. Bool m_displayStats = false;
  155. UiImmediateModeBuilderPtr m_console;
  156. Bool m_consoleEnabled = false;
  157. Timestamp m_globalTimestamp = 1;
  158. ThreadHive* m_threadHive = nullptr;
  159. String m_settingsDir; ///< The path that holds the configuration
  160. String m_cacheDir; ///< This is used as a cache
  161. Second m_timerTick;
  162. U64 m_resourceCompletedAsyncTaskCount = 0;
  163. class MemStats
  164. {
  165. public:
  166. Atomic<PtrSize> m_allocatedMem = {0};
  167. Atomic<U64> m_allocCount = {0};
  168. Atomic<U64> m_freeCount = {0};
  169. void* m_originalUserData = nullptr;
  170. AllocAlignedCallback m_originalAllocCallback = nullptr;
  171. static void* allocCallback(void* userData, void* ptr, PtrSize size, PtrSize alignment);
  172. } m_memStats;
  173. void initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData);
  174. ANKI_USE_RESULT Error initInternal(const ConfigSet& config, AllocAlignedCallback allocCb, void* allocCbUserData);
  175. ANKI_USE_RESULT Error initDirs(const ConfigSet& cfg);
  176. void cleanup();
  177. /// Inject a new UI element in the render queue for displaying various stuff.
  178. void injectUiElements(DynamicArrayAuto<UiQueueElement>& elements, RenderQueue& rqueue);
  179. void setSignalHandlers();
  180. };
  181. } // end namespace anki