summaryrefslogtreecommitdiff
path: root/src/mbgl/map/raster_tile_data.cpp
blob: e5a26260351bffb35870f524902d4cf5ab0ed6c0 (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
#include <mbgl/map/raster_tile_data.hpp>
#include <mbgl/map/source.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 <sstream>

using namespace mbgl;

RasterTileData::RasterTileData(const TileID& id_,
                               TexturePool &texturePool,
                               const SourceInfo &source_)
    : TileData(id_),
      source(source_),
      bucket(texturePool, layout) {
}

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

void RasterTileData::request(Worker& worker,
                       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::current.get()->get(), [url, callback, &worker, this](const Response &res) {
        req = nullptr;

        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;

        workRequest = worker.parseRasterTile(bucket, res.data, [this, callback] (bool result) {
            if (state != State::loaded) {
                return;
            }

            if (result) {
                state = State::parsed;
            } else {
                state = State::invalid;
            }

            callback();
        });
    });
}

bool RasterTileData::reparse(Worker&, std::function<void()>) {
    assert(false);
    return false;
}

Bucket* RasterTileData::getBucket(StyleLayer const&) {
    return &bucket;
}

void RasterTileData::cancel() {
    if (state != State::obsolete) {
        state = State::obsolete;
    }
    if (req) {
        util::ThreadContext::getFileSource()->cancel(req);
        req = nullptr;
    }
    workRequest.reset();
}