summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-08-08 15:08:41 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-08-08 15:08:41 +0200
commita045d9c9d4e7040b699f1fbab88893440969ffe9 (patch)
treed7ad04d2035fbfd1ef712afc1a964c249d0f1905 /src
parent70a31baf5c78b9dad4f2dcdc7920b7eb1453e402 (diff)
downloadqtlocation-mapboxgl-a045d9c9d4e7040b699f1fbab88893440969ffe9.tar.gz
use filesource object for loading all http stuff
Diffstat (limited to 'src')
-rw-r--r--src/map/map.cpp15
-rw-r--r--src/map/sprite.cpp11
-rw-r--r--src/map/tile_data.cpp5
-rw-r--r--src/text/glyph_store.cpp9
-rw-r--r--src/util/filesource.cpp27
5 files changed, 50 insertions, 17 deletions
diff --git a/src/map/map.cpp b/src/map/map.cpp
index b7a9db505b..e394b71b09 100644
--- a/src/map/map.cpp
+++ b/src/map/map.cpp
@@ -18,6 +18,7 @@
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/util/texturepool.hpp>
#include <mbgl/geometry/sprite_atlas.hpp>
+#include <mbgl/util/filesource.hpp>
#include <algorithm>
#include <memory>
@@ -29,15 +30,16 @@
using namespace mbgl;
Map::Map(View& view)
- : view(view),
+ : loop(std::make_shared<uv::loop>()),
+ view(view),
transform(view),
+ fileSource(std::make_shared<FileSource>(loop)),
style(std::make_shared<Style>()),
glyphAtlas(std::make_shared<GlyphAtlas>(1024, 1024)),
- glyphStore(std::make_shared<GlyphStore>()),
+ glyphStore(std::make_shared<GlyphStore>(fileSource)),
spriteAtlas(std::make_shared<SpriteAtlas>(512, 512)),
texturepool(std::make_shared<Texturepool>()),
- painter(*this),
- loop(std::make_shared<uv::loop>()) {
+ painter(*this) {
view.initialize(this);
@@ -184,10 +186,11 @@ void Map::setup() {
painter.setup();
}
-void Map::setStyleJSON(std::string newStyleJSON) {
+void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) {
styleJSON.swap(newStyleJSON);
sprite.reset();
style->loadJSON((const uint8_t *)styleJSON.c_str());
+ fileSource->setBase(base);
glyphStore->setURL(style->glyph_url);
update();
}
@@ -208,7 +211,7 @@ std::shared_ptr<Sprite> Map::getSprite() {
const float pixelRatio = state.getPixelRatio();
const std::string &sprite_url = style->getSpriteURL();
if (!sprite || sprite->pixelRatio != pixelRatio) {
- sprite = Sprite::Create(sprite_url, pixelRatio);
+ sprite = Sprite::Create(sprite_url, pixelRatio, fileSource);
}
return sprite;
diff --git a/src/map/sprite.cpp b/src/map/sprite.cpp
index 68453de52b..f396fec07d 100644
--- a/src/map/sprite.cpp
+++ b/src/map/sprite.cpp
@@ -5,6 +5,7 @@
#include <string>
#include <mbgl/platform/platform.hpp>
+#include <mbgl/util/filesource.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/util/std.hpp>
@@ -20,9 +21,9 @@ SpritePosition::SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t
pixelRatio(pixelRatio) {
}
-std::shared_ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio) {
+std::shared_ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio, const std::shared_ptr<FileSource> &fileSource) {
std::shared_ptr<Sprite> sprite(std::make_shared<Sprite>(Key(), base_url, pixelRatio));
- sprite->load();
+ sprite->load(fileSource);
return sprite;
}
@@ -49,7 +50,7 @@ Sprite::operator bool() const {
// 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 Sprite::load() {
+void Sprite::load(const std::shared_ptr<FileSource> &fileSource) {
if (!valid) {
// Treat a non-existent sprite as a successfully loaded empty sprite.
loadedImage = true;
@@ -60,7 +61,7 @@ void Sprite::load() {
std::shared_ptr<Sprite> sprite = shared_from_this();
- platform::request_http(jsonURL, [sprite](platform::Response *res) {
+ fileSource->load(ResourceType::JSON, jsonURL, [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->body.swap(res->body);
sprite->parseJSON();
@@ -73,7 +74,7 @@ void Sprite::load() {
}
});
- platform::request_http(spriteURL, [sprite](platform::Response *res) {
+ fileSource->load(ResourceType::Image, spriteURL, [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->image.swap(res->body);
sprite->parseImage();
diff --git a/src/map/tile_data.cpp b/src/map/tile_data.cpp
index 57ad6944bf..a94eb00fbf 100644
--- a/src/map/tile_data.cpp
+++ b/src/map/tile_data.cpp
@@ -4,6 +4,7 @@
#include <mbgl/util/token.hpp>
#include <mbgl/util/string.hpp>
+#include <mbgl/util/filesource.hpp>
using namespace mbgl;
@@ -38,7 +39,7 @@ void TileData::request() {
// Note: Somehow this feels slower than the change to request_http()
std::weak_ptr<TileData> weak_tile = shared_from_this();
- req = platform::request_http(url, [weak_tile](platform::Response *res) {
+ map.getFileSource()->load(ResourceType::Tile, url, [weak_tile](platform::Response *res) {
std::shared_ptr<TileData> tile = weak_tile.lock();
if (!tile || tile->state == State::obsolete) {
// noop. Tile is obsolete and we're now just waiting for the refcount
@@ -55,7 +56,7 @@ void TileData::request() {
fprintf(stderr, "[%s] tile loading failed: %d, %s\n", tile->url.c_str(), res->code, res->error_message.c_str());
#endif
}
- }, map.getLoop());
+ });
}
void TileData::cancel() {
diff --git a/src/text/glyph_store.cpp b/src/text/glyph_store.cpp
index fe4f7de9c2..4b90b51c24 100644
--- a/src/text/glyph_store.cpp
+++ b/src/text/glyph_store.cpp
@@ -7,6 +7,7 @@
#include <mbgl/util/constants.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/util/math.hpp>
+#include <mbgl/util/filesource.hpp>
#include <mbgl/platform/platform.hpp>
#include <uv.h>
#include <algorithm>
@@ -135,7 +136,7 @@ void FontStack::lineWrap(Shaping &shaping, const float lineHeight, const float m
align(shaping, justify, horizontalAlign, verticalAlign, maxLineLength, lineHeight, line);
}
-GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange)
+GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange, const std::shared_ptr<FileSource> &fileSource)
: future(promise.get_future().share())
{
// Load the glyph set URL
@@ -152,7 +153,7 @@ GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, Gl
fprintf(stderr, "%s\n", url.c_str());
#endif
- platform::request_http(url, [&](platform::Response *res) {
+ fileSource->load(ResourceType::Glyphs, url, [&](platform::Response *res) {
if (res->code != 200) {
// Something went wrong with loading the glyph pbf. Pass on the error to the future listeners.
const std::string msg = util::sprintf<255>("[ERROR] failed to load glyphs (%d): %s\n", res->code, res->error_message.c_str());
@@ -226,7 +227,7 @@ void GlyphPBF::parse(FontStack &stack) {
data.clear();
}
-GlyphStore::GlyphStore() {}
+GlyphStore::GlyphStore(const std::shared_ptr<FileSource> &fileSource) : fileSource(fileSource) {}
void GlyphStore::setURL(const std::string &url) {
glyphURL = url;
@@ -269,7 +270,7 @@ std::shared_future<GlyphPBF &> GlyphStore::loadGlyphRange(const std::string &fon
auto range_it = rangeSets.find(range);
if (range_it == rangeSets.end()) {
// We don't have this glyph set yet for this font stack.
- range_it = rangeSets.emplace(range, std::make_unique<GlyphPBF>(glyphURL, fontStack, range)).first;
+ range_it = rangeSets.emplace(range, std::make_unique<GlyphPBF>(glyphURL, fontStack, range, fileSource)).first;
}
return range_it->second->getFuture();
diff --git a/src/util/filesource.cpp b/src/util/filesource.cpp
new file mode 100644
index 0000000000..a910d25191
--- /dev/null
+++ b/src/util/filesource.cpp
@@ -0,0 +1,27 @@
+#include <mbgl/util/filesource.hpp>
+#include <mbgl/platform/platform.hpp>
+
+namespace mbgl {
+
+FileSource::FileSource(const std::shared_ptr<uv::loop> &loop)
+ : loop(loop) {}
+
+
+void FileSource::setBase(const std::string &value) {
+ base = value;
+}
+
+const std::string &FileSource::getBase() const {
+ return base;
+}
+
+void FileSource::load(ResourceType type, const std::string &url, std::function<void(platform::Response *)> callback) {
+ // convert relative URLs to absolute URLs
+
+ // try to load from disk (not yet)
+
+ // load from the internet
+ platform::request_http(url, callback, loop);
+}
+
+} \ No newline at end of file