diff options
-rw-r--r-- | src/mbgl/annotation/sprite_store.cpp | 27 | ||||
-rw-r--r-- | src/mbgl/annotation/sprite_store.hpp | 2 | ||||
-rw-r--r-- | test/annotations/sprite_store.cpp | 22 |
3 files changed, 43 insertions, 8 deletions
diff --git a/src/mbgl/annotation/sprite_store.cpp b/src/mbgl/annotation/sprite_store.cpp index 37acace87f..270348abab 100644 --- a/src/mbgl/annotation/sprite_store.cpp +++ b/src/mbgl/annotation/sprite_store.cpp @@ -1,5 +1,7 @@ #include <mbgl/annotation/sprite_store.hpp> +#include <mbgl/platform/log.hpp> + #include <mbgl/util/exception.hpp> namespace mbgl { @@ -8,20 +10,29 @@ SpriteStore::SpriteStore(const float pixelRatio_) : pixelRatio(pixelRatio_) { } void SpriteStore::setSprite(const std::string& name, std::shared_ptr<const SpriteImage> sprite) { - if (sprite && sprite->pixelRatio != pixelRatio) { - throw util::SpriteImageException("Sprite image has wrong pixel ratio"); - } std::lock_guard<std::mutex> lock(mutex); + _setSprite(name, sprite); +} + +void SpriteStore::_setSprite(const std::string& name, + const std::shared_ptr<const SpriteImage>& sprite) { if (sprite) { - sprites.emplace(name, sprite); - } else { - sprites.erase(name); + if (sprite->pixelRatio == pixelRatio) { + sprites.emplace(name, sprite); + dirty.emplace(name, sprite); + return; + } else { + Log::Warning(Event::Sprite, "Sprite image has wrong pixel ratio"); + } + } + if (sprites.erase(name) > 0) { + dirty.emplace(name, nullptr); } - dirty.emplace(name, sprite); } void SpriteStore::removeSprite(const std::string& name) { - setSprite(name); + std::lock_guard<std::mutex> lock(mutex); + _setSprite(name); } std::shared_ptr<const SpriteImage> SpriteStore::getSprite(const std::string& name) { diff --git a/src/mbgl/annotation/sprite_store.hpp b/src/mbgl/annotation/sprite_store.hpp index d8c8372f9d..80ea6bbe7f 100644 --- a/src/mbgl/annotation/sprite_store.hpp +++ b/src/mbgl/annotation/sprite_store.hpp @@ -38,6 +38,8 @@ public: const float pixelRatio; private: + void _setSprite(const std::string&, const std::shared_ptr<const SpriteImage>& = nullptr); + // Lock for sprites and dirty maps. std::mutex mutex; diff --git a/test/annotations/sprite_store.cpp b/test/annotations/sprite_store.cpp index 287efaedab..b22c46be48 100644 --- a/test/annotations/sprite_store.cpp +++ b/test/annotations/sprite_store.cpp @@ -1,4 +1,5 @@ #include "../fixtures/util.hpp" +#include "../fixtures/fixture_log_observer.hpp" #include <mbgl/annotation/sprite_store.hpp> @@ -49,3 +50,24 @@ TEST(Annotations, SpriteStore) { }), store.getDirty()); EXPECT_EQ(Sprites(), store.getDirty()); } + + +TEST(Annotations, SpriteStoreWrongPixelRatio) { + FixtureLog log; + + const auto sprite1 = std::make_shared<SpriteImage>(8, 8, 1, std::string(8 * 8 * 4, '\0')); + + using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>; + SpriteStore store(2); + + // Adding mismatched sprite image + store.setSprite("one", sprite1); + EXPECT_EQ(Sprites({}), store.getDirty()); + + EXPECT_EQ(1u, log.count({ + EventSeverity::Warning, + Event::Sprite, + int64_t(-1), + "Sprite image has wrong pixel ratio", + })); +} |