#include #include #include #include #include #include #include #include #include #include namespace mbgl { class Worker::Impl { public: Impl(uv_loop_t*, FileSource* fs) { // FIXME: Workers should not access the FileSource but it // is currently needed because of GlyphsPBF. See #1664. util::ThreadContext::setFileSource(fs); } void parseRasterTile(RasterBucket* bucket, std::string data, std::function callback) { callback(bucket->setImage(data)); } void parseVectorTile(TileWorker* worker, std::string data, std::function callback) { try { pbf tilePBF(reinterpret_cast(data.data()), data.size()); callback(worker->parse(VectorTile(tilePBF))); } catch (const std::exception& ex) { callback(TileParseResult(ex.what())); } } void parseLiveTile(TileWorker* worker, const LiveTile* tile, std::function callback) { try { callback(worker->parse(*tile)); } catch (const std::exception& ex) { callback(TileParseResult(ex.what())); } } void redoPlacement(TileWorker* worker, float angle, bool collisionDebug, std::function callback) { worker->redoPlacement(angle, collisionDebug); callback(); } }; Worker::Worker(std::size_t count) { util::ThreadContext context = {"Worker", util::ThreadType::Worker, util::ThreadPriority::Low}; for (std::size_t i = 0; i < count; i++) { threads.emplace_back(std::make_unique>(context, util::ThreadContext::getFileSource())); } } Worker::~Worker() = default; std::unique_ptr Worker::parseRasterTile(RasterBucket& bucket, std::string data, std::function callback) { current = (current + 1) % threads.size(); return threads[current]->invokeWithCallback(&Worker::Impl::parseRasterTile, callback, &bucket, data); } std::unique_ptr Worker::parseVectorTile(TileWorker& worker, std::string data, std::function callback) { current = (current + 1) % threads.size(); return threads[current]->invokeWithCallback(&Worker::Impl::parseVectorTile, callback, &worker, data); } std::unique_ptr Worker::parseLiveTile(TileWorker& worker, const LiveTile& tile, std::function callback) { current = (current + 1) % threads.size(); return threads[current]->invokeWithCallback(&Worker::Impl::parseLiveTile, callback, &worker, &tile); } std::unique_ptr Worker::redoPlacement(TileWorker& worker, float angle, bool collisionDebug, std::function callback) { current = (current + 1) % threads.size(); return threads[current]->invokeWithCallback(&Worker::Impl::redoPlacement, callback, &worker, angle, collisionDebug); } } // end namespace mbgl