Make request and reparse pure virtual

This commit is contained in:
John Firebaugh
2015-06-04 15:54:42 -07:00
parent ee46378583
commit 7e71145a89
6 changed files with 101 additions and 51 deletions

View File

@@ -1,5 +1,12 @@
#include <mbgl/map/raster_tile_data.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/worker.hpp>
#include <mbgl/util/work_request.hpp>
#include <sstream>
using namespace mbgl;
@@ -14,6 +21,41 @@ RasterTileData::~RasterTileData() {
cancel();
}
void RasterTileData::request(Worker& worker,
float pixelRatio,
const std::function<void()>& 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<void()> callback) {
if (!mayStartParsing()) {
return false;
}
workRequest = worker.send([this] { parse(); endParsing(); }, callback);
return true;
}
void RasterTileData::parse() {
if (getState() != State::loaded) {
return;

View File

@@ -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<void()>& callback) override;
bool reparse(Worker&, std::function<void ()> callback) override;
void parse();
Bucket* getBucket(StyleLayer const &layer_desc) override;
protected:

View File

@@ -1,13 +1,6 @@
#include <mbgl/map/tile_data.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/work_request.hpp>
#include <mbgl/util/worker.hpp>
#include <sstream>
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<void()>& 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<void()> 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);

View File

@@ -77,13 +77,16 @@ public:
TileData(const TileID&, const SourceInfo&);
~TileData();
void request(Worker&, float pixelRatio, const std::function<void()>& callback);
virtual void request(Worker&,
float pixelRatio,
const std::function<void()>& 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<void ()> callback);
virtual bool reparse(Worker&,
std::function<void ()> 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> workRequest;
private:
std::atomic<State> state;
private:
std::string error;
protected:

View File

@@ -7,6 +7,9 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/text/collision_tile.hpp>
#include <mbgl/util/pbf.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/worker.hpp>
#include <mbgl/util/work_request.hpp>
#include <mbgl/style/style.hpp>
@@ -32,6 +35,41 @@ VectorTileData::~VectorTileData() {
style.glyphAtlas->removeGlyphs(reinterpret_cast<uintptr_t>(this));
}
void VectorTileData::request(Worker& worker,
float pixelRatio,
const std::function<void()>& 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<void()> 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;

View File

@@ -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<void()>& callback) override;
bool reparse(Worker&, std::function<void ()> callback) override;
protected:
void redoPlacement();