summaryrefslogtreecommitdiff
path: root/src/mbgl/map/tile_worker.hpp
blob: bcd8e59bf96d6a1490888d7c11e14c8f4e7748f2 (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
#ifndef MBGL_MAP_TILE_WORKER
#define MBGL_MAP_TILE_WORKER

#include <mapbox/variant.hpp>

#include <mbgl/map/mode.hpp>
#include <mbgl/map/tile_data.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/text/placement_config.hpp>

#include <string>
#include <memory>
#include <mutex>
#include <list>
#include <unordered_map>

namespace mbgl {

class CollisionTile;
class GeometryTile;
class SpriteStore;
class GlyphAtlas;
class GlyphStore;
class Bucket;
class StyleLayer;
class SymbolLayer;

// We're using this class to shuttle the resulting buckets from the worker thread to the MapContext
// thread. This class is movable-only because the vector contains movable-only value elements.
class TileParseResultBuckets {
public:
    TileData::State state = TileData::State::invalid;
    std::unordered_map<std::string, std::unique_ptr<Bucket>> buckets;
};

using TileParseResult = mapbox::util::variant<
    TileParseResultBuckets, // success
    std::exception_ptr>;    // error

class TileWorker : public util::noncopyable {
public:
    TileWorker(TileID,
               std::string sourceID,
               SpriteStore&,
               GlyphAtlas&,
               GlyphStore&,
               const std::atomic<TileData::State>&,
               const MapMode);
    ~TileWorker();

    TileParseResult parseAllLayers(std::vector<std::unique_ptr<StyleLayer>>,
                                   std::unique_ptr<const GeometryTile> geometryTile,
                                   PlacementConfig);

    TileParseResult parsePendingLayers(PlacementConfig);

    void redoPlacement(const std::unordered_map<std::string, std::unique_ptr<Bucket>>*,
                       PlacementConfig);

private:
    void parseLayer(const StyleLayer*, const GeometryTile&);
    void insertBucket(const std::string& name, std::unique_ptr<Bucket>);
    void placeLayers(PlacementConfig);

    const TileID id;
    const std::string sourceID;

    SpriteStore& spriteStore;
    GlyphAtlas& glyphAtlas;
    GlyphStore& glyphStore;
    const std::atomic<TileData::State>& state;
    const MapMode mode;

    bool partialParse = false;

    std::vector<std::unique_ptr<StyleLayer>> layers;

    // Contains buckets that we couldn't parse so far due to missing resources.
    // They will be attempted on subsequent parses.
    std::list<std::pair<const SymbolLayer*, std::unique_ptr<Bucket>>> pending;

    // Contains buckets that have been parsed, but still need placement.
    // They will be placed when all buckets have been parsed.
    std::unordered_map<std::string, std::unique_ptr<Bucket>> placementPending;

    // Temporary holder
    TileParseResultBuckets result;
};

} // namespace mbgl

#endif