1
1
#include < mbgl/renderer/image_atlas.hpp>
2
2
#include < mbgl/renderer/image_manager.hpp>
3
+ #include < mbgl/gfx/context.hpp>
4
+ #include < mbgl/util/hash.hpp>
3
5
4
6
#include < mapbox/shelf-pack.hpp>
5
7
6
8
namespace mbgl {
7
9
8
- static constexpr uint32_t padding = 1 ;
9
-
10
- ImagePosition::ImagePosition (const mapbox::Bin& bin, const style::Image::Impl& image, uint32_t version_)
11
- : pixelRatio(image.pixelRatio),
10
+ ImagePosition::ImagePosition (const mapbox::Bin& bin, const style::Image::Impl& image, uint32_t version_, std::optional<gfx::TextureHandle> handle_)
11
+ : handle(handle_),
12
+ pixelRatio (image.pixelRatio),
12
13
paddedRect(bin.x, bin.y, bin.w, bin.h),
13
14
version(version_),
14
15
stretchX(image.stretchX),
@@ -17,35 +18,6 @@ ImagePosition::ImagePosition(const mapbox::Bin& bin, const style::Image::Impl& i
17
18
textFitWidth(image.textFitWidth),
18
19
textFitHeight(image.textFitHeight) {}
19
20
20
- namespace {
21
-
22
- const mapbox::Bin& _packImage (mapbox::ShelfPack& pack,
23
- const style::Image::Impl& image,
24
- ImageAtlas& resultImage,
25
- ImageType imageType) {
26
- const mapbox::Bin& bin = *pack.packOne (
27
- -1 , image.image .size .width + 2 * padding, image.image .size .height + 2 * padding);
28
-
29
- resultImage.image .resize ({static_cast <uint32_t >(pack.width ()), static_cast <uint32_t >(pack.height ())});
30
-
31
- PremultipliedImage::copy (
32
- image.image , resultImage.image , {0 , 0 }, {bin.x + padding, bin.y + padding}, image.image .size );
33
-
34
- if (imageType == ImageType::Pattern) {
35
- const uint32_t x = bin.x + padding;
36
- const uint32_t y = bin.y + padding;
37
- const uint32_t w = image.image .size .width ;
38
- const uint32_t h = image.image .size .height ;
39
-
40
- // Add 1 pixel wrapped padding on each side of the image.
41
- PremultipliedImage::copy (image.image , resultImage.image , {0 , h - 1 }, {x, y - 1 }, {w, 1 }); // T
42
- PremultipliedImage::copy (image.image , resultImage.image , {0 , 0 }, {x, y + h}, {w, 1 }); // B
43
- PremultipliedImage::copy (image.image , resultImage.image , {w - 1 , 0 }, {x - 1 , y}, {1 , h}); // L
44
- PremultipliedImage::copy (image.image , resultImage.image , {0 , 0 }, {x + w, y}, {1 , h}); // R
45
- }
46
- return bin;
47
- }
48
-
49
21
void populateImagePatches (ImagePositions& imagePositions,
50
22
const ImageManager& imageManager,
51
23
std::vector<ImagePatch>& /* out*/ patches) {
@@ -69,46 +41,42 @@ void populateImagePatches(ImagePositions& imagePositions,
69
41
}
70
42
}
71
43
72
- } // namespace
73
-
74
- std::vector<ImagePatch> ImageAtlas::getImagePatchesAndUpdateVersions (const ImageManager& imageManager) {
75
- std::vector<ImagePatch> imagePatches;
76
- populateImagePatches (iconPositions, imageManager, imagePatches);
77
- populateImagePatches (patternPositions, imageManager, imagePatches);
78
- return imagePatches;
79
- }
80
-
81
- ImageAtlas makeImageAtlas (const ImageMap& icons, const ImageMap& patterns, const ImageVersionMap& versionMap) {
82
- ImageAtlas result;
83
-
84
- mapbox::ShelfPack::ShelfPackOptions options;
85
- options.autoResize = true ;
86
- mapbox::ShelfPack pack (0 , 0 , options);
87
-
88
- result.iconPositions .reserve (icons.size ());
44
+ ImagePositions uploadIcons (const ImageMap& icons, const ImageVersionMap& versionMap) {
45
+ ImagePositions iconPositions;
46
+ iconPositions.reserve (icons.size ());
89
47
90
48
for (const auto & entry : icons) {
91
49
const style::Image::Impl& image = *entry.second ;
92
- const mapbox::Bin& bin = _packImage (pack, image, result, ImageType::Icon);
50
+ auto imageHash = util::hash (image.id );
51
+ int32_t uniqueId = static_cast <int32_t >(sqrt (imageHash) / 2 );
52
+ auto iconHandle = gfx::Context::getDynamicTextureRGBA ()->addImage (image.image , uniqueId);
93
53
const auto it = versionMap.find (entry.first );
94
54
const auto version = it != versionMap.end () ? it->second : 0 ;
95
- result.iconPositions .emplace (image.id , ImagePosition{bin, image, version});
55
+ if (iconHandle) {
56
+ iconPositions.emplace (image.id , ImagePosition{*iconHandle->getBin (), image, version, iconHandle});
57
+ }
96
58
}
59
+
60
+ return iconPositions;
61
+ }
97
62
98
- result.patternPositions .reserve (patterns.size ());
63
+ ImagePositions uploadPatterns (const ImageMap& patterns, const ImageVersionMap& versionMap) {
64
+ ImagePositions patternPositions;
65
+ patternPositions.reserve (patterns.size ());
99
66
100
67
for (const auto & entry : patterns) {
101
68
const style::Image::Impl& image = *entry.second ;
102
- const mapbox::Bin& bin = _packImage (pack, image, result, ImageType::Pattern);
69
+ auto imageHash = util::hash (image.id );
70
+ int32_t uniqueId = static_cast <int32_t >(sqrt (imageHash) / 2 );
71
+ auto patternHandle = gfx::Context::getDynamicTextureRGBA ()->addImage (image.image , uniqueId);
103
72
const auto it = versionMap.find (entry.first );
104
73
const auto version = it != versionMap.end () ? it->second : 0 ;
105
- result.patternPositions .emplace (image.id , ImagePosition{bin, image, version});
74
+ if (patternHandle) {
75
+ patternPositions.emplace (image.id , ImagePosition{*patternHandle->getBin (), image, version, patternHandle});
76
+ }
106
77
}
107
-
108
- pack.shrink ();
109
- result.image .resize ({static_cast <uint32_t >(pack.width ()), static_cast <uint32_t >(pack.height ())});
110
-
111
- return result;
78
+
79
+ return patternPositions;
112
80
}
113
81
114
82
} // namespace mbgl
0 commit comments