From 7af016e52c0c7e7df1ae527d7177ba91dda27a85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Fri, 20 May 2016 16:12:22 +0200 Subject: [PATCH] [core] restructure TileSource construction and callbacks --- src/mbgl/style/source.cpp | 46 ++++---- src/mbgl/tile/annotation_tile_source.cpp | 18 ++- src/mbgl/tile/annotation_tile_source.hpp | 5 +- src/mbgl/tile/geojson_tile_source.cpp | 16 +-- src/mbgl/tile/geojson_tile_source.hpp | 5 +- src/mbgl/tile/geometry_tile_data.cpp | 136 +++++++++++------------ src/mbgl/tile/geometry_tile_data.hpp | 20 ++-- src/mbgl/tile/image_tile_source.cpp | 25 +++++ src/mbgl/tile/image_tile_source.hpp | 23 ++++ src/mbgl/tile/raster_tile_data.cpp | 71 ++++++------ src/mbgl/tile/raster_tile_data.hpp | 9 +- src/mbgl/tile/raster_tile_source.cpp | 25 ----- src/mbgl/tile/raster_tile_source.hpp | 36 ------ src/mbgl/tile/tile_data.cpp | 10 +- src/mbgl/tile/tile_data.hpp | 10 +- src/mbgl/tile/tile_source.cpp | 7 ++ src/mbgl/tile/tile_source.hpp | 40 ++++--- src/mbgl/tile/vector_tile_source.cpp | 21 ++-- src/mbgl/tile/vector_tile_source.hpp | 6 +- 19 files changed, 264 insertions(+), 265 deletions(-) create mode 100644 src/mbgl/tile/image_tile_source.cpp create mode 100644 src/mbgl/tile/image_tile_source.hpp delete mode 100644 src/mbgl/tile/raster_tile_source.cpp delete mode 100644 src/mbgl/tile/raster_tile_source.hpp diff --git a/src/mbgl/style/source.cpp b/src/mbgl/style/source.cpp index 1b5ae05f508..51e6334440b 100644 --- a/src/mbgl/style/source.cpp +++ b/src/mbgl/style/source.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include @@ -193,8 +193,7 @@ const std::map& Source::getTiles() const { std::unique_ptr Source::createTile(const OverscaledTileID& overscaledTileID, const UpdateParameters& parameters) { - std::unique_ptr data = cache.get(overscaledTileID); - if (data) { + if (auto data = cache.get(overscaledTileID)) { return data; } @@ -207,33 +206,40 @@ std::unique_ptr Source::createTile(const OverscaledTileID& overscaledT const auto resource = Resource::tile( tileset->tiles.at(0), parameters.pixelRatio, overscaledTileID.canonical.x, overscaledTileID.canonical.y, overscaledTileID.canonical.z); - auto monitor = std::make_unique(resource, parameters.fileSource); - data = - std::make_unique(overscaledTileID, std::move(monitor), - parameters.texturePool, parameters.worker, callback); + auto data = std::make_unique(overscaledTileID, parameters.texturePool, + parameters.worker, callback); + data->setTileSource( + std::make_unique(*data, resource, parameters.fileSource)); + + // Need a std::move here to create a std::unique_ptr from + // std::unique_ptr. + return std::move(data); } else { - std::unique_ptr monitor; - + auto data = std::make_unique(overscaledTileID, id, parameters.style, + parameters.mode, callback); if (type == SourceType::Vector) { assert(!tileset->tiles.empty()); const auto resource = Resource::tile( tileset->tiles.at(0), parameters.pixelRatio, overscaledTileID.canonical.x, overscaledTileID.canonical.y, overscaledTileID.canonical.z); - monitor = std::make_unique(resource, parameters.fileSource); + data->setTileSource( + std::make_unique(*data, resource, parameters.fileSource)); } else if (type == SourceType::Annotations) { - monitor = std::make_unique(overscaledTileID, parameters.annotationManager); + data->setTileSource(std::make_unique( + *data, overscaledTileID, parameters.annotationManager)); } else if (type == SourceType::GeoJSON) { - monitor = std::make_unique(geojsonvt.get(), overscaledTileID); + data->setTileSource( + std::make_unique(*data, geojsonvt.get(), overscaledTileID)); } else { - Log::Warning(Event::Style, "Source type '%s' is not implemented", SourceTypeClass(type).c_str()); + Log::Warning(Event::Style, "Source type '%s' is not implemented", + SourceTypeClass(type).c_str()); return nullptr; } - data = std::make_unique(overscaledTileID, std::move(monitor), id, - parameters.style, parameters.mode, callback); + // Need a std::move here to create a std::unique_ptr from + // std::unique_ptr. + return std::move(data); } - - return data; } TileData* Source::getTileData(const OverscaledTileID& overscaledTileID) const { @@ -322,13 +328,9 @@ bool Source::update(const UpdateParameters& parameters) { } for (auto& pair : tileDataMap) { - const auto& dataTileID = pair.first; auto tileData = pair.second.get(); if (parameters.shouldReparsePartialTiles && tileData->isIncomplete()) { - auto callback = std::bind(&Source::tileLoadingCallback, this, dataTileID, - std::placeholders::_1, false); - - if (!tileData->parsePending(callback)) { + if (!tileData->parsePending()) { allTilesUpdated = false; } } else { diff --git a/src/mbgl/tile/annotation_tile_source.cpp b/src/mbgl/tile/annotation_tile_source.cpp index a89b62f66f5..143bcc23f58 100644 --- a/src/mbgl/tile/annotation_tile_source.cpp +++ b/src/mbgl/tile/annotation_tile_source.cpp @@ -1,28 +1,24 @@ #include #include +#include #include #include namespace mbgl { -AnnotationTileSource::AnnotationTileSource(const OverscaledTileID& tileID_, - AnnotationManager& annotationManager_) - : tileID(tileID_), annotationManager(annotationManager_) { +AnnotationTileSource::AnnotationTileSource(GeometryTileData& tileData_, + const OverscaledTileID& tileID_, + AnnotationManager& annotationManager_) + : GeometryTileSource(tileData_), tileID(tileID_), annotationManager(annotationManager_) { + annotationManager.addTileSource(*this); } AnnotationTileSource::~AnnotationTileSource() { annotationManager.removeTileSource(*this); } -std::unique_ptr -AnnotationTileSource::monitorTile(const GeometryTileSource::Callback& callback_) { - callback = callback_; - annotationManager.addTileSource(*this); - return nullptr; -} - void AnnotationTileSource::update(std::unique_ptr tile) { - callback(nullptr, std::move(tile), {}, {}); + tileData.setData(nullptr, std::move(tile), {}, {}); } } // namespace mbgl diff --git a/src/mbgl/tile/annotation_tile_source.hpp b/src/mbgl/tile/annotation_tile_source.hpp index 8626791e1ae..8d7a1d8a1fe 100644 --- a/src/mbgl/tile/annotation_tile_source.hpp +++ b/src/mbgl/tile/annotation_tile_source.hpp @@ -8,20 +8,19 @@ namespace mbgl { class AnnotationManager; +class GeometryTile; class AnnotationTileSource : public GeometryTileSource { public: - AnnotationTileSource(const OverscaledTileID&, AnnotationManager&); + AnnotationTileSource(GeometryTileData&, const OverscaledTileID&, AnnotationManager&); ~AnnotationTileSource(); void update(std::unique_ptr); - std::unique_ptr monitorTile(const GeometryTileSource::Callback&) override; OverscaledTileID tileID; private: AnnotationManager& annotationManager; - GeometryTileSource::Callback callback; }; } // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile_source.cpp b/src/mbgl/tile/geojson_tile_source.cpp index 824df4bd490..3daf7cd02ee 100644 --- a/src/mbgl/tile/geojson_tile_source.cpp +++ b/src/mbgl/tile/geojson_tile_source.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -7,9 +8,11 @@ namespace mbgl { -GeoJSONTileSource::GeoJSONTileSource(mapbox::geojsonvt::GeoJSONVT* geojsonvt_, +GeoJSONTileSource::GeoJSONTileSource(GeometryTileData& tileData_, + mapbox::geojsonvt::GeoJSONVT* geojsonvt_, const OverscaledTileID& id) - : tileID(id), geojsonvt(geojsonvt_) { + : GeometryTileSource(tileData_), tileID(id), geojsonvt(geojsonvt_) { + update(); } GeoJSONTileSource::~GeoJSONTileSource() = default; @@ -89,15 +92,8 @@ void GeoJSONTileSource::update() { if (geojsonvt) { auto tile = convertTile( geojsonvt->getTile(tileID.canonical.z, tileID.canonical.x, tileID.canonical.y)); - callback(nullptr, std::move(tile), {}, {}); + tileData.setData(nullptr, std::move(tile), {}, {}); } } -std::unique_ptr -GeoJSONTileSource::monitorTile(const GeometryTileSource::Callback& cb) { - callback = cb; - update(); - return nullptr; -} - } // namespace mbgl diff --git a/src/mbgl/tile/geojson_tile_source.hpp b/src/mbgl/tile/geojson_tile_source.hpp index d37f3cf617a..bfa5f872525 100644 --- a/src/mbgl/tile/geojson_tile_source.hpp +++ b/src/mbgl/tile/geojson_tile_source.hpp @@ -16,11 +16,9 @@ namespace mbgl { class GeoJSONTileSource : public GeometryTileSource { public: - GeoJSONTileSource(mapbox::geojsonvt::GeoJSONVT*, const OverscaledTileID&); + GeoJSONTileSource(GeometryTileData&, mapbox::geojsonvt::GeoJSONVT*, const OverscaledTileID&); virtual ~GeoJSONTileSource(); - std::unique_ptr monitorTile(const GeometryTileSource::Callback&) override; - void setGeoJSONVT(mapbox::geojsonvt::GeoJSONVT*); private: @@ -31,7 +29,6 @@ class GeoJSONTileSource : public GeometryTileSource { private: mapbox::geojsonvt::GeoJSONVT* geojsonvt = nullptr; - GeometryTileSource::Callback callback; }; } // namespace mbgl diff --git a/src/mbgl/tile/geometry_tile_data.cpp b/src/mbgl/tile/geometry_tile_data.cpp index 8797976ed1c..9e813a86330 100644 --- a/src/mbgl/tile/geometry_tile_data.cpp +++ b/src/mbgl/tile/geometry_tile_data.cpp @@ -13,12 +13,11 @@ namespace mbgl { GeometryTileData::GeometryTileData(const OverscaledTileID& id_, - std::unique_ptr tileSource_, std::string sourceID, style::Style& style_, const MapMode mode_, - const std::function& callback) - : TileData(id_, std::move(tileSource_)), + const std::function& callback_) + : TileData(id_), style(style_), worker(style_.workers), tileWorker(id_, @@ -27,69 +26,68 @@ GeometryTileData::GeometryTileData(const OverscaledTileID& id_, *style_.glyphAtlas, *style_.glyphStore, obsolete, - mode_) { - auto geometryTileSource = reinterpret_cast(tileSource.get()); - tileRequest = geometryTileSource->monitorTile([callback, this](std::exception_ptr err, - std::unique_ptr tile, - optional modified_, - optional expires_) { - if (err) { - callback(err); - return; - } + mode_), + callback(callback_) { +} - modified = modified_; - expires = expires_; +void GeometryTileData::setData(std::exception_ptr err, + std::unique_ptr tile, + optional modified_, + optional expires_) { + if (err) { + callback(err); + return; + } - if (!tile) { - // This is a 404 response. We're treating these as empty tiles. - workRequest.reset(); - availableData = DataAvailability::All; - buckets.clear(); - callback(err); - return; - } + modified = modified_; + expires = expires_; - // Mark the tile as pending again if it was complete before to prevent signaling a complete - // state despite pending parse operations. - if (availableData == DataAvailability::All) { - availableData = DataAvailability::Some; - } + if (!tile) { + // This is a 404 response. We're treating these as empty tiles. + workRequest.reset(); + availableData = DataAvailability::All; + buckets.clear(); + callback(err); + return; + } - // Kick off a fresh parse of this tile. This happens when the tile is new, or - // when tile data changed. Replacing the workdRequest will cancel a pending work - // request in case there is one. + // Mark the tile as pending again if it was complete before to prevent signaling a complete + // state despite pending parse operations. + if (availableData == DataAvailability::All) { + availableData = DataAvailability::Some; + } + + // Kick off a fresh parse of this tile. This happens when the tile is new, or + // when tile data changed. Replacing the workdRequest will cancel a pending work + // request in case there is one. + workRequest.reset(); + workRequest = worker.parseGeometryTile(tileWorker, style.getLayers(), std::move(tile), targetConfig, [this, config = targetConfig] (TileParseResult result) { workRequest.reset(); - workRequest = worker.parseGeometryTile(tileWorker, style.getLayers(), std::move(tile), targetConfig, [callback, this, config = targetConfig] (TileParseResult result) { - workRequest.reset(); - - std::exception_ptr error; - if (result.is()) { - auto& resultBuckets = result.get(); - availableData = - resultBuckets.complete ? DataAvailability::All : DataAvailability::Some; - - // Persist the configuration we just placed so that we can later check whether we need to - // place again in case the configuration has changed. - placedConfig = config; - - // Move over all buckets we received in this parse request, potentially overwriting - // existing buckets in case we got a refresh parse. - buckets = std::move(resultBuckets.buckets); - - if (isComplete()) { - featureIndex = std::move(resultBuckets.featureIndex); - geometryTile = std::move(resultBuckets.geometryTile); - } - - } else { - // This is triggered when parsing fails (e.g. due to an invalid vector tile) - error = result.get(); - availableData = DataAvailability::All; + + std::exception_ptr error; + if (result.is()) { + auto& resultBuckets = result.get(); + availableData = resultBuckets.complete ? DataAvailability::All : DataAvailability::Some; + + // Persist the configuration we just placed so that we can later check whether we need to + // place again in case the configuration has changed. + placedConfig = config; + + // Move over all buckets we received in this parse request, potentially overwriting + // existing buckets in case we got a refresh parse. + buckets = std::move(resultBuckets.buckets); + + if (isComplete()) { + featureIndex = std::move(resultBuckets.featureIndex); + geometryTile = std::move(resultBuckets.geometryTile); } - callback(error); - }); + } else { + error = result.get(); + availableData = DataAvailability::All; + } + + callback(error); }); } @@ -97,21 +95,20 @@ GeometryTileData::~GeometryTileData() { cancel(); } -bool GeometryTileData::parsePending(std::function callback) { +bool GeometryTileData::parsePending() { if (workRequest) { // There's already parsing or placement going on. return false; } workRequest.reset(); - workRequest = worker.parsePendingGeometryTileLayers(tileWorker, targetConfig, [this, callback, config = targetConfig] (TileParseResult result) { + workRequest = worker.parsePendingGeometryTileLayers(tileWorker, targetConfig, [this, config = targetConfig] (TileParseResult result) { workRequest.reset(); std::exception_ptr error; if (result.is()) { auto& resultBuckets = result.get(); - availableData = - resultBuckets.complete ? DataAvailability::All : DataAvailability::Some; + availableData = resultBuckets.complete ? DataAvailability::All : DataAvailability::Some; // Move over all buckets we received in this parse request, potentially overwriting // existing buckets in case we got a refresh parse. @@ -149,20 +146,20 @@ Bucket* GeometryTileData::getBucket(const style::Layer& layer) { return it->second.get(); } -void GeometryTileData::redoPlacement(const PlacementConfig newConfig, const std::function& callback) { +void GeometryTileData::redoPlacement(const PlacementConfig newConfig, const std::function& cb) { if (newConfig != placedConfig) { targetConfig = newConfig; - redoPlacement(callback); + redoPlacement(cb); } } -void GeometryTileData::redoPlacement(const std::function& callback) { +void GeometryTileData::redoPlacement(const std::function& cb) { // 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, callback, config = targetConfig](std::unique_ptr collisionTile) { + workRequest = worker.redoPlacement(tileWorker, buckets, targetConfig, [this, cb, config = targetConfig](std::unique_ptr collisionTile) { workRequest.reset(); // Persist the configuration we just placed so that we can later check whether we need to @@ -180,9 +177,9 @@ void GeometryTileData::redoPlacement(const std::function& callback) { // The target configuration could have changed since we started placement. In this case, // we're starting another placement run. if (placedConfig != targetConfig) { - redoPlacement(callback); + redoPlacement(cb); } else { - callback(); + cb(); } }); } @@ -208,7 +205,6 @@ void GeometryTileData::queryRenderedFeatures( void GeometryTileData::cancel() { obsolete = true; - tileRequest.reset(); workRequest.reset(); } diff --git a/src/mbgl/tile/geometry_tile_data.hpp b/src/mbgl/tile/geometry_tile_data.hpp index 5203c8f145d..3bbabcdff0a 100644 --- a/src/mbgl/tile/geometry_tile_data.hpp +++ b/src/mbgl/tile/geometry_tile_data.hpp @@ -12,6 +12,7 @@ namespace mbgl { class AsyncRequest; +class GeometryTile; class GeometryTileSource; class FeatureIndex; @@ -22,17 +23,21 @@ class Style; class GeometryTileData : public TileData { public: GeometryTileData(const OverscaledTileID&, - std::unique_ptr tileSource, - std::string sourceID, - style::Style&, - const MapMode, - const std::function& callback); + std::string sourceID, + style::Style&, + const MapMode, + const std::function& callback); ~GeometryTileData(); + void setData(std::exception_ptr err, + std::unique_ptr tile, + optional modified_, + optional expires_); + Bucket* getBucket(const style::Layer&) override; - bool parsePending(std::function callback) override; + bool parsePending() override; void redoPlacement(PlacementConfig config, const std::function&) override; void redoPlacement(const std::function&) override; @@ -50,7 +55,6 @@ class GeometryTileData : public TileData { Worker& worker; TileWorker tileWorker; - std::unique_ptr tileRequest; std::unique_ptr workRequest; // Contains all the Bucket objects for the tile. Buckets are render @@ -69,6 +73,8 @@ class GeometryTileData : public TileData { // Used to signal the worker that it should abandon parsing this tile as soon as possible. std::atomic obsolete { false }; + + const std::function callback; }; } // namespace mbgl diff --git a/src/mbgl/tile/image_tile_source.cpp b/src/mbgl/tile/image_tile_source.cpp new file mode 100644 index 00000000000..e552c0e3159 --- /dev/null +++ b/src/mbgl/tile/image_tile_source.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +namespace mbgl { + +ImageTileSource::ImageTileSource(RasterTileData& tileData_, + const Resource& resource_, + FileSource& fileSource_) + : RasterTileSource(tileData_), resource(resource_), fileSource(fileSource_) { + request = fileSource.request(resource, [this](Response res) { + if (res.error) { + tileData.setData(std::make_exception_ptr(std::runtime_error(res.error->message)), + nullptr, res.modified, res.expires); + } else if (res.notModified) { + return; + } else if (res.noContent) { + tileData.setData(nullptr, nullptr, res.modified, res.expires); + } else { + tileData.setData(nullptr, res.data, res.modified, res.expires); + } + }); +} + +} // namespace mbgl diff --git a/src/mbgl/tile/image_tile_source.hpp b/src/mbgl/tile/image_tile_source.hpp new file mode 100644 index 00000000000..8b4e9c0980e --- /dev/null +++ b/src/mbgl/tile/image_tile_source.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include + +namespace mbgl { + +class FileSource; +class AsyncRequest; + +class ImageTileSource : public RasterTileSource { +public: + ImageTileSource(RasterTileData&, const Resource&, FileSource&); + virtual ~ImageTileSource() = default; + +private: + const Resource resource; + FileSource& fileSource; + std::unique_ptr request; +}; + +} // namespace mbgl diff --git a/src/mbgl/tile/raster_tile_data.cpp b/src/mbgl/tile/raster_tile_data.cpp index 1a5b78d7ff0..2dcc78e4081 100644 --- a/src/mbgl/tile/raster_tile_data.cpp +++ b/src/mbgl/tile/raster_tile_data.cpp @@ -10,51 +10,51 @@ using namespace mbgl; RasterTileData::RasterTileData(const OverscaledTileID& id_, - std::unique_ptr tileSource_, gl::TexturePool& texturePool_, Worker& worker_, - const std::function& callback) - : TileData(id_, std::move(tileSource_)), + const std::function& callback_) + : TileData(id_), texturePool(texturePool_), - worker(worker_) { - auto rasterTileSource = reinterpret_cast(tileSource.get()); - tileRequest = rasterTileSource->monitorTile([callback, this](std::exception_ptr err, - std::shared_ptr data, - optional modified_, - optional expires_) { - if (err) { - callback(err); - return; - } + worker(worker_), + callback(callback_) { +} - modified = modified_; - expires = expires_; +void RasterTileData::setData(std::exception_ptr err, + std::shared_ptr data, + optional modified_, + optional expires_) { + if (err) { + callback(err); + return; + } - if (!data) { - // This is a 404 response. We're treating these as empty tiles. - workRequest.reset(); - availableData = DataAvailability::All; - bucket.reset(); - callback(err); - return; - } + modified = modified_; + expires = expires_; + if (!data) { + // This is a 404 response. We're treating these as empty tiles. workRequest.reset(); - workRequest = worker.parseRasterTile(std::make_unique(texturePool), data, [this, callback] (RasterTileParseResult result) { - workRequest.reset(); + availableData = DataAvailability::All; + bucket.reset(); + callback(err); + return; + } - std::exception_ptr error; - if (result.is>()) { - bucket = std::move(result.get>()); - } else { - error = result.get(); - bucket.reset(); - } + workRequest.reset(); + workRequest = worker.parseRasterTile(std::make_unique(texturePool), data, [this] (RasterTileParseResult result) { + workRequest.reset(); + + std::exception_ptr error; + if (result.is>()) { + bucket = std::move(result.get>()); + } else { + error = result.get(); + bucket.reset(); + } - availableData = DataAvailability::All; + availableData = DataAvailability::All; - callback(error); - }); + callback(error); }); } @@ -67,6 +67,5 @@ Bucket* RasterTileData::getBucket(const style::Layer&) { } void RasterTileData::cancel() { - tileRequest.reset(); workRequest.reset(); } diff --git a/src/mbgl/tile/raster_tile_data.hpp b/src/mbgl/tile/raster_tile_data.hpp index 5c522e6c752..94802921315 100644 --- a/src/mbgl/tile/raster_tile_data.hpp +++ b/src/mbgl/tile/raster_tile_data.hpp @@ -17,12 +17,16 @@ class Layer; class RasterTileData : public TileData { public: RasterTileData(const OverscaledTileID&, - std::unique_ptr, gl::TexturePool&, Worker&, const std::function& callback); ~RasterTileData(); + void setData(std::exception_ptr err, + std::shared_ptr data, + optional modified_, + optional expires_); + void cancel() override; Bucket* getBucket(const style::Layer&) override; @@ -30,12 +34,13 @@ class RasterTileData : public TileData { gl::TexturePool& texturePool; Worker& worker; - std::unique_ptr tileRequest; std::unique_ptr workRequest; // Contains the Bucket object for the tile. Buckets are render // objects and they get added by tile parsing operations. std::unique_ptr bucket; + + const std::function callback; }; } // namespace mbgl diff --git a/src/mbgl/tile/raster_tile_source.cpp b/src/mbgl/tile/raster_tile_source.cpp deleted file mode 100644 index 8d07bad07a6..00000000000 --- a/src/mbgl/tile/raster_tile_source.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include - -namespace mbgl { - -RasterTileSource::RasterTileSource(const Resource& resource_, FileSource& fileSource_) - : resource(resource_), fileSource(fileSource_) { -} - -std::unique_ptr RasterTileSource::monitorTile(const Callback& callback) { - return fileSource.request(resource, [callback, this](Response res) { - if (res.error) { - callback(std::make_exception_ptr(std::runtime_error(res.error->message)), nullptr, - res.modified, res.expires); - } else if (res.notModified) { - return; - } else if (res.noContent) { - callback(nullptr, nullptr, res.modified, res.expires); - } else { - callback(nullptr, res.data, res.modified, res.expires); - } - }); -} - -} // namespace mbgl diff --git a/src/mbgl/tile/raster_tile_source.hpp b/src/mbgl/tile/raster_tile_source.hpp deleted file mode 100644 index 800f31fefbf..00000000000 --- a/src/mbgl/tile/raster_tile_source.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include -#include -#include - -namespace mbgl { - -class FileSource; - -class RasterTileSource : public TileSource { -public: - RasterTileSource(const Resource&, FileSource&); - virtual ~RasterTileSource() = default; - - using Callback = std::function, - optional modified, - optional expires)>; - - /* - * Monitor the tile held by this object for changes. When the tile is loaded for the first time, - * or updates, the callback is executed. If an error occurs, the first parameter will be set. - * Otherwise it will be null. If there is no data for the requested tile, the second parameter - * will be null. - * - * To cease monitoring, release the returned Request. - */ - std::unique_ptr monitorTile(const Callback&); - -private: - const Resource resource; - FileSource& fileSource; -}; - -} // namespace mbgl diff --git a/src/mbgl/tile/tile_data.cpp b/src/mbgl/tile/tile_data.cpp index 9214bf98eea..34866c84cac 100644 --- a/src/mbgl/tile/tile_data.cpp +++ b/src/mbgl/tile/tile_data.cpp @@ -5,12 +5,18 @@ namespace mbgl { -TileData::TileData(const OverscaledTileID& id_, std::unique_ptr tileSource_) - : id(id_), tileSource(std::move(tileSource_)) { +TileData::TileData(const OverscaledTileID& id_) + : id(id_) { } TileData::~TileData() = default; +void TileData::setTileSource(std::unique_ptr tileSource_) { + assert(!tileSource); + assert(tileSource_); + tileSource = std::move(tileSource_); +} + void TileData::dumpDebugLogs() const { Log::Info(Event::General, "TileData::id: %s", util::toString(id).c_str()); Log::Info(Event::General, "TileData::renderable: %s", isRenderable() ? "yes" : "no"); diff --git a/src/mbgl/tile/tile_data.hpp b/src/mbgl/tile/tile_data.hpp index d5b178fd248..c7b878879df 100644 --- a/src/mbgl/tile/tile_data.hpp +++ b/src/mbgl/tile/tile_data.hpp @@ -27,15 +27,17 @@ class Layer; class TileData : private util::noncopyable { public: - TileData(const OverscaledTileID&, std::unique_ptr); + TileData(const OverscaledTileID&); virtual ~TileData(); + void setTileSource(std::unique_ptr); + // Mark this tile as no longer needed and cancel any pending work. virtual void cancel() = 0; virtual Bucket* getBucket(const style::Layer&) = 0; - virtual bool parsePending(std::function) { return true; } + virtual bool parsePending() { return true; } virtual void redoPlacement(PlacementConfig, const std::function&) {} virtual void redoPlacement(const std::function&) {} @@ -59,12 +61,12 @@ class TileData : private util::noncopyable { return availableData == DataAvailability::Some; } + void dumpDebugLogs() const; const OverscaledTileID id; optional modified; optional expires; - const std::unique_ptr tileSource; // Contains the tile ID string for painting debug information. std::unique_ptr debugBucket; @@ -84,6 +86,8 @@ class TileData : private util::noncopyable { }; DataAvailability availableData = DataAvailability::None; + + std::unique_ptr tileSource; }; } // namespace mbgl diff --git a/src/mbgl/tile/tile_source.cpp b/src/mbgl/tile/tile_source.cpp index e69de29bb2d..c45da0c779b 100644 --- a/src/mbgl/tile/tile_source.cpp +++ b/src/mbgl/tile/tile_source.cpp @@ -0,0 +1,7 @@ +#include + +#include + +namespace mbgl { + +} // namespace mbgl diff --git a/src/mbgl/tile/tile_source.hpp b/src/mbgl/tile/tile_source.hpp index 52601e9b5e1..294ddd0d6b9 100644 --- a/src/mbgl/tile/tile_source.hpp +++ b/src/mbgl/tile/tile_source.hpp @@ -1,39 +1,37 @@ #pragma once #include -#include -#include - -#include -#include namespace mbgl { -class GeometryTile; -class AsyncRequest; - class TileSource : private util::noncopyable { public: virtual ~TileSource() = default; }; +class GeometryTileData; +class RasterTileData; + class GeometryTileSource : public TileSource { +protected: + GeometryTileSource(GeometryTileData& tileData_) : tileData(tileData_) {} + public: virtual ~GeometryTileSource() = default; - using Callback = std::function, - optional modified, - optional expires)>; - /* - * Monitor the tile held by this object for changes. When the tile is loaded for the first time, - * or updates, the callback is executed. If an error occurs, the first parameter will be set. - * Otherwise it will be null. If there is no data for the requested tile, the second parameter - * will be null. - * - * To cease monitoring, release the returned Request. - */ - virtual std::unique_ptr monitorTile(const Callback&) = 0; +protected: + GeometryTileData& tileData; +}; + +class RasterTileSource : public TileSource { +protected: + RasterTileSource(RasterTileData& tileData_) : tileData(tileData_) {}; + +public: + virtual ~RasterTileSource() = default; + +protected: + RasterTileData& tileData; }; } // namespace mbgl diff --git a/src/mbgl/tile/vector_tile_source.cpp b/src/mbgl/tile/vector_tile_source.cpp index cfff47f5983..2ee0827b149 100644 --- a/src/mbgl/tile/vector_tile_source.cpp +++ b/src/mbgl/tile/vector_tile_source.cpp @@ -1,24 +1,25 @@ #include #include +#include #include namespace mbgl { -VectorTileSource::VectorTileSource(const Resource& resource_, FileSource& fileSource_) - : resource(resource_), fileSource(fileSource_) { -} - -std::unique_ptr VectorTileSource::monitorTile(const Callback& callback) { - return fileSource.request(resource, [callback, this](Response res) { +VectorTileSource::VectorTileSource(GeometryTileData& tileData_, + const Resource& resource_, + FileSource& fileSource_) + : GeometryTileSource(tileData_), resource(resource_), fileSource(fileSource_) { + request = fileSource.request(resource, [this](Response res) { if (res.error) { - callback(std::make_exception_ptr(std::runtime_error(res.error->message)), nullptr, - res.modified, res.expires); + tileData.setData(std::make_exception_ptr(std::runtime_error(res.error->message)), + nullptr, res.modified, res.expires); } else if (res.notModified) { return; } else if (res.noContent) { - callback(nullptr, nullptr, res.modified, res.expires); + tileData.setData(nullptr, nullptr, res.modified, res.expires); } else { - callback(nullptr, std::make_unique(res.data), res.modified, res.expires); + tileData.setData(nullptr, std::make_unique(res.data), res.modified, + res.expires); } }); } diff --git a/src/mbgl/tile/vector_tile_source.hpp b/src/mbgl/tile/vector_tile_source.hpp index 32f45a1a23a..fb64c7512ed 100644 --- a/src/mbgl/tile/vector_tile_source.hpp +++ b/src/mbgl/tile/vector_tile_source.hpp @@ -7,17 +7,17 @@ namespace mbgl { class FileSource; +class AsyncRequest; class VectorTileSource : public GeometryTileSource { public: - VectorTileSource(const Resource&, FileSource&); + VectorTileSource(GeometryTileData&, const Resource&, FileSource&); virtual ~VectorTileSource() = default; - std::unique_ptr monitorTile(const Callback&) override; - private: const Resource resource; FileSource& fileSource; + std::unique_ptr request; }; } // namespace mbgl