From 8972ebae7346afd116a4d0636214cd174e35cacb Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 15 Aug 2018 16:28:39 -0700 Subject: [PATCH] Metrics: Added io.MetricsActiveAllocations, moving away from the cross-context global counters than we previously used. (#1565, #1599, #586) --- CHANGELOG.txt | 1 + imgui.cpp | 12 +++++++----- imgui.h | 1 + 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 959125e4b..5cc4f75f4 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -70,6 +70,7 @@ Other Changes: - Metrics: Changed io.MetricsActiveWindows to reflect the number of active windows (!= from visible windows), which is useful for lazy/idle render mechanisms as new windows are typically not visible for one frame. - Metrics: Added io.MetricsRenderWindow to reflect the number of visible windows. + - Metrics: Added io.MetricsActiveAllocations, moving away from the cross-context global counters than we previously used. (#1565, #1599, #586) - Demo: Added basic Drag and Drop demo. (#143) - Demo: Clarified the use of IsItemHovered()/IsItemActive() right after being in the "Active, Focused, Hovered & Focused Tests" section. - Examples: Tweaked the main.cpp of each example. diff --git a/imgui.cpp b/imgui.cpp index 885c79e3b..09dde3f51 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -933,7 +933,7 @@ static void SetClipboardTextFn_DefaultImpl(void* user_data, const ch static void ImeSetInputScreenPosFn_DefaultImpl(int x, int y); //----------------------------------------------------------------------------- -// Context +// Context and Memory Allocators //----------------------------------------------------------------------------- // Current context pointer. Implicitly used by all ImGui functions. Always assumed to be != NULL. @@ -960,7 +960,6 @@ static void FreeWrapper(void* ptr, void* user_data) { (void)user_data; static void* (*GImAllocatorAllocFunc)(size_t size, void* user_data) = MallocWrapper; static void (*GImAllocatorFreeFunc)(void* ptr, void* user_data) = FreeWrapper; static void* GImAllocatorUserData = NULL; -static size_t GImAllocatorActiveAllocationsCount = 0; //----------------------------------------------------------------------------- // User facing structures @@ -2845,13 +2844,16 @@ float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) void* ImGui::MemAlloc(size_t size) { - GImAllocatorActiveAllocationsCount++; + if (ImGuiContext* ctx = GImGui) + ctx->IO.MetricsActiveAllocations++; return GImAllocatorAllocFunc(size, GImAllocatorUserData); } void ImGui::MemFree(void* ptr) { - if (ptr) GImAllocatorActiveAllocationsCount--; + if (ptr) + if (ImGuiContext* ctx = GImGui) + ctx->IO.MetricsActiveAllocations--; return GImAllocatorFreeFunc(ptr, GImAllocatorUserData); } @@ -14032,7 +14034,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate); ImGui::Text("%d vertices, %d indices (%d triangles)", io.MetricsRenderVertices, io.MetricsRenderIndices, io.MetricsRenderIndices / 3); ImGui::Text("%d active windows (%d visible)", io.MetricsActiveWindows, io.MetricsRenderWindows); - ImGui::Text("%d allocations", (int)GImAllocatorActiveAllocationsCount); + ImGui::Text("%d allocations", io.MetricsActiveAllocations); ImGui::Checkbox("Show clipping rectangles when hovering draw commands", &show_draw_cmd_clip_rects); ImGui::Checkbox("Ctrl shows window begin order", &show_window_begin_order); diff --git a/imgui.h b/imgui.h index 70ca231b4..78f135686 100644 --- a/imgui.h +++ b/imgui.h @@ -1161,6 +1161,7 @@ struct ImGuiIO int MetricsRenderIndices; // Indices output during last call to Render() = number of triangles * 3 int MetricsRenderWindows; // Number of visible windows int MetricsActiveWindows; // Number of active windows + int MetricsActiveAllocations; // Number of active allocations, updated by MemAlloc/MemFree based on current context. May be off if you have multiple imgui contexts. ImVec2 MouseDelta; // Mouse delta. Note that this is zero if either current or previous position are invalid (-FLT_MAX,-FLT_MAX), so a disappearing/reappearing mouse won't have a huge delta. //------------------------------------------------------------------