App.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. #include <AnKi/Core/App.h>
  6. #include <AnKi/Core/ConfigSet.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Util/File.h>
  9. #include <AnKi/Util/Filesystem.h>
  10. #include <AnKi/Util/System.h>
  11. #include <AnKi/Util/ThreadHive.h>
  12. #include <AnKi/Util/Tracer.h>
  13. #include <AnKi/Util/HighRezTimer.h>
  14. #include <AnKi/Core/CoreTracer.h>
  15. #include <AnKi/Core/DeveloperConsole.h>
  16. #include <AnKi/Core/StatsUi.h>
  17. #include <AnKi/Core/NativeWindow.h>
  18. #include <AnKi/Core/MaliHwCounters.h>
  19. #include <AnKi/Input/Input.h>
  20. #include <AnKi/Scene/SceneGraph.h>
  21. #include <AnKi/Renderer/RenderQueue.h>
  22. #include <AnKi/Resource/ResourceManager.h>
  23. #include <AnKi/Physics/PhysicsWorld.h>
  24. #include <AnKi/Renderer/MainRenderer.h>
  25. #include <AnKi/Script/ScriptManager.h>
  26. #include <AnKi/Resource/ResourceFilesystem.h>
  27. #include <AnKi/Resource/AsyncLoader.h>
  28. #include <AnKi/Core/GpuMemoryPools.h>
  29. #include <AnKi/Ui/UiManager.h>
  30. #include <AnKi/Ui/Canvas.h>
  31. #include <csignal>
  32. #if ANKI_OS_ANDROID
  33. # include <android_native_app_glue.h>
  34. #endif
  35. namespace anki {
  36. #if ANKI_OS_ANDROID
  37. /// The one and only android hack
  38. android_app* g_androidApp = nullptr;
  39. #endif
  40. void* App::MemStats::allocCallback(void* userData, void* ptr, PtrSize size, PtrSize alignment)
  41. {
  42. ANKI_ASSERT(userData);
  43. static const PtrSize MAX_ALIGNMENT = 64;
  44. struct alignas(MAX_ALIGNMENT) Header
  45. {
  46. PtrSize m_allocatedSize;
  47. Array<U8, MAX_ALIGNMENT - sizeof(PtrSize)> _m_padding;
  48. };
  49. static_assert(sizeof(Header) == MAX_ALIGNMENT, "See file");
  50. static_assert(alignof(Header) == MAX_ALIGNMENT, "See file");
  51. void* out = nullptr;
  52. if(ptr == nullptr)
  53. {
  54. // Need to allocate
  55. ANKI_ASSERT(size > 0);
  56. ANKI_ASSERT(alignment > 0 && alignment <= MAX_ALIGNMENT);
  57. const PtrSize newAlignment = MAX_ALIGNMENT;
  58. const PtrSize newSize = sizeof(Header) + size;
  59. // Allocate
  60. MemStats* self = static_cast<MemStats*>(userData);
  61. Header* allocation = static_cast<Header*>(
  62. self->m_originalAllocCallback(self->m_originalUserData, nullptr, newSize, newAlignment));
  63. allocation->m_allocatedSize = size;
  64. ++allocation;
  65. out = static_cast<void*>(allocation);
  66. // Update stats
  67. self->m_allocatedMem.fetchAdd(size);
  68. self->m_allocCount.fetchAdd(1);
  69. }
  70. else
  71. {
  72. // Need to free
  73. MemStats* self = static_cast<MemStats*>(userData);
  74. Header* allocation = static_cast<Header*>(ptr);
  75. --allocation;
  76. ANKI_ASSERT(allocation->m_allocatedSize > 0);
  77. // Update stats
  78. self->m_freeCount.fetchAdd(1);
  79. self->m_allocatedMem.fetchSub(allocation->m_allocatedSize);
  80. // Free
  81. self->m_originalAllocCallback(self->m_originalUserData, allocation, 0, 0);
  82. }
  83. return out;
  84. }
  85. App::App()
  86. {
  87. }
  88. App::~App()
  89. {
  90. ANKI_CORE_LOGI("Destroying application");
  91. cleanup();
  92. }
  93. void App::cleanup()
  94. {
  95. m_statsUi.reset(nullptr);
  96. m_console.reset(nullptr);
  97. m_heapAlloc.deleteInstance(m_scene);
  98. m_scene = nullptr;
  99. m_heapAlloc.deleteInstance(m_script);
  100. m_script = nullptr;
  101. m_heapAlloc.deleteInstance(m_renderer);
  102. m_renderer = nullptr;
  103. m_heapAlloc.deleteInstance(m_ui);
  104. m_ui = nullptr;
  105. m_heapAlloc.deleteInstance(m_resources);
  106. m_resources = nullptr;
  107. m_heapAlloc.deleteInstance(m_resourceFs);
  108. m_resourceFs = nullptr;
  109. m_heapAlloc.deleteInstance(m_physics);
  110. m_physics = nullptr;
  111. m_heapAlloc.deleteInstance(m_stagingMem);
  112. m_stagingMem = nullptr;
  113. m_heapAlloc.deleteInstance(m_vertexMem);
  114. m_vertexMem = nullptr;
  115. m_heapAlloc.deleteInstance(m_threadHive);
  116. m_threadHive = nullptr;
  117. m_heapAlloc.deleteInstance(m_maliHwCounters);
  118. m_maliHwCounters = nullptr;
  119. GrManager::deleteInstance(m_gr);
  120. m_gr = nullptr;
  121. Input::deleteInstance(m_input);
  122. m_input = nullptr;
  123. NativeWindow::deleteInstance(m_window);
  124. m_window = nullptr;
  125. #if ANKI_ENABLE_TRACE
  126. m_heapAlloc.deleteInstance(m_coreTracer);
  127. m_coreTracer = nullptr;
  128. #endif
  129. m_settingsDir.destroy(m_heapAlloc);
  130. m_cacheDir.destroy(m_heapAlloc);
  131. }
  132. Error App::init(ConfigSet* config, CString executableFilename, AllocAlignedCallback allocCb, void* allocCbUserData)
  133. {
  134. ANKI_ASSERT(config);
  135. m_config = config;
  136. const Error err = initInternal(executableFilename, allocCb, allocCbUserData);
  137. if(err)
  138. {
  139. ANKI_CORE_LOGE("App initialization failed. Shutting down");
  140. cleanup();
  141. }
  142. return err;
  143. }
  144. Error App::initInternal(CString executableFilename, AllocAlignedCallback allocCb, void* allocCbUserData)
  145. {
  146. LoggerSingleton::get().enableVerbosity(m_config->getCoreVerboseLog());
  147. setSignalHandlers();
  148. Thread::setNameOfCurrentThread("AnKiMain");
  149. initMemoryCallbacks(allocCb, allocCbUserData);
  150. m_heapAlloc = HeapAllocator<U8>(m_allocCb, m_allocCbData, "Core");
  151. ANKI_CHECK(initDirs());
  152. // Print a message
  153. const char* buildType =
  154. #if ANKI_OPTIMIZE
  155. "optimized, "
  156. #else
  157. "NOT optimized, "
  158. #endif
  159. #if ANKI_DEBUG_SYMBOLS
  160. "dbg symbols, "
  161. #else
  162. "NO dbg symbols, "
  163. #endif
  164. #if ANKI_EXTRA_CHECKS
  165. "extra checks, "
  166. #else
  167. "NO extra checks, "
  168. #endif
  169. #if ANKI_ENABLE_TRACE
  170. "built with tracing";
  171. #else
  172. "NOT built with tracing";
  173. #endif
  174. ANKI_CORE_LOGI("Initializing application ("
  175. "version %u.%u, "
  176. "%s, "
  177. "compiler %s, "
  178. "build date %s, "
  179. "commit %s)",
  180. ANKI_VERSION_MAJOR, ANKI_VERSION_MINOR, buildType, ANKI_COMPILER_STR, __DATE__, ANKI_REVISION);
  181. // Check SIMD support
  182. #if ANKI_SIMD_SSE && ANKI_COMPILER_GCC_COMPATIBLE
  183. if(!__builtin_cpu_supports("sse4.2"))
  184. {
  185. ANKI_CORE_LOGF(
  186. "AnKi is built with sse4.2 support but your CPU doesn't support it. Try bulding without SSE support");
  187. }
  188. #endif
  189. ANKI_CORE_LOGI("Number of job threads: %u", m_config->getCoreJobThreadCount());
  190. //
  191. // Core tracer
  192. //
  193. #if ANKI_ENABLE_TRACE
  194. m_coreTracer = m_heapAlloc.newInstance<CoreTracer>();
  195. ANKI_CHECK(m_coreTracer->init(m_heapAlloc, m_settingsDir));
  196. #endif
  197. //
  198. // Window
  199. //
  200. NativeWindowInitInfo nwinit;
  201. nwinit.m_allocCallback = m_allocCb;
  202. nwinit.m_allocCallbackUserData = m_allocCbData;
  203. nwinit.m_width = m_config->getWidth();
  204. nwinit.m_height = m_config->getHeight();
  205. nwinit.m_depthBits = 0;
  206. nwinit.m_stencilBits = 0;
  207. nwinit.m_fullscreenDesktopRez = m_config->getWindowFullscreen() > 0;
  208. nwinit.m_exclusiveFullscreen = m_config->getWindowFullscreen() == 2;
  209. ANKI_CHECK(NativeWindow::newInstance(nwinit, m_window));
  210. //
  211. // Input
  212. //
  213. ANKI_CHECK(Input::newInstance(m_allocCb, m_allocCbData, m_window, m_input));
  214. //
  215. // ThreadPool
  216. //
  217. m_threadHive = m_heapAlloc.newInstance<ThreadHive>(m_config->getCoreJobThreadCount(), m_heapAlloc, true);
  218. //
  219. // Graphics API
  220. //
  221. GrManagerInitInfo grInit;
  222. grInit.m_allocCallback = m_allocCb;
  223. grInit.m_allocCallbackUserData = m_allocCbData;
  224. grInit.m_cacheDirectory = m_cacheDir.toCString();
  225. grInit.m_config = m_config;
  226. grInit.m_window = m_window;
  227. ANKI_CHECK(GrManager::newInstance(grInit, m_gr));
  228. //
  229. // Mali HW counters
  230. //
  231. if(m_gr->getDeviceCapabilities().m_gpuVendor == GpuVendor::ARM && m_config->getCoreMaliHwCounters())
  232. {
  233. m_maliHwCounters = m_heapAlloc.newInstance<MaliHwCounters>(m_heapAlloc);
  234. }
  235. //
  236. // GPU mem
  237. //
  238. m_vertexMem = m_heapAlloc.newInstance<VertexGpuMemoryPool>();
  239. ANKI_CHECK(m_vertexMem->init(m_heapAlloc, m_gr, *m_config));
  240. m_stagingMem = m_heapAlloc.newInstance<StagingGpuMemoryPool>();
  241. ANKI_CHECK(m_stagingMem->init(m_gr, *m_config));
  242. //
  243. // Physics
  244. //
  245. m_physics = m_heapAlloc.newInstance<PhysicsWorld>();
  246. ANKI_CHECK(m_physics->init(m_allocCb, m_allocCbData));
  247. //
  248. // Resource FS
  249. //
  250. #if !ANKI_OS_ANDROID
  251. // Add the location of the executable where the shaders are supposed to be
  252. StringAuto executableFname(m_heapAlloc);
  253. ANKI_CHECK(getApplicationPath(executableFname));
  254. ANKI_CORE_LOGI("Executable path is: %s", executableFname.cstr());
  255. StringAuto shadersPath(m_heapAlloc);
  256. getParentFilepath(executableFname, shadersPath);
  257. shadersPath.append(":");
  258. shadersPath.append(m_config->getRsrcDataPaths());
  259. m_config->setRsrcDataPaths(shadersPath);
  260. #endif
  261. m_resourceFs = m_heapAlloc.newInstance<ResourceFilesystem>(m_heapAlloc);
  262. ANKI_CHECK(m_resourceFs->init(*m_config, m_cacheDir.toCString()));
  263. //
  264. // Resources
  265. //
  266. ResourceManagerInitInfo rinit;
  267. rinit.m_gr = m_gr;
  268. rinit.m_physics = m_physics;
  269. rinit.m_resourceFs = m_resourceFs;
  270. rinit.m_vertexMemory = m_vertexMem;
  271. rinit.m_config = m_config;
  272. rinit.m_allocCallback = m_allocCb;
  273. rinit.m_allocCallbackData = m_allocCbData;
  274. m_resources = m_heapAlloc.newInstance<ResourceManager>();
  275. ANKI_CHECK(m_resources->init(rinit));
  276. //
  277. // UI
  278. //
  279. m_ui = m_heapAlloc.newInstance<UiManager>();
  280. ANKI_CHECK(m_ui->init(m_allocCb, m_allocCbData, m_resources, m_gr, m_stagingMem, m_input));
  281. //
  282. // Renderer
  283. //
  284. MainRendererInitInfo renderInit;
  285. renderInit.m_swapchainSize = UVec2(m_window->getWidth(), m_window->getHeight());
  286. renderInit.m_allocCallback = m_allocCb;
  287. renderInit.m_allocCallbackUserData = m_allocCbData;
  288. renderInit.m_threadHive = m_threadHive;
  289. renderInit.m_resourceManager = m_resources;
  290. renderInit.m_gr = m_gr;
  291. renderInit.m_stagingMemory = m_stagingMem;
  292. renderInit.m_ui = m_ui;
  293. renderInit.m_config = m_config;
  294. renderInit.m_globTimestamp = &m_globalTimestamp;
  295. m_renderer = m_heapAlloc.newInstance<MainRenderer>();
  296. ANKI_CHECK(m_renderer->init(renderInit));
  297. //
  298. // Script
  299. //
  300. m_script = m_heapAlloc.newInstance<ScriptManager>();
  301. ANKI_CHECK(m_script->init(m_allocCb, m_allocCbData));
  302. //
  303. // Scene
  304. //
  305. m_scene = m_heapAlloc.newInstance<SceneGraph>();
  306. ANKI_CHECK(m_scene->init(m_allocCb, m_allocCbData, m_threadHive, m_resources, m_input, m_script, m_ui, m_config,
  307. &m_globalTimestamp));
  308. // Inform the script engine about some subsystems
  309. m_script->setRenderer(m_renderer);
  310. m_script->setSceneGraph(m_scene);
  311. //
  312. // Misc
  313. //
  314. ANKI_CHECK(m_ui->newInstance<StatsUi>(m_statsUi));
  315. ANKI_CHECK(m_ui->newInstance<DeveloperConsole>(m_console, m_allocCb, m_allocCbData, m_script));
  316. ANKI_CORE_LOGI("Application initialized");
  317. return Error::NONE;
  318. }
  319. Error App::initDirs()
  320. {
  321. // Settings path
  322. #if !ANKI_OS_ANDROID
  323. StringAuto home(m_heapAlloc);
  324. ANKI_CHECK(getHomeDirectory(home));
  325. m_settingsDir.sprintf(m_heapAlloc, "%s/.anki", &home[0]);
  326. #else
  327. m_settingsDir.sprintf(m_heapAlloc, "%s/.anki", g_androidApp->activity->internalDataPath);
  328. #endif
  329. if(!directoryExists(m_settingsDir.toCString()))
  330. {
  331. ANKI_CORE_LOGI("Creating settings dir \"%s\"", &m_settingsDir[0]);
  332. ANKI_CHECK(createDirectory(m_settingsDir.toCString()));
  333. }
  334. else
  335. {
  336. ANKI_CORE_LOGI("Using settings dir \"%s\"", &m_settingsDir[0]);
  337. }
  338. // Cache
  339. m_cacheDir.sprintf(m_heapAlloc, "%s/cache", &m_settingsDir[0]);
  340. const Bool cacheDirExists = directoryExists(m_cacheDir.toCString());
  341. if(m_config->getCoreClearCaches() && cacheDirExists)
  342. {
  343. ANKI_CORE_LOGI("Will delete the cache dir and start fresh: %s", &m_cacheDir[0]);
  344. ANKI_CHECK(removeDirectory(m_cacheDir.toCString(), m_heapAlloc));
  345. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  346. }
  347. else if(!cacheDirExists)
  348. {
  349. ANKI_CORE_LOGI("Will create cache dir: %s", &m_cacheDir[0]);
  350. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  351. }
  352. return Error::NONE;
  353. }
  354. Error App::mainLoop()
  355. {
  356. ANKI_CORE_LOGI("Entering main loop");
  357. Bool quit = false;
  358. Second prevUpdateTime = HighRezTimer::getCurrentTime();
  359. Second crntTime = prevUpdateTime;
  360. while(!quit)
  361. {
  362. {
  363. ANKI_TRACE_SCOPED_EVENT(FRAME);
  364. const Second startTime = HighRezTimer::getCurrentTime();
  365. prevUpdateTime = crntTime;
  366. crntTime = HighRezTimer::getCurrentTime();
  367. // Update
  368. ANKI_CHECK(m_input->handleEvents());
  369. // User update
  370. ANKI_CHECK(userMainLoop(quit, crntTime - prevUpdateTime));
  371. ANKI_CHECK(m_scene->update(prevUpdateTime, crntTime));
  372. RenderQueue rqueue;
  373. m_scene->doVisibilityTests(rqueue);
  374. // Inject stats UI
  375. DynamicArrayAuto<UiQueueElement> newUiElementArr(m_heapAlloc);
  376. injectUiElements(newUiElementArr, rqueue);
  377. // Render
  378. TexturePtr presentableTex = m_gr->acquireNextPresentableTexture();
  379. m_renderer->setStatsEnabled(m_config->getCoreDisplayStats()
  380. #if ANKI_ENABLE_TRACE
  381. || TracerSingleton::get().getEnabled()
  382. #endif
  383. );
  384. ANKI_CHECK(m_renderer->render(rqueue, presentableTex));
  385. // Pause and sync async loader. That will force all tasks before the pause to finish in this frame.
  386. m_resources->getAsyncLoader().pause();
  387. m_gr->swapBuffers();
  388. m_stagingMem->endFrame();
  389. // Update the trace info with some async loader stats
  390. U64 asyncTaskCount = m_resources->getAsyncLoader().getCompletedTaskCount();
  391. ANKI_TRACE_INC_COUNTER(RESOURCE_ASYNC_TASKS, asyncTaskCount - m_resourceCompletedAsyncTaskCount);
  392. m_resourceCompletedAsyncTaskCount = asyncTaskCount;
  393. // Now resume the loader
  394. m_resources->getAsyncLoader().resume();
  395. // Sleep
  396. const Second endTime = HighRezTimer::getCurrentTime();
  397. const Second frameTime = endTime - startTime;
  398. const Second timerTick = 1.0 / Second(m_config->getCoreTargetFps());
  399. if(frameTime < timerTick)
  400. {
  401. ANKI_TRACE_SCOPED_EVENT(TIMER_TICK_SLEEP);
  402. HighRezTimer::sleep(timerTick - frameTime);
  403. }
  404. // Stats
  405. if(m_config->getCoreDisplayStats())
  406. {
  407. StatsUi& statsUi = *static_cast<StatsUi*>(m_statsUi.get());
  408. statsUi.setFrameTime(frameTime);
  409. statsUi.setRenderTime(m_renderer->getStats().m_renderingCpuTime);
  410. statsUi.setSceneUpdateTime(m_scene->getStats().m_updateTime);
  411. statsUi.setVisibilityTestsTime(m_scene->getStats().m_visibilityTestsTime);
  412. statsUi.setPhysicsTime(m_scene->getStats().m_physicsUpdate);
  413. statsUi.setGpuTime(m_renderer->getStats().m_renderingGpuTime);
  414. if(m_maliHwCounters)
  415. {
  416. MaliHwCountersOut out;
  417. m_maliHwCounters->sample(out);
  418. statsUi.setGpuActiveCycles(out.m_gpuActive);
  419. statsUi.setGpuReadBandwidth(out.m_readBandwidth);
  420. statsUi.setGpuWriteBandwidth(out.m_writeBandwidth);
  421. }
  422. statsUi.setAllocatedCpuMemory(m_memStats.m_allocatedMem.load());
  423. statsUi.setCpuAllocationCount(m_memStats.m_allocCount.load());
  424. statsUi.setCpuFreeCount(m_memStats.m_freeCount.load());
  425. statsUi.setGrStats(m_gr->getStats());
  426. BuddyAllocatorBuilderStats vertMemStats;
  427. m_vertexMem->getMemoryStats(vertMemStats);
  428. statsUi.setGlobalVertexMemoryPoolStats(vertMemStats);
  429. statsUi.setDrawableCount(rqueue.countAllRenderables());
  430. }
  431. #if ANKI_ENABLE_TRACE
  432. if(m_renderer->getStats().m_renderingGpuTime >= 0.0)
  433. {
  434. ANKI_TRACE_CUSTOM_EVENT(GPU_TIME, m_renderer->getStats().m_renderingGpuSubmitTimestamp,
  435. m_renderer->getStats().m_renderingGpuTime);
  436. }
  437. #endif
  438. ++m_globalTimestamp;
  439. }
  440. #if ANKI_ENABLE_TRACE
  441. static U64 frame = 1;
  442. m_coreTracer->flushFrame(frame++);
  443. #endif
  444. }
  445. return Error::NONE;
  446. }
  447. void App::injectUiElements(DynamicArrayAuto<UiQueueElement>& newUiElementArr, RenderQueue& rqueue)
  448. {
  449. const U32 originalCount = rqueue.m_uis.getSize();
  450. if(m_config->getCoreDisplayStats() || m_consoleEnabled)
  451. {
  452. const U32 extraElements = m_config->getCoreDisplayStats() + (m_consoleEnabled != 0);
  453. newUiElementArr.create(originalCount + extraElements);
  454. if(originalCount > 0)
  455. {
  456. memcpy(&newUiElementArr[0], &rqueue.m_uis[0], rqueue.m_uis.getSizeInBytes());
  457. }
  458. rqueue.m_uis = WeakArray<UiQueueElement>(newUiElementArr);
  459. }
  460. U32 count = originalCount;
  461. if(m_config->getCoreDisplayStats())
  462. {
  463. newUiElementArr[count].m_userData = m_statsUi.get();
  464. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  465. static_cast<StatsUi*>(userData)->build(canvas);
  466. };
  467. ++count;
  468. }
  469. if(m_consoleEnabled)
  470. {
  471. newUiElementArr[count].m_userData = m_console.get();
  472. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  473. static_cast<DeveloperConsole*>(userData)->build(canvas);
  474. };
  475. ++count;
  476. }
  477. }
  478. void App::initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData)
  479. {
  480. if(m_config->getCoreDisplayStats())
  481. {
  482. m_memStats.m_originalAllocCallback = allocCb;
  483. m_memStats.m_originalUserData = allocCbUserData;
  484. m_allocCb = MemStats::allocCallback;
  485. m_allocCbData = &m_memStats;
  486. }
  487. else
  488. {
  489. m_allocCb = allocCb;
  490. m_allocCbData = allocCbUserData;
  491. }
  492. }
  493. void App::setSignalHandlers()
  494. {
  495. auto handler = [](int signum) -> void {
  496. const char* name = nullptr;
  497. switch(signum)
  498. {
  499. case SIGABRT:
  500. name = "SIGABRT";
  501. break;
  502. case SIGSEGV:
  503. name = "SIGSEGV";
  504. break;
  505. #if ANKI_POSIX
  506. case SIGBUS:
  507. name = "SIGBUS";
  508. break;
  509. #endif
  510. case SIGILL:
  511. name = "SIGILL";
  512. break;
  513. case SIGFPE:
  514. name = "SIGFPE";
  515. break;
  516. }
  517. if(name)
  518. printf("Caught signal %d (%s)\n", signum, name);
  519. else
  520. printf("Caught signal %d\n", signum);
  521. U32 count = 0;
  522. printf("Backtrace:\n");
  523. backtrace(HeapAllocator<U8>(allocAligned, nullptr), [&count](CString symbol) {
  524. printf("%.2u: %s\n", count++, symbol.cstr());
  525. });
  526. ANKI_DEBUG_BREAK();
  527. };
  528. signal(SIGSEGV, handler);
  529. signal(SIGILL, handler);
  530. signal(SIGFPE, handler);
  531. #if ANKI_POSIX
  532. signal(SIGBUS, handler);
  533. #endif
  534. // Ignore for now: signal(SIGABRT, handler);
  535. }
  536. } // end namespace anki