Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when user updates icons with images of different sizes #2989

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/mbgl/renderer/image_atlas.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <mbgl/renderer/image_atlas.hpp>
#include <mbgl/renderer/image_manager.hpp>
#include <mbgl/util/logging.hpp>

#include <mapbox/shelf-pack.hpp>

Expand Down Expand Up @@ -63,7 +64,44 @@ void populateImagePatches(ImagePositions& imagePositions,
const auto updatedImage = imageManager.getSharedImage(name);
if (updatedImage == nullptr) continue;

patches.emplace_back(*updatedImage, position.paddedRect);
Immutable<style::Image::Impl> imagePatch = *updatedImage;

// The max patch size that fits the current Atlas is the paddedRect size and the padding
assert(position.paddedRect.w > ImagePosition::padding * 2);
assert(position.paddedRect.h > ImagePosition::padding * 2);
uint32_t maxPatchWidth = position.paddedRect.w - ImagePosition::padding * 2;
uint32_t maxPatchHeight = position.paddedRect.h - ImagePosition::padding * 2;
if (maxPatchWidth < imagePatch->image.size.width || maxPatchHeight < imagePatch->image.size.height) {
// imagePositions are created in makeImageAtlas
// User can update the image. e.g. an Android call to
// MapLibreMap.getStyle.addImage(imageId, imageBitmap), which will call ImageManager.updateImage
// If the updated image is larger than the previous image then position.paddedRect area in the atlas
// won't fit the new image. ImageManager is unaware of the the atlas packing.
// This code simply prints an error message and resizes the image to fit the atlas to avoid crashes
// A better solution (potentially expensive) is to repack the atlas: this requires keeping the
// previous atlas image and detect when a repack is required.
// Another possibility is to simply throw an exception and requires the user to provide different
// IDs for images with different sizes
const auto& imageImpl = *updatedImage->get();
Log::Error(Event::General,
imageImpl.id + " does not fit the atlas. " + imageImpl.id +
" will be resized from:" + std::to_string(imageImpl.image.size.width) + "x" +
std::to_string(imageImpl.image.size.height) + " to:" + std::to_string(maxPatchWidth) +
"x" + std::to_string(maxPatchHeight));
auto resizedImage = imagePatch->image.clone();
auto newWidth = std::min(maxPatchWidth, imagePatch->image.size.width);
auto newHeight = std::min(maxPatchHeight, imagePatch->image.size.height);
resizedImage.resize({newWidth, newHeight});
auto mutableImagePatch = makeMutable<style::Image::Impl>(imageImpl.id,
std::move(resizedImage),
imageImpl.pixelRatio,
imageImpl.sdf,
style::ImageStretches(),
style::ImageStretches());
imagePatch = std::move(mutableImagePatch);
}

patches.emplace_back(imagePatch, position.paddedRect);
position.version = version;
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/mbgl/tile/geometry_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,14 @@ void GeometryTileRenderData::upload(gfx::UploadPass& uploadPass) {

if (atlasTextures->icon && !imagePatches.empty()) {
for (const auto& imagePatch : imagePatches) { // patch updated images.
uint32_t xDest = imagePatch.paddedRect.x + ImagePosition::padding;
uint32_t yDest = imagePatch.paddedRect.y + ImagePosition::padding;
assert(xDest + imagePatch.image->image.size.width <= atlasTextures->icon->getSize().width);
assert(yDest + imagePatch.image->image.size.height <= atlasTextures->icon->getSize().height);
#if MLN_DRAWABLE_RENDERER
atlasTextures->icon->uploadSubRegion(imagePatch.image->image,
imagePatch.paddedRect.x + ImagePosition::padding,
imagePatch.paddedRect.y + ImagePosition::padding);
atlasTextures->icon->uploadSubRegion(imagePatch.image->image, xDest, yDest);
#else
uploadPass.updateTextureSub(*atlasTextures->icon,
imagePatch.image->image,
imagePatch.paddedRect.x + ImagePosition::padding,
imagePatch.paddedRect.y + ImagePosition::padding);
uploadPass.updateTextureSub(*atlasTextures->icon, imagePatch.image->image, xDest, yDest);
#endif
}
imagePatches.clear();
Expand Down
Loading