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

using namespace mbgl;

RasterTileData::RasterTileData(const TileID& id_,
                               float pixelRatio,
                               const std::string& urlTemplate,
                               gl::TexturePool &texturePool_,
                               Worker& worker_,
                               FileSource& fileSource,
                               const std::function<void(std::exception_ptr)>& callback)
    : TileData(id_),
      texturePool(texturePool_),
      worker(worker_) {
    state = State::loading;

    const Resource resource = Resource::tile(urlTemplate, pixelRatio, id.x, id.y, id.sourceZ);
    req = fileSource.request(resource, [callback, this](Response res) {
        if (res.error) {
            callback(std::make_exception_ptr(std::runtime_error(res.error->message)));
        } else if (res.notModified) {
            modified = res.modified;
            expires = res.expires;
        } else if (res.noContent) {
            state = State::parsed;
            modified = res.modified;
            expires = res.expires;
            workRequest.reset();
            bucket.reset();
            callback(nullptr);
        } else {
            modified = res.modified;
            expires = res.expires;

            // Only overwrite the state when we didn't have a previous tile.
            if (state == State::loading) {
                state = State::loaded;
            }

            workRequest.reset();
            workRequest = worker.parseRasterTile(std::make_unique<RasterBucket>(texturePool), res.data, [this, callback] (RasterTileParseResult result) {
                workRequest.reset();
                if (state != State::loaded) {
                    return;
                }

                std::exception_ptr error;
                if (result.is<std::unique_ptr<Bucket>>()) {
                    state = State::parsed;
                    bucket = std::move(result.get<std::unique_ptr<Bucket>>());
                } else {
                    error = result.get<std::exception_ptr>();
                    state = State::obsolete;
                    bucket.reset();
                }

                callback(error);
            });
        }
    });
}

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

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

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