From 00a8c6d41468fbd10ebb7af6d9342725d434ddd4 Mon Sep 17 00:00:00 2001 From: bofeng Date: Thu, 12 Dec 2024 13:54:24 +0800 Subject: [PATCH] refine --- .../editor-support/MiddlewareManager.cpp | 68 ++++--------------- .../cocos/editor-support/MiddlewareManager.h | 12 ++-- .../CCArmatureCacheDisplay.cpp | 1 - .../CCArmatureDisplay.cpp | 1 - .../SkeletonCacheAnimation.cpp | 1 - .../SkeletonRenderer.cpp | 1 - 6 files changed, 16 insertions(+), 68 deletions(-) diff --git a/native/cocos/editor-support/MiddlewareManager.cpp b/native/cocos/editor-support/MiddlewareManager.cpp index 74f9447eb34..fd52ba19df3 100644 --- a/native/cocos/editor-support/MiddlewareManager.cpp +++ b/native/cocos/editor-support/MiddlewareManager.cpp @@ -54,20 +54,20 @@ 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::updateCache() { + for (auto &iter: _updateMap) { + auto it = std::find(_updateList.begin(), _updateList.end(), iter.first); + if (iter.second && it == _updateList.end()) { + _updateList.push_back(iter.first); + } else if (!iter.second && it != _updateList.end()) { _updateList.erase(it); } } - - _removeList.clear(); + _updateMap.clear(); } void MiddlewareManager::update(float dt) { - isUpdating = true; - + updateCache(); _attachInfo.reset(); auto *attachBuffer = _attachInfo.getBuffer(); if (attachBuffer) { @@ -76,23 +76,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 +88,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,33 +113,14 @@ void MiddlewareManager::render(float dt) { } batch2d->syncMeshBuffersToNative(accID, std::move(uiMeshArray)); } - - clearRemoveList(); } void MiddlewareManager::addTimer(IMiddleware *editor) { - auto it1 = std::find(_removeList.begin(), _removeList.end(), editor); - if (it1 != _removeList.end()) { - _removeList.erase(it1); - } - - auto it0 = std::find(_updateList.begin(), _updateList.end(), editor); - if (it0 != _updateList.end()) { - return; - } - - _updateList.push_back(editor); + _updateMap[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); - } - } + _updateMap[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..8f19f63f888 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" @@ -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 updateCache(); ccstd::vector _updateList; - ccstd::vector _removeList; - std::map _mbMap; + ccstd::unordered_map _updateMap; + 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();