Skip to content

Commit

Permalink
fix texture data race (#1202)
Browse files Browse the repository at this point in the history
* Lock on texture mutex when updating atlas reference counts

* 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.
  • Loading branch information
hjanetzek authored and tallytalwar committed Jan 11, 2017
1 parent 33b40ca commit 1534a8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
35 changes: 20 additions & 15 deletions core/src/text/fontContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,7 @@ void FontContext::releaseAtlas(std::bitset<max_textures> _refs) {
if (!_refs.any()) { return; }
std::lock_guard<std::mutex> 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; }
}
}

Expand Down Expand Up @@ -216,18 +210,29 @@ 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<std::mutex> lock(m_textureMutex);
for (; it != _quads.end(); ++it) {

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;
// 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;
Expand Down
2 changes: 0 additions & 2 deletions core/src/text/fontContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ class FontContext : public alfons::TextureCallback {

void releaseAtlas(std::bitset<max_textures> _refs);

alfons::GlyphAtlas& atlas() { return m_atlas; }

/* Update all textures batches, uploads the data to the GPU */
void updateTextures(RenderState& rs);

Expand Down

0 comments on commit 1534a8d

Please sign in to comment.