Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[render] Add a RunLoop to render main thread
Browse files Browse the repository at this point in the history
Also simplified the code making the logic more linear.
  • Loading branch information
tmpsantos committed Dec 1, 2015
1 parent 3d07aaf commit e7b6d72
Showing 1 changed file with 10 additions and 26 deletions.
36 changes: 10 additions & 26 deletions bin/render.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include <mbgl/map/map.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>

#include <mbgl/platform/default/headless_view.hpp>
#include <mbgl/platform/default/headless_display.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/sqlite_cache.hpp>

Expand All @@ -17,18 +17,9 @@

namespace po = boost::program_options;

#include <uv.h>

#include <cassert>
#include <cstdlib>
#include <iostream>

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
#define UV_ASYNC_PARAMS(handle) uv_async_t *handle, int
#else
#define UV_ASYNC_PARAMS(handle) uv_async_t *handle
#endif

int main(int argc, char *argv[]) {
std::string style_path;
double lat = 0, lon = 0;
Expand Down Expand Up @@ -73,8 +64,9 @@ int main(int argc, char *argv[]) {

using namespace mbgl;

mbgl::SQLiteCache cache(cache_file);
mbgl::DefaultFileSource fileSource(&cache);
util::RunLoop loop;
SQLiteCache cache(cache_file);
DefaultFileSource fileSource(&cache);

// Try to load the token from the environment.
if (!token.size()) {
Expand Down Expand Up @@ -102,16 +94,7 @@ int main(int argc, char *argv[]) {
map.setDebug(debug ? mbgl::MapDebugOptions::TileBorders | mbgl::MapDebugOptions::ParseStatus : mbgl::MapDebugOptions::NoDebug);
}

uv_async_t *async = new uv_async_t;
uv_async_init(uv_default_loop(), async, [](UV_ASYNC_PARAMS(as)) {
std::unique_ptr<PremultipliedImage> image(reinterpret_cast<PremultipliedImage*>(as->data));
uv_close(reinterpret_cast<uv_handle_t *>(as), [](uv_handle_t *handle) {
delete reinterpret_cast<uv_async_t *>(handle);
});
util::write_file(output, encodePNG(*image));
});

map.renderStill([async](std::exception_ptr error, PremultipliedImage&& image) {
map.renderStill([&](std::exception_ptr error, PremultipliedImage&& image) {
try {
if (error) {
std::rethrow_exception(error);
Expand All @@ -121,10 +104,11 @@ int main(int argc, char *argv[]) {
exit(1);
}

async->data = new PremultipliedImage(std::move(image));
uv_async_send(async);
util::write_file(output, encodePNG(image));
loop.stop();
});

// This loop will terminate once the async was fired.
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
loop.run();

return 0;
}

0 comments on commit e7b6d72

Please sign in to comment.