summaryrefslogtreecommitdiff
path: root/src/mbgl/style
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2015-03-12 15:54:54 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2015-03-13 10:55:47 -0700
commitbffee0715458530c6c86f440f757a4de667278a2 (patch)
tree603205a6487065d7d9f61663879ce91b70cdddc8 /src/mbgl/style
parentc36522e2a0481eaa0960f45f59ac3b023846b3e0 (diff)
downloadqtlocation-mapboxgl-bffee0715458530c6c86f440f757a4de667278a2.tar.gz
Move atlas ownership to Style
This follows gl-js and just makes sense -- whenever the style changes the atlases should be blown away. Refs #957
Diffstat (limited to 'src/mbgl/style')
-rw-r--r--src/mbgl/style/style.cpp81
-rw-r--r--src/mbgl/style/style.hpp31
2 files changed, 99 insertions, 13 deletions
diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp
index 45217950f6..c606039a2c 100644
--- a/src/mbgl/style/style.cpp
+++ b/src/mbgl/style/style.cpp
@@ -1,5 +1,6 @@
#include <mbgl/style/style.hpp>
#include <mbgl/map/sprite.hpp>
+#include <mbgl/map/source.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/style/style_parser.hpp>
#include <mbgl/style/style_bucket.hpp>
@@ -8,6 +9,13 @@
#include <mbgl/util/std.hpp>
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/platform/log.hpp>
+#include <mbgl/text/glyph_store.hpp>
+#include <mbgl/geometry/glyph_atlas.hpp>
+#include <mbgl/geometry/sprite_atlas.hpp>
+#include <mbgl/geometry/line_atlas.hpp>
+#include <mbgl/util/mapbox.hpp>
+#include <mbgl/map/map.hpp>
+
#include <csscolorparser/csscolorparser.hpp>
#include <rapidjson/document.h>
@@ -16,14 +24,76 @@
namespace mbgl {
-Style::Style()
- : mtx(util::make_unique<uv::rwlock>()) {
+Style::Style(Environment& env)
+ : glyphAtlas(util::make_unique<GlyphAtlas>(1024, 1024)),
+ spriteAtlas(util::make_unique<SpriteAtlas>(512, 512)),
+ lineAtlas(util::make_unique<LineAtlas>(512, 512)),
+ mtx(util::make_unique<uv::rwlock>()),
+ glyphStore(util::make_unique<GlyphStore>(env))
+{
}
// Note: This constructor is seemingly empty, but we need to declare it anyway
// because this file includes uv_detail.hpp, which has the declarations necessary
// for deleting the std::unique_ptr<uv::rwlock>.
-Style::~Style() {}
+Style::~Style() {
+}
+
+void Style::updateSources(Map& map,
+ Environment& env,
+ uv::worker& worker,
+ TexturePool& texturePool,
+ std::function<void()> callback) {
+ // First, disable all existing sources.
+ for (const auto& source : activeSources) {
+ source->enabled = false;
+ }
+
+ // Then, reenable all of those that we actually use when drawing this layer.
+ if (!layers) {
+ return;
+ }
+ for (const util::ptr<StyleLayer> &layer : layers->layers) {
+ if (!layer) continue;
+ if (layer->bucket && layer->bucket->style_source) {
+ (*activeSources.emplace(layer->bucket->style_source).first)->enabled = true;
+ }
+ }
+
+ // Then, construct or destroy the actual source object, depending on enabled state.
+ for (const auto& source : activeSources) {
+ if (source->enabled) {
+ if (!source->source) {
+ source->source = std::make_shared<Source>(source->info);
+ source->source->load(map, env);
+ }
+ } else {
+ source->source.reset();
+ }
+ }
+
+ // Finally, remove all sources that are disabled.
+ util::erase_if(activeSources, [](util::ptr<StyleSource> source){
+ return !source->enabled;
+ });
+
+ // Allow the sprite atlas to potentially pull new sprite images if needed.
+ const float pixelRatio = map.getState().getPixelRatio();
+ if (!sprite || sprite->pixelRatio != pixelRatio) {
+ sprite = Sprite::Create(sprite_url, pixelRatio, env);
+ spriteAtlas->resize(pixelRatio);
+ spriteAtlas->setSprite(sprite);
+ }
+
+ glyphStore->setURL(util::mapbox::normalizeGlyphsURL(glyph_url, map.getAccessToken()));
+
+ for (const auto &source : activeSources) {
+ source->source->update(map, env, worker, shared_from_this(),
+ *glyphAtlas, *glyphStore,
+ *spriteAtlas, sprite,
+ texturePool, callback);
+ }
+}
void Style::updateProperties(float z, std::chrono::steady_clock::time_point now) {
uv::writelock lock(mtx);
@@ -41,10 +111,6 @@ void Style::updateProperties(float z, std::chrono::steady_clock::time_point now)
}
}
-const std::string &Style::getSpriteURL() const {
- return sprite_url;
-}
-
void Style::setDefaultTransitionDuration(std::chrono::steady_clock::duration duration) {
defaultTransition.duration = duration;
}
@@ -64,7 +130,6 @@ bool Style::hasTransitions() const {
return false;
}
-
void Style::loadJSON(const uint8_t *const data) {
uv::writelock lock(mtx);
diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp
index 4de827a38c..04979b3377 100644
--- a/src/mbgl/style/style.hpp
+++ b/src/mbgl/style/style.hpp
@@ -15,23 +15,39 @@
#include <vector>
#include <set>
#include <chrono>
+#include <memory>
namespace mbgl {
+class Map;
+class Environment;
+class GlyphAtlas;
+class GlyphStore;
+class SpriteAtlas;
class Sprite;
+class LineAtlas;
class StyleLayer;
class StyleLayerGroup;
+class TexturePool;
-class Style : public util::noncopyable {
+class Style : public util::noncopyable,
+ public std::enable_shared_from_this<Style> {
public:
struct exception : std::runtime_error { exception(const char *msg) : std::runtime_error(msg) {} };
- Style();
+ Style(Environment&);
~Style();
void loadJSON(const uint8_t *const data);
size_t layerCount() const;
+
+ void updateSources(Map&,
+ Environment&,
+ uv::worker&,
+ TexturePool&,
+ std::function<void()> callback);
+
void updateProperties(float z, std::chrono::steady_clock::time_point now);
void setDefaultTransitionDuration(std::chrono::steady_clock::duration duration = std::chrono::steady_clock::duration::zero());
@@ -39,19 +55,24 @@ public:
bool hasTransitions() const;
- const std::string &getSpriteURL() const;
+ const std::unique_ptr<GlyphAtlas> glyphAtlas;
+ const std::unique_ptr<SpriteAtlas> spriteAtlas;
+ const std::unique_ptr<LineAtlas> lineAtlas;
util::ptr<StyleLayerGroup> layers;
- std::vector<std::string> appliedClasses;
- std::string glyph_url;
+ std::set<util::ptr<StyleSource>> activeSources;
std::string base;
private:
std::string sprite_url;
+ std::string glyph_url;
PropertyTransition defaultTransition;
bool initial_render_complete = false;
std::unique_ptr<uv::rwlock> mtx;
ZoomHistory zoomHistory;
+
+ const std::unique_ptr<GlyphStore> glyphStore;
+ util::ptr<Sprite> sprite;
};
}