diff options
author | Ansis Brammanis <ansis.brammanis@gmail.com> | 2019-04-02 12:57:45 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-02 12:57:45 -0400 |
commit | 2455275bf47e1671a92eb2c1296c6b48f11f6b2e (patch) | |
tree | ad027661edc899feae56ad8b50ebd208f7a36d12 /src/mbgl/renderer/image_atlas.cpp | |
parent | 805b1402452da74042b995d693c48ee4b79ae337 (diff) | |
download | qtlocation-mapboxgl-2455275bf47e1671a92eb2c1296c6b48f11f6b2e.tar.gz |
add onStyleImageMissing to allow dynamically loaded or generated images (#14253)
Also make `Style#updateImage(...)` much faster when the image doesn't change size. This can be useful for asynchronously generating images.
Diffstat (limited to 'src/mbgl/renderer/image_atlas.cpp')
-rw-r--r-- | src/mbgl/renderer/image_atlas.cpp | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/src/mbgl/renderer/image_atlas.cpp b/src/mbgl/renderer/image_atlas.cpp index b39c788ced..282f135ac9 100644 --- a/src/mbgl/renderer/image_atlas.cpp +++ b/src/mbgl/renderer/image_atlas.cpp @@ -1,4 +1,6 @@ #include <mbgl/renderer/image_atlas.hpp> +#include <mbgl/gfx/context.hpp> +#include <mbgl/renderer/image_manager.hpp> #include <mapbox/shelf-pack.hpp> @@ -6,14 +8,15 @@ namespace mbgl { static constexpr uint32_t padding = 1; -ImagePosition::ImagePosition(const mapbox::Bin& bin, const style::Image::Impl& image) +ImagePosition::ImagePosition(const mapbox::Bin& bin, const style::Image::Impl& image, uint32_t version_) : pixelRatio(image.pixelRatio), textureRect( bin.x + padding, bin.y + padding, bin.w - padding * 2, bin.h - padding * 2 - ) { + ), + version(version_) { } const mapbox::Bin& _packImage(mapbox::ShelfPack& pack, const style::Image::Impl& image, ImageAtlas& resultImage, ImageType imageType) { @@ -49,7 +52,30 @@ const mapbox::Bin& _packImage(mapbox::ShelfPack& pack, const style::Image::Impl& return bin; } -ImageAtlas makeImageAtlas(const ImageMap& icons, const ImageMap& patterns) { +void ImageAtlas::patchUpdatedImages(gfx::Context& context, gfx::Texture& atlasTexture, const ImageManager& imageManager) { + for (auto& updatedImageVersion : imageManager.updatedImageVersions) { + auto iconPosition = iconPositions.find(updatedImageVersion.first); + if (iconPosition != iconPositions.end()) { + patchUpdatedImage(context, atlasTexture, iconPosition->second, imageManager, updatedImageVersion.first, updatedImageVersion.second); + } + auto patternPosition = patternPositions.find(updatedImageVersion.first); + if (patternPosition != patternPositions.end()) { + patchUpdatedImage(context, atlasTexture, patternPosition->second, imageManager, updatedImageVersion.first, updatedImageVersion.second); + } + } +} + +void ImageAtlas::patchUpdatedImage(gfx::Context& context, gfx::Texture& atlasTexture, ImagePosition& position, const ImageManager& imageManager, const std::string& name, uint16_t version) { + if (position.version == version) return; + + auto updatedImage = imageManager.getImage(name); + if (updatedImage == nullptr) return; + + context.updateTextureSub(atlasTexture, updatedImage->image, position.textureRect.x, position.textureRect.y); + position.version = version; +} + +ImageAtlas makeImageAtlas(const ImageMap& icons, const ImageMap& patterns, const std::unordered_map<std::string, uint32_t>& versionMap) { ImageAtlas result; mapbox::ShelfPack::ShelfPackOptions options; @@ -59,13 +85,17 @@ ImageAtlas makeImageAtlas(const ImageMap& icons, const ImageMap& patterns) { for (const auto& entry : icons) { const style::Image::Impl& image = *entry.second; const mapbox::Bin& bin = _packImage(pack, image, result, ImageType::Icon); - result.iconPositions.emplace(image.id, ImagePosition { bin, image }); + auto it = versionMap.find(entry.first); + auto version = it != versionMap.end() ? it->second : 0; + result.iconPositions.emplace(image.id, ImagePosition { bin, image, version }); } for (const auto& entry : patterns) { const style::Image::Impl& image = *entry.second; const mapbox::Bin& bin = _packImage(pack, image, result, ImageType::Pattern); - result.patternPositions.emplace(image.id, ImagePosition { bin, image }); + auto it = versionMap.find(entry.first); + auto version = it != versionMap.end() ? it->second : 0; + result.patternPositions.emplace(image.id, ImagePosition { bin, image, version }); } pack.shrink(); |