Introduce the ResourceLoader

ResourceLoader will handle all the tile changes and notifications
through the Source object owned by the Style. This object lives
entirely on the Map thread and should be only accessed by it.

The observer will receive notifications every time the tile data changes
and this will probably trigger a new rendering operation.

This first patch will delegate Source loading to the ResourceLoader.
This commit is contained in:
Thiago Marcos P. Santos
2015-04-21 12:05:37 +02:00
parent fab65e75d0
commit 4956da61b9
4 changed files with 112 additions and 7 deletions

View File

@@ -62,6 +62,7 @@ MapContext::~MapContext() {
// Explicit resets currently necessary because these abandon resources that need to be
// cleaned up by env.performCleanup();
resourceLoader.reset();
style.reset();
sprite.reset();
painter.reset();
@@ -141,6 +142,7 @@ util::ptr<Sprite> MapContext::getSprite() {
void MapContext::loadStyleJSON(const std::string& json, const std::string& base) {
assert(Environment::currentlyOn(ThreadType::Map));
resourceLoader.reset();
sprite.reset();
style.reset();
@@ -153,12 +155,10 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base)
const std::string glyphURL = util::mapbox::normalizeGlyphsURL(style->glyph_url, data.getAccessToken());
glyphStore->setURL(glyphURL);
for (const auto& source : style->sources) {
source->load(data.getAccessToken(), env, [this]() {
assert(Environment::currentlyOn(ThreadType::Map));
triggerUpdate();
});
}
resourceLoader = util::make_unique<ResourceLoader>();
resourceLoader->setAccessToken(data.getAccessToken());
resourceLoader->setObserver(this);
resourceLoader->setStyle(style.get());
triggerUpdate(Update::Zoom);
}
@@ -299,4 +299,9 @@ void MapContext::onLowMemory() {
view.invalidate([this] { render(); });
}
void MapContext::onTileDataChanged() {
assert(Environment::currentlyOn(ThreadType::Map));
triggerUpdate();
}
}

View File

@@ -4,6 +4,7 @@
#include <mbgl/map/tile_id.hpp>
#include <mbgl/map/update.hpp>
#include <mbgl/map/environment.hpp>
#include <mbgl/map/resource_loader.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/util/ptr.hpp>
@@ -32,7 +33,7 @@ class StillImage;
struct LatLng;
struct LatLngBounds;
class MapContext {
class MapContext : public ResourceLoader::Observer {
public:
MapContext(uv_loop_t*, View&, FileSource&, MapData&, bool startPaused);
~MapContext();
@@ -58,6 +59,9 @@ public:
void setSourceTileCacheSize(size_t size);
void onLowMemory();
// ResourceLoader::Observer implementation.
void onTileDataChanged() override;
private:
util::ptr<Sprite> getSprite();
void updateTiles();
@@ -84,6 +88,7 @@ private:
std::unique_ptr<TexturePool> texturePool;
std::unique_ptr<Painter> painter;
std::unique_ptr<Style> style;
std::unique_ptr<ResourceLoader> resourceLoader;
util::ptr<Sprite> sprite;

View File

@@ -0,0 +1,48 @@
#include <mbgl/map/resource_loader.hpp>
#include <mbgl/map/environment.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/style/style.hpp>
#include <cassert>
#include <functional>
namespace mbgl {
ResourceLoader::ResourceLoader() {
assert(Environment::currentlyOn(ThreadType::Map));
}
ResourceLoader::~ResourceLoader() {
assert(Environment::currentlyOn(ThreadType::Map));
}
void ResourceLoader::setObserver(Observer* observer) {
assert(Environment::currentlyOn(ThreadType::Map));
assert(!observer_);
observer_ = observer;
}
void ResourceLoader::setStyle(Style* style) {
style_ = style;
Environment& env = Environment::Get();
for (const auto& source : style->sources) {
source->load(accessToken_, env, std::bind(&ResourceLoader::emitTileDataChanged, this));
}
}
void ResourceLoader::setAccessToken(const std::string& accessToken) {
accessToken_ = accessToken;
}
void ResourceLoader::emitTileDataChanged() {
assert(Environment::currentlyOn(ThreadType::Map));
if (observer_) {
observer_->onTileDataChanged();
}
}
}

View File

@@ -0,0 +1,47 @@
#ifndef MBGL_MAP_RESOURCE_LOADER
#define MBGL_MAP_RESOURCE_LOADER
#include <mbgl/util/noncopyable.hpp>
#include <string>
namespace mbgl {
class Style;
// ResourceLoader is responsible for loading and updating the Source(s) owned
// by the Style. The Source object currently owns all the tiles, thus this
// class will notify its observers of any change on these tiles which will
// ultimately cause a new rendering to be triggered.
class ResourceLoader : private util::noncopyable {
public:
class Observer {
public:
virtual ~Observer() = default;
virtual void onTileDataChanged() = 0;
};
ResourceLoader();
~ResourceLoader();
void setObserver(Observer* observer);
// The style object currently owns all the sources. When setting
// a new style we will go through all of them and try to load.
void setStyle(Style* style);
// Set the access token to be used for loading the tile data.
void setAccessToken(const std::string& accessToken);
private:
void emitTileDataChanged();
std::string accessToken_;
Style* style_ = nullptr;
Observer* observer_ = nullptr;
};
}
#endif