OptickProfilerEventForwarder.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/Component/TickBus.h>
  10. #include <AzCore/Debug/Profiler.h>
  11. #include <AzCore/Memory/SystemAllocator.h>
  12. #include <AzCore/Name/Name.h>
  13. #include <AzCore/RTTI/RTTI.h>
  14. #include <AzCore/std/containers/unordered_map.h>
  15. #include <AzCore/std/parallel/mutex.h>
  16. #include <AzCore/std/parallel/shared_mutex.h>
  17. #include <AzCore/std/parallel/threadbus.h>
  18. namespace Optick
  19. {
  20. struct EventDescription;
  21. struct EventStorage;
  22. } // namespace Optick
  23. namespace OptickProfiler
  24. {
  25. //! Listen to 03DE frame/profiling events and forward them to the Optick profiling library
  26. class OptickProfilerEventForwarder final
  27. : public AZ::Debug::Profiler
  28. , public AZ::TickBus::Handler
  29. , public AZStd::ThreadEventBus::Handler
  30. {
  31. public:
  32. AZ_RTTI(OptickProfilerEventForwarder, "{E4076EA4-EF44-499A-9750-37B623BBBF7C}", AZ::Debug::Profiler);
  33. AZ_CLASS_ALLOCATOR(OptickProfilerEventForwarder, AZ::SystemAllocator);
  34. OptickProfilerEventForwarder() = default;
  35. ~OptickProfilerEventForwarder() = default;
  36. //! Registers/un-registers the AZ::Debug::Profiler instance to the interface
  37. void Init();
  38. void Shutdown();
  39. //! AZ::Debug::Profiler overrides
  40. void BeginRegion(const AZ::Debug::Budget* budget, const char* eventName, ...) final override;
  41. void EndRegion(const AZ::Debug::Budget* budget) final override;
  42. //! AZ::TickBus::Handler overrides
  43. int GetTickOrder() final override;
  44. void OnTick(float deltaTime, AZ::ScriptTimePoint timePoint) final override;
  45. //! AZStd::ThreadEventBus::Handler overrides
  46. virtual void OnThreadEnter(const AZStd::thread::id& id, const AZStd::thread_desc* desc) final override;
  47. virtual void OnThreadExit(const AZStd::thread::id& id) final override;
  48. private:
  49. // This lock will only be contested when the OptickProfilerEventForwarder's Shutdown() method has been called
  50. AZStd::shared_mutex m_shutdownMutex;
  51. AZStd::shared_mutex m_threadIdToNameMutex;
  52. AZStd::unordered_map<AZStd::native_thread_id_type, AZStd::string> m_threadIdToName;
  53. static thread_local Optick::EventStorage* m_pOptickStorage;
  54. Optick::EventDescription* m_pOptickFrameTag = nullptr;
  55. bool m_initialized = false;
  56. };
  57. } // namespace OptickProfiler