Make the FileSource available from the ThreadContext
For now we set the FileSource for the Worker context but in the future after fixing #1664 we can assert() that FileSource are set only for the Map thread context.
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <mbgl/renderer/painter.hpp>
|
||||
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
|
||||
@@ -35,6 +36,8 @@ MapContext::MapContext(uv_loop_t* loop, View& view_, FileSource& fileSource, Map
|
||||
texturePool(std::make_unique<TexturePool>()) {
|
||||
assert(util::ThreadContext::currentlyOn(util::ThreadType::Map));
|
||||
|
||||
util::ThreadContext::setFileSource(&fileSource);
|
||||
|
||||
asyncUpdate->unref();
|
||||
|
||||
view.activate();
|
||||
@@ -91,7 +94,8 @@ void MapContext::setStyleURL(const std::string& url) {
|
||||
base = styleURL.substr(0, pos + 1);
|
||||
}
|
||||
|
||||
env.request({ Resource::Kind::Style, styleURL }, [this, base](const Response &res) {
|
||||
FileSource* fs = util::ThreadContext::getFileSource();
|
||||
fs->request({ Resource::Kind::Style, styleURL }, util::RunLoop::current.get()->get(), [this, base](const Response &res) {
|
||||
if (res.status == Response::Successful) {
|
||||
loadStyleJSON(res.data, base);
|
||||
} else {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <mbgl/map/source.hpp>
|
||||
#include <mbgl/map/map_data.hpp>
|
||||
#include <mbgl/map/environment.hpp>
|
||||
#include <mbgl/map/transform.hpp>
|
||||
#include <mbgl/map/tile.hpp>
|
||||
#include <mbgl/renderer/painter.hpp>
|
||||
@@ -11,6 +10,7 @@
|
||||
#include <mbgl/util/math.hpp>
|
||||
#include <mbgl/util/box.hpp>
|
||||
#include <mbgl/util/mapbox.hpp>
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
#include <mbgl/style/style_layer.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
@@ -120,7 +120,7 @@ Source::Source() {}
|
||||
|
||||
Source::~Source() {
|
||||
if (req) {
|
||||
Environment::Get().cancelRequest(req);
|
||||
util::ThreadContext::getFileSource()->cancel(req);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +147,8 @@ void Source::load() {
|
||||
return;
|
||||
}
|
||||
|
||||
req = Environment::Get().request({ Resource::Kind::Source, info.url }, [this](const Response &res) {
|
||||
FileSource* fs = util::ThreadContext::getFileSource();
|
||||
req = fs->request({ Resource::Kind::Source, info.url }, util::RunLoop::current.get()->get(), [this](const Response &res) {
|
||||
req = nullptr;
|
||||
|
||||
if (res.status != Response::Successful) {
|
||||
|
||||
@@ -72,8 +72,6 @@ public:
|
||||
void load();
|
||||
bool isLoaded() const;
|
||||
|
||||
void load(MapData&, Environment&, std::function<void()> callback);
|
||||
|
||||
// Request or parse all the tiles relevant for the "TransformState". This method
|
||||
// will return true if all the tiles were scheduled for updating of false if
|
||||
// they were not. shouldReparsePartialTiles must be set to "true" if there is
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#include <mbgl/map/sprite.hpp>
|
||||
|
||||
#include <mbgl/map/environment.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/util/exception.hpp>
|
||||
#include <mbgl/util/raster.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
|
||||
#include <rapidjson/document.h>
|
||||
@@ -29,8 +30,7 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
|
||||
: pixelRatio(pixelRatio_ > 1 ? 2 : 1),
|
||||
raster(),
|
||||
loadedImage(false),
|
||||
loadedJSON(false),
|
||||
env(Environment::Get()) {
|
||||
loadedJSON(false) {
|
||||
if (baseUrl.empty()) {
|
||||
// Treat a non-existent sprite as a successfully loaded empty sprite.
|
||||
loadedImage = true;
|
||||
@@ -41,7 +41,8 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
|
||||
std::string spriteURL(baseUrl + (pixelRatio_ > 1 ? "@2x" : "") + ".png");
|
||||
std::string jsonURL(baseUrl + (pixelRatio_ > 1 ? "@2x" : "") + ".json");
|
||||
|
||||
jsonRequest = env.request({ Resource::Kind::JSON, jsonURL }, [this, jsonURL](const Response &res) {
|
||||
FileSource* fs = util::ThreadContext::getFileSource();
|
||||
jsonRequest = fs->request({ Resource::Kind::JSON, jsonURL }, util::RunLoop::current.get()->get(), [this, jsonURL](const Response &res) {
|
||||
jsonRequest = nullptr;
|
||||
if (res.status == Response::Successful) {
|
||||
body = res.data;
|
||||
@@ -55,7 +56,7 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
|
||||
emitSpriteLoadedIfComplete();
|
||||
});
|
||||
|
||||
spriteRequest = env.request({ Resource::Kind::Image, spriteURL }, [this, spriteURL](const Response &res) {
|
||||
spriteRequest = fs->request({ Resource::Kind::Image, spriteURL }, util::RunLoop::current.get()->get(), [this, spriteURL](const Response &res) {
|
||||
spriteRequest = nullptr;
|
||||
if (res.status == Response::Successful) {
|
||||
image = res.data;
|
||||
@@ -72,11 +73,11 @@ Sprite::Sprite(const std::string& baseUrl, float pixelRatio_)
|
||||
|
||||
Sprite::~Sprite() {
|
||||
if (jsonRequest) {
|
||||
env.cancelRequest(jsonRequest);
|
||||
util::ThreadContext::getFileSource()->cancel(jsonRequest);
|
||||
}
|
||||
|
||||
if (spriteRequest) {
|
||||
env.cancelRequest(spriteRequest);
|
||||
util::ThreadContext::getFileSource()->cancel(spriteRequest);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class Environment;
|
||||
class Request;
|
||||
|
||||
class SpritePosition {
|
||||
@@ -69,7 +68,6 @@ private:
|
||||
std::unordered_map<std::string, SpritePosition> pos;
|
||||
const SpritePosition empty;
|
||||
|
||||
Environment& env;
|
||||
Request* jsonRequest = nullptr;
|
||||
Request* spriteRequest = nullptr;
|
||||
Observer* observer = nullptr;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <mbgl/map/tile_data.hpp>
|
||||
|
||||
#include <mbgl/map/environment.hpp>
|
||||
#include <mbgl/map/source.hpp>
|
||||
#include <mbgl/map/transform_state.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
@@ -16,7 +15,6 @@ TileData::TileData(const TileID& id_, const SourceInfo& source_)
|
||||
: id(id_),
|
||||
name(id),
|
||||
source(source_),
|
||||
env(Environment::Get()),
|
||||
state(State::initial),
|
||||
debugBucket(debugFontBuffer) {
|
||||
// Initialize tile debug coordinates
|
||||
@@ -43,7 +41,8 @@ void TileData::request(Worker& worker,
|
||||
std::string url = source.tileURL(id, pixelRatio);
|
||||
state = State::loading;
|
||||
|
||||
req = env.request({ Resource::Kind::Tile, url }, [url, callback, &worker, this](const Response &res) {
|
||||
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) {
|
||||
@@ -67,7 +66,7 @@ void TileData::cancel() {
|
||||
state = State::obsolete;
|
||||
}
|
||||
if (req) {
|
||||
env.cancelRequest(req);
|
||||
util::ThreadContext::getFileSource()->cancel(req);
|
||||
req = nullptr;
|
||||
}
|
||||
workRequest.reset();
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class Environment;
|
||||
class Painter;
|
||||
class SourceInfo;
|
||||
class StyleLayer;
|
||||
@@ -136,7 +135,6 @@ protected:
|
||||
bool mayStartParsing();
|
||||
|
||||
const SourceInfo& source;
|
||||
Environment& env;
|
||||
|
||||
Request *req = nullptr;
|
||||
std::string data;
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#include <mbgl/text/glyph_pbf.hpp>
|
||||
#include <mbgl/text/font_stack.hpp>
|
||||
|
||||
#include <mbgl/map/environment.hpp>
|
||||
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
|
||||
#include <mbgl/util/pbf.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/util/token.hpp>
|
||||
#include <mbgl/util/url.hpp>
|
||||
|
||||
@@ -18,10 +18,9 @@ namespace mbgl {
|
||||
GlyphPBF::GlyphPBF(const std::string& glyphURL,
|
||||
const std::string& fontStack,
|
||||
GlyphRange glyphRange,
|
||||
Environment& env_,
|
||||
const GlyphLoadedCallback& successCallback,
|
||||
const GlyphLoadingFailedCallback& failureCallback)
|
||||
: parsed(false), env(env_) {
|
||||
: parsed(false) {
|
||||
// Load the glyph set URL
|
||||
url = util::replaceTokens(glyphURL, [&](const std::string &name) -> std::string {
|
||||
if (name == "fontstack") return util::percentEncode(fontStack);
|
||||
@@ -30,7 +29,8 @@ GlyphPBF::GlyphPBF(const std::string& glyphURL,
|
||||
});
|
||||
|
||||
// The prepare call jumps back to the main thread.
|
||||
req = env.request({ Resource::Kind::Glyphs, url }, [&, successCallback, failureCallback](const Response &res) {
|
||||
FileSource* fs = util::ThreadContext::getFileSource();
|
||||
req = fs->request({ Resource::Kind::Glyphs, url }, util::RunLoop::current.get()->get(), [&, successCallback, failureCallback](const Response &res) {
|
||||
req = nullptr;
|
||||
|
||||
if (res.status != Response::Successful) {
|
||||
@@ -50,7 +50,7 @@ GlyphPBF::GlyphPBF(const std::string& glyphURL,
|
||||
|
||||
GlyphPBF::~GlyphPBF() {
|
||||
if (req) {
|
||||
env.cancelRequest(req);
|
||||
util::ThreadContext::getFileSource()->cancel(req);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class Environment;
|
||||
class FontStack;
|
||||
class Request;
|
||||
|
||||
@@ -21,7 +20,6 @@ public:
|
||||
GlyphPBF(const std::string &glyphURL,
|
||||
const std::string &fontStack,
|
||||
GlyphRange glyphRange,
|
||||
Environment &env,
|
||||
const GlyphLoadedCallback& successCallback,
|
||||
const GlyphLoadingFailedCallback& failureCallback);
|
||||
~GlyphPBF();
|
||||
@@ -43,7 +41,6 @@ private:
|
||||
std::string url;
|
||||
std::atomic<bool> parsed;
|
||||
|
||||
Environment& env;
|
||||
Request* req = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ bool GlyphStore::requestGlyphRangesIfNeeded(const std::string& fontStackName,
|
||||
for (const auto& range : glyphRanges) {
|
||||
const auto& rangeSets_it = rangeSets.find(range);
|
||||
if (rangeSets_it == rangeSets.end()) {
|
||||
auto glyph = std::make_unique<GlyphPBF>(glyphURL, fontStackName, range, env,
|
||||
auto glyph = std::make_unique<GlyphPBF>(glyphURL, fontStackName, range,
|
||||
successCallback, failureCallback);
|
||||
rangeSets.emplace(range, std::move(glyph));
|
||||
requestIsNeeded = true;
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
#include <thread>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class FileSource;
|
||||
|
||||
namespace util {
|
||||
|
||||
enum class ThreadPriority : bool {
|
||||
@@ -38,11 +41,21 @@ public:
|
||||
return current.get()->priority;
|
||||
}
|
||||
|
||||
static FileSource* getFileSource() {
|
||||
return current.get()->fileSource;
|
||||
}
|
||||
|
||||
static void setFileSource(FileSource* fileSource) {
|
||||
current.get()->fileSource = fileSource;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
ThreadType type;
|
||||
ThreadPriority priority;
|
||||
|
||||
FileSource* fileSource = nullptr;
|
||||
|
||||
static uv::tls<ThreadContext> current;
|
||||
|
||||
friend class MainThreadContextRegistrar;
|
||||
|
||||
@@ -20,7 +20,16 @@ public:
|
||||
Worker::Worker(std::size_t count) {
|
||||
util::ThreadContext context = {"Worker", util::ThreadType::Worker, util::ThreadPriority::Low};
|
||||
for (std::size_t i = 0; i < count; i++) {
|
||||
threads.emplace_back(std::make_unique<util::Thread<Impl>>(context));
|
||||
std::unique_ptr<util::Thread<Impl>> worker(new util::Thread<Impl>(context));
|
||||
|
||||
// FIXME: Workers should not access the FileSource but it
|
||||
// is currently needed because of GlyphsPBF. See #1664.
|
||||
auto task = std::make_shared<WorkTask>([fs = util::ThreadContext::getFileSource()]{
|
||||
util::ThreadContext::setFileSource(fs);
|
||||
}, []{});
|
||||
|
||||
worker->invoke(&Worker::Impl::doWork, task);
|
||||
threads.emplace_back(std::move(worker));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ public:
|
||||
envScope_(env_, ThreadType::Map, "Map"),
|
||||
data_(view, MapMode::Still),
|
||||
callback_(callback) {
|
||||
util::ThreadContext::setFileSource(&fileSource);
|
||||
|
||||
data_.transform.resize(1000, 1000, 1.0, 1000, 1000);
|
||||
data_.transform.setLatLngZoom({0, 0}, 16);
|
||||
|
||||
Reference in New Issue
Block a user