App.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. StringAuto shadersPath(m_heapAlloc);
  255. getParentFilepath(executableFname, shadersPath);
  256. shadersPath.append(":");
  257. shadersPath.append(m_config->getRsrcDataPaths());
  258. m_config->setRsrcDataPaths(shadersPath);
  259. #endif
  260. m_resourceFs = m_heapAlloc.newInstance<ResourceFilesystem>(m_heapAlloc);
  261. ANKI_CHECK(m_resourceFs->init(*m_config, m_cacheDir.toCString()));
  262. //
  263. // Resources
  264. //
  265. ResourceManagerInitInfo rinit;
  266. rinit.m_gr = m_gr;
  267. rinit.m_physics = m_physics;
  268. rinit.m_resourceFs = m_resourceFs;
  269. rinit.m_vertexMemory = m_vertexMem;
  270. rinit.m_config = m_config;
  271. rinit.m_allocCallback = m_allocCb;
  272. rinit.m_allocCallbackData = m_allocCbData;
  273. m_resources = m_heapAlloc.newInstance<ResourceManager>();
  274. ANKI_CHECK(m_resources->init(rinit));
  275. //
  276. // UI
  277. //
  278. m_ui = m_heapAlloc.newInstance<UiManager>();
  279. ANKI_CHECK(m_ui->init(m_allocCb, m_allocCbData, m_resources, m_gr, m_stagingMem, m_input));
  280. //
  281. // Renderer
  282. //
  283. MainRendererInitInfo renderInit;
  284. renderInit.m_swapchainSize = UVec2(m_window->getWidth(), m_window->getHeight());
  285. renderInit.m_allocCallback = m_allocCb;
  286. renderInit.m_allocCallbackUserData = m_allocCbData;
  287. renderInit.m_threadHive = m_threadHive;
  288. renderInit.m_resourceManager = m_resources;
  289. renderInit.m_gr = m_gr;
  290. renderInit.m_stagingMemory = m_stagingMem;
  291. renderInit.m_ui = m_ui;
  292. renderInit.m_config = m_config;
  293. renderInit.m_globTimestamp = &m_globalTimestamp;
  294. m_renderer = m_heapAlloc.newInstance<MainRenderer>();
  295. ANKI_CHECK(m_renderer->init(renderInit));
  296. //
  297. // Script
  298. //
  299. m_script = m_heapAlloc.newInstance<ScriptManager>();
  300. ANKI_CHECK(m_script->init(m_allocCb, m_allocCbData));
  301. //
  302. // Scene
  303. //
  304. m_scene = m_heapAlloc.newInstance<SceneGraph>();
  305. ANKI_CHECK(m_scene->init(m_allocCb, m_allocCbData, m_threadHive, m_resources, m_input, m_script, m_ui, m_config,
  306. &m_globalTimestamp));
  307. // Inform the script engine about some subsystems
  308. m_script->setRenderer(m_renderer);
  309. m_script->setSceneGraph(m_scene);
  310. //
  311. // Misc
  312. //
  313. ANKI_CHECK(m_ui->newInstance<StatsUi>(m_statsUi));
  314. ANKI_CHECK(m_ui->newInstance<DeveloperConsole>(m_console, m_allocCb, m_allocCbData, m_script));
  315. ANKI_CORE_LOGI("Application initialized");
  316. return Error::NONE;
  317. }
  318. Error App::initDirs()
  319. {
  320. // Settings path
  321. #if !ANKI_OS_ANDROID
  322. StringAuto home(m_heapAlloc);
  323. ANKI_CHECK(getHomeDirectory(home));
  324. m_settingsDir.sprintf(m_heapAlloc, "%s/.anki", &home[0]);
  325. #else
  326. m_settingsDir.sprintf(m_heapAlloc, "%s/.anki", g_androidApp->activity->internalDataPath);
  327. #endif
  328. if(!directoryExists(m_settingsDir.toCString()))
  329. {
  330. ANKI_CORE_LOGI("Creating settings dir \"%s\"", &m_settingsDir[0]);
  331. ANKI_CHECK(createDirectory(m_settingsDir.toCString()));
  332. }
  333. else
  334. {
  335. ANKI_CORE_LOGI("Using settings dir \"%s\"", &m_settingsDir[0]);
  336. }
  337. // Cache
  338. m_cacheDir.sprintf(m_heapAlloc, "%s/cache", &m_settingsDir[0]);
  339. const Bool cacheDirExists = directoryExists(m_cacheDir.toCString());
  340. if(m_config->getCoreClearCaches() && cacheDirExists)
  341. {
  342. ANKI_CORE_LOGI("Will delete the cache dir and start fresh: %s", &m_cacheDir[0]);
  343. ANKI_CHECK(removeDirectory(m_cacheDir.toCString(), m_heapAlloc));
  344. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  345. }
  346. else if(!cacheDirExists)
  347. {
  348. ANKI_CORE_LOGI("Will create cache dir: %s", &m_cacheDir[0]);
  349. ANKI_CHECK(createDirectory(m_cacheDir.toCString()));
  350. }
  351. return Error::NONE;
  352. }
  353. Error App::mainLoop()
  354. {
  355. ANKI_CORE_LOGI("Entering main loop");
  356. Bool quit = false;
  357. Second prevUpdateTime = HighRezTimer::getCurrentTime();
  358. Second crntTime = prevUpdateTime;
  359. while(!quit)
  360. {
  361. {
  362. ANKI_TRACE_SCOPED_EVENT(FRAME);
  363. const Second startTime = HighRezTimer::getCurrentTime();
  364. prevUpdateTime = crntTime;
  365. crntTime = HighRezTimer::getCurrentTime();
  366. // Update
  367. ANKI_CHECK(m_input->handleEvents());
  368. // User update
  369. ANKI_CHECK(userMainLoop(quit, crntTime - prevUpdateTime));
  370. ANKI_CHECK(m_scene->update(prevUpdateTime, crntTime));
  371. RenderQueue rqueue;
  372. m_scene->doVisibilityTests(rqueue);
  373. // Inject stats UI
  374. DynamicArrayAuto<UiQueueElement> newUiElementArr(m_heapAlloc);
  375. injectUiElements(newUiElementArr, rqueue);
  376. // Render
  377. TexturePtr presentableTex = m_gr->acquireNextPresentableTexture();
  378. m_renderer->setStatsEnabled(m_config->getCoreDisplayStats()
  379. #if ANKI_ENABLE_TRACE
  380. || TracerSingleton::get().getEnabled()
  381. #endif
  382. );
  383. ANKI_CHECK(m_renderer->render(rqueue, presentableTex));
  384. // Pause and sync async loader. That will force all tasks before the pause to finish in this frame.
  385. m_resources->getAsyncLoader().pause();
  386. m_gr->swapBuffers();
  387. m_stagingMem->endFrame();
  388. // Update the trace info with some async loader stats
  389. U64 asyncTaskCount = m_resources->getAsyncLoader().getCompletedTaskCount();
  390. ANKI_TRACE_INC_COUNTER(RESOURCE_ASYNC_TASKS, asyncTaskCount - m_resourceCompletedAsyncTaskCount);
  391. m_resourceCompletedAsyncTaskCount = asyncTaskCount;
  392. // Now resume the loader
  393. m_resources->getAsyncLoader().resume();
  394. // Sleep
  395. const Second endTime = HighRezTimer::getCurrentTime();
  396. const Second frameTime = endTime - startTime;
  397. const Second timerTick = 1.0 / Second(m_config->getCoreTargetFps());
  398. if(frameTime < timerTick)
  399. {
  400. ANKI_TRACE_SCOPED_EVENT(TIMER_TICK_SLEEP);
  401. HighRezTimer::sleep(timerTick - frameTime);
  402. }
  403. // Stats
  404. if(m_config->getCoreDisplayStats())
  405. {
  406. StatsUi& statsUi = *static_cast<StatsUi*>(m_statsUi.get());
  407. statsUi.setFrameTime(frameTime);
  408. statsUi.setRenderTime(m_renderer->getStats().m_renderingCpuTime);
  409. statsUi.setSceneUpdateTime(m_scene->getStats().m_updateTime);
  410. statsUi.setVisibilityTestsTime(m_scene->getStats().m_visibilityTestsTime);
  411. statsUi.setPhysicsTime(m_scene->getStats().m_physicsUpdate);
  412. statsUi.setGpuTime(m_renderer->getStats().m_renderingGpuTime);
  413. if(m_maliHwCounters)
  414. {
  415. MaliHwCountersOut out;
  416. m_maliHwCounters->sample(out);
  417. statsUi.setGpuActiveCycles(out.m_gpuActive);
  418. statsUi.setGpuReadBandwidth(out.m_readBandwidth);
  419. statsUi.setGpuWriteBandwidth(out.m_writeBandwidth);
  420. }
  421. statsUi.setAllocatedCpuMemory(m_memStats.m_allocatedMem.load());
  422. statsUi.setCpuAllocationCount(m_memStats.m_allocCount.load());
  423. statsUi.setCpuFreeCount(m_memStats.m_freeCount.load());
  424. statsUi.setGrStats(m_gr->getStats());
  425. BuddyAllocatorBuilderStats vertMemStats;
  426. m_vertexMem->getMemoryStats(vertMemStats);
  427. statsUi.setGlobalVertexMemoryPoolStats(vertMemStats);
  428. statsUi.setDrawableCount(rqueue.countAllRenderables());
  429. }
  430. #if ANKI_ENABLE_TRACE
  431. if(m_renderer->getStats().m_renderingGpuTime >= 0.0)
  432. {
  433. ANKI_TRACE_CUSTOM_EVENT(GPU_TIME, m_renderer->getStats().m_renderingGpuSubmitTimestamp,
  434. m_renderer->getStats().m_renderingGpuTime);
  435. }
  436. #endif
  437. ++m_globalTimestamp;
  438. }
  439. #if ANKI_ENABLE_TRACE
  440. static U64 frame = 1;
  441. m_coreTracer->flushFrame(frame++);
  442. #endif
  443. }
  444. return Error::NONE;
  445. }
  446. void App::injectUiElements(DynamicArrayAuto<UiQueueElement>& newUiElementArr, RenderQueue& rqueue)
  447. {
  448. const U32 originalCount = rqueue.m_uis.getSize();
  449. if(m_config->getCoreDisplayStats() || m_consoleEnabled)
  450. {
  451. const U32 extraElements = m_config->getCoreDisplayStats() + (m_consoleEnabled != 0);
  452. newUiElementArr.create(originalCount + extraElements);
  453. if(originalCount > 0)
  454. {
  455. memcpy(&newUiElementArr[0], &rqueue.m_uis[0], rqueue.m_uis.getSizeInBytes());
  456. }
  457. rqueue.m_uis = WeakArray<UiQueueElement>(newUiElementArr);
  458. }
  459. U32 count = originalCount;
  460. if(m_config->getCoreDisplayStats())
  461. {
  462. newUiElementArr[count].m_userData = m_statsUi.get();
  463. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  464. static_cast<StatsUi*>(userData)->build(canvas);
  465. };
  466. ++count;
  467. }
  468. if(m_consoleEnabled)
  469. {
  470. newUiElementArr[count].m_userData = m_console.get();
  471. newUiElementArr[count].m_drawCallback = [](CanvasPtr& canvas, void* userData) -> void {
  472. static_cast<DeveloperConsole*>(userData)->build(canvas);
  473. };
  474. ++count;
  475. }
  476. }
  477. void App::initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData)
  478. {
  479. if(m_config->getCoreDisplayStats())
  480. {
  481. m_memStats.m_originalAllocCallback = allocCb;
  482. m_memStats.m_originalUserData = allocCbUserData;
  483. m_allocCb = MemStats::allocCallback;
  484. m_allocCbData = &m_memStats;
  485. }
  486. else
  487. {
  488. m_allocCb = allocCb;
  489. m_allocCbData = allocCbUserData;
  490. }
  491. }
  492. void App::setSignalHandlers()
  493. {
  494. auto handler = [](int signum) -> void {
  495. const char* name = nullptr;
  496. switch(signum)
  497. {
  498. case SIGABRT:
  499. name = "SIGABRT";
  500. break;
  501. case SIGSEGV:
  502. name = "SIGSEGV";
  503. break;
  504. #if ANKI_POSIX
  505. case SIGBUS:
  506. name = "SIGBUS";
  507. break;
  508. #endif
  509. case SIGILL:
  510. name = "SIGILL";
  511. break;
  512. case SIGFPE:
  513. name = "SIGFPE";
  514. break;
  515. }
  516. if(name)
  517. printf("Caught signal %d (%s)\n", signum, name);
  518. else
  519. printf("Caught signal %d\n", signum);
  520. U32 count = 0;
  521. printf("Backtrace:\n");
  522. backtrace(HeapAllocator<U8>(allocAligned, nullptr), [&count](CString symbol) {
  523. printf("%.2u: %s\n", count++, symbol.cstr());
  524. });
  525. ANKI_DEBUG_BREAK();
  526. };
  527. signal(SIGSEGV, handler);
  528. signal(SIGILL, handler);
  529. signal(SIGFPE, handler);
  530. #if ANKI_POSIX
  531. signal(SIGBUS, handler);
  532. #endif
  533. // Ignore for now: signal(SIGABRT, handler);
  534. }
  535. } // end namespace anki