move to libuv for the rendering loop

This commit is contained in:
Konstantin Käfer
2014-04-30 12:01:26 +02:00
parent ddc359f006
commit 1216a061d3
32 changed files with 1312 additions and 911 deletions

View File

@@ -10,7 +10,7 @@
#include <llmr/util/raster.hpp>
#include <llmr/util/string.hpp>
#include <uv.h>
#include <llmr/util/uv.hpp>
using namespace llmr;
@@ -47,16 +47,14 @@ void TileData::request() {
// to drop to zero for destruction.
} else if (res->code == 200) {
tile->state = State::loaded;
tile->data.swap(res->body);
tile->parse();
// Schedule tile parsing in another thread
tile->scheduleParsing();
} else {
fprintf(stderr, "[%s] tile loading failed: %d, %s\n", tile->url.c_str(), res->code, res->error_message.c_str());
}
}, [weak_tile]() {
std::shared_ptr<TileData> tile = weak_tile.lock();
if (tile) {
tile->map.redraw();
}
}, map.getLoop());
}
@@ -67,9 +65,19 @@ void TileData::cancel() {
}
}
bool TileData::parse() {
void TileData::scheduleParsing() {
// We're creating a new work request. The work request deletes itself after it executed
// the after work handler
new uv::work<std::shared_ptr<TileData>>(
map.getLoop(),
[](std::shared_ptr<TileData> &tile) { tile->parse(); },
[](std::shared_ptr<TileData> &tile) { tile->map.update(); },
shared_from_this());
}
void TileData::parse() {
if (state != State::loaded) {
return false;
return;
}
try {
@@ -80,14 +88,10 @@ bool TileData::parse() {
} catch (const std::exception& ex) {
fprintf(stderr, "[%p] exception [%d/%d/%d]... failed: %s\n", this, id.z, id.x, id.y, ex.what());
cancel();
return false;
return;
}
if (state == State::obsolete) {
return false;
} else {
if (state != State::obsolete) {
state = State::parsed;
}
return true;
}