summaryrefslogtreecommitdiff
path: root/src/mbgl/style/style.hpp
blob: 5dac4d4790ad8877c2889bd7b821d1a99f16a073 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef MBGL_STYLE_STYLE
#define MBGL_STYLE_STYLE

#include <mbgl/style/zoom_history.hpp>
#include <mbgl/style/property_transition.hpp>

#include <mbgl/source/source.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/sprite/sprite_store.hpp>
#include <mbgl/map/mode.hpp>

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/worker.hpp>
#include <mbgl/util/optional.hpp>

#include <cstdint>
#include <string>
#include <vector>

namespace mbgl {

class FileSource;
class GlyphAtlas;
class GlyphStore;
class SpriteStore;
class SpriteAtlas;
class LineAtlas;
class StyleLayer;
class Tile;
class Bucket;
class StyleUpdateParameters;
class TileCoordinate;

struct RenderItem {
    inline RenderItem(const StyleLayer& layer_,
                      const Tile* tile_ = nullptr,
                      Bucket* bucket_ = nullptr)
        : tile(tile_), bucket(bucket_), layer(layer_) {
    }

    const Tile* const tile;
    Bucket* const bucket;
    const StyleLayer& layer;
};

struct RenderData {
    Color backgroundColor = {{ 0, 0, 0, 0 }};
    std::set<Source*> sources;
    std::vector<RenderItem> order;
};

class Style : public GlyphStore::Observer,
              public SpriteStore::Observer,
              public Source::Observer,
              public util::noncopyable {
public:
    Style(FileSource&, float pixelRatio);
    ~Style();

    class Observer : public GlyphStore::Observer,
                     public SpriteStore::Observer,
                     public Source::Observer {
    public:
        /**
         * In addition to the individual glyph, sprite, and source events, the
         * following "rollup" events are provided for convenience. They are
         * strictly additive; e.g. when a source is loaded, both `onSourceLoaded`
         * and `onResourceLoaded` will be called.
         */
         virtual void onResourceLoaded() {};
         virtual void onResourceError(std::exception_ptr) {};
    };

    void setJSON(const std::string& data, const std::string& base);

    void setObserver(Observer*);

    bool isLoaded() const;

    // Fetch the tiles needed by the current viewport and emit a signal when
    // a tile is ready so observers can render the tile.
    void update(const StyleUpdateParameters&);

    void cascade(const TimePoint&, MapMode);
    void recalculate(float z, const TimePoint&, MapMode);

    bool hasTransitions() const;

    std::exception_ptr getLastError() const {
        return lastError;
    }

    Source* getSource(const std::string& id) const;
    void addSource(std::unique_ptr<Source>);

    std::vector<std::unique_ptr<StyleLayer>> getLayers() const;
    StyleLayer* getLayer(const std::string& id) const;
    void addLayer(std::unique_ptr<StyleLayer>,
                  optional<std::string> beforeLayerID = {});
    void removeLayer(const std::string& layerID);

    bool addClass(const std::string&, const PropertyTransition& = {});
    bool removeClass(const std::string&, const PropertyTransition& = {});
    bool hasClass(const std::string&) const;
    void setClasses(const std::vector<std::string>&, const PropertyTransition& = {});
    std::vector<std::string> getClasses() const;

    RenderData getRenderData() const;

    std::vector<std::string> queryRenderedFeatures(
            const std::vector<TileCoordinate>& queryGeometry,
            const double zoom,
            const double bearing,
            const optional<std::vector<std::string>>& layerIDs);

    float getQueryRadius() const;

    void setSourceTileCacheSize(size_t);
    void onLowMemory();

    void dumpDebugLogs() const;

    FileSource& fileSource;
    std::unique_ptr<GlyphStore> glyphStore;
    std::unique_ptr<GlyphAtlas> glyphAtlas;
    std::unique_ptr<SpriteStore> spriteStore;
    std::unique_ptr<SpriteAtlas> spriteAtlas;
    std::unique_ptr<LineAtlas> lineAtlas;

private:
    std::vector<std::unique_ptr<Source>> sources;
    std::vector<std::unique_ptr<StyleLayer>> layers;
    std::vector<std::string> classes;
    optional<PropertyTransition> transitionProperties;

    std::vector<std::unique_ptr<StyleLayer>>::const_iterator findLayer(const std::string& layerID) const;

    // GlyphStore::Observer implementation.
    void onGlyphsLoaded(const FontStack&, const GlyphRange&) override;
    void onGlyphsError(const FontStack&, const GlyphRange&, std::exception_ptr) override;

    // SpriteStore::Observer implementation.
    void onSpriteLoaded() override;
    void onSpriteError(std::exception_ptr) override;

    // Source::Observer implementation.
    void onSourceLoaded(Source&) override;
    void onSourceError(Source&, std::exception_ptr) override;
    void onTileLoaded(Source&, const TileID&, bool isNewTile) override;
    void onTileError(Source&, const TileID&, std::exception_ptr) override;
    void onPlacementRedone() override;

    Observer nullObserver;
    Observer* observer = &nullObserver;

    std::exception_ptr lastError;

    ZoomHistory zoomHistory;
    bool hasPendingTransitions = false;

public:
    bool shouldReparsePartialTiles = false;
    bool loaded = false;
    Worker workers;
};

} // namespace mbgl

#endif