summaryrefslogtreecommitdiff
path: root/src/mbgl/map/vector_tile_data.cpp
blob: 2e683daaffea3cdbbb01ee65c28970a90bc08274 (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
#include <mbgl/map/vector_tile_data.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/style/style_bucket.hpp>
#include <mbgl/map/source.hpp>
#include <mbgl/text/collision_tile.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/worker.hpp>
#include <mbgl/util/work_request.hpp>
#include <mbgl/style/style.hpp>

using namespace mbgl;

VectorTileData::VectorTileData(const TileID& id_,
                               Style& style_,
                               const SourceInfo& source_,
                               float angle,
                               float pitch,
                               bool collisionDebug)
    : TileData(id_),
      worker(style_.workers),
      tileWorker(id_,
                 source_.source_id,
                 style_,
                 style_.layers,
                 state,
                 std::make_unique<CollisionTile>(angle, pitch, collisionDebug)),
      source(source_),
      lastAngle(angle),
      currentAngle(angle),
      currentPitch(pitch),
      currentCollisionDebug(collisionDebug) {
}

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

void VectorTileData::request(float pixelRatio, const std::function<void()>& callback) {
    std::string url = source.tileURL(id, pixelRatio);
    state = State::loading;

    FileSource* fs = util::ThreadContext::getFileSource();
    req = fs->request({ Resource::Kind::Tile, url }, util::RunLoop::getLoop(), [url, callback, this](const Response &res) {
        if (res.stale) {
            // Only handle fresh responses.
            return;
        }
        req = nullptr;

        if (res.status == Response::NotFound) {
            state = State::parsed;
            callback();
            return;
        }

        if (res.status != Response::Successful) {
            std::stringstream message;
            message <<  "Failed to load [" << url << "]: " << res.message;
            error = message.str();
            state = State::obsolete;
            callback();
            return;
        }

        state = State::loaded;
        data = res.data;

        reparse(callback);
    });
}

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

    parsing = true;

    workRequest = worker.parseVectorTile(tileWorker, data, [this, callback] (TileParseResult result) {
        parsing = false;

        if (state == State::obsolete) {
            return;
        }

        if (result.is<State>()) {
            state = result.get<State>();
        } else {
            std::stringstream message;
            message <<  "Failed to parse [" << std::string(id) << "]: " << result.get<std::string>();
            error = message.str();
            state = State::obsolete;
        }

        callback();
    });

    return true;
}

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

    return tileWorker.getBucket(layer);
}

void VectorTileData::redoPlacement(float angle, float pitch, bool collisionDebug) {
    if (angle == currentAngle &&
        pitch == currentPitch &&
        collisionDebug == currentCollisionDebug)
        return;

    lastAngle = angle;
    lastPitch = pitch;
    lastCollisionDebug = collisionDebug;

    if (state != State::parsed || redoingPlacement)
        return;

    redoingPlacement = true;
    currentAngle = angle;
    currentPitch = pitch;
    currentCollisionDebug = collisionDebug;

    workRequest = worker.redoPlacement(tileWorker, angle, pitch, collisionDebug, [this] {
        for (const auto& layer : tileWorker.layers) {
            auto bucket = getBucket(*layer);
            if (bucket) {
                bucket->swapRenderData();
            }
        }
        redoingPlacement = false;
        redoPlacement(lastAngle, lastPitch, lastCollisionDebug);
    });
}

void VectorTileData::cancel() {
    if (state != State::obsolete) {
        state = State::obsolete;
    }
    req = nullptr;
    workRequest.reset();
}