Skip to content

Commit

Permalink
Fix spine can not show normally after disable and then active node. (#…
Browse files Browse the repository at this point in the history
…18024)

* Fix spine can not show normally after disable and then active node.
  • Loading branch information
bofeng-song authored Dec 12, 2024
1 parent 63f8fd9 commit 796a327
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 69 deletions.
69 changes: 14 additions & 55 deletions native/cocos/editor-support/MiddlewareManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
16 changes: 6 additions & 10 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 @@ -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);
Expand All @@ -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<IMiddleware *> _updateList;
ccstd::vector<IMiddleware *> _removeList;
std::map<int, MeshBuffer *> _mbMap;
ccstd::unordered_map<IMiddleware *, bool> _operateCacheMap;
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 796a327

Please sign in to comment.