App.h 4.3 KB

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