From a75cc8c06b8d931b3b85dd9ac6d852f84b06e5b2 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 9 Jul 2015 11:26:45 +0300 Subject: [PATCH] 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). --- src/mbgl/renderer/symbol_bucket.cpp | 2 +- src/mbgl/style/style.cpp | 4 +- src/mbgl/text/glyph_store.cpp | 102 ++++++++++++++-------------- src/mbgl/text/glyph_store.hpp | 36 ++++------ 4 files changed, 68 insertions(+), 76 deletions(-) diff --git a/src/mbgl/renderer/symbol_bucket.cpp b/src/mbgl/renderer/symbol_bucket.cpp index cfa56c992..c99b725e7 100644 --- a/src/mbgl/renderer/symbol_bucket.cpp +++ b/src/mbgl/renderer/symbol_bucket.cpp @@ -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; } diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index 994592408..5f92bb266 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -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(loop)), + glyphStore(std::make_unique()), glyphAtlas(std::make_unique(1024, 1024)), spriteStore(std::make_unique()), spriteAtlas(std::make_unique(512, 512, data.pixelRatio, *spriteStore)), diff --git a/src/mbgl/text/glyph_store.cpp b/src/mbgl/text/glyph_store.cpp index 0f507deb4..17c1524a8 100644 --- a/src/mbgl/text/glyph_store.cpp +++ b/src/mbgl/text/glyph_store.cpp @@ -1,95 +1,98 @@ #include -#include -#include +#include +#include #include -#include +#include namespace mbgl { -GlyphStore::GlyphStore(uv_loop_t* loop) - : asyncEmitGlyphRangeLoaded(std::make_unique(loop, [this] { emitGlyphRangeLoaded(); })), - asyncEmitGlyphRangeLoadedingFailed(std::make_unique(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& 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 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 lock(errorMessageMutex); - errorMessage = message; - asyncEmitGlyphRangeLoadedingFailed->send(); + emitGlyphRangeLoadingFailed(message); }; std::lock_guard lock(rangesMutex); auto& rangeSets = ranges[fontStackName]; + const auto& rangeSetsIt = rangeSets.find(range); + if (rangeSetsIt != rangeSets.end()) { + return; + } + + rangeSets.emplace(range, std::make_unique(glyphURL, fontStackName, range, + successCallback, failureCallback)); +} + + +bool GlyphStore::hasGlyphRanges(const std::string& fontStackName, const std::set& glyphRanges) { + if (glyphRanges.empty()) { + return true; + } + + std::lock_guard 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(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 GlyphStore::createFontStack(const std::string &fontStack) { +util::exclusive GlyphStore::createFontStack(const std::string& fontStack) { auto lock = std::make_unique>(stacksMutex); - auto stack_it = stacks.find(fontStack); - if (stack_it == stacks.end()) { - stack_it = stacks.emplace(fontStack, std::make_unique()).first; + auto it = stacks.find(fontStack); + if (it == stacks.end()) { + it = stacks.emplace(fontStack, std::make_unique()).first; } - return { stack_it->second.get(), std::move(lock) }; + return { it->second.get(), std::move(lock) }; } util::exclusive GlyphStore::getFontStack(const std::string &fontStack) { auto lock = std::make_unique>(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 lock(errorMessageMutex); - auto error = std::make_exception_ptr(util::GlyphRangeLoadingException(errorMessage)); + auto error = std::make_exception_ptr(util::GlyphRangeLoadingException(message)); observer->onGlyphRangeLoadingFailed(error); } diff --git a/src/mbgl/text/glyph_store.hpp b/src/mbgl/text/glyph_store.hpp index a1f69b994..5d0f498c7 100644 --- a/src/mbgl/text/glyph_store.hpp +++ b/src/mbgl/text/glyph_store.hpp @@ -2,19 +2,14 @@ #define MBGL_TEXT_GLYPH_STORE #include - #include +#include +#include +#include #include #include #include -#include - -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 &glyphRanges); + util::exclusive getFontStack(const std::string& fontStack); - util::exclusive getFontStack(const std::string &fontStack); + bool hasGlyphRanges(const std::string& fontStackName, const std::set& 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 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> stacks; std::mutex stacksMutex; - std::string errorMessage; - std::mutex errorMessageMutex; + util::WorkQueue workQueue; - std::unique_ptr asyncEmitGlyphRangeLoaded; - std::unique_ptr asyncEmitGlyphRangeLoadedingFailed; - - Observer* observer; + Observer* observer = nullptr; }; }