From a3609531c449cfc7fe56308a07ae4bae3aceec46 Mon Sep 17 00:00:00 2001 From: Karim Naaji Date: Thu, 22 Dec 2016 15:48:57 -0500 Subject: [PATCH 1/2] Lock on texture mutex when updating atlas reference counts --- core/src/text/fontContext.cpp | 21 ++++++++++++--------- core/src/text/fontContext.h | 2 -- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/core/src/text/fontContext.cpp b/core/src/text/fontContext.cpp index b689e0ed62..540fe63889 100644 --- a/core/src/text/fontContext.cpp +++ b/core/src/text/fontContext.cpp @@ -217,17 +217,20 @@ bool FontContext::layoutText(TextStyle::Parameters& _params, const std::string& (metrics.aabb.y + height * 0.5) * TextVertex::position_scale); - for (; it != _quads.end(); ++it) { + { + std::lock_guard lock(m_textureMutex); + for (; it != _quads.end(); ++it) { - if (!_refs[it->atlas]) { - _refs[it->atlas] = true; - m_atlasRefCount[it->atlas]++; - } + if (!_refs[it->atlas]) { + _refs[it->atlas] = true; + m_atlasRefCount[it->atlas]++; + } - it->quad[0].pos -= offset; - it->quad[1].pos -= offset; - it->quad[2].pos -= offset; - it->quad[3].pos -= offset; + it->quad[0].pos -= offset; + it->quad[1].pos -= offset; + it->quad[2].pos -= offset; + it->quad[3].pos -= offset; + } } return true; diff --git a/core/src/text/fontContext.h b/core/src/text/fontContext.h index c14782a87c..17681471a2 100644 --- a/core/src/text/fontContext.h +++ b/core/src/text/fontContext.h @@ -88,8 +88,6 @@ class FontContext : public alfons::TextureCallback { void releaseAtlas(std::bitset _refs); - alfons::GlyphAtlas& atlas() { return m_atlas; } - /* Update all textures batches, uploads the data to the GPU */ void updateTextures(RenderState& rs); From abbeba1248ecfbc462a06897d7c24150984bcaba Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Mon, 26 Dec 2016 23:48:11 +0100 Subject: [PATCH 2/2] squash synchronize FontContext m_atlasRefs - don't clear textures directly in releaseAtlas. This could clear the atlas while it's used in layoutText, before the atlas ref count got updated. --- core/src/text/fontContext.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/src/text/fontContext.cpp b/core/src/text/fontContext.cpp index 540fe63889..d8608cba42 100644 --- a/core/src/text/fontContext.cpp +++ b/core/src/text/fontContext.cpp @@ -98,13 +98,7 @@ void FontContext::releaseAtlas(std::bitset _refs) { if (!_refs.any()) { return; } std::lock_guard lock(m_textureMutex); for (size_t i = 0; i < m_textures.size(); i++) { - if (!_refs[i]) { continue; } - - if (--m_atlasRefCount[i] == 0) { - LOGD("CLEAR ATLAS %d", i); - m_atlas.clear(i); - m_textures[i].texData.assign(GlyphTexture::size * GlyphTexture::size, 0); - } + if (_refs[i]) { m_atlasRefCount[i] -= 1; } } } @@ -216,7 +210,6 @@ bool FontContext::layoutText(TextStyle::Parameters& _params, const std::string& glm::vec2 offset((metrics.aabb.x + width * 0.5) * TextVertex::position_scale, (metrics.aabb.y + height * 0.5) * TextVertex::position_scale); - { std::lock_guard lock(m_textureMutex); for (; it != _quads.end(); ++it) { @@ -231,6 +224,15 @@ bool FontContext::layoutText(TextStyle::Parameters& _params, const std::string& it->quad[2].pos -= offset; it->quad[3].pos -= offset; } + + // Clear unused textures + for (size_t i = 0; i < m_textures.size(); i++) { + if (m_atlasRefCount[i] == 0) { + m_atlas.clear(i); + m_textures[i].texData.assign(GlyphTexture::size * + GlyphTexture::size, 0); + } + } } return true;