Browse Source

Added static bool to EventProfiler

svifylabs 9 years ago
parent
commit
a91a6cf924

+ 11 - 17
Source/Urho3D/Core/EventProfiler.cpp

@@ -37,7 +37,6 @@ static const int NAME_MAX_LENGTH = 30;
 
 EventProfiler::EventProfiler(Context* context) : 
     Object(context),
-    active_(false),
     current_(0),
     root_(0),
     intervalFrames_(0),
@@ -56,23 +55,11 @@ EventProfiler::~EventProfiler()
 
 void EventProfiler::SetActive(bool active)
 {
-    if (!active && active_)
-    {
-        /// deleting all blocks and creating new root block.
-        delete root_;
-        root_ = 0;
-        root_ = new EventProfilerBlock(0, "Root");
-        current_ = root_;
-        current_->name_ = "Root";
-    }
-    
     active_ = active;
 }
 
 void EventProfiler::BeginFrame()
 {
-    if (!active_)
-        return;
     // End the previous frame if any
     EndFrame();
 
@@ -82,8 +69,6 @@ void EventProfiler::BeginFrame()
 
 void EventProfiler::EndFrame()
 {
-    if (!active_)
-        return;
     if (current_ != root_)
     {
         EndBlock();
@@ -98,12 +83,19 @@ void EventProfiler::EndFrame()
 
 void EventProfiler::BeginInterval()
 {
-    if (!active_)
-        return;
+
     root_->BeginInterval();
     intervalFrames_ = 0;
 }
 
+void EventProfiler::Clear()
+{
+    delete root_;
+    root_ = new EventProfilerBlock(0, "Root");
+    current_ = root_;
+    current_->name_ = "Root";
+}
+
 Urho3D::String EventProfiler::PrintData(bool showUnused /*= false*/, bool showTotal /*= false*/, unsigned maxDepth /*= M_MAX_UNSIGNED*/) const
 {
     String output;
@@ -180,4 +172,6 @@ void EventProfiler::PrintData(EventProfilerBlock* block, String& output, unsigne
         PrintData(i->second_, output, depth, maxDepth, showUnused, showTotal);
 }
 
+bool EventProfiler::active_ = false;
+
 }

+ 12 - 35
Source/Urho3D/Core/EventProfiler.h

@@ -35,7 +35,7 @@ namespace Urho3D
 class URHO3D_API EventProfilerBlock
 {
 public:
-    /// Construct with the specified parent block and name.
+    /// Construct with the specified parent block and event id.
     EventProfilerBlock(EventProfilerBlock* parent, StringHash eventID) :
         eventID_(eventID),
         name_(0),
@@ -163,7 +163,7 @@ public:
     unsigned totalCount_;
 };
 
-/// Hierarchical performance profiler subsystem.
+/// Hierarchical performance event profiler subsystem.
 class URHO3D_API EventProfiler : public Object
 {
     URHO3D_OBJECT(EventProfiler, Object);
@@ -175,15 +175,15 @@ public:
     virtual ~EventProfiler();
 
     /// Activate the event profiler to collect information. Request deactivation, will delete all blocks!
-    void SetActive(bool active);
+    static void SetActive(bool active);
     /// Return true if active.
-    bool IsActive() const { return active_; }
+    static bool IsActive() { return active_; }
 
     /// Begin timing a profiling block.
     void BeginBlock(StringHash eventID)
     {
         // Profiler supports only the main thread currently
-        if (!active_ || !Thread::IsMainThread())
+        if ( !Thread::IsMainThread())
             return;
 
         current_ = current_->GetChild(eventID);
@@ -193,7 +193,7 @@ public:
     /// End timing the current profiling block.
     void EndBlock()
     {
-        if (!active_ || !Thread::IsMainThread())
+        if (!Thread::IsMainThread())
             return;
 
         if (current_ != root_)
@@ -203,13 +203,14 @@ public:
         }
     }
 
-    /// Begin the profiling frame. Called by HandleBeginFrame().
+    /// Begin the profiling frame. Called by Engine::RunFrame().
     void BeginFrame();
-    /// End the profiling frame. Called by HandleEndFrame().
+    /// End the profiling frame. Called by Engine::RunFrame().
     void EndFrame();
     /// Begin a new interval.
     void BeginInterval();
-
+    /// Delete all blocks and recreate root block.
+    void Clear();
     /// Return profiling data as text output.
     String PrintData(bool showUnused = false, bool showTotal = false, unsigned maxDepth = M_MAX_UNSIGNED) const;
     /// Return the current profiling block.
@@ -219,8 +220,6 @@ public:
 private:
     /// Return profiling data as text output for a specified profiling block.
     void PrintData(EventProfilerBlock* block, String& output, unsigned depth, unsigned maxDepth, bool showUnused, bool showTotal) const;
-    /// Is the profiler collecting event information.
-    bool active_;
     /// Current profiling block.
     EventProfilerBlock* current_;
     /// Root profiling block.
@@ -229,30 +228,8 @@ private:
     unsigned intervalFrames_;
     /// Total frames.
     unsigned totalFrames_;
-};
-
-/// Helper class for automatically beginning and ending a profiling block
-class URHO3D_API AutoEventProfileBlock
-{
-public:
-    /// Construct. Begin a profiling block with the specified name and optional call count.
-    AutoEventProfileBlock(EventProfiler* profiler, StringHash eventID) :
-        profiler_(profiler)
-    {
-        if (profiler_)
-            profiler_->BeginBlock(eventID);
-    }
-
-    /// Destruct. End the profiling block.
-    ~AutoEventProfileBlock()
-    {
-        if (profiler_)
-            profiler_->EndBlock();
-    }
-
-private:
-    /// Profiler.
-    EventProfiler* profiler_;
+    /// Profiler active.
+    static bool active_;
 };
 
 }

+ 12 - 5
Source/Urho3D/Core/Object.cpp

@@ -305,9 +305,13 @@ void Object::SendEvent(StringHash eventType, VariantMap& eventData)
         return;
     }
 #ifdef URHO3D_PROFILING
-    EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
-    if (eventProfiler)
-        eventProfiler->BeginBlock(eventType);
+    EventProfiler* eventProfiler = NULL;
+    if (EventProfiler::IsActive())
+    {
+        eventProfiler = GetSubsystem<EventProfiler>();
+        if (eventProfiler)
+            eventProfiler->BeginBlock(eventType);
+    }
 #endif
 
     // Make a weak pointer to self to check for destruction during event handling
@@ -407,8 +411,11 @@ void Object::SendEvent(StringHash eventType, VariantMap& eventData)
     context->EndSendEvent();
 
 #ifdef URHO3D_PROFILING
-    if (eventProfiler)
-        eventProfiler->EndBlock();
+    if (EventProfiler::IsActive())
+    {
+        if (eventProfiler)
+            eventProfiler->EndBlock();
+    }
 #endif
 }
 

+ 1 - 1
Source/Urho3D/Core/Object.h

@@ -357,7 +357,7 @@ private:
 #endif
 
 /// Register event names.
-struct EventNameRegistrar
+struct URHO3D_API EventNameRegistrar
 {
     static StringHash RegisterEventName(const char* eventName);
     /// Return Event name or empty string if not found.

+ 8 - 4
Source/Urho3D/Engine/DebugHud.cpp

@@ -240,17 +240,21 @@ void DebugHud::SetMode(unsigned mode)
 #ifdef URHO3D_PROFILING
     if ((mode & DEBUGHUD_SHOW_EVENTPROFILER) != 0)
     {
+        // event profiler is created on engine initialization if "EventProfiler" parameter is set
         EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
-        if (!eventProfiler)
-            context_->RegisterSubsystem(eventProfiler = new EventProfiler(context_));
         if (eventProfiler)
-            eventProfiler->SetActive(true);
+            EventProfiler::SetActive(true);
     }
     else
     {
+        // event profiler is created on engine initialization if "EventProfiler" parameter is set
         EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
         if (eventProfiler)
-            eventProfiler->SetActive(false);
+        {
+            EventProfiler::SetActive(false);
+            eventProfiler->Clear();
+        }
+           
     }
 #endif
     mode_ = mode;

+ 10 - 10
Source/Urho3D/Engine/Engine.cpp

@@ -427,9 +427,8 @@ bool Engine::Initialize(const VariantMap& parameters)
 #ifdef URHO3D_PROFILING
     if (GetParameter(parameters, "EventProfiler", true).GetBool())
     {
-        EventProfiler* evpr = new EventProfiler(context_);
-        context_->RegisterSubsystem(evpr);
-        evpr->SetActive(true);
+        context_->RegisterSubsystem(new EventProfiler(context_));
+        EventProfiler::SetActive(true);
     }
 #endif
     frameTimer_.Reset();
@@ -455,11 +454,16 @@ void Engine::RunFrame()
     Time* time = GetSubsystem<Time>();
     Input* input = GetSubsystem<Input>();
     Audio* audio = GetSubsystem<Audio>();
+
 #ifdef URHO3D_PROFILING
-    EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
-    if (eventProfiler)
-        eventProfiler->BeginFrame();
+    if (EventProfiler::IsActive())
+    {
+        EventProfiler* eventProfiler = GetSubsystem<EventProfiler>();
+        if (eventProfiler)
+            eventProfiler->BeginFrame();
+    }
 #endif
+
     time->BeginFrame(timeStep_);
 
     // If pause when minimized -mode is in use, stop updates and audio as necessary
@@ -487,10 +491,6 @@ void Engine::RunFrame()
     ApplyFrameLimit();
 
     time->EndFrame();
-#ifdef URHO3D_PROFILING
-    if (eventProfiler)
-        eventProfiler->EndFrame();
-#endif
 }
 
 Console* Engine::CreateConsole()