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
|
#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_,
Worker& worker_)
: TileData(id_),
source(source_),
worker(worker_),
bucket(texturePool, layout) {
}
RasterTileData::~RasterTileData() {
cancel();
}
void RasterTileData::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) {
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] (TileParseResult result) {
if (state != State::loaded) {
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();
});
});
}
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();
}
|