diff options
author | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-05-12 23:19:00 +0300 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-05-12 13:19:00 -0700 |
commit | cc9f040a2d35293c51dcc5be9c7affea7f1263bd (patch) | |
tree | de62a5610e719f5bfe07226c3382d4d2a5e17530 /src/mbgl/style | |
parent | c80f3e9d29d1c26ccc88ef30f8f17329c9bfb1b7 (diff) | |
download | qtlocation-mapboxgl-cc9f040a2d35293c51dcc5be9c7affea7f1263bd.tar.gz |
[core] Split style image collection from SpriteAtlas
Diffstat (limited to 'src/mbgl/style')
-rw-r--r-- | src/mbgl/style/image.cpp | 38 | ||||
-rw-r--r-- | src/mbgl/style/image_impl.cpp | 22 | ||||
-rw-r--r-- | src/mbgl/style/image_impl.hpp | 25 | ||||
-rw-r--r-- | src/mbgl/style/layers/symbol_layer_impl.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/style/observer.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/style/style.cpp | 41 | ||||
-rw-r--r-- | src/mbgl/style/style.hpp | 15 |
7 files changed, 123 insertions, 23 deletions
diff --git a/src/mbgl/style/image.cpp b/src/mbgl/style/image.cpp index 4c0c6a859b..c5fecec2b2 100644 --- a/src/mbgl/style/image.cpp +++ b/src/mbgl/style/image.cpp @@ -1,21 +1,35 @@ #include <mbgl/style/image.hpp> +#include <mbgl/style/image_impl.hpp> #include <mbgl/util/exception.hpp> namespace mbgl { namespace style { -Image::Image(PremultipliedImage&& image_, - const float pixelRatio_, - bool sdf_) - : image(std::move(image_)), - pixelRatio(pixelRatio_), - sdf(sdf_) { - - if (!image.valid()) { - throw util::SpriteImageException("Sprite image dimensions may not be zero"); - } else if (pixelRatio <= 0) { - throw util::SpriteImageException("Sprite pixelRatio may not be <= 0"); - } +Image::Image(PremultipliedImage &&image, + const float pixelRatio, + bool sdf) + : impl(std::make_shared<Impl>(std::move(image), pixelRatio, sdf)) { +} + +PremultipliedImage& Image::getImage() const { + assert(impl); + return impl->image; +} + +bool Image::isSdf() const { + return impl->sdf; +} + +float Image::getPixelRatio() const { + return impl->pixelRatio; +} + +float Image::getWidth() const { + return impl->getWidth(); +} + +float Image::getHeight() const { + return impl->getHeight(); } } // namespace style diff --git a/src/mbgl/style/image_impl.cpp b/src/mbgl/style/image_impl.cpp new file mode 100644 index 0000000000..910bffa905 --- /dev/null +++ b/src/mbgl/style/image_impl.cpp @@ -0,0 +1,22 @@ +#include <mbgl/style/image_impl.hpp> +#include <mbgl/util/exception.hpp> + +namespace mbgl { +namespace style { + +Image::Impl::Impl(PremultipliedImage&& image_, + const float pixelRatio_, + bool sdf_) + : image(std::move(image_)), + pixelRatio(pixelRatio_), + sdf(sdf_) { + + if (!image.valid()) { + throw util::SpriteImageException("Sprite image dimensions may not be zero"); + } else if (pixelRatio <= 0) { + throw util::SpriteImageException("Sprite pixelRatio may not be <= 0"); + } +} + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/image_impl.hpp b/src/mbgl/style/image_impl.hpp new file mode 100644 index 0000000000..dce4a6e4c0 --- /dev/null +++ b/src/mbgl/style/image_impl.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include <mbgl/style/image.hpp> + +namespace mbgl { +namespace style { + +class Image::Impl { +public: + Impl(PremultipliedImage&&, float pixelRatio, bool sdf = false); + + PremultipliedImage image; + + // Pixel ratio of the sprite image. + const float pixelRatio; + + // Whether this image should be interpreted as a signed distance field icon. + const bool sdf; + + float getWidth() const { return image.size.width / pixelRatio; } + float getHeight() const { return image.size.height / pixelRatio; } +}; + +} // namespace style +} // namespace mbgl diff --git a/src/mbgl/style/layers/symbol_layer_impl.hpp b/src/mbgl/style/layers/symbol_layer_impl.hpp index 5e04834583..a20d001e0e 100644 --- a/src/mbgl/style/layers/symbol_layer_impl.hpp +++ b/src/mbgl/style/layers/symbol_layer_impl.hpp @@ -1,6 +1,5 @@ #pragma once -#include <mbgl/sprite/sprite_atlas.hpp> #include <mbgl/style/layer_impl.hpp> #include <mbgl/style/layers/symbol_layer.hpp> #include <mbgl/style/layers/symbol_layer_properties.hpp> diff --git a/src/mbgl/style/observer.hpp b/src/mbgl/style/observer.hpp index 60432334e2..77d97685be 100644 --- a/src/mbgl/style/observer.hpp +++ b/src/mbgl/style/observer.hpp @@ -1,7 +1,7 @@ #pragma once #include <mbgl/text/glyph_atlas_observer.hpp> -#include <mbgl/sprite/sprite_atlas_observer.hpp> +#include <mbgl/sprite/sprite_loader_observer.hpp> #include <mbgl/style/source_observer.hpp> #include <mbgl/map/update.hpp> @@ -11,7 +11,7 @@ namespace mbgl { namespace style { class Observer : public GlyphAtlasObserver, - public SpriteAtlasObserver, + public SpriteLoaderObserver, public SourceObserver { public: virtual void onUpdate(Update) {} diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index 6af876d740..0dcb110443 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -17,6 +17,8 @@ #include <mbgl/style/transition_options.hpp> #include <mbgl/style/class_dictionary.hpp> #include <mbgl/sprite/sprite_atlas.hpp> +#include <mbgl/sprite/sprite_image_collection.hpp> +#include <mbgl/sprite/sprite_loader.hpp> #include <mbgl/text/glyph_atlas.hpp> #include <mbgl/geometry/line_atlas.hpp> #include <mbgl/renderer/update_parameters.hpp> @@ -72,13 +74,14 @@ Style::Style(Scheduler& scheduler_, FileSource& fileSource_, float pixelRatio) : scheduler(scheduler_), fileSource(fileSource_), glyphAtlas(std::make_unique<GlyphAtlas>(Size{ 2048, 2048 }, fileSource)), + spriteLoader(std::make_unique<SpriteLoader>(pixelRatio)), spriteAtlas(std::make_unique<SpriteAtlas>(Size{ 1024, 1024 }, pixelRatio)), lineAtlas(std::make_unique<LineAtlas>(Size{ 256, 512 })), light(std::make_unique<Light>()), renderLight(light->impl), observer(&nullObserver) { glyphAtlas->setObserver(this); - spriteAtlas->setObserver(this); + spriteLoader->setObserver(this); light->setObserver(this); } @@ -160,7 +163,7 @@ void Style::setJSON(const std::string& json) { setLight(std::make_unique<Light>(parser.light)); glyphAtlas->setURL(parser.glyphURL); - spriteAtlas->load(parser.spriteURL, scheduler, fileSource); + spriteLoader->load(parser.spriteURL, scheduler, fileSource); loaded = true; @@ -539,6 +542,24 @@ bool Style::isLoaded() const { return true; } +void Style::addImage(const std::string& id, std::unique_ptr<style::Image> image) { + addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) { + spriteAtlas->addImage(id, std::make_unique<style::Image>(added)); + observer->onUpdate(Update::Repaint); + }); +} + +void Style::removeImage(const std::string& id) { + removeSpriteImage(spriteImages, id, [&] () { + spriteAtlas->removeImage(id); + observer->onUpdate(Update::Repaint); + }); +} + +const style::Image* Style::getImage(const std::string& id) const { + return spriteAtlas->getImage(id); +} + RenderData Style::getRenderData(MapDebugOptions debugOptions, float angle) const { RenderData result; @@ -755,8 +776,20 @@ void Style::onTileError(RenderSource& source, const OverscaledTileID& tileID, st observer->onResourceError(error); } -void Style::onSpriteLoaded() { - observer->onSpriteLoaded(); +void Style::onSpriteLoaded(SpriteLoader::Images&& images) { + // Add images to collection + Images addedImages; + for (auto& entry : images) { + addSpriteImage(spriteImages, entry.first, std::move(entry.second), [&] (style::Image& added) { + addedImages.emplace(entry.first, std::make_unique<Image>(added)); + }); + } + + // Update render sprite atlas + spriteAtlas->onSpriteLoaded(std::move(addedImages)); + + // Update observer + observer->onSpriteLoaded(std::move(images)); observer->onUpdate(Update::Repaint); // For *-pattern properties. } diff --git a/src/mbgl/style/style.hpp b/src/mbgl/style/style.hpp index 04d3a11d55..5953d823d9 100644 --- a/src/mbgl/style/style.hpp +++ b/src/mbgl/style/style.hpp @@ -11,7 +11,7 @@ #include <mbgl/renderer/render_layer.hpp> #include <mbgl/renderer/render_light.hpp> #include <mbgl/text/glyph_atlas_observer.hpp> -#include <mbgl/sprite/sprite_atlas_observer.hpp> +#include <mbgl/sprite/sprite_loader_observer.hpp> #include <mbgl/map/mode.hpp> #include <mbgl/map/zoom_history.hpp> @@ -31,6 +31,7 @@ namespace mbgl { class FileSource; class GlyphAtlas; class SpriteAtlas; +class SpriteLoader; class LineAtlas; class RenderData; class TransformState; @@ -44,7 +45,7 @@ class Layer; class QueryParameters; class Style : public GlyphAtlasObserver, - public SpriteAtlasObserver, + public SpriteLoaderObserver, public SourceObserver, public RenderSourceObserver, public LayerObserver, @@ -106,6 +107,10 @@ public: Light* getLight() const; const RenderLight& getRenderLight() const; + const style::Image* getImage(const std::string&) const; + void addImage(const std::string&, std::unique_ptr<style::Image>); + void removeImage(const std::string&); + RenderData getRenderData(MapDebugOptions, float angle) const; std::vector<Feature> queryRenderedFeatures(const ScreenLineString& geometry, @@ -120,6 +125,7 @@ public: Scheduler& scheduler; FileSource& fileSource; std::unique_ptr<GlyphAtlas> glyphAtlas; + std::unique_ptr<SpriteLoader> spriteLoader; std::unique_ptr<SpriteAtlas> spriteAtlas; std::unique_ptr<LineAtlas> lineAtlas; @@ -152,8 +158,9 @@ private: void onGlyphsLoaded(const FontStack&, const GlyphRange&) override; void onGlyphsError(const FontStack&, const GlyphRange&, std::exception_ptr) override; - // SpriteStoreObserver implementation. - void onSpriteLoaded() override; + // SpriteLoaderObserver implementation. + std::unordered_map<std::string, std::unique_ptr<style::Image>> spriteImages; + void onSpriteLoaded(SpriteLoaderObserver::Images&&) override; void onSpriteError(std::exception_ptr) override; // SourceObserver implementation. |