Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeng-song committed Dec 12, 2024
1 parent 2586ead commit 00a8c6d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 68 deletions.
68 changes: 12 additions & 56 deletions native/cocos/editor-support/MiddlewareManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
12 changes: 4 additions & 8 deletions native/cocos/editor-support/MiddlewareManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#pragma once

#include <map>
#include <unordered_map>
#include <vector>
#include "MeshBuffer.h"
#include "MiddlewareMacro.h"
Expand Down Expand Up @@ -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<IMiddleware *> _updateList;
ccstd::vector<IMiddleware *> _removeList;
std::map<int, MeshBuffer *> _mbMap;
ccstd::unordered_map<IMiddleware *, bool> _updateMap;
ccstd::unordered_map<int, MeshBuffer *> _mbMap;

SharedBufferManager _renderInfo;
SharedBufferManager _attachInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 00a8c6d

Please sign in to comment.