Textures: Adding a RefCount to textures so backend can avoid destroying them on shutdown if atlas is shared.

This commit is contained in:
ocornut
2025-03-07 16:14:11 +01:00
committed by ocornut
parent a21a2e855b
commit 208705368e
2 changed files with 17 additions and 7 deletions

View File

@@ -3942,6 +3942,7 @@ ImGuiContext::ImGuiContext(ImFontAtlas* shared_font_atlas)
Font = NULL;
FontSize = FontBaseSize = FontScale = CurrentDpiScale = 0.0f;
IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)();
IO.Fonts->RefCount++;
Time = 0.0f;
FrameCount = 0;
FrameCountEnded = FrameCountRendered = -1;
@@ -4212,12 +4213,15 @@ void ImGui::Shutdown()
IM_ASSERT_USER_ERROR(g.IO.BackendRendererUserData == NULL, "Forgot to shutdown Renderer backend?");
// The fonts atlas can be used prior to calling NewFrame(), so we clear it even if g.Initialized is FALSE (which would happen if we never called NewFrame)
if (g.IO.Fonts)
ImFontAtlasRemoveDrawListSharedData(g.IO.Fonts, &g.DrawListSharedData);
if (g.IO.Fonts && g.FontAtlasOwnedByContext)
if (ImFontAtlas* atlas = g.IO.Fonts)
{
g.IO.Fonts->Locked = false;
IM_DELETE(g.IO.Fonts);
ImFontAtlasRemoveDrawListSharedData(atlas, &g.DrawListSharedData);
atlas->RefCount--;
if (g.FontAtlasOwnedByContext)
{
atlas->Locked = false;
IM_DELETE(atlas);
}
}
g.IO.Fonts = NULL;
g.DrawListSharedData.TempBuffer.clear();
@@ -5192,7 +5196,12 @@ static void ImGui::UpdateTexturesEndFrame()
g.PlatformIO.Textures.resize(0);
g.PlatformIO.Textures.reserve(atlas->TexList.Size);
for (ImTextureData* tex : atlas->TexList)
{
// We provide this information so backends can decide whether to destroy textures.
// This means in practice that if N imgui contexts are created with a shared atlas, we assume all of them have a backend initialized.
tex->RefCount = (unsigned short)atlas->RefCount;
g.PlatformIO.Textures.push_back(tex);
}
}
// Called once a frame. Followed by SetCurrentFont() which sets up the remaining data.