From b6debf5d3f2eaff7642a8d5d35becd6366d90fdf Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 16 May 2022 11:36:33 +0800 Subject: [PATCH] refactor Profiler startup/shutdown (#11105) --- native/cocos/engine/Engine.cpp | 4 ++-- native/cocos/engine/Engine.h | 2 ++ native/cocos/profiler/Profiler.cpp | 15 +++------------ native/cocos/profiler/Profiler.h | 11 +++++------ 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/native/cocos/engine/Engine.cpp b/native/cocos/engine/Engine.cpp index 652f15531a..df701e3af9 100644 --- a/native/cocos/engine/Engine.cpp +++ b/native/cocos/engine/Engine.cpp @@ -101,7 +101,7 @@ Engine::Engine() { se::ScriptEngine::getInstance(); #if CC_USE_PROFILER - Profiler::getInstance(); + _profiler = new Profiler(); #endif } @@ -123,7 +123,7 @@ Engine::~Engine() { Root::getInstance()->destroy(); #if CC_USE_PROFILER - Profiler::destroyInstance(); + delete _profiler; #endif DebugRenderer::destroyInstance(); FreeTypeFontFace::destroyFreeType(); diff --git a/native/cocos/engine/Engine.h b/native/cocos/engine/Engine.h index 2752f8da79..dc55c5d986 100644 --- a/native/cocos/engine/Engine.h +++ b/native/cocos/engine/Engine.h @@ -37,6 +37,7 @@ namespace cc { class FileUtils; +class Profiler; #define NANOSECONDS_PER_SECOND 1000000000 #define NANOSECONDS_60FPS 16666667L @@ -133,6 +134,7 @@ private: // Subsystems FileUtils *_fs{nullptr}; + Profiler *_profiler{nullptr}; std::map _eventCallbacks; CC_DISALLOW_COPY_MOVE_ASSIGN(Engine); diff --git a/native/cocos/profiler/Profiler.cpp b/native/cocos/profiler/Profiler.cpp index a89239911c..6e6f15e54c 100644 --- a/native/cocos/profiler/Profiler.cpp +++ b/native/cocos/profiler/Profiler.cpp @@ -72,7 +72,6 @@ ProfilerBlock::~ProfilerBlock() { for (auto *child : _children) { CC_SAFE_DELETE(child); } - _children.clear(); } @@ -123,29 +122,21 @@ struct ProfilerBlockDepth { */ Profiler *Profiler::instance = nullptr; Profiler *Profiler::getInstance() { - if (!instance) { - instance = ccnew Profiler(); - } - return instance; } -void Profiler::destroyInstance() { - if (instance) { - delete instance; - instance = nullptr; - } -} - Profiler::Profiler() { _mainThreadId = std::this_thread::get_id(); _root = ccnew ProfilerBlock(nullptr, "MainThread"); _current = _root; + + Profiler::instance = this; } Profiler::~Profiler() { CC_SAFE_DELETE(_root); _current = nullptr; + Profiler::instance = nullptr; } void Profiler::setEnable(ShowOption option, bool b) { diff --git a/native/cocos/profiler/Profiler.h b/native/cocos/profiler/Profiler.h index 39fda15684..651f8ebda6 100644 --- a/native/cocos/profiler/Profiler.h +++ b/native/cocos/profiler/Profiler.h @@ -48,14 +48,15 @@ enum class ShowOption : uint32_t { */ class Profiler { public: + static Profiler *getInstance(); + + Profiler(); + ~Profiler(); Profiler(const Profiler &) = delete; Profiler(Profiler &&) = delete; Profiler &operator=(const Profiler &) = delete; Profiler &operator=(Profiler &&) = delete; - static Profiler *getInstance(); - static void destroyInstance(); - void setEnable(ShowOption option, bool b = true); bool isEnabled(ShowOption option) const; @@ -68,11 +69,9 @@ public: inline ObjectStats &getObjectStats() { return _objectStats; } private: - Profiler(); - ~Profiler(); + static void doFrameUpdate(); void doIntervalUpdate(); - static void doFrameUpdate(); void printStats(); void beginBlock(const ccstd::string &name);