diff options
author | Leith Bade <leith@mapbox.com> | 2014-12-04 08:02:57 +1100 |
---|---|---|
committer | Leith Bade <leith@mapbox.com> | 2014-12-04 08:02:57 +1100 |
commit | 991a74774e1e835ff2277b3997d60f09245593dd (patch) | |
tree | f881149caec5f1e1a55a4e9900548f8a5e92bf0b /src | |
parent | 58833dd56a29d14afb7b40d8484328941d3d5020 (diff) | |
parent | 21b4f8c501d67ed8ecf6dedbdd55064a5c8f823c (diff) | |
download | qtlocation-mapboxgl-991a74774e1e835ff2277b3997d60f09245593dd.tar.gz |
Merge branch 'master' of github.com:mapbox/mapbox-gl-native into android-mason
Conflicts:
gyp/mbgl-ios.gypi
gyp/mbgl-linux.gypi
gyp/mbgl-osx.gypi
platform/default/caching_http_file_source.cpp
Diffstat (limited to 'src')
-rw-r--r-- | src/map/map.cpp | 35 | ||||
-rw-r--r-- | src/storage/file_source.cpp | 110 |
2 files changed, 16 insertions, 129 deletions
diff --git a/src/map/map.cpp b/src/map/map.cpp index 3fe770e8e6..9236c5d2ec 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -90,13 +90,14 @@ const static bool sqlite_version_check = []() { using namespace mbgl; -Map::Map(View& view_) +Map::Map(View& view_, FileSource& fileSource_) : loop(util::make_unique<uv::loop>()), view(view_), #ifndef NDEBUG mainThread(uv_thread_self()), #endif transform(view_), + fileSource(fileSource_), glyphAtlas(1024, 1024), spriteAtlas(512, 512), texturePool(std::make_shared<TexturePool>()), @@ -120,7 +121,6 @@ Map::~Map() { glyphStore.reset(); style.reset(); texturePool.reset(); - fileSource.reset(); workers.reset(); uv_run(**loop, UV_RUN_DEFAULT); @@ -150,7 +150,6 @@ void Map::start(bool startPaused) { // Remove all of these to make sure they are destructed in the correct thread. glyphStore.reset(); - fileSource.reset(); style.reset(); workers.reset(); activeSources.clear(); @@ -371,11 +370,9 @@ void Map::terminate() { void Map::setReachability(bool reachable) { // Note: This function may be called from *any* thread. if (reachable) { - if (fileSource) { - fileSource->prepare([&]() { - fileSource->retryAllPending(); - }); - } + fileSource.prepare([&]() { + fileSource.retryAllPending(); + }); } } @@ -407,11 +404,11 @@ void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) { } style->loadJSON((const uint8_t *)styleJSON.c_str()); - if (!fileSource) { - fileSource = std::make_shared<FileSource>(**loop, platform::defaultCacheDatabase()); - glyphStore = std::make_shared<GlyphStore>(*fileSource); + if (!fileSource.hasLoop()) { + fileSource.setLoop(**loop); + glyphStore = std::make_shared<GlyphStore>(fileSource); } - fileSource->setBase(base); + fileSource.setBase(base); glyphStore->setURL(util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken())); style->setDefaultTransitionDuration(defaultTransitionDuration); @@ -447,7 +444,7 @@ util::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, *fileSource); + sprite = Sprite::Create(sprite_url, pixelRatio, fileSource); } return sprite; @@ -671,7 +668,7 @@ void Map::updateSources() { if (source->enabled) { if (!source->source) { source->source = std::make_shared<Source>(source->info); - source->source->load(*this, *fileSource); + source->source->load(*this, fileSource); } } else { source->source.reset(); @@ -705,20 +702,20 @@ void Map::updateTiles() { source->source->update(*this, getWorker(), style, glyphAtlas, *glyphStore, spriteAtlas, getSprite(), - *texturePool, *fileSource, [this](){ update(); }); + *texturePool, fileSource, [this](){ update(); }); } } void Map::prepare() { - if (!fileSource) { - fileSource = std::make_shared<FileSource>(**loop, platform::defaultCacheDatabase()); - glyphStore = std::make_shared<GlyphStore>(*fileSource); + if (!fileSource.hasLoop()) { + fileSource.setLoop(**loop); + glyphStore = std::make_shared<GlyphStore>(fileSource); } if (!style) { style = std::make_shared<Style>(); - fileSource->request(ResourceType::JSON, styleURL)->onload([&](const Response &res) { + fileSource.request(ResourceType::JSON, styleURL)->onload([&](const Response &res) { if (res.code == 200) { // Calculate the base const size_t pos = styleURL.rfind('/'); diff --git a/src/storage/file_source.cpp b/src/storage/file_source.cpp deleted file mode 100644 index 7c1ffc47f8..0000000000 --- a/src/storage/file_source.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include <mbgl/storage/file_source.hpp> -#include <mbgl/storage/file_request.hpp> -#include <mbgl/storage/http_request.hpp> -#include <mbgl/storage/sqlite_store.hpp> -#include <mbgl/storage/asset_request.hpp> -#include <mbgl/util/uv-messenger.h> -#include <mbgl/util/std.hpp> - -#include <uv.h> - -namespace mbgl { - -FileSource::FileSource(uv_loop_t *loop_, const std::string &path) - : thread_id(uv_thread_self()), - store(!path.empty() ? util::ptr<SQLiteStore>(new SQLiteStore(loop_, path)) : nullptr), - loop(loop_), - queue(new uv_messenger_t) { - - uv_messenger_init(loop, queue, [](void *ptr) { - std::unique_ptr<std::function<void()>> fn { reinterpret_cast<std::function<void()> *>(ptr) }; - (*fn)(); - }); - uv_unref((uv_handle_t *)&queue->async); -} - -FileSource::~FileSource() { - // FIXME temp fix for #608 crash - //assert(thread_id == uv_thread_self()); - uv_messenger_stop(queue, [](uv_messenger_t *msgr) { - delete msgr; - }); - - util::ptr<BaseRequest> req; - - // Send a cancel() message to all requests that we are still holding. - for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) { - if ((req = pair.second.lock())) { - req->cancel(); - } - } -} - -void FileSource::setBase(const std::string &value) { - assert(thread_id == uv_thread_self()); - base = value; -} - -const std::string &FileSource::getBase() const { - assert(thread_id == uv_thread_self()); - return base; -} - -std::unique_ptr<Request> FileSource::request(ResourceType type, const std::string &url) { - assert(thread_id == uv_thread_self()); - - // Make URL absolute. - const std::string absoluteURL = [&]() -> std::string { - const size_t separator = url.find("://"); - if (separator == std::string::npos) { - // Relative URL. - return base + url; - } else { - return url; - } - }(); - - util::ptr<BaseRequest> req; - - // First, try to find an existing Request object. - auto it = pending.find(absoluteURL); - if (it != pending.end()) { - req = it->second.lock(); - } - - if (!req) { - if (absoluteURL.substr(0, 7) == "file://") { - req = std::make_shared<FileRequest>(absoluteURL.substr(7), loop); - } else if (absoluteURL.substr(0, 8) == "asset://") { - req = std::make_shared<AssetRequest>(absoluteURL.substr(8), loop); - } else { - req = std::make_shared<HTTPRequest>(type, absoluteURL, loop, store); - } - - pending.emplace(absoluteURL, req); - } - - return util::make_unique<Request>(req); -} - -void FileSource::prepare(std::function<void()> fn) { - if (thread_id == uv_thread_self()) { - fn(); - } else { - uv_messenger_send(queue, new std::function<void()>(std::move(fn))); - } -} - -void FileSource::retryAllPending() { - assert(thread_id == uv_thread_self()); - - util::ptr<BaseRequest> req; - for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) { - if ((req = pair.second.lock())) { - req->retryImmediately(); - } - } - -} - -} |