ImGuiTreemapImpl.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzCore/Name/Name.h>
  10. #include <AzCore/std/containers/unordered_map.h>
  11. #include <AzCore/std/containers/unordered_set.h>
  12. #include <AzCore/std/parallel/mutex.h>
  13. #include <Profiler/ImGuiTreemap.h>
  14. namespace Profiler
  15. {
  16. class ImGuiTreemapImpl : public ImGuiTreemap
  17. {
  18. public:
  19. ~ImGuiTreemapImpl() override = default;
  20. const AZ::Name& GetName() const override;
  21. void SetName(const char* name) override;
  22. void SetName(AZ::Name name) override;
  23. void SetUnitLabel(const char* unitLabel) override;
  24. void AddMask(const char* label, uint32_t mask) override;
  25. void SetRoots(AZStd::vector<TreemapNode>&& roots) override;
  26. void Render(int x, int y, int w, int h) override;
  27. void WeighAndComputeLayout(int w, int h) override;
  28. private:
  29. void WeighNodes();
  30. void AssignColors();
  31. void ComputeLayout(int w, int h);
  32. void SquarifyChildren(TreemapNode& node);
  33. AZ::Name m_name;
  34. const char* m_unitLabel;
  35. TreemapNode m_root;
  36. // Stores pointers to nodes at the same depth level. This is needed for both weight normalization
  37. // and back-to-front drawing for ImGui
  38. AZStd::vector<AZStd::vector<TreemapNode*>> m_levelSets;
  39. // Masks are used to include or exclude nodes from the treemap
  40. AZStd::unordered_map<const char*, uint32_t> m_masks;
  41. // The total sum of all weights for nodes occupying a given depth in the tree
  42. AZStd::vector<float> m_levelWeights;
  43. // The set of groups used to tag constituent nodes
  44. struct GroupConfig
  45. {
  46. bool m_active = false;
  47. float m_hue;
  48. float m_saturation;
  49. };
  50. AZStd::unordered_map<AZ::Name, GroupConfig> m_groups;
  51. // This set of nodes are reset each frame and used to store treemap nodes beneath the user cursor
  52. AZStd::vector<TreemapNode*> m_tooltipNodes;
  53. TreemapNode* m_selectedNode = nullptr;
  54. // A common pattern in the implementation is to use a stack to traverse the tree via BFS. Keeping
  55. // the stack around as a member variable avoids unnecessary allocations and maintains a reservation
  56. // that matches the maximum size the stack expands to.
  57. AZStd::vector<TreemapNode*> m_stack;
  58. int m_lastExtent[2] = { 0, 0 };
  59. uint32_t m_currentMask = 0xffffffff;
  60. bool m_dirty = true;
  61. };
  62. class ImGuiTreemapFactoryImpl : public ImGuiTreemapFactory
  63. {
  64. public:
  65. ImGuiTreemap& Create(AZ::Name name, const char* unitLabel) override;
  66. void Destroy(ImGuiTreemap* treemap) override;
  67. private:
  68. AZStd::mutex m_mutex;
  69. AZStd::unordered_map<AZ::Name, ImGuiTreemapImpl> m_treemaps;
  70. };
  71. }