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.
This commit is contained in:
Konstantin Käfer
2015-04-16 10:34:30 +02:00
parent cd496645d4
commit 46bf226910
11 changed files with 301 additions and 120 deletions

View File

@@ -1,4 +1,5 @@
#include <mbgl/map/map.hpp>
#include <mbgl/map/still_image.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/io.hpp>
@@ -19,12 +20,13 @@
namespace po = boost::program_options;
#include <uv.h>
#include <cassert>
#include <cstdlib>
#include <iostream>
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<std::string> 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<const StillImage> image(reinterpret_cast<const StillImage *>(as->data));
uv_close(reinterpret_cast<uv_handle_t *>(as), [](uv_handle_t *handle) {
delete reinterpret_cast<uv_async_t *>(handle);
});
// Get the data from the GPU.
auto pixels = view.readPixels();
const std::string png = util::compress_png(image->width, image->height, image->pixels.get());
util::write_file(output, png);
});
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.renderStill([async](std::unique_ptr<const StillImage> image) {
async->data = const_cast<StillImage *>(image.release());
uv_async_send(async);
});
// This loop will terminate once the async was fired.
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
map.stop();
}