Browse Source

Make the UiManager a singleton

Panagiotis Christopoulos Charitos 2 years ago
parent
commit
07dc2b51d0

+ 4 - 8
AnKi/Core/App.cpp

@@ -124,8 +124,7 @@ void App::cleanup()
 	m_script = nullptr;
 	m_script = nullptr;
 	deleteInstance(m_mainPool, m_renderer);
 	deleteInstance(m_mainPool, m_renderer);
 	m_renderer = nullptr;
 	m_renderer = nullptr;
-	deleteInstance(m_mainPool, m_ui);
-	m_ui = nullptr;
+	UiManager::freeSingleton();
 	GpuSceneMicroPatcher::freeSingleton();
 	GpuSceneMicroPatcher::freeSingleton();
 	ResourceManager::freeSingleton();
 	ResourceManager::freeSingleton();
 	PhysicsWorld::freeSingleton();
 	PhysicsWorld::freeSingleton();
@@ -319,8 +318,7 @@ Error App::initInternal(AllocAlignedCallback allocCb, void* allocCbUserData)
 	uiInitInfo.m_allocCallback = m_mainPool.getAllocationCallback();
 	uiInitInfo.m_allocCallback = m_mainPool.getAllocationCallback();
 	uiInitInfo.m_allocCallbackUserData = m_mainPool.getAllocationCallbackUserData();
 	uiInitInfo.m_allocCallbackUserData = m_mainPool.getAllocationCallbackUserData();
 	uiInitInfo.m_grManager = m_gr;
 	uiInitInfo.m_grManager = m_gr;
-	m_ui = newInstance<UiManager>(m_mainPool);
-	ANKI_CHECK(m_ui->init(uiInitInfo));
+	ANKI_CHECK(UiManager::allocateSingleton().init(uiInitInfo));
 
 
 	//
 	//
 	// GPU scene
 	// GPU scene
@@ -336,7 +334,6 @@ Error App::initInternal(AllocAlignedCallback allocCb, void* allocCbUserData)
 	renderInit.m_allocCallback = m_mainPool.getAllocationCallback();
 	renderInit.m_allocCallback = m_mainPool.getAllocationCallback();
 	renderInit.m_allocCallbackUserData = m_mainPool.getAllocationCallbackUserData();
 	renderInit.m_allocCallbackUserData = m_mainPool.getAllocationCallbackUserData();
 	renderInit.m_grManager = m_gr;
 	renderInit.m_grManager = m_gr;
-	renderInit.m_uiManager = m_ui;
 	renderInit.m_globTimestamp = &m_globalTimestamp;
 	renderInit.m_globTimestamp = &m_globalTimestamp;
 	m_renderer = newInstance<MainRenderer>(m_mainPool);
 	m_renderer = newInstance<MainRenderer>(m_mainPool);
 	ANKI_CHECK(m_renderer->init(renderInit));
 	ANKI_CHECK(m_renderer->init(renderInit));
@@ -357,7 +354,6 @@ Error App::initInternal(AllocAlignedCallback allocCb, void* allocCbUserData)
 	sceneInit.m_allocCallbackData = m_mainPool.getAllocationCallbackUserData();
 	sceneInit.m_allocCallbackData = m_mainPool.getAllocationCallbackUserData();
 	sceneInit.m_globalTimestamp = &m_globalTimestamp;
 	sceneInit.m_globalTimestamp = &m_globalTimestamp;
 	sceneInit.m_scriptManager = m_script;
 	sceneInit.m_scriptManager = m_script;
-	sceneInit.m_uiManager = m_ui;
 	sceneInit.m_grManager = m_gr;
 	sceneInit.m_grManager = m_gr;
 	ANKI_CHECK(m_scene->init(sceneInit));
 	ANKI_CHECK(m_scene->init(sceneInit));
 
 
@@ -368,8 +364,8 @@ Error App::initInternal(AllocAlignedCallback allocCb, void* allocCbUserData)
 	//
 	//
 	// Misc
 	// Misc
 	//
 	//
-	ANKI_CHECK(m_ui->newInstance<StatsUi>(m_statsUi));
-	ANKI_CHECK(m_ui->newInstance<DeveloperConsole>(m_console, m_script));
+	ANKI_CHECK(UiManager::getSingleton().newInstance<StatsUi>(m_statsUi));
+	ANKI_CHECK(UiManager::getSingleton().newInstance<DeveloperConsole>(m_console, m_script));
 
 
 	ANKI_CORE_LOGI("Application initialized");
 	ANKI_CORE_LOGI("Application initialized");
 
 

+ 0 - 2
AnKi/Core/App.h

@@ -17,7 +17,6 @@ class GrManager;
 class MainRenderer;
 class MainRenderer;
 class SceneGraph;
 class SceneGraph;
 class ScriptManager;
 class ScriptManager;
-class UiManager;
 class UiQueueElement;
 class UiQueueElement;
 class RenderQueue;
 class RenderQueue;
 
 
@@ -91,7 +90,6 @@ private:
 
 
 	// Sybsystems
 	// Sybsystems
 	GrManager* m_gr = nullptr;
 	GrManager* m_gr = nullptr;
-	UiManager* m_ui = nullptr;
 	MainRenderer* m_renderer = nullptr;
 	MainRenderer* m_renderer = nullptr;
 	SceneGraph* m_scene = nullptr;
 	SceneGraph* m_scene = nullptr;
 	ScriptManager* m_script = nullptr;
 	ScriptManager* m_script = nullptr;

+ 7 - 9
AnKi/Core/DeveloperConsole.cpp

@@ -15,9 +15,7 @@ DeveloperConsole::~DeveloperConsole()
 	{
 	{
 		LogItem* item = &m_logItems.getFront();
 		LogItem* item = &m_logItems.getFront();
 		m_logItems.popFront();
 		m_logItems.popFront();
-		item->m_threadName.destroy(getMemoryPool());
-		item->m_msg.destroy(getMemoryPool());
-		deleteInstance(getMemoryPool(), item);
+		deleteInstance(UiMemoryPool::getSingleton(), item);
 	}
 	}
 }
 }
 
 
@@ -25,7 +23,7 @@ Error DeveloperConsole::init(ScriptManager* scriptManager)
 {
 {
 	zeroMemory(m_inputText);
 	zeroMemory(m_inputText);
 
 
-	ANKI_CHECK(m_manager->newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{16}));
+	ANKI_CHECK(UiManager::getSingleton().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{16}));
 
 
 	// Add a new callback to the logger
 	// Add a new callback to the logger
 	Logger::getSingleton().addMessageHandler(this, loggerCallback);
 	Logger::getSingleton().addMessageHandler(this, loggerCallback);
@@ -122,8 +120,8 @@ void DeveloperConsole::newLogItem(const LoggerMessageInfo& inf)
 		LogItem* first = &m_logItems.getFront();
 		LogItem* first = &m_logItems.getFront();
 		m_logItems.popFront();
 		m_logItems.popFront();
 
 
-		first->m_msg.destroy(getMemoryPool());
-		first->m_threadName.destroy(getMemoryPool());
+		first->m_msg.destroy();
+		first->m_threadName.destroy();
 
 
 		// Re-use the log item
 		// Re-use the log item
 		newLogItem = first;
 		newLogItem = first;
@@ -131,15 +129,15 @@ void DeveloperConsole::newLogItem(const LoggerMessageInfo& inf)
 	}
 	}
 	else
 	else
 	{
 	{
-		newLogItem = newInstance<LogItem>(getMemoryPool());
+		newLogItem = newInstance<LogItem>(UiMemoryPool::getSingleton());
 	}
 	}
 
 
 	// Create the new item
 	// Create the new item
 	newLogItem->m_file = inf.m_file;
 	newLogItem->m_file = inf.m_file;
 	newLogItem->m_func = inf.m_func;
 	newLogItem->m_func = inf.m_func;
 	newLogItem->m_subsystem = inf.m_subsystem;
 	newLogItem->m_subsystem = inf.m_subsystem;
-	newLogItem->m_threadName.create(getMemoryPool(), inf.m_threadName);
-	newLogItem->m_msg.create(getMemoryPool(), inf.m_msg);
+	newLogItem->m_threadName.create(inf.m_threadName);
+	newLogItem->m_msg.create(inf.m_msg);
 	newLogItem->m_line = inf.m_line;
 	newLogItem->m_line = inf.m_line;
 	newLogItem->m_type = inf.m_type;
 	newLogItem->m_type = inf.m_type;
 
 

+ 3 - 6
AnKi/Core/DeveloperConsole.h

@@ -19,10 +19,7 @@ namespace anki {
 class DeveloperConsole : public UiImmediateModeBuilder
 class DeveloperConsole : public UiImmediateModeBuilder
 {
 {
 public:
 public:
-	DeveloperConsole(UiManager* ui)
-		: UiImmediateModeBuilder(ui)
-	{
-	}
+	DeveloperConsole() = default;
 
 
 	~DeveloperConsole();
 	~DeveloperConsole();
 
 
@@ -39,8 +36,8 @@ private:
 		const Char* m_file;
 		const Char* m_file;
 		const Char* m_func;
 		const Char* m_func;
 		const Char* m_subsystem;
 		const Char* m_subsystem;
-		String m_threadName;
-		String m_msg;
+		UiString m_threadName;
+		UiString m_msg;
 		I32 m_line;
 		I32 m_line;
 		LoggerMessageType m_type;
 		LoggerMessageType m_type;
 	};
 	};

+ 2 - 2
AnKi/Core/StatsUi.cpp

@@ -16,7 +16,7 @@ StatsUi::~StatsUi()
 
 
 Error StatsUi::init()
 Error StatsUi::init()
 {
 {
-	ANKI_CHECK(m_manager->newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{24}));
+	ANKI_CHECK(UiManager::getSingleton().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{24}));
 
 
 	return Error::kNone;
 	return Error::kNone;
 }
 }
@@ -36,7 +36,7 @@ void StatsUi::labelBytes(PtrSize val, CString name) const
 
 
 	b = val;
 	b = val;
 
 
-	StringRaii timestamp(&getMemoryPool());
+	UiString timestamp;
 	if(gb)
 	if(gb)
 	{
 	{
 		timestamp.sprintf("%s: %zu,%04zu,%04zu,%04zu", name.cstr(), gb, mb, kb, b);
 		timestamp.sprintf("%s: %zu,%04zu,%04zu,%04zu", name.cstr(), gb, mb, kb, b);

+ 1 - 4
AnKi/Core/StatsUi.h

@@ -37,10 +37,7 @@ enum class StatsUiDetail : U8
 class StatsUi : public UiImmediateModeBuilder
 class StatsUi : public UiImmediateModeBuilder
 {
 {
 public:
 public:
-	StatsUi(UiManager* ui)
-		: UiImmediateModeBuilder(ui)
-	{
-	}
+	StatsUi() = default;
 
 
 	~StatsUi();
 	~StatsUi();
 
 

+ 0 - 1
AnKi/Renderer/Common.h

@@ -76,7 +76,6 @@ class RendererExternalSubsystems
 {
 {
 public:
 public:
 	GrManager* m_grManager = nullptr;
 	GrManager* m_grManager = nullptr;
-	UiManager* m_uiManager = nullptr;
 	Timestamp* m_globTimestamp = nullptr;
 	Timestamp* m_globTimestamp = nullptr;
 };
 };
 
 

+ 4 - 4
AnKi/Renderer/UiStage.cpp

@@ -23,10 +23,10 @@ UiStage::~UiStage()
 
 
 Error UiStage::init()
 Error UiStage::init()
 {
 {
-	ANKI_CHECK(getExternalSubsystems().m_uiManager->newInstance(m_font, "EngineAssets/UbuntuRegular.ttf",
-																Array<U32, 3>{12, 16, 20}));
-	ANKI_CHECK(getExternalSubsystems().m_uiManager->newInstance(
-		m_canvas, m_font, 12, m_r->getPostProcessResolution().x(), m_r->getPostProcessResolution().y()));
+	ANKI_CHECK(
+		UiManager::getSingleton().newInstance(m_font, "EngineAssets/UbuntuRegular.ttf", Array<U32, 3>{12, 16, 20}));
+	ANKI_CHECK(UiManager::getSingleton().newInstance(m_canvas, m_font, 12, m_r->getPostProcessResolution().x(),
+													 m_r->getPostProcessResolution().y()));
 
 
 	return Error::kNone;
 	return Error::kNone;
 }
 }

+ 0 - 1
AnKi/Scene/Common.h

@@ -46,7 +46,6 @@ class SceneGraphExternalSubsystems
 {
 {
 public:
 public:
 	ScriptManager* m_scriptManager = nullptr;
 	ScriptManager* m_scriptManager = nullptr;
-	UiManager* m_uiManager = nullptr;
 	GrManager* m_grManager = nullptr;
 	GrManager* m_grManager = nullptr;
 	const Timestamp* m_globalTimestamp = nullptr;
 	const Timestamp* m_globalTimestamp = nullptr;
 };
 };

+ 2 - 18
AnKi/Ui/Canvas.cpp

@@ -14,18 +14,11 @@
 
 
 namespace anki {
 namespace anki {
 
 
-Canvas::Canvas(UiManager* manager)
-	: UiObject(manager)
-{
-}
-
 Canvas::~Canvas()
 Canvas::~Canvas()
 {
 {
 	if(m_imCtx)
 	if(m_imCtx)
 	{
 	{
-		setImAllocator();
 		ImGui::DestroyContext(m_imCtx);
 		ImGui::DestroyContext(m_imCtx);
-		unsetImAllocator();
 	}
 	}
 }
 }
 
 
@@ -58,11 +51,7 @@ Error Canvas::init(FontPtr font, U32 fontHeight, U32 width, U32 height)
 	samplerInit.m_mipmapFilter = SamplingFilter::kNearest;
 	samplerInit.m_mipmapFilter = SamplingFilter::kNearest;
 	m_nearestNearestRepeatSampler = getExternalSubsystems().m_grManager->newSampler(samplerInit);
 	m_nearestNearestRepeatSampler = getExternalSubsystems().m_grManager->newSampler(samplerInit);
 
 
-	// Allocator
-	m_tempPool.init(getMemoryPool().getAllocationCallback(), getMemoryPool().getAllocationCallbackUserData(), 512_B);
-
 	// Create the context
 	// Create the context
-	setImAllocator();
 	m_imCtx = ImGui::CreateContext(font->getImFontAtlas());
 	m_imCtx = ImGui::CreateContext(font->getImFontAtlas());
 	ImGui::SetCurrentContext(m_imCtx);
 	ImGui::SetCurrentContext(m_imCtx);
 	ImGui::GetIO().IniFilename = nullptr;
 	ImGui::GetIO().IniFilename = nullptr;
@@ -97,7 +86,6 @@ Error Canvas::init(FontPtr font, U32 fontHeight, U32 width, U32 height)
 #undef ANKI_HANDLE
 #undef ANKI_HANDLE
 
 
 	ImGui::SetCurrentContext(nullptr);
 	ImGui::SetCurrentContext(nullptr);
-	unsetImAllocator();
 
 
 	return Error::kNone;
 	return Error::kNone;
 }
 }
@@ -107,7 +95,6 @@ void Canvas::handleInput()
 	const Input& in = Input::getSingleton();
 	const Input& in = Input::getSingleton();
 
 
 	// Begin
 	// Begin
-	setImAllocator();
 	ImGui::SetCurrentContext(m_imCtx);
 	ImGui::SetCurrentContext(m_imCtx);
 	ImGuiIO& io = ImGui::GetIO();
 	ImGuiIO& io = ImGui::GetIO();
 
 
@@ -166,12 +153,10 @@ void Canvas::handleInput()
 
 
 	// End
 	// End
 	ImGui::SetCurrentContext(nullptr);
 	ImGui::SetCurrentContext(nullptr);
-	unsetImAllocator();
 }
 }
 
 
 void Canvas::beginBuilding()
 void Canvas::beginBuilding()
 {
 {
-	setImAllocator();
 	ImGui::SetCurrentContext(m_imCtx);
 	ImGui::SetCurrentContext(m_imCtx);
 
 
 	ImGuiIO& io = ImGui::GetIO();
 	ImGuiIO& io = ImGui::GetIO();
@@ -184,7 +169,7 @@ void Canvas::beginBuilding()
 
 
 void Canvas::pushFont(const FontPtr& font, U32 fontHeight)
 void Canvas::pushFont(const FontPtr& font, U32 fontHeight)
 {
 {
-	m_references.pushBack(m_tempPool, IntrusivePtr<UiObject>(const_cast<Font*>(font.get())));
+	m_references.pushBack(UiObjectPtr(const_cast<Font*>(font.get())));
 	ImGui::PushFont(&font->getImFont(fontHeight));
 	ImGui::PushFont(&font->getImFont(fontHeight));
 }
 }
 
 
@@ -195,8 +180,7 @@ void Canvas::appendToCommandBuffer(CommandBufferPtr cmdb)
 	// Done
 	// Done
 	ImGui::SetCurrentContext(nullptr);
 	ImGui::SetCurrentContext(nullptr);
 
 
-	m_references.destroy(m_tempPool);
-	m_tempPool.reset();
+	m_references.destroy();
 }
 }
 
 
 void Canvas::appendToCommandBufferInternal(CommandBufferPtr& cmdb)
 void Canvas::appendToCommandBufferInternal(CommandBufferPtr& cmdb)

+ 3 - 5
AnKi/Ui/Canvas.h

@@ -18,9 +18,9 @@ namespace anki {
 class Canvas : public UiObject
 class Canvas : public UiObject
 {
 {
 public:
 public:
-	Canvas(UiManager* manager);
+	Canvas() = default;
 
 
-	virtual ~Canvas();
+	~Canvas();
 
 
 	Error init(FontPtr font, U32 fontHeight, U32 width, U32 height);
 	Error init(FontPtr font, U32 fontHeight, U32 width, U32 height);
 
 
@@ -85,9 +85,7 @@ private:
 	SamplerPtr m_linearLinearRepeatSampler;
 	SamplerPtr m_linearLinearRepeatSampler;
 	SamplerPtr m_nearestNearestRepeatSampler;
 	SamplerPtr m_nearestNearestRepeatSampler;
 
 
-	StackMemoryPool m_tempPool;
-
-	List<IntrusivePtr<UiObject>> m_references;
+	UiList<UiObjectPtr> m_references;
 
 
 	void appendToCommandBufferInternal(CommandBufferPtr& cmdb);
 	void appendToCommandBufferInternal(CommandBufferPtr& cmdb);
 };
 };

+ 33 - 7
AnKi/Ui/Common.h

@@ -16,11 +16,8 @@ namespace anki {
 
 
 // Forward
 // Forward
 class UiManager;
 class UiManager;
-class ResourceManager;
 class GrManager;
 class GrManager;
-class Input;
-class ResourceFilesystem;
-class RebarStagingGpuMemoryPool;
+class UiObject;
 
 
 /// @addtogroup ui
 /// @addtogroup ui
 /// @{
 /// @{
@@ -30,15 +27,44 @@ class RebarStagingGpuMemoryPool;
 #define ANKI_UI_LOGW(...) ANKI_LOG("UI", kWarning, __VA_ARGS__)
 #define ANKI_UI_LOGW(...) ANKI_LOG("UI", kWarning, __VA_ARGS__)
 #define ANKI_UI_LOGF(...) ANKI_LOG("UI", kFatal, __VA_ARGS__)
 #define ANKI_UI_LOGF(...) ANKI_LOG("UI", kFatal, __VA_ARGS__)
 
 
-#define ANKI_UI_OBJECT_FW(name_) \
-	class name_; \
-	using name_##Ptr = IntrusivePtr<name_>;
+class UiMemoryPool : public HeapMemoryPool, public MakeSingleton<UiMemoryPool>
+{
+	template<typename>
+	friend class MakeSingleton;
+
+private:
+	UiMemoryPool(AllocAlignedCallback allocCb, void* allocCbUserData)
+		: HeapMemoryPool(allocCb, allocCbUserData, "UiMemPool")
+	{
+	}
+
+	~UiMemoryPool() = default;
+};
+
+ANKI_DEFINE_SUBMODULE_UTIL_CONTAINERS(Ui, UiMemoryPool)
+
+template<typename T>
+class UiObjectDeleter
+{
+public:
+	void operator()(T* x)
+	{
+		static_cast<UiObject*>(x)->~UiObject();
+		UiMemoryPool::getSingleton().free(x);
+	}
+};
+
+#define ANKI_UI_OBJECT_FW(className) \
+	class className; \
+	using className##Ptr = IntrusivePtr<className, UiObjectDeleter<className>>;
 
 
 ANKI_UI_OBJECT_FW(Font)
 ANKI_UI_OBJECT_FW(Font)
 ANKI_UI_OBJECT_FW(Canvas)
 ANKI_UI_OBJECT_FW(Canvas)
 ANKI_UI_OBJECT_FW(UiImmediateModeBuilder)
 ANKI_UI_OBJECT_FW(UiImmediateModeBuilder)
 #undef ANKI_UI_OBJECT
 #undef ANKI_UI_OBJECT
 
 
+using UiObjectPtr = IntrusivePtr<UiObject, UiObjectDeleter<UiObject>>;
+
 class UiExternalSubsystems
 class UiExternalSubsystems
 {
 {
 public:
 public:

+ 2 - 9
AnKi/Ui/Font.cpp

@@ -16,26 +16,20 @@ namespace anki {
 
 
 Font::~Font()
 Font::~Font()
 {
 {
-	setImAllocator();
 	m_imFontAtlas.destroy();
 	m_imFontAtlas.destroy();
-	unsetImAllocator();
-
-	m_fonts.destroy(getMemoryPool());
-	m_fontData.destroy(getMemoryPool());
 }
 }
 
 
 Error Font::init(const CString& filename, ConstWeakArray<U32> fontHeights)
 Error Font::init(const CString& filename, ConstWeakArray<U32> fontHeights)
 {
 {
-	setImAllocator();
 	m_imFontAtlas.init();
 	m_imFontAtlas.init();
 
 
 	// Load font in memory
 	// Load font in memory
 	ResourceFilePtr file;
 	ResourceFilePtr file;
 	ANKI_CHECK(ResourceManager::getSingleton().getFilesystem().openFile(filename, file));
 	ANKI_CHECK(ResourceManager::getSingleton().getFilesystem().openFile(filename, file));
-	m_fontData.create(getMemoryPool(), U32(file->getSize()));
+	m_fontData.create(U32(file->getSize()));
 	ANKI_CHECK(file->read(&m_fontData[0], file->getSize()));
 	ANKI_CHECK(file->read(&m_fontData[0], file->getSize()));
 
 
-	m_fonts.create(getMemoryPool(), U32(fontHeights.getSize()));
+	m_fonts.create(U32(fontHeights.getSize()));
 
 
 	// Bake font
 	// Bake font
 	ImFontConfig cfg;
 	ImFontConfig cfg;
@@ -60,7 +54,6 @@ Error Font::init(const CString& filename, ConstWeakArray<U32> fontHeights)
 	m_imFontAtlas->GetTexDataAsRGBA32(&img, &width, &height);
 	m_imFontAtlas->GetTexDataAsRGBA32(&img, &width, &height);
 	createTexture(img, width, height);
 	createTexture(img, width, height);
 
 
-	unsetImAllocator();
 	return Error::kNone;
 	return Error::kNone;
 }
 }
 
 

+ 3 - 6
AnKi/Ui/Font.h

@@ -19,10 +19,7 @@ namespace anki {
 class Font : public UiObject
 class Font : public UiObject
 {
 {
 public:
 public:
-	Font(UiManager* manager)
-		: UiObject(manager)
-	{
-	}
+	Font() = default;
 
 
 	~Font();
 	~Font();
 
 
@@ -61,7 +58,7 @@ public:
 
 
 private:
 private:
 	ClassWrapper<ImFontAtlas> m_imFontAtlas;
 	ClassWrapper<ImFontAtlas> m_imFontAtlas;
-	DynamicArray<U8> m_fontData;
+	UiDynamicArray<U8> m_fontData;
 
 
 	class FontEntry
 	class FontEntry
 	{
 	{
@@ -70,7 +67,7 @@ private:
 		U32 m_height;
 		U32 m_height;
 	};
 	};
 
 
-	DynamicArray<FontEntry> m_fonts;
+	UiDynamicArray<FontEntry> m_fonts;
 
 
 	TexturePtr m_tex;
 	TexturePtr m_tex;
 	TextureViewPtr m_texView; ///< Whole texture view
 	TextureViewPtr m_texView; ///< Whole texture view

+ 2 - 7
AnKi/Ui/UiImmediateModeBuilder.h

@@ -16,14 +16,9 @@ namespace anki {
 class UiImmediateModeBuilder : public UiObject
 class UiImmediateModeBuilder : public UiObject
 {
 {
 public:
 public:
-	UiImmediateModeBuilder(UiManager* manager)
-		: UiObject(manager)
-	{
-	}
+	UiImmediateModeBuilder() = default;
 
 
-	virtual ~UiImmediateModeBuilder()
-	{
-	}
+	~UiImmediateModeBuilder() = default;
 
 
 	virtual void build(CanvasPtr ctx) = 0;
 	virtual void build(CanvasPtr ctx) = 0;
 
 

+ 17 - 1
AnKi/Ui/UiManager.cpp

@@ -15,12 +15,28 @@ UiManager::UiManager()
 
 
 UiManager::~UiManager()
 UiManager::~UiManager()
 {
 {
+	ImGui::SetAllocatorFunctions(nullptr, nullptr, nullptr);
+	UiMemoryPool::freeSingleton();
 }
 }
 
 
 Error UiManager::init(UiManagerInitInfo& initInfo)
 Error UiManager::init(UiManagerInitInfo& initInfo)
 {
 {
-	m_pool.init(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData);
+	UiMemoryPool::allocateSingleton(initInfo.m_allocCallback, initInfo.m_allocCallbackUserData);
 	m_subsystems = initInfo;
 	m_subsystems = initInfo;
+
+	auto allocCallback = [](size_t size, [[maybe_unused]] void* userData) -> void* {
+		return UiMemoryPool::getSingleton().allocate(size, 16);
+	};
+
+	auto freeCallback = [](void* ptr, [[maybe_unused]] void* userData) -> void {
+		if(ptr)
+		{
+			UiMemoryPool::getSingleton().free(ptr);
+		}
+	};
+
+	ImGui::SetAllocatorFunctions(allocCallback, freeCallback, nullptr);
+
 	return Error::kNone;
 	return Error::kNone;
 }
 }
 
 

+ 16 - 16
AnKi/Ui/UiManager.h

@@ -20,42 +20,42 @@ public:
 };
 };
 
 
 /// UI manager.
 /// UI manager.
-class UiManager
+class UiManager : public MakeSingleton<UiManager>
 {
 {
-	friend class UiObject;
+	template<typename>
+	friend class MakeSingleton;
 
 
 public:
 public:
-	UiManager();
-
-	~UiManager();
-
 	Error init(UiManagerInitInfo& initInfo);
 	Error init(UiManagerInitInfo& initInfo);
 
 
-	HeapMemoryPool& getMemoryPool() const
-	{
-		return m_pool;
-	}
-
 	/// Create a new UI object.
 	/// Create a new UI object.
 	template<typename T, typename Y, typename... Args>
 	template<typename T, typename Y, typename... Args>
-	Error newInstance(IntrusivePtr<Y>& ptr, Args&&... args)
+	Error newInstance(IntrusivePtr<Y, UiObjectDeleter<Y>>& ptr, Args&&... args)
 	{
 	{
-		T* p = anki::newInstance<T>(m_pool, this);
+		T* p = anki::newInstance<T>(UiMemoryPool::getSingleton());
 		ptr.reset(static_cast<Y*>(p));
 		ptr.reset(static_cast<Y*>(p));
 		return p->init(args...);
 		return p->init(args...);
 	}
 	}
 
 
 	/// Create a new UI object.
 	/// Create a new UI object.
 	template<typename T, typename... Args>
 	template<typename T, typename... Args>
-	Error newInstance(IntrusivePtr<T>& ptr, Args&&... args)
+	Error newInstance(IntrusivePtr<T, UiObjectDeleter<T>>& ptr, Args&&... args)
 	{
 	{
-		ptr.reset(anki::newInstance<T>(m_pool, this));
+		ptr.reset(anki::newInstance<T>(UiMemoryPool::getSingleton()));
 		return ptr->init(args...);
 		return ptr->init(args...);
 	}
 	}
 
 
+	UiExternalSubsystems& getExternalSubsystems()
+	{
+		return m_subsystems;
+	}
+
 private:
 private:
-	mutable HeapMemoryPool m_pool;
 	UiExternalSubsystems m_subsystems;
 	UiExternalSubsystems m_subsystems;
+
+	UiManager();
+
+	~UiManager();
 };
 };
 /// @}
 /// @}
 
 

+ 1 - 6
AnKi/Ui/UiObject.cpp

@@ -8,14 +8,9 @@
 
 
 namespace anki {
 namespace anki {
 
 
-HeapMemoryPool& UiObject::getMemoryPool() const
-{
-	return m_manager->getMemoryPool();
-}
-
 UiExternalSubsystems& UiObject::getExternalSubsystems() const
 UiExternalSubsystems& UiObject::getExternalSubsystems() const
 {
 {
-	return m_manager->m_subsystems;
+	return UiManager::getSingleton().getExternalSubsystems();
 }
 }
 
 
 } // end namespace anki
 } // end namespace anki

+ 7 - 37
AnKi/Ui/UiObject.h

@@ -15,17 +15,12 @@ namespace anki {
 /// The base of all UI objects.
 /// The base of all UI objects.
 class UiObject
 class UiObject
 {
 {
-public:
-	UiObject(UiManager* manager)
-		: m_manager(manager)
-	{
-		ANKI_ASSERT(manager);
-	}
+	template<typename>
+	friend class UiObjectDeleter;
 
 
+public:
 	virtual ~UiObject() = default;
 	virtual ~UiObject() = default;
 
 
-	HeapMemoryPool& getMemoryPool() const;
-
 	void retain() const
 	void retain() const
 	{
 	{
 		m_refcount.fetchAdd(1);
 		m_refcount.fetchAdd(1);
@@ -36,38 +31,13 @@ public:
 		return m_refcount.fetchSub(1);
 		return m_refcount.fetchSub(1);
 	}
 	}
 
 
-	/// Set the global IMGUI allocator.
-	void setImAllocator(BaseMemoryPool* pool = nullptr)
-	{
-		pool = (pool) ? pool : &getMemoryPool();
-
-		auto allocCallback = [](size_t size, void* userData) -> void* {
-			BaseMemoryPool* pool = static_cast<BaseMemoryPool*>(userData);
-			return pool->allocate(size, 16);
-		};
-
-		auto freeCallback = [](void* ptr, void* userData) -> void {
-			if(ptr)
-			{
-				BaseMemoryPool* pool = static_cast<BaseMemoryPool*>(userData);
-				pool->free(ptr);
-			}
-		};
-
-		ImGui::SetAllocatorFunctions(allocCallback, freeCallback, pool);
-	}
-
-	/// Unset the global IMGUI allocator.
-	static void unsetImAllocator()
-	{
-		ImGui::SetAllocatorFunctions(nullptr, nullptr, nullptr);
-	}
-
 protected:
 protected:
-	UiManager* m_manager;
-	mutable Atomic<I32> m_refcount = {0};
+	UiObject() = default;
 
 
 	UiExternalSubsystems& getExternalSubsystems() const;
 	UiExternalSubsystems& getExternalSubsystems() const;
+
+private:
+	mutable Atomic<I32> m_refcount = {0};
 };
 };
 /// @}
 /// @}
 
 

+ 3 - 3
Tests/Ui/Ui.cpp

@@ -69,7 +69,7 @@ ANKI_TEST(Ui, Ui)
 	ANKI_TEST_EXPECT_NO_ERR(Input::allocateSingleton().init());
 	ANKI_TEST_EXPECT_NO_ERR(Input::allocateSingleton().init());
 	GrManager* gr = createGrManager(win);
 	GrManager* gr = createGrManager(win);
 	createResourceManager(gr);
 	createResourceManager(gr);
-	UiManager* ui = new UiManager();
+	UiManager* ui = &UiManager::allocateSingleton();
 
 
 	RebarStagingGpuMemoryPool::allocateSingleton().init(gr);
 	RebarStagingGpuMemoryPool::allocateSingleton().init(gr);
 
 
@@ -87,7 +87,7 @@ ANKI_TEST(Ui, Ui)
 		CanvasPtr canvas;
 		CanvasPtr canvas;
 		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(canvas, font, 20, win->getWidth(), win->getHeight()));
 		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(canvas, font, 20, win->getWidth(), win->getHeight()));
 
 
-		IntrusivePtr<Label> label;
+		IntrusivePtr<Label, UiObjectDeleter<Label>> label;
 		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(label));
 		ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(label));
 
 
 		Bool done = false;
 		Bool done = false;
@@ -153,7 +153,7 @@ ANKI_TEST(Ui, Ui)
 		}
 		}
 	}
 	}
 
 
-	delete ui;
+	UiManager::freeSingleton();
 	RebarStagingGpuMemoryPool::freeSingleton();
 	RebarStagingGpuMemoryPool::freeSingleton();
 	ResourceManager::freeSingleton();
 	ResourceManager::freeSingleton();
 	GrManager::deleteInstance(gr);
 	GrManager::deleteInstance(gr);

+ 2 - 2
Tools/Image/ImageViewerMain.cpp

@@ -22,8 +22,8 @@ public:
 			},
 			},
 			this);
 			this);
 
 
-		ANKI_CHECK_AND_IGNORE(getExternalSubsystems().m_uiManager->newInstance(
-			m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{16}));
+		ANKI_CHECK_AND_IGNORE(
+			UiManager::getSingleton().newInstance(m_font, "EngineAssets/UbuntuMonoRegular.ttf", Array<U32, 1>{16}));
 
 
 		ANKI_CHECK_AND_IGNORE(ResourceManager::getSingleton().loadResource(
 		ANKI_CHECK_AND_IGNORE(ResourceManager::getSingleton().loadResource(
 			"ShaderBinaries/UiVisualizeImage.ankiprogbin", m_imageProgram));
 			"ShaderBinaries/UiVisualizeImage.ankiprogbin", m_imageProgram));