Make Glyph requests from the Map Thread
Now the requests are bound to the Map Thread loop and we can cancel it quickly when shutting down or changing style. Previously requests were made by the workers and we would have to wait them to finish because workers are destroyed before the GlyphStore. This was giving the impression that the application was frozen (in fact it was, momentarily).
This commit is contained in:
@@ -160,7 +160,7 @@ bool SymbolBucket::needsDependencies(const GeometryTileLayer& layer,
|
||||
util::mergeLines(features);
|
||||
}
|
||||
|
||||
if (glyphStore.requestGlyphRangesIfNeeded(layout.text.font, ranges)) {
|
||||
if (!glyphStore.hasGlyphRanges(layout.text.font, ranges)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
Style::Style(MapData& data_, uv_loop_t* loop)
|
||||
Style::Style(MapData& data_, uv_loop_t*)
|
||||
: data(data_),
|
||||
glyphStore(std::make_unique<GlyphStore>(loop)),
|
||||
glyphStore(std::make_unique<GlyphStore>()),
|
||||
glyphAtlas(std::make_unique<GlyphAtlas>(1024, 1024)),
|
||||
spriteStore(std::make_unique<SpriteStore>()),
|
||||
spriteAtlas(std::make_unique<SpriteAtlas>(512, 512, data.pixelRatio, *spriteStore)),
|
||||
|
||||
@@ -1,95 +1,98 @@
|
||||
#include <mbgl/text/glyph_store.hpp>
|
||||
#include <mbgl/text/glyph_pbf.hpp>
|
||||
#include <mbgl/text/font_stack.hpp>
|
||||
|
||||
#include <mbgl/text/font_stack.hpp>
|
||||
#include <mbgl/text/glyph_pbf.hpp>
|
||||
#include <mbgl/util/exception.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
#include <mbgl/util/thread_context.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
GlyphStore::GlyphStore(uv_loop_t* loop)
|
||||
: asyncEmitGlyphRangeLoaded(std::make_unique<uv::async>(loop, [this] { emitGlyphRangeLoaded(); })),
|
||||
asyncEmitGlyphRangeLoadedingFailed(std::make_unique<uv::async>(loop, [this] { emitGlyphRangeLoadingFailed(); })),
|
||||
observer(nullptr) {
|
||||
asyncEmitGlyphRangeLoaded->unref();
|
||||
asyncEmitGlyphRangeLoadedingFailed->unref();
|
||||
GlyphStore::GlyphStore() {
|
||||
}
|
||||
|
||||
GlyphStore::~GlyphStore() {
|
||||
observer = nullptr;
|
||||
}
|
||||
|
||||
void GlyphStore::setURL(const std::string &url) {
|
||||
glyphURL = url;
|
||||
}
|
||||
|
||||
bool GlyphStore::requestGlyphRangesIfNeeded(const std::string& fontStackName,
|
||||
const std::set<GlyphRange>& glyphRanges) {
|
||||
bool requestIsNeeded = false;
|
||||
|
||||
if (glyphRanges.empty()) {
|
||||
return requestIsNeeded;
|
||||
}
|
||||
void GlyphStore::requestGlyphRange(const std::string& fontStackName, const GlyphRange& range) {
|
||||
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
|
||||
|
||||
auto successCallback = [this, fontStackName](GlyphPBF* glyph) {
|
||||
auto fontStack = createFontStack(fontStackName);
|
||||
try {
|
||||
glyph->parse(**fontStack);
|
||||
asyncEmitGlyphRangeLoaded->send();
|
||||
{
|
||||
auto fontStack = createFontStack(fontStackName);
|
||||
glyph->parse(**fontStack);
|
||||
}
|
||||
emitGlyphRangeLoaded();
|
||||
} catch (const std::exception&) {
|
||||
std::lock_guard<std::mutex> lock(errorMessageMutex);
|
||||
errorMessage = "Failed to parse [" + glyph->getURL() + "]";
|
||||
asyncEmitGlyphRangeLoadedingFailed->send();
|
||||
std::string message = "Failed to parse [" + glyph->getURL() + "]";
|
||||
emitGlyphRangeLoadingFailed(message);
|
||||
}
|
||||
};
|
||||
|
||||
auto failureCallback = [this](const std::string& message) {
|
||||
std::lock_guard<std::mutex> lock(errorMessageMutex);
|
||||
errorMessage = message;
|
||||
asyncEmitGlyphRangeLoadedingFailed->send();
|
||||
emitGlyphRangeLoadingFailed(message);
|
||||
};
|
||||
|
||||
std::lock_guard<std::mutex> lock(rangesMutex);
|
||||
auto& rangeSets = ranges[fontStackName];
|
||||
|
||||
const auto& rangeSetsIt = rangeSets.find(range);
|
||||
if (rangeSetsIt != rangeSets.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
rangeSets.emplace(range, std::make_unique<GlyphPBF>(glyphURL, fontStackName, range,
|
||||
successCallback, failureCallback));
|
||||
}
|
||||
|
||||
|
||||
bool GlyphStore::hasGlyphRanges(const std::string& fontStackName, const std::set<GlyphRange>& glyphRanges) {
|
||||
if (glyphRanges.empty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(rangesMutex);
|
||||
const auto& rangeSets = ranges[fontStackName];
|
||||
|
||||
bool hasRanges = true;
|
||||
|
||||
for (const auto& range : glyphRanges) {
|
||||
const auto& rangeSets_it = rangeSets.find(range);
|
||||
if (rangeSets_it == rangeSets.end()) {
|
||||
auto glyph = std::make_unique<GlyphPBF>(glyphURL, fontStackName, range,
|
||||
successCallback, failureCallback);
|
||||
rangeSets.emplace(range, std::move(glyph));
|
||||
requestIsNeeded = true;
|
||||
const auto& rangeSetsIt = rangeSets.find(range);
|
||||
if (rangeSetsIt == rangeSets.end()) {
|
||||
// Post the request to the Map thread.
|
||||
workQueue.push(std::bind(&GlyphStore::requestGlyphRange, this, fontStackName, range));
|
||||
hasRanges = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!rangeSets_it->second->isParsed()) {
|
||||
requestIsNeeded = true;
|
||||
if (!rangeSetsIt->second->isParsed()) {
|
||||
hasRanges = false;
|
||||
}
|
||||
}
|
||||
|
||||
return requestIsNeeded;
|
||||
return hasRanges;
|
||||
}
|
||||
|
||||
util::exclusive<FontStack> GlyphStore::createFontStack(const std::string &fontStack) {
|
||||
util::exclusive<FontStack> GlyphStore::createFontStack(const std::string& fontStack) {
|
||||
auto lock = std::make_unique<std::lock_guard<std::mutex>>(stacksMutex);
|
||||
|
||||
auto stack_it = stacks.find(fontStack);
|
||||
if (stack_it == stacks.end()) {
|
||||
stack_it = stacks.emplace(fontStack, std::make_unique<FontStack>()).first;
|
||||
auto it = stacks.find(fontStack);
|
||||
if (it == stacks.end()) {
|
||||
it = stacks.emplace(fontStack, std::make_unique<FontStack>()).first;
|
||||
}
|
||||
|
||||
return { stack_it->second.get(), std::move(lock) };
|
||||
return { it->second.get(), std::move(lock) };
|
||||
}
|
||||
|
||||
util::exclusive<FontStack> GlyphStore::getFontStack(const std::string &fontStack) {
|
||||
auto lock = std::make_unique<std::lock_guard<std::mutex>>(stacksMutex);
|
||||
|
||||
const auto& stack_it = stacks.find(fontStack);
|
||||
if (stack_it == stacks.end()) {
|
||||
const auto& it = stacks.find(fontStack);
|
||||
if (it == stacks.end()) {
|
||||
return { nullptr, nullptr };
|
||||
}
|
||||
|
||||
return { stack_it->second.get(), std::move(lock) };
|
||||
return { it->second.get(), std::move(lock) };
|
||||
}
|
||||
|
||||
void GlyphStore::setObserver(Observer* observer_) {
|
||||
@@ -102,13 +105,12 @@ void GlyphStore::emitGlyphRangeLoaded() {
|
||||
}
|
||||
}
|
||||
|
||||
void GlyphStore::emitGlyphRangeLoadingFailed() {
|
||||
void GlyphStore::emitGlyphRangeLoadingFailed(const std::string& message) {
|
||||
if (!observer) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(errorMessageMutex);
|
||||
auto error = std::make_exception_ptr(util::GlyphRangeLoadingException(errorMessage));
|
||||
auto error = std::make_exception_ptr(util::GlyphRangeLoadingException(message));
|
||||
observer->onGlyphRangeLoadingFailed(error);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,19 +2,14 @@
|
||||
#define MBGL_TEXT_GLYPH_STORE
|
||||
|
||||
#include <mbgl/text/glyph.hpp>
|
||||
|
||||
#include <mbgl/util/exclusive.hpp>
|
||||
#include <mbgl/util/run_loop.hpp>
|
||||
#include <mbgl/util/work_queue.hpp>
|
||||
|
||||
#include <exception>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <exception>
|
||||
|
||||
typedef struct uv_loop_s uv_loop_t;
|
||||
|
||||
namespace uv {
|
||||
class async;
|
||||
}
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -32,26 +27,25 @@ public:
|
||||
virtual void onGlyphRangeLoadingFailed(std::exception_ptr error) = 0;
|
||||
};
|
||||
|
||||
GlyphStore(uv_loop_t* loop);
|
||||
GlyphStore();
|
||||
~GlyphStore();
|
||||
|
||||
// Asynchronously request for GlyphRanges and when it gets loaded, notifies the
|
||||
// observer subscribed to this object. Successive requests for the same range are
|
||||
// going to be discarded. Returns true if a request was made or false if all the
|
||||
// GlyphRanges are already available, and thus, no request is performed.
|
||||
bool requestGlyphRangesIfNeeded(const std::string &fontStack, const std::set<GlyphRange> &glyphRanges);
|
||||
util::exclusive<FontStack> getFontStack(const std::string& fontStack);
|
||||
|
||||
util::exclusive<FontStack> getFontStack(const std::string &fontStack);
|
||||
bool hasGlyphRanges(const std::string& fontStackName, const std::set<GlyphRange>& glyphRanges);
|
||||
|
||||
void setURL(const std::string &url);
|
||||
void setURL(const std::string &url) {
|
||||
glyphURL = url;
|
||||
}
|
||||
|
||||
void setObserver(Observer* observer);
|
||||
|
||||
private:
|
||||
void emitGlyphRangeLoaded();
|
||||
void emitGlyphRangeLoadingFailed();
|
||||
void emitGlyphRangeLoadingFailed(const std::string& message);
|
||||
|
||||
util::exclusive<FontStack> createFontStack(const std::string &fontStack);
|
||||
void requestGlyphRange(const std::string& fontStackName, const GlyphRange& range);
|
||||
|
||||
std::string glyphURL;
|
||||
|
||||
@@ -61,13 +55,9 @@ private:
|
||||
std::unordered_map<std::string, std::unique_ptr<FontStack>> stacks;
|
||||
std::mutex stacksMutex;
|
||||
|
||||
std::string errorMessage;
|
||||
std::mutex errorMessageMutex;
|
||||
util::WorkQueue workQueue;
|
||||
|
||||
std::unique_ptr<uv::async> asyncEmitGlyphRangeLoaded;
|
||||
std::unique_ptr<uv::async> asyncEmitGlyphRangeLoadedingFailed;
|
||||
|
||||
Observer* observer;
|
||||
Observer* observer = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user