1 #pragma once
2 
3 #include <mbgl/style/image_impl.hpp>
4 #include <mbgl/renderer/image_atlas.hpp>
5 #include <mbgl/util/noncopyable.hpp>
6 #include <mbgl/util/immutable.hpp>
7 #include <mbgl/util/optional.hpp>
8 #include <mbgl/gl/texture.hpp>
9 
10 #include <mapbox/shelf-pack.hpp>
11 
12 #include <set>
13 #include <string>
14 
15 namespace mbgl {
16 
17 namespace gl {
18 class Context;
19 } // namespace gl
20 
21 class ImageRequestor {
22 public:
23     virtual ~ImageRequestor() = default;
24     virtual void onImagesAvailable(ImageMap, uint64_t imageCorrelationID) = 0;
25 };
26 
27 /*
28     ImageManager does two things:
29 
30         1. Tracks requests for icon images from tile workers and sends responses when the requests are fulfilled.
31         2. Builds a texture atlas for pattern images.
32 
33     These are disparate responsibilities and should eventually be handled by different classes. When we implement
34     data-driven support for `*-pattern`, we'll likely use per-bucket pattern atlases, and that would be a good time
35     to refactor this.
36 */
37 class ImageManager : public util::noncopyable {
38 public:
39     ImageManager();
40     ~ImageManager();
41 
42     void setLoaded(bool);
43     bool isLoaded() const;
44 
45     void dumpDebugLogs() const;
46 
47     const style::Image::Impl* getImage(const std::string&) const;
48 
49     void addImage(Immutable<style::Image::Impl>);
50     void updateImage(Immutable<style::Image::Impl>);
51     void removeImage(const std::string&);
52 
53     void getImages(ImageRequestor&, ImageRequestPair&&);
54     void removeRequestor(ImageRequestor&);
55 
56 private:
57     void notify(ImageRequestor&, const ImageRequestPair&) const;
58 
59     bool loaded = false;
60 
61     std::unordered_map<ImageRequestor*, ImageRequestPair> requestors;
62     ImageMap images;
63 
64 // Pattern stuff
65 public:
66     optional<ImagePosition> getPattern(const std::string& name);
67 
68     void bind(gl::Context&, gl::TextureUnit unit);
69     void upload(gl::Context&, gl::TextureUnit unit);
70 
71     Size getPixelSize() const;
72 
73     // Only for use in tests.
getAtlasImage() const74     const PremultipliedImage& getAtlasImage() const {
75         return atlasImage;
76     }
77 
78 private:
79     struct Pattern {
80         mapbox::Bin* bin;
81         ImagePosition position;
82     };
83 
84     mapbox::ShelfPack shelfPack;
85     std::unordered_map<std::string, Pattern> patterns;
86     PremultipliedImage atlasImage;
87     mbgl::optional<gl::Texture> atlasTexture;
88     bool dirty = true;
89 };
90 
91 } // namespace mbgl
92