diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-05-23 13:27:43 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2016-06-10 12:42:14 +0200 |
commit | 436d8eb5e09948b3baa86570fe16e3964fddaae0 (patch) | |
tree | bb9b50af3ebe9a7e805807c8fbe2a92ef089fafe /src/mbgl | |
parent | 2b6d0751cf3495c9246af27fdff6f565186bb2f8 (diff) | |
download | qtlocation-mapboxgl-436d8eb5e09948b3baa86570fe16e3964fddaae0.tar.gz |
[core] introduce TileDataObserver
avoids passing around bound callback functions
Diffstat (limited to 'src/mbgl')
-rw-r--r-- | src/mbgl/style/source.cpp | 46 | ||||
-rw-r--r-- | src/mbgl/style/source.hpp | 9 | ||||
-rw-r--r-- | src/mbgl/tile/geometry_tile_data.cpp | 35 | ||||
-rw-r--r-- | src/mbgl/tile/geometry_tile_data.hpp | 9 | ||||
-rw-r--r-- | src/mbgl/tile/raster_tile_data.cpp | 21 | ||||
-rw-r--r-- | src/mbgl/tile/raster_tile_data.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/tile/tile_data.cpp | 9 | ||||
-rw-r--r-- | src/mbgl/tile/tile_data.hpp | 8 | ||||
-rw-r--r-- | src/mbgl/tile/tile_data_observer.hpp | 18 |
9 files changed, 84 insertions, 76 deletions
diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index 51e6334440..a31c804b01 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -197,9 +197,6 @@ std::unique_ptr<TileData> Source::createTile(const OverscaledTileID& overscaledT return data; } - auto callback = std::bind(&Source::tileLoadingCallback, this, overscaledTileID, - std::placeholders::_1, true); - // If we don't find working tile data, we're just going to load it. if (type == SourceType::Raster) { assert(!tileset->tiles.empty()); @@ -207,7 +204,8 @@ std::unique_ptr<TileData> Source::createTile(const OverscaledTileID& overscaledT tileset->tiles.at(0), parameters.pixelRatio, overscaledTileID.canonical.x, overscaledTileID.canonical.y, overscaledTileID.canonical.z); auto data = std::make_unique<RasterTileData>(overscaledTileID, parameters.texturePool, - parameters.worker, callback); + parameters.worker); + data->setObserver(this); data->setTileSource( std::make_unique<ImageTileSource>(*data, resource, parameters.fileSource)); @@ -216,7 +214,8 @@ std::unique_ptr<TileData> Source::createTile(const OverscaledTileID& overscaledT return std::move(data); } else { auto data = std::make_unique<GeometryTileData>(overscaledTileID, id, parameters.style, - parameters.mode, callback); + parameters.mode); + data->setObserver(this); if (type == SourceType::Vector) { assert(!tileset->tiles.empty()); const auto resource = Resource::tile( @@ -327,6 +326,9 @@ bool Source::update(const UpdateParameters& parameters) { } } + const PlacementConfig newConfig{ parameters.transformState.getAngle(), + parameters.transformState.getPitch(), + parameters.debugOptions & MapDebugOptions::Collision }; for (auto& pair : tileDataMap) { auto tileData = pair.second.get(); if (parameters.shouldReparsePartialTiles && tileData->isIncomplete()) { @@ -334,10 +336,7 @@ bool Source::update(const UpdateParameters& parameters) { allTilesUpdated = false; } } else { - tileData->redoPlacement({ parameters.transformState.getAngle(), - parameters.transformState.getPitch(), - parameters.debugOptions & MapDebugOptions::Collision }, - [this]() { observer->onPlacementRedone(); }); + tileData->redoPlacement(newConfig); } } @@ -406,28 +405,17 @@ void Source::setObserver(SourceObserver* observer_) { observer = observer_; } -void Source::tileLoadingCallback(const OverscaledTileID& tileID, - std::exception_ptr error, - bool isNewTile) { - auto it = tileDataMap.find(tileID); - if (it == tileDataMap.end()) { - return; - } - - auto& tileData = it->second; - if (!tileData) { - return; - } +void Source::onTileLoaded(TileData& tileData, bool isNewTile) { + tileData.redoPlacement(); + observer->onTileLoaded(*this, tileData.id, isNewTile); +} - if (error) { - observer->onTileError(*this, tileID, error); - return; - } +void Source::onTileError(TileData& tileData, std::exception_ptr error) { + observer->onTileError(*this, tileData.id, error); +} - tileData->redoPlacement([this]() { - observer->onPlacementRedone(); - }); - observer->onTileLoaded(*this, tileID, isNewTile); +void Source::onPlacementRedone(TileData&) { + observer->onPlacementRedone(); } void Source::dumpDebugLogs() const { diff --git a/src/mbgl/style/source.hpp b/src/mbgl/style/source.hpp index 07e1aeaf58..4eae37a2a4 100644 --- a/src/mbgl/style/source.hpp +++ b/src/mbgl/style/source.hpp @@ -1,10 +1,12 @@ #pragma once #include <mbgl/tile/tile_id.hpp> +#include <mbgl/tile/tile_data_observer.hpp> #include <mbgl/tile/tile_data.hpp> #include <mbgl/tile/tile_cache.hpp> #include <mbgl/style/types.hpp> +#include <mbgl/util/noncopyable.hpp> #include <mbgl/util/mat4.hpp> #include <mbgl/util/rapidjson.hpp> #include <mbgl/util/feature.hpp> @@ -36,7 +38,7 @@ class UpdateParameters; class QueryParameters; class SourceObserver; -class Source : private util::noncopyable { +class Source : public TileDataObserver, private util::noncopyable { public: Source(SourceType, const std::string& id, @@ -87,7 +89,10 @@ public: bool enabled = false; private: - void tileLoadingCallback(const OverscaledTileID&, std::exception_ptr, bool isNewTile); + // TileDataObserver implementation. + void onTileLoaded(TileData&, bool isNewTile) override; + void onTileError(TileData&, std::exception_ptr) override; + void onPlacementRedone(TileData&) override; std::unique_ptr<TileData> createTile(const OverscaledTileID&, const UpdateParameters&); diff --git a/src/mbgl/tile/geometry_tile_data.cpp b/src/mbgl/tile/geometry_tile_data.cpp index 9e813a8633..d367fa4d07 100644 --- a/src/mbgl/tile/geometry_tile_data.cpp +++ b/src/mbgl/tile/geometry_tile_data.cpp @@ -1,4 +1,5 @@ #include <mbgl/tile/geometry_tile_data.hpp> +#include <mbgl/tile/tile_data_observer.hpp> #include <mbgl/tile/tile_source.hpp> #include <mbgl/tile/geometry_tile.hpp> #include <mbgl/style/layer_impl.hpp> @@ -15,8 +16,7 @@ namespace mbgl { GeometryTileData::GeometryTileData(const OverscaledTileID& id_, std::string sourceID, style::Style& style_, - const MapMode mode_, - const std::function<void(std::exception_ptr)>& callback_) + const MapMode mode_) : TileData(id_), style(style_), worker(style_.workers), @@ -26,8 +26,7 @@ GeometryTileData::GeometryTileData(const OverscaledTileID& id_, *style_.glyphAtlas, *style_.glyphStore, obsolete, - mode_), - callback(callback_) { + mode_) { } void GeometryTileData::setData(std::exception_ptr err, @@ -35,7 +34,7 @@ void GeometryTileData::setData(std::exception_ptr err, optional<Timestamp> modified_, optional<Timestamp> expires_) { if (err) { - callback(err); + observer->onTileError(*this, err); return; } @@ -47,7 +46,7 @@ void GeometryTileData::setData(std::exception_ptr err, workRequest.reset(); availableData = DataAvailability::All; buckets.clear(); - callback(err); + observer->onTileLoaded(*this, true); return; } @@ -64,7 +63,6 @@ void GeometryTileData::setData(std::exception_ptr err, workRequest = worker.parseGeometryTile(tileWorker, style.getLayers(), std::move(tile), targetConfig, [this, config = targetConfig] (TileParseResult result) { workRequest.reset(); - std::exception_ptr error; if (result.is<TileParseResultData>()) { auto& resultBuckets = result.get<TileParseResultData>(); availableData = resultBuckets.complete ? DataAvailability::All : DataAvailability::Some; @@ -82,12 +80,11 @@ void GeometryTileData::setData(std::exception_ptr err, geometryTile = std::move(resultBuckets.geometryTile); } + observer->onTileLoaded(*this, true); } else { - error = result.get<std::exception_ptr>(); availableData = DataAvailability::All; + observer->onTileError(*this, result.get<std::exception_ptr>()); } - - callback(error); }); } @@ -105,7 +102,6 @@ bool GeometryTileData::parsePending() { workRequest = worker.parsePendingGeometryTileLayers(tileWorker, targetConfig, [this, config = targetConfig] (TileParseResult result) { workRequest.reset(); - std::exception_ptr error; if (result.is<TileParseResultData>()) { auto& resultBuckets = result.get<TileParseResultData>(); availableData = resultBuckets.complete ? DataAvailability::All : DataAvailability::Some; @@ -125,12 +121,11 @@ bool GeometryTileData::parsePending() { geometryTile = std::move(resultBuckets.geometryTile); } + observer->onTileLoaded(*this, false); } else { - error = result.get<std::exception_ptr>(); availableData = DataAvailability::All; + observer->onTileError(*this, result.get<std::exception_ptr>()); } - - callback(error); }); return true; @@ -146,20 +141,20 @@ Bucket* GeometryTileData::getBucket(const style::Layer& layer) { return it->second.get(); } -void GeometryTileData::redoPlacement(const PlacementConfig newConfig, const std::function<void()>& cb) { +void GeometryTileData::redoPlacement(const PlacementConfig newConfig) { if (newConfig != placedConfig) { targetConfig = newConfig; - redoPlacement(cb); + redoPlacement(); } } -void GeometryTileData::redoPlacement(const std::function<void()>& cb) { +void GeometryTileData::redoPlacement() { // Don't start a new placement request when the current one hasn't completed yet, or when // we are parsing buckets. if (workRequest) return; - workRequest = worker.redoPlacement(tileWorker, buckets, targetConfig, [this, cb, config = targetConfig](std::unique_ptr<CollisionTile> collisionTile) { + workRequest = worker.redoPlacement(tileWorker, buckets, targetConfig, [this, config = targetConfig](std::unique_ptr<CollisionTile> collisionTile) { workRequest.reset(); // Persist the configuration we just placed so that we can later check whether we need to @@ -177,9 +172,9 @@ void GeometryTileData::redoPlacement(const std::function<void()>& cb) { // The target configuration could have changed since we started placement. In this case, // we're starting another placement run. if (placedConfig != targetConfig) { - redoPlacement(cb); + redoPlacement(); } else { - cb(); + observer->onPlacementRedone(*this); } }); } diff --git a/src/mbgl/tile/geometry_tile_data.hpp b/src/mbgl/tile/geometry_tile_data.hpp index 8a16a32958..d4eef956a5 100644 --- a/src/mbgl/tile/geometry_tile_data.hpp +++ b/src/mbgl/tile/geometry_tile_data.hpp @@ -25,8 +25,7 @@ public: GeometryTileData(const OverscaledTileID&, std::string sourceID, style::Style&, - const MapMode, - const std::function<void(std::exception_ptr)>& callback); + const MapMode); ~GeometryTileData(); @@ -39,8 +38,8 @@ public: bool parsePending() override; - void redoPlacement(PlacementConfig config, const std::function<void()>&) override; - void redoPlacement(const std::function<void()>&) override; + void redoPlacement(PlacementConfig config) override; + void redoPlacement() override; void queryRenderedFeatures( std::unordered_map<std::string, std::vector<Feature>>& result, @@ -73,8 +72,6 @@ private: // Used to signal the worker that it should abandon parsing this tile as soon as possible. util::Atomic<bool> obsolete { false }; - - const std::function<void(std::exception_ptr)> callback; }; } // namespace mbgl diff --git a/src/mbgl/tile/raster_tile_data.cpp b/src/mbgl/tile/raster_tile_data.cpp index 5e1948101b..4702f60f48 100644 --- a/src/mbgl/tile/raster_tile_data.cpp +++ b/src/mbgl/tile/raster_tile_data.cpp @@ -1,5 +1,6 @@ #include <mbgl/tile/raster_tile_data.hpp> #include <mbgl/style/source.hpp> +#include <mbgl/tile/tile_data_observer.hpp> #include <mbgl/storage/resource.hpp> #include <mbgl/storage/response.hpp> #include <mbgl/storage/file_source.hpp> @@ -10,12 +11,10 @@ using namespace mbgl; RasterTileData::RasterTileData(const OverscaledTileID& id_, gl::TexturePool& texturePool_, - Worker& worker_, - const std::function<void(std::exception_ptr)>& callback_) + Worker& worker_) : TileData(id_), texturePool(texturePool_), - worker(worker_), - callback(callback_) { + worker(worker_) { } void RasterTileData::setData(std::exception_ptr err, @@ -23,7 +22,7 @@ void RasterTileData::setData(std::exception_ptr err, optional<Timestamp> modified_, optional<Timestamp> expires_) { if (err) { - callback(err); + observer->onTileError(*this, err); return; } @@ -35,7 +34,7 @@ void RasterTileData::setData(std::exception_ptr err, workRequest.reset(); availableData = DataAvailability::All; bucket.reset(); - callback(err); + observer->onTileLoaded(*this, true); return; } @@ -43,17 +42,15 @@ void RasterTileData::setData(std::exception_ptr err, workRequest = worker.parseRasterTile(std::make_unique<RasterBucket>(texturePool), data, [this] (RasterTileParseResult result) { workRequest.reset(); - std::exception_ptr error; + availableData = DataAvailability::All; + if (result.is<std::unique_ptr<Bucket>>()) { bucket = std::move(result.get<std::unique_ptr<Bucket>>()); + observer->onTileLoaded(*this, true); } else { - error = result.get<std::exception_ptr>(); bucket.reset(); + observer->onTileError(*this, result.get<std::exception_ptr>()); } - - availableData = DataAvailability::All; - - callback(error); }); } diff --git a/src/mbgl/tile/raster_tile_data.hpp b/src/mbgl/tile/raster_tile_data.hpp index 9480292131..17e1465ebc 100644 --- a/src/mbgl/tile/raster_tile_data.hpp +++ b/src/mbgl/tile/raster_tile_data.hpp @@ -18,8 +18,7 @@ class RasterTileData : public TileData { public: RasterTileData(const OverscaledTileID&, gl::TexturePool&, - Worker&, - const std::function<void(std::exception_ptr)>& callback); + Worker&); ~RasterTileData(); void setData(std::exception_ptr err, @@ -39,8 +38,6 @@ private: // Contains the Bucket object for the tile. Buckets are render // objects and they get added by tile parsing operations. std::unique_ptr<Bucket> bucket; - - const std::function<void(std::exception_ptr)> callback; }; } // namespace mbgl diff --git a/src/mbgl/tile/tile_data.cpp b/src/mbgl/tile/tile_data.cpp index 34866c84ca..a5822b2c17 100644 --- a/src/mbgl/tile/tile_data.cpp +++ b/src/mbgl/tile/tile_data.cpp @@ -1,16 +1,23 @@ #include <mbgl/tile/tile_data.hpp> +#include <mbgl/tile/tile_data_observer.hpp> #include <mbgl/tile/tile_source.hpp> #include <mbgl/renderer/debug_bucket.hpp> #include <mbgl/util/string.hpp> namespace mbgl { +static TileDataObserver nullObserver; + TileData::TileData(const OverscaledTileID& id_) - : id(id_) { + : id(id_), observer(&nullObserver) { } TileData::~TileData() = default; +void TileData::setObserver(TileDataObserver* observer_) { + observer = observer_; +} + void TileData::setTileSource(std::unique_ptr<TileSource> tileSource_) { assert(!tileSource); assert(tileSource_); diff --git a/src/mbgl/tile/tile_data.hpp b/src/mbgl/tile/tile_data.hpp index c7b878879d..a0614455b6 100644 --- a/src/mbgl/tile/tile_data.hpp +++ b/src/mbgl/tile/tile_data.hpp @@ -20,6 +20,7 @@ class Worker; class DebugBucket; class TransformState; class TileSource; +class TileDataObserver; namespace style { class Layer; @@ -30,6 +31,7 @@ public: TileData(const OverscaledTileID&); virtual ~TileData(); + void setObserver(TileDataObserver* observer); void setTileSource(std::unique_ptr<TileSource>); // Mark this tile as no longer needed and cancel any pending work. @@ -38,8 +40,8 @@ public: virtual Bucket* getBucket(const style::Layer&) = 0; virtual bool parsePending() { return true; } - virtual void redoPlacement(PlacementConfig, const std::function<void()>&) {} - virtual void redoPlacement(const std::function<void()>&) {} + virtual void redoPlacement(PlacementConfig) {} + virtual void redoPlacement() {} virtual void queryRenderedFeatures( std::unordered_map<std::string, std::vector<Feature>>& result, @@ -88,6 +90,8 @@ protected: DataAvailability availableData = DataAvailability::None; std::unique_ptr<TileSource> tileSource; + + TileDataObserver* observer = nullptr; }; } // namespace mbgl diff --git a/src/mbgl/tile/tile_data_observer.hpp b/src/mbgl/tile/tile_data_observer.hpp new file mode 100644 index 0000000000..ce24609890 --- /dev/null +++ b/src/mbgl/tile/tile_data_observer.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <exception> + +namespace mbgl { + +class TileData; + +class TileDataObserver { +public: + virtual ~TileDataObserver() = default; + + virtual void onTileLoaded(TileData&, bool /*isNewTile*/) {} + virtual void onTileError(TileData&, std::exception_ptr) {} + virtual void onPlacementRedone(TileData&) {} +}; + +} // namespace mbgl |