App.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright (C) 2009-2022, 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. namespace anki {
  12. // Forward
  13. class CoreTracer;
  14. class ConfigSet;
  15. class ThreadHive;
  16. class NativeWindow;
  17. class Input;
  18. class GrManager;
  19. class MainRenderer;
  20. class PhysicsWorld;
  21. class SceneGraph;
  22. class ScriptManager;
  23. class ResourceManager;
  24. class ResourceFilesystem;
  25. class StagingGpuMemoryPool;
  26. class VertexGpuMemoryPool;
  27. class UiManager;
  28. class UiQueueElement;
  29. class RenderQueue;
  30. class MaliHwCounters;
  31. /// The core class of the engine.
  32. class App
  33. {
  34. public:
  35. App();
  36. virtual ~App();
  37. /// Initialize the application.
  38. /// @param[in,out] config The config. Needs to be alive as long as the app is alive.
  39. ANKI_USE_RESULT Error init(ConfigSet* config, CString executableFilename, AllocAlignedCallback allocCb,
  40. void* allocCbUserData);
  41. const String& getSettingsDirectory() const
  42. {
  43. return m_settingsDir;
  44. }
  45. const String& getCacheDirectory() const
  46. {
  47. return m_cacheDir;
  48. }
  49. AllocAlignedCallback getAllocationCallback() const
  50. {
  51. return m_allocCb;
  52. }
  53. void* getAllocationCallbackData() const
  54. {
  55. return m_allocCbData;
  56. }
  57. ThreadHive& getThreadHive()
  58. {
  59. return *m_threadHive;
  60. }
  61. HeapAllocator<U8>& getAllocator()
  62. {
  63. return m_heapAlloc;
  64. }
  65. Timestamp getGlobalTimestamp() const
  66. {
  67. return m_globalTimestamp;
  68. }
  69. /// Run the main loop.
  70. ANKI_USE_RESULT Error mainLoop();
  71. /// The user code to run along with the other main loop code.
  72. virtual ANKI_USE_RESULT Error userMainLoop(Bool& quit, Second elapsedTime)
  73. {
  74. // Do nothing
  75. return Error::NONE;
  76. }
  77. const ConfigSet& getConfig() const
  78. {
  79. return *m_config;
  80. }
  81. ConfigSet& getConfig()
  82. {
  83. return *m_config;
  84. }
  85. Input& getInput()
  86. {
  87. return *m_input;
  88. }
  89. SceneGraph& getSceneGraph()
  90. {
  91. return *m_scene;
  92. }
  93. MainRenderer& getMainRenderer()
  94. {
  95. return *m_renderer;
  96. }
  97. ResourceManager& getResourceManager()
  98. {
  99. return *m_resources;
  100. }
  101. ScriptManager& getScriptManager()
  102. {
  103. return *m_script;
  104. }
  105. PhysicsWorld& getPhysicsWorld()
  106. {
  107. return *m_physics;
  108. }
  109. NativeWindow& getWindow()
  110. {
  111. return *m_window;
  112. }
  113. HeapAllocator<U8> getAllocator() const
  114. {
  115. return m_heapAlloc;
  116. }
  117. void setDisplayDeveloperConsole(Bool display)
  118. {
  119. m_consoleEnabled = display;
  120. }
  121. Bool getDisplayDeveloperConsole() const
  122. {
  123. return m_consoleEnabled;
  124. }
  125. private:
  126. // Allocation
  127. AllocAlignedCallback m_allocCb;
  128. void* m_allocCbData;
  129. HeapAllocator<U8> m_heapAlloc;
  130. // Sybsystems
  131. ConfigSet* m_config = nullptr;
  132. #if ANKI_ENABLE_TRACE
  133. CoreTracer* m_coreTracer = nullptr;
  134. #endif
  135. NativeWindow* m_window = nullptr;
  136. Input* m_input = nullptr;
  137. ThreadHive* m_threadHive = nullptr;
  138. GrManager* m_gr = nullptr;
  139. MaliHwCounters* m_maliHwCounters = nullptr;
  140. VertexGpuMemoryPool* m_vertexMem = nullptr;
  141. StagingGpuMemoryPool* m_stagingMem = nullptr;
  142. PhysicsWorld* m_physics = nullptr;
  143. ResourceFilesystem* m_resourceFs = nullptr;
  144. ResourceManager* m_resources = nullptr;
  145. UiManager* m_ui = nullptr;
  146. MainRenderer* m_renderer = nullptr;
  147. SceneGraph* m_scene = nullptr;
  148. ScriptManager* m_script = nullptr;
  149. // Misc
  150. UiImmediateModeBuilderPtr m_statsUi;
  151. UiImmediateModeBuilderPtr m_console;
  152. Bool m_consoleEnabled = false;
  153. Timestamp m_globalTimestamp = 1;
  154. String m_settingsDir; ///< The path that holds the configuration
  155. String m_cacheDir; ///< This is used as a cache
  156. U64 m_resourceCompletedAsyncTaskCount = 0;
  157. class MemStats
  158. {
  159. public:
  160. Atomic<PtrSize> m_allocatedMem = {0};
  161. Atomic<U64> m_allocCount = {0};
  162. Atomic<U64> m_freeCount = {0};
  163. void* m_originalUserData = nullptr;
  164. AllocAlignedCallback m_originalAllocCallback = nullptr;
  165. static void* allocCallback(void* userData, void* ptr, PtrSize size, PtrSize alignment);
  166. } m_memStats;
  167. void initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData);
  168. ANKI_USE_RESULT Error initInternal(CString executableFilename, AllocAlignedCallback allocCb, void* allocCbUserData);
  169. ANKI_USE_RESULT Error initDirs();
  170. void cleanup();
  171. /// Inject a new UI element in the render queue for displaying various stuff.
  172. void injectUiElements(DynamicArrayAuto<UiQueueElement>& elements, RenderQueue& rqueue);
  173. void setSignalHandlers();
  174. };
  175. } // end namespace anki