[core] Remove ThreadContext::getFileSource; instead thread FileSource through

This commit is contained in:
John Firebaugh
2016-02-18 10:48:51 -08:00
parent d5eb5240f8
commit 7b39ce9521
23 changed files with 79 additions and 80 deletions

View File

@@ -28,8 +28,9 @@
namespace mbgl {
MapContext::MapContext(View& view_, FileSource& fileSource, MapMode mode_, GLContextMode contextMode_, const float pixelRatio_)
MapContext::MapContext(View& view_, FileSource& fileSource_, MapMode mode_, GLContextMode contextMode_, const float pixelRatio_)
: view(view_),
fileSource(fileSource_),
dataPtr(std::make_unique<MapData>(mode_, contextMode_, pixelRatio_)),
data(*dataPtr),
asyncUpdate([this] { update(); }),
@@ -37,7 +38,6 @@ MapContext::MapContext(View& view_, FileSource& fileSource, MapMode mode_, GLCon
texturePool(std::make_unique<gl::TexturePool>()) {
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
util::ThreadContext::setFileSource(&fileSource);
util::ThreadContext::setGLObjectStore(&glObjectStore);
view.activate();
@@ -96,7 +96,7 @@ void MapContext::setStyleURL(const std::string& url) {
styleURL = url;
styleJSON.clear();
style = std::make_unique<Style>(data);
style = std::make_unique<Style>(data, fileSource);
const size_t pos = styleURL.rfind('/');
std::string base = "";
@@ -104,8 +104,7 @@ void MapContext::setStyleURL(const std::string& url) {
base = styleURL.substr(0, pos + 1);
}
FileSource* fs = util::ThreadContext::getFileSource();
styleRequest = fs->request(Resource::style(styleURL), [this, base](Response res) {
styleRequest = fileSource.request(Resource::style(styleURL), [this, base](Response res) {
if (res.error) {
if (res.error->reason == Response::Error::Reason::NotFound &&
util::mapbox::isMapboxURL(styleURL)) {
@@ -130,7 +129,7 @@ void MapContext::setStyleJSON(const std::string& json, const std::string& base)
styleURL.clear();
styleJSON.clear();
style = std::make_unique<Style>(data);
style = std::make_unique<Style>(data, fileSource);
loadStyleJSON(json, base);
}

View File

@@ -78,6 +78,7 @@ private:
void loadStyleJSON(const std::string& json, const std::string& base);
View& view;
FileSource& fileSource;
std::unique_ptr<MapData> dataPtr;
MapData& data;

View File

@@ -68,10 +68,7 @@ bool Source::isLoading() const {
return !loaded && req.operator bool();
}
// Note: This is a separate function that must be called exactly once after creation
// The reason this isn't part of the constructor is that calling shared_from_this() in
// the constructor fails.
void Source::load() {
void Source::load(FileSource& fileSource) {
if (url.empty()) {
// In case there is no URL set, we assume that we already have all of the data because the
// TileJSON was specified inline in the stylesheet.
@@ -86,8 +83,7 @@ void Source::load() {
}
// URL may either be a TileJSON file, or a GeoJSON file.
FileSource* fs = util::ThreadContext::getFileSource();
req = fs->request(Resource::source(url), [this](Response res) {
req = fileSource.request(Resource::source(url), [this](Response res) {
if (res.error) {
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message)));
} else if (res.notModified) {
@@ -259,12 +255,13 @@ TileData::State Source::addTile(const TileID& tileID, const StyleUpdateParameter
info->tiles.at(0),
parameters.texturePool,
parameters.worker,
parameters.fileSource,
callback);
} else {
std::unique_ptr<GeometryTileMonitor> monitor;
if (type == SourceType::Vector) {
monitor = std::make_unique<VectorTileMonitor>(normalizedID, parameters.pixelRatio, info->tiles.at(0));
monitor = std::make_unique<VectorTileMonitor>(normalizedID, parameters.pixelRatio, info->tiles.at(0), parameters.fileSource);
} else if (type == SourceType::Annotations) {
monitor = std::make_unique<AnnotationTileMonitor>(normalizedID, parameters.data);
} else if (type == SourceType::GeoJSON) {

View File

@@ -22,6 +22,7 @@ namespace mbgl {
class StyleUpdateParameters;
class Painter;
class FileSource;
class FileRequest;
class TransformState;
class Tile;
@@ -51,7 +52,7 @@ public:
~Source();
bool loaded = false;
void load();
void load(FileSource&);
bool isLoading() const;
bool isLoaded() const;

View File

@@ -5,7 +5,6 @@
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/thread_context.hpp>
#include <cassert>
#include <string>
@@ -25,7 +24,7 @@ SpriteStore::SpriteStore(float pixelRatio_)
SpriteStore::~SpriteStore() = default;
void SpriteStore::setURL(const std::string& url) {
void SpriteStore::load(const std::string& url, FileSource& fileSource) {
if (url.empty()) {
// Treat a non-existent sprite as a successfully loaded empty sprite.
loaded = true;
@@ -34,8 +33,7 @@ void SpriteStore::setURL(const std::string& url) {
loader = std::make_unique<Loader>();
FileSource* fs = util::ThreadContext::getFileSource();
loader->jsonRequest = fs->request(Resource::spriteJSON(url, pixelRatio), [this](Response res) {
loader->jsonRequest = fileSource.request(Resource::spriteJSON(url, pixelRatio), [this](Response res) {
if (res.error) {
observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message)));
} else if (res.notModified) {
@@ -50,7 +48,7 @@ void SpriteStore::setURL(const std::string& url) {
}
});
loader->spriteRequest = fs->request(Resource::spriteImage(url, pixelRatio), [this](Response res) {
loader->spriteRequest = fileSource.request(Resource::spriteImage(url, pixelRatio), [this](Response res) {
if (res.error) {
observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message)));
} else if (res.notModified) {

View File

@@ -10,6 +10,8 @@
namespace mbgl {
class FileSource;
class SpriteStore : private util::noncopyable {
public:
using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>;
@@ -25,7 +27,7 @@ public:
SpriteStore(float pixelRatio);
~SpriteStore();
void setURL(const std::string&);
void load(const std::string& url, FileSource&);
bool isLoaded() const {
return loaded;

View File

@@ -27,9 +27,10 @@
namespace mbgl {
Style::Style(MapData& data_)
Style::Style(MapData& data_, FileSource& fileSource_)
: data(data_),
glyphStore(std::make_unique<GlyphStore>()),
fileSource(fileSource_),
glyphStore(std::make_unique<GlyphStore>(fileSource)),
glyphAtlas(std::make_unique<GlyphAtlas>(1024, 1024)),
spriteStore(std::make_unique<SpriteStore>(data.pixelRatio)),
spriteAtlas(std::make_unique<SpriteAtlas>(1024, 1024, data.pixelRatio, *spriteStore)),
@@ -55,7 +56,7 @@ void Style::setJSON(const std::string& json, const std::string&) {
}
glyphStore->setURL(parser.glyphURL);
spriteStore->setURL(parser.spriteURL);
spriteStore->load(parser.spriteURL, fileSource);
loaded = true;
}
@@ -123,6 +124,7 @@ void Style::update(const TransformState& transform,
data.getAnimationTime(),
transform,
workers,
fileSource,
texturePool,
shouldReparsePartialTiles,
data.mode,
@@ -180,7 +182,9 @@ void Style::recalculate(float z) {
Source* source = getSource(layer->source);
if (source && layer->needsRendering()) {
source->enabled = true;
if (!source->loaded && !source->isLoading()) source->load();
if (!source->loaded && !source->isLoading()) {
source->load(fileSource);
}
}
}
}

View File

@@ -19,6 +19,7 @@
namespace mbgl {
class MapData;
class FileSource;
class GlyphAtlas;
class GlyphStore;
class SpriteStore;
@@ -54,7 +55,7 @@ class Style : public GlyphStore::Observer,
public Source::Observer,
public util::noncopyable {
public:
Style(MapData&);
Style(MapData&, FileSource&);
~Style();
class Observer : public GlyphStore::Observer,
@@ -107,6 +108,7 @@ public:
void dumpDebugLogs() const;
MapData& data;
FileSource& fileSource;
std::unique_ptr<GlyphStore> glyphStore;
std::unique_ptr<GlyphAtlas> glyphAtlas;
std::unique_ptr<SpriteStore> spriteStore;

View File

@@ -7,6 +7,7 @@ namespace mbgl {
class TransformState;
class Worker;
class FileSource;
class MapData;
class Style;
namespace gl { class TexturePool; }
@@ -18,6 +19,7 @@ public:
TimePoint animationTime_,
const TransformState& transformState_,
Worker& worker_,
FileSource& fileSource_,
gl::TexturePool& texturePool_,
bool shouldReparsePartialTiles_,
const MapMode mode_,
@@ -28,6 +30,7 @@ public:
animationTime(animationTime_),
transformState(transformState_),
worker(worker_),
fileSource(fileSource_),
texturePool(texturePool_),
shouldReparsePartialTiles(shouldReparsePartialTiles_),
mode(mode_),
@@ -39,6 +42,7 @@ public:
TimePoint animationTime;
const TransformState& transformState;
Worker& worker;
FileSource& fileSource;
gl::TexturePool& texturePool;
bool shouldReparsePartialTiles;
const MapMode mode;

View File

@@ -8,7 +8,6 @@
#include <mbgl/util/exception.hpp>
#include <mbgl/util/pbf.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/thread_context.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/util/url.hpp>
@@ -64,11 +63,11 @@ namespace mbgl {
GlyphPBF::GlyphPBF(GlyphStore* store,
const std::string& fontStack,
const GlyphRange& glyphRange,
GlyphStore::Observer* observer_)
GlyphStore::Observer* observer_,
FileSource& fileSource)
: parsed(false),
observer(observer_) {
FileSource* fs = util::ThreadContext::getFileSource();
req = fs->request(Resource::glyphs(store->getURL(), fontStack, glyphRange), [this, store, fontStack, glyphRange](Response res) {
req = fileSource.request(Resource::glyphs(store->getURL(), fontStack, glyphRange), [this, store, fontStack, glyphRange](Response res) {
if (res.error) {
observer->onGlyphsError(fontStack, glyphRange, std::make_exception_ptr(std::runtime_error(res.error->message)));
} else if (res.notModified) {

View File

@@ -14,13 +14,15 @@ namespace mbgl {
class FontStack;
class FileRequest;
class FileSource;
class GlyphPBF : private util::noncopyable {
public:
GlyphPBF(GlyphStore* store,
const std::string& fontStack,
const GlyphRange&,
GlyphStore::Observer*);
GlyphStore::Observer*,
FileSource&);
~GlyphPBF();
bool isParsed() const {

View File

@@ -7,7 +7,10 @@
namespace mbgl {
GlyphStore::GlyphStore() = default;
GlyphStore::GlyphStore(FileSource& fileSource_)
: fileSource(fileSource_) {
}
GlyphStore::~GlyphStore() = default;
void GlyphStore::requestGlyphRange(const std::string& fontStackName, const GlyphRange& range) {
@@ -22,7 +25,7 @@ void GlyphStore::requestGlyphRange(const std::string& fontStackName, const Glyph
}
rangeSets.emplace(range,
std::make_unique<GlyphPBF>(this, fontStackName, range, observer));
std::make_unique<GlyphPBF>(this, fontStackName, range, observer, fileSource));
}

View File

@@ -14,6 +14,7 @@
namespace mbgl {
class FileSource;
class GlyphPBF;
// The GlyphStore manages the loading and storage of Glyphs
@@ -29,7 +30,7 @@ public:
virtual void onGlyphsError(const std::string& /* fontStack */, const GlyphRange&, std::exception_ptr) {};
};
GlyphStore();
GlyphStore(FileSource&);
~GlyphStore();
util::exclusive<FontStack> getFontStack(const std::string& fontStack);
@@ -54,6 +55,7 @@ public:
private:
void requestGlyphRange(const std::string& fontStackName, const GlyphRange& range);
FileSource& fileSource;
std::string glyphURL;
std::unordered_map<std::string, std::map<GlyphRange, std::unique_ptr<GlyphPBF>>> ranges;

View File

@@ -13,6 +13,7 @@ RasterTileData::RasterTileData(const TileID& id_,
const std::string& urlTemplate,
gl::TexturePool &texturePool_,
Worker& worker_,
FileSource& fileSource,
const std::function<void(std::exception_ptr)>& callback)
: TileData(id_),
texturePool(texturePool_),
@@ -20,7 +21,7 @@ RasterTileData::RasterTileData(const TileID& id_,
state = State::loading;
const Resource resource = Resource::tile(urlTemplate, pixelRatio, id.x, id.y, id.sourceZ);
req = util::ThreadContext::getFileSource()->request(resource, [callback, this](Response res) {
req = fileSource.request(resource, [callback, this](Response res) {
if (res.error) {
callback(std::make_exception_ptr(std::runtime_error(res.error->message)));
} else if (res.notModified) {

View File

@@ -6,6 +6,7 @@
namespace mbgl {
class FileSource;
class FileRequest;
class StyleLayer;
class WorkRequest;
@@ -18,6 +19,7 @@ public:
const std::string& urlTemplate,
gl::TexturePool&,
Worker&,
FileSource&,
const std::function<void(std::exception_ptr)>& callback);
~RasterTileData();

View File

@@ -3,7 +3,6 @@
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/thread_context.hpp>
#include <mbgl/util/url.hpp>
#include <utility>
@@ -182,15 +181,16 @@ util::ptr<const GeometryTileFeature> VectorTileLayer::getFeature(std::size_t i)
return std::make_shared<VectorTileFeature>(features.at(i), *this);
}
VectorTileMonitor::VectorTileMonitor(const TileID& tileID_, float pixelRatio_, const std::string& urlTemplate_)
VectorTileMonitor::VectorTileMonitor(const TileID& tileID_, float pixelRatio_, const std::string& urlTemplate_, FileSource& fileSource_)
: tileID(tileID_),
pixelRatio(pixelRatio_),
urlTemplate(urlTemplate_) {
urlTemplate(urlTemplate_),
fileSource(fileSource_) {
}
std::unique_ptr<FileRequest> VectorTileMonitor::monitorTile(const GeometryTileMonitor::Callback& callback) {
const Resource resource = Resource::tile(urlTemplate, pixelRatio, tileID.x, tileID.y, tileID.sourceZ);
return util::ThreadContext::getFileSource()->request(resource, [callback, this](Response res) {
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) {

View File

@@ -59,10 +59,11 @@ private:
};
class TileID;
class FileSource;
class VectorTileMonitor : public GeometryTileMonitor {
public:
VectorTileMonitor(const TileID&, float pixelRatio, const std::string& urlTemplate);
VectorTileMonitor(const TileID&, float pixelRatio, const std::string& urlTemplate, FileSource&);
std::unique_ptr<FileRequest> monitorTile(const GeometryTileMonitor::Callback&) override;
@@ -70,6 +71,7 @@ private:
TileID tileID;
float pixelRatio;
std::string urlTemplate;
FileSource& fileSource;
};
} // namespace mbgl

View File

@@ -44,22 +44,6 @@ ThreadPriority ThreadContext::getPriority() {
}
}
FileSource* ThreadContext::getFileSource() {
if (current.get() != nullptr) {
return current.get()->fileSource;
} else {
return nullptr;
}
}
void ThreadContext::setFileSource(FileSource* fileSource) {
if (current.get() != nullptr) {
current.get()->fileSource = fileSource;
} else {
throw std::runtime_error("Current thread has no current ThreadContext.");
}
}
gl::GLObjectStore* ThreadContext::getGLObjectStore() {
if (current.get() != nullptr) {
return current.get()->glObjectStore;

View File

@@ -7,7 +7,6 @@
namespace mbgl {
class FileSource;
namespace gl { class GLObjectStore; }
namespace util {
@@ -34,8 +33,6 @@ public:
static std::string getName();
static ThreadPriority getPriority();
static FileSource* getFileSource();
static void setFileSource(FileSource* fileSource);
static gl::GLObjectStore* getGLObjectStore();
static void setGLObjectStore(gl::GLObjectStore* glObjectStore);
@@ -43,7 +40,6 @@ public:
ThreadType type;
ThreadPriority priority;
FileSource* fileSource = nullptr;
gl::GLObjectStore* glObjectStore = nullptr;
};

View File

@@ -156,7 +156,6 @@ public:
SpriteStoreTest()
: spriteStore(1.0) {}
util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
util::RunLoop loop;
StubFileSource fileSource;
StubStyleObserver observer;
@@ -166,11 +165,8 @@ public:
// Squelch logging.
Log::setObserver(std::make_unique<Log::NullObserver>());
util::ThreadContext::Set(&context);
util::ThreadContext::setFileSource(&fileSource);
spriteStore.setObserver(&observer);
spriteStore.setURL("test/fixtures/resources/sprite");
spriteStore.load("test/fixtures/resources/sprite", fileSource);
loop.run();
}

View File

@@ -17,14 +17,13 @@ public:
util::RunLoop loop;
StubFileSource fileSource;
StubStyleObserver observer;
GlyphStore glyphStore;
GlyphStore glyphStore { fileSource };
void run(const std::string& url, const std::string& fontStack, const std::set<GlyphRange>& glyphRanges) {
// Squelch logging.
Log::setObserver(std::make_unique<Log::NullObserver>());
util::ThreadContext::Set(&context);
util::ThreadContext::setFileSource(&fileSource);
glyphStore.setObserver(&observer);
glyphStore.setURL(url);

View File

@@ -31,7 +31,7 @@ public:
Worker worker { 1 };
gl::TexturePool texturePool;
MapData mapData { MapMode::Still, GLContextMode::Unique, 1.0 };
Style style { mapData };
Style style { mapData, fileSource };
StyleUpdateParameters updateParameters {
1.0,
@@ -39,6 +39,7 @@ public:
TimePoint(),
transformState,
worker,
fileSource,
texturePool,
true,
MapMode::Continuous,
@@ -51,7 +52,6 @@ public:
Log::setObserver(std::make_unique<Log::NullObserver>());
util::ThreadContext::Set(&context);
util::ThreadContext::setFileSource(&fileSource);
transform.resize({{ 512, 512 }});
transform.setLatLngZoom({0, 0}, 0);
@@ -88,7 +88,7 @@ TEST(Source, LoadingFail) {
Source source(SourceType::Vector, "source", "url", 512, nullptr, nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
test.run();
}
@@ -111,7 +111,7 @@ TEST(Source, LoadingCorrupt) {
Source source(SourceType::Vector, "source", "url", 512, nullptr, nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
test.run();
}
@@ -139,7 +139,7 @@ TEST(Source, RasterTileEmpty) {
Source source(SourceType::Raster, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();
@@ -168,7 +168,7 @@ TEST(Source, VectorTileEmpty) {
Source source(SourceType::Vector, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();
@@ -197,7 +197,7 @@ TEST(Source, RasterTileFail) {
Source source(SourceType::Raster, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();
@@ -226,7 +226,7 @@ TEST(Source, VectorTileFail) {
Source source(SourceType::Vector, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();
@@ -254,7 +254,7 @@ TEST(Source, RasterTileCorrupt) {
Source source(SourceType::Raster, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();
@@ -287,7 +287,7 @@ TEST(Source, VectorTileCorrupt) {
Source source(SourceType::Vector, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();
@@ -314,7 +314,7 @@ TEST(Source, RasterTileCancel) {
Source source(SourceType::Raster, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();
@@ -341,7 +341,7 @@ TEST(Source, VectorTileCancel) {
Source source(SourceType::Vector, "source", "", 512, std::move(info), nullptr);
source.setObserver(&test.observer);
source.load();
source.load(test.fileSource);
source.update(test.updateParameters);
test.run();

View File

@@ -1,4 +1,5 @@
#include "../fixtures/util.hpp"
#include "../fixtures/stub_file_source.hpp"
#include <mbgl/map/map_data.hpp>
#include <mbgl/style/style.hpp>
@@ -7,11 +8,13 @@
using namespace mbgl;
TEST(Style, UnusedSource) {
util::RunLoop loop;
util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
util::ThreadContext::Set(&context);
MapData data { MapMode::Still, GLContextMode::Unique, 1.0 };
Style style { data };
StubFileSource fileSource;
Style style { data, fileSource };
style.setJSON(util::read_file("test/fixtures/resources/style-unused-sources.json"), "");
style.cascade();
@@ -27,11 +30,13 @@ TEST(Style, UnusedSource) {
}
TEST(Style, UnusedSourceActiveViaClassUpdate) {
util::RunLoop loop;
util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular };
util::ThreadContext::Set(&context);
MapData data { MapMode::Still, GLContextMode::Unique, 1.0 };
Style style { data };
StubFileSource fileSource;
Style style { data, fileSource };
data.addClass("visible");