use filesource object for loading all http stuff
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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();
|
||||
|
||||
27
src/util/filesource.cpp
Normal file
27
src/util/filesource.cpp
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user