#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define _USE_MATH_DEFINES #include #include // Check libuv library version. const static bool uv_version_check = []() { const unsigned int version = uv_version(); const unsigned int major = (version >> 16) & 0xFF; const unsigned int minor = (version >> 8) & 0xFF; const unsigned int patch = version & 0xFF; #ifndef UV_VERSION_PATCH // 0.10 doesn't have UV_VERSION_PATCH defined, so we "fake" it by using the library patch level. const unsigned int UV_VERSION_PATCH = version & 0xFF; #endif if (major != UV_VERSION_MAJOR || minor != UV_VERSION_MINOR || patch != UV_VERSION_PATCH) { throw std::runtime_error(mbgl::util::sprintf<96>( "libuv version mismatch: headers report %d.%d.%d, but library reports %d.%d.%d", UV_VERSION_MAJOR, UV_VERSION_MINOR, UV_VERSION_PATCH, major, minor, patch)); } return true; }(); #include // Check zlib library version. const static bool zlib_version_check = []() { const char *const version = zlibVersion(); if (version[0] != ZLIB_VERSION[0]) { throw std::runtime_error(mbgl::util::sprintf<96>( "zlib version mismatch: headers report %s, but library reports %s", ZLIB_VERSION, version)); } return true; }(); #include // Check sqlite3 library version. const static bool sqlite_version_check = []() { if (sqlite3_libversion_number() != SQLITE_VERSION_NUMBER) { throw std::runtime_error(mbgl::util::sprintf<96>( "sqlite3 libversion mismatch: headers report %d, but library reports %d", SQLITE_VERSION_NUMBER, sqlite3_libversion_number())); } if (strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID) != 0) { throw std::runtime_error(mbgl::util::sprintf<256>( "sqlite3 sourceid mismatch: headers report \"%s\", but library reports \"%s\"", SQLITE_SOURCE_ID, sqlite3_sourceid())); } return true; }(); using namespace mbgl; Map::Map(View& view_) : loop(std::make_unique()), thread(std::make_unique()), async_terminate(new uv_async_t()), async_render(new uv_async_t()), async_cleanup(new uv_async_t()), view(view_), #ifndef NDEBUG main_thread(uv_thread_self()), #endif transform(view_), glyphAtlas(1024, 1024), spriteAtlas(512, 512), texturepool(std::make_shared()), painter(spriteAtlas, glyphAtlas) { view.initialize(this); // Make sure that we're doing an initial drawing in all cases. is_clean.clear(); is_rendered.clear(); is_swapped.test_and_set(); } Map::~Map() { if (async) { stop(); } // Explicitly reset all pointers. texturepool.reset(); sprite.reset(); glyphStore.reset(); style.reset(); fileSource.reset(); workers.reset(); uv_run(**loop, UV_RUN_DEFAULT); } uv::worker &Map::getWorker() { if (!workers) { workers = std::make_unique(**loop, 4, "Tile Worker"); } return *workers; } void Map::start() { assert(uv_thread_self() == main_thread); assert(!async); // When starting map rendering in another thread, we perform async/continuously // updated rendering. Only in these cases, we attach the async handlers. async = true; // Reset the flag. is_stopped = false; // Setup async notifications // Iron out the differences between libuv 0.10 and 0.11 #ifdef UV_ASYNC_CALLBACK #error Cannot overwrite UV_ASYNC_CALLBACK #endif #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 #define UV_ASYNC_CALLBACK(name) [](uv_async_t *a, int) { return Map::name(a); } #else #define UV_ASYNC_CALLBACK(name) name #endif uv_async_init(**loop, async_terminate.get(), UV_ASYNC_CALLBACK(terminate)); async_terminate->data = this; uv_async_init(**loop, async_render.get(), UV_ASYNC_CALLBACK(render)); async_render->data = this; uv_async_init(**loop, async_cleanup.get(), UV_ASYNC_CALLBACK(cleanup)); async_cleanup->data = this; #undef UV_ASYNC_CALLBACK uv_thread_create(*thread, [](void *arg) { Map *map = static_cast(arg); #ifndef NDEBUG map->map_thread = uv_thread_self(); #endif #ifdef __APPLE__ pthread_setname_np("Map"); #endif map->run(); #ifndef NDEBUG map->map_thread = -1; #endif // Make sure that the stop() function knows when to stop invoking the callback function. map->is_stopped = true; map->view.notify(); }, this); } void Map::stop(stop_callback cb, void *data) { assert(uv_thread_self() == main_thread); assert(main_thread != map_thread); assert(async); uv_async_send(async_terminate.get()); if (cb) { // Wait until the render thread stopped. We are using this construct instead of plainly // relying on the thread_join because the system might need to run things in the current // thread that is required for the render thread to terminate correctly. This is for example // the case with Cocoa's NSURLRequest. Otherwise, we will eventually deadlock because this // thread (== main thread) is blocked. The callback function should use an efficient waiting // function to avoid a busy waiting loop. while (!is_stopped) { cb(data); } } // If a callback function was provided, this should return immediately because the thread has // already finished executing. uv_thread_join(*thread); async = false; } void Map::run() { #ifndef NDEBUG if (!async) { map_thread = main_thread; } #endif assert(uv_thread_self() == map_thread); setup(); prepare(); uv_run(**loop, UV_RUN_DEFAULT); // Run the event loop once more to make sure our async delete handlers are called. uv_run(**loop, UV_RUN_ONCE); // If the map rendering wasn't started asynchronously, we perform one render // *after* all events have been processed. if (!async) { render(); #ifndef NDEBUG map_thread = -1; #endif } } 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.get()); } } void Map::update() { is_clean.clear(); rerender(); } bool Map::needsSwap() { return is_swapped.test_and_set() == false; } void Map::swapped() { is_rendered.clear(); rerender(); } void Map::cleanup() { if (async_cleanup != nullptr) { uv_async_send(async_cleanup.get()); } } void Map::cleanup(uv_async_t *async) { Map *map = static_cast(async->data); map->painter.cleanup(); } void Map::terminate() { painter.terminate(); } void Map::setReachability(bool reachable) { // Note: This function may be called from *any* thread. if (reachable) { if (fileSource) { fileSource->prepare([&]() { fileSource->retryAllPending(); }); } } } void Map::render(uv_async_t *async) { Map *map = static_cast(async->data); assert(uv_thread_self() == map->map_thread); if (map->state.hasSize()) { if (map->is_rendered.test_and_set() == false) { map->prepare(); if (map->is_clean.test_and_set() == false) { map->render(); 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->is_rendered.clear(); } } } } void Map::terminate(uv_async_t *async) { // Closes all open handles on the loop. This means that the loop will automatically terminate. Map *map = static_cast(async->data); assert(uv_thread_self() == map->map_thread); // Remove all of these to make sure they are destructed in the correct thread. map->glyphStore.reset(); map->fileSource.reset(); map->style.reset(); map->workers.reset(); map->activeSources.clear(); uv_close((uv_handle_t *)map->async_cleanup.get(), nullptr); uv_close((uv_handle_t *)map->async_render.get(), nullptr); uv_close((uv_handle_t *)map->async_terminate.get(), nullptr); } #pragma mark - Setup void Map::setup() { assert(uv_thread_self() == map_thread); view.make_active(); painter.setup(); view.make_inactive(); } void Map::setStyleURL(const std::string &url) { // TODO: Make threadsafe. styleURL = url; } void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) { // TODO: Make threadsafe. styleJSON.swap(newStyleJSON); sprite.reset(); if (!style) { style = std::make_shared