use filesource object for loading all http stuff
This commit is contained in:
@@ -27,6 +27,7 @@ class StyleLayer;
|
||||
class StyleLayerGroup;
|
||||
class StyleSource;
|
||||
class Texturepool;
|
||||
class FileSource;
|
||||
class View;
|
||||
|
||||
class Map : private util::noncopyable {
|
||||
@@ -64,7 +65,7 @@ public:
|
||||
void toggleClass(const std::string &name);
|
||||
const std::vector<std::string> &getAppliedClasses() const;
|
||||
void setDefaultTransitionDuration(uint64_t duration_milliseconds = 0);
|
||||
void setStyleJSON(std::string newStyleJSON);
|
||||
void setStyleJSON(std::string newStyleJSON, const std::string &base = "");
|
||||
std::string getStyleJSON() const;
|
||||
void setAccessToken(std::string access_token);
|
||||
std::string getAccessToken() const;
|
||||
@@ -111,6 +112,7 @@ public:
|
||||
|
||||
public:
|
||||
inline const TransformState &getState() const { return state; }
|
||||
inline std::shared_ptr<FileSource> getFileSource() const { return fileSource; }
|
||||
inline std::shared_ptr<Style> getStyle() const { return style; }
|
||||
inline std::shared_ptr<GlyphAtlas> getGlyphAtlas() { return glyphAtlas; }
|
||||
inline std::shared_ptr<GlyphStore> getGlyphStore() { return glyphStore; }
|
||||
@@ -150,6 +152,14 @@ private:
|
||||
void renderLayers(std::shared_ptr<StyleLayerGroup> group);
|
||||
void renderLayer(std::shared_ptr<StyleLayer> layer_desc, RenderPass pass);
|
||||
|
||||
private:
|
||||
bool async = false;
|
||||
std::shared_ptr<uv::loop> loop;
|
||||
uv_thread_t thread;
|
||||
uv_async_t *async_terminate = nullptr;
|
||||
uv_async_t *async_render = nullptr;
|
||||
uv_async_t *async_cleanup = nullptr;
|
||||
|
||||
private:
|
||||
// If cleared, the next time the render thread attempts to render the map, it will *actually*
|
||||
// render the map.
|
||||
@@ -170,6 +180,8 @@ private:
|
||||
Transform transform;
|
||||
TransformState state;
|
||||
|
||||
std::shared_ptr<FileSource> fileSource;
|
||||
|
||||
std::shared_ptr<Style> style;
|
||||
std::shared_ptr<GlyphAtlas> glyphAtlas;
|
||||
std::shared_ptr<GlyphStore> glyphStore;
|
||||
@@ -189,13 +201,6 @@ private:
|
||||
|
||||
std::set<std::shared_ptr<StyleSource>> activeSources;
|
||||
|
||||
private:
|
||||
bool async = false;
|
||||
std::shared_ptr<uv::loop> loop;
|
||||
uv_thread_t thread;
|
||||
uv_async_t *async_terminate = nullptr;
|
||||
uv_async_t *async_render = nullptr;
|
||||
uv_async_t *async_cleanup = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class Map;
|
||||
class FileSource;
|
||||
|
||||
class SpritePosition {
|
||||
public:
|
||||
@@ -33,11 +33,11 @@ public:
|
||||
class Sprite : public std::enable_shared_from_this<Sprite>, private util::noncopyable {
|
||||
private:
|
||||
struct Key {};
|
||||
void load();
|
||||
void load(const std::shared_ptr<FileSource> &fileSource);
|
||||
|
||||
public:
|
||||
Sprite(const Key &, const std::string& base_url, float pixelRatio);
|
||||
static std::shared_ptr<Sprite> Create(const std::string& base_url, float pixelRatio);
|
||||
static std::shared_ptr<Sprite> Create(const std::string& base_url, float pixelRatio, const std::shared_ptr<FileSource> &fileSource);
|
||||
|
||||
const SpritePosition &getSpritePosition(const std::string& name) const;
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class FileSource;
|
||||
|
||||
class SDFGlyph {
|
||||
public:
|
||||
@@ -46,7 +47,7 @@ private:
|
||||
|
||||
class GlyphPBF {
|
||||
public:
|
||||
GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange);
|
||||
GlyphPBF(const std::string &glyphURL, const std::string &fontStack, GlyphRange glyphRange, const std::shared_ptr<FileSource> &fileSource);
|
||||
|
||||
void parse(FontStack &stack);
|
||||
|
||||
@@ -62,7 +63,7 @@ private:
|
||||
// Manages Glyphrange PBF loading.
|
||||
class GlyphStore {
|
||||
public:
|
||||
GlyphStore();
|
||||
GlyphStore(const std::shared_ptr<FileSource> &fileSource);
|
||||
|
||||
// Block until all specified GlyphRanges of the specified font stack are loaded.
|
||||
void waitForGlyphRanges(const std::string &fontStack, const std::set<GlyphRange> &glyphRanges);
|
||||
@@ -81,6 +82,7 @@ public:
|
||||
std::string glyphURL;
|
||||
|
||||
private:
|
||||
const std::shared_ptr<FileSource> fileSource;
|
||||
std::unordered_map<std::string, std::map<GlyphRange, std::unique_ptr<GlyphPBF>>> ranges;
|
||||
std::unordered_map<std::string, std::unique_ptr<FontStack>> stacks;
|
||||
std::mutex mtx;
|
||||
|
||||
47
include/mbgl/util/filesource.hpp
Normal file
47
include/mbgl/util/filesource.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef MBGL_UTIL_FILESOURCE
|
||||
#define MBGL_UTIL_FILESOURCE
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
|
||||
namespace uv {
|
||||
class loop;
|
||||
}
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
namespace platform {
|
||||
class Response;
|
||||
}
|
||||
|
||||
enum class ResourceType : uint8_t {
|
||||
Unknown,
|
||||
Tile,
|
||||
Glyphs,
|
||||
Image,
|
||||
JSON
|
||||
};
|
||||
|
||||
class FileSource {
|
||||
public:
|
||||
FileSource(const std::shared_ptr<uv::loop> &loop);
|
||||
|
||||
void setBase(const std::string &value);
|
||||
const std::string &getBase() const;
|
||||
|
||||
void load(ResourceType type, const std::string &url, std::function<void(platform::Response *)> callback);
|
||||
|
||||
private:
|
||||
std::shared_ptr<uv::loop> loop;
|
||||
|
||||
// Stores a URL that is used as a base for loading resources with relative path.
|
||||
std::string base;
|
||||
|
||||
// Stores the absolute path to the cache directory.
|
||||
const std::string cache;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -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