From 666f593803d29ff88173714e373a35886b51efe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 25 Apr 2014 14:26:59 +0200 Subject: [PATCH 01/10] use an event loop per map that is not the render thread --- common/curl_request.cpp | 14 +++--- common/foundation_request.mm | 20 ++++---- common/glfw_view.cpp | 73 ++++++------------------------ common/glfw_view.hpp | 3 +- include/llmr/map/map.hpp | 23 +++++++++- include/llmr/map/tile_data.hpp | 9 ++-- include/llmr/map/tile_parser.hpp | 3 ++ include/llmr/platform/platform.hpp | 5 +- include/llmr/platform/request.hpp | 7 ++- include/llmr/renderer/painter.hpp | 1 + include/llmr/style/sprite.hpp | 5 +- src/map/map.cpp | 58 ++++++++++++++++++++---- src/map/source.cpp | 2 +- src/map/tile_data.cpp | 22 +++++---- src/map/tile_parser.cpp | 1 + src/platform/request.cpp | 8 ++-- src/renderer/painter.cpp | 1 + src/style/sprite.cpp | 9 ++-- 18 files changed, 152 insertions(+), 112 deletions(-) diff --git a/common/curl_request.cpp b/common/curl_request.cpp index cca50447f..e00e80578 100644 --- a/common/curl_request.cpp +++ b/common/curl_request.cpp @@ -88,9 +88,10 @@ static std::queue curl_handle_cache; class CURLRequest : public llmr::platform::Request { public: CURLRequest(const std::string &url, - std::function background_function, - std::function foreground_callback) - : Request(url, background_function, foreground_callback) {} + std::function background_function, + std::function foreground_callback, + uv_loop_t *loop) + : Request(url, background_function, foreground_callback, loop) {} CURL *curl = nullptr; }; @@ -177,7 +178,7 @@ void curl_perform(uv_poll_t *req, int /*status*/, int events) { // Executes the background_function in a libuv thread pool, and the after_work_cb back // in the *main* event loop. - uv_queue_work(uv_default_loop(), work, Request::work_callback, Request::after_work_callback); + uv_queue_work((*req)->loop, work, Request::work_callback, Request::after_work_callback); CURL *handle = message->easy_handle; remove_curl_handle(handle); @@ -367,10 +368,11 @@ void thread_init_cb() { std::shared_ptr platform::request_http(const std::string &url, std::function background_function, - std::function foreground_callback) { + std::function foreground_callback, + uv_loop_t *loop) { using namespace request; init_thread_once(thread_init_cb); - std::shared_ptr req = std::make_shared(url, background_function, foreground_callback); + std::shared_ptr req = std::make_shared(url, background_function, foreground_callback, loop); // Note that we are creating a new shared_ptr pointer(!) because the lockless queue can't store // objects with nontrivial destructors. We have to make absolutely sure that we manually delete diff --git a/common/foundation_request.mm b/common/foundation_request.mm index 2bd651808..c0b12cc73 100644 --- a/common/foundation_request.mm +++ b/common/foundation_request.mm @@ -41,8 +41,9 @@ class FoundationRequest : public llmr::platform::Request { public: FoundationRequest(const std::string &url, std::function background_function, - std::function foreground_callback) - : Request(url, background_function, foreground_callback) { + std::function foreground_callback, + uv_loop_t *loop) + : Request(url, background_function, foreground_callback, loop) { #if TARGET_OS_IPHONE active_tasks++; dispatch_async(dispatch_get_main_queue(), ^(void) { @@ -68,11 +69,12 @@ public: std::shared_ptr llmr::platform::request_http(const std::string &url, std::function background_function, - std::function foreground_callback) { + std::function foreground_callback, + uv_loop_t *loop) { uv_once(&request_initialize, request_initialize_cb); std::shared_ptr req = - std::make_shared(url, background_function, foreground_callback); + std::make_shared(url, background_function, foreground_callback, loop); // Note that we are creating a new shared_ptr pointer(!) to make sure there is at least one // shared_ptr in existence while the NSURLSession is loading our data. We are making sure in the @@ -91,13 +93,13 @@ llmr::platform::request_http(const std::string &url, return; } - req->res = std::make_unique(); + (*req_ptr)->res = std::make_unique(); if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) { - req->res->code = [(NSHTTPURLResponse *)response statusCode]; - req->res->body = {(const char *)[data bytes], [data length]}; + (*req_ptr)->res->code = [(NSHTTPURLResponse *)response statusCode]; + (*req_ptr)->res->body = {(const char *)[data bytes], [data length]}; } else { - req->res->error_message = [[error localizedDescription] UTF8String]; + (*req_ptr)->res->error_message = [[error localizedDescription] UTF8String]; } // We're currently in the request thread. We're going to schedule a uv_work request that @@ -111,7 +113,7 @@ llmr::platform::request_http(const std::string &url, // Executes the background_function in a libuv thread pool, and the after_work_cb back in // the *main* event loop. - uv_queue_work(uv_default_loop(), work, Request::work_callback, + uv_queue_work((*req_ptr)->loop, work, Request::work_callback, Request::after_work_callback); }]; diff --git a/common/glfw_view.cpp b/common/glfw_view.cpp index 82e9aad74..d3034016f 100644 --- a/common/glfw_view.cpp +++ b/common/glfw_view.cpp @@ -33,8 +33,7 @@ MapView::MapView(llmr::Settings &settings, bool fullscreen) - : fullscreen(fullscreen), settings(settings), map(settings), - stop_event_listener(false) {} + : fullscreen(fullscreen), settings(settings), map(settings) {} MapView::~MapView() { glfwTerminate(); } @@ -196,69 +195,27 @@ void MapView::mousemove(GLFWwindow *window, double x, double y) { mapView->last_y = y; } -void MapView::eventloop(void *arg) { - MapView *view = static_cast(arg); - int r = 0; - int fd = 0; - int timeout; - - while (!view->stop_event_listener) { - fd = uv_backend_fd(uv_default_loop()); - timeout = uv_backend_timeout(uv_default_loop()); - - do { -#if defined(HAVE_KQUEUE) - struct timespec ts; - ts.tv_sec = timeout / 1000; - ts.tv_nsec = (timeout % 1000) * 1000000; - r = kevent(fd, NULL, 0, NULL, 0, &ts); -#elif defined(HAVE_EPOLL) - { - struct epoll_event ev; - r = epoll_wait(fd, &ev, 1, timeout); - } -#endif - } while (r == -1 && errno == EINTR); - glfwPostEmptyEvent(); - uv_sem_wait(&view->event_listener); - } -} - int MapView::run() { - uv_thread_t embed_thread; - - uv_run(uv_default_loop(), UV_RUN_NOWAIT); - - /* Start worker that will interrupt external loop */ - stop_event_listener = false; - uv_sem_init(&event_listener, 0); - uv_thread_create(&embed_thread, eventloop, this); + map.start(); while (!glfwWindowShouldClose(window)) { - - bool dirty = false; - try { - dirty = map.render(); - } - catch (std::exception &ex) { - fprintf(stderr, "exception: %s\n", ex.what()); - } - glfwSwapBuffers(window); - fps(); - - if (dirty) { - glfwPollEvents(); - } else { - glfwWaitEvents(); + if (map.clean.test_and_set() == false) { + // This branch is executed when the previous value of the "clean" + // flag is false (i.e. it is unclean == dirty). + try { + map.render(); + } + catch (std::exception &ex) { + fprintf(stderr, "exception: %s\n", ex.what()); + } + glfwSwapBuffers(window); + fps(); } - uv_run(uv_default_loop(), UV_RUN_NOWAIT); - uv_sem_post(&event_listener); + glfwWaitEvents(); } - stop_event_listener = true; - - uv_thread_join(&embed_thread); + map.stop(); return 0; } diff --git a/common/glfw_view.hpp b/common/glfw_view.hpp index 8647c8ad6..5dbb64efb 100644 --- a/common/glfw_view.hpp +++ b/common/glfw_view.hpp @@ -37,8 +37,7 @@ public: llmr::Settings &settings; llmr::Map map; - uv_sem_t event_listener; - std::atomic stop_event_listener; + uv_loop_t *loop = nullptr; }; #endif diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index b7562bb83..245aa2992 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -1,6 +1,8 @@ #ifndef LLMR_MAP_MAP #define LLMR_MAP_MAP +#include + #include #include #include @@ -24,6 +26,9 @@ public: explicit Map(Settings& settings); ~Map(); + void start(); + void stop(); + /* setup */ void setup(); void loadStyle(const uint8_t *const data, uint32_t bytes); @@ -33,7 +38,7 @@ public: /* callback */ void update(); - bool render(); + void render(); void cancelAnimations(); /* position */ @@ -64,16 +69,27 @@ public: void startRotating(); void stopRotating(); + + void redraw(); + void toggleDebug(); void toggleRaster(); box cornersToBox(uint32_t z) const; + + +public: // Getters float getPixelRatio() const; Style& getStyle(); GlyphAtlas& getGlyphAtlas(); + uv_loop_t *getLoop(); private: bool updateTiles(); + static void eventloop(void *arg); + +public: + std::atomic_flag clean = ATOMIC_FLAG_INIT; private: Settings& settings; @@ -84,6 +100,11 @@ private: Painter painter; std::map> sources; + +private: + uv_loop_t *loop = nullptr; + uv_thread_t thread; + uv_async_t async_terminate; }; } diff --git a/include/llmr/map/tile_data.hpp b/include/llmr/map/tile_data.hpp index c6283e370..fc03a8507 100644 --- a/include/llmr/map/tile_data.hpp +++ b/include/llmr/map/tile_data.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -30,7 +29,7 @@ namespace llmr { -class Style; +class Map; class Raster; class LayerDescription; class BucketDescription; @@ -56,7 +55,7 @@ public: }; public: - TileData(Tile::ID id, const Style& style, GlyphAtlas& glyphAtlas, const std::string url, const bool is_raster); + TileData(Tile::ID id, Map &map, const std::string url, const bool is_raster); ~TileData(); void request(); @@ -91,8 +90,8 @@ private: const std::string url; const bool is_raster = false; std::string data; - const Style& style; - GlyphAtlas& glyphAtlas; + + Map ↦ // Stores a request that is in progress. std::weak_ptr req; diff --git a/include/llmr/map/tile_parser.hpp b/include/llmr/map/tile_parser.hpp index ee623f1ff..013d650de 100644 --- a/include/llmr/map/tile_parser.hpp +++ b/include/llmr/map/tile_parser.hpp @@ -6,6 +6,9 @@ namespace llmr { +class Style; +class GlyphAtlas; + class TileParser { public: TileParser(const std::string& data, TileData& tile, const Style& style, GlyphAtlas& glyphAtlas, bool is_raster = false); diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index 4536bf045..b8ec955c7 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -5,6 +5,8 @@ #include #include +typedef struct uv_loop_s uv_loop_t; + namespace llmr { extern const char *kSpriteURL; @@ -30,7 +32,8 @@ struct Response { // Returns a cancellable request. std::shared_ptr request_http(const std::string &url, std::function background_function, - std::function foreground_callback); + std::function foreground_callback, + uv_loop_t *loop); // Cancels an HTTP request. void cancel_request_http(const std::shared_ptr &req); diff --git a/include/llmr/platform/request.hpp b/include/llmr/platform/request.hpp index 7ba1daf14..0aa8fc639 100644 --- a/include/llmr/platform/request.hpp +++ b/include/llmr/platform/request.hpp @@ -10,6 +10,7 @@ // Forward definition. typedef struct uv_work_s uv_work_t; typedef struct uv_check_s uv_check_t; +typedef struct uv_loop_s uv_loop_t; namespace llmr { namespace platform { @@ -20,7 +21,8 @@ class Request : public std::enable_shared_from_this, private util::nonc public: Request(const std::string &url, std::function background_function, - std::function foreground_callback); + std::function foreground_callback, + uv_loop_t *loop); ~Request(); public: @@ -34,8 +36,9 @@ public: std::unique_ptr res; bool cancelled = false; -private: +public: uv_check_t *check = nullptr; + uv_loop_t *loop = nullptr; }; } } diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index feb875d8d..4f56dbbbb 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -22,6 +22,7 @@ namespace llmr { class Transform; class Style; class Tile; +class GlyphAtlas; class FillBucket; class LineBucket; diff --git a/include/llmr/style/sprite.hpp b/include/llmr/style/sprite.hpp index 3ac81b39d..450b47aee 100644 --- a/include/llmr/style/sprite.hpp +++ b/include/llmr/style/sprite.hpp @@ -11,6 +11,8 @@ namespace llmr { +class Map; + class SpritePosition { public: SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t pixelRatio = 1); @@ -33,7 +35,7 @@ public: class Sprite : public std::enable_shared_from_this { public: - Sprite(float pixelRatio = 1); + Sprite(Map &map, float pixelRatio = 1); void load(const std::string& base_url); @@ -49,6 +51,7 @@ private: void parseJSON(const std::string& data); private: + Map ↦ mutable std::mutex mtx; bool loaded = false; std::map pos; diff --git a/src/map/map.cpp b/src/map/map.cpp index 6fb5b608a..661b41cdf 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -17,13 +17,38 @@ Map::Map(Settings& settings) texturepool(), style(), glyphAtlas(1024, 1024), - painter(transform, style, glyphAtlas) { + painter(transform, style, glyphAtlas), + loop(uv_loop_new()) { + clean.clear(); } Map::~Map() { settings.sync(); } +void Map::start() { + uv_async_init(loop, &async_terminate, [](uv_async_t *async) { + uv_stop(static_cast(async->data)); + }); + async_terminate.data = loop; + + uv_thread_create(&thread, eventloop, this); +} + +void Map::stop() { + uv_async_send(&async_terminate); + uv_thread_join(&thread); + + uv_loop_delete(loop); + loop = nullptr; +} + +void Map::eventloop(void *arg) { + Map *map = static_cast(arg); + + uv_run(map->loop, UV_RUN_DEFAULT); +} + void Map::setup() { painter.setup(); @@ -78,7 +103,7 @@ void Map::resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width painter.resize(); if (!style.sprite || style.sprite->pixelRatio != transform.getPixelRatio()) { - style.sprite = std::make_shared(transform.getPixelRatio()); + style.sprite = std::make_shared(*this, transform.getPixelRatio()); style.sprite->load(kSpriteURL); } } @@ -99,8 +124,13 @@ GlyphAtlas& Map::getGlyphAtlas() { return glyphAtlas; } +uv_loop_t *Map::getLoop() { + return loop; +} + void Map::moveBy(double dx, double dy, double duration) { transform.moveBy(dx, dy, duration); + update(); transform.getLonLat(settings.longitude, settings.latitude); @@ -109,12 +139,12 @@ void Map::moveBy(double dx, double dy, double duration) { void Map::startPanning() { transform.startPanning(); - platform::restart(); + redraw(); } void Map::stopPanning() { transform.stopPanning(); - platform::restart(); + redraw(); } void Map::scaleBy(double ds, double cx, double cy, double duration) { @@ -128,12 +158,12 @@ void Map::scaleBy(double ds, double cx, double cy, double duration) { void Map::startScaling() { transform.startScaling(); - platform::restart(); + redraw(); } void Map::stopScaling() { transform.stopScaling(); - platform::restart(); + redraw(); } void Map::rotateBy(double sx, double sy, double ex, double ey, double duration) { @@ -146,14 +176,15 @@ void Map::rotateBy(double sx, double sy, double ex, double ey, double duration) void Map::startRotating() { transform.startRotating(); - platform::restart(); + redraw(); } void Map::stopRotating() { transform.stopRotating(); - platform::restart(); + redraw(); } + void Map::setLonLat(double lon, double lat, double duration) { transform.setLonLat(lon, lat, duration); update(); @@ -294,6 +325,11 @@ void Map::update() { style.cascade(transform.getNormalizedZoom()); // Schedules a repaint + redraw(); +} + +void Map::redraw() { + clean.clear(); platform::restart(); } @@ -310,7 +346,7 @@ bool Map::updateTiles() { return changed; } -bool Map::render() { +void Map::render() { double animationTime = platform::elapsed(); if (transform.needsAnimation()) { @@ -360,5 +396,7 @@ bool Map::render() { bool transition = transform.needsAnimation(); bool animation = painter.needsAnimation(); - return changed || transition || animation; + if (changed || transition || animation) { + redraw(); + } } diff --git a/src/map/source.cpp b/src/map/source.cpp index ca6dea980..be464aaf2 100644 --- a/src/map/source.cpp +++ b/src/map/source.cpp @@ -116,7 +116,7 @@ TileData::State Source::addTile(const Tile::ID& id) { formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y, (map.getPixelRatio() > 1.0 ? "@2x" : "")); } - new_tile.data = std::make_shared(normalized_id, map.getStyle(), map.getGlyphAtlas(), formed_url, (type == Source::Type::raster)); + new_tile.data = std::make_shared(normalized_id, map, formed_url, (type == Source::Type::raster)); new_tile.data->request(); tile_data.push_front(new_tile.data); } diff --git a/src/map/tile_data.cpp b/src/map/tile_data.cpp index 3c379bd1a..905f5c745 100644 --- a/src/map/tile_data.cpp +++ b/src/map/tile_data.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -9,24 +10,24 @@ #include #include +#include + using namespace llmr; -TileData::TileData(Tile::ID id, const Style& style, GlyphAtlas& glyphAtlas, const std::string url, const bool is_raster) +TileData::TileData(Tile::ID id, Map &map, const std::string url, const bool is_raster) : id(id), state(State::initial), raster(), url(url), is_raster(is_raster), - style(style), - glyphAtlas(glyphAtlas) { - + map(map) { // Initialize tile debug coordinates const std::string str = util::sprintf<32>("%d/%d/%d", id.z, id.x, id.y); debugFontBuffer.addText(str.c_str(), 50, 200, 5); } TileData::~TileData() { - glyphAtlas.removeGlyphs((uint64_t)id); + map.getGlyphAtlas().removeGlyphs((uint64_t)id); cancel(); } @@ -51,9 +52,12 @@ void TileData::request() { } else { fprintf(stderr, "[%s] tile loading failed: %d, %s\n", tile->url.c_str(), res->code, res->error_message.c_str()); } - }, []() { - platform::restart(); - }); + }, [weak_tile]() { + std::shared_ptr tile = weak_tile.lock(); + if (tile) { + tile->map.redraw(); + } + }, map.getLoop()); } void TileData::cancel() { @@ -72,7 +76,7 @@ bool TileData::parse() { // Parsing creates state that is encapsulated in TileParser. While parsing, // the TileParser object writes results into this objects. All other state // is going to be discarded afterwards. - TileParser parser(data, *this, style, glyphAtlas, is_raster); + TileParser parser(data, *this, map.getStyle(), map.getGlyphAtlas(), is_raster); } 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(); diff --git a/src/map/tile_parser.cpp b/src/map/tile_parser.cpp index 9e75fe3f7..ed8ad14e4 100644 --- a/src/map/tile_parser.cpp +++ b/src/map/tile_parser.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include diff --git a/src/platform/request.cpp b/src/platform/request.cpp index c5a5922e5..36b2dc77e 100644 --- a/src/platform/request.cpp +++ b/src/platform/request.cpp @@ -6,10 +6,12 @@ using namespace llmr::platform; Request::Request(const std::string &url, std::function background_function, - std::function foreground_callback) + std::function foreground_callback, + uv_loop_t *loop) : url(url), background_function(background_function), - foreground_callback(foreground_callback) { + foreground_callback(foreground_callback), + loop(loop) { // Add a check handle without a callback to keep the default loop running. // We don't have a real handler attached to the default loop right from the @@ -17,7 +19,7 @@ Request::Request(const std::string &url, // request in the request thread. Only after the request is complete, we // create an actual work request that is attached to the default loop. check = new uv_check_t(); - uv_check_init(uv_default_loop(), check); + uv_check_init(loop, check); uv_check_start(check, [](uv_check_t *) {}); } diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 2ae81d071..fc1cc481b 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include #include diff --git a/src/style/sprite.cpp b/src/style/sprite.cpp index e7d48e5b7..f46457cb9 100644 --- a/src/style/sprite.cpp +++ b/src/style/sprite.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -20,7 +21,7 @@ ImagePosition::ImagePosition(const vec2& size, vec2 tl, vec2()) {} +Sprite::Sprite(Map &map, float pixelRatio) : pixelRatio(pixelRatio), raster(std::make_shared()), map(map) {} void Sprite::load(const std::string& base_url) { std::shared_ptr sprite = shared_from_this(); @@ -29,7 +30,7 @@ void Sprite::load(const std::string& base_url) { std::lock_guard lock(sprite->mtx); if (*sprite->raster && sprite->pos.size()) { sprite->loaded = true; - platform::restart(); + sprite->map.redraw(); fprintf(stderr, "sprite loaded\n"); } }; @@ -42,7 +43,7 @@ void Sprite::load(const std::string& base_url) { } else { fprintf(stderr, "failed to load sprite\n"); } - }, complete); + }, complete, map.getLoop()); platform::request_http(base_url + suffix + ".png", [sprite](const platform::Response *res) { if (res->code == 200) { @@ -50,7 +51,7 @@ void Sprite::load(const std::string& base_url) { } else { fprintf(stderr, "failed to load sprite image\n"); } - }, complete); + }, complete, map.getLoop()); } Sprite::operator bool() const { From 3e92f36afc258f4483ab20b10d81dca0bf9e51b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 25 Apr 2014 16:12:03 +0200 Subject: [PATCH 02/10] fix headless test case --- include/llmr/map/map.hpp | 1 + src/map/map.cpp | 5 ++++- test/headless.cpp | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 245aa2992..d7c88b128 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -27,6 +27,7 @@ public: ~Map(); void start(); + void run(); void stop(); /* setup */ diff --git a/src/map/map.cpp b/src/map/map.cpp index 661b41cdf..019ba97c6 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -45,8 +45,11 @@ void Map::stop() { void Map::eventloop(void *arg) { Map *map = static_cast(arg); + map->run(); +} - uv_run(map->loop, UV_RUN_DEFAULT); +void Map::run() { + uv_run(loop, UV_RUN_DEFAULT); } void Map::setup() { diff --git a/test/headless.cpp b/test/headless.cpp index 0dbf79d40..86f112c2a 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -105,7 +105,7 @@ TEST(Headless, initialize) { map.update(); // Run the loop. It will terminate when we don't have any further listeners. - uv_run(uv_default_loop(), UV_RUN_DEFAULT); + map.run(); map.render(); From 3d69e38a1c67ea0eb6b02925cf37fe3db9707e9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 25 Apr 2014 16:14:15 +0200 Subject: [PATCH 03/10] require explicit initialization to point devs toward empty timer names --- include/llmr/util/timer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/llmr/util/timer.hpp b/include/llmr/util/timer.hpp index e4165044d..95a58e500 100644 --- a/include/llmr/util/timer.hpp +++ b/include/llmr/util/timer.hpp @@ -11,7 +11,7 @@ namespace util { class timer { using hr = std::chrono::high_resolution_clock; public: - timer(const std::string &name) : name(name), start(hr::now()) {} + explicit timer(const std::string &name) : name(name), start(hr::now()) {} ~timer() { using namespace std::chrono; std::cerr << name << ": " From ddc359f006c47e2a503b616d27e22f5d29c13e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 25 Apr 2014 17:04:22 +0200 Subject: [PATCH 04/10] add more timers to headless test --- include/llmr/util/timer.hpp | 26 +++++++++++++++----------- src/renderer/painter.cpp | 3 +++ src/shader/shader.cpp | 3 +++ test/headless.cpp | 29 ++++++++++++++++++++++++++++- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/include/llmr/util/timer.hpp b/include/llmr/util/timer.hpp index 95a58e500..a1e8e05a9 100644 --- a/include/llmr/util/timer.hpp +++ b/include/llmr/util/timer.hpp @@ -1,28 +1,32 @@ #ifndef LLMR_UTIL_TIMER #define LLMR_UTIL_TIMER -#include #include -#include namespace llmr { namespace util { +#ifndef DISABLE_TIMER class timer { - using hr = std::chrono::high_resolution_clock; public: - explicit timer(const std::string &name) : name(name), start(hr::now()) {} - ~timer() { - using namespace std::chrono; - std::cerr << name << ": " - << (double)duration_cast(hr::now() - start).count() / 1000 - << "ms" << std::endl; - } + timer(); + timer(const std::string &name); + void report(const std::string &name); + ~timer(); private: const std::string name; - const hr::time_point start; + uint64_t start; }; +#else +class timer { + inline timer() {} + inline timer(const std::string &name) {} + inline void report (const std::string &name) {} + inline ~timer() {} +}; +#endif + } } diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index fc1cc481b..944a5083b 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -19,6 +19,7 @@ #include #include #include +#include using namespace llmr; @@ -35,6 +36,8 @@ bool Painter::needsAnimation() const { } void Painter::setup() { + util::timer timer("painter setup"); + setupShaders(); assert(pointShader); diff --git a/src/shader/shader.cpp b/src/shader/shader.cpp index 5fe91e62f..97e494f65 100644 --- a/src/shader/shader.cpp +++ b/src/shader/shader.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -10,6 +11,8 @@ using namespace llmr; Shader::Shader(const GLchar *vertSource, const GLchar *fragSource) : valid(false), program(0) { + util::timer timer("shader compilation"); + GLuint vertShader; if (!compileShader(&vertShader, GL_VERTEX_SHADER, vertSource)) { diff --git a/test/headless.cpp b/test/headless.cpp index 86f112c2a..0da898245 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -23,6 +24,7 @@ TEST(Headless, initialize) { llmr::Map map(settings); + llmr::util::timer timer; // Setup OpenGL CGLContextObj gl_context; @@ -57,6 +59,8 @@ TEST(Headless, initialize) { return; } + timer.report("gl setup"); + int width = 1024; int height = 768; @@ -84,7 +88,6 @@ TEST(Headless, initialize) { GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); - if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { fprintf(stderr, "Couldn't create framebuffer: "); switch (status) { @@ -97,26 +100,48 @@ TEST(Headless, initialize) { return; } + timer.report("gl framebuffer"); map.setup(); + + timer.report("map setup"); + map.resize(width, height); + + timer.report("map resize"); + map.loadSettings(); + timer.report("map settings"); + map.update(); + timer.report("map update"); + // Run the loop. It will terminate when we don't have any further listeners. map.run(); + timer.report("map loop"); + map.render(); + timer.report("map render"); uint32_t *pixels = new uint32_t[width * height]; glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + timer.report("gl readpixels"); + std::string result = llmr::util::compress_png(width, height, pixels, true); + + timer.report("compress png"); + llmr::util::write_file("out.png", result); + timer.report("save file"); + + delete[] pixels; glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); @@ -125,4 +150,6 @@ TEST(Headless, initialize) { glDeleteRenderbuffersEXT(1, &fbo_depth_stencil); CGLDestroyContext(gl_context); + + timer.report("destruct"); } From 1216a061d3e46eae983a38e42e71115e5c9faef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 30 Apr 2014 12:01:26 +0200 Subject: [PATCH 05/10] move to libuv for the rendering loop --- common/curl_request.cpp | 25 +- common/foundation_request.mm | 28 +- common/glfw_view.cpp | 66 +--- common/glfw_view.hpp | 5 +- include/llmr/llmr.hpp | 1 + include/llmr/map/map.hpp | 95 +++-- include/llmr/map/source.hpp | 10 +- include/llmr/map/tile_data.hpp | 3 +- include/llmr/map/transform.hpp | 124 +++---- include/llmr/map/transform_state.hpp | 68 ++++ include/llmr/map/view.hpp | 19 + include/llmr/platform/platform.hpp | 13 +- include/llmr/platform/request.hpp | 20 +- include/llmr/renderer/painter.hpp | 10 +- include/llmr/style/sprite.hpp | 8 +- include/llmr/util/time.hpp | 42 +++ include/llmr/util/uv.hpp | 68 +++- ios/MBXViewController.h | 3 +- ios/MBXViewController.mm | 133 +++---- linux/main.cpp | 4 +- src/map/map.cpp | 426 +++++++++++----------- src/map/source.cpp | 17 +- src/map/tile_data.cpp | 34 +- src/map/transform.cpp | 519 ++++++++++++++++----------- src/map/transform_state.cpp | 132 +++++++ src/platform/request.cpp | 42 +-- src/renderer/frame_history.cpp | 3 +- src/renderer/painter.cpp | 150 +++++--- src/style/sprite.cpp | 69 ++-- src/util/raster.cpp | 3 +- src/util/timer.cpp | 35 ++ test/headless.cpp | 48 +-- 32 files changed, 1312 insertions(+), 911 deletions(-) create mode 100644 include/llmr/map/transform_state.hpp create mode 100644 include/llmr/map/view.hpp create mode 100644 include/llmr/util/time.hpp create mode 100644 src/map/transform_state.cpp create mode 100644 src/util/timer.cpp diff --git a/common/curl_request.cpp b/common/curl_request.cpp index e00e80578..0702e1e72 100644 --- a/common/curl_request.cpp +++ b/common/curl_request.cpp @@ -88,10 +88,9 @@ static std::queue curl_handle_cache; class CURLRequest : public llmr::platform::Request { public: CURLRequest(const std::string &url, - std::function background_function, - std::function foreground_callback, + std::function callback, uv_loop_t *loop) - : Request(url, background_function, foreground_callback, loop) {} + : Request(url, callback, loop) {} CURL *curl = nullptr; }; @@ -169,16 +168,7 @@ void curl_perform(uv_poll_t *req, int /*status*/, int events) { // We're currently in the CURL request thread. We're going to schedule a uv_work request // that executes the background function in a threadpool, and tell it to call the // after callback back in the main uv loop. - - uv_work_t *work = new uv_work_t(); - - // We're passing on the pointer we created to the work structure. - // It is going to be deleted in the after_work_cb; - work->data = req; - - // Executes the background_function in a libuv thread pool, and the after_work_cb back - // in the *main* event loop. - uv_queue_work((*req)->loop, work, Request::work_callback, Request::after_work_callback); + (*req)->complete(); CURL *handle = message->easy_handle; remove_curl_handle(handle); @@ -187,6 +177,8 @@ void curl_perform(uv_poll_t *req, int /*status*/, int events) { // be cancelled. ((CURLRequest *)req->get())->curl = nullptr; + // Delete the shared_ptr pointer we created earlier. + delete req; break; } @@ -312,8 +304,6 @@ void async_add_cb(uv_async_t * /*async*/) { continue; } - (*req)->res = std::make_unique(); - // Obtain a curl handle (and try to reuse existing handles before creating new ones). CURL *handle = nullptr; if (!curl_handle_cache.empty()) { @@ -367,12 +357,11 @@ void thread_init_cb() { std::shared_ptr platform::request_http(const std::string &url, - std::function background_function, - std::function foreground_callback, + std::function callback, uv_loop_t *loop) { using namespace request; init_thread_once(thread_init_cb); - std::shared_ptr req = std::make_shared(url, background_function, foreground_callback, loop); + std::shared_ptr req = std::make_shared(url, callback, loop); // Note that we are creating a new shared_ptr pointer(!) because the lockless queue can't store // objects with nontrivial destructors. We have to make absolutely sure that we manually delete diff --git a/common/foundation_request.mm b/common/foundation_request.mm index c0b12cc73..d18834946 100644 --- a/common/foundation_request.mm +++ b/common/foundation_request.mm @@ -40,10 +40,9 @@ void request_initialize_cb() { class FoundationRequest : public llmr::platform::Request { public: FoundationRequest(const std::string &url, - std::function background_function, - std::function foreground_callback, + std::function callback, uv_loop_t *loop) - : Request(url, background_function, foreground_callback, loop) { + : Request(url, callback, loop) { #if TARGET_OS_IPHONE active_tasks++; dispatch_async(dispatch_get_main_queue(), ^(void) { @@ -68,13 +67,12 @@ public: std::shared_ptr llmr::platform::request_http(const std::string &url, - std::function background_function, - std::function foreground_callback, + std::function callback, uv_loop_t *loop) { uv_once(&request_initialize, request_initialize_cb); std::shared_ptr req = - std::make_shared(url, background_function, foreground_callback, loop); + std::make_shared(url, callback, loop); // Note that we are creating a new shared_ptr pointer(!) to make sure there is at least one // shared_ptr in existence while the NSURLSession is loading our data. We are making sure in the @@ -93,8 +91,6 @@ llmr::platform::request_http(const std::string &url, return; } - (*req_ptr)->res = std::make_unique(); - if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) { (*req_ptr)->res->code = [(NSHTTPURLResponse *)response statusCode]; (*req_ptr)->res->body = {(const char *)[data bytes], [data length]}; @@ -102,19 +98,9 @@ llmr::platform::request_http(const std::string &url, (*req_ptr)->res->error_message = [[error localizedDescription] UTF8String]; } - // We're currently in the request thread. We're going to schedule a uv_work request that - // executes the background function in a threadpool, and tell it to call the after callback - // back in the main uv loop. - uv_work_t *work = new uv_work_t(); - - // We're passing on the pointer we created to the work structure. It is going to be deleted - // in the after_work_cb; - work->data = req_ptr; - - // Executes the background_function in a libuv thread pool, and the after_work_cb back in - // the *main* event loop. - uv_queue_work((*req_ptr)->loop, work, Request::work_callback, - Request::after_work_callback); + (*req_ptr)->complete(); + + delete req_ptr; }]; req->task = task; diff --git a/common/glfw_view.cpp b/common/glfw_view.cpp index d3034016f..cfa1156a1 100644 --- a/common/glfw_view.cpp +++ b/common/glfw_view.cpp @@ -1,39 +1,7 @@ #include "glfw_view.hpp" - -#ifndef HAVE_KQUEUE -# if defined(__APPLE__) || \ - defined(__DragonFly__) || \ - defined(__FreeBSD__) || \ - defined(__OpenBSD__) || \ - defined(__NetBSD__) -# define HAVE_KQUEUE 1 -# endif -#endif - -#ifndef HAVE_EPOLL -# if defined(__linux__) -# define HAVE_EPOLL 1 -# endif -#endif - -#if defined(HAVE_KQUEUE) || defined(HAVE_EPOLL) - -#if defined(HAVE_KQUEUE) -# include -# include -# include -#endif - -#if defined(HAVE_EPOLL) -# include -#endif - -#endif - - MapView::MapView(llmr::Settings &settings, bool fullscreen) - : fullscreen(fullscreen), settings(settings), map(settings) {} + : fullscreen(fullscreen), settings(settings), map(*this) {} MapView::~MapView() { glfwTerminate(); } @@ -77,20 +45,17 @@ void MapView::init() { glfwGetFramebufferSize(window, &fb_width, &fb_height); settings.load(); - map.setup(); resize(window, 0, 0); - glfwSwapInterval(1); - - map.loadSettings(); - glfwSetCursorPosCallback(window, mousemove); glfwSetMouseButtonCallback(window, mouseclick); glfwSetWindowSizeCallback(window, resize); glfwSetFramebufferSizeCallback(window, resize); glfwSetScrollCallback(window, scroll); glfwSetKeyCallback(window, key); + + glfwMakeContextCurrent(nullptr); } void MapView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int mods) { @@ -152,7 +117,6 @@ void MapView::resize(GLFWwindow *window, int, int) { glfwGetFramebufferSize(window, &fb_width, &fb_height); mapView->map.resize(width, height, (float)fb_width / (float)width, fb_width, fb_height); - mapView->map.update(); } void MapView::mouseclick(GLFWwindow *window, int button, int action, int modifiers) { @@ -199,17 +163,11 @@ int MapView::run() { map.start(); while (!glfwWindowShouldClose(window)) { - if (map.clean.test_and_set() == false) { - // This branch is executed when the previous value of the "clean" - // flag is false (i.e. it is unclean == dirty). - try { - map.render(); - } - catch (std::exception &ex) { - fprintf(stderr, "exception: %s\n", ex.what()); - } + if (map.swapped.test_and_set() == false) { glfwSwapBuffers(window); + map.rendered.clear(); fps(); + map.rerender(); } glfwWaitEvents(); @@ -220,6 +178,14 @@ int MapView::run() { return 0; } +void MapView::make_active() { + glfwMakeContextCurrent(window); +} + +void MapView::swap() { + glfwPostEmptyEvent(); +} + void MapView::fps() { static int frames = 0; static double time_elapsed = 0; @@ -267,9 +233,5 @@ void show_debug_image(std::string name, const char *data, size_t width, size_t h glfwMakeContextCurrent(current_window); } -void restart() { - glfwPostEmptyEvent(); -} - } } diff --git a/common/glfw_view.hpp b/common/glfw_view.hpp index 5dbb64efb..7c9f41811 100644 --- a/common/glfw_view.hpp +++ b/common/glfw_view.hpp @@ -5,13 +5,16 @@ #include #include -class MapView { +class MapView : public llmr::View { public: MapView(llmr::Settings &settings, bool fullscreen = false); ~MapView(); void init(); + void swap(); + void make_active(); + static void key(GLFWwindow *window, int key, int scancode, int action, int mods); static void scroll(GLFWwindow *window, double xoffset, double yoffset); static void resize(GLFWwindow *window, int, int); diff --git a/include/llmr/llmr.hpp b/include/llmr/llmr.hpp index 88d20d268..1bf737483 100644 --- a/include/llmr/llmr.hpp +++ b/include/llmr/llmr.hpp @@ -2,6 +2,7 @@ #define LLMR_MAIN #include "map/map.hpp" +#include "map/view.hpp" #include "map/settings.hpp" #endif diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index d7c88b128..c1cc7c370 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -3,8 +3,9 @@ #include -#include -#include +#include +// #include +// #include #include #include #include @@ -18,31 +19,34 @@ namespace llmr { -class Settings; class Source; class Map : private util::noncopyable { public: - explicit Map(Settings& settings); + explicit Map(View &view); ~Map(); + // Start/stop the map render thread void start(); - void run(); void stop(); - /* setup */ - void setup(); - void loadStyle(const uint8_t *const data, uint32_t bytes); - void loadSettings(); + // Runs the map event loop. + void run(); + + // Triggers a lazy rerender: only performs a render when the map is not clean. + void rerender(); + + // Forces a map update: always triggers a rerender. + void update(); + + // Size void resize(uint16_t width, uint16_t height, float ratio = 1); void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height); - /* callback */ - void update(); - void render(); + // Animations void cancelAnimations(); - /* position */ + // Position void moveBy(double dx, double dy, double duration = 0); void setLonLat(double lon, double lat, double duration = 0); void getLonLat(double &lon, double &lat) const; @@ -50,7 +54,7 @@ public: void stopPanning(); void resetPosition(); - /* scale */ + // Scale void scaleBy(double ds, double cx = -1, double cy = -1, double duration = 0); void setScale(double scale, double cx = -1, double cy = -1, double duration = 0); double getScale() const; @@ -62,39 +66,63 @@ public: void startScaling(); void stopScaling(); - /* rotation */ + // Rotation void rotateBy(double sx, double sy, double ex, double ey, double duration = 0); - void setAngle(double angle, double cx = -1, double cy = -1, double duration = 0); + void setAngle(double angle, double cx = -1, double cy = -1); double getAngle() const; void resetNorth(); void startRotating(); void stopRotating(); - - void redraw(); - + // Toggles + void setDebug(bool value); void toggleDebug(); + bool getDebug() const; void toggleRaster(); - box cornersToBox(uint32_t z) const; - - -public: // Getters - float getPixelRatio() const; - Style& getStyle(); - GlyphAtlas& getGlyphAtlas(); - uv_loop_t *getLoop(); +public: + inline const TransformState &getState() const { return state; } + inline const Style &getStyle() const { return style; } + inline GlyphAtlas &getGlyphAtlas() { return glyphAtlas; } + inline uv_loop_t *getLoop() { return loop; } private: - bool updateTiles(); - static void eventloop(void *arg); + // uv async callbacks + static void render(uv_async_t *async); + static void terminate(uv_async_t *async); + static void delete_async(uv_handle_t *handle); + + // Setup + void setup(); + void loadStyle(const uint8_t *const data, uint32_t bytes); + + void updateTiles(); + + // Prepares a map render by updating the tiles we need for the current view, as well as updating + // the stylesheet. + void prepare(); + + // Unconditionally performs a render with the current map state. + void render(); public: + // If cleared, the next time the render thread attempts to render the map, it will *actually* + // render the map. std::atomic_flag clean = ATOMIC_FLAG_INIT; + // If this flag is cleared, the current back buffer is ready for being swapped with the front + // buffer (i.e. it has rendered data). + std::atomic_flag swapped = ATOMIC_FLAG_INIT; + + // This is cleared once the current front buffer has been presented and the back buffer is + // ready for rendering. + std::atomic_flag rendered = ATOMIC_FLAG_INIT; + private: - Settings& settings; + View &view; Transform transform; + TransformState state; + Texturepool texturepool; Style style; GlyphAtlas glyphAtlas; @@ -102,10 +130,15 @@ private: std::map> sources; + bool debug = false; + double animationTime = 0; + private: + bool async = false; uv_loop_t *loop = nullptr; uv_thread_t thread; - uv_async_t async_terminate; + uv_async_t *async_terminate = nullptr; + uv_async_t *async_render = nullptr; }; } diff --git a/include/llmr/map/source.hpp b/include/llmr/map/source.hpp index 029bc7873..6a2e1362b 100644 --- a/include/llmr/map/source.hpp +++ b/include/llmr/map/source.hpp @@ -13,7 +13,7 @@ namespace llmr { class Map; -class Transform; +class TransformState; class Painter; class Texturepool; @@ -25,15 +25,18 @@ public: }; public: - Source(Map &map, Transform &transform, Painter &painter, Texturepool &texturepool, + Source(Map &map, Painter &painter, Texturepool &texturepool, const char *url = "", Type type = Type::vector, std::vector zooms = {0}, uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 14, bool enabled = true); + + bool update(); - void prepare_render(bool is_baselayer = false); + void prepare_render(const TransformState &transform, bool is_baselayer = false); void render(double animationTime); + public: bool enabled; @@ -51,7 +54,6 @@ private: private: Map& map; - Transform& transform; Painter& painter; Texturepool& texturepool; diff --git a/include/llmr/map/tile_data.hpp b/include/llmr/map/tile_data.hpp index fc03a8507..fb9eceedf 100644 --- a/include/llmr/map/tile_data.hpp +++ b/include/llmr/map/tile_data.hpp @@ -59,7 +59,8 @@ public: ~TileData(); void request(); - bool parse(); + void scheduleParsing(); + void parse(); void cancel(); const std::string toString() const; diff --git a/include/llmr/map/transform.hpp b/include/llmr/map/transform.hpp index 6cf0295ef..ec4011e12 100644 --- a/include/llmr/map/transform.hpp +++ b/include/llmr/map/transform.hpp @@ -1,11 +1,11 @@ #ifndef LLMR_MAP_TRANSFORM #define LLMR_MAP_TRANSFORM -#include -#include #include #include -#include +#include + +#include "transform_state.hpp" #include @@ -17,82 +17,72 @@ struct box; public: Transform(); + // Map view + // Note: width * ratio does not necessarily equal fb_width + bool resize(uint16_t width, uint16_t height, float ratio, + uint16_t fb_width, uint16_t fb_height); + + // Position + void moveBy(double dx, double dy, double duration = 0); + void setLonLat(double lon, double lat, double duration = 0); + void setLonLatZoom(double lon, double lat, double zoom, double duration = 0); + void getLonLat(double& lon, double& lat) const; + void getLonLatZoom(double& lon, double& lat, double& zoom) const; + void startPanning(); + void stopPanning(); + + // Zoom + void scaleBy(double ds, double cx = -1, double cy = -1, double duration = 0); + void setScale(double scale, double cx = -1, double cy = -1, double duration = 0); + void setZoom(double zoom, double duration = 0); + double getZoom() const; + double getScale() const; + void startRotating(); + void stopRotating(); + + // Angle + void rotateBy(double sx, double sy, double ex, double ey, double duration = 0); + void setAngle(double angle, double duration = 0); + void setAngle(double angle, double cx, double cy); + double getAngle() const; + void startScaling(); + void stopScaling(); + // Animations bool needsAnimation() const; void updateAnimations(double time); void cancelAnimations(); - // Map view changes. - // Note: width * ratio does not necessarily equal fb_width - void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height); - - // Relative changes - void moveBy(double dx, double dy, double duration = 0); - void scaleBy(double ds, double cx = -1, double cy = -1, double duration = 0); - void rotateBy(double sx, double sy, double ex, double ey, double duration = 0); - - // Absolute changes - void setScale(double scale, double cx = -1, double cy = -1, double duration = 0); - void setAngle(double angle, double duration = 0); - void setZoom(double zoom, double duration = 0); - void setLonLat(double lon, double lat, double duration = 0); - void setLonLatZoom(double lon, double lat, double zoom, double duration = 0); - - // Getters - void matrixFor(mat4& matrix, const Tile::ID& id) const; - float getZoom() const; - float getNormalizedZoom() const; - int32_t getIntegerZoom() const; - double getScale() const; - double getAngle() const; - void getLonLat(double& lon, double& lat) const; - void getLonLatZoom(double& lon, double& lat, double& zoom) const; - - // Animations - void startPanning(); - void stopPanning(); - void startRotating(); - void stopRotating(); - void startScaling(); - void stopScaling(); - - box cornersToBox(uint32_t z) const; - - // More getters - inline uint16_t getWidth() const { return width; } - inline uint16_t getHeight() const { return height; } - inline uint16_t getFramebufferWidth() const { return fb_width; } - inline uint16_t getFramebufferHeight() const { return fb_height; } - inline float getPixelRatio() const { return pixelRatio; } + // Transform state + const TransformState currentState() const; + const TransformState finalState() const; private: - void setScaleXY(double new_scale, double xn, double yn, double duration = 0); - double pixel_x() const; - double pixel_y() const; + // Functions prefixed with underscores will *not* perform any locks. It is the caller's + // responsibility to lock this object. + void _moveBy(double dx, double dy, double duration = 0); + void _setScale(double scale, double cx, double cy, double duration = 0); + void _setScaleXY(double new_scale, double xn, double yn, double duration = 0); + void _setAngle(double angle, double duration = 0); + void _clearPanning(); + void _clearRotating(); + void _clearScaling(); private: - // logical dimensions - uint16_t width = 0; - uint16_t height = 0; + mutable uv::rwlock mtx; - // physical (framebuffer) dimensions - uint16_t fb_width = 0; - uint16_t fb_height = 0; + // This reflects the current state of the transform, representing the actual position of the + // map. After calling a transform function with a timer, this will likely remain the same until + // you render a new frame. + TransformState current; - float pixelRatio = 1; + // This reflects the final position of the transform, after all possible animations took place. + TransformState final; -public: - bool rotating = false; - bool scaling = false; - bool panning = false; - -private: - double x = 0, y = 0; // pixel values of the map center in the current scale - double angle = 0; - double scale = 1; - - double min_scale = pow(2, 0); - double max_scale = pow(2, 20); + // Limit the amount of zooming possible on the map. + // TODO: make these modifiable from outside. + const double min_scale = pow(2, 0); + const double max_scale = pow(2, 20); // cache values for spherical mercator math double zc, Bc, Cc; diff --git a/include/llmr/map/transform_state.hpp b/include/llmr/map/transform_state.hpp new file mode 100644 index 000000000..6c360b3f3 --- /dev/null +++ b/include/llmr/map/transform_state.hpp @@ -0,0 +1,68 @@ +#ifndef LLMR_MAP_TRANSFORM_STATE +#define LLMR_MAP_TRANSFORM_STATE + +#include +#include + +#include + +namespace llmr { + +class Transform; + +class TransformState { + friend class Transform; + +public: + // Matrix + void matrixFor(mat4& matrix, const Tile::ID& id) const; + box cornersToBox(uint32_t z) const; + + // Dimensions + uint16_t getWidth() const; + uint16_t getHeight() const; + uint16_t getFramebufferWidth() const; + uint16_t getFramebufferHeight() const; + const std::array getFramebufferDimensions() const; + float getPixelRatio() const; + + // Zoom + float getNormalizedZoom() const; + int32_t getIntegerZoom() const; + double getZoom() const; + double getScale() const; + + // Rotation + float getAngle() const; + + // Changing + bool isChanging() const; + +private: + double pixel_x() const; + double pixel_y() const; + +private: + // logical dimensions + uint16_t width = 0, height = 0; + + // physical (framebuffer) dimensions + std::array framebuffer = {{ 0, 0 }}; + + // map scale factor + float pixelRatio = 0; + + // animation state + bool rotating = false; + bool scaling = false; + bool panning = false; + + // map position + double x = 0, y = 0; + double angle = 0; + double scale = 1; +}; + +} + +#endif diff --git a/include/llmr/map/view.hpp b/include/llmr/map/view.hpp new file mode 100644 index 000000000..8f198e3c4 --- /dev/null +++ b/include/llmr/map/view.hpp @@ -0,0 +1,19 @@ +#ifndef LLMR_MAP_VIEW +#define LLMR_MAP_VIEW + +namespace llmr { + +class View { +public: + // Called from the render (=GL) thread. Signals that the context should + // swap the front and the back buffer. + virtual void swap() = 0; + + // Called from the render thread. Makes the GL context active in the current + // thread. This is typically just called once at the beginning of the + // renderer setup since the render thread doesn't switch the contexts. + virtual void make_active() = 0; +}; +} + +#endif diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index b8ec955c7..deeb397b4 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -13,34 +13,27 @@ extern const char *kSpriteURL; namespace platform { -// Restarts painting. This could for example trigger the event loop of the controlling application. -void restart(); - class Request; struct Response { - + Response(std::function callback) : callback(callback) {} int16_t code = -1; std::string body; - std::string error_message; + std::function callback; }; // Makes an HTTP request of a URL on a background thread, calls a function with the results in // another thread, then calls the foreground callback in the original main thread. // Returns a cancellable request. std::shared_ptr request_http(const std::string &url, - std::function background_function, - std::function foreground_callback, + std::function callback, uv_loop_t *loop); // Cancels an HTTP request. void cancel_request_http(const std::shared_ptr &req); -// Returns a relative timestamp in seconds. This value must be monotonic. -double elapsed(); - // Shows an RGBA image with the specified dimensions in a named window. void show_debug_image(std::string name, const char *data, size_t width, size_t height); diff --git a/include/llmr/platform/request.hpp b/include/llmr/platform/request.hpp index 0aa8fc639..974f854bf 100644 --- a/include/llmr/platform/request.hpp +++ b/include/llmr/platform/request.hpp @@ -4,13 +4,13 @@ #include #include #include +#include #include // Forward definition. -typedef struct uv_work_s uv_work_t; -typedef struct uv_check_s uv_check_t; typedef struct uv_loop_s uv_loop_t; +typedef struct uv_async_s uv_async_t; namespace llmr { namespace platform { @@ -20,24 +20,22 @@ struct Response; class Request : public std::enable_shared_from_this, private util::noncopyable { public: Request(const std::string &url, - std::function background_function, - std::function foreground_callback, + std::function callback, uv_loop_t *loop); ~Request(); -public: - static void work_callback(uv_work_t *work); - static void after_work_callback(uv_work_t *work, int status); + void complete(); + +private: + static void complete(uv_async_t *async); public: const std::string url; - const std::function background_function; - const std::function foreground_callback; std::unique_ptr res; - bool cancelled = false; + std::atomic cancelled; public: - uv_check_t *check = nullptr; + uv_async_t *async = nullptr; uv_loop_t *loop = nullptr; }; } diff --git a/include/llmr/renderer/painter.hpp b/include/llmr/renderer/painter.hpp index 4f56dbbbb..c611f56d4 100644 --- a/include/llmr/renderer/painter.hpp +++ b/include/llmr/renderer/painter.hpp @@ -17,6 +17,8 @@ #include #include +#include + namespace llmr { class Transform; @@ -32,7 +34,8 @@ class RasterBucket; class Painter : private util::noncopyable { public: - Painter(Transform& transform, Style& style, GlyphAtlas& glyphAtlas); + Painter(Map &map); + void setup(); void clear(); @@ -72,9 +75,7 @@ public: mat4 extrudeMatrix; private: - Transform& transform; - Style& style; - GlyphAtlas& glyphAtlas; + Map& map; FrameHistory frameHistory; @@ -83,6 +84,7 @@ private: uint32_t gl_program = 0; float gl_lineWidth = 0; bool gl_depthMask = true; + std::array gl_viewport = {{ 0, 0 }}; float strata = 0; const float strata_epsilon = 1.0f / (1 << 16); enum { Opaque, Translucent } pass = Opaque; diff --git a/include/llmr/style/sprite.hpp b/include/llmr/style/sprite.hpp index 450b47aee..fa0f86ecf 100644 --- a/include/llmr/style/sprite.hpp +++ b/include/llmr/style/sprite.hpp @@ -41,18 +41,22 @@ public: ImagePosition getPosition(const std::string& name, bool repeating = false); - operator bool() const; + bool isLoaded() const; public: const float pixelRatio; std::shared_ptr raster; private: - void parseJSON(const std::string& data); + void asyncParseJSON(); + + static void parseJSON(std::shared_ptr &sprite); + static void complete(std::shared_ptr &sprite); private: Map ↦ mutable std::mutex mtx; + std::string body; bool loaded = false; std::map pos; }; diff --git a/include/llmr/util/time.hpp b/include/llmr/util/time.hpp new file mode 100644 index 000000000..f9e3cc05c --- /dev/null +++ b/include/llmr/util/time.hpp @@ -0,0 +1,42 @@ +#ifndef LLMR_UTIL_TIME +#define LLMR_UTIL_TIME + +#include + +namespace llmr { + +typedef uint64_t time; + +namespace util { + + +// Returns time in nanoseconds since an arbitrary point in the past. This has +// no relation to the current time, but is continuously increasing, so +// comparisons between different timestamps produce meaningful values. +inline time now() { + return uv_hrtime(); +} +} + +// used for time conversions +constexpr time operator"" _nanoseconds (long double ns) { return ns; } +constexpr time operator"" _nanosecond (long double ns) { return ns; } +constexpr time operator"" _microseconds (long double us) { return us * 1e3; } +constexpr time operator"" _microsecond (long double us) { return us * 1e3; } +constexpr time operator"" _milliseconds (long double ms) { return ms * 1e6; } +constexpr time operator"" _millisecond (long double ms) { return ms * 1e6; } +constexpr time operator"" _seconds (long double s) { return s * 1e9; } +constexpr time operator"" _second (long double s) { return s * 1e9; } + +constexpr time operator"" _nanoseconds (unsigned long long ns) { return ns; } +constexpr time operator"" _nanosecond (unsigned long long ns) { return ns; } +constexpr time operator"" _microseconds (unsigned long long us) { return us * 1e3; } +constexpr time operator"" _microsecond (unsigned long long us) { return us * 1e3; } +constexpr time operator"" _milliseconds (unsigned long long ms) { return ms * 1e6; } +constexpr time operator"" _millisecond (unsigned long long ms) { return ms * 1e6; } +constexpr time operator"" _seconds (unsigned long long s) { return s * 1e9; } +constexpr time operator"" _second (unsigned long long s) { return s * 1e9; } + +} + +#endif diff --git a/include/llmr/util/uv.hpp b/include/llmr/util/uv.hpp index 3a1f48980..7ad7fd6d1 100644 --- a/include/llmr/util/uv.hpp +++ b/include/llmr/util/uv.hpp @@ -8,7 +8,6 @@ namespace uv { -typedef void (*callback)(); class mutex { public: @@ -16,7 +15,6 @@ public: inline ~mutex() { uv_mutex_destroy(&mtx); } inline void lock() { uv_mutex_lock(&mtx); } inline void unlock() { uv_mutex_unlock(&mtx); } - inline bool trylock() { return uv_mutex_trylock(&mtx); } private: uv_mutex_t mtx; @@ -31,8 +29,40 @@ private: mutex &mtx; }; +class rwlock { +public: + inline rwlock() { assert(uv_rwlock_init(&mtx) == 0); } + inline ~rwlock() { uv_rwlock_destroy(&mtx); } + inline void rdlock() { uv_rwlock_rdlock(&mtx); } + inline void wrlock() { uv_rwlock_wrlock(&mtx); } + inline void rdunlock() { uv_rwlock_rdunlock(&mtx); } + inline void wrunlock() { uv_rwlock_wrunlock(&mtx); } + +private: + uv_rwlock_t mtx; +}; + +class readlock { +public: + inline readlock(rwlock &mtx) : mtx(mtx) { mtx.rdlock(); } + inline ~readlock() { mtx.rdunlock(); } + +private: + rwlock &mtx; +}; + +class writelock { +public: + inline writelock(rwlock &mtx) : mtx(mtx) { mtx.wrlock(); } + inline ~writelock() { mtx.wrunlock(); } + +private: + rwlock &mtx; +}; + class once { public: + typedef void (*callback)(); void operator()(void (*callback)(void)) { uv_once(&o, callback); } @@ -41,6 +71,40 @@ private: uv_once_t o = UV_ONCE_INIT; }; +template +class work { +public: + typedef void (*work_callback)(T &object); + typedef void (*after_work_callback)(T &object); + + template + work(uv_loop_t *loop, work_callback work_cb, after_work_callback after_work_cb, Args&&... args) + : data(std::forward(args)...), + work_cb(work_cb), + after_work_cb(after_work_cb) { + req.data = this; + uv_queue_work(loop, &req, do_work, after_work); + } + +private: + static void do_work(uv_work_t *req) { + work *w = static_cast *>(req->data); + w->work_cb(w->data); + } + + static void after_work(uv_work_t *req, int) { + work *w = static_cast *>(req->data); + w->after_work_cb(w->data); + delete w; + } + +private: + uv_work_t req; + T data; + work_callback work_cb; + after_work_callback after_work_cb; +}; + } #endif diff --git a/ios/MBXViewController.h b/ios/MBXViewController.h index c66114ffa..924d3af60 100644 --- a/ios/MBXViewController.h +++ b/ios/MBXViewController.h @@ -1,6 +1,5 @@ #import -#import -@interface MBXViewController : GLKViewController +@interface MBXViewController : UIViewController @end diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index cc18e97ed..c6f3b1bc4 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -6,6 +6,7 @@ #import #import +#import #include #include @@ -25,19 +26,18 @@ @implementation MBXViewController +class View; + llmr::Map *map = nullptr; +View *viewObj = nullptr; llmr::Settings_iOS *settings = nullptr; MBXViewController *viewController = nullptr; #pragma mark - Setup -- (void)viewDidLoad +- (void)loadView { - [super viewDidLoad]; - - viewController = self; - - self.preferredFramesPerSecond = 60; + [super loadView]; self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; @@ -46,29 +46,34 @@ MBXViewController *viewController = nullptr; NSLog(@"Failed to create OpenGL ES context"); } - GLKView *view = (GLKView *)self.view; - - view.context = self.context; + GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:self.context]; + view.enableSetNeedsDisplay = NO; view.drawableStencilFormat = GLKViewDrawableStencilFormat8; view.drawableDepthFormat = GLKViewDrawableDepthFormat16; - [view bindDrawable]; + self.view = view; +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + [self setupMap]; [self setupInteraction]; [self setupDebugUI]; + + map->start(); } - (void)setupMap { - [EAGLContext setCurrentContext:self.context]; - settings = new llmr::Settings_iOS(); settings->load(); - map = new llmr::Map(*settings); - map->setup(); - map->loadSettings(); + viewObj = new View(self); + + map = new llmr::Map(*viewObj); } @@ -146,26 +151,23 @@ MBXViewController *viewController = nullptr; #pragma mark - Rendering delegates -- (void)viewWillLayoutSubviews +- (void)swap { - // This callback is called whenever the view dimensions change. We trigger an update to have a fresh render to display - // while the view change is transitioned. - [super viewWillLayoutSubviews]; - - // TODO: Pause all current animations during animation. We don't want other events to fire while the view is rotated. - self.paused = NO; + if (map->swapped.test_and_set() == false) + { + [(GLKView *)self.view display]; + map->rendered.clear(); + map->rerender(); + } } -- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect +- (void)viewWillLayoutSubviews { - // Render the map - map->resize(rect.size.width, rect.size.height, view.contentScaleFactor, view.drawableWidth, view.drawableHeight); + [super viewWillLayoutSubviews]; - if (map->render() == false) - { - self.paused = YES; - settings->sync(); - } + GLKView *view = (GLKView *)self.view; + CGRect rect = [view bounds]; + map->resize(rect.size.width, rect.size.height, view.contentScaleFactor, view.drawableWidth, view.drawableHeight); } #pragma mark - Debugging UI @@ -211,7 +213,7 @@ MBXViewController *viewController = nullptr; - (void)toggleRaster { - map->toggleRaster(); + map->toggleRaster(); } #pragma mark - Destruction @@ -224,6 +226,12 @@ MBXViewController *viewController = nullptr; map = nullptr; } + if (viewObj) + { + delete viewObj; + viewObj = nullptr; + } + if (settings) { settings->sync(); @@ -270,8 +278,6 @@ MBXViewController *viewController = nullptr; map->moveBy(finalCenter.x - self.center.x, finalCenter.y - self.center.y, duration); } - - self.paused = NO; } - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch @@ -324,8 +330,6 @@ MBXViewController *viewController = nullptr; { map->stopScaling(); } - - self.paused = NO; } - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate @@ -346,8 +350,6 @@ MBXViewController *viewController = nullptr; { map->stopRotating(); } - - self.paused = NO; } - (void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap @@ -366,8 +368,6 @@ MBXViewController *viewController = nullptr; { map->scaleBy(2, [doubleTap locationInView:doubleTap.view].x, [doubleTap locationInView:doubleTap.view].y, 0.5); } - - self.paused = NO; } - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap @@ -378,8 +378,6 @@ MBXViewController *viewController = nullptr; { map->scaleBy(0.5, [twoFingerTap locationInView:twoFingerTap.view].x, [twoFingerTap locationInView:twoFingerTap.view].y, 0.5); } - - self.paused = NO; } - (void)handleQuickZoomGesture:(UILongPressGestureRecognizer *)quickZoom @@ -400,8 +398,6 @@ MBXViewController *viewController = nullptr; map->scaleBy(powf(2, newZoom) / map->getScale(), self.view.bounds.size.width / 2, self.view.bounds.size.height / 2); } - - self.paused = NO; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer @@ -411,40 +407,21 @@ MBXViewController *viewController = nullptr; return ([validSimultaneousGestures containsObject:[gestureRecognizer class]] && [validSimultaneousGestures containsObject:[otherGestureRecognizer class]]); } -@end +class View : public llmr::View { +public: + View(MBXViewController *controller) : controller(controller) {} + virtual ~View() {} - -namespace llmr -{ - namespace platform - { - class Request - { - public: - int16_t identifier; - std::string original_url; - }; - - void restart() - { - if ([NSThread isMainThread]) - { - viewController.paused = NO; - } - else - { - [viewController performSelectorOnMainThread:@selector(setPaused:) withObject:@(NO) waitUntilDone:NO]; - } - } - - double time() - { - return [viewController timeSinceFirstResume]; - } - - double elapsed() - { - return CACurrentMediaTime(); - } + void make_active() { + [EAGLContext setCurrentContext:controller.context]; } -} + + void swap() { + [controller performSelectorOnMainThread:@selector(swap) withObject:nil waitUntilDone:NO]; + } + +private: + MBXViewController *controller = nullptr; +}; + +@end diff --git a/linux/main.cpp b/linux/main.cpp index 49de8a2ac..32ae04cca 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -9,11 +9,11 @@ MapView *mapView = nullptr; -void quit_handler(int s) { +void quit_handler(int) { if (mapView) { fprintf(stderr, "waiting for quit...\n"); glfwSetWindowShouldClose(mapView->window, true); - llmr::platform::restart(); + glfwPostEmptyEvent(); } else { exit(0); } diff --git a/src/map/map.cpp b/src/map/map.cpp index 019ba97c6..feaf14f6c 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -1,65 +1,130 @@ #include -#include #include #include #include #include #include +#include #include #include using namespace llmr; -Map::Map(Settings& settings) - : settings(settings), +Map::Map(View& view) + : view(view), transform(), texturepool(), style(), glyphAtlas(1024, 1024), - painter(transform, style, glyphAtlas), + painter(*this), loop(uv_loop_new()) { + + // Make sure that we're doing an initial drawing in all cases. clean.clear(); + rendered.clear(); + swapped.test_and_set(); } Map::~Map() { - settings.sync(); -} - -void Map::start() { - uv_async_init(loop, &async_terminate, [](uv_async_t *async) { - uv_stop(static_cast(async->data)); - }); - async_terminate.data = loop; - - uv_thread_create(&thread, eventloop, this); -} - -void Map::stop() { - uv_async_send(&async_terminate); - uv_thread_join(&thread); - uv_loop_delete(loop); loop = nullptr; } -void Map::eventloop(void *arg) { - Map *map = static_cast(arg); - map->run(); +void Map::start() { + // When starting map rendering in another thread, we perform async/continuously + // updated rendering. Only in these cases, we attach the async handlers. + async = true; + + // Setup async notifications + async_terminate = new uv_async_t(); + uv_async_init(loop, async_terminate, terminate); + async_terminate->data = loop; + + async_render = new uv_async_t(); + uv_async_init(loop, async_render, render); + async_render->data = this; + + uv_thread_create(&thread, [](void *arg) { + Map *map = static_cast(arg); + map->run(); + }, this); +} + +void Map::stop() { + uv_async_send(async_terminate); + uv_thread_join(&thread); + + uv_close((uv_handle_t *)async_terminate, delete_async); + async_terminate = nullptr; + uv_close((uv_handle_t *)async_render, delete_async); + async_render = nullptr; + + // Run the event loop once to make sure our async delete handlers are called. + uv_run(loop, UV_RUN_ONCE); + + async = false; +} + +void Map::delete_async(uv_handle_t *handle) { + delete (uv_async_t *)handle; } void Map::run() { + setup(); + prepareRender(); uv_run(loop, UV_RUN_DEFAULT); + + // If the map rendering wasn't started asynchronously, we perform one render + // *after* all events have been processed. + if (!async) { + render(); + } } -void Map::setup() { - painter.setup(); +void Map::rerender() { + // We only send render events if we want to continuously update the map + // (== async rendering). + if (async) { + uv_async_send(async_render); + } +} - style.loadJSON(resources::style, resources::style_size); +void Map::update() { + clean.clear(); + rerender(); +} + +void Map::render(uv_async_t *async) { + Map *map = static_cast(async->data); + + map->prepare(); + + if (map->rendered.test_and_set() == false) { + if (map->clean.test_and_set() == false) { + map->swapped.clear(); + map->view.swap(); + } else { + // We set the rendered flag in the test above, so we have to reset it + // now that we're not actually rendering because the map is clean. + map->rendered.clear(); + } + } +} + +void Map::terminate(uv_async_t *async) { + uv_stop(static_cast(async->data)); +} + +#pragma mark - Setup + +void Map::setup() { + view.make_active(); + + painter.setup(); sources.emplace("mapbox streets", std::unique_ptr(new Source(*this, - transform, painter, texturepool, "http://a.gl-api-us-east-1.tilestream.net/v3/mapbox.mapbox-streets-v4/%d/%d/%d.gl.pbf", @@ -72,7 +137,6 @@ void Map::setup() { sources.emplace("satellite", std::unique_ptr(new Source(*this, - transform, painter, texturepool, "https://a.tiles.mapbox.com/v3/justin.hh0gkdfm/%d/%d/%d%s.png256", @@ -82,6 +146,8 @@ void Map::setup() { 0, 21, false))); + + loadStyle(resources::style, resources::style_size); } void Map::loadStyle(const uint8_t *const data, uint32_t bytes) { @@ -89,176 +155,120 @@ void Map::loadStyle(const uint8_t *const data, uint32_t bytes) { update(); } -void Map::loadSettings() { - transform.setAngle(settings.angle); - transform.setScale(settings.scale); - transform.setLonLat(settings.longitude, settings.latitude); - painter.setDebug(settings.debug); - update(); -} +#pragma mark - Size void Map::resize(uint16_t width, uint16_t height, float ratio) { resize(width, height, ratio, width * ratio, height * ratio); } void Map::resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height) { - transform.resize(width, height, ratio, fb_width, fb_height); - painter.resize(); - - if (!style.sprite || style.sprite->pixelRatio != transform.getPixelRatio()) { - style.sprite = std::make_shared(*this, transform.getPixelRatio()); - style.sprite->load(kSpriteURL); + if (transform.resize(width, height, ratio, fb_width, fb_height)) { + update(); } } -box Map::cornersToBox(uint32_t z) const { - return transform.cornersToBox(z); +#pragma mark - Animations + +void Map::cancelAnimations() { + transform.cancelAnimations(); + + update(); } -float Map::getPixelRatio() const { - return transform.getPixelRatio(); -} -Style& Map::getStyle() { - return style; -} - -GlyphAtlas& Map::getGlyphAtlas() { - return glyphAtlas; -} - -uv_loop_t *Map::getLoop() { - return loop; -} +#pragma mark - Position void Map::moveBy(double dx, double dy, double duration) { transform.moveBy(dx, dy, duration); - update(); - - transform.getLonLat(settings.longitude, settings.latitude); - settings.persist(); } -void Map::startPanning() { - transform.startPanning(); - redraw(); -} - -void Map::stopPanning() { - transform.stopPanning(); - redraw(); -} - -void Map::scaleBy(double ds, double cx, double cy, double duration) { - transform.scaleBy(ds, cx, cy, duration); - update(); - - transform.getLonLat(settings.longitude, settings.latitude); - settings.scale = transform.getScale(); - settings.persist(); -} - -void Map::startScaling() { - transform.startScaling(); - redraw(); -} - -void Map::stopScaling() { - transform.stopScaling(); - redraw(); -} - -void Map::rotateBy(double sx, double sy, double ex, double ey, double duration) { - transform.rotateBy(sx, sy, ex, ey, duration); - update(); - - settings.angle = transform.getAngle(); - settings.persist(); -} - -void Map::startRotating() { - transform.startRotating(); - redraw(); -} - -void Map::stopRotating() { - transform.stopRotating(); - redraw(); -} - - void Map::setLonLat(double lon, double lat, double duration) { transform.setLonLat(lon, lat, duration); update(); - - transform.getLonLat(settings.longitude, settings.latitude); - settings.persist(); } void Map::getLonLat(double& lon, double& lat) const { transform.getLonLat(lon, lat); } -void Map::setLonLatZoom(double lon, double lat, double zoom, double duration) { - transform.setLonLatZoom(lon, lat, zoom, duration); +void Map::startPanning() { + transform.startPanning(); update(); - - transform.getLonLat(settings.longitude, settings.latitude); - settings.scale = transform.getScale(); - settings.persist(); } -void Map::getLonLatZoom(double& lon, double& lat, double& zoom) const { - transform.getLonLatZoom(lon, lat, zoom); +void Map::stopPanning() { + transform.stopPanning(); + update(); +} + +void Map::resetPosition() { + transform.setAngle(0); + transform.setLonLat(0, 0); + transform.setZoom(0); + update(); +} + + +#pragma mark - Scale + +void Map::scaleBy(double ds, double cx, double cy, double duration) { + transform.scaleBy(ds, cx, cy, duration); + update(); } void Map::setScale(double scale, double cx, double cy, double duration) { transform.setScale(scale, cx, cy, duration); update(); +} - transform.getLonLat(settings.longitude, settings.latitude); - settings.scale = transform.getScale(); - settings.persist(); +double Map::getScale() const { + return transform.getScale(); } void Map::setZoom(double zoom, double duration) { transform.setZoom(zoom, duration); update(); - - transform.getLonLat(settings.longitude, settings.latitude); - settings.scale = transform.getScale(); - settings.persist(); } double Map::getZoom() const { return transform.getZoom(); } -void Map::setAngle(double angle, double x, double y, double duration) { - double dx = 0, dy = 0; - - if (x >= 0 && y >= 0) { - dx = (transform.getWidth() / 2) - x; - dy = (transform.getWidth() / 2) - y; - transform.moveBy(dx, dy, duration); - } - - transform.setAngle(angle, duration); - - if (x >= 0 && y >= 0) { - transform.moveBy(-dx, -dy, duration); - } - +void Map::setLonLatZoom(double lon, double lat, double zoom, double duration) { + transform.setLonLatZoom(lon, lat, zoom, duration); update(); - - transform.getLonLat(settings.longitude, settings.latitude); - settings.angle = transform.getAngle(); - settings.persist(); } -double Map::getScale() const { - return transform.getScale(); +void Map::getLonLatZoom(double& lon, double& lat, double& zoom) const { + transform.getLonLatZoom(lon, lat, zoom); +} + +void Map::resetZoom() { + setZoom(0); +} + +void Map::startScaling() { + transform.startScaling(); + update(); +} + +void Map::stopScaling() { + transform.stopScaling(); + update(); +} + + +#pragma mark - Rotation + +void Map::rotateBy(double sx, double sy, double ex, double ey, double duration) { + transform.rotateBy(sx, sy, ex, ey, duration); + update(); +} + +void Map::setAngle(double angle, double cx, double cy) { + transform.setAngle(angle, cx, cy); + update(); } double Map::getAngle() const { @@ -268,33 +278,33 @@ double Map::getAngle() const { void Map::resetNorth() { transform.setAngle(0, 0.5); update(); - - settings.angle = transform.getAngle(); - settings.persist(); } -void Map::resetPosition() { - transform.setAngle(0); - transform.setLonLat(0, 0); - transform.setZoom(0); +void Map::startRotating() { + transform.startRotating(); update(); - - transform.getLonLat(settings.longitude, settings.latitude); - settings.scale = transform.getScale(); - settings.angle = transform.getAngle(); - settings.persist(); } -void Map::resetZoom() { - setZoom(0); +void Map::stopRotating() { + transform.stopRotating(); + update(); +} + + +#pragma mark - Toggles + +void Map::setDebug(bool value) { + debug = value; + painter.setDebug(debug); + update(); } void Map::toggleDebug() { - settings.debug = !settings.debug; - painter.setDebug(settings.debug); - update(); + setDebug(!debug); +} - settings.persist(); +bool Map::getDebug() const { + return debug; } void Map::toggleRaster() { @@ -309,64 +319,45 @@ void Map::toggleRaster() { satellite_source.enabled = true; style.appliedClasses.insert("satellite"); } - - style.cascade(transform.getNormalizedZoom()); - - update(); } + + update(); } -void Map::cancelAnimations() { - transform.cancelAnimations(); -} - -void Map::update() { - // Loads new tiles and removes old ones. - updateTiles(); - - // Updates the stylesheet to the new zoom level - style.cascade(transform.getNormalizedZoom()); - - // Schedules a repaint - redraw(); -} - -void Map::redraw() { - clean.clear(); - platform::restart(); -} - -bool Map::updateTiles() { - bool changed = false; - +void Map::updateTiles() { for (auto &pair : sources) { - Source &source = *pair.second; - if (source.enabled) { - changed = source.update() || changed; + const std::unique_ptr &source = pair.second; + if (source->enabled) { + source->update(); } } +} - return changed; +void Map::prepare() { + view.make_active(); + + // Update animations + animationTime = (double)util::now() / 1e9; + bool animating = transform.needsAnimation(); + if (animating) { + transform.updateAnimations(animationTime); + } + + state = transform.currentState(); + style.cascade(state.getNormalizedZoom()); + if (!style.sprite || style.sprite->pixelRatio != state.getPixelRatio()) { + style.sprite = std::make_shared(*this, state.getPixelRatio()); + style.sprite->load(kSpriteURL); + } + + updateTiles(); } void Map::render() { - double animationTime = platform::elapsed(); - - if (transform.needsAnimation()) { - transform.updateAnimations(animationTime); - - // Animations change the transform which affects the stylesheet. - style.cascade(transform.getNormalizedZoom()); - } - - if (*style.sprite->raster && !style.sprite->raster->textured) { - style.sprite->raster->setTexturepool(&texturepool); - } - - bool changed = updateTiles(); - painter.clear(); + painter.resize(); + painter.changeMatrix(); // First, update the sources' tile matrices with the new @@ -379,7 +370,7 @@ void Map::render() { for (auto &pair : sources) { Source &source = *pair.second; - source.prepare_render(is_baselayer); + source.prepare_render(state, is_baselayer); is_baselayer = source.enabled ? false : (true && is_baselayer); } @@ -395,11 +386,8 @@ void Map::render() { painter.renderMatte(); - - bool transition = transform.needsAnimation(); - bool animation = painter.needsAnimation(); - - if (changed || transition || animation) { - redraw(); + // Schedule another rerender when we definitely need a next frame. + if (transform.needsAnimation()) { + update(); } } diff --git a/src/map/source.cpp b/src/map/source.cpp index be464aaf2..0b1e2925d 100644 --- a/src/map/source.cpp +++ b/src/map/source.cpp @@ -11,12 +11,11 @@ using namespace llmr; -Source::Source(Map &map, Transform &transform, Painter &painter, Texturepool &texturepool, +Source::Source(Map &map, Painter &painter, Texturepool &texturepool, const char *url, Source::Type type, std::vector zooms, uint32_t tile_size, uint32_t min_zoom, uint32_t max_zoom, bool enabled) : enabled(enabled), map(map), - transform(transform), painter(painter), texturepool(texturepool), type(type), @@ -30,7 +29,7 @@ bool Source::update() { return updateTiles(); } -void Source::prepare_render(bool is_baselayer) { +void Source::prepare_render(const TransformState &transform, bool is_baselayer) { if (!enabled) return; uint8_t i = 1; @@ -113,7 +112,7 @@ TileData::State Source::addTile(const Tile::ID& id) { if (type == Source::Type::vector) { formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y); } else { - formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y, (map.getPixelRatio() > 1.0 ? "@2x" : "")); + formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y, (map.getState().getPixelRatio() > 1.0 ? "@2x" : "")); } new_tile.data = std::make_shared(normalized_id, map, formed_url, (type == Source::Type::raster)); @@ -179,7 +178,7 @@ bool Source::updateTiles() { bool changed = false; // Figure out what tiles we need to load - int32_t zoom = map.getZoom(); + int32_t zoom = map.getState().getIntegerZoom(); if (zoom > max_zoom) zoom = max_zoom; if (zoom < min_zoom) zoom = min_zoom; @@ -190,7 +189,7 @@ bool Source::updateTiles() { if (min_covering_zoom < min_zoom) min_covering_zoom = min_zoom; // Map four viewport corners to pixel coordinates - box box = map.cornersToBox(zoom); + box box = map.getState().cornersToBox(zoom); // Performs a scanline algorithm search that covers the rectangle of the box // and sorts them by proximity to the center. @@ -263,7 +262,7 @@ bool Source::updateTiles() { return false; } }); - + return changed; } @@ -331,8 +330,8 @@ void _scanTriangle(const llmr::vec2 a, const llmr::vec2 b, const double Source::getZoom() const { double offset = log(util::tileSize / tile_size) / log(2); - offset += (map.getPixelRatio() > 1.0 ? 1 :0); - return map.getZoom() + offset; + offset += (map.getState().getPixelRatio() > 1.0 ? 1 :0); + return map.getState().getZoom() + offset; } std::forward_list Source::covering_tiles(int32_t zoom, const box& points) { diff --git a/src/map/tile_data.cpp b/src/map/tile_data.cpp index 905f5c745..5cfada7ff 100644 --- a/src/map/tile_data.cpp +++ b/src/map/tile_data.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include 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 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>( + map.getLoop(), + [](std::shared_ptr &tile) { tile->parse(); }, + [](std::shared_ptr &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; } diff --git a/src/map/transform.cpp b/src/map/transform.cpp index aed7afba9..10bb43f4d 100644 --- a/src/map/transform.cpp +++ b/src/map/transform.cpp @@ -3,113 +3,280 @@ #include #include #include +#include #include #include using namespace llmr; - const double D2R = M_PI / 180.0; const double R2D = 180.0 / M_PI; const double M2PI = 2 * M_PI; - Transform::Transform() { - setScale(scale); - setAngle(angle); + setScale(current.scale); + setAngle(current.angle); } -bool Transform::needsAnimation() const { - return !animations.empty(); -} +#pragma mark - Map View -void Transform::updateAnimations(double time) { - animations.remove_if([time](const std::shared_ptr& animation) { - return animation->update(time) == util::animation::complete; - }); -} +bool Transform::resize(const uint16_t w, const uint16_t h, const float ratio, + const uint16_t fb_w, const uint16_t fb_h) { + uv::writelock lock(mtx); -void Transform::cancelAnimations() { - animations.clear(); -} + if (final.width != w || final.height != h || final.pixelRatio != ratio || + final.framebuffer[0] != fb_w || final.framebuffer[1] != fb_h) { -void Transform::resize(uint16_t w, uint16_t h, float ratio, uint16_t fb_w, uint16_t fb_h) { - width = w; - height = h; - pixelRatio = ratio; - fb_width = fb_w; - fb_height = fb_h; -} - -void Transform::moveBy(double dx, double dy, double duration) { - double xn = x + cos(angle) * dx + sin(angle) * dy; - double yn = y + cos(angle) * dy + sin(-angle) * dx; - if (duration == 0) { - x = xn; - y = yn; + current.width = final.width = w; + current.height = final.height = h; + current.pixelRatio = final.pixelRatio = ratio; + current.framebuffer[0] = final.framebuffer[0] = fb_w; + current.framebuffer[1] = final.framebuffer[1] = fb_h; + return true; } else { - // Use a common start time for all of the animations to avoid divergent animations. - double start = platform::elapsed(); - animations.emplace_front(std::make_shared(x, xn, x, start, duration)); - animations.emplace_front(std::make_shared(y, yn, y, start, duration)); + return false; } } +#pragma mark - Position + +void Transform::moveBy(const double dx, const double dy, const double duration) { + uv::writelock lock(mtx); + + _moveBy(dx, dy, duration); +} + +void Transform::_moveBy(const double dx, const double dy, const double duration) { + // This is only called internally, so we don't need a lock here. + + final.x = current.x + cos(current.angle) * dx + sin(current.angle) * dy; + final.y = current.y + cos(current.angle) * dy + sin(-current.angle) * dx; + + if (duration == 0) { + current.x = final.x; + current.y = final.y; + } else { + // Use a common start time for all of the animations to avoid divergent animations. + double start = (double)util::now() / 1_second; + animations.emplace_front( + std::make_shared(current.x, final.x, current.x, start, duration)); + animations.emplace_front( + std::make_shared(current.y, final.y, current.y, start, duration)); + } +} + +void Transform::setLonLat(const double lon, const double lat, const double duration) { + uv::writelock lock(mtx); + + const double f = fmin(fmax(sin(D2R * lat), -0.9999), 0.9999); + double xn = -round(lon * Bc); + double yn = round(0.5 * Cc * log((1 + f) / (1 - f))); + + _setScaleXY(current.scale, xn, yn, duration); +} + +void Transform::setLonLatZoom(const double lon, const double lat, const double zoom, + const double duration) { + uv::writelock lock(mtx); + + double new_scale = pow(2.0, zoom); + + const double s = new_scale * util::tileSize; + zc = s / 2; + Bc = s / 360; + Cc = s / (2 * M_PI); + + const double f = fmin(fmax(sin(D2R * lat), -0.9999), 0.9999); + double xn = -round(lon * Bc); + double yn = round(0.5 * Cc * log((1 + f) / (1 - f))); + + _setScaleXY(new_scale, xn, yn, duration); +} + +void Transform::getLonLat(double &lon, double &lat) const { + uv::readlock lock(mtx); + + lon = -final.x / Bc; + lat = R2D * (2 * atan(exp(final.y / Cc)) - 0.5 * M_PI); +} + +void Transform::getLonLatZoom(double &lon, double &lat, double &zoom) const { + uv::readlock lock(mtx); + + getLonLat(lon, lat); + zoom = getZoom(); +} + void Transform::startPanning() { - stopPanning(); + uv::writelock lock(mtx); + + _clearPanning(); // Add a 200ms timeout for resetting this to false - panning = true; - double start = platform::elapsed(); - pan_timeout = std::make_shared>(false, panning, start, 0.2); + current.panning = true; + double start = (double)util::now() / 1_second; + pan_timeout = std::make_shared>(false, current.panning, start, 0.2); animations.emplace_front(pan_timeout); } void Transform::stopPanning() { - panning = false; + uv::writelock lock(mtx); + + _clearPanning(); +} + +void Transform::_clearPanning() { + current.panning = false; if (pan_timeout) { animations.remove(pan_timeout); pan_timeout.reset(); } } -void Transform::scaleBy(double ds, double cx, double cy, double duration) { +#pragma mark - Zoom + +void Transform::scaleBy(const double ds, const double cx, const double cy, const double duration) { + uv::writelock lock(mtx); + // clamp scale to min/max values - double new_scale = scale * ds; + double new_scale = current.scale * ds; if (new_scale < min_scale) { new_scale = min_scale; } else if (new_scale > max_scale) { new_scale = max_scale; } - setScale(new_scale, cx, cy, duration); + _setScale(new_scale, cx, cy, duration); +} + +void Transform::setScale(const double scale, const double cx, const double cy, + const double duration) { + uv::writelock lock(mtx); + + _setScale(scale, cx, cy, duration); +} + +void Transform::setZoom(const double zoom, const double duration) { + uv::writelock lock(mtx); + + _setScale(pow(2.0, zoom), -1, -1, duration); +} + +double Transform::getZoom() const { + uv::readlock lock(mtx); + + return log(final.scale) / M_LN2; +} + +double Transform::getScale() const { + uv::readlock lock(mtx); + + return final.scale; } void Transform::startScaling() { - stopScaling(); + uv::writelock lock(mtx); + + _clearScaling(); // Add a 200ms timeout for resetting this to false - scaling = true; - double start = platform::elapsed(); - scale_timeout = std::make_shared>(false, scaling, start, 0.2); + current.scaling = true; + double start = (double)util::now() / 1_second; + scale_timeout = std::make_shared>(false, current.scaling, start, 0.2); animations.emplace_front(scale_timeout); } void Transform::stopScaling() { - scaling = false; + uv::writelock lock(mtx); + + _clearScaling(); +} + +void Transform::_clearScaling() { + // This is only called internally, so we don't need a lock here. + + current.scaling = false; if (scale_timeout) { animations.remove(scale_timeout); scale_timeout.reset(); } } -void Transform::rotateBy(double start_x, double start_y, double end_x, double end_y, double duration) { - double center_x = width / 2, center_y = height / 2; +void Transform::_setScale(double new_scale, double cx, double cy, const double duration) { + // This is only called internally, so we don't need a lock here. + + // Ensure that we don't zoom in further than the maximum allowed. + if (new_scale < min_scale) { + new_scale = min_scale; + } else if (new_scale > max_scale) { + new_scale = max_scale; + } + + // Zoom in on the center if we don't have click or gesture anchor coordinates. + if (cx < 0 || cy < 0) { + cx = current.width / 2; + cy = current.height / 2; + } + + // Account for the x/y offset from the center (= where the user clicked or pinched) + const double factor = new_scale / current.scale; + const double dx = (cx - current.width / 2) * (1.0 - factor); + const double dy = (cy - current.height / 2) * (1.0 - factor); + + // Account for angle + const double angle_sin = sin(-current.angle); + const double angle_cos = cos(-current.angle); + const double ax = angle_cos * dx - angle_sin * dy; + const double ay = angle_sin * dx + angle_cos * dy; + + const double xn = current.x * factor + ax; + const double yn = current.y * factor + ay; + + _setScaleXY(new_scale, xn, yn, duration); +} + +void Transform::_setScaleXY(const double new_scale, const double xn, const double yn, + const double duration) { + // This is only called internally, so we don't need a lock here. + + final.scale = new_scale; + final.x = xn; + final.y = yn; + + if (duration == 0) { + current.scale = final.scale; + current.x = final.x; + current.y = final.y; + } else { + // Use a common start time for all of the animations to avoid divergent animations. + double start = (double)util::now() / 1_second; + animations.emplace_front(std::make_shared( + current.scale, final.scale, current.scale, start, duration)); + animations.emplace_front( + std::make_shared(current.x, final.x, current.x, start, duration)); + animations.emplace_front( + std::make_shared(current.y, final.y, current.y, start, duration)); + } + + const double s = final.scale * util::tileSize; + zc = s / 2; + Bc = s / 360; + Cc = s / (2 * M_PI); +} + +#pragma mark - Angle + +void Transform::rotateBy(const double start_x, const double start_y, const double end_x, + const double end_y, const double duration) { + uv::writelock lock(mtx); + + double center_x = current.width / 2, center_y = current.height / 2; const double begin_center_x = start_x - center_x; const double begin_center_y = start_y - center_y; - const double beginning_center_dist = sqrt(begin_center_x * begin_center_x + begin_center_y * begin_center_y); + const double beginning_center_dist = + sqrt(begin_center_x * begin_center_x + begin_center_y * begin_center_y); // If the first click was too close to the center, move the center of rotation by 200 pixels // in the direction of the click. @@ -125,206 +292,120 @@ void Transform::rotateBy(double start_x, double start_y, double end_x, double en const double first_x = start_x - center_x, first_y = start_y - center_y; const double second_x = end_x - center_x, second_y = end_y - center_y; - const double ang = angle + util::angle_between(first_x, first_y, second_x, second_y); + const double ang = current.angle + util::angle_between(first_x, first_y, second_x, second_y); - setAngle(ang, duration); + _setAngle(ang, duration); } -void Transform::setAngle(double new_angle, double duration) { - while (new_angle > M_PI) new_angle -= M2PI; - while (new_angle <= -M_PI) new_angle += M2PI; +void Transform::setAngle(const double new_angle, const double duration) { + uv::writelock lock(mtx); - if (duration == 0) { - angle = new_angle; - } else { - double start = platform::elapsed(); - animations.emplace_front(std::make_shared(angle, new_angle, angle, start, duration)); + _setAngle(new_angle, duration); +} + +void Transform::setAngle(const double new_angle, const double cx, const double cy) { + uv::writelock lock(mtx); + + double dx = 0, dy = 0; + + if (cx >= 0 && cy >= 0) { + dx = (final.width / 2) - cx; + dy = (final.height / 2) - cy; + _moveBy(dx, dy, 0); + } + + _setAngle(new_angle, 0); + + if (cx >= 0 && cy >= 0) { + _moveBy(-dx, -dy, 0); } } +void Transform::_setAngle(double new_angle, const double duration) { + // This is only called internally, so we don't need a lock here. + + while (new_angle > M_PI) + new_angle -= M2PI; + while (new_angle <= -M_PI) + new_angle += M2PI; + + final.angle = new_angle; + + if (duration == 0) { + current.angle = final.angle; + } else { + double start = (double)util::now() / 1_second; + animations.emplace_front(std::make_shared( + current.angle, final.angle, current.angle, start, duration)); + } +} + +double Transform::getAngle() const { + uv::readlock lock(mtx); + + return final.angle; +} + void Transform::startRotating() { - stopRotating(); + uv::writelock lock(mtx); + + _clearRotating(); // Add a 200ms timeout for resetting this to false - rotating = true; - double start = platform::elapsed(); - rotate_timeout = std::make_shared>(false, rotating, start, 0.2); + current.rotating = true; + double start = (double)util::now() / 1_second; + rotate_timeout = std::make_shared>(false, current.rotating, start, 0.2); animations.emplace_front(rotate_timeout); } void Transform::stopRotating() { - rotating = false; + uv::writelock lock(mtx); + + _clearRotating(); +} + +void Transform::_clearRotating() { + // This is only called internally, so we don't need a lock here. + + current.rotating = false; if (rotate_timeout) { animations.remove(rotate_timeout); rotate_timeout.reset(); } } -void Transform::setScaleXY(double new_scale, double xn, double yn, double duration) { - if (duration == 0) { - scale = new_scale; - x = xn; - y = yn; - } else { - // Use a common start time for all of the animations to avoid divergent animations. - double start = platform::elapsed(); - animations.emplace_front(std::make_shared(scale, new_scale, scale, start, duration)); - animations.emplace_front(std::make_shared(x, xn, x, start, duration)); - animations.emplace_front(std::make_shared(y, yn, y, start, duration)); - } +#pragma mark - Animation - const double s = scale * util::tileSize; - zc = s / 2; - Bc = s / 360; - Cc = s / (2 * M_PI); +bool Transform::needsAnimation() const { + uv::readlock lock(mtx); + + return !animations.empty(); } -void Transform::setScale(double new_scale, double cx, double cy, double duration) { - // Ensure that we don't zoom in further than the maximum allowed. - if (new_scale < min_scale) { - new_scale = min_scale; - } else if (new_scale > max_scale) { - new_scale = max_scale; - } +void Transform::updateAnimations(const double time) { + uv::writelock lock(mtx); - // Zoom in on the center if we don't have click or gesture anchor coordinates. - if (cx < 0 || cy < 0) { - cx = width / 2; - cy = height / 2; - } - - // Account for the x/y offset from the center (= where the user clicked or pinched) - const double factor = new_scale / scale; - const double dx = (cx - width / 2) * (1.0 - factor); - const double dy = (cy - height / 2) * (1.0 - factor); - - // Account for angle - const double angle_sin = sin(-angle); - const double angle_cos = cos(-angle); - const double ax = angle_cos * dx - angle_sin * dy; - const double ay = angle_sin * dx + angle_cos * dy; - - const double xn = x * factor + ax; - const double yn = y * factor + ay; - - setScaleXY(new_scale, xn, yn, duration); + animations.remove_if([time](const std::shared_ptr &animation) { + return animation->update(time) == util::animation::complete; + }); } -void Transform::setZoom(double zoom, double duration) { - setScale(pow(2.0, zoom), -1, -1, duration); +void Transform::cancelAnimations() { + uv::writelock lock(mtx); + + animations.clear(); } -void Transform::setLonLat(double lon, double lat, double duration) { - const double f = fmin(fmax(sin(D2R * lat), -0.9999), 0.9999); - double xn = -round(lon * Bc); - double yn = round(0.5 * Cc * log((1 + f) / (1 - f))); +#pragma mark - Transform state - setScaleXY(scale, xn, yn, duration); +const TransformState Transform::currentState() const { + uv::readlock lock(mtx); + + return current; } -void Transform::setLonLatZoom(double lon, double lat, double zoom, double duration) { - double new_scale = pow(2.0, zoom); +const TransformState Transform::finalState() const { + uv::readlock lock(mtx); - const double s = new_scale * util::tileSize; - zc = s / 2; - Bc = s / 360; - Cc = s / (2 * M_PI); - - const double f = fmin(fmax(sin(D2R * lat), -0.9999), 0.9999); - double xn = -round(lon * Bc); - double yn = round(0.5 * Cc * log((1 + f) / (1 - f))); - - setScaleXY(new_scale, xn, yn, duration); + return final; } - -void Transform::getLonLat(double &lon, double &lat) const { - lon = -x / Bc; - lat = R2D * (2 * atan(exp(y / Cc)) - 0.5 * M_PI); -} - -void Transform::getLonLatZoom(double& lon, double& lat, double& zoom) const { - getLonLat(lon, lat); - zoom = getZoom(); -} - -double Transform::pixel_x() const { - const double center = (width - scale * util::tileSize) / 2; - return center + x; -} - -double Transform::pixel_y() const { - const double center = (height - scale * util::tileSize) / 2; - return center + y; -} - -void Transform::matrixFor(mat4& matrix, const Tile::ID& id) const { - const double tile_scale = pow(2, id.z); - const double tile_size = scale * util::tileSize / tile_scale; - - matrix::identity(matrix); - - matrix::translate(matrix, matrix, 0.5f * (float)width, 0.5f * (float)height, 0); - matrix::rotate_z(matrix, matrix, angle); - matrix::translate(matrix, matrix, -0.5f * (float)width, -0.5f * (float)height, 0); - - matrix::translate(matrix, matrix, pixel_x() + id.x * tile_size, pixel_y() + id.y * tile_size, 0); - - // TODO: Get rid of the 8 (scaling from 4096 to tile size); - float factor = scale / tile_scale / (4096.0f / util::tileSize); - matrix::scale(matrix, matrix, factor, factor, 1); -} - -float Transform::getZoom() const { - return log(scale) / M_LN2; -} - -float Transform::getNormalizedZoom() const { - return log(scale * util::tileSize / 256.0f) / M_LN2; -} - -int32_t Transform::getIntegerZoom() const { - return floor(log(scale) / M_LN2); -} - -double Transform::getScale() const { - return scale; -} - -double Transform::getAngle() const { - return angle; -} - -box Transform::cornersToBox(uint32_t z) const { - const double ref_scale = pow(2, z); - - const double angle_sin = sin(-angle); - const double angle_cos = cos(-angle); - - const double w_2 = width / 2; - const double h_2 = height / 2; - const double ss_0 = scale * util::tileSize; - const double ss_1 = ref_scale / ss_0; - const double ss_2 = ss_0 / 2.0; - - // Calculate the corners of the map view. The resulting coordinates will be - // in fractional tile coordinates. - box b; - - b.tl.x = ((-w_2) * angle_cos - (-h_2) * angle_sin + ss_2 - x) * ss_1; - b.tl.y = ((-w_2) * angle_sin + (-h_2) * angle_cos + ss_2 - y) * ss_1; - - b.tr.x = ((+w_2) * angle_cos - (-h_2) * angle_sin + ss_2 - x) * ss_1; - b.tr.y = ((+w_2) * angle_sin + (-h_2) * angle_cos + ss_2 - y) * ss_1; - - b.bl.x = ((-w_2) * angle_cos - (+h_2) * angle_sin + ss_2 - x) * ss_1; - b.bl.y = ((-w_2) * angle_sin + (+h_2) * angle_cos + ss_2 - y) * ss_1; - - b.br.x = ((+w_2) * angle_cos - (+h_2) * angle_sin + ss_2 - x) * ss_1; - b.br.y = ((+w_2) * angle_sin + (+h_2) * angle_cos + ss_2 - y) * ss_1; - - b.center.x = (ss_2 - x) * ss_1; - b.center.y = (ss_2 - y) * ss_1; - - return b; -} - diff --git a/src/map/transform_state.cpp b/src/map/transform_state.cpp new file mode 100644 index 000000000..c6f8be71b --- /dev/null +++ b/src/map/transform_state.cpp @@ -0,0 +1,132 @@ +#include +#include + +using namespace llmr; + +#pragma mark - Matrix + +void TransformState::matrixFor(mat4& matrix, const Tile::ID& id) const { + const double tile_scale = pow(2, id.z); + const double tile_size = scale * util::tileSize / tile_scale; + + matrix::identity(matrix); + + matrix::translate(matrix, matrix, 0.5f * (float)width, 0.5f * (float)height, 0); + matrix::rotate_z(matrix, matrix, angle); + matrix::translate(matrix, matrix, -0.5f * (float)width, -0.5f * (float)height, 0); + + matrix::translate(matrix, matrix, pixel_x() + id.x * tile_size, pixel_y() + id.y * tile_size, 0); + + // TODO: Get rid of the 8 (scaling from 4096 to tile size); + float factor = scale / tile_scale / (4096.0f / util::tileSize); + matrix::scale(matrix, matrix, factor, factor, 1); +} + +box TransformState::cornersToBox(uint32_t z) const { + const double ref_scale = pow(2, z); + + const double angle_sin = sin(-angle); + const double angle_cos = cos(-angle); + + const double w_2 = width / 2; + const double h_2 = height / 2; + const double ss_0 = scale * util::tileSize; + const double ss_1 = ref_scale / ss_0; + const double ss_2 = ss_0 / 2.0; + + // Calculate the corners of the map view. The resulting coordinates will be + // in fractional tile coordinates. + box b; + + b.tl.x = ((-w_2) * angle_cos - (-h_2) * angle_sin + ss_2 - x) * ss_1; + b.tl.y = ((-w_2) * angle_sin + (-h_2) * angle_cos + ss_2 - y) * ss_1; + + b.tr.x = ((+w_2) * angle_cos - (-h_2) * angle_sin + ss_2 - x) * ss_1; + b.tr.y = ((+w_2) * angle_sin + (-h_2) * angle_cos + ss_2 - y) * ss_1; + + b.bl.x = ((-w_2) * angle_cos - (+h_2) * angle_sin + ss_2 - x) * ss_1; + b.bl.y = ((-w_2) * angle_sin + (+h_2) * angle_cos + ss_2 - y) * ss_1; + + b.br.x = ((+w_2) * angle_cos - (+h_2) * angle_sin + ss_2 - x) * ss_1; + b.br.y = ((+w_2) * angle_sin + (+h_2) * angle_cos + ss_2 - y) * ss_1; + + b.center.x = (ss_2 - x) * ss_1; + b.center.y = (ss_2 - y) * ss_1; + + return b; +} + + +#pragma mark - Dimensions + +uint16_t TransformState::getWidth() const { + return width; +} + +uint16_t TransformState::getHeight() const { + return height; +} + +uint16_t TransformState::getFramebufferWidth() const { + return framebuffer[0]; +} + +uint16_t TransformState::getFramebufferHeight() const { + return framebuffer[1]; +} + +const std::array TransformState::getFramebufferDimensions() const { + return framebuffer; +} + + +float TransformState::getPixelRatio() const { + return pixelRatio; +} + + +#pragma mark - Zoom + +float TransformState::getNormalizedZoom() const { + return log(scale * util::tileSize / 256.0f) / M_LN2; +} + +int32_t TransformState::getIntegerZoom() const { + return floor(getZoom()); +} + +double TransformState::getZoom() const { + return log(scale) / M_LN2; +} + +double TransformState::getScale() const { + return scale; +} + + +#pragma mark - Rotation + +float TransformState::getAngle() const { + return angle; +} + + +#pragma mark - Rotation + +bool TransformState::isChanging() const { + return rotating || scaling || panning; +} + + +#pragma mark - (private helper functions) + + +double TransformState::pixel_x() const { + const double center = (width - scale * util::tileSize) / 2; + return center + x; +} + +double TransformState::pixel_y() const { + const double center = (height - scale * util::tileSize) / 2; + return center + y; +} diff --git a/src/platform/request.cpp b/src/platform/request.cpp index 36b2dc77e..aadf83dea 100644 --- a/src/platform/request.cpp +++ b/src/platform/request.cpp @@ -1,46 +1,46 @@ #include #include +#include #include using namespace llmr::platform; Request::Request(const std::string &url, - std::function background_function, - std::function foreground_callback, + std::function callback, uv_loop_t *loop) : url(url), - background_function(background_function), - foreground_callback(foreground_callback), + res(std::make_unique(callback)), + cancelled(false), loop(loop) { - // Add a check handle without a callback to keep the default loop running. // We don't have a real handler attached to the default loop right from the // beginning, because we're using asynchronous messaging to perform the actual // request in the request thread. Only after the request is complete, we // create an actual work request that is attached to the default loop. - check = new uv_check_t(); - uv_check_init(loop, check); - uv_check_start(check, [](uv_check_t *) {}); + async = new uv_async_t(); + async->data = new std::unique_ptr(); + uv_async_init(loop, async, complete); } Request::~Request() { - // We need to remove our no-op handle again to allow the main event loop to exit. - uv_check_stop(check); } -void Request::work_callback(uv_work_t *work) { - std::shared_ptr *req = static_cast *>(work->data); - (*req)->background_function((*req)->res.get()); +void Request::complete() { + // We're scheduling the response callback to be invoked in the event loop. + // Since the Request object will be deleted before the callback is invoked, + // we move over the Response object to be owned by the async handle. + ((std::unique_ptr *)async->data)->swap(res); + uv_async_send(async); } -// This callback is executed in the main loop. -void Request::after_work_callback(uv_work_t *work, int /*status*/) { - std::shared_ptr *req = static_cast *>(work->data); +void Request::complete(uv_async_t *async) { + Response *res = static_cast *>(async->data)->get(); - (*req)->foreground_callback(); + res->callback(res); - // This finally deletes the *pointer* to the shared pointer we've been holding on since we - // pushed it on the add_queue on request creation. - delete req; - delete work; + // We need to remove our async handle again to allow the main event loop to exit. + uv_close((uv_handle_t *)async, [](uv_handle_t *handle) { + delete static_cast *>(handle->data); + delete (uv_async_t *)handle; + }); } diff --git a/src/renderer/frame_history.cpp b/src/renderer/frame_history.cpp index 2cbc6f1bc..229c15ae4 100644 --- a/src/renderer/frame_history.cpp +++ b/src/renderer/frame_history.cpp @@ -1,4 +1,5 @@ #include +#include using namespace llmr; @@ -11,7 +12,7 @@ void FrameHistory::record(float zoom) { } if (history.size() > 0 || history.back().z != zoom) { - history.emplace_back(FrameSnapshot{static_cast(platform::elapsed() * 1000), zoom}); + history.emplace_back(FrameSnapshot{static_cast((double) util::now() / 1_millisecond), zoom}); } } diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 944a5083b..73b5779cf 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -20,15 +21,14 @@ #include #include #include +#include using namespace llmr; #define BUFFER_OFFSET(i) ((char *)nullptr + (i)) -Painter::Painter(Transform& transform, Style& style, GlyphAtlas& glyphAtlas) - : transform(transform), - style(style), - glyphAtlas(glyphAtlas) { +Painter::Painter(Map &map) + : map(map) { } bool Painter::needsAnimation() const { @@ -79,8 +79,12 @@ void Painter::setupShaders() { } void Painter::resize() { - assert(transform.getFramebufferWidth() > 0 && transform.getFramebufferHeight() > 0); - glViewport(0, 0, transform.getFramebufferWidth(), transform.getFramebufferHeight()); + const TransformState &state = map.getState(); + if (gl_viewport != state.getFramebufferDimensions()) { + gl_viewport = state.getFramebufferDimensions(); + assert(gl_viewport[0] > 0 && gl_viewport[1] > 0); + glViewport(0, 0, gl_viewport[0], gl_viewport[1]); + } } void Painter::setDebug(bool enabled) { @@ -110,12 +114,12 @@ void Painter::depthMask(bool value) { void Painter::changeMatrix() { // Initialize projection matrix - matrix::ortho(projMatrix, 0, transform.getWidth(), transform.getHeight(), 0, 0, 1); + matrix::ortho(projMatrix, 0, map.getState().getWidth(), map.getState().getHeight(), 0, 0, 1); // The extrusion matrix. matrix::identity(extrudeMatrix); matrix::multiply(extrudeMatrix, projMatrix, extrudeMatrix); - matrix::rotate_z(extrudeMatrix, extrudeMatrix, transform.getAngle()); + matrix::rotate_z(extrudeMatrix, extrudeMatrix, map.getState().getAngle()); // The native matrix is a 1:1 matrix that paints the coordinates at the // same screen position as the vertex specifies. @@ -137,11 +141,11 @@ void Painter::drawClippingMask(const mat4& matrix, uint8_t clip_id) { gl::group group("mask"); plainShader->setMatrix(matrix); - Color background = style.computed.background.color; - background[0] *= style.computed.background.opacity; - background[1] *= style.computed.background.opacity; - background[2] *= style.computed.background.opacity; - background[3] *= style.computed.background.opacity; + Color background = map.getStyle().computed.background.color; + background[0] *= map.getStyle().computed.background.opacity; + background[1] *= map.getStyle().computed.background.opacity; + background[2] *= map.getStyle().computed.background.opacity; + background[3] *= map.getStyle().computed.background.opacity; plainShader->setColor(background); @@ -177,12 +181,12 @@ void Painter::render(const Tile& tile) { return; } - frameHistory.record(transform.getNormalizedZoom()); + frameHistory.record(map.getState().getNormalizedZoom()); matrix = tile.matrix; glStencilFunc(GL_EQUAL, tile.clip_id, 0xFF); - renderLayers(tile.data, style.layers); + renderLayers(tile.data, map.getStyle().layers); if (debug) { renderDebug(tile.data); @@ -234,15 +238,18 @@ void Painter::renderLayer(const std::shared_ptr& tile_data, const Laye } else { // This is a singular layer. Try to find the bucket associated with // this layer and render it. - if (style.buckets[layer_desc.bucket_name].type == BucketType::Raster) { - if (tile_data && tile_data->raster) { - renderRaster(layer_desc.name, tile_data); - } - } else { - auto bucket_it = tile_data->buckets.find(layer_desc.bucket_name); - if (bucket_it != tile_data->buckets.end()) { - assert(bucket_it->second); - bucket_it->second->render(*this, layer_desc.name, tile_data->id); + auto bucket_it = map.getStyle().buckets.find(layer_desc.bucket_name); + if (bucket_it != map.getStyle().buckets.end()) { + if (bucket_it->second.type == BucketType::Raster) { + if (tile_data && tile_data->raster) { + renderRaster(layer_desc.name, tile_data); + } + } else { + auto databucket_it = tile_data->buckets.find(layer_desc.bucket_name); + if (databucket_it != tile_data->buckets.end()) { + assert(databucket_it->second); + databucket_it->second->render(*this, layer_desc.name, tile_data->id); + } } } } @@ -251,7 +258,11 @@ void Painter::renderLayer(const std::shared_ptr& tile_data, const Laye void Painter::renderRaster(const std::string& layer_name, const std::shared_ptr& tile_data) { if (pass == Opaque) return; - const RasterProperties& properties = style.computed.rasters[layer_name]; + auto raster_properties = map.getStyle().computed.rasters; + auto raster_properties_it = raster_properties.find(layer_name); + if (raster_properties_it == raster_properties.end()) return; + + const RasterProperties& properties = raster_properties_it->second; if (!properties.enabled) return; gl::group group(layer_name + " (raster)"); @@ -271,7 +282,11 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // Abort early. if (bucket.empty()) return; - const FillProperties& properties = style.computed.fills[layer_name]; + auto fill_properties = map.getStyle().computed.fills; + auto fill_properties_it = fill_properties.find(layer_name); + if (fill_properties_it == fill_properties.end()) return; + + const FillProperties& properties = fill_properties_it->second; if (!properties.enabled) return; Color fill_color = properties.fill_color; @@ -305,8 +320,8 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // Draw the entire line outlineShader->setWorld({{ - static_cast(transform.getFramebufferWidth()), - static_cast(transform.getFramebufferHeight()) + static_cast(map.getState().getFramebufferWidth()), + static_cast(map.getState().getFramebufferHeight()) }}); glDepthRange(strata, 1.0f); bucket.drawVertices(*outlineShader); @@ -329,12 +344,13 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons } if ((fill_color[3] >= 1.0f) == (pass == Opaque)) { - if (properties.image.size() && *style.sprite) { + auto &sprite = map.getStyle().sprite; + if (properties.image.size() && sprite && sprite->isLoaded()) { // Draw texture fill - ImagePosition imagePos = style.sprite->getPosition(properties.image, true); + ImagePosition imagePos = sprite->getPosition(properties.image, true); - float factor = 8.0 / pow(2, transform.getIntegerZoom() - id.z); - float mix = fmod(transform.getZoom(), 1.0); + float factor = 8.0 / pow(2, map.getState().getIntegerZoom() - id.z); + float mix = fmod(map.getState().getZoom(), 1.0); std::array imageSize = {{ imagePos.size.x * factor, @@ -356,7 +372,7 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons patternShader->setPatternBottomRight({{ imagePos.br.x, imagePos.br.y }}); patternShader->setColor(fill_color); patternShader->setMix(mix); - style.sprite->raster->bind(true); + sprite->raster->bind(true); // Draw the actual triangles into the color & stencil buffer. glDepthRange(strata + strata_epsilon, 1.0f); @@ -387,8 +403,8 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // Draw the entire line outlineShader->setWorld({{ - static_cast(transform.getFramebufferWidth()), - static_cast(transform.getFramebufferHeight()) + static_cast(map.getState().getFramebufferWidth()), + static_cast(map.getState().getFramebufferHeight()) }}); glDepthRange(strata + strata_epsilon, 1.0f); @@ -401,7 +417,11 @@ void Painter::renderLine(LineBucket& bucket, const std::string& layer_name, cons if (pass == Opaque) return; if (bucket.empty()) return; - const LineProperties& properties = style.computed.lines[layer_name]; + auto line_properties = map.getStyle().computed.lines; + auto line_properties_it = line_properties.find(layer_name); + if (line_properties_it == line_properties.end()) return; + + const LineProperties& properties = line_properties_it->second; if (!properties.enabled) return; float width = properties.width; @@ -428,17 +448,17 @@ void Painter::renderLine(LineBucket& bucket, const std::string& layer_name, cons linejoinShader->setMatrix(matrix); linejoinShader->setColor(color); linejoinShader->setWorld({{ - transform.getFramebufferWidth() * 0.5f, - transform.getFramebufferHeight() * 0.5f + map.getState().getFramebufferWidth() * 0.5f, + map.getState().getFramebufferHeight() * 0.5f } }); linejoinShader->setLineWidth({{ - ((outset - 0.25f) * transform.getPixelRatio()), - ((inset - 0.25f) * transform.getPixelRatio()) + ((outset - 0.25f) * map.getState().getPixelRatio()), + ((inset - 0.25f) * map.getState().getPixelRatio()) } }); - float pointSize = ceil(transform.getPixelRatio() * outset * 2.0); + float pointSize = ceil(map.getState().getPixelRatio() * outset * 2.0); #if defined(GL_ES_VERSION_2_0) linejoinShader->setSize(pointSize); #else @@ -468,7 +488,7 @@ void Painter::renderLine(LineBucket& bucket, const std::string& layer_name, cons lineShader->setExtrudeMatrix(extrudeMatrix); // TODO: Move this to transform? - const float tilePixelRatio = transform.getScale() / (1 << transform.getIntegerZoom()) / 8; + const float tilePixelRatio = map.getState().getScale() / (1 << map.getState().getIntegerZoom()) / 8; lineShader->setDashArray({{ 1, -1 }}); lineShader->setLineWidth({{ outset, inset }}); @@ -483,9 +503,16 @@ void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, co if (!bucket.hasPoints()) return; if (pass == Opaque) return; - const PointProperties& properties = style.computed.points[layer_name]; + auto point_properties = map.getStyle().computed.points; + auto point_properties_it = point_properties.find(layer_name); + if (point_properties_it == point_properties.end()) return; + + const PointProperties& properties = point_properties_it->second; if (!properties.enabled) return; + auto &sprite = map.getStyle().sprite; + if (!sprite || !sprite->isLoaded()) return; + gl::group group(layer_name + " (point)"); Color color = properties.color; @@ -498,7 +525,7 @@ void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, co sized_image.append("-"); sized_image.append(std::to_string(static_cast(std::round(properties.size)))); - ImagePosition imagePos = style.sprite->getPosition(sized_image, false); + ImagePosition imagePos = sprite->getPosition(sized_image, false); // fprintf(stderr, "%f/%f => %f/%f\n", imagePos.tl.x, imagePos.tl.y, imagePos.br.x, imagePos.br.y); @@ -506,7 +533,7 @@ void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, co pointShader->setMatrix(matrix); pointShader->setImage(0); pointShader->setColor(color); - const float pointSize = properties.size * 1.4142135623730951 * transform.getPixelRatio(); + const float pointSize = properties.size * 1.4142135623730951 * map.getState().getPixelRatio(); #if defined(GL_ES_VERSION_2_0) pointShader->setSize(pointSize); #else @@ -515,8 +542,8 @@ void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, co #endif pointShader->setPointTopLeft({{ imagePos.tl.x, imagePos.tl.y }}); pointShader->setPointBottomRight({{ imagePos.br.x, imagePos.br.y }}); - if (*style.sprite->raster) { - style.sprite->raster->bind(transform.rotating || transform.scaling || transform.panning); + if (*sprite->raster) { + sprite->raster->bind(map.getState().isChanging()); } glDepthRange(strata, 1.0f); bucket.drawPoints(*pointShader); @@ -527,7 +554,11 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons if (pass == Opaque) return; if (bucket.empty()) return; - const TextProperties& properties = style.computed.texts[layer_name]; + auto text_properties = map.getStyle().computed.texts; + auto text_properties_it = text_properties.find(layer_name); + if (text_properties_it == text_properties.end()) return; + + const TextProperties& properties = text_properties_it->second; if (!properties.enabled) return; gl::group group(layer_name + " (text)"); @@ -535,7 +566,7 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons mat4 exMatrix; matrix::copy(exMatrix, projMatrix); if (bucket.geom_desc.path == TextPathType::Curve) { - matrix::rotate_z(exMatrix, exMatrix, transform.getAngle()); + matrix::rotate_z(exMatrix, exMatrix, map.getState().getAngle()); } const float rotate = properties.rotate; @@ -551,24 +582,25 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons textShader->setMatrix(matrix); textShader->setExtrudeMatrix(exMatrix); - glyphAtlas.bind(); - textShader->setTextureSize({{ static_cast(glyphAtlas.width), static_cast(glyphAtlas.height) }}); + map.getGlyphAtlas().bind(); + textShader->setTextureSize({{static_cast(map.getGlyphAtlas().width), + static_cast(map.getGlyphAtlas().height)}}); - textShader->setGamma(2.5f / fontSize / transform.getPixelRatio()); + textShader->setGamma(2.5f / fontSize / map.getState().getPixelRatio()); // Convert the -pi..pi to an int8 range. - float angle = round((transform.getAngle() + rotate) / M_PI * 128); + float angle = round((map.getState().getAngle() + rotate) / M_PI * 128); // adjust min/max zooms for variable font sies float zoomAdjust = log(fontSize / bucket.geom_desc.font_size) / log(2); textShader->setAngle((int32_t)(angle + 256) % 256); textShader->setFlip(bucket.geom_desc.path == TextPathType::Curve ? 1 : 0); - textShader->setZoom((transform.getNormalizedZoom() - zoomAdjust) * 10); // current zoom level + textShader->setZoom((map.getState().getNormalizedZoom() - zoomAdjust) * 10); // current zoom level // Label fading const float duration = 300.0f; - const float currentTime = platform::elapsed() * 1000; + const float currentTime = util::now() / 1_millisecond; std::deque &history = frameHistory.history; @@ -606,7 +638,7 @@ void Painter::renderText(TextBucket& bucket, const std::string& layer_name, cons textShader->setFadeDist(fadedist * 10); textShader->setMinFadeZoom(floor(lowZ * 10)); textShader->setMaxFadeZoom(floor(highZ * 10)); - textShader->setFadeZoom((transform.getZoom() + bump) * 10); + textShader->setFadeZoom((map.getState().getZoom() + bump) * 10); // We're drawing in the translucent pass which is bottom-to-top, so we need // to draw the halo first. @@ -638,16 +670,16 @@ void Painter::renderDebug(const TileData::Ptr& tile_data) { // draw tile outline tileBorderArray.bind(*plainShader, tileBorderBuffer, BUFFER_OFFSET(0)); plainShader->setColor(1.0f, 0.0f, 0.0f, 1.0f); - lineWidth(4.0f * transform.getPixelRatio()); + lineWidth(4.0f * map.getState().getPixelRatio()); glDrawArrays(GL_LINE_STRIP, 0, (GLsizei)tileBorderBuffer.index()); // draw debug info tile_data->debugFontArray.bind(*plainShader, tile_data->debugFontBuffer, BUFFER_OFFSET(0)); plainShader->setColor(1.0f, 1.0f, 1.0f, 1.0f); - lineWidth(4.0f * transform.getPixelRatio()); + lineWidth(4.0f * map.getState().getPixelRatio()); glDrawArrays(GL_LINES, 0, (GLsizei)tile_data->debugFontBuffer.index()); plainShader->setColor(0.0f, 0.0f, 0.0f, 1.0f); - lineWidth(2.0f * transform.getPixelRatio()); + lineWidth(2.0f * map.getState().getPixelRatio()); glDrawArrays(GL_LINES, 0, (GLsizei)tile_data->debugFontBuffer.index()); glEnable(GL_DEPTH_TEST); diff --git a/src/style/sprite.cpp b/src/style/sprite.cpp index f46457cb9..74aa9fa73 100644 --- a/src/style/sprite.cpp +++ b/src/style/sprite.cpp @@ -4,6 +4,7 @@ #include #include +#include #include @@ -24,46 +25,56 @@ ImagePosition::ImagePosition(const vec2& size, vec2 tl, vec2()), map(map) {} void Sprite::load(const std::string& base_url) { - std::shared_ptr sprite = shared_from_this(); + std::shared_ptr sprite = shared_from_this(); - auto complete = [sprite]() { - std::lock_guard lock(sprite->mtx); - if (*sprite->raster && sprite->pos.size()) { - sprite->loaded = true; - sprite->map.redraw(); - fprintf(stderr, "sprite loaded\n"); - } - }; + std::string suffix = (pixelRatio > 1 ? "@2x" : ""); - std::string suffix = (pixelRatio > 1 ? "@2x" : ""); + platform::request_http(base_url + suffix + ".json", [sprite](platform::Response *res) { + if (res->code == 200) { + sprite->body.swap(res->body); + sprite->asyncParseJSON(); + } else { + fprintf(stderr, "failed to load sprite\n"); + } + }, map.getLoop()); - platform::request_http(base_url + suffix + ".json", [sprite](const platform::Response *res) { - if (res->code == 200) { - sprite->parseJSON(res->body); - } else { - fprintf(stderr, "failed to load sprite\n"); - } - }, complete, map.getLoop()); - - platform::request_http(base_url + suffix + ".png", [sprite](const platform::Response *res) { - if (res->code == 200) { + platform::request_http(base_url + suffix + ".png", [sprite](platform::Response *res) { + if (res->code == 200) { + // TODO: move raster loading to other thread + // TODO: call complete handler sprite->raster->load(res->body); - } else { - fprintf(stderr, "failed to load sprite image\n"); - } - }, complete, map.getLoop()); + } else { + fprintf(stderr, "failed to load sprite image\n"); + } + }, map.getLoop()); } -Sprite::operator bool() const { +void Sprite::complete(std::shared_ptr &sprite) { + std::lock_guard lock(sprite->mtx); + if (*sprite->raster && sprite->pos.size()) { + sprite->loaded = true; + sprite->map.update(); + fprintf(stderr, "sprite loaded\n"); + } +} + +bool Sprite::isLoaded() const { std::lock_guard lock(mtx); return loaded; } -void Sprite::parseJSON(const std::string& data) { - std::lock_guard lock(mtx); +void Sprite::asyncParseJSON() { + new uv::work>(map.getLoop(), + parseJSON, + complete, + shared_from_this()); +} + +void Sprite::parseJSON(std::shared_ptr &sprite) { + std::lock_guard lock(sprite->mtx); rapidjson::Document d; - d.Parse<0>(data.c_str()); + d.Parse<0>(sprite->body.c_str()); if (d.IsObject()) { for (rapidjson::Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) { @@ -83,7 +94,7 @@ void Sprite::parseJSON(const std::string& data) { if (value.HasMember("height")) height = value["height"].GetInt(); if (value.HasMember("pixelRatio")) pixelRatio = value["height"].GetInt(); - pos.insert({ name, { x, y, width, height, pixelRatio } }); + sprite->pos.insert({ name, { x, y, width, height, pixelRatio } }); } } } diff --git a/src/util/raster.cpp b/src/util/raster.cpp index 0de63f125..31eae5951 100644 --- a/src/util/raster.cpp +++ b/src/util/raster.cpp @@ -6,6 +6,7 @@ #include #include +#include #include @@ -182,7 +183,7 @@ void Raster::bind(bool linear) { } void Raster::beginFadeInAnimation() { - double start = platform::elapsed(); + double start = (double)util::now() / 1_second; fade_animation = std::make_shared(opacity, 1.0, opacity, start, 0.25); } diff --git a/src/util/timer.cpp b/src/util/timer.cpp new file mode 100644 index 000000000..56ad4fbbe --- /dev/null +++ b/src/util/timer.cpp @@ -0,0 +1,35 @@ +#ifndef DISABLE_TIMER +#include +#include + +#include +#include + +using namespace llmr::util; + +std::atomic indent; + +timer::timer() : start(now()) { + indent++; +} + +timer::timer(const std::string &name) : name(name), start(now()) { + indent++; +} + +void timer::report(const std::string &name) { + time duration = now() - start; + std::cerr << std::string((indent - 1) * 4, ' ') + << name << ": " + << (double)(duration) / 1_millisecond + << "ms" << std::endl; + start += duration; +} + +timer::~timer() { + if (name.size()) { + report(name); + } + indent--; +} +#endif diff --git a/test/headless.cpp b/test/headless.cpp index 0da898245..c515b36c9 100644 --- a/test/headless.cpp +++ b/test/headless.cpp @@ -11,23 +11,24 @@ #include -double llmr::platform::elapsed() { - return 0; -} +class View : public llmr::View { +public: + void make_active() { + CGLError error = CGLSetCurrentContext(gl_context); + if (error) { + fprintf(stderr, "Switching OpenGL context failed\n"); + } + } + void swap() {} -void llmr::platform::restart() { - // noop -} +CGLContextObj gl_context; +}; TEST(Headless, initialize) { - llmr::Settings settings; - - llmr::Map map(settings); - llmr::util::timer timer; // Setup OpenGL - CGLContextObj gl_context; + View view; // TODO: test if OpenGL 4.1 with GL_ARB_ES2_compatibility is supported // If it is, use kCGLOGLPVersion_3_2_Core and enable that extension. @@ -46,18 +47,14 @@ TEST(Headless, initialize) { return; } - error = CGLCreateContext(pixelFormat, NULL, &gl_context); + error = CGLCreateContext(pixelFormat, NULL, &view.gl_context); CGLDestroyPixelFormat(pixelFormat); if (error) { fprintf(stderr, "Error creating GL context object\n"); return; } - error = CGLSetCurrentContext(gl_context); - if (error) { - fprintf(stderr, "Switching OpenGL context failed\n"); - return; - } + view.make_active(); timer.report("gl setup"); @@ -102,31 +99,18 @@ TEST(Headless, initialize) { timer.report("gl framebuffer"); - map.setup(); - timer.report("map setup"); + llmr::Map map(view); map.resize(width, height); timer.report("map resize"); - map.loadSettings(); - - timer.report("map settings"); - - map.update(); - - timer.report("map update"); - // Run the loop. It will terminate when we don't have any further listeners. map.run(); timer.report("map loop"); - map.render(); - - timer.report("map render"); - uint32_t *pixels = new uint32_t[width * height]; glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); @@ -149,7 +133,7 @@ TEST(Headless, initialize) { glDeleteTextures(1, &fbo_color); glDeleteRenderbuffersEXT(1, &fbo_depth_stencil); - CGLDestroyContext(gl_context); + CGLDestroyContext(view.gl_context); timer.report("destruct"); } From fcfd506a3cbce432cfa30dd70a6db65ee2e0fe3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 30 Apr 2014 12:09:38 +0200 Subject: [PATCH 06/10] fix rendering + build --- src/map/map.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/map/map.cpp b/src/map/map.cpp index feaf14f6c..861c373bd 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -72,7 +72,7 @@ void Map::delete_async(uv_handle_t *handle) { void Map::run() { setup(); - prepareRender(); + prepare(); uv_run(loop, UV_RUN_DEFAULT); // If the map rendering wasn't started asynchronously, we perform one render @@ -102,6 +102,7 @@ void Map::render(uv_async_t *async) { if (map->rendered.test_and_set() == false) { if (map->clean.test_and_set() == false) { + map->render(); map->swapped.clear(); map->view.swap(); } else { From f683cbf02ad62867834fe7f703de74954f25ea4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 30 Apr 2014 12:29:50 +0200 Subject: [PATCH 07/10] hide atomic flags as implementation detail --- common/glfw_view.cpp | 5 ++--- include/llmr/map/map.hpp | 12 ++++++++---- ios/MBXViewController.mm | 5 ++--- src/map/map.cpp | 25 +++++++++++++++++-------- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/common/glfw_view.cpp b/common/glfw_view.cpp index cfa1156a1..321670b66 100644 --- a/common/glfw_view.cpp +++ b/common/glfw_view.cpp @@ -163,11 +163,10 @@ int MapView::run() { map.start(); while (!glfwWindowShouldClose(window)) { - if (map.swapped.test_and_set() == false) { + if (map.needsSwap()) { glfwSwapBuffers(window); - map.rendered.clear(); + map.swapped(); fps(); - map.rerender(); } glfwWaitEvents(); diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index c1cc7c370..3dfc830a3 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -39,6 +39,10 @@ public: // Forces a map update: always triggers a rerender. void update(); + // Controls buffer swapping. + bool needsSwap(); + void swapped(); + // Size void resize(uint16_t width, uint16_t height, float ratio = 1); void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height); @@ -105,18 +109,18 @@ private: // Unconditionally performs a render with the current map state. void render(); -public: +private: // If cleared, the next time the render thread attempts to render the map, it will *actually* // render the map. - std::atomic_flag clean = ATOMIC_FLAG_INIT; + std::atomic_flag is_clean = ATOMIC_FLAG_INIT; // If this flag is cleared, the current back buffer is ready for being swapped with the front // buffer (i.e. it has rendered data). - std::atomic_flag swapped = ATOMIC_FLAG_INIT; + std::atomic_flag is_swapped = ATOMIC_FLAG_INIT; // This is cleared once the current front buffer has been presented and the back buffer is // ready for rendering. - std::atomic_flag rendered = ATOMIC_FLAG_INIT; + std::atomic_flag is_rendered = ATOMIC_FLAG_INIT; private: View &view; diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index c6f3b1bc4..ca3b65621 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -153,11 +153,10 @@ MBXViewController *viewController = nullptr; - (void)swap { - if (map->swapped.test_and_set() == false) + if (map->needsSwap()) { [(GLKView *)self.view display]; - map->rendered.clear(); - map->rerender(); + map->swapped(); } } diff --git a/src/map/map.cpp b/src/map/map.cpp index 861c373bd..df1e5ac38 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -21,9 +21,9 @@ Map::Map(View& view) loop(uv_loop_new()) { // Make sure that we're doing an initial drawing in all cases. - clean.clear(); - rendered.clear(); - swapped.test_and_set(); + is_clean.clear(); + is_rendered.clear(); + is_swapped.test_and_set(); } Map::~Map() { @@ -91,7 +91,16 @@ void Map::rerender() { } void Map::update() { - clean.clear(); + is_clean.clear(); + rerender(); +} + +bool Map::needsSwap() { + return is_swapped.test_and_set() == false; +} + +void Map::swapped() { + is_rendered.clear(); rerender(); } @@ -100,15 +109,15 @@ void Map::render(uv_async_t *async) { map->prepare(); - if (map->rendered.test_and_set() == false) { - if (map->clean.test_and_set() == false) { + if (map->is_rendered.test_and_set() == false) { + if (map->is_clean.test_and_set() == false) { map->render(); - map->swapped.clear(); + map->is_swapped.clear(); map->view.swap(); } else { // We set the rendered flag in the test above, so we have to reset it // now that we're not actually rendering because the map is clean. - map->rendered.clear(); + map->is_rendered.clear(); } } } From 74768abeb8a22205a137c19eeff8b8f50e9ad136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 30 Apr 2014 12:58:38 +0200 Subject: [PATCH 08/10] make sure we call the sprite load callback --- include/llmr/style/sprite.hpp | 2 ++ include/llmr/util/raster.hpp | 11 +++++++++-- src/map/tile_parser.cpp | 3 ++- src/renderer/painter.cpp | 2 +- src/style/sprite.cpp | 22 +++++++++++++--------- src/util/raster.cpp | 19 +++++++++++-------- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/include/llmr/style/sprite.hpp b/include/llmr/style/sprite.hpp index fa0f86ecf..ea1a29dff 100644 --- a/include/llmr/style/sprite.hpp +++ b/include/llmr/style/sprite.hpp @@ -49,8 +49,10 @@ public: private: void asyncParseJSON(); + void asyncParseImage(); static void parseJSON(std::shared_ptr &sprite); + static void parseImage(std::shared_ptr &sprite); static void complete(std::shared_ptr &sprite); private: diff --git a/include/llmr/util/raster.hpp b/include/llmr/util/raster.hpp index afb37b30b..229d32c01 100644 --- a/include/llmr/util/raster.hpp +++ b/include/llmr/util/raster.hpp @@ -8,6 +8,8 @@ #include #include +typedef struct uv_loop_s uv_loop_t; + namespace llmr { class Raster : public std::enable_shared_from_this { @@ -16,7 +18,7 @@ public: ~Raster(); // load image data - void load(const std::string& data); + void load(); // set shared texture pool void setTexturepool(Texturepool* texturepool); @@ -25,13 +27,15 @@ public: void bind(bool linear = false); // loaded status - operator bool() const; + bool isLoaded() const; // animations void beginFadeInAnimation(); bool needsAnimation() const; void updateAnimations(double time); + inline void setData(const std::string &img) { data = img; } + public: // loaded image dimensions uint32_t width = 0, height = 0; @@ -61,6 +65,9 @@ private: // min/mag filter uint32_t filter = 0; + // the encoded image data. + std::string data; + // the raw pixels char *img = nullptr; diff --git a/src/map/tile_parser.cpp b/src/map/tile_parser.cpp index ed8ad14e4..48fc24718 100644 --- a/src/map/tile_parser.cpp +++ b/src/map/tile_parser.cpp @@ -84,7 +84,8 @@ std::unique_ptr TileParser::createBucket(const BucketDescription& bucket if (bucket_desc.type == BucketType::Raster) { if (raster_data.length()) { tile.raster = std::make_shared(); - tile.raster->load(raster_data); + tile.raster->setData(raster_data); + tile.raster->load(); } return createRasterBucket(bucket_desc); } else { diff --git a/src/renderer/painter.cpp b/src/renderer/painter.cpp index 73b5779cf..f024fd386 100644 --- a/src/renderer/painter.cpp +++ b/src/renderer/painter.cpp @@ -542,7 +542,7 @@ void Painter::renderPoint(PointBucket& bucket, const std::string& layer_name, co #endif pointShader->setPointTopLeft({{ imagePos.tl.x, imagePos.tl.y }}); pointShader->setPointBottomRight({{ imagePos.br.x, imagePos.br.y }}); - if (*sprite->raster) { + if (sprite->raster->isLoaded()) { sprite->raster->bind(map.getState().isChanging()); } glDepthRange(strata, 1.0f); diff --git a/src/style/sprite.cpp b/src/style/sprite.cpp index 74aa9fa73..408ed11f0 100644 --- a/src/style/sprite.cpp +++ b/src/style/sprite.cpp @@ -40,9 +40,8 @@ void Sprite::load(const std::string& base_url) { platform::request_http(base_url + suffix + ".png", [sprite](platform::Response *res) { if (res->code == 200) { - // TODO: move raster loading to other thread - // TODO: call complete handler - sprite->raster->load(res->body); + sprite->raster->setData(res->body); + sprite->asyncParseImage(); } else { fprintf(stderr, "failed to load sprite image\n"); } @@ -51,7 +50,7 @@ void Sprite::load(const std::string& base_url) { void Sprite::complete(std::shared_ptr &sprite) { std::lock_guard lock(sprite->mtx); - if (*sprite->raster && sprite->pos.size()) { + if (sprite->raster && sprite->raster->isLoaded() && sprite->pos.size()) { sprite->loaded = true; sprite->map.update(); fprintf(stderr, "sprite loaded\n"); @@ -63,11 +62,16 @@ bool Sprite::isLoaded() const { return loaded; } +void Sprite::asyncParseImage() { + new uv::work>(map.getLoop(), parseImage, complete, shared_from_this()); +} + void Sprite::asyncParseJSON() { - new uv::work>(map.getLoop(), - parseJSON, - complete, - shared_from_this()); + new uv::work>(map.getLoop(), parseJSON, complete, shared_from_this()); +} + +void Sprite::parseImage(std::shared_ptr &sprite) { + sprite->raster->load(); } void Sprite::parseJSON(std::shared_ptr &sprite) { @@ -101,7 +105,7 @@ void Sprite::parseJSON(std::shared_ptr &sprite) { } ImagePosition Sprite::getPosition(const std::string& name, bool repeating) { - if (!*this->raster) return {}; + if (!this->raster || !this->raster->isLoaded()) return {}; // `repeating` indicates that the image will be used in a repeating pattern // repeating pattern images are assumed to have a 1px padding that mirrors the opposite edge diff --git a/src/util/raster.cpp b/src/util/raster.cpp index 31eae5951..7741b9f53 100644 --- a/src/util/raster.cpp +++ b/src/util/raster.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -20,20 +21,22 @@ Raster::~Raster() { } } -Raster::operator bool() const { +bool Raster::isLoaded() const { std::lock_guard lock(mtx); return loaded; } -void Raster::load(const std::string& data) { - std::shared_ptr raster = shared_from_this(); +void Raster::load() { + loadImage(data); - raster->loadImage(data); - - std::lock_guard lock(raster->mtx); - if (raster->img) { - raster->loaded = true; + { + std::lock_guard lock(mtx); + if (img) { + loaded = true; + } } + + data.clear(); } struct Buffer { From e95ece83335b85ed063f8ea903acc71d5a2fb995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 30 Apr 2014 13:05:04 +0200 Subject: [PATCH 09/10] fix sprite rendering --- src/map/map.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/map/map.cpp b/src/map/map.cpp index df1e5ac38..238d6b72f 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -360,6 +360,10 @@ void Map::prepare() { style.sprite->load(kSpriteURL); } + if (style.sprite && style.sprite->raster->isLoaded() && !style.sprite->raster->textured) { + style.sprite->raster->setTexturepool(&texturepool); + } + updateTiles(); } From 9bbdba9eab28c2ba44002bada4b3bb1c0962bd90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 30 Apr 2014 13:47:03 +0200 Subject: [PATCH 10/10] remove concept of settings object from library --- common/glfw_view.cpp | 97 +++++++++---------- common/glfw_view.hpp | 11 +-- .../settings.cpp => common/settings_json.cpp | 31 +++--- common/settings_json.hpp | 24 +++++ common/settings_nsuserdefaults.hpp | 24 +++++ .../settings_nsuserdefaults.mm | 21 ++-- include/llmr/llmr.hpp | 1 - include/llmr/map/settings.hpp | 27 ------ include/llmr/map/view.hpp | 9 ++ ios/MBXSettings.h | 22 ----- ios/MBXSettings.mm | 52 ---------- ios/MBXViewController.mm | 36 ++++--- ios/llmr-app.gyp | 4 +- linux/llmr-app.gyp | 4 +- linux/main.cpp | 29 ++++-- linux/settings.hpp | 19 ---- macosx/llmr-app.gyp | 4 +- macosx/main.mm | 27 ++++-- macosx/settings.hpp | 19 ---- src/map/map.cpp | 2 + 20 files changed, 198 insertions(+), 265 deletions(-) rename linux/settings.cpp => common/settings_json.cpp (70%) create mode 100644 common/settings_json.hpp create mode 100644 common/settings_nsuserdefaults.hpp rename macosx/settings.mm => common/settings_nsuserdefaults.mm (81%) delete mode 100644 include/llmr/map/settings.hpp delete mode 100644 ios/MBXSettings.h delete mode 100644 ios/MBXSettings.mm delete mode 100644 linux/settings.hpp delete mode 100644 macosx/settings.hpp diff --git a/common/glfw_view.cpp b/common/glfw_view.cpp index 321670b66..4bdac5f6b 100644 --- a/common/glfw_view.cpp +++ b/common/glfw_view.cpp @@ -1,11 +1,12 @@ #include "glfw_view.hpp" -MapView::MapView(llmr::Settings &settings, bool fullscreen) - : fullscreen(fullscreen), settings(settings), map(*this) {} +GLFWView::GLFWView(bool fullscreen) : fullscreen(fullscreen) {} -MapView::~MapView() { glfwTerminate(); } +GLFWView::~GLFWView() { glfwTerminate(); } + +void GLFWView::initialize(llmr::Map *map) { + View::initialize(map); -void MapView::init() { if (!glfwInit()) { fprintf(stderr, "Failed to initialize glfw\n"); exit(1); @@ -44,8 +45,6 @@ void MapView::init() { int fb_width, fb_height; glfwGetFramebufferSize(window, &fb_width, &fb_height); - settings.load(); - resize(window, 0, 0); glfwSetCursorPosCallback(window, mousemove); @@ -58,8 +57,8 @@ void MapView::init() { glfwMakeContextCurrent(nullptr); } -void MapView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int mods) { - MapView *mapView = (MapView *)glfwGetWindowUserPointer(window); +void GLFWView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int mods) { + GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); if (action == GLFW_RELEASE) { switch (key) { @@ -67,26 +66,26 @@ void MapView::key(GLFWwindow *window, int key, int /*scancode*/, int action, int glfwSetWindowShouldClose(window, true); break; case GLFW_KEY_TAB: - mapView->map.toggleDebug(); + view->map->toggleDebug(); break; case GLFW_KEY_X: if (!mods) - mapView->map.resetPosition(); + view->map->resetPosition(); break; case GLFW_KEY_R: if (!mods) - mapView->map.toggleRaster(); + view->map->toggleRaster(); break; case GLFW_KEY_N: if (!mods) - mapView->map.resetNorth(); + view->map->resetNorth(); break; } } } -void MapView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) { - MapView *mapView = (MapView *)glfwGetWindowUserPointer(window); +void GLFWView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) { + GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); double delta = yoffset * 40; bool is_wheel = delta != 0 && fmod(delta, 4.000244140625) == 0; @@ -104,88 +103,88 @@ void MapView::scroll(GLFWwindow *window, double /*xoffset*/, double yoffset) { scale = 1.0 / scale; } - mapView->map.startScaling(); - mapView->map.scaleBy(scale, mapView->last_x, mapView->last_y); + view->map->startScaling(); + view->map->scaleBy(scale, view->last_x, view->last_y); } -void MapView::resize(GLFWwindow *window, int, int) { - MapView *mapView = (MapView *)glfwGetWindowUserPointer(window); +void GLFWView::resize(GLFWwindow *window, int, int) { + GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); int width, height; glfwGetWindowSize(window, &width, &height); int fb_width, fb_height; glfwGetFramebufferSize(window, &fb_width, &fb_height); - mapView->map.resize(width, height, (float)fb_width / (float)width, fb_width, fb_height); + view->map->resize(width, height, (float)fb_width / (float)width, fb_width, fb_height); } -void MapView::mouseclick(GLFWwindow *window, int button, int action, int modifiers) { - MapView *mapView = (MapView *)glfwGetWindowUserPointer(window); +void GLFWView::mouseclick(GLFWwindow *window, int button, int action, int modifiers) { + GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); if (button == GLFW_MOUSE_BUTTON_RIGHT || (button == GLFW_MOUSE_BUTTON_LEFT && modifiers & GLFW_MOD_CONTROL)) { - mapView->rotating = action == GLFW_PRESS; - if (!mapView->rotating) { - mapView->map.stopRotating(); + view->rotating = action == GLFW_PRESS; + if (!view->rotating) { + view->map->stopRotating(); } } else if (button == GLFW_MOUSE_BUTTON_LEFT) { - mapView->tracking = action == GLFW_PRESS; + view->tracking = action == GLFW_PRESS; if (action == GLFW_RELEASE) { - mapView->map.stopPanning(); + view->map->stopPanning(); double now = glfwGetTime(); - if (now - mapView->last_click < 0.4 /* ms */) { - mapView->map.scaleBy(2.0, mapView->last_x, mapView->last_y, 0.5); + if (now - view->last_click < 0.4 /* ms */) { + view->map->scaleBy(2.0, view->last_x, view->last_y, 0.5); } - mapView->last_click = now; + view->last_click = now; } } } -void MapView::mousemove(GLFWwindow *window, double x, double y) { - MapView *mapView = (MapView *)glfwGetWindowUserPointer(window); - if (mapView->tracking) { - double dx = x - mapView->last_x; - double dy = y - mapView->last_y; +void GLFWView::mousemove(GLFWwindow *window, double x, double y) { + GLFWView *view = (GLFWView *)glfwGetWindowUserPointer(window); + if (view->tracking) { + double dx = x - view->last_x; + double dy = y - view->last_y; if (dx || dy) { - mapView->map.startPanning(); - mapView->map.moveBy(dx, dy); + view->map->startPanning(); + view->map->moveBy(dx, dy); } - } else if (mapView->rotating) { - mapView->map.startRotating(); - mapView->map.rotateBy(mapView->last_x, mapView->last_y, x, y); + } else if (view->rotating) { + view->map->startRotating(); + view->map->rotateBy(view->last_x, view->last_y, x, y); } - mapView->last_x = x; - mapView->last_y = y; + view->last_x = x; + view->last_y = y; } -int MapView::run() { - map.start(); +int GLFWView::run() { + map->start(); while (!glfwWindowShouldClose(window)) { - if (map.needsSwap()) { + if (map->needsSwap()) { glfwSwapBuffers(window); - map.swapped(); + map->swapped(); fps(); } glfwWaitEvents(); } - map.stop(); + map->stop(); return 0; } -void MapView::make_active() { +void GLFWView::make_active() { glfwMakeContextCurrent(window); } -void MapView::swap() { +void GLFWView::swap() { glfwPostEmptyEvent(); } -void MapView::fps() { +void GLFWView::fps() { static int frames = 0; static double time_elapsed = 0; diff --git a/common/glfw_view.hpp b/common/glfw_view.hpp index 7c9f41811..d8ea7a90a 100644 --- a/common/glfw_view.hpp +++ b/common/glfw_view.hpp @@ -5,13 +5,12 @@ #include #include -class MapView : public llmr::View { +class GLFWView : public llmr::View { public: - MapView(llmr::Settings &settings, bool fullscreen = false); - ~MapView(); - - void init(); + GLFWView(bool fullscreen = false); + ~GLFWView(); + void initialize(llmr::Map *map); void swap(); void make_active(); @@ -37,8 +36,6 @@ public: double last_click = -1; GLFWwindow *window = nullptr; - llmr::Settings &settings; - llmr::Map map; uv_loop_t *loop = nullptr; }; diff --git a/linux/settings.cpp b/common/settings_json.cpp similarity index 70% rename from linux/settings.cpp rename to common/settings_json.cpp index 73df446f6..771d89e1a 100644 --- a/linux/settings.cpp +++ b/common/settings_json.cpp @@ -1,17 +1,14 @@ -#include "settings.hpp" -#include "rapidjson/prettywriter.h" -#include "rapidjson/filestream.h" -#include "rapidjson/document.h" +#include "settings_json.hpp" +#include +#include +#include #include using namespace llmr; -Settings_JSON::Settings_JSON() -{ -} +Settings_JSON::Settings_JSON() { load(); } -void Settings_JSON::load() -{ +void Settings_JSON::load() { FILE *settingsFile = fopen("/tmp/llmr-native.json", "r"); if (settingsFile != NULL) { rapidjson::FileStream is(settingsFile); @@ -20,36 +17,30 @@ void Settings_JSON::load() if (document.IsArray()) { longitude = document[rapidjson::SizeType(0)].GetDouble(); latitude = document[1].GetDouble(); - scale = document[2].GetDouble(); + zoom = document[2].GetDouble(); angle = document[3].GetDouble(); debug = document[4].GetBool(); } } } -void Settings_JSON::persist() -{ -} - -void Settings_JSON::sync() -{ +void Settings_JSON::save() { rapidjson::FileStream s(fopen("/tmp/llmr-native.json", "w")); rapidjson::PrettyWriter writer(s); writer.StartArray(); writer.Double(longitude); writer.Double(latitude); - writer.Double(scale); + writer.Double(zoom); writer.Double(angle); writer.Bool(debug); writer.EndArray(); } -void Settings_JSON::clear() -{ +void Settings_JSON::clear() { longitude = 0; latitude = 0; - scale = 0; + zoom = 0; angle = 0; debug = false; } diff --git a/common/settings_json.hpp b/common/settings_json.hpp new file mode 100644 index 000000000..fa9525465 --- /dev/null +++ b/common/settings_json.hpp @@ -0,0 +1,24 @@ +#ifndef LLMR_JSON_SETTINGS +#define LLMR_JSON_SETTINGS + +namespace llmr { + +class Settings_JSON { +public: + Settings_JSON(); + void load(); + void save(); + void clear(); + +public: + double longitude = 0; + double latitude = 0; + double zoom = 0; + double angle = 0; + + bool debug = false; +}; + +} + +#endif diff --git a/common/settings_nsuserdefaults.hpp b/common/settings_nsuserdefaults.hpp new file mode 100644 index 000000000..575cc4a75 --- /dev/null +++ b/common/settings_nsuserdefaults.hpp @@ -0,0 +1,24 @@ +#ifndef LLMR_COMMON_SETTINGS_NSUSERDEFAULTS +#define LLMR_COMMON_SETTINGS_NSUSERDEFAULTS + +namespace llmr { + +class Settings_NSUserDefaults { +public: + Settings_NSUserDefaults(); + void load(); + void save(); + void clear(); + +public: + double longitude = 0; + double latitude = 0; + double zoom = 0; + double angle = 0; + + bool debug = false; +}; + +} + +#endif diff --git a/macosx/settings.mm b/common/settings_nsuserdefaults.mm similarity index 81% rename from macosx/settings.mm rename to common/settings_nsuserdefaults.mm index 59091241e..783aa500d 100644 --- a/macosx/settings.mm +++ b/common/settings_nsuserdefaults.mm @@ -1,44 +1,41 @@ #import -#include "settings.hpp" +#include "settings_nsuserdefaults.hpp" using namespace llmr; -Settings_MacOSX::Settings_MacOSX() +Settings_NSUserDefaults::Settings_NSUserDefaults() { [[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"longitude" : @(longitude), @"latitude" : @(latitude), - @"scale" : @(scale), + @"zoom" : @(zoom), @"angle" : @(angle), @"debug" : @(debug) }]; + load(); } -void Settings_MacOSX::load() +void Settings_NSUserDefaults::load() { NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; longitude = [settings[@"longitude"] doubleValue]; latitude = [settings[@"latitude"] doubleValue]; - scale = [settings[@"scale"] doubleValue]; + zoom = [settings[@"zoom"] doubleValue]; angle = [settings[@"angle"] doubleValue]; debug = [settings[@"debug"] boolValue]; } -void Settings_MacOSX::persist() +void Settings_NSUserDefaults::save() { [[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:@{ @"longitude" : @(longitude), @"latitude" : @(latitude), - @"scale" : @(scale), + @"zoom" : @(zoom), @"angle" : @(angle), @"debug" : @(debug) }]; -} - -void Settings_MacOSX::sync() -{ [[NSUserDefaults standardUserDefaults] synchronize]; } -void Settings_MacOSX::clear() +void Settings_NSUserDefaults::clear() { [NSUserDefaults resetStandardUserDefaults]; } \ No newline at end of file diff --git a/include/llmr/llmr.hpp b/include/llmr/llmr.hpp index 1bf737483..349b15d8c 100644 --- a/include/llmr/llmr.hpp +++ b/include/llmr/llmr.hpp @@ -3,6 +3,5 @@ #include "map/map.hpp" #include "map/view.hpp" -#include "map/settings.hpp" #endif diff --git a/include/llmr/map/settings.hpp b/include/llmr/map/settings.hpp deleted file mode 100644 index 01b0e7a02..000000000 --- a/include/llmr/map/settings.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef LLMR_MAP_SETTINGS -#define LLMR_MAP_SETTINGS - -#include - -namespace llmr { - -class Settings : private util::noncopyable { -public: - virtual void load() {}; // load into memory - virtual void persist() {}; // persist in memory (then call sync() if platform-appropriate) - virtual void sync() {}; // save to disk - virtual void clear() {}; // clear from memory and disk - virtual ~Settings() {} - -public: - double longitude = 0; - double latitude = 0; - double scale = 0; - double angle = 0; - - bool debug = false; -}; - -} - -#endif diff --git a/include/llmr/map/view.hpp b/include/llmr/map/view.hpp index 8f198e3c4..47ea24459 100644 --- a/include/llmr/map/view.hpp +++ b/include/llmr/map/view.hpp @@ -3,8 +3,14 @@ namespace llmr { +class Map; + class View { public: + virtual void initialize(Map *map) { + this->map = map; + } + // Called from the render (=GL) thread. Signals that the context should // swap the front and the back buffer. virtual void swap() = 0; @@ -13,6 +19,9 @@ public: // thread. This is typically just called once at the beginning of the // renderer setup since the render thread doesn't switch the contexts. virtual void make_active() = 0; + +protected: + llmr::Map *map = nullptr; }; } diff --git a/ios/MBXSettings.h b/ios/MBXSettings.h deleted file mode 100644 index a0b928ac7..000000000 --- a/ios/MBXSettings.h +++ /dev/null @@ -1,22 +0,0 @@ -// -// MBXSettings.hpp -// llmr -// -// Created by Justin R. Miller on 1/27/14. -// -// - -#import - -namespace llmr -{ - class Settings_iOS : public Settings - { - public: - Settings_iOS(); - virtual void load(); - virtual void persist(); - virtual void sync(); - virtual void clear(); - }; -} \ No newline at end of file diff --git a/ios/MBXSettings.mm b/ios/MBXSettings.mm deleted file mode 100644 index 57f1d0ac1..000000000 --- a/ios/MBXSettings.mm +++ /dev/null @@ -1,52 +0,0 @@ -// -// MBXSettings.cpp -// llmr -// -// Created by Justin R. Miller on 1/27/14. -// -// - -#import "MBXSettings.h" - -#import - -using namespace llmr; - -Settings_iOS::Settings_iOS() -{ - [[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"longitude" : @(longitude), - @"latitude" : @(latitude), - @"scale" : @(scale), - @"angle" : @(angle), - @"debug" : @(debug) }]; -} - -void Settings_iOS::load() -{ - NSDictionary *settings = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]; - - longitude = [settings[@"longitude"] doubleValue]; - latitude = [settings[@"latitude"] doubleValue]; - scale = [settings[@"scale"] doubleValue]; - angle = [settings[@"angle"] doubleValue]; - debug = [settings[@"debug"] boolValue]; -} - -void Settings_iOS::persist() -{ - [[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:@{ @"longitude" : @(longitude), - @"latitude" : @(latitude), - @"scale" : @(scale), - @"angle" : @(angle), - @"debug" : @(debug) }]; -} - -void Settings_iOS::sync() -{ - [[NSUserDefaults standardUserDefaults] synchronize]; -} - -void Settings_iOS::clear() -{ - [NSUserDefaults resetStandardUserDefaults]; -} \ No newline at end of file diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index ca3b65621..375c546c1 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -1,7 +1,6 @@ #import "MBXViewController.h" -#import "MBXSettings.h" - +#import "../common/settings_nsuserdefaults.hpp" #import "../common/foundation_request.h" #import @@ -30,7 +29,7 @@ class View; llmr::Map *map = nullptr; View *viewObj = nullptr; -llmr::Settings_iOS *settings = nullptr; +llmr::Settings_NSUserDefaults *settings = nullptr; MBXViewController *viewController = nullptr; #pragma mark - Setup @@ -68,12 +67,14 @@ MBXViewController *viewController = nullptr; - (void)setupMap { - settings = new llmr::Settings_iOS(); - settings->load(); - viewObj = new View(self); - map = new llmr::Map(*viewObj); + + // Load settings + settings = new llmr::Settings_NSUserDefaults(); + map->setLonLatZoom(settings->longitude, settings->latitude, settings->zoom); + map->setAngle(settings->angle); + map->setDebug(settings->debug); } @@ -219,6 +220,20 @@ MBXViewController *viewController = nullptr; - (void)dealloc { + if (settings) + { + // Save settings + if (map) + { + map->getLonLatZoom(settings->longitude, settings->latitude, settings->zoom); + settings->angle = map->getAngle(); + settings->debug = map->getDebug(); + settings->save(); + } + delete settings; + settings = nullptr; + } + if (map) { delete map; @@ -231,13 +246,6 @@ MBXViewController *viewController = nullptr; viewObj = nullptr; } - if (settings) - { - settings->sync(); - delete settings; - settings = nullptr; - } - if ([[EAGLContext currentContext] isEqual:self.context]) { [EAGLContext setCurrentContext:nil]; diff --git a/ios/llmr-app.gyp b/ios/llmr-app.gyp index 288e2bcac..6ae1240e2 100644 --- a/ios/llmr-app.gyp +++ b/ios/llmr-app.gyp @@ -11,8 +11,10 @@ "sources": [ "./main.m", "./MBXAppDelegate.m", - "./MBXSettings.mm", "./MBXViewController.mm", + "../common/settings_nsuserdefaults.hpp", + "../common/settings_nsuserdefaults.mm", + "../common/foundation_request.h", "../common/foundation_request.mm", ], 'product_extension': 'app', diff --git a/linux/llmr-app.gyp b/linux/llmr-app.gyp index 8cf352554..d979f00b4 100644 --- a/linux/llmr-app.gyp +++ b/linux/llmr-app.gyp @@ -10,8 +10,8 @@ 'type': 'executable', 'sources': [ './main.cpp', - './settings.cpp', - './settings.hpp', + '../common/settings_json.cpp', + '../common/settings_json.hpp', '../common/glfw_view.hpp', '../common/glfw_view.cpp', '../common/curl_request.cpp', diff --git a/linux/main.cpp b/linux/main.cpp index 32ae04cca..2c7440c87 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -4,15 +4,15 @@ #include #include -#include "settings.hpp" +#include "../common/settings_json.hpp" #include "../common/glfw_view.hpp" -MapView *mapView = nullptr; +GLFWView *view = nullptr; void quit_handler(int) { - if (mapView) { + if (view) { fprintf(stderr, "waiting for quit...\n"); - glfwSetWindowShouldClose(mapView->window, true); + glfwSetWindowShouldClose(view->window, true); glfwPostEmptyEvent(); } else { exit(0); @@ -40,13 +40,22 @@ int main(int argc, char *argv[]) { sigIntHandler.sa_flags = 0; sigaction(SIGINT, &sigIntHandler, NULL); - // main loop + view = new GLFWView(); + llmr::Map map(*view); + + // Load settings llmr::Settings_JSON settings; - mapView = new MapView(settings, fullscreen_flag); - mapView->init(); - int ret = mapView->run(); - mapView->settings.sync(); - delete mapView; + map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom); + map.setAngle(settings.angle); + map.setDebug(settings.debug); + + int ret = view->run(); + + // Save settings + map.getLonLatZoom(settings.longitude, settings.latitude, settings.zoom); + settings.angle = map.getAngle(); + settings.debug = map.getDebug(); + settings.save(); return ret; } diff --git a/linux/settings.hpp b/linux/settings.hpp deleted file mode 100644 index a171e8ab8..000000000 --- a/linux/settings.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef LLMR_JSON_SETTINGS -#define LLMR_JSON_SETTINGS - -#include - -namespace llmr { - -class Settings_JSON : public Settings { -public: - Settings_JSON(); - virtual void load(); - virtual void persist(); - virtual void sync(); - virtual void clear(); -}; - -} - -#endif diff --git a/macosx/llmr-app.gyp b/macosx/llmr-app.gyp index b6a42c91c..560b22300 100644 --- a/macosx/llmr-app.gyp +++ b/macosx/llmr-app.gyp @@ -10,8 +10,8 @@ 'type': 'executable', 'sources': [ './main.mm', - './settings.mm', - './settings.hpp', + '../common/settings_nsuserdefaults.hpp', + '../common/settings_nsuserdefaults.mm', '../common/glfw_view.hpp', '../common/glfw_view.cpp', '../common/foundation_request.h', diff --git a/macosx/main.mm b/macosx/main.mm index 8d2b0d8ce..4347a7ce8 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -1,14 +1,25 @@ -// #import -// #import - -#include "settings.hpp" +#include "../common/settings_nsuserdefaults.hpp" #include "../common/glfw_view.hpp" int main() { - llmr::Settings_MacOSX settings; - MapView view(settings); - view.init(); + GLFWView view; + llmr::Map map(view); + + // Load settings + llmr::Settings_NSUserDefaults settings; + map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom); + map.setAngle(settings.angle); + map.setDebug(settings.debug); + + // fprintf(stderr, "lon: %f, lat: %f, zoom: %f, angle: %f, debug: %d\n", settings.l) + int ret = view.run(); - settings.sync(); + + // Save settings + map.getLonLatZoom(settings.longitude, settings.latitude, settings.zoom); + settings.angle = map.getAngle(); + settings.debug = map.getDebug(); + settings.save(); + return ret; } diff --git a/macosx/settings.hpp b/macosx/settings.hpp deleted file mode 100644 index e507c6bd1..000000000 --- a/macosx/settings.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef LLMR_MACOSX_SETTINGS -#define LLMR_MACOSX_SETTINGS - -#include - -namespace llmr { - -class Settings_MacOSX : public Settings { -public: - Settings_MacOSX(); - virtual void load(); - virtual void persist(); - virtual void sync(); - virtual void clear(); -}; - -} - -#endif diff --git a/src/map/map.cpp b/src/map/map.cpp index 238d6b72f..04d811812 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -20,6 +20,8 @@ Map::Map(View& view) painter(*this), loop(uv_loop_new()) { + view.initialize(this); + // Make sure that we're doing an initial drawing in all cases. is_clean.clear(); is_rendered.clear();