fix runtime errors

This commit is contained in:
Konstantin Käfer
2014-01-14 16:29:20 +01:00
parent 654972bb3f
commit 60a8fdca57
13 changed files with 170 additions and 94 deletions

View File

@@ -8,6 +8,7 @@ namespace llmr {
class linevertexbuffer { class linevertexbuffer {
public: public:
linevertexbuffer(); linevertexbuffer();
~linevertexbuffer();
void addDegenerate(); void addDegenerate();
void addCoordinate(int16_t x, int16_t y); void addCoordinate(int16_t x, int16_t y);

View File

@@ -22,8 +22,8 @@ public:
void moveBy(double dx, double dy); void moveBy(double dx, double dy);
void scaleBy(double ds, double cx, double cy); void scaleBy(double ds, double cx, double cy);
void rotateBy(double cx, double cy, double sx, double sy, double ex, double ey); void rotateBy(double cx, double cy, double sx, double sy, double ex, double ey);
void tileLoaded(tile *tile); void tileLoaded(std::shared_ptr<tile> tile);
void tileFailed(tile *tile); void tileFailed(std::shared_ptr<tile> tile);
private: private:
void updateTiles(); void updateTiles();
@@ -35,7 +35,7 @@ private:
// int32_t min_zoom; // int32_t min_zoom;
// int32_t max_zoom; // int32_t max_zoom;
std::vector<tile *> tiles; std::vector<std::shared_ptr<tile>> tiles;
}; };
} }

View File

@@ -5,13 +5,18 @@
#include <stdint.h> #include <stdint.h>
#include <vector> #include <vector>
#include <mutex>
#include <llmr/util/vec2.hpp> #include <llmr/util/vec2.hpp>
namespace llmr { namespace llmr {
class tile { class tile {
public:
typedef std::shared_ptr<tile> ptr;
public: public:
tile(int32_t z, int32_t x, int32_t y); tile(int32_t z, int32_t x, int32_t y);
~tile();
void setData(uint8_t *data, uint32_t bytes); void setData(uint8_t *data, uint32_t bytes);
bool parse(); bool parse();
@@ -21,8 +26,8 @@ public:
void cancel(); void cancel();
static uint64_t toID(int32_t z, int32_t x, int32_t y, int32_t w = 0); // static int64_t toID(int32_t z, int32_t x, int32_t y, int32_t w = 0);
static vec4<int32_t> fromID(uint64_t id); // static vec4<int32_t> fromID(int64_t id);
public: public:
const int32_t z, x, y; const int32_t z, x, y;
@@ -31,6 +36,8 @@ public:
linevertexbuffer lineVertex; linevertexbuffer lineVertex;
private: private:
std::mutex mutex;
// Source data // Source data
uint8_t *data; uint8_t *data;
uint32_t bytes; uint32_t bytes;

View File

@@ -1,6 +1,8 @@
#ifndef LLMR_PLATFORM_IOS #ifndef LLMR_PLATFORM_IOS
#define LLMR_PLATFORM_IOS #define LLMR_PLATFORM_IOS
#include <memory>
namespace llmr { namespace llmr {
class tile; class tile;
@@ -8,7 +10,7 @@ class tile;
namespace platform { namespace platform {
void restart(void *obj); void restart(void *obj);
void request(void *obj, tile *tile); void request(void *obj, std::shared_ptr<tile> tile);
} }

View File

@@ -19,13 +19,13 @@ public:
void clear(); void clear();
void render(tile *tile); void render(std::shared_ptr<tile> tile);
void switchShader(Shader *shader, float matrix[16]); void switchShader(Shader *shader, float matrix[16]);
private: private:
void setupShaders(); void setupShaders();
void changeMatrix(tile *tile); void changeMatrix(std::shared_ptr<tile> tile);
public: public:

View File

@@ -7,27 +7,27 @@ template <typename T = double>
struct vec2 { struct vec2 {
T x, y; T x, y;
// inline bool operator==(const vec2& rhs) const { inline bool operator==(const vec2& rhs) const {
// return x == rhs.x && y == rhs.y; return x == rhs.x && y == rhs.y;
// } }
}; };
template <typename T = double> template <typename T = double>
struct vec3 { struct vec3 {
T x, y, z; T x, y, z;
// inline bool operator==(const vec3& rhs) const { inline bool operator==(const vec3& rhs) const {
// return x == rhs.x && y == rhs.y && z == rhs.z; return x == rhs.x && y == rhs.y && z == rhs.z;
// } }
}; };
template <typename T = double> template <typename T = double>
struct vec4 { struct vec4 {
T x, y, z, w; T x, y, z, w;
// inline bool operator==(const vec4& rhs) const { inline bool operator==(const vec4& rhs) const {
// return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w; return x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w;
// } }
}; };

View File

@@ -162,7 +162,7 @@ public:
llmr::map *map; llmr::map *map;
}; };
NSOperationQueue *queue = NULL;
MapView *view; MapView *view;
namespace llmr { namespace llmr {
@@ -172,23 +172,26 @@ void restart(void *) {
view->dirty = true; view->dirty = true;
} }
void request(void *, tile *tile) { void request(void *, tile::ptr tile) {
fprintf(stderr, "request %d/%d/%d\n", tile->z, tile->x, tile->y); // fprintf(stderr, "request %d/%d/%d\n", tile->z, tile->x, tile->y);
fprintf(stderr, "requesting tile\n"); // fprintf(stderr, "requesting tile\n");
// NSString *urlTemplate = @"http://api.tiles.mapbox.com/v3/mapbox.mapbox-streets-v4/%d/%d/%d.vector.pbf"; // NSString *urlTemplate = @"http://api.tiles.mapbox.com/v3/mapbox.mapbox-streets-v4/%d/%d/%d.vector.pbf";
NSString *urlTemplate = @"http://localhost:3333/gl/tiles/%d-%d-%d.vector.pbf"; NSString *urlTemplate = @"http://localhost:3333/gl/tiles/plain/%d-%d-%d.vector.pbf";
NSString *urlString = [NSString NSString *urlString = [NSString
stringWithFormat:urlTemplate, stringWithFormat:urlTemplate,
tile->z, tile->z,
tile->x, tile->x,
tile->y]; tile->y];
NSURL *url = [NSURL URLWithString:urlString]; NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"Requesting %@", urlString); // NSLog(@"Requesting %@", urlString);
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; if (!queue) {
queue = [[NSOperationQueue alloc] init];
}
[NSURLConnection [NSURLConnection
sendAsynchronousRequest:urlRequest sendAsynchronousRequest:urlRequest
queue:queue queue:queue
@@ -196,12 +199,22 @@ void request(void *, tile *tile) {
NSData * data, NSData * data,
NSError * error) { NSError * error) {
if (error == nil) { if (error == nil) {
tile->setData((uint8_t *)[data bytes], [data length]); NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if (tile->parse()) { int code = [httpResponse statusCode];
if (code == 200) {
tile->setData((uint8_t *)[data bytes], [data length]);
if (tile->parse()) {
dispatch_async(dispatch_get_main_queue(), ^ {
view->map->tileLoaded(tile);
});
return;
}
} else {
fprintf(stderr, "[%s] status code %d\n", [urlString UTF8String], code);
dispatch_async(dispatch_get_main_queue(), ^ { dispatch_async(dispatch_get_main_queue(), ^ {
view->map->tileLoaded(tile); view->map->tileFailed(tile);
}); });
return;
} }
} }

View File

@@ -9,6 +9,12 @@ linevertexbuffer::linevertexbuffer()
: buffer(0) { : buffer(0) {
} }
linevertexbuffer::~linevertexbuffer() {
if (buffer != 0) {
glDeleteBuffers(1, &buffer);
}
}
void linevertexbuffer::addDegenerate() { void linevertexbuffer::addDegenerate() {
array.push_back(32767); array.push_back(32767);
array.push_back(0); array.push_back(0);
@@ -27,7 +33,7 @@ uint32_t linevertexbuffer::length() {
void linevertexbuffer::bind() { void linevertexbuffer::bind() {
if (buffer == 0) { if (buffer == 0) {
glGenBuffers(1, &buffer); glGenBuffers(1, &buffer);
fprintf(stderr, "new buffer id: %d\n", buffer); // fprintf(stderr, "new buffer id: %d\n", buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(uint16_t), array.data(), GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, array.size() * sizeof(uint16_t), array.data(), GL_STATIC_DRAW);
} else { } else {

View File

@@ -65,11 +65,14 @@ void map::updateTiles() {
br.x = fmin(max_dim, ceil(fmax(box.tr.x, box.br.x))); br.x = fmin(max_dim, ceil(fmax(box.tr.x, box.br.x)));
br.y = fmin(max_dim, ceil(fmax(box.bl.y, box.br.y))); br.y = fmin(max_dim, ceil(fmax(box.bl.y, box.br.y)));
typedef vec3<int32_t> tile_id;
// TODO: Discard tiles that are outside the viewport // TODO: Discard tiles that are outside the viewport
std::vector<uint64_t> required; std::vector<tile_id> required;
for (int32_t y = tl.y; y < br.y; y++) { for (int32_t y = tl.y; y < br.y; y++) {
for (int32_t x = tl.x; x < br.x; x++) { for (int32_t x = tl.x; x < br.x; x++) {
required.push_back(tile::toID(zoom, x, y)); tile_id id; id.z = zoom; id.x = x; id.y = y;
required.push_back(id);
} }
} }
@@ -79,17 +82,17 @@ void map::updateTiles() {
// Remove tiles that we definitely don't need, i.e. tiles that are not on // Remove tiles that we definitely don't need, i.e. tiles that are not on
// the required list. // the required list.
for (std::vector<tile *>::iterator it = tiles.begin(); it != tiles.end(); it++) { for (std::vector<tile::ptr>::iterator it = tiles.begin(); it != tiles.end(); it++) {
tile *tile = *it; tile::ptr& tile = *it;
uint64_t id = tile::toID(tile->z, tile->x, tile->y); tile_id id; id.z = tile->z; id.x = tile->x; id.y = tile->y;
std::vector<uint64_t>::const_iterator required_it = std::find(required.begin(), required.end(), id); std::vector<tile_id>::const_iterator required_it = std::find(required.begin(), required.end(), id);
bool retain = required_it != required.end(); bool retain = required_it != required.end();
if (retain) { if (retain) {
// We already have the tile; remove it from the list of required tiles. // We already have the tile; remove it from the list of required tiles.
required.erase(required_it); required.erase(required_it);
} else { } else {
// We don't need this tile, so we can remove it entirely. // We don't need this tile, so we can remove it entirely.
std::vector<class tile *>::iterator to_remove = it - 1; std::vector<tile::ptr>::iterator to_remove = it - 1;
tile->cancel(); tile->cancel();
tiles.erase(it); tiles.erase(it);
it = to_remove; it = to_remove;
@@ -98,9 +101,8 @@ void map::updateTiles() {
// Required now only contains those tiles that are not yet in the list of // Required now only contains those tiles that are not yet in the list of
// tiles, so we should add them all. // tiles, so we should add them all.
for (int64_t id : required) { for (tile_id& id : required) {
vec4<int32_t> tile_coord = tile::fromID(id); tile::ptr tile = std::make_shared<class tile>(id.z, id.x, id.y);
tile *tile = new class tile(tile_coord.z, tile_coord.x, tile_coord.y);
tiles.push_back(tile); tiles.push_back(tile);
platform::request(this, tile); platform::request(this, tile);
} }
@@ -109,7 +111,7 @@ void map::updateTiles() {
bool map::render() { bool map::render() {
painter->clear(); painter->clear();
for (tile *tile : tiles) { for (tile::ptr& tile : tiles) {
// painter->viewport(); // painter->viewport();
if (tile->loaded) { if (tile->loaded) {
painter->render(tile); painter->render(tile);
@@ -119,12 +121,12 @@ bool map::render() {
return false; return false;
} }
void map::tileLoaded(tile *tile) { void map::tileLoaded(tile::ptr tile) {
platform::restart(this); platform::restart(this);
} }
void map::tileFailed(tile *tile) { void map::tileFailed(tile::ptr tile) {
fprintf(stderr, "[%8zx] tile failed to load %d/%d/%d\n", // fprintf(stderr, "[%8zx] tile failed to load %d/%d/%d\n",
std::hash<std::thread::id>()(std::this_thread::get_id()), // std::hash<std::thread::id>()(std::this_thread::get_id()),
tile->z, tile->x, tile->y); // tile->z, tile->x, tile->y);
} }

View File

@@ -13,6 +13,12 @@
#include <string> #include <string>
// #include <cassert> // #include <cassert>
#define PBF_USE_SETJMP
#ifdef PBF_USE_SETJMP
#include <csetjmp>
#endif
namespace llmr { namespace llmr {
struct pbf { struct pbf {
@@ -29,9 +35,6 @@ struct pbf {
// inline int64_t int64(); // inline int64_t int64();
inline bool boolean(); inline bool boolean();
template <typename T, typename... Args>
inline T *message(const Args&... args);
inline void skip(); inline void skip();
inline void skipValue(uint32_t val); inline void skipValue(uint32_t val);
inline void skipBytes(uint32_t bytes); inline void skipBytes(uint32_t bytes);
@@ -40,6 +43,17 @@ struct pbf {
const uint8_t *end; const uint8_t *end;
uint32_t value; uint32_t value;
uint32_t tag; uint32_t tag;
enum error_code {
UNTERMINATED_VARINT = 1,
UNKNOWN_TYPE = 2,
END_OF_BUFFER = 3
};
#ifdef PBF_USE_SETJMP
std::jmp_buf jump_buffer;
std::string msg;
#endif
}; };
pbf::pbf(const unsigned char *data, uint32_t length) pbf::pbf(const unsigned char *data, uint32_t length)
@@ -76,7 +90,10 @@ T pbf::varint()
int bitpos; int bitpos;
for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) { for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
if (data >= end) { if (data >= end) {
fprintf(stderr, "unterminated varint, unexpected end of buffer"); #ifdef PBF_USE_SETJMP
msg = "unterminated varint, unexpected end of buffer";
std::longjmp(jump_buffer, UNTERMINATED_VARINT);
#endif
exit(1); exit(1);
} }
result |= ((T)(byte = *data) & 0x7F) << bitpos; result |= ((T)(byte = *data) & 0x7F) << bitpos;
@@ -84,7 +101,10 @@ T pbf::varint()
data++; data++;
} }
if (bitpos == 70 && (byte & 0x80)) { if (bitpos == 70 && (byte & 0x80)) {
fprintf(stderr, "unterminated varint (too long)"); #ifdef PBF_USE_SETJMP
msg = "unterminated varint (too long)";
std::longjmp(jump_buffer, UNTERMINATED_VARINT);
#endif
exit(1); exit(1);
} }
@@ -133,14 +153,13 @@ bool pbf::boolean()
return *(bool *)(data - 1); return *(bool *)(data - 1);
} }
// template <typename T, typename... Args> T *pbf::message(const Args&... args)
template <typename T, typename... Args> T *pbf::message(const Args&... args) // {
{ // uint32_t bytes = (uint32_t)varint();
uint32_t bytes = (uint32_t)varint(); // T *result = new T(data, bytes, args...);
T *result = new T(data, bytes, args...); // skipBytes(bytes);
skipBytes(bytes); // return result;
return result; // }
}
void pbf::skip() void pbf::skip()
{ {
@@ -163,7 +182,12 @@ void pbf::skipValue(uint32_t val)
skipBytes(4); skipBytes(4);
break; break;
default: default:
fprintf(stderr, "cannot skip unknown type %d", val & 0x7); char text[32];
snprintf(text, 32, "cannot skip unknown type %d", val & 0x7);
#ifdef PBF_USE_SETJMP
msg = text;
std::longjmp(jump_buffer, UNKNOWN_TYPE);
#endif
exit(1); exit(1);
} }
} }
@@ -172,7 +196,10 @@ void pbf::skipBytes(uint32_t bytes)
{ {
data += bytes; data += bytes;
if (data > end) { if (data > end) {
fprintf(stderr, "unexpected end of buffer"); #ifdef PBF_USE_SETJMP
msg = "unexpected end of buffer";
std::longjmp(jump_buffer, END_OF_BUFFER);
#endif
exit(1); exit(1);
} }
} }

View File

@@ -12,25 +12,27 @@
using namespace llmr; using namespace llmr;
uint64_t tile::toID(int32_t z, int32_t x, int32_t y, int32_t w) { // int64_t tile::toID(int32_t z, int32_t x, int32_t y, int32_t w) {
w *= 2; // w *= 2;
if (w < 0) w = w * -1 -1; // if (w < 0) w = w * -1 -1;
int32_t dim = 1 << z; // int32_t dim = 1 << z;
return ((dim * dim * w + dim * y + x) * 32) + z; // return ((dim * dim * w + dim * y + x) * 32) + z;
} // }
vec4<int32_t> tile::fromID(uint64_t id) { // vec4<int32_t> tile::fromID(int64_t id) {
vec4<int32_t> coord; // vec4<int32_t> coord;
coord.z = id % 32; // coord.z = id % 32;
int32_t dim = 1 << coord.z; // int32_t dim = 1 << coord.z;
int32_t xy = ((id - coord.z) / 32); // int64_t xy = ((id - coord.z) / 32);
coord.x = xy % dim; // coord.x = xy % dim;
coord.y = ((xy - coord.x) / dim) % dim; // coord.y = ((xy - coord.x) / dim) % dim;
coord.w = floor(xy / (dim * dim)); // coord.w = floor(xy / (dim * dim));
if (coord.w % 2 != 0) coord.w = coord.w * -1 -1; // if (coord.w % 2 != 0) coord.w = coord.w * -1 -1;
coord.w /= 2; // coord.w /= 2;
return coord;
}; // fprintf(stderr, "tile: %d/%d/%d/%d\n", coord.z, coord.x, coord.y, coord.w);
// return coord;
// };
tile::tile(int32_t z, int32_t x, int32_t y) tile::tile(int32_t z, int32_t x, int32_t y)
: z(z), : z(z),
@@ -42,6 +44,13 @@ tile::tile(int32_t z, int32_t x, int32_t y)
bytes(0) { bytes(0) {
} }
tile::~tile() {
fprintf(stderr, "[%p] deleting tile %d/%d/%d\n", this, z, x, y);
if (this->data) {
free(this->data);
}
}
void tile::setData(uint8_t *data, uint32_t bytes) { void tile::setData(uint8_t *data, uint32_t bytes) {
this->data = (uint8_t *)malloc(bytes); this->data = (uint8_t *)malloc(bytes);
this->bytes = bytes; this->bytes = bytes;
@@ -50,12 +59,11 @@ void tile::setData(uint8_t *data, uint32_t bytes) {
void tile::cancel() void tile::cancel()
{ {
std::lock_guard<std::mutex> lock(mutex);
// TODO: thread safety // TODO: thread safety
if (!cancelled) { if (!cancelled) {
cancelled = true; cancelled = true;
if (loaded) {
delete this;
}
} else { } else {
assert((!"logic error? multiple cancelleations")); assert((!"logic error? multiple cancelleations"));
} }
@@ -63,14 +71,24 @@ void tile::cancel()
bool tile::parse() bool tile::parse()
{ {
if (cancelled) { {
delete this; std::lock_guard<std::mutex> lock(mutex);
if (cancelled) {
return false;
}
}
// fprintf(stderr, "[%p] parsing tile [%d/%d/%d]...\n", this, z, x, y);
pbf tile(data, bytes);
int code = setjmp(tile.jump_buffer);
if (code > 0) {
fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... failed: %s\n", this, z, x, y, tile.msg.c_str());
cancel();
return false; return false;
} }
fprintf(stderr, "[%p] parsing tile...\n", this);
pbf tile(data, bytes);
while (tile.next()) { while (tile.next()) {
if (tile.tag == 3) { // layer if (tile.tag == 3) { // layer
uint32_t bytes = (uint32_t)tile.varint(); uint32_t bytes = (uint32_t)tile.varint();
@@ -81,11 +99,13 @@ bool tile::parse()
} }
} }
fprintf(stderr, "[%p] parsing tile...done\n", this); // fprintf(stderr, "[%p] parsing tile [%d/%d/%d]... done\n", this, z, x, y);
if (cancelled) { {
delete this; std::lock_guard<std::mutex> lock(mutex);
return false; if (cancelled) {
return false;
}
} }
loaded = true; loaded = true;

View File

@@ -52,8 +52,6 @@ void transform::scaleBy(double ds, double cx, double cy) {
scale *= ds; scale *= ds;
x = (x * ds) + fx; x = (x * ds) + fx;
y = (y * ds) + fy; y = (y * ds) + fy;
fprintf(stderr, "scale: %f, %f, %f\n", scale, x, y);
} }

View File

@@ -79,7 +79,7 @@ void painter::teardown() {
} }
void painter::changeMatrix(tile *tile) { void painter::changeMatrix(std::shared_ptr<tile> tile) {
assert(transform); assert(transform);
// Initialize projection matrix // Initialize projection matrix
@@ -97,7 +97,7 @@ void painter::clear() {
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
} }
void painter::render(tile *tile) { void painter::render(std::shared_ptr<tile> tile) {
changeMatrix(tile); changeMatrix(tile);
glDisable(GL_STENCIL_TEST); glDisable(GL_STENCIL_TEST);