diff --git a/src/mbgl/map/raster_tile_data.cpp b/src/mbgl/map/raster_tile_data.cpp index 0f8efe3b4..ee53a6a28 100644 --- a/src/mbgl/map/raster_tile_data.cpp +++ b/src/mbgl/map/raster_tile_data.cpp @@ -1,5 +1,12 @@ #include -#include +#include +#include +#include +#include +#include +#include + +#include using namespace mbgl; @@ -14,6 +21,41 @@ RasterTileData::~RasterTileData() { cancel(); } +void RasterTileData::request(Worker& worker, + float pixelRatio, + const std::function& callback) { + std::string url = source.tileURL(id, pixelRatio); + state = State::loading; + + FileSource* fs = util::ThreadContext::getFileSource(); + req = fs->request({ Resource::Kind::Tile, url }, util::RunLoop::current.get()->get(), [url, callback, &worker, this](const Response &res) { + req = nullptr; + + if (res.status != Response::Successful) { + std::stringstream message; + message << "Failed to load [" << url << "]: " << res.message; + setError(message.str()); + callback(); + return; + } + + state = State::loaded; + data = res.data; + + // Schedule tile parsing in another thread + reparse(worker, callback); + }); +} + +bool RasterTileData::reparse(Worker& worker, std::function callback) { + if (!mayStartParsing()) { + return false; + } + + workRequest = worker.send([this] { parse(); endParsing(); }, callback); + return true; +} + void RasterTileData::parse() { if (getState() != State::loaded) { return; diff --git a/src/mbgl/map/raster_tile_data.hpp b/src/mbgl/map/raster_tile_data.hpp index 45aee8f74..fb59482ef 100644 --- a/src/mbgl/map/raster_tile_data.hpp +++ b/src/mbgl/map/raster_tile_data.hpp @@ -13,13 +13,17 @@ class StyleLayer; class TexturePool; class RasterTileData : public TileData { - friend class TileParser; - public: RasterTileData(const TileID&, TexturePool&, const SourceInfo&); ~RasterTileData(); - void parse() override; + void request(Worker&, + float pixelRatio, + const std::function& callback) override; + + bool reparse(Worker&, std::function callback) override; + + void parse(); Bucket* getBucket(StyleLayer const &layer_desc) override; protected: diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp index f1c661300..4a30639cb 100644 --- a/src/mbgl/map/tile_data.cpp +++ b/src/mbgl/map/tile_data.cpp @@ -1,13 +1,6 @@ #include - -#include -#include -#include #include #include -#include - -#include using namespace mbgl; @@ -35,32 +28,6 @@ void TileData::setState(const State& state_) { state = state_; } -void TileData::request(Worker& worker, - float pixelRatio, - const std::function& callback) { - std::string url = source.tileURL(id, pixelRatio); - state = State::loading; - - FileSource* fs = util::ThreadContext::getFileSource(); - req = fs->request({ Resource::Kind::Tile, url }, util::RunLoop::current.get()->get(), [url, callback, &worker, this](const Response &res) { - req = nullptr; - - if (res.status != Response::Successful) { - std::stringstream message; - message << "Failed to load [" << url << "]: " << res.message; - setError(message.str()); - callback(); - return; - } - - state = State::loaded; - data = res.data; - - // Schedule tile parsing in another thread - reparse(worker, callback); - }); -} - void TileData::cancel() { if (state != State::obsolete) { state = State::obsolete; @@ -80,15 +47,6 @@ void TileData::endParsing() { parsing.clear(std::memory_order_release); } -bool TileData::reparse(Worker& worker, std::function callback) { - if (!mayStartParsing()) { - return false; - } - - workRequest = worker.send([this] { parse(); endParsing(); }, callback); - return true; -} - void TileData::setError(const std::string& message) { error = message; setState(State::obsolete); diff --git a/src/mbgl/map/tile_data.hpp b/src/mbgl/map/tile_data.hpp index aa302a4fa..a3aaf1dfd 100644 --- a/src/mbgl/map/tile_data.hpp +++ b/src/mbgl/map/tile_data.hpp @@ -77,13 +77,16 @@ public: TileData(const TileID&, const SourceInfo&); ~TileData(); - void request(Worker&, float pixelRatio, const std::function& callback); + virtual void request(Worker&, + float pixelRatio, + const std::function& callback) = 0; // 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); + virtual bool reparse(Worker&, + std::function callback) = 0; void cancel(); const std::string toString() const; @@ -116,7 +119,6 @@ public: } // Override this in the child class. - virtual void parse() = 0; virtual Bucket* getBucket(StyleLayer const &layer_desc) = 0; virtual void redoPlacement(float, bool) {} @@ -141,9 +143,9 @@ protected: std::unique_ptr workRequest; -private: std::atomic state; +private: std::string error; protected: diff --git a/src/mbgl/map/vector_tile_data.cpp b/src/mbgl/map/vector_tile_data.cpp index 840e21b8a..34cfe4311 100644 --- a/src/mbgl/map/vector_tile_data.cpp +++ b/src/mbgl/map/vector_tile_data.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -32,6 +35,41 @@ VectorTileData::~VectorTileData() { style.glyphAtlas->removeGlyphs(reinterpret_cast(this)); } +void VectorTileData::request(Worker& worker, + float pixelRatio, + const std::function& callback) { + std::string url = source.tileURL(id, pixelRatio); + state = State::loading; + + FileSource* fs = util::ThreadContext::getFileSource(); + req = fs->request({ Resource::Kind::Tile, url }, util::RunLoop::current.get()->get(), [url, callback, &worker, this](const Response &res) { + req = nullptr; + + if (res.status != Response::Successful) { + std::stringstream message; + message << "Failed to load [" << url << "]: " << res.message; + setError(message.str()); + callback(); + return; + } + + state = State::loaded; + data = res.data; + + // Schedule tile parsing in another thread + reparse(worker, callback); + }); +} + +bool VectorTileData::reparse(Worker& worker, std::function callback) { + if (!mayStartParsing()) { + return false; + } + + workRequest = worker.send([this] { parse(); endParsing(); }, callback); + return true; +} + void VectorTileData::parse() { if (getState() != State::loaded && getState() != State::partial) { return; diff --git a/src/mbgl/map/vector_tile_data.hpp b/src/mbgl/map/vector_tile_data.hpp index 697a054e0..faddb1dc7 100644 --- a/src/mbgl/map/vector_tile_data.hpp +++ b/src/mbgl/map/vector_tile_data.hpp @@ -35,7 +35,7 @@ public: bool collisionDebug_); ~VectorTileData(); - void parse() override; + virtual void parse(); void redoPlacement(float angle, bool collisionDebug) override; virtual Bucket* getBucket(StyleLayer const &layer_desc) override; @@ -48,6 +48,12 @@ public: return collision.get(); } + void request(Worker&, + float pixelRatio, + const std::function& callback) override; + + bool reparse(Worker&, std::function callback) override; + protected: void redoPlacement();