summaryrefslogtreecommitdiff
path: root/src/mbgl/map/tile_data.cpp
blob: 80fd6346d3d93ec6a164b67df020598a2a4e44e1 (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
#include <mbgl/map/tile_data.hpp>
#include <mbgl/map/environment.hpp>
#include <mbgl/map/source.hpp>

#include <mbgl/storage/file_source.hpp>
#include <mbgl/util/worker.hpp>
#include <mbgl/platform/log.hpp>

using namespace mbgl;

TileData::TileData(const TileID& id_, const SourceInfo& source_)
    : id(id_),
      name(id),
      state(State::initial),
      source(source_),
      env(Environment::Get()),
      debugBucket(debugFontBuffer) {
    // Initialize tile debug coordinates
    debugFontBuffer.addText(name.c_str(), 50, 200, 5);
}

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

const std::string TileData::toString() const {
    return std::string { "[tile " } + name + "]";
}

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

    req = env.request({ Resource::Kind::Tile, url }, [url, callback, &worker, this](const Response &res) {
        req = nullptr;

        if (res.status != Response::Successful) {
            Log::Error(Event::HttpRequest, "[%s] tile loading failed: %s", url.c_str(), res.message.c_str());
            return;
        }

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

        // Schedule tile parsing in another thread
        reparse(worker, callback);
    });
}

void TileData::cancel() {
    if (state != State::obsolete) {
        state = State::obsolete;
    }
    if (req) {
        env.cancelRequest(req);
        req = nullptr;
    }
}

void TileData::reparse(Worker& worker, std::function<void()> callback) {
    util::ptr<TileData> tile = shared_from_this();
    worker.send(
        [this]() {
            EnvironmentScope scope(env, ThreadType::TileWorker, "TileWorker_" + name);
            parse();
        },
        [tile, callback]() {
             // `tile` is bound in this lambda to ensure that if it's the last owning pointer,
             // destruction happens on the map thread, not the worker thread. It is _not_ bound
             // in the above lambda, because we do not want the possibility to arise that the
             // after callback could execute and release the penultimate reference before the
             // work callback has been destructed.
            callback();
        });
}