Skip to content

Commit

Permalink
fix crash with framebuffer inside another framebuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Nov 21, 2023
1 parent 5876849 commit 6c41378
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
11 changes: 5 additions & 6 deletions src/framework/graphics/drawpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ DrawPool* DrawPool::create(const DrawPoolType type)
if (type == DrawPoolType::MAP) {
pool->m_framebuffer->m_useAlphaWriting = false;
pool->m_framebuffer->disableBlend();
pool->m_bindedFramebuffers = 0; // forces to use only framebuffer without smoothing.
} else if (type == DrawPoolType::FOREGROUND) {
pool->setFPS(FPS10);
}
Expand Down Expand Up @@ -363,11 +364,10 @@ void DrawPool::addAction(const std::function<void()>& action)

void DrawPool::bindFrameBuffer(const Size& size)
{
const uint8_t frameIndex = m_type == DrawPoolType::MAP ? 0 : 1;

++m_bindedFramebuffers;
m_oldState = std::move(m_state);
m_state = {};
addAction([size, frameIndex, drawState = m_state] {
addAction([size, frameIndex = m_bindedFramebuffers, drawState = m_state] {
drawState.execute();
const auto& frame = g_framebuffers.getTemporaryFrameBuffer(frameIndex);
frame->resize(size);
Expand All @@ -376,14 +376,13 @@ void DrawPool::bindFrameBuffer(const Size& size)
}
void DrawPool::releaseFrameBuffer(const Rect& dest)
{
const uint8_t frameIndex = m_type == DrawPoolType::MAP ? 0 : 1;

m_state = std::move(m_oldState);
addAction([dest, frameIndex, drawState = m_state] {
addAction([dest, frameIndex = m_bindedFramebuffers, drawState = m_state] {
const auto& frame = g_framebuffers.getTemporaryFrameBuffer(frameIndex);
frame->release();
drawState.execute();
frame->draw(dest);
});
if (hasFrameBuffer() && !dest.isNull()) stdext::hash_union(m_status.second, dest.hash());
--m_bindedFramebuffers;
}
1 change: 1 addition & 0 deletions src/framework/graphics/drawpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class DrawPool
bool m_enabled{ true };
bool m_alwaysGroupDrawings{ false };

int8_t m_bindedFramebuffers{ -1 };
uint8_t m_depthLevel{ 0 };

uint16_t m_refreshDelay{ 0 }, m_shaderRefreshDelay{ 0 };
Expand Down
18 changes: 11 additions & 7 deletions src/framework/graphics/framebuffermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ FrameBufferManager g_framebuffers;

void FrameBufferManager::init()
{
m_temporaryFramebuffer.reserve(2);
for (uint_fast8_t i = 0; i < 2; ++i) {
const auto& frame = m_temporaryFramebuffer.emplace_back(std::make_shared<FrameBuffer>());
if (i == 0) {
frame->setSmooth(false);
}
};
m_temporaryFramebuffer.emplace_back(std::make_shared<FrameBuffer>());
}

void FrameBufferManager::terminate() {
m_temporaryFramebuffer.clear();
}

const FrameBufferPtr& FrameBufferManager::getTemporaryFrameBuffer(const uint8_t index) {
if (index < m_temporaryFramebuffer.size()) {
return m_temporaryFramebuffer[index];
}

const auto& tempfb = m_temporaryFramebuffer.emplace_back(std::make_shared<FrameBuffer>());
tempfb->setSmooth(false);
return tempfb;
}
4 changes: 1 addition & 3 deletions src/framework/graphics/framebuffermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class FrameBufferManager
void init();
void terminate();

const FrameBufferPtr& getTemporaryFrameBuffer(const uint8_t index) const {
return m_temporaryFramebuffer[index];
}
const FrameBufferPtr& getTemporaryFrameBuffer(const uint8_t index);

protected:
std::vector<FrameBufferPtr> m_temporaryFramebuffer;
Expand Down

0 comments on commit 6c41378

Please sign in to comment.