diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 38a4104e7..f373274eb 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -36,7 +36,7 @@ MapContext::MapContext(uv_loop_t* loop, View& view_, FileSource& fileSource, Map envScope(env, ThreadType::Map, "Map"), updated(static_cast(Update::Nothing)), asyncUpdate(util::make_unique(loop, [this] { update(); })), - glyphStore(util::make_unique(env)), + glyphStore(util::make_unique(loop, env)), glyphAtlas(util::make_unique(1024, 1024)), spriteAtlas(util::make_unique(512, 512)), lineAtlas(util::make_unique(512, 512)), @@ -134,6 +134,7 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base) resourceLoader->setAccessToken(data.getAccessToken()); resourceLoader->setObserver(this); resourceLoader->setStyle(style.get()); + resourceLoader->setGlyphStore(glyphStore.get()); triggerUpdate(Update::Zoom); } @@ -141,8 +142,7 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base) void MapContext::updateTiles() { assert(Environment::currentlyOn(ThreadType::Map)); - resourceLoader->update(data, transformState, *glyphAtlas, *glyphStore, - *spriteAtlas, *texturePool); + resourceLoader->update(data, transformState, *glyphAtlas, *spriteAtlas, *texturePool); } void MapContext::updateAnnotationTiles(const std::vector& ids) { diff --git a/src/mbgl/map/resource_loader.cpp b/src/mbgl/map/resource_loader.cpp index fba61cc64..2be86dc5e 100644 --- a/src/mbgl/map/resource_loader.cpp +++ b/src/mbgl/map/resource_loader.cpp @@ -25,6 +25,10 @@ ResourceLoader::~ResourceLoader() { if (sprite_) { sprite_->setObserver(nullptr); } + + if (glyphStore_) { + glyphStore_->setObserver(nullptr); + } } void ResourceLoader::setObserver(Observer* observer) { @@ -45,6 +49,18 @@ void ResourceLoader::setStyle(Style* style) { } } +void ResourceLoader::setGlyphStore(GlyphStore* glyphStore) { + assert(glyphStore); + + if (glyphStore_) { + glyphStore_->setObserver(nullptr); + } + + glyphStore_ = glyphStore; + glyphStore_->setObserver(this); +} + + void ResourceLoader::setAccessToken(const std::string& accessToken) { accessToken_ = accessToken; } @@ -52,7 +68,6 @@ void ResourceLoader::setAccessToken(const std::string& accessToken) { void ResourceLoader::update(MapData& data, const TransformState& transform, GlyphAtlas& glyphAtlas, - GlyphStore& glyphStore, SpriteAtlas& spriteAtlas, TexturePool& texturePool) { if (!style_) { @@ -68,13 +83,24 @@ void ResourceLoader::update(MapData& data, spriteAtlas.setSprite(sprite_); } + bool allTilesUpdated = true; for (const auto& source : style_->sources) { - source->update( - data, transform, *style_, glyphAtlas, glyphStore, spriteAtlas, sprite_, texturePool); + if (!source->update(data, transform, *style_, glyphAtlas, *glyphStore_, + spriteAtlas, sprite_, texturePool, shouldReparsePartialTiles_)) { + allTilesUpdated = false; + } + } + + // We can only stop updating "partial" tiles when all of them + // were notified of the arrival of the new resources. + if (allTilesUpdated) { + shouldReparsePartialTiles_ = false; } } void ResourceLoader::onGlyphRangeLoaded() { + shouldReparsePartialTiles_ = true; + emitTileDataChanged(); } @@ -82,11 +108,17 @@ void ResourceLoader::onSourceLoaded() { emitTileDataChanged(); } -void ResourceLoader::onTileLoaded() { +void ResourceLoader::onTileLoaded(bool isNewTile) { + if (isNewTile) { + shouldReparsePartialTiles_ = true; + } + emitTileDataChanged(); } void ResourceLoader::onSpriteLoaded() { + shouldReparsePartialTiles_ = true; + emitTileDataChanged(); } diff --git a/src/mbgl/map/resource_loader.hpp b/src/mbgl/map/resource_loader.hpp index a4d10d038..379444135 100644 --- a/src/mbgl/map/resource_loader.hpp +++ b/src/mbgl/map/resource_loader.hpp @@ -43,13 +43,17 @@ public: // a new style we will go through all of them and try to load. void setStyle(Style* style); + // TODO: Move GlyphStore to ResourceLoader. We cannot do it now + // because we reset the ResourceLoader every time we change the + // style. + void setGlyphStore(GlyphStore* glyphStore); + // Set the access token to be used for loading the tile data. void setAccessToken(const std::string& accessToken); // Fetch the tiles needed by the current viewport and emit a signal when // a tile is ready so observers can render the tile. - void update(MapData&, const TransformState&, GlyphAtlas&, GlyphStore&, - SpriteAtlas&, TexturePool&); + void update(MapData&, const TransformState&, GlyphAtlas&, SpriteAtlas&, TexturePool&); // FIXME: There is probably a better place for this. inline util::ptr getSprite() const { @@ -61,7 +65,7 @@ public: // Source::Observer implementation. void onSourceLoaded() override; - void onTileLoaded() override; + void onTileLoaded(bool isNewTile) override; // Sprite::Observer implementation. void onSpriteLoaded() override; @@ -69,9 +73,14 @@ public: private: void emitTileDataChanged(); + bool shouldReparsePartialTiles_ = false; + std::string accessToken_; util::ptr sprite_; + + GlyphStore* glyphStore_ = nullptr; Style* style_ = nullptr; + Observer* observer_ = nullptr; }; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 686b15c24..e8dc3468d 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -219,21 +219,30 @@ TileData::State Source::hasTile(const TileID& id) { return TileData::State::invalid; } -void Source::handlePartialTile(const TileID& id, Worker& worker) { +bool Source::handlePartialTile(const TileID& id, Worker& worker) { const TileID normalized_id = id.normalized(); auto it = tile_data.find(normalized_id); if (it == tile_data.end()) { - return; + return true; } util::ptr data = it->second.lock(); if (!data) { - return; + return true; } - auto callback = std::bind(&Source::emitTileLoaded, this); - data->reparse(worker, callback); + // The signal is only emitted if there was an actual change on the tile. The + // tile can be in a "partial" state waiting for resources and get reparsed on + // the arrival of new resources that were needed by another tile. + size_t bucketCount = static_cast(data.get())->countBuckets(); + auto callback = [this, data, bucketCount]() { + if (static_cast(data.get())->countBuckets() > bucketCount) { + emitTileLoaded(false); + } + }; + + return data->reparse(worker, callback); } TileData::State Source::addTile(MapData& data, @@ -273,7 +282,7 @@ TileData::State Source::addTile(MapData& data, new_tile.data = cache.get(normalized_id.to_uint64()); } - auto callback = std::bind(&Source::emitTileLoaded, this); + auto callback = std::bind(&Source::emitTileLoaded, this, true); if (!new_tile.data) { // If we don't find working tile data, we're just going to load it. if (info.type == SourceType::Vector) { @@ -377,16 +386,19 @@ bool Source::findLoadedParent(const TileID& id, int32_t minCoveringZoom, std::fo return false; } -void Source::update(MapData& data, +bool Source::update(MapData& data, const TransformState& transformState, Style& style, GlyphAtlas& glyphAtlas, GlyphStore& glyphStore, SpriteAtlas& spriteAtlas, util::ptr sprite, - TexturePool& texturePool) { + TexturePool& texturePool, + bool shouldReparsePartialTiles) { + bool allTilesUpdated = true; + if (!loaded || data.getAnimationTime() <= updated) { - return; + return allTilesUpdated; } int32_t zoom = std::floor(getZoom(transformState)); @@ -407,7 +419,11 @@ void Source::update(MapData& data, switch (state) { case TileData::State::partial: - handlePartialTile(id, style.workers); + if (shouldReparsePartialTiles) { + if (!handlePartialTile(id, style.workers)) { + allTilesUpdated = false; + } + } break; case TileData::State::invalid: state = addTile(data, transformState, style, glyphAtlas, glyphStore, @@ -482,6 +498,8 @@ void Source::update(MapData& data, updateTilePtrs(); updated = data.getAnimationTime(); + + return allTilesUpdated; } void Source::invalidateTiles(const std::vector& ids) { @@ -518,9 +536,9 @@ void Source::emitSourceLoaded() { } } -void Source::emitTileLoaded() { +void Source::emitTileLoaded(bool isNewTile) { if (observer_) { - observer_->onTileLoaded(); + observer_->onTileLoaded(isNewTile); } } diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp index 15cc8a817..9d3de81b7 100644 --- a/src/mbgl/map/source.hpp +++ b/src/mbgl/map/source.hpp @@ -58,7 +58,7 @@ public: virtual ~Observer() = default; virtual void onSourceLoaded() = 0; - virtual void onTileLoaded() = 0; + virtual void onTileLoaded(bool isNewTile) = 0; }; Source(); @@ -68,14 +68,20 @@ public: bool isLoaded() const; void load(MapData&, Environment&, std::function callback); - void update(MapData&, + + // Request or parse all the tiles relevant for the "TransformState". This method + // will return true if all the tiles were scheduled for updating of false if + // they were not. shouldReparsePartialTiles must be set to "true" if there is + // new data available that a tile in the "partial" state might be interested at. + bool update(MapData&, const TransformState&, Style&, GlyphAtlas&, GlyphStore&, SpriteAtlas&, util::ptr, - TexturePool&); + TexturePool&, + bool shouldReparsePartialTiles); void invalidateTiles(const std::vector&); @@ -96,9 +102,9 @@ public: private: void emitSourceLoaded(); - void emitTileLoaded(); + void emitTileLoaded(bool isNewTile); - void handlePartialTile(const TileID &id, Worker &worker); + bool handlePartialTile(const TileID &id, Worker &worker); bool findLoadedChildren(const TileID& id, int32_t maxCoveringZoom, std::forward_list& retain); bool findLoadedParent(const TileID& id, int32_t minCoveringZoom, std::forward_list& retain); int32_t coveringZoomLevel(const TransformState&) const; diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index aa66ceb80..e9084bdff 100644 --- a/src/mbgl/map/tile_data.cpp +++ b/src/mbgl/map/tile_data.cpp @@ -73,10 +73,11 @@ void TileData::endParsing() { parsing.clear(std::memory_order_release); } -void TileData::reparse(Worker& worker, std::function callback) { +bool TileData::reparse(Worker& worker, std::function callback) { if (!mayStartParsing()) { - return; + return false; } workRequest = worker.send([this] { parse(); endParsing(); }, callback); + return true; } diff --git a/src/mbgl/map/tile_data.hpp b/src/mbgl/map/tile_data.hpp index 7f16e7c41..3945a3214 100644 --- a/src/mbgl/map/tile_data.hpp +++ b/src/mbgl/map/tile_data.hpp @@ -45,7 +45,13 @@ public: ~TileData(); void request(Worker&, float pixelRatio, std::function callback); - void reparse(Worker&, std::function callback); + + // Schedule a tile reparse on a worker thread and call the callback on + // completion. It will return true if the work was schedule or false it was + // not, which can occur if the tile is already being parsed by another + // worker (see "mayStartParsing()"). + bool reparse(Worker&, std::function callback); + void cancel(); const std::string toString() const; diff --git a/src/mbgl/map/vector_tile_data.cpp b/src/mbgl/map/vector_tile_data.cpp index 263ad52b9..801d9a7c5 100644 --- a/src/mbgl/map/vector_tile_data.cpp +++ b/src/mbgl/map/vector_tile_data.cpp @@ -82,6 +82,12 @@ Bucket* VectorTileData::getBucket(StyleLayer const& layer) { return it->second.get(); } +size_t VectorTileData::countBuckets() const { + std::lock_guard lock(bucketsMutex); + + return buckets.size(); +} + void VectorTileData::setBucket(StyleLayer const& layer, std::unique_ptr bucket) { assert(layer.bucket); diff --git a/src/mbgl/map/vector_tile_data.hpp b/src/mbgl/map/vector_tile_data.hpp index 5fe351ca0..6549c106e 100644 --- a/src/mbgl/map/vector_tile_data.hpp +++ b/src/mbgl/map/vector_tile_data.hpp @@ -44,6 +44,7 @@ public: void parse() override; virtual Bucket* getBucket(StyleLayer const &layer_desc) override; + size_t countBuckets() const; void setBucket(StyleLayer const &layer_desc, std::unique_ptr bucket); void setState(const State& state) override; diff --git a/src/mbgl/text/glyph_store.cpp b/src/mbgl/text/glyph_store.cpp index 71c1be6d9..853191218 100644 --- a/src/mbgl/text/glyph_store.cpp +++ b/src/mbgl/text/glyph_store.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -236,8 +237,11 @@ bool GlyphPBF::isParsed() const { return parsed; } -GlyphStore::GlyphStore(Environment& env_) - : env(env_), observer(nullptr) { +GlyphStore::GlyphStore(uv_loop_t* loop, Environment& env_) + : env(env_), + asyncEmitGlyphRangeLoaded(util::make_unique(loop, [this] { emitGlyphRangeLoaded(); })), + observer(nullptr) { + asyncEmitGlyphRangeLoaded->unref(); } GlyphStore::~GlyphStore() { @@ -258,7 +262,7 @@ bool GlyphStore::requestGlyphRangesIfNeeded(const std::string& fontStack, auto callback = [this, fontStack](GlyphPBF* glyph) { glyph->parse(*createFontStack(fontStack)); - emitGlyphRangeLoaded(); + asyncEmitGlyphRangeLoaded->send(); }; std::lock_guard lock(rangesMutex); diff --git a/src/mbgl/text/glyph_store.hpp b/src/mbgl/text/glyph_store.hpp index a41eec533..efa848dd0 100644 --- a/src/mbgl/text/glyph_store.hpp +++ b/src/mbgl/text/glyph_store.hpp @@ -13,6 +13,14 @@ #include #include +typedef struct uv_loop_s uv_loop_t; + +namespace uv { + +class async; + +} + namespace mbgl { class FileSource; @@ -87,7 +95,7 @@ public: virtual void onGlyphRangeLoaded() = 0; }; - GlyphStore(Environment &); + GlyphStore(uv_loop_t* loop, Environment &); ~GlyphStore(); // Asynchronously request for GlyphRanges and when it gets loaded, notifies the @@ -116,6 +124,8 @@ private: std::unordered_map> stacks; std::mutex stacksMutex; + std::unique_ptr asyncEmitGlyphRangeLoaded; + Observer* observer; };