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
|
#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(
[tile]() {
EnvironmentScope scope(tile->env, ThreadType::TileWorker, "TileWorker_" + tile->name);
tile->parse();
},
callback);
}
|