ScriptAutomationSystemComponent.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <ScriptAutomation/ScriptAutomationBus.h>
  10. #include <ImageComparisonSettings.h>
  11. #include <AzCore/Component/Component.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzCore/std/smart_ptr/unique_ptr.h>
  14. #include <AzFramework/API/ApplicationAPI.h>
  15. #include <Atom/Feature/Utils/ProfilingCaptureBus.h>
  16. #include <Atom/Feature/Utils/FrameCaptureBus.h>
  17. #include <Atom/Feature/Utils/FrameCaptureTestBus.h>
  18. namespace AZ
  19. {
  20. class ScriptContext;
  21. } // namespace AZ
  22. namespace AZ::ScriptAutomation
  23. {
  24. //! Manages running lua scripts for test automation.
  25. //! This initializes a lua context, binds C++ callback functions and does per-frame processing
  26. //! to execute scripts
  27. //!
  28. //! This uses an asynchronous execution model, which is necessary in order to allow scripts to
  29. //! simply call functions like IdleFrames() or IdleSeconds() to insert delays, making scripts
  30. //! much easier to write. When a script runs, every callback function adds an entry to an operations
  31. //! queue, and the Tick() function works its way through this queue every frame.
  32. //! Note that this means the C++ functions we expose to lua cannot return dynamic data; the only
  33. //! data we can return are constants like the number of samples available, or stateless utility
  34. //! functions
  35. class ScriptAutomationSystemComponent
  36. : public AZ::Component
  37. , public AZ::TickBus::Handler
  38. , public ScriptAutomationRequestBus::Handler
  39. , public AZ::Render::ProfilingCaptureNotificationBus::Handler
  40. , public AZ::Render::FrameCaptureNotificationBus::Handler
  41. , public AzFramework::LevelSystemLifecycleNotificationBus::Handler
  42. {
  43. public:
  44. AZ_COMPONENT(ScriptAutomationSystemComponent, "{755280BF-F227-4048-B323-D5E28EC55D61}", ScriptAutomationRequests);
  45. static void Reflect(AZ::ReflectContext* context);
  46. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  47. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible);
  48. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  49. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  50. ScriptAutomationSystemComponent();
  51. ~ScriptAutomationSystemComponent();
  52. void SetIdleFrames(int numFrames) override;
  53. void SetIdleSeconds(float numSeconds) override;
  54. void SetFrameCaptureId(AZ::Render::FrameCaptureId frameCaptureId) override;
  55. void StartProfilingCapture() override;
  56. void ActivateScript(const char* scriptPath) override;
  57. void DeactivateScripts() override;
  58. protected:
  59. // AZ::Component implementation
  60. void Activate() override;
  61. void Deactivate() override;
  62. // AZ::TickBus implementation
  63. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  64. // ScriptAutomationRequests implementation
  65. AZ::BehaviorContext* GetAutomationContext() override;
  66. void PauseAutomation(float timeout = DefaultPauseTimeout) override;
  67. void ResumeAutomation() override;
  68. void QueueScriptOperation(ScriptAutomationRequests::ScriptOperation&& operation) override;
  69. void ExecuteScript(const char* scriptFilePath) override;
  70. const ImageComparisonToleranceLevel* FindToleranceLevel(const AZStd::string& name) override;
  71. void LoadLevel(const char* levelName) override;
  72. // FrameCaptureNotificationBus implementation
  73. void OnFrameCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string& info) override;
  74. // ProfilingCaptureNotificationBus implementation
  75. void OnCaptureQueryTimestampFinished(bool result, const AZStd::string& info) override;
  76. void OnCaptureCpuFrameTimeFinished(bool result, const AZStd::string& info) override;
  77. void OnCaptureQueryPipelineStatisticsFinished(bool result, const AZStd::string& info) override;
  78. void OnCaptureBenchmarkMetadataFinished(bool result, const AZStd::string& info) override;
  79. // LevelSystemLifecycleNotificationBus implementation
  80. void OnLevelNotFound(const char* levelName) override;
  81. void OnLoadingComplete(const char* levelName) override;
  82. void OnLoadingError(const char* levelName, const char* error) override;
  83. AZStd::unique_ptr<AZ::ScriptContext> m_scriptContext; //< Provides the lua scripting system
  84. AZStd::unique_ptr<AZ::BehaviorContext> m_scriptBehaviorContext; //< Used to bind script callback functions to lua
  85. ImageComparisonSettings m_imageComparisonSettings;
  86. AZStd::queue<ScriptAutomationRequests::ScriptOperation> m_scriptOperations;
  87. AZStd::string m_automationScript;
  88. AZStd::string m_levelName;
  89. bool m_levelLoading = false;
  90. int m_scriptIdleFrames = 0;
  91. float m_scriptIdleSeconds = 0.0f;
  92. float m_scriptPauseTimeout = 0.0f;
  93. bool m_scriptPaused = false;
  94. AZ::Render::FrameCaptureId m_scriptFrameCaptureId = AZ::Render::InvalidFrameCaptureId;
  95. bool m_isStarted = false;
  96. bool m_exitOnFinish = false;
  97. bool m_doFinalCleanup = false;
  98. };
  99. } // namespace AZ::ScriptAutomation