summaryrefslogtreecommitdiff
path: root/include/mbgl/map/map.hpp
blob: 4c5952a2c1a8e977f415429230ca7203f68c9673 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#ifndef MBGL_MAP_MAP
#define MBGL_MAP_MAP

#include <mbgl/map/transform.hpp>
#include <mbgl/renderer/painter.hpp>

#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/time.hpp>
#include <mbgl/util/uv.hpp>

#include <cstdint>
#include <atomic>
#include <iosfwd>
#include <memory>
#include <set>
#include <vector>

namespace mbgl {

class GlyphAtlas;
class GlyphStore;
class LayerDescription;
class SpriteAtlas;
class Sprite;
class Style;
class StyleLayer;
class StyleLayerGroup;
class StyleSource;
class Texturepool;
class FileSource;
class View;

class Map : private util::noncopyable {
public:
    explicit Map(View &view);
    ~Map();

    // Start/stop the map render thread
    void start();
    void stop();

    // Runs the map event loop.
    void run();

    // Triggers a lazy rerender: only performs a render when the map is not clean.
    void rerender();

    void renderLayer(std::shared_ptr<StyleLayer> layer_desc, RenderPass pass, const Tile::ID* id = nullptr, const mat4* matrix = nullptr);

    // Forces a map update: always triggers a rerender.
    void update();

    // Triggers a cleanup that releases resources.
    void cleanup();

    // Controls buffer swapping.
    bool needsSwap();
    void swapped();

    // Size
    void resize(uint16_t width, uint16_t height, float ratio = 1);
    void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height);

    // Styling
    const std::set<std::shared_ptr<StyleSource>> getActiveSources() const;
    void setAppliedClasses(const std::vector<std::string> &classes);
    void toggleClass(const std::string &name);
    const std::vector<std::string> &getAppliedClasses() const;
    void setDefaultTransitionDuration(uint64_t duration_milliseconds = 0);
    void setStyleURL(const std::string &url);
    void setStyleJSON(std::string newStyleJSON, const std::string &base = "");
    std::string getStyleJSON() const;
    void setAccessToken(std::string access_token);
    std::string getAccessToken() const;

    // Transition
    void cancelTransitions();

    // Position
    void moveBy(double dx, double dy, double duration = 0);
    void setLonLat(double lon, double lat, double duration = 0);
    void getLonLat(double &lon, double &lat) const;
    void startPanning();
    void stopPanning();
    void resetPosition();

    // Scale
    void scaleBy(double ds, double cx = -1, double cy = -1, double duration = 0);
    void setScale(double scale, double cx = -1, double cy = -1, double duration = 0);
    double getScale() const;
    void setZoom(double zoom, double duration = 0);
    double getZoom() const;
    void setLonLatZoom(double lon, double lat, double zoom, double duration = 0);
    void getLonLatZoom(double &lon, double &lat, double &zoom) const;
    void resetZoom();
    void startScaling();
    void stopScaling();
    double getMinZoom() const;
    double getMaxZoom() const;

    // Rotation
    void rotateBy(double sx, double sy, double ex, double ey, double duration = 0);
    void setBearing(double degrees, double duration = 0);
    void setBearing(double degrees, double cx, double cy);
    double getBearing() const;
    void resetNorth();
    void startRotating();
    void stopRotating();
    bool canRotate();

    // Debug
    void setDebug(bool value);
    void toggleDebug();
    bool getDebug() const;

public:
    inline const TransformState &getState() const { return state; }
    inline std::shared_ptr<FileSource> getFileSource() const { return fileSource; }
    inline std::shared_ptr<Style> getStyle() const { return style; }
    inline std::shared_ptr<GlyphAtlas> getGlyphAtlas() { return glyphAtlas; }
    inline std::shared_ptr<GlyphStore> getGlyphStore() { return glyphStore; }
    inline std::shared_ptr<SpriteAtlas> getSpriteAtlas() { return spriteAtlas; }
    std::shared_ptr<Sprite> getSprite();
    inline std::shared_ptr<Texturepool> getTexturepool() { return texturepool; }
    inline std::shared_ptr<uv::loop> getLoop() { return loop; }
    inline timestamp getAnimationTime() const { return animationTime; }
    inline timestamp getTime() const { return animationTime; }
    void updateTiles();

private:
    // uv async callbacks
    static void render(uv_async_t *async, int status);
    static void terminate(uv_async_t *async, int status);
    static void cleanup(uv_async_t *async, int status);
    static void delete_async(uv_handle_t *handle, int status);

    // Setup
    void setup();

    void updateSources();
    void updateSources(const std::shared_ptr<StyleLayerGroup> &group);

    void updateRenderState();

    size_t countLayers(const std::vector<LayerDescription>& layers);

    // Prepares a map render by updating the tiles we need for the current view, as well as updating
    // the stylesheet.
    void prepare();


    // Unconditionally performs a render with the current map state.
    void render();
    void renderLayers(std::shared_ptr<StyleLayerGroup> group);

private:
    bool async = false;
    std::shared_ptr<uv::loop> loop;
    std::unique_ptr<uv::thread> thread;
    uv_async_t *async_terminate = nullptr;
    uv_async_t *async_render = nullptr;
    uv_async_t *async_cleanup = nullptr;

private:
    // If cleared, the next time the render thread attempts to render the map, it will *actually*
    // render the map.
    std::atomic_flag is_clean = ATOMIC_FLAG_INIT;

    // If this flag is cleared, the current back buffer is ready for being swapped with the front
    // buffer (i.e. it has rendered data).
    std::atomic_flag is_swapped = ATOMIC_FLAG_INIT;

    // This is cleared once the current front buffer has been presented and the back buffer is
    // ready for rendering.
    std::atomic_flag is_rendered = ATOMIC_FLAG_INIT;

public:
    View &view;

private:
    Transform transform;
    TransformState state;

    std::shared_ptr<FileSource> fileSource;

    std::shared_ptr<Style> style;
    std::shared_ptr<GlyphAtlas> glyphAtlas;
    std::shared_ptr<GlyphStore> glyphStore;
    std::shared_ptr<SpriteAtlas> spriteAtlas;
    std::shared_ptr<Sprite> sprite;
    std::shared_ptr<Texturepool> texturepool;

    Painter painter;

    std::string styleJSON = "";
    std::string accessToken = "";

    bool debug = false;
    timestamp animationTime = 0;

    int indent = 0;

    std::set<std::shared_ptr<StyleSource>> activeSources;

};

}

#endif