summaryrefslogtreecommitdiff
path: root/src/mbgl/sprite/sprite_store.hpp
blob: 995cb8190084ef2fa3fbb109edc6957e5e514eed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef MBGL_SPRITE_STORE
#define MBGL_SPRITE_STORE

#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/util/noncopyable.hpp>

#include <map>
#include <memory>
#include <mutex>

namespace mbgl {

class FileSource;

class SpriteStore : private util::noncopyable {
public:
    using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>;

    class Observer {
    public:
        virtual ~Observer() = default;

        virtual void onSpriteLoaded() {};
        virtual void onSpriteError(std::exception_ptr) {};
    };

    SpriteStore(float pixelRatio);
    ~SpriteStore();

    void load(const std::string& url, FileSource&);

    bool isLoaded() const {
        return loaded;
    }

    void dumpDebugLogs() const;

    void setObserver(Observer* observer);

    // Adds/replaces a Sprite image.
    void setSprite(const std::string&, std::shared_ptr<const SpriteImage> = nullptr);

    // Adds/replaces mutliple Sprite images.
    void setSprites(const Sprites& sprites);

    // Removes a Sprite.
    void removeSprite(const std::string&);

    // Obtains a Sprite image.
    std::shared_ptr<const SpriteImage> getSprite(const std::string&);

    // Returns Sprite images that changed since the last invocation of this function.
    Sprites getDirty();

    const float pixelRatio;

private:
    void _setSprite(const std::string&, const std::shared_ptr<const SpriteImage>& = nullptr);
    void emitSpriteLoadedIfComplete();

    struct Loader;
    std::unique_ptr<Loader> loader;

    bool loaded = false;

    Observer nullObserver;
    Observer* observer = &nullObserver;

    // Lock for sprites and dirty maps.
    std::mutex mutex;

    // Stores all current sprites.
    Sprites sprites;

    // Stores all Sprite IDs that changed since the last invocation.
    Sprites dirty;
};

} // namespace mbgl

#endif