summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2015-06-24 18:40:46 +0300
committerThiago Marcos P. Santos <thiago@mapbox.com>2015-06-25 16:26:21 +0300
commitbe4cb6786babca3368b10e2c1c1aaa9eb43e5663 (patch)
treeea3def9013e1666d92d35cff261e0f9bff16beba /src
parentb56c356de71a3edec76984ddf4202c92930bd17b (diff)
downloadqtlocation-mapboxgl-be4cb6786babca3368b10e2c1c1aaa9eb43e5663.tar.gz
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.
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/map/map_context.cpp6
-rw-r--r--src/mbgl/map/source.cpp7
-rw-r--r--src/mbgl/map/source.hpp2
-rw-r--r--src/mbgl/map/sprite.cpp15
-rw-r--r--src/mbgl/map/sprite.hpp2
-rw-r--r--src/mbgl/map/tile_data.cpp7
-rw-r--r--src/mbgl/map/tile_data.hpp2
-rw-r--r--src/mbgl/text/glyph_pbf.cpp12
-rw-r--r--src/mbgl/text/glyph_pbf.hpp3
-rw-r--r--src/mbgl/text/glyph_store.cpp2
-rw-r--r--src/mbgl/util/thread_context.hpp13
-rw-r--r--src/mbgl/util/worker.cpp11
12 files changed, 50 insertions, 32 deletions
diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp
index ecb9d84639..d3a014030a 100644
--- a/src/mbgl/map/map_context.cpp
+++ b/src/mbgl/map/map_context.cpp
@@ -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 {
diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp
index a45c371756..c9f3e84210 100644
--- a/src/mbgl/map/source.cpp
+++ b/src/mbgl/map/source.cpp
@@ -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) {
diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp
index aea8939c45..dbc1272d71 100644
--- a/src/mbgl/map/source.hpp
+++ b/src/mbgl/map/source.hpp
@@ -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
diff --git a/src/mbgl/map/sprite.cpp b/src/mbgl/map/sprite.cpp
index 017cc61e19..8d05ea9f79 100644
--- a/src/mbgl/map/sprite.cpp
+++ b/src/mbgl/map/sprite.cpp
@@ -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);
}
}
diff --git a/src/mbgl/map/sprite.hpp b/src/mbgl/map/sprite.hpp
index b0fda30018..47b871faf7 100644
--- a/src/mbgl/map/sprite.hpp
+++ b/src/mbgl/map/sprite.hpp
@@ -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;
diff --git a/src/mbgl/map/tile_data.cpp b/src/mbgl/map/tile_data.cpp
index 6ff92bb6e5..f1c661300c 100644
--- a/src/mbgl/map/tile_data.cpp
+++ b/src/mbgl/map/tile_data.cpp
@@ -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();
diff --git a/src/mbgl/map/tile_data.hpp b/src/mbgl/map/tile_data.hpp
index 97852c5cd2..aa302a4fa2 100644
--- a/src/mbgl/map/tile_data.hpp
+++ b/src/mbgl/map/tile_data.hpp
@@ -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;
diff --git a/src/mbgl/text/glyph_pbf.cpp b/src/mbgl/text/glyph_pbf.cpp
index 899df39d38..142461ec71 100644
--- a/src/mbgl/text/glyph_pbf.cpp
+++ b/src/mbgl/text/glyph_pbf.cpp
@@ -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);
}
}
diff --git a/src/mbgl/text/glyph_pbf.hpp b/src/mbgl/text/glyph_pbf.hpp
index bb6fa83ae6..cc083e7f10 100644
--- a/src/mbgl/text/glyph_pbf.hpp
+++ b/src/mbgl/text/glyph_pbf.hpp
@@ -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;
};
diff --git a/src/mbgl/text/glyph_store.cpp b/src/mbgl/text/glyph_store.cpp
index 9520b63c06..9afa765424 100644
--- a/src/mbgl/text/glyph_store.cpp
+++ b/src/mbgl/text/glyph_store.cpp
@@ -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;
diff --git a/src/mbgl/util/thread_context.hpp b/src/mbgl/util/thread_context.hpp
index d4b56cafa8..c22f513289 100644
--- a/src/mbgl/util/thread_context.hpp
+++ b/src/mbgl/util/thread_context.hpp
@@ -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;
diff --git a/src/mbgl/util/worker.cpp b/src/mbgl/util/worker.cpp
index 1806ba78ce..4c56057347 100644
--- a/src/mbgl/util/worker.cpp
+++ b/src/mbgl/util/worker.cpp
@@ -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));
}
}