ProfilerImGuiSystemComponent.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <ProfilerImGuiSystemComponent.h>
  9. #include "ImGuiTreemapImpl.h"
  10. #include <AzCore/Debug/ProfilerBus.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/EditContextConstants.inl>
  14. #include <AzFramework/Components/ConsoleBus.h>
  15. namespace Profiler
  16. {
  17. static constexpr AZ::Crc32 profilerImGuiServiceCrc = AZ_CRC_CE("ProfilerImGuiService");
  18. void ProfilerImGuiSystemComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  21. {
  22. serialize->Class<ProfilerImGuiSystemComponent, AZ::Component>()
  23. ->Version(0);
  24. if (AZ::EditContext* ec = serialize->GetEditContext())
  25. {
  26. ec->Class<ProfilerImGuiSystemComponent>("ProfilerImGui", "Provides in-game visualization of the performance data gathered by the ProfilerSystemComponent")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::AutoExpand, true);
  29. }
  30. }
  31. }
  32. void ProfilerImGuiSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  33. {
  34. provided.push_back(profilerImGuiServiceCrc);
  35. }
  36. void ProfilerImGuiSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  37. {
  38. incompatible.push_back(profilerImGuiServiceCrc);
  39. }
  40. void ProfilerImGuiSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  41. {
  42. }
  43. void ProfilerImGuiSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  44. {
  45. }
  46. ProfilerImGuiSystemComponent::ProfilerImGuiSystemComponent()
  47. {
  48. #if defined(IMGUI_ENABLED)
  49. if (ProfilerImGuiInterface::Get() == nullptr)
  50. {
  51. ProfilerImGuiInterface::Register(this);
  52. }
  53. if (!ImGuiTreemapFactory::Interface::Get())
  54. {
  55. ImGuiTreemapFactory::Interface::Register(&m_imguiTreemapFactory);
  56. }
  57. #endif // defined(IMGUI_ENABLED)
  58. }
  59. ProfilerImGuiSystemComponent::~ProfilerImGuiSystemComponent()
  60. {
  61. #if defined(IMGUI_ENABLED)
  62. if (ImGuiTreemapFactory::Interface::Get() == &m_imguiTreemapFactory)
  63. {
  64. ImGuiTreemapFactory::Interface::Unregister(&m_imguiTreemapFactory);
  65. }
  66. if (ProfilerImGuiInterface::Get() == this)
  67. {
  68. ProfilerImGuiInterface::Unregister(this);
  69. }
  70. #endif // defined(IMGUI_ENABLED)
  71. }
  72. void ProfilerImGuiSystemComponent::Activate()
  73. {
  74. #if defined(IMGUI_ENABLED)
  75. ImGui::ImGuiUpdateListenerBus::Handler::BusConnect();
  76. #endif // defined(IMGUI_ENABLED)
  77. }
  78. void ProfilerImGuiSystemComponent::Deactivate()
  79. {
  80. #if defined(IMGUI_ENABLED)
  81. ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect();
  82. #endif // defined(IMGUI_ENABLED)
  83. }
  84. #if defined(IMGUI_ENABLED)
  85. void ProfilerImGuiSystemComponent::ShowCpuProfilerWindow(bool& keepDrawing)
  86. {
  87. m_imguiCpuProfiler.Draw(keepDrawing);
  88. }
  89. void ProfilerImGuiSystemComponent::OnImGuiUpdate()
  90. {
  91. if (m_showCpuProfiler)
  92. {
  93. ShowCpuProfilerWindow(m_showCpuProfiler);
  94. }
  95. }
  96. void ProfilerImGuiSystemComponent::OnImGuiMainMenuUpdate()
  97. {
  98. if (ImGui::BeginMenu("Profiler"))
  99. {
  100. if (ImGui::MenuItem("CPU", "", &m_showCpuProfiler))
  101. {
  102. AZ::Debug::ProfilerSystemInterface::Get()->SetActive(m_showCpuProfiler);
  103. }
  104. ImGui::EndMenu();
  105. }
  106. }
  107. #endif // defined(IMGUI_ENABLED)
  108. } // namespace Profiler