From 46bf2269107b21d20fc84ef46a9ce5c2bfeea519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 16 Apr 2015 10:34:30 +0200 Subject: align static render mode and still image render mode - static rendering now also runs in a separate thread; you have to start it with map.start(Map::Mode::Static) and join the thread with map.stop() before destructing the Map object - map.renderStill() takes a callback with will be invoked on the *map* thread, so you'll have to figure out your own method of dispatching back to the main thread. --- bin/render.cpp | 34 ++++++++++++++++++++++++---------- bin/render.gyp | 3 +++ 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'bin') diff --git a/bin/render.cpp b/bin/render.cpp index 440256b38c..78cb5b59f5 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -19,12 +20,13 @@ namespace po = boost::program_options; +#include + #include #include #include int main(int argc, char *argv[]) { - std::string style_path; double lat = 0, lon = 0; double zoom = 0; @@ -33,7 +35,7 @@ int main(int argc, char *argv[]) { int width = 512; int height = 512; double pixelRatio = 1.0; - std::string output = "out.png"; + static std::string output = "out.png"; std::string cache_file = "cache.sqlite"; std::vector classes; std::string token; @@ -81,6 +83,8 @@ int main(int argc, char *argv[]) { HeadlessView view; Map map(view, fileSource); + map.start(Map::Mode::Static); + // Set access token if present if (token.size()) { map.setAccessToken(std::string(token)); @@ -93,14 +97,24 @@ int main(int argc, char *argv[]) { map.setLatLngZoom({ lat, lon }, zoom); map.setBearing(bearing); - // Run the loop. It will terminate when we don't have any further listeners. - map.run(); + uv_async_t *async = new uv_async_t; + uv_async_init(uv_default_loop(), async, [](uv_async_t *as, int) { + std::unique_ptr image(reinterpret_cast(as->data)); + uv_close(reinterpret_cast(as), [](uv_handle_t *handle) { + delete reinterpret_cast(handle); + }); + + const std::string png = util::compress_png(image->width, image->height, image->pixels.get()); + util::write_file(output, png); + }); + + map.renderStill([async](std::unique_ptr image) { + async->data = const_cast(image.release()); + uv_async_send(async); + }); - // Get the data from the GPU. - auto pixels = view.readPixels(); + // This loop will terminate once the async was fired. + uv_run(uv_default_loop(), UV_RUN_DEFAULT); - const unsigned int w = width * pixelRatio; - const unsigned int h = height * pixelRatio; - const std::string image = util::compress_png(w, h, pixels.get()); - util::write_file(output, image); + map.stop(); } diff --git a/bin/render.gyp b/bin/render.gyp index 316a7f3ed0..b205e7a959 100644 --- a/bin/render.gyp +++ b/bin/render.gyp @@ -28,15 +28,18 @@ 'variables' : { 'cflags_cc': [ '<@(glfw3_cflags)', + '<@(uv_cflags)', '<@(boost_cflags)', ], 'ldflags': [ '<@(glfw3_ldflags)', + '<@(uv_ldflags)', '<@(boost_ldflags)', '-lboost_program_options' ], 'libraries': [ '<@(glfw3_static_libs)', + '<@(uv_static_libs)', ], }, -- cgit v1.2.1