From f222efb939a6c6055d97fdae6ed36960948864e6 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 7 May 2015 16:56:04 +0300 Subject: [PATCH 1/5] Keep a pointer to GlyphStore on ResourceLoader For now, keep a pointer so we can subscribe/unsubscribe to events. In the future, we should move this object completely to ResourceLoader, but we can't do it now because we delete the ResourceLoader when changing the style. --- src/mbgl/map/map_context.cpp | 4 ++-- src/mbgl/map/resource_loader.cpp | 21 ++++++++++++++++++--- src/mbgl/map/resource_loader.hpp | 11 +++++++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 38a4104e7..5191293a0 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -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..0f5c33abd 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_) { @@ -69,8 +84,8 @@ void ResourceLoader::update(MapData& data, } for (const auto& source : style_->sources) { - source->update( - data, transform, *style_, glyphAtlas, glyphStore, spriteAtlas, sprite_, texturePool); + source->update(data, transform, *style_, glyphAtlas, *glyphStore_, + spriteAtlas, sprite_, texturePool); } } diff --git a/src/mbgl/map/resource_loader.hpp b/src/mbgl/map/resource_loader.hpp index a4d10d038..6f63fe226 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 { @@ -71,7 +75,10 @@ private: std::string accessToken_; util::ptr sprite_; + + GlyphStore* glyphStore_ = nullptr; Style* style_ = nullptr; + Observer* observer_ = nullptr; }; From 41dc5e836a9b37a8c3bf4f1116de09aceec4264d Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 7 May 2015 21:00:20 +0300 Subject: [PATCH 2/5] Emit the signal on the MapThread We were previously emitting the signal on the thread parsing the glyph, but signals can only be trigger from the MapThread because the observers are living there. --- src/mbgl/map/map_context.cpp | 2 +- src/mbgl/text/glyph_store.cpp | 10 +++++++--- src/mbgl/text/glyph_store.hpp | 12 +++++++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 5191293a0..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)), 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; }; From 3f445e8578b25dbe4adccacd59f317cbb3b9a8d7 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 7 May 2015 16:09:15 +0300 Subject: [PATCH 3/5] Do not process partial tiles if no new resources have arrived Partial tiles are now only processed again when there is a new resource available. If we don't have new resources - like new glyphs - the end result of the reparsing is the same, because no new SymbolBuckets would are created. In my tests debugging with PowerTOP, this patch drops the CPU utilization during the sampling period from 100ms/s to 40ms/s. The improvements on slow connections (i.e. more time waiting for resources) are even more noticeable. --- src/mbgl/map/resource_loader.cpp | 8 +++++++- src/mbgl/map/resource_loader.hpp | 2 ++ src/mbgl/map/source.cpp | 6 ++++-- src/mbgl/map/source.hpp | 3 ++- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/mbgl/map/resource_loader.cpp b/src/mbgl/map/resource_loader.cpp index 0f5c33abd..46b7af9a2 100644 --- a/src/mbgl/map/resource_loader.cpp +++ b/src/mbgl/map/resource_loader.cpp @@ -85,11 +85,15 @@ void ResourceLoader::update(MapData& data, for (const auto& source : style_->sources) { source->update(data, transform, *style_, glyphAtlas, *glyphStore_, - spriteAtlas, sprite_, texturePool); + spriteAtlas, sprite_, texturePool, shouldReparsePartialTiles_); } + + shouldReparsePartialTiles_ = false; } void ResourceLoader::onGlyphRangeLoaded() { + shouldReparsePartialTiles_ = true; + emitTileDataChanged(); } @@ -102,6 +106,8 @@ void ResourceLoader::onTileLoaded() { } void ResourceLoader::onSpriteLoaded() { + shouldReparsePartialTiles_ = true; + emitTileDataChanged(); } diff --git a/src/mbgl/map/resource_loader.hpp b/src/mbgl/map/resource_loader.hpp index 6f63fe226..cf73db5fa 100644 --- a/src/mbgl/map/resource_loader.hpp +++ b/src/mbgl/map/resource_loader.hpp @@ -73,6 +73,8 @@ public: private: void emitTileDataChanged(); + bool shouldReparsePartialTiles_ = false; + std::string accessToken_; util::ptr sprite_; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 686b15c24..501dde5c2 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -384,7 +384,8 @@ void Source::update(MapData& data, GlyphStore& glyphStore, SpriteAtlas& spriteAtlas, util::ptr sprite, - TexturePool& texturePool) { + TexturePool& texturePool, + bool shouldReparsePartialTiles) { if (!loaded || data.getAnimationTime() <= updated) { return; } @@ -407,7 +408,8 @@ void Source::update(MapData& data, switch (state) { case TileData::State::partial: - handlePartialTile(id, style.workers); + if (shouldReparsePartialTiles) + handlePartialTile(id, style.workers); break; case TileData::State::invalid: state = addTile(data, transformState, style, glyphAtlas, glyphStore, diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp index 15cc8a817..d5f4f8b5d 100644 --- a/src/mbgl/map/source.hpp +++ b/src/mbgl/map/source.hpp @@ -75,7 +75,8 @@ public: GlyphStore&, SpriteAtlas&, util::ptr, - TexturePool&); + TexturePool&, + bool shouldReparsePartialTiles); void invalidateTiles(const std::vector&); From 4e7c97466cdca899973dce929b7e13621e558d4a Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Fri, 8 May 2015 19:21:50 +0300 Subject: [PATCH 4/5] Allow reparsing partial tiles when we have new tiles When parsing a tile, we request resources from the network, like sprites and glyphs. In the rare case of the resources arriving before the tile is parsed for the first time, if we don't mark it for being parsed again, it can end up with a tile eternally stuck in the partial state. --- src/mbgl/map/resource_loader.cpp | 6 +++++- src/mbgl/map/resource_loader.hpp | 2 +- src/mbgl/map/source.cpp | 8 ++++---- src/mbgl/map/source.hpp | 4 ++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/mbgl/map/resource_loader.cpp b/src/mbgl/map/resource_loader.cpp index 46b7af9a2..7b5ebd19d 100644 --- a/src/mbgl/map/resource_loader.cpp +++ b/src/mbgl/map/resource_loader.cpp @@ -101,7 +101,11 @@ void ResourceLoader::onSourceLoaded() { emitTileDataChanged(); } -void ResourceLoader::onTileLoaded() { +void ResourceLoader::onTileLoaded(bool isNewTile) { + if (isNewTile) { + shouldReparsePartialTiles_ = true; + } + emitTileDataChanged(); } diff --git a/src/mbgl/map/resource_loader.hpp b/src/mbgl/map/resource_loader.hpp index cf73db5fa..379444135 100644 --- a/src/mbgl/map/resource_loader.hpp +++ b/src/mbgl/map/resource_loader.hpp @@ -65,7 +65,7 @@ public: // Source::Observer implementation. void onSourceLoaded() override; - void onTileLoaded() override; + void onTileLoaded(bool isNewTile) override; // Sprite::Observer implementation. void onSpriteLoaded() override; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 501dde5c2..e40cb0f16 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -232,7 +232,7 @@ void Source::handlePartialTile(const TileID& id, Worker& worker) { return; } - auto callback = std::bind(&Source::emitTileLoaded, this); + auto callback = std::bind(&Source::emitTileLoaded, this, false); data->reparse(worker, callback); } @@ -273,7 +273,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) { @@ -520,9 +520,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 d5f4f8b5d..fcb1b4183 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(); @@ -97,7 +97,7 @@ public: private: void emitSourceLoaded(); - void emitTileLoaded(); + void emitTileLoaded(bool isNewTile); void handlePartialTile(const TileID &id, Worker &worker); bool findLoadedChildren(const TileID& id, int32_t maxCoveringZoom, std::forward_list& retain); From aaaf1e4c148282d3371836f9fe7838e8fb09bb83 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Mon, 11 May 2015 18:41:44 +0300 Subject: [PATCH 5/5] Only emit tile updated signals when the tile really changes We only emit signals when the number of buckets increase (i.e. the new data that arrived added a new bucket to the tile). Also, if we try to reparse a partial tile but it is not possible, we keep the flag up for trying to reparse it again on the next update, because the data might have arrived after the bucket that is waiting for the data was processed but before the end of the parsing. Fixes #1458 --- src/mbgl/map/resource_loader.cpp | 13 +++++++++--- src/mbgl/map/source.cpp | 34 +++++++++++++++++++++++-------- src/mbgl/map/source.hpp | 9 ++++++-- src/mbgl/map/tile_data.cpp | 5 +++-- src/mbgl/map/tile_data.hpp | 8 +++++++- src/mbgl/map/vector_tile_data.cpp | 6 ++++++ src/mbgl/map/vector_tile_data.hpp | 1 + 7 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/mbgl/map/resource_loader.cpp b/src/mbgl/map/resource_loader.cpp index 7b5ebd19d..2be86dc5e 100644 --- a/src/mbgl/map/resource_loader.cpp +++ b/src/mbgl/map/resource_loader.cpp @@ -83,12 +83,19 @@ 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, shouldReparsePartialTiles_); + if (!source->update(data, transform, *style_, glyphAtlas, *glyphStore_, + spriteAtlas, sprite_, texturePool, shouldReparsePartialTiles_)) { + allTilesUpdated = false; + } } - shouldReparsePartialTiles_ = 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() { diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index e40cb0f16..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, false); - 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, @@ -377,7 +386,7 @@ 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, @@ -386,8 +395,10 @@ void Source::update(MapData& data, util::ptr sprite, TexturePool& texturePool, bool shouldReparsePartialTiles) { + bool allTilesUpdated = true; + if (!loaded || data.getAnimationTime() <= updated) { - return; + return allTilesUpdated; } int32_t zoom = std::floor(getZoom(transformState)); @@ -408,8 +419,11 @@ void Source::update(MapData& data, switch (state) { case TileData::State::partial: - if (shouldReparsePartialTiles) - 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, @@ -484,6 +498,8 @@ void Source::update(MapData& data, updateTilePtrs(); updated = data.getAnimationTime(); + + return allTilesUpdated; } void Source::invalidateTiles(const std::vector& ids) { diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp index fcb1b4183..9d3de81b7 100644 --- a/src/mbgl/map/source.hpp +++ b/src/mbgl/map/source.hpp @@ -68,7 +68,12 @@ 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&, @@ -99,7 +104,7 @@ private: void emitSourceLoaded(); 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;