summaryrefslogtreecommitdiff
path: root/src/mbgl/map/live_tile_data.cpp
blob: 89b1d6fda45d0a2952af3ab53c86c5f17eaf4b49 (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
#include <mbgl/map/live_tile_data.hpp>
#include <mbgl/annotation/annotation_tile.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/text/collision_tile.hpp>
#include <mbgl/util/worker.hpp>
#include <mbgl/util/work_request.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/style/style_bucket.hpp>

#include <sstream>

using namespace mbgl;

LiveTileData::LiveTileData(const TileID& id_,
                           std::unique_ptr<AnnotationTile> tile_,
                           Style& style,
                           const SourceInfo& source,
                           std::function<void()> callback)
    : TileData(id_),
      worker(style.workers),
      tileWorker(id, source.source_id, style, style.layers, state),
      tile(std::move(tile_)) {
    state = State::loaded;

    if (!tile) {
        state = State::parsed;
        return;
    }

    parsePending(callback);
}

bool LiveTileData::parsePending(std::function<void()> callback) {
    if (workRequest || (state != State::loaded && state != State::partial)) {
        return false;
    }

    workRequest.reset();
    workRequest = worker.parseLiveTile(tileWorker, *tile, targetConfig, [this, callback, config = targetConfig] (TileParseResult result) {
        workRequest.reset();

        if (result.is<TileParseResultBuckets>()) {
            auto& resultBuckets = result.get<TileParseResultBuckets>();
            state = resultBuckets.state;

            // Persist the configuration we just placed so that we can later check whether we need
            // to place again in case the configuration has changed.
            placedConfig = config;

            // Move over all buckets we received in this parse request, potentially overwriting
            // existing buckets in case we got a refresh parse.
            for (auto& bucket : resultBuckets.buckets) {
                buckets[bucket.first] = std::move(bucket.second);
            }

            // The target configuration could have changed since we started placement. In this case,
            // we're starting another placement run.
            if (placedConfig != targetConfig) {
                redoPlacement();
            }
        } else {
            error = result.get<std::string>();
            state = State::obsolete;
        }

        callback();

        // The target configuration could have changed since we started placement. In this case,
        // we're starting another placement run.
        if (!workRequest && placedConfig != targetConfig) {
            redoPlacement();
        }
    });

    return true;
}

LiveTileData::~LiveTileData() {
    cancel();
}

Bucket* LiveTileData::getBucket(const StyleLayer& layer) {
    if (!isReady() || !layer.bucket) {
        return nullptr;
    }

    const auto it = buckets.find(layer.bucket->name);
    if (it == buckets.end()) {
        return nullptr;
    }

    assert(it->second);
    return it->second.get();
}

void LiveTileData::cancel() {
    state = State::obsolete;
    workRequest.reset();
}

void LiveTileData::redoPlacement(const PlacementConfig newConfig) {
    if (newConfig != placedConfig) {
        targetConfig = newConfig;

        if (!workRequest) {
            // Don't start a new placement request when the current one hasn't completed yet, or
            // when we are parsing buckets.
            redoPlacement();
        }
    }
}

void LiveTileData::redoPlacement() {
    workRequest.reset();
    workRequest = worker.redoPlacement(tileWorker, buckets, targetConfig, [this, config = targetConfig] {
        workRequest.reset();

        // Persist the configuration we just placed so that we can later check whether we need to
        // place again in case the configuration has changed.
        placedConfig = config;

        for (auto& bucket : buckets) {
            bucket.second->swapRenderData();
        }

        // The target configuration could have changed since we started placement. In this case,
        // we're starting another placement run.
        if (placedConfig != targetConfig) {
            redoPlacement();
        }
    });
}