[core] Replace three maps/mutexes in GlyphAtlas with a single map and mutex
This commit is contained in:
@@ -256,7 +256,7 @@ void SymbolLayout::prepare(uintptr_t tileUID,
|
|||||||
|
|
||||||
// Add the glyphs we need for this label to the glyph atlas.
|
// Add the glyphs we need for this label to the glyph atlas.
|
||||||
if (result) {
|
if (result) {
|
||||||
glyphAtlas.addGlyphs(tileUID, text, layout.get<TextFont>(), **glyphSet, face);
|
glyphAtlas.addGlyphs(tileUID, text, layout.get<TextFont>(), glyphSet, face);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -23,16 +23,17 @@ GlyphAtlas::GlyphAtlas(const Size size, FileSource& fileSource_)
|
|||||||
GlyphAtlas::~GlyphAtlas() = default;
|
GlyphAtlas::~GlyphAtlas() = default;
|
||||||
|
|
||||||
void GlyphAtlas::requestGlyphRange(const FontStack& fontStack, const GlyphRange& range) {
|
void GlyphAtlas::requestGlyphRange(const FontStack& fontStack, const GlyphRange& range) {
|
||||||
std::lock_guard<std::mutex> lock(rangesMutex);
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
auto& rangeSets = ranges[fontStack];
|
auto& rangeSets = entries[fontStack].ranges;
|
||||||
|
|
||||||
const auto& rangeSetsIt = rangeSets.find(range);
|
const auto& rangeSetsIt = rangeSets.find(range);
|
||||||
if (rangeSetsIt != rangeSets.end()) {
|
if (rangeSetsIt != rangeSets.end()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
rangeSets.emplace(range,
|
rangeSets.emplace(std::piecewise_construct,
|
||||||
std::make_unique<GlyphPBF>(this, fontStack, range, observer, fileSource));
|
std::forward_as_tuple(range),
|
||||||
|
std::forward_as_tuple(this, fontStack, range, observer, fileSource));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GlyphAtlas::hasGlyphRanges(const FontStack& fontStack, const GlyphRangeSet& glyphRanges) {
|
bool GlyphAtlas::hasGlyphRanges(const FontStack& fontStack, const GlyphRangeSet& glyphRanges) {
|
||||||
@@ -40,8 +41,8 @@ bool GlyphAtlas::hasGlyphRanges(const FontStack& fontStack, const GlyphRangeSet&
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(rangesMutex);
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
const auto& rangeSets = ranges[fontStack];
|
const auto& rangeSets = entries[fontStack].ranges;
|
||||||
|
|
||||||
bool hasRanges = true;
|
bool hasRanges = true;
|
||||||
for (const auto& range : glyphRanges) {
|
for (const auto& range : glyphRanges) {
|
||||||
@@ -55,7 +56,7 @@ bool GlyphAtlas::hasGlyphRanges(const FontStack& fontStack, const GlyphRangeSet&
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!rangeSetsIt->second->isParsed()) {
|
if (!rangeSetsIt->second.isParsed()) {
|
||||||
hasRanges = false;
|
hasRanges = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,16 +65,8 @@ bool GlyphAtlas::hasGlyphRanges(const FontStack& fontStack, const GlyphRangeSet&
|
|||||||
}
|
}
|
||||||
|
|
||||||
util::exclusive<GlyphSet> GlyphAtlas::getGlyphSet(const FontStack& fontStack) {
|
util::exclusive<GlyphSet> GlyphAtlas::getGlyphSet(const FontStack& fontStack) {
|
||||||
auto lock = std::make_unique<std::lock_guard<std::mutex>>(glyphSetsMutex);
|
auto lock = std::make_unique<std::lock_guard<std::mutex>>(mutex);
|
||||||
|
return { &entries[fontStack].glyphSet, std::move(lock) };
|
||||||
auto it = glyphSets.find(fontStack);
|
|
||||||
if (it == glyphSets.end()) {
|
|
||||||
it = glyphSets.emplace(fontStack, std::make_unique<GlyphSet>()).first;
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: We lock all GlyphSets, but what we should
|
|
||||||
// really do is lock only the one we are returning.
|
|
||||||
return { it->second.get(), std::move(lock) };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlyphAtlas::setObserver(GlyphAtlasObserver* observer_) {
|
void GlyphAtlas::setObserver(GlyphAtlasObserver* observer_) {
|
||||||
@@ -83,12 +76,10 @@ void GlyphAtlas::setObserver(GlyphAtlasObserver* observer_) {
|
|||||||
void GlyphAtlas::addGlyphs(uintptr_t tileUID,
|
void GlyphAtlas::addGlyphs(uintptr_t tileUID,
|
||||||
const std::u16string& text,
|
const std::u16string& text,
|
||||||
const FontStack& fontStack,
|
const FontStack& fontStack,
|
||||||
const GlyphSet& glyphSet,
|
const util::exclusive<GlyphSet>& glyphSet,
|
||||||
GlyphPositions& face)
|
GlyphPositions& face)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
const std::map<uint32_t, SDFGlyph>& sdfs = glyphSet->getSDFs();
|
||||||
|
|
||||||
const std::map<uint32_t, SDFGlyph>& sdfs = glyphSet.getSDFs();
|
|
||||||
|
|
||||||
for (char16_t chr : text)
|
for (char16_t chr : text)
|
||||||
{
|
{
|
||||||
@@ -107,7 +98,7 @@ Rect<uint16_t> GlyphAtlas::addGlyph(uintptr_t tileUID,
|
|||||||
const FontStack& fontStack,
|
const FontStack& fontStack,
|
||||||
const SDFGlyph& glyph)
|
const SDFGlyph& glyph)
|
||||||
{
|
{
|
||||||
std::map<uint32_t, GlyphValue>& face = index[fontStack];
|
std::map<uint32_t, GlyphValue>& face = entries[fontStack].glyphValues;
|
||||||
auto it = face.find(glyph.id);
|
auto it = face.find(glyph.id);
|
||||||
|
|
||||||
// The glyph is already in this texture.
|
// The glyph is already in this texture.
|
||||||
@@ -151,10 +142,10 @@ Rect<uint16_t> GlyphAtlas::addGlyph(uintptr_t tileUID,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GlyphAtlas::removeGlyphs(uintptr_t tileUID) {
|
void GlyphAtlas::removeGlyphs(uintptr_t tileUID) {
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mutex);
|
||||||
|
|
||||||
for (auto& faces : index) {
|
for (auto& entry : entries) {
|
||||||
std::map<uint32_t, GlyphValue>& face = faces.second;
|
std::map<uint32_t, GlyphValue>& face = entry.second.glyphValues;
|
||||||
for (auto it = face.begin(); it != face.end(); /* we advance in the body */) {
|
for (auto it = face.begin(); it != face.end(); /* we advance in the body */) {
|
||||||
GlyphValue& value = it->second;
|
GlyphValue& value = it->second;
|
||||||
value.ids.erase(tileUID);
|
value.ids.erase(tileUID);
|
||||||
@@ -190,8 +181,6 @@ Size GlyphAtlas::getSize() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GlyphAtlas::upload(gl::Context& context, gl::TextureUnit unit) {
|
void GlyphAtlas::upload(gl::Context& context, gl::TextureUnit unit) {
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
|
||||||
|
|
||||||
if (!texture) {
|
if (!texture) {
|
||||||
texture = context.createTexture(image, unit);
|
texture = context.createTexture(image, unit);
|
||||||
} else if (dirty) {
|
} else if (dirty) {
|
||||||
|
|||||||
@@ -17,8 +17,6 @@
|
|||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <exception>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace mbgl {
|
namespace mbgl {
|
||||||
|
|
||||||
@@ -57,7 +55,7 @@ public:
|
|||||||
void addGlyphs(uintptr_t tileUID,
|
void addGlyphs(uintptr_t tileUID,
|
||||||
const std::u16string& text,
|
const std::u16string& text,
|
||||||
const FontStack&,
|
const FontStack&,
|
||||||
const GlyphSet&,
|
const util::exclusive<GlyphSet>&,
|
||||||
GlyphPositions&);
|
GlyphPositions&);
|
||||||
void removeGlyphs(uintptr_t tileUID);
|
void removeGlyphs(uintptr_t tileUID);
|
||||||
|
|
||||||
@@ -77,20 +75,9 @@ private:
|
|||||||
const FontStack&,
|
const FontStack&,
|
||||||
const SDFGlyph&);
|
const SDFGlyph&);
|
||||||
|
|
||||||
|
|
||||||
FileSource& fileSource;
|
FileSource& fileSource;
|
||||||
std::string glyphURL;
|
std::string glyphURL;
|
||||||
|
|
||||||
std::unordered_map<FontStack, std::map<GlyphRange, std::unique_ptr<GlyphPBF>>, FontStackHash> ranges;
|
|
||||||
std::mutex rangesMutex;
|
|
||||||
|
|
||||||
std::unordered_map<FontStack, std::unique_ptr<GlyphSet>, FontStackHash> glyphSets;
|
|
||||||
std::mutex glyphSetsMutex;
|
|
||||||
|
|
||||||
util::WorkQueue workQueue;
|
|
||||||
|
|
||||||
GlyphAtlasObserver* observer = nullptr;
|
|
||||||
|
|
||||||
struct GlyphValue {
|
struct GlyphValue {
|
||||||
GlyphValue(Rect<uint16_t> rect_, uintptr_t id)
|
GlyphValue(Rect<uint16_t> rect_, uintptr_t id)
|
||||||
: rect(std::move(rect_)), ids({ id }) {}
|
: rect(std::move(rect_)), ids({ id }) {}
|
||||||
@@ -98,9 +85,19 @@ private:
|
|||||||
std::unordered_set<uintptr_t> ids;
|
std::unordered_set<uintptr_t> ids;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::mutex mtx;
|
struct Entry {
|
||||||
|
std::map<GlyphRange, GlyphPBF> ranges;
|
||||||
|
GlyphSet glyphSet;
|
||||||
|
std::map<uint32_t, GlyphValue> glyphValues;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<FontStack, Entry, FontStackHash> entries;
|
||||||
|
std::mutex mutex;
|
||||||
|
|
||||||
|
util::WorkQueue workQueue;
|
||||||
|
GlyphAtlasObserver* observer = nullptr;
|
||||||
|
|
||||||
BinPack<uint16_t> bin;
|
BinPack<uint16_t> bin;
|
||||||
std::unordered_map<FontStack, std::map<uint32_t, GlyphValue>, FontStackHash> index;
|
|
||||||
AlphaImage image;
|
AlphaImage image;
|
||||||
std::atomic<bool> dirty;
|
std::atomic<bool> dirty;
|
||||||
mbgl::optional<gl::Texture> texture;
|
mbgl::optional<gl::Texture> texture;
|
||||||
|
|||||||
@@ -83,8 +83,7 @@ TEST(GlyphAtlas, LoadingFail) {
|
|||||||
EXPECT_TRUE(error != nullptr);
|
EXPECT_TRUE(error != nullptr);
|
||||||
EXPECT_EQ(util::toString(error), "Failed by the test case");
|
EXPECT_EQ(util::toString(error), "Failed by the test case");
|
||||||
|
|
||||||
auto glyphSet = test.glyphAtlas.getGlyphSet({{"Test Stack"}});
|
ASSERT_TRUE(test.glyphAtlas.getGlyphSet({{"Test Stack"}})->getSDFs().empty());
|
||||||
ASSERT_TRUE(glyphSet->getSDFs().empty());
|
|
||||||
ASSERT_FALSE(test.glyphAtlas.hasGlyphRanges({{"Test Stack"}}, {{0, 255}}));
|
ASSERT_FALSE(test.glyphAtlas.hasGlyphRanges({{"Test Stack"}}, {{0, 255}}));
|
||||||
|
|
||||||
test.end();
|
test.end();
|
||||||
@@ -112,8 +111,7 @@ TEST(GlyphAtlas, LoadingCorrupted) {
|
|||||||
EXPECT_TRUE(error != nullptr);
|
EXPECT_TRUE(error != nullptr);
|
||||||
EXPECT_EQ(util::toString(error), "unknown pbf field type exception");
|
EXPECT_EQ(util::toString(error), "unknown pbf field type exception");
|
||||||
|
|
||||||
auto glyphSet = test.glyphAtlas.getGlyphSet({{"Test Stack"}});
|
ASSERT_TRUE(test.glyphAtlas.getGlyphSet({{"Test Stack"}})->getSDFs().empty());
|
||||||
ASSERT_TRUE(glyphSet->getSDFs().empty());
|
|
||||||
ASSERT_FALSE(test.glyphAtlas.hasGlyphRanges({{"Test Stack"}}, {{0, 255}}));
|
ASSERT_FALSE(test.glyphAtlas.hasGlyphRanges({{"Test Stack"}}, {{0, 255}}));
|
||||||
|
|
||||||
test.end();
|
test.end();
|
||||||
@@ -144,21 +142,21 @@ TEST(GlyphAtlas, LoadingCancel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(GlyphAtlas, InvalidSDFGlyph) {
|
TEST(GlyphAtlas, InvalidSDFGlyph) {
|
||||||
GlyphSet glyphSet;
|
|
||||||
glyphSet.insert(66, SDFGlyph{ 66 /* ASCII 'B' */,
|
|
||||||
AlphaImage({7, 7}), /* correct */
|
|
||||||
{ 1 /* width */, 1 /* height */, 0 /* left */, 0 /* top */,
|
|
||||||
0 /* advance */ } });
|
|
||||||
glyphSet.insert(67, SDFGlyph{ 67 /* ASCII 'C' */,
|
|
||||||
AlphaImage({518, 8}), /* correct */
|
|
||||||
{ 512 /* width */, 2 /* height */, 0 /* left */, 0 /* top */,
|
|
||||||
0 /* advance */ } });
|
|
||||||
|
|
||||||
|
|
||||||
const FontStack fontStack{ "Mock Font" };
|
const FontStack fontStack{ "Mock Font" };
|
||||||
|
|
||||||
GlyphAtlasTest test;
|
GlyphAtlasTest test;
|
||||||
GlyphPositions positions;
|
GlyphPositions positions;
|
||||||
|
|
||||||
|
auto glyphSet = test.glyphAtlas.getGlyphSet(fontStack);
|
||||||
|
glyphSet->insert(66, SDFGlyph{ 66 /* ASCII 'B' */,
|
||||||
|
AlphaImage({7, 7}), /* correct */
|
||||||
|
{ 1 /* width */, 1 /* height */, 0 /* left */, 0 /* top */,
|
||||||
|
0 /* advance */ } });
|
||||||
|
glyphSet->insert(67, SDFGlyph{ 67 /* ASCII 'C' */,
|
||||||
|
AlphaImage({518, 8}), /* correct */
|
||||||
|
{ 512 /* width */, 2 /* height */, 0 /* left */, 0 /* top */,
|
||||||
|
0 /* advance */ } });
|
||||||
|
|
||||||
test.glyphAtlas.addGlyphs(1, std::u16string{u"ABC"}, fontStack, glyphSet, positions);
|
test.glyphAtlas.addGlyphs(1, std::u16string{u"ABC"}, fontStack, glyphSet, positions);
|
||||||
|
|
||||||
ASSERT_EQ(2u, positions.size());
|
ASSERT_EQ(2u, positions.size());
|
||||||
|
|||||||
Reference in New Issue
Block a user