From 8fb1a81c46f4358d731712c16868aa1108d5d34c Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 12 May 2017 12:32:42 -0700 Subject: [core] Immutable for Image --- src/mbgl/annotation/annotation_manager.cpp | 7 ++++--- src/mbgl/sprite/sprite_atlas.cpp | 21 +++++++++++---------- src/mbgl/sprite/sprite_atlas.hpp | 8 ++++---- src/mbgl/style/image.cpp | 5 ++--- src/mbgl/style/style.cpp | 5 +++-- 5 files changed, 24 insertions(+), 22 deletions(-) (limited to 'src/mbgl') diff --git a/src/mbgl/annotation/annotation_manager.cpp b/src/mbgl/annotation/annotation_manager.cpp index 96cdd96750..8e75f8e63f 100644 --- a/src/mbgl/annotation/annotation_manager.cpp +++ b/src/mbgl/annotation/annotation_manager.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -192,7 +193,7 @@ void AnnotationManager::removeTile(AnnotationTile& tile) { void AnnotationManager::addImage(const std::string& id, std::unique_ptr image) { addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) { - spriteAtlas.addImage(id, std::make_unique(added)); + spriteAtlas.addImage(id, added.impl); }); } @@ -203,8 +204,8 @@ void AnnotationManager::removeImage(const std::string& id) { } double AnnotationManager::getTopOffsetPixelsForImage(const std::string& id) { - const style::Image* image = spriteAtlas.getImage(id); - return image ? -(image->getImage().size.height / image->getPixelRatio()) / 2 : 0; + const style::Image::Impl* impl = spriteAtlas.getImage(id); + return impl ? -(impl->image.size.height / impl->pixelRatio) / 2 : 0; } } // namespace mbgl diff --git a/src/mbgl/sprite/sprite_atlas.cpp b/src/mbgl/sprite/sprite_atlas.cpp index bef74b7ce5..f677f7bb60 100644 --- a/src/mbgl/sprite/sprite_atlas.cpp +++ b/src/mbgl/sprite/sprite_atlas.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -14,11 +15,11 @@ namespace mbgl { SpriteAtlasElement::SpriteAtlasElement(Rect rect_, - const style::Image& image, + const style::Image::Impl& image, Size size_, float pixelRatio) : pos(std::move(rect_)), - sdf(image.isSdf()), - relativePixelRatio(image.getPixelRatio() / pixelRatio), + sdf(image.sdf), + relativePixelRatio(image.pixelRatio / pixelRatio), width(image.getWidth()), height(image.getHeight()) { @@ -45,7 +46,7 @@ void SpriteAtlas::onSpriteLoaded(Images&& result) { markAsLoaded(); for (auto& pair : result) { - addImage(pair.first, std::move(pair.second)); + addImage(pair.first, pair.second->impl); } for (auto requestor : requestors) { @@ -54,7 +55,7 @@ void SpriteAtlas::onSpriteLoaded(Images&& result) { requestors.clear(); } -void SpriteAtlas::addImage(const std::string& id, std::unique_ptr image_) { +void SpriteAtlas::addImage(const std::string& id, Immutable image_) { icons.clear(); auto it = entries.find(id); @@ -66,7 +67,7 @@ void SpriteAtlas::addImage(const std::string& id, std::unique_ptr Entry& entry = it->second; // There is already a sprite with that name in our store. - assert(entry.image->getImage().size == image_->getImage().size); + assert(entry.image->image.size == image_->image.size); entry.image = std::move(image_); @@ -98,7 +99,7 @@ void SpriteAtlas::removeImage(const std::string& id) { entries.erase(it); } -const style::Image* SpriteAtlas::getImage(const std::string& id) const { +const style::Image::Impl* SpriteAtlas::getImage(const std::string& id) const { const auto it = entries.find(id); if (it != entries.end()) { return it->second.image.get(); @@ -152,8 +153,8 @@ optional SpriteAtlas::getImage(const std::string& id, }; } - const uint16_t pixelWidth = std::ceil(entry.image->getImage().size.width / pixelRatio); - const uint16_t pixelHeight = std::ceil(entry.image->getImage().size.height / pixelRatio); + const uint16_t pixelWidth = std::ceil(entry.image->image.size.width / pixelRatio); + const uint16_t pixelHeight = std::ceil(entry.image->image.size.height / pixelRatio); // Increase to next number divisible by 4, but at least 1. // This is so we can scale down the texture coordinates and pack them @@ -188,7 +189,7 @@ void SpriteAtlas::copy(const Entry& entry, optional> Entry::*entr image.fill(0); } - const PremultipliedImage& src = entry.image->getImage(); + const PremultipliedImage& src = entry.image->image; const Rect& rect = *(entry.*entryRect); const uint32_t padding = 1; diff --git a/src/mbgl/sprite/sprite_atlas.hpp b/src/mbgl/sprite/sprite_atlas.hpp index 7ae583dcba..4e03c32b30 100644 --- a/src/mbgl/sprite/sprite_atlas.hpp +++ b/src/mbgl/sprite/sprite_atlas.hpp @@ -20,7 +20,7 @@ class Context; class SpriteAtlasElement { public: - SpriteAtlasElement(Rect, const style::Image&, Size size, float pixelRatio); + SpriteAtlasElement(Rect, const style::Image::Impl&, Size size, float pixelRatio); Rect pos; bool sdf; @@ -60,8 +60,8 @@ public: void dumpDebugLogs() const; - const style::Image* getImage(const std::string&) const; - void addImage(const std::string&, std::unique_ptr); + const style::Image::Impl* getImage(const std::string&) const; + void addImage(const std::string&, Immutable); void removeImage(const std::string&); void getIcons(IconRequestor& requestor); @@ -91,7 +91,7 @@ private: bool loaded = false; struct Entry { - std::unique_ptr image; + Immutable image; // One sprite image might be used as both an icon image and a pattern image. If so, // it must have two distinct entries in the texture. The one for the icon image has diff --git a/src/mbgl/style/image.cpp b/src/mbgl/style/image.cpp index c5fecec2b2..606d9907a4 100644 --- a/src/mbgl/style/image.cpp +++ b/src/mbgl/style/image.cpp @@ -8,11 +8,10 @@ namespace style { Image::Image(PremultipliedImage &&image, const float pixelRatio, bool sdf) - : impl(std::make_shared(std::move(image), pixelRatio, sdf)) { + : impl(makeMutable(std::move(image), pixelRatio, sdf)) { } -PremultipliedImage& Image::getImage() const { - assert(impl); +const PremultipliedImage& Image::getImage() const { return impl->image; } diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index ae9ed97427..256949af3b 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -544,7 +544,7 @@ bool Style::isLoaded() const { void Style::addImage(const std::string& id, std::unique_ptr image) { addSpriteImage(spriteImages, id, std::move(image), [&](style::Image& added) { - spriteAtlas->addImage(id, std::make_unique(added)); + spriteAtlas->addImage(id, added.impl); observer->onUpdate(Update::Repaint); }); } @@ -557,7 +557,8 @@ void Style::removeImage(const std::string& id) { } const style::Image* Style::getImage(const std::string& id) const { - return spriteAtlas->getImage(id); + auto it = spriteImages.find(id); + return it == spriteImages.end() ? nullptr : it->second.get(); } RenderData Style::getRenderData(MapDebugOptions debugOptions, float angle) const { -- cgit v1.2.1