diff --git a/native/cocos/editor-support/MiddlewareManager.cpp b/native/cocos/editor-support/MiddlewareManager.cpp index 21e66aaca80..d50d85ed86a 100644 --- a/native/cocos/editor-support/MiddlewareManager.cpp +++ b/native/cocos/editor-support/MiddlewareManager.cpp @@ -54,20 +54,22 @@ MeshBuffer *MiddlewareManager::getMeshBuffer(int format) { return mb; } -void MiddlewareManager::clearRemoveList() { - for (auto *editor : _removeList) { - auto it = std::find(_updateList.begin(), _updateList.end(), editor); - if (it != _updateList.end()) { +void MiddlewareManager::updateOperateCache() { + for (auto &iter: _operateCacheMap) { + auto it = std::find(_updateList.begin(), _updateList.end(), iter.first); + if (iter.second) { + if (it == _updateList.end()) { + _updateList.push_back(iter.first); + } + } else if (it != _updateList.end()) { _updateList.erase(it); } } - - _removeList.clear(); + _operateCacheMap.clear(); } void MiddlewareManager::update(float dt) { - isUpdating = true; - + updateOperateCache(); _attachInfo.reset(); auto *attachBuffer = _attachInfo.getBuffer(); if (attachBuffer) { @@ -76,23 +78,8 @@ void MiddlewareManager::update(float dt) { for (size_t i = 0, len = _updateList.size(); i < len; ++i) { auto *editor = _updateList[i]; - if (!_removeList.empty()) { - auto removeIt = std::find(_removeList.begin(), _removeList.end(), editor); - if (removeIt == _removeList.end()) { - if (editor) { - editor->update(dt); - } - } - } else { - if (editor) { - editor->update(dt); - } - } + editor->update(dt); } - - isUpdating = false; - - clearRemoveList(); } void MiddlewareManager::render(float dt) { @@ -103,22 +90,12 @@ void MiddlewareManager::render(float dt) { } } - isRendering = true; for (size_t i = 0, len = _updateList.size(); i < len; ++i) { auto *editor = _updateList[i]; - if (!_removeList.empty()) { - auto removeIt = std::find(_removeList.begin(), _removeList.end(), editor); - if (removeIt == _removeList.end()) { - editor->render(dt); - } - } else { - editor->render(dt); - } + editor->render(dt); } - isRendering = false; - for (auto it : _mbMap) { auto *buffer = it.second; if (buffer) { @@ -138,32 +115,14 @@ void MiddlewareManager::render(float dt) { } batch2d->syncMeshBuffersToNative(accID, std::move(uiMeshArray)); } - - clearRemoveList(); } void MiddlewareManager::addTimer(IMiddleware *editor) { - auto it0 = std::find(_updateList.begin(), _updateList.end(), editor); - if (it0 != _updateList.end()) { - return; - } - - auto it1 = std::find(_removeList.begin(), _removeList.end(), editor); - if (it1 != _removeList.end()) { - _removeList.erase(it1); - } - _updateList.push_back(editor); + _operateCacheMap[editor] = true; } void MiddlewareManager::removeTimer(IMiddleware *editor) { - if (isUpdating || isRendering) { - _removeList.push_back(editor); - } else { - auto it = std::find(_updateList.begin(), _updateList.end(), editor); - if (it != _updateList.end()) { - _updateList.erase(it); - } - } + _operateCacheMap[editor] = false; } se_object_ptr MiddlewareManager::getVBTypedArray(int format, int bufferPos) { diff --git a/native/cocos/editor-support/MiddlewareManager.h b/native/cocos/editor-support/MiddlewareManager.h index 9c326dbb30f..75b697e834b 100644 --- a/native/cocos/editor-support/MiddlewareManager.h +++ b/native/cocos/editor-support/MiddlewareManager.h @@ -24,7 +24,7 @@ #pragma once -#include +#include #include #include "MeshBuffer.h" #include "MiddlewareMacro.h" @@ -83,13 +83,13 @@ class MiddlewareManager { void render(float dt); /** - * @brief Third party module add in _updateMap,it will update perframe. + * @brief Mark the add flag to _operateCacheMap, and perform the update in the next frame * @param[in] editor Module must implement IMiddleware interface. */ void addTimer(IMiddleware *editor); /** - * @brief Third party module remove from _updateMap,it will stop update. + * @brief Mark the remove flag to _operateCacheMap, and perform the update in the next frame * @param[in] editor Module must implement IMiddleware interface. */ void removeTimer(IMiddleware *editor); @@ -108,16 +108,12 @@ class MiddlewareManager { MiddlewareManager(); ~MiddlewareManager(); - // If manager is traversing _updateMap, will set the flag untill traverse is finished. - bool isRendering = false; - bool isUpdating = false; - private: - void clearRemoveList(); + void updateOperateCache(); ccstd::vector _updateList; - ccstd::vector _removeList; - std::map _mbMap; + ccstd::unordered_map _operateCacheMap; + ccstd::unordered_map _mbMap; SharedBufferManager _renderInfo; SharedBufferManager _attachInfo; diff --git a/native/cocos/editor-support/dragonbones-creator-support/CCArmatureCacheDisplay.cpp b/native/cocos/editor-support/dragonbones-creator-support/CCArmatureCacheDisplay.cpp index cc832e7ba9f..529d95aba91 100644 --- a/native/cocos/editor-support/dragonbones-creator-support/CCArmatureCacheDisplay.cpp +++ b/native/cocos/editor-support/dragonbones-creator-support/CCArmatureCacheDisplay.cpp @@ -141,7 +141,6 @@ void CCArmatureCacheDisplay::render(float /*dt*/) { if (!frameData) return; auto *mgr = MiddlewareManager::getInstance(); - if (!mgr->isRendering) return; auto *entity = _entity; entity->clearDynamicRenderDrawInfos(); diff --git a/native/cocos/editor-support/dragonbones-creator-support/CCArmatureDisplay.cpp b/native/cocos/editor-support/dragonbones-creator-support/CCArmatureDisplay.cpp index 5033b2dcd0a..fdd28e3fca0 100644 --- a/native/cocos/editor-support/dragonbones-creator-support/CCArmatureDisplay.cpp +++ b/native/cocos/editor-support/dragonbones-creator-support/CCArmatureDisplay.cpp @@ -108,7 +108,6 @@ void CCArmatureDisplay::dbRender() { entity->clearDynamicRenderDrawInfos(); auto *mgr = MiddlewareManager::getInstance(); - if (!mgr->isRendering) return; auto *attachMgr = mgr->getAttachInfoMgr(); auto *attachInfo = attachMgr->getBuffer(); diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonCacheAnimation.cpp b/native/cocos/editor-support/spine-creator-support/SkeletonCacheAnimation.cpp index fd5ca483c1d..b65d6a736d1 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonCacheAnimation.cpp +++ b/native/cocos/editor-support/spine-creator-support/SkeletonCacheAnimation.cpp @@ -165,7 +165,6 @@ void SkeletonCacheAnimation::render(float /*dt*/) { if (segments.empty() || colors.empty()) return; auto *mgr = MiddlewareManager::getInstance(); - if (!mgr->isRendering) return; _sharedBufferOffset->reset(); _sharedBufferOffset->clear(); diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp b/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp index 2411f9604ad..21d248e932a 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp +++ b/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp @@ -266,7 +266,6 @@ void SkeletonRenderer::render(float /*deltaTime*/) { // avoid other place call update. auto *mgr = MiddlewareManager::getInstance(); - if (!mgr->isRendering) return; auto *attachMgr = mgr->getAttachInfoMgr(); auto *attachInfo = attachMgr->getBuffer();